Context and Streaming Message Management

Context and Streaming Message Management#

Obtain context environment#

During the operation of the graph engine, each node as well as the tools called by ToolManager will have an independent context environment StreamCtx, which can be obtained by calling current_ctx().

Contextual environment includes:

  • node_name: Current node name.

  • call_id: A unique UUID generated at node runtime. The ID is different for the same node in different run rounds.

  • tool_name: the name of the currently executing tool.

  • tool_call_id: Tool Execution ID. Corresponds to the id in the ToolCall class of the large language model’s output.

Obtain context information in the node:

from evofabric.core.graph import current_ctx
from evofabric.core.typing import State


def node_a(state: State):
    ctx = current_ctx()
    print(ctx.node_name)

Manage Streaming Messages#

We support registering custom streaming message Handlers via set_streaming_handler() to process streaming messages from node outputs.

Streaming message output is in dict format, containing the context environment and a payload field, where the value of payload is the streaming message output by the node through the put() method.

Registration Example:

from evofabric.core.graph import set_streaming_handler

def print_streaming_msg(x):
    """
    Example input:
    inside node_a
    {
        "node_name": "node_a",
        "call_id": "xxxx-xxx-xxx-xxxx",
        "payload": "xxxxx"
    }

    inside tool_b of agent node b
    {
        "node_name": "node_b",
        "call_id": "xxxx-xxx-xxx-xxxx",
        "tool_name": "tool_b",
        "tool_call_id": "tool-call-id-xxx-xxx-xxx",
        "payload": "xxxxx"
    }
    """
    print(x)

set_streaming_handler(print_streaming_msg)