Fully Automated - 100% Backpressure - Pipeline runner ๐Ÿƒ for AI workflows. ๐Ÿš€

Commit 9307d550

9307d550b53fMerged

Scott Nixon <[email protected]> ยท 9 months ago ยท 1 file ยท ~1 modified

Description

[attractor-7cr] Terminal PTY: accept explicit cwd parameter

Updated spawn_claude_pty to accept explicit working directory for multi-project support.

Changes:

  • Removed unused continue_session parameter from spawn_claude_pty
  • Added cwd: &Path parameter to spawn_claude_pty
  • Extended WsQuery with folder: Option field
  • Added folder validation logic at both call sites (session reconnect and new session)
  • Updated documentation to describe ?folder= query parameter

The terminal backend now supports per-project working directories via WebSocket URL.

Details

Commit
9307d550b53f
Parent
8efc45d649d4

crates/attractor-web/src/server/terminal.rsmodifiedView file

@@ -22,6 +22,9 @@
2222 //!
2323 //! The WebSocket carries a mix of JSON control messages and raw terminal bytes:
2424 //!
25+//! - **Connection URL**: `ws://host/path?session=<id>&folder=<path>`
26+//! - `session` (optional): reconnect to existing session by ID
27+//! - `folder` (optional): working directory for new PTY sessions
2528 //! - **Server โ†’ Client** (first msg): `{"type":"session","session_id":"..."}`
2629 //! - **Server โ†’ Client** (ongoing): binary frames of PTY stdout
2730 //! - **Client โ†’ Server**: binary frames of keyboard input
@@ -100,10 +103,11 @@
100103 #[derive(Deserialize)]
101104 pub struct WsQuery {
102105 session: Option<String>,
106+ folder: Option<String>,
103107 }
104108
105-/// Spawn a new claude PTY. If `continue_session` is true, uses `--continue`.
106-fn spawn_claude_pty(continue_session: bool) -> Result<TerminalSession, String> {
109+/// Spawn a new claude PTY in the specified working directory.
110+fn spawn_claude_pty(cwd: &std::path::Path) -> Result<TerminalSession, String> {
107111 let pty_system = native_pty_system();
108112
109113 let pty_pair = pty_system
@@ -116,12 +120,7 @@
116120 .map_err(|e| format!("Failed to open PTY: {e}"))?;
117121
118122 let mut cmd = CommandBuilder::new("claude");
119- if continue_session {
120- cmd.arg("--continue");
121- }
122- if let Ok(cwd) = std::env::current_dir() {
123- cmd.cwd(cwd);
124- }
123+ cmd.cwd(cwd);
125124
126125 let child = pty_pair
127126 .slave
@@ -178,7 +177,27 @@
178177 "Session {} not found (server restarted), spawning fresh claude",
179178 id
180179 );
181- match spawn_claude_pty(false) {
180+
181+ // Determine the working directory
182+ let cwd = if let Some(ref folder_path) = query.folder {
183+ let path = std::path::Path::new(folder_path);
184+ if path.exists() && path.is_dir() {
185+ path
186+ } else {
187+ tracing::warn!("Invalid folder path: {}, falling back to current_dir", folder_path);
188+ std::env::current_dir()
189+ .as_ref()
190+ .map(|p| p.as_path())
191+ .unwrap_or_else(|_| std::path::Path::new("/"))
192+ }
193+ } else {
194+ std::env::current_dir()
195+ .as_ref()
196+ .map(|p| p.as_path())
197+ .unwrap_or_else(|_| std::path::Path::new("/"))
198+ };
199+
200+ match spawn_claude_pty(cwd) {
182201 Ok(s) => {
183202 let session = Arc::new(s);
184203 let new_id = uuid::Uuid::new_v4().to_string();
@@ -193,7 +212,26 @@
193212 }
194213 } else {
195214 // Brand new session
196- match spawn_claude_pty(false) {
215+ // Determine the working directory
216+ let cwd = if let Some(ref folder_path) = query.folder {
217+ let path = std::path::Path::new(folder_path);
218+ if path.exists() && path.is_dir() {
219+ path
220+ } else {
221+ tracing::warn!("Invalid folder path: {}, falling back to current_dir", folder_path);
222+ std::env::current_dir()
223+ .as_ref()
224+ .map(|p| p.as_path())
225+ .unwrap_or_else(|_| std::path::Path::new("/"))
226+ }
227+ } else {
228+ std::env::current_dir()
229+ .as_ref()
230+ .map(|p| p.as_path())
231+ .unwrap_or_else(|_| std::path::Path::new("/"))
232+ };
233+
234+ match spawn_claude_pty(cwd) {
197235 Ok(s) => {
198236 let session = Arc::new(s);
199237 let id = uuid::Uuid::new_v4().to_string();