Fix quoted Bash tool titles
This commit is contained in:
1 parent
4c15150338
commit
f9c8f640ce
3 files changed
+67
-52
No files matched your search
@@ -80,7 +80,11 @@ fun parseToolInput(tool: String, input: String): ToolInput {
|
||||
)
|
||||
}
|
||||
val (subjectKey, language) = SUBJECTS[tool] ?: (null to null)
|
||||
val subject = subjectKey?.let { json.optString(it) }?.takeIf { it.isNotBlank() }
|
||||
val subject =
|
||||
subjectKey
|
||||
?.let { json.optString(it) }
|
||||
?.takeIf { it.isNotBlank() }
|
||||
?.let { if (tool == "Bash") renderedBashScript(it) ?: it else it }
|
||||
val description = DESCRIPTIONS.firstNotNullOfOrNull {
|
||||
json.optString(it).takeIf { v -> v.isNotBlank() }
|
||||
}
|
||||
@@ -98,6 +102,28 @@ fun parseToolInput(tool: String, input: String): ToolInput {
|
||||
return ToolInput(subject, language, description, timeout, rest)
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes Codex's rendered Bash argv from old transcript rows.
|
||||
*
|
||||
* New events arrive normalized by the server, but persisted transcripts keep the input originally
|
||||
* written to them. Only the outer pair are presentation quoting: quotes inside the command belong
|
||||
* to the command and must not be parsed as an early end delimiter.
|
||||
*/
|
||||
internal fun renderedBashScript(command: String): String? {
|
||||
val prefix =
|
||||
listOf("/usr/bin/bash -lc ", "/bin/bash -lc ", "bash -lc ").firstOrNull {
|
||||
command.startsWith(it)
|
||||
} ?: return null
|
||||
val quoted = command.removePrefix(prefix)
|
||||
return quoted
|
||||
.takeIf {
|
||||
it.length >= 2 &&
|
||||
((it.startsWith('\'') && it.endsWith('\'')) ||
|
||||
(it.startsWith('"') && it.endsWith('"')))
|
||||
}
|
||||
?.substring(1, quoted.lastIndex)
|
||||
}
|
||||
|
||||
/**
|
||||
* A tool call's input: its subject highlighted, then whatever else it carried.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.example.aiapp
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNull
|
||||
|
||||
class ToolInputTest {
|
||||
@Test
|
||||
fun `bash wrapper ignores double quotes inside its outer pair`() {
|
||||
assertEquals(
|
||||
"rg -n \"needle\" server app",
|
||||
renderedBashScript("/usr/bin/bash -lc \"rg -n \"needle\" server app\""),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `bash wrapper ignores single quotes inside its outer pair`() {
|
||||
assertEquals(
|
||||
"printf 'hello'",
|
||||
renderedBashScript("/bin/bash -lc 'printf 'hello''"),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `unquoted or unfamiliar commands stay intact`() {
|
||||
assertNull(renderedBashScript("/usr/bin/bash -lc echo hello"))
|
||||
assertNull(renderedBashScript("/usr/bin/fish -lc 'echo hello'"))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user