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

Copy Markdown

Protocol for managing Graph Nodes.

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.

Types

t()

@type t() :: term()

All the types that implement this protocol.

Functions

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.

build_node_lookup(graph_node, options \\ nil)

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

description(graph_node)

Obtain the description of a graph node.

Examples

When Set

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

When Not Set

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

description(graph_node, default)

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

Examples

When Set

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

When Not Set

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

handle(graph_node)

Obtain the handle of a graph node.

Examples

When Set

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

When Not Set

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

handle(graph_node, default)

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

Examples

When Set

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

When Not Set

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

handle_record(graph_node)

Return full handle record not just the handle name.

id(graph_node)

Obtain the id of a graph node.

Examples

When Set

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

When Not Set

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

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.

name(graph_node)

Obtain the name of a graph node.

Examples

When Set

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

When Not Set

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

name(graph_node, default)

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

Examples

When Set

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

When Not Set

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

node(graph_node, id)

Obtain a node nested under current node,

node_type(graph_node)

Obtain the node type for a graph node.

Example

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

nodes(graph_node, options \\ nil)

Obtain direct children node of current node.

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.

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

Process graph to execute tasks or run inference.

register_link(graph_node, graph, link, options)

Register a link with the graph node.

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

with_id(graph_node)

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

Examples

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

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}