Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions bin/ask
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ CALLER_ENV_HINTS = {
VALID_CALLERS = set(CALLER_SESSION_FILES.keys()) | {"email", "manual"}


def _caller_pane_info() -> tuple[str, str]:
"""Return (pane_id, terminal_type) from the current environment."""
wez = (os.environ.get("WEZTERM_PANE") or "").strip()
if wez:
return wez, "wezterm"
tmux = (os.environ.get("TMUX_PANE") or "").strip()
if tmux:
return tmux, "tmux"
return "", ""


def _env_int(name: str, default: int) -> int:
raw = (os.environ.get(name) or "").strip()
if not raw:
Expand Down Expand Up @@ -335,6 +346,8 @@ def _send_via_unified_daemon(
print("[ERROR] Invalid askd state", file=sys.stderr)
return EXIT_ERROR

caller_pane_id, caller_terminal = _caller_pane_info()

req = {
"type": "ask.request",
"v": 1,
Expand All @@ -346,6 +359,8 @@ def _send_via_unified_daemon(
"message": message,
"no_wrap": no_wrap,
"caller": caller,
"caller_pane_id": caller_pane_id,
"caller_terminal": caller_terminal,
}

# Pass email-related env vars for email caller
Expand Down Expand Up @@ -670,6 +685,13 @@ def main(argv: list[str]) -> int:
if val:
email_env_lines += f'$env:{key} = "{val}"\n'

win_caller_pane_id, win_caller_terminal = _caller_pane_info()
win_pane_env_lines = ""
if win_caller_pane_id:
win_pane_env_lines += f'$env:CCB_CALLER_PANE_ID = "{win_caller_pane_id}"\n'
if win_caller_terminal:
win_pane_env_lines += f'$env:CCB_CALLER_TERMINAL = "{win_caller_terminal}"\n'

script_content = f'''$ErrorActionPreference = "SilentlyContinue"
$OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
Expand All @@ -678,7 +700,7 @@ $env:PYTHONIOENCODING = "utf-8"
$env:CCB_REQ_ID = "{task_id}"
$env:CCB_CALLER = "{caller}"
$env:CCB_WORK_DIR = "{os.getcwd()}"
{run_dir_line}{email_env_lines}$statusFile = "{status_file_win}"
{run_dir_line}{email_env_lines}{win_pane_env_lines}$statusFile = "{status_file_win}"
$logFile = "{log_file_win}"
function Write-CcbStatus([string]$line) {{
Add-Content -Path $statusFile -Value ("{{0}} {{1}}" -f (Get-Date -Format "yyyy-MM-ddTHH:mm:sszzz"), $line) -Encoding UTF8
Expand Down Expand Up @@ -717,6 +739,13 @@ exit $rc
ccb_run_dir = os.environ.get("CCB_RUN_DIR", "")
run_dir_line = f'export CCB_RUN_DIR="{ccb_run_dir}"\n' if ccb_run_dir else ""

bg_pane_id, bg_terminal = _caller_pane_info()
pane_env_lines = ""
if bg_pane_id:
pane_env_lines += f'export CCB_CALLER_PANE_ID="{bg_pane_id}"\n'
if bg_terminal:
pane_env_lines += f'export CCB_CALLER_TERMINAL="{bg_terminal}"\n'

quoted_status = shlex.quote(str(status_file))
quoted_ask_cmd = shlex.quote(ask_cmd)
quoted_provider = shlex.quote(provider)
Expand All @@ -730,7 +759,7 @@ echo "[CCB_TASK_START] task={task_id} provider={provider} caller={caller} pid=$$
export CCB_REQ_ID="{task_id}"
export CCB_CALLER="{caller}"
export CCB_WORK_DIR="{os.getcwd()}"
{run_dir_line}{email_env_lines}python3 {quoted_ask_cmd} {quoted_provider} --foreground --timeout {timeout} <<'ASKEOF'
{run_dir_line}{email_env_lines}{pane_env_lines}python3 {quoted_ask_cmd} {quoted_provider} --foreground --timeout {timeout} <<'ASKEOF'
{message}
ASKEOF
rc=$?
Expand Down
Loading