WIP Telegram: modulo akode-telegram (API e impostazioni), in pausa
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -409,6 +409,54 @@ pub async fn start(mut args: InProcessStartArgs) -> IoResult<InProcessClientHand
|
||||
Ok(client)
|
||||
}
|
||||
|
||||
/// One JSON-RPC message from the in-process Telegram bridge, with the bookkeeping the
|
||||
/// socket transport loop does for its connections.
|
||||
async fn handle_bridge_message(
|
||||
processor: &Arc<MessageProcessor>,
|
||||
session: &Arc<ConnectionSessionState>,
|
||||
message: akode_app_server_protocol::JSONRPCMessage,
|
||||
outbound_initialized: &AtomicBool,
|
||||
outbound_experimental_api_enabled: &AtomicBool,
|
||||
outbound_opted_out_notification_methods: &RwLock<HashSet<String>>,
|
||||
) {
|
||||
use akode_app_server_protocol::JSONRPCMessage;
|
||||
let bridge_id = crate::telegram_bridge::IN_PROCESS_BRIDGE_CONNECTION_ID;
|
||||
match message {
|
||||
JSONRPCMessage::Request(request) => {
|
||||
let was_initialized = session.initialized();
|
||||
processor
|
||||
.process_request(
|
||||
bridge_id,
|
||||
request,
|
||||
&crate::transport::AppServerTransport::Off,
|
||||
Arc::clone(session),
|
||||
)
|
||||
.await;
|
||||
if let Ok(mut opted_out) = outbound_opted_out_notification_methods.write() {
|
||||
*opted_out = session.opted_out_notification_methods();
|
||||
}
|
||||
outbound_experimental_api_enabled
|
||||
.store(session.experimental_api_enabled(), Ordering::Release);
|
||||
if !was_initialized && session.initialized() {
|
||||
processor
|
||||
.send_initialize_notifications_to_connection(bridge_id)
|
||||
.await;
|
||||
processor.connection_initialized(bridge_id).await;
|
||||
outbound_initialized.store(true, Ordering::Release);
|
||||
}
|
||||
}
|
||||
JSONRPCMessage::Response(response) => {
|
||||
processor.process_response(bridge_id, response).await;
|
||||
}
|
||||
JSONRPCMessage::Notification(notification) => {
|
||||
processor.process_notification(notification).await;
|
||||
}
|
||||
JSONRPCMessage::Error(error) => {
|
||||
processor.process_error(bridge_id, error).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_outbound_router(
|
||||
mut outgoing_rx: mpsc::Receiver<OutgoingEnvelope>,
|
||||
mut outbound_connections: HashMap<ConnectionId, OutboundConnectionState>,
|
||||
@@ -477,7 +525,57 @@ async fn start_uninitialized(mut args: InProcessStartArgs) -> IoResult<InProcess
|
||||
let outbound_experimental_api_enabled = Arc::new(AtomicBool::new(false));
|
||||
let outbound_opted_out_notification_methods = Arc::new(RwLock::new(HashSet::new()));
|
||||
|
||||
// The Telegram bridge is a second, internal connection of this app-server.
|
||||
let bridge_id = crate::telegram_bridge::IN_PROCESS_BRIDGE_CONNECTION_ID;
|
||||
let (bridge_writer_tx, mut bridge_writer_rx) =
|
||||
mpsc::channel::<QueuedOutgoingMessage>(channel_capacity);
|
||||
let bridge_initialized = Arc::new(AtomicBool::new(false));
|
||||
let bridge_experimental_api_enabled = Arc::new(AtomicBool::new(false));
|
||||
let bridge_opted_out_notification_methods = Arc::new(RwLock::new(HashSet::new()));
|
||||
let bridge_sink = Arc::new(std::sync::Mutex::new(
|
||||
None::<mpsc::UnboundedSender<akode_app_server_protocol::JSONRPCMessage>>,
|
||||
));
|
||||
let bridge_forward = tokio::spawn({
|
||||
let bridge_sink = Arc::clone(&bridge_sink);
|
||||
async move {
|
||||
while let Some(queued) = bridge_writer_rx.recv().await {
|
||||
let sink = bridge_sink
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.clone();
|
||||
if let Some(sink) = sink
|
||||
&& let Some(message) =
|
||||
crate::telegram_bridge::to_jsonrpc(&queued.message)
|
||||
{
|
||||
let _ = sink.send(message);
|
||||
}
|
||||
if let Some(done) = queued.write_complete_tx {
|
||||
let _ = done.send(());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
let (bridge_events_tx, mut bridge_events_rx) = mpsc::unbounded_channel();
|
||||
let bridge_shutdown = tokio_util::sync::CancellationToken::new();
|
||||
let bridge_task = (args.session_source == SessionSource::Cli).then(|| {
|
||||
crate::telegram_bridge::spawn_in_process_bridge(
|
||||
args.config.codex_home.to_path_buf(),
|
||||
bridge_events_tx,
|
||||
bridge_shutdown.clone(),
|
||||
)
|
||||
});
|
||||
|
||||
let mut outbound_connections = HashMap::<ConnectionId, OutboundConnectionState>::new();
|
||||
outbound_connections.insert(
|
||||
bridge_id,
|
||||
OutboundConnectionState::new(
|
||||
bridge_writer_tx,
|
||||
Arc::clone(&bridge_initialized),
|
||||
Arc::clone(&bridge_experimental_api_enabled),
|
||||
Arc::clone(&bridge_opted_out_notification_methods),
|
||||
/*disconnect_sender*/ None,
|
||||
),
|
||||
);
|
||||
outbound_connections.insert(
|
||||
IN_PROCESS_CONNECTION_ID,
|
||||
OutboundConnectionState::new(
|
||||
@@ -526,9 +624,51 @@ async fn start_uninitialized(mut args: InProcessStartArgs) -> IoResult<InProcess
|
||||
crate::transport::ConnectionOrigin::InProcess,
|
||||
));
|
||||
let mut listen_for_threads = true;
|
||||
let mut bridge_events_open = bridge_task.is_some();
|
||||
let mut bridge_session: Option<Arc<ConnectionSessionState>> = None;
|
||||
|
||||
loop {
|
||||
tokio::select! {
|
||||
event = bridge_events_rx.recv(), if bridge_events_open => {
|
||||
use crate::telegram_bridge::InProcessBridgeEvent;
|
||||
match event {
|
||||
Some(InProcessBridgeEvent::Opened(sink)) => {
|
||||
if let Some(old) = bridge_session.take() {
|
||||
processor.connection_closed(bridge_id, &old).await;
|
||||
}
|
||||
bridge_initialized.store(false, Ordering::Release);
|
||||
*bridge_sink
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner) = Some(sink);
|
||||
bridge_session = Some(Arc::new(ConnectionSessionState::new(
|
||||
crate::transport::ConnectionOrigin::InProcess,
|
||||
)));
|
||||
}
|
||||
Some(InProcessBridgeEvent::Message(message)) => {
|
||||
if let Some(bridge) = bridge_session.as_ref() {
|
||||
handle_bridge_message(
|
||||
&processor,
|
||||
bridge,
|
||||
message,
|
||||
&bridge_initialized,
|
||||
&bridge_experimental_api_enabled,
|
||||
&bridge_opted_out_notification_methods,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
Some(InProcessBridgeEvent::Closed) => {
|
||||
bridge_initialized.store(false, Ordering::Release);
|
||||
*bridge_sink
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner) = None;
|
||||
if let Some(old) = bridge_session.take() {
|
||||
processor.connection_closed(bridge_id, &old).await;
|
||||
}
|
||||
}
|
||||
None => bridge_events_open = false,
|
||||
}
|
||||
}
|
||||
command = processor_rx.recv() => {
|
||||
match command {
|
||||
Some(ProcessorCommand::Request(request, cancellation)) => {
|
||||
@@ -577,11 +717,17 @@ async fn start_uninitialized(mut args: InProcessStartArgs) -> IoResult<InProcess
|
||||
created = thread_created_rx.recv(), if listen_for_threads => {
|
||||
match created {
|
||||
Ok(thread_id) => {
|
||||
let connection_ids = if session.initialized() {
|
||||
let mut connection_ids = if session.initialized() {
|
||||
vec![IN_PROCESS_CONNECTION_ID]
|
||||
} else {
|
||||
Vec::<ConnectionId>::new()
|
||||
};
|
||||
if bridge_session
|
||||
.as_ref()
|
||||
.is_some_and(|bridge| bridge.initialized())
|
||||
{
|
||||
connection_ids.push(bridge_id);
|
||||
}
|
||||
processor
|
||||
.try_attach_thread_listener(thread_id, connection_ids)
|
||||
.await;
|
||||
@@ -597,6 +743,14 @@ async fn start_uninitialized(mut args: InProcessStartArgs) -> IoResult<InProcess
|
||||
}
|
||||
}
|
||||
|
||||
bridge_shutdown.cancel();
|
||||
if let Some(task) = bridge_task {
|
||||
task.abort();
|
||||
}
|
||||
bridge_forward.abort();
|
||||
if let Some(bridge) = bridge_session.take() {
|
||||
processor.connection_closed(bridge_id, &bridge).await;
|
||||
}
|
||||
processor.clear_runtime_references();
|
||||
processor.cancel_active_login().await;
|
||||
processor
|
||||
|
||||
Reference in New Issue
Block a user