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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//! Typed hook contracts and sequential compositions. The kernel owns plans and invocations.
use crate::{Context, Error, Host, Result, core};
use futures::{FutureExt, future::LocalBoxFuture};
use serde::{Serialize, de::DeserializeOwned};
use serde_json::Value;
use std::{any::TypeId, future::Future, rc::Rc};
pub(crate) type Compose<A, R> =
Rc<dyn Fn(Host, Context, Rc<[core::Id]>, A) -> LocalBoxFuture<'static, Result<R>>>;
pub(crate) type Encode<R> = Rc<dyn Fn(R) -> Result<Option<Value>>>;
pub(crate) type Decode<R> = Rc<dyn Fn(Option<Value>) -> Result<R>>;
pub(crate) type LocalContribution = Rc<
dyn Fn(
Context,
Box<dyn std::any::Any>,
) -> LocalBoxFuture<'static, Result<Box<dyn std::any::Any>>>,
>;
pub(crate) type PortableContribution =
Rc<dyn Fn(Context, Value) -> LocalBoxFuture<'static, Result<Option<Value>>>>;
/// Local Rust identity and an optional prepared WIT identity share one contribution registry.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Signature {
pub native: Option<TypeId>,
pub wit: Option<String>,
}
impl Signature {
pub fn matches(&self, other: &Self) -> bool {
match (&self.wit, &other.wit) {
(Some(left), Some(right)) if left != right => false,
_ if self.native.is_some() && self.native == other.native => true,
(Some(left), Some(right)) => left == right,
_ => false,
}
}
}
#[derive(Clone)]
pub(crate) struct Contribution {
pub target: String,
pub hook: String,
pub signature: Signature,
pub priority: i32,
pub local: Option<LocalContribution>,
pub portable: PortableContribution,
}
/// A named, typed hook contract. Share this descriptor with contributing orbs.
/// Rust values remain local unless a participant belongs to another language.
///
/// ```
/// use tembra_runtime::{Hook, Orb};
/// let transform = Hook::<String, String>::waterfall("transform");
/// let provider = Orb::new("text", 1).hook(transform);
/// # let _ = provider;
/// ```
pub struct Hook<A, R> {
pub(crate) name: String,
pub(crate) wit: Option<String>,
pub(crate) compose: Compose<A, R>,
pub(crate) encode: Encode<R>,
pub(crate) decode: Decode<R>,
}
impl<A, R> Clone for Hook<A, R> {
fn clone(&self) -> Self {
Self {
name: self.name.clone(),
wit: self.wit.clone(),
compose: self.compose.clone(),
encode: self.encode.clone(),
decode: self.decode.clone(),
}
}
}
impl<A: Clone + Serialize + DeserializeOwned + 'static> Hook<A, ()> {
/// Run participants in plan order with the same input, ignoring their results.
pub fn series(name: impl Into<String>) -> Self {
let decode: Decode<()> = Rc::new(|_| Ok(()));
let participant = decode.clone();
Self {
name: name.into(),
wit: None,
encode: Rc::new(|()| Ok(None)),
decode,
compose: Rc::new(move |host, ctx, plan, input: A| {
let decode = participant.clone();
async move {
for operation in plan.iter() {
host.hook_participant::<A, ()>(*operation, &ctx, input.clone(), &decode)
.await?;
}
Ok(())
}
.boxed_local()
}),
}
}
}
impl<A: Serialize + DeserializeOwned + 'static> Hook<A, A> {
/// Pass each result to the next participant; an empty plan returns the input.
pub fn waterfall(name: impl Into<String>) -> Self {
let decode: Decode<A> = Rc::new(|value| from_value(value.unwrap_or(Value::Null)));
let participant = decode.clone();
Self {
name: name.into(),
wit: None,
encode: Rc::new(|value| to_value(value).map(Some)),
decode,
compose: Rc::new(move |host, ctx, plan, mut input| {
let decode = participant.clone();
async move {
for operation in plan.iter() {
input = host
.hook_participant(*operation, &ctx, input, &decode)
.await?;
}
Ok(input)
}
.boxed_local()
}),
}
}
/// Pass each result to the next participant until `should_stop` accepts the accumulated value.
///
/// # Guarantees
///
/// Participants run sequentially in the versioned plan's priority and registration order.
/// The predicate checks the initial input and each successful participant result. The first
/// accepted value is returned unchanged, without invoking later participants. An empty plan
/// returns the input. Native participants receive typed values without serialization;
/// participants in another language use the same encoding as [`Self::waterfall`].
///
/// Participant errors propagate without invoking later participants or checking the predicate
/// again. Work already performed by earlier participants is not rolled back. Stopping is a
/// normal successful result and does not cancel the context or change object ownership.
///
/// # Examples
///
/// ```
/// use futures::{FutureExt, executor::LocalPool};
/// use tembra_runtime::{Hook, Host, Orb, Service, core::Requirement};
///
/// let mut pool = LocalPool::new();
/// let host = Host::new(pool.spawner());
/// let hook = Hook::<i32, i32>::waterfall_until("limit", |value| *value >= 10);
/// let declared = hook.clone();
/// host.install(vec![
/// Orb::new("provider", 1).hook(hook.clone()).service(
/// Service::new("provider", move |self_| self_.hook(&declared))
/// .method("run", |hook, ctx, input: i32| async move {
/// hook.run(&ctx, input).await
/// }),
/// ),
/// Orb::new("contributor", 1)
/// .requires(Requirement {
/// signatures: Default::default(),
/// hook_signatures: Default::default(),
/// orb: "provider".into(), api_version: 1,
/// methods: Default::default(), hooks: ["limit".into()].into(),
/// })
/// .contribute("provider", &hook, 0, |_, value| async move { Ok(value + 10) })
/// .contribute("provider", &hook, 1, |_, _| async { panic!("already stopped") }),
/// ]).unwrap();
/// pool.run_until_stalled();
/// let reference = host.get("provider", vec!["main".into()], "alice").unwrap();
/// let result: i32 = pool.run_until(host.root("alice", move |ctx| {
/// async move { reference.call(&ctx, "run", 1).await }.boxed_local()
/// })).unwrap();
/// assert_eq!(result, 11);
/// pool.run_until(host.close()).unwrap();
/// ```
pub fn waterfall_until(
name: impl Into<String>,
should_stop: impl Fn(&A) -> bool + 'static,
) -> Self {
let decode: Decode<A> = Rc::new(|value| from_value(value.unwrap_or(Value::Null)));
let participant = decode.clone();
let should_stop = Rc::new(should_stop);
Self {
name: name.into(),
wit: None,
encode: Rc::new(|value| to_value(value).map(Some)),
decode,
compose: Rc::new(move |host, ctx, plan, mut input| {
let decode = participant.clone();
let should_stop = should_stop.clone();
async move {
if should_stop(&input) {
return Ok(input);
}
for operation in plan.iter() {
input = host
.hook_participant(*operation, &ctx, input, &decode)
.await?;
if should_stop(&input) {
break;
}
}
Ok(input)
}
.boxed_local()
}),
}
}
}
impl<A, R> Hook<A, Option<R>>
where
A: Clone + Serialize + DeserializeOwned + 'static,
R: Serialize + DeserializeOwned + 'static,
{
/// Return the first `Some` result. `None` corresponds to JavaScript `undefined`.
/// A defined JavaScript `null` is a decision and is decoded as the chosen result type.
pub fn bail(name: impl Into<String>) -> Self {
let decode: Decode<Option<R>> = Rc::new(|value| value.map(from_value).transpose());
let participant = decode.clone();
Self {
name: name.into(),
wit: None,
encode: Rc::new(|value| value.map(to_value).transpose()),
decode,
compose: Rc::new(move |host, ctx, plan, input: A| {
let decode = participant.clone();
async move {
for operation in plan.iter() {
let result = host
.hook_participant(*operation, &ctx, input.clone(), &decode)
.await?;
if result.is_some() {
return Ok(result);
}
}
Ok(None)
}
.boxed_local()
}),
}
}
}
impl<A: 'static, R: 'static> Hook<A, R> {
/// Read the hook identifier within its declaring orb.
pub fn name(&self) -> &str {
&self.name
}
/// Attach the prepared WIT identity required for participants from an isolated Orb.
pub fn signature(mut self, signature: impl Into<String>) -> Self {
self.wit = Some(signature.into());
self
}
pub(crate) fn identity(&self) -> Signature {
Signature {
native: Some(TypeId::of::<(A, R)>()),
wit: self.wit.clone(),
}
}
pub(crate) fn contribution<F, Fut>(
&self,
target: String,
priority: i32,
handle: F,
) -> Contribution
where
A: DeserializeOwned,
F: Fn(Context, A) -> Fut + 'static,
Fut: Future<Output = Result<R>> + 'static,
{
let handle = Rc::new(handle);
let portable = handle.clone();
let encode = self.encode.clone();
Contribution {
target,
hook: self.name.clone(),
signature: self.identity(),
priority,
local: Some(Rc::new(move |ctx, input| {
let input = input.downcast::<A>().map_err(|_| crate::orb::type_error());
let handle = handle.clone();
async move { Ok(Box::new(handle(ctx, *input?).await?) as Box<dyn std::any::Any>) }
.boxed_local()
})),
portable: Rc::new(move |ctx, input| {
let input = serde_json::from_value(input)
.map_err(|error| Error::invalid(core::Reason::Arguments, error.to_string()));
let (handle, encode) = (portable.clone(), encode.clone());
async move { encode(handle(ctx, input?).await?) }.boxed_local()
}),
}
}
}
/// A hook bound to its declaring service object. Keeping a handle does not retain the object.
pub struct HookHandle<A, R> {
pub(crate) host: Host,
pub(crate) orb: core::Id,
pub(crate) target: String,
pub(crate) object: Option<core::Id>,
pub(crate) operation: core::Id,
pub(crate) hook: Hook<A, R>,
}
impl<A, R> Clone for HookHandle<A, R> {
fn clone(&self) -> Self {
Self {
host: self.host.clone(),
orb: self.orb,
target: self.target.clone(),
object: self.object,
operation: self.operation,
hook: self.hook.clone(),
}
}
}
impl<A: 'static, R: 'static> HookHandle<A, R> {
/// Execute the caller's versioned plan under a managed declaring-object invocation.
/// Dropping this wait leaves physical execution owned by the host until it settles.
pub async fn run(&self, context: &Context, input: A) -> Result<R> {
if !self.host.same(&context.host) {
return Err(Error::invalid(
core::Reason::Context,
"foreign host context",
));
}
context.check()?;
self.host.ready_orb(self.orb).await?;
let plan = self
.host
.kernel()
.hook_plan(context.root, &self.target, &self.hook.name)?;
let call = self.host.kernel().begin_operation_with_context(
self.orb,
context.root,
self.operation,
self.object,
context.frame(),
context.issuer(),
)?;
let entered = self.host.context(call.root, Some(call.clone()))?;
let (host, compose) = (self.host.clone(), self.hook.compose.clone());
let receive = self.host.spawn_managed(call.id, async move {
compose(host, entered, plan, input).await
})?;
receive
.await
.map_err(|_| Error::invalid(core::Reason::Executor, "hook executor stopped"))?
}
}
pub(crate) fn to_value<T: Serialize>(value: T) -> Result<Value> {
serde_json::to_value(value)
.map_err(|error| Error::invalid(core::Reason::Result, error.to_string()))
}
pub(crate) fn from_value<T: DeserializeOwned>(value: Value) -> Result<T> {
serde_json::from_value(value)
.map_err(|error| Error::invalid(core::Reason::Result, error.to_string()))
}