Runtime Interface
The core runtime interface provides a single method for triggering agent execution:apps/x/packages/core/src/agents/runtime.ts:32
AgentRuntime Class
The main runtime implementation handles:- Run locking - Prevents concurrent execution of the same run
- Abort management - Handles user-requested stops
- Event streaming - Publishes events to the message bus
- State persistence - Saves events to runs repository
Constructor Dependencies
Constructor Dependencies
apps/x/packages/core/src/agents/runtime.ts:36Execution Flow
1. Run Initialization
apps/x/packages/core/src/agents/runtime.ts:71
2. Main Loop
The runtime executes in a loop until completion:apps/x/packages/core/src/agents/runtime.ts:83
3. Cleanup
Always executed, even on error:apps/x/packages/core/src/agents/runtime.ts:139
Agent Streaming
ThestreamAgent function implements the core agent logic:
Location:
apps/x/packages/core/src/agents/runtime.ts:672This is the heart of agent execution - it loads the agent, builds tools, runs the LLM loop, and handles tool execution.Setup Phase
apps/x/packages/core/src/agents/runtime.ts:696
Agent Loop
The agent executes in iterations:apps/x/packages/core/src/agents/runtime.ts:717
Tool Call Processing
After LLM response, process tool calls:apps/x/packages/core/src/agents/runtime.ts:917
Message Building
TheStreamStepMessageBuilder assembles assistant messages from LLM stream events:
apps/x/packages/core/src/agents/runtime.ts:224
Agent Loading
Agents are loaded from Markdown files or special built-in agents:apps/x/packages/core/src/agents/runtime.ts:313
Tool Building
Tools are mapped from agent definitions to executable functions:apps/x/packages/core/src/agents/runtime.ts:464
State Management
AgentState maintains execution state and processes events:
State Properties
State Properties
apps/x/packages/core/src/agents/runtime.ts:484Event Ingestion
Event Ingestion
apps/x/packages/core/src/agents/runtime.ts:585Abort Handling
The runtime supports graceful abort:apps/x/packages/core/src/agents/runtime.ts:84
Run Logging
Events are logged to JSONL files for debugging:apps/x/packages/core/src/agents/runtime.ts:193