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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use std::collections::BTreeMap;
use std::convert::Infallible;
use tonic::codegen::async_trait;
use crate::agent::AgentToolRef;
use crate::catalog::Catalog;
use crate::error::{Error, Result};
/// Split a canonical subject ID such as `user:ada` into kind and id.
pub fn parse_subject_id(subject_id: &str) -> Option<(&str, &str)> {
let trimmed = subject_id.trim();
let (kind, id) = trimmed.split_once(':')?;
let kind = kind.trim();
let id = id.trim();
if kind.is_empty() || id.is_empty() {
return None;
}
Some((kind, id))
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
/// Identifies the caller that initiated an operation.
pub struct Subject {
/// Stable subject id.
pub id: String,
/// Subject id used for credential lookup, when different from the actor.
pub credential_subject_id: String,
/// Email address resolved by the Gestalt host for user subjects.
pub email: String,
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
/// Describes the resolved credential used to authorize an operation.
pub struct Credential {
/// Credential mode used by the host.
pub mode: String,
/// Subject id associated with the credential.
pub subject_id: String,
/// Connection id or name associated with the credential.
pub connection: String,
/// Provider instance id or name associated with the credential.
pub instance: String,
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
/// Summarizes the host-side access decision attached to an operation.
pub struct Access {
/// Policy name or id applied to the request.
pub policy: String,
/// Effective role granted to the request.
pub role: String,
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
/// Describes public host metadata attached to a request.
pub struct Host {
/// Public base URL for the Gestalt host.
pub public_base_url: String,
}
#[derive(Clone, Debug, Default, PartialEq)]
/// Carries execution-scoped metadata into typed operation handlers.
pub struct Request {
/// Request token supplied to hosted HTTP operation handlers.
pub token: String,
/// Connection parameters resolved by the host.
pub connection_params: BTreeMap<String, String>,
/// Subject that initiated the request.
pub subject: Subject,
/// Original agent caller when an agent tool runs as a delegated subject.
pub agent_subject: Subject,
/// Credential used to authorize the request.
pub credential: Credential,
/// Access decision attached to the request.
pub access: Access,
/// Public host metadata attached to the request.
pub host: Host,
/// Idempotency key supplied by the host.
pub idempotency_key: String,
/// Workflow callback metadata uses a JSON-style lowerCamelCase object
/// such as `runId`, `target.steps[0].app.name`,
/// `trigger.activationId`, and `trigger.event.specVersion`.
pub workflow: serde_json::Map<String, serde_json::Value>,
/// Agent tool refs granted to the current operation request.
pub tool_refs: Vec<AgentToolRef>,
/// Whether the host attached a tool-ref context to this request.
pub tool_refs_set: bool,
/// Invocation token used to call host services.
pub invocation_token: String,
}
impl Request {
/// Returns one resolved connection parameter by name.
pub fn connection_param(&self, name: &str) -> Option<&str> {
self.connection_params.get(name).map(String::as_str)
}
/// Returns the invocation token used to call host services.
pub fn invocation_token(&self) -> &str {
&self.invocation_token
}
/// Creates an app client using this request's invocation token.
pub async fn app(&self) -> std::result::Result<crate::App, crate::AppError> {
crate::App::connect(self.invocation_token()).await
}
/// Creates a workflow using this request's invocation token.
pub async fn workflow(&self) -> std::result::Result<crate::Workflow, crate::WorkflowError> {
crate::Workflow::connect_with_idempotency_key(
self.invocation_token(),
self.idempotency_key.trim(),
)
.await
}
/// Creates an agent using this request's invocation token.
pub async fn agent(&self) -> std::result::Result<crate::Agent, crate::AgentError> {
crate::Agent::connect(self.invocation_token()).await
}
}
#[derive(Clone, Debug, Default, PartialEq)]
/// Carries one verified hosted HTTP request into a provider subject resolver.
pub struct HTTPSubjectRequest {
/// Hosted HTTP binding name from the app manifest.
pub binding: String,
/// HTTP method used for the inbound request.
pub method: String,
/// Request path received by the hosted HTTP binding.
pub path: String,
/// Request content type.
pub content_type: String,
/// Request headers after host-side verification.
pub headers: BTreeMap<String, Vec<String>>,
/// Request query parameters.
pub query: BTreeMap<String, Vec<String>>,
/// Decoded request parameters.
pub params: serde_json::Map<String, serde_json::Value>,
/// Raw request body bytes.
pub raw_body: Vec<u8>,
/// Security scheme used to verify the request.
pub security_scheme: String,
/// Subject string verified by the security scheme, when available.
pub verified_subject: String,
/// Claims verified by the security scheme.
pub verified_claims: BTreeMap<String, String>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
/// Wraps a typed handler response plus an optional explicit HTTP status code.
pub struct Response<T> {
/// Optional explicit HTTP-style status code.
pub status: Option<u16>,
/// HTTP response headers returned by the handler.
pub headers: BTreeMap<String, Vec<String>>,
/// Typed response body returned by the handler.
pub body: T,
}
impl<T> Response<T> {
/// Creates a response with an explicit HTTP status code.
pub fn new(status: u16, body: T) -> Self {
Self {
status: Some(status),
headers: BTreeMap::new(),
body,
}
}
/// Adds one HTTP response header value.
pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.headers
.entry(name.into())
.or_default()
.push(value.into());
self
}
}
/// Returns a successful JSON response with status code `200`.
pub fn ok<T>(body: T) -> Response<T> {
Response::new(200, body)
}
/// Converts handler return values into a typed [`Response`].
pub trait IntoResponse<T> {
/// Converts a handler return value into a typed response wrapper.
fn into_response(self) -> Response<T>;
}
impl<T> IntoResponse<T> for Response<T> {
fn into_response(self) -> Response<T> {
self
}
}
impl<T> IntoResponse<T> for T {
fn into_response(self) -> Response<T> {
ok(self)
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
/// Describes provider metadata that should be surfaced by the runtime.
pub struct RuntimeMetadata {
/// Provider name to report to the host.
pub name: String,
/// Human-readable provider display name.
pub display_name: String,
/// Human-readable provider description.
pub description: String,
/// Provider version string.
pub version: String,
}
#[async_trait]
/// Shared lifecycle contract for Gestalt integration providers.
pub trait Provider: Send + Sync + 'static {
/// Configures the provider before it starts serving requests.
async fn configure(
&self,
_name: &str,
_config: serde_json::Map<String, serde_json::Value>,
) -> Result<()> {
Ok(())
}
/// Returns runtime metadata that should augment the static manifest.
fn metadata(&self) -> Option<RuntimeMetadata> {
None
}
/// Returns non-fatal warnings the host should surface to users.
fn warnings(&self) -> Vec<String> {
Vec::new()
}
/// Performs an optional health check.
async fn health_check(&self) -> Result<()> {
Ok(())
}
/// Starts provider-owned background work after configuration.
async fn start(&self) -> Result<()> {
Ok(())
}
/// Reports whether this provider can derive additional operations from the
/// current request context.
fn supports_session_catalog(&self) -> bool {
false
}
/// Returns an optional request-scoped catalog extension.
async fn catalog_for_request(&self, _request: &Request) -> Result<Option<Catalog>> {
Ok(None)
}
/// Resolves a hosted HTTP request to a concrete subject before dispatch.
async fn resolve_http_subject(
&self,
_request: HTTPSubjectRequest,
_context: &Request,
) -> Result<Option<Subject>> {
Ok(None)
}
/// Shuts the provider down before the runtime exits.
async fn close(&self) -> Result<()> {
Ok(())
}
}
impl From<Infallible> for Error {
fn from(_value: Infallible) -> Self {
Error::internal("unreachable infallible error")
}
}