-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.sh
More file actions
executable file
·471 lines (429 loc) · 12.6 KB
/
Copy pathlayout.sh
File metadata and controls
executable file
·471 lines (429 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#!/usr/bin/env bash
# Agentic three-column Herdr layout: agent | review/shell | sidebar.
# shellcheck disable=SC1091
set -euo pipefail
HERDR="${HERDR_BIN_PATH:-herdr}"
METADATA_SOURCE="agentic-dev.layout"
_plugin_root() {
if [[ -n "${HERDR_PLUGIN_ROOT:-}" ]]; then
printf '%s' "$HERDR_PLUGIN_ROOT"
else
cd "$(dirname "${BASH_SOURCE[0]}")" && pwd
fi
}
PLUGIN_ROOT="$(_plugin_root)"
_layout_core() {
local bin="${PLUGIN_ROOT}/target/release/herdr-layout"
[[ -x "$bin" ]] || bin="${PLUGIN_ROOT}/target/debug/herdr-layout"
if [[ ! -x "$bin" ]]; then
printf 'agentic-layout: herdr-layout binary missing (cargo build --release -p herdr-sidebar)\n' >&2
return 127
fi
"$bin" "$@"
}
_herdr_json() {
"$HERDR" "$@" 2>/dev/null
}
_jq() {
jq -r "$@"
}
# shellcheck source=config-reader.sh
source "$PLUGIN_ROOT/config-reader.sh"
# shellcheck source=state.sh
source "$PLUGIN_ROOT/state.sh"
_agent_cmd() {
if declare -F agentic_dev_agent_command >/dev/null 2>&1; then
agentic_dev_agent_command
else
printf '%s' "cursor-agent"
fi
}
_file_editor() {
printf '%s' "fresh"
}
# Column shares of the full tab: agent 2/6, center 3/6 (biggest), sidebar 1/6.
_agent_ratio() {
if declare -F agentic_dev_layout_agent_ratio >/dev/null 2>&1; then
agentic_dev_layout_agent_ratio
else
printf '%s' "0.333333"
fi
}
_sidebar_ratio() {
if declare -F agentic_dev_layout_sidebar_ratio >/dev/null 2>&1; then
agentic_dev_layout_sidebar_ratio
else
printf '%s' "0.166667"
fi
}
_layout_geometry() {
jq -n --argjson agent "$(_agent_ratio)" --argjson sidebar "$(_sidebar_ratio)" \
'{agent_ratio:$agent, sidebar_ratio:$sidebar}' | _layout_core --split-ratios
}
_agent_split_ratio() {
_layout_geometry | jq -r '.agent_split'
}
_sidebar_split_ratio() {
_layout_geometry | jq -r '.sidebar_split'
}
_agent_move_ratio() {
_layout_geometry | jq -r '.agent_move'
}
_id_after_move() {
local json="$1" fallback="$2" id
id="$(printf '%s' "${json:-{}}" | _jq '.result.move_result.pane.pane_id // .result.pane.pane_id // empty' 2>/dev/null || true)"
if [[ -n "$id" && "$id" != "null" ]]; then
printf '%s' "$id"
else
printf '%s' "$fallback"
fi
}
# Herdr plugin events wrap the payload in `.data` (0.8+). Older builds used a
# top-level field; keep that one fallback.
_event_id() {
local field="$1"
printf '%s' "${HERDR_PLUGIN_EVENT_JSON:-{}}" | jq -r --arg f "$field" \
'.data[$f] // .[$f] // empty' \
2>/dev/null || true
}
_review_cmd() {
if declare -F agentic_dev_layout_review >/dev/null 2>&1; then
agentic_dev_layout_review
else
printf '%s' "hunk diff"
fi
}
# On-demand Review is a hunk session. Always watch so comments stream.
_review_launch() {
local cmd
cmd="$(_review_cmd)"
case "$cmd" in
hunk|"hunk diff")
printf '%s' "hunk diff --watch"
;;
hunk\ diff*)
[[ "$cmd" == *"--watch"* ]] && printf '%s' "$cmd" || printf '%s' "$cmd --watch"
;;
*)
printf '%s' "$cmd"
;;
esac
}
_sidebar_bin() {
local root="$PLUGIN_ROOT" candidate
for candidate in "$root/target/release/herdr-sidebar" "$root/bin/herdr-sidebar"; do
if [[ -x "$candidate" ]]; then
printf '%s' "$candidate"
return 0
fi
done
printf '%s' "$root/target/release/herdr-sidebar"
}
_login_shell() {
local shell="${SHELL:-}" resolved
if [[ -n "$shell" && -x "$shell" ]]; then
printf '%s' "$shell"
return 0
fi
if [[ -n "$shell" ]]; then
resolved="$(command -v "$shell" 2>/dev/null || true)"
if [[ -n "$resolved" && -x "$resolved" ]]; then
printf '%s' "$resolved"
return 0
fi
fi
if [[ "$(uname -s)" == Darwin && -x /bin/zsh ]]; then
printf '%s' "/bin/zsh"
return 0
fi
printf '%s' "bash"
}
# shellcheck source=lifecycle.sh
source "$PLUGIN_ROOT/lifecycle.sh"
# shellcheck source=topology.sh
source "$PLUGIN_ROOT/topology.sh"
_dev_workspace_id() {
if [[ -n "${HERDR_WORKSPACE_ID:-}" ]]; then
printf '%s' "$HERDR_WORKSPACE_ID"
return 0
fi
_focused_workspace_id
}
_dev_state() {
local workspace_id
workspace_id="$(_dev_workspace_id)"
[[ -n "$workspace_id" ]] || return 1
_state_load "$workspace_id"
}
_select_center() {
local view="$1" state
state="$(_dev_state)" || state="$(_layout_ensure)"
if [[ -z "$state" ]]; then
return 0
fi
if [[ "$view" == review ]]; then
_open_review || return 1
fi
_activate_center_view "$view"
}
_focus_agent() {
local state agent_pane view
state="$(_dev_state)" || state="$(_layout_ensure)"
[[ -n "$state" ]] || return 0
view="$(printf '%s' "$state" | _jq '.active_center_view // "shell"')"
_activate_center_view "$view"
agent_pane="$(printf '%s' "$state" | _jq '.agent_pane_id // empty')"
if ! _pane_exists "$agent_pane"; then
state="$(_layout_ensure)"
agent_pane="$(printf '%s' "$state" | _jq '.agent_pane_id // empty')"
fi
if _pane_exists "$agent_pane" && _pane_is_shell "$agent_pane"; then
_launch_agent_on_pane "$agent_pane" || true
fi
if _pane_exists "$agent_pane"; then
_herdr_json agent focus "$agent_pane" >/dev/null 2>&1 \
|| _herdr_json pane focus "$agent_pane" >/dev/null 2>&1 \
|| true
fi
}
_refresh_review() {
local state review
_select_center review
state="$(_dev_state)" || return 0
review="$(printf '%s' "$state" | _jq '.review_pane_id // empty')"
[[ -n "$review" ]] && _pane_exists "$review" || return 0
_restart_pane_cmd "$review" "$(_review_launch)"
}
_toggle_sidebar() {
local state sidebar
state="$(_dev_state)" || return 0
sidebar="$(printf '%s' "$state" | _jq '.sidebar_pane_id // empty')"
[[ -n "$sidebar" ]] || return 0
_activate_center_view "$(printf '%s' "$state" | _jq '.active_center_view // "shell"')"
_herdr_json pane zoom "$sidebar" --toggle >/dev/null 2>&1 || true
}
_sidebar_send_key() {
local key="$1" state sidebar
state="$(_dev_state)" || return 0
sidebar="$(printf '%s' "$state" | _jq '.sidebar_pane_id // empty')"
[[ -n "$sidebar" ]] || return 0
_activate_center_view "$(printf '%s' "$state" | _jq '.active_center_view // "shell"')"
_herdr_json pane send-keys "$sidebar" "$key" >/dev/null 2>&1 || true
}
_select_sidebar_view() {
local view="$1" key="$2" workspace_id
_sidebar_send_key "$key"
workspace_id="$(_dev_workspace_id)"
[[ -n "$workspace_id" ]] || return 0
_state_update "$workspace_id" --arg view "$view" '.active_sidebar_view = $view'
}
_select_files() {
_select_sidebar_view files 1
}
_select_source_control() {
_select_sidebar_view source_control 2
}
_abs_path() {
local path="$1"
if [[ "$path" != /* ]]; then
path="$(cd "$(dirname "$path")" 2>/dev/null && pwd)/$(basename "$path")"
fi
printf '%s' "$path"
}
_editor_launch_cmd() {
local path="$1"
printf '%s %s' "$(_file_editor)" "$(printf '%q' "$path")"
}
_open_editor() {
local path="${1:-${AGENTIC_OPEN_PATH:-}}"
local state workspace_id pane tab_id tab_label existing cwd
[[ -n "$path" ]] || { echo "agentic-layout: open-editor requires a path" >&2; return 1; }
path="$(_abs_path "$path")"
[[ -e "$path" ]] || { echo "agentic-layout: open-editor path not found: $path" >&2; return 1; }
state="$(_dev_state)" || state="$(_layout_ensure)"
workspace_id="$(printf '%s' "$state" | _jq '.workspace_id')"
existing="$(printf '%s' "$state" | jq -r --arg path "$path" '.editors[$path].pane_id // empty')"
if [[ -n "$existing" ]] && _pane_exists "$existing"; then
tab_id="$(printf '%s' "$state" | jq -r --arg path "$path" '.editors[$path].tab_id // empty')"
[[ -n "$tab_id" ]] && _activate_tab "$tab_id"
return 0
fi
cwd="$(dirname "$path")"
tab_label="$(basename "$path")"
tab_id="$(_herdr_json tab create --workspace "$workspace_id" --label "$tab_label" --cwd "$cwd" --no-focus \
| _jq '.result.tab.tab_id')"
[[ -n "$tab_id" && "$tab_id" != "null" ]] || {
echo "agentic-layout: failed to create editor tab for $path" >&2
return 1
}
pane="$(_pane_on_tab "$workspace_id" "$tab_id")"
[[ -n "$pane" ]] || {
echo "agentic-layout: editor tab $tab_id has no pane" >&2
return 1
}
_pane_run_login "$pane" "$(_editor_launch_cmd "$path")"
_stamp_metadata "$pane" editor "agentic_path=$(basename "$path")"
_rename_pane "$pane" editor
_state_update "$workspace_id" --arg path "$path" --arg tab "$tab_id" --arg pane "$pane" \
'.editors[$path] = {tab_id: $tab, pane_id: $pane}'
_activate_tab "$tab_id"
}
_on_pane_exited() {
local pane_id="${1:-}" path workspace_id close_tab close_ws
[[ -n "$pane_id" ]] || return 0
close_tab=""
close_ws=""
shopt -s nullglob
for path in "$(_state_dir)"/*.json; do
[[ -f "$path" ]] || continue
workspace_id="${path##*/}"
workspace_id="${workspace_id%.json}"
if [[ -z "$close_tab" ]]; then
close_tab="$(jq -r --arg pane "$pane_id" '
[(.editors // {})[] | select(.pane_id == $pane) | .tab_id][0] //
(if .review_pane_id == $pane then .review_tab_id else empty end)
' "$path" 2>/dev/null || true)"
if [[ -n "$close_tab" ]]; then
close_ws="$workspace_id"
fi
fi
_state_update "$workspace_id" --arg pane "$pane_id" '
if .agent_pane_id == $pane then .agent_pane_id = "" else . end
| if .review_pane_id == $pane then .review_pane_id = "" else . end
| if .shell_pane_id == $pane then .shell_pane_id = "" else . end
| if .sidebar_pane_id == $pane then .sidebar_pane_id = "" else . end
| .editors = ((.editors // {}) | to_entries | map(select(.value.pane_id != $pane)) | from_entries)'
done
shopt -u nullglob
if [[ -n "$close_tab" && -n "$close_ws" ]]; then
export HERDR_WORKSPACE_ID="$close_ws"
_close_tab_id "$close_tab"
fi
}
_on_workspace_closed() {
local workspace_id="${1:-}"
[[ -n "$workspace_id" ]] || return 0
_state_delete "$workspace_id"
}
_on_tab_focused() {
local tab_id="${1:-}" workspace_id
[[ -n "$tab_id" ]] || return 0
workspace_id="$(_event_id workspace_id)"
[[ -n "$workspace_id" ]] && export HERDR_WORKSPACE_ID="$workspace_id"
_activate_tab "$tab_id" 1
}
_resolve_context() {
local focused
if [[ -n "${WT_HERDR_LABEL:-}" && -n "${WT_HERDR_WORKDIR:-}" ]]; then
return 0
fi
if [[ -n "${HERDR_WORKSPACE_ID:-}" ]]; then
return 0
fi
focused="$(_focused_workspace_id)"
[[ -n "$focused" ]] && export HERDR_WORKSPACE_ID="$focused"
}
main() {
local cmd="${1:-}"
[[ -n "$cmd" ]] || cmd="${HERDR_PLUGIN_ACTION_ID:-}"
[[ -n "$cmd" ]] || cmd="${HERDR_PLUGIN_EVENT:-}"
case "$cmd" in
create)
_resolve_context
_layout_ensure
if [[ -z "${WT_HERDR_NO_ATTACH:-}" ]]; then
_select_center shell
fi
;;
apply)
_resolve_context
state="$(_layout_ensure)"
pane="$(printf '%s' "$state" | _jq '.sidebar_pane_id // empty')"
_restart_sidebar_pane "$pane"
if [[ -z "${WT_HERDR_NO_ATTACH:-}" ]]; then
_select_center shell
fi
;;
start-agent|start_agent)
_resolve_context
_start_agent
;;
startup) _on_startup ;;
focus-agent|focus_agent)
_resolve_context
_focus_agent
;;
select-review|select_review)
_resolve_context
_select_center review
;;
close-review|close_review)
_resolve_context
_close_review
;;
refresh-review|refresh_review)
_resolve_context
_refresh_review
;;
select-shell|select_terminal)
_resolve_context
_select_center shell
;;
toggle-sidebar)
_resolve_context
_toggle_sidebar
;;
select-files)
_resolve_context
_select_files
;;
select-source-control)
_resolve_context
_select_source_control
;;
open-editor)
_resolve_context
_open_editor "${2:-}"
;;
close-tab|close_tab)
_resolve_context
_close_current_tab
;;
close-pane|close_pane)
_resolve_context
_close_focused_pane
;;
select-tab|select_tab)
_resolve_context
_select_tab_number "${2:-1}"
;;
select-tab-*|select_tab_*)
_resolve_context
_select_tab_number "${cmd##*[-_]}"
;;
select-prev-tab|select_prev_tab)
_resolve_context
_select_tab_relative -1
;;
select-next-tab|select_next_tab)
_resolve_context
_select_tab_relative 1
;;
event-pane-exited|event_pane_exited|pane.exited)
_on_pane_exited "$(_event_id pane_id)"
;;
event-workspace-closed|event_workspace_closed|workspace.closed)
_on_workspace_closed "$(_event_id workspace_id)"
;;
event-tab-focused|event_tab_focused|tab.focused)
_on_tab_focused "$(_event_id tab_id)"
;;
*)
echo "agentic-layout: unknown command '$cmd'" >&2
exit 1
;;
esac
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
main "$@"
fi