Skip to content

Local TUI Runtime ​

InMemoryAgentTuiRuntime adapts a local AgentRuntime to the TUI runtime interface. The terminal app and the runtime run in the same process.

Use this mode when you want the shortest path from an agent definition to an interactive terminal.

Build The Runtime ​

csharp
using HPD.Agent;
using HPD.Agent.Providers.OpenAI;
using HPD.Agent.TUI;
using HPD.Agent.TUI.Runtime;

var agentId = "local-tui-agent";
var sessionId = "local-session";

var assistant = new AgentConfig()
    .WithOpenAI(model: "gpt-5-mini")
    .WithInstructions("You are concise and helpful.");

await using var agentRuntime = await new AgentRuntimeBuilder()
    .AddAgent(agentId, assistant)
    .BuildAsync();

The TUI runtime receives a built AgentRuntime. Provider setup, tools, and instructions belong on the AgentConfig; stores and shared middleware belong on the AgentRuntimeBuilder.

Run The TUI ​

csharp
var scope = new AgentTuiRuntimeScope(agentId, sessionId, "main");
await using var runtime = new InMemoryAgentTuiRuntime(agentRuntime, scope);
await using var app = HpdAgentTuiApp.Create(
    runtime,
    new DirectAgentTuiExecutionTarget(scope),
    tui => tui
        .AddAgentTuiDefaults()
        .AddSubAgentTui()
        .AddEventHandler("myapp.text", new TextMessageStreamHandler()));

await app.RunAsync();

HpdAgentTuiApp.RunAsync() ensures the scope, loads existing thread events if the runtime can provide them, starts observing new events, and then runs the terminal prompt loop.

Scope Defaults ​

If you do not pass a default scope to InMemoryAgentTuiRuntime, it uses:

text
agentId: the runtime's default agent
sessionId: local-session
threadId: main

Passing the scope explicitly is clearer in docs and production code because it makes the session and thread visible at the call site.

AgentTuiRuntimeScope requires non-empty agentId, sessionId, and threadId values. Invalid scope identifiers fail at construction rather than being converted into empty route or store keys.

Session Store Behavior ​

If the requested session or thread does not exist, InMemoryAgentTuiRuntime creates it through the AgentRuntime before the TUI starts.

The TUI runtime also exposes session, thread, and agent operations. Agent operations go through runtime.Agents: listing shows code and stored agents with their source, and creating, updating, or deleting applies to stored agents only, with the same validation and read-only rules as the hosted API.

AddSubAgentTui() adds /subagents when the runtime supports session and thread operations. It lists the threads the current thread created; selecting one opens it with the agent that last ran on it.

Thread Projection And Compaction ​

The local runtime uses the same append-only thread journal and projection model as other HPD runtime surfaces. Compaction appends a THREAD_HISTORY_COMPACTION_CHECKPOINT, and the transcript projection applies it like any other committed event.

Current local TUI thread-fork APIs mirror metadata-only fork requests. Do not document a TUI-level fork-compaction toggle unless the runtime API adds one; configure compaction on the agent's definition.

Agent Switching ​

The local runtime reports CanSwitchAgents == false. The TUI runs the agent its scope names. A local TUI can list and manage the runtime's agents, but it does not switch the scope's agent the way the hosted runtime can.

Event Rendering ​

The TUI shell receives events; it does not automatically know how your app wants every event family displayed. Add handlers for the transcript behavior you need:

csharp
tui => tui
    .AddAgentTuiDefaults()
    .AddEventHandler("myapp.text", new TextMessageStreamHandler())

The TextMessageStreamHandler shown here is application code, not a guaranteed built-in default.

HpdAgentTuiApp receives bounded event batches in ThreadSequenceNumber order. Historical, catch-up, and live batches use the same handlers; the applied cursor advances only after the entire batch projects successfully. During live operation the app tracks the authoritative active execution. Enter always submits the message: while a turn runs it joins that turn, and otherwise it starts one. InMemoryAgentTuiRuntime passes it to the AgentRuntime, which applies the same rules as a hosted runtime.

Built for production .NET agent applications.