From 0c75df5085d3036b10397f665c1d9068ad75c34e Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Wed, 20 May 2026 01:16:58 -0700 Subject: [PATCH] codex: make desktop launch helpers cwd-aware --- dotfiles/codex/config.toml | 8 ++--- dotfiles/lib/functions/tmcodex | 62 +++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/dotfiles/codex/config.toml b/dotfiles/codex/config.toml index af435db7..6c7a663a 100644 --- a/dotfiles/codex/config.toml +++ b/dotfiles/codex/config.toml @@ -9,12 +9,12 @@ suppress_unstable_features_warning = true # ~/.codex/config.local-state.toml. [mcp_servers.chrome-devtools] -command = "npx" -args = ["-y", "chrome-devtools-mcp@latest", "--auto-connect"] +command = "/usr/bin/env" +args = ["PATH=/etc/profiles/per-user/imalison/bin:/run/current-system/sw/bin:/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin", "npx", "-y", "chrome-devtools-mcp@latest", "--auto-connect"] [mcp_servers.observability] -command = "npx" -args = ["-y", "@google-cloud/observability-mcp"] +command = "/usr/bin/env" +args = ["PATH=/etc/profiles/per-user/imalison/bin:/run/current-system/sw/bin:/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin", "npx", "-y", "@google-cloud/observability-mcp"] [mcp_servers.openaiDeveloperDocs] url = "https://developers.openai.com/mcp" diff --git a/dotfiles/lib/functions/tmcodex b/dotfiles/lib/functions/tmcodex index 1fe9de1b..fd14fa73 100644 --- a/dotfiles/lib/functions/tmcodex +++ b/dotfiles/lib/functions/tmcodex @@ -1,22 +1,76 @@ #!/usr/bin/env sh +function _tmcodex_expand_dir { + case "$1" in + "~") + printf '%s\n' "$HOME" + ;; + "~/"*) + printf '%s\n' "$HOME/${1#"~/"}" + ;; + *) + printf '%s\n' "$1" + ;; + esac +} + +function _tmcodex_resolve_dir { + dir="$(_tmcodex_expand_dir "$1")" + if [ ! -d "$dir" ]; then + echo "tmcodex: directory not found: $1" >&2 + return 1 + fi + + cd -- "$dir" && pwd -P +} + function tmcodex { + launch_dir="$PWD" + + case "${1:-}" in + -C|--cd) + if [ -z "${2:-}" ]; then + echo "tmcodex: $1 requires a directory" >&2 + return 2 + fi + launch_dir="$2" + shift 2 + ;; + --cd=*) + launch_dir="${1#--cd=}" + shift + ;; + *) + expanded_first_arg="$(_tmcodex_expand_dir "${1:-}")" + if [ -n "${1:-}" ] && [ -d "$expanded_first_arg" ]; then + launch_dir="$1" + shift + fi + ;; + esac + + launch_dir="$(_tmcodex_resolve_dir "$launch_dir")" || return + # Record launch directories so rofi_tmcodex can offer good defaults. state_dir="${XDG_STATE_HOME:-$HOME/.local/state}/rofi-tmcodex" history_file="$state_dir/dirs" mkdir -p "$state_dir" 2>/dev/null || true - if [ -d "$PWD" ]; then + if [ -d "$launch_dir" ]; then tmp="$(mktemp 2>/dev/null || true)" if [ -n "$tmp" ]; then - { printf '%s\n' "$PWD"; cat "$history_file" 2>/dev/null || true; } \ + { printf '%s\n' "$launch_dir"; cat "$history_file" 2>/dev/null || true; } \ | awk 'NF && !seen[$0]++' \ | head -n 200 >"$tmp" 2>/dev/null || true mv -f "$tmp" "$history_file" 2>/dev/null || true else - printf '%s\n' "$PWD" >>"$history_file" 2>/dev/null || true + printf '%s\n' "$launch_dir" >>"$history_file" 2>/dev/null || true fi fi - trw codex --dangerously-bypass-approvals-and-sandbox --cd "$PWD" "$@" + + ( + cd -- "$launch_dir" || exit + trw codex --dangerously-bypass-approvals-and-sandbox --cd "$launch_dir" "$@" + ) } tmcodex "$@"