@humanu/orchestra 0.5.76 → 0.5.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/bin/{gw.js → orchestra-cli.js} +1 -1
  2. package/bin/orchestra.js +2 -2
  3. package/install.js +41 -42
  4. package/package.json +2 -3
  5. package/resources/api/git.sh +4 -444
  6. package/resources/api/tmux.sh +4 -2791
  7. package/resources/prebuilt/linux-x64/{gw-env-copy → env-copy} +0 -0
  8. package/resources/prebuilt/linux-x64/orchestra +0 -0
  9. package/resources/prebuilt/macos-arm64/{gw-env-copy → env-copy} +0 -0
  10. package/resources/prebuilt/macos-arm64/orchestra +0 -0
  11. package/resources/prebuilt/macos-intel/{gw-env-copy → env-copy} +0 -0
  12. package/resources/prebuilt/macos-intel/orchestra +0 -0
  13. package/resources/scripts/{gw.sh → orchestra-cli.sh} +14 -14
  14. package/resources/scripts/orchestra-local.sh +6 -6
  15. package/resources/scripts/{gwr.sh → orchestra.sh} +11 -55
  16. package/resources/scripts/{shell/bridge → server/services}/ai.sh +4 -4
  17. package/resources/scripts/{gw-bridge.sh → server/services/dispatch.sh} +62 -59
  18. package/resources/scripts/server/services/git/api.sh +447 -0
  19. package/resources/scripts/{shell/git/bridge_check_branch.sh → server/services/git/check_branch_api.sh} +1 -1
  20. package/resources/scripts/{shell/git/bridge_create_worktree.sh → server/services/git/create_worktree_api.sh} +3 -3
  21. package/resources/scripts/{shell/git/bridge_create_worktree_from_existing.sh → server/services/git/create_worktree_from_existing_api.sh} +2 -2
  22. package/resources/scripts/{shell/git/bridge_create_worktree_from_remote.sh → server/services/git/create_worktree_from_remote_api.sh} +2 -2
  23. package/resources/scripts/{shell/git/bridge_delete_branch_only.sh → server/services/git/delete_branch_only_api.sh} +1 -1
  24. package/resources/scripts/{shell/git/bridge_delete_worktree.sh → server/services/git/delete_worktree_api.sh} +2 -2
  25. package/resources/scripts/{shell/git/bridge_delete_worktree_only.sh → server/services/git/delete_worktree_only_api.sh} +1 -1
  26. package/resources/scripts/{shell/git/bridge_enhanced_git_status.sh → server/services/git/enhanced_git_status_api.sh} +3 -3
  27. package/resources/scripts/{shell/git/bridge_git_status.sh → server/services/git/git_status_api.sh} +2 -2
  28. package/resources/scripts/{shell/git/bridge_list_worktrees.sh → server/services/git/list_worktrees_api.sh} +2 -2
  29. package/resources/scripts/server/services/git/merge_api.sh +12 -0
  30. package/resources/scripts/{shell/git/bridge_merge_from_primary.sh → server/services/git/merge_from_primary_api.sh} +1 -1
  31. package/resources/scripts/{shell/git/bridge_merge_into_primary.sh → server/services/git/merge_into_primary_api.sh} +1 -1
  32. package/resources/scripts/{shell/git/bridge_primary_branch.sh → server/services/git/primary_branch_api.sh} +1 -1
  33. package/resources/scripts/{shell/git/bridge_rebase_from_primary.sh → server/services/git/rebase_from_primary_api.sh} +1 -1
  34. package/resources/scripts/server/services/git/repo_api.sh +12 -0
  35. package/resources/scripts/{shell/git/bridge_repo_info.sh → server/services/git/repo_info_api.sh} +1 -1
  36. package/resources/scripts/{shell/git/bridge_squash_into_primary.sh → server/services/git/squash_into_primary_api.sh} +1 -1
  37. package/resources/scripts/{shell/git/bridge_switch_worktree.sh → server/services/git/switch_worktree_api.sh} +2 -2
  38. package/resources/scripts/server/services/git/worktree_api.sh +17 -0
  39. package/resources/scripts/{shell/bridge/utils.sh → server/services/json.sh} +23 -23
  40. package/resources/scripts/{shell/bridge → server/services/session}/tmux.sh +14 -14
  41. package/resources/scripts/server/session/tmux_api.sh +2806 -0
  42. package/resources/scripts/services.sh +6 -0
  43. package/resources/scripts/shell/AGENTS.md +63 -74
  44. package/resources/scripts/shell/build/dependencies.sh +33 -0
  45. package/resources/scripts/shell/build/install.sh +7 -0
  46. package/resources/scripts/shell/build/load.sh +10 -0
  47. package/resources/scripts/shell/build/logging.sh +6 -0
  48. package/resources/scripts/shell/build/rust.sh +18 -0
  49. package/resources/scripts/shell/build/services.sh +17 -0
  50. package/resources/scripts/shell/build/{build_usage.sh → usage.sh} +6 -6
  51. package/resources/scripts/shell/cli_load.sh +9 -0
  52. package/resources/scripts/shell/{gw_env_copy.sh → env_copy.sh} +11 -11
  53. package/resources/scripts/shell/env_copy_command.sh +2 -2
  54. package/resources/scripts/shell/git/checkout_worktree.sh +4 -4
  55. package/resources/scripts/shell/git/create_worktree.sh +2 -2
  56. package/resources/scripts/shell/git/delete_worktree.sh +1 -1
  57. package/resources/scripts/shell/git/merge.sh +1 -1
  58. package/resources/scripts/shell/git/repo.sh +1 -1
  59. package/resources/scripts/shell/git/worktree.sh +1 -1
  60. package/resources/scripts/shell/gwr/check-updates.sh +1 -1
  61. package/resources/scripts/shell/gwr_binary.sh +4 -4
  62. package/resources/scripts/shell/gwr_load.sh +1 -1
  63. package/resources/scripts/shell/gwr_services.sh +10 -0
  64. package/resources/scripts/shell/gwr_usage.sh +10 -10
  65. package/resources/scripts/shell/orchestra-command-hook.sh +15 -15
  66. package/resources/scripts/shell/orchestra-local.sh +6 -6
  67. package/resources/scripts/shell/tmux/new_session_command.sh +1 -1
  68. package/bin/gwr.js +0 -10
  69. package/resources/scripts/shell/build/build_bridge.sh +0 -17
  70. package/resources/scripts/shell/build/build_dependencies.sh +0 -33
  71. package/resources/scripts/shell/build/build_install.sh +0 -7
  72. package/resources/scripts/shell/build/build_load.sh +0 -10
  73. package/resources/scripts/shell/build/build_logging.sh +0 -6
  74. package/resources/scripts/shell/build/build_rust.sh +0 -18
  75. package/resources/scripts/shell/git/bridge_merge.sh +0 -12
  76. package/resources/scripts/shell/git/bridge_repo.sh +0 -12
  77. package/resources/scripts/shell/git/bridge_worktree.sh +0 -17
  78. package/resources/scripts/shell/gw_legacy_wrappers.sh +0 -7
  79. package/resources/scripts/shell/gw_load.sh +0 -10
  80. package/resources/scripts/shell/gwr_bridge.sh +0 -10
  81. /package/resources/scripts/shell/{gw_debug.sh → cli_debug.sh} +0 -0
  82. /package/resources/scripts/shell/{gw_err.sh → cli_err.sh} +0 -0
  83. /package/resources/scripts/shell/{gw_have_cmd.sh → cli_have_cmd.sh} +0 -0
  84. /package/resources/scripts/shell/{gw_info.sh → cli_info.sh} +0 -0
@@ -1,2794 +1,7 @@
1
1
  #!/bin/bash
2
2
 
3
- ###############################################################################
4
- # tmux.sh – Dedicated tmux session management API
5
- # ---------------------------------------------------------------------------
6
- # This script provides a clean API for all tmux session operations.
7
- # It can be sourced by other modules that need tmux functionality.
8
- ###############################################################################
3
+ # shellcheck shell=bash
9
4
 
