pub struct ConversationGraph { /* private fields */ }Expand description
Directed acyclic graph of conversation nodes.
§Invariants
- Every
NodeIdreturned byadd_nodeis unique and stable for the lifetime of the graph. add_edgerejects any edge that would create a cycle.linearizealways returns a valid topological ordering.
Implementations§
Source§impl ConversationGraph
impl ConversationGraph
Sourcepub fn add_node(
&mut self,
role: impl Into<String>,
content: impl Into<String>,
tokens: usize,
) -> NodeId
pub fn add_node( &mut self, role: impl Into<String>, content: impl Into<String>, tokens: usize, ) -> NodeId
Add a new node to the graph and return its NodeId.
Sourcepub fn add_edge(&mut self, from: NodeId, to: NodeId) -> Result<(), GraphError>
pub fn add_edge(&mut self, from: NodeId, to: NodeId) -> Result<(), GraphError>
Add a directed edge from -> to.
§Errors
GraphError::NodeNotFoundif eitherfromortois unknown.GraphError::InvalidEdgeiffrom == to(self-loop).GraphError::CycleDetectedif the edge would create a cycle.
Sourcepub fn branch_from(&mut self, node_id: NodeId) -> Result<NodeId, GraphError>
pub fn branch_from(&mut self, node_id: NodeId) -> Result<NodeId, GraphError>
Clone the node at node_id, attach the clone as a child, and return
the clone’s NodeId.
§Errors
Returns GraphError::NodeNotFound if node_id is unknown.
Sourcepub fn merge_paths(
&mut self,
path_a: &[NodeId],
path_b: &[NodeId],
) -> Result<NodeId, GraphError>
pub fn merge_paths( &mut self, path_a: &[NodeId], path_b: &[NodeId], ) -> Result<NodeId, GraphError>
Create a merge node that references both path_a and path_b.
The merge node has role "merge" and empty content. Edges are added
from the last node of each path to the merge node.
§Errors
GraphError::InvalidEdgeif either path is empty.- Propagates errors from
add_edge.
Sourcepub fn linearize(&self, root: NodeId) -> Result<Vec<NodeId>, GraphError>
pub fn linearize(&self, root: NodeId) -> Result<Vec<NodeId>, GraphError>
Topological sort of all nodes reachable from root using Kahn’s algorithm.
Returns nodes in breadth-first topological order.
§Errors
GraphError::NodeNotFoundifrootis unknown.GraphError::CycleDetectedif the reachable subgraph is not a DAG (should not happen if all edges were added throughadd_edge).
Sourcepub fn path_tokens(&self, path: &[NodeId]) -> usize
pub fn path_tokens(&self, path: &[NodeId]) -> usize
Sum the token counts of all nodes in path.
Unknown node IDs are silently skipped.
Sourcepub fn get_node(&self, id: NodeId) -> Option<&ConversationNode>
pub fn get_node(&self, id: NodeId) -> Option<&ConversationNode>
Borrow the node for id, if it exists.
Trait Implementations§
Source§impl Debug for ConversationGraph
impl Debug for ConversationGraph
Source§impl Default for ConversationGraph
impl Default for ConversationGraph
Source§fn default() -> ConversationGraph
fn default() -> ConversationGraph
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for ConversationGraph
impl RefUnwindSafe for ConversationGraph
impl Send for ConversationGraph
impl Sync for ConversationGraph
impl Unpin for ConversationGraph
impl UnsafeUnpin for ConversationGraph
impl UnwindSafe for ConversationGraph
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request