> ## Documentation Index
> Fetch the complete documentation index at: https://docs.inworld.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Edge Demo

This demo shows how an `Edge` works in the graph node system.

## Run the Template

1. Go to `Assets/InworldRuntime/Scenes/Nodes` and play the `EdgeDemo` scene.
   <img src="https://mintcdn.com/inworldai/09jBaDxLDhFWSIuG/img/unity/framework/Edge00.png?fit=max&auto=format&n=09jBaDxLDhFWSIuG&q=85&s=f4410838e50d5834ee88eb790122fd18" alt="Edge00" width="1119" height="651" data-path="img/unity/framework/Edge00.png" />
2. The overall experience is the same as in the [LLM Node Demo](./llm).
   Type your text; the AI agent will respond.

<img src="https://mintcdn.com/inworldai/09jBaDxLDhFWSIuG/img/unity/framework/Edge.gif?s=33f08ca4e060c7b68f52ba3e57e8e43f" alt="Edge" width="1920" height="1080" data-path="img/unity/framework/Edge.gif" />

## Understanding the Graph

`NodeConnectionCanvas` contains an `InworldGraphExecutor`.

<img src="https://mintcdn.com/inworldai/09jBaDxLDhFWSIuG/img/unity/framework/Edge01.png?fit=max&auto=format&n=09jBaDxLDhFWSIuG&q=85&s=7a138ab7466887c107db296b4f2650af" alt="EdgeNode01" width="1365" height="714" data-path="img/unity/framework/Edge01.png" />

The graph contains two nodes: a custom node `TextToLLM` and an `LLMNode`, connected by an edge `Edge_SampleText_To_LLM`.

`TextToLLM` is the `StartNode` and `LLMNode` is the `EndNode`.

<img src="https://mintcdn.com/inworldai/09jBaDxLDhFWSIuG/img/unity/framework/Edge03.png?fit=max&auto=format&n=09jBaDxLDhFWSIuG&q=85&s=382461bcf6e6e039ba7db6f0291e9416" alt="EdgeNode01" width="1002" height="238" data-path="img/unity/framework/Edge03.png" />

You can also see this connection in the Graph Editor.

<img src="https://mintcdn.com/inworldai/09jBaDxLDhFWSIuG/img/unity/framework/Edge02.png?fit=max&auto=format&n=09jBaDxLDhFWSIuG&q=85&s=ffa8c2eeb66a46dc8dfceb1a1c8868ba" alt="EdgeNode01" width="1080" height="339" data-path="img/unity/framework/Edge02.png" />

### CustomNode details

This node is a `TxtToPromptSampleNodeAsset`, implemented by inheriting from `CustomNodeAsset`.

In its overridden `ProcessBaseData()`, it inspects the input `InworldBaseData`, wraps it into an `LLMChatRequest` (the type accepted by `LLMNode`), and sends it onward.

```c# TxtToPromptSampleNodeAsset.cs theme={"system"}
public class TxtToPromptSampleNodeAsset : CustomNodeAsset
{
    protected override InworldBaseData ProcessBaseData(InworldVector<InworldBaseData> inputs)
    {
        string strInput = "";
        if (inputs != null)
        {
            for (int i = 0; i < inputs.Size; i++)
            {
                InworldText text = new InworldText(inputs[i]);
                if (text.IsValid)
                {
                    strInput += text.Text;
                }
            }
        }
        InworldVector<InworldMessage> messages = new InworldVector<InworldMessage>();
        InworldMessage message = new InworldMessage();
        message.Role = Role.User;
        message.Content = strInput;
        messages.Add(message);
        return new LLMChatRequest(messages);
    }
}
```

### Edge

This edge uses the default behavior: it simply forwards all output from the previous node to the next node.

### InworldController

The `InworldController` is also simple; it contains only one primitive module: `LLM`.

<img src="https://mintcdn.com/inworldai/pDD5vvrZThONehMe/img/unity/framework/LLM05.png?fit=max&auto=format&n=pDD5vvrZThONehMe&q=85&s=911a11e32174478b4c9207eb184d6569" alt="LLMNode04" width="1362" height="651" data-path="img/unity/framework/LLM05.png" />

## Workflow

1. When the game starts, `InworldController` initializes its only module, `LLMModule`, which creates the `LLMInterface`.
2. Next, `InworldGraphExecutor` initializes its graph asset by calling each component’s `CreateRuntime()`.

In this case, `TextToPromptNode` initializes immediately.

When `LLMNode.CreateRuntime()` is called, it uses the created `LLMInterface` as input.

3. After initialization, the graph calls `Compile()` and returns the executor handle.

4. After compilation, the `OnGraphCompiled` event is invoked.

In this demo, `NodeConnectionTemplate` subscribes to it and enables the UI components. Users can then interact with the graph system.

```c# CustomNodeAsset.cs theme={"system"}
public override bool CreateRuntime(InworldGraphAsset graphAsset)
{
    m_Graph = graphAsset;
    m_Executor = new EdgeNodeProcessExecutor(ProcessBaseDataIO);
    Runtime = new EdgeNodeWrapper(NodeName, m_Executor);
    return Runtime?.IsValid ?? false;
}

protected virtual void ProcessBaseDataIO(IntPtr contextPtr)
{
    try
    {
        // Here is the virtual ProcessBaseData for override.
        EdgeNodeProcessExecutor.SetLastOutput(ProcessBaseData(EdgeNodeProcessExecutor.LastIntputs));
    }
    ...
}
```

```c# NodeConnectionTemplate.cs theme={"system"}
protected override void OnGraphCompiled(InworldGraphAsset obj)
{
    foreach (InworldUIElement element in m_UIElements)
        element.Interactable = true;

}
```

5. After the UI is initialized, send the input text to the graph.

6. Calling `ExecuteGraphAsync()` eventually produces a result and invokes `OnGraphResult()`, which `NodeConnectionTemplate` subscribes to in order to receive the data.

```c# NodeConnectionTemplate.cs theme={"system"}
protected override void OnGraphResult(InworldBaseData obj)
{
    LLMChatResponse response = new LLMChatResponse(obj);
    Debug.Log(obj);
    if (response.IsValid && response.Content != null && response.Content.IsValid)
    {
        string message = response.Content.ToString();
        InsertBubble(m_BubbleLeft, Role.Assistant.ToString(), message);
    }
}
```
