mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-07 14:31:35 +08:00
Compare commits
3 Commits
codex/exec
...
fix/api-er
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2ae58a348 | ||
|
|
ae3bbc430b | ||
|
|
169d5f9e7e |
16
CHANGELOG.md
16
CHANGELOG.md
@@ -7,7 +7,6 @@ Docs: https://docs.openclaw.ai
|
||||
### Breaking
|
||||
|
||||
- Nodes/exec: remove the duplicated `nodes.run` shell wrapper from the CLI and agent `nodes` tool so node shell execution always goes through `exec host=node`, keeping node-specific capabilities on `nodes invoke` and the dedicated media/location/notify actions.
|
||||
- Background tasks: replace the old JSON task ledger with the SQLite-backed task store, so undocumented tooling or scripts that read `tasks/runs.json` directly must switch to the supported `openclaw tasks` surfaces instead of depending on state-dir internals. Thanks @vincentkoc and @mbelinky.
|
||||
|
||||
### Changes
|
||||
|
||||
@@ -19,9 +18,6 @@ Docs: https://docs.openclaw.ai
|
||||
- ACP/plugins: add an explicit default-off ACPX plugin-tools MCP bridge config, document the trust boundary, and harden the built-in bridge packaging/logging path so global installs and stdio MCP sessions work reliably. (#56867) Thanks @joe2643.
|
||||
- Agents/LLM: add a configurable idle-stream timeout for embedded runner requests so stalled model streams abort cleanly instead of hanging until the broader run timeout fires. (#55072) Thanks @liuy.
|
||||
- OpenAI/Responses: forward configured `text.verbosity` across Responses HTTP and WebSocket transports, surface it in `/status`, and keep per-agent verbosity precedence aligned with runtime behavior. (#47106) Thanks @merc1305 and @vincentkoc.
|
||||
- Android/notifications: add notification-forwarding controls with package filtering, quiet hours, rate limiting, and safer picker behavior for forwarded notification events. (#40175) Thanks @nimbleenigma.
|
||||
- Matrix/network: add explicit `channels.matrix.proxy` config for routing Matrix traffic through an HTTP(S) proxy, including account-level overrides and matching probe/runtime behavior. (#56931) thanks @patrick-yingxi-pan.
|
||||
- Background tasks: turn tasks into a real shared background-run control plane instead of ACP-only bookkeeping by unifying ACP, subagent, cron, and background CLI execution under one SQLite-backed ledger, routing detached lifecycle updates through the executor seam, adding audit/maintenance/status visibility, tightening auto-cleanup and lost-run recovery, improving task awareness in internal status/tool surfaces, and clarifying the split between heartbeat/main-session automation and detached scheduled runs. Thanks @vincentkoc and @mbelinky.
|
||||
|
||||
### Fixes
|
||||
|
||||
@@ -36,7 +32,6 @@ Docs: https://docs.openclaw.ai
|
||||
- Memory/QMD: keep `qmd embed` active in `search` mode too, so BM25-first setups still build a complete index for later vector and hybrid retrieval. (#54509) Thanks @hnshah and @vincentkoc.
|
||||
- Memory/QMD: point `QMD_CONFIG_DIR` at the nested `xdg-config/qmd` directory so per-agent collection config resolves correctly. (#39078) Thanks @smart-tinker and @vincentkoc.
|
||||
- Memory/QMD: include deduplicated default plus per-agent `memorySearch.extraPaths` when building QMD custom collections, so shared and agent-specific extra roots both get indexed consistently. (#57315) Thanks @Vitalcheffe and @vincentkoc.
|
||||
- Memory/session indexer: include `.jsonl.reset.*` and `.jsonl.deleted.*` transcripts in the memory host session scan while still excluding `.jsonl.bak.*` compaction backups and lock files, so memory search sees archived session history without duplicating stale snapshots. Thanks @hclsys and @vincentkoc.
|
||||
- Agents/sandbox: honor `tools.sandbox.tools.alsoAllow`, let explicit sandbox re-allows remove matching built-in default-deny tools, and keep sandbox explain/error guidance aligned with the effective sandbox tool policy. (#54492) Thanks @ngutman.
|
||||
- LINE/ACP: add current-conversation binding and inbound binding-routing parity so `/acp spawn ... --thread here`, configured ACP bindings, and active conversation-bound ACP sessions work on LINE like the other conversation channels.
|
||||
- LINE/markdown: preserve underscores inside Latin, Cyrillic, and CJK words when stripping markdown, while still removing standalone `_italic_` markers on the shared text-runtime path used by LINE and TTS. (#47465) Thanks @jackjin1997.
|
||||
@@ -54,11 +49,7 @@ Docs: https://docs.openclaw.ai
|
||||
- Agents/MCP: reuse bundled MCP runtimes across turns in the same session, while recreating them when MCP config changes and disposing stale runtimes cleanly on session rollover. (#55090) Thanks @allan0509.
|
||||
- Memory/QMD: honor `memory.qmd.update.embedInterval` even when regular QMD update cadence is disabled or slower by arming a dedicated embed-cadence maintenance timer, while avoiding redundant timers when regular updates are already frequent enough. (#37326) Thanks @barronlroth.
|
||||
- Memory/QMD: add `memory.qmd.searchTool` as an exact mcporter tool override, so custom QMD MCP tools such as `hybrid_search` can be used without weakening the validated `searchMode` config surface. (#27801) Thanks @keramblock.
|
||||
- Memory/QMD: keep reset and deleted session transcripts in QMD session export so daily session resets do not silently drop most historical recall from `memory_search`. (#30220) Thanks @pushkarsingh32.
|
||||
- Memory/QMD: rebind collections when QMD reports a changed pattern but omits path metadata, so config pattern changes stop being silently ignored on restart. (#49897) Thanks @Madruru.
|
||||
- Memory/QMD: warn explicitly when `memory.backend=qmd` is configured but the `qmd` binary is missing, so doctor and runtime fallback no longer fail as a silent builtin downgrade. (#50439) Thanks @Jimmy-xuzimo and @vincentkoc.
|
||||
- Memory/QMD: pass a direct-session key on `openclaw memory search` so CLI QMD searches no longer get denied as `session=<none>` under direct-only scope defaults. (#43517) Thanks @waynecc-at and @vincentkoc.
|
||||
- Memory/QMD: keep `memory_search` session-hit paths roundtrip-safe when exported session markdown lives under the workspace `qmd/` directory, so `memory_get` can read the exact returned path instead of failing on the generic `qmd/sessions/...` alias. (#43519) Thanks @holgergruenhagen and @vincentkoc.
|
||||
- Agents/memory flush: keep daily memory flush files append-only during embedded attempts so compaction writes do not overwrite earlier notes. (#53725) Thanks @HPluseven.
|
||||
- Web UI/markdown: stop bare auto-links from swallowing adjacent CJK text while preserving valid mixed-script path and query characters in rendered links. (#48410) Thanks @jnuyao.
|
||||
- BlueBubbles/iMessage: coalesce URL-only inbound messages with their link-preview balloon again so sharing a bare link no longer drops the URL from agent context. Thanks @vincentkoc.
|
||||
@@ -95,12 +86,7 @@ Docs: https://docs.openclaw.ai
|
||||
- Cron/announce: preserve all deliverable text payloads for announce mode instead of collapsing to the last chunk, so multi-line cron reports deliver in full to Telegram forum topics.
|
||||
- Harden async approval followup delivery in webchat-only sessions (#57359) Thanks @joshavant.
|
||||
- Status: fix cache hit rate exceeding 100% by deriving denominator from prompt-side token fields instead of potentially undersized totalTokens. Fixes #26643.
|
||||
- Config/update: stop `openclaw doctor` write-backs from persisting plugin-injected channel defaults, so `openclaw update` no longer seeds config keys that later break service refresh validation. (#56834) Thanks @openperf.
|
||||
- Agents/Anthropic failover: treat Anthropic `api_error` payloads with `An unexpected error occurred while processing the response` as transient so retry/fallback can engage instead of surfacing a terminal failure. (#57441) Thanks @zijiess and @vincentkoc.
|
||||
- Agents/compaction: keep late compaction-retry rejections handled after the aggregate timeout path wins without swallowing real pre-timeout wait failures, so timed-out retries no longer surface an unhandled rejection on later unsubscribe. (#57451) Thanks @mpz4life and @vincentkoc.
|
||||
- Matrix/delivery recovery: treat Synapse `User not in room` replay failures as permanent during startup recovery so poisoned queued messages move to `failed/` instead of crash-looping Matrix after restart. (#57426) thanks @dlardo.
|
||||
- Plugins/facades: guard bundled plugin facade loads with a cache-first sentinel so circular re-entry stops crashing `xai`, `sglang`, and `vllm` during gateway plugin startup. (#57508) Thanks @openperf.
|
||||
- Agents/MCP: dispose bundled MCP runtimes after one-shot `openclaw agent --local` runs finish, while preserving bundled MCP state across in-run retries so local JSON runs exit cleanly without restarting stateful MCP tools mid-run.
|
||||
|
||||
## 2026.3.28
|
||||
|
||||
@@ -229,7 +215,6 @@ Docs: https://docs.openclaw.ai
|
||||
- Plugins/diffs: load bundled Pierre themes without JSON module imports so diff rendering keeps working on newer Node builds. (#45869) thanks @NickHood1984.
|
||||
- Plugins/uninstall: remove owned `channels.<id>` config when uninstalling channel plugins, and keep the uninstall preview aligned with explicit channel ownership so built-in channels and shared keys stay intact. (#35915) Thanks @wbxl2000.
|
||||
- Plugins/Matrix: prefer explicit DM signals when choosing outbound direct rooms and routing unmapped verification summaries, so strict 2-person fallback rooms do not outrank the real DM. (#56076) thanks @gumadeiras
|
||||
- Slack/interactive replies: resolve Slack Block Kit reply delivery from both authored `channelData.slack.blocks` payloads and directive-generated interactive replies, and keep Slack button styles mapped onto valid Block Kit button rendering so interactive replies stop dropping on Slack-specific delivery paths. Thanks @vincentkoc.
|
||||
- Plugins/Matrix: resolve env-backed `accessToken` and `password` SecretRefs against the active Matrix config env path during startup, and officially accept SecretRef `accessToken` config values. (#54980) thanks @kakahu2015.
|
||||
- Microsoft Teams/proactive DMs: prefer the freshest personal conversation reference for `user:<aadObjectId>` sends when multiple stored references exist, so replies stop targeting stale DM threads. (#54702) Thanks @gumclaw.
|
||||
- Gateway/plugins: reuse the session workspace when building HTTP `/tools/invoke` tool lists and harden tool construction to infer the session agent workspace by default, so workspace plugins do not re-register on repeated HTTP tool calls. (#56101) thanks @neeravmakwana
|
||||
@@ -303,7 +288,6 @@ Docs: https://docs.openclaw.ai
|
||||
- Security/path resolution: prefer non-user-writable absolute helper binaries for OpenClaw CLI, ffmpeg, and OpenSSL resolution so PATH hijacks cannot replace trusted helpers with attacker-controlled executables.
|
||||
- Security/gateway command scopes: require `operator.admin` before Telegram target writeback and Talk Voice `/voice set` config writes persist through gateway message flows.
|
||||
- Security/OpenShell mirror: exclude workspace `hooks/` from mirror sync so untrusted sandbox files cannot become trusted host hooks on gateway startup.
|
||||
- Exec approvals/channels: unify Discord and Telegram exec approval runtime handling, move approval buttons onto the shared interactive reply model, and fix Telegram approval buttons and typed `/approve` commands so configured approvers can resolve requests reliably again. (#57516) Thanks @scoootscooob.
|
||||
|
||||
## 2026.3.24-beta.2
|
||||
|
||||
|
||||
@@ -31,13 +31,6 @@
|
||||
android:name="android.hardware.telephony"
|
||||
android:required="false" />
|
||||
|
||||
<queries>
|
||||
<intent>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent>
|
||||
</queries>
|
||||
|
||||
<application
|
||||
android:name=".NodeApp"
|
||||
android:allowBackup="true"
|
||||
|
||||
@@ -56,17 +56,6 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
|
||||
|
||||
val gateways: StateFlow<List<GatewayEndpoint>> = runtimeState(initial = emptyList()) { it.gateways }
|
||||
val discoveryStatusText: StateFlow<String> = runtimeState(initial = "Searching…") { it.discoveryStatusText }
|
||||
val notificationForwardingEnabled: StateFlow<Boolean> = prefs.notificationForwardingEnabled
|
||||
val notificationForwardingMode: StateFlow<NotificationPackageFilterMode> =
|
||||
prefs.notificationForwardingMode
|
||||
val notificationForwardingPackages: StateFlow<Set<String>> = prefs.notificationForwardingPackages
|
||||
val notificationForwardingQuietHoursEnabled: StateFlow<Boolean> =
|
||||
prefs.notificationForwardingQuietHoursEnabled
|
||||
val notificationForwardingQuietStart: StateFlow<String> = prefs.notificationForwardingQuietStart
|
||||
val notificationForwardingQuietEnd: StateFlow<String> = prefs.notificationForwardingQuietEnd
|
||||
val notificationForwardingMaxEventsPerMinute: StateFlow<Int> =
|
||||
prefs.notificationForwardingMaxEventsPerMinute
|
||||
val notificationForwardingSessionKey: StateFlow<String?> = prefs.notificationForwardingSessionKey
|
||||
|
||||
val isConnected: StateFlow<Boolean> = runtimeState(initial = false) { it.isConnected }
|
||||
val isNodeConnected: StateFlow<Boolean> = runtimeState(initial = false) { it.nodeConnected }
|
||||
@@ -208,39 +197,6 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
|
||||
prefs.setCanvasDebugStatusEnabled(value)
|
||||
}
|
||||
|
||||
fun setNotificationForwardingEnabled(value: Boolean) {
|
||||
ensureRuntime().setNotificationForwardingEnabled(value)
|
||||
}
|
||||
|
||||
fun setNotificationForwardingMode(mode: NotificationPackageFilterMode) {
|
||||
ensureRuntime().setNotificationForwardingMode(mode)
|
||||
}
|
||||
|
||||
fun setNotificationForwardingPackagesCsv(csv: String) {
|
||||
val packages =
|
||||
csv
|
||||
.split(',')
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotEmpty() }
|
||||
ensureRuntime().setNotificationForwardingPackages(packages)
|
||||
}
|
||||
|
||||
fun setNotificationForwardingQuietHours(
|
||||
enabled: Boolean,
|
||||
start: String,
|
||||
end: String,
|
||||
): Boolean {
|
||||
return ensureRuntime().setNotificationForwardingQuietHours(enabled = enabled, start = start, end = end)
|
||||
}
|
||||
|
||||
fun setNotificationForwardingMaxEventsPerMinute(value: Int) {
|
||||
ensureRuntime().setNotificationForwardingMaxEventsPerMinute(value)
|
||||
}
|
||||
|
||||
fun setNotificationForwardingSessionKey(value: String?) {
|
||||
ensureRuntime().setNotificationForwardingSessionKey(value)
|
||||
}
|
||||
|
||||
fun setVoiceScreenActive(active: Boolean) {
|
||||
ensureRuntime().setVoiceScreenActive(active)
|
||||
}
|
||||
|
||||
@@ -139,7 +139,6 @@ class NodeRuntime(
|
||||
motionPedometerAvailable = { motionHandler.isPedometerAvailable() },
|
||||
sendSmsAvailable = { BuildConfig.OPENCLAW_ENABLE_SMS && sms.canSendSms() },
|
||||
readSmsAvailable = { BuildConfig.OPENCLAW_ENABLE_SMS && sms.canReadSms() },
|
||||
smsSearchPossible = { BuildConfig.OPENCLAW_ENABLE_SMS && sms.hasTelephonyFeature() },
|
||||
callLogAvailable = { BuildConfig.OPENCLAW_ENABLE_CALL_LOG },
|
||||
hasRecordAudioPermission = { hasRecordAudioPermission() },
|
||||
manualTls = { manualTls.value },
|
||||
@@ -165,8 +164,6 @@ class NodeRuntime(
|
||||
locationEnabled = { locationMode.value != LocationMode.Off },
|
||||
sendSmsAvailable = { BuildConfig.OPENCLAW_ENABLE_SMS && sms.canSendSms() },
|
||||
readSmsAvailable = { BuildConfig.OPENCLAW_ENABLE_SMS && sms.canReadSms() },
|
||||
smsFeatureEnabled = { BuildConfig.OPENCLAW_ENABLE_SMS },
|
||||
smsTelephonyAvailable = { sms.hasTelephonyFeature() },
|
||||
callLogAvailable = { BuildConfig.OPENCLAW_ENABLE_CALL_LOG },
|
||||
debugBuild = { BuildConfig.DEBUG },
|
||||
refreshNodeCanvasCapability = { nodeSession.refreshNodeCanvasCapability() },
|
||||
@@ -537,17 +534,6 @@ class NodeRuntime(
|
||||
fun setOnboardingCompleted(value: Boolean) = prefs.setOnboardingCompleted(value)
|
||||
val lastDiscoveredStableId: StateFlow<String> = prefs.lastDiscoveredStableId
|
||||
val canvasDebugStatusEnabled: StateFlow<Boolean> = prefs.canvasDebugStatusEnabled
|
||||
val notificationForwardingEnabled: StateFlow<Boolean> = prefs.notificationForwardingEnabled
|
||||
val notificationForwardingMode: StateFlow<NotificationPackageFilterMode> =
|
||||
prefs.notificationForwardingMode
|
||||
val notificationForwardingPackages: StateFlow<Set<String>> = prefs.notificationForwardingPackages
|
||||
val notificationForwardingQuietHoursEnabled: StateFlow<Boolean> =
|
||||
prefs.notificationForwardingQuietHoursEnabled
|
||||
val notificationForwardingQuietStart: StateFlow<String> = prefs.notificationForwardingQuietStart
|
||||
val notificationForwardingQuietEnd: StateFlow<String> = prefs.notificationForwardingQuietEnd
|
||||
val notificationForwardingMaxEventsPerMinute: StateFlow<Int> =
|
||||
prefs.notificationForwardingMaxEventsPerMinute
|
||||
val notificationForwardingSessionKey: StateFlow<String?> = prefs.notificationForwardingSessionKey
|
||||
|
||||
private var didAutoConnect = false
|
||||
|
||||
@@ -700,34 +686,6 @@ class NodeRuntime(
|
||||
prefs.setCanvasDebugStatusEnabled(value)
|
||||
}
|
||||
|
||||
fun setNotificationForwardingEnabled(value: Boolean) {
|
||||
prefs.setNotificationForwardingEnabled(value)
|
||||
}
|
||||
|
||||
fun setNotificationForwardingMode(mode: NotificationPackageFilterMode) {
|
||||
prefs.setNotificationForwardingMode(mode)
|
||||
}
|
||||
|
||||
fun setNotificationForwardingPackages(packages: List<String>) {
|
||||
prefs.setNotificationForwardingPackages(packages)
|
||||
}
|
||||
|
||||
fun setNotificationForwardingQuietHours(
|
||||
enabled: Boolean,
|
||||
start: String,
|
||||
end: String,
|
||||
): Boolean {
|
||||
return prefs.setNotificationForwardingQuietHours(enabled = enabled, start = start, end = end)
|
||||
}
|
||||
|
||||
fun setNotificationForwardingMaxEventsPerMinute(value: Int) {
|
||||
prefs.setNotificationForwardingMaxEventsPerMinute(value)
|
||||
}
|
||||
|
||||
fun setNotificationForwardingSessionKey(value: String?) {
|
||||
prefs.setNotificationForwardingSessionKey(value)
|
||||
}
|
||||
|
||||
fun setVoiceScreenActive(active: Boolean) {
|
||||
if (!active) {
|
||||
stopActiveVoiceSession()
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
package ai.openclaw.app
|
||||
|
||||
import java.time.Instant
|
||||
import java.time.ZoneId
|
||||
|
||||
enum class NotificationPackageFilterMode(val rawValue: String) {
|
||||
Allowlist("allowlist"),
|
||||
Blocklist("blocklist"),
|
||||
;
|
||||
|
||||
companion object {
|
||||
fun fromRawValue(raw: String?): NotificationPackageFilterMode {
|
||||
return entries.firstOrNull { it.rawValue == raw?.trim()?.lowercase() } ?: Blocklist
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal data class NotificationForwardingPolicy(
|
||||
val enabled: Boolean,
|
||||
val mode: NotificationPackageFilterMode,
|
||||
val packages: Set<String>,
|
||||
val quietHoursEnabled: Boolean,
|
||||
val quietStart: String,
|
||||
val quietEnd: String,
|
||||
val maxEventsPerMinute: Int,
|
||||
val sessionKey: String?,
|
||||
)
|
||||
|
||||
internal fun NotificationForwardingPolicy.allowsPackage(packageName: String): Boolean {
|
||||
val normalized = packageName.trim()
|
||||
if (normalized.isEmpty()) {
|
||||
return false
|
||||
}
|
||||
return when (mode) {
|
||||
NotificationPackageFilterMode.Allowlist -> packages.contains(normalized)
|
||||
NotificationPackageFilterMode.Blocklist -> !packages.contains(normalized)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun NotificationForwardingPolicy.isWithinQuietHours(
|
||||
nowEpochMs: Long,
|
||||
zoneId: ZoneId = ZoneId.systemDefault(),
|
||||
): Boolean {
|
||||
if (!quietHoursEnabled) {
|
||||
return false
|
||||
}
|
||||
val startMinutes = parseLocalHourMinute(quietStart) ?: return false
|
||||
val endMinutes = parseLocalHourMinute(quietEnd) ?: return false
|
||||
if (startMinutes == endMinutes) {
|
||||
return true
|
||||
}
|
||||
val now =
|
||||
Instant.ofEpochMilli(nowEpochMs)
|
||||
.atZone(zoneId)
|
||||
.toLocalTime()
|
||||
val nowMinutes = now.hour * 60 + now.minute
|
||||
return if (startMinutes < endMinutes) {
|
||||
nowMinutes in startMinutes until endMinutes
|
||||
} else {
|
||||
nowMinutes >= startMinutes || nowMinutes < endMinutes
|
||||
}
|
||||
}
|
||||
|
||||
private val localHourMinuteRegex = Regex("""^([01]\d|2[0-3]):([0-5]\d)$""")
|
||||
|
||||
internal fun normalizeLocalHourMinute(raw: String): String? {
|
||||
val trimmed = raw.trim()
|
||||
val match = localHourMinuteRegex.matchEntire(trimmed) ?: return null
|
||||
return "${match.groupValues[1]}:${match.groupValues[2]}"
|
||||
}
|
||||
|
||||
internal fun parseLocalHourMinute(raw: String): Int? {
|
||||
val normalized = normalizeLocalHourMinute(raw) ?: return null
|
||||
val parts = normalized.split(':')
|
||||
val hour = parts[0].toInt()
|
||||
val minute = parts[1].toInt()
|
||||
return hour * 60 + minute
|
||||
}
|
||||
|
||||
internal class NotificationBurstLimiter {
|
||||
private val lock = Any()
|
||||
private var windowStartMs: Long = -1L
|
||||
private var eventsInWindow: Int = 0
|
||||
|
||||
fun allow(nowEpochMs: Long, maxEventsPerMinute: Int): Boolean {
|
||||
if (maxEventsPerMinute <= 0) {
|
||||
return false
|
||||
}
|
||||
val currentWindow = nowEpochMs - (nowEpochMs % 60_000L)
|
||||
synchronized(lock) {
|
||||
if (currentWindow != windowStartMs) {
|
||||
windowStartMs = currentWindow
|
||||
eventsInWindow = 0
|
||||
}
|
||||
if (eventsInWindow >= maxEventsPerMinute) {
|
||||
return false
|
||||
}
|
||||
eventsInWindow += 1
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -185,16 +185,7 @@ class PermissionRequester(private val activity: ComponentActivity) {
|
||||
when (permission) {
|
||||
Manifest.permission.CAMERA -> "Camera"
|
||||
Manifest.permission.RECORD_AUDIO -> "Microphone"
|
||||
Manifest.permission.SEND_SMS -> "Send SMS"
|
||||
Manifest.permission.READ_SMS -> "Read SMS"
|
||||
Manifest.permission.READ_CONTACTS -> "Read Contacts"
|
||||
Manifest.permission.WRITE_CONTACTS -> "Write Contacts"
|
||||
Manifest.permission.READ_CALENDAR -> "Read Calendar"
|
||||
Manifest.permission.WRITE_CALENDAR -> "Write Calendar"
|
||||
Manifest.permission.READ_CALL_LOG -> "Read Call Log"
|
||||
Manifest.permission.ACTIVITY_RECOGNITION -> "Motion Activity"
|
||||
Manifest.permission.READ_MEDIA_IMAGES -> "Photos"
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE -> "Photos"
|
||||
Manifest.permission.SEND_SMS -> "SMS"
|
||||
else -> permission
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,17 +26,6 @@ class SecurePrefs(
|
||||
private const val voiceWakeModeKey = "voiceWake.mode"
|
||||
private const val plainPrefsName = "openclaw.node"
|
||||
private const val securePrefsName = "openclaw.node.secure"
|
||||
private const val notificationsForwardingEnabledKey = "notifications.forwarding.enabled"
|
||||
private const val defaultNotificationForwardingEnabled = false
|
||||
private const val notificationsForwardingModeKey = "notifications.forwarding.mode"
|
||||
private const val notificationsForwardingPackagesKey = "notifications.forwarding.packages"
|
||||
private const val notificationsForwardingQuietHoursEnabledKey =
|
||||
"notifications.forwarding.quietHoursEnabled"
|
||||
private const val notificationsForwardingQuietStartKey = "notifications.forwarding.quietStart"
|
||||
private const val notificationsForwardingQuietEndKey = "notifications.forwarding.quietEnd"
|
||||
private const val notificationsForwardingMaxEventsPerMinuteKey =
|
||||
"notifications.forwarding.maxEventsPerMinute"
|
||||
private const val notificationsForwardingSessionKeyKey = "notifications.forwarding.sessionKey"
|
||||
}
|
||||
|
||||
private val appContext = context.applicationContext
|
||||
@@ -107,55 +96,6 @@ class SecurePrefs(
|
||||
MutableStateFlow(plainPrefs.getBoolean("canvas.debugStatusEnabled", false))
|
||||
val canvasDebugStatusEnabled: StateFlow<Boolean> = _canvasDebugStatusEnabled
|
||||
|
||||
private val _notificationForwardingEnabled =
|
||||
MutableStateFlow(plainPrefs.getBoolean(notificationsForwardingEnabledKey, defaultNotificationForwardingEnabled))
|
||||
val notificationForwardingEnabled: StateFlow<Boolean> = _notificationForwardingEnabled
|
||||
|
||||
private val _notificationForwardingMode =
|
||||
MutableStateFlow(
|
||||
NotificationPackageFilterMode.fromRawValue(
|
||||
plainPrefs.getString(notificationsForwardingModeKey, null),
|
||||
),
|
||||
)
|
||||
val notificationForwardingMode: StateFlow<NotificationPackageFilterMode> = _notificationForwardingMode
|
||||
|
||||
private val _notificationForwardingPackages = MutableStateFlow(loadNotificationForwardingPackages())
|
||||
val notificationForwardingPackages: StateFlow<Set<String>> = _notificationForwardingPackages
|
||||
|
||||
private val storedQuietStart =
|
||||
normalizeLocalHourMinute(plainPrefs.getString(notificationsForwardingQuietStartKey, "22:00").orEmpty())
|
||||
?: "22:00"
|
||||
private val storedQuietEnd =
|
||||
normalizeLocalHourMinute(plainPrefs.getString(notificationsForwardingQuietEndKey, "07:00").orEmpty())
|
||||
?: "07:00"
|
||||
private val storedQuietHoursEnabled =
|
||||
plainPrefs.getBoolean(notificationsForwardingQuietHoursEnabledKey, false) &&
|
||||
normalizeLocalHourMinute(plainPrefs.getString(notificationsForwardingQuietStartKey, "22:00").orEmpty()) != null &&
|
||||
normalizeLocalHourMinute(plainPrefs.getString(notificationsForwardingQuietEndKey, "07:00").orEmpty()) != null
|
||||
|
||||
private val _notificationForwardingQuietHoursEnabled =
|
||||
MutableStateFlow(storedQuietHoursEnabled)
|
||||
val notificationForwardingQuietHoursEnabled: StateFlow<Boolean> = _notificationForwardingQuietHoursEnabled
|
||||
|
||||
private val _notificationForwardingQuietStart = MutableStateFlow(storedQuietStart)
|
||||
val notificationForwardingQuietStart: StateFlow<String> = _notificationForwardingQuietStart
|
||||
|
||||
private val _notificationForwardingQuietEnd = MutableStateFlow(storedQuietEnd)
|
||||
val notificationForwardingQuietEnd: StateFlow<String> = _notificationForwardingQuietEnd
|
||||
|
||||
private val _notificationForwardingMaxEventsPerMinute =
|
||||
MutableStateFlow(plainPrefs.getInt(notificationsForwardingMaxEventsPerMinuteKey, 20).coerceAtLeast(1))
|
||||
val notificationForwardingMaxEventsPerMinute: StateFlow<Int> = _notificationForwardingMaxEventsPerMinute
|
||||
|
||||
private val _notificationForwardingSessionKey =
|
||||
MutableStateFlow(
|
||||
plainPrefs
|
||||
.getString(notificationsForwardingSessionKeyKey, "")
|
||||
?.trim()
|
||||
?.takeIf { it.isNotEmpty() },
|
||||
)
|
||||
val notificationForwardingSessionKey: StateFlow<String?> = _notificationForwardingSessionKey
|
||||
|
||||
private val _wakeWords = MutableStateFlow(loadWakeWords())
|
||||
val wakeWords: StateFlow<List<String>> = _wakeWords
|
||||
|
||||
@@ -245,114 +185,6 @@ class SecurePrefs(
|
||||
_canvasDebugStatusEnabled.value = value
|
||||
}
|
||||
|
||||
internal fun getNotificationForwardingPolicy(appPackageName: String): NotificationForwardingPolicy {
|
||||
val modeRaw = plainPrefs.getString(notificationsForwardingModeKey, null)
|
||||
val mode = NotificationPackageFilterMode.fromRawValue(modeRaw)
|
||||
|
||||
val configuredPackages = loadNotificationForwardingPackages()
|
||||
val normalizedAppPackage = appPackageName.trim()
|
||||
val defaultBlockedPackages =
|
||||
if (normalizedAppPackage.isNotEmpty()) setOf(normalizedAppPackage) else emptySet()
|
||||
|
||||
val packages =
|
||||
when (mode) {
|
||||
NotificationPackageFilterMode.Allowlist -> configuredPackages
|
||||
NotificationPackageFilterMode.Blocklist -> configuredPackages + defaultBlockedPackages
|
||||
}
|
||||
|
||||
val maxEvents = plainPrefs.getInt(notificationsForwardingMaxEventsPerMinuteKey, 20)
|
||||
val quietStart =
|
||||
normalizeLocalHourMinute(plainPrefs.getString(notificationsForwardingQuietStartKey, "22:00").orEmpty())
|
||||
?: "22:00"
|
||||
val quietEnd =
|
||||
normalizeLocalHourMinute(plainPrefs.getString(notificationsForwardingQuietEndKey, "07:00").orEmpty())
|
||||
?: "07:00"
|
||||
val sessionKey =
|
||||
plainPrefs
|
||||
.getString(notificationsForwardingSessionKeyKey, "")
|
||||
?.trim()
|
||||
?.takeIf { it.isNotEmpty() }
|
||||
|
||||
val quietHoursEnabled =
|
||||
plainPrefs.getBoolean(notificationsForwardingQuietHoursEnabledKey, false) &&
|
||||
normalizeLocalHourMinute(plainPrefs.getString(notificationsForwardingQuietStartKey, "22:00").orEmpty()) != null &&
|
||||
normalizeLocalHourMinute(plainPrefs.getString(notificationsForwardingQuietEndKey, "07:00").orEmpty()) != null
|
||||
|
||||
return NotificationForwardingPolicy(
|
||||
enabled = plainPrefs.getBoolean(notificationsForwardingEnabledKey, defaultNotificationForwardingEnabled),
|
||||
mode = mode,
|
||||
packages = packages,
|
||||
quietHoursEnabled = quietHoursEnabled,
|
||||
quietStart = quietStart,
|
||||
quietEnd = quietEnd,
|
||||
maxEventsPerMinute = maxEvents.coerceAtLeast(1),
|
||||
sessionKey = sessionKey,
|
||||
)
|
||||
}
|
||||
|
||||
internal fun setNotificationForwardingEnabled(value: Boolean) {
|
||||
plainPrefs.edit { putBoolean(notificationsForwardingEnabledKey, value) }
|
||||
_notificationForwardingEnabled.value = value
|
||||
}
|
||||
|
||||
internal fun setNotificationForwardingMode(mode: NotificationPackageFilterMode) {
|
||||
plainPrefs.edit { putString(notificationsForwardingModeKey, mode.rawValue) }
|
||||
_notificationForwardingMode.value = mode
|
||||
}
|
||||
|
||||
internal fun setNotificationForwardingPackages(packages: List<String>) {
|
||||
val sanitized =
|
||||
packages
|
||||
.asSequence()
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotEmpty() }
|
||||
.toSet()
|
||||
.toList()
|
||||
.sorted()
|
||||
val encoded = JsonArray(sanitized.map { JsonPrimitive(it) }).toString()
|
||||
plainPrefs.edit { putString(notificationsForwardingPackagesKey, encoded) }
|
||||
_notificationForwardingPackages.value = sanitized.toSet()
|
||||
}
|
||||
|
||||
internal fun setNotificationForwardingQuietHours(
|
||||
enabled: Boolean,
|
||||
start: String,
|
||||
end: String,
|
||||
): Boolean {
|
||||
if (!enabled) {
|
||||
plainPrefs.edit { putBoolean(notificationsForwardingQuietHoursEnabledKey, false) }
|
||||
_notificationForwardingQuietHoursEnabled.value = false
|
||||
return true
|
||||
}
|
||||
val normalizedStart = normalizeLocalHourMinute(start) ?: return false
|
||||
val normalizedEnd = normalizeLocalHourMinute(end) ?: return false
|
||||
plainPrefs.edit {
|
||||
putBoolean(notificationsForwardingQuietHoursEnabledKey, enabled)
|
||||
putString(notificationsForwardingQuietStartKey, normalizedStart)
|
||||
putString(notificationsForwardingQuietEndKey, normalizedEnd)
|
||||
}
|
||||
_notificationForwardingQuietHoursEnabled.value = enabled
|
||||
_notificationForwardingQuietStart.value = normalizedStart
|
||||
_notificationForwardingQuietEnd.value = normalizedEnd
|
||||
return true
|
||||
}
|
||||
|
||||
internal fun setNotificationForwardingMaxEventsPerMinute(value: Int) {
|
||||
val normalized = value.coerceAtLeast(1)
|
||||
plainPrefs.edit {
|
||||
putInt(notificationsForwardingMaxEventsPerMinuteKey, normalized)
|
||||
}
|
||||
_notificationForwardingMaxEventsPerMinute.value = normalized
|
||||
}
|
||||
|
||||
internal fun setNotificationForwardingSessionKey(value: String?) {
|
||||
val normalized = value?.trim()?.takeIf { it.isNotEmpty() }
|
||||
plainPrefs.edit {
|
||||
putString(notificationsForwardingSessionKeyKey, normalized.orEmpty())
|
||||
}
|
||||
_notificationForwardingSessionKey.value = normalized
|
||||
}
|
||||
|
||||
fun loadGatewayToken(): String? {
|
||||
val manual =
|
||||
_gatewayToken.value.trim().ifEmpty {
|
||||
@@ -476,28 +308,6 @@ class SecurePrefs(
|
||||
_speakerEnabled.value = value
|
||||
}
|
||||
|
||||
private fun loadNotificationForwardingPackages(): Set<String> {
|
||||
val raw = plainPrefs.getString(notificationsForwardingPackagesKey, null)?.trim()
|
||||
if (raw.isNullOrEmpty()) {
|
||||
return emptySet()
|
||||
}
|
||||
return try {
|
||||
val element = json.parseToJsonElement(raw)
|
||||
val array = element as? JsonArray ?: return emptySet()
|
||||
array
|
||||
.mapNotNull { item ->
|
||||
when (item) {
|
||||
is JsonNull -> null
|
||||
is JsonPrimitive -> item.content.trim().takeIf { it.isNotEmpty() }
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
.toSet()
|
||||
} catch (_: Throwable) {
|
||||
emptySet()
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadVoiceWakeMode(): VoiceWakeMode {
|
||||
val raw = plainPrefs.getString(voiceWakeModeKey, null)
|
||||
val resolved = VoiceWakeMode.fromRawValue(raw)
|
||||
|
||||
@@ -181,10 +181,17 @@ class GatewaySession(
|
||||
|
||||
suspend fun sendNodeEvent(event: String, payloadJson: String?): Boolean {
|
||||
val conn = currentConnection ?: return false
|
||||
val parsedPayload = payloadJson?.let { parseJsonOrNull(it) }
|
||||
val params =
|
||||
buildJsonObject {
|
||||
put("event", JsonPrimitive(event))
|
||||
put("payloadJSON", JsonPrimitive(payloadJson ?: "{}"))
|
||||
if (parsedPayload != null) {
|
||||
put("payload", parsedPayload)
|
||||
} else if (payloadJson != null) {
|
||||
put("payloadJSON", JsonPrimitive(payloadJson))
|
||||
} else {
|
||||
put("payloadJSON", JsonNull)
|
||||
}
|
||||
}
|
||||
try {
|
||||
conn.request("node.event", params, timeoutMs = 8_000)
|
||||
|
||||
@@ -19,7 +19,6 @@ class ConnectionManager(
|
||||
private val motionPedometerAvailable: () -> Boolean,
|
||||
private val sendSmsAvailable: () -> Boolean,
|
||||
private val readSmsAvailable: () -> Boolean,
|
||||
private val smsSearchPossible: () -> Boolean,
|
||||
private val callLogAvailable: () -> Boolean,
|
||||
private val hasRecordAudioPermission: () -> Boolean,
|
||||
private val manualTls: () -> Boolean,
|
||||
@@ -83,7 +82,6 @@ class ConnectionManager(
|
||||
locationEnabled = locationMode() != LocationMode.Off,
|
||||
sendSmsAvailable = sendSmsAvailable(),
|
||||
readSmsAvailable = readSmsAvailable(),
|
||||
smsSearchPossible = smsSearchPossible(),
|
||||
callLogAvailable = callLogAvailable(),
|
||||
voiceWakeEnabled = voiceWakeMode() != VoiceWakeMode.Off && hasRecordAudioPermission(),
|
||||
motionActivityAvailable = motionActivityAvailable(),
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package ai.openclaw.app.node
|
||||
|
||||
import ai.openclaw.app.BuildConfig
|
||||
import android.Manifest
|
||||
import android.app.ActivityManager
|
||||
import android.content.Context
|
||||
@@ -16,9 +15,9 @@ import android.os.PowerManager
|
||||
import android.os.StatFs
|
||||
import android.os.SystemClock
|
||||
import androidx.core.content.ContextCompat
|
||||
import ai.openclaw.app.BuildConfig
|
||||
import ai.openclaw.app.gateway.GatewaySession
|
||||
import java.util.Locale
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import kotlinx.serialization.json.buildJsonArray
|
||||
import kotlinx.serialization.json.buildJsonObject
|
||||
@@ -29,25 +28,6 @@ class DeviceHandler(
|
||||
private val smsEnabled: Boolean = BuildConfig.OPENCLAW_ENABLE_SMS,
|
||||
private val callLogEnabled: Boolean = BuildConfig.OPENCLAW_ENABLE_CALL_LOG,
|
||||
) {
|
||||
companion object {
|
||||
internal fun hasAnySmsCapability(
|
||||
smsEnabled: Boolean,
|
||||
telephonyAvailable: Boolean,
|
||||
smsSendGranted: Boolean,
|
||||
smsReadGranted: Boolean,
|
||||
): Boolean {
|
||||
return smsEnabled && telephonyAvailable && (smsSendGranted || smsReadGranted)
|
||||
}
|
||||
|
||||
internal fun isSmsPromptable(
|
||||
smsEnabled: Boolean,
|
||||
telephonyAvailable: Boolean,
|
||||
smsSendGranted: Boolean,
|
||||
smsReadGranted: Boolean,
|
||||
): Boolean {
|
||||
return smsEnabled && telephonyAvailable && (!smsSendGranted || !smsReadGranted)
|
||||
}
|
||||
}
|
||||
private data class BatterySnapshot(
|
||||
val status: Int,
|
||||
val plugged: Int,
|
||||
@@ -151,8 +131,6 @@ class DeviceHandler(
|
||||
|
||||
private fun permissionsPayloadJson(): String {
|
||||
val canSendSms = appContext.packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)
|
||||
val smsSendGranted = hasPermission(Manifest.permission.SEND_SMS)
|
||||
val smsReadGranted = hasPermission(Manifest.permission.READ_SMS)
|
||||
val notificationAccess = DeviceNotificationListenerService.isAccessEnabled(appContext)
|
||||
val photosGranted =
|
||||
if (Build.VERSION.SDK_INT >= 33) {
|
||||
@@ -196,34 +174,10 @@ class DeviceHandler(
|
||||
)
|
||||
put(
|
||||
"sms",
|
||||
buildJsonObject {
|
||||
put(
|
||||
"status",
|
||||
JsonPrimitive(
|
||||
if (hasAnySmsCapability(smsEnabled, canSendSms, smsSendGranted, smsReadGranted)) "granted" else "denied",
|
||||
),
|
||||
)
|
||||
put("promptable", JsonPrimitive(isSmsPromptable(smsEnabled, canSendSms, smsSendGranted, smsReadGranted)))
|
||||
put(
|
||||
"capabilities",
|
||||
buildJsonObject {
|
||||
put(
|
||||
"send",
|
||||
permissionStateJson(
|
||||
granted = smsEnabled && smsSendGranted && canSendSms,
|
||||
promptableWhenDenied = smsEnabled && canSendSms,
|
||||
),
|
||||
)
|
||||
put(
|
||||
"read",
|
||||
permissionStateJson(
|
||||
granted = smsEnabled && smsReadGranted && canSendSms,
|
||||
promptableWhenDenied = smsEnabled && canSendSms,
|
||||
),
|
||||
)
|
||||
},
|
||||
)
|
||||
},
|
||||
permissionStateJson(
|
||||
granted = smsEnabled && hasPermission(Manifest.permission.SEND_SMS) && canSendSms,
|
||||
promptableWhenDenied = smsEnabled && canSendSms,
|
||||
),
|
||||
)
|
||||
put(
|
||||
"notificationListener",
|
||||
|
||||
@@ -8,10 +8,6 @@ import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.service.notification.NotificationListenerService
|
||||
import android.service.notification.StatusBarNotification
|
||||
import ai.openclaw.app.NotificationBurstLimiter
|
||||
import ai.openclaw.app.SecurePrefs
|
||||
import ai.openclaw.app.allowsPackage
|
||||
import ai.openclaw.app.isWithinQuietHours
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import kotlinx.serialization.json.buildJsonObject
|
||||
@@ -130,9 +126,6 @@ private object DeviceNotificationStore {
|
||||
}
|
||||
|
||||
class DeviceNotificationListenerService : NotificationListenerService() {
|
||||
private val securePrefs by lazy { SecurePrefs(applicationContext) }
|
||||
private val forwardingLimiter = NotificationBurstLimiter()
|
||||
|
||||
override fun onListenerConnected() {
|
||||
super.onListenerConnected()
|
||||
activeService = this
|
||||
@@ -159,12 +152,24 @@ class DeviceNotificationListenerService : NotificationListenerService() {
|
||||
super.onNotificationPosted(sbn)
|
||||
val entry = sbn?.toEntry() ?: return
|
||||
DeviceNotificationStore.upsert(entry)
|
||||
rememberRecentPackage(entry.packageName)
|
||||
if (entry.packageName == packageName) {
|
||||
return
|
||||
}
|
||||
val payload = notificationChangedPayload(entry) ?: return
|
||||
emitNotificationsChanged(payload)
|
||||
emitNotificationsChanged(
|
||||
buildJsonObject {
|
||||
put("change", JsonPrimitive("posted"))
|
||||
put("key", JsonPrimitive(entry.key))
|
||||
put("packageName", JsonPrimitive(entry.packageName))
|
||||
put("postTimeMs", JsonPrimitive(entry.postTimeMs))
|
||||
put("isOngoing", JsonPrimitive(entry.isOngoing))
|
||||
put("isClearable", JsonPrimitive(entry.isClearable))
|
||||
entry.title?.let { put("title", JsonPrimitive(it)) }
|
||||
entry.text?.let { put("text", JsonPrimitive(it)) }
|
||||
entry.subText?.let { put("subText", JsonPrimitive(it)) }
|
||||
entry.category?.let { put("category", JsonPrimitive(it)) }
|
||||
entry.channelId?.let { put("channelId", JsonPrimitive(it)) }
|
||||
}.toString(),
|
||||
)
|
||||
}
|
||||
|
||||
override fun onNotificationRemoved(sbn: StatusBarNotification?) {
|
||||
@@ -175,79 +180,21 @@ class DeviceNotificationListenerService : NotificationListenerService() {
|
||||
return
|
||||
}
|
||||
DeviceNotificationStore.remove(key)
|
||||
rememberRecentPackage(removed.packageName)
|
||||
if (removed.packageName == packageName) {
|
||||
return
|
||||
}
|
||||
val packageName = removed.packageName.trim()
|
||||
val payload =
|
||||
notificationChangedPayload(
|
||||
entry = null,
|
||||
change = "removed",
|
||||
key = key,
|
||||
packageName = packageName,
|
||||
postTimeMs = removed.postTime,
|
||||
isOngoing = removed.isOngoing,
|
||||
isClearable = removed.isClearable,
|
||||
) ?: return
|
||||
emitNotificationsChanged(payload)
|
||||
}
|
||||
|
||||
private fun notificationChangedPayload(entry: DeviceNotificationEntry): String? {
|
||||
return notificationChangedPayload(
|
||||
entry = entry,
|
||||
change = "posted",
|
||||
key = entry.key,
|
||||
packageName = entry.packageName,
|
||||
postTimeMs = entry.postTimeMs,
|
||||
isOngoing = entry.isOngoing,
|
||||
isClearable = entry.isClearable,
|
||||
emitNotificationsChanged(
|
||||
buildJsonObject {
|
||||
put("change", JsonPrimitive("removed"))
|
||||
put("key", JsonPrimitive(key))
|
||||
val packageName = removed.packageName.trim()
|
||||
if (packageName.isNotEmpty()) {
|
||||
put("packageName", JsonPrimitive(packageName))
|
||||
}
|
||||
}.toString(),
|
||||
)
|
||||
}
|
||||
|
||||
private fun notificationChangedPayload(
|
||||
entry: DeviceNotificationEntry?,
|
||||
change: String,
|
||||
key: String,
|
||||
packageName: String,
|
||||
postTimeMs: Long,
|
||||
isOngoing: Boolean,
|
||||
isClearable: Boolean,
|
||||
): String? {
|
||||
val normalizedPackage = packageName.trim()
|
||||
if (normalizedPackage.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
val policy = securePrefs.getNotificationForwardingPolicy(appPackageName = this.packageName)
|
||||
if (!policy.enabled) {
|
||||
return null
|
||||
}
|
||||
if (!policy.allowsPackage(normalizedPackage)) {
|
||||
return null
|
||||
}
|
||||
val nowEpochMs = System.currentTimeMillis()
|
||||
if (policy.isWithinQuietHours(nowEpochMs = nowEpochMs)) {
|
||||
return null
|
||||
}
|
||||
if (!forwardingLimiter.allow(nowEpochMs, policy.maxEventsPerMinute)) {
|
||||
return null
|
||||
}
|
||||
return buildJsonObject {
|
||||
put("change", JsonPrimitive(change))
|
||||
put("key", JsonPrimitive(key))
|
||||
put("packageName", JsonPrimitive(normalizedPackage))
|
||||
put("postTimeMs", JsonPrimitive(postTimeMs))
|
||||
put("isOngoing", JsonPrimitive(isOngoing))
|
||||
put("isClearable", JsonPrimitive(isClearable))
|
||||
policy.sessionKey?.let { put("sessionKey", JsonPrimitive(it)) }
|
||||
entry?.title?.let { put("title", JsonPrimitive(it)) }
|
||||
entry?.text?.let { put("text", JsonPrimitive(it)) }
|
||||
entry?.subText?.let { put("subText", JsonPrimitive(it)) }
|
||||
entry?.category?.let { put("category", JsonPrimitive(it)) }
|
||||
entry?.channelId?.let { put("channelId", JsonPrimitive(it)) }
|
||||
}.toString()
|
||||
}
|
||||
|
||||
private fun refreshActiveNotifications() {
|
||||
val entries =
|
||||
runCatching {
|
||||
@@ -281,9 +228,6 @@ class DeviceNotificationListenerService : NotificationListenerService() {
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val recentPackagesPref = "notifications.forwarding.recentPackages"
|
||||
private const val legacyRecentPackagesPref = "notifications.recentPackages"
|
||||
private const val recentPackagesLimit = 64
|
||||
@Volatile private var activeService: DeviceNotificationListenerService? = null
|
||||
@Volatile private var nodeEventSink: ((event: String, payloadJson: String?) -> Unit)? = null
|
||||
|
||||
@@ -295,31 +239,6 @@ class DeviceNotificationListenerService : NotificationListenerService() {
|
||||
nodeEventSink = sink
|
||||
}
|
||||
|
||||
private fun recentPackagesPrefs(context: Context) =
|
||||
context.applicationContext.getSharedPreferences("openclaw.secure", Context.MODE_PRIVATE)
|
||||
|
||||
private fun migrateLegacyRecentPackagesIfNeeded(context: Context) {
|
||||
val prefs = recentPackagesPrefs(context)
|
||||
val hasNew = prefs.contains(recentPackagesPref)
|
||||
val legacy = prefs.getString(legacyRecentPackagesPref, null)?.trim().orEmpty()
|
||||
if (!hasNew && legacy.isNotEmpty()) {
|
||||
prefs.edit().putString(recentPackagesPref, legacy).remove(legacyRecentPackagesPref).apply()
|
||||
} else if (hasNew && prefs.contains(legacyRecentPackagesPref)) {
|
||||
prefs.edit().remove(legacyRecentPackagesPref).apply()
|
||||
}
|
||||
}
|
||||
|
||||
fun recentPackages(context: Context): List<String> {
|
||||
migrateLegacyRecentPackagesIfNeeded(context)
|
||||
val prefs = recentPackagesPrefs(context)
|
||||
val stored = prefs.getString(recentPackagesPref, null).orEmpty()
|
||||
return stored
|
||||
.split(',')
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotEmpty() }
|
||||
.distinct()
|
||||
}
|
||||
|
||||
fun isAccessEnabled(context: Context): Boolean {
|
||||
val manager = context.getSystemService(NotificationManager::class.java) ?: return false
|
||||
return manager.isNotificationListenerAccessGranted(serviceComponent(context))
|
||||
@@ -357,21 +276,6 @@ class DeviceNotificationListenerService : NotificationListenerService() {
|
||||
nodeEventSink?.invoke(NOTIFICATIONS_CHANGED_EVENT, payloadJson)
|
||||
}
|
||||
}
|
||||
|
||||
private fun rememberRecentPackage(packageName: String?) {
|
||||
val service = activeService ?: return
|
||||
val normalized = packageName?.trim().orEmpty()
|
||||
if (normalized.isEmpty() || normalized == service.packageName) return
|
||||
migrateLegacyRecentPackagesIfNeeded(service.applicationContext)
|
||||
val prefs = recentPackagesPrefs(service.applicationContext)
|
||||
val existing = prefs.getString(recentPackagesPref, null).orEmpty()
|
||||
.split(',')
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotEmpty() && it != normalized }
|
||||
.take(recentPackagesLimit - 1)
|
||||
val updated = listOf(normalized) + existing
|
||||
prefs.edit().putString(recentPackagesPref, updated.joinToString(",")).apply()
|
||||
}
|
||||
}
|
||||
|
||||
private fun executeActionInternal(request: NotificationActionRequest): NotificationActionResult {
|
||||
|
||||
@@ -20,7 +20,6 @@ data class NodeRuntimeFlags(
|
||||
val locationEnabled: Boolean,
|
||||
val sendSmsAvailable: Boolean,
|
||||
val readSmsAvailable: Boolean,
|
||||
val smsSearchPossible: Boolean,
|
||||
val callLogAvailable: Boolean,
|
||||
val voiceWakeEnabled: Boolean,
|
||||
val motionActivityAvailable: Boolean,
|
||||
@@ -34,7 +33,6 @@ enum class InvokeCommandAvailability {
|
||||
LocationEnabled,
|
||||
SendSmsAvailable,
|
||||
ReadSmsAvailable,
|
||||
RequestableSmsSearchAvailable,
|
||||
CallLogAvailable,
|
||||
MotionActivityAvailable,
|
||||
MotionPedometerAvailable,
|
||||
@@ -201,7 +199,7 @@ object InvokeCommandRegistry {
|
||||
),
|
||||
InvokeCommandSpec(
|
||||
name = OpenClawSmsCommand.Search.rawValue,
|
||||
availability = InvokeCommandAvailability.RequestableSmsSearchAvailable,
|
||||
availability = InvokeCommandAvailability.ReadSmsAvailable,
|
||||
),
|
||||
InvokeCommandSpec(
|
||||
name = OpenClawCallLogCommand.Search.rawValue,
|
||||
@@ -246,7 +244,6 @@ object InvokeCommandRegistry {
|
||||
InvokeCommandAvailability.LocationEnabled -> flags.locationEnabled
|
||||
InvokeCommandAvailability.SendSmsAvailable -> flags.sendSmsAvailable
|
||||
InvokeCommandAvailability.ReadSmsAvailable -> flags.readSmsAvailable
|
||||
InvokeCommandAvailability.RequestableSmsSearchAvailable -> flags.smsSearchPossible
|
||||
InvokeCommandAvailability.CallLogAvailable -> flags.callLogAvailable
|
||||
InvokeCommandAvailability.MotionActivityAvailable -> flags.motionActivityAvailable
|
||||
InvokeCommandAvailability.MotionPedometerAvailable -> flags.motionPedometerAvailable
|
||||
|
||||
@@ -14,44 +14,6 @@ import ai.openclaw.app.protocol.OpenClawNotificationsCommand
|
||||
import ai.openclaw.app.protocol.OpenClawSmsCommand
|
||||
import ai.openclaw.app.protocol.OpenClawSystemCommand
|
||||
|
||||
internal enum class SmsSearchAvailabilityReason {
|
||||
Available,
|
||||
PermissionRequired,
|
||||
Unavailable,
|
||||
}
|
||||
|
||||
internal fun classifySmsSearchAvailability(
|
||||
readSmsAvailable: Boolean,
|
||||
smsFeatureEnabled: Boolean,
|
||||
smsTelephonyAvailable: Boolean,
|
||||
): SmsSearchAvailabilityReason {
|
||||
if (readSmsAvailable) return SmsSearchAvailabilityReason.Available
|
||||
if (!smsFeatureEnabled || !smsTelephonyAvailable) return SmsSearchAvailabilityReason.Unavailable
|
||||
return SmsSearchAvailabilityReason.PermissionRequired
|
||||
}
|
||||
|
||||
internal fun smsSearchAvailabilityError(
|
||||
readSmsAvailable: Boolean,
|
||||
smsFeatureEnabled: Boolean,
|
||||
smsTelephonyAvailable: Boolean,
|
||||
): GatewaySession.InvokeResult? {
|
||||
return when (
|
||||
classifySmsSearchAvailability(
|
||||
readSmsAvailable = readSmsAvailable,
|
||||
smsFeatureEnabled = smsFeatureEnabled,
|
||||
smsTelephonyAvailable = smsTelephonyAvailable,
|
||||
)
|
||||
) {
|
||||
SmsSearchAvailabilityReason.Available,
|
||||
SmsSearchAvailabilityReason.PermissionRequired -> null
|
||||
SmsSearchAvailabilityReason.Unavailable ->
|
||||
GatewaySession.InvokeResult.error(
|
||||
code = "SMS_UNAVAILABLE",
|
||||
message = "SMS_UNAVAILABLE: SMS not available on this device",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
class InvokeDispatcher(
|
||||
private val canvas: CanvasController,
|
||||
private val cameraHandler: CameraHandler,
|
||||
@@ -72,8 +34,6 @@ class InvokeDispatcher(
|
||||
private val locationEnabled: () -> Boolean,
|
||||
private val sendSmsAvailable: () -> Boolean,
|
||||
private val readSmsAvailable: () -> Boolean,
|
||||
private val smsFeatureEnabled: () -> Boolean,
|
||||
private val smsTelephonyAvailable: () -> Boolean,
|
||||
private val callLogAvailable: () -> Boolean,
|
||||
private val debugBuild: () -> Boolean,
|
||||
private val refreshNodeCanvasCapability: suspend () -> Boolean,
|
||||
@@ -308,13 +268,15 @@ class InvokeDispatcher(
|
||||
message = "SMS_UNAVAILABLE: SMS not available on this device",
|
||||
)
|
||||
}
|
||||
InvokeCommandAvailability.ReadSmsAvailable,
|
||||
InvokeCommandAvailability.RequestableSmsSearchAvailable ->
|
||||
smsSearchAvailabilityError(
|
||||
readSmsAvailable = readSmsAvailable(),
|
||||
smsFeatureEnabled = smsFeatureEnabled(),
|
||||
smsTelephonyAvailable = smsTelephonyAvailable(),
|
||||
)
|
||||
InvokeCommandAvailability.ReadSmsAvailable ->
|
||||
if (readSmsAvailable()) {
|
||||
null
|
||||
} else {
|
||||
GatewaySession.InvokeResult.error(
|
||||
code = "SMS_UNAVAILABLE",
|
||||
message = "SMS_UNAVAILABLE: SMS not available on this device",
|
||||
)
|
||||
}
|
||||
InvokeCommandAvailability.CallLogAvailable ->
|
||||
if (callLogAvailable()) {
|
||||
null
|
||||
|
||||
@@ -9,28 +9,23 @@ class SmsHandler(
|
||||
val res = sms.send(paramsJson)
|
||||
if (res.ok) {
|
||||
return GatewaySession.InvokeResult.ok(res.payloadJson)
|
||||
} else {
|
||||
val error = res.error ?: "SMS_SEND_FAILED"
|
||||
val idx = error.indexOf(':')
|
||||
val code = if (idx > 0) error.substring(0, idx).trim() else "SMS_SEND_FAILED"
|
||||
return GatewaySession.InvokeResult.error(code = code, message = error)
|
||||
}
|
||||
return errorResult(res.error, defaultCode = "SMS_SEND_FAILED")
|
||||
}
|
||||
|
||||
suspend fun handleSmsSearch(paramsJson: String?): GatewaySession.InvokeResult {
|
||||
val res = sms.search(paramsJson)
|
||||
if (res.ok) {
|
||||
return GatewaySession.InvokeResult.ok(res.payloadJson)
|
||||
} else {
|
||||
val error = res.error ?: "SMS_SEARCH_FAILED"
|
||||
val idx = error.indexOf(':')
|
||||
val code = if (idx > 0) error.substring(0, idx).trim() else "SMS_SEARCH_FAILED"
|
||||
return GatewaySession.InvokeResult.error(code = code, message = error)
|
||||
}
|
||||
return errorResult(res.error, defaultCode = "SMS_SEARCH_FAILED")
|
||||
}
|
||||
|
||||
private fun errorResult(error: String?, defaultCode: String): GatewaySession.InvokeResult {
|
||||
val rawMessage = error ?: defaultCode
|
||||
val idx = rawMessage.indexOf(':')
|
||||
val code = if (idx > 0) rawMessage.substring(0, idx).trim() else defaultCode
|
||||
val message =
|
||||
if (idx > 0 && code == rawMessage.substring(0, idx).trim()) {
|
||||
rawMessage.substring(idx + 1).trim().ifEmpty { rawMessage }
|
||||
} else {
|
||||
rawMessage
|
||||
}
|
||||
return GatewaySession.InvokeResult.error(code = code, message = message)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,21 +3,21 @@ package ai.openclaw.app.node
|
||||
import android.Manifest
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.database.Cursor
|
||||
import android.net.Uri
|
||||
import android.provider.ContactsContract
|
||||
import android.provider.Telephony
|
||||
import android.telephony.SmsManager as AndroidSmsManager
|
||||
import androidx.core.content.ContextCompat
|
||||
import ai.openclaw.app.PermissionRequester
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import kotlinx.serialization.Serializable
|
||||
import ai.openclaw.app.PermissionRequester
|
||||
|
||||
/**
|
||||
* Sends SMS messages via the Android SMS API.
|
||||
@@ -39,7 +39,7 @@ class SmsManager(private val context: Context) {
|
||||
)
|
||||
|
||||
/**
|
||||
* Represents a single SMS message.
|
||||
* Represents a single SMS message
|
||||
*/
|
||||
@Serializable
|
||||
data class SmsMessage(
|
||||
@@ -53,7 +53,6 @@ class SmsManager(private val context: Context) {
|
||||
val type: Int,
|
||||
val body: String?,
|
||||
val status: Int,
|
||||
val transportType: String? = null,
|
||||
)
|
||||
|
||||
data class SearchResult(
|
||||
@@ -63,13 +62,6 @@ class SmsManager(private val context: Context) {
|
||||
val payloadJson: String,
|
||||
)
|
||||
|
||||
internal data class QueryMetadata(
|
||||
val mmsRequested: Boolean,
|
||||
val mmsEligible: Boolean,
|
||||
val mmsAttempted: Boolean,
|
||||
val mmsIncluded: Boolean,
|
||||
)
|
||||
|
||||
internal data class ParsedParams(
|
||||
val to: String,
|
||||
val message: String,
|
||||
@@ -92,8 +84,6 @@ class SmsManager(private val context: Context) {
|
||||
val keyword: String? = null,
|
||||
val type: Int? = null,
|
||||
val isRead: Boolean? = null,
|
||||
val includeMms: Boolean = false,
|
||||
val conversationReview: Boolean = false,
|
||||
val limit: Int = DEFAULT_SMS_LIMIT,
|
||||
val offset: Int = 0,
|
||||
)
|
||||
@@ -110,11 +100,6 @@ class SmsManager(private val context: Context) {
|
||||
|
||||
companion object {
|
||||
private const val DEFAULT_SMS_LIMIT = 25
|
||||
internal const val MAX_MIXED_BY_PHONE_CANDIDATE_WINDOW = 500
|
||||
private const val MMS_SMS_BY_PHONE_BASE = "content://mms-sms/messages/byphone"
|
||||
private const val MMS_CONTENT_BASE = "content://mms"
|
||||
private const val MMS_PART_URI = "content://mms/part"
|
||||
private val PHONE_FORMATTING_REGEX = Regex("""[\s\-()]""")
|
||||
internal val JsonConfig = Json { ignoreUnknownKeys = true }
|
||||
|
||||
internal fun parseParams(paramsJson: String?, json: Json = JsonConfig): ParseResult {
|
||||
@@ -172,333 +157,31 @@ class SmsManager(private val context: Context) {
|
||||
val keyword = (obj["keyword"] as? JsonPrimitive)?.content?.trim()
|
||||
val type = (obj["type"] as? JsonPrimitive)?.content?.toIntOrNull()
|
||||
val isRead = (obj["isRead"] as? JsonPrimitive)?.content?.toBooleanStrictOrNull()
|
||||
val includeMms = (obj["includeMms"] as? JsonPrimitive)?.content?.toBooleanStrictOrNull() ?: false
|
||||
val conversationReview = (obj["conversationReview"] as? JsonPrimitive)?.content?.toBooleanStrictOrNull() ?: false
|
||||
val limit = ((obj["limit"] as? JsonPrimitive)?.content?.toIntOrNull() ?: DEFAULT_SMS_LIMIT)
|
||||
.coerceIn(1, 200)
|
||||
val offset = ((obj["offset"] as? JsonPrimitive)?.content?.toIntOrNull() ?: 0)
|
||||
.coerceAtLeast(0)
|
||||
|
||||
// Validate time range
|
||||
if (startTime != null && endTime != null && startTime > endTime) {
|
||||
return QueryParseResult.Error("INVALID_REQUEST: startTime must be less than or equal to endTime")
|
||||
}
|
||||
|
||||
return QueryParseResult.Ok(
|
||||
QueryParams(
|
||||
startTime = startTime,
|
||||
endTime = endTime,
|
||||
contactName = contactName,
|
||||
phoneNumber = phoneNumber,
|
||||
keyword = keyword,
|
||||
type = type,
|
||||
isRead = isRead,
|
||||
includeMms = includeMms,
|
||||
conversationReview = conversationReview,
|
||||
limit = limit,
|
||||
offset = offset,
|
||||
)
|
||||
)
|
||||
return QueryParseResult.Ok(QueryParams(
|
||||
startTime = startTime,
|
||||
endTime = endTime,
|
||||
contactName = contactName,
|
||||
phoneNumber = phoneNumber,
|
||||
keyword = keyword,
|
||||
type = type,
|
||||
isRead = isRead,
|
||||
limit = limit,
|
||||
offset = offset,
|
||||
))
|
||||
}
|
||||
|
||||
private fun normalizePhoneNumber(phone: String): String {
|
||||
return phone.replace(PHONE_FORMATTING_REGEX, "")
|
||||
}
|
||||
|
||||
internal fun normalizePhoneNumberOrNull(phone: String?): String? {
|
||||
val normalized = phone?.let(::normalizePhoneNumber)?.trim().orEmpty()
|
||||
if (normalized.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
val digits = toByPhoneLookupNumber(normalized)
|
||||
return normalized.takeIf { digits.isNotEmpty() }
|
||||
}
|
||||
|
||||
internal fun sanitizeContactPhoneNumberOrNull(phone: String?): String? {
|
||||
val normalized = normalizePhoneNumberOrNull(phone) ?: return null
|
||||
return normalized.takeUnless(::hasSqlLikeWildcard)
|
||||
}
|
||||
|
||||
internal fun shouldPromptForContactNameSearchPermission(
|
||||
contactName: String?,
|
||||
phoneNumber: String?,
|
||||
hasReadContactsPermission: Boolean,
|
||||
): Boolean {
|
||||
return !contactName.isNullOrEmpty() && phoneNumber.isNullOrEmpty() && !hasReadContactsPermission
|
||||
}
|
||||
|
||||
internal fun mapMmsMsgBoxToSearchType(msgBox: Int?): Int? {
|
||||
return when (msgBox) {
|
||||
1 -> 1 // inbox
|
||||
2 -> 2 // sent
|
||||
3 -> 3 // draft
|
||||
4 -> 4 // outbox
|
||||
5 -> 5 // failed
|
||||
6 -> 6 // queued
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
internal fun escapeSqlLikeLiteral(value: String): String {
|
||||
return buildString(value.length) {
|
||||
for (ch in value) {
|
||||
when (ch) {
|
||||
'\\', '%', '_' -> {
|
||||
append('\\')
|
||||
append(ch)
|
||||
}
|
||||
else -> append(ch)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun buildContactNameLikeSelection(): String {
|
||||
return "${ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME} LIKE ? ESCAPE '\\'"
|
||||
}
|
||||
|
||||
internal fun buildContactNameLikeArg(contactName: String): String {
|
||||
return "%${escapeSqlLikeLiteral(contactName)}%"
|
||||
}
|
||||
|
||||
internal fun buildKeywordLikeSelection(): String {
|
||||
return "${Telephony.Sms.BODY} LIKE ? ESCAPE '\\'"
|
||||
}
|
||||
|
||||
internal fun buildKeywordLikeArg(keyword: String): String {
|
||||
return "%${escapeSqlLikeLiteral(keyword)}%"
|
||||
}
|
||||
|
||||
internal fun buildMixedByPhoneProjection(): Array<String> {
|
||||
return arrayOf(
|
||||
"_id",
|
||||
"thread_id",
|
||||
"transport_type",
|
||||
"address",
|
||||
"date",
|
||||
"date_sent",
|
||||
"read",
|
||||
"type",
|
||||
"body",
|
||||
"status",
|
||||
)
|
||||
}
|
||||
|
||||
internal fun hasSqlLikeWildcard(value: String): Boolean {
|
||||
return value.contains('%') || value.contains('_')
|
||||
}
|
||||
|
||||
internal fun isExplicitPhoneInputInvalid(rawPhone: String?, normalizedPhone: String?): Boolean {
|
||||
if (rawPhone.isNullOrBlank()) {
|
||||
return false
|
||||
}
|
||||
if (normalizedPhone == null) {
|
||||
return true
|
||||
}
|
||||
return hasSqlLikeWildcard(normalizedPhone)
|
||||
}
|
||||
|
||||
internal fun resolveMixedByPhoneRowStatus(transportType: String?, smsStatus: Int?): Int {
|
||||
return if (transportType.equals("mms", ignoreCase = true)) -1 else (smsStatus ?: 0)
|
||||
}
|
||||
|
||||
internal fun resolveMixedByPhoneRowAddress(
|
||||
providerAddress: String?,
|
||||
phoneNumber: String,
|
||||
mmsAddress: String? = null,
|
||||
): String? {
|
||||
val resolvedMmsAddress = normalizePhoneNumberOrNull(mmsAddress)
|
||||
if (resolvedMmsAddress != null) {
|
||||
return resolvedMmsAddress
|
||||
}
|
||||
|
||||
val resolvedProviderAddress = normalizePhoneNumberOrNull(providerAddress)
|
||||
return resolvedProviderAddress ?: phoneNumber
|
||||
}
|
||||
|
||||
internal fun selectPreferredMmsAddress(
|
||||
addressRows: List<Pair<String?, Int?>>,
|
||||
lookupNumber: String,
|
||||
): String? {
|
||||
val lookupDigits = toByPhoneLookupNumber(lookupNumber)
|
||||
val normalizedRows = addressRows.mapNotNull { (address, type) ->
|
||||
val normalized = normalizePhoneNumberOrNull(address) ?: return@mapNotNull null
|
||||
val digits = toByPhoneLookupNumber(normalized)
|
||||
if (digits.isBlank()) return@mapNotNull null
|
||||
Triple(normalized, digits, type)
|
||||
}
|
||||
|
||||
fun firstPreferred(vararg types: Int): String? {
|
||||
return normalizedRows.firstOrNull { row ->
|
||||
(types.isEmpty() || types.contains(row.third ?: -1)) && row.second != lookupDigits
|
||||
}?.first
|
||||
}
|
||||
|
||||
return firstPreferred(137)
|
||||
?: firstPreferred(151, 130, 129)
|
||||
?: firstPreferred()
|
||||
?: normalizedRows.firstOrNull()?.first
|
||||
}
|
||||
|
||||
internal fun shouldUseConversationReviewByPhoneMode(
|
||||
params: QueryParams,
|
||||
resolvedPhoneNumbers: List<String> = emptyList(),
|
||||
): Boolean {
|
||||
val hasExplicitPhoneNumber = !params.phoneNumber.isNullOrEmpty()
|
||||
val hasSingleResolvedPhoneNumber = resolvedPhoneNumbers.size == 1
|
||||
return params.conversationReview && params.includeMms && (hasExplicitPhoneNumber || hasSingleResolvedPhoneNumber)
|
||||
}
|
||||
|
||||
internal fun effectiveSearchParams(
|
||||
params: QueryParams,
|
||||
resolvedPhoneNumbers: List<String> = emptyList(),
|
||||
): QueryParams {
|
||||
if (!shouldUseConversationReviewByPhoneMode(params, resolvedPhoneNumbers)) return params
|
||||
val reviewLimit = maxOf(params.limit, 25)
|
||||
return params.copy(limit = reviewLimit)
|
||||
}
|
||||
|
||||
internal fun resolveSearchParams(
|
||||
params: QueryParams,
|
||||
normalizedPhoneNumber: String?,
|
||||
resolvedPhoneNumbers: List<String> = emptyList(),
|
||||
): QueryParams {
|
||||
val effectivePhoneNumber = normalizedPhoneNumber ?: resolvedPhoneNumbers.singleOrNull()
|
||||
val normalizedParams = params.copy(phoneNumber = effectivePhoneNumber)
|
||||
return effectiveSearchParams(normalizedParams, resolvedPhoneNumbers)
|
||||
}
|
||||
|
||||
internal fun toByPhoneLookupNumber(phone: String): String {
|
||||
return phone.filter { it.isDigit() }
|
||||
}
|
||||
|
||||
internal fun normalizeProviderDateMillis(rawDate: Long): Long {
|
||||
return if (rawDate in 1..99_999_999_999L) rawDate * 1000L else rawDate
|
||||
}
|
||||
|
||||
internal fun canonicalizeMixedPathPhoneFilters(phoneNumbers: List<String>): List<String> {
|
||||
return phoneNumbers
|
||||
.map(::toByPhoneLookupNumber)
|
||||
.filter { it.isNotBlank() }
|
||||
.distinct()
|
||||
}
|
||||
|
||||
internal fun requestedMixedByPhoneCandidateWindow(params: QueryParams): Long {
|
||||
return params.offset.toLong() + params.limit.toLong()
|
||||
}
|
||||
|
||||
internal fun exceedsMixedByPhoneCandidateWindow(
|
||||
params: QueryParams,
|
||||
allPhoneNumbers: List<String>,
|
||||
): Boolean {
|
||||
return params.includeMms &&
|
||||
allPhoneNumbers.size == 1 &&
|
||||
requestedMixedByPhoneCandidateWindow(params) > MAX_MIXED_BY_PHONE_CANDIDATE_WINDOW
|
||||
}
|
||||
|
||||
internal fun mixedByPhoneWindowError(): String {
|
||||
return "INVALID_REQUEST: includeMms offset+limit exceeds supported window ($MAX_MIXED_BY_PHONE_CANDIDATE_WINDOW)"
|
||||
}
|
||||
|
||||
internal fun isMmsTransportRow(message: SmsMessage): Boolean {
|
||||
return message.transportType.equals("mms", ignoreCase = true)
|
||||
}
|
||||
|
||||
internal fun shouldHydrateMmsByPhoneRow(transportType: String?, body: String?, type: Int): Boolean {
|
||||
return transportType.equals("mms", ignoreCase = true) && (body.isNullOrBlank() || type == 0)
|
||||
}
|
||||
|
||||
internal fun buildQueryMetadata(
|
||||
params: QueryParams,
|
||||
allPhoneNumbers: List<String>,
|
||||
messages: List<SmsMessage>,
|
||||
): QueryMetadata {
|
||||
val mmsRequested = params.includeMms
|
||||
val mmsEligible = mmsRequested && allPhoneNumbers.size == 1
|
||||
val mmsAttempted = mmsEligible
|
||||
val mmsIncluded = mmsAttempted && messages.any(::isMmsTransportRow)
|
||||
return QueryMetadata(
|
||||
mmsRequested = mmsRequested,
|
||||
mmsEligible = mmsEligible,
|
||||
mmsAttempted = mmsAttempted,
|
||||
mmsIncluded = mmsIncluded,
|
||||
)
|
||||
}
|
||||
|
||||
internal fun compareByPhoneCandidateOrder(left: SmsMessage, right: SmsMessage): Int {
|
||||
return when {
|
||||
left.date != right.date -> right.date.compareTo(left.date)
|
||||
left.id != right.id -> right.id.compareTo(left.id)
|
||||
else -> 0
|
||||
}
|
||||
}
|
||||
|
||||
internal fun buildMixedRowIdentity(rowId: Long, transportType: String?): String {
|
||||
return "${transportType?.ifBlank { "unknown" } ?: "unknown"}:$rowId"
|
||||
}
|
||||
|
||||
internal fun upsertTopDateCandidates(
|
||||
candidates: MutableList<Pair<String, SmsMessage>>,
|
||||
identityKey: String,
|
||||
message: SmsMessage,
|
||||
maxCandidates: Int,
|
||||
) {
|
||||
if (maxCandidates <= 0) {
|
||||
return
|
||||
}
|
||||
|
||||
candidates.removeAll { existing -> existing.first == identityKey }
|
||||
candidates.add(identityKey to message)
|
||||
candidates.sortWith { left, right -> compareByPhoneCandidateOrder(left.second, right.second) }
|
||||
|
||||
while (candidates.size > maxCandidates) {
|
||||
candidates.removeAt(candidates.lastIndex)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun materializeByPhoneCandidate(
|
||||
candidates: MutableMap<String, SmsMessage>,
|
||||
identityKey: String,
|
||||
message: SmsMessage,
|
||||
) {
|
||||
candidates[identityKey] = message
|
||||
}
|
||||
|
||||
internal fun collectMixedByPhoneCandidate(
|
||||
topCandidates: MutableList<Pair<String, SmsMessage>>,
|
||||
materializedCandidates: MutableMap<String, SmsMessage>,
|
||||
identityKey: String,
|
||||
message: SmsMessage,
|
||||
maxCandidates: Int,
|
||||
reviewMode: Boolean,
|
||||
) {
|
||||
if (reviewMode) {
|
||||
materializeByPhoneCandidate(materializedCandidates, identityKey, message)
|
||||
} else {
|
||||
upsertTopDateCandidates(topCandidates, identityKey, message, maxCandidates)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun pageMixedByPhoneCandidates(
|
||||
topCandidates: Collection<Pair<String, SmsMessage>>,
|
||||
materializedCandidates: Map<String, SmsMessage>,
|
||||
params: QueryParams,
|
||||
reviewMode: Boolean,
|
||||
): List<SmsMessage> {
|
||||
return if (reviewMode) {
|
||||
pageByPhoneCandidates(materializedCandidates.values, params)
|
||||
} else {
|
||||
pageByPhoneCandidates(topCandidates.map { it.second }, params)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun pageByPhoneCandidates(
|
||||
candidates: Collection<SmsMessage>,
|
||||
params: QueryParams,
|
||||
): List<SmsMessage> {
|
||||
return candidates
|
||||
.sortedWith(::compareByPhoneCandidateOrder)
|
||||
.drop(params.offset)
|
||||
.take(params.limit)
|
||||
return phone.replace(Regex("""[\s\-()]"""), "")
|
||||
}
|
||||
|
||||
internal fun buildSendPlan(
|
||||
@@ -531,21 +214,14 @@ class SmsManager(private val context: Context) {
|
||||
ok: Boolean,
|
||||
messages: List<SmsMessage>,
|
||||
error: String? = null,
|
||||
queryMetadata: QueryMetadata? = null,
|
||||
): String {
|
||||
val messagesArray = json.encodeToString(messages)
|
||||
val messagesElement = json.parseToJsonElement(messagesArray)
|
||||
val payload = mutableMapOf<String, JsonElement>(
|
||||
"ok" to JsonPrimitive(ok),
|
||||
"count" to JsonPrimitive(messages.size),
|
||||
"messages" to messagesElement,
|
||||
"messages" to messagesElement
|
||||
)
|
||||
queryMetadata?.let {
|
||||
payload["mmsRequested"] = JsonPrimitive(it.mmsRequested)
|
||||
payload["mmsEligible"] = JsonPrimitive(it.mmsEligible)
|
||||
payload["mmsAttempted"] = JsonPrimitive(it.mmsAttempted)
|
||||
payload["mmsIncluded"] = JsonPrimitive(it.mmsIncluded)
|
||||
}
|
||||
if (!ok && error != null) {
|
||||
payload["error"] = JsonPrimitive(error)
|
||||
}
|
||||
@@ -578,12 +254,8 @@ class SmsManager(private val context: Context) {
|
||||
return hasSmsPermission() && hasTelephonyFeature()
|
||||
}
|
||||
|
||||
fun canSearchSms(): Boolean {
|
||||
return hasReadSmsPermission() && hasTelephonyFeature()
|
||||
}
|
||||
|
||||
fun canReadSms(): Boolean {
|
||||
return canSearchSms()
|
||||
return hasReadSmsPermission() && hasTelephonyFeature()
|
||||
}
|
||||
|
||||
fun hasTelephonyFeature(): Boolean {
|
||||
@@ -630,19 +302,19 @@ class SmsManager(private val context: Context) {
|
||||
val plan = buildSendPlan(params.message) { smsManager.divideMessage(it) }
|
||||
if (plan.useMultipart) {
|
||||
smsManager.sendMultipartTextMessage(
|
||||
params.to,
|
||||
null,
|
||||
ArrayList(plan.parts),
|
||||
null,
|
||||
null,
|
||||
params.to, // destination
|
||||
null, // service center (null = default)
|
||||
ArrayList(plan.parts), // message parts
|
||||
null, // sent intents
|
||||
null, // delivery intents
|
||||
)
|
||||
} else {
|
||||
smsManager.sendTextMessage(
|
||||
params.to,
|
||||
null,
|
||||
params.message,
|
||||
null,
|
||||
null,
|
||||
params.to, // destination
|
||||
null, // service center (null = default)
|
||||
params.message,// message
|
||||
null, // sent intent
|
||||
null, // delivery intent
|
||||
)
|
||||
}
|
||||
|
||||
@@ -662,82 +334,6 @@ class SmsManager(private val context: Context) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search SMS messages with the specified parameters.
|
||||
*/
|
||||
suspend fun search(paramsJson: String?): SearchResult = withContext(Dispatchers.IO) {
|
||||
if (!hasTelephonyFeature()) {
|
||||
return@withContext queryError("SMS_UNAVAILABLE: telephony not available")
|
||||
}
|
||||
|
||||
if (!ensureReadSmsPermission()) {
|
||||
return@withContext queryError("SMS_PERMISSION_REQUIRED: grant READ_SMS permission")
|
||||
}
|
||||
|
||||
val parseResult = parseQueryParams(paramsJson, json)
|
||||
if (parseResult is QueryParseResult.Error) {
|
||||
return@withContext queryError(parseResult.error)
|
||||
}
|
||||
val parsedParams = (parseResult as QueryParseResult.Ok).params
|
||||
val normalizedPhoneNumber = normalizePhoneNumberOrNull(parsedParams.phoneNumber)
|
||||
if (isExplicitPhoneInputInvalid(parsedParams.phoneNumber, normalizedPhoneNumber)) {
|
||||
val error =
|
||||
if (!parsedParams.phoneNumber.isNullOrBlank() && normalizedPhoneNumber != null && hasSqlLikeWildcard(normalizedPhoneNumber)) {
|
||||
"INVALID_REQUEST: phoneNumber must not contain SQL LIKE wildcard characters"
|
||||
} else {
|
||||
"INVALID_REQUEST: phoneNumber must contain at least one digit"
|
||||
}
|
||||
return@withContext queryError(error)
|
||||
}
|
||||
val normalizedParams = resolveSearchParams(parsedParams, normalizedPhoneNumber)
|
||||
|
||||
return@withContext try {
|
||||
val contactsPermissionGranted = hasReadContactsPermission()
|
||||
val shouldPromptForContactsPermission =
|
||||
shouldPromptForContactNameSearchPermission(
|
||||
contactName = normalizedParams.contactName,
|
||||
phoneNumber = normalizedParams.phoneNumber,
|
||||
hasReadContactsPermission = contactsPermissionGranted,
|
||||
)
|
||||
val phoneNumbers = if (!normalizedParams.contactName.isNullOrEmpty()) {
|
||||
if (contactsPermissionGranted || (shouldPromptForContactsPermission && ensureReadContactsPermission())) {
|
||||
getPhoneNumbersFromContactName(normalizedParams.contactName)
|
||||
} else if (shouldPromptForContactsPermission) {
|
||||
return@withContext queryError("CONTACTS_PERMISSION_REQUIRED: grant READ_CONTACTS permission")
|
||||
} else {
|
||||
emptyList()
|
||||
}
|
||||
} else {
|
||||
emptyList()
|
||||
}
|
||||
val params = resolveSearchParams(parsedParams, normalizedPhoneNumber, phoneNumbers)
|
||||
|
||||
val mixedPathPhoneFilters = if (!params.phoneNumber.isNullOrEmpty()) {
|
||||
canonicalizeMixedPathPhoneFilters(phoneNumbers + params.phoneNumber)
|
||||
} else {
|
||||
canonicalizeMixedPathPhoneFilters(phoneNumbers)
|
||||
}
|
||||
|
||||
if (exceedsMixedByPhoneCandidateWindow(params, mixedPathPhoneFilters)) {
|
||||
val error = mixedByPhoneWindowError()
|
||||
return@withContext queryError(error)
|
||||
}
|
||||
|
||||
if (!params.contactName.isNullOrEmpty() && phoneNumbers.isEmpty() && params.phoneNumber.isNullOrEmpty()) {
|
||||
val queryMetadata = buildQueryMetadata(params, mixedPathPhoneFilters, emptyList())
|
||||
return@withContext queryOk(emptyList(), queryMetadata)
|
||||
}
|
||||
|
||||
val messages = querySmsMessages(params, phoneNumbers)
|
||||
val queryMetadata = buildQueryMetadata(params, mixedPathPhoneFilters, messages)
|
||||
queryOk(messages, queryMetadata)
|
||||
} catch (e: SecurityException) {
|
||||
queryError("SMS_PERMISSION_REQUIRED: ${e.message}")
|
||||
} catch (e: Throwable) {
|
||||
queryError("SMS_QUERY_FAILED: ${e.message ?: "unknown error"}")
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun ensureSmsPermission(): Boolean {
|
||||
if (hasSmsPermission()) return true
|
||||
val requester = permissionRequester ?: return false
|
||||
@@ -779,31 +375,98 @@ class SmsManager(private val context: Context) {
|
||||
)
|
||||
}
|
||||
|
||||
private fun queryOk(
|
||||
messages: List<SmsMessage>,
|
||||
queryMetadata: QueryMetadata? = null,
|
||||
): SearchResult {
|
||||
return SearchResult(
|
||||
ok = true,
|
||||
messages = messages,
|
||||
error = null,
|
||||
payloadJson = buildQueryPayloadJson(json, ok = true, messages = messages, queryMetadata = queryMetadata),
|
||||
)
|
||||
}
|
||||
|
||||
private fun queryError(error: String): SearchResult {
|
||||
return SearchResult(
|
||||
ok = false,
|
||||
messages = emptyList(),
|
||||
error = error,
|
||||
payloadJson = buildQueryPayloadJson(json, ok = false, messages = emptyList(), error = error),
|
||||
)
|
||||
/**
|
||||
* search SMS messages with the specified parameters.
|
||||
*
|
||||
* @param paramsJson JSON with optional fields:
|
||||
* - startTime (Long): Start time in milliseconds
|
||||
* - endTime (Long): End time in milliseconds
|
||||
* - contactName (String): Contact name to search
|
||||
* - phoneNumber (String): Phone number to search (supports partial matching)
|
||||
* - keyword (String): Keyword to search in message body
|
||||
* - type (Int): SMS type (1=Inbox, 2=Sent, 3=Draft, etc.)
|
||||
* - isRead (Boolean): Read status
|
||||
* - limit (Int): Number of records to return (default: 25, range: 1-200)
|
||||
* - offset (Int): Number of records to skip (default: 0)
|
||||
* @return SearchResult containing the list of SMS messages or an error
|
||||
*/
|
||||
suspend fun search(paramsJson: String?): SearchResult = withContext(Dispatchers.IO) {
|
||||
if (!hasTelephonyFeature()) {
|
||||
return@withContext SearchResult(
|
||||
ok = false,
|
||||
messages = emptyList(),
|
||||
error = "SMS_UNAVAILABLE: telephony not available",
|
||||
payloadJson = buildQueryPayloadJson(json, ok = false, messages = emptyList(), error = "SMS_UNAVAILABLE: telephony not available")
|
||||
)
|
||||
}
|
||||
|
||||
if (!ensureReadSmsPermission()) {
|
||||
return@withContext SearchResult(
|
||||
ok = false,
|
||||
messages = emptyList(),
|
||||
error = "SMS_PERMISSION_REQUIRED: grant READ_SMS permission",
|
||||
payloadJson = buildQueryPayloadJson(json, ok = false, messages = emptyList(), error = "SMS_PERMISSION_REQUIRED: grant READ_SMS permission")
|
||||
)
|
||||
}
|
||||
|
||||
val parseResult = parseQueryParams(paramsJson, json)
|
||||
if (parseResult is QueryParseResult.Error) {
|
||||
return@withContext SearchResult(
|
||||
ok = false,
|
||||
messages = emptyList(),
|
||||
error = parseResult.error,
|
||||
payloadJson = buildQueryPayloadJson(json, ok = false, messages = emptyList(), error = parseResult.error)
|
||||
)
|
||||
}
|
||||
val params = (parseResult as QueryParseResult.Ok).params
|
||||
|
||||
return@withContext try {
|
||||
// Get phone numbers from contact name if provided
|
||||
val phoneNumbers = if (!params.contactName.isNullOrEmpty()) {
|
||||
if (!ensureReadContactsPermission()) {
|
||||
return@withContext SearchResult(
|
||||
ok = false,
|
||||
messages = emptyList(),
|
||||
error = "CONTACTS_PERMISSION_REQUIRED: grant READ_CONTACTS permission",
|
||||
payloadJson = buildQueryPayloadJson(json, ok = false, messages = emptyList(), error = "CONTACTS_PERMISSION_REQUIRED: grant READ_CONTACTS permission")
|
||||
)
|
||||
}
|
||||
getPhoneNumbersFromContactName(params.contactName)
|
||||
} else {
|
||||
emptyList()
|
||||
}
|
||||
|
||||
val messages = querySmsMessages(params, phoneNumbers)
|
||||
SearchResult(
|
||||
ok = true,
|
||||
messages = messages,
|
||||
error = null,
|
||||
payloadJson = buildQueryPayloadJson(json, ok = true, messages = messages)
|
||||
)
|
||||
} catch (e: SecurityException) {
|
||||
SearchResult(
|
||||
ok = false,
|
||||
messages = emptyList(),
|
||||
error = "SMS_PERMISSION_REQUIRED: ${e.message}",
|
||||
payloadJson = buildQueryPayloadJson(json, ok = false, messages = emptyList(), error = "SMS_PERMISSION_REQUIRED: ${e.message}")
|
||||
)
|
||||
} catch (e: Throwable) {
|
||||
SearchResult(
|
||||
ok = false,
|
||||
messages = emptyList(),
|
||||
error = "SMS_QUERY_FAILED: ${e.message ?: "unknown error"}",
|
||||
payloadJson = buildQueryPayloadJson(json, ok = false, messages = emptyList(), error = "SMS_QUERY_FAILED: ${e.message ?: "unknown error"}")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all phone numbers associated with a contact name
|
||||
*/
|
||||
private fun getPhoneNumbersFromContactName(contactName: String): List<String> {
|
||||
val phoneNumbers = mutableListOf<String>()
|
||||
val selection = buildContactNameLikeSelection()
|
||||
val selectionArgs = arrayOf(buildContactNameLikeArg(contactName))
|
||||
val selection = "${ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME} LIKE ?"
|
||||
val selectionArgs = arrayOf("%$contactName%")
|
||||
|
||||
val cursor = context.contentResolver.query(
|
||||
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
|
||||
@@ -817,19 +480,26 @@ class SmsManager(private val context: Context) {
|
||||
val numberIndex = it.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
|
||||
while (it.moveToNext()) {
|
||||
val number = it.getString(numberIndex)
|
||||
sanitizeContactPhoneNumberOrNull(number)?.let(phoneNumbers::add)
|
||||
if (!number.isNullOrBlank()) {
|
||||
phoneNumbers.add(normalizePhoneNumber(number))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return phoneNumbers
|
||||
}
|
||||
|
||||
/**
|
||||
* Query SMS messages based on the provided parameters
|
||||
*/
|
||||
private fun querySmsMessages(params: QueryParams, phoneNumbers: List<String>): List<SmsMessage> {
|
||||
val messages = mutableListOf<SmsMessage>()
|
||||
|
||||
// Build selection and selectionArgs
|
||||
val selections = mutableListOf<String>()
|
||||
val selectionArgs = mutableListOf<String>()
|
||||
|
||||
// Time range
|
||||
if (params.startTime != null) {
|
||||
selections.add("${Telephony.Sms.DATE} >= ?")
|
||||
selectionArgs.add(params.startTime.toString())
|
||||
@@ -839,17 +509,11 @@ class SmsManager(private val context: Context) {
|
||||
selectionArgs.add(params.endTime.toString())
|
||||
}
|
||||
|
||||
// Phone numbers (from contact name or direct phone number)
|
||||
val allPhoneNumbers = if (!params.phoneNumber.isNullOrEmpty()) {
|
||||
(phoneNumbers + normalizePhoneNumber(params.phoneNumber)).distinct()
|
||||
phoneNumbers + normalizePhoneNumber(params.phoneNumber)
|
||||
} else {
|
||||
phoneNumbers.distinct()
|
||||
}
|
||||
val mixedPathPhoneFilters = canonicalizeMixedPathPhoneFilters(allPhoneNumbers)
|
||||
|
||||
// Unified SMS+MMS query path is opt-in to keep sms.search semantics
|
||||
// stable by default. Use includeMms=true for by-phone provider behavior.
|
||||
if (params.includeMms && mixedPathPhoneFilters.size == 1) {
|
||||
return querySmsMmsMessagesByPhone(mixedPathPhoneFilters.first(), params)
|
||||
phoneNumbers
|
||||
}
|
||||
|
||||
if (allPhoneNumbers.isNotEmpty()) {
|
||||
@@ -862,16 +526,19 @@ class SmsManager(private val context: Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// Keyword in body
|
||||
if (!params.keyword.isNullOrEmpty()) {
|
||||
selections.add(buildKeywordLikeSelection())
|
||||
selectionArgs.add(buildKeywordLikeArg(params.keyword))
|
||||
selections.add("${Telephony.Sms.BODY} LIKE ?")
|
||||
selectionArgs.add("%${params.keyword}%")
|
||||
}
|
||||
|
||||
// Type
|
||||
if (params.type != null) {
|
||||
selections.add("${Telephony.Sms.TYPE} = ?")
|
||||
selectionArgs.add(params.type.toString())
|
||||
}
|
||||
|
||||
// Read status
|
||||
if (params.isRead != null) {
|
||||
selections.add("${Telephony.Sms.READ} = ?")
|
||||
selectionArgs.add(if (params.isRead) "1" else "0")
|
||||
@@ -889,8 +556,7 @@ class SmsManager(private val context: Context) {
|
||||
null
|
||||
}
|
||||
|
||||
// Android SMS providers still honor LIMIT/OFFSET through sortOrder on this path.
|
||||
// Keep the bounded interpolation here because parseQueryParams already clamps both values.
|
||||
// Query SMS with SQL-level LIMIT and OFFSET to avoid loading all matching rows
|
||||
val sortOrder = "${Telephony.Sms.DATE} DESC LIMIT ${params.limit} OFFSET ${params.offset}"
|
||||
val cursor = context.contentResolver.query(
|
||||
Telephony.Sms.CONTENT_URI,
|
||||
@@ -904,7 +570,7 @@ class SmsManager(private val context: Context) {
|
||||
Telephony.Sms.READ,
|
||||
Telephony.Sms.TYPE,
|
||||
Telephony.Sms.BODY,
|
||||
Telephony.Sms.STATUS,
|
||||
Telephony.Sms.STATUS
|
||||
),
|
||||
selection,
|
||||
selectionArgsArray,
|
||||
@@ -935,7 +601,7 @@ class SmsManager(private val context: Context) {
|
||||
read = it.getInt(readIndex) == 1,
|
||||
type = it.getInt(typeIndex),
|
||||
body = it.getString(bodyIndex),
|
||||
status = it.getInt(statusIndex),
|
||||
status = it.getInt(statusIndex)
|
||||
)
|
||||
messages.add(message)
|
||||
count++
|
||||
@@ -944,184 +610,4 @@ class SmsManager(private val context: Context) {
|
||||
|
||||
return messages
|
||||
}
|
||||
|
||||
private fun querySmsMmsMessagesByPhone(phoneNumber: String, params: QueryParams): List<SmsMessage> {
|
||||
val lookupNumber = toByPhoneLookupNumber(phoneNumber)
|
||||
if (lookupNumber.isBlank()) {
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
val uri = Uri.parse("$MMS_SMS_BY_PHONE_BASE/${Uri.encode(lookupNumber)}")
|
||||
val projection = buildMixedByPhoneProjection()
|
||||
|
||||
val maxCandidates = params.offset + params.limit
|
||||
if (maxCandidates <= 0) {
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
val reviewMode = shouldUseConversationReviewByPhoneMode(params)
|
||||
val topCandidates = mutableListOf<Pair<String, SmsMessage>>()
|
||||
val materializedCandidates = linkedMapOf<String, SmsMessage>()
|
||||
val cursor = context.contentResolver.query(uri, projection, null, null, "date DESC")
|
||||
cursor?.use {
|
||||
val idIndex = it.getColumnIndex("_id")
|
||||
val threadIdIndex = it.getColumnIndex("thread_id")
|
||||
val transportTypeIndex = it.getColumnIndex("transport_type")
|
||||
val addressIndex = it.getColumnIndex("address")
|
||||
val dateIndex = it.getColumnIndex("date")
|
||||
val dateSentIndex = it.getColumnIndex("date_sent")
|
||||
val readIndex = it.getColumnIndex("read")
|
||||
val typeIndex = it.getColumnIndex("type")
|
||||
val bodyIndex = it.getColumnIndex("body")
|
||||
val statusIndex = it.getColumnIndex("status")
|
||||
|
||||
while (it.moveToNext()) {
|
||||
val id = if (idIndex >= 0 && !it.isNull(idIndex)) it.getLong(idIndex) else continue
|
||||
val rawDate = if (dateIndex >= 0 && !it.isNull(dateIndex)) it.getLong(dateIndex) else 0L
|
||||
val dateMs = normalizeProviderDateMillis(rawDate)
|
||||
|
||||
if (params.startTime != null && dateMs < params.startTime) continue
|
||||
if (params.endTime != null && dateMs > params.endTime) continue
|
||||
|
||||
val threadId = if (threadIdIndex >= 0 && !it.isNull(threadIdIndex)) it.getLong(threadIdIndex) else 0L
|
||||
val transportType = if (transportTypeIndex >= 0 && !it.isNull(transportTypeIndex)) it.getString(transportTypeIndex) else null
|
||||
val providerAddress = if (addressIndex >= 0 && !it.isNull(addressIndex)) it.getString(addressIndex) else null
|
||||
val mmsAddress = if (transportType.equals("mms", ignoreCase = true)) getMmsAddress(id, phoneNumber) else null
|
||||
val address = resolveMixedByPhoneRowAddress(providerAddress, phoneNumber, mmsAddress)
|
||||
var read = if (readIndex >= 0 && !it.isNull(readIndex)) it.getInt(readIndex) == 1 else true
|
||||
var type = if (typeIndex >= 0 && !it.isNull(typeIndex)) it.getInt(typeIndex) else 0
|
||||
var body = if (bodyIndex >= 0 && !it.isNull(bodyIndex)) it.getString(bodyIndex) else null
|
||||
val smsStatus = if (statusIndex >= 0 && !it.isNull(statusIndex)) it.getInt(statusIndex) else null
|
||||
|
||||
// Only MMS transport rows are allowed to hydrate from MMS storage.
|
||||
if (shouldHydrateMmsByPhoneRow(transportType, body, type)) {
|
||||
body = body?.takeIf { msg -> msg.isNotBlank() } ?: getMmsTextBody(id)
|
||||
val mmsMeta = getMmsMeta(id)
|
||||
if (type == 0) {
|
||||
type = mmsMeta.first ?: type
|
||||
}
|
||||
if (readIndex < 0 || it.isNull(readIndex)) {
|
||||
read = mmsMeta.second ?: read
|
||||
}
|
||||
}
|
||||
|
||||
val dateSentRaw = if (dateSentIndex >= 0 && !it.isNull(dateSentIndex)) it.getLong(dateSentIndex) else 0L
|
||||
val dateSentMs = normalizeProviderDateMillis(dateSentRaw)
|
||||
|
||||
if (!params.keyword.isNullOrEmpty()) {
|
||||
val keyword = params.keyword
|
||||
if (body.isNullOrEmpty() || !body.contains(keyword, ignoreCase = true)) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if (params.type != null && type != params.type) continue
|
||||
if (params.isRead != null && read != params.isRead) continue
|
||||
|
||||
val message = SmsMessage(
|
||||
id = id,
|
||||
threadId = threadId,
|
||||
address = address,
|
||||
person = null,
|
||||
date = dateMs,
|
||||
dateSent = dateSentMs,
|
||||
read = read,
|
||||
type = type,
|
||||
body = body,
|
||||
status = resolveMixedByPhoneRowStatus(transportType, smsStatus),
|
||||
transportType = transportType,
|
||||
)
|
||||
val identityKey = buildMixedRowIdentity(id, transportType)
|
||||
collectMixedByPhoneCandidate(
|
||||
topCandidates = topCandidates,
|
||||
materializedCandidates = materializedCandidates,
|
||||
identityKey = identityKey,
|
||||
message = message,
|
||||
maxCandidates = maxCandidates,
|
||||
reviewMode = reviewMode,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return pageMixedByPhoneCandidates(
|
||||
topCandidates = topCandidates,
|
||||
materializedCandidates = materializedCandidates,
|
||||
params = params,
|
||||
reviewMode = reviewMode,
|
||||
)
|
||||
}
|
||||
|
||||
private fun getMmsTextBody(messageId: Long): String? {
|
||||
val cursor = context.contentResolver.query(
|
||||
Uri.parse(MMS_PART_URI),
|
||||
arrayOf("text", "ct"),
|
||||
"mid=?",
|
||||
arrayOf(messageId.toString()),
|
||||
null,
|
||||
)
|
||||
|
||||
cursor?.use {
|
||||
val textIndex = it.getColumnIndex("text")
|
||||
val ctIndex = it.getColumnIndex("ct")
|
||||
while (it.moveToNext()) {
|
||||
val contentType = if (ctIndex >= 0 && !it.isNull(ctIndex)) it.getString(ctIndex) else null
|
||||
if (contentType != null && contentType != "text/plain") continue
|
||||
val text = if (textIndex >= 0 && !it.isNull(textIndex)) it.getString(textIndex) else null
|
||||
if (!text.isNullOrBlank()) return text
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
private fun getMmsMeta(messageId: Long): Pair<Int?, Boolean?> {
|
||||
val cursor = context.contentResolver.query(
|
||||
Uri.parse("$MMS_CONTENT_BASE/$messageId"),
|
||||
arrayOf("msg_box", "read"),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
)
|
||||
|
||||
cursor?.use {
|
||||
if (it.moveToFirst()) {
|
||||
val msgBoxIndex = it.getColumnIndex("msg_box")
|
||||
val readIndex = it.getColumnIndex("read")
|
||||
val msgBox = if (msgBoxIndex >= 0 && !it.isNull(msgBoxIndex)) it.getInt(msgBoxIndex) else null
|
||||
val mappedType = mapMmsMsgBoxToSearchType(msgBox)
|
||||
val read = if (readIndex >= 0 && !it.isNull(readIndex)) it.getInt(readIndex) == 1 else null
|
||||
return mappedType to read
|
||||
}
|
||||
}
|
||||
|
||||
return null to null
|
||||
}
|
||||
|
||||
private fun getMmsAddress(messageId: Long, phoneNumber: String): String? {
|
||||
val lookupNumber = toByPhoneLookupNumber(phoneNumber)
|
||||
if (lookupNumber.isBlank()) {
|
||||
return null
|
||||
}
|
||||
|
||||
val cursor = context.contentResolver.query(
|
||||
Uri.parse("$MMS_CONTENT_BASE/$messageId/addr"),
|
||||
arrayOf("address", "type"),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
)
|
||||
|
||||
cursor?.use {
|
||||
val addressIndex = it.getColumnIndex("address")
|
||||
val typeIndex = it.getColumnIndex("type")
|
||||
val addressRows = mutableListOf<Pair<String?, Int?>>()
|
||||
while (it.moveToNext()) {
|
||||
val address = if (addressIndex >= 0 && !it.isNull(addressIndex)) it.getString(addressIndex) else null
|
||||
val type = if (typeIndex >= 0 && !it.isNull(typeIndex)) it.getInt(typeIndex) else null
|
||||
addressRows.add(address to type)
|
||||
}
|
||||
return selectPreferredMmsAddress(addressRows, lookupNumber)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1459,8 +1459,8 @@ private fun PermissionsStep(
|
||||
subtitle = "Send and search text messages via the gateway",
|
||||
checked = enableSms,
|
||||
granted =
|
||||
isPermissionGranted(context, Manifest.permission.SEND_SMS) ||
|
||||
isPermissionGranted(context, Manifest.permission.READ_SMS),
|
||||
isPermissionGranted(context, Manifest.permission.SEND_SMS) &&
|
||||
isPermissionGranted(context, Manifest.permission.READ_SMS),
|
||||
onCheckedChange = onSmsChange,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
import androidx.compose.material3.HorizontalDivider
|
||||
@@ -53,23 +54,20 @@ import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.net.toUri
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleEventObserver
|
||||
import androidx.lifecycle.compose.LocalLifecycleOwner
|
||||
import ai.openclaw.app.BuildConfig
|
||||
import ai.openclaw.app.LocationMode
|
||||
import ai.openclaw.app.MainViewModel
|
||||
import ai.openclaw.app.normalizeLocalHourMinute
|
||||
import ai.openclaw.app.NotificationPackageFilterMode
|
||||
import ai.openclaw.app.node.DeviceNotificationListenerService
|
||||
|
||||
@Composable
|
||||
@@ -83,55 +81,6 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
||||
val locationPreciseEnabled by viewModel.locationPreciseEnabled.collectAsState()
|
||||
val preventSleep by viewModel.preventSleep.collectAsState()
|
||||
val canvasDebugStatusEnabled by viewModel.canvasDebugStatusEnabled.collectAsState()
|
||||
val notificationForwardingEnabled by viewModel.notificationForwardingEnabled.collectAsState()
|
||||
val notificationForwardingMode by viewModel.notificationForwardingMode.collectAsState()
|
||||
val notificationForwardingPackages by viewModel.notificationForwardingPackages.collectAsState()
|
||||
val notificationForwardingQuietHoursEnabled by viewModel.notificationForwardingQuietHoursEnabled.collectAsState()
|
||||
val notificationForwardingQuietStart by viewModel.notificationForwardingQuietStart.collectAsState()
|
||||
val notificationForwardingQuietEnd by viewModel.notificationForwardingQuietEnd.collectAsState()
|
||||
val notificationForwardingMaxEventsPerMinute by viewModel.notificationForwardingMaxEventsPerMinute.collectAsState()
|
||||
val notificationForwardingSessionKey by viewModel.notificationForwardingSessionKey.collectAsState()
|
||||
|
||||
var notificationQuietStartDraft by remember(notificationForwardingQuietStart) {
|
||||
mutableStateOf(notificationForwardingQuietStart)
|
||||
}
|
||||
var notificationQuietEndDraft by remember(notificationForwardingQuietEnd) {
|
||||
mutableStateOf(notificationForwardingQuietEnd)
|
||||
}
|
||||
var notificationRateDraft by remember(notificationForwardingMaxEventsPerMinute) {
|
||||
mutableStateOf(notificationForwardingMaxEventsPerMinute.toString())
|
||||
}
|
||||
var notificationSessionKeyDraft by remember(notificationForwardingSessionKey) {
|
||||
mutableStateOf(notificationForwardingSessionKey.orEmpty())
|
||||
}
|
||||
val normalizedQuietStartDraft = remember(notificationQuietStartDraft) {
|
||||
normalizeLocalHourMinute(notificationQuietStartDraft)
|
||||
}
|
||||
val normalizedQuietEndDraft = remember(notificationQuietEndDraft) {
|
||||
normalizeLocalHourMinute(notificationQuietEndDraft)
|
||||
}
|
||||
val quietHoursDraftValid = normalizedQuietStartDraft != null && normalizedQuietEndDraft != null
|
||||
val selectedPackagesSummary = remember(notificationForwardingMode, notificationForwardingPackages) {
|
||||
when (notificationForwardingMode) {
|
||||
NotificationPackageFilterMode.Allowlist ->
|
||||
if (notificationForwardingPackages.isEmpty()) {
|
||||
"Selected: none — allowlist mode forwards nothing until you add apps."
|
||||
} else {
|
||||
"Selected: ${notificationForwardingPackages.size} app(s) allowed."
|
||||
}
|
||||
NotificationPackageFilterMode.Blocklist ->
|
||||
if (notificationForwardingPackages.isEmpty()) {
|
||||
"Selected: none — blocklist mode forwards all apps except OpenClaw."
|
||||
} else {
|
||||
"Selected: ${notificationForwardingPackages.size} app(s) blocked."
|
||||
}
|
||||
}
|
||||
}
|
||||
val quietHoursCanEnable = notificationForwardingEnabled && quietHoursDraftValid
|
||||
val quietHoursDraftDirty =
|
||||
notificationForwardingQuietStart != (normalizedQuietStartDraft ?: notificationQuietStartDraft.trim()) ||
|
||||
notificationForwardingQuietEnd != (normalizedQuietEndDraft ?: notificationQuietEndDraft.trim())
|
||||
val quietHoursSaveEnabled = notificationForwardingEnabled && quietHoursDraftValid && quietHoursDraftDirty
|
||||
|
||||
val listState = rememberLazyListState()
|
||||
val deviceModel =
|
||||
@@ -226,16 +175,6 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
||||
remember {
|
||||
mutableStateOf(isNotificationListenerEnabled(context))
|
||||
}
|
||||
val notificationForwardingAvailable = notificationForwardingEnabled && notificationListenerEnabled
|
||||
val notificationForwardingControlsAlpha = if (notificationForwardingAvailable) 1f else 0.6f
|
||||
|
||||
var notificationPickerExpanded by remember { mutableStateOf(false) }
|
||||
var notificationAppSearch by remember { mutableStateOf("") }
|
||||
var notificationShowSystemApps by remember { mutableStateOf(false) }
|
||||
var installedNotificationApps by
|
||||
remember(context, notificationForwardingPackages) {
|
||||
mutableStateOf(queryInstalledApps(context, notificationForwardingPackages))
|
||||
}
|
||||
|
||||
var photosPermissionGranted by
|
||||
remember {
|
||||
@@ -310,19 +249,16 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
||||
remember {
|
||||
mutableStateOf(
|
||||
ContextCompat.checkSelfPermission(context, Manifest.permission.SEND_SMS) ==
|
||||
PackageManager.PERMISSION_GRANTED ||
|
||||
PackageManager.PERMISSION_GRANTED &&
|
||||
ContextCompat.checkSelfPermission(context, Manifest.permission.READ_SMS) ==
|
||||
PackageManager.PERMISSION_GRANTED,
|
||||
)
|
||||
}
|
||||
val smsPermissionLauncher =
|
||||
rememberLauncherForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
|
||||
smsPermissionGranted =
|
||||
ContextCompat.checkSelfPermission(context, Manifest.permission.SEND_SMS) ==
|
||||
PackageManager.PERMISSION_GRANTED
|
||||
||
|
||||
ContextCompat.checkSelfPermission(context, Manifest.permission.READ_SMS) ==
|
||||
PackageManager.PERMISSION_GRANTED
|
||||
rememberLauncherForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { perms ->
|
||||
val sendOk = perms[Manifest.permission.SEND_SMS] == true
|
||||
val readOk = perms[Manifest.permission.READ_SMS] == true
|
||||
smsPermissionGranted = sendOk && readOk
|
||||
viewModel.refreshGatewayConnection()
|
||||
}
|
||||
|
||||
@@ -335,7 +271,6 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
||||
PackageManager.PERMISSION_GRANTED
|
||||
notificationsPermissionGranted = hasNotificationsPermission(context)
|
||||
notificationListenerEnabled = isNotificationListenerEnabled(context)
|
||||
installedNotificationApps = queryInstalledApps(context, notificationForwardingPackages)
|
||||
photosPermissionGranted =
|
||||
ContextCompat.checkSelfPermission(context, photosPermission) ==
|
||||
PackageManager.PERMISSION_GRANTED
|
||||
@@ -358,8 +293,7 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
||||
PackageManager.PERMISSION_GRANTED
|
||||
smsPermissionGranted =
|
||||
ContextCompat.checkSelfPermission(context, Manifest.permission.SEND_SMS) ==
|
||||
PackageManager.PERMISSION_GRANTED
|
||||
||
|
||||
PackageManager.PERMISSION_GRANTED &&
|
||||
ContextCompat.checkSelfPermission(context, Manifest.permission.READ_SMS) ==
|
||||
PackageManager.PERMISSION_GRANTED
|
||||
}
|
||||
@@ -417,20 +351,6 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
||||
}
|
||||
}
|
||||
|
||||
val normalizedAppSearch = notificationAppSearch.trim().lowercase()
|
||||
val filteredNotificationApps =
|
||||
remember(installedNotificationApps, normalizedAppSearch, notificationShowSystemApps) {
|
||||
installedNotificationApps
|
||||
.asSequence()
|
||||
.filter { app -> notificationShowSystemApps || !app.isSystemApp }
|
||||
.filter { app ->
|
||||
normalizedAppSearch.isEmpty() ||
|
||||
app.label.lowercase().contains(normalizedAppSearch) ||
|
||||
app.packageName.lowercase().contains(normalizedAppSearch)
|
||||
}
|
||||
.toList()
|
||||
}
|
||||
|
||||
Box(
|
||||
modifier =
|
||||
Modifier
|
||||
@@ -571,12 +491,9 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
||||
ListItem(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
colors = listItemColors,
|
||||
headlineContent = { Text("Notification Listener Access", style = mobileHeadline) },
|
||||
headlineContent = { Text("Notification Listener", style = mobileHeadline) },
|
||||
supportingContent = {
|
||||
Text(
|
||||
"Required for `notifications.list`, `notifications.actions`, and forwarded notification events.",
|
||||
style = mobileCallout,
|
||||
)
|
||||
Text("Read and interact with notifications.", style = mobileCallout)
|
||||
},
|
||||
trailingContent = {
|
||||
Button(
|
||||
@@ -613,11 +530,7 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
||||
shape = RoundedCornerShape(14.dp),
|
||||
) {
|
||||
Text(
|
||||
if (smsPermissionGranted) {
|
||||
"Manage"
|
||||
} else {
|
||||
"Grant"
|
||||
},
|
||||
if (smsPermissionGranted) "Manage" else "Grant",
|
||||
style = mobileCallout.copy(fontWeight = FontWeight.Bold),
|
||||
)
|
||||
}
|
||||
@@ -626,297 +539,6 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
||||
}
|
||||
}
|
||||
}
|
||||
item {
|
||||
ListItem(
|
||||
modifier = Modifier.settingsRowModifier(),
|
||||
colors = listItemColors,
|
||||
headlineContent = { Text("Forward Notification Events", style = mobileHeadline) },
|
||||
supportingContent = {
|
||||
Text(
|
||||
if (notificationListenerEnabled) {
|
||||
"Forward listener events into gateway node events. Off by default until you enable it."
|
||||
} else {
|
||||
"Notification listener access is off, so no notification events can be forwarded yet."
|
||||
},
|
||||
style = mobileCallout,
|
||||
)
|
||||
},
|
||||
trailingContent = {
|
||||
Switch(
|
||||
checked = notificationForwardingEnabled,
|
||||
onCheckedChange = viewModel::setNotificationForwardingEnabled,
|
||||
enabled = notificationListenerEnabled,
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
item {
|
||||
Text(
|
||||
if (notificationListenerEnabled) {
|
||||
"Forwarding is available when enabled below."
|
||||
} else {
|
||||
"Forwarding controls stay disabled until Notification Listener Access is enabled in system Settings."
|
||||
},
|
||||
style = mobileCallout,
|
||||
color = mobileTextSecondary,
|
||||
)
|
||||
}
|
||||
item {
|
||||
Column(
|
||||
modifier = Modifier.settingsRowModifier().alpha(notificationForwardingControlsAlpha),
|
||||
verticalArrangement = Arrangement.spacedBy(0.dp),
|
||||
) {
|
||||
ListItem(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
colors = listItemColors,
|
||||
headlineContent = { Text("Package Filter: Allowlist", style = mobileHeadline) },
|
||||
supportingContent = {
|
||||
Text("Only listed package IDs are forwarded.", style = mobileCallout)
|
||||
},
|
||||
trailingContent = {
|
||||
RadioButton(
|
||||
selected = notificationForwardingMode == NotificationPackageFilterMode.Allowlist,
|
||||
onClick = {
|
||||
viewModel.setNotificationForwardingMode(NotificationPackageFilterMode.Allowlist)
|
||||
},
|
||||
enabled = notificationForwardingAvailable,
|
||||
)
|
||||
},
|
||||
)
|
||||
HorizontalDivider(color = mobileBorder)
|
||||
ListItem(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
colors = listItemColors,
|
||||
headlineContent = { Text("Package Filter: Blocklist", style = mobileHeadline) },
|
||||
supportingContent = {
|
||||
Text("All packages except listed IDs are forwarded.", style = mobileCallout)
|
||||
},
|
||||
trailingContent = {
|
||||
RadioButton(
|
||||
selected = notificationForwardingMode == NotificationPackageFilterMode.Blocklist,
|
||||
onClick = {
|
||||
viewModel.setNotificationForwardingMode(NotificationPackageFilterMode.Blocklist)
|
||||
},
|
||||
enabled = notificationForwardingAvailable,
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
item {
|
||||
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) {
|
||||
Button(
|
||||
onClick = { notificationPickerExpanded = !notificationPickerExpanded },
|
||||
enabled = notificationForwardingAvailable,
|
||||
colors = settingsPrimaryButtonColors(),
|
||||
shape = RoundedCornerShape(14.dp),
|
||||
) {
|
||||
Text(
|
||||
if (notificationPickerExpanded) "Close App Picker" else "Open App Picker",
|
||||
style = mobileCallout.copy(fontWeight = FontWeight.Bold),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
item {
|
||||
Text(
|
||||
selectedPackagesSummary,
|
||||
style = mobileCallout,
|
||||
color = mobileTextSecondary,
|
||||
)
|
||||
}
|
||||
if (notificationPickerExpanded) {
|
||||
item {
|
||||
OutlinedTextField(
|
||||
value = notificationAppSearch,
|
||||
onValueChange = { notificationAppSearch = it },
|
||||
label = {
|
||||
Text("Search apps", style = mobileCaption1, color = mobileTextSecondary)
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
textStyle = mobileBody.copy(color = mobileText),
|
||||
colors = settingsTextFieldColors(),
|
||||
enabled = notificationForwardingAvailable,
|
||||
)
|
||||
}
|
||||
item {
|
||||
ListItem(
|
||||
modifier = Modifier.settingsRowModifier().alpha(notificationForwardingControlsAlpha),
|
||||
colors = listItemColors,
|
||||
headlineContent = { Text("Show System Apps", style = mobileHeadline) },
|
||||
supportingContent = {
|
||||
Text("Include Android/system packages in results.", style = mobileCallout)
|
||||
},
|
||||
trailingContent = {
|
||||
Switch(
|
||||
checked = notificationShowSystemApps,
|
||||
onCheckedChange = { notificationShowSystemApps = it },
|
||||
enabled = notificationForwardingAvailable,
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
items(filteredNotificationApps, key = { it.packageName }) { app ->
|
||||
ListItem(
|
||||
modifier = Modifier.settingsRowModifier().alpha(notificationForwardingControlsAlpha),
|
||||
colors = listItemColors,
|
||||
headlineContent = { Text(app.label, style = mobileHeadline) },
|
||||
supportingContent = { Text(app.packageName, style = mobileCallout) },
|
||||
trailingContent = {
|
||||
Switch(
|
||||
checked = notificationForwardingPackages.contains(app.packageName),
|
||||
onCheckedChange = { checked ->
|
||||
val next = notificationForwardingPackages.toMutableSet()
|
||||
if (checked) {
|
||||
next.add(app.packageName)
|
||||
} else {
|
||||
next.remove(app.packageName)
|
||||
}
|
||||
viewModel.setNotificationForwardingPackagesCsv(next.sorted().joinToString(","))
|
||||
},
|
||||
enabled = notificationForwardingAvailable,
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
item {
|
||||
ListItem(
|
||||
modifier = Modifier.settingsRowModifier().alpha(notificationForwardingControlsAlpha),
|
||||
colors = listItemColors,
|
||||
headlineContent = { Text("Quiet Hours", style = mobileHeadline) },
|
||||
supportingContent = {
|
||||
Text("Suppress forwarding during a local time window.", style = mobileCallout)
|
||||
},
|
||||
trailingContent = {
|
||||
Switch(
|
||||
checked = notificationForwardingQuietHoursEnabled,
|
||||
onCheckedChange = {
|
||||
if (!quietHoursCanEnable && it) return@Switch
|
||||
viewModel.setNotificationForwardingQuietHours(
|
||||
enabled = it,
|
||||
start = notificationQuietStartDraft,
|
||||
end = notificationQuietEndDraft,
|
||||
)
|
||||
},
|
||||
enabled = if (notificationForwardingQuietHoursEnabled) notificationForwardingAvailable else quietHoursCanEnable,
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
item {
|
||||
OutlinedTextField(
|
||||
value = notificationQuietStartDraft,
|
||||
onValueChange = { notificationQuietStartDraft = it },
|
||||
label = { Text("Quiet Start (HH:mm)", style = mobileCaption1, color = mobileTextSecondary) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
textStyle = mobileBody.copy(color = mobileText),
|
||||
colors = settingsTextFieldColors(),
|
||||
enabled = notificationForwardingAvailable,
|
||||
isError = notificationForwardingAvailable && normalizedQuietStartDraft == null,
|
||||
supportingText = {
|
||||
if (notificationForwardingAvailable && normalizedQuietStartDraft == null) {
|
||||
Text("Use 24-hour HH:mm format, for example 22:00.", style = mobileCaption1, color = mobileDanger)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
item {
|
||||
OutlinedTextField(
|
||||
value = notificationQuietEndDraft,
|
||||
onValueChange = { notificationQuietEndDraft = it },
|
||||
label = { Text("Quiet End (HH:mm)", style = mobileCaption1, color = mobileTextSecondary) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
textStyle = mobileBody.copy(color = mobileText),
|
||||
colors = settingsTextFieldColors(),
|
||||
enabled = notificationForwardingAvailable,
|
||||
isError = notificationForwardingAvailable && normalizedQuietEndDraft == null,
|
||||
supportingText = {
|
||||
if (notificationForwardingAvailable && normalizedQuietEndDraft == null) {
|
||||
Text("Use 24-hour HH:mm format, for example 07:00.", style = mobileCaption1, color = mobileDanger)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
item {
|
||||
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) {
|
||||
Button(
|
||||
onClick = {
|
||||
viewModel.setNotificationForwardingQuietHours(
|
||||
enabled = notificationForwardingQuietHoursEnabled,
|
||||
start = notificationQuietStartDraft,
|
||||
end = notificationQuietEndDraft,
|
||||
)
|
||||
},
|
||||
enabled = quietHoursSaveEnabled,
|
||||
colors = settingsPrimaryButtonColors(),
|
||||
shape = RoundedCornerShape(14.dp),
|
||||
) {
|
||||
Text("Save Quiet Hours", style = mobileCallout.copy(fontWeight = FontWeight.Bold))
|
||||
}
|
||||
}
|
||||
}
|
||||
item {
|
||||
OutlinedTextField(
|
||||
value = notificationRateDraft,
|
||||
onValueChange = { notificationRateDraft = it.filter { c -> c.isDigit() } },
|
||||
label = { Text("Max Events / Minute", style = mobileCaption1, color = mobileTextSecondary) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
textStyle = mobileBody.copy(color = mobileText),
|
||||
colors = settingsTextFieldColors(),
|
||||
enabled = notificationForwardingAvailable,
|
||||
)
|
||||
}
|
||||
item {
|
||||
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) {
|
||||
Button(
|
||||
onClick = {
|
||||
val parsed = notificationRateDraft.toIntOrNull() ?: notificationForwardingMaxEventsPerMinute
|
||||
viewModel.setNotificationForwardingMaxEventsPerMinute(parsed)
|
||||
},
|
||||
enabled = notificationForwardingAvailable,
|
||||
colors = settingsPrimaryButtonColors(),
|
||||
shape = RoundedCornerShape(14.dp),
|
||||
) {
|
||||
Text("Save Rate", style = mobileCallout.copy(fontWeight = FontWeight.Bold))
|
||||
}
|
||||
}
|
||||
}
|
||||
item {
|
||||
OutlinedTextField(
|
||||
value = notificationSessionKeyDraft,
|
||||
onValueChange = { notificationSessionKeyDraft = it },
|
||||
label = {
|
||||
Text(
|
||||
"Route Session Key (optional)",
|
||||
style = mobileCaption1,
|
||||
color = mobileTextSecondary,
|
||||
)
|
||||
},
|
||||
placeholder = {
|
||||
Text("Blank keeps notification events on this device's default notification route. Set a key only to pin forwarding into a different session.", style = mobileCaption1, color = mobileTextSecondary)
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
textStyle = mobileBody.copy(color = mobileText),
|
||||
colors = settingsTextFieldColors(),
|
||||
enabled = notificationForwardingAvailable,
|
||||
)
|
||||
}
|
||||
item {
|
||||
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) {
|
||||
Button(
|
||||
onClick = {
|
||||
viewModel.setNotificationForwardingSessionKey(notificationSessionKeyDraft.trim().ifEmpty { null })
|
||||
},
|
||||
enabled = notificationForwardingAvailable,
|
||||
colors = settingsPrimaryButtonColors(),
|
||||
shape = RoundedCornerShape(14.dp),
|
||||
) {
|
||||
Text("Save Session Route", style = mobileCallout.copy(fontWeight = FontWeight.Bold))
|
||||
}
|
||||
}
|
||||
}
|
||||
item { HorizontalDivider(color = mobileBorder) }
|
||||
|
||||
// ── Data Access ──
|
||||
item {
|
||||
@@ -1152,78 +774,6 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
||||
}
|
||||
}
|
||||
|
||||
data class InstalledApp(
|
||||
val label: String,
|
||||
val packageName: String,
|
||||
val isSystemApp: Boolean,
|
||||
)
|
||||
|
||||
private fun queryInstalledApps(
|
||||
context: Context,
|
||||
configuredPackages: Set<String>,
|
||||
): List<InstalledApp> {
|
||||
val packageManager = context.packageManager
|
||||
val launcherIntent = Intent(Intent.ACTION_MAIN).apply { addCategory(Intent.CATEGORY_LAUNCHER) }
|
||||
|
||||
val launcherPackages =
|
||||
packageManager
|
||||
.queryIntentActivities(launcherIntent, PackageManager.MATCH_ALL)
|
||||
.asSequence()
|
||||
.mapNotNull { it.activityInfo?.packageName?.trim()?.takeIf(String::isNotEmpty) }
|
||||
.toMutableSet()
|
||||
|
||||
val recentNotificationPackages =
|
||||
DeviceNotificationListenerService
|
||||
.recentPackages(context)
|
||||
.asSequence()
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotEmpty() }
|
||||
.toList()
|
||||
|
||||
val candidatePackages =
|
||||
resolveNotificationCandidatePackages(
|
||||
launcherPackages = launcherPackages,
|
||||
recentPackages = recentNotificationPackages,
|
||||
configuredPackages = configuredPackages,
|
||||
appPackageName = context.packageName,
|
||||
)
|
||||
|
||||
return candidatePackages
|
||||
.asSequence()
|
||||
.mapNotNull { packageName ->
|
||||
runCatching {
|
||||
val appInfo = packageManager.getApplicationInfo(packageName, 0)
|
||||
val label = packageManager.getApplicationLabel(appInfo)?.toString()?.trim().orEmpty()
|
||||
InstalledApp(
|
||||
label = if (label.isEmpty()) packageName else label,
|
||||
packageName = packageName,
|
||||
isSystemApp = (appInfo.flags and android.content.pm.ApplicationInfo.FLAG_SYSTEM) != 0,
|
||||
)
|
||||
}.getOrNull()
|
||||
}
|
||||
.sortedWith(compareBy<InstalledApp> { it.label.lowercase() }.thenBy { it.packageName })
|
||||
.toList()
|
||||
}
|
||||
|
||||
internal fun resolveNotificationCandidatePackages(
|
||||
launcherPackages: Set<String>,
|
||||
recentPackages: List<String>,
|
||||
configuredPackages: Set<String>,
|
||||
appPackageName: String,
|
||||
): Set<String> {
|
||||
val blockedPackage = appPackageName.trim()
|
||||
return sequenceOf(
|
||||
configuredPackages.asSequence(),
|
||||
launcherPackages.asSequence(),
|
||||
recentPackages.asSequence(),
|
||||
)
|
||||
.flatten()
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotEmpty() && it != blockedPackage }
|
||||
.toSet()
|
||||
}
|
||||
|
||||
|
||||
@Composable
|
||||
private fun settingsTextFieldColors() =
|
||||
OutlinedTextFieldDefaults.colors(
|
||||
@@ -1292,5 +842,5 @@ private fun isNotificationListenerEnabled(context: Context): Boolean {
|
||||
private fun hasMotionCapabilities(context: Context): Boolean {
|
||||
val sensorManager = context.getSystemService(SensorManager::class.java) ?: return false
|
||||
return sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null ||
|
||||
sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER) != null
|
||||
sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER) != null
|
||||
}
|
||||
|
||||
@@ -1,189 +0,0 @@
|
||||
package ai.openclaw.app
|
||||
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneId
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class NotificationForwardingPolicyTest {
|
||||
@Test
|
||||
fun parseLocalHourMinute_parsesValidValues() {
|
||||
assertEquals(0, parseLocalHourMinute("00:00"))
|
||||
assertEquals(23 * 60 + 59, parseLocalHourMinute("23:59"))
|
||||
assertEquals(7 * 60 + 5, parseLocalHourMinute("07:05"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun normalizeLocalHourMinute_acceptsStrict24HourDrafts() {
|
||||
assertEquals("00:00", normalizeLocalHourMinute("00:00"))
|
||||
assertEquals("23:59", normalizeLocalHourMinute("23:59"))
|
||||
assertEquals("07:05", normalizeLocalHourMinute("07:05"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseLocalHourMinute_rejectsInvalidValues() {
|
||||
assertEquals(null, parseLocalHourMinute(""))
|
||||
assertEquals(null, parseLocalHourMinute("24:00"))
|
||||
assertEquals(null, parseLocalHourMinute("12:60"))
|
||||
assertEquals(null, parseLocalHourMinute("abc"))
|
||||
assertEquals(null, parseLocalHourMinute("7:05"))
|
||||
assertEquals(null, parseLocalHourMinute("07:5"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun normalizeLocalHourMinute_rejectsNonCanonicalDrafts() {
|
||||
assertEquals(null, normalizeLocalHourMinute(""))
|
||||
assertEquals(null, normalizeLocalHourMinute("7:05"))
|
||||
assertEquals(null, normalizeLocalHourMinute("07:5"))
|
||||
assertEquals(null, normalizeLocalHourMinute("24:00"))
|
||||
assertEquals(null, normalizeLocalHourMinute("12:60"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun allowsPackage_blocklistBlocksConfiguredPackages() {
|
||||
val policy =
|
||||
NotificationForwardingPolicy(
|
||||
enabled = true,
|
||||
mode = NotificationPackageFilterMode.Blocklist,
|
||||
packages = setOf("com.blocked.app"),
|
||||
quietHoursEnabled = false,
|
||||
quietStart = "22:00",
|
||||
quietEnd = "07:00",
|
||||
maxEventsPerMinute = 20,
|
||||
sessionKey = null,
|
||||
)
|
||||
|
||||
assertFalse(policy.allowsPackage("com.blocked.app"))
|
||||
assertTrue(policy.allowsPackage("com.allowed.app"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun allowsPackage_allowlistOnlyAllowsConfiguredPackages() {
|
||||
val policy =
|
||||
NotificationForwardingPolicy(
|
||||
enabled = true,
|
||||
mode = NotificationPackageFilterMode.Allowlist,
|
||||
packages = setOf("com.allowed.app"),
|
||||
quietHoursEnabled = false,
|
||||
quietStart = "22:00",
|
||||
quietEnd = "07:00",
|
||||
maxEventsPerMinute = 20,
|
||||
sessionKey = null,
|
||||
)
|
||||
|
||||
assertTrue(policy.allowsPackage("com.allowed.app"))
|
||||
assertFalse(policy.allowsPackage("com.other.app"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isWithinQuietHours_handlesWindowCrossingMidnight() {
|
||||
val policy =
|
||||
NotificationForwardingPolicy(
|
||||
enabled = true,
|
||||
mode = NotificationPackageFilterMode.Blocklist,
|
||||
packages = emptySet(),
|
||||
quietHoursEnabled = true,
|
||||
quietStart = "22:00",
|
||||
quietEnd = "07:00",
|
||||
maxEventsPerMinute = 20,
|
||||
sessionKey = null,
|
||||
)
|
||||
|
||||
val zone = ZoneId.of("UTC")
|
||||
val at2330 =
|
||||
LocalDateTime
|
||||
.of(2024, 1, 6, 23, 30)
|
||||
.atZone(zone)
|
||||
.toInstant()
|
||||
.toEpochMilli()
|
||||
val at1200 =
|
||||
LocalDateTime
|
||||
.of(2024, 1, 6, 12, 0)
|
||||
.atZone(zone)
|
||||
.toInstant()
|
||||
.toEpochMilli()
|
||||
|
||||
assertTrue(policy.isWithinQuietHours(nowEpochMs = at2330, zoneId = zone))
|
||||
assertFalse(policy.isWithinQuietHours(nowEpochMs = at1200, zoneId = zone))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isWithinQuietHours_sameStartEndMeansAlwaysQuiet() {
|
||||
val policy =
|
||||
NotificationForwardingPolicy(
|
||||
enabled = true,
|
||||
mode = NotificationPackageFilterMode.Blocklist,
|
||||
packages = emptySet(),
|
||||
quietHoursEnabled = true,
|
||||
quietStart = "00:00",
|
||||
quietEnd = "00:00",
|
||||
maxEventsPerMinute = 20,
|
||||
sessionKey = null,
|
||||
)
|
||||
|
||||
assertTrue(policy.isWithinQuietHours(nowEpochMs = 1_704_098_400_000L, zoneId = ZoneId.of("UTC")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun blocksEventsWhenDisabledOrQuietHoursOrRateLimited() {
|
||||
val disabled =
|
||||
NotificationForwardingPolicy(
|
||||
enabled = false,
|
||||
mode = NotificationPackageFilterMode.Blocklist,
|
||||
packages = emptySet(),
|
||||
quietHoursEnabled = false,
|
||||
quietStart = "22:00",
|
||||
quietEnd = "07:00",
|
||||
maxEventsPerMinute = 20,
|
||||
sessionKey = null,
|
||||
)
|
||||
assertFalse(disabled.enabled && disabled.allowsPackage("com.allowed.app"))
|
||||
|
||||
val quiet =
|
||||
NotificationForwardingPolicy(
|
||||
enabled = true,
|
||||
mode = NotificationPackageFilterMode.Blocklist,
|
||||
packages = emptySet(),
|
||||
quietHoursEnabled = true,
|
||||
quietStart = "22:00",
|
||||
quietEnd = "07:00",
|
||||
maxEventsPerMinute = 20,
|
||||
sessionKey = null,
|
||||
)
|
||||
val zone = ZoneId.of("UTC")
|
||||
val at2330 =
|
||||
LocalDateTime
|
||||
.of(2024, 1, 6, 23, 30)
|
||||
.atZone(zone)
|
||||
.toInstant()
|
||||
.toEpochMilli()
|
||||
assertTrue(quiet.isWithinQuietHours(nowEpochMs = at2330, zoneId = zone))
|
||||
|
||||
val limiter = NotificationBurstLimiter()
|
||||
val minute = 1_704_098_400_000L
|
||||
assertTrue(limiter.allow(nowEpochMs = minute, maxEventsPerMinute = 1))
|
||||
assertFalse(limiter.allow(nowEpochMs = minute + 500L, maxEventsPerMinute = 1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun burstLimiter_blocksEventsAboveLimitInSameMinute() {
|
||||
val limiter = NotificationBurstLimiter()
|
||||
val minute = 1_704_098_400_000L
|
||||
|
||||
assertTrue(limiter.allow(nowEpochMs = minute, maxEventsPerMinute = 2))
|
||||
assertTrue(limiter.allow(nowEpochMs = minute + 1_000L, maxEventsPerMinute = 2))
|
||||
assertFalse(limiter.allow(nowEpochMs = minute + 2_000L, maxEventsPerMinute = 2))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun burstLimiter_resetsOnNextMinuteWindow() {
|
||||
val limiter = NotificationBurstLimiter()
|
||||
val minute = 1_704_098_400_000L
|
||||
|
||||
assertTrue(limiter.allow(nowEpochMs = minute, maxEventsPerMinute = 1))
|
||||
assertFalse(limiter.allow(nowEpochMs = minute + 1_000L, maxEventsPerMinute = 1))
|
||||
assertTrue(limiter.allow(nowEpochMs = minute + 60_000L, maxEventsPerMinute = 1))
|
||||
}
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
package ai.openclaw.app
|
||||
|
||||
import android.content.Context
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class SecurePrefsNotificationForwardingTest {
|
||||
@Test
|
||||
fun setNotificationForwardingQuietHours_rejectsInvalidDraftsWithoutMutatingStoredValues() {
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
val plainPrefs = context.getSharedPreferences("openclaw.node", Context.MODE_PRIVATE)
|
||||
plainPrefs.edit().clear().commit()
|
||||
|
||||
val prefs = SecurePrefs(context)
|
||||
|
||||
assertTrue(
|
||||
prefs.setNotificationForwardingQuietHours(
|
||||
enabled = false,
|
||||
start = "22:00",
|
||||
end = "07:00",
|
||||
),
|
||||
)
|
||||
|
||||
val originalStart = prefs.notificationForwardingQuietStart.value
|
||||
val originalEnd = prefs.notificationForwardingQuietEnd.value
|
||||
val originalEnabled = prefs.notificationForwardingQuietHoursEnabled.value
|
||||
|
||||
assertFalse(
|
||||
prefs.setNotificationForwardingQuietHours(
|
||||
enabled = true,
|
||||
start = "7:00",
|
||||
end = "07:00",
|
||||
),
|
||||
)
|
||||
|
||||
assertEquals(originalStart, prefs.notificationForwardingQuietStart.value)
|
||||
assertEquals(originalEnd, prefs.notificationForwardingQuietEnd.value)
|
||||
assertEquals(originalEnabled, prefs.notificationForwardingQuietHoursEnabled.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun setNotificationForwardingQuietHours_persistsValidDraftsAndEnabledState() {
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
val plainPrefs = context.getSharedPreferences("openclaw.node", Context.MODE_PRIVATE)
|
||||
plainPrefs.edit().clear().commit()
|
||||
|
||||
val prefs = SecurePrefs(context)
|
||||
|
||||
assertTrue(
|
||||
prefs.setNotificationForwardingQuietHours(
|
||||
enabled = true,
|
||||
start = "22:30",
|
||||
end = "06:45",
|
||||
),
|
||||
)
|
||||
|
||||
assertTrue(prefs.notificationForwardingQuietHoursEnabled.value)
|
||||
assertEquals("22:30", prefs.notificationForwardingQuietStart.value)
|
||||
assertEquals("06:45", prefs.notificationForwardingQuietEnd.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun setNotificationForwardingQuietHours_disablesWithoutRevalidatingDrafts() {
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
val plainPrefs = context.getSharedPreferences("openclaw.node", Context.MODE_PRIVATE)
|
||||
plainPrefs.edit().clear().commit()
|
||||
|
||||
val prefs = SecurePrefs(context)
|
||||
assertTrue(
|
||||
prefs.setNotificationForwardingQuietHours(
|
||||
enabled = true,
|
||||
start = "22:30",
|
||||
end = "06:45",
|
||||
),
|
||||
)
|
||||
|
||||
assertTrue(
|
||||
prefs.setNotificationForwardingQuietHours(
|
||||
enabled = false,
|
||||
start = "7:00",
|
||||
end = "06:45",
|
||||
),
|
||||
)
|
||||
|
||||
assertFalse(prefs.notificationForwardingQuietHoursEnabled.value)
|
||||
assertEquals("22:30", prefs.notificationForwardingQuietStart.value)
|
||||
assertEquals("06:45", prefs.notificationForwardingQuietEnd.value)
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun getNotificationForwardingPolicy_readsLatestQuietHoursImmediately() {
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
val plainPrefs = context.getSharedPreferences("openclaw.node", Context.MODE_PRIVATE)
|
||||
plainPrefs.edit().clear().commit()
|
||||
|
||||
val prefs = SecurePrefs(context)
|
||||
assertTrue(
|
||||
prefs.setNotificationForwardingQuietHours(
|
||||
enabled = true,
|
||||
start = "21:15",
|
||||
end = "06:10",
|
||||
),
|
||||
)
|
||||
|
||||
val policy = prefs.getNotificationForwardingPolicy(appPackageName = "ai.openclaw.app")
|
||||
|
||||
assertTrue(policy.quietHoursEnabled)
|
||||
assertEquals("21:15", policy.quietStart)
|
||||
assertEquals("06:10", policy.quietEnd)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun notificationForwarding_defaultsDisabledForSaferPosture() {
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
val plainPrefs = context.getSharedPreferences("openclaw.node", Context.MODE_PRIVATE)
|
||||
plainPrefs.edit().clear().commit()
|
||||
|
||||
val prefs = SecurePrefs(context)
|
||||
val policy = prefs.getNotificationForwardingPolicy(appPackageName = "ai.openclaw.app")
|
||||
|
||||
assertFalse(prefs.notificationForwardingEnabled.value)
|
||||
assertFalse(policy.enabled)
|
||||
assertEquals(NotificationPackageFilterMode.Blocklist, policy.mode)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -173,50 +173,15 @@ class CallLogHandlerTest : NodeHandlerRobolectricTest() {
|
||||
assertTrue(callLogObj.containsKey("number"))
|
||||
assertTrue(callLogObj.containsKey("cachedName"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleCallLogSearch_clampsLimitAndOffsetBeforeSearch() {
|
||||
val source = FakeCallLogDataSource(canRead = true)
|
||||
val handler = CallLogHandler.forTesting(appContext(), source)
|
||||
|
||||
val result = handler.handleCallLogSearch("""{"limit":999,"offset":-5}""")
|
||||
|
||||
assertTrue(result.ok)
|
||||
assertEquals(200, source.lastRequest?.limit)
|
||||
assertEquals(0, source.lastRequest?.offset)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleCallLogSearch_mapsSearchFailuresToUnavailable() {
|
||||
val handler =
|
||||
CallLogHandler.forTesting(
|
||||
appContext(),
|
||||
FakeCallLogDataSource(
|
||||
canRead = true,
|
||||
failure = IllegalStateException("provider down"),
|
||||
),
|
||||
)
|
||||
|
||||
val result = handler.handleCallLogSearch(null)
|
||||
|
||||
assertFalse(result.ok)
|
||||
assertEquals("CALL_LOG_UNAVAILABLE", result.error?.code)
|
||||
assertEquals("CALL_LOG_UNAVAILABLE: provider down", result.error?.message)
|
||||
}
|
||||
}
|
||||
|
||||
private class FakeCallLogDataSource(
|
||||
private val canRead: Boolean,
|
||||
private val searchResults: List<CallLogRecord> = emptyList(),
|
||||
private val failure: Throwable? = null,
|
||||
) : CallLogDataSource {
|
||||
var lastRequest: CallLogSearchRequest? = null
|
||||
|
||||
override fun hasReadPermission(context: Context): Boolean = canRead
|
||||
|
||||
override fun search(context: Context, request: CallLogSearchRequest): List<CallLogRecord> {
|
||||
lastRequest = request
|
||||
failure?.let { throw it }
|
||||
val startIndex = request.offset.coerceAtLeast(0)
|
||||
val endIndex = (startIndex + request.limit).coerceAtMost(searchResults.size)
|
||||
return if (startIndex < searchResults.size) {
|
||||
|
||||
@@ -1,25 +1,10 @@
|
||||
package ai.openclaw.app.node
|
||||
|
||||
import ai.openclaw.app.LocationMode
|
||||
import ai.openclaw.app.SecurePrefs
|
||||
import ai.openclaw.app.VoiceWakeMode
|
||||
import ai.openclaw.app.protocol.OpenClawCallLogCommand
|
||||
import ai.openclaw.app.protocol.OpenClawCameraCommand
|
||||
import ai.openclaw.app.protocol.OpenClawCapability
|
||||
import ai.openclaw.app.protocol.OpenClawLocationCommand
|
||||
import ai.openclaw.app.protocol.OpenClawMotionCommand
|
||||
import ai.openclaw.app.protocol.OpenClawSmsCommand
|
||||
import ai.openclaw.app.gateway.GatewayEndpoint
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class ConnectionManagerTest {
|
||||
@Test
|
||||
fun resolveTlsParamsForEndpoint_prefersStoredPinOverAdvertisedFingerprint() {
|
||||
@@ -88,173 +73,4 @@ class ConnectionManagerTest {
|
||||
assertNull(on?.expectedFingerprint)
|
||||
assertEquals(false, on?.allowTOFU)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildNodeConnectOptions_advertisesRequestableSmsSearchWithoutSmsCapability() {
|
||||
val options =
|
||||
newManager(
|
||||
sendSmsAvailable = false,
|
||||
readSmsAvailable = false,
|
||||
smsSearchPossible = true,
|
||||
).buildNodeConnectOptions()
|
||||
|
||||
assertTrue(options.commands.contains(OpenClawSmsCommand.Search.rawValue))
|
||||
assertFalse(options.commands.contains(OpenClawSmsCommand.Send.rawValue))
|
||||
assertFalse(options.caps.contains(OpenClawCapability.Sms.rawValue))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildNodeConnectOptions_doesNotAdvertiseSmsWhenSearchIsImpossible() {
|
||||
val options =
|
||||
newManager(
|
||||
sendSmsAvailable = false,
|
||||
readSmsAvailable = false,
|
||||
smsSearchPossible = false,
|
||||
).buildNodeConnectOptions()
|
||||
|
||||
assertFalse(options.commands.contains(OpenClawSmsCommand.Search.rawValue))
|
||||
assertFalse(options.commands.contains(OpenClawSmsCommand.Send.rawValue))
|
||||
assertFalse(options.caps.contains(OpenClawCapability.Sms.rawValue))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildNodeConnectOptions_advertisesSmsCapabilityWhenReadSmsIsAvailable() {
|
||||
val options =
|
||||
newManager(
|
||||
sendSmsAvailable = false,
|
||||
readSmsAvailable = true,
|
||||
smsSearchPossible = true,
|
||||
).buildNodeConnectOptions()
|
||||
|
||||
assertTrue(options.commands.contains(OpenClawSmsCommand.Search.rawValue))
|
||||
assertTrue(options.caps.contains(OpenClawCapability.Sms.rawValue))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildNodeConnectOptions_advertisesSmsSendWithoutSearchWhenOnlySendIsAvailable() {
|
||||
val options =
|
||||
newManager(
|
||||
sendSmsAvailable = true,
|
||||
readSmsAvailable = false,
|
||||
smsSearchPossible = false,
|
||||
).buildNodeConnectOptions()
|
||||
|
||||
assertTrue(options.commands.contains(OpenClawSmsCommand.Send.rawValue))
|
||||
assertFalse(options.commands.contains(OpenClawSmsCommand.Search.rawValue))
|
||||
assertTrue(options.caps.contains(OpenClawCapability.Sms.rawValue))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildNodeConnectOptions_advertisesAvailableNonSmsCommandsAndCapabilities() {
|
||||
val options =
|
||||
newManager(
|
||||
cameraEnabled = true,
|
||||
locationMode = LocationMode.WhileUsing,
|
||||
voiceWakeMode = VoiceWakeMode.Always,
|
||||
motionActivityAvailable = true,
|
||||
callLogAvailable = true,
|
||||
hasRecordAudioPermission = true,
|
||||
).buildNodeConnectOptions()
|
||||
|
||||
assertTrue(options.commands.contains(OpenClawCameraCommand.List.rawValue))
|
||||
assertTrue(options.commands.contains(OpenClawLocationCommand.Get.rawValue))
|
||||
assertTrue(options.commands.contains(OpenClawMotionCommand.Activity.rawValue))
|
||||
assertTrue(options.commands.contains(OpenClawCallLogCommand.Search.rawValue))
|
||||
assertTrue(options.caps.contains(OpenClawCapability.Camera.rawValue))
|
||||
assertTrue(options.caps.contains(OpenClawCapability.Location.rawValue))
|
||||
assertTrue(options.caps.contains(OpenClawCapability.Motion.rawValue))
|
||||
assertTrue(options.caps.contains(OpenClawCapability.CallLog.rawValue))
|
||||
assertTrue(options.caps.contains(OpenClawCapability.VoiceWake.rawValue))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildNodeConnectOptions_omitsVoiceWakeWithoutMicrophonePermission() {
|
||||
val options =
|
||||
newManager(
|
||||
voiceWakeMode = VoiceWakeMode.Always,
|
||||
hasRecordAudioPermission = false,
|
||||
).buildNodeConnectOptions()
|
||||
|
||||
assertFalse(options.caps.contains(OpenClawCapability.VoiceWake.rawValue))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildNodeConnectOptions_omitsUnavailableCameraLocationAndCallLogSurfaces() {
|
||||
val options =
|
||||
newManager(
|
||||
cameraEnabled = false,
|
||||
locationMode = LocationMode.Off,
|
||||
callLogAvailable = false,
|
||||
).buildNodeConnectOptions()
|
||||
|
||||
assertFalse(options.commands.contains(OpenClawCameraCommand.List.rawValue))
|
||||
assertFalse(options.commands.contains(OpenClawCameraCommand.Snap.rawValue))
|
||||
assertFalse(options.commands.contains(OpenClawCameraCommand.Clip.rawValue))
|
||||
assertFalse(options.commands.contains(OpenClawLocationCommand.Get.rawValue))
|
||||
assertFalse(options.commands.contains(OpenClawCallLogCommand.Search.rawValue))
|
||||
assertFalse(options.caps.contains(OpenClawCapability.Camera.rawValue))
|
||||
assertFalse(options.caps.contains(OpenClawCapability.Location.rawValue))
|
||||
assertFalse(options.caps.contains(OpenClawCapability.CallLog.rawValue))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildNodeConnectOptions_advertisesOnlyAvailableMotionCommand() {
|
||||
val options =
|
||||
newManager(
|
||||
motionActivityAvailable = false,
|
||||
motionPedometerAvailable = true,
|
||||
).buildNodeConnectOptions()
|
||||
|
||||
assertFalse(options.commands.contains(OpenClawMotionCommand.Activity.rawValue))
|
||||
assertTrue(options.commands.contains(OpenClawMotionCommand.Pedometer.rawValue))
|
||||
assertTrue(options.caps.contains(OpenClawCapability.Motion.rawValue))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildNodeConnectOptions_omitsMotionSurfaceWhenMotionApisUnavailable() {
|
||||
val options =
|
||||
newManager(
|
||||
motionActivityAvailable = false,
|
||||
motionPedometerAvailable = false,
|
||||
).buildNodeConnectOptions()
|
||||
|
||||
assertFalse(options.commands.contains(OpenClawMotionCommand.Activity.rawValue))
|
||||
assertFalse(options.commands.contains(OpenClawMotionCommand.Pedometer.rawValue))
|
||||
assertFalse(options.caps.contains(OpenClawCapability.Motion.rawValue))
|
||||
}
|
||||
|
||||
private fun newManager(
|
||||
cameraEnabled: Boolean = false,
|
||||
locationMode: LocationMode = LocationMode.Off,
|
||||
voiceWakeMode: VoiceWakeMode = VoiceWakeMode.Off,
|
||||
motionActivityAvailable: Boolean = false,
|
||||
motionPedometerAvailable: Boolean = false,
|
||||
sendSmsAvailable: Boolean = false,
|
||||
readSmsAvailable: Boolean = false,
|
||||
smsSearchPossible: Boolean = false,
|
||||
callLogAvailable: Boolean = false,
|
||||
hasRecordAudioPermission: Boolean = false,
|
||||
): ConnectionManager {
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
val prefs =
|
||||
SecurePrefs(
|
||||
context,
|
||||
securePrefsOverride = context.getSharedPreferences("connection-manager-test", android.content.Context.MODE_PRIVATE),
|
||||
)
|
||||
|
||||
return ConnectionManager(
|
||||
prefs = prefs,
|
||||
cameraEnabled = { cameraEnabled },
|
||||
locationMode = { locationMode },
|
||||
voiceWakeMode = { voiceWakeMode },
|
||||
motionActivityAvailable = { motionActivityAvailable },
|
||||
motionPedometerAvailable = { motionPedometerAvailable },
|
||||
sendSmsAvailable = { sendSmsAvailable },
|
||||
readSmsAvailable = { readSmsAvailable },
|
||||
smsSearchPossible = { smsSearchPossible },
|
||||
callLogAvailable = { callLogAvailable },
|
||||
hasRecordAudioPermission = { hasRecordAudioPermission },
|
||||
manualTls = { false },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,131 +101,9 @@ class DeviceHandlerTest {
|
||||
val status = state.getValue("status").jsonPrimitive.content
|
||||
assertTrue(status == "granted" || status == "denied")
|
||||
state.getValue("promptable").jsonPrimitive.boolean
|
||||
if (key == "sms") {
|
||||
val capabilities = state.getValue("capabilities").jsonObject
|
||||
for (capabilityKey in listOf("send", "read")) {
|
||||
val capability = capabilities.getValue(capabilityKey).jsonObject
|
||||
val capabilityStatus = capability.getValue("status").jsonPrimitive.content
|
||||
assertTrue(capabilityStatus == "granted" || capabilityStatus == "denied")
|
||||
capability.getValue("promptable").jsonPrimitive.boolean
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun smsTopLevelStatusTreatsSendOnlyPartialGrantAsGranted() {
|
||||
assertTrue(
|
||||
DeviceHandler.hasAnySmsCapability(
|
||||
smsEnabled = true,
|
||||
telephonyAvailable = true,
|
||||
smsSendGranted = true,
|
||||
smsReadGranted = false,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun smsTopLevelStatusTreatsReadOnlyPartialGrantAsGranted() {
|
||||
assertTrue(
|
||||
DeviceHandler.hasAnySmsCapability(
|
||||
smsEnabled = true,
|
||||
telephonyAvailable = true,
|
||||
smsSendGranted = false,
|
||||
smsReadGranted = true,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun smsTopLevelStatusTreatsNoSmsGrantAsDenied() {
|
||||
assertTrue(
|
||||
!DeviceHandler.hasAnySmsCapability(
|
||||
smsEnabled = true,
|
||||
telephonyAvailable = true,
|
||||
smsSendGranted = false,
|
||||
smsReadGranted = false,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun smsTopLevelStatusTreatsDisabledSmsAsDenied() {
|
||||
assertTrue(
|
||||
!DeviceHandler.hasAnySmsCapability(
|
||||
smsEnabled = false,
|
||||
telephonyAvailable = true,
|
||||
smsSendGranted = true,
|
||||
smsReadGranted = true,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun smsTopLevelStatusTreatsMissingTelephonyAsDenied() {
|
||||
assertTrue(
|
||||
!DeviceHandler.hasAnySmsCapability(
|
||||
smsEnabled = true,
|
||||
telephonyAvailable = false,
|
||||
smsSendGranted = true,
|
||||
smsReadGranted = true,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun smsTopLevelPromptableStaysTrueUntilBothSmsPermissionsAreGranted() {
|
||||
assertTrue(
|
||||
DeviceHandler.isSmsPromptable(
|
||||
smsEnabled = true,
|
||||
telephonyAvailable = true,
|
||||
smsSendGranted = true,
|
||||
smsReadGranted = false,
|
||||
),
|
||||
)
|
||||
assertTrue(
|
||||
!DeviceHandler.isSmsPromptable(
|
||||
smsEnabled = true,
|
||||
telephonyAvailable = true,
|
||||
smsSendGranted = true,
|
||||
smsReadGranted = true,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun smsTopLevelPromptableIsFalseWhenSmsCannotExist() {
|
||||
assertTrue(
|
||||
!DeviceHandler.isSmsPromptable(
|
||||
smsEnabled = false,
|
||||
telephonyAvailable = true,
|
||||
smsSendGranted = false,
|
||||
smsReadGranted = false,
|
||||
),
|
||||
)
|
||||
assertTrue(
|
||||
!DeviceHandler.isSmsPromptable(
|
||||
smsEnabled = true,
|
||||
telephonyAvailable = false,
|
||||
smsSendGranted = false,
|
||||
smsReadGranted = false,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleDevicePermissions_marksCallLogUnpromptableWhenFeatureDisabled() {
|
||||
val handler = DeviceHandler(appContext(), callLogEnabled = false)
|
||||
|
||||
val result = handler.handleDevicePermissions(null)
|
||||
|
||||
assertTrue(result.ok)
|
||||
val payload = parsePayload(result.payloadJson)
|
||||
val callLog = payload.getValue("permissions").jsonObject.getValue("callLog").jsonObject
|
||||
assertEquals("denied", callLog.getValue("status").jsonPrimitive.content)
|
||||
assertTrue(!callLog.getValue("promptable").jsonPrimitive.boolean)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleDeviceHealth_returnsExpectedShape() {
|
||||
val handler = DeviceHandler(appContext())
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
package ai.openclaw.app.node
|
||||
|
||||
import android.content.Context
|
||||
import ai.openclaw.app.NotificationBurstLimiter
|
||||
import ai.openclaw.app.NotificationForwardingPolicy
|
||||
import ai.openclaw.app.NotificationPackageFilterMode
|
||||
import ai.openclaw.app.isWithinQuietHours
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class DeviceNotificationListenerServiceTest {
|
||||
@Test
|
||||
fun recentPackages_migratesLegacyPreferenceKey() {
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
val prefs = context.getSharedPreferences("openclaw.secure", Context.MODE_PRIVATE)
|
||||
prefs.edit()
|
||||
.clear()
|
||||
.putString("notifications.recentPackages", "com.example.one, com.example.two")
|
||||
.commit()
|
||||
|
||||
val packages = DeviceNotificationListenerService.recentPackages(context)
|
||||
|
||||
assertEquals(listOf("com.example.one", "com.example.two"), packages)
|
||||
assertEquals(
|
||||
"com.example.one, com.example.two",
|
||||
prefs.getString("notifications.forwarding.recentPackages", null),
|
||||
)
|
||||
assertFalse(prefs.contains("notifications.recentPackages"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun recentPackages_cleansUpLegacyKeyWhenNewKeyAlreadyExists() {
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
val prefs = context.getSharedPreferences("openclaw.secure", Context.MODE_PRIVATE)
|
||||
prefs.edit()
|
||||
.clear()
|
||||
.putString("notifications.forwarding.recentPackages", "com.example.new")
|
||||
.putString("notifications.recentPackages", "com.example.legacy")
|
||||
.commit()
|
||||
|
||||
val packages = DeviceNotificationListenerService.recentPackages(context)
|
||||
|
||||
assertEquals(listOf("com.example.new"), packages)
|
||||
assertNull(prefs.getString("notifications.recentPackages", null))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun recentPackages_trimsDedupesAndPreservesRecencyOrder() {
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
val prefs = context.getSharedPreferences("openclaw.secure", Context.MODE_PRIVATE)
|
||||
prefs.edit()
|
||||
.clear()
|
||||
.putString(
|
||||
"notifications.forwarding.recentPackages",
|
||||
" com.example.recent , ,com.example.other,com.example.recent, com.example.third ",
|
||||
)
|
||||
.commit()
|
||||
|
||||
val packages = DeviceNotificationListenerService.recentPackages(context)
|
||||
|
||||
assertEquals(
|
||||
listOf("com.example.recent", "com.example.other", "com.example.third"),
|
||||
packages,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun quietHoursAndRateLimitingUseWallClockTimeNotNotificationPostTime() {
|
||||
val zone = java.time.ZoneId.systemDefault()
|
||||
val now = java.time.ZonedDateTime.now(zone)
|
||||
val quietStart = now.minusMinutes(5).toLocalTime().withSecond(0).withNano(0)
|
||||
val quietEnd = now.plusMinutes(5).toLocalTime().withSecond(0).withNano(0)
|
||||
val stalePostTime =
|
||||
now
|
||||
.minusHours(2)
|
||||
.withMinute(0)
|
||||
.withSecond(0)
|
||||
.withNano(0)
|
||||
.toInstant()
|
||||
.toEpochMilli()
|
||||
|
||||
val policy =
|
||||
NotificationForwardingPolicy(
|
||||
enabled = true,
|
||||
mode = NotificationPackageFilterMode.Blocklist,
|
||||
packages = emptySet(),
|
||||
quietHoursEnabled = true,
|
||||
quietStart = "%02d:%02d".format(quietStart.hour, quietStart.minute),
|
||||
quietEnd = "%02d:%02d".format(quietEnd.hour, quietEnd.minute),
|
||||
maxEventsPerMinute = 1,
|
||||
sessionKey = null,
|
||||
)
|
||||
|
||||
assertFalse(policy.isWithinQuietHours(nowEpochMs = stalePostTime, zoneId = zone))
|
||||
assertTrue(policy.isWithinQuietHours(nowEpochMs = System.currentTimeMillis(), zoneId = zone))
|
||||
|
||||
val limiter = NotificationBurstLimiter()
|
||||
assertTrue(limiter.allow(nowEpochMs = stalePostTime, maxEventsPerMinute = 1))
|
||||
assertTrue(limiter.allow(nowEpochMs = System.currentTimeMillis(), maxEventsPerMinute = 1))
|
||||
assertFalse(limiter.allow(nowEpochMs = System.currentTimeMillis(), maxEventsPerMinute = 1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun burstLimiter_capsAnyForwardedNotificationEvent() {
|
||||
val limiter = NotificationBurstLimiter()
|
||||
val nowEpochMs = System.currentTimeMillis()
|
||||
|
||||
assertTrue(limiter.allow(nowEpochMs = nowEpochMs, maxEventsPerMinute = 2))
|
||||
assertTrue(limiter.allow(nowEpochMs = nowEpochMs, maxEventsPerMinute = 2))
|
||||
assertFalse(limiter.allow(nowEpochMs = nowEpochMs, maxEventsPerMinute = 2))
|
||||
}
|
||||
}
|
||||
@@ -12,9 +12,6 @@ import ai.openclaw.app.protocol.OpenClawNotificationsCommand
|
||||
import ai.openclaw.app.protocol.OpenClawPhotosCommand
|
||||
import ai.openclaw.app.protocol.OpenClawSmsCommand
|
||||
import ai.openclaw.app.protocol.OpenClawSystemCommand
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
@@ -89,7 +86,6 @@ class InvokeCommandRegistryTest {
|
||||
locationEnabled = true,
|
||||
sendSmsAvailable = true,
|
||||
readSmsAvailable = true,
|
||||
smsSearchPossible = true,
|
||||
callLogAvailable = true,
|
||||
voiceWakeEnabled = true,
|
||||
motionActivityAvailable = true,
|
||||
@@ -117,7 +113,6 @@ class InvokeCommandRegistryTest {
|
||||
locationEnabled = true,
|
||||
sendSmsAvailable = true,
|
||||
readSmsAvailable = true,
|
||||
smsSearchPossible = true,
|
||||
callLogAvailable = true,
|
||||
motionActivityAvailable = true,
|
||||
motionPedometerAvailable = true,
|
||||
@@ -137,7 +132,6 @@ class InvokeCommandRegistryTest {
|
||||
locationEnabled = false,
|
||||
sendSmsAvailable = false,
|
||||
readSmsAvailable = false,
|
||||
smsSearchPossible = false,
|
||||
callLogAvailable = false,
|
||||
voiceWakeEnabled = false,
|
||||
motionActivityAvailable = true,
|
||||
@@ -154,22 +148,17 @@ class InvokeCommandRegistryTest {
|
||||
fun advertisedCommands_splitsSmsSendAndSearchAvailability() {
|
||||
val readOnlyCommands =
|
||||
InvokeCommandRegistry.advertisedCommands(
|
||||
defaultFlags(readSmsAvailable = true, smsSearchPossible = true),
|
||||
defaultFlags(readSmsAvailable = true),
|
||||
)
|
||||
val sendOnlyCommands =
|
||||
InvokeCommandRegistry.advertisedCommands(
|
||||
defaultFlags(sendSmsAvailable = true),
|
||||
)
|
||||
val requestableSearchCommands =
|
||||
InvokeCommandRegistry.advertisedCommands(
|
||||
defaultFlags(smsSearchPossible = true),
|
||||
)
|
||||
|
||||
assertTrue(readOnlyCommands.contains(OpenClawSmsCommand.Search.rawValue))
|
||||
assertFalse(readOnlyCommands.contains(OpenClawSmsCommand.Send.rawValue))
|
||||
assertTrue(sendOnlyCommands.contains(OpenClawSmsCommand.Send.rawValue))
|
||||
assertFalse(sendOnlyCommands.contains(OpenClawSmsCommand.Search.rawValue))
|
||||
assertTrue(requestableSearchCommands.contains(OpenClawSmsCommand.Search.rawValue))
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -182,14 +171,9 @@ class InvokeCommandRegistryTest {
|
||||
InvokeCommandRegistry.advertisedCapabilities(
|
||||
defaultFlags(sendSmsAvailable = true),
|
||||
)
|
||||
val requestableSearchCapabilities =
|
||||
InvokeCommandRegistry.advertisedCapabilities(
|
||||
defaultFlags(smsSearchPossible = true),
|
||||
)
|
||||
|
||||
assertTrue(readOnlyCapabilities.contains(OpenClawCapability.Sms.rawValue))
|
||||
assertTrue(sendOnlyCapabilities.contains(OpenClawCapability.Sms.rawValue))
|
||||
assertFalse(requestableSearchCapabilities.contains(OpenClawCapability.Sms.rawValue))
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -206,37 +190,11 @@ class InvokeCommandRegistryTest {
|
||||
assertFalse(capabilities.contains(OpenClawCapability.CallLog.rawValue))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun advertisedCapabilities_includesVoiceWakeWithoutAdvertisingCommands() {
|
||||
val capabilities = InvokeCommandRegistry.advertisedCapabilities(defaultFlags(voiceWakeEnabled = true))
|
||||
val commands = InvokeCommandRegistry.advertisedCommands(defaultFlags(voiceWakeEnabled = true))
|
||||
|
||||
assertTrue(capabilities.contains(OpenClawCapability.VoiceWake.rawValue))
|
||||
assertFalse(commands.any { it.contains("voice", ignoreCase = true) })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun find_returnsForegroundMetadataForCameraCommands() {
|
||||
val list = InvokeCommandRegistry.find(OpenClawCameraCommand.List.rawValue)
|
||||
val location = InvokeCommandRegistry.find(OpenClawLocationCommand.Get.rawValue)
|
||||
|
||||
assertNotNull(list)
|
||||
assertEquals(true, list?.requiresForeground)
|
||||
assertNotNull(location)
|
||||
assertEquals(false, location?.requiresForeground)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun find_returnsNullForUnknownCommand() {
|
||||
assertNull(InvokeCommandRegistry.find("not.real"))
|
||||
}
|
||||
|
||||
private fun defaultFlags(
|
||||
cameraEnabled: Boolean = false,
|
||||
locationEnabled: Boolean = false,
|
||||
sendSmsAvailable: Boolean = false,
|
||||
readSmsAvailable: Boolean = false,
|
||||
smsSearchPossible: Boolean = false,
|
||||
callLogAvailable: Boolean = false,
|
||||
voiceWakeEnabled: Boolean = false,
|
||||
motionActivityAvailable: Boolean = false,
|
||||
@@ -248,7 +206,6 @@ class InvokeCommandRegistryTest {
|
||||
locationEnabled = locationEnabled,
|
||||
sendSmsAvailable = sendSmsAvailable,
|
||||
readSmsAvailable = readSmsAvailable,
|
||||
smsSearchPossible = smsSearchPossible,
|
||||
callLogAvailable = callLogAvailable,
|
||||
voiceWakeEnabled = voiceWakeEnabled,
|
||||
motionActivityAvailable = motionActivityAvailable,
|
||||
|
||||
@@ -1,368 +0,0 @@
|
||||
package ai.openclaw.app.node
|
||||
|
||||
import ai.openclaw.app.gateway.DeviceIdentityStore
|
||||
import ai.openclaw.app.gateway.GatewaySession
|
||||
import ai.openclaw.app.protocol.OpenClawCallLogCommand
|
||||
import ai.openclaw.app.protocol.OpenClawCameraCommand
|
||||
import ai.openclaw.app.protocol.OpenClawLocationCommand
|
||||
import ai.openclaw.app.protocol.OpenClawMotionCommand
|
||||
import ai.openclaw.app.protocol.OpenClawSmsCommand
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class InvokeDispatcherTest {
|
||||
@Test
|
||||
fun classifySmsSearchAvailability_returnsAvailable_whenReadSmsIsAvailable() {
|
||||
assertEquals(
|
||||
SmsSearchAvailabilityReason.Available,
|
||||
classifySmsSearchAvailability(
|
||||
readSmsAvailable = true,
|
||||
smsFeatureEnabled = true,
|
||||
smsTelephonyAvailable = true,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun classifySmsSearchAvailability_returnsUnavailable_whenSmsFeatureDisabled() {
|
||||
assertEquals(
|
||||
SmsSearchAvailabilityReason.Unavailable,
|
||||
classifySmsSearchAvailability(
|
||||
readSmsAvailable = false,
|
||||
smsFeatureEnabled = false,
|
||||
smsTelephonyAvailable = true,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun classifySmsSearchAvailability_returnsUnavailable_whenTelephonyUnavailable() {
|
||||
assertEquals(
|
||||
SmsSearchAvailabilityReason.Unavailable,
|
||||
classifySmsSearchAvailability(
|
||||
readSmsAvailable = false,
|
||||
smsFeatureEnabled = true,
|
||||
smsTelephonyAvailable = false,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun classifySmsSearchAvailability_returnsPermissionRequired_whenOnlyReadSmsPermissionIsMissing() {
|
||||
assertEquals(
|
||||
SmsSearchAvailabilityReason.PermissionRequired,
|
||||
classifySmsSearchAvailability(
|
||||
readSmsAvailable = false,
|
||||
smsFeatureEnabled = true,
|
||||
smsTelephonyAvailable = true,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun smsSearchAvailabilityError_returnsNull_whenReadSmsPermissionIsRequestable() {
|
||||
assertNull(
|
||||
smsSearchAvailabilityError(
|
||||
readSmsAvailable = false,
|
||||
smsFeatureEnabled = true,
|
||||
smsTelephonyAvailable = true,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun smsSearchAvailabilityError_returnsUnavailable_whenSmsSearchIsImpossible() {
|
||||
val result =
|
||||
smsSearchAvailabilityError(
|
||||
readSmsAvailable = false,
|
||||
smsFeatureEnabled = false,
|
||||
smsTelephonyAvailable = true,
|
||||
)
|
||||
|
||||
assertEquals("SMS_UNAVAILABLE", result?.error?.code)
|
||||
assertEquals("SMS_UNAVAILABLE: SMS not available on this device", result?.error?.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleInvoke_allowsRequestableSmsSearchToReachHandler() =
|
||||
runTest {
|
||||
val result =
|
||||
newDispatcher(
|
||||
readSmsAvailable = false,
|
||||
smsFeatureEnabled = true,
|
||||
smsTelephonyAvailable = true,
|
||||
).handleInvoke(OpenClawSmsCommand.Search.rawValue, "not-json")
|
||||
|
||||
assertEquals("SMS_PERMISSION_REQUIRED", result.error?.code)
|
||||
assertEquals("grant READ_SMS permission", result.error?.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleInvoke_blocksSmsSearchWhenFeatureIsUnavailable() =
|
||||
runTest {
|
||||
val result =
|
||||
newDispatcher(
|
||||
readSmsAvailable = false,
|
||||
smsFeatureEnabled = false,
|
||||
smsTelephonyAvailable = true,
|
||||
).handleInvoke(OpenClawSmsCommand.Search.rawValue, "not-json")
|
||||
|
||||
assertEquals("SMS_UNAVAILABLE", result.error?.code)
|
||||
assertEquals("SMS_UNAVAILABLE: SMS not available on this device", result.error?.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleInvoke_allowsAvailableSmsSendToReachHandler() =
|
||||
runTest {
|
||||
val result =
|
||||
newDispatcher(
|
||||
sendSmsAvailable = true,
|
||||
smsFeatureEnabled = true,
|
||||
smsTelephonyAvailable = true,
|
||||
).handleInvoke(OpenClawSmsCommand.Send.rawValue, """{"to":"+15551234567","message":"hi"}""")
|
||||
|
||||
assertEquals("SMS_PERMISSION_REQUIRED", result.error?.code)
|
||||
assertEquals("grant SMS permission", result.error?.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleInvoke_blocksSmsSendWhenUnavailable() =
|
||||
runTest {
|
||||
val result =
|
||||
newDispatcher(
|
||||
sendSmsAvailable = false,
|
||||
smsFeatureEnabled = true,
|
||||
smsTelephonyAvailable = true,
|
||||
).handleInvoke(OpenClawSmsCommand.Send.rawValue, """{"to":"+15551234567","message":"hi"}""")
|
||||
|
||||
assertEquals("SMS_UNAVAILABLE", result.error?.code)
|
||||
assertEquals("SMS_UNAVAILABLE: SMS not available on this device", result.error?.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleInvoke_blocksCameraCommandsWhenCameraDisabled() =
|
||||
runTest {
|
||||
val result = newDispatcher(cameraEnabled = false).handleInvoke(OpenClawCameraCommand.List.rawValue, null)
|
||||
|
||||
assertEquals("CAMERA_DISABLED", result.error?.code)
|
||||
assertEquals("CAMERA_DISABLED: enable Camera in Settings", result.error?.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleInvoke_blocksLocationCommandWhenLocationDisabled() =
|
||||
runTest {
|
||||
val result = newDispatcher(locationEnabled = false).handleInvoke(OpenClawLocationCommand.Get.rawValue, null)
|
||||
|
||||
assertEquals("LOCATION_DISABLED", result.error?.code)
|
||||
assertEquals("LOCATION_DISABLED: enable Location in Settings", result.error?.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleInvoke_blocksMotionActivityWhenUnavailable() =
|
||||
runTest {
|
||||
val result =
|
||||
newDispatcher(motionActivityAvailable = false)
|
||||
.handleInvoke(OpenClawMotionCommand.Activity.rawValue, null)
|
||||
|
||||
assertEquals("MOTION_UNAVAILABLE", result.error?.code)
|
||||
assertEquals("MOTION_UNAVAILABLE: accelerometer not available", result.error?.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleInvoke_blocksMotionPedometerWhenUnavailable() =
|
||||
runTest {
|
||||
val result =
|
||||
newDispatcher(motionPedometerAvailable = false)
|
||||
.handleInvoke(OpenClawMotionCommand.Pedometer.rawValue, null)
|
||||
|
||||
assertEquals("PEDOMETER_UNAVAILABLE", result.error?.code)
|
||||
assertEquals("PEDOMETER_UNAVAILABLE: step counter not available", result.error?.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleInvoke_blocksCallLogWhenUnavailable() =
|
||||
runTest {
|
||||
val result =
|
||||
newDispatcher(callLogAvailable = false).handleInvoke(OpenClawCallLogCommand.Search.rawValue, null)
|
||||
|
||||
assertEquals("CALL_LOG_UNAVAILABLE", result.error?.code)
|
||||
assertEquals("CALL_LOG_UNAVAILABLE: call log not available on this build", result.error?.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleInvoke_treatsDebugCommandsAsUnknownOutsideDebugBuilds() =
|
||||
runTest {
|
||||
val result = newDispatcher(debugBuild = false).handleInvoke("debug.logs", null)
|
||||
|
||||
assertEquals("INVALID_REQUEST", result.error?.code)
|
||||
assertEquals("INVALID_REQUEST: unknown command", result.error?.message)
|
||||
}
|
||||
|
||||
private fun newDispatcher(
|
||||
cameraEnabled: Boolean = false,
|
||||
locationEnabled: Boolean = false,
|
||||
sendSmsAvailable: Boolean = false,
|
||||
readSmsAvailable: Boolean = false,
|
||||
smsFeatureEnabled: Boolean = true,
|
||||
smsTelephonyAvailable: Boolean = true,
|
||||
callLogAvailable: Boolean = false,
|
||||
debugBuild: Boolean = false,
|
||||
motionActivityAvailable: Boolean = false,
|
||||
motionPedometerAvailable: Boolean = false,
|
||||
): InvokeDispatcher {
|
||||
val appContext = RuntimeEnvironment.getApplication()
|
||||
shadowOf(appContext.packageManager).setSystemFeature(PackageManager.FEATURE_TELEPHONY, smsTelephonyAvailable)
|
||||
val canvas = CanvasController()
|
||||
return InvokeDispatcher(
|
||||
canvas = canvas,
|
||||
cameraHandler = newCameraHandler(appContext),
|
||||
locationHandler =
|
||||
LocationHandler.forTesting(
|
||||
appContext = appContext,
|
||||
dataSource = InvokeDispatcherFakeLocationDataSource(),
|
||||
),
|
||||
deviceHandler = DeviceHandler(appContext),
|
||||
notificationsHandler =
|
||||
NotificationsHandler.forTesting(
|
||||
appContext = appContext,
|
||||
stateProvider = InvokeDispatcherFakeNotificationsStateProvider(),
|
||||
),
|
||||
systemHandler = SystemHandler.forTesting(InvokeDispatcherFakeSystemNotificationPoster()),
|
||||
photosHandler = PhotosHandler.forTesting(appContext, InvokeDispatcherFakePhotosDataSource()),
|
||||
contactsHandler = ContactsHandler.forTesting(appContext, InvokeDispatcherFakeContactsDataSource()),
|
||||
calendarHandler = CalendarHandler.forTesting(appContext, InvokeDispatcherFakeCalendarDataSource()),
|
||||
motionHandler = MotionHandler.forTesting(appContext, InvokeDispatcherFakeMotionDataSource()),
|
||||
smsHandler = SmsHandler(SmsManager(appContext)),
|
||||
a2uiHandler =
|
||||
A2UIHandler(
|
||||
canvas = canvas,
|
||||
json = Json { ignoreUnknownKeys = true },
|
||||
getNodeCanvasHostUrl = { null },
|
||||
getOperatorCanvasHostUrl = { null },
|
||||
),
|
||||
debugHandler = DebugHandler(appContext, DeviceIdentityStore(appContext)),
|
||||
callLogHandler = CallLogHandler.forTesting(appContext, InvokeDispatcherFakeCallLogDataSource()),
|
||||
isForeground = { true },
|
||||
cameraEnabled = { cameraEnabled },
|
||||
locationEnabled = { locationEnabled },
|
||||
sendSmsAvailable = { sendSmsAvailable },
|
||||
readSmsAvailable = { readSmsAvailable },
|
||||
smsFeatureEnabled = { smsFeatureEnabled },
|
||||
smsTelephonyAvailable = { smsTelephonyAvailable },
|
||||
callLogAvailable = { callLogAvailable },
|
||||
debugBuild = { debugBuild },
|
||||
refreshNodeCanvasCapability = { false },
|
||||
onCanvasA2uiPush = {},
|
||||
onCanvasA2uiReset = {},
|
||||
motionActivityAvailable = { motionActivityAvailable },
|
||||
motionPedometerAvailable = { motionPedometerAvailable },
|
||||
)
|
||||
}
|
||||
|
||||
private fun newCameraHandler(appContext: Context): CameraHandler {
|
||||
return CameraHandler(
|
||||
appContext = appContext,
|
||||
camera = CameraCaptureManager(appContext),
|
||||
externalAudioCaptureActive = MutableStateFlow(false),
|
||||
showCameraHud = { _, _, _ -> },
|
||||
triggerCameraFlash = {},
|
||||
invokeErrorFromThrowable = { err -> "UNAVAILABLE" to (err.message ?: "camera failed") },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private class InvokeDispatcherFakeLocationDataSource : LocationDataSource {
|
||||
override fun hasFinePermission(context: Context): Boolean = false
|
||||
|
||||
override fun hasCoarsePermission(context: Context): Boolean = false
|
||||
|
||||
override suspend fun fetchLocation(
|
||||
desiredProviders: List<String>,
|
||||
maxAgeMs: Long?,
|
||||
timeoutMs: Long,
|
||||
isPrecise: Boolean,
|
||||
): LocationCaptureManager.Payload {
|
||||
error("unused in InvokeDispatcherTest")
|
||||
}
|
||||
}
|
||||
|
||||
private class InvokeDispatcherFakeNotificationsStateProvider : NotificationsStateProvider {
|
||||
override fun readSnapshot(context: Context): DeviceNotificationSnapshot {
|
||||
return DeviceNotificationSnapshot(enabled = false, connected = false, notifications = emptyList())
|
||||
}
|
||||
|
||||
override fun requestServiceRebind(context: Context) = Unit
|
||||
|
||||
override fun executeAction(context: Context, request: NotificationActionRequest): NotificationActionResult {
|
||||
return NotificationActionResult(ok = true, code = null, message = null)
|
||||
}
|
||||
}
|
||||
|
||||
private class InvokeDispatcherFakeSystemNotificationPoster : SystemNotificationPoster {
|
||||
override fun isAuthorized(): Boolean = true
|
||||
|
||||
override fun post(request: SystemNotifyRequest) = Unit
|
||||
}
|
||||
|
||||
private class InvokeDispatcherFakePhotosDataSource : PhotosDataSource {
|
||||
override fun hasPermission(context: Context): Boolean = true
|
||||
|
||||
override fun latest(context: Context, request: PhotosLatestRequest): List<EncodedPhotoPayload> = emptyList()
|
||||
}
|
||||
|
||||
private class InvokeDispatcherFakeContactsDataSource : ContactsDataSource {
|
||||
override fun hasReadPermission(context: Context): Boolean = true
|
||||
|
||||
override fun hasWritePermission(context: Context): Boolean = true
|
||||
|
||||
override fun search(context: Context, request: ContactsSearchRequest): List<ContactRecord> = emptyList()
|
||||
|
||||
override fun add(context: Context, request: ContactsAddRequest): ContactRecord {
|
||||
error("unused in InvokeDispatcherTest")
|
||||
}
|
||||
}
|
||||
|
||||
private class InvokeDispatcherFakeCalendarDataSource : CalendarDataSource {
|
||||
override fun hasReadPermission(context: Context): Boolean = true
|
||||
|
||||
override fun hasWritePermission(context: Context): Boolean = true
|
||||
|
||||
override fun events(context: Context, request: CalendarEventsRequest): List<CalendarEventRecord> = emptyList()
|
||||
|
||||
override fun add(context: Context, request: CalendarAddRequest): CalendarEventRecord {
|
||||
error("unused in InvokeDispatcherTest")
|
||||
}
|
||||
}
|
||||
|
||||
private class InvokeDispatcherFakeMotionDataSource : MotionDataSource {
|
||||
override fun isActivityAvailable(context: Context): Boolean = false
|
||||
|
||||
override fun isPedometerAvailable(context: Context): Boolean = false
|
||||
|
||||
override fun hasPermission(context: Context): Boolean = true
|
||||
|
||||
override suspend fun activity(context: Context, request: MotionActivityRequest): MotionActivityRecord {
|
||||
error("unused in InvokeDispatcherTest")
|
||||
}
|
||||
|
||||
override suspend fun pedometer(context: Context, request: MotionPedometerRequest): PedometerRecord {
|
||||
error("unused in InvokeDispatcherTest")
|
||||
}
|
||||
}
|
||||
|
||||
private class InvokeDispatcherFakeCallLogDataSource : CallLogDataSource {
|
||||
override fun hasReadPermission(context: Context): Boolean = true
|
||||
|
||||
override fun search(context: Context, request: CallLogSearchRequest): List<CallLogRecord> = emptyList()
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
package ai.openclaw.app.node
|
||||
|
||||
import android.content.Context
|
||||
import android.location.LocationManager
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
@@ -67,110 +65,12 @@ class LocationHandlerTest : NodeHandlerRobolectricTest() {
|
||||
assertTrue(granted.hasFineLocationPermission())
|
||||
assertFalse(granted.hasCoarseLocationPermission())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleLocationGet_usesPreciseGpsFirstWhenFinePermissionAndPreciseEnabled() =
|
||||
runTest {
|
||||
val source =
|
||||
FakeLocationDataSource(
|
||||
fineGranted = true,
|
||||
coarseGranted = true,
|
||||
payload = LocationCaptureManager.Payload("""{"ok":true}"""),
|
||||
)
|
||||
val handler =
|
||||
LocationHandler.forTesting(
|
||||
appContext = appContext(),
|
||||
dataSource = source,
|
||||
locationPreciseEnabled = { true },
|
||||
)
|
||||
|
||||
val result = handler.handleLocationGet("""{"desiredAccuracy":"precise","maxAgeMs":1234,"timeoutMs":2000}""")
|
||||
|
||||
assertTrue(result.ok)
|
||||
assertEquals(listOf(LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER), source.lastDesiredProviders)
|
||||
assertEquals(1234L, source.lastMaxAgeMs)
|
||||
assertEquals(2000L, source.lastTimeoutMs)
|
||||
assertTrue(source.lastIsPrecise)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleLocationGet_fallsBackToBalancedWhenPreciseUnavailable() =
|
||||
runTest {
|
||||
val source =
|
||||
FakeLocationDataSource(
|
||||
fineGranted = false,
|
||||
coarseGranted = true,
|
||||
payload = LocationCaptureManager.Payload("""{"ok":true}"""),
|
||||
)
|
||||
val handler =
|
||||
LocationHandler.forTesting(
|
||||
appContext = appContext(),
|
||||
dataSource = source,
|
||||
locationPreciseEnabled = { true },
|
||||
)
|
||||
|
||||
val result = handler.handleLocationGet("""{"desiredAccuracy":"precise"}""")
|
||||
|
||||
assertTrue(result.ok)
|
||||
assertEquals(listOf(LocationManager.NETWORK_PROVIDER, LocationManager.GPS_PROVIDER), source.lastDesiredProviders)
|
||||
assertFalse(source.lastIsPrecise)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleLocationGet_mapsTimeoutToLocationTimeout() =
|
||||
runTest {
|
||||
val handler =
|
||||
LocationHandler.forTesting(
|
||||
appContext = appContext(),
|
||||
dataSource =
|
||||
FakeLocationDataSource(
|
||||
fineGranted = true,
|
||||
coarseGranted = true,
|
||||
timeout = true,
|
||||
),
|
||||
)
|
||||
|
||||
val result = handler.handleLocationGet(null)
|
||||
|
||||
assertFalse(result.ok)
|
||||
assertEquals("LOCATION_TIMEOUT", result.error?.code)
|
||||
assertEquals("LOCATION_TIMEOUT: no fix in time", result.error?.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleLocationGet_mapsOtherFailuresToLocationUnavailable() =
|
||||
runTest {
|
||||
val handler =
|
||||
LocationHandler.forTesting(
|
||||
appContext = appContext(),
|
||||
dataSource =
|
||||
FakeLocationDataSource(
|
||||
fineGranted = true,
|
||||
coarseGranted = true,
|
||||
failure = IllegalStateException("gps offline"),
|
||||
),
|
||||
)
|
||||
|
||||
val result = handler.handleLocationGet(null)
|
||||
|
||||
assertFalse(result.ok)
|
||||
assertEquals("LOCATION_UNAVAILABLE", result.error?.code)
|
||||
assertEquals("gps offline", result.error?.message)
|
||||
}
|
||||
}
|
||||
|
||||
private class FakeLocationDataSource(
|
||||
private val fineGranted: Boolean,
|
||||
private val coarseGranted: Boolean,
|
||||
private val payload: LocationCaptureManager.Payload? = null,
|
||||
private val failure: Throwable? = null,
|
||||
private val timeout: Boolean = false,
|
||||
) : LocationDataSource {
|
||||
var lastDesiredProviders: List<String> = emptyList()
|
||||
var lastMaxAgeMs: Long? = null
|
||||
var lastTimeoutMs: Long? = null
|
||||
var lastIsPrecise: Boolean = false
|
||||
|
||||
override fun hasFinePermission(context: Context): Boolean = fineGranted
|
||||
|
||||
override fun hasCoarsePermission(context: Context): Boolean = coarseGranted
|
||||
@@ -181,16 +81,8 @@ private class FakeLocationDataSource(
|
||||
timeoutMs: Long,
|
||||
isPrecise: Boolean,
|
||||
): LocationCaptureManager.Payload {
|
||||
lastDesiredProviders = desiredProviders
|
||||
lastMaxAgeMs = maxAgeMs
|
||||
lastTimeoutMs = timeoutMs
|
||||
lastIsPrecise = isPrecise
|
||||
if (timeout) {
|
||||
kotlinx.coroutines.withTimeout(1) {
|
||||
kotlinx.coroutines.delay(5)
|
||||
}
|
||||
}
|
||||
failure?.let { throw it }
|
||||
return payload ?: LocationCaptureManager.Payload(Json.encodeToString(mapOf("ok" to true)))
|
||||
throw IllegalStateException(
|
||||
"LocationHandlerTest: fetchLocation must not run in this scenario",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,46 +140,6 @@ class NotificationsHandlerTest {
|
||||
assertEquals(0, provider.actionRequests)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun notificationsActions_rejectsMissingKey() =
|
||||
runTest {
|
||||
val provider =
|
||||
FakeNotificationsStateProvider(
|
||||
DeviceNotificationSnapshot(
|
||||
enabled = true,
|
||||
connected = true,
|
||||
notifications = listOf(sampleEntry("n3")),
|
||||
),
|
||||
)
|
||||
val handler = NotificationsHandler.forTesting(appContext = appContext(), stateProvider = provider)
|
||||
|
||||
val result = handler.handleNotificationsActions("""{"action":"open"}""")
|
||||
|
||||
assertFalse(result.ok)
|
||||
assertEquals("INVALID_REQUEST", result.error?.code)
|
||||
assertEquals(0, provider.actionRequests)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun notificationsActions_rejectsInvalidAction() =
|
||||
runTest {
|
||||
val provider =
|
||||
FakeNotificationsStateProvider(
|
||||
DeviceNotificationSnapshot(
|
||||
enabled = true,
|
||||
connected = true,
|
||||
notifications = listOf(sampleEntry("n3")),
|
||||
),
|
||||
)
|
||||
val handler = NotificationsHandler.forTesting(appContext = appContext(), stateProvider = provider)
|
||||
|
||||
val result = handler.handleNotificationsActions("""{"key":"n3","action":"archive"}""")
|
||||
|
||||
assertFalse(result.ok)
|
||||
assertEquals("INVALID_REQUEST", result.error?.code)
|
||||
assertEquals(0, provider.actionRequests)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun notificationsActions_propagatesProviderError() =
|
||||
runTest {
|
||||
@@ -207,29 +167,6 @@ class NotificationsHandlerTest {
|
||||
assertEquals(1, provider.actionRequests)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun notificationsActions_fallsBackWhenProviderOmitsErrorDetails() =
|
||||
runTest {
|
||||
val provider =
|
||||
FakeNotificationsStateProvider(
|
||||
DeviceNotificationSnapshot(
|
||||
enabled = true,
|
||||
connected = true,
|
||||
notifications = listOf(sampleEntry("n4")),
|
||||
),
|
||||
).also {
|
||||
it.actionResult = NotificationActionResult(ok = false)
|
||||
}
|
||||
val handler = NotificationsHandler.forTesting(appContext = appContext(), stateProvider = provider)
|
||||
|
||||
val result = handler.handleNotificationsActions("""{"key":"n4","action":"open"}""")
|
||||
|
||||
assertFalse(result.ok)
|
||||
assertEquals("UNAVAILABLE", result.error?.code)
|
||||
assertEquals("notification action failed", result.error?.message)
|
||||
assertEquals(1, provider.actionRequests)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun notificationsActions_requestsRebindWhenEnabledButDisconnected() =
|
||||
runTest {
|
||||
|
||||
@@ -1,39 +1,15 @@
|
||||
package ai.openclaw.app.node
|
||||
|
||||
import kotlinx.serialization.json.jsonArray
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class SmsManagerTest {
|
||||
private val json = SmsManager.JsonConfig
|
||||
|
||||
private fun smsMessage(
|
||||
id: Long,
|
||||
date: Long,
|
||||
status: Int = 0,
|
||||
body: String? = "msg-$id",
|
||||
transportType: String? = null,
|
||||
): SmsManager.SmsMessage =
|
||||
SmsManager.SmsMessage(
|
||||
id = id,
|
||||
threadId = 1L,
|
||||
address = "+15551234567",
|
||||
person = null,
|
||||
date = date,
|
||||
dateSent = date,
|
||||
read = true,
|
||||
type = 1,
|
||||
body = body,
|
||||
status = status,
|
||||
transportType = transportType,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun parseParamsRejectsEmptyPayload() {
|
||||
val result = SmsManager.parseParams("", json)
|
||||
@@ -85,73 +61,6 @@ class SmsManagerTest {
|
||||
assertEquals("Hello", ok.params.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseQueryParamsDefaultsWhenPayloadEmpty() {
|
||||
val result = SmsManager.parseQueryParams(null, json)
|
||||
assertTrue(result is SmsManager.QueryParseResult.Ok)
|
||||
val ok = result as SmsManager.QueryParseResult.Ok
|
||||
assertEquals(25, ok.params.limit)
|
||||
assertEquals(0, ok.params.offset)
|
||||
assertEquals(null, ok.params.startTime)
|
||||
assertEquals(null, ok.params.endTime)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseQueryParamsRejectsInvalidJson() {
|
||||
val result = SmsManager.parseQueryParams("not-json", json)
|
||||
assertTrue(result is SmsManager.QueryParseResult.Error)
|
||||
val error = result as SmsManager.QueryParseResult.Error
|
||||
assertEquals("INVALID_REQUEST: expected JSON object", error.error)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseQueryParamsRejectsInvertedTimeRange() {
|
||||
val result = SmsManager.parseQueryParams("{\"startTime\":200,\"endTime\":100}", json)
|
||||
assertTrue(result is SmsManager.QueryParseResult.Error)
|
||||
val error = result as SmsManager.QueryParseResult.Error
|
||||
assertEquals("INVALID_REQUEST: startTime must be less than or equal to endTime", error.error)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseQueryParamsClampsLimitAndOffset() {
|
||||
val result = SmsManager.parseQueryParams("{\"limit\":999,\"offset\":-5}", json)
|
||||
assertTrue(result is SmsManager.QueryParseResult.Ok)
|
||||
val ok = result as SmsManager.QueryParseResult.Ok
|
||||
assertEquals(200, ok.params.limit)
|
||||
assertEquals(0, ok.params.offset)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseQueryParamsParsesAllSupportedFields() {
|
||||
val result = SmsManager.parseQueryParams(
|
||||
"""
|
||||
{
|
||||
"startTime": 100,
|
||||
"endTime": 200,
|
||||
"contactName": " Leah ",
|
||||
"phoneNumber": " +1555 ",
|
||||
"keyword": " ping ",
|
||||
"type": 1,
|
||||
"isRead": true,
|
||||
"limit": 10,
|
||||
"offset": 2
|
||||
}
|
||||
""".trimIndent(),
|
||||
json,
|
||||
)
|
||||
assertTrue(result is SmsManager.QueryParseResult.Ok)
|
||||
val ok = result as SmsManager.QueryParseResult.Ok
|
||||
assertEquals(100L, ok.params.startTime)
|
||||
assertEquals(200L, ok.params.endTime)
|
||||
assertEquals("Leah", ok.params.contactName)
|
||||
assertEquals("+1555", ok.params.phoneNumber)
|
||||
assertEquals("ping", ok.params.keyword)
|
||||
assertEquals(1, ok.params.type)
|
||||
assertEquals(true, ok.params.isRead)
|
||||
assertEquals(10, ok.params.limit)
|
||||
assertEquals(2, ok.params.offset)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildPayloadJsonEscapesFields() {
|
||||
val payload = SmsManager.buildPayloadJson(
|
||||
@@ -166,69 +75,6 @@ class SmsManagerTest {
|
||||
assertEquals("SMS_SEND_FAILED: \"nope\"", parsed["error"]?.jsonPrimitive?.content)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildQueryPayloadJsonIncludesCountAndMessages() {
|
||||
val payload = SmsManager.buildQueryPayloadJson(
|
||||
json = json,
|
||||
ok = true,
|
||||
messages = listOf(
|
||||
SmsManager.SmsMessage(
|
||||
id = 1L,
|
||||
threadId = 2L,
|
||||
address = "+1555",
|
||||
person = null,
|
||||
date = 123L,
|
||||
dateSent = 124L,
|
||||
read = true,
|
||||
type = 1,
|
||||
body = "hello",
|
||||
status = 0,
|
||||
)
|
||||
),
|
||||
)
|
||||
val parsed = json.parseToJsonElement(payload).jsonObject
|
||||
assertEquals("true", parsed["ok"]?.jsonPrimitive?.content)
|
||||
assertEquals(1, parsed["count"]?.jsonPrimitive?.content?.toInt())
|
||||
val messages = parsed["messages"]?.jsonArray
|
||||
assertEquals(1, messages?.size)
|
||||
assertEquals("hello", messages?.get(0)?.jsonObject?.get("body")?.jsonPrimitive?.content)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildQueryPayloadJsonIncludesErrorOnFailure() {
|
||||
val payload = SmsManager.buildQueryPayloadJson(
|
||||
json = json,
|
||||
ok = false,
|
||||
messages = emptyList(),
|
||||
error = "SMS_QUERY_FAILED: nope",
|
||||
)
|
||||
val parsed = json.parseToJsonElement(payload).jsonObject
|
||||
assertEquals("false", parsed["ok"]?.jsonPrimitive?.content)
|
||||
assertEquals(0, parsed["count"]?.jsonPrimitive?.content?.toInt())
|
||||
assertEquals("SMS_QUERY_FAILED: nope", parsed["error"]?.jsonPrimitive?.content)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildQueryPayloadJsonIncludesMmsMetadataWhenProvided() {
|
||||
val payload = SmsManager.buildQueryPayloadJson(
|
||||
json = json,
|
||||
ok = true,
|
||||
messages = listOf(smsMessage(id = 1L, date = 1000L)),
|
||||
queryMetadata =
|
||||
SmsManager.QueryMetadata(
|
||||
mmsRequested = true,
|
||||
mmsEligible = true,
|
||||
mmsAttempted = true,
|
||||
mmsIncluded = false,
|
||||
),
|
||||
)
|
||||
val parsed = json.parseToJsonElement(payload).jsonObject
|
||||
assertEquals("true", parsed["mmsRequested"]?.jsonPrimitive?.content)
|
||||
assertEquals("true", parsed["mmsEligible"]?.jsonPrimitive?.content)
|
||||
assertEquals("true", parsed["mmsAttempted"]?.jsonPrimitive?.content)
|
||||
assertEquals("false", parsed["mmsIncluded"]?.jsonPrimitive?.content)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildSendPlanUsesMultipartWhenMultipleParts() {
|
||||
val plan = SmsManager.buildSendPlan("hello") { listOf("a", "b") }
|
||||
@@ -252,6 +98,14 @@ class SmsManagerTest {
|
||||
assertEquals(0, ok.params.offset)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseQueryParamsRejectsInvalidJson() {
|
||||
val result = SmsManager.parseQueryParams("not-json", json)
|
||||
assertTrue(result is SmsManager.QueryParseResult.Error)
|
||||
val error = result as SmsManager.QueryParseResult.Error
|
||||
assertEquals("INVALID_REQUEST: expected JSON object", error.error)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseQueryParamsRejectsNonObjectJson() {
|
||||
val result = SmsManager.parseQueryParams("[]", json)
|
||||
@@ -325,749 +179,4 @@ class SmsManagerTest {
|
||||
val ok = result as SmsManager.QueryParseResult.Ok
|
||||
assertEquals(true, ok.params.isRead)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseQueryParamsIncludeMmsDefaultsFalse() {
|
||||
val result = SmsManager.parseQueryParams("{}", json)
|
||||
assertTrue(result is SmsManager.QueryParseResult.Ok)
|
||||
val ok = result as SmsManager.QueryParseResult.Ok
|
||||
assertFalse(ok.params.includeMms)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseQueryParamsParsesIncludeMmsTrue() {
|
||||
val result = SmsManager.parseQueryParams("{\"includeMms\":true}", json)
|
||||
assertTrue(result is SmsManager.QueryParseResult.Ok)
|
||||
val ok = result as SmsManager.QueryParseResult.Ok
|
||||
assertTrue(ok.params.includeMms)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseQueryParamsParsesConversationReviewTrue() {
|
||||
val result = SmsManager.parseQueryParams("{\"conversationReview\":true}", json)
|
||||
assertTrue(result is SmsManager.QueryParseResult.Ok)
|
||||
val ok = result as SmsManager.QueryParseResult.Ok
|
||||
assertTrue(ok.params.conversationReview)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toByPhoneLookupNumberStripsFormattingToDigits() {
|
||||
assertEquals("12107588120", SmsManager.toByPhoneLookupNumber("+1 (210) 758-8120"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun normalizePhoneNumberOrNullReturnsNullForFormattingOnlyInput() {
|
||||
assertNull(SmsManager.normalizePhoneNumberOrNull("() - "))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun normalizePhoneNumberOrNullReturnsNullForPlusOnlyInput() {
|
||||
assertNull(SmsManager.normalizePhoneNumberOrNull(" + "))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun normalizePhoneNumberOrNullKeepsUsableNormalizedNumber() {
|
||||
assertEquals("+15551234567", SmsManager.normalizePhoneNumberOrNull(" +1 (555) 123-4567 "))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sanitizeContactPhoneNumberOrNullDropsFormattingOnlyInput() {
|
||||
assertNull(SmsManager.sanitizeContactPhoneNumberOrNull(" () - "))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sanitizeContactPhoneNumberOrNullDropsPlusOnlyInput() {
|
||||
assertNull(SmsManager.sanitizeContactPhoneNumberOrNull(" + "))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sanitizeContactPhoneNumberOrNullKeepsUsableNormalizedNumber() {
|
||||
assertEquals("+15551234567", SmsManager.sanitizeContactPhoneNumberOrNull(" +1 (555) 123-4567 "))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sanitizeContactPhoneNumberOrNullDropsPercentWildcardInput() {
|
||||
assertNull(SmsManager.sanitizeContactPhoneNumberOrNull("1%2"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sanitizeContactPhoneNumberOrNullDropsUnderscoreWildcardInput() {
|
||||
assertNull(SmsManager.sanitizeContactPhoneNumberOrNull("1_2"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shouldPromptForContactNameSearchPermissionTrueForContactNameOnlyWithoutContactsAccess() {
|
||||
assertTrue(
|
||||
SmsManager.shouldPromptForContactNameSearchPermission(
|
||||
contactName = "Alice",
|
||||
phoneNumber = null,
|
||||
hasReadContactsPermission = false,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shouldPromptForContactNameSearchPermissionFalseWhenExplicitPhoneFallbackExists() {
|
||||
assertFalse(
|
||||
SmsManager.shouldPromptForContactNameSearchPermission(
|
||||
contactName = "Alice",
|
||||
phoneNumber = "+15551234567",
|
||||
hasReadContactsPermission = false,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shouldPromptForContactNameSearchPermissionFalseWhenContactsAlreadyGranted() {
|
||||
assertFalse(
|
||||
SmsManager.shouldPromptForContactNameSearchPermission(
|
||||
contactName = "Alice",
|
||||
phoneNumber = null,
|
||||
hasReadContactsPermission = true,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun escapeSqlLikeLiteralEscapesPercentUnderscoreAndBackslash() {
|
||||
assertEquals("\\%a\\_b\\\\c", SmsManager.escapeSqlLikeLiteral("%a_b\\c"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun escapeSqlLikeLiteralLeavesOrdinaryTextUnchanged() {
|
||||
assertEquals("Leah", SmsManager.escapeSqlLikeLiteral("Leah"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildContactNameLikeSelectionUsesSingleBackslashEscapeLiteral() {
|
||||
assertEquals(
|
||||
"display_name LIKE ? ESCAPE '\\'",
|
||||
SmsManager.buildContactNameLikeSelection(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildContactNameLikeArgEscapesWildcardsAndBackslash() {
|
||||
assertEquals("%\\%a\\_b\\\\c%", SmsManager.buildContactNameLikeArg("%a_b\\c"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildKeywordLikeSelectionUsesSingleBackslashEscapeLiteral() {
|
||||
assertEquals(
|
||||
"body LIKE ? ESCAPE '\\'",
|
||||
SmsManager.buildKeywordLikeSelection(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildKeywordLikeArgEscapesWildcardsAndBackslash() {
|
||||
assertEquals("%\\%a\\_b\\\\c%", SmsManager.buildKeywordLikeArg("%a_b\\c"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildMixedByPhoneProjectionMatchesExpectedStatusAwareShape() {
|
||||
assertArrayEquals(
|
||||
arrayOf(
|
||||
"_id",
|
||||
"thread_id",
|
||||
"transport_type",
|
||||
"address",
|
||||
"date",
|
||||
"date_sent",
|
||||
"read",
|
||||
"type",
|
||||
"body",
|
||||
"status",
|
||||
),
|
||||
SmsManager.buildMixedByPhoneProjection(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun compareByPhoneCandidateOrderUsesDateThenIdDescending() {
|
||||
val newer = smsMessage(id = 1L, date = 2000L)
|
||||
val older = smsMessage(id = 2L, date = 1000L)
|
||||
val sameDateHigherId = smsMessage(id = 9L, date = 1500L)
|
||||
val sameDateLowerId = smsMessage(id = 3L, date = 1500L)
|
||||
|
||||
assertTrue(SmsManager.compareByPhoneCandidateOrder(newer, older) < 0)
|
||||
assertTrue(SmsManager.compareByPhoneCandidateOrder(sameDateHigherId, sameDateLowerId) < 0)
|
||||
assertTrue(SmsManager.compareByPhoneCandidateOrder(sameDateLowerId, sameDateHigherId) > 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun upsertTopDateCandidatesKeepsDescendingOrderAndBounds() {
|
||||
val candidates = mutableListOf<Pair<String, SmsManager.SmsMessage>>()
|
||||
val max = 2
|
||||
|
||||
SmsManager.upsertTopDateCandidates(candidates, "sms:1", smsMessage(id = 1L, date = 1700L), max)
|
||||
SmsManager.upsertTopDateCandidates(candidates, "sms:2", smsMessage(id = 2L, date = 2000L), max)
|
||||
SmsManager.upsertTopDateCandidates(candidates, "sms:3", smsMessage(id = 3L, date = 1500L), max)
|
||||
|
||||
assertEquals(listOf(2L, 1L), candidates.map { it.second.id })
|
||||
assertEquals(listOf(2000L, 1700L), candidates.map { it.second.date })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun upsertTopDateCandidatesSupportsDefaultMixedPathBoundedWindow() {
|
||||
val params = SmsManager.QueryParams(limit = 3, offset = 2, includeMms = true, phoneNumber = "+15551234567")
|
||||
val candidates = mutableListOf<Pair<String, SmsManager.SmsMessage>>()
|
||||
val max = params.offset + params.limit
|
||||
|
||||
SmsManager.upsertTopDateCandidates(candidates, "sms:1", smsMessage(id = 1L, date = 1000L), max)
|
||||
SmsManager.upsertTopDateCandidates(candidates, "sms:2", smsMessage(id = 2L, date = 2000L), max)
|
||||
SmsManager.upsertTopDateCandidates(candidates, "sms:3", smsMessage(id = 3L, date = 3000L), max)
|
||||
SmsManager.upsertTopDateCandidates(candidates, "sms:4", smsMessage(id = 4L, date = 4000L), max)
|
||||
SmsManager.upsertTopDateCandidates(candidates, "sms:5", smsMessage(id = 5L, date = 5000L), max)
|
||||
SmsManager.upsertTopDateCandidates(candidates, "sms:6", smsMessage(id = 6L, date = 6000L), max)
|
||||
|
||||
assertEquals(5, candidates.size)
|
||||
assertEquals(listOf(6L, 5L, 4L, 3L, 2L), candidates.map { it.second.id })
|
||||
assertEquals(listOf(4000L, 3000L, 2000L), SmsManager.pageByPhoneCandidates(candidates.map { it.second }, params).map { it.date })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun upsertTopDateCandidatesDedupesBySourceAwareIdentityAndKeepsBestOrdering() {
|
||||
val candidates = mutableListOf<Pair<String, SmsManager.SmsMessage>>()
|
||||
val max = 5
|
||||
|
||||
SmsManager.upsertTopDateCandidates(candidates, "sms:1987", smsMessage(id = 1987L, date = 1773950752506L), max)
|
||||
SmsManager.upsertTopDateCandidates(candidates, "sms:1986", smsMessage(id = 1986L, date = 1773899354039L), max)
|
||||
SmsManager.upsertTopDateCandidates(candidates, "sms:1985", smsMessage(id = 1985L, date = 1773872989602L), max)
|
||||
SmsManager.upsertTopDateCandidates(candidates, "sms:1981", smsMessage(id = 1981L, date = 1773790733566L), max)
|
||||
SmsManager.upsertTopDateCandidates(candidates, "sms:1976", smsMessage(id = 1976L, date = 1773784153770L), max)
|
||||
|
||||
// same source-aware identity should replace, not duplicate
|
||||
SmsManager.upsertTopDateCandidates(candidates, "sms:1986", smsMessage(id = 1986L, date = 1773899354039L), max)
|
||||
// different source-aware identity with same raw id must be preserved
|
||||
SmsManager.upsertTopDateCandidates(candidates, "mms:1986", smsMessage(id = 1986L, date = 1773899354038L), max)
|
||||
|
||||
assertEquals(5, candidates.size)
|
||||
assertEquals(2, candidates.count { it.second.id == 1986L })
|
||||
assertEquals(listOf("sms:1987", "sms:1986", "mms:1986", "sms:1985", "sms:1981"), candidates.map { it.first })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun materializeByPhoneCandidateDedupesBySourceAwareIdentity() {
|
||||
val candidates = linkedMapOf<String, SmsManager.SmsMessage>()
|
||||
|
||||
SmsManager.materializeByPhoneCandidate(candidates, "sms:1", smsMessage(id = 1L, date = 1000L))
|
||||
SmsManager.materializeByPhoneCandidate(candidates, "sms:1", smsMessage(id = 1L, date = 2000L))
|
||||
SmsManager.materializeByPhoneCandidate(candidates, "mms:1", smsMessage(id = 1L, date = 1500L))
|
||||
|
||||
assertEquals(2, candidates.size)
|
||||
assertEquals(2000L, candidates["sms:1"]?.date)
|
||||
assertEquals(1500L, candidates["mms:1"]?.date)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun collectMixedByPhoneCandidateUsesBoundedCollectorWhenReviewModeDisabled() {
|
||||
val topCandidates = mutableListOf<Pair<String, SmsManager.SmsMessage>>()
|
||||
val materializedCandidates = linkedMapOf<String, SmsManager.SmsMessage>()
|
||||
|
||||
SmsManager.collectMixedByPhoneCandidate(
|
||||
topCandidates = topCandidates,
|
||||
materializedCandidates = materializedCandidates,
|
||||
identityKey = "sms:1",
|
||||
message = smsMessage(id = 1L, date = 1000L),
|
||||
maxCandidates = 1,
|
||||
reviewMode = false,
|
||||
)
|
||||
SmsManager.collectMixedByPhoneCandidate(
|
||||
topCandidates = topCandidates,
|
||||
materializedCandidates = materializedCandidates,
|
||||
identityKey = "mms:2",
|
||||
message = smsMessage(id = 2L, date = 2000L, transportType = "mms"),
|
||||
maxCandidates = 1,
|
||||
reviewMode = false,
|
||||
)
|
||||
|
||||
assertEquals(listOf(2L), topCandidates.map { it.second.id })
|
||||
assertTrue(materializedCandidates.isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun collectMixedByPhoneCandidateMaterializesFullSetWhenReviewModeEnabled() {
|
||||
val topCandidates = mutableListOf<Pair<String, SmsManager.SmsMessage>>()
|
||||
val materializedCandidates = linkedMapOf<String, SmsManager.SmsMessage>()
|
||||
|
||||
SmsManager.collectMixedByPhoneCandidate(
|
||||
topCandidates = topCandidates,
|
||||
materializedCandidates = materializedCandidates,
|
||||
identityKey = "sms:1",
|
||||
message = smsMessage(id = 1L, date = 1000L),
|
||||
maxCandidates = 1,
|
||||
reviewMode = true,
|
||||
)
|
||||
SmsManager.collectMixedByPhoneCandidate(
|
||||
topCandidates = topCandidates,
|
||||
materializedCandidates = materializedCandidates,
|
||||
identityKey = "mms:2",
|
||||
message = smsMessage(id = 2L, date = 2000L, transportType = "mms"),
|
||||
maxCandidates = 1,
|
||||
reviewMode = true,
|
||||
)
|
||||
|
||||
assertTrue(topCandidates.isEmpty())
|
||||
assertEquals(listOf(1L, 2L), materializedCandidates.values.map { it.id })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun pageMixedByPhoneCandidatesLetsReviewModeSurfaceOlderRowsBeyondBoundedDefaultWindow() {
|
||||
val params =
|
||||
SmsManager.QueryParams(
|
||||
limit = 2,
|
||||
offset = 2,
|
||||
includeMms = true,
|
||||
phoneNumber = "+15551234567",
|
||||
conversationReview = true,
|
||||
)
|
||||
val topCandidates = listOf(
|
||||
"sms:9" to smsMessage(id = 9L, date = 9000L),
|
||||
"sms:8" to smsMessage(id = 8L, date = 8000L),
|
||||
"sms:7" to smsMessage(id = 7L, date = 7000L),
|
||||
)
|
||||
val materializedCandidates =
|
||||
linkedMapOf(
|
||||
"sms:9" to smsMessage(id = 9L, date = 9000L),
|
||||
"sms:8" to smsMessage(id = 8L, date = 8000L),
|
||||
"sms:7" to smsMessage(id = 7L, date = 7000L),
|
||||
"mms:6" to smsMessage(id = 6L, date = 6000L, transportType = "mms"),
|
||||
)
|
||||
|
||||
val defaultPage =
|
||||
SmsManager.pageMixedByPhoneCandidates(
|
||||
topCandidates = topCandidates,
|
||||
materializedCandidates = materializedCandidates,
|
||||
params = params.copy(conversationReview = false),
|
||||
reviewMode = false,
|
||||
)
|
||||
val reviewPage =
|
||||
SmsManager.pageMixedByPhoneCandidates(
|
||||
topCandidates = topCandidates,
|
||||
materializedCandidates = materializedCandidates,
|
||||
params = params,
|
||||
reviewMode = true,
|
||||
)
|
||||
|
||||
assertEquals(listOf(7L), defaultPage.map { it.id })
|
||||
assertEquals(listOf(7L, 6L), reviewPage.map { it.id })
|
||||
assertEquals(4, materializedCandidates.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun pageByPhoneCandidatesHonorsDeepOffsetAfterStableSort() {
|
||||
val params = SmsManager.QueryParams(limit = 5, offset = 5, includeMms = true)
|
||||
val candidates = listOf(
|
||||
smsMessage(id = 1399L, date = 1741112335720L),
|
||||
smsMessage(id = 1976L, date = 1773784153770L),
|
||||
smsMessage(id = 1981L, date = 1773790733566L),
|
||||
smsMessage(id = 1985L, date = 1773872989602L),
|
||||
smsMessage(id = 1986L, date = 1773899354039L),
|
||||
smsMessage(id = 1987L, date = 1773950752506L),
|
||||
)
|
||||
|
||||
assertEquals(listOf(1399L), SmsManager.pageByPhoneCandidates(candidates, params).map { it.id })
|
||||
assertTrue(SmsManager.pageByPhoneCandidates(candidates, params.copy(offset = 10)).isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun upsertTopDateCandidatesNoOpWhenMaxIsZero() {
|
||||
val candidates = mutableListOf<Pair<String, SmsManager.SmsMessage>>()
|
||||
SmsManager.upsertTopDateCandidates(candidates, "sms:1", smsMessage(id = 1L, date = 2000L), 0)
|
||||
assertTrue(candidates.isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildMixedRowIdentityUsesTransportTypeAndRowId() {
|
||||
assertEquals("sms:7", SmsManager.buildMixedRowIdentity(7L, "sms"))
|
||||
assertEquals("mms:7", SmsManager.buildMixedRowIdentity(7L, "mms"))
|
||||
assertEquals("unknown:7", SmsManager.buildMixedRowIdentity(7L, null))
|
||||
assertEquals("unknown:7", SmsManager.buildMixedRowIdentity(7L, ""))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun normalizeProviderDateMillisConvertsSecondsToMillis() {
|
||||
assertEquals(1773944910000L, SmsManager.normalizeProviderDateMillis(1773944910L))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun normalizeProviderDateMillisKeepsMillisUnchanged() {
|
||||
assertEquals(1773944910123L, SmsManager.normalizeProviderDateMillis(1773944910123L))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun normalizeProviderDateMillisKeepsHistoricMillisUnchanged() {
|
||||
assertEquals(946684800000L, SmsManager.normalizeProviderDateMillis(946684800000L))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resolveMixedByPhoneRowStatusPreservesRealSmsStatus() {
|
||||
assertEquals(64, SmsManager.resolveMixedByPhoneRowStatus("sms", 64))
|
||||
assertEquals(32, SmsManager.resolveMixedByPhoneRowStatus(null, 32))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resolveMixedByPhoneRowStatusKeepsMmsOnSentinelValue() {
|
||||
assertEquals(-1, SmsManager.resolveMixedByPhoneRowStatus("mms", 64))
|
||||
assertEquals(-1, SmsManager.resolveMixedByPhoneRowStatus("MMS", null))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resolveMixedByPhoneRowStatusFallsBackToZeroWhenSmsStatusMissing() {
|
||||
assertEquals(0, SmsManager.resolveMixedByPhoneRowStatus("sms", null))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resolveMixedByPhoneRowAddressPreservesProviderAddressWhenPresent() {
|
||||
assertEquals(
|
||||
"+12107588120",
|
||||
SmsManager.resolveMixedByPhoneRowAddress("+12107588120", "12107588120"),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resolveMixedByPhoneRowAddressFallsBackToLookupNumberWhenProviderAddressMissing() {
|
||||
assertEquals(
|
||||
"12107588120",
|
||||
SmsManager.resolveMixedByPhoneRowAddress(null, "12107588120"),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resolveMixedByPhoneRowAddressCanPreserveLookupNumberWhenProviderAlreadyReturnsIt() {
|
||||
assertEquals(
|
||||
"12107588120",
|
||||
SmsManager.resolveMixedByPhoneRowAddress("12107588120", "12107588120"),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resolveMixedByPhoneRowAddressPreservesNonMatchingProviderAddress() {
|
||||
assertEquals(
|
||||
"+13105550123",
|
||||
SmsManager.resolveMixedByPhoneRowAddress("+13105550123", "12107588120"),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resolveMixedByPhoneRowAddressPrefersResolvedMmsParticipantAddress() {
|
||||
assertEquals(
|
||||
"+13105550123",
|
||||
SmsManager.resolveMixedByPhoneRowAddress("insert-address-token", "12107588120", "+13105550123"),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun selectPreferredMmsAddressPrefersType137AddressThatDoesNotMatchLookup() {
|
||||
assertEquals(
|
||||
"+13105550123",
|
||||
SmsManager.selectPreferredMmsAddress(
|
||||
listOf(
|
||||
"+12107588120" to 151,
|
||||
"+13105550123" to 137,
|
||||
"+12107588120" to 130,
|
||||
),
|
||||
"12107588120",
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun selectPreferredMmsAddressFallsBackToFirstNormalizedAddressWhenOnlyLookupMatchesExist() {
|
||||
assertEquals(
|
||||
"+12107588120",
|
||||
SmsManager.selectPreferredMmsAddress(
|
||||
listOf(
|
||||
"insert-address-token" to 137,
|
||||
"+12107588120" to 151,
|
||||
),
|
||||
"12107588120",
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isExplicitPhoneInputInvalidTrueWhenCallerSuppliesOnlyFormatting() {
|
||||
val normalized = SmsManager.normalizePhoneNumberOrNull(" + ")
|
||||
assertTrue(SmsManager.isExplicitPhoneInputInvalid(" + ", normalized))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun hasSqlLikeWildcardDetectsPercentAndUnderscore() {
|
||||
assertTrue(SmsManager.hasSqlLikeWildcard("+1555%1234"))
|
||||
assertTrue(SmsManager.hasSqlLikeWildcard("+1555_1234"))
|
||||
assertFalse(SmsManager.hasSqlLikeWildcard("+15551234"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isExplicitPhoneInputInvalidRejectsLikeWildcardPhoneFilter() {
|
||||
assertTrue(SmsManager.isExplicitPhoneInputInvalid("+1555%1234", "+1555%1234"))
|
||||
assertTrue(SmsManager.isExplicitPhoneInputInvalid("+1555_1234", "+1555_1234"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isExplicitPhoneInputInvalidFalseWhenPhoneWasOmitted() {
|
||||
assertFalse(SmsManager.isExplicitPhoneInputInvalid(null, null))
|
||||
assertFalse(SmsManager.isExplicitPhoneInputInvalid(" ", null))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun mapMmsMsgBoxToSearchTypeCoversSearchRelevantMmsBoxes() {
|
||||
assertEquals(1, SmsManager.mapMmsMsgBoxToSearchType(1))
|
||||
assertEquals(2, SmsManager.mapMmsMsgBoxToSearchType(2))
|
||||
assertEquals(3, SmsManager.mapMmsMsgBoxToSearchType(3))
|
||||
assertEquals(4, SmsManager.mapMmsMsgBoxToSearchType(4))
|
||||
assertEquals(5, SmsManager.mapMmsMsgBoxToSearchType(5))
|
||||
assertEquals(6, SmsManager.mapMmsMsgBoxToSearchType(6))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun mapMmsMsgBoxToSearchTypeLeavesUnsupportedBoxesUnmapped() {
|
||||
assertNull(SmsManager.mapMmsMsgBoxToSearchType(0))
|
||||
assertNull(SmsManager.mapMmsMsgBoxToSearchType(99))
|
||||
assertNull(SmsManager.mapMmsMsgBoxToSearchType(null))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shouldUseConversationReviewByPhoneModeOnlyForMixedByPhoneReviewPulls() {
|
||||
val active =
|
||||
SmsManager.QueryParams(
|
||||
limit = 5,
|
||||
offset = 0,
|
||||
isRead = null,
|
||||
contactName = null,
|
||||
phoneNumber = "+12107588120",
|
||||
keyword = null,
|
||||
startTime = null,
|
||||
endTime = null,
|
||||
includeMms = true,
|
||||
conversationReview = true,
|
||||
)
|
||||
val disabledByMode = active.copy(conversationReview = false)
|
||||
val disabledByMms = active.copy(includeMms = false)
|
||||
val disabledByPhone = active.copy(phoneNumber = null)
|
||||
|
||||
assertTrue(SmsManager.shouldUseConversationReviewByPhoneMode(active))
|
||||
assertFalse(SmsManager.shouldUseConversationReviewByPhoneMode(disabledByMode))
|
||||
assertFalse(SmsManager.shouldUseConversationReviewByPhoneMode(disabledByMms))
|
||||
assertFalse(SmsManager.shouldUseConversationReviewByPhoneMode(disabledByPhone))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun effectiveSearchParamsRaisesConversationReviewLimitFloor() {
|
||||
val params =
|
||||
SmsManager.QueryParams(
|
||||
limit = 5,
|
||||
offset = 0,
|
||||
isRead = null,
|
||||
contactName = null,
|
||||
phoneNumber = "+12107588120",
|
||||
keyword = null,
|
||||
startTime = null,
|
||||
endTime = null,
|
||||
includeMms = true,
|
||||
conversationReview = true,
|
||||
)
|
||||
|
||||
assertEquals(25, SmsManager.effectiveSearchParams(params).limit)
|
||||
assertEquals(40, SmsManager.effectiveSearchParams(params.copy(limit = 40)).limit)
|
||||
assertEquals(5, SmsManager.effectiveSearchParams(params.copy(conversationReview = false)).limit)
|
||||
|
||||
val singleResolvedContact = params.copy(phoneNumber = null, contactName = "Leah")
|
||||
assertEquals(25, SmsManager.effectiveSearchParams(singleResolvedContact, listOf("15551234567")).limit)
|
||||
assertEquals(5, SmsManager.effectiveSearchParams(singleResolvedContact, listOf("15551234567", "15557654321")).limit)
|
||||
assertEquals(
|
||||
SmsManager.effectiveSearchParams(params).limit,
|
||||
SmsManager.effectiveSearchParams(singleResolvedContact, listOf("15551234567")).limit,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resolveSearchParamsCarriesSingleResolvedContactIntoReviewMode() {
|
||||
val params =
|
||||
SmsManager.QueryParams(
|
||||
limit = 5,
|
||||
offset = 0,
|
||||
isRead = null,
|
||||
contactName = "Leah",
|
||||
phoneNumber = null,
|
||||
keyword = null,
|
||||
startTime = null,
|
||||
endTime = null,
|
||||
includeMms = true,
|
||||
conversationReview = true,
|
||||
)
|
||||
|
||||
val beforeResolution = SmsManager.resolveSearchParams(params, normalizedPhoneNumber = null)
|
||||
val singleResolved =
|
||||
SmsManager.resolveSearchParams(
|
||||
params,
|
||||
normalizedPhoneNumber = null,
|
||||
resolvedPhoneNumbers = listOf("15551234567"),
|
||||
)
|
||||
val multiResolved =
|
||||
SmsManager.resolveSearchParams(
|
||||
params,
|
||||
normalizedPhoneNumber = null,
|
||||
resolvedPhoneNumbers = listOf("15551234567", "15557654321"),
|
||||
)
|
||||
val explicit =
|
||||
SmsManager.resolveSearchParams(
|
||||
params.copy(contactName = null, phoneNumber = "+12107588120"),
|
||||
normalizedPhoneNumber = "12107588120",
|
||||
)
|
||||
val nonReview =
|
||||
SmsManager.resolveSearchParams(
|
||||
params.copy(conversationReview = false),
|
||||
normalizedPhoneNumber = null,
|
||||
resolvedPhoneNumbers = listOf("15551234567"),
|
||||
)
|
||||
|
||||
assertEquals(5, beforeResolution.limit)
|
||||
assertEquals(25, singleResolved.limit)
|
||||
assertEquals("15551234567", singleResolved.phoneNumber)
|
||||
assertTrue(SmsManager.shouldUseConversationReviewByPhoneMode(singleResolved))
|
||||
assertEquals(5, multiResolved.limit)
|
||||
assertNull(multiResolved.phoneNumber)
|
||||
assertFalse(SmsManager.shouldUseConversationReviewByPhoneMode(multiResolved))
|
||||
assertEquals(25, explicit.limit)
|
||||
assertEquals("12107588120", explicit.phoneNumber)
|
||||
assertEquals(5, nonReview.limit)
|
||||
assertEquals("15551234567", nonReview.phoneNumber)
|
||||
assertFalse(SmsManager.shouldUseConversationReviewByPhoneMode(nonReview))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun canonicalizeMixedPathPhoneFiltersDedupesEquivalentExplicitAndContactNumbers() {
|
||||
assertEquals(
|
||||
listOf("15551234567"),
|
||||
SmsManager.canonicalizeMixedPathPhoneFilters(listOf("+15551234567", "15551234567")),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun canonicalizeMixedPathPhoneFiltersDropsBlankByPhoneValues() {
|
||||
assertEquals(
|
||||
listOf("15551234567"),
|
||||
SmsManager.canonicalizeMixedPathPhoneFilters(listOf("+15551234567", "+", " ")),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildQueryMetadataUsesCanonicalizedSingleMixedFilterAsEligible() {
|
||||
val params = SmsManager.QueryParams(includeMms = true, phoneNumber = "+15551234567")
|
||||
val canonical = SmsManager.canonicalizeMixedPathPhoneFilters(listOf("+15551234567", "15551234567"))
|
||||
|
||||
val metadata = SmsManager.buildQueryMetadata(params, canonical, emptyList())
|
||||
|
||||
assertTrue(metadata.mmsEligible)
|
||||
assertTrue(metadata.mmsAttempted)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun requestedMixedByPhoneCandidateWindowAddsOffsetAndLimitSafely() {
|
||||
val params = SmsManager.QueryParams(includeMms = true, phoneNumber = "+15551234567", limit = 200, offset = 300)
|
||||
assertEquals(500L, SmsManager.requestedMixedByPhoneCandidateWindow(params))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun exceedsMixedByPhoneCandidateWindowFalseAtSupportedBoundary() {
|
||||
val params = SmsManager.QueryParams(includeMms = true, phoneNumber = "+15551234567", limit = 200, offset = 300)
|
||||
assertFalse(SmsManager.exceedsMixedByPhoneCandidateWindow(params, listOf("+15551234567")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun exceedsMixedByPhoneCandidateWindowTrueWhenSingleNumberMixedWindowTooLarge() {
|
||||
val params = SmsManager.QueryParams(includeMms = true, phoneNumber = "+15551234567", limit = 200, offset = 301)
|
||||
assertTrue(SmsManager.exceedsMixedByPhoneCandidateWindow(params, listOf("+15551234567")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun exceedsMixedByPhoneCandidateWindowFalseForSmsOnlyQueries() {
|
||||
val params = SmsManager.QueryParams(includeMms = false, phoneNumber = "+15551234567", limit = 200, offset = 50000)
|
||||
assertFalse(SmsManager.exceedsMixedByPhoneCandidateWindow(params, listOf("+15551234567")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun exceedsMixedByPhoneCandidateWindowFalseWhenMultiplePhoneNumbersDisableMixedByPhonePath() {
|
||||
val params = SmsManager.QueryParams(includeMms = true, phoneNumber = null, limit = 200, offset = 50000)
|
||||
assertFalse(SmsManager.exceedsMixedByPhoneCandidateWindow(params, listOf("+15551234567", "+15557654321")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun mixedByPhoneWindowErrorMentionsSupportedWindow() {
|
||||
assertEquals(
|
||||
"INVALID_REQUEST: includeMms offset+limit exceeds supported window (500)",
|
||||
SmsManager.mixedByPhoneWindowError(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildQueryMetadataMarksIneligibleWhenIncludeMmsNotRequested() {
|
||||
val params = SmsManager.QueryParams(includeMms = false)
|
||||
|
||||
val metadata = SmsManager.buildQueryMetadata(params, emptyList(), emptyList())
|
||||
|
||||
assertFalse(metadata.mmsRequested)
|
||||
assertFalse(metadata.mmsEligible)
|
||||
assertFalse(metadata.mmsAttempted)
|
||||
assertFalse(metadata.mmsIncluded)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildQueryMetadataMarksEligibleAttemptedButNotIncludedForSingleNumberFallback() {
|
||||
val params = SmsManager.QueryParams(includeMms = true, phoneNumber = "+15551234567")
|
||||
val messages = listOf(smsMessage(id = 1L, date = 1000L))
|
||||
|
||||
val metadata = SmsManager.buildQueryMetadata(params, listOf("+15551234567"), messages)
|
||||
|
||||
assertTrue(metadata.mmsRequested)
|
||||
assertTrue(metadata.mmsEligible)
|
||||
assertTrue(metadata.mmsAttempted)
|
||||
assertFalse(metadata.mmsIncluded)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isMmsTransportRowTrueOnlyForMmsTransport() {
|
||||
assertTrue(SmsManager.isMmsTransportRow(smsMessage(id = 1L, date = 1000L, transportType = "mms")))
|
||||
assertFalse(SmsManager.isMmsTransportRow(smsMessage(id = 2L, date = 1000L, transportType = "sms")))
|
||||
assertFalse(SmsManager.isMmsTransportRow(smsMessage(id = 3L, date = 1000L, transportType = null)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shouldHydrateMmsByPhoneRowTrueOnlyForMmsTransportWithBlankBodyOrZeroType() {
|
||||
assertTrue(SmsManager.shouldHydrateMmsByPhoneRow("mms", null, 1))
|
||||
assertTrue(SmsManager.shouldHydrateMmsByPhoneRow("mms", "", 1))
|
||||
assertTrue(SmsManager.shouldHydrateMmsByPhoneRow("mms", "body", 0))
|
||||
assertFalse(SmsManager.shouldHydrateMmsByPhoneRow("sms", null, 0))
|
||||
assertFalse(SmsManager.shouldHydrateMmsByPhoneRow(null, null, 0))
|
||||
assertFalse(SmsManager.shouldHydrateMmsByPhoneRow("mms", "body", 1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildQueryMetadataDoesNotTreatSmsStatusSentinelAsMmsInclusion() {
|
||||
val params = SmsManager.QueryParams(includeMms = true, phoneNumber = "+15551234567")
|
||||
val smsLikeMessage = smsMessage(id = 7L, date = 1000L, status = -1, transportType = "sms")
|
||||
|
||||
val metadata = SmsManager.buildQueryMetadata(params, listOf("15551234567"), listOf(smsLikeMessage))
|
||||
|
||||
assertTrue(metadata.mmsRequested)
|
||||
assertTrue(metadata.mmsEligible)
|
||||
assertTrue(metadata.mmsAttempted)
|
||||
assertFalse(metadata.mmsIncluded)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildQueryMetadataMarksIncludedWhenMixedQueryYieldsMmsTransportRow() {
|
||||
val params = SmsManager.QueryParams(includeMms = true, phoneNumber = "+15551234567")
|
||||
val mmsTransportMessage = smsMessage(id = 7L, date = 1000L, status = 0, body = null, transportType = "mms")
|
||||
|
||||
val metadata = SmsManager.buildQueryMetadata(params, listOf("15551234567"), listOf(mmsTransportMessage))
|
||||
|
||||
assertTrue(metadata.mmsRequested)
|
||||
assertTrue(metadata.mmsEligible)
|
||||
assertTrue(metadata.mmsAttempted)
|
||||
assertTrue(metadata.mmsIncluded)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,16 +26,6 @@ class SystemHandlerTest {
|
||||
assertEquals("INVALID_REQUEST", result.error?.code)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleSystemNotify_rejectsInvalidRequestObject() {
|
||||
val handler = SystemHandler.forTesting(poster = FakePoster(authorized = true))
|
||||
|
||||
val result = handler.handleSystemNotify("""{"title":"OpenClaw"}""")
|
||||
|
||||
assertFalse(result.ok)
|
||||
assertEquals("INVALID_REQUEST", result.error?.code)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleSystemNotify_postsNotification() {
|
||||
val poster = FakePoster(authorized = true)
|
||||
@@ -47,23 +37,6 @@ class SystemHandlerTest {
|
||||
assertEquals(1, poster.posts)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleSystemNotify_trimsAndPassesOptionalFields() {
|
||||
val poster = FakePoster(authorized = true)
|
||||
val handler = SystemHandler.forTesting(poster = poster)
|
||||
|
||||
val result =
|
||||
handler.handleSystemNotify(
|
||||
"""{"title":" OpenClaw ","body":" done ","priority":" passive ","sound":" silent "}""",
|
||||
)
|
||||
|
||||
assertTrue(result.ok)
|
||||
assertEquals("OpenClaw", poster.lastRequest?.title)
|
||||
assertEquals("done", poster.lastRequest?.body)
|
||||
assertEquals("passive", poster.lastRequest?.priority)
|
||||
assertEquals("silent", poster.lastRequest?.sound)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handleSystemNotify_returnsUnauthorizedWhenPostFailsPermission() {
|
||||
val handler = SystemHandler.forTesting(poster = ThrowingPoster(authorized = true, error = SecurityException("denied")))
|
||||
@@ -82,7 +55,6 @@ class SystemHandlerTest {
|
||||
|
||||
assertFalse(result.ok)
|
||||
assertEquals("UNAVAILABLE", result.error?.code)
|
||||
assertEquals("NOTIFICATION_FAILED: boom", result.error?.message)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,14 +63,11 @@ private class FakePoster(
|
||||
) : SystemNotificationPoster {
|
||||
var posts: Int = 0
|
||||
private set
|
||||
var lastRequest: SystemNotifyRequest? = null
|
||||
private set
|
||||
|
||||
override fun isAuthorized(): Boolean = authorized
|
||||
|
||||
override fun post(request: SystemNotifyRequest) {
|
||||
posts += 1
|
||||
lastRequest = request
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -86,15 +86,13 @@ class OpenClawProtocolConstantsTest {
|
||||
assertEquals("motion.pedometer", OpenClawMotionCommand.Pedometer.rawValue)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun smsCommandsUseStableStrings() {
|
||||
assertEquals("sms.send", OpenClawSmsCommand.Send.rawValue)
|
||||
assertEquals("sms.search", OpenClawSmsCommand.Search.rawValue)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun callLogCommandsUseStableStrings() {
|
||||
assertEquals("callLog.search", OpenClawCallLogCommand.Search.rawValue)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun smsCommandsUseStableStrings() {
|
||||
assertEquals("sms.search", OpenClawSmsCommand.Search.rawValue)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
package ai.openclaw.app.ui
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class SettingsSheetNotificationAppsTest {
|
||||
@Test
|
||||
fun resolveNotificationCandidatePackages_keepsConfiguredPackagesVisible() {
|
||||
val packages =
|
||||
resolveNotificationCandidatePackages(
|
||||
launcherPackages = setOf("com.example.launcher"),
|
||||
recentPackages = listOf("com.example.recent", "com.example.launcher"),
|
||||
configuredPackages = setOf("com.example.configured"),
|
||||
appPackageName = "ai.openclaw.app",
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
setOf("com.example.launcher", "com.example.recent", "com.example.configured"),
|
||||
packages,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resolveNotificationCandidatePackages_filtersBlankAndSelfPackages() {
|
||||
val packages =
|
||||
resolveNotificationCandidatePackages(
|
||||
launcherPackages = setOf(" ", "ai.openclaw.app"),
|
||||
recentPackages = listOf("com.example.recent", " "),
|
||||
configuredPackages = setOf("ai.openclaw.app", "com.example.configured"),
|
||||
appPackageName = "ai.openclaw.app",
|
||||
)
|
||||
|
||||
assertEquals(setOf("com.example.recent", "com.example.configured"), packages)
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@@ -14,11 +14,6 @@ title: "Cron Jobs"
|
||||
Cron is the Gateway’s built-in scheduler. It persists jobs, wakes the agent at
|
||||
the right time, and can optionally deliver output back to a chat.
|
||||
|
||||
All cron executions create [background task](/automation/tasks) records. The key difference is visibility:
|
||||
|
||||
- `sessionTarget: "main"` creates a task with `silent` notify policy — it schedules a system event for the main session and heartbeat flow but does not generate notifications.
|
||||
- `sessionTarget: "isolated"` or `sessionTarget: "session:..."` creates a visible task that shows up in `openclaw tasks` with delivery notifications.
|
||||
|
||||
If you want _“run this every morning”_ or _“poke the agent in 20 minutes”_,
|
||||
cron is the mechanism.
|
||||
|
||||
@@ -160,8 +155,6 @@ They must use `payload.kind = "systemEvent"`.
|
||||
This is the best fit when you want the normal heartbeat prompt + main-session context.
|
||||
See [Heartbeat](/gateway/heartbeat).
|
||||
|
||||
Main-session cron jobs create [background task](/automation/tasks) records with `silent` notify policy (no notifications by default). They appear in `openclaw tasks list` but do not generate delivery messages.
|
||||
|
||||
#### Isolated jobs (dedicated cron sessions)
|
||||
|
||||
Isolated jobs run a dedicated agent turn in session `cron:<jobId>` or a custom session.
|
||||
@@ -183,8 +176,6 @@ Key behaviors:
|
||||
Use isolated jobs for noisy, frequent, or "background chores" that shouldn't spam
|
||||
your main chat history.
|
||||
|
||||
These detached runs create [background task](/automation/tasks) records visible in `openclaw tasks` and subject to task audit and maintenance.
|
||||
|
||||
### Payload shapes (what runs)
|
||||
|
||||
Two payload kinds are supported:
|
||||
@@ -734,11 +725,3 @@ openclaw system event --mode now --text "Next heartbeat: check battery."
|
||||
- If the announce flow returns `false` (e.g. requester session is busy), the gateway retries up to 3 times with tracking via `announceRetryCount`.
|
||||
- Announces older than 5 minutes past `endedAt` are force-expired to prevent stale entries from looping indefinitely.
|
||||
- If you see repeated announce deliveries in logs, check the subagent registry for entries with high `announceRetryCount` values.
|
||||
|
||||
## Related
|
||||
|
||||
- [Automation Overview](/automation) — all automation mechanisms at a glance
|
||||
- [Cron vs Heartbeat](/automation/cron-vs-heartbeat) — when to use each
|
||||
- [Background Tasks](/automation/tasks) — task ledger for cron executions
|
||||
- [Heartbeat](/gateway/heartbeat) — periodic main-session turns
|
||||
- [Troubleshooting](/automation/troubleshooting) — debugging automation issues
|
||||
|
||||
@@ -11,14 +11,6 @@ title: "Cron vs Heartbeat"
|
||||
|
||||
Both heartbeats and cron jobs let you run tasks on a schedule. This guide helps you choose the right mechanism for your use case.
|
||||
|
||||
One important distinction:
|
||||
|
||||
- **Heartbeat** is a scheduled **main-session turn** — no task record created.
|
||||
- **Cron (main)** is a scheduled **system event into the main session** — creates a task record with `silent` notify policy.
|
||||
- **Cron (isolated)** is a scheduled **background run** — creates a task record tracked in `openclaw tasks`.
|
||||
|
||||
All cron job executions (main and isolated) create [task records](/automation/tasks). Heartbeat turns do not. Main-session cron tasks use `silent` notify policy by default so they do not generate notifications.
|
||||
|
||||
## Quick Decision Guide
|
||||
|
||||
| Use Case | Recommended | Why |
|
||||
@@ -48,7 +40,6 @@ Heartbeats run in the **main session** at a regular interval (default: 30 min).
|
||||
- **Context-aware**: The agent knows what you've been working on and can prioritize accordingly.
|
||||
- **Smart suppression**: If nothing needs attention, the agent replies `HEARTBEAT_OK` and no message is delivered.
|
||||
- **Natural timing**: Drifts slightly based on queue load, which is fine for most monitoring.
|
||||
- **No task record**: heartbeat turns stay in main-session history (see [Background Tasks](/automation/tasks)).
|
||||
|
||||
### Heartbeat example: HEARTBEAT.md checklist
|
||||
|
||||
@@ -107,7 +98,6 @@ per-job offset in a 0-5 minute window.
|
||||
- **Immediate delivery**: Announce mode posts directly without waiting for heartbeat.
|
||||
- **No agent context needed**: Runs even if main session is idle or compacted.
|
||||
- **One-shot support**: `--at` for precise future timestamps.
|
||||
- **Task tracking**: isolated jobs create [background task](/automation/tasks) records visible in `openclaw tasks` and `openclaw tasks audit`.
|
||||
|
||||
### Cron example: Daily morning briefing
|
||||
|
||||
@@ -229,14 +219,13 @@ See [Lobster](/tools/lobster) for full usage and examples.
|
||||
|
||||
Both heartbeat and cron can interact with the main session, but differently:
|
||||
|
||||
| | Heartbeat | Cron (main) | Cron (isolated) |
|
||||
| -------------------------- | ------------------------------- | ------------------------ | ----------------------------------------------- |
|
||||
| Session | Main | Main (via system event) | `cron:<jobId>` or custom session |
|
||||
| History | Shared | Shared | Fresh each run (isolated) / Persistent (custom) |
|
||||
| Context | Full | Full | None (isolated) / Cumulative (custom) |
|
||||
| Model | Main session model | Main session model | Can override |
|
||||
| Output | Delivered if not `HEARTBEAT_OK` | Heartbeat prompt + event | Announce summary (default) |
|
||||
| [Tasks](/automation/tasks) | No task record | Task record (silent) | Task record (visible in `openclaw tasks`) |
|
||||
| | Heartbeat | Cron (main) | Cron (isolated) |
|
||||
| ------- | ------------------------------- | ------------------------ | ----------------------------------------------- |
|
||||
| Session | Main | Main (via system event) | `cron:<jobId>` or custom session |
|
||||
| History | Shared | Shared | Fresh each run (isolated) / Persistent (custom) |
|
||||
| Context | Full | Full | None (isolated) / Cumulative (custom) |
|
||||
| Model | Main session model | Main session model | Can override |
|
||||
| Output | Delivered if not `HEARTBEAT_OK` | Heartbeat prompt + event | Announce summary (default) |
|
||||
|
||||
### When to use main session cron
|
||||
|
||||
@@ -292,8 +281,6 @@ openclaw cron add \
|
||||
|
||||
## Related
|
||||
|
||||
- [Automation Overview](/automation) — all automation mechanisms at a glance
|
||||
- [Heartbeat](/gateway/heartbeat) — full heartbeat configuration
|
||||
- [Cron jobs](/automation/cron-jobs) — full cron CLI and API reference
|
||||
- [Background Tasks](/automation/tasks) — task ledger, audit, and lifecycle
|
||||
- [System](/cli/system) — system events + heartbeat controls
|
||||
- [Heartbeat](/gateway/heartbeat) - full heartbeat configuration
|
||||
- [Cron jobs](/automation/cron-jobs) - full cron CLI and API reference
|
||||
- [System](/cli/system) - system events + heartbeat controls
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
---
|
||||
summary: "Overview of all automation mechanisms: heartbeat, cron, tasks, hooks, webhooks, and more"
|
||||
read_when:
|
||||
- Deciding how to automate work with OpenClaw
|
||||
- Choosing between heartbeat, cron, hooks, and webhooks
|
||||
- Looking for the right automation entry point
|
||||
title: "Automation Overview"
|
||||
---
|
||||
|
||||
# Automation
|
||||
|
||||
OpenClaw provides several automation mechanisms, each suited to different use cases. This page helps you choose the right one.
|
||||
|
||||
## Quick decision guide
|
||||
|
||||
```
|
||||
Do you need something to run on a schedule?
|
||||
YES → Is exact timing critical?
|
||||
YES → Cron (isolated)
|
||||
NO → Can it batch with other checks?
|
||||
YES → Heartbeat
|
||||
NO → Cron
|
||||
NO → Continue...
|
||||
|
||||
Do you need to react to an event (message, tool call, session change)?
|
||||
YES → Hooks (or plugin hooks)
|
||||
|
||||
Do you need to receive external HTTP events?
|
||||
YES → Webhooks
|
||||
|
||||
Do you want persistent instructions the agent always follows?
|
||||
YES → Standing Orders
|
||||
|
||||
Do you want to track what background work happened?
|
||||
→ Background Tasks (automatic for cron, ACP, subagents)
|
||||
```
|
||||
|
||||
## Mechanisms at a glance
|
||||
|
||||
| Mechanism | What it does | Runs in | Creates task record |
|
||||
|---|---|---|---|
|
||||
| [Heartbeat](/gateway/heartbeat) | Periodic main-session turn — batches multiple checks | Main session | No |
|
||||
| [Cron](/automation/cron-jobs) | Scheduled jobs with precise timing | Main or isolated session | Yes (all types) |
|
||||
| [Background Tasks](/automation/tasks) | Tracks detached work (cron, ACP, subagents, CLI) | N/A (ledger) | N/A |
|
||||
| [Hooks](/automation/hooks) | Event-driven scripts triggered by agent lifecycle events | Hook runner | No |
|
||||
| [Standing Orders](/automation/standing-orders) | Persistent instructions injected into the system prompt | Main session | No |
|
||||
| [Webhooks](/automation/webhook) | Receive inbound HTTP events and route to the agent | Gateway HTTP | No |
|
||||
|
||||
### Specialized automation
|
||||
|
||||
| Mechanism | What it does |
|
||||
|---|---|
|
||||
| [Gmail PubSub](/automation/gmail-pubsub) | Real-time Gmail notifications via Google PubSub |
|
||||
| [Polling](/automation/poll) | Periodic data source checks (RSS, APIs, etc.) |
|
||||
| [Auth Monitoring](/automation/auth-monitoring) | Credential health and expiry alerts |
|
||||
|
||||
## How they work together
|
||||
|
||||
The most effective setups combine multiple mechanisms:
|
||||
|
||||
1. **Heartbeat** handles routine monitoring (inbox, calendar, notifications) in one batched turn every 30 minutes.
|
||||
2. **Cron** handles precise schedules (daily reports, weekly reviews) and one-shot reminders.
|
||||
3. **Hooks** react to specific events (tool calls, session resets, compaction) with custom scripts.
|
||||
4. **Standing Orders** give the agent persistent context ("always check the project board before replying").
|
||||
5. **Background Tasks** automatically track all detached work so you can inspect and audit it.
|
||||
|
||||
See [Cron vs Heartbeat](/automation/cron-vs-heartbeat) for a detailed comparison of the two scheduling mechanisms.
|
||||
|
||||
## Related
|
||||
|
||||
- [Cron vs Heartbeat](/automation/cron-vs-heartbeat) — detailed comparison guide
|
||||
- [Troubleshooting](/automation/troubleshooting) — debugging automation issues
|
||||
- [Configuration Reference](/gateway/configuration-reference) — all config keys
|
||||
@@ -247,8 +247,5 @@ Each program should have:
|
||||
|
||||
## Related
|
||||
|
||||
- [Automation Overview](/automation) — all automation mechanisms at a glance
|
||||
- [Cron Jobs](/automation/cron-jobs) — schedule enforcement for standing orders
|
||||
- [Hooks](/automation/hooks) — event-driven scripts for agent lifecycle events
|
||||
- [Webhooks](/automation/webhook) — inbound HTTP event triggers
|
||||
- [Agent Workspace](/concepts/agent-workspace) — where standing orders live, including the full list of auto-injected bootstrap files (AGENTS.md, SOUL.md, etc.)
|
||||
- [Cron Jobs](/automation/cron-jobs) — Schedule enforcement for standing orders
|
||||
- [Agent Workspace](/concepts/agent-workspace) — Where standing orders live, including the full list of auto-injected bootstrap files (AGENTS.md, SOUL.md, etc.)
|
||||
|
||||
@@ -1,239 +0,0 @@
|
||||
---
|
||||
summary: "Background task tracking for ACP runs, subagents, isolated cron jobs, and CLI operations"
|
||||
read_when:
|
||||
- Inspecting background work in progress or recently completed
|
||||
- Debugging delivery failures for detached agent runs
|
||||
- Understanding how background runs relate to sessions, cron, and heartbeat
|
||||
title: "Background Tasks"
|
||||
---
|
||||
|
||||
# Background Tasks
|
||||
|
||||
> **Cron vs Heartbeat vs Tasks?** See [Cron vs Heartbeat](/automation/cron-vs-heartbeat) for choosing the right scheduling mechanism. This page covers **tracking** background work, not scheduling it.
|
||||
|
||||
Background tasks track work that runs **outside your main conversation session**:
|
||||
ACP runs, subagent spawns, isolated cron job executions, and CLI-initiated operations.
|
||||
|
||||
Tasks do **not** replace sessions, cron jobs, or heartbeats — they are the **activity ledger** that records what detached work happened, when, and whether it succeeded.
|
||||
|
||||
<Note>
|
||||
Not every agent run creates a task. Heartbeat turns and normal interactive chat do not. All cron executions, ACP spawns, subagent spawns, and CLI agent commands do.
|
||||
</Note>
|
||||
|
||||
## TL;DR
|
||||
|
||||
- Tasks are **records**, not schedulers — cron and heartbeat decide _when_ work runs, tasks track _what happened_.
|
||||
- ACP, subagents, all cron jobs, and CLI operations create tasks. Heartbeat turns do not.
|
||||
- Each task moves through `queued → running → terminal` (succeeded, failed, timed_out, cancelled, or lost).
|
||||
- Completion notifications are delivered directly to a channel or queued for the next heartbeat.
|
||||
- `openclaw tasks list` shows all tasks; `openclaw tasks audit` surfaces issues.
|
||||
- Terminal records are kept for 7 days, then automatically pruned.
|
||||
|
||||
## Quick start
|
||||
|
||||
```bash
|
||||
# List all tasks (newest first)
|
||||
openclaw tasks list
|
||||
|
||||
# Filter by runtime or status
|
||||
openclaw tasks list --runtime acp
|
||||
openclaw tasks list --status running
|
||||
|
||||
# Show details for a specific task (by ID, run ID, or session key)
|
||||
openclaw tasks show <lookup>
|
||||
|
||||
# Cancel a running task (kills the child session)
|
||||
openclaw tasks cancel <lookup>
|
||||
|
||||
# Change notification policy for a task
|
||||
openclaw tasks notify <lookup> state_changes
|
||||
|
||||
# Run a health audit
|
||||
openclaw tasks audit
|
||||
```
|
||||
|
||||
## What creates a task
|
||||
|
||||
| Source | Runtime type | When a task record is created | Default notify policy |
|
||||
| ---------------------- | ------------ | ------------------------------------------------------ | --------------------- |
|
||||
| ACP background runs | `acp` | Spawning a child ACP session | `done_only` |
|
||||
| Subagent orchestration | `subagent` | Spawning a subagent via `sessions_spawn` | `done_only` |
|
||||
| Cron jobs (all types) | `cron` | Every cron execution (main-session and isolated) | `silent` |
|
||||
| CLI operations | `cli` | `openclaw agent` commands that run through the gateway | `done_only` |
|
||||
|
||||
Main-session cron tasks use `silent` notify policy by default — they create records for tracking but do not generate notifications. Isolated cron tasks also default to `silent` but are more visible because they run in their own session.
|
||||
|
||||
**What does not create tasks:**
|
||||
|
||||
- Heartbeat turns — main-session; see [Heartbeat](/gateway/heartbeat)
|
||||
- Normal interactive chat turns
|
||||
- Direct `/command` responses
|
||||
|
||||
## Task lifecycle
|
||||
|
||||
```mermaid
|
||||
stateDiagram-v2
|
||||
[*] --> queued
|
||||
queued --> running : agent starts
|
||||
running --> succeeded : completes ok
|
||||
running --> failed : error
|
||||
running --> timed_out : timeout exceeded
|
||||
running --> cancelled : operator cancels
|
||||
queued --> lost : session gone > 5 min
|
||||
running --> lost : session gone > 5 min
|
||||
```
|
||||
|
||||
| Status | What it means |
|
||||
| ----------- | -------------------------------------------------------------------------- |
|
||||
| `queued` | Created, waiting for the agent to start |
|
||||
| `running` | Agent turn is actively executing |
|
||||
| `succeeded` | Completed successfully |
|
||||
| `failed` | Completed with an error |
|
||||
| `timed_out` | Exceeded the configured timeout |
|
||||
| `cancelled` | Stopped by the operator via `openclaw tasks cancel` |
|
||||
| `lost` | Backing child session disappeared (detected after a 5-minute grace period) |
|
||||
|
||||
Transitions happen automatically — when the associated agent run ends, the task status updates to match.
|
||||
|
||||
## Delivery and notifications
|
||||
|
||||
When a task reaches a terminal state, OpenClaw notifies you. There are two delivery paths:
|
||||
|
||||
**Direct delivery** — if the task has a channel target (the `requesterOrigin`), the completion message goes straight to that channel (Telegram, Discord, Slack, etc.).
|
||||
|
||||
**Session-queued delivery** — if direct delivery fails or no origin is set, the update is queued as a system event in the requester's session and surfaces on the next heartbeat.
|
||||
|
||||
<Tip>
|
||||
Task completion triggers an immediate heartbeat wake so you see the result quickly — you do not have to wait for the next scheduled heartbeat tick.
|
||||
</Tip>
|
||||
|
||||
### Notification policies
|
||||
|
||||
Control how much you hear about each task:
|
||||
|
||||
| Policy | What is delivered |
|
||||
| --------------------- | ----------------------------------------------------------------------- |
|
||||
| `done_only` (default) | Only terminal state (succeeded, failed, etc.) — **this is the default** |
|
||||
| `state_changes` | Every state transition and progress update |
|
||||
| `silent` | Nothing at all |
|
||||
|
||||
Change the policy while a task is running:
|
||||
|
||||
```bash
|
||||
openclaw tasks notify <lookup> state_changes
|
||||
```
|
||||
|
||||
## CLI reference
|
||||
|
||||
### `tasks list`
|
||||
|
||||
```bash
|
||||
openclaw tasks list [--runtime <acp|subagent|cron|cli>] [--status <status>] [--json]
|
||||
```
|
||||
|
||||
Output columns: Task ID, Kind, Status, Delivery, Run ID, Child Session, Summary.
|
||||
|
||||
### `tasks show`
|
||||
|
||||
```bash
|
||||
openclaw tasks show <lookup>
|
||||
```
|
||||
|
||||
The lookup token accepts a task ID, run ID, or session key. Shows the full record including timing, delivery state, error, and terminal summary.
|
||||
|
||||
### `tasks cancel`
|
||||
|
||||
```bash
|
||||
openclaw tasks cancel <lookup>
|
||||
```
|
||||
|
||||
For ACP and subagent tasks, this kills the child session. Status transitions to `cancelled` and a delivery notification is sent.
|
||||
|
||||
### `tasks notify`
|
||||
|
||||
```bash
|
||||
openclaw tasks notify <lookup> <done_only|state_changes|silent>
|
||||
```
|
||||
|
||||
### `tasks audit`
|
||||
|
||||
```bash
|
||||
openclaw tasks audit [--json]
|
||||
```
|
||||
|
||||
Surfaces operational issues. Findings also appear in `openclaw status` when issues are detected.
|
||||
|
||||
| Finding | Severity | Trigger |
|
||||
| ------------------------- | -------- | ----------------------------------------------------- |
|
||||
| `stale_queued` | warn | Queued for more than 10 minutes |
|
||||
| `stale_running` | error | Running for more than 30 minutes |
|
||||
| `lost` | error | Backing session is gone |
|
||||
| `delivery_failed` | warn | Delivery failed and notify policy is not `silent` |
|
||||
| `missing_cleanup` | warn | Terminal task with no cleanup timestamp |
|
||||
| `inconsistent_timestamps` | warn | Timeline violation (for example ended before started) |
|
||||
|
||||
## Status integration (task pressure)
|
||||
|
||||
`openclaw status` includes an at-a-glance task summary:
|
||||
|
||||
```
|
||||
Tasks: 3 queued · 2 running · 1 issues
|
||||
```
|
||||
|
||||
The summary reports:
|
||||
|
||||
- **active** — count of `queued` + `running`
|
||||
- **failures** — count of `failed` + `timed_out` + `lost`
|
||||
- **byRuntime** — breakdown by `acp`, `subagent`, `cron`, `cli`
|
||||
|
||||
## Storage and maintenance
|
||||
|
||||
### Where tasks live
|
||||
|
||||
Task records persist in SQLite at:
|
||||
|
||||
```
|
||||
$OPENCLAW_STATE_DIR/tasks/runs.sqlite
|
||||
```
|
||||
|
||||
The registry loads into memory at gateway start and syncs writes to SQLite for durability across restarts.
|
||||
|
||||
### Automatic maintenance
|
||||
|
||||
A sweeper runs every **60 seconds** and handles three things:
|
||||
|
||||
1. **Reconciliation** — checks if active tasks' backing sessions still exist. If a child session has been gone for more than 5 minutes, the task is marked `lost`.
|
||||
2. **Cleanup stamping** — sets a `cleanupAfter` timestamp on terminal tasks (endedAt + 7 days).
|
||||
3. **Pruning** — deletes records past their `cleanupAfter` date.
|
||||
|
||||
**Retention**: terminal task records are kept for **7 days**, then automatically pruned. No configuration needed.
|
||||
|
||||
## How tasks relate to other systems
|
||||
|
||||
### Tasks and cron
|
||||
|
||||
A cron job **definition** lives in `~/.openclaw/cron/jobs.json`. **Every** cron execution creates a task record — both main-session and isolated. Main-session cron tasks default to `silent` notify policy so they track without generating notifications.
|
||||
|
||||
See [Cron Jobs](/automation/cron-jobs).
|
||||
|
||||
### Tasks and heartbeat
|
||||
|
||||
Heartbeat runs are main-session turns — they do not create task records. When a task completes, it can trigger a heartbeat wake so you see the result promptly.
|
||||
|
||||
See [Heartbeat](/gateway/heartbeat).
|
||||
|
||||
### Tasks and sessions
|
||||
|
||||
A task may reference a `childSessionKey` (where work runs) and a `requesterSessionKey` (who started it). Sessions are conversation context; tasks are activity tracking on top of that.
|
||||
|
||||
### Tasks and agent runs
|
||||
|
||||
A task's `runId` links to the agent run doing the work. Agent lifecycle events (start, end, error) automatically update the task status — you do not need to manage the lifecycle manually.
|
||||
|
||||
## Related
|
||||
|
||||
- [Automation Overview](/automation) — all automation mechanisms at a glance
|
||||
- [Cron Jobs](/automation/cron-jobs) — scheduling background work
|
||||
- [Cron vs Heartbeat](/automation/cron-vs-heartbeat) — choosing the right mechanism
|
||||
- [Heartbeat](/gateway/heartbeat) — periodic main-session turns
|
||||
- [CLI: Tasks](/cli/index#tasks) — CLI command reference
|
||||
@@ -420,11 +420,3 @@ Prefer `chat_guid` for stable routing:
|
||||
- For status/health info: `openclaw status --all` or `openclaw status --deep`.
|
||||
|
||||
For general channel workflow reference, see [Channels](/channels) and the [Plugins](/tools/plugin) guide.
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -1232,9 +1232,7 @@ High-signal Discord fields:
|
||||
## Related
|
||||
|
||||
- [Pairing](/channels/pairing)
|
||||
- [Groups](/channels/groups)
|
||||
- [Channel routing](/channels/channel-routing)
|
||||
- [Security](/gateway/security)
|
||||
- [Multi-agent routing](/concepts/multi-agent)
|
||||
- [Troubleshooting](/channels/troubleshooting)
|
||||
- [Slash commands](/tools/slash-commands)
|
||||
|
||||
@@ -750,11 +750,3 @@ Feishu currently exposes these runtime actions:
|
||||
- `channel-info`
|
||||
- `channel-list`
|
||||
- `react` and `reactions` when reactions are enabled in config
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -260,11 +260,3 @@ Related docs:
|
||||
- [Gateway configuration](/gateway/configuration)
|
||||
- [Security](/gateway/security)
|
||||
- [Reactions](/tools/reactions)
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -417,11 +417,3 @@ imsg send <handle> "test"
|
||||
- [Gateway configuration](/gateway/configuration)
|
||||
- [Pairing](/channels/pairing)
|
||||
- [BlueBubbles](/channels/bluebubbles)
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -240,11 +240,3 @@ Default account supports:
|
||||
- If the bot connects but never replies in channels, verify `channels.irc.groups` **and** whether mention-gating is dropping messages (`missing-mention`). If you want it to reply without pings, set `requireMention:false` for the channel.
|
||||
- If login fails, verify nick availability and server password.
|
||||
- If TLS fails on a custom network, verify host/port and certificate setup.
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -211,11 +211,3 @@ Generic media sends fall back to the existing image-only route when a LINE-speci
|
||||
and that the gateway is reachable from LINE.
|
||||
- **Media download errors:** raise `channels.line.mediaMaxMb` if media exceeds the
|
||||
default limit.
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -679,25 +679,6 @@ openclaw matrix account add \
|
||||
This opt-in only allows trusted private/internal targets. Public cleartext homeservers such as
|
||||
`http://matrix.example.org:8008` remain blocked. Prefer `https://` whenever possible.
|
||||
|
||||
## Proxying Matrix traffic
|
||||
|
||||
If your Matrix deployment needs an explicit outbound HTTP(S) proxy, set `channels.matrix.proxy`:
|
||||
|
||||
```json5
|
||||
{
|
||||
channels: {
|
||||
matrix: {
|
||||
homeserver: "https://matrix.example.org",
|
||||
accessToken: "syt_bot_xxx",
|
||||
proxy: "http://127.0.0.1:7890",
|
||||
},
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
Named accounts can override the top-level default with `channels.matrix.accounts.<id>.proxy`.
|
||||
OpenClaw uses the same proxy setting for runtime Matrix traffic and account status probes.
|
||||
|
||||
## Target resolution
|
||||
|
||||
Matrix accepts these target forms anywhere OpenClaw asks you for a room or user target:
|
||||
@@ -719,7 +700,6 @@ Live directory lookup uses the logged-in Matrix account:
|
||||
- `defaultAccount`: preferred account ID when multiple Matrix accounts are configured.
|
||||
- `homeserver`: homeserver URL, for example `https://matrix.example.org`.
|
||||
- `allowPrivateNetwork`: allow this Matrix account to connect to private/internal homeservers. Enable this when the homeserver resolves to `localhost`, a LAN/Tailscale IP, or an internal host such as `matrix-synapse`.
|
||||
- `proxy`: optional HTTP(S) proxy URL for Matrix traffic. Named accounts can override the top-level default with their own `proxy`.
|
||||
- `userId`: full Matrix user ID, for example `@bot:example.org`.
|
||||
- `accessToken`: access token for token-based auth. Plaintext values and SecretRef values are supported for `channels.matrix.accessToken` and `channels.matrix.accounts.<id>.accessToken` across env/file/exec providers. See [Secrets Management](/gateway/secrets).
|
||||
- `password`: password for password-based login. Plaintext values and SecretRef values are supported.
|
||||
@@ -753,11 +733,3 @@ Live directory lookup uses the logged-in Matrix account:
|
||||
- `groups`: per-room policy map. Prefer room IDs or aliases; unresolved room names are ignored at runtime. Session/group identity uses the stable room ID after resolution, while human-readable labels still come from room names.
|
||||
- `rooms`: legacy alias for `groups`.
|
||||
- `actions`: per-action tool gating (`messages`, `reactions`, `pins`, `profile`, `memberInfo`, `channelInfo`, `verification`).
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -425,11 +425,3 @@ Mattermost supports multiple accounts under `channels.mattermost.accounts`:
|
||||
- Gateway logs `missing _token in context`: the `_token` field is not in the button's context. Ensure it is included when building the integration payload.
|
||||
- Confirmation shows raw ID instead of button name: `context.action_id` does not match the button's `id`. Set both to the same sanitized value.
|
||||
- Agent doesn't know about buttons: add `capabilities: ["inlineButtons"]` to the Mattermost channel config.
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -779,11 +779,3 @@ Bots have limited support in private channels:
|
||||
- [RSC permissions reference](https://learn.microsoft.com/en-us/microsoftteams/platform/graph-api/rsc/resource-specific-consent)
|
||||
- [Teams bot file handling](https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/bots-filesv4) (channel/group requires Graph)
|
||||
- [Proactive messaging](https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/conversations/send-proactive-messages)
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -136,11 +136,3 @@ Provider options:
|
||||
- `channels.nextcloud-talk.blockStreaming`: disable block streaming for this channel.
|
||||
- `channels.nextcloud-talk.blockStreamingCoalesce`: block streaming coalesce tuning.
|
||||
- `channels.nextcloud-talk.mediaMaxMb`: inbound media cap (MB).
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -247,11 +247,3 @@ docker run -p 7777:7777 ghcr.io/hoytech/strfry
|
||||
- Direct messages only (no group chats).
|
||||
- No media attachments.
|
||||
- NIP-04 only (NIP-17 gift-wrap planned).
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -327,11 +327,3 @@ Related global options:
|
||||
- `agents.list[].groupChat.mentionPatterns` (Signal does not support native mentions).
|
||||
- `messages.groupChat.mentionPatterns` (global fallback).
|
||||
- `messages.responsePrefix`.
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -599,8 +599,6 @@ Primary reference:
|
||||
## Related
|
||||
|
||||
- [Pairing](/channels/pairing)
|
||||
- [Groups](/channels/groups)
|
||||
- [Security](/gateway/security)
|
||||
- [Channel routing](/channels/channel-routing)
|
||||
- [Troubleshooting](/channels/troubleshooting)
|
||||
- [Configuration](/gateway/configuration)
|
||||
|
||||
@@ -140,11 +140,3 @@ but duplicate exact paths are still rejected fail-closed. Prefer explicit per-ac
|
||||
- Prefer `dmPolicy: "allowlist"` for production.
|
||||
- Keep `dangerouslyAllowNameMatching` off unless you explicitly need legacy username-based reply delivery.
|
||||
- Keep `dangerouslyAllowInheritedWebhookPath` off unless you explicitly accept shared-path routing risk in a multi-account setup.
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -984,8 +984,6 @@ Telegram-specific high-signal fields:
|
||||
## Related
|
||||
|
||||
- [Pairing](/channels/pairing)
|
||||
- [Groups](/channels/groups)
|
||||
- [Security](/gateway/security)
|
||||
- [Channel routing](/channels/channel-routing)
|
||||
- [Multi-agent routing](/concepts/multi-agent)
|
||||
- [Troubleshooting](/channels/troubleshooting)
|
||||
|
||||
@@ -274,11 +274,3 @@ Provider options:
|
||||
- Thread replies: if the inbound message is in a thread, OpenClaw replies in-thread.
|
||||
- Rich text: Markdown formatting (bold, italic, code, headers, lists) is converted to Tlon's native format.
|
||||
- Images: URLs are uploaded to Tlon storage and embedded as image blocks.
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -377,11 +377,3 @@ Example:
|
||||
- **500 characters** per message (auto-chunked at word boundaries)
|
||||
- Markdown is stripped before chunking
|
||||
- No rate limiting (uses Twitch's built-in rate limits)
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -455,8 +455,6 @@ High-signal WhatsApp fields:
|
||||
## Related
|
||||
|
||||
- [Pairing](/channels/pairing)
|
||||
- [Groups](/channels/groups)
|
||||
- [Security](/gateway/security)
|
||||
- [Channel routing](/channels/channel-routing)
|
||||
- [Multi-agent routing](/concepts/multi-agent)
|
||||
- [Troubleshooting](/channels/troubleshooting)
|
||||
|
||||
@@ -241,11 +241,3 @@ Multi-account options:
|
||||
- `channels.zalo.accounts.<id>.webhookSecret`: per-account webhook secret.
|
||||
- `channels.zalo.accounts.<id>.webhookPath`: per-account webhook path.
|
||||
- `channels.zalo.accounts.<id>.proxy`: per-account proxy URL.
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -179,11 +179,3 @@ Accounts map to `zalouser` profiles in OpenClaw state. Example:
|
||||
|
||||
- Remove any old external `zca` process assumptions.
|
||||
- The channel now runs fully in OpenClaw without external CLI binaries.
|
||||
|
||||
## Related
|
||||
|
||||
- [Channels Overview](/channels) — all supported channels
|
||||
- [Pairing](/channels/pairing) — DM authentication and pairing flow
|
||||
- [Groups](/channels/groups) — group chat behavior and mention gating
|
||||
- [Channel Routing](/channels/channel-routing) — session routing for messages
|
||||
- [Security](/gateway/security) — access model and hardening
|
||||
|
||||
@@ -801,13 +801,12 @@ Notes:
|
||||
|
||||
### `tasks`
|
||||
|
||||
List and manage [background task](/automation/tasks) runs across agents.
|
||||
List and manage task runs across agents.
|
||||
|
||||
- `tasks list` — show active and recent task runs
|
||||
- `tasks show <id>` — show details for a specific task run
|
||||
- `tasks notify <id>` — change notification policy for a task run
|
||||
- `tasks notify <id>` — send a notification for a task run
|
||||
- `tasks cancel <id>` — cancel a running task
|
||||
- `tasks audit` — surface operational issues (stale, lost, delivery failures)
|
||||
|
||||
## Gateway
|
||||
|
||||
|
||||
@@ -156,11 +156,3 @@ See [Plugin hooks](/plugins/architecture#provider-runtime-hooks) for the hook AP
|
||||
- AbortSignal (cancel)
|
||||
- Gateway disconnect or RPC timeout
|
||||
- `agent.wait` timeout (wait-only, does not stop agent)
|
||||
|
||||
## Related
|
||||
|
||||
- [Tools](/tools) — available agent tools
|
||||
- [Hooks](/automation/hooks) — event-driven scripts triggered by agent lifecycle events
|
||||
- [Compaction](/concepts/compaction) — how long conversations are summarized
|
||||
- [Exec Approvals](/tools/exec-approvals) — approval gates for shell commands
|
||||
- [Thinking](/tools/thinking) — thinking/reasoning level configuration
|
||||
|
||||
@@ -550,11 +550,3 @@ If you need per-agent boundaries, use `agents.list[].tools` to deny `exec`.
|
||||
For group targeting, use `agents.list[].groupChat.mentionPatterns` so @mentions map cleanly to the intended agent.
|
||||
|
||||
See [Multi-Agent Sandbox & Tools](/tools/multi-agent-sandbox-tools) for detailed examples.
|
||||
|
||||
## Related
|
||||
|
||||
- [Channel Routing](/channels/channel-routing) — how messages route to agents
|
||||
- [Sub-Agents](/tools/subagents) — spawning background agent runs
|
||||
- [ACP Agents](/tools/acp-agents) — running external coding harnesses
|
||||
- [Presence](/concepts/presence) — agent presence and availability
|
||||
- [Session](/concepts/session) — session isolation and routing
|
||||
|
||||
@@ -79,7 +79,7 @@ Defaults: `debounceMs: 1000`, `cap: 20`, `drop: summarize`.
|
||||
|
||||
- Applies to auto-reply agent runs across all inbound channels that use the gateway reply pipeline (WhatsApp web, Telegram, Slack, Discord, Signal, iMessage, webchat, etc.).
|
||||
- Default lane (`main`) is process-wide for inbound + main heartbeats; set `agents.defaults.maxConcurrent` to allow multiple sessions in parallel.
|
||||
- Additional lanes may exist (e.g. `cron`, `subagent`) so background jobs can run in parallel without blocking inbound replies. These detached runs are tracked as [background tasks](/automation/tasks).
|
||||
- Additional lanes may exist (e.g. `cron`, `subagent`) so background jobs can run in parallel without blocking inbound replies.
|
||||
- Per-session lanes guarantee that only one agent run touches a given session at a time.
|
||||
- No external dependencies or background worker threads; pure TypeScript + promises.
|
||||
|
||||
|
||||
@@ -111,6 +111,3 @@ Preview with `openclaw sessions cleanup --dry-run`.
|
||||
- [Session Tools](/concepts/session-tool) -- agent tools for cross-session work
|
||||
- [Session Management Deep Dive](/reference/session-management-compaction) --
|
||||
store schema, transcripts, send policy, origin metadata, and advanced config
|
||||
- [Multi-Agent](/concepts/multi-agent) — routing and session isolation across agents
|
||||
- [Background Tasks](/automation/tasks) — how detached work creates task records with session references
|
||||
- [Channel Routing](/channels/channel-routing) — how inbound messages are routed to sessions
|
||||
|
||||
@@ -89,9 +89,3 @@ The system prompt includes:
|
||||
You can control the prompt format with `agents.defaults.timeFormat` (`auto` | `12` | `24`).
|
||||
|
||||
See [Date & Time](/date-time) for the full behavior and examples.
|
||||
|
||||
## Related
|
||||
|
||||
- [Heartbeat](/gateway/heartbeat) — active hours use timezone for scheduling
|
||||
- [Cron Jobs](/automation/cron-jobs) — cron expressions use timezone for scheduling
|
||||
- [Date & Time](/date-time) — full date/time behavior and examples
|
||||
|
||||
@@ -924,7 +924,6 @@
|
||||
"pages": [
|
||||
"install/ansible",
|
||||
"install/bun",
|
||||
"install/clawdock",
|
||||
"install/docker",
|
||||
"install/nix",
|
||||
"install/podman"
|
||||
@@ -990,6 +989,7 @@
|
||||
"channels/telegram",
|
||||
"channels/tlon",
|
||||
"channels/twitch",
|
||||
"plugins/voice-call",
|
||||
"channels/whatsapp",
|
||||
"channels/zalo",
|
||||
"channels/zalouser"
|
||||
@@ -1077,7 +1077,6 @@
|
||||
"tools/plugin",
|
||||
"plugins/community",
|
||||
"plugins/bundles",
|
||||
"plugins/voice-call",
|
||||
{
|
||||
"group": "Building Plugins",
|
||||
"pages": [
|
||||
@@ -1115,12 +1114,10 @@
|
||||
{
|
||||
"group": "Automation",
|
||||
"pages": [
|
||||
"automation/index",
|
||||
"automation/hooks",
|
||||
"automation/standing-orders",
|
||||
"automation/cron-jobs",
|
||||
"automation/cron-vs-heartbeat",
|
||||
"automation/tasks",
|
||||
"automation/troubleshooting",
|
||||
"automation/webhook",
|
||||
"automation/gmail-pubsub",
|
||||
@@ -1163,7 +1160,6 @@
|
||||
"tools/elevated",
|
||||
"tools/exec",
|
||||
"tools/exec-approvals",
|
||||
"tools/image-generation",
|
||||
"tools/llm-task",
|
||||
"tools/lobster",
|
||||
"tools/loop-detection",
|
||||
@@ -1350,28 +1346,18 @@
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Nodes and media",
|
||||
"group": "Nodes and devices",
|
||||
"pages": [
|
||||
"nodes/index",
|
||||
"nodes/troubleshooting",
|
||||
{
|
||||
"group": "Media capabilities",
|
||||
"pages": [
|
||||
"nodes/media-understanding",
|
||||
"nodes/images",
|
||||
"nodes/audio",
|
||||
"nodes/camera",
|
||||
"tools/tts"
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Node features",
|
||||
"pages": [
|
||||
"nodes/talk",
|
||||
"nodes/voicewake",
|
||||
"nodes/location-command"
|
||||
]
|
||||
}
|
||||
"nodes/media-understanding",
|
||||
"nodes/images",
|
||||
"nodes/audio",
|
||||
"nodes/camera",
|
||||
"nodes/talk",
|
||||
"nodes/voicewake",
|
||||
"nodes/location-command",
|
||||
"tools/tts"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -571,45 +571,6 @@ exec ssh -T gateway-host imsg "$@"
|
||||
|
||||
</Accordion>
|
||||
|
||||
### Matrix
|
||||
|
||||
Matrix is extension-backed and configured under `channels.matrix`.
|
||||
|
||||
```json5
|
||||
{
|
||||
channels: {
|
||||
matrix: {
|
||||
enabled: true,
|
||||
homeserver: "https://matrix.example.org",
|
||||
accessToken: "syt_bot_xxx",
|
||||
proxy: "http://127.0.0.1:7890",
|
||||
encryption: true,
|
||||
initialSyncLimit: 20,
|
||||
defaultAccount: "ops",
|
||||
accounts: {
|
||||
ops: {
|
||||
name: "Ops",
|
||||
userId: "@ops:example.org",
|
||||
accessToken: "syt_ops_xxx",
|
||||
},
|
||||
alerts: {
|
||||
userId: "@alerts:example.org",
|
||||
password: "secret",
|
||||
proxy: "http://127.0.0.1:7891",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
- Token auth uses `accessToken`; password auth uses `userId` + `password`.
|
||||
- `channels.matrix.proxy` routes Matrix HTTP traffic through an explicit HTTP(S) proxy. Named accounts can override it with `channels.matrix.accounts.<id>.proxy`.
|
||||
- `channels.matrix.allowPrivateNetwork` allows private/internal homeservers. `proxy` and `allowPrivateNetwork` are independent controls.
|
||||
- `channels.matrix.defaultAccount` selects the preferred account in multi-account setups.
|
||||
- Matrix status probes and live directory lookups use the same proxy policy as runtime traffic.
|
||||
- Full Matrix configuration, targeting rules, and setup examples are documented in [Matrix](/channels/matrix).
|
||||
|
||||
### Microsoft Teams
|
||||
|
||||
Microsoft Teams is extension-backed and configured under `channels.msteams`.
|
||||
@@ -3316,7 +3277,7 @@ Applies only to one-shot cron jobs. Recurring jobs use separate failure handling
|
||||
- `mode`: delivery mode — `"announce"` sends via a channel message; `"webhook"` posts to the configured webhook.
|
||||
- `accountId`: optional account or channel id to scope alert delivery.
|
||||
|
||||
See [Cron Jobs](/automation/cron-jobs). Isolated cron executions are tracked as [background tasks](/automation/tasks).
|
||||
See [Cron Jobs](/automation/cron-jobs).
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -13,9 +13,6 @@ title: "Heartbeat"
|
||||
Heartbeat runs **periodic agent turns** in the main session so the model can
|
||||
surface anything that needs attention without spamming you.
|
||||
|
||||
Heartbeat is a scheduled main-session turn — it does **not** create [background task](/automation/tasks) records.
|
||||
Task records are for detached work (ACP runs, subagents, isolated cron jobs).
|
||||
|
||||
Troubleshooting: [/automation/troubleshooting](/automation/troubleshooting)
|
||||
|
||||
## Quick start (beginner)
|
||||
@@ -68,8 +65,6 @@ The default prompt is intentionally broad:
|
||||
occasional lightweight “anything you need?” message, but avoids night-time spam
|
||||
by using your configured local timezone (see [/concepts/timezone](/concepts/timezone)).
|
||||
|
||||
Heartbeat can react to completed [background tasks](/automation/tasks), but a heartbeat run itself does not create a task record.
|
||||
|
||||
If you want a heartbeat to do something very specific (e.g. “check Gmail PubSub
|
||||
stats” or “verify gateway health”), set `agents.defaults.heartbeat.prompt` (or
|
||||
`agents.list[].heartbeat.prompt`) to a custom body (sent verbatim).
|
||||
@@ -258,7 +253,6 @@ Use `accountId` to target a specific account on multi-account channels like Tele
|
||||
outbound message is sent.
|
||||
- Heartbeat-only replies do **not** keep the session alive; the last `updatedAt`
|
||||
is restored so idle expiry behaves normally.
|
||||
- Detached [background tasks](/automation/tasks) can enqueue a system event and wake heartbeat when the main session should notice something quickly. That wake does not make the heartbeat run a background task.
|
||||
|
||||
## Visibility controls
|
||||
|
||||
@@ -397,11 +391,3 @@ Heartbeats run full agent turns. Shorter intervals burn more tokens. To reduce c
|
||||
- Set a cheaper `model` (e.g. `ollama/llama3.2:1b`).
|
||||
- Keep `HEARTBEAT.md` small.
|
||||
- Use `target: "none"` if you only want internal state updates.
|
||||
|
||||
## Related
|
||||
|
||||
- [Automation Overview](/automation) — all automation mechanisms at a glance
|
||||
- [Cron vs Heartbeat](/automation/cron-vs-heartbeat) — when to use each
|
||||
- [Background Tasks](/automation/tasks) — how detached work is tracked
|
||||
- [Timezone](/concepts/timezone) — how timezone affects heartbeat scheduling
|
||||
- [Troubleshooting](/automation/troubleshooting) — debugging automation issues
|
||||
|
||||
@@ -952,7 +952,7 @@ for usage/billing and raise limits as needed.
|
||||
Token tip: long tasks and sub-agents both consume tokens. If cost is a concern, set a
|
||||
cheaper model for sub-agents via `agents.defaults.subagents.model`.
|
||||
|
||||
Docs: [Sub-agents](/tools/subagents), [Background Tasks](/automation/tasks).
|
||||
Docs: [Sub-agents](/tools/subagents).
|
||||
|
||||
</Accordion>
|
||||
|
||||
|
||||
@@ -266,51 +266,6 @@ flowchart TD
|
||||
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="Exec suddenly asks for approval">
|
||||
```bash
|
||||
openclaw config get tools.exec.host
|
||||
openclaw config get tools.exec.security
|
||||
openclaw config get tools.exec.ask
|
||||
openclaw gateway restart
|
||||
```
|
||||
|
||||
What changed:
|
||||
|
||||
- If `tools.exec.host` is unset, the default is `auto`.
|
||||
- `host=auto` resolves to `sandbox` when a sandbox runtime is active, `gateway` otherwise.
|
||||
- On `gateway` and `node`, unset `tools.exec.security` defaults to `allowlist`.
|
||||
- Unset `tools.exec.ask` defaults to `on-miss`.
|
||||
- Result: ordinary host commands can now pause with `Approval required` instead of running immediately.
|
||||
|
||||
Restore the old gateway no-approval behavior:
|
||||
|
||||
```bash
|
||||
openclaw config set tools.exec.host gateway
|
||||
openclaw config set tools.exec.security full
|
||||
openclaw config set tools.exec.ask off
|
||||
openclaw gateway restart
|
||||
```
|
||||
|
||||
Safer alternatives:
|
||||
|
||||
- Set only `tools.exec.host=gateway` if you just want stable host routing and still want approvals.
|
||||
- Keep `security=allowlist` with `ask=on-miss` if you want host exec but still want review on allowlist misses.
|
||||
- Enable sandbox mode if you want `host=auto` to resolve back to `sandbox`.
|
||||
|
||||
Common log signatures:
|
||||
|
||||
- `Approval required.` → command is waiting on `/approve ...`.
|
||||
- `SYSTEM_RUN_DENIED: approval required` → node-host exec approval is pending.
|
||||
- `exec host=sandbox requires a sandbox runtime for this session` → implicit/explicit sandbox selection but sandbox mode is off.
|
||||
|
||||
Deep pages:
|
||||
|
||||
- [/tools/exec](/tools/exec)
|
||||
- [/tools/exec-approvals](/tools/exec-approvals)
|
||||
- [/gateway/security#runtime-expectation-drift](/gateway/security#runtime-expectation-drift)
|
||||
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="Browser tool fails">
|
||||
```bash
|
||||
openclaw status
|
||||
|
||||
@@ -13,7 +13,7 @@ title: "ACP Agents"
|
||||
|
||||
[Agent Client Protocol (ACP)](https://agentclientprotocol.com/) sessions let OpenClaw run external coding harnesses (for example Pi, Claude Code, Codex, Cursor, Copilot, OpenClaw ACP, OpenCode, Gemini CLI, and other supported ACPX harnesses) through an ACP backend plugin.
|
||||
|
||||
If you ask OpenClaw in plain language to "run this in Codex" or "start Claude Code in a thread", OpenClaw should route that request to the ACP runtime (not the native sub-agent runtime). Each ACP session spawn is tracked as a [background task](/automation/tasks).
|
||||
If you ask OpenClaw in plain language to "run this in Codex" or "start Claude Code in a thread", OpenClaw should route that request to the ACP runtime (not the native sub-agent runtime).
|
||||
|
||||
If you want Codex or Claude Code to connect as an external MCP client directly
|
||||
to existing OpenClaw channel conversations, use [`openclaw mcp serve`](/cli/mcp)
|
||||
|
||||
@@ -1,125 +0,0 @@
|
||||
---
|
||||
summary: "Generate and edit images using configured providers (OpenAI, Google Gemini, fal, MiniMax)"
|
||||
read_when:
|
||||
- Generating images via the agent
|
||||
- Configuring image generation providers and models
|
||||
- Understanding the image_generate tool parameters
|
||||
title: "Image Generation"
|
||||
---
|
||||
|
||||
# Image Generation
|
||||
|
||||
The `image_generate` tool lets the agent create and edit images using your configured providers. Generated images are delivered automatically as media attachments in the agent's reply.
|
||||
|
||||
<Note>
|
||||
The tool only appears when at least one image generation provider is available. If you don't see `image_generate` in your agent's tools, configure `agents.defaults.imageGenerationModel` or set up a provider API key.
|
||||
</Note>
|
||||
|
||||
## Quick start
|
||||
|
||||
1. Set an API key for at least one provider (for example `OPENAI_API_KEY` or `GEMINI_API_KEY`).
|
||||
2. Optionally set your preferred model:
|
||||
|
||||
```json5
|
||||
{
|
||||
agents: {
|
||||
defaults: {
|
||||
imageGenerationModel: "openai/gpt-image-1",
|
||||
},
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
3. Ask the agent: _"Generate an image of a friendly lobster mascot."_
|
||||
|
||||
The agent calls `image_generate` automatically. No tool allow-listing needed — it's enabled by default when a provider is available.
|
||||
|
||||
## Supported providers
|
||||
|
||||
| Provider | Default model | Edit support | API key |
|
||||
|---|---|---|---|
|
||||
| OpenAI | `gpt-image-1` | No | `OPENAI_API_KEY` |
|
||||
| Google | `gemini-3.1-flash-image-preview` | Yes | `GEMINI_API_KEY` or `GOOGLE_API_KEY` |
|
||||
| fal | `fal-ai/flux/dev` | Yes | `FAL_KEY` |
|
||||
| MiniMax | `image-01` | Yes (subject reference) | `MINIMAX_API_KEY` |
|
||||
|
||||
Use `action: "list"` to inspect available providers and models at runtime:
|
||||
|
||||
```
|
||||
/tool image_generate action=list
|
||||
```
|
||||
|
||||
## Tool parameters
|
||||
|
||||
| Parameter | Type | Description |
|
||||
|---|---|---|
|
||||
| `prompt` | string | Image generation prompt (required for `action: "generate"`) |
|
||||
| `action` | string | `"generate"` (default) or `"list"` to inspect providers |
|
||||
| `model` | string | Provider/model override, e.g. `openai/gpt-image-1` |
|
||||
| `image` | string | Single reference image path or URL for edit mode |
|
||||
| `images` | string[] | Multiple reference images for edit mode (up to 5) |
|
||||
| `size` | string | Size hint: `1024x1024`, `1536x1024`, `1024x1536`, `1024x1792`, `1792x1024` |
|
||||
| `aspectRatio` | string | Aspect ratio: `1:1`, `2:3`, `3:2`, `3:4`, `4:3`, `4:5`, `5:4`, `9:16`, `16:9`, `21:9` |
|
||||
| `resolution` | string | Resolution hint: `1K`, `2K`, or `4K` |
|
||||
| `count` | number | Number of images to generate (1–4) |
|
||||
| `filename` | string | Output filename hint |
|
||||
|
||||
Not all providers support all parameters. The tool passes what each provider supports and ignores the rest.
|
||||
|
||||
## Configuration
|
||||
|
||||
### Model selection
|
||||
|
||||
```json5
|
||||
{
|
||||
agents: {
|
||||
defaults: {
|
||||
// String form: primary model only
|
||||
imageGenerationModel: "google/gemini-3-pro-image-preview",
|
||||
|
||||
// Object form: primary + ordered fallbacks
|
||||
imageGenerationModel: {
|
||||
primary: "openai/gpt-image-1",
|
||||
fallbacks: ["google/gemini-3.1-flash-image-preview", "fal/fal-ai/flux/dev"],
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
### Provider selection order
|
||||
|
||||
When generating an image, OpenClaw tries providers in this order:
|
||||
|
||||
1. **`model` parameter** from the tool call (if the agent specifies one)
|
||||
2. **`imageGenerationModel.primary`** from config
|
||||
3. **`imageGenerationModel.fallbacks`** in order
|
||||
4. **Auto-detection** — queries all registered providers for defaults, preferring: configured primary provider, then OpenAI, then Google, then others
|
||||
|
||||
If a provider fails (auth error, rate limit, etc.), the next candidate is tried automatically. If all fail, the error includes details from each attempt.
|
||||
|
||||
### Image editing
|
||||
|
||||
Google, fal, and MiniMax support editing reference images. Pass a reference image path or URL:
|
||||
|
||||
```
|
||||
"Generate a watercolor version of this photo" + image: "/path/to/photo.jpg"
|
||||
```
|
||||
|
||||
Google supports up to 5 reference images via the `images` parameter. fal and MiniMax support 1.
|
||||
|
||||
## Provider capabilities
|
||||
|
||||
| Capability | OpenAI | Google | fal | MiniMax |
|
||||
|---|---|---|---|---|
|
||||
| Generate | Yes (up to 4) | Yes (up to 4) | Yes (up to 4) | Yes (up to 9) |
|
||||
| Edit/reference | No | Yes (up to 5 images) | Yes (1 image) | Yes (1 image, subject ref) |
|
||||
| Size control | Yes | Yes | Yes | No |
|
||||
| Aspect ratio | No | Yes | Yes (generate only) | Yes |
|
||||
| Resolution (1K/2K/4K) | No | Yes | Yes | No |
|
||||
|
||||
## Related
|
||||
|
||||
- [Tools Overview](/tools) — all available agent tools
|
||||
- [Configuration Reference](/gateway/configuration-reference#agent-defaults) — `imageGenerationModel` config
|
||||
- [Models](/concepts/models) — model configuration and failover
|
||||
@@ -9,7 +9,7 @@ title: "Sub-Agents"
|
||||
|
||||
# Sub-agents
|
||||
|
||||
Sub-agents are background agent runs spawned from an existing agent run. They run in their own session (`agent:<agentId>:subagent:<uuid>`) and, when finished, **announce** their result back to the requester chat channel. Each sub-agent run is tracked as a [background task](/automation/tasks).
|
||||
Sub-agents are background agent runs spawned from an existing agent run. They run in their own session (`agent:<agentId>:subagent:<uuid>`) and, when finished, **announce** their result back to the requester chat channel.
|
||||
|
||||
## Slash command
|
||||
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createDiscordNativeApprovalAdapter } from "./approval-native.js";
|
||||
|
||||
describe("createDiscordNativeApprovalAdapter", () => {
|
||||
it("normalizes prefixed turn-source channel ids", async () => {
|
||||
const adapter = createDiscordNativeApprovalAdapter();
|
||||
|
||||
const target = await adapter.native?.resolveOriginTarget?.({
|
||||
cfg: {} as never,
|
||||
accountId: "main",
|
||||
approvalKind: "plugin",
|
||||
request: {
|
||||
id: "abc",
|
||||
request: {
|
||||
title: "Plugin approval",
|
||||
turnSourceChannel: "discord",
|
||||
turnSourceTo: "channel:123456789",
|
||||
turnSourceAccountId: "main",
|
||||
},
|
||||
createdAtMs: 1,
|
||||
expiresAtMs: 2,
|
||||
},
|
||||
});
|
||||
|
||||
expect(target).toEqual({ to: "123456789" });
|
||||
});
|
||||
|
||||
it("falls back to extracting the channel id from the session key", async () => {
|
||||
const adapter = createDiscordNativeApprovalAdapter();
|
||||
|
||||
const target = await adapter.native?.resolveOriginTarget?.({
|
||||
cfg: {} as never,
|
||||
accountId: "main",
|
||||
approvalKind: "plugin",
|
||||
request: {
|
||||
id: "abc",
|
||||
request: {
|
||||
title: "Plugin approval",
|
||||
sessionKey: "agent:main:discord:channel:987654321",
|
||||
},
|
||||
createdAtMs: 1,
|
||||
expiresAtMs: 2,
|
||||
},
|
||||
});
|
||||
|
||||
expect(target).toEqual({ to: "987654321" });
|
||||
});
|
||||
});
|
||||
@@ -1,150 +0,0 @@
|
||||
import { createApproverRestrictedNativeApprovalAdapter } from "openclaw/plugin-sdk/approval-runtime";
|
||||
import type { DiscordExecApprovalConfig, OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import type {
|
||||
ExecApprovalRequest,
|
||||
ExecApprovalSessionTarget,
|
||||
PluginApprovalRequest,
|
||||
} from "openclaw/plugin-sdk/infra-runtime";
|
||||
import { resolveExecApprovalSessionTarget } from "openclaw/plugin-sdk/approval-runtime";
|
||||
import { normalizeAccountId } from "openclaw/plugin-sdk/routing";
|
||||
import { listDiscordAccountIds, resolveDiscordAccount } from "./accounts.js";
|
||||
import {
|
||||
getDiscordExecApprovalApprovers,
|
||||
isDiscordExecApprovalApprover,
|
||||
isDiscordExecApprovalClientEnabled,
|
||||
} from "./exec-approvals.js";
|
||||
|
||||
type ApprovalRequest = ExecApprovalRequest | PluginApprovalRequest;
|
||||
|
||||
export function extractDiscordChannelId(sessionKey?: string | null): string | null {
|
||||
if (!sessionKey) {
|
||||
return null;
|
||||
}
|
||||
const match = sessionKey.match(/discord:(?:channel|group):(\d+)/);
|
||||
return match ? match[1] : null;
|
||||
}
|
||||
|
||||
function isExecApprovalRequest(request: ApprovalRequest): request is ExecApprovalRequest {
|
||||
return "command" in request.request;
|
||||
}
|
||||
|
||||
function toExecLikeRequest(request: ApprovalRequest): ExecApprovalRequest {
|
||||
if (isExecApprovalRequest(request)) {
|
||||
return request;
|
||||
}
|
||||
return {
|
||||
id: request.id,
|
||||
request: {
|
||||
command: request.request.title,
|
||||
sessionKey: request.request.sessionKey ?? undefined,
|
||||
turnSourceChannel: request.request.turnSourceChannel ?? undefined,
|
||||
turnSourceTo: request.request.turnSourceTo ?? undefined,
|
||||
turnSourceAccountId: request.request.turnSourceAccountId ?? undefined,
|
||||
},
|
||||
createdAtMs: request.createdAtMs,
|
||||
expiresAtMs: request.expiresAtMs,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeDiscordOriginChannelId(value?: string | null): string | null {
|
||||
if (!value) {
|
||||
return null;
|
||||
}
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed) {
|
||||
return null;
|
||||
}
|
||||
const prefixed = trimmed.match(/^(?:channel|group):(\d+)$/i);
|
||||
if (prefixed) {
|
||||
return prefixed[1];
|
||||
}
|
||||
return /^\d+$/.test(trimmed) ? trimmed : null;
|
||||
}
|
||||
|
||||
function resolveRequestSessionTarget(params: {
|
||||
cfg: OpenClawConfig;
|
||||
request: ApprovalRequest;
|
||||
}): ExecApprovalSessionTarget | null {
|
||||
const execLikeRequest = toExecLikeRequest(params.request);
|
||||
return resolveExecApprovalSessionTarget({
|
||||
cfg: params.cfg,
|
||||
request: execLikeRequest,
|
||||
turnSourceChannel: execLikeRequest.request.turnSourceChannel ?? undefined,
|
||||
turnSourceTo: execLikeRequest.request.turnSourceTo ?? undefined,
|
||||
turnSourceAccountId: execLikeRequest.request.turnSourceAccountId ?? undefined,
|
||||
});
|
||||
}
|
||||
|
||||
function resolveDiscordOriginTarget(params: {
|
||||
cfg: OpenClawConfig;
|
||||
accountId?: string | null;
|
||||
request: ApprovalRequest;
|
||||
}) {
|
||||
const turnSourceChannel = params.request.request.turnSourceChannel?.trim().toLowerCase() || "";
|
||||
const turnSourceTo = normalizeDiscordOriginChannelId(params.request.request.turnSourceTo);
|
||||
const turnSourceAccountId = params.request.request.turnSourceAccountId?.trim() || "";
|
||||
if (turnSourceChannel === "discord" && turnSourceTo) {
|
||||
if (
|
||||
params.accountId &&
|
||||
turnSourceAccountId &&
|
||||
normalizeAccountId(turnSourceAccountId) !== normalizeAccountId(params.accountId)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return { to: turnSourceTo };
|
||||
}
|
||||
|
||||
const sessionTarget = resolveRequestSessionTarget(params);
|
||||
if (!sessionTarget || sessionTarget.channel !== "discord") {
|
||||
const channelId = extractDiscordChannelId(params.request.request.sessionKey?.trim() || null);
|
||||
return channelId ? { to: channelId } : null;
|
||||
}
|
||||
if (
|
||||
params.accountId &&
|
||||
sessionTarget.accountId &&
|
||||
normalizeAccountId(sessionTarget.accountId) !== normalizeAccountId(params.accountId)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const targetTo = normalizeDiscordOriginChannelId(sessionTarget.to);
|
||||
return targetTo ? { to: targetTo } : null;
|
||||
}
|
||||
|
||||
function resolveDiscordApproverDmTargets(params: {
|
||||
cfg: OpenClawConfig;
|
||||
accountId?: string | null;
|
||||
configOverride?: DiscordExecApprovalConfig | null;
|
||||
}) {
|
||||
return getDiscordExecApprovalApprovers({
|
||||
cfg: params.cfg,
|
||||
accountId: params.accountId,
|
||||
configOverride: params.configOverride,
|
||||
}).map((approver) => ({ to: String(approver) }));
|
||||
}
|
||||
|
||||
export function createDiscordNativeApprovalAdapter(
|
||||
configOverride?: DiscordExecApprovalConfig | null,
|
||||
) {
|
||||
return createApproverRestrictedNativeApprovalAdapter({
|
||||
channel: "discord",
|
||||
channelLabel: "Discord",
|
||||
listAccountIds: listDiscordAccountIds,
|
||||
hasApprovers: ({ cfg, accountId }) =>
|
||||
getDiscordExecApprovalApprovers({ cfg, accountId, configOverride }).length > 0,
|
||||
isExecAuthorizedSender: ({ cfg, accountId, senderId }) =>
|
||||
isDiscordExecApprovalApprover({ cfg, accountId, senderId, configOverride }),
|
||||
isNativeDeliveryEnabled: ({ cfg, accountId }) =>
|
||||
isDiscordExecApprovalClientEnabled({ cfg, accountId, configOverride }),
|
||||
resolveNativeDeliveryMode: ({ cfg, accountId }) =>
|
||||
configOverride?.target ??
|
||||
resolveDiscordAccount({ cfg, accountId }).config.execApprovals?.target ??
|
||||
"dm",
|
||||
resolveOriginTarget: ({ cfg, accountId, request }) =>
|
||||
resolveDiscordOriginTarget({ cfg, accountId, request }),
|
||||
resolveApproverDmTargets: ({ cfg, accountId }) =>
|
||||
resolveDiscordApproverDmTargets({ cfg, accountId, configOverride }),
|
||||
notifyOriginWhenDmOnly: true,
|
||||
});
|
||||
}
|
||||
|
||||
export const discordNativeApprovalAdapter = createDiscordNativeApprovalAdapter();
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
createAccountScopedAllowlistNameResolver,
|
||||
createNestedAllowlistOverrideResolver,
|
||||
} from "openclaw/plugin-sdk/allowlist-config-edit";
|
||||
import { createApproverRestrictedNativeApprovalAdapter } from "openclaw/plugin-sdk/approval-runtime";
|
||||
import { createScopedDmSecurityResolver } from "openclaw/plugin-sdk/channel-config-helpers";
|
||||
import { createPairingPrefixStripper } from "openclaw/plugin-sdk/channel-pairing";
|
||||
import { createOpenProviderConfiguredRouteWarningCollector } from "openclaw/plugin-sdk/channel-policy";
|
||||
@@ -27,13 +28,17 @@ import {
|
||||
resolveDiscordAccount,
|
||||
type ResolvedDiscordAccount,
|
||||
} from "./accounts.js";
|
||||
import { discordNativeApprovalAdapter } from "./approval-native.js";
|
||||
import { auditDiscordChannelPermissions, collectDiscordAuditChannelIds } from "./audit.js";
|
||||
import {
|
||||
listDiscordDirectoryGroupsFromConfig,
|
||||
listDiscordDirectoryPeersFromConfig,
|
||||
} from "./directory-config.js";
|
||||
import { shouldSuppressLocalDiscordExecApprovalPrompt } from "./exec-approvals.js";
|
||||
import {
|
||||
getDiscordExecApprovalApprovers,
|
||||
isDiscordExecApprovalApprover,
|
||||
isDiscordExecApprovalClientEnabled,
|
||||
shouldSuppressLocalDiscordExecApprovalPrompt,
|
||||
} from "./exec-approvals.js";
|
||||
import {
|
||||
resolveDiscordGroupRequireMention,
|
||||
resolveDiscordGroupToolPolicy,
|
||||
@@ -146,6 +151,20 @@ function buildDiscordCrossContextComponents(params: {
|
||||
return [new DiscordUiContainer({ cfg: params.cfg, accountId: params.accountId, components })];
|
||||
}
|
||||
|
||||
const discordNativeApprovalAdapter = createApproverRestrictedNativeApprovalAdapter({
|
||||
channel: "discord",
|
||||
channelLabel: "Discord",
|
||||
listAccountIds: listDiscordAccountIds,
|
||||
hasApprovers: ({ cfg, accountId }) =>
|
||||
getDiscordExecApprovalApprovers({ cfg, accountId }).length > 0,
|
||||
isExecAuthorizedSender: ({ cfg, accountId, senderId }) =>
|
||||
isDiscordExecApprovalApprover({ cfg, accountId, senderId }),
|
||||
isNativeDeliveryEnabled: ({ cfg, accountId }) =>
|
||||
isDiscordExecApprovalClientEnabled({ cfg, accountId }),
|
||||
resolveNativeDeliveryMode: ({ cfg, accountId }) =>
|
||||
resolveDiscordAccount({ cfg, accountId }).config.execApprovals?.target ?? "dm",
|
||||
});
|
||||
|
||||
const resolveDiscordAllowlistGroupOverrides = createNestedAllowlistOverrideResolver({
|
||||
resolveRecord: (account: ResolvedDiscordAccount) => account.config.guilds,
|
||||
outerLabel: (guildKey) => `guild ${guildKey}`,
|
||||
@@ -328,7 +347,6 @@ export const discordPlugin: ChannelPlugin<ResolvedDiscordAccount, DiscordProbe>
|
||||
auth: discordNativeApprovalAdapter.auth,
|
||||
approvals: {
|
||||
delivery: discordNativeApprovalAdapter.delivery,
|
||||
native: discordNativeApprovalAdapter.native,
|
||||
},
|
||||
directory: createChannelDirectoryAdapter({
|
||||
listPeers: async (params) => listDiscordDirectoryPeersFromConfig(params),
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { getExecApprovalReplyMetadata } from "openclaw/plugin-sdk/approval-runtime";
|
||||
import { resolveApprovalApprovers } from "openclaw/plugin-sdk/approval-runtime";
|
||||
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import type { DiscordExecApprovalConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import type { ReplyPayload } from "openclaw/plugin-sdk/reply-runtime";
|
||||
import { resolveDiscordAccount } from "./accounts.js";
|
||||
import { parseDiscordTarget } from "./targets.js";
|
||||
@@ -25,11 +24,10 @@ function normalizeDiscordApproverId(value: string): string | undefined {
|
||||
export function getDiscordExecApprovalApprovers(params: {
|
||||
cfg: OpenClawConfig;
|
||||
accountId?: string | null;
|
||||
configOverride?: DiscordExecApprovalConfig | null;
|
||||
}): string[] {
|
||||
const account = resolveDiscordAccount(params).config;
|
||||
return resolveApprovalApprovers({
|
||||
explicit: params.configOverride?.approvers ?? account.execApprovals?.approvers,
|
||||
explicit: account.execApprovals?.approvers,
|
||||
allowFrom: account.allowFrom,
|
||||
extraAllowFrom: account.dm?.allowFrom,
|
||||
defaultTo: account.defaultTo,
|
||||
@@ -48,34 +46,21 @@ export function getDiscordExecApprovalApprovers(params: {
|
||||
export function isDiscordExecApprovalClientEnabled(params: {
|
||||
cfg: OpenClawConfig;
|
||||
accountId?: string | null;
|
||||
configOverride?: DiscordExecApprovalConfig | null;
|
||||
}): boolean {
|
||||
const config = params.configOverride ?? resolveDiscordAccount(params).config.execApprovals;
|
||||
return Boolean(
|
||||
config?.enabled &&
|
||||
getDiscordExecApprovalApprovers({
|
||||
cfg: params.cfg,
|
||||
accountId: params.accountId,
|
||||
configOverride: params.configOverride,
|
||||
}).length > 0,
|
||||
);
|
||||
const config = resolveDiscordAccount(params).config.execApprovals;
|
||||
return Boolean(config?.enabled && getDiscordExecApprovalApprovers(params).length > 0);
|
||||
}
|
||||
|
||||
export function isDiscordExecApprovalApprover(params: {
|
||||
cfg: OpenClawConfig;
|
||||
accountId?: string | null;
|
||||
senderId?: string | null;
|
||||
configOverride?: DiscordExecApprovalConfig | null;
|
||||
}): boolean {
|
||||
const senderId = params.senderId?.trim();
|
||||
if (!senderId) {
|
||||
return false;
|
||||
}
|
||||
return getDiscordExecApprovalApprovers({
|
||||
cfg: params.cfg,
|
||||
accountId: params.accountId,
|
||||
configOverride: params.configOverride,
|
||||
}).includes(senderId);
|
||||
return getDiscordExecApprovalApprovers(params).includes(senderId);
|
||||
}
|
||||
|
||||
export function shouldSuppressLocalDiscordExecApprovalPrompt(params: {
|
||||
|
||||
@@ -3,9 +3,9 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import type { ButtonInteraction, ComponentData } from "@buape/carbon";
|
||||
import { Routes } from "discord-api-types/v10";
|
||||
import { clearSessionStoreCacheForTest } from "openclaw/plugin-sdk/config-runtime";
|
||||
import type { DiscordExecApprovalConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { clearSessionStoreCacheForTest } from "../../../../src/config/sessions.js";
|
||||
import type { DiscordExecApprovalConfig } from "../../../../src/config/types.discord.js";
|
||||
|
||||
const STORE_PATH = path.join(os.tmpdir(), "openclaw-exec-approvals-test.json");
|
||||
|
||||
@@ -46,7 +46,9 @@ const mockRestPatch = vi.hoisted(() => vi.fn());
|
||||
const mockRestDelete = vi.hoisted(() => vi.fn());
|
||||
const gatewayClientStarts = vi.hoisted(() => vi.fn());
|
||||
const gatewayClientStops = vi.hoisted(() => vi.fn());
|
||||
const gatewayClientRequests = vi.hoisted(() => vi.fn(async (..._args: unknown[]) => ({ ok: true })));
|
||||
const gatewayClientRequests = vi.hoisted(() =>
|
||||
vi.fn(async (_method?: string, _params?: unknown) => ({ ok: true })),
|
||||
);
|
||||
const gatewayClientParams = vi.hoisted(() => [] as Array<Record<string, unknown>>);
|
||||
const mockGatewayClientCtor = vi.hoisted(() => vi.fn());
|
||||
const mockResolveGatewayConnectionAuth = vi.hoisted(() => vi.fn());
|
||||
@@ -85,8 +87,8 @@ vi.mock("openclaw/plugin-sdk/gateway-runtime", async (importOriginal) => {
|
||||
stop() {
|
||||
gatewayClientStops();
|
||||
}
|
||||
async request(...args: unknown[]) {
|
||||
return gatewayClientRequests(...args);
|
||||
async request(method: string, params?: unknown) {
|
||||
return gatewayClientRequests(method, params);
|
||||
}
|
||||
}
|
||||
return {
|
||||
@@ -126,9 +128,56 @@ vi.mock("openclaw/plugin-sdk/gateway-runtime", async (importOriginal) => {
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("../../../../src/gateway/operator-approvals-client.js", async () => {
|
||||
class MockGatewayClient {
|
||||
private params: Record<string, unknown>;
|
||||
vi.mock("../../../../src/gateway/operator-approvals-client.js", () => ({
|
||||
createOperatorApprovalsGatewayClient: async (params: {
|
||||
config?: unknown;
|
||||
gatewayUrl?: string;
|
||||
clientDisplayName?: string;
|
||||
onEvent?: unknown;
|
||||
onHelloOk?: unknown;
|
||||
onConnectError?: unknown;
|
||||
onClose?: unknown;
|
||||
}) => {
|
||||
mockCreateOperatorApprovalsGatewayClient(params);
|
||||
const envUrl = process.env.OPENCLAW_GATEWAY_URL?.trim();
|
||||
const gatewayUrl = params.gatewayUrl?.trim() || envUrl || "ws://127.0.0.1:18789";
|
||||
const urlOverrideSource = params.gatewayUrl?.trim() ? "cli" : envUrl ? "env" : undefined;
|
||||
const auth = await mockResolveGatewayConnectionAuth({
|
||||
config: params.config,
|
||||
env: process.env,
|
||||
...(urlOverrideSource
|
||||
? {
|
||||
urlOverride: gatewayUrl,
|
||||
urlOverrideSource,
|
||||
}
|
||||
: {}),
|
||||
});
|
||||
const clientParams = {
|
||||
url: gatewayUrl,
|
||||
token: auth?.token,
|
||||
password: auth?.password,
|
||||
clientName: "gateway-client",
|
||||
clientDisplayName: params.clientDisplayName,
|
||||
mode: "backend",
|
||||
scopes: ["operator.approvals"],
|
||||
onEvent: params.onEvent,
|
||||
onHelloOk: params.onHelloOk,
|
||||
onConnectError: params.onConnectError,
|
||||
onClose: params.onClose,
|
||||
};
|
||||
gatewayClientParams.push(clientParams);
|
||||
mockGatewayClientCtor(clientParams);
|
||||
return {
|
||||
start: gatewayClientStarts,
|
||||
stop: gatewayClientStops,
|
||||
request: gatewayClientRequests,
|
||||
};
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("../../../../src/gateway/client.js", () => ({
|
||||
GatewayClient: class {
|
||||
params: Record<string, unknown>;
|
||||
constructor(params: Record<string, unknown>) {
|
||||
this.params = params;
|
||||
gatewayClientParams.push(params);
|
||||
@@ -140,58 +189,31 @@ vi.mock("../../../../src/gateway/operator-approvals-client.js", async () => {
|
||||
stop() {
|
||||
gatewayClientStops();
|
||||
}
|
||||
async request(...args: unknown[]) {
|
||||
return gatewayClientRequests(...args);
|
||||
async request() {
|
||||
return gatewayClientRequests();
|
||||
}
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
return {
|
||||
createOperatorApprovalsGatewayClient: async (params: {
|
||||
config?: {
|
||||
gateway?: {
|
||||
auth?: {
|
||||
token?: string;
|
||||
password?: string;
|
||||
};
|
||||
};
|
||||
};
|
||||
gatewayUrl?: string;
|
||||
clientDisplayName: string;
|
||||
onEvent?: unknown;
|
||||
onHelloOk?: () => void;
|
||||
onConnectError?: (err: Error) => void;
|
||||
onClose?: (code: number, reason: string) => void;
|
||||
}) => {
|
||||
mockCreateOperatorApprovalsGatewayClient(params);
|
||||
const envUrl = process.env.OPENCLAW_GATEWAY_URL?.trim();
|
||||
const gatewayUrl = params.gatewayUrl?.trim() || envUrl || "ws://127.0.0.1:18789";
|
||||
const urlOverrideSource = params.gatewayUrl?.trim() ? "cli" : envUrl ? "env" : undefined;
|
||||
const auth = await mockResolveGatewayConnectionAuth({
|
||||
config: params.config,
|
||||
env: process.env,
|
||||
...(urlOverrideSource
|
||||
? {
|
||||
urlOverride: gatewayUrl,
|
||||
urlOverrideSource,
|
||||
}
|
||||
: {}),
|
||||
});
|
||||
return new MockGatewayClient({
|
||||
url: gatewayUrl,
|
||||
token: auth?.token,
|
||||
password: auth?.password,
|
||||
clientName: "gateway-client",
|
||||
clientDisplayName: params.clientDisplayName,
|
||||
mode: "backend",
|
||||
scopes: ["operator.approvals"],
|
||||
onEvent: params.onEvent,
|
||||
onHelloOk: params.onHelloOk,
|
||||
onConnectError: params.onConnectError,
|
||||
onClose: params.onClose,
|
||||
});
|
||||
vi.mock("../../../../src/gateway/connection-auth.js", () => ({
|
||||
resolveGatewayConnectionAuth: (params: {
|
||||
config?: unknown;
|
||||
env: NodeJS.ProcessEnv;
|
||||
urlOverride?: string;
|
||||
urlOverrideSource?: "cli" | "env";
|
||||
}) => mockResolveGatewayConnectionAuth(params),
|
||||
}));
|
||||
|
||||
vi.mock("../client.js", () => ({
|
||||
createDiscordClient: () => ({
|
||||
rest: {
|
||||
post: mockRestPost,
|
||||
patch: mockRestPatch,
|
||||
delete: mockRestDelete,
|
||||
},
|
||||
};
|
||||
});
|
||||
request: (_fn: () => Promise<unknown>, _label: string) => _fn(),
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("openclaw/plugin-sdk/text-runtime", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/text-runtime")>();
|
||||
@@ -220,6 +242,66 @@ type ExecApprovalRequest = import("./exec-approvals.js").ExecApprovalRequest;
|
||||
type PluginApprovalRequest = import("./exec-approvals.js").PluginApprovalRequest;
|
||||
type ExecApprovalButtonContext = import("./exec-approvals.js").ExecApprovalButtonContext;
|
||||
|
||||
function createTestingDeps() {
|
||||
return {
|
||||
createGatewayClient: async (params: {
|
||||
config?: unknown;
|
||||
gatewayUrl?: string;
|
||||
clientDisplayName?: string;
|
||||
onEvent?: unknown;
|
||||
onHelloOk?: unknown;
|
||||
onConnectError?: unknown;
|
||||
onClose?: unknown;
|
||||
}) => {
|
||||
mockCreateOperatorApprovalsGatewayClient(params);
|
||||
const envUrl = process.env.OPENCLAW_GATEWAY_URL?.trim();
|
||||
const gatewayUrl = params.gatewayUrl?.trim() || envUrl || "ws://127.0.0.1:18789";
|
||||
const urlOverrideSource = params.gatewayUrl?.trim() ? "cli" : envUrl ? "env" : undefined;
|
||||
const auth = await mockResolveGatewayConnectionAuth({
|
||||
config: params.config,
|
||||
env: process.env,
|
||||
...(urlOverrideSource
|
||||
? {
|
||||
urlOverride: gatewayUrl,
|
||||
urlOverrideSource,
|
||||
}
|
||||
: {}),
|
||||
});
|
||||
const clientParams = {
|
||||
url: gatewayUrl,
|
||||
token: auth?.token,
|
||||
password: auth?.password,
|
||||
clientName: "gateway-client",
|
||||
clientDisplayName: params.clientDisplayName,
|
||||
mode: "backend",
|
||||
scopes: ["operator.approvals"],
|
||||
onEvent: params.onEvent,
|
||||
onHelloOk: params.onHelloOk,
|
||||
onConnectError: params.onConnectError,
|
||||
onClose: params.onClose,
|
||||
};
|
||||
gatewayClientParams.push(clientParams);
|
||||
mockGatewayClientCtor(clientParams);
|
||||
return {
|
||||
start: gatewayClientStarts,
|
||||
stop: gatewayClientStops,
|
||||
request: gatewayClientRequests,
|
||||
} as unknown as InstanceType<
|
||||
typeof import("../../../../src/gateway/client.js").GatewayClient
|
||||
>;
|
||||
},
|
||||
createDiscordClient: () => ({
|
||||
rest: {
|
||||
post: mockRestPost,
|
||||
patch: mockRestPatch,
|
||||
delete: mockRestDelete,
|
||||
},
|
||||
request: (_fn: () => Promise<unknown>, _label: string) => _fn(),
|
||||
token: "test-token",
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
||||
|
||||
function createHandler(config: DiscordExecApprovalConfig, accountId = "default") {
|
||||
@@ -228,6 +310,7 @@ function createHandler(config: DiscordExecApprovalConfig, accountId = "default")
|
||||
accountId,
|
||||
config,
|
||||
cfg: { session: { store: STORE_PATH } },
|
||||
__testing: createTestingDeps(),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -287,6 +370,30 @@ async function expectGatewayAuthStart(params: {
|
||||
expect(mockGatewayClientCtor).toHaveBeenCalledWith(expect.objectContaining(expectedClientParams));
|
||||
}
|
||||
|
||||
type ExecApprovalHandlerInternals = {
|
||||
pending: Map<
|
||||
string,
|
||||
{ discordMessageId: string; discordChannelId: string; timeoutId: NodeJS.Timeout }
|
||||
>;
|
||||
requestCache: Map<string, unknown>;
|
||||
handleApprovalRequested: (request: ExecApprovalRequest | PluginApprovalRequest) => Promise<void>;
|
||||
handleApprovalTimeout: (approvalId: string, source?: "channel" | "dm") => Promise<void>;
|
||||
};
|
||||
|
||||
function getHandlerInternals(
|
||||
handler: DiscordExecApprovalHandlerInstance,
|
||||
): ExecApprovalHandlerInternals {
|
||||
return handler as unknown as ExecApprovalHandlerInternals;
|
||||
}
|
||||
|
||||
function clearPendingTimeouts(handler: DiscordExecApprovalHandlerInstance) {
|
||||
const internals = getHandlerInternals(handler);
|
||||
for (const pending of internals.pending.values()) {
|
||||
clearTimeout(pending.timeoutId);
|
||||
}
|
||||
internals.pending.clear();
|
||||
}
|
||||
|
||||
function createRequest(
|
||||
overrides: Partial<ExecApprovalRequest["request"]> = {},
|
||||
): ExecApprovalRequest {
|
||||
@@ -311,10 +418,11 @@ function createPluginRequest(
|
||||
return {
|
||||
id: "plugin:test-id",
|
||||
request: {
|
||||
title: "Plugin approval required",
|
||||
description: "Allow plugin action",
|
||||
pluginId: "test-plugin",
|
||||
toolName: "test-tool",
|
||||
title: "Sensitive plugin action",
|
||||
description: "The plugin wants to run a sensitive tool action.",
|
||||
severity: "warning",
|
||||
toolName: "plugin.tool",
|
||||
pluginId: "plugin-test",
|
||||
agentId: "test-agent",
|
||||
sessionKey: "agent:test-agent:discord:channel:999888777",
|
||||
...overrides,
|
||||
@@ -324,6 +432,19 @@ function createPluginRequest(
|
||||
};
|
||||
}
|
||||
|
||||
function createMockButtonInteraction(userId: string) {
|
||||
const reply = vi.fn().mockResolvedValue(undefined);
|
||||
const acknowledge = vi.fn().mockResolvedValue(undefined);
|
||||
const followUp = vi.fn().mockResolvedValue(undefined);
|
||||
const interaction = {
|
||||
userId,
|
||||
reply,
|
||||
acknowledge,
|
||||
followUp,
|
||||
} as unknown as ButtonInteraction;
|
||||
return { interaction, reply, acknowledge, followUp };
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
mockRestPost.mockReset();
|
||||
mockRestPatch.mockReset();
|
||||
@@ -337,7 +458,6 @@ beforeEach(() => {
|
||||
});
|
||||
|
||||
beforeAll(async () => {
|
||||
vi.resetModules();
|
||||
({
|
||||
buildExecApprovalCustomId,
|
||||
extractDiscordChannelId,
|
||||
@@ -606,6 +726,104 @@ describe("DiscordExecApprovalHandler.shouldHandle", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("DiscordExecApprovalHandler plugin approvals", () => {
|
||||
beforeEach(() => {
|
||||
mockRestPost.mockClear().mockResolvedValue({ id: "mock-message", channel_id: "mock-channel" });
|
||||
mockRestPatch.mockClear().mockResolvedValue({});
|
||||
mockRestDelete.mockClear().mockResolvedValue({});
|
||||
});
|
||||
|
||||
it("delivers plugin approval requests with interactive approval buttons", async () => {
|
||||
const handler = createHandler({ enabled: true, approvers: ["123"] });
|
||||
const internals = getHandlerInternals(handler);
|
||||
mockSuccessfulDmDelivery({ throwOnUnexpectedRoute: true });
|
||||
|
||||
await internals.handleApprovalRequested(createPluginRequest());
|
||||
|
||||
const dmCall = mockRestPost.mock.calls.find(
|
||||
(call) => call[0] === Routes.channelMessages("dm-1"),
|
||||
) as [string, { body?: unknown }] | undefined;
|
||||
expect(dmCall).toBeDefined();
|
||||
expect(dmCall?.[1]?.body).toBeDefined();
|
||||
const bodyJson = JSON.stringify(dmCall?.[1]?.body ?? {});
|
||||
expect(bodyJson).toContain("Plugin Approval Required");
|
||||
expect(bodyJson).toContain("plugin:test-id");
|
||||
expect(bodyJson).toContain("execapproval:id=plugin%3Atest-id;action=allow-once");
|
||||
|
||||
clearPendingTimeouts(handler);
|
||||
});
|
||||
|
||||
it("handles plugin approvals end-to-end via gateway event, button resolve, and card update", async () => {
|
||||
const handler = createHandler({ enabled: true, approvers: ["123"] });
|
||||
mockSuccessfulDmDelivery({
|
||||
noteChannelId: "999888777",
|
||||
expectedNoteText: "I sent approval DMs to the approvers for this account",
|
||||
throwOnUnexpectedRoute: true,
|
||||
});
|
||||
|
||||
await handler.start();
|
||||
try {
|
||||
const onEvent = gatewayClientParams[0]?.onEvent as
|
||||
| ((evt: { event: string; payload: unknown }) => void)
|
||||
| undefined;
|
||||
expect(typeof onEvent).toBe("function");
|
||||
|
||||
const request = createPluginRequest();
|
||||
onEvent?.({
|
||||
event: "plugin.approval.requested",
|
||||
payload: request,
|
||||
});
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(mockRestPost).toHaveBeenCalledWith(
|
||||
Routes.channelMessages("dm-1"),
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
components: expect.any(Array),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
const button = new ExecApprovalButton({ handler });
|
||||
const { interaction, acknowledge } = createMockButtonInteraction("123");
|
||||
await button.run(interaction, { id: request.id, action: "allow-once" });
|
||||
|
||||
expect(acknowledge).toHaveBeenCalledTimes(1);
|
||||
expect(gatewayClientRequests).toHaveBeenCalledWith("plugin.approval.resolve", {
|
||||
id: request.id,
|
||||
decision: "allow-once",
|
||||
});
|
||||
|
||||
onEvent?.({
|
||||
event: "plugin.approval.resolved",
|
||||
payload: {
|
||||
id: request.id,
|
||||
decision: "allow-once",
|
||||
resolvedBy: "discord:123",
|
||||
ts: Date.now(),
|
||||
request: request.request,
|
||||
},
|
||||
});
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(mockRestPatch).toHaveBeenCalledWith(
|
||||
Routes.channelMessage("dm-1", "msg-1"),
|
||||
expect.objectContaining({ body: expect.any(Object) }),
|
||||
);
|
||||
});
|
||||
const patchCall = mockRestPatch.mock.calls.find(
|
||||
(call) => call[0] === Routes.channelMessage("dm-1", "msg-1"),
|
||||
) as [string, { body?: unknown }] | undefined;
|
||||
const patchBody = JSON.stringify(patchCall?.[1]?.body ?? {});
|
||||
expect(patchBody).toContain("Plugin Approval: Allowed (once)");
|
||||
} finally {
|
||||
clearPendingTimeouts(handler);
|
||||
await handler.stop();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ─── DiscordExecApprovalHandler.getApprovers ──────────────────────────────────
|
||||
|
||||
describe("DiscordExecApprovalHandler.getApprovers", () => {
|
||||
@@ -635,6 +853,40 @@ describe("DiscordExecApprovalHandler.getApprovers", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("DiscordExecApprovalHandler.resolveApproval", () => {
|
||||
it("routes non-prefixed approval IDs to exec.approval.resolve", async () => {
|
||||
const handler = createHandler({ enabled: true, approvers: ["123"] });
|
||||
await handler.start();
|
||||
|
||||
try {
|
||||
const ok = await handler.resolveApproval("exec-123", "allow-once");
|
||||
expect(ok).toBe(true);
|
||||
expect(gatewayClientRequests).toHaveBeenCalledWith("exec.approval.resolve", {
|
||||
id: "exec-123",
|
||||
decision: "allow-once",
|
||||
});
|
||||
} finally {
|
||||
await handler.stop();
|
||||
}
|
||||
});
|
||||
|
||||
it("routes plugin-prefixed approval IDs to plugin.approval.resolve", async () => {
|
||||
const handler = createHandler({ enabled: true, approvers: ["123"] });
|
||||
await handler.start();
|
||||
|
||||
try {
|
||||
const ok = await handler.resolveApproval("plugin:abc-123", "deny");
|
||||
expect(ok).toBe(true);
|
||||
expect(gatewayClientRequests).toHaveBeenCalledWith("plugin.approval.resolve", {
|
||||
id: "plugin:abc-123",
|
||||
decision: "deny",
|
||||
});
|
||||
} finally {
|
||||
await handler.stop();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ─── ExecApprovalButton authorization ─────────────────────────────────────────
|
||||
|
||||
describe("ExecApprovalButton", () => {
|
||||
@@ -672,7 +924,7 @@ describe("ExecApprovalButton", () => {
|
||||
await button.run(interaction, data);
|
||||
|
||||
expect(reply).toHaveBeenCalledWith({
|
||||
content: "⛔ You are not authorized to approve exec requests.",
|
||||
content: "⛔ You are not authorized to approve requests.",
|
||||
ephemeral: true,
|
||||
});
|
||||
expect(acknowledge).not.toHaveBeenCalled();
|
||||
@@ -849,6 +1101,7 @@ describe("DiscordExecApprovalHandler gateway auth", () => {
|
||||
auth: { mode: "token", token: "shared-gateway-token" },
|
||||
},
|
||||
},
|
||||
__testing: createTestingDeps(),
|
||||
});
|
||||
|
||||
await handler.start();
|
||||
@@ -875,6 +1128,7 @@ describe("DiscordExecApprovalHandler gateway auth", () => {
|
||||
auth: { mode: "token" },
|
||||
},
|
||||
},
|
||||
__testing: createTestingDeps(),
|
||||
});
|
||||
|
||||
try {
|
||||
@@ -899,6 +1153,40 @@ describe("DiscordExecApprovalHandler timeout cleanup", () => {
|
||||
mockRestPatch.mockClear().mockResolvedValue({});
|
||||
mockRestDelete.mockClear().mockResolvedValue({});
|
||||
});
|
||||
|
||||
it("cleans up request cache for the exact approval id", async () => {
|
||||
const handler = createHandler({ enabled: true, approvers: ["123"] });
|
||||
const internals = getHandlerInternals(handler);
|
||||
const requestA = { ...createRequest(), id: "abc" };
|
||||
const requestB = { ...createRequest(), id: "abc2" };
|
||||
|
||||
internals.requestCache.set("abc", { kind: "exec", request: requestA });
|
||||
internals.requestCache.set("abc2", { kind: "exec", request: requestB });
|
||||
|
||||
const timeoutIdA = setTimeout(() => {}, 0);
|
||||
const timeoutIdB = setTimeout(() => {}, 0);
|
||||
clearTimeout(timeoutIdA);
|
||||
clearTimeout(timeoutIdB);
|
||||
|
||||
internals.pending.set("abc:dm", {
|
||||
discordMessageId: "m1",
|
||||
discordChannelId: "c1",
|
||||
timeoutId: timeoutIdA,
|
||||
});
|
||||
internals.pending.set("abc2:dm", {
|
||||
discordMessageId: "m2",
|
||||
discordChannelId: "c2",
|
||||
timeoutId: timeoutIdB,
|
||||
});
|
||||
|
||||
await internals.handleApprovalTimeout("abc", "dm");
|
||||
|
||||
expect(internals.pending.has("abc:dm")).toBe(false);
|
||||
expect(internals.requestCache.has("abc")).toBe(false);
|
||||
expect(internals.requestCache.has("abc2")).toBe(true);
|
||||
|
||||
clearPendingTimeouts(handler);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Delivery routing ────────────────────────────────────────────────────────
|
||||
@@ -916,11 +1204,12 @@ describe("DiscordExecApprovalHandler delivery routing", () => {
|
||||
approvers: ["123"],
|
||||
target: "channel",
|
||||
});
|
||||
const internals = getHandlerInternals(handler);
|
||||
|
||||
mockSuccessfulDmDelivery();
|
||||
|
||||
const request = createRequest({ sessionKey: "agent:main:discord:dm:123" });
|
||||
await handler.handleApprovalRequested(request);
|
||||
await internals.handleApprovalRequested(request);
|
||||
|
||||
expect(mockRestPost).toHaveBeenCalledTimes(2);
|
||||
expect(mockRestPost).toHaveBeenCalledWith(Routes.userChannels(), {
|
||||
@@ -934,6 +1223,8 @@ describe("DiscordExecApprovalHandler delivery routing", () => {
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
clearPendingTimeouts(handler);
|
||||
});
|
||||
|
||||
it("posts an in-channel note when target is dm and the request came from a non-DM discord conversation", async () => {
|
||||
@@ -942,6 +1233,7 @@ describe("DiscordExecApprovalHandler delivery routing", () => {
|
||||
approvers: ["123"],
|
||||
target: "dm",
|
||||
});
|
||||
const internals = getHandlerInternals(handler);
|
||||
|
||||
mockSuccessfulDmDelivery({
|
||||
noteChannelId: "999888777",
|
||||
@@ -949,15 +1241,13 @@ describe("DiscordExecApprovalHandler delivery routing", () => {
|
||||
throwOnUnexpectedRoute: true,
|
||||
});
|
||||
|
||||
await handler.handleApprovalRequested(createRequest());
|
||||
await internals.handleApprovalRequested(createRequest());
|
||||
|
||||
expect(mockRestPost).toHaveBeenCalledWith(
|
||||
Routes.channelMessages("999888777"),
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
content: expect.stringContaining(
|
||||
"I sent approval DMs to the approvers for this account",
|
||||
),
|
||||
content: expect.stringContaining("I sent approval DMs to the approvers for this account"),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
@@ -967,6 +1257,8 @@ describe("DiscordExecApprovalHandler delivery routing", () => {
|
||||
body: expect.any(Object),
|
||||
}),
|
||||
);
|
||||
|
||||
clearPendingTimeouts(handler);
|
||||
});
|
||||
|
||||
it("does not post an in-channel note when the request already came from a discord DM", async () => {
|
||||
@@ -975,10 +1267,11 @@ describe("DiscordExecApprovalHandler delivery routing", () => {
|
||||
approvers: ["123"],
|
||||
target: "dm",
|
||||
});
|
||||
const internals = getHandlerInternals(handler);
|
||||
|
||||
mockSuccessfulDmDelivery({ throwOnUnexpectedRoute: true });
|
||||
|
||||
await handler.handleApprovalRequested(
|
||||
await internals.handleApprovalRequested(
|
||||
createRequest({ sessionKey: "agent:main:discord:dm:123" }),
|
||||
);
|
||||
|
||||
@@ -986,55 +1279,8 @@ describe("DiscordExecApprovalHandler delivery routing", () => {
|
||||
Routes.channelMessages("999888777"),
|
||||
expect.anything(),
|
||||
);
|
||||
});
|
||||
|
||||
it("delivers plugin approvals through the shared runtime flow", async () => {
|
||||
const handler = createHandler({
|
||||
enabled: true,
|
||||
approvers: ["123"],
|
||||
target: "dm",
|
||||
});
|
||||
|
||||
mockSuccessfulDmDelivery({ throwOnUnexpectedRoute: true });
|
||||
|
||||
await handler.handleApprovalRequested(createPluginRequest());
|
||||
|
||||
expect(mockRestPost).toHaveBeenCalledWith(
|
||||
Routes.channelMessages("dm-1"),
|
||||
expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
components: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
components: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
content: expect.stringContaining("Plugin Approval Required"),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
content: expect.stringContaining("Plugin approval required"),
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DiscordExecApprovalHandler resolve routing", () => {
|
||||
it("routes plugin approval ids through plugin.approval.resolve", async () => {
|
||||
const handler = createHandler({
|
||||
enabled: true,
|
||||
approvers: ["123"],
|
||||
});
|
||||
|
||||
await handler.start();
|
||||
await expect(handler.resolveApproval("plugin:test-id", "allow-once")).resolves.toBe(true);
|
||||
|
||||
expect(gatewayClientRequests).toHaveBeenCalledWith("plugin.approval.resolve", {
|
||||
id: "plugin:test-id",
|
||||
decision: "allow-once",
|
||||
});
|
||||
clearPendingTimeouts(handler);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1050,6 +1296,7 @@ describe("DiscordExecApprovalHandler gateway auth resolution", () => {
|
||||
gatewayUrl: "wss://override.example/ws",
|
||||
config: { enabled: true, approvers: ["123"] },
|
||||
cfg: { session: { store: STORE_PATH } },
|
||||
__testing: createTestingDeps(),
|
||||
});
|
||||
|
||||
await expectGatewayAuthStart({
|
||||
@@ -1072,6 +1319,7 @@ describe("DiscordExecApprovalHandler gateway auth resolution", () => {
|
||||
accountId: "default",
|
||||
config: { enabled: true, approvers: ["123"] },
|
||||
cfg: { session: { store: STORE_PATH } },
|
||||
__testing: createTestingDeps(),
|
||||
});
|
||||
|
||||
await expectGatewayAuthStart({
|
||||
|
||||
@@ -10,25 +10,20 @@ import {
|
||||
type TopLevelComponents,
|
||||
} from "@buape/carbon";
|
||||
import { ButtonStyle, Routes } from "discord-api-types/v10";
|
||||
import {
|
||||
getExecApprovalApproverDmNoticeText,
|
||||
resolveExecApprovalCommandDisplay,
|
||||
type ExecApprovalDecision,
|
||||
type ExecApprovalRequest,
|
||||
type ExecApprovalResolved,
|
||||
type PluginApprovalRequest,
|
||||
type PluginApprovalResolved,
|
||||
} from "openclaw/plugin-sdk/approval-runtime";
|
||||
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import { loadSessionStore, resolveStorePath } from "openclaw/plugin-sdk/config-runtime";
|
||||
import type { DiscordExecApprovalConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import {
|
||||
createExecApprovalChannelRuntime,
|
||||
type ExecApprovalChannelRuntime,
|
||||
resolveChannelNativeApprovalDeliveryPlan,
|
||||
} from "openclaw/plugin-sdk/infra-runtime";
|
||||
import { buildExecApprovalActionDescriptors } from "openclaw/plugin-sdk/infra-runtime";
|
||||
import { resolveExecApprovalCommandDisplay } from "openclaw/plugin-sdk/infra-runtime";
|
||||
import { getExecApprovalApproverDmNoticeText } from "openclaw/plugin-sdk/infra-runtime";
|
||||
import type {
|
||||
ExecApprovalActionDescriptor,
|
||||
ExecApprovalDecision,
|
||||
ExecApprovalRequest,
|
||||
ExecApprovalResolved,
|
||||
PluginApprovalRequest,
|
||||
PluginApprovalResolved,
|
||||
} from "openclaw/plugin-sdk/infra-runtime";
|
||||
import type { EventFrame } from "openclaw/plugin-sdk/gateway-runtime";
|
||||
import * as gatewayRuntime from "openclaw/plugin-sdk/gateway-runtime";
|
||||
import {
|
||||
normalizeAccountId,
|
||||
normalizeMessageChannel,
|
||||
@@ -37,12 +32,10 @@ import {
|
||||
import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
|
||||
import { compileSafeRegex, testRegexWithBoundedInput } from "openclaw/plugin-sdk/security-runtime";
|
||||
import { logDebug, logError } from "openclaw/plugin-sdk/text-runtime";
|
||||
import { createDiscordNativeApprovalAdapter } from "../approval-native.js";
|
||||
import { createDiscordClient, stripUndefinedFields } from "../send.shared.js";
|
||||
import * as sendShared from "../send.shared.js";
|
||||
import { DiscordUiContainer } from "../ui.js";
|
||||
|
||||
const EXEC_APPROVAL_KEY = "execapproval";
|
||||
export { extractDiscordChannelId } from "../approval-native.js";
|
||||
export type {
|
||||
ExecApprovalRequest,
|
||||
ExecApprovalResolved,
|
||||
@@ -50,9 +43,15 @@ export type {
|
||||
PluginApprovalResolved,
|
||||
};
|
||||
|
||||
type ApprovalRequest = ExecApprovalRequest | PluginApprovalRequest;
|
||||
type ApprovalResolved = ExecApprovalResolved | PluginApprovalResolved;
|
||||
type ApprovalKind = "exec" | "plugin";
|
||||
/** Extract Discord channel ID from a session key like "agent:main:discord:channel:123456789" */
|
||||
export function extractDiscordChannelId(sessionKey?: string | null): string | null {
|
||||
if (!sessionKey) {
|
||||
return null;
|
||||
}
|
||||
// Session key format: agent:<id>:discord:channel:<channelId> or agent:<id>:discord:group:<channelId>
|
||||
const match = sessionKey.match(/discord:(?:channel|group):(\d+)/);
|
||||
return match ? match[1] : null;
|
||||
}
|
||||
|
||||
function buildDiscordApprovalDmRedirectNotice(): { content: string } {
|
||||
return {
|
||||
@@ -63,16 +62,14 @@ function buildDiscordApprovalDmRedirectNotice(): { content: string } {
|
||||
type PendingApproval = {
|
||||
discordMessageId: string;
|
||||
discordChannelId: string;
|
||||
timeoutId?: NodeJS.Timeout;
|
||||
timeoutId: NodeJS.Timeout;
|
||||
};
|
||||
|
||||
function resolveApprovalKindFromId(approvalId: string): ApprovalKind {
|
||||
return approvalId.startsWith("plugin:") ? "plugin" : "exec";
|
||||
}
|
||||
type ApprovalKind = "exec" | "plugin";
|
||||
|
||||
function isPluginApprovalRequest(request: ApprovalRequest): request is PluginApprovalRequest {
|
||||
return resolveApprovalKindFromId(request.id) === "plugin";
|
||||
}
|
||||
type CachedApprovalRequest =
|
||||
| { kind: "exec"; request: ExecApprovalRequest }
|
||||
| { kind: "plugin"; request: PluginApprovalRequest };
|
||||
|
||||
function encodeCustomIdValue(value: string): string {
|
||||
return encodeURIComponent(value);
|
||||
@@ -118,6 +115,16 @@ export function parseExecApprovalData(
|
||||
};
|
||||
}
|
||||
|
||||
function resolveApprovalKindFromId(approvalId: string): ApprovalKind {
|
||||
return approvalId.startsWith("plugin:") ? "plugin" : "exec";
|
||||
}
|
||||
|
||||
function isPluginApprovalRequest(
|
||||
request: ExecApprovalRequest | PluginApprovalRequest,
|
||||
): request is PluginApprovalRequest {
|
||||
return resolveApprovalKindFromId(request.id) === "plugin";
|
||||
}
|
||||
|
||||
type ExecApprovalContainerParams = {
|
||||
cfg: OpenClawConfig;
|
||||
accountId: string;
|
||||
@@ -170,36 +177,49 @@ class ExecApprovalActionButton extends Button {
|
||||
label: string;
|
||||
style: ButtonStyle;
|
||||
|
||||
constructor(params: { approvalId: string; descriptor: ExecApprovalActionDescriptor }) {
|
||||
constructor(params: {
|
||||
approvalId: string;
|
||||
action: ExecApprovalDecision;
|
||||
label: string;
|
||||
style: ButtonStyle;
|
||||
}) {
|
||||
super();
|
||||
this.customId = buildExecApprovalCustomId(params.approvalId, params.descriptor.decision);
|
||||
this.label = params.descriptor.label;
|
||||
this.style =
|
||||
params.descriptor.style === "success"
|
||||
? ButtonStyle.Success
|
||||
: params.descriptor.style === "primary"
|
||||
? ButtonStyle.Primary
|
||||
: params.descriptor.style === "danger"
|
||||
? ButtonStyle.Danger
|
||||
: ButtonStyle.Secondary;
|
||||
this.customId = buildExecApprovalCustomId(params.approvalId, params.action);
|
||||
this.label = params.label;
|
||||
this.style = params.style;
|
||||
}
|
||||
}
|
||||
|
||||
class ExecApprovalActionRow extends Row<Button> {
|
||||
constructor(approvalId: string) {
|
||||
super([
|
||||
...buildExecApprovalActionDescriptors({ approvalCommandId: approvalId }).map(
|
||||
(descriptor) => new ExecApprovalActionButton({ approvalId, descriptor }),
|
||||
),
|
||||
new ExecApprovalActionButton({
|
||||
approvalId,
|
||||
action: "allow-once",
|
||||
label: "Allow once",
|
||||
style: ButtonStyle.Success,
|
||||
}),
|
||||
new ExecApprovalActionButton({
|
||||
approvalId,
|
||||
action: "allow-always",
|
||||
label: "Always allow",
|
||||
style: ButtonStyle.Primary,
|
||||
}),
|
||||
new ExecApprovalActionButton({
|
||||
approvalId,
|
||||
action: "deny",
|
||||
label: "Deny",
|
||||
style: ButtonStyle.Danger,
|
||||
}),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
function resolveExecApprovalAccountId(params: {
|
||||
function resolveAccountIdFromSessionKey(params: {
|
||||
cfg: OpenClawConfig;
|
||||
request: ExecApprovalRequest;
|
||||
sessionKey?: string | null;
|
||||
}): string | null {
|
||||
const sessionKey = params.request.request.sessionKey?.trim();
|
||||
const sessionKey = params.sessionKey?.trim();
|
||||
if (!sessionKey) {
|
||||
return null;
|
||||
}
|
||||
@@ -219,21 +239,23 @@ function resolveExecApprovalAccountId(params: {
|
||||
}
|
||||
}
|
||||
|
||||
function resolveExecApprovalAccountId(params: {
|
||||
cfg: OpenClawConfig;
|
||||
request: ExecApprovalRequest;
|
||||
}): string | null {
|
||||
return resolveAccountIdFromSessionKey({
|
||||
cfg: params.cfg,
|
||||
sessionKey: params.request.request.sessionKey,
|
||||
});
|
||||
}
|
||||
|
||||
function resolvePluginApprovalAccountId(params: {
|
||||
cfg: OpenClawConfig;
|
||||
request: PluginApprovalRequest;
|
||||
}): string | null {
|
||||
const fromSession = resolveExecApprovalAccountId({
|
||||
const fromSession = resolveAccountIdFromSessionKey({
|
||||
cfg: params.cfg,
|
||||
request: {
|
||||
id: params.request.id,
|
||||
request: {
|
||||
command: params.request.request.title,
|
||||
sessionKey: params.request.request.sessionKey ?? undefined,
|
||||
},
|
||||
createdAtMs: params.request.createdAtMs,
|
||||
expiresAtMs: params.request.expiresAtMs,
|
||||
},
|
||||
sessionKey: params.request.request.sessionKey,
|
||||
});
|
||||
if (fromSession) {
|
||||
return fromSession;
|
||||
@@ -243,18 +265,22 @@ function resolvePluginApprovalAccountId(params: {
|
||||
|
||||
function resolveApprovalAccountId(params: {
|
||||
cfg: OpenClawConfig;
|
||||
request: ApprovalRequest;
|
||||
request: ExecApprovalRequest | PluginApprovalRequest;
|
||||
}): string | null {
|
||||
return isPluginApprovalRequest(params.request)
|
||||
? resolvePluginApprovalAccountId({ cfg: params.cfg, request: params.request })
|
||||
: resolveExecApprovalAccountId({ cfg: params.cfg, request: params.request });
|
||||
}
|
||||
|
||||
function resolveApprovalAgentId(request: ApprovalRequest): string | null {
|
||||
function resolveApprovalAgentId(
|
||||
request: ExecApprovalRequest | PluginApprovalRequest,
|
||||
): string | null {
|
||||
return request.request.agentId?.trim() || null;
|
||||
}
|
||||
|
||||
function resolveApprovalSessionKey(request: ApprovalRequest): string | null {
|
||||
function resolveApprovalSessionKey(
|
||||
request: ExecApprovalRequest | PluginApprovalRequest,
|
||||
): string | null {
|
||||
return request.request.sessionKey?.trim() || null;
|
||||
}
|
||||
|
||||
@@ -500,38 +526,31 @@ export type DiscordExecApprovalHandlerOpts = {
|
||||
cfg: OpenClawConfig;
|
||||
runtime?: RuntimeEnv;
|
||||
onResolve?: (id: string, decision: ExecApprovalDecision) => Promise<void>;
|
||||
__testing?: {
|
||||
createGatewayClient?: typeof gatewayRuntime.createOperatorApprovalsGatewayClient;
|
||||
createDiscordClient?: (...args: Parameters<typeof sendShared.createDiscordClient>) => {
|
||||
rest: {
|
||||
post: (...args: unknown[]) => Promise<unknown>;
|
||||
patch: (...args: unknown[]) => Promise<unknown>;
|
||||
delete: (...args: unknown[]) => Promise<unknown>;
|
||||
};
|
||||
request: (fn: () => Promise<unknown>, label: string) => Promise<unknown>;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
export class DiscordExecApprovalHandler {
|
||||
private readonly runtime: ExecApprovalChannelRuntime<ApprovalRequest, ApprovalResolved>;
|
||||
private gatewayClient: gatewayRuntime.GatewayClient | null = null;
|
||||
private pending = new Map<string, PendingApproval>();
|
||||
private requestCache = new Map<string, CachedApprovalRequest>();
|
||||
private opts: DiscordExecApprovalHandlerOpts;
|
||||
private started = false;
|
||||
|
||||
constructor(opts: DiscordExecApprovalHandlerOpts) {
|
||||
this.opts = opts;
|
||||
this.runtime = createExecApprovalChannelRuntime<
|
||||
PendingApproval,
|
||||
ApprovalRequest,
|
||||
ApprovalResolved
|
||||
>({
|
||||
label: "discord/exec-approvals",
|
||||
clientDisplayName: "Discord Exec Approvals",
|
||||
cfg: this.opts.cfg,
|
||||
gatewayUrl: this.opts.gatewayUrl,
|
||||
eventKinds: ["exec", "plugin"],
|
||||
isConfigured: () =>
|
||||
Boolean(this.opts.config.enabled && (this.opts.config.approvers?.length ?? 0) > 0),
|
||||
shouldHandle: (request) => this.shouldHandle(request),
|
||||
deliverRequested: async (request) => await this.deliverRequested(request),
|
||||
finalizeResolved: async ({ request, resolved, entries }) => {
|
||||
await this.finalizeResolved(request, resolved, entries);
|
||||
},
|
||||
finalizeExpired: async ({ request, entries }) => {
|
||||
await this.finalizeExpired(request, entries);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
shouldHandle(request: ApprovalRequest): boolean {
|
||||
shouldHandle(request: ExecApprovalRequest | PluginApprovalRequest): boolean {
|
||||
const config = this.opts.config;
|
||||
if (!config.enabled) {
|
||||
return false;
|
||||
@@ -584,51 +603,133 @@ export class DiscordExecApprovalHandler {
|
||||
}
|
||||
|
||||
async start(): Promise<void> {
|
||||
await this.runtime.start();
|
||||
if (this.started) {
|
||||
return;
|
||||
}
|
||||
this.started = true;
|
||||
|
||||
const config = this.opts.config;
|
||||
if (!config.enabled) {
|
||||
logDebug("discord exec approvals: disabled");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!config.approvers || config.approvers.length === 0) {
|
||||
logDebug("discord exec approvals: no approvers configured");
|
||||
return;
|
||||
}
|
||||
|
||||
logDebug("discord exec approvals: starting handler");
|
||||
|
||||
this.gatewayClient = await (
|
||||
this.opts.__testing?.createGatewayClient ??
|
||||
gatewayRuntime.createOperatorApprovalsGatewayClient
|
||||
)({
|
||||
config: this.opts.cfg,
|
||||
gatewayUrl: this.opts.gatewayUrl,
|
||||
clientDisplayName: "Discord Exec Approvals",
|
||||
onEvent: (evt) => this.handleGatewayEvent(evt),
|
||||
onHelloOk: () => {
|
||||
logDebug("discord exec approvals: connected to gateway");
|
||||
},
|
||||
onConnectError: (err) => {
|
||||
logError(`discord exec approvals: connect error: ${err.message}`);
|
||||
},
|
||||
onClose: (code, reason) => {
|
||||
logDebug(`discord exec approvals: gateway closed: ${code} ${reason}`);
|
||||
},
|
||||
});
|
||||
|
||||
this.gatewayClient.start();
|
||||
}
|
||||
|
||||
async stop(): Promise<void> {
|
||||
await this.runtime.stop();
|
||||
if (!this.started) {
|
||||
return;
|
||||
}
|
||||
this.started = false;
|
||||
|
||||
// Clear all pending timeouts
|
||||
for (const pending of this.pending.values()) {
|
||||
clearTimeout(pending.timeoutId);
|
||||
}
|
||||
this.pending.clear();
|
||||
this.requestCache.clear();
|
||||
|
||||
this.gatewayClient?.stop();
|
||||
this.gatewayClient = null;
|
||||
|
||||
logDebug("discord exec approvals: stopped");
|
||||
}
|
||||
|
||||
private async deliverRequested(request: ApprovalRequest): Promise<PendingApproval[]> {
|
||||
const { rest, request: discordRequest } = createDiscordClient(
|
||||
{ token: this.opts.token, accountId: this.opts.accountId },
|
||||
this.opts.cfg,
|
||||
);
|
||||
private handleGatewayEvent(evt: EventFrame): void {
|
||||
if (evt.event === "exec.approval.requested") {
|
||||
const request = evt.payload as ExecApprovalRequest;
|
||||
void this.handleApprovalRequested(request);
|
||||
} else if (evt.event === "plugin.approval.requested") {
|
||||
const request = evt.payload as PluginApprovalRequest;
|
||||
void this.handleApprovalRequested(request);
|
||||
} else if (evt.event === "exec.approval.resolved") {
|
||||
const resolved = evt.payload as ExecApprovalResolved;
|
||||
void this.handleApprovalResolved(resolved);
|
||||
} else if (evt.event === "plugin.approval.resolved") {
|
||||
const resolved = evt.payload as PluginApprovalResolved;
|
||||
void this.handleApprovalResolved(resolved);
|
||||
}
|
||||
}
|
||||
|
||||
const actionRow = new ExecApprovalActionRow(request.id);
|
||||
const container = isPluginApprovalRequest(request)
|
||||
? createPluginApprovalRequestContainer({
|
||||
request,
|
||||
cfg: this.opts.cfg,
|
||||
accountId: this.opts.accountId,
|
||||
actionRow,
|
||||
})
|
||||
: createExecApprovalRequestContainer({
|
||||
request,
|
||||
cfg: this.opts.cfg,
|
||||
accountId: this.opts.accountId,
|
||||
actionRow,
|
||||
});
|
||||
private async handleApprovalRequested(
|
||||
request: ExecApprovalRequest | PluginApprovalRequest,
|
||||
): Promise<void> {
|
||||
if (!this.shouldHandle(request)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const pluginRequest: PluginApprovalRequest | null = isPluginApprovalRequest(request)
|
||||
? request
|
||||
: null;
|
||||
logDebug(
|
||||
`discord exec approvals: received ${pluginRequest ? "plugin" : "exec"} request ${request.id}`,
|
||||
);
|
||||
let container: ExecApprovalContainer;
|
||||
if (pluginRequest) {
|
||||
this.requestCache.set(request.id, { kind: "plugin", request: pluginRequest });
|
||||
container = createPluginApprovalRequestContainer({
|
||||
request: pluginRequest,
|
||||
cfg: this.opts.cfg,
|
||||
accountId: this.opts.accountId,
|
||||
actionRow: new ExecApprovalActionRow(request.id),
|
||||
});
|
||||
} else {
|
||||
const execRequest = request as ExecApprovalRequest;
|
||||
this.requestCache.set(request.id, { kind: "exec", request: execRequest });
|
||||
container = createExecApprovalRequestContainer({
|
||||
request: execRequest,
|
||||
cfg: this.opts.cfg,
|
||||
accountId: this.opts.accountId,
|
||||
actionRow: new ExecApprovalActionRow(request.id),
|
||||
});
|
||||
}
|
||||
|
||||
const { rest, request: discordRequest } = (
|
||||
this.opts.__testing?.createDiscordClient ?? sendShared.createDiscordClient
|
||||
)({ token: this.opts.token, accountId: this.opts.accountId }, this.opts.cfg);
|
||||
const payload = buildExecApprovalPayload(container);
|
||||
const body = stripUndefinedFields(serializePayload(payload));
|
||||
const approvalKind: ApprovalKind = isPluginApprovalRequest(request) ? "plugin" : "exec";
|
||||
const nativeApprovalAdapter = createDiscordNativeApprovalAdapter(this.opts.config);
|
||||
const deliveryPlan = await resolveChannelNativeApprovalDeliveryPlan({
|
||||
cfg: this.opts.cfg,
|
||||
accountId: this.opts.accountId,
|
||||
approvalKind,
|
||||
request,
|
||||
adapter: nativeApprovalAdapter.native,
|
||||
});
|
||||
const pendingEntries: PendingApproval[] = [];
|
||||
const originTarget = deliveryPlan.originTarget;
|
||||
if (deliveryPlan.notifyOriginWhenDmOnly && originTarget) {
|
||||
const body = sendShared.stripUndefinedFields(serializePayload(payload));
|
||||
|
||||
const target = this.opts.config.target ?? "dm";
|
||||
const sendToDm = target === "dm" || target === "both";
|
||||
const sendToChannel = target === "channel" || target === "both";
|
||||
let fallbackToDm = false;
|
||||
const sessionKey = resolveApprovalSessionKey(request);
|
||||
const originatingChannelId =
|
||||
sessionKey && target === "dm" ? extractDiscordChannelId(sessionKey) : null;
|
||||
|
||||
if (target === "dm" && originatingChannelId) {
|
||||
try {
|
||||
await discordRequest(
|
||||
() =>
|
||||
rest.post(Routes.channelMessages(originTarget.to), {
|
||||
rest.post(Routes.channelMessages(originatingChannelId), {
|
||||
body: buildDiscordApprovalDmRedirectNotice(),
|
||||
}) as Promise<{ id: string; channel_id: string }>,
|
||||
"send-approval-dm-redirect-notice",
|
||||
@@ -638,130 +739,199 @@ export class DiscordExecApprovalHandler {
|
||||
}
|
||||
}
|
||||
|
||||
for (const deliveryTarget of deliveryPlan.targets) {
|
||||
if (deliveryTarget.surface === "origin") {
|
||||
// Send to originating channel if configured
|
||||
if (sendToChannel) {
|
||||
const channelId = extractDiscordChannelId(sessionKey);
|
||||
if (channelId) {
|
||||
try {
|
||||
const message = (await discordRequest(
|
||||
() =>
|
||||
rest.post(Routes.channelMessages(deliveryTarget.target.to), {
|
||||
rest.post(Routes.channelMessages(channelId), {
|
||||
body,
|
||||
}) as Promise<{ id: string; channel_id: string }>,
|
||||
"send-approval-channel",
|
||||
)) as { id: string; channel_id: string };
|
||||
|
||||
if (message?.id) {
|
||||
pendingEntries.push({
|
||||
const timeoutMs = Math.max(0, request.expiresAtMs - Date.now());
|
||||
const timeoutId = setTimeout(() => {
|
||||
void this.handleApprovalTimeout(request.id, "channel");
|
||||
}, timeoutMs);
|
||||
|
||||
this.pending.set(`${request.id}:channel`, {
|
||||
discordMessageId: message.id,
|
||||
discordChannelId: deliveryTarget.target.to,
|
||||
discordChannelId: channelId,
|
||||
timeoutId,
|
||||
});
|
||||
|
||||
logDebug(
|
||||
`discord exec approvals: sent approval ${request.id} to channel ${deliveryTarget.target.to}`,
|
||||
);
|
||||
logDebug(`discord exec approvals: sent approval ${request.id} to channel ${channelId}`);
|
||||
}
|
||||
} catch (err) {
|
||||
logError(`discord exec approvals: failed to send to channel: ${String(err)}`);
|
||||
}
|
||||
} else {
|
||||
if (!sendToDm) {
|
||||
logError(
|
||||
`discord exec approvals: target is "channel" but could not extract channel id from session key "${sessionKey ?? "(none)"}" — falling back to DM delivery for approval ${request.id}`,
|
||||
);
|
||||
fallbackToDm = true;
|
||||
} else {
|
||||
logDebug("discord exec approvals: could not extract channel id from session key");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Send to approver DMs if configured (or as fallback when channel extraction fails)
|
||||
if (sendToDm || fallbackToDm) {
|
||||
const approvers = this.opts.config.approvers ?? [];
|
||||
|
||||
for (const approver of approvers) {
|
||||
const userId = String(approver);
|
||||
try {
|
||||
// Create DM channel
|
||||
const dmChannel = (await discordRequest(
|
||||
() =>
|
||||
rest.post(Routes.userChannels(), {
|
||||
body: { recipient_id: userId },
|
||||
}) as Promise<{ id: string }>,
|
||||
"dm-channel",
|
||||
)) as { id: string };
|
||||
|
||||
if (!dmChannel?.id) {
|
||||
logError(`discord exec approvals: failed to create DM for user ${userId}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Send message with components v2 + buttons
|
||||
const message = (await discordRequest(
|
||||
() =>
|
||||
rest.post(Routes.channelMessages(dmChannel.id), {
|
||||
body,
|
||||
}) as Promise<{ id: string; channel_id: string }>,
|
||||
"send-approval",
|
||||
)) as { id: string; channel_id: string };
|
||||
|
||||
if (!message?.id) {
|
||||
logError(`discord exec approvals: failed to send message to user ${userId}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Clear any existing pending DM entry to avoid timeout leaks
|
||||
const existingDm = this.pending.get(`${request.id}:dm`);
|
||||
if (existingDm) {
|
||||
clearTimeout(existingDm.timeoutId);
|
||||
}
|
||||
|
||||
// Set up timeout
|
||||
const timeoutMs = Math.max(0, request.expiresAtMs - Date.now());
|
||||
const timeoutId = setTimeout(() => {
|
||||
void this.handleApprovalTimeout(request.id, "dm");
|
||||
}, timeoutMs);
|
||||
|
||||
this.pending.set(`${request.id}:dm`, {
|
||||
discordMessageId: message.id,
|
||||
discordChannelId: dmChannel.id,
|
||||
timeoutId,
|
||||
});
|
||||
|
||||
logDebug(`discord exec approvals: sent approval ${request.id} to user ${userId}`);
|
||||
} catch (err) {
|
||||
logError(`discord exec approvals: failed to notify user ${userId}: ${String(err)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async handleApprovalResolved(
|
||||
resolved: ExecApprovalResolved | PluginApprovalResolved,
|
||||
): Promise<void> {
|
||||
// Clean up all pending entries for this approval (channel + dm)
|
||||
const cached = this.requestCache.get(resolved.id);
|
||||
this.requestCache.delete(resolved.id);
|
||||
|
||||
if (!cached) {
|
||||
return;
|
||||
}
|
||||
|
||||
logDebug(
|
||||
`discord exec approvals: resolved ${cached.kind} ${resolved.id} with ${resolved.decision}`,
|
||||
);
|
||||
|
||||
const container =
|
||||
cached.kind === "plugin"
|
||||
? createPluginResolvedContainer({
|
||||
request: cached.request,
|
||||
decision: resolved.decision,
|
||||
resolvedBy: resolved.resolvedBy,
|
||||
cfg: this.opts.cfg,
|
||||
accountId: this.opts.accountId,
|
||||
})
|
||||
: createExecResolvedContainer({
|
||||
request: cached.request,
|
||||
decision: resolved.decision,
|
||||
resolvedBy: resolved.resolvedBy,
|
||||
cfg: this.opts.cfg,
|
||||
accountId: this.opts.accountId,
|
||||
});
|
||||
|
||||
for (const suffix of [":channel", ":dm", ""]) {
|
||||
const key = `${resolved.id}${suffix}`;
|
||||
const pending = this.pending.get(key);
|
||||
if (!pending) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const userId = deliveryTarget.target.to;
|
||||
try {
|
||||
const dmChannel = (await discordRequest(
|
||||
() =>
|
||||
rest.post(Routes.userChannels(), {
|
||||
body: { recipient_id: userId },
|
||||
}) as Promise<{ id: string }>,
|
||||
"dm-channel",
|
||||
)) as { id: string };
|
||||
clearTimeout(pending.timeoutId);
|
||||
this.pending.delete(key);
|
||||
|
||||
if (!dmChannel?.id) {
|
||||
logError(`discord exec approvals: failed to create DM for user ${userId}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const message = (await discordRequest(
|
||||
() =>
|
||||
rest.post(Routes.channelMessages(dmChannel.id), {
|
||||
body,
|
||||
}) as Promise<{ id: string; channel_id: string }>,
|
||||
"send-approval",
|
||||
)) as { id: string; channel_id: string };
|
||||
|
||||
if (!message?.id) {
|
||||
logError(`discord exec approvals: failed to send message to user ${userId}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
pendingEntries.push({
|
||||
discordMessageId: message.id,
|
||||
discordChannelId: dmChannel.id,
|
||||
});
|
||||
|
||||
logDebug(`discord exec approvals: sent approval ${request.id} to user ${userId}`);
|
||||
} catch (err) {
|
||||
logError(`discord exec approvals: failed to notify user ${userId}: ${String(err)}`);
|
||||
}
|
||||
}
|
||||
return pendingEntries;
|
||||
}
|
||||
|
||||
async handleApprovalRequested(request: ApprovalRequest): Promise<void> {
|
||||
await this.runtime.handleRequested(request);
|
||||
}
|
||||
|
||||
async handleApprovalResolved(resolved: ApprovalResolved): Promise<void> {
|
||||
await this.runtime.handleResolved(resolved);
|
||||
}
|
||||
|
||||
async handleApprovalTimeout(approvalId: string, _source?: "channel" | "dm"): Promise<void> {
|
||||
await this.runtime.handleExpired(approvalId);
|
||||
}
|
||||
|
||||
private async finalizeResolved(
|
||||
request: ApprovalRequest,
|
||||
resolved: ApprovalResolved,
|
||||
entries: PendingApproval[],
|
||||
): Promise<void> {
|
||||
const container = isPluginApprovalRequest(request)
|
||||
? createPluginResolvedContainer({
|
||||
request,
|
||||
decision: resolved.decision,
|
||||
resolvedBy: resolved.resolvedBy,
|
||||
cfg: this.opts.cfg,
|
||||
accountId: this.opts.accountId,
|
||||
})
|
||||
: createExecResolvedContainer({
|
||||
request,
|
||||
decision: resolved.decision,
|
||||
resolvedBy: resolved.resolvedBy,
|
||||
cfg: this.opts.cfg,
|
||||
accountId: this.opts.accountId,
|
||||
});
|
||||
|
||||
for (const pending of entries) {
|
||||
await this.finalizeMessage(pending.discordChannelId, pending.discordMessageId, container);
|
||||
}
|
||||
}
|
||||
|
||||
private async finalizeExpired(
|
||||
request: ApprovalRequest,
|
||||
entries: PendingApproval[],
|
||||
private async handleApprovalTimeout(
|
||||
approvalId: string,
|
||||
source?: "channel" | "dm",
|
||||
): Promise<void> {
|
||||
const container = isPluginApprovalRequest(request)
|
||||
? createPluginExpiredContainer({
|
||||
request,
|
||||
cfg: this.opts.cfg,
|
||||
accountId: this.opts.accountId,
|
||||
})
|
||||
: createExecExpiredContainer({
|
||||
request,
|
||||
cfg: this.opts.cfg,
|
||||
accountId: this.opts.accountId,
|
||||
});
|
||||
for (const pending of entries) {
|
||||
await this.finalizeMessage(pending.discordChannelId, pending.discordMessageId, container);
|
||||
const key = source ? `${approvalId}:${source}` : approvalId;
|
||||
const pending = this.pending.get(key);
|
||||
if (!pending) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.pending.delete(key);
|
||||
|
||||
const cached = this.requestCache.get(approvalId);
|
||||
|
||||
// Only clean up requestCache if no other pending entries exist for this approval
|
||||
const hasOtherPending =
|
||||
this.pending.has(`${approvalId}:channel`) ||
|
||||
this.pending.has(`${approvalId}:dm`) ||
|
||||
this.pending.has(approvalId);
|
||||
if (!hasOtherPending) {
|
||||
this.requestCache.delete(approvalId);
|
||||
}
|
||||
|
||||
if (!cached) {
|
||||
return;
|
||||
}
|
||||
|
||||
logDebug(
|
||||
`discord exec approvals: timeout for ${cached.kind} ${approvalId} (${source ?? "default"})`,
|
||||
);
|
||||
|
||||
const container =
|
||||
cached.kind === "plugin"
|
||||
? createPluginExpiredContainer({
|
||||
request: cached.request,
|
||||
cfg: this.opts.cfg,
|
||||
accountId: this.opts.accountId,
|
||||
})
|
||||
: createExecExpiredContainer({
|
||||
request: cached.request,
|
||||
cfg: this.opts.cfg,
|
||||
accountId: this.opts.accountId,
|
||||
});
|
||||
await this.finalizeMessage(pending.discordChannelId, pending.discordMessageId, container);
|
||||
}
|
||||
|
||||
private async finalizeMessage(
|
||||
@@ -775,10 +945,9 @@ export class DiscordExecApprovalHandler {
|
||||
}
|
||||
|
||||
try {
|
||||
const { rest, request: discordRequest } = createDiscordClient(
|
||||
{ token: this.opts.token, accountId: this.opts.accountId },
|
||||
this.opts.cfg,
|
||||
);
|
||||
const { rest, request: discordRequest } = (
|
||||
this.opts.__testing?.createDiscordClient ?? sendShared.createDiscordClient
|
||||
)({ token: this.opts.token, accountId: this.opts.accountId }, this.opts.cfg);
|
||||
|
||||
await discordRequest(
|
||||
() => rest.delete(Routes.channelMessage(channelId, messageId)) as Promise<void>,
|
||||
@@ -796,16 +965,15 @@ export class DiscordExecApprovalHandler {
|
||||
container: DiscordUiContainer,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const { rest, request: discordRequest } = createDiscordClient(
|
||||
{ token: this.opts.token, accountId: this.opts.accountId },
|
||||
this.opts.cfg,
|
||||
);
|
||||
const { rest, request: discordRequest } = (
|
||||
this.opts.__testing?.createDiscordClient ?? sendShared.createDiscordClient
|
||||
)({ token: this.opts.token, accountId: this.opts.accountId }, this.opts.cfg);
|
||||
const payload = buildExecApprovalPayload(container);
|
||||
|
||||
await discordRequest(
|
||||
() =>
|
||||
rest.patch(Routes.channelMessage(channelId, messageId), {
|
||||
body: stripUndefinedFields(serializePayload(payload)),
|
||||
body: sendShared.stripUndefinedFields(serializePayload(payload)),
|
||||
}),
|
||||
"update-approval",
|
||||
);
|
||||
@@ -815,6 +983,11 @@ export class DiscordExecApprovalHandler {
|
||||
}
|
||||
|
||||
async resolveApproval(approvalId: string, decision: ExecApprovalDecision): Promise<boolean> {
|
||||
if (!this.gatewayClient) {
|
||||
logError("discord exec approvals: gateway client not connected");
|
||||
return false;
|
||||
}
|
||||
|
||||
const method =
|
||||
resolveApprovalKindFromId(approvalId) === "plugin"
|
||||
? "plugin.approval.resolve"
|
||||
@@ -822,7 +995,7 @@ export class DiscordExecApprovalHandler {
|
||||
logDebug(`discord exec approvals: resolving ${approvalId} with ${decision} via ${method}`);
|
||||
|
||||
try {
|
||||
await this.runtime.request(method, {
|
||||
await this.gatewayClient.request(method, {
|
||||
id: approvalId,
|
||||
decision,
|
||||
});
|
||||
@@ -875,7 +1048,7 @@ export class ExecApprovalButton extends Button {
|
||||
if (!approvers.some((id) => String(id) === userId)) {
|
||||
try {
|
||||
await interaction.reply({
|
||||
content: "⛔ You are not authorized to approve exec requests.",
|
||||
content: "⛔ You are not authorized to approve requests.",
|
||||
ephemeral: true,
|
||||
});
|
||||
} catch {
|
||||
|
||||
@@ -240,14 +240,13 @@ describe("matrix setup post-write bootstrap", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("clears allowPrivateNetwork and proxy when deleting the default Matrix account config", () => {
|
||||
it("clears allowPrivateNetwork when deleting the default Matrix account config", () => {
|
||||
const updated = matrixPlugin.config.deleteAccount?.({
|
||||
cfg: {
|
||||
channels: {
|
||||
matrix: {
|
||||
homeserver: "http://localhost.localdomain:8008",
|
||||
allowPrivateNetwork: true,
|
||||
proxy: "http://127.0.0.1:7890",
|
||||
accounts: {
|
||||
ops: {
|
||||
enabled: true,
|
||||
|
||||
@@ -154,7 +154,6 @@ const matrixConfigAdapter = createScopedChannelConfigAdapter<
|
||||
"name",
|
||||
"homeserver",
|
||||
"allowPrivateNetwork",
|
||||
"proxy",
|
||||
"userId",
|
||||
"accessToken",
|
||||
"password",
|
||||
@@ -440,7 +439,6 @@ export const matrixPlugin: ChannelPlugin<ResolvedMatrixAccount, MatrixProbe> =
|
||||
accountId: account.accountId,
|
||||
allowPrivateNetwork: auth.allowPrivateNetwork,
|
||||
ssrfPolicy: auth.ssrfPolicy,
|
||||
dispatcherPolicy: auth.dispatcherPolicy,
|
||||
});
|
||||
} catch (err) {
|
||||
return {
|
||||
|
||||
@@ -158,7 +158,6 @@ async function addMatrixAccount(params: {
|
||||
name?: string;
|
||||
avatarUrl?: string;
|
||||
homeserver?: string;
|
||||
proxy?: string;
|
||||
userId?: string;
|
||||
accessToken?: string;
|
||||
password?: string;
|
||||
@@ -178,7 +177,6 @@ async function addMatrixAccount(params: {
|
||||
avatarUrl: params.avatarUrl,
|
||||
homeserver: params.homeserver,
|
||||
allowPrivateNetwork: params.allowPrivateNetwork,
|
||||
proxy: params.proxy,
|
||||
userId: params.userId,
|
||||
accessToken: params.accessToken,
|
||||
password: params.password,
|
||||
@@ -677,7 +675,6 @@ export function registerMatrixCli(params: { program: Command }): void {
|
||||
.option("--name <name>", "Optional display name for this account")
|
||||
.option("--avatar-url <url>", "Optional Matrix avatar URL (mxc:// or http(s) URL)")
|
||||
.option("--homeserver <url>", "Matrix homeserver URL")
|
||||
.option("--proxy <url>", "Optional HTTP(S) proxy URL for Matrix requests")
|
||||
.option(
|
||||
"--allow-private-network",
|
||||
"Allow Matrix homeserver traffic to private/internal hosts for this account",
|
||||
@@ -699,7 +696,6 @@ export function registerMatrixCli(params: { program: Command }): void {
|
||||
name?: string;
|
||||
avatarUrl?: string;
|
||||
homeserver?: string;
|
||||
proxy?: string;
|
||||
allowPrivateNetwork?: boolean;
|
||||
userId?: string;
|
||||
accessToken?: string;
|
||||
@@ -719,7 +715,6 @@ export function registerMatrixCli(params: { program: Command }): void {
|
||||
name: options.name,
|
||||
avatarUrl: options.avatarUrl,
|
||||
homeserver: options.homeserver,
|
||||
proxy: options.proxy,
|
||||
allowPrivateNetwork: options.allowPrivateNetwork === true,
|
||||
userId: options.userId,
|
||||
accessToken: options.accessToken,
|
||||
|
||||
@@ -53,7 +53,6 @@ export const MatrixConfigSchema = z.object({
|
||||
markdown: MarkdownConfigSchema,
|
||||
homeserver: z.string().optional(),
|
||||
allowPrivateNetwork: z.boolean().optional(),
|
||||
proxy: z.string().optional(),
|
||||
userId: z.string().optional(),
|
||||
accessToken: buildSecretInputSchema().optional(),
|
||||
password: buildSecretInputSchema().optional(),
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { matrixAuthedHttpClientCtorMock, requestJsonMock } = vi.hoisted(() => ({
|
||||
matrixAuthedHttpClientCtorMock: vi.fn(),
|
||||
const { requestJsonMock } = vi.hoisted(() => ({
|
||||
requestJsonMock: vi.fn(),
|
||||
}));
|
||||
|
||||
@@ -11,10 +10,6 @@ vi.mock("./matrix/client.js", () => ({
|
||||
|
||||
vi.mock("./matrix/sdk/http-client.js", () => ({
|
||||
MatrixAuthedHttpClient: class {
|
||||
constructor(params: unknown) {
|
||||
matrixAuthedHttpClientCtorMock(params);
|
||||
}
|
||||
|
||||
requestJson(params: unknown) {
|
||||
return requestJsonMock(params);
|
||||
}
|
||||
@@ -40,7 +35,6 @@ describe("matrix directory live", () => {
|
||||
userId: "@bot:example.org",
|
||||
accessToken: "test-token",
|
||||
});
|
||||
matrixAuthedHttpClientCtorMock.mockReset();
|
||||
requestJsonMock.mockReset();
|
||||
requestJsonMock.mockResolvedValue({ results: [] });
|
||||
});
|
||||
@@ -67,35 +61,6 @@ describe("matrix directory live", () => {
|
||||
expect(resolveMatrixAuth).toHaveBeenCalledWith({ cfg, accountId: "assistant" });
|
||||
});
|
||||
|
||||
it("passes dispatcherPolicy through to the live directory client", async () => {
|
||||
vi.mocked(resolveMatrixAuth).mockResolvedValue({
|
||||
accountId: "assistant",
|
||||
homeserver: "https://matrix.example.org",
|
||||
userId: "@bot:example.org",
|
||||
accessToken: "test-token",
|
||||
dispatcherPolicy: {
|
||||
mode: "explicit-proxy",
|
||||
proxyUrl: "http://proxy.internal:8080",
|
||||
},
|
||||
});
|
||||
|
||||
await listMatrixDirectoryPeersLive({
|
||||
cfg,
|
||||
accountId: "assistant",
|
||||
query: "alice",
|
||||
});
|
||||
|
||||
expect(matrixAuthedHttpClientCtorMock).toHaveBeenCalledWith({
|
||||
homeserver: "https://matrix.example.org",
|
||||
accessToken: "test-token",
|
||||
ssrfPolicy: undefined,
|
||||
dispatcherPolicy: {
|
||||
mode: "explicit-proxy",
|
||||
proxyUrl: "http://proxy.internal:8080",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("returns no peer results for empty query without resolving auth", async () => {
|
||||
const result = await listMatrixDirectoryPeersLive({
|
||||
cfg,
|
||||
|
||||
@@ -46,12 +46,7 @@ function resolveMatrixDirectoryLimit(limit?: number | null): number {
|
||||
}
|
||||
|
||||
function createMatrixDirectoryClient(auth: MatrixResolvedAuth): MatrixAuthedHttpClient {
|
||||
return new MatrixAuthedHttpClient({
|
||||
homeserver: auth.homeserver,
|
||||
accessToken: auth.accessToken,
|
||||
ssrfPolicy: auth.ssrfPolicy,
|
||||
dispatcherPolicy: auth.dispatcherPolicy,
|
||||
});
|
||||
return new MatrixAuthedHttpClient(auth.homeserver, auth.accessToken, auth.ssrfPolicy);
|
||||
}
|
||||
|
||||
async function resolveMatrixDirectoryContext(params: MatrixDirectoryLiveParams): Promise<{
|
||||
|
||||
@@ -542,51 +542,6 @@ describe("resolveMatrixConfig", () => {
|
||||
).toBe("http://matrix-synapse:8008");
|
||||
});
|
||||
|
||||
it("resolves an explicit proxy dispatcher from top-level Matrix config", () => {
|
||||
const cfg = {
|
||||
channels: {
|
||||
matrix: {
|
||||
homeserver: "https://matrix.example.org",
|
||||
accessToken: "tok-123",
|
||||
proxy: "http://127.0.0.1:7890",
|
||||
},
|
||||
},
|
||||
} as CoreConfig;
|
||||
|
||||
const resolved = resolveMatrixConfig(cfg, {} as NodeJS.ProcessEnv);
|
||||
|
||||
expect(resolved.dispatcherPolicy).toEqual({
|
||||
mode: "explicit-proxy",
|
||||
proxyUrl: "http://127.0.0.1:7890",
|
||||
});
|
||||
});
|
||||
|
||||
it("prefers account proxy overrides over top-level Matrix proxy config", () => {
|
||||
const cfg = {
|
||||
channels: {
|
||||
matrix: {
|
||||
homeserver: "https://matrix.example.org",
|
||||
accessToken: "base-token",
|
||||
proxy: "http://127.0.0.1:7890",
|
||||
accounts: {
|
||||
ops: {
|
||||
homeserver: "https://matrix.ops.example.org",
|
||||
accessToken: "ops-token",
|
||||
proxy: "http://127.0.0.1:7891",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as CoreConfig;
|
||||
|
||||
const resolved = resolveMatrixConfigForAccount(cfg, "ops", {} as NodeJS.ProcessEnv);
|
||||
|
||||
expect(resolved.dispatcherPolicy).toEqual({
|
||||
mode: "explicit-proxy",
|
||||
proxyUrl: "http://127.0.0.1:7891",
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects public http homeservers even when private-network access is enabled", async () => {
|
||||
await expect(
|
||||
resolveValidatedMatrixHomeserverUrl("http://matrix.example.org:8008", {
|
||||
|
||||
@@ -2,7 +2,6 @@ import {
|
||||
coerceSecretRef,
|
||||
resolveConfiguredSecretInputString,
|
||||
} from "openclaw/plugin-sdk/config-runtime";
|
||||
import type { PinnedDispatcherPolicy } from "openclaw/plugin-sdk/infra-runtime";
|
||||
import {
|
||||
requiresExplicitMatrixDefaultAccount,
|
||||
resolveMatrixDefaultOrOnlyAccountId,
|
||||
@@ -291,22 +290,15 @@ function clampMatrixInitialSyncLimit(value: unknown): number | undefined {
|
||||
const MATRIX_HTTP_HOMESERVER_ERROR =
|
||||
"Matrix homeserver must use https:// unless it targets a private or loopback host";
|
||||
|
||||
function buildMatrixNetworkFields(params: {
|
||||
allowPrivateNetwork: boolean | undefined;
|
||||
proxy?: string;
|
||||
dispatcherPolicy?: PinnedDispatcherPolicy;
|
||||
}): Pick<MatrixResolvedConfig, "allowPrivateNetwork" | "ssrfPolicy" | "dispatcherPolicy"> {
|
||||
const dispatcherPolicy: PinnedDispatcherPolicy | undefined =
|
||||
params.dispatcherPolicy ??
|
||||
(params.proxy ? { mode: "explicit-proxy", proxyUrl: params.proxy } : undefined);
|
||||
if (!params.allowPrivateNetwork && !dispatcherPolicy) {
|
||||
function buildMatrixNetworkFields(
|
||||
allowPrivateNetwork: boolean | undefined,
|
||||
): Pick<MatrixResolvedConfig, "allowPrivateNetwork" | "ssrfPolicy"> {
|
||||
if (!allowPrivateNetwork) {
|
||||
return {};
|
||||
}
|
||||
return {
|
||||
...(params.allowPrivateNetwork
|
||||
? { allowPrivateNetwork: true, ssrfPolicy: ssrfPolicyFromAllowPrivateNetwork(true) }
|
||||
: {}),
|
||||
...(dispatcherPolicy ? { dispatcherPolicy } : {}),
|
||||
allowPrivateNetwork: true,
|
||||
ssrfPolicy: ssrfPolicyFromAllowPrivateNetwork(true),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -500,7 +492,7 @@ export function resolveMatrixConfig(
|
||||
deviceName: resolvedStrings.deviceName || undefined,
|
||||
initialSyncLimit,
|
||||
encryption,
|
||||
...buildMatrixNetworkFields({ allowPrivateNetwork, proxy: matrix.proxy }),
|
||||
...buildMatrixNetworkFields(allowPrivateNetwork),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -570,10 +562,7 @@ export function resolveMatrixConfigForAccount(
|
||||
deviceName: resolvedStrings.deviceName || undefined,
|
||||
initialSyncLimit,
|
||||
encryption,
|
||||
...buildMatrixNetworkFields({
|
||||
allowPrivateNetwork,
|
||||
proxy: account.proxy ?? matrix.proxy,
|
||||
}),
|
||||
...buildMatrixNetworkFields(allowPrivateNetwork),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -673,7 +662,6 @@ export async function resolveMatrixAuth(params?: {
|
||||
ensureMatrixSdkLoggingConfigured();
|
||||
const tempClient = new MatrixClient(homeserver, accessToken, undefined, undefined, {
|
||||
ssrfPolicy: resolved.ssrfPolicy,
|
||||
dispatcherPolicy: resolved.dispatcherPolicy,
|
||||
});
|
||||
const whoami = (await tempClient.doRequest("GET", "/_matrix/client/v3/account/whoami")) as {
|
||||
user_id?: string;
|
||||
@@ -722,10 +710,7 @@ export async function resolveMatrixAuth(params?: {
|
||||
deviceName: resolved.deviceName,
|
||||
initialSyncLimit: resolved.initialSyncLimit,
|
||||
encryption: resolved.encryption,
|
||||
...buildMatrixNetworkFields({
|
||||
allowPrivateNetwork: resolved.allowPrivateNetwork,
|
||||
dispatcherPolicy: resolved.dispatcherPolicy,
|
||||
}),
|
||||
...buildMatrixNetworkFields(resolved.allowPrivateNetwork),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -742,10 +727,7 @@ export async function resolveMatrixAuth(params?: {
|
||||
deviceName: resolved.deviceName,
|
||||
initialSyncLimit: resolved.initialSyncLimit,
|
||||
encryption: resolved.encryption,
|
||||
...buildMatrixNetworkFields({
|
||||
allowPrivateNetwork: resolved.allowPrivateNetwork,
|
||||
dispatcherPolicy: resolved.dispatcherPolicy,
|
||||
}),
|
||||
...buildMatrixNetworkFields(resolved.allowPrivateNetwork),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -770,7 +752,6 @@ export async function resolveMatrixAuth(params?: {
|
||||
ensureMatrixSdkLoggingConfigured();
|
||||
const loginClient = new MatrixClient(homeserver, "", undefined, undefined, {
|
||||
ssrfPolicy: resolved.ssrfPolicy,
|
||||
dispatcherPolicy: resolved.dispatcherPolicy,
|
||||
});
|
||||
const login = (await loginClient.doRequest("POST", "/_matrix/client/v3/login", undefined, {
|
||||
type: "m.login.password",
|
||||
@@ -799,10 +780,7 @@ export async function resolveMatrixAuth(params?: {
|
||||
deviceName: resolved.deviceName,
|
||||
initialSyncLimit: resolved.initialSyncLimit,
|
||||
encryption: resolved.encryption,
|
||||
...buildMatrixNetworkFields({
|
||||
allowPrivateNetwork: resolved.allowPrivateNetwork,
|
||||
dispatcherPolicy: resolved.dispatcherPolicy,
|
||||
}),
|
||||
...buildMatrixNetworkFields(resolved.allowPrivateNetwork),
|
||||
};
|
||||
|
||||
const { saveMatrixCredentials } = await loadCredentialsWriter();
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import fs from "node:fs";
|
||||
import type { PinnedDispatcherPolicy } from "openclaw/plugin-sdk/infra-runtime";
|
||||
import type { SsrFPolicy } from "../../runtime-api.js";
|
||||
import { MatrixClient } from "../sdk.js";
|
||||
import { resolveValidatedMatrixHomeserverUrl } from "./config.js";
|
||||
@@ -23,7 +22,6 @@ export async function createMatrixClient(params: {
|
||||
autoBootstrapCrypto?: boolean;
|
||||
allowPrivateNetwork?: boolean;
|
||||
ssrfPolicy?: SsrFPolicy;
|
||||
dispatcherPolicy?: PinnedDispatcherPolicy;
|
||||
}): Promise<MatrixClient> {
|
||||
ensureMatrixSdkLoggingConfigured();
|
||||
const env = process.env;
|
||||
@@ -70,6 +68,5 @@ export async function createMatrixClient(params: {
|
||||
cryptoDatabasePrefix,
|
||||
autoBootstrapCrypto: params.autoBootstrapCrypto,
|
||||
ssrfPolicy: params.ssrfPolicy,
|
||||
dispatcherPolicy: params.dispatcherPolicy,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -230,33 +230,4 @@ describe("resolveSharedMatrixClient", () => {
|
||||
}),
|
||||
).rejects.toThrow("Matrix shared client account mismatch");
|
||||
});
|
||||
|
||||
it("recreates the shared client when dispatcherPolicy changes", async () => {
|
||||
const firstAuth = {
|
||||
...authFor("main"),
|
||||
dispatcherPolicy: {
|
||||
mode: "explicit-proxy" as const,
|
||||
proxyUrl: "http://127.0.0.1:7890",
|
||||
},
|
||||
};
|
||||
const secondAuth = {
|
||||
...authFor("main"),
|
||||
dispatcherPolicy: {
|
||||
mode: "explicit-proxy" as const,
|
||||
proxyUrl: "http://127.0.0.1:7891",
|
||||
},
|
||||
};
|
||||
const firstClient = createMockClient("main-first");
|
||||
const secondClient = createMockClient("main-second");
|
||||
|
||||
resolveMatrixAuthMock.mockResolvedValueOnce(firstAuth).mockResolvedValueOnce(secondAuth);
|
||||
createMatrixClientMock.mockResolvedValueOnce(firstClient).mockResolvedValueOnce(secondClient);
|
||||
|
||||
const first = await resolveSharedMatrixClient({ accountId: "main", startClient: false });
|
||||
const second = await resolveSharedMatrixClient({ accountId: "main", startClient: false });
|
||||
|
||||
expect(first).toBe(firstClient);
|
||||
expect(second).toBe(secondClient);
|
||||
expect(createMatrixClientMock).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -18,10 +18,6 @@ type SharedMatrixClientState = {
|
||||
const sharedClientStates = new Map<string, SharedMatrixClientState>();
|
||||
const sharedClientPromises = new Map<string, Promise<SharedMatrixClientState>>();
|
||||
|
||||
function serializeDispatcherPolicyKey(auth: MatrixAuth): string {
|
||||
return JSON.stringify(auth.dispatcherPolicy ?? null);
|
||||
}
|
||||
|
||||
function buildSharedClientKey(auth: MatrixAuth): string {
|
||||
return [
|
||||
auth.homeserver,
|
||||
@@ -29,7 +25,6 @@ function buildSharedClientKey(auth: MatrixAuth): string {
|
||||
auth.accessToken,
|
||||
auth.encryption ? "e2ee" : "plain",
|
||||
auth.allowPrivateNetwork ? "private-net" : "strict-net",
|
||||
serializeDispatcherPolicyKey(auth),
|
||||
auth.accountId,
|
||||
].join("|");
|
||||
}
|
||||
@@ -50,7 +45,6 @@ async function createSharedMatrixClient(params: {
|
||||
accountId: params.auth.accountId,
|
||||
allowPrivateNetwork: params.auth.allowPrivateNetwork,
|
||||
ssrfPolicy: params.auth.ssrfPolicy,
|
||||
dispatcherPolicy: params.auth.dispatcherPolicy,
|
||||
});
|
||||
return {
|
||||
client,
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import type { PinnedDispatcherPolicy } from "openclaw/plugin-sdk/infra-runtime";
|
||||
import type { SsrFPolicy } from "../../runtime-api.js";
|
||||
|
||||
export type MatrixResolvedConfig = {
|
||||
@@ -12,7 +11,6 @@ export type MatrixResolvedConfig = {
|
||||
encryption?: boolean;
|
||||
allowPrivateNetwork?: boolean;
|
||||
ssrfPolicy?: SsrFPolicy;
|
||||
dispatcherPolicy?: PinnedDispatcherPolicy;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -35,7 +33,6 @@ export type MatrixAuth = {
|
||||
encryption?: boolean;
|
||||
allowPrivateNetwork?: boolean;
|
||||
ssrfPolicy?: SsrFPolicy;
|
||||
dispatcherPolicy?: PinnedDispatcherPolicy;
|
||||
};
|
||||
|
||||
export type MatrixStoragePaths = {
|
||||
|
||||
@@ -73,7 +73,7 @@ describe("updateMatrixAccountConfig", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("stores and clears Matrix allowBots, allowPrivateNetwork, and proxy settings", () => {
|
||||
it("stores and clears Matrix allowBots and allowPrivateNetwork settings", () => {
|
||||
const cfg = {
|
||||
channels: {
|
||||
matrix: {
|
||||
@@ -81,7 +81,6 @@ describe("updateMatrixAccountConfig", () => {
|
||||
default: {
|
||||
allowBots: true,
|
||||
allowPrivateNetwork: true,
|
||||
proxy: "http://127.0.0.1:7890",
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -91,14 +90,12 @@ describe("updateMatrixAccountConfig", () => {
|
||||
const updated = updateMatrixAccountConfig(cfg, "default", {
|
||||
allowBots: "mentions",
|
||||
allowPrivateNetwork: null,
|
||||
proxy: null,
|
||||
});
|
||||
|
||||
expect(updated.channels?.["matrix"]?.accounts?.default).toMatchObject({
|
||||
allowBots: "mentions",
|
||||
});
|
||||
expect(updated.channels?.["matrix"]?.accounts?.default?.allowPrivateNetwork).toBeUndefined();
|
||||
expect(updated.channels?.["matrix"]?.accounts?.default?.proxy).toBeUndefined();
|
||||
});
|
||||
|
||||
it("normalizes account id and defaults account enabled=true", () => {
|
||||
|
||||
@@ -10,7 +10,6 @@ export type MatrixAccountPatch = {
|
||||
enabled?: boolean;
|
||||
homeserver?: string | null;
|
||||
allowPrivateNetwork?: boolean | null;
|
||||
proxy?: string | null;
|
||||
userId?: string | null;
|
||||
accessToken?: MatrixConfig["accessToken"] | null;
|
||||
password?: MatrixConfig["password"] | null;
|
||||
@@ -172,7 +171,6 @@ export function updateMatrixAccountConfig(
|
||||
}
|
||||
|
||||
applyNullableStringField(nextAccount, "homeserver", patch.homeserver);
|
||||
applyNullableStringField(nextAccount, "proxy", patch.proxy);
|
||||
applyNullableStringField(nextAccount, "userId", patch.userId);
|
||||
applyNullableSecretInputField(
|
||||
nextAccount,
|
||||
|
||||
@@ -69,29 +69,6 @@ describe("probeMatrix", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("passes dispatcherPolicy through to client creation", async () => {
|
||||
await probeMatrix({
|
||||
homeserver: "https://matrix.example.org",
|
||||
accessToken: "tok",
|
||||
timeoutMs: 500,
|
||||
dispatcherPolicy: {
|
||||
mode: "explicit-proxy",
|
||||
proxyUrl: "http://127.0.0.1:7890",
|
||||
},
|
||||
});
|
||||
|
||||
expect(createMatrixClientMock).toHaveBeenCalledWith({
|
||||
homeserver: "https://matrix.example.org",
|
||||
userId: undefined,
|
||||
accessToken: "tok",
|
||||
localTimeoutMs: 500,
|
||||
dispatcherPolicy: {
|
||||
mode: "explicit-proxy",
|
||||
proxyUrl: "http://127.0.0.1:7890",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("returns client validation errors for insecure public http homeservers", async () => {
|
||||
createMatrixClientMock.mockRejectedValue(
|
||||
new Error("Matrix homeserver must use https:// unless it targets a private or loopback host"),
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import type { PinnedDispatcherPolicy } from "openclaw/plugin-sdk/infra-runtime";
|
||||
import type { SsrFPolicy } from "../runtime-api.js";
|
||||
import type { BaseProbeResult } from "../runtime-api.js";
|
||||
import { createMatrixClient, isBunRuntime } from "./client.js";
|
||||
@@ -17,7 +16,6 @@ export async function probeMatrix(params: {
|
||||
accountId?: string | null;
|
||||
allowPrivateNetwork?: boolean;
|
||||
ssrfPolicy?: SsrFPolicy;
|
||||
dispatcherPolicy?: PinnedDispatcherPolicy;
|
||||
}): Promise<MatrixProbe> {
|
||||
const started = Date.now();
|
||||
const result: MatrixProbe = {
|
||||
@@ -57,7 +55,6 @@ export async function probeMatrix(params: {
|
||||
accountId: params.accountId,
|
||||
allowPrivateNetwork: params.allowPrivateNetwork,
|
||||
ssrfPolicy: params.ssrfPolicy,
|
||||
dispatcherPolicy: params.dispatcherPolicy,
|
||||
});
|
||||
// The client wrapper resolves user ID via whoami when needed.
|
||||
const userId = await client.getUserId();
|
||||
|
||||
@@ -11,7 +11,6 @@ import {
|
||||
} from "matrix-js-sdk/lib/matrix.js";
|
||||
import { VerificationMethod } from "matrix-js-sdk/lib/types.js";
|
||||
import { KeyedAsyncQueue } from "openclaw/plugin-sdk/core";
|
||||
import type { PinnedDispatcherPolicy } from "openclaw/plugin-sdk/infra-runtime";
|
||||
import type { SsrFPolicy } from "../runtime-api.js";
|
||||
import { resolveMatrixRoomKeyBackupReadinessError } from "./backup-health.js";
|
||||
import { FileBackedMatrixSyncStore } from "./client/file-sync-store.js";
|
||||
@@ -222,15 +221,9 @@ export class MatrixClient {
|
||||
cryptoDatabasePrefix?: string;
|
||||
autoBootstrapCrypto?: boolean;
|
||||
ssrfPolicy?: SsrFPolicy;
|
||||
dispatcherPolicy?: PinnedDispatcherPolicy;
|
||||
} = {},
|
||||
) {
|
||||
this.httpClient = new MatrixAuthedHttpClient({
|
||||
homeserver,
|
||||
accessToken,
|
||||
ssrfPolicy: opts.ssrfPolicy,
|
||||
dispatcherPolicy: opts.dispatcherPolicy,
|
||||
});
|
||||
this.httpClient = new MatrixAuthedHttpClient(homeserver, accessToken, opts.ssrfPolicy);
|
||||
this.localTimeoutMs = Math.max(1, opts.localTimeoutMs ?? 60_000);
|
||||
this.initialSyncLimit = opts.initialSyncLimit;
|
||||
this.encryptionEnabled = opts.encryption === true;
|
||||
@@ -251,10 +244,7 @@ export class MatrixClient {
|
||||
deviceId: opts.deviceId,
|
||||
logger: createMatrixJsSdkClientLogger("MatrixClient"),
|
||||
localTimeoutMs: this.localTimeoutMs,
|
||||
fetchFn: createMatrixGuardedFetch({
|
||||
ssrfPolicy: opts.ssrfPolicy,
|
||||
dispatcherPolicy: opts.dispatcherPolicy,
|
||||
}),
|
||||
fetchFn: createMatrixGuardedFetch({ ssrfPolicy: opts.ssrfPolicy }),
|
||||
store: this.syncStore,
|
||||
cryptoCallbacks: cryptoCallbacks as never,
|
||||
verificationMethods: [
|
||||
|
||||
@@ -27,16 +27,8 @@ describe("MatrixAuthedHttpClient", () => {
|
||||
buffer: Buffer.from('{"ok":true}', "utf8"),
|
||||
});
|
||||
|
||||
const client = new MatrixAuthedHttpClient({
|
||||
homeserver: "https://matrix.example.org",
|
||||
accessToken: "token",
|
||||
ssrfPolicy: {
|
||||
allowPrivateNetwork: true,
|
||||
},
|
||||
dispatcherPolicy: {
|
||||
mode: "explicit-proxy",
|
||||
proxyUrl: "http://proxy.internal:8080",
|
||||
},
|
||||
const client = new MatrixAuthedHttpClient("https://matrix.example.org", "token", {
|
||||
allowPrivateNetwork: true,
|
||||
});
|
||||
const result = await client.requestJson({
|
||||
method: "GET",
|
||||
@@ -52,10 +44,6 @@ describe("MatrixAuthedHttpClient", () => {
|
||||
endpoint: "https://matrix.example.org/_matrix/client/v3/account/whoami",
|
||||
allowAbsoluteEndpoint: true,
|
||||
ssrfPolicy: { allowPrivateNetwork: true },
|
||||
dispatcherPolicy: {
|
||||
mode: "explicit-proxy",
|
||||
proxyUrl: "http://proxy.internal:8080",
|
||||
},
|
||||
}),
|
||||
);
|
||||
});
|
||||
@@ -70,10 +58,7 @@ describe("MatrixAuthedHttpClient", () => {
|
||||
buffer: Buffer.from("pong", "utf8"),
|
||||
});
|
||||
|
||||
const client = new MatrixAuthedHttpClient({
|
||||
homeserver: "https://matrix.example.org",
|
||||
accessToken: "token",
|
||||
});
|
||||
const client = new MatrixAuthedHttpClient("https://matrix.example.org", "token");
|
||||
const result = await client.requestJson({
|
||||
method: "GET",
|
||||
endpoint: "/_matrix/client/v3/ping",
|
||||
@@ -91,10 +76,7 @@ describe("MatrixAuthedHttpClient", () => {
|
||||
buffer: payload,
|
||||
});
|
||||
|
||||
const client = new MatrixAuthedHttpClient({
|
||||
homeserver: "https://matrix.example.org",
|
||||
accessToken: "token",
|
||||
});
|
||||
const client = new MatrixAuthedHttpClient("https://matrix.example.org", "token");
|
||||
const result = await client.requestRaw({
|
||||
method: "GET",
|
||||
endpoint: "/_matrix/media/v3/download/example/id",
|
||||
@@ -114,10 +96,7 @@ describe("MatrixAuthedHttpClient", () => {
|
||||
buffer: Buffer.from(JSON.stringify({ error: "forbidden" }), "utf8"),
|
||||
});
|
||||
|
||||
const client = new MatrixAuthedHttpClient({
|
||||
homeserver: "https://matrix.example.org",
|
||||
accessToken: "token",
|
||||
});
|
||||
const client = new MatrixAuthedHttpClient("https://matrix.example.org", "token");
|
||||
await expect(
|
||||
client.requestJson({
|
||||
method: "GET",
|
||||
|
||||
@@ -1,27 +1,13 @@
|
||||
import type { PinnedDispatcherPolicy } from "openclaw/plugin-sdk/infra-runtime";
|
||||
import type { SsrFPolicy } from "../../runtime-api.js";
|
||||
import { buildHttpError } from "./event-helpers.js";
|
||||
import { type HttpMethod, type QueryParams, performMatrixRequest } from "./transport.js";
|
||||
|
||||
type MatrixAuthedHttpClientParams = {
|
||||
homeserver: string;
|
||||
accessToken: string;
|
||||
ssrfPolicy?: SsrFPolicy;
|
||||
dispatcherPolicy?: PinnedDispatcherPolicy;
|
||||
};
|
||||
|
||||
export class MatrixAuthedHttpClient {
|
||||
private readonly homeserver: string;
|
||||
private readonly accessToken: string;
|
||||
private readonly ssrfPolicy?: SsrFPolicy;
|
||||
private readonly dispatcherPolicy?: PinnedDispatcherPolicy;
|
||||
|
||||
constructor(params: MatrixAuthedHttpClientParams) {
|
||||
this.homeserver = params.homeserver;
|
||||
this.accessToken = params.accessToken;
|
||||
this.ssrfPolicy = params.ssrfPolicy;
|
||||
this.dispatcherPolicy = params.dispatcherPolicy;
|
||||
}
|
||||
constructor(
|
||||
private readonly homeserver: string,
|
||||
private readonly accessToken: string,
|
||||
private readonly ssrfPolicy?: SsrFPolicy,
|
||||
) {}
|
||||
|
||||
async requestJson(params: {
|
||||
method: HttpMethod;
|
||||
@@ -40,7 +26,6 @@ export class MatrixAuthedHttpClient {
|
||||
body: params.body,
|
||||
timeoutMs: params.timeoutMs,
|
||||
ssrfPolicy: this.ssrfPolicy,
|
||||
dispatcherPolicy: this.dispatcherPolicy,
|
||||
allowAbsoluteEndpoint: params.allowAbsoluteEndpoint,
|
||||
});
|
||||
if (!response.ok) {
|
||||
@@ -76,7 +61,6 @@ export class MatrixAuthedHttpClient {
|
||||
maxBytes: params.maxBytes,
|
||||
readIdleTimeoutMs: params.readIdleTimeoutMs,
|
||||
ssrfPolicy: this.ssrfPolicy,
|
||||
dispatcherPolicy: this.dispatcherPolicy,
|
||||
allowAbsoluteEndpoint: params.allowAbsoluteEndpoint,
|
||||
});
|
||||
if (!response.ok) {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import type { PinnedDispatcherPolicy } from "openclaw/plugin-sdk/infra-runtime";
|
||||
import {
|
||||
buildTimeoutAbortSignal,
|
||||
closeDispatcher,
|
||||
@@ -89,7 +88,6 @@ async function fetchWithMatrixGuardedRedirects(params: {
|
||||
signal?: AbortSignal;
|
||||
timeoutMs?: number;
|
||||
ssrfPolicy?: SsrFPolicy;
|
||||
dispatcherPolicy?: PinnedDispatcherPolicy;
|
||||
}): Promise<{ response: Response; release: () => Promise<void>; finalUrl: string }> {
|
||||
let currentUrl = new URL(params.url);
|
||||
let method = (params.init?.method ?? "GET").toUpperCase();
|
||||
@@ -108,7 +106,7 @@ async function fetchWithMatrixGuardedRedirects(params: {
|
||||
const pinned = await resolvePinnedHostnameWithPolicy(currentUrl.hostname, {
|
||||
policy: params.ssrfPolicy,
|
||||
});
|
||||
dispatcher = createPinnedDispatcher(pinned, params.dispatcherPolicy, params.ssrfPolicy);
|
||||
dispatcher = createPinnedDispatcher(pinned, undefined, params.ssrfPolicy);
|
||||
const response = await fetch(currentUrl.toString(), {
|
||||
...params.init,
|
||||
method,
|
||||
@@ -186,10 +184,7 @@ async function fetchWithMatrixGuardedRedirects(params: {
|
||||
throw new Error(`Too many redirects while requesting ${params.url}`);
|
||||
}
|
||||
|
||||
export function createMatrixGuardedFetch(params: {
|
||||
ssrfPolicy?: SsrFPolicy;
|
||||
dispatcherPolicy?: PinnedDispatcherPolicy;
|
||||
}): typeof fetch {
|
||||
export function createMatrixGuardedFetch(params: { ssrfPolicy?: SsrFPolicy }): typeof fetch {
|
||||
return (async (resource: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url = toFetchUrl(resource);
|
||||
const { signal, ...requestInit } = init ?? {};
|
||||
@@ -198,7 +193,6 @@ export function createMatrixGuardedFetch(params: {
|
||||
init: requestInit,
|
||||
signal: signal ?? undefined,
|
||||
ssrfPolicy: params.ssrfPolicy,
|
||||
dispatcherPolicy: params.dispatcherPolicy,
|
||||
});
|
||||
|
||||
try {
|
||||
@@ -226,7 +220,6 @@ export async function performMatrixRequest(params: {
|
||||
maxBytes?: number;
|
||||
readIdleTimeoutMs?: number;
|
||||
ssrfPolicy?: SsrFPolicy;
|
||||
dispatcherPolicy?: PinnedDispatcherPolicy;
|
||||
allowAbsoluteEndpoint?: boolean;
|
||||
}): Promise<{ response: Response; text: string; buffer: Buffer }> {
|
||||
const isAbsoluteEndpoint =
|
||||
@@ -271,7 +264,6 @@ export async function performMatrixRequest(params: {
|
||||
},
|
||||
timeoutMs: params.timeoutMs,
|
||||
ssrfPolicy: params.ssrfPolicy,
|
||||
dispatcherPolicy: params.dispatcherPolicy,
|
||||
});
|
||||
|
||||
try {
|
||||
|
||||
@@ -208,7 +208,6 @@ export function applyMatrixSetupAccountConfig(params: {
|
||||
enabled: true,
|
||||
homeserver: null,
|
||||
allowPrivateNetwork: null,
|
||||
proxy: null,
|
||||
userId: null,
|
||||
accessToken: null,
|
||||
password: null,
|
||||
@@ -227,7 +226,6 @@ export function applyMatrixSetupAccountConfig(params: {
|
||||
typeof params.input.allowPrivateNetwork === "boolean"
|
||||
? params.input.allowPrivateNetwork
|
||||
: undefined,
|
||||
proxy: params.input.proxy?.trim() || undefined,
|
||||
userId: password && !userId ? null : userId,
|
||||
accessToken: accessToken || (password ? null : undefined),
|
||||
password: password || (accessToken ? null : undefined),
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user