Skip to content

Commit 7efcfc0

Browse files
Harden Termux callback parsing for AI handoff
1 parent b12b5d9 commit 7efcfc0

1 file changed

Lines changed: 37 additions & 15 deletions

File tree

app/src/main/kotlin/com/google/ai/sample/ScreenOperatorAccessibilityService.kt

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -632,31 +632,53 @@ class ScreenOperatorAccessibilityService : AccessibilityService() {
632632
}
633633
val resultBundle = intent.getBundleExtra("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE")
634634
?: intent.getBundleExtra("result")
635-
if (resultBundle == null) {
636-
Log.w(TAG, "Termux result bundle missing; available extras=${intent.extras?.keySet()?.joinToString()}")
637-
unregisterSelf()
638-
return
639-
}
640635

641-
val stdout = resultBundle.getString("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDOUT")
642-
?: resultBundle.getString("stdout")
643-
?: ""
644-
val stderr = resultBundle.getString("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDERR")
645-
?: resultBundle.getString("stderr")
646-
?: ""
636+
val extras = intent.extras
637+
val stdout = sequenceOf(
638+
resultBundle?.getString("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDOUT"),
639+
resultBundle?.getString("stdout"),
640+
extras?.getString("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDOUT"),
641+
extras?.getString("stdout")
642+
).firstOrNull { !it.isNullOrBlank() }.orEmpty()
643+
val stderr = sequenceOf(
644+
resultBundle?.getString("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDERR"),
645+
resultBundle?.getString("stderr"),
646+
extras?.getString("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDERR"),
647+
extras?.getString("stderr")
648+
).firstOrNull { !it.isNullOrBlank() }.orEmpty()
647649
val exitCode = when {
648-
resultBundle.containsKey("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_EXIT_CODE") -> {
650+
resultBundle?.containsKey("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_EXIT_CODE") == true -> {
649651
resultBundle.getInt("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_EXIT_CODE", Int.MIN_VALUE)
650652
}
651-
resultBundle.containsKey("exitCode") -> resultBundle.getInt("exitCode", Int.MIN_VALUE)
653+
resultBundle?.containsKey("exitCode") == true -> resultBundle.getInt("exitCode", Int.MIN_VALUE)
654+
extras?.containsKey("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_EXIT_CODE") == true -> {
655+
extras.getInt("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_EXIT_CODE", Int.MIN_VALUE)
656+
}
657+
extras?.containsKey("exitCode") == true -> extras.getInt("exitCode", Int.MIN_VALUE)
652658
else -> Int.MIN_VALUE
653659
}
654660

655-
Log.i(TAG, "Termux result received: exitCode=$exitCode stdoutLen=${stdout.length} stderrLen=${stderr.length} keys=${resultBundle.keySet().joinToString()}")
661+
val resultKeys = resultBundle?.keySet()?.joinToString().orEmpty()
662+
val extraKeys = extras?.keySet()?.joinToString().orEmpty()
663+
Log.i(TAG, "Termux result received: exitCode=$exitCode stdoutLen=${stdout.length} stderrLen=${stderr.length} bundleKeys=$resultKeys extraKeys=$extraKeys")
656664

657665
val hasKnownResult = stdout.isNotBlank() || stderr.isNotBlank() || exitCode != Int.MIN_VALUE
658666
if (!hasKnownResult) {
659-
Log.w(TAG, "Ignoring Termux callback without stdout/stderr/exitCode to avoid polluting pending output.")
667+
val rawExtrasDump = extras?.keySet()?.joinToString("\n") { key -> "$key=${extras.get(key)}" }.orEmpty().trim()
668+
if (rawExtrasDump.isBlank()) {
669+
Log.w(TAG, "Ignoring Termux callback without stdout/stderr/exitCode and no readable extras.")
670+
unregisterSelf()
671+
return
672+
}
673+
Log.w(TAG, "Termux callback missing standard stdout/stderr/exitCode fields; falling back to raw extras dump for AI handoff.")
674+
TermuxOutputPreferences.appendOutput(appContext, "Termux callback raw extras:\n$rawExtrasDump")
675+
mainHandler.post {
676+
MainActivity.getInstance()?.updateStatusMessage("Termux raw result captured", false)
677+
}
678+
serviceInstance?.handler?.post {
679+
Log.d(TAG, "Termux raw callback captured, scheduling next command processing.")
680+
serviceInstance?.scheduleNextCommandProcessing()
681+
}
660682
unregisterSelf()
661683
return
662684
}

0 commit comments

Comments
 (0)