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
use std::pin::Pin;
use std::sync::Arc;
use async_trait::async_trait;
use futures::Stream;
use crate::{
error::PlatformError,
types::{ChannelId, InboundMessage, OutboundMessage},
};
#[async_trait]
pub trait PlatformAdapter: Send + Sync + 'static {
fn name(&self) -> &'static str;
async fn start(&self, handler: Arc<dyn MessageHandler>) -> Result<(), PlatformError>;
/// Signal the platform's background task to stop. Default no-op for adapters
/// that do not support clean cancellation (e.g. axum-based HTTP servers that
/// rely on process exit). Adapters that own a JoinHandle should abort it here.
async fn stop(&self) {}
/// Liveness probe called by the `/health` endpoint. Return `Ok(())` when the
/// adapter is operating normally, or an error string describing the fault.
/// Default implementation always returns `Ok(())` — adapters may override this
/// to ping their upstream API or check their internal connection state.
async fn health_check(&self) -> Result<(), String> {
Ok(())
}
async fn send_message(
&self,
channel: &ChannelId,
message: OutboundMessage,
) -> Result<(), PlatformError>;
async fn send_stream(
&self,
channel: &ChannelId,
stream: Pin<Box<dyn Stream<Item = String> + Send>>,
) -> Result<(), PlatformError>;
/// Show a transient "thinking…" indicator while the gateway is still
/// processing the user's last message. Best-effort: adapters that have no
/// equivalent surface simply do nothing. The handler invokes this when an
/// image arrives so the user sees feedback during the multi-second
/// view_image round-trip.
async fn show_typing(&self, _channel: &ChannelId) -> Result<(), PlatformError> {
Ok(())
}
}
#[async_trait]
pub trait MessageHandler: Send + Sync + 'static {
async fn handle(&self, msg: InboundMessage) -> Result<(), anyhow::Error>;
}