10
- # Ensure we have access to core utilities
11
- if ! declare -f repo_root >/dev/null 2>&1 && ! declare -f git_repo_root >/dev/null 2>&1; then
12
- echo "Error: tmux.sh must be sourced from gw.sh or have core utilities available" >&2
13
- return 1 2>/dev/null || exit 1
14
- fi
15
-
16
- # Use git_repo_root if available, otherwise fallback to repo_root
17
- if declare -f git_repo_root >/dev/null 2>&1; then
18
- repo_root() { git_repo_root; }
19
- fi
20
-
21
- # --------------------------- Tmux Core API ----------------------------------
22
-
23
- # Session delimiter (hardcoded)
24
- # Note: tmux session names cannot contain ':'; use a safe delimiter
25
- ORCHESTRA_SESSION_DELIM="__"
26
-
27
- # Absolute directory that contains this script when sourced.
28
- _TMUX_API_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
29
-
30
- # Helper: return delimiter
31
- _tmux_delim() { echo "$ORCHESTRA_SESSION_DELIM"; }
32
-
33
- # Helper: sanitize tmux session name for filesystem usage
34
- _orchestra_history_key() {
35
- local key="$1"
36
- key="${key//\//_}"
37
- key=$(echo "$key" | tr '[:space:]' '_')
38
- key=$(echo "$key" | tr -c '[:alnum:]_-' '_')
39
- while [[ "$key" == *"__"* ]]; do
40
- key="${key//__/_}"
41
- done
42
- key="${key##_}"
43
- key="${key%%_}"
44
- echo "$key"
45
- }
46
-
47
- _tmux_normalize_app_from_command() {
48
- local cmd="$1"
49
- cmd="${cmd#"${cmd%%[![:space:]]*}"}"
50
- cmd="${cmd%"${cmd##*[![:space:]]}"}"
51
- if [[ -z "$cmd" ]]; then
52
- return
53
- fi
54
-
55
- local tokens=()
56
- read -r -a tokens <<< "$cmd"
57
- if [[ ${#tokens[@]} -eq 0 ]]; then
58
- return
59
- fi
60
-
61
- local i=0
62
- while [[ $i -lt ${#tokens[@]} ]]; do
63
- local t="${tokens[$i]}"
64
- if [[ "$t" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then
65
- ((i++))
66
- continue
67
- fi
68
- case "$t" in
69
- sudo|env|command|nohup|time)
70
- ((i++))
71
- continue
72
- ;;
73
- esac
74
- break
75
- done
76
-
77
- if [[ $i -ge ${#tokens[@]} ]]; then
78
- return
79
- fi
80
-
81
- local base="${tokens[$i]}"
82
- base="${base##*/}"
83
- base="$(printf '%s' "$base" | tr '[:upper:]' '[:lower:]' | tr -c 'a-z0-9._:-' '_')"
84
- base="${base//__/_}"
85
- base="${base##_}"
86
- base="${base%%_}"
87
-
88
- case "$base" in
89
- python3|python3.*)
90
- base="python"
91
- ;;
92
- pip3|pip3.*)
93
- base="pip"
94
- ;;
95
- docker-compose|docker_compose)
96
- base="docker_compose"
97
- ;;
98
- nvim|nvim.app)
99
- base="nvim"
100
- ;;
101
- esac
102
-
103
- case "$base" in
104
- ""|bash|zsh|sh|fish|tmux|login|sudo|man|less|more|cat|tail|watch|source|export|set|alias|unalias|history|bindkey)
105
- return
106
- ;;
107
- esac
108
-
109
- printf '%s' "$base"
110
- }
111
-
112
- _tmux_truthy() {
113
- case "$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')" in
114
- 1|yes|true|on)
115
- return 0
116
- ;;
117
- esac
118
- return 1
119
- }
120
-
121
- _tmux_is_known_tui_app() {
122
- local app
123
- app="$(_tmux_normalize_app_from_command "$1")"
124
- case "$app" in
125
- opencode|claude|vim|nvim|vi|lazygit|gitui|tig|top|htop|btop|k9s|fzf|yazi|ranger|nnn|less|man)
126
- return 0
127
- ;;
128
- esac
129
- return 1
130
- }
131
-
132
- _tmux_is_tui_context() {
133
- local pane_cmd="$1"
134
- local alternate_on="$2"
135
- local mouse_any_flag="$3"
136
- local pane_mode="$4"
137
- local window_name="$5"
138
- local pane_title="$6"
139
-
140
- if _tmux_truthy "$alternate_on" || _tmux_truthy "$mouse_any_flag"; then
141
- return 0
142
- fi
143
-
144
- if [[ -n "$pane_mode" ]]; then
145
- return 0
146
- fi
147
-
148
- if _tmux_is_known_tui_app "$pane_cmd"; then
149
- return 0
150
- fi
151
-
152
- local combined
153
- combined="$(printf '%s %s' "$window_name" "$pane_title" | tr '[:upper:]' '[:lower:]')"
154
- case "$combined" in
155
- *opencode*|*claude*|*vim*|*nvim*|*lazygit*|*gitui*|*tig*|*k9s*|*fzf*|*yazi*|*ranger*|*nnn*)
156
- return 0
157
- ;;
158
- esac
159
-
160
- return 1
161
- }
162
-
163
- # Helper: absolute path to the command hook script (if present)
164
- _orchestra_command_hook() {
165
- local hook=""
166
-
167
- # 1) Explicit install root set by wrappers (Homebrew/NPM)
168
- if [[ -n "${GW_ORCHESTRATOR_ROOT-}" ]]; then
169
- hook="$GW_ORCHESTRATOR_ROOT/shell/orchestra-command-hook.sh"
170
- if [[ -f "$hook" ]]; then
171
- echo "$hook"
172
- return
173
- fi
174
- fi
175
-
176
- # 2) Resolve relative to this script's install location
177
- local script_root
178
- script_root="$(cd "$_TMUX_API_DIR/.." && pwd -P)"
179
- hook="$script_root/shell/orchestra-command-hook.sh"
180
- if [[ -f "$hook" ]]; then
181
- echo "$hook"
182
- return
183
- fi
184
-
185
- # 3) Fallback to repo root for legacy/dev flows
186
- local root
187
- root="$(repo_root)"
188
- if [[ -n "$root" ]]; then
189
- hook="$root/shell/orchestra-command-hook.sh"
190
- if [[ -f "$hook" ]]; then
191
- echo "$hook"
192
- return
193
- fi
194
- fi
195
-
196
- echo ""
197
- }
198
-
199
- _orchestra_bridge_script() {
200
- echo "$(dirname "$_TMUX_API_DIR")/gw-bridge.sh"
201
- }
202
-
203
- # Source the command hook inside a tmux session to enable command history logging
204
- _tmux_source_command_hook() {
205
- local session="$1"
206
- local hook
207
- hook="$(_orchestra_command_hook)"
208
- if [[ -z "$hook" ]]; then
209
- return
210
- fi
211
- if ! tmux_session_exists "$session"; then
212
- return
213
- fi
214
- local bridge
215
- bridge="$(_orchestra_bridge_script)"
216
- local panes
217
- panes=$(tmux list-panes -t "$session" -F '#{pane_id} #{pane_current_command}' 2>/dev/null || echo "")
218
- if [[ -z "$panes" ]]; then
219
- return
220
- fi
221
- while IFS= read -r line; do
222
- [[ -z "$line" ]] && continue
223
- local pane_id pane_cmd
224
- pane_id="${line%% *}"
225
- pane_cmd="${line#* }"
226
- case "$pane_cmd" in
227
- bash|zsh)
228
- tmux send-keys -t "$pane_id" "export ORCHESTRA_BRIDGE_PATH='$bridge'; . '$hook'" C-m 2>/dev/null || true
229
- ;;
230
- *)
231
- ;;
232
- esac
233
- done <<< "$panes"
234
- }
235
-
236
- # Helper: orchestra prefix including delimiter
237
- _tmux_orch_prefix() { printf "orchestra%s" "$( _tmux_delim )"; }
238
-
239
- _tmux_session_registry_path() {
240
- if [[ -n "${ORCHESTRA_SESSION_REGISTRY_PATH-}" ]]; then
241
- printf '%s\n' "$ORCHESTRA_SESSION_REGISTRY_PATH"
242
- return
243
- fi
244
- printf '%s\n' "$HOME/.orchestra/orchestra.sqlite3"
245
- }
246
-
247
- _tmux_shared_root_for_path() {
248
- local path="$1"
249
- local old_pwd="$PWD"
250
- local root=""
251
- if [[ -n "$path" && -d "$path" ]]; then
252
- cd "$path" 2>/dev/null || return 1
253
- root="$(git_shared_root 2>/dev/null || git_repo_root 2>/dev/null || true)"
254
- cd "$old_pwd" 2>/dev/null || true
255
- fi
256
- [[ -n "$root" ]] || return 1
257
- printf '%s\n' "$root"
258
- }
259
-
260
- _tmux_branch_for_path() {
261
- local path="$1"
262
- local old_pwd="$PWD"
263
- local branch=""
264
- if [[ -n "$path" && -d "$path" ]]; then
265
- cd "$path" 2>/dev/null || return 1
266
- branch="$(git_current_branch 2>/dev/null || true)"
267
- cd "$old_pwd" 2>/dev/null || true
268
- fi
269
- [[ -n "$branch" ]] || return 1
270
- printf '%s\n' "$branch"
271
- }
272
-
273
- _tmux_registry_upsert_session() {
274
- local repo_root="$1"
275
- local branch="$2"
276
- local worktree_path="$3"
277
- local tmux_name="$4"
278
- local db_path
279
- db_path="$(_tmux_session_registry_path)"
280
-
281
- [[ -n "$repo_root" && -n "$branch" && -n "$worktree_path" && -n "$tmux_name" ]] || return 1
282
- have_cmd python3 || return 1
283
-
284
- python3 - "$db_path" "$repo_root" "$branch" "$worktree_path" "$tmux_name" <<'PY'
285
- import os
286
- import sqlite3
287
- import sys
288
- import time
289
-
290
- db_path, repo_root, branch, worktree_path, tmux_name = sys.argv[1:6]
291
- os.makedirs(os.path.dirname(db_path), exist_ok=True)
292
- slug = branch.replace("/", "-")
293
- now = int(time.time())
294
-
295
- conn = sqlite3.connect(db_path)
296
- conn.execute(
297
- """
298
- CREATE TABLE IF NOT EXISTS sessions (
299
- id INTEGER PRIMARY KEY AUTOINCREMENT,
300
- tmux_name TEXT NOT NULL UNIQUE,
301
- repo_root TEXT NOT NULL,
302
- worktree_path TEXT NOT NULL,
303
- branch TEXT NOT NULL,
304
- slug TEXT NOT NULL,
305
- display_name TEXT,
306
- created_at INTEGER NOT NULL,
307
- updated_at INTEGER NOT NULL,
308
- last_seen_at INTEGER NOT NULL
309
- )
310
- """
311
- )
312
- conn.execute(
313
- "CREATE INDEX IF NOT EXISTS idx_sessions_worktree ON sessions(repo_root, worktree_path)"
314
- )
315
- conn.execute("CREATE INDEX IF NOT EXISTS idx_sessions_slug ON sessions(repo_root, slug)")
316
- conn.execute(
317
- """
318
- INSERT INTO sessions (
319
- tmux_name, repo_root, worktree_path, branch, slug,
320
- created_at, updated_at, last_seen_at
321
- ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?6, ?6)
322
- ON CONFLICT(tmux_name) DO UPDATE SET
323
- repo_root = excluded.repo_root,
324
- worktree_path = excluded.worktree_path,
325
- branch = excluded.branch,
326
- slug = excluded.slug,
327
- updated_at = excluded.updated_at,
328
- last_seen_at = excluded.last_seen_at
329
- """,
330
- (tmux_name, repo_root, worktree_path, branch, slug, now),
331
- )
332
- conn.commit()
333
- PY
334
- }
335
-
336
- _tmux_registry_rename_session() {
337
- local old_tmux_name="$1"
338
- local new_tmux_name="$2"
339
- local display_name="${3:-}"
340
- local db_path
341
- db_path="$(_tmux_session_registry_path)"
342
-
343
- [[ -f "$db_path" ]] || return 0
344
- have_cmd python3 || return 1
345
-
346
- python3 - "$db_path" "$old_tmux_name" "$new_tmux_name" "$display_name" <<'PY'
347
- import sqlite3
348
- import sys
349
- import time
350
-
351
- db_path, old_tmux_name, new_tmux_name, display_name = sys.argv[1:5]
352
- now = int(time.time())
353
- value = display_name if display_name else None
354
-
355
- conn = sqlite3.connect(db_path)
356
- conn.execute(
357
- """
358
- UPDATE sessions
359
- SET tmux_name = ?1,
360
- display_name = COALESCE(?2, display_name),
361
- updated_at = ?3,
362
- last_seen_at = ?3
363
- WHERE tmux_name = ?4
364
- """,
365
- (new_tmux_name, value, now, old_tmux_name),
366
- )
367
- conn.commit()
368
- PY
369
- }
370
-
371
- _tmux_registry_remove_session() {
372
- local tmux_name="$1"
373
- local db_path
374
- db_path="$(_tmux_session_registry_path)"
375
-
376
- [[ -f "$db_path" ]] || return 0
377
- have_cmd python3 || return 1
378
-
379
- python3 - "$db_path" "$tmux_name" <<'PY'
380
- import sqlite3
381
- import sys
382
-
383
- db_path, tmux_name = sys.argv[1:3]
384
- conn = sqlite3.connect(db_path)
385
- conn.execute("DELETE FROM sessions WHERE tmux_name = ?1", (tmux_name,))
386
- conn.commit()
387
- PY
388
- }
389
-
390
- _tmux_registry_upsert_current_session() {
391
- local session_name="$1"
392
- local session_dir repo_root branch
393
- session_dir="$(tmux display-message -t "$session_name" -p '#{pane_current_path}' 2>/dev/null || echo "")"
394
- repo_root="$(_tmux_shared_root_for_path "$session_dir" 2>/dev/null || true)"
395
- branch="$(_tmux_branch_for_path "$session_dir" 2>/dev/null || true)"
396
- [[ -n "$repo_root" && -n "$branch" ]] || return 1
397
- _tmux_registry_upsert_session "$repo_root" "$branch" "$session_dir" "$session_name"
398
- }
399
-
400
- _tmux_registry_sync_active_workspace_sessions() {
401
- local repo_root="$1"
402
- [[ -n "$repo_root" ]] || return 1
403
- tmux_available || return 1
404
-
405
- local session_name session_dir session_root branch display_name
406
- while IFS= read -r session_name; do
407
- [[ -n "$session_name" ]] || continue
408
- display_name="$(tmux show-option -t "$session_name" -qv @orchestra_display_name 2>/dev/null || true)"
409
- [[ -n "$display_name" ]] || continue
410
- session_dir="$(tmux display-message -t "$session_name" -p '#{pane_current_path}' 2>/dev/null || echo "")"
411
- session_root="$(_tmux_shared_root_for_path "$session_dir" 2>/dev/null || true)"
412
- [[ "$session_root" == "$repo_root" ]] || continue
413
- branch="$(_tmux_branch_for_path "$session_dir" 2>/dev/null || true)"
414
- [[ -n "$branch" ]] || continue
415
- _tmux_registry_upsert_session "$repo_root" "$branch" "$session_dir" "$session_name" >/dev/null 2>&1 || true
416
- done < <(tmux list-sessions -F '#{session_name}' 2>/dev/null || true)
417
- }
418
-
419
- _tmux_workspace_cycle_target_cached() {
420
- local current_session="$1"
421
- local direction="$2"
422
- local db_path="$3"
423
- local active_file="$4"
424
-
425
- python3 - "$db_path" "$current_session" "$direction" "$active_file" <<'PY'
426
- import sqlite3
427
- import sys
428
-
429
- db_path, current_session, direction, active_path = sys.argv[1:5]
430
- active = set()
431
- active_orchestra = set()
432
- with open(active_path, "r", encoding="utf-8") as handle:
433
- for raw_line in handle:
434
- line = raw_line.rstrip("\n")
435
- if not line:
436
- continue
437
- parts = line.split("\t")
438
- name = parts[0].strip()
439
- display_name = parts[1].strip() if len(parts) > 1 else ""
440
- if not name:
441
- continue
442
- active.add(name)
443
- if display_name:
444
- active_orchestra.add(name)
445
-
446
- try:
447
- conn = sqlite3.connect(db_path)
448
- current_row = conn.execute(
449
- "SELECT repo_root FROM sessions WHERE tmux_name = ?1",
450
- (current_session,),
451
- ).fetchone()
452
- if current_row is None:
453
- raise SystemExit(2)
454
-
455
- registered = {
456
- name
457
- for (name,) in conn.execute("SELECT tmux_name FROM sessions").fetchall()
458
- }
459
- if active_orchestra.difference(registered):
460
- raise SystemExit(2)
461
-
462
- rows = conn.execute(
463
- """
464
- SELECT tmux_name
465
- FROM sessions
466
- WHERE repo_root = ?1
467
- ORDER BY worktree_path COLLATE NOCASE, created_at, tmux_name COLLATE NOCASE
468
- """,
469
- (current_row[0],),
470
- ).fetchall()
471
- except sqlite3.Error:
472
- raise SystemExit(2)
473
-
474
- names = []
475
- seen = set()
476
- for (name,) in rows:
477
- if name in active and name not in seen:
478
- names.append(name)
479
- seen.add(name)
480
-
481
- if len(names) < 2 or current_session not in seen:
482
- raise SystemExit(3)
483
-
484
- index = names.index(current_session)
485
- if direction == "next":
486
- index = (index + 1) % len(names)
487
- else:
488
- index = (index - 1) % len(names)
489
- print(names[index])
490
- PY
491
- }
492
-
493
- _tmux_status_escape_text() {
494
- local text="$1"
495
- text="${text//\#/##}"
496
- printf '%s' "$text"
497
- }
498
-
499
- _tmux_truncate_tab_label() {
500
- local label="$1"
501
- local max_len="${2:-14}"
502
- if (( ${#label} > max_len )); then
503
- printf '%s' "${label:0:max_len}"
504
- else
505
- printf '%s' "$label"
506
- fi
507
- }
508
-
509
- _tmux_pad_menu_label() {
510
- local label="$1"
511
- local width="${2:-56}"
512
-
513
- label="$(_tmux_truncate_tab_label "$label" "$width")"
514
- printf '%-*s' "$width" "$label"
515
- }
516
-
517
- _tmux_workspace_session_rows() {
518
- local current_session="$1"
519
- local repo_root="$2"
520
- local mode="${3:-visible}"
521
- local db_path active_file
522
- db_path="$(_tmux_session_registry_path)"
523
-
524
- [[ -f "$db_path" ]] || return 0
525
- have_cmd python3 || return 0
526
-
527
- active_file="$(mktemp)"
528
- tmux list-sessions -F '#{session_name}' > "$active_file" 2>/dev/null || {
529
- rm -f "$active_file"
530
- return 0
531
- }
532
-
533
- python3 - "$db_path" "$repo_root" "$current_session" "$active_file" "$mode" <<'PY'
534
- import sqlite3
535
- import sys
536
-
537
- db_path, repo_root, current_session, active_path, mode = sys.argv[1:6]
538
- with open(active_path, "r", encoding="utf-8") as handle:
539
- active = {line.strip() for line in handle if line.strip()}
540
-
541
- conn = sqlite3.connect(db_path)
542
- rows = conn.execute(
543
- """
544
- SELECT tmux_name, COALESCE(display_name, '')
545
- FROM sessions
546
- WHERE repo_root = ?1
547
- ORDER BY worktree_path COLLATE NOCASE, created_at, tmux_name COLLATE NOCASE
548
- """,
549
- (repo_root,),
550
- ).fetchall()
551
-
552
- filtered = []
553
- seen = set()
554
- for name, display_name in rows:
555
- if name in active and name not in seen:
556
- filtered.append((name, display_name or ""))
557
- seen.add(name)
558
-
559
- if not filtered:
560
- raise SystemExit(0)
561
-
562
- max_tabs = 8
563
- if mode == "all" or len(filtered) <= max_tabs:
564
- visible = filtered
565
- prefix = suffix = False
566
- else:
567
- try:
568
- current_index = [name for name, _ in filtered].index(current_session)
569
- except ValueError:
570
- current_index = 0
571
- start = max(0, min(current_index - 2, len(filtered) - max_tabs))
572
- end = min(len(filtered), start + max_tabs)
573
- visible = filtered[start:end]
574
- prefix = start > 0
575
- suffix = end < len(filtered)
576
-
577
- if prefix:
578
- print("__ellipsis__\t")
579
- for name, display_name in visible:
580
- print(f"{name}\t{display_name}")
581
- if suffix:
582
- print("__ellipsis__\t")
583
- PY
584
- local status=$?
585
- rm -f "$active_file"
586
- return $status
587
- }
588
-
589
- _tmux_workspace_session_tabs() {
590
- local current_session="$1"
591
- local fallback_display_name="$2"
592
- local session_dir repo_root rows tabs name display_name tab_label escaped_tab_label divider active_style inactive_style muted_style reset_style
593
- local tab_max_width=24
594
- local active_label_max_width=22
595
-
596
- session_dir="$(tmux display-message -t "$current_session" -p '#{pane_current_path}' 2>/dev/null || echo "")"
597
- repo_root="$(_tmux_shared_root_for_path "$session_dir" 2>/dev/null || true)"
598
- if [[ -n "$repo_root" ]]; then
599
- _tmux_registry_sync_active_workspace_sessions "$repo_root" >/dev/null 2>&1 || true
600
- rows="$(_tmux_workspace_session_rows "$current_session" "$repo_root" 2>/dev/null || true)"
601
- else
602
- rows=""
603
- fi
604
-
605
- if [[ -z "$rows" ]]; then
606
- tab_label="$(_tmux_truncate_tab_label "$fallback_display_name" "$active_label_max_width")"
607
- escaped_tab_label="$(_tmux_status_escape_text "$tab_label")"
608
- printf '#[bg=#414868] #[fg=#ff9e64,bg=#414868,bold]● #[fg=#c0caf5,bg=#414868,bold]%s #[default]' "$escaped_tab_label"
609
- return
610
- fi
611
-
612
- # Tokyo Night palette: midnight footer, muted inactive tabs, orange active marker.
613
- active_style="#[fg=#c0caf5,bg=#414868,bold]"
614
- inactive_style="#[fg=#a9b1d6,bg=#24283b,nobold]"
615
- muted_style="#[fg=#565f89,bg=#1a1b26,nobold]"
616
- reset_style="#[default]"
617
- tabs=""
618
- divider="${muted_style}|${reset_style}"
619
- while IFS=$'\t' read -r name display_name; do
620
- [[ -n "$name" ]] || continue
621
- if [[ -n "$tabs" ]]; then
622
- tabs+="$divider"
623
- fi
624
- if [[ "$name" == "__ellipsis__" ]]; then
625
- tab_label="⋯"
626
- escaped_tab_label="$(_tmux_status_escape_text "$tab_label")"
627
- tabs+="${muted_style} ${escaped_tab_label} ${reset_style}"
628
- continue
629
- fi
630
-
631
- if [[ -z "$display_name" ]]; then
632
- display_name="$(tmux_format_session_display "$name" without-timestamp)"
633
- fi
634
- if [[ "$name" == "$current_session" ]]; then
635
- tab_label="$(_tmux_truncate_tab_label "$display_name" "$active_label_max_width")"
636
- escaped_tab_label="$(_tmux_status_escape_text "$tab_label")"
637
- tabs+="#[bg=#414868] #[fg=#ff9e64,bg=#414868,bold]● ${active_style}${escaped_tab_label} ${reset_style}"
638
- else
639
- tab_label="$(_tmux_truncate_tab_label "$display_name" "$tab_max_width")"
640
- escaped_tab_label="$(_tmux_status_escape_text "$tab_label")"
641
- tabs+="${inactive_style} ${escaped_tab_label} ${reset_style}"
642
- fi
643
- done <<< "$rows"
644
-
645
- printf '%s' "$tabs"
646
- }
647
-
648
- _tmux_orchestra_status_left() {
649
- local session_name="$1"
650
- local session_display_name="$3"
651
- _tmux_workspace_session_tabs "$session_name" "$session_display_name"
652
- }
653
-
654
- _tmux_orchestra_status_right() {
655
- printf '#[fg=#565f89,bg=#1a1b26]Ctrl+b,h for help#[default]'
656
- }
657
-
658
- _tmux_configure_orchestra_bindings() {
659
- local bridge
660
- bridge="$(_orchestra_bridge_script)"
661
- [[ -f "$bridge" ]] || return
662
-
663
- local quoted_bridge rename_command prompt_command next_command previous_command new_session_command list_command close_command close_prompt_command help_command
664
- printf -v quoted_bridge '%q' "$bridge"
665
- rename_command="$quoted_bridge manual-rename-session \\\"#{session_name}\\\" \\\"%%\\\" >/dev/null 2>&1"
666
- prompt_command="command-prompt -p 'Rename Orchestra session:' 'run-shell -b \"$rename_command\"'"
667
- next_command="$quoted_bridge cycle-workspace-session \\\"#{session_name}\\\" next \\\"#{client_tty}\\\" >/dev/null 2>&1"
668
- previous_command="$quoted_bridge cycle-workspace-session \\\"#{session_name}\\\" previous \\\"#{client_tty}\\\" >/dev/null 2>&1"
669
- new_session_command="$quoted_bridge create-workspace-session \\\"#{session_name}\\\" \\\"#{client_tty}\\\" >/dev/null 2>&1 || true"
670
- list_command="$quoted_bridge workspace-session-menu \\\"#{session_name}\\\" \\\"#{client_tty}\\\" >/dev/null 2>&1 || true"
671
- close_command="$quoted_bridge close-workspace-session \\\"#{session_name}\\\" \\\"#{client_tty}\\\" >/dev/null 2>&1 || true"
672
- close_prompt_command="confirm-before -p 'Close current Orchestra session? (y/n)' 'run-shell -b \"$close_command\"'"
673
- help_command="$quoted_bridge tmux-help-popup \\\"#{client_tty}\\\" >/dev/null 2>&1 || true"
674
-
675
- tmux bind-key -T prefix '?' if-shell -F '#{@orchestra_display_name}' \
676
- "run-shell -b \"$help_command\"" 'list-keys -N' >/dev/null 2>&1 || true
677
- tmux bind-key -T prefix h if-shell -F '#{@orchestra_display_name}' \
678
- "run-shell -b \"$help_command\"" 'refresh-client -S' >/dev/null 2>&1 || true
679
- tmux bind-key -T prefix n if-shell -F '#{@orchestra_display_name}' \
680
- "run-shell -b \"$new_session_command\"" 'next-window' >/dev/null 2>&1 || true
681
- tmux bind-key -T prefix l if-shell -F '#{@orchestra_display_name}' \
682
- "run-shell -b \"$list_command\"" 'last-window' >/dev/null 2>&1 || true
683
- tmux bind-key -T prefix X if-shell -F '#{@orchestra_display_name}' \
684
- "$close_prompt_command" 'confirm-before -p "kill-session #S? (y/n)" kill-session' >/dev/null 2>&1 || true
685
- tmux bind-key -T prefix r if-shell -F '#{@orchestra_display_name}' \
686
- "$prompt_command" 'refresh-client -S' >/dev/null 2>&1 || true
687
- tmux bind-key -T prefix '>' if-shell -F '#{@orchestra_display_name}' \
688
- "run-shell -b \"$next_command\"" 'switch-client -n' >/dev/null 2>&1 || true
689
- tmux bind-key -T prefix '<' if-shell -F '#{@orchestra_display_name}' \
690
- "run-shell -b \"$previous_command\"" 'switch-client -p' >/dev/null 2>&1 || true
691
- tmux bind-key -r -T prefix Right if-shell -F '#{@orchestra_display_name}' \
692
- "run-shell -b \"$next_command\"" 'select-pane -R' >/dev/null 2>&1 || true
693
- tmux bind-key -r -T prefix Left if-shell -F '#{@orchestra_display_name}' \
694
- "run-shell -b \"$previous_command\"" 'select-pane -L' >/dev/null 2>&1 || true
695
- }
696
-
697
- _tmux_configure_orchestra_status() {
698
- local session_name="$1"
699
- local worktree_name="$2"
700
- local session_display_name="${3:-}"
701
- local status_left status_right
702
- if [[ -z "$session_display_name" ]]; then
703
- session_display_name="$(tmux_format_session_display "$session_name" without-timestamp)"
704
- fi
705
- status_left="$(_tmux_orchestra_status_left "$session_name" "$worktree_name" "$session_display_name")"
706
- status_right="$(_tmux_orchestra_status_right "$worktree_name")"
707
-
708
- tmux set-option -t "$session_name" @orchestra_display_name "$session_display_name" >/dev/null 2>&1 || true
709
- tmux set-option -t "$session_name" @orchestra_worktree_name "$worktree_name" >/dev/null 2>&1 || true
710
- tmux set-option -t "$session_name" status on >/dev/null 2>&1 || true
711
- tmux set-option -t "$session_name" status-position bottom >/dev/null 2>&1 || true
712
- tmux set-option -t "$session_name" status-style "fg=#c0caf5,bg=#1a1b26" >/dev/null 2>&1 || true
713
- tmux set-option -t "$session_name" status-left "$status_left" >/dev/null 2>&1 || true
714
- tmux set-option -t "$session_name" status-left-length 1000 >/dev/null 2>&1 || true
715
- tmux set-option -t "$session_name" status-right "$status_right" >/dev/null 2>&1 || true
716
- tmux set-option -t "$session_name" status-right-length 40 >/dev/null 2>&1 || true
717
- tmux set-option -t "$session_name" window-status-format "" >/dev/null 2>&1 || true
718
- tmux set-option -t "$session_name" window-status-current-format "" >/dev/null 2>&1 || true
719
- tmux set-option -t "$session_name" window-status-separator "" >/dev/null 2>&1 || true
720
- _tmux_configure_orchestra_bindings
721
- }
722
-
723
- _tmux_refresh_orchestra_session_status() {
724
- local session_name="$1"
725
- local session_dir branch_name worktree_name display_name old_pwd
726
-
727
- session_dir="$(tmux display-message -t "$session_name" -p '#{pane_current_path}' 2>/dev/null || echo "")"
728
- branch_name=""
729
- if [[ -n "$session_dir" && -d "$session_dir" ]]; then
730
- old_pwd="$PWD"
731
- cd "$session_dir" 2>/dev/null || true
732
- branch_name="$(git_current_branch 2>/dev/null || git rev-parse --abbrev-ref HEAD 2>/dev/null || true)"
733
- cd "$old_pwd" 2>/dev/null || true
734
- fi
735
-
736
- if [[ -n "$branch_name" && "$branch_name" != "detached" ]]; then
737
- worktree_name="$branch_name"
738
- elif [[ -n "$session_dir" && -d "$session_dir" ]]; then
739
- worktree_name="$(basename "$session_dir")"
740
- else
741
- worktree_name="$branch_name"
742
- fi
743
-
744
- display_name="$(tmux show-option -t "$session_name" -qv @orchestra_display_name 2>/dev/null || true)"
745
- _tmux_configure_orchestra_status "$session_name" "$worktree_name" "$display_name"
746
- }
747
-
748
- # Helper: split a string by multi-char delimiter into bash array named by ref
749
- # Usage: _tmux_split_by_delim "string" "::" out_array_name
750
- _tmux_split_by_delim() {
751
- local _s="$1" _d="$2" _ref="$3"
752
- local _arr=()
753
- if [[ -z "$_d" ]]; then
754
- _arr=("$_s")
755
- else
756
- while :; do
757
- if [[ "$_s" == *"$_d"* ]]; then
758
- _arr+=("${_s%%"$_d"*}")
759
- _s="${_s#*"$_d"}"
760
- else
761
- _arr+=("$_s")
762
- break
763
- fi
764
- done
765
- fi
766
- # Use printf with %q to properly quote array elements for eval
767
- local _quoted=()
768
- local _item
769
- for _item in "${_arr[@]}"; do
770
- printf -v _q "%q" "$_item"
771
- _quoted+=("$_q")
772
- done
773
- eval "$_ref=( ${_quoted[*]} )"
774
- }
775
-
776
-
777
- # Check if tmux is available
778
- tmux_available() {
779
- have_cmd tmux
780
- }
781
-
782
- # Check if currently inside a tmux session
783
- tmux_inside_session() {
784
- [[ -n "${TMUX-}" ]]
785
- }
786
-
787
- # Generate random readable name for tmux sessions
788
- tmux_generate_readable_name() {
789
- local adjectives=(
790
- "swift" "brave" "clever" "gentle" "bright" "calm" "eager" "fierce" "happy" "kind"
791
- "lively" "noble" "proud" "quick" "smart" "wise" "bold" "cool" "daring" "epic"
792
- "fuzzy" "jolly" "lucky" "merry" "peppy" "rosy" "sunny" "zesty" "crisp" "fresh"
793
- )
794
- local animals=(
795
- "bear" "wolf" "fox" "eagle" "hawk" "lion" "tiger" "panda" "otter" "seal"
796
- "whale" "shark" "dolphin" "falcon" "raven" "deer" "moose" "lynx" "badger" "heron"
797
- "phoenix" "dragon" "griffin" "unicorn" "pegasus" "kraken" "sphinx" "chimera" "hydra" "basilisk"
798
- )
799
-
800
- local adj_idx=$((RANDOM % ${#adjectives[@]}))
801
- local animal_idx=$((RANDOM % ${#animals[@]}))
802
-
803
- echo "${adjectives[$adj_idx]}_${animals[$animal_idx]}"
804
- }
805
-
806
- # --------------------------- Session Management -----------------------------
807
-
808
- # Create a new tmux session
809
- # Usage: tmux_create_session <session_name> <working_directory>
810
- tmux_create_session() {
811
- local session_name="$1"
812
- local working_dir="$2"
813
-
814
- if ! tmux_available; then
815
- err "tmux not installed"
816
- return 1
817
- fi
818
-
819
- # Get repository info from the working directory context
820
- local repo_name=""
821
- local branch_name=""
822
- local repo_root=""
823
- local old_pwd="$PWD"
824
- local resolved_working_dir="$working_dir"
825
- if [[ -n "$working_dir" && -d "$working_dir" ]]; then
826
- resolved_working_dir="$(cd "$working_dir" 2>/dev/null && pwd -P || printf '%s' "$working_dir")"
827
- fi
828
-
829
- # Change to working directory to get accurate git info
830
- cd "$resolved_working_dir" 2>/dev/null || true
831
-
832
- if command -v git >/dev/null 2>&1 && git rev-parse --git-dir >/dev/null 2>&1; then
833
- repo_name="$(basename "$(git rev-parse --show-toplevel 2>/dev/null)" 2>/dev/null || echo "unknown")"
834
- branch_name="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "detached")"
835
- repo_root="$(git_shared_root 2>/dev/null || git_repo_root 2>/dev/null || true)"
836
- fi
837
-
838
- # Restore original directory
839
- cd "$old_pwd" 2>/dev/null || true
840
-
841
- # Ensure orchestra prefix (configurable delimiter) on session name to mark origin
842
- local ORCH_PREFIX
843
- ORCH_PREFIX="$(_tmux_orch_prefix)"
844
- if [[ "$session_name" != ${ORCH_PREFIX}* ]]; then
845
- session_name="${ORCH_PREFIX}${session_name}"
846
- fi
847
-
848
- # Create session with custom Orchestra status configuration
849
- tmux new-session -Ad -s "$session_name" -c "$resolved_working_dir" >/dev/null 2>&1 || {
850
- err "Failed to create tmux session: $session_name"
851
- return 1
852
- }
853
-
854
- local worktree_name=""
855
- if [[ -n "$branch_name" && "$branch_name" != "detached" ]]; then
856
- worktree_name="$branch_name"
857
- else
858
- worktree_name="$(basename "$resolved_working_dir")"
859
- fi
860
-
861
- if [[ -n "$repo_root" && -n "$branch_name" && "$branch_name" != "detached" ]]; then
862
- _tmux_registry_upsert_session "$repo_root" "$branch_name" "$resolved_working_dir" "$session_name" >/dev/null 2>&1 || true
863
- fi
864
-
865
- # Customize the default status bar to include Orchestra info on the left
866
- if [[ -n "$repo_name" ]]; then
867
- _tmux_configure_orchestra_status "$session_name" "$worktree_name"
868
- fi
869
-
870
- echo "$session_name"
871
- }
872
-
873
- # Generate a short hash from a path for unique session identification
874
- # Usage: tmux_path_hash <path>
875
- tmux_path_hash() {
876
- local path="$1"
877
- # Use MD5 hash of the path, take first 8 chars
878
- # Works on both macOS and Linux
879
- if command -v md5sum >/dev/null 2>&1; then
880
- echo -n "$path" | md5sum | cut -c1-8
881
- elif command -v md5 >/dev/null 2>&1; then
882
- echo -n "$path" | md5 -q | cut -c1-8
883
- else
884
- # Fallback: use cksum if neither md5 available
885
- echo -n "$path" | cksum | cut -d' ' -f1 | cut -c1-8
886
- fi
887
- }
888
-
889
- # Alias for backward compatibility
890
- tmux_repo_hash() {
891
- tmux_path_hash "$1"
892
- }
893
-
894
- # Ensure a session exists for slug+name in worktree; prints session name
895
- # Creates sessions with format: [worktreename]_[worktreetreehash]_[datetime]_[readable_name]
896
- # Usage: tmux_ensure_session <slug> <name> <worktree_path>
897
- tmux_ensure_session() {
898
- local slug="$1"
899
- local name="$2"
900
- local wt="$3"
901
- local d
902
- d="$(_tmux_delim)"
903
- local date_part time_part
904
- date_part="$(date +%Y%m%d)"
905
- time_part="$(date +%H%M%S)"
906
-
907
- # Use a repo-scoped hash to avoid cross-repo collisions
908
- # Hash the absolute worktree path (backward-compat listing supports old slug-hash)
909
- local worktree_hash
910
- worktree_hash="$(tmux_path_hash "$wt")"
911
-
912
- # If name is provided, use it; otherwise generate a random readable name with auto_ prefix
913
- if [[ -z "$name" || "$name" == "main" ]]; then
914
- name="auto_$(tmux_generate_readable_name)"
915
- fi
916
-
917
- local sess="${slug}${d}${worktree_hash}${d}${date_part}${d}${time_part}${d}${name}"
918
- tmux_create_session "$sess" "$wt"
919
- _tmux_source_command_hook "$sess"
920
- }
921
-
922
- # Create a new auto-named Orchestra session in the current session's worktree and switch to it.
923
- # Uses the same tmux_ensure_session path as the TUI tree view create action.
924
- # Usage: tmux_create_workspace_session <current_session> [client_tty]
925
- tmux_create_workspace_session() {
926
- local current_session="$1"
927
- local target_client="${2:-}"
928
- local session_dir branch slug new_session
929
-
930
- if ! tmux_available; then
931
- err "tmux not installed"
932
- return 1
933
- fi
934
- if [[ -z "$current_session" ]]; then
935
- err "tmux_create_workspace_session: current session required"
936
- return 1
937
- fi
938
-
939
- session_dir="$(tmux display-message -t "$current_session" -p '#{pane_current_path}' 2>/dev/null || echo "")"
940
- if [[ -z "$session_dir" || ! -d "$session_dir" ]]; then
941
- err "Unable to determine current worktree path"
942
- return 1
943
- fi
944
-
945
- branch="$(_tmux_branch_for_path "$session_dir" 2>/dev/null || true)"
946
- if [[ -z "$branch" || "$branch" == "detached" ]]; then
947
- err "Unable to determine current worktree branch"
948
- return 1
949
- fi
950
-
951
- slug="$(git_branch_to_slug "$branch")"
952
- new_session="$(tmux_ensure_session "$slug" "" "$session_dir")" || return 1
953
-
954
- if [[ -n "$target_client" ]]; then
955
- tmux switch-client -c "$target_client" -t "$new_session" >/dev/null 2>&1 || return 1
956
- else
957
- tmux switch-client -t "$new_session" >/dev/null 2>&1 || return 1
958
- fi
959
-
960
- printf '%s\n' "$new_session"
961
- }
962
-
963
- # Close the current Orchestra session after switching the client to the next one.
964
- # Usage: tmux_close_workspace_session <current_session> [client_tty]
965
- tmux_close_workspace_session() {
966
- local current_session="$1"
967
- local target_client="${2:-}"
968
- local target_session
969
-
970
- if ! tmux_available; then
971
- err "tmux not installed"
972
- return 1
973
- fi
974
- if [[ -z "$current_session" ]]; then
975
- err "tmux_close_workspace_session: current session required"
976
- return 1
977
- fi
978
-
979
- if ! target_session="$(tmux_workspace_cycle_target "$current_session" next)"; then
980
- tmux display-message -d 2500 "No other Orchestra sessions; current session kept" 2>/dev/null || true
981
- return 1
982
- fi
983
-
984
- if [[ -n "$target_client" ]]; then
985
- tmux switch-client -c "$target_client" -t "$target_session" >/dev/null 2>&1 || return 1
986
- else
987
- tmux switch-client -t "$target_session" >/dev/null 2>&1 || return 1
988
- fi
989
-
990
- tmux_kill_session "$current_session" >/dev/null 2>&1 || return 1
991
- _tmux_refresh_orchestra_session_status "$target_session" >/dev/null 2>&1 || true
992
- }
993
-
994
- # Check if a session exists
995
- # Usage: tmux_session_exists <session_name>
996
- tmux_session_exists() {
997
- local session_name="$1"
998
- tmux_available && tmux has-session -t "$session_name" 2>/dev/null
999
- }
1000
-
1001
- # Kill/delete a tmux session
1002
- # Usage: tmux_kill_session <session_name>
1003
- tmux_kill_session() {
1004
- local session_name="$1"
1005
-
1006
- if ! tmux_available; then
1007
- err "tmux not installed"
1008
- return 1
1009
- fi
1010
-
1011
- tmux kill-session -t "$session_name" 2>/dev/null || {
1012
- err "Failed to kill session: $session_name"
1013
- return 1
1014
- }
1015
- _tmux_registry_remove_session "$session_name" >/dev/null 2>&1 || true
1016
- }
1017
-
1018
- # Attach or switch to session
1019
- # Usage: tmux_attach_session <session_name>
1020
- tmux_attach_session() {
1021
- local sess="$1"
1022
-
1023
- if ! tmux_available; then
1024
- err "tmux not installed"
1025
- return 1
1026
- fi
1027
-
1028
- _tmux_source_command_hook "$sess"
1029
-
1030
- # Get repository info for terminal title and banner update
1031
- local repo_name=""
1032
- local branch_name=""
1033
- local session_dir=""
1034
-
1035
- # Try to get the session's working directory
1036
- session_dir="$(tmux display-message -t "$sess" -p '#{pane_current_path}' 2>/dev/null || echo "")"
1037
-
1038
- if [[ -n "$session_dir" ]] && [[ -d "$session_dir" ]]; then
1039
- # Get git info from session's directory
1040
- local old_pwd="$PWD"
1041
- cd "$session_dir" 2>/dev/null || true
1042
-
1043
- if command -v git >/dev/null 2>&1 && git rev-parse --git-dir >/dev/null 2>&1; then
1044
- repo_name="$(basename "$(git rev-parse --show-toplevel 2>/dev/null)" 2>/dev/null || echo "unknown")"
1045
- branch_name="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "detached")"
1046
- fi
1047
-
1048
- cd "$old_pwd" 2>/dev/null || true
1049
- else
1050
- # Fallback to current directory
1051
- if command -v git >/dev/null 2>&1 && git rev-parse --git-dir >/dev/null 2>&1; then
1052
- repo_name="$(basename "$(git rev-parse --show-toplevel 2>/dev/null)" 2>/dev/null || echo "unknown")"
1053
- branch_name="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "detached")"
1054
- fi
1055
- fi
1056
-
1057
- # Set terminal window title (works in most terminal emulators)
1058
- if [[ -n "$repo_name" ]]; then
1059
- printf "\033]0;🎼 Orchestra: %s [%s]\007" "$repo_name" "$branch_name" >/dev/tty 2>/dev/null || true
1060
- fi
1061
-
1062
- # Show welcome message when attaching
1063
- if [[ -n "$repo_name" ]]; then
1064
- tmux display-message -t "$sess" -d 2000 \
1065
- "🎼 Orchestra Session | Repository: $repo_name | Branch: $branch_name" 2>/dev/null || true
1066
- fi
1067
-
1068
- _tmux_refresh_orchestra_session_status "$sess"
1069
-
1070
- if tmux_inside_session; then
1071
- tmux switch-client -t "$sess" >/dev/null 2>&1 || true
1072
- else
1073
- tmux attach -t "$sess" >/dev/null 2>&1 || true
1074
- fi
1075
- }
1076
-
1077
- # Rename a tmux session while preserving the worktree prefix, repo hash, and datetime
1078
- # Usage: tmux_rename_session <old_session_name> <new_name>
1079
- tmux_rename_session() {
1080
- local old_session="$1"
1081
- local new_name="$2"
1082
-
1083
- if ! tmux_available; then
1084
- err "tmux not installed"
1085
- return 1
1086
- fi
1087
-
1088
- local d ORCH_PREFIX
1089
- d="$(_tmux_delim)"
1090
- ORCH_PREFIX="$(_tmux_orch_prefix)"
1091
-
1092
- # Detect and strip orchestra prefix for parsing
1093
- local orch=""
1094
- local base="$old_session"
1095
- if [[ "$base" == ${ORCH_PREFIX}* ]]; then
1096
- orch="$ORCH_PREFIX"
1097
- base="${base#${ORCH_PREFIX}}"
1098
- fi
1099
-
1100
- # Split by current delimiter and scan from the right to tolerate delimiter inside segments
1101
- local parts
1102
- _tmux_split_by_delim "$base" "$d" parts
1103
-
1104
- local n=${#parts[@]}
1105
- local idx_time=-1 idx_date=-1 idx_hash=-1
1106
- local i
1107
- for (( i=n-1; i>=0; i-- )); do
1108
- if [[ ${parts[$i]} =~ ^[0-9]{6}$ ]]; then idx_time=$i; break; fi
1109
- done
1110
- if (( idx_time > 0 )) && [[ ${parts[$((idx_time-1))]} =~ ^[0-9]{8}$ ]]; then
1111
- idx_date=$((idx_time-1))
1112
- fi
1113
- if (( idx_date > 0 )) && [[ ${parts[$((idx_date-1))]} =~ ^[0-9a-f]{8}$ ]]; then
1114
- idx_hash=$((idx_date-1))
1115
- fi
1116
-
1117
- local prefix=""
1118
- if (( idx_time >= 0 && idx_date >= 0 )); then
1119
- # Build prefix: slug + d + [hash + d] + date + d + time + d
1120
- local upto=$idx_time
1121
- local j
1122
- for (( j=0; j<=upto; j++ )); do
1123
- if (( j > 0 )); then prefix+="$d"; fi
1124
- prefix+="${parts[$j]}"
1125
- done
1126
- prefix+="$d"
1127
-
1128
- # Debug logging for rename parsing
1129
- if [[ -n "${GW_DEBUG_RENAME-}" || -n "${DEBUG-}" ]]; then
1130
- >&2 echo "[orchestra] rename DEBUG: old='$old_session' base='$base' delim='$d'"
1131
- >&2 echo "[orchestra] parts: ${parts[*]}"
1132
- >&2 echo "[orchestra] idx_time=$idx_time idx_date=$idx_date idx_hash=$idx_hash"
1133
- >&2 echo "[orchestra] prefix='${orch}${prefix}' new_name='$new_name'"
1134
- fi
1135
- else
1136
- err "Invalid session format"
1137
- return 1
1138
- fi
1139
-
1140
- local new_session="${orch}${prefix}${new_name}"
1141
-
1142
- tmux rename-session -t "$old_session" "$new_session" 2>/dev/null || {
1143
- err "Failed to rename session"
1144
- return 1
1145
- }
1146
- _tmux_registry_rename_session "$old_session" "$new_session" "$(format_session_display_name "$new_name")" >/dev/null 2>&1 || true
1147
- local worktree_name
1148
- worktree_name="$(tmux show-option -t "$new_session" -qv @orchestra_worktree_name 2>/dev/null || true)"
1149
- if [[ -z "$worktree_name" ]]; then
1150
- local session_dir old_pwd branch_name
1151
- session_dir="$(tmux display-message -t "$new_session" -p '#{pane_current_path}' 2>/dev/null || echo "")"
1152
- if [[ -n "$session_dir" && -d "$session_dir" ]]; then
1153
- old_pwd="$PWD"
1154
- cd "$session_dir" 2>/dev/null || true
1155
- branch_name="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "")"
1156
- cd "$old_pwd" 2>/dev/null || true
1157
- if [[ -n "$branch_name" && "$branch_name" != "detached" ]]; then
1158
- worktree_name="$branch_name"
1159
- else
1160
- worktree_name="$(basename "$session_dir")"
1161
- fi
1162
- fi
1163
- fi
1164
- _tmux_configure_orchestra_status "$new_session" "$worktree_name"
1165
- >&2 echo "✏️ Renamed session to: $new_name"
1166
- return 0
1167
- }
1168
-
1169
- # --------------------------- Session Discovery ------------------------------
1170
-
1171
- # Get tmux sessions for a slug prefix; prints session names sorted by creation time, oldest first
1172
- # Expected session format: [worktreename]_[worktreehash]_[datetime]_[readable_name]
1173
- # Usage: tmux_list_sessions_for_slug <slug> [worktree_path]
1174
- tmux_list_sessions_for_slug() {
1175
- local slug="$1"
1176
- local worktree_path="${2:-}"
1177
- tmux_available || return 0
1178
-
1179
- local d ORCH_PREFIX
1180
- d="$(_tmux_delim)"
1181
- ORCH_PREFIX="$(_tmux_orch_prefix)"
1182
-
1183
- # If worktree_path provided, match hash-based and no-hash prefixes
1184
- if [[ -n "$worktree_path" ]]; then
1185
- local hash_slug hash_path branch_name
1186
- hash_slug="$(tmux_path_hash "$slug")"
1187
- hash_path="$(tmux_path_hash "$worktree_path")"
1188
- branch_name="$(git_worktree_path_to_branch "$worktree_path" 2>/dev/null || true)"
1189
-
1190
- local p1_new="${ORCH_PREFIX}${slug}${d}${hash_slug}${d}"
1191
- local p2_new="${slug}${d}${hash_slug}${d}"
1192
- local p1_old="${ORCH_PREFIX}${slug}${d}${hash_path}${d}"
1193
- local p2_old="${slug}${d}${hash_path}${d}"
1194
- local p1_branch_hash=""
1195
- local p2_branch_hash=""
1196
- if [[ -n "$branch_name" ]]; then
1197
- p1_branch_hash="${ORCH_PREFIX}${branch_name}${d}${hash_path}${d}"
1198
- p2_branch_hash="${branch_name}${d}${hash_path}${d}"
1199
- fi
1200
-
1201
- # List sessions with any known prefix variant
1202
- tmux list-sessions -F '#{session_name}|||#{session_created}' 2>/dev/null \
1203
- | sed 's/|||/\t/g' \
1204
- | awk -v a="$p1_new" -v b="$p2_new" -v c="$p1_old" -v d="$p2_old" -v e="$p1_branch_hash" -v f="$p2_branch_hash" 'BEGIN{FS="\t"} (length(a)>0 && index($1, a)==1) || (length(b)>0 && index($1, b)==1) || (length(c)>0 && index($1, c)==1) || (length(d)>0 && index($1, d)==1) || (length(e)>0 && index($1, e)==1) || (length(f)>0 && index($1, f)==1) {print $1"\t"$2}' \
1205
- | sort -t $'\t' -k2,2n -k1,1f \
1206
- | awk -F '\t' '{print $1}' | awk '!seen[$0]++' || true
1207
- else
1208
- # New-format prefix matching without hash: orchestra__slug__... or slug__...
1209
- local prefix1="${ORCH_PREFIX}${slug}${d}"
1210
- local prefix2="${slug}${d}"
1211
- tmux list-sessions -F '#{session_name}|||#{session_created}' 2>/dev/null \
1212
- | sed 's/|||/\t/g' \
1213
- | awk -v p1="$prefix1" -v p2="$prefix2" 'BEGIN{FS="\t"} index($1, p1)==1 || index($1, p2)==1 {print $1"\t"$2}' \
1214
- | sort -t $'\t' -k2,2n -k1,1f \
1215
- | awk -F '\t' '{print $1}' || true
1216
- fi
1217
- }
1218
-
1219
- # List all tmux sessions
1220
- # Usage: tmux_list_all_sessions
1221
- tmux_list_all_sessions() {
1222
- tmux_available || return 0
1223
- tmux list-sessions -F '#{session_name}' 2>/dev/null || true
1224
- }
1225
-
1226
- # Find sessions matching a pattern
1227
- # Usage: tmux_find_session <pattern>
1228
- tmux_find_session() {
1229
- local pattern="$1"
1230
-
1231
- if ! tmux_available; then
1232
- return 1
1233
- fi
1234
-
1235
- # Try exact match first
1236
- if tmux_session_exists "$pattern"; then
1237
- echo "$pattern"
1238
- return 0
1239
- fi
1240
-
1241
- # Try pattern match
1242
- tmux_list_all_sessions | grep -E "$pattern" | head -1 || true
1243
- }
1244
-
1245
- # --------------------------- Session Information ----------------------------
1246
-
1247
- # Helper function to format session display names
1248
- # Input: session_content (e.g., "opencode_fixing_auth_bug" or "my_feature_work")
1249
- # Output: "Opencode Fixing Auth Bug" or "My Feature Work"
1250
- format_session_display_name() {
1251
- local session_content="$1"
1252
- local d
1253
- d="$(_tmux_delim)"
1254
- local ORCH_PREFIX
1255
- ORCH_PREFIX="$(_tmux_orch_prefix)"
1256
- if [[ "$session_content" == ${ORCH_PREFIX}* ]]; then
1257
- session_content="${session_content#${ORCH_PREFIX}}"
1258
- fi
1259
- if [[ "$session_content" == auto_* ]]; then
1260
- session_content="${session_content#auto_}"
1261
- fi
1262
- local description
1263
- description="$(echo "$session_content" | tr '_-' ' ')"
1264
- description="$(echo "$description" | awk '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2));}1')"
1265
- echo "$description"
1266
- }
1267
-
1268
- _tmux_format_session_timestamp() {
1269
- local date_part="$1"
1270
- local time_part="$2"
1271
-
1272
- if [[ ! "$date_part" =~ ^[0-9]{8}$ || ! "$time_part" =~ ^[0-9]{6}$ ]]; then
1273
- printf '%s %s\n' "$date_part" "$time_part"
1274
- return
1275
- fi
1276
-
1277
- local month day hour minute ampm display_hour
1278
- month=$((10#${date_part:4:2}))
1279
- day=$((10#${date_part:6:2}))
1280
- hour=$((10#${time_part:0:2}))
1281
- minute="${time_part:2:2}"
1282
-
1283
- ampm="am"
1284
- display_hour="$hour"
1285
- if (( hour >= 12 )); then
1286
- ampm="pm"
1287
- if (( hour > 12 )); then
1288
- display_hour=$((hour - 12))
1289
- fi
1290
- fi
1291
- if (( hour == 0 )); then
1292
- display_hour=12
1293
- fi
1294
-
1295
- local month_names=("" "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
1296
- local month_name="???"
1297
- if (( month > 0 && month <= 12 )); then
1298
- month_name="${month_names[$month]}"
1299
- fi
1300
-
1301
- printf '%s %s %s:%s%s\n' "$month_name" "$day" "$display_hour" "$minute" "$ampm"
1302
- }
1303
-
1304
- # Parse session name and format for display
1305
- # Input: session_name (format: worktreename_repohash_YYYYMMDD_HHMMSS_readable_name or old formats)
1306
- # Output: formatted display name
1307
- # Usage: tmux_format_session_display <session_name>
1308
- tmux_format_session_display() {
1309
- local session_name="$1"
1310
- local timestamp_mode="${2:-with-timestamp}"
1311
-
1312
- # Handle temporary renaming sessions first
1313
- if [[ "$session_name" =~ _renaming$ ]]; then
1314
- # Extract base name before _renaming suffix
1315
- local base_name="${session_name%_renaming}"
1316
- # Recursively process the base name to get proper display
1317
- tmux_format_session_display "$base_name" "$timestamp_mode"
1318
- return $?
1319
- fi
1320
-
1321
- local d ORCH_PREFIX
1322
- d="$(_tmux_delim)"
1323
- ORCH_PREFIX="$(_tmux_orch_prefix)"
1324
-
1325
- if [[ "$session_name" == ${ORCH_PREFIX}* ]]; then
1326
- session_name="${session_name#${ORCH_PREFIX}}"
1327
- elif [[ "$session_name" =~ ^orchestra_(.+)$ ]]; then
1328
- session_name="${BASH_REMATCH[1]}"
1329
- fi
1330
-
1331
- local parts
1332
- _tmux_split_by_delim "$session_name" "$d" parts
1333
- local idx_time=-1 idx_date=-1 i
1334
- for (( i=${#parts[@]}-1; i>=0; i-- )); do
1335
- if [[ ${parts[$i]} =~ ^[0-9]{6}$ ]] && (( i > 0 )) && [[ ${parts[$((i-1))]} =~ ^[0-9]{8}$ ]]; then
1336
- idx_time=$i
1337
- idx_date=$((i-1))
1338
- break
1339
- fi
1340
- done
1341
- if (( idx_time >= 0 && idx_date >= 0 )); then
1342
- local readable_name=""
1343
- local j
1344
- for (( j=idx_time+1; j<${#parts[@]}; j++ )); do
1345
- if [[ -n "$readable_name" ]]; then
1346
- readable_name+="$d"
1347
- fi
1348
- readable_name+="${parts[$j]}"
1349
- done
1350
- if [[ -n "$readable_name" ]]; then
1351
- local formatted_name timestamp
1352
- formatted_name="$(format_session_display_name "$readable_name")"
1353
- timestamp="$(_tmux_format_session_timestamp "${parts[$idx_date]}" "${parts[$idx_time]}")"
1354
- if [[ "$timestamp_mode" == "without-timestamp" ]]; then
1355
- echo "$formatted_name"
1356
- else
1357
- echo "${formatted_name} (${timestamp})"
1358
- fi
1359
- return 0
1360
- fi
1361
- fi
1362
-
1363
- # New format with repo hash: worktreename_repohash_YYYYMMDD_HHMMSS_readable_name
1364
- if [[ "$session_name" =~ ^[^_]+_[a-f0-9]{8}_([0-9]{8})_([0-9]{6})_(.+)$ ]]; then
1365
- local date_part="${BASH_REMATCH[1]}" # YYYYMMDD
1366
- local time_part="${BASH_REMATCH[2]}" # HHMMSS
1367
- local readable_name="${BASH_REMATCH[3]}" # readable_name
1368
-
1369
- # Parse date: YYYYMMDD -> Jul 21
1370
- local year="${date_part:0:4}"
1371
- local month="${date_part:4:2}"
1372
- local day="${date_part:6:2}"
1373
-
1374
- # Parse time: HHMMSS -> 12:30am
1375
- local hour="${time_part:0:2}"
1376
- local minute="${time_part:2:2}"
1377
-
1378
- # Convert to 12-hour format
1379
- local ampm="am"
1380
- local display_hour="$hour"
1381
- if [[ "$hour" -ge 12 ]]; then
1382
- ampm="pm"
1383
- if [[ "$hour" -gt 12 ]]; then
1384
- display_hour=$((hour - 12))
1385
- fi
1386
- fi
1387
- if [[ "$hour" == "00" ]]; then
1388
- display_hour="12"
1389
- fi
1390
-
1391
- # Remove leading zero from hour
1392
- display_hour="${display_hour#0}"
1393
-
1394
- # Convert month number to name
1395
- local month_names=("" "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
1396
- local month_name="${month_names[${month#0}]}"
1397
-
1398
- # Remove leading zero from day
1399
- local display_day="${day#0}"
1400
-
1401
- # Format the readable name with proper formatting
1402
- local formatted_name="$(format_session_display_name "$readable_name")"
1403
-
1404
- if [[ "$timestamp_mode" == "without-timestamp" ]]; then
1405
- echo "$formatted_name"
1406
- else
1407
- echo "${formatted_name} (${month_name} ${display_day} ${display_hour}:${minute}${ampm})"
1408
- fi
1409
- # Fallback: Try format without repo hash: appname_description_YYYYMMDD_HHMMSS
1410
- elif [[ "$session_name" =~ ^(.*)_[0-9]{8}_[0-9]{6}$ ]]; then
1411
- local session_content="${BASH_REMATCH[1]}" # appname_description
1412
-
1413
- # Convert to proper display format
1414
- local formatted_name="$(format_session_display_name "$session_content")"
1415
- echo "$formatted_name"
1416
- else
1417
- # Check for old format without repo hash: worktreename_YYYYMMDD_HHMMSS_readable_name
1418
- if [[ "$session_name" =~ ^[^_]+_([0-9]{8})_([0-9]{6})_(.+)$ ]]; then
1419
- local date_part="${BASH_REMATCH[1]}" # YYYYMMDD
1420
- local time_part="${BASH_REMATCH[2]}" # HHMMSS
1421
- local readable_name="${BASH_REMATCH[3]}" # readable_name
1422
-
1423
- # Parse date: YYYYMMDD -> Jul 21
1424
- local year="${date_part:0:4}"
1425
- local month="${date_part:4:2}"
1426
- local day="${date_part:6:2}"
1427
-
1428
- # Parse time: HHMMSS -> 12:30am
1429
- local hour="${time_part:0:2}"
1430
- local minute="${time_part:2:2}"
1431
-
1432
- # Convert to 12-hour format
1433
- local ampm="am"
1434
- local display_hour="$hour"
1435
- if [[ "$hour" -ge 12 ]]; then
1436
- ampm="pm"
1437
- if [[ "$hour" -gt 12 ]]; then
1438
- display_hour=$((hour - 12))
1439
- fi
1440
- fi
1441
- if [[ "$hour" == "00" ]]; then
1442
- display_hour="12"
1443
- fi
1444
-
1445
- # Remove leading zero from hour
1446
- display_hour="${display_hour#0}"
1447
-
1448
- # Convert month number to name
1449
- local month_names=("" "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
1450
- local month_name="${month_names[${month#0}]}"
1451
-
1452
- # Remove leading zero from day
1453
- local display_day="${day#0}"
1454
-
1455
- # Format the readable name with proper formatting
1456
- local formatted_name="$(format_session_display_name "$readable_name")"
1457
-
1458
- if [[ "$timestamp_mode" == "without-timestamp" ]]; then
1459
- echo "$formatted_name"
1460
- else
1461
- echo "${formatted_name} (${month_name} ${display_day} ${display_hour}:${minute}${ampm})"
1462
- fi
1463
- else
1464
- # Fallback for sessions that don't match either format
1465
- echo "$session_name"
1466
- fi
1467
- fi
1468
- }
1469
-
1470
- # Get active pane id for a session (best effort)
1471
- # Usage: tmux_get_active_pane <session_name>
1472
- tmux_get_active_pane() {
1473
- local s="$1"
1474
- tmux_available || return 1
1475
-
1476
- # Find active window id
1477
- local win
1478
- win="$(tmux list-windows -t "$s" -F '#{window_active} #{window_id}' 2>/dev/null | awk '$1==1{print $2; exit}')" || true
1479
- [[ -z "$win" ]] && return 1
1480
-
1481
- # Find active pane id within that window
1482
- tmux list-panes -t "$win" -F '#{pane_active} #{pane_id}' 2>/dev/null | awk '$1==1{print $2; exit}' || true
1483
- }
1484
-
1485
- # Capture enhanced session preview showing current terminal view (bottom lines)
1486
- # Usage: tmux_session_preview <session_name>
1487
- tmux_session_preview() {
1488
- local s="$1"
1489
- tmux_available || { echo "(tmux not installed)"; return 0; }
1490
-
1491
- local pane
1492
- pane="$(tmux_get_active_pane "$s" || true)"
1493
- if [[ -z "$pane" ]]; then
1494
- echo "(no active pane found)"
1495
- return 0
1496
- fi
1497
-
1498
- # Always capture from the BOTTOM of the terminal buffer (last visible lines)
1499
- # -e flag preserves escape sequences for colors and formatting
1500
- # -p prints to stdout
1501
- # NO -S/-E flags means capture the current viewport (what's visible NOW)
1502
- # This ensures we always see the bottom/most recent output
1503
- local txt
1504
- txt="$(tmux capture-pane -e -p -t "$pane" 2>/dev/null)"
1505
-
1506
- if [[ -z "$txt" ]]; then
1507
- echo "(no output yet)"
1508
- return 0
1509
- fi
1510
-
1511
- # Check if the pane is idle (cursor at prompt, no running process)
1512
- local pane_cmd pane_mode
1513
- pane_cmd="$(tmux display-message -t "$pane" -p '#{pane_current_command}' 2>/dev/null || echo "")"
1514
- pane_mode="$(tmux display-message -t "$pane" -p '#{pane_mode}' 2>/dev/null || echo "")"
1515
-
1516
- # Detect idle state: shell running and not in copy mode
1517
- local is_idle="false"
1518
- case "$pane_cmd" in
1519
- bash|zsh|sh|fish|dash|ksh)
1520
- # Shell is running, check if we're at a prompt (not in copy mode)
1521
- if [[ "$pane_mode" == "" ]]; then
1522
- is_idle="true"
1523
- fi
1524
- ;;
1525
- esac
1526
-
1527
- # Optimized: Get terminal info in a single command instead of multiple calls
1528
- local term_info has_rgb
1529
- term_info="$(tmux show-environment -t "$s" TERM 2>/dev/null | cut -d= -f2 || echo "unknown")"
1530
-
1531
- # Fast check for RGB support without full grep
1532
- if tmux show-options -t "$s" -s terminal-overrides 2>/dev/null | grep -q "RGB"; then
1533
- has_rgb="true"
1534
- else
1535
- has_rgb="false"
1536
- fi
1537
-
1538
- # For ANSI-preserved preview with color mode info
1539
- # Add markers for the Rust parser to detect color capabilities and idle state
1540
- if [[ "$has_rgb" == "true" || "$term_info" == *"direct"* || "$term_info" == *"truecolor"* ]]; then
1541
- echo "<<<COLORMODE:RGB>>>"
1542
- elif [[ "$term_info" == *"256color"* ]]; then
1543
- echo "<<<COLORMODE:256>>>"
1544
- else
1545
- echo "<<<COLORMODE:16>>>"
1546
- fi
1547
-
1548
- # Add idle marker so Rust can stop polling when terminal is idle
1549
- if [[ "$is_idle" == "true" ]]; then
1550
- echo "<<<IDLE:true>>>"
1551
- else
1552
- echo "<<<IDLE:false>>>"
1553
- fi
1554
-
1555
- # If no content after processing, show placeholder
1556
- if [[ -z "$txt" ]]; then
1557
- echo "(session active, no visible output)"
1558
- else
1559
- echo "$txt"
1560
- fi
1561
- }
1562
-
1563
- # --------------------------- Advanced Operations ----------------------------
1564
-
1565
- # Send keys to a session and press Enter
1566
- # Usage: tmux_send_keys <session_name> <command...>
1567
- tmux_send_keys() {
1568
- local session_name="$1"; shift || true
1569
- local command_line="$*"
1570
- if ! tmux_available; then
1571
- err "tmux not installed"
1572
- return 1
1573
- fi
1574
- if [[ -z "$session_name" || -z "$command_line" ]]; then
1575
- err "tmux_send_keys: session and command required"
1576
- return 1
1577
- fi
1578
- tmux send-keys -t "$session_name" -l -- "$command_line" 2>/dev/null || return 1
1579
- tmux send-keys -t "$session_name" C-m 2>/dev/null || return 1
1580
- }
1581
-
1582
- # Show Orchestra tmux shortcuts in a popup modal.
1583
- # Usage: tmux_show_orchestra_help_popup [client_tty]
1584
- tmux_show_orchestra_help_popup() {
1585
- local target_client="${1:-}"
1586
- if ! tmux_available; then
1587
- err "tmux not installed"
1588
- return 1
1589
- fi
1590
-
1591
- local target_args=()
1592
- if [[ -n "$target_client" ]]; then
1593
- target_args=(-c "$target_client")
1594
- fi
1595
-
1596
- local popup_command
1597
- popup_command="$(cat <<'EOF'
1598
- bash -lc '
1599
- trap "exit 0" INT TERM
1600
- printf "%s\n" \
1601
- "Orchestra tmux shortcuts" \
1602
- "" \
1603
- "Ctrl+b, d Detach and return to Orchestra" \
1604
- "Ctrl+b, r Rename the current Orchestra session" \
1605
- "Ctrl+b, n New Orchestra session in this worktree" \
1606
- "Ctrl+b, X Close current session and switch next" \
1607
- "Ctrl+b, l List active sessions" \
1608
- "" \
1609
- "Ctrl+b, Left Previous Orchestra session in this workspace" \
1610
- "Ctrl+b, Right Next Orchestra session in this workspace" \
1611
- "Ctrl+b, < Previous Orchestra session in this workspace" \
1612
- "Ctrl+b, > Next Orchestra session in this workspace" \
1613
- "" \
1614
- "Ctrl+b, [ Copy/scroll mode" \
1615
- "Ctrl+b, h Show this help" \
1616
- "Ctrl+b, ? Show this help" \
1617
- "" \
1618
- "Press any key to close..."
1619
- IFS= read -rsn1 _ || true
1620
- exit 0
1621
- '
1622
- EOF
1623
- )"
1624
-
1625
- tmux display-popup "${target_args[@]}" \
1626
- -E \
1627
- -w 84 \
1628
- -h 22 \
1629
- -s "fg=#c0caf5,bg=#1a1b26" \
1630
- -S "fg=#7aa2f7,bg=#1a1b26" \
1631
- -T "Orchestra shortcuts" \
1632
- "$popup_command" >/dev/null 2>&1 || true
1633
- }
1634
-
1635
- # Show active Orchestra sessions in a native tmux menu for arrow-key selection.
1636
- # Usage: tmux_show_workspace_session_menu <current_session> [client_tty]
1637
- tmux_show_workspace_session_menu() {
1638
- local current_session="$1"
1639
- local target_client="${2:-}"
1640
- local session_dir repo_root rows name display_name label target_command
1641
- local menu_width=56
1642
- local min_menu_rows=10
1643
- local menu_row_count=0
1644
-
1645
- if ! tmux_available; then
1646
- err "tmux not installed"
1647
- return 1
1648
- fi
1649
- if [[ -z "$current_session" ]]; then
1650
- err "tmux_show_workspace_session_menu: current session required"
1651
- return 1
1652
- fi
1653
-
1654
- session_dir="$(tmux display-message -t "$current_session" -p '#{pane_current_path}' 2>/dev/null || echo "")"
1655
- repo_root="$(_tmux_shared_root_for_path "$session_dir" 2>/dev/null || true)"
1656
- if [[ -z "$repo_root" ]]; then
1657
- tmux display-message -d 2500 "Unable to determine Orchestra workspace" 2>/dev/null || true
1658
- return 1
1659
- fi
1660
-
1661
- _tmux_registry_sync_active_workspace_sessions "$repo_root" >/dev/null 2>&1 || true
1662
- rows="$(_tmux_workspace_session_rows "$current_session" "$repo_root" all 2>/dev/null || true)"
1663
- if [[ -z "$rows" ]]; then
1664
- tmux display-message -d 2500 "No active Orchestra sessions in this workspace" 2>/dev/null || true
1665
- return 1
1666
- fi
1667
-
1668
- local target_args=()
1669
- if [[ -n "$target_client" ]]; then
1670
- target_args=(-c "$target_client")
1671
- fi
1672
-
1673
- local menu_items=()
1674
- while IFS=$'\t' read -r name display_name; do
1675
- [[ -n "$name" && "$name" != "__ellipsis__" ]] || continue
1676
- if [[ -z "$display_name" ]]; then
1677
- display_name="$(tmux_format_session_display "$name" without-timestamp)"
1678
- fi
1679
- label="$(_tmux_truncate_tab_label "$display_name" 42)"
1680
- if [[ "$name" == "$current_session" ]]; then
1681
- label="● $label"
1682
- else
1683
- label=" $label"
1684
- fi
1685
- label="$(_tmux_pad_menu_label "$label" "$menu_width")"
1686
- label="$(_tmux_status_escape_text "$label")"
1687
- target_command="switch-client -t \"$name\""
1688
- menu_items+=("$label" "" "$target_command")
1689
- menu_row_count=$((menu_row_count + 1))
1690
- done <<< "$rows"
1691
-
1692
- if [[ ${#menu_items[@]} -eq 0 ]]; then
1693
- tmux display-message -d 2500 "No active Orchestra sessions in this workspace" 2>/dev/null || true
1694
- return 1
1695
- fi
1696
-
1697
- while (( menu_row_count < min_menu_rows )); do
1698
- menu_items+=("$(_tmux_pad_menu_label "" "$menu_width")" "" "")
1699
- menu_row_count=$((menu_row_count + 1))
1700
- done
1701
-
1702
- tmux display-menu "${target_args[@]}" \
1703
- -s "fg=#c0caf5,bg=#1a1b26" \
1704
- -S "fg=#7aa2f7,bg=#1a1b26" \
1705
- -H "fg=#1a1b26,bg=#ff9e64,bold" \
1706
- -T "Orchestra sessions" \
1707
- "${menu_items[@]}" >/dev/null 2>&1 || true
1708
- }
1709
-
1710
- # Find the adjacent active Orchestra session registered for the current repo.
1711
- # Usage: tmux_workspace_cycle_target <current_session> <next|previous>
1712
- tmux_workspace_cycle_target() {
1713
- local current_session="$1"
1714
- local direction="$2"
1715
- local session_dir repo_root db_path active_file target query_status
1716
-
1717
- if ! tmux_available; then
1718
- err "tmux not installed"
1719
- return 1
1720
- fi
1721
- if [[ -z "$current_session" ]]; then
1722
- err "tmux_workspace_cycle_target: current session required"
1723
- return 1
1724
- fi
1725
- case "$direction" in
1726
- next|previous|prev) ;;
1727
- *)
1728
- err "tmux_workspace_cycle_target: direction must be next or previous"
1729
- return 1
1730
- ;;
1731
- esac
1732
-
1733
- db_path="$(_tmux_session_registry_path)"
1734
- have_cmd python3 || {
1735
- err "python3 is required to read the Orchestra session registry"
1736
- return 1
1737
- }
1738
-
1739
- active_file="$(mktemp)"
1740
- tmux list-sessions -F $'#{session_name}\t#{@orchestra_display_name}\t#{session_last_attached}\t#{session_activity}' > "$active_file" 2>/dev/null || {
1741
- rm -f "$active_file"
1742
- err "Unable to list tmux sessions"
1743
- return 1
1744
- }
1745
-
1746
- if [[ -f "$db_path" ]]; then
1747
- if target="$(_tmux_workspace_cycle_target_cached "$current_session" "$direction" "$db_path" "$active_file")"; then
1748
- query_status=0
1749
- else
1750
- query_status=$?
1751
- fi
1752
- if [[ $query_status -eq 0 && -n "$target" ]]; then
1753
- rm -f "$active_file"
1754
- printf '%s\n' "$target"
1755
- return 0
1756
- fi
1757
- if [[ $query_status -eq 3 ]]; then
1758
- rm -f "$active_file"
1759
- return 1
1760
- fi
1761
- fi
1762
-
1763
- _tmux_registry_upsert_current_session "$current_session" >/dev/null 2>&1 || true
1764
-
1765
- session_dir="$(tmux display-message -t "$current_session" -p '#{pane_current_path}' 2>/dev/null || echo "")"
1766
- repo_root="$(_tmux_shared_root_for_path "$session_dir" 2>/dev/null || true)"
1767
- if [[ -z "$repo_root" ]]; then
1768
- rm -f "$active_file"
1769
- err "Unable to determine Orchestra workspace for session"
1770
- return 1
1771
- fi
1772
- _tmux_registry_sync_active_workspace_sessions "$repo_root" >/dev/null 2>&1 || true
1773
-
1774
- if [[ ! -f "$db_path" ]]; then
1775
- rm -f "$active_file"
1776
- err "No Orchestra session registry found"
1777
- return 1
1778
- fi
1779
-
1780
- if target="$(python3 - "$db_path" "$repo_root" "$current_session" "$direction" "$active_file" <<'PY'
1781
- import sqlite3
1782
- import sys
1783
-
1784
- db_path, repo_root, current_session, direction, active_path = sys.argv[1:6]
1785
- with open(active_path, "r", encoding="utf-8") as handle:
1786
- active = {
1787
- line.rstrip("\n").split("\t", 1)[0].strip()
1788
- for line in handle
1789
- if line.rstrip("\n").split("\t", 1)[0].strip()
1790
- }
1791
-
1792
- conn = sqlite3.connect(db_path)
1793
- rows = conn.execute(
1794
- """
1795
- SELECT tmux_name
1796
- FROM sessions
1797
- WHERE repo_root = ?1
1798
- ORDER BY worktree_path COLLATE NOCASE, created_at, tmux_name COLLATE NOCASE
1799
- """,
1800
- (repo_root,),
1801
- ).fetchall()
1802
-
1803
- names = []
1804
- seen = set()
1805
- for (name,) in rows:
1806
- if name in active and name not in seen:
1807
- names.append(name)
1808
- seen.add(name)
1809
-
1810
- if len(names) < 2 or current_session not in seen:
1811
- raise SystemExit(0)
1812
-
1813
- index = names.index(current_session)
1814
- if direction == "next":
1815
- index = (index + 1) % len(names)
1816
- else:
1817
- index = (index - 1) % len(names)
1818
- print(names[index])
1819
- PY
1820
- )"; then
1821
- query_status=0
1822
- else
1823
- query_status=$?
1824
- fi
1825
- rm -f "$active_file"
1826
- if [[ $query_status -ne 0 ]]; then
1827
- err "Unable to query Orchestra session registry"
1828
- return 1
1829
- fi
1830
- [[ -n "$target" ]] || return 1
1831
- printf '%s\n' "$target"
1832
- }
1833
-
1834
- # Switch the current tmux client to an adjacent registered Orchestra session.
1835
- # Usage: tmux_cycle_workspace_session <current_session> <next|previous> [client_tty]
1836
- tmux_cycle_workspace_session() {
1837
- local current_session="$1"
1838
- local direction="$2"
1839
- local target_client="${3:-}"
1840
- local target_session
1841
-
1842
- if ! target_session="$(tmux_workspace_cycle_target "$current_session" "$direction")"; then
1843
- tmux display-message -d 2500 "No other Orchestra sessions in this workspace" 2>/dev/null || true
1844
- return 1
1845
- fi
1846
-
1847
- if [[ -n "$target_client" ]]; then
1848
- tmux switch-client -c "$target_client" -t "$target_session" >/dev/null 2>&1 || return 1
1849
- else
1850
- tmux switch-client -t "$target_session" >/dev/null 2>&1 || return 1
1851
- fi
1852
-
1853
- {
1854
- _tmux_refresh_orchestra_session_status "$target_session" >/dev/null 2>&1 || true
1855
- } &
1856
- }
1857
-
1858
- # Load .env file if it exists
1859
- # Usage: tmux_load_env_file [env_file_path]
1860
- tmux_load_env_file() {
1861
- local env_file="${1:-$PWD/.env}"
1862
- if [[ -f "$env_file" ]]; then
1863
- # Source the .env file, but only export ANTHROPIC_API_KEY
1864
- set -a # Auto-export variables
1865
- source "$env_file"
1866
- set +a # Turn off auto-export
1867
- fi
1868
- }
1869
-
1870
- # Load Anthropic API key from config file or fallback to .env files
1871
- tmux_load_anthropic_api_key() {
1872
- # First try to load from ~/.orchestra/config.json
1873
- local config_file="$HOME/.orchestra/config.json"
1874
- if [[ -f "$config_file" ]] && command -v jq >/dev/null 2>&1; then
1875
- local api_key
1876
- api_key="$(jq -r '.anthropic_api_key // empty' "$config_file" 2>/dev/null)"
1877
- if [[ -n "$api_key" ]] && [[ "$api_key" != "null" ]]; then
1878
- export ANTHROPIC_API_KEY="$api_key"
1879
- return 0
1880
- fi
1881
- fi
1882
-
1883
- # Fallback to .env file loading (existing logic)
1884
- tmux_load_env_file "$PWD/.env"
1885
-
1886
- # If still no API key, try repo root
1887
- if [[ -z "${ANTHROPIC_API_KEY-}" ]]; then
1888
- local root
1889
- root="$(repo_root)"
1890
- if [[ -n "$root" ]]; then
1891
- tmux_load_env_file "$root/.env"
1892
- fi
1893
- fi
1894
- }
1895
-
1896
- # Load OpenAI API key from config file or fallback to .env files
1897
- tmux_load_openai_api_key() {
1898
- local config_file="$HOME/.orchestra/config.json"
1899
- if [[ -f "$config_file" ]]; then
1900
- local api_key=""
1901
- if command -v jq >/dev/null 2>&1; then
1902
- api_key="$(jq -r '.openai_api_key // empty' "$config_file" 2>/dev/null)"
1903
- elif command -v python3 >/dev/null 2>&1; then
1904
- api_key="$(python3 - "$config_file" <<'PY'
1905
- import json
1906
- import sys
1907
- try:
1908
- data = json.load(open(sys.argv[1], 'r'))
1909
- except Exception:
1910
- data = {}
1911
- value = data.get('openai_api_key') or ''
1912
- print(value)
1913
- PY
1914
- )"
1915
- elif command -v node >/dev/null 2>&1; then
1916
- api_key="$(node - "$config_file" <<'NODE'
1917
- const fs = require('fs');
1918
- let value = '';
1919
- try {
1920
- const data = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
1921
- value = data.openai_api_key || '';
1922
- } catch (err) {
1923
- value = '';
1924
- }
1925
- process.stdout.write(value);
1926
- NODE
1927
- )"
1928
- fi
1929
- if [[ -n "$api_key" ]] && [[ "$api_key" != "null" ]]; then
1930
- export OPENAI_API_KEY="$api_key"
1931
- return 0
1932
- fi
1933
- fi
1934
-
1935
- tmux_load_env_file "$PWD/.env"
1936
- if [[ -z "${OPENAI_API_KEY-}" ]]; then
1937
- local root
1938
- root="$(repo_root)"
1939
- if [[ -n "$root" ]]; then
1940
- tmux_load_env_file "$root/.env"
1941
- fi
1942
- fi
1943
- }
1944
-
1945
- # Load primary AI provider from config (defaults to anthropic)
1946
- tmux_load_ai_primary_provider() {
1947
- if [[ -n "${AI_PRIMARY_PROVIDER-}" ]]; then
1948
- return 0
1949
- fi
1950
- local config_file="$HOME/.orchestra/config.json"
1951
- local provider=""
1952
- if [[ -f "$config_file" ]]; then
1953
- if command -v jq >/dev/null 2>&1; then
1954
- provider="$(jq -r '.ai_primary_provider // empty' "$config_file" 2>/dev/null)"
1955
- elif command -v python3 >/dev/null 2>&1; then
1956
- provider="$(python3 - "$config_file" <<'PY'
1957
- import json
1958
- import sys
1959
- try:
1960
- data = json.load(open(sys.argv[1], 'r'))
1961
- except Exception:
1962
- data = {}
1963
- value = data.get('ai_primary_provider') or ''
1964
- print(value)
1965
- PY
1966
- )"
1967
- elif command -v node >/dev/null 2>&1; then
1968
- provider="$(node - "$config_file" <<'NODE'
1969
- const fs = require('fs');
1970
- let value = '';
1971
- try {
1972
- const data = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
1973
- value = data.ai_primary_provider || '';
1974
- } catch (err) {
1975
- value = '';
1976
- }
1977
- process.stdout.write(value);
1978
- NODE
1979
- )"
1980
- fi
1981
- fi
1982
- provider="$(printf '%s' "$provider" | tr '[:upper:]' '[:lower:]')"
1983
- if [[ "$provider" != "openai" && "$provider" != "anthropic" ]]; then
1984
- provider="anthropic"
1985
- fi
1986
- export AI_PRIMARY_PROVIDER="$provider"
1987
- }
1988
-
1989
- # Load OpenAI model from config (defaults to gpt-4o-mini)
1990
- tmux_load_openai_model() {
1991
- if [[ -n "${OPENAI_MODEL-}" ]]; then
1992
- return 0
1993
- fi
1994
- local config_file="$HOME/.orchestra/config.json"
1995
- local model=""
1996
- if [[ -f "$config_file" ]]; then
1997
- if command -v jq >/dev/null 2>&1; then
1998
- model="$(jq -r '.openai_model // empty' "$config_file" 2>/dev/null)"
1999
- elif command -v python3 >/dev/null 2>&1; then
2000
- model="$(python3 - "$config_file" <<'PY'
2001
- import json
2002
- import sys
2003
- try:
2004
- data = json.load(open(sys.argv[1], 'r'))
2005
- except Exception:
2006
- data = {}
2007
- value = data.get('openai_model') or ''
2008
- print(value)
2009
- PY
2010
- )"
2011
- elif command -v node >/dev/null 2>&1; then
2012
- model="$(node - "$config_file" <<'NODE'
2013
- const fs = require('fs');
2014
- let value = '';
2015
- try {
2016
- const data = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
2017
- value = data.openai_model || '';
2018
- } catch (err) {
2019
- value = '';
2020
- }
2021
- process.stdout.write(value);
2022
- NODE
2023
- )"
2024
- fi
2025
- fi
2026
- if [[ -z "$model" || "$model" == "null" ]]; then
2027
- model="gpt-4o-mini"
2028
- fi
2029
- export OPENAI_MODEL="$model"
2030
- }
2031
-
2032
- # Generate a descriptive name for a tmux session using AI
2033
- # Usage: tmux_generate_ai_session_name <session_name>
2034
- tmux_generate_ai_session_name() {
2035
- local session="$1"
2036
-
2037
- # Load AI provider config and API keys
2038
- tmux_load_ai_primary_provider
2039
- tmux_load_openai_model
2040
- tmux_load_anthropic_api_key
2041
- tmux_load_openai_api_key
2042
-
2043
- local primary="${AI_PRIMARY_PROVIDER:-anthropic}"
2044
- primary="$(printf '%s' "$primary" | tr '[:upper:]' '[:lower:]')"
2045
- local provider=""
2046
- if [[ "$primary" == "openai" ]]; then
2047
- if [[ -n "${OPENAI_API_KEY-}" ]]; then
2048
- provider="openai"
2049
- elif [[ -n "${ANTHROPIC_API_KEY-}" ]]; then
2050
- provider="anthropic"
2051
- fi
2052
- else
2053
- if [[ -n "${ANTHROPIC_API_KEY-}" ]]; then
2054
- provider="anthropic"
2055
- elif [[ -n "${OPENAI_API_KEY-}" ]]; then
2056
- provider="openai"
2057
- fi
2058
- fi
2059
-
2060
- if [[ -z "$provider" ]]; then
2061
- err "No AI API key found in config file or .env file"
2062
- return 1
2063
- fi
2064
-
2065
- local openai_model="${OPENAI_MODEL:-gpt-4o-mini}"
2066
-
2067
- local pane_cmd pane_mode window_name pane_title alternate_on mouse_any_flag
2068
- pane_cmd="$(tmux display-message -t "$session" -p '#{pane_current_command}' 2>/dev/null || echo "")"
2069
- pane_mode="$(tmux display-message -t "$session" -p '#{pane_mode}' 2>/dev/null || echo "")"
2070
- window_name="$(tmux display-message -t "$session" -p '#{window_name}' 2>/dev/null || echo "")"
2071
- pane_title="$(tmux display-message -t "$session" -p '#{pane_title}' 2>/dev/null || echo "")"
2072
- alternate_on="$(tmux display-message -t "$session" -p '#{alternate_on}' 2>/dev/null || echo "0")"
2073
- mouse_any_flag="$(tmux display-message -t "$session" -p '#{mouse_any_flag}' 2>/dev/null || echo "0")"
2074
-
2075
- local history_temp=""
2076
- local history_arg=""
2077
- local history_dir="${ORCHESTRA_HISTORY_DIR:-$HOME/.orchestra/history}"
2078
- local pane_id
2079
- pane_id="$(tmux display-message -t "$session" -p '#{pane_id}' 2>/dev/null || echo "")"
2080
- local pane_key=""
2081
- if [[ -n "$pane_id" ]]; then
2082
- pane_key="$(_orchestra_history_key "$pane_id")"
2083
- fi
2084
- local session_key
2085
- session_key="$(_orchestra_history_key "$session")"
2086
-
2087
- local is_tui="false"
2088
- if _tmux_is_tui_context "$pane_cmd" "$alternate_on" "$mouse_any_flag" "$pane_mode" "$window_name" "$pane_title"; then
2089
- is_tui="true"
2090
- fi
2091
-
2092
- local content=""
2093
- local visible_content=""
2094
- if [[ "$is_tui" == "true" ]]; then
2095
- visible_content="$(tmux capture-pane -e -p -q -a -t "${pane_id:-$session}" 2>/dev/null || echo "")"
2096
- fi
2097
- local scrollback_content
2098
- scrollback_content="$(tmux capture-pane -t "$session" -p -S -200 2>/dev/null)" || {
2099
- err "Failed to capture tmux pane content"
2100
- return 1
2101
- }
2102
- if [[ -n "$visible_content" ]]; then
2103
- content="### Visible terminal view\n${visible_content}\n\n### Recent scrollback\n${scrollback_content}"
2104
- else
2105
- content="$scrollback_content"
2106
- fi
2107
-
2108
- local history_path=""
2109
- if [[ -n "$pane_key" && -f "$history_dir/$pane_key.log" ]]; then
2110
- history_path="$history_dir/$pane_key.log"
2111
- elif [[ -n "$session_key" && -f "$history_dir/$session_key.log" ]]; then
2112
- history_path="$history_dir/$session_key.log"
2113
- fi
2114
-
2115
- if [[ -n "$history_path" ]]; then
2116
- if [[ "$OSTYPE" == "darwin"* ]]; then
2117
- history_temp=$(mktemp -t gw_hist)
2118
- else
2119
- history_temp=$(mktemp)
2120
- fi
2121
- tail -n 50 "$history_path" > "$history_temp"
2122
- history_arg="$history_temp"
2123
- fi
2124
-
2125
- local metadata_block=""
2126
- metadata_block+="Session metadata:\n"
2127
- metadata_block+="- pane_current_command: ${pane_cmd:-unknown}\n"
2128
- metadata_block+="- window_name: ${window_name:-}\n"
2129
- metadata_block+="- pane_title: ${pane_title:-}\n"
2130
- metadata_block+="- pane_mode: ${pane_mode:-}\n"
2131
- metadata_block+="- alternate_screen_active: ${alternate_on:-0}\n"
2132
- metadata_block+="- mouse_mode_active: ${mouse_any_flag:-0}\n"
2133
- metadata_block+="- likely_tui_app: ${is_tui}\n"
2134
-
2135
- # If content is empty or too short, keep a placeholder
2136
- if [[ ${#content} -lt 10 ]]; then
2137
- content="(no terminal output captured)"
2138
- fi
2139
-
2140
- # Truncate content if too long (to stay within token limits)
2141
- if [[ ${#content} -gt 8000 ]]; then
2142
- content="${content: -8000}"
2143
- fi
2144
-
2145
- # Create a temporary file for the content to avoid escaping issues
2146
- # mktemp works differently on macOS vs Linux
2147
- local temp_file
2148
- if [[ "$OSTYPE" == "darwin"* ]]; then
2149
- temp_file=$(mktemp -t gw_tmp)
2150
- else
2151
- temp_file=$(mktemp)
2152
- fi
2153
- printf '%s' "$content" > "$temp_file"
2154
-
2155
- # Prepare the API request using Python for proper JSON encoding
2156
- local request_body=""
2157
- if have_cmd python3; then
2158
- # Use Python to safely build the request body and extract typed commands from the capture
2159
- request_payload=$(
2160
- python3 - "$temp_file" "$history_arg" "$provider" "$openai_model" "$pane_cmd" "$window_name" "$pane_title" "$pane_mode" "$alternate_on" "$mouse_any_flag" "$is_tui" 2>/dev/null <<'PYCODE'
2161
- import json
2162
- import re
2163
- import sys
2164
- from pathlib import Path
2165
-
2166
- temp_path = Path(sys.argv[1])
2167
- content = temp_path.read_text()
2168
- history_arg = sys.argv[2] if len(sys.argv) > 2 else ""
2169
- provider = sys.argv[3] if len(sys.argv) > 3 else "anthropic"
2170
- openai_model = sys.argv[4] if len(sys.argv) > 4 and sys.argv[4] else "gpt-4o-mini"
2171
- pane_cmd = sys.argv[5] if len(sys.argv) > 5 else ""
2172
- window_name = sys.argv[6] if len(sys.argv) > 6 else ""
2173
- pane_title = sys.argv[7] if len(sys.argv) > 7 else ""
2174
- pane_mode = sys.argv[8] if len(sys.argv) > 8 else ""
2175
- alternate_on = sys.argv[9] if len(sys.argv) > 9 else "0"
2176
- mouse_any_flag = sys.argv[10] if len(sys.argv) > 10 else "0"
2177
- is_tui = sys.argv[11] if len(sys.argv) > 11 else "false"
2178
-
2179
- lines = content.splitlines()
2180
- command_candidates = [] # Commands captured with high confidence
2181
- fallback_candidates = [] # All prompt-like lines (used if we fail to classify)
2182
-
2183
- history_commands = []
2184
- if history_arg:
2185
- hist_path = Path(history_arg)
2186
- if hist_path.exists():
2187
- for line in hist_path.read_text().splitlines():
2188
- line = line.strip()
2189
- if not line:
2190
- continue
2191
- if "\t" in line:
2192
- history_commands.append(line.split("\t", 1)[1])
2193
- else:
2194
- history_commands.append(line)
2195
- history_commands = history_commands[-50:]
2196
-
2197
- prompt_pattern = re.compile(r"^[A-Za-z0-9_.@~/-]+$")
2198
- prompt_sigil_pattern = re.compile(r"^(?P<prompt>[^\t]{0,120}?)(?P<sigil>[$#%])\s+(?P<cmd>.+)$")
2199
- simple_sigil_pattern = re.compile(r"^(?P<sigil>[$#%])\s+(?P<cmd>.+)$")
2200
- disallowed_prompts = {
2201
- "warning",
2202
- "error",
2203
- "fatal",
2204
- "hint",
2205
- "note",
2206
- "usage",
2207
- "info",
2208
- "debug",
2209
- "trace",
2210
- }
2211
-
2212
- disallowed_first_tokens = {
2213
- "warning",
2214
- "error",
2215
- "fatal",
2216
- "hint",
2217
- "note",
2218
- "usage",
2219
- "not",
2220
- "see",
2221
- "for",
2222
- "from",
2223
- "with",
2224
- "and",
2225
- "or",
2226
- "but",
2227
- "at",
2228
- "to",
2229
- "in",
2230
- "info",
2231
- "debug",
2232
- "trace",
2233
- }
2234
-
2235
- common_commands = {
2236
- "git",
2237
- "gh",
2238
- "npm",
2239
- "pnpm",
2240
- "yarn",
2241
- "node",
2242
- "npx",
2243
- "bun",
2244
- "cargo",
2245
- "go",
2246
- "python",
2247
- "python3",
2248
- "pip",
2249
- "pip3",
2250
- "poetry",
2251
- "pipenv",
2252
- "pytest",
2253
- "uvicorn",
2254
- "gunicorn",
2255
- "flask",
2256
- "django-admin",
2257
- "rails",
2258
- "bundle",
2259
- "rake",
2260
- "mix",
2261
- "make",
2262
- "cmake",
2263
- "gradle",
2264
- "mvn",
2265
- "ant",
2266
- "docker",
2267
- "docker-compose",
2268
- "kubectl",
2269
- "helm",
2270
- "terraform",
2271
- "ansible",
2272
- "ssh",
2273
- "scp",
2274
- "rsync",
2275
- "sftp",
2276
- "psql",
2277
- "mysql",
2278
- "mongo",
2279
- "redis-cli",
2280
- "sqlite3",
2281
- "composer",
2282
- "php",
2283
- "ruby",
2284
- "java",
2285
- "javac",
2286
- "deno",
2287
- "dotnet",
2288
- "msbuild",
2289
- "tsc",
2290
- "nx",
2291
- "lerna",
2292
- "eslint",
2293
- "prettier",
2294
- "ls",
2295
- "cd",
2296
- "pwd",
2297
- "cat",
2298
- "tail",
2299
- "head",
2300
- "less",
2301
- "more",
2302
- "grep",
2303
- "rg",
2304
- "fd",
2305
- "find",
2306
- "watch",
2307
- "code",
2308
- "open",
2309
- "vim",
2310
- "nvim",
2311
- "tmux",
2312
- "htop",
2313
- "top",
2314
- "brew",
2315
- "tox",
2316
- }
2317
-
2318
- allowed_prefixes = (
2319
- "./",
2320
- "../",
2321
- "~/",
2322
- "bin/",
2323
- "sbin/",
2324
- )
2325
-
2326
-
2327
- def clean_tokens(tokens):
2328
- while tokens and tokens[0] in {"$", "#", "%"}:
2329
- tokens = tokens[1:]
2330
- while tokens and (tokens[0].endswith("$") or tokens[0].endswith("#") or tokens[0].endswith("%")):
2331
- tokens = tokens[1:]
2332
- return tokens
2333
-
2334
-
2335
- def extract_prompt_and_command(raw_line):
2336
- stripped = raw_line.strip()
2337
- if not stripped:
2338
- return "", ""
2339
-
2340
- # Pattern 1: host:path:command (common bash/zsh prompts)
2341
- if ":" in stripped:
2342
- maybe_prompt, maybe_cmd = stripped.split(":", 1)
2343
- maybe_prompt = maybe_prompt.strip().rstrip("#$%")
2344
- maybe_cmd = maybe_cmd.strip()
2345
- if maybe_prompt and maybe_cmd and prompt_pattern.match(maybe_prompt):
2346
- prompt_lower = maybe_prompt.lower()
2347
- if prompt_lower not in disallowed_prompts:
2348
- return maybe_prompt, maybe_cmd
2349
-
2350
- # Pattern 2: prompt ending with $, #, % (supports prompts without ':')
2351
- match = prompt_sigil_pattern.match(stripped)
2352
- if match:
2353
- maybe_prompt = (match.group("prompt") or "").strip().rstrip("#$%")
2354
- maybe_cmd = (match.group("cmd") or "").strip()
2355
- if maybe_cmd:
2356
- if maybe_prompt and maybe_prompt.lower() in disallowed_prompts:
2357
- return "", ""
2358
- return (maybe_prompt if maybe_prompt else match.group("sigil")), maybe_cmd
2359
-
2360
- # Pattern 3: bare prompt lines like "$ git status"
2361
- match = simple_sigil_pattern.match(stripped)
2362
- if match:
2363
- maybe_cmd = (match.group("cmd") or "").strip()
2364
- if maybe_cmd:
2365
- return match.group("sigil"), maybe_cmd
2366
-
2367
- return "", ""
2368
-
2369
-
2370
- for raw_line in lines:
2371
- prompt_part, command_part = extract_prompt_and_command(raw_line)
2372
- if not command_part:
2373
- continue
2374
-
2375
- tokens = clean_tokens(command_part.split())
2376
- if not tokens:
2377
- continue
2378
-
2379
- first_token = tokens[0].lower()
2380
- if first_token == "sudo" and len(tokens) > 1:
2381
- first_token = tokens[1].lower()
2382
-
2383
- if first_token in disallowed_first_tokens:
2384
- continue
2385
-
2386
- normalized_command = " ".join(tokens)
2387
- formatted_line = f"{prompt_part}:{normalized_command}"
2388
- fallback_candidates.append(formatted_line)
2389
-
2390
- allowed = (
2391
- first_token in common_commands
2392
- or normalized_command.startswith(allowed_prefixes)
2393
- or any(
2394
- first_token.startswith(prefix)
2395
- for prefix in (
2396
- "git",
2397
- "npm",
2398
- "pnpm",
2399
- "yarn",
2400
- "node",
2401
- "npx",
2402
- "bun",
2403
- "cargo",
2404
- "python",
2405
- "pip",
2406
- "poetry",
2407
- "pytest",
2408
- "uvicorn",
2409
- "docker",
2410
- "kubectl",
2411
- "helm",
2412
- "terraform",
2413
- "ansible",
2414
- "ssh",
2415
- "scp",
2416
- "rsync",
2417
- "rails",
2418
- "bundle",
2419
- "rake",
2420
- "mix",
2421
- "psql",
2422
- "mysql",
2423
- "mongo",
2424
- "redis",
2425
- )
2426
- )
2427
- )
2428
-
2429
- if allowed:
2430
- command_candidates.append(formatted_line)
2431
-
2432
-
2433
- if history_commands:
2434
- command_candidates = history_commands
2435
- elif not command_candidates:
2436
- command_candidates = fallback_candidates
2437
-
2438
- extracted_commands = command_candidates
2439
- history_lines = [f"{idx + 1}. {line}" for idx, line in enumerate(extracted_commands)]
2440
- command_history = "\n".join(history_lines) if history_lines else "None detected."
2441
- last_command_line = extracted_commands[-1] if extracted_commands else ""
2442
- commands_detected = bool(extracted_commands)
2443
-
2444
- # Capture the first 10 lines of output as fallback context (joined by newline)
2445
- first_ten_lines = "\n".join(lines[:10]) if lines else ""
2446
-
2447
- description_guidance = """Description rules:
2448
- - Describe the primary activity in a short phrase (3-8 words).
2449
- - Do NOT include any app/tool name or prefix in the output.
2450
- - If git activity is visible, the description should reflect it (e.g., reviewing changes), but do not add 'git_'."""
2451
-
2452
- metadata_summary = "\n".join(
2453
- line
2454
- for line in [
2455
- f"pane_current_command: {pane_cmd}" if pane_cmd else "",
2456
- f"window_name: {window_name}" if window_name else "",
2457
- f"pane_title: {pane_title}" if pane_title else "",
2458
- f"pane_mode: {pane_mode}" if pane_mode else "",
2459
- f"alternate_screen_active: {alternate_on}",
2460
- f"mouse_mode_active: {mouse_any_flag}",
2461
- f"likely_tui_app: {is_tui}",
2462
- ]
2463
- if line
2464
- )
2465
-
2466
- prompt = f"""Analyze this terminal session and create a descriptive tmux session name based on what the user actually did.
2467
-
2468
- Terminal output (last capture):
2469
- {content}
2470
-
2471
- Tmux metadata (use as supporting hints, not as the primary source of truth):
2472
- {metadata_summary}
2473
-
2474
- Extracted command history (oldest to newest):
2475
- {command_history}
2476
-
2477
- Most recent command (anchor for description and summary):
2478
- {last_command_line if last_command_line else 'None detected'}
2479
-
2480
- Focus: produce a concise description of the activity. Follow these rules:
2481
- {description_guidance}
2482
-
2483
- {'No clear prompt/command lines were detected. Rely on the terminal output context.' if not commands_detected else ''}
2484
-
2485
- {'An interactive TUI is likely running. Prefer the visible screen content plus recent command history over generic app names.' if is_tui.lower() in {'1', 'true', 'yes', 'on'} else ''}
2486
-
2487
- {'First 10 lines of terminal output (useful for identifying running app/service):\n' + first_ten_lines if not commands_detected and first_ten_lines else ''}
2488
-
2489
- If no commands were detected, fall back to terminal output analysis but describe the main activity in the name. Identify the dominant application or process implied by the output (e.g., web server, test runner, build tool).
2490
-
2491
- Instructions:
2492
- 1. Produce a session name using lowercase letters, numbers, and underscores only.
2493
- 2. Maximum length: 100 characters.
2494
- 3. Do NOT include any app/tool name or prefix.
2495
- 4. Describe the concrete task or state in a short phrase.
2496
- 5. If git activity is visible anywhere, the description must reflect git activity.
2497
- 6. Respond with ONLY the final session name, nothing else."""
2498
-
2499
- if provider == "openai":
2500
- request = {
2501
- "model": openai_model,
2502
- "max_tokens": 100,
2503
- "temperature": 0.2,
2504
- "messages": [
2505
- {
2506
- "role": "user",
2507
- "content": prompt
2508
- }
2509
- ]
2510
- }
2511
- else:
2512
- request = {
2513
- "model": "claude-3-5-haiku-latest",
2514
- "max_tokens": 100,
2515
- "messages": [
2516
- {
2517
- "role": "user",
2518
- "content": prompt
2519
- }
2520
- ]
2521
- }
2522
-
2523
- print(json.dumps(request))
2524
- PYCODE
2525
- )
2526
- request_body="$request_payload"
2527
- fi
2528
- if [[ -z "$request_body" ]]; then
2529
- # Fallback: base64 encode the content to avoid escaping issues
2530
- local encoded_content
2531
- local fallback_file
2532
- if [[ "$OSTYPE" == "darwin"* ]]; then
2533
- fallback_file=$(mktemp -t gw_fb)
2534
- else
2535
- fallback_file=$(mktemp)
2536
- fi
2537
- {
2538
- printf '%b\n\n' "$metadata_block"
2539
- cat "$temp_file"
2540
- } > "$fallback_file"
2541
- encoded_content=$(base64 < "$fallback_file" | tr -d '\n')
2542
- rm -f "$fallback_file"
2543
- if [[ "$provider" == "openai" ]]; then
2544
- request_body=$(cat <<EOF
2545
- {
2546
- "model": "$openai_model",
2547
- "max_tokens": 100,
2548
- "temperature": 0.2,
2549
- "messages": [
2550
- {
2551
- "role": "user",
2552
- "content": "I'll send you base64-encoded terminal output. Please decode it, analyze it, and create a descriptive name for this tmux session.\n\nIMPORTANT: Use terminal metadata only as supporting hints. Prioritize the visible terminal content and recent command history.\n\nCRITICAL: Check for Git patterns FIRST:\n- Git commands: git status, diff, add, commit, push, pull, checkout, branch, merge, rebase, log, stash\n- Git output: 'On branch', 'Changes not staged', 'modified:', 'new file:', diff output (+/-/@@), commit hashes\n- If ANY Git patterns found, the description should reflect git activity (but do not add 'git_').\n\nDESCRIPTION ONLY: Return a short description of the activity.\n- Do NOT include the app/tool name or any prefix in the output.\n\nUse underscores only. Max 100 chars total.\n\nExamples:\n- reviewing_changes_before_commit\n- resolving_merge_conflicts\n- fixing_auth_bug\n- running_nextjs_dev_server\n\nBase64 terminal output:\n${encoded_content}\n\nRespond with ONLY the session name, nothing else."
2553
- }
2554
- ]
2555
- }
2556
- EOF
2557
- )
2558
- else
2559
- request_body=$(cat <<EOF
2560
- {
2561
- "model": "claude-3-5-haiku-latest",
2562
- "max_tokens": 100,
2563
- "messages": [
2564
- {
2565
- "role": "user",
2566
- "content": "I'll send you base64-encoded terminal output. Please decode it, analyze it, and create a descriptive name for this tmux session.\n\nIMPORTANT: Use terminal metadata only as supporting hints. Prioritize the visible terminal content and recent command history.\n\nCRITICAL: Check for Git patterns FIRST:\n- Git commands: git status, diff, add, commit, push, pull, checkout, branch, merge, rebase, log, stash\n- Git output: 'On branch', 'Changes not staged', 'modified:', 'new file:', diff output (+/-/@@), commit hashes\n- If ANY Git patterns found, the description should reflect git activity (but do not add 'git_').\n\nDESCRIPTION ONLY: Return a short description of the activity.\n- Do NOT include the app/tool name or any prefix in the output.\n\nUse underscores only. Max 100 chars total.\n\nExamples:\n- reviewing_changes_before_commit\n- resolving_merge_conflicts\n- fixing_auth_bug\n- running_nextjs_dev_server\n\nBase64 terminal output:\n${encoded_content}\n\nRespond with ONLY the session name, nothing else."
2567
- }
2568
- ]
2569
- }
2570
- EOF
2571
- )
2572
- fi
2573
- fi
2574
-
2575
- # Make the API call
2576
- local response
2577
- local new_name=""
2578
- if [[ "$provider" == "openai" ]]; then
2579
- # First attempt: OpenAI Responses API + Files API for structured app/description output.
2580
- # If this path fails, we fall back to chat completions.
2581
- local context_file=""
2582
- if [[ "$OSTYPE" == "darwin"* ]]; then
2583
- context_file=$(mktemp -t gw_ctx)
2584
- else
2585
- context_file=$(mktemp)
2586
- fi
2587
- {
2588
- printf '%b\n\n' "$metadata_block"
2589
- printf 'TMUX SESSION CAPTURE\n\n'
2590
- cat "$temp_file"
2591
- if [[ -n "$history_arg" && -f "$history_arg" ]]; then
2592
- printf '\n\nRECENT COMMAND HISTORY\n\n'
2593
- cat "$history_arg"
2594
- fi
2595
- } > "$context_file"
2596
-
2597
- local file_upload_resp openai_file_id
2598
- file_upload_resp=$(curl -s -X POST https://api.openai.com/v1/files \
2599
- --max-time 20 \
2600
- -H "Authorization: Bearer $OPENAI_API_KEY" \
2601
- -F purpose="user_data" \
2602
- -F "file=@$context_file;type=text/plain" 2>/dev/null)
2603
- rm -f "$context_file"
2604
-
2605
- openai_file_id=""
2606
- if have_cmd python3; then
2607
- openai_file_id="$(python3 -c 'import json,sys
2608
- try:
2609
- data = json.load(sys.stdin)
2610
- except Exception:
2611
- data = {}
2612
- print(data.get("id", ""))' <<<"$file_upload_resp" 2>/dev/null || true)"
2613
- fi
2614
-
2615
- if [[ -n "$openai_file_id" ]]; then
2616
- local responses_body responses_resp ai_desc
2617
- responses_body=$(python3 - "$openai_model" "$openai_file_id" <<'PYCODE'
2618
- import json
2619
- import sys
2620
-
2621
- model = sys.argv[1]
2622
- file_id = sys.argv[2]
2623
-
2624
- prompt = """Analyze the provided terminal session file and produce ONLY a short activity description.
2625
-
2626
- Return STRICT JSON in this exact shape:
2627
- {"description":"<short_snake_case_description>"}
2628
-
2629
- Rules:
2630
- - description: concise snake_case description of the activity
2631
- - do NOT include app/tool name or any prefix in the description
2632
- - do not include punctuation beyond underscores
2633
- - keep it short and specific
2634
- """
2635
-
2636
- req = {
2637
- "model": model,
2638
- "temperature": 0.2,
2639
- "max_output_tokens": 180,
2640
- "input": [
2641
- {
2642
- "role": "user",
2643
- "content": [
2644
- {"type": "input_text", "text": prompt},
2645
- {"type": "input_file", "file_id": file_id},
2646
- ],
2647
- }
2648
- ],
2649
- }
2650
- print(json.dumps(req))
2651
- PYCODE
2652
- )
2653
-
2654
- responses_resp=$(curl -s -X POST https://api.openai.com/v1/responses \
2655
- --max-time 20 \
2656
- -H "Content-Type: application/json" \
2657
- -H "Authorization: Bearer $OPENAI_API_KEY" \
2658
- -d "$responses_body" 2>/dev/null)
2659
-
2660
- # Best-effort cleanup of uploaded file
2661
- curl -s -X DELETE "https://api.openai.com/v1/files/$openai_file_id" \
2662
- -H "Authorization: Bearer $OPENAI_API_KEY" >/dev/null 2>&1 || true
2663
-
2664
- ai_desc="$(python3 -c 'import json
2665
- import re
2666
- import sys
2667
-
2668
- def clean(value):
2669
- value = (value or "").strip().lower()
2670
- value = re.sub(r"[^a-z0-9_ -]+", "", value)
2671
- value = value.replace(" ", "_")
2672
- value = re.sub(r"_+", "_", value).strip("_")
2673
- return value
2674
-
2675
- try:
2676
- data = json.load(sys.stdin)
2677
- except Exception:
2678
- print("")
2679
- raise SystemExit
2680
-
2681
- text = ""
2682
- if isinstance(data.get("output_text"), str):
2683
- text = data["output_text"]
2684
-
2685
- if not text:
2686
- for item in data.get("output", []):
2687
- if not isinstance(item, dict):
2688
- continue
2689
- if item.get("type") != "message":
2690
- continue
2691
- for content in item.get("content", []):
2692
- if not isinstance(content, dict):
2693
- continue
2694
- if isinstance(content.get("text"), str):
2695
- text += content["text"]
2696
-
2697
- obj = None
2698
- if text:
2699
- text = text.strip()
2700
- try:
2701
- obj = json.loads(text)
2702
- except Exception:
2703
- match = re.search(r"\{.*\}", text, re.S)
2704
- if match:
2705
- try:
2706
- obj = json.loads(match.group(0))
2707
- except Exception:
2708
- obj = None
2709
-
2710
- description = ""
2711
- if isinstance(obj, dict):
2712
- description = clean(str(obj.get("description", "")))
2713
-
2714
- if not description and text:
2715
- description = clean(text)
2716
-
2717
- print(description)' <<<"$responses_resp" 2>/dev/null || true)"
2718
-
2719
- if [[ -n "$ai_desc" ]]; then
2720
- new_name="$ai_desc"
2721
- fi
2722
- fi
2723
-
2724
- if [[ -z "$new_name" ]]; then
2725
- # Fallback: existing chat completions path
2726
- response=$(curl -s -X POST https://api.openai.com/v1/chat/completions \
2727
- --max-time 20 \
2728
- -H "Content-Type: application/json" \
2729
- -H "Authorization: Bearer $OPENAI_API_KEY" \
2730
- -d "$request_body" 2>&1) || {
2731
- err "Failed to call OpenAI API: $response"
2732
- return 1
2733
- }
2734
-
2735
- if echo "$response" | grep -q '"error"'; then
2736
- local error_msg=$(echo "$response" | python3 -c "import sys, json; print(json.load(sys.stdin).get('error', {}).get('message', 'Unknown error'))" 2>/dev/null || echo "API error")
2737
- err "API error: $error_msg"
2738
- return 1
2739
- fi
2740
-
2741
- new_name=$(echo "$response" | python3 -c "import sys, json; data = json.load(sys.stdin); print(data.get('choices', [{}])[0].get('message', {}).get('content', ''))" 2>/dev/null) || {
2742
- new_name=$(echo "$response" | grep -o '"content":"[^"]*"' | head -1 | sed 's/"content":"\([^"]*\)"/\1/')
2743
- }
2744
- fi
2745
- else
2746
- response=$(curl -s -X POST https://api.anthropic.com/v1/messages \
2747
- --max-time 20 \
2748
- -H "Content-Type: application/json" \
2749
- -H "x-api-key: $ANTHROPIC_API_KEY" \
2750
- -H "anthropic-version: 2023-06-01" \
2751
- -d "$request_body" 2>&1) || {
2752
- err "Failed to call Anthropic API: $response"
2753
- return 1
2754
- }
2755
-
2756
- if echo "$response" | grep -q '"error"'; then
2757
- local error_msg=$(echo "$response" | python3 -c "import sys, json; print(json.load(sys.stdin).get('error', {}).get('message', 'Unknown error'))" 2>/dev/null || echo "API error")
2758
- err "API error: $error_msg"
2759
- return 1
2760
- fi
2761
-
2762
- new_name=$(echo "$response" | python3 -c "import sys, json; data = json.load(sys.stdin); print(data.get('content', [{}])[0].get('text', ''))" 2>/dev/null) || {
2763
- new_name=$(echo "$response" | grep -o '"text":"[^"]*"' | head -1 | sed 's/"text":"\([^"]*\)"/\1/')
2764
- }
2765
- fi
2766
-
2767
- # Clean up temp files
2768
- rm -f "$temp_file"
2769
- if [[ -n "$history_temp" ]]; then
2770
- rm -f "$history_temp"
2771
- fi
2772
-
2773
- # Clean up the AI description (remove any spaces, special chars except underscore and dash)
2774
- new_name=$(echo "$new_name" | tr ' ' '_' | sed 's/[^a-zA-Z0-9_-]//g' | cut -c1-100)
2775
- if [[ -z "$new_name" ]]; then
2776
- err "Failed to generate name from AI response"
2777
- return 1
2778
- fi
2779
-
2780
- new_name="${new_name:0:100}"
2781
-
2782
- echo "$new_name"
2783
- }
2784
-
2785
- # Display tmux message
2786
- # Usage: tmux_display_message <message> [duration_ms]
2787
- tmux_display_message() {
2788
- local message="$1"
2789
- local duration="${2:-3000}"
2790
-
2791
- if tmux_available && tmux_inside_session; then
2792
- tmux display-message -d "$duration" "$message"
2793
- fi
2794
- }
5
+ _ORCHESTRA_API_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6
+ # shellcheck source=/dev/null
7
+ source "$_ORCHESTRA_API_DIR/../server/session/tmux_api.sh"