GenAI.Graph.NodeProtocol protocol (GenAI Core v0.2.0)

Protocol for managing Graph Nodes.

Link to this section Summary

Types

t()

All the types that implement this protocol.

Functions

build handle lookup table for graph root, including local handles that can override standard handles based on scope.

build Graph Root node lookup allowing nodes to link across descendents/siblings to far away elements.

Obtain the description of a graph node.

Obtain the description of a graph node, or return a default value if the description is nil.

Obtain the handle of a graph node.

Obtain the handle of a graph node, or return a default value if the handle is nil.

Return full handle record not just the handle name.

Obtain the id of a graph node.

Obtain inbound links of a graph node.

Obtain the name of a graph node.

Obtain the name of a graph node, or return a default value if the name is nil.

Obtain a node nested under current node,

Obtain the node type for a graph node.

Obtain direct children node of current node.

Obtain outbound links of a graph node.

Register a link with the graph node.

Ensure the graph node has an id, generating one if necessary.

Link to this section Types

@type t() :: term()

All the types that implement this protocol.

Link to this section Functions

Link to this function

build_handle_lookup(graph_node, options \\ nil)

build handle lookup table for graph root, including local handles that can override standard handles based on scope.

Link to this function

build_node_lookup(graph_node, options \\ nil)

build Graph Root node lookup allowing nodes to link across descendents/siblings to far away elements.

Link to this function

description(graph_node)

Obtain the description of a graph node.

examples

Examples

when-set

When Set

iex> node = %GenAI.Graph.Node{description: "B"}
...> GenAI.Graph.NodeProtocol.description(node)
{:ok, "B"}

when-not-set

When Not Set

iex> node = %GenAI.Graph.Node{description: nil}
...> GenAI.Graph.NodeProtocol.description(node)
{:error, {:description, :is_nil}}
Link to this function

description(graph_node, default)

Obtain the description of a graph node, or return a default value if the description is nil.

examples

Examples

when-set

When Set

iex> node = %GenAI.Graph.Node{description: "B"}
...> GenAI.Graph.NodeProtocol.description(node, "default")
{:ok, "B"}

when-not-set

When Not Set

iex> node = %GenAI.Graph.Node{description: nil}
...> GenAI.Graph.NodeProtocol.description(node, "default")
{:ok, "default"}
Link to this function

handle(graph_node)

Obtain the handle of a graph node.

examples

Examples

when-set

When Set

iex> node = %GenAI.Graph.Node{handle: :foo}
...> GenAI.Graph.NodeProtocol.handle(node)
{:ok, :foo}

when-not-set

When Not Set

iex> node = %GenAI.Graph.Node{handle: nil}
...> GenAI.Graph.NodeProtocol.handle(node)
{:error, {:handle, :is_nil}}
Link to this function

handle(graph_node, default)

Obtain the handle of a graph node, or return a default value if the handle is nil.

examples

Examples

when-set

When Set

iex> node = %GenAI.Graph.Node{handle: :foo}
...> GenAI.Graph.NodeProtocol.handle(node, :default)
{:ok, :foo}

when-not-set

When Not Set

iex> node = %GenAI.Graph.Node{handle: nil}
...> GenAI.Graph.NodeProtocol.handle(node, :default)
{:ok, :default}
Link to this function

handle_record(graph_node)

Return full handle record not just the handle name.

Obtain the id of a graph node.

examples

Examples

when-set

When Set

iex> node = %GenAI.Graph.Node{id: UUID.uuid4()}
...> GenAI.Graph.NodeProtocol.id(node)
{:ok, node.id}

when-not-set

When Not Set

iex> node = %GenAI.Graph.Node{id: nil}
...> GenAI.Graph.NodeProtocol.id(node)
{:error, {:id, :is_nil}}
Link to this function

inbound_links(graph_node, graph, options)

@spec inbound_links(
  graph_node :: GenAI.Types.Graph.graph_node(),
  graph :: GenAI.Types.Graph.graph(),
  options :: map()
) :: {:ok, [GenAI.Types.Graph.graph_link_id()]} | {:error, term()}

Obtain inbound links of a graph node.

Link to this function

name(graph_node)

Obtain the name of a graph node.

examples

Examples

when-set

When Set

iex> node = %GenAI.Graph.Node{name: "A"}
...> GenAI.Graph.NodeProtocol.name(node)
{:ok, "A"}

when-not-set

When Not Set

iex> node = %GenAI.Graph.Node{name: nil}
...> GenAI.Graph.NodeProtocol.name(node)
{:error, {:name, :is_nil}}
Link to this function

name(graph_node, default)

Obtain the name of a graph node, or return a default value if the name is nil.

examples

Examples

when-set

When Set

iex> node = %GenAI.Graph.Node{name: "A"}
...> GenAI.Graph.NodeProtocol.name(node, "default")
{:ok, "A"}

when-not-set

When Not Set

iex> node = %GenAI.Graph.Node{name: nil}
...> GenAI.Graph.NodeProtocol.name(node, "default")
{:ok, "default"}
Link to this function

node(graph_node, id)

Obtain a node nested under current node,

Link to this function

node_type(graph_node)

Obtain the node type for a graph node.

example

Example

iex> node = %GenAI.Graph.Node{id: UUID.uuid4()}
...> GenAI.Graph.NodeProtocol.node_type(node)
{:ok, GenAI.Graph.Node}
Link to this function

nodes(graph_node, options \\ nil)

Obtain direct children node of current node.

Link to this function

outbound_links(graph_node, graph, options)

@spec outbound_links(
  graph_node :: GenAI.Types.Graph.graph_node(),
  graph :: GenAI.Types.Graph.graph(),
  options :: map()
) :: {:ok, [GenAI.Types.Graph.graph_link_id()]} | {:error, term()}

Obtain outbound links of a graph node.

Link to this function

process_node(graph_node, graph_link, graph_container, session, context, options)

Process graph to execute tasks or run inference.

Link to this function

register_link(graph_node, graph, link, options)

Register a link with the graph node.

examples

Examples

iex> n1 = UUID.uuid5(:oid, "node-1")
...> n2 = UUID.uuid5(:oid, "node-2")
...> n = %GenAI.Graph.Node{id: n1}
...> link = GenAI.Graph.Link.new(n1, n2)
...> link_id = link.id
...> {:ok, updated} = GenAI.Graph.NodeProtocol.register_link(n, %{}, link, nil)
...> updated
%GenAI.Graph.Node{outbound_links: %{default: [^link_id]}} = updated
Link to this function

with_id(graph_node)

Ensure the graph node has an id, generating one if necessary.

examples

Examples

when-already-set

When Already Set

iex> node = %GenAI.Graph.Node{id: UUID.uuid4()}
...> {:ok, node2} = GenAI.Graph.NodeProtocol.with_id(node)
...> %{was_nil: is_nil(node.id), is_nil: is_nil(node2.id), id_change: node.id != node2.id}
%{was_nil: false, is_nil: false, id_change: false}

when-not-set

When Not Set

iex> node = %GenAI.Graph.Node{id: nil}
...> {:ok, node2} = GenAI.Graph.NodeProtocol.with_id(node)
...> %{was_nil: is_nil(node.id), is_nil: is_nil(node2.id), id_change: node.id != node2.id}
%{was_nil: true, is_nil: false, id_change: true}