Class: Turn
Makaio Framework / services-core / Turn
Class: Turn
Section titled “Class: Turn”Defined in: ../../../packages/services/core/src/session/entities/turn.ts:84
Represents a single turn in a session.
A turn is a semantic bracket: user message(s) → agent response(s). Turn encapsulates state and logic, while SessionOrchestrator handles bus events and emissions.
Design:
- Immutable identity (turnId, sessionId, agentIds set at creation)
- Mutable state (messages, completion tracking)
- State changes return results for orchestrator to act on
- No bus dependency (testable in isolation)
Example
Section titled “Example”const turn = new Turn({ sessionId: 'sess1', agentIds: ['agent1', 'agent2'] });turn.addMessage('msg1');
const change = turn.markAgentCompleted('agent1');// change.turnComplete === false (agent2 still pending)
const change2 = turn.markAgentCompleted('agent2');// change2.turnComplete === true, change2.result.success === trueConstructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Turn(
config):Turn
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:112
Parameters
Section titled “Parameters”config
Section titled “config”Returns
Section titled “Returns”Turn
Properties
Section titled “Properties”sessionId
Section titled “sessionId”
readonlysessionId:string
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:89
Session this turn belongs to
startedAt
Section titled “startedAt”
readonlystartedAt:number
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:95
Timestamp when turn was created
turnId
Section titled “turnId”
readonlyturnId:string
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:86
Unique turn identifier
turnNumber
Section titled “turnNumber”
readonlyturnNumber:number
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:92
Monotonic per-session ordinal (1-based).
Accessors
Section titled “Accessors”agentIds
Section titled “agentIds”Get Signature
Section titled “Get Signature”get agentIds(): readonly
string[]
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:132
Agents participating in this turn.
Returns
Section titled “Returns”readonly string[]
Immutable array of agent IDs
completedAgents
Section titled “completedAgents”Get Signature
Section titled “Get Signature”get completedAgents():
ReadonlySet<string>
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:157
Agents that have completed successfully.
Returns
Section titled “Returns”ReadonlySet<string>
Read-only set of completed agent IDs
erroredAgents
Section titled “erroredAgents”Get Signature
Section titled “Get Signature”get erroredAgents():
ReadonlyMap<string,string>
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:165
Agents that errored.
Returns
Section titled “Returns”ReadonlyMap<string, string>
Read-only map of agent ID to error message
initiator
Section titled “initiator”Get Signature
Section titled “Get Signature”get initiator():
object
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:141
Origin of this turn for loop prevention and audit. Returned as a defensive copy to avoid external mutation of internal state.
Returns
Section titled “Returns”Defensive copy of turn initiator metadata
source
Section titled “source”source:
"user"|"extension"|"system"
Origin category
sourceId?
Section titled “sourceId?”
optionalsourceId?:string
Identifier for the specific origin.
Examples: 'routine:validation', 'loop', 'subagent:xyz'.
messageIds
Section titled “messageIds”Get Signature
Section titled “Get Signature”get messageIds(): readonly
string[]
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:149
Messages sent during this turn.
Returns
Section titled “Returns”readonly string[]
Immutable array of message IDs
Methods
Section titled “Methods”addMessage()
Section titled “addMessage()”addMessage(
messageId):void
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:177
Add a message to this turn.
Parameters
Section titled “Parameters”messageId
Section titled “messageId”string
The message ID to add
Returns
Section titled “Returns”void
getContext()
Section titled “getContext()”getContext():
TurnContext
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:241
Get context for agent dispatch. Used when sending messages to agents for context propagation.
Returns
Section titled “Returns”Turn context with turnId and current messageId
Throws
Section titled “Throws”Error if no messages have been added yet
getContextForMessage()
Section titled “getContextForMessage()”getContextForMessage(
messageId):TurnContext
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:256
Get context for a specific message (for events emitted per-message).
Parameters
Section titled “Parameters”messageId
Section titled “messageId”string
The message ID to get context for
Returns
Section titled “Returns”Turn context with turnId and the specified messageId
getResult()
Section titled “getResult()”getResult():
TurnResult
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:228
Get the turn result (only meaningful when turn is complete).
Returns
Section titled “Returns”Turn result with success status and any errors
hasAgent()
Section titled “hasAgent()”hasAgent(
agentId):boolean
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:211
Check if an agent is part of this turn.
Parameters
Section titled “Parameters”agentId
Section titled “agentId”string
The agent ID to check
Returns
Section titled “Returns”boolean
True if agent is participating in this turn
isComplete()
Section titled “isComplete()”isComplete():
boolean
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:219
Check if the turn is complete (all agents finished).
Returns
Section titled “Returns”boolean
True if all agents have completed or errored
markAgentCompleted()
Section titled “markAgentCompleted()”markAgentCompleted(
agentId):TurnStateChange
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:186
Mark an agent as completed successfully.
Parameters
Section titled “Parameters”agentId
Section titled “agentId”string
The agent that completed
Returns
Section titled “Returns”State change indicating if turn is now complete
markAgentErrored()
Section titled “markAgentErrored()”markAgentErrored(
agentId,error):TurnStateChange
Defined in: ../../../packages/services/core/src/session/entities/turn.ts:197
Mark an agent as errored.
Parameters
Section titled “Parameters”agentId
Section titled “agentId”string
The agent that errored
string
Error message
Returns
Section titled “Returns”State change indicating if turn is now complete