Skip to main content

gestalt/
plugin_runtime.rs

1use std::sync::Arc;
2
3use tonic::codegen::async_trait;
4use tonic::{Request as GrpcRequest, Response as GrpcResponse, Status};
5
6use crate::api::RuntimeMetadata;
7use crate::error::Result as ProviderResult;
8use crate::generated::v1::{self as pb};
9
10#[async_trait]
11/// Provider trait for serving hosted plugin-runtime sessions.
12pub trait PluginRuntimeProvider:
13    pb::plugin_runtime_provider_server::PluginRuntimeProvider + Send + Sync + 'static
14{
15    /// Configures the provider before it starts serving requests.
16    async fn configure(
17        &self,
18        _name: &str,
19        _config: serde_json::Map<String, serde_json::Value>,
20    ) -> ProviderResult<()> {
21        Ok(())
22    }
23
24    /// Returns runtime metadata that should augment the static manifest.
25    fn metadata(&self) -> Option<RuntimeMetadata> {
26        None
27    }
28
29    /// Returns non-fatal warnings the host should surface to users.
30    fn warnings(&self) -> Vec<String> {
31        Vec::new()
32    }
33
34    /// Performs an optional health check.
35    async fn health_check(&self) -> ProviderResult<()> {
36        Ok(())
37    }
38
39    /// Starts provider-owned background work after configuration.
40    async fn start(&self) -> ProviderResult<()> {
41        Ok(())
42    }
43
44    /// Shuts the provider down before the runtime exits.
45    async fn close(&self) -> ProviderResult<()> {
46        Ok(())
47    }
48}
49
50#[derive(Clone)]
51pub(crate) struct PluginRuntimeServer<P> {
52    provider: Arc<P>,
53}
54
55impl<P> PluginRuntimeServer<P> {
56    pub(crate) fn new(provider: Arc<P>) -> Self {
57        Self { provider }
58    }
59}
60
61#[async_trait]
62impl<P> pb::plugin_runtime_provider_server::PluginRuntimeProvider for PluginRuntimeServer<P>
63where
64    P: PluginRuntimeProvider,
65{
66    async fn get_support(
67        &self,
68        request: GrpcRequest<()>,
69    ) -> std::result::Result<GrpcResponse<pb::PluginRuntimeSupport>, Status> {
70        self.provider.get_support(request).await
71    }
72
73    async fn start_session(
74        &self,
75        request: GrpcRequest<pb::StartPluginRuntimeSessionRequest>,
76    ) -> std::result::Result<GrpcResponse<pb::PluginRuntimeSession>, Status> {
77        self.provider.start_session(request).await
78    }
79
80    async fn get_session(
81        &self,
82        request: GrpcRequest<pb::GetPluginRuntimeSessionRequest>,
83    ) -> std::result::Result<GrpcResponse<pb::PluginRuntimeSession>, Status> {
84        self.provider.get_session(request).await
85    }
86
87    async fn list_sessions(
88        &self,
89        request: GrpcRequest<pb::ListPluginRuntimeSessionsRequest>,
90    ) -> std::result::Result<GrpcResponse<pb::ListPluginRuntimeSessionsResponse>, Status> {
91        self.provider.list_sessions(request).await
92    }
93
94    async fn stop_session(
95        &self,
96        request: GrpcRequest<pb::StopPluginRuntimeSessionRequest>,
97    ) -> std::result::Result<GrpcResponse<()>, Status> {
98        self.provider.stop_session(request).await
99    }
100
101    async fn start_plugin(
102        &self,
103        request: GrpcRequest<pb::StartHostedPluginRequest>,
104    ) -> std::result::Result<GrpcResponse<pb::HostedPlugin>, Status> {
105        self.provider.start_plugin(request).await
106    }
107}