feat(v1.1.1-kv): Rhai kv:: SDK module + ctx.event wiring
Wires the KV store into Rhai scripts via the handle pattern:
let widgets = kv::collection("widgets");
widgets.set("k", #{ n: 1 });
let v = widgets.get("k"); // value or () if absent
widgets.has("k") / widgets.delete("k")
let page = widgets.list(); // cursor-style pagination
`KvHandle` is a custom Rhai type holding `Arc<dyn KvService>` + the
per-call `Arc<SdkCallCx>`. Methods route async service calls through
`tokio::Handle::current().block_on(...)` — works because
`LocalExecutorClient` runs the script under `spawn_blocking` so a
runtime is reachable. The bridge surfaces `app_id` exclusively
through `cx.app_id`; no public-facing argument can spoof an app.
`TriggerEvent` lands in `picloud-shared` as the wire shape the
dispatcher will emit (KV + DeadLetter variants — KV exercised now,
DL hooks up with the dispatcher in commit 5/8). `SdkCallCx` and
`ExecRequest` grow `is_dead_letter_handler: bool` and
`event: Option<TriggerEvent>`. `engine.rs::build_ctx_map` flattens
the event into `ctx.event` for triggered handlers; direct ingress
leaves the key absent so scripts can `if "event" in ctx`.
Tests:
- 7 `sdk_kv.rs` integration tests covering the full Rhai surface
(round-trip, missing-key unit, has bool, delete was-present,
empty-collection rejection, cursor pagination, cross-app
isolation through the bridge).
- 3 new `engine.rs` tests pinning `ctx.event` shape per
design notes §4 (KV insert with value, delete with unit value,
direct invocations have no `event` key).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
105
crates/shared/src/trigger_event.rs
Normal file
105
crates/shared/src/trigger_event.rs
Normal file
@@ -0,0 +1,105 @@
|
||||
//! `TriggerEvent` — the description of the event that fired a script.
|
||||
//!
|
||||
//! Built by the dispatcher (in `manager-core`) from the outbox row and
|
||||
//! attached to the `ExecRequest` that's handed to `executor-core`. The
|
||||
//! Rhai bridge in `executor-core::engine::build_ctx_map` flattens this
|
||||
//! into `ctx.event` for the script.
|
||||
//!
|
||||
//! Living in `picloud-shared` so the dispatcher and the executor agree
|
||||
//! on the wire shape. Serializable so cluster mode (v1.3+) can ship
|
||||
//! ExecRequests over HTTP without rewriting this type.
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{DeadLetterId, ScriptId, TriggerId};
|
||||
|
||||
/// Operations a KV trigger can fire on. Stored as a lowercase string
|
||||
/// in `kv_trigger_details.ops` (Postgres `text[]`).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum KvEventOp {
|
||||
Insert,
|
||||
Update,
|
||||
Delete,
|
||||
}
|
||||
|
||||
impl KvEventOp {
|
||||
#[must_use]
|
||||
pub const fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::Insert => "insert",
|
||||
Self::Update => "update",
|
||||
Self::Delete => "delete",
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn from_wire(s: &str) -> Option<Self> {
|
||||
match s {
|
||||
"insert" => Some(Self::Insert),
|
||||
"update" => Some(Self::Update),
|
||||
"delete" => Some(Self::Delete),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Discriminated description of a triggering event. Lifted from the
|
||||
/// outbox row's payload at dispatch time. Each variant carries the
|
||||
/// fields the corresponding `ctx.event` shape exposes to the script.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "source", rename_all = "snake_case")]
|
||||
pub enum TriggerEvent {
|
||||
/// A KV insert / update / delete fired this handler.
|
||||
Kv {
|
||||
op: KvEventOp,
|
||||
collection: String,
|
||||
key: String,
|
||||
/// Present on `insert` and `update`. Absent on `delete`.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
value: Option<serde_json::Value>,
|
||||
},
|
||||
|
||||
/// A dead-letter row fired this handler. The original event is
|
||||
/// nested verbatim plus the dead-letter metadata the design notes
|
||||
/// §4 require.
|
||||
DeadLetter {
|
||||
dead_letter_id: DeadLetterId,
|
||||
original: Box<TriggerEvent>,
|
||||
attempts: u32,
|
||||
last_error: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
trigger_id: Option<TriggerId>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
script_id: Option<ScriptId>,
|
||||
first_attempt_at: DateTime<Utc>,
|
||||
last_attempt_at: DateTime<Utc>,
|
||||
},
|
||||
}
|
||||
|
||||
impl TriggerEvent {
|
||||
/// The `source` discriminant the script sees on `ctx.event.source`.
|
||||
#[must_use]
|
||||
pub const fn source(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Kv { .. } => "kv",
|
||||
Self::DeadLetter { .. } => "dead_letter",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convenience accessor on the dead-letter variant for places that
|
||||
/// already know they're handling a DL event. Pulled out so the
|
||||
/// dispatcher and the dashboard don't have to repeat the match.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DeadLetterEventDetail {
|
||||
pub dead_letter_id: DeadLetterId,
|
||||
pub original: TriggerEvent,
|
||||
pub attempts: u32,
|
||||
pub last_error: String,
|
||||
pub trigger_id: Option<TriggerId>,
|
||||
pub script_id: Option<ScriptId>,
|
||||
pub first_attempt_at: DateTime<Utc>,
|
||||
pub last_attempt_at: DateTime<Utc>,
|
||||
}
|
||||
Reference in New Issue
Block a user