1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//! Session state management for dialogue execution.
//!
//! This module contains the state machine for managing dialogue sessions,
//! including broadcast and sequential execution modes.
use super::super::{AgentError, Payload, PayloadMessage};
use super::message::{DialogueMessage, MessageMetadata, MessageOrigin, Speaker};
use super::{BroadcastOrder, Dialogue, DialogueTurn, ParticipantInfo};
use tokio::task::JoinSet;
use tracing::{info, trace};
/// Internal state for broadcast execution.
pub(super) struct BroadcastState {
pub(super) pending: JoinSet<(usize, String, Result<String, AgentError>)>,
pub(super) order: BroadcastOrder,
pub(super) buffered: Vec<Option<Result<String, AgentError>>>,
pub(super) next_emit: usize,
pub(super) current_turn: usize,
/// For Completion mode: stores (participant_idx, participant_name) for each buffered result
pub(super) completion_metadata: Vec<(usize, String)>,
}
impl BroadcastState {
pub(super) fn new(
pending: JoinSet<(usize, String, Result<String, AgentError>)>,
order: BroadcastOrder,
participant_count: usize,
current_turn: usize,
) -> Self {
let buffered = match order {
BroadcastOrder::Completion => Vec::new(),
BroadcastOrder::ParticipantOrder => std::iter::repeat_with(|| None)
.take(participant_count)
.collect::<Vec<Option<Result<String, AgentError>>>>(),
BroadcastOrder::Explicit(_) => std::iter::repeat_with(|| None)
.take(participant_count)
.collect::<Vec<Option<Result<String, AgentError>>>>(),
};
Self {
pending,
order,
buffered,
next_emit: 0,
current_turn,
completion_metadata: Vec::new(),
}
}
pub(super) fn record_result(
&mut self,
idx: usize,
participant_name: String,
result: Result<String, AgentError>,
) {
match self.order {
BroadcastOrder::Completion => {
// For Completion mode, append to buffered with metadata
let content_len = result.as_ref().map(|s| s.len()).unwrap_or(0);
trace!(
target = "llm_toolkit::dialogue",
participant = %participant_name,
participant_index = idx,
content_length = content_len,
is_error = result.is_err(),
turn = self.current_turn,
"Recording result to buffered (Completion mode)"
);
self.buffered.push(Some(result));
self.completion_metadata.push((idx, participant_name));
}
BroadcastOrder::ParticipantOrder => {
if idx < self.buffered.len() {
let content_len = result.as_ref().map(|s| s.len()).unwrap_or(0);
trace!(
target = "llm_toolkit::dialogue",
participant = %participant_name,
participant_index = idx,
content_length = content_len,
is_error = result.is_err(),
turn = self.current_turn,
"Recording result to buffered (ParticipantOrder mode)"
);
self.buffered[idx] = Some(result);
}
}
BroadcastOrder::Explicit(_) => {
// For Explicit order mode, use participant order approach for now
if idx < self.buffered.len() {
let content_len = result.as_ref().map(|s| s.len()).unwrap_or(0);
trace!(
target = "llm_toolkit::dialogue",
participant = %participant_name,
participant_index = idx,
content_length = content_len,
is_error = result.is_err(),
turn = self.current_turn,
"Recording result to buffered (Explicit order mode)"
);
self.buffered[idx] = Some(result);
}
}
}
}
pub(super) fn try_emit(
&mut self,
dialogue: &mut Dialogue,
) -> Option<Result<DialogueTurn, AgentError>> {
match self.order {
BroadcastOrder::Completion => {
// For Completion mode, emit results in completion order
if self.buffered.is_empty() {
return None;
}
// Pop from front (FIFO - first completed, first emitted)
let result = self.buffered.remove(0).expect("checked is_empty");
let (idx, participant_name) = self.completion_metadata.remove(0);
match result {
Ok(content) => {
// Content is already stored in MessageStore by session.rs
// Just create and return the DialogueTurn
let participant = &dialogue.participants[idx];
let turn = DialogueTurn {
speaker: Speaker::agent(
participant_name.clone(),
participant.persona.role.clone(),
),
content: content.clone(),
};
info!(
target = "llm_toolkit::dialogue",
mode = "broadcast_completion",
participant = %participant_name,
participant_index = idx,
total_participants = dialogue.participants.len(),
event = "dialogue_turn_emitted"
);
Some(Ok(turn))
}
Err(err) => Some(Err(err)),
}
}
BroadcastOrder::ParticipantOrder => {
let participant_total = dialogue.participants.len();
if self.next_emit >= participant_total {
return None;
}
let idx = self.next_emit;
let slot_ready = self
.buffered
.get(idx)
.and_then(|slot| slot.as_ref())
.is_some();
if !slot_ready {
return None;
}
let result = self.buffered[idx].take().expect("checked is_some");
self.next_emit += 1;
match result {
Ok(content) => {
let participant = &dialogue.participants[idx];
let participant_name = participant.name().to_string();
// Store in MessageStore
let metadata =
MessageMetadata::new().with_origin(MessageOrigin::AgentGenerated);
let message = DialogueMessage::new(
self.current_turn,
Speaker::agent(
participant_name.clone(),
participant.persona.role.clone(),
),
content.clone(),
)
.with_metadata(&metadata);
dialogue.message_store.push(message);
let turn = DialogueTurn {
speaker: Speaker::agent(
participant_name.clone(),
participant.persona.role.clone(),
),
content: content.clone(),
};
info!(
target = "llm_toolkit::dialogue",
mode = "broadcast_participant_order",
participant = %participant_name,
participant_index = idx,
total_participants = participant_total,
event = "dialogue_turn_emitted"
);
Some(Ok(turn))
}
Err(err) => Some(Err(err)),
}
}
BroadcastOrder::Explicit(_) => {
// For Explicit order mode, use participant order approach for now
let participant_total = dialogue.participants.len();
if self.next_emit >= participant_total {
return None;
}
let idx = self.next_emit;
let slot_ready = self
.buffered
.get(idx)
.and_then(|slot| slot.as_ref())
.is_some();
if !slot_ready {
return None;
}
let result = self.buffered[idx].take().expect("checked is_some");
self.next_emit += 1;
match result {
Ok(content) => {
let participant = &dialogue.participants[idx];
let participant_name = participant.name().to_string();
// Store in MessageStore
let metadata =
MessageMetadata::new().with_origin(MessageOrigin::AgentGenerated);
let message = DialogueMessage::new(
self.current_turn,
Speaker::agent(
participant_name.clone(),
participant.persona.role.clone(),
),
content.clone(),
)
.with_metadata(&metadata);
dialogue.message_store.push(message);
let turn = DialogueTurn {
speaker: Speaker::agent(
participant_name.clone(),
participant.persona.role.clone(),
),
content: content.clone(),
};
info!(
target = "llm_toolkit::dialogue",
mode = "broadcast_explicit_order",
participant = %participant_name,
participant_index = idx,
total_participants = participant_total,
event = "dialogue_turn_emitted"
);
Some(Ok(turn))
}
Err(err) => Some(Err(err)),
}
}
}
}
}
/// Session state enum for tracking execution progress.
pub(super) enum SessionState {
Broadcast(BroadcastState),
Sequential {
next_index: usize,
current_turn: usize,
sequence: Vec<usize>,
payload: Payload,
prev_agent_outputs: Vec<PayloadMessage>,
current_turn_outputs: Vec<PayloadMessage>,
participants_info: Vec<ParticipantInfo>,
},
Failed(Option<AgentError>),
Completed,
}