Expand description
Write the native backend of a Cadencr custom tab.
A custom tab may ship a native executable that Cadencr starts per
conversation and talks to over stdin/stdout, one JSON object per line.
The protocol is the contract — see docs/TAB_BACKEND_PROTOCOL.md —
and this crate is one way to implement it:
use std::process::ExitCode;
use cadencr_tab_backend::{serve, Context, Error};
use serde_json::{json, Value};
fn handle(cx: &Context) -> Result<Value, Error> {
match cx.method() {
"greet" => {
let name: String = cx.params()?;
Ok(json!(format!("hello, {name}")))
}
other => Err(Error::unknown_method(other)),
}
}
fn main() -> ExitCode {
serve(handle)
}serve reads and checks the hello, answers ready, then runs each
request on a small worker pool (a slow request does not hold up the
others; responses may be written out of order), turns a handler panic into
an codes::INTERNAL_ERROR response, logs and skips malformed lines, and
returns once stdin closes. On Unix it also reserves stdin and stdout for
the protocol: anything else printed to stdout goes to stderr, the log,
and child processes see /dev/null as stdin.
Features: api adds a blocking client for the Cadencr API
(Context::api); conformance adds conformance::run, which checks
any backend binary against the protocol. testing drives a handler
in-process.
Re-exports§
pub use protocol::PROTOCOL_VERSION;
Modules§
- api
- A blocking client for the Cadencr API (feature
api). - codes
- Error codes this crate sends on its own. The protocol reserves them: a backend should use them only with the meaning given here.
- conformance
- Check a backend binary against the stdio protocol (feature
conformance). - protocol
- Reference types of the stdio protocol, version
PROTOCOL_VERSION. - testing
- Drive a handler in-process, without spawning a process.
Structs§
- Config
- How
serve_withruns requests. - Context
- One request: its method, its params and the backend’s
hello. - Error
- A failed request: a stable
SCREAMING_SNAKE_CASEcode and a message for a person.
Enums§
- Serve
Error - Why a run ended before stdin closed.
Constants§
- SDK
ready.sdkof backends built with this crate.
Traits§
- Handler
- Handles every request of one backend. Called concurrently from several
worker threads, hence
Send + Sync.
Functions§
- serve
- Run
handleras this process’s backend with the defaultConfig. Returns once stdin closes; use it asmain’s result. - serve_
with servewith an explicitConfig.