Skip to content

Commit 489af13

Browse files
fix(android): accept boolean flag aliases in node invoke params (#99873)
* fix(android): accept boolean flag aliases in node invoke params Extend parseJsonBooleanFlag to recognize yes/no/1/0 like TalkDirectiveParser so node handlers honor common string boolean inputs. Co-authored-by: Cursor <[email protected]> * fix(android): route CameraHandler includeAudio through alias parser Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: Cursor <[email protected]>
1 parent 9f8bc7f commit 489af13

3 files changed

Lines changed: 69 additions & 24 deletions

File tree

‎apps/android/app/src/main/java/ai/openclaw/app/node/CameraHandler.kt‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ import kotlinx.coroutines.Dispatchers
88
import kotlinx.coroutines.flow.MutableStateFlow
99
import kotlinx.coroutines.CancellationException
1010
import kotlinx.coroutines.withContext
11-
import kotlinx.serialization.json.Json
1211
import kotlinx.serialization.json.JsonPrimitive
1312
import kotlinx.serialization.json.buildJsonArray
1413
import kotlinx.serialization.json.buildJsonObject
15-
import kotlinx.serialization.json.contentOrNull
1614
import kotlinx.serialization.json.put
1715

1816
internal const val CAMERA_CLIP_MAX_RAW_BYTES: Long = 18L * 1024L * 1024L
@@ -178,23 +176,6 @@ class CameraHandler(
178176
}
179177
}
180178

181-
private fun parseIncludeAudio(paramsJson: String?): Boolean? {
182-
if (paramsJson.isNullOrBlank()) return null
183-
val root =
184-
try {
185-
Json.parseToJsonElement(paramsJson).asObjectOrNull()
186-
} catch (_: Throwable) {
187-
null
188-
} ?: return null
189-
val value =
190-
(root["includeAudio"] as? JsonPrimitive)
191-
?.contentOrNull
192-
?.trim()
193-
?.lowercase()
194-
return when (value) {
195-
"true" -> true
196-
"false" -> false
197-
else -> null
198-
}
199-
}
179+
private fun parseIncludeAudio(paramsJson: String?): Boolean? =
180+
parseJsonBooleanFlag(parseJsonParamsObject(paramsJson), "includeAudio")
200181
}

‎apps/android/app/src/main/java/ai/openclaw/app/node/NodeUtils.kt‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ fun parseJsonString(
6666
key: String,
6767
): String? = readJsonPrimitive(params, key)?.contentOrNull
6868

69-
/** Parses strict true/false flags from string-like JSON primitives. */
69+
/** Parses true/false flags from JSON primitives, including common string aliases. */
7070
fun parseJsonBooleanFlag(
7171
params: JsonObject?,
7272
key: String,
7373
): Boolean? {
7474
val value = readJsonPrimitive(params, key)?.contentOrNull?.trim()?.lowercase() ?: return null
7575
return when (value) {
76-
"true" -> true
77-
"false" -> false
76+
"true", "yes", "1" -> true
77+
"false", "no", "0" -> false
7878
else -> null
7979
}
8080
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ai.openclaw.app.node
2+
3+
import kotlinx.serialization.json.Json
4+
import kotlinx.serialization.json.JsonObject
5+
import kotlinx.serialization.json.buildJsonObject
6+
import kotlinx.serialization.json.put
7+
import org.junit.Assert.assertEquals
8+
import org.junit.Assert.assertNull
9+
import org.junit.Test
10+
11+
class NodeUtilsTest {
12+
private val json = Json { ignoreUnknownKeys = true }
13+
14+
@Test
15+
fun parseJsonBooleanFlag_acceptsCommonStringAliases() {
16+
val cases =
17+
linkedMapOf(
18+
"""{"enabled":"true"}""" to true,
19+
"""{"enabled":"false"}""" to false,
20+
"""{"enabled":"yes"}""" to true,
21+
"""{"enabled":"no"}""" to false,
22+
"""{"enabled":"1"}""" to true,
23+
"""{"enabled":"0"}""" to false,
24+
"""{"enabled":" YES "}""" to true,
25+
)
26+
for ((source, expected) in cases) {
27+
val params = json.parseToJsonElement(source) as JsonObject
28+
assertEquals(source, expected, parseJsonBooleanFlag(params, "enabled"))
29+
}
30+
}
31+
32+
@Test
33+
fun parseJsonBooleanFlag_acceptsJsonBooleanLiterals() {
34+
val params = buildJsonObject {
35+
put("enabled", true)
36+
put("disabled", false)
37+
}
38+
39+
assertEquals(true, parseJsonBooleanFlag(params, "enabled"))
40+
assertEquals(false, parseJsonBooleanFlag(params, "disabled"))
41+
}
42+
43+
@Test
44+
fun parseJsonBooleanFlag_returnsNullForUnknownValues() {
45+
val params = json.parseToJsonElement("""{"enabled":"maybe"}""") as JsonObject
46+
47+
assertNull(parseJsonBooleanFlag(params, "enabled"))
48+
assertNull(parseJsonBooleanFlag(params, "missing"))
49+
}
50+
51+
@Test
52+
fun parseJsonBooleanFlag_parsesIncludeAudioAliasesForCameraClip() {
53+
val cases =
54+
linkedMapOf(
55+
"""{"includeAudio":"no"}""" to false,
56+
"""{"includeAudio":"0"}""" to false,
57+
"""{"includeAudio":"yes"}""" to true,
58+
)
59+
for ((source, expected) in cases) {
60+
val params = json.parseToJsonElement(source) as JsonObject
61+
assertEquals(source, expected, parseJsonBooleanFlag(params, "includeAudio"))
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)