1use eyre::Result;
4
5use crate::zellij;
6
7pub fn build(pane_id: &str, name: Option<&str>, rz_path: &str) -> Result<String> {
11 let panes = zellij::list_panes()?;
12 let identity = name.unwrap_or(pane_id);
13
14 let mut peers = String::new();
15 for p in &panes {
16 if p.is_plugin || p.pane_id() == pane_id {
17 continue;
18 }
19 let cmd = p.pane_command.as_deref().unwrap_or("shell");
20 let tab = p.tab_name.as_deref().unwrap_or("-");
21 peers.push_str(&format!(" - {} ({}, tab: {})\n", p.pane_id(), cmd, tab));
22 }
23 if peers.is_empty() {
24 peers.push_str(" (none)\n");
25 }
26
27 let workspace = std::env::var("ZELLIJ_SESSION_NAME")
29 .ok()
30 .map(|s| format!("/tmp/rz-{s}"))
31 .filter(|p| std::path::Path::new(p).exists());
32
33 let workspace_section = if let Some(ref ws) = workspace {
34 format!(
35 r#"### Workspace
36
37A shared workspace is available at `{ws}/shared/`.
38Write large outputs (research, code drafts, logs) there instead of
39inlining them in messages. Reference the file path in your message, e.g.:
40`{rz_path} send 0 "findings at {ws}/shared/research.md"`
41
42### Project Files
43
44The workspace has coordination files that all agents should use:
45
46- **`{ws}/goals.md`** — READ this when you start. It describes session goals. Add sub-goals as you discover them.
47- **`{ws}/context.md`** — UPDATE this with important decisions, discoveries, and context as you work.
48- **`{ws}/agents.md`** — UPDATE this with your pane ID, name, and current task when you start or switch tasks.
49"#
50 )
51 } else {
52 String::new()
53 };
54
55 Ok(format!(
56 r#"## Multi-Agent Environment
57
58You are agent "{identity}" (pane: {pane_id}) in a multi-agent Zellij session.
59
60You are **long-lived** — you will receive multiple tasks over time, not just one.
61After completing a task, report back and wait for the next one. Your context
62and knowledge accumulate across tasks, making you more valuable over time.
63Do not exit after finishing a task.
64
65### Communication
66
67You have `rz` at `{rz_path}`. Use it to talk to other agents:
68
69```bash
70# Send a message to another agent (use just the number)
71{rz_path} send <pane_id> "your message"
72
73# Send and block until reply (timeout in seconds)
74{rz_path} send --wait 30 <pane_id> "question"
75
76# List all agents
77{rz_path} list
78
79# Session overview with message counts
80{rz_path} status
81
82# Read another agent's scrollback (last N lines)
83{rz_path} dump <pane_id> --last 50
84
85# View protocol messages only
86{rz_path} log <pane_id>
87
88# Broadcast to all agents
89{rz_path} broadcast "message"
90```
91
92{workspace_section}### Active agents
93
94{peers}
95### Protocol
96
97When you receive a message starting with `@@RZ:` it is a protocol envelope.
98The JSON inside has `from`, `kind`, and `ts` fields. Reply with
99`{rz_path} send --ref <message_id> <from_pane_id> "your response"`.
100
101Keep messages short. Use the workspace for large outputs."#
102 ))
103}