feat: add main features
Agents for developpement added + frontend add + backend added. Git viewer created + agent and template creator + layout and project creator
This commit is contained in:
84
crates/app-tauri/src/pty.rs
Normal file
84
crates/app-tauri/src/pty.rs
Normal file
@ -0,0 +1,84 @@
|
||||
//! Generic **PTY ↔ Tauri Channel** bridge infrastructure.
|
||||
//!
|
||||
//! ARCHITECTURE §2 decides that high-frequency PTY byte streams travel over
|
||||
//! per-session [`tauri::ipc::Channel`]s rather than global events, for
|
||||
//! throughput and isolation. This module provides the transport-side plumbing
|
||||
//! that L3 will plug a real `PtyPort` into; here there is **no real PTY** yet —
|
||||
//! only the registry + the abstraction that routes byte chunks to the right
|
||||
//! frontend channel.
|
||||
//!
|
||||
//! Design:
|
||||
//! - The frontend opens a terminal and passes a [`tauri::ipc::Channel`] for that
|
||||
//! session. The backend registers it in [`PtyBridge`] keyed by `SessionId`.
|
||||
//! - Whatever produces output (the PTY adapter in L3) calls
|
||||
//! [`PtyBridge::send_output`], which forwards the bytes on the matching
|
||||
//! channel. Bytes are sent as-is; the frontend xterm wrapper consumes them.
|
||||
//! - [`PtyBridge::unregister`] tears the channel down on terminal close.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use tauri::ipc::Channel;
|
||||
|
||||
use domain::ids::SessionId;
|
||||
|
||||
/// A chunk of PTY output bytes destined for a specific session's channel.
|
||||
///
|
||||
/// Sent as a raw byte vector; serde encodes it for the IPC channel. Kept as a
|
||||
/// distinct type so the wire shape can evolve (e.g. add a sequence number for
|
||||
/// backpressure handling) without touching call sites.
|
||||
pub type PtyChunk = Vec<u8>;
|
||||
|
||||
/// Registry mapping live terminal sessions to their output [`Channel`].
|
||||
///
|
||||
/// Thread-safe; cloned `Arc<PtyBridge>` is held in [`crate::state::AppState`].
|
||||
#[derive(Default)]
|
||||
pub struct PtyBridge {
|
||||
channels: Mutex<HashMap<SessionId, Channel<PtyChunk>>>,
|
||||
}
|
||||
|
||||
impl PtyBridge {
|
||||
/// Creates an empty bridge.
|
||||
#[must_use]
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
channels: Mutex::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Registers the output channel for a session (called when a terminal is
|
||||
/// opened, from a `#[tauri::command]` that receives the `Channel` argument).
|
||||
pub fn register(&self, session: SessionId, channel: Channel<PtyChunk>) {
|
||||
if let Ok(mut map) = self.channels.lock() {
|
||||
map.insert(session, channel);
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes a session's channel (terminal closed).
|
||||
pub fn unregister(&self, session: &SessionId) {
|
||||
if let Ok(mut map) = self.channels.lock() {
|
||||
map.remove(session);
|
||||
}
|
||||
}
|
||||
|
||||
/// Forwards a chunk of output bytes to a session's channel.
|
||||
///
|
||||
/// Returns `true` if the chunk was delivered, `false` if no channel is
|
||||
/// registered for the session (e.g. already closed). In L3 the PTY adapter's
|
||||
/// output stream drives this.
|
||||
pub fn send_output(&self, session: &SessionId, chunk: PtyChunk) -> bool {
|
||||
let Ok(map) = self.channels.lock() else {
|
||||
return false;
|
||||
};
|
||||
match map.get(session) {
|
||||
Some(channel) => channel.send(chunk).is_ok(),
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Number of currently-registered sessions (handy for tests/diagnostics).
|
||||
#[must_use]
|
||||
pub fn active_sessions(&self) -> usize {
|
||||
self.channels.lock().map(|m| m.len()).unwrap_or(0)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user