Skip to content

Instantly share code, notes, and snippets.

@agustif
Created April 29, 2025 22:34
Show Gist options
  • Save agustif/ac4a22bda3281501342c34e3a4439baf to your computer and use it in GitHub Desktop.
Save agustif/ac4a22bda3281501342c34e3a4439baf to your computer and use it in GitHub Desktop.
An OpenAI CLI codex quiet mode wrapper that parses the json logs with jq and awk and uses streamdown to parse markdown responses
# Wrapper for codex -q for compact, colored text log with $ prefix
codexq() {
# Check dependencies
if ! command -v jq >/dev/null 2>&1; then
echo "Error: jq is not installed. Please install jq." >&2
return 1
fi
if ! command -v streamdown >/dev/null 2>&1; then
echo "Error: streamdown is not installed. Please install streamdown." >&2
return 1
fi
# Define the jq script for REAL-TIME non-AI messages
local jq_script_non_ai_realtime='
# Wrap in try-catch to ignore non-JSON lines
try (
# ANSI Color Codes
def color($fg; $bg): "\u001b[" + $fg + ";" + $bg + "m";
def reset: "\u001b[0m";
# Format non-AI message (line-by-line)
if .type == "message" and .role == "user" then
(color("30";"46") + " USER " + reset + " " + .content[0].text)
elif .type == "function_call" then
((.arguments | fromjson? | .command // null) as $cmd_array |
if $cmd_array != null then ($cmd_array | join(" ")) else .arguments end) as $cmd_string |
if .name == "shell" then
(color("30";"43") + " TOOL " + reset + " $ " + $cmd_string)
else
(color("30";"43") + " TOOL " + reset + " \(.name // "unknown"): " + $cmd_string)
end
elif .type == "function_call_output" then
(.output | fromjson? | .output // (.output | tostring)) as $output_content |
(color("37";"40") + "OUTPUT" + reset + " " + $output_content)
# Skip AI messages in this filter
else
empty
end
) catch empty
'
# Define the jq script for REAL-TIME AI messages (piped to streamdown)
local jq_script_ai_realtime='
# Wrap in try-catch to ignore non-JSON lines
try (
select(.type == "message" and .role == "assistant") | .content[0].text
) catch empty
'
# awk script to filter consecutive identical tool call lines
local awk_dedupe_tool_calls='
/^{.*"type":"function_call"/ { # If line contains type:function_call JSON
if ($0 == last_line) {
next # Skip if identical to previous line
}
}
{ last_line = $0; print }'
# Run codex, filter tool call duplicates, then tee output to BOTH real-time filters
command codex -q "$@" \
| awk "$awk_dedupe_tool_calls" \
| tee >(jq -r --unbuffered "$jq_script_non_ai_realtime") >(jq -r --unbuffered "$jq_script_ai_realtime" | streamdown) > /dev/null
# Return the exit status (potentially unreliable due to multiple pipe stages)
# Consider checking $? directly after the pipeline if exact codex status is critical
return ${PIPESTATUS[1]:-1} # This likely gets awk's status now
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment