pub struct Context { /* private fields */ }Expand description
What a method knows about the call it answers.
Implementations§
Source§impl Context
impl Context
Sourcepub fn try_new(
key: CallKey,
spec: &TypeSpec,
generic: Option<&CallTypes>,
cancelled: Cancellation,
) -> Result<Context, DecodeError>
pub fn try_new( key: CallKey, spec: &TypeSpec, generic: Option<&CallTypes>, cancelled: Cancellation, ) -> Result<Context, DecodeError>
The context of the call key names to the declaration spec
describes, with generic’s views for a generic one, validated
completely before the author’s code sees it. Its log lines go
nowhere.
§Errors
Refuses views that do not fit spec: views given to a nongeneric
declaration, missing from a generic one, or describing types the
declaration does not admit.
Sourcepub fn types(&self) -> Option<&ValidatedTypes>
pub fn types(&self) -> Option<&ValidatedTypes>
The admitted concrete type description, if this Tool is generic.
Sourcepub fn for_test(key: CallKey) -> Context
pub fn for_test(key: CallKey) -> Context
The context of a call made by a test rather than a host: the call
key names, of a nongeneric Tool, not cancelled. Context::cancel
makes it one that withdraws its call.
Sourcepub fn key(&self) -> CallKey
pub fn key(&self) -> CallKey
The call’s key, its run’s id and its own together: the address a Tool
hands the outside world as an idempotency key or a callback address,
and that reply answers.
Sourcepub fn schema(&self) -> Option<&str>
pub fn schema(&self) -> Option<&str>
For a call to a Tool with exactly one type parameter, the canonical
text of the JSON Schema of the concrete type the call binds to it,
made from the call’s types: what a model’s structured output, or any
other outside producer, is asked to fit. None for a nongeneric call
and for one whose Tool has several type parameters.
Sourcepub fn decode<T: Shaped>(&self, data: &str) -> Result<T, DecodeError>
pub fn decode<T: Shaped>(&self, data: &str) -> Result<T, DecodeError>
Reads JSON text data, such as a model’s output, as a value of the
concrete type the call binds to its one type parameter, by the rules
of Decoding (abi.md), and gives it as T, the Rust type the Tool
writes for it: Encoded for the type parameter itself.
§Errors
Refuses data that is not JSON of a value of that type, saying why:
the author turns the error into a value of the Tool’s declared result.
Refused too for a nongeneric call, and for one whose Tool has several
type parameters.
Sourcepub fn to_json<T: Shaped>(&self, value: &T) -> Result<String, DecodeError>
pub fn to_json<T: Shaped>(&self, value: &T) -> Result<String, DecodeError>
The canonical text of value’s JSON form (abi.md, The JSON
form) at the concrete type the call binds to its one type parameter,
as its CallTypes gives it: what a Tool hands a model.
§Errors
Refuses a value that is not one of that type, and a call that does not bind exactly one type parameter.
Sourcepub fn log(&self, level: Level, message: &str)
pub fn log(&self, level: Level, message: &str)
Writes one line to the run’s log at level, as a log frame naming
the call’s run and call. A call a test makes logs nowhere.
Sourcepub fn cancelled(&self) -> bool
pub fn cancelled(&self) -> bool
Whether the host asked to stop work on this call, or withdrew it: a held call that was closed is sent once more with this set, so the author can withdraw the question it asked, and whatever it answers is dropped. It proves nothing either way.
Sourcepub fn cancellation(&self) -> Cancellation
pub fn cancellation(&self) -> Cancellation
The call’s cancellation, which work that blocks can wait on beside
whatever else it waits for, instead of asking Context::cancelled
again and again.