Skip to main content

Out

Struct Out 

Source
pub struct Out<'id, Op: Operation> { /* private fields */ }
Expand description

Where a decoder writes one reply. The 'id brand ties it, and every part handle it hands out, to that one reply: a handle cannot be used with another reply’s writer or kept past its own.

ⓘ
use rig_core::operation::Completion;
use rig_core::wire::Out;

// A text part opened on one reply cannot grow on another.
fn cross<'a, 'b>(a: &mut Out<'a, Completion>, b: &mut Out<'b, Completion>) {
    let part = a.text();
    b.push_text(&part, "x");
}
ⓘ
use rig_core::operation::{Completion, TextPart};
use rig_core::wire::Out;

// A handle cannot outlive its reply to be used on a later one.
struct Stash(Option<TextPart<'static>>);

fn keep<'id>(stash: &mut Stash, out: &mut Out<'id, Completion>) {
    stash.0 = Some(out.text());
}

Implementations§

Source§

impl<'id> Out<'id, Completion>

Source

pub fn text(&mut self) -> TextPart<'id>

Open a text part. Nothing is emitted until its first fragment.

Source

pub fn push_text(&mut self, part: &TextPart<'id>, text: &str)

Append to an open text part.

Source

pub fn text_params( &mut self, part: &TextPart<'id>, additional_params: AdditionalParams, )

Merge provider metadata into an open text part. Metadata is content: the part starts here if no text started it.

Source

pub fn close_text(&mut self, part: TextPart<'id>)

Close a text part. One with neither text nor metadata is dropped.

Source

pub fn reasoning(&mut self) -> ReasoningPart<'id>

Open a reasoning part. Nothing is emitted until its first fragment.

Source

pub fn push_reasoning(&mut self, part: &ReasoningPart<'id>, text: &str)

Append to an open reasoning part.

Source

pub fn close_reasoning(&mut self, part: ReasoningPart<'id>, seal: Seal)

Close a reasoning part, sealed to the reply’s issuer. One with nothing to replay is dropped.

Source

pub fn reasoning_block(&mut self, reasoning: Reasoning)

A whole reasoning part the provider sent in one piece.

Source

pub fn call( &mut self, id: CallId, name: ToolName, ) -> Result<CallPart<'id>, ProviderError>

Open a tool call with its id and name. A provider id already used by another call of this reply is ProviderError::DuplicateCallId.

Both are required, so no call opens without an id:

ⓘ
use rig_core::message::ToolName;
use rig_core::operation::Completion;
use rig_core::wire::Out;

fn idless(out: &mut Out<'_, Completion>, name: ToolName) {
    let _ = out.call(None, name);
}

or without a name:

ⓘ
use rig_core::message::CallId;
use rig_core::operation::Completion;
use rig_core::wire::Out;

fn nameless(out: &mut Out<'_, Completion>, id: CallId) {
    let _ = out.call(id, "");
}
Source

pub fn push_arguments(&mut self, part: &CallPart<'id>, json: &str)

Append a fragment of an open call’s argument JSON.

Source

pub fn decorate_call( &mut self, part: &CallPart<'id>, signature: Option<String>, additional_params: Option<Value>, )

Attach a provider signature and metadata to an open call.

Source

pub fn close_call(&mut self, part: CallPart<'id>) -> Result<(), ProviderError>

Close a call: it becomes visible. Arguments that do not parse are ProviderError::MalformedToolInput.

Source

pub fn abandon_call(&mut self, part: CallPart<'id>)

Drop an open call: it never becomes visible.

Source

pub fn tool_call(&mut self, call: ToolCall) -> Result<(), ProviderError>

A whole tool call the provider sent in one piece.

Source

pub fn image(&mut self, image: Image)

An image part.

Source

pub fn content( &mut self, content: AssistantContent, ) -> Result<(), ProviderError>

A whole part of an already assembled response.

Source

pub fn message_id(&mut self, id: impl Into<String>)

Record the assistant message id. It outranks the one the end names.

Source

pub fn issued_by(&mut self, issuer: impl Into<Issuer>)

Name the issuer of this reply’s reasoning: a gateway relaying another provider’s models.

Source

pub fn call_fragment( &mut self, index: usize, fragment: CallFragment<'_>, ) -> Result<(), ProviderError>

Buffer one fragment of the tool call the provider streams under index. The call opens when its id and name are both known; a provider id another call already has is ProviderError::DuplicateCallId.

Source

pub fn announce_pending(&mut self, index: usize, arguments: Value)

Arguments the provider announced for the buffered call at index, used only if no fragment arrives.

Source

pub fn decorate_pending( &mut self, provider_id: &str, signature: Option<String>, additional_params: Option<Value>, )

Attach a signature and metadata to the buffered call the provider names provider_id. What it already has wins.

Source

pub fn decorate_pending_at( &mut self, index: usize, signature: Option<String>, additional_params: Option<Value>, )

Attach a signature and metadata to the buffered call at index.

Source

pub fn pending_calls(&self) -> Vec<usize>

The wire indices of the buffered calls, in order.

Source

pub fn pending_id(&self, index: usize) -> Option<String>

The provider id of the buffered call at index, when it has one.

Source

pub fn pending_name(&self, index: usize) -> String

The tool name the buffered call at index has so far.

Source

pub fn pending_has_arguments(&self, index: usize) -> bool

Whether the buffered call at index received argument bytes that are not blank, or announced arguments.

Source

pub fn close_pending( &mut self, index: usize, if_malformed: IfMalformed, ) -> Result<(), ProviderError>

Close the buffered call at index: it becomes visible, under the provider’s id or, for a wire that sent none, one rig issues. A call with no name is dropped; if_malformed decides for one whose arguments do not parse.

Source

pub fn drop_pending(&mut self, index: usize)

Drop the buffered call at index: it never becomes visible.

Source§

impl<'id, Op: Operation> Out<'id, Op>

Source

pub fn end(self, end: Op::End) -> Flow

End the reply with what the provider sent at its end. It consumes the writer: nothing is written after the end.

Source

pub fn raw(&mut self, raw: Value)

Record the reply’s provider document, the response’s raw, for any operation. A whole JSON body the transport reported outranks it, and a raw the decoder writes onto its response is replaced: a decoder whose reply is not one JSON document records it here.

Source

pub fn unknown(&mut self, payload: UnknownPayload)

A payload the provider sent that this decoder does not model. It reaches the consumer as Item::Unknown.

Source§

impl<Op: Operation<Emit = Free>> Out<'_, Op>

Source

pub fn event(&mut self, event: Op::Event)

One event of the reply.

Only an operation whose decoders build their own events has this; a completion’s events come from its part handles:

ⓘ
use rig_core::operation::Completion;
use rig_core::streaming::StreamEvent;
use rig_core::wire::Out;

fn reinject(out: &mut Out<'_, Completion>, seen: &StreamEvent) {
    out.event(seen.clone());
}

An event rebuilt from its serialized form is no different:

ⓘ
use rig_core::operation::Completion;
use rig_core::streaming::Transcript;
use rig_core::wire::Out;

fn rebuild(out: &mut Out<'_, Completion>, recorded: serde_json::Value) {
    let Ok(transcript) = Transcript::parse(recorded) else { return };
    for event in transcript.events() {
        out.event(event.clone());
    }
}

Auto Trait Implementations§

§

impl<'id, Op> Freeze for Out<'id, Op>
where &'id Mutex<Shared<Op>>: Freeze,

§

impl<'id, Op> RefUnwindSafe for Out<'id, Op>
where &'id Mutex<Shared<Op>>: RefUnwindSafe,

§

impl<'id, Op> Send for Out<'id, Op>
where &'id Mutex<Shared<Op>>: Send,

§

impl<'id, Op> Sync for Out<'id, Op>
where &'id Mutex<Shared<Op>>: Sync,

§

impl<'id, Op> Unpin for Out<'id, Op>
where &'id Mutex<Shared<Op>>: Unpin,

§

impl<'id, Op> UnsafeUnpin for Out<'id, Op>
where &'id Mutex<Shared<Op>>: UnsafeUnpin,

§

impl<'id, Op> UnwindSafe for Out<'id, Op>
where &'id Mutex<Shared<Op>>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more