MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / assemble

Function assemble

atomic-agent/src/transcript/opencode.rs:232–377  ·  view source on GitHub ↗

Fold messages and parts into [`TurnData`]. The current turn starts at the last user message; reasoning, response, and step statistics are scoped to that window, while the transcript covers the whole session (mirroring how Claude Code's transcript file is a full-session record).

(messages: &[(String, String)], parts: &[(String, Value)])

Source from the content-addressed store, hash-verified

230/// covers the whole session (mirroring how Claude Code's transcript file
231/// is a full-session record).
232fn assemble(messages: &[(String, String)], parts: &[(String, Value)]) -> TurnData {
233 let role_of: HashMap<&str, &str> = messages
234 .iter()
235 .map(|(id, role)| (id.as_str(), role.as_str()))
236 .collect();
237
238 // Turn window: message indices from the last user message onward.
239 let turn_start = messages
240 .iter()
241 .rposition(|(_, role)| role == "user")
242 .unwrap_or(0);
243 let in_turn: HashMap<&str, bool> = messages
244 .iter()
245 .enumerate()
246 .map(|(i, (id, _))| (id.as_str(), i >= turn_start))
247 .collect();
248
249 let mut data = TurnData {
250 transcript_jsonl: String::new(),
251 reasoning_blocks: Vec::new(),
252 response: None,
253 input_tokens: 0,
254 output_tokens: 0,
255 reasoning_tokens: 0,
256 cache_read_tokens: 0,
257 cache_write_tokens: 0,
258 cost_usd: 0.0,
259 finish_reason: None,
260 step_count: 0,
261 tool_parts: Vec::new(),
262 };
263
264 let push_line = |line: Value, out: &mut TurnData| {
265 if let Ok(s) = serde_json::to_string(&line) {
266 out.transcript_jsonl.push_str(&s);
267 out.transcript_jsonl.push('\n');
268 }
269 };
270
271 for (message_id, part) in parts {
272 let part_type = part.get("type").and_then(Value::as_str).unwrap_or("");
273 let in_turn = in_turn.get(message_id.as_str()).copied().unwrap_or(false);
274 let role = role_of
275 .get(message_id.as_str())
276 .copied()
277 .unwrap_or("assistant");
278
279 match part_type {
280 "text" => {
281 let Some(text) = part.get("text").and_then(Value::as_str) else {
282 continue;
283 };
284 if text.trim().is_empty() {
285 continue;
286 }
287 let line = serde_json::json!({ "type": role, "text": text });
288 push_line(line, &mut data);
289 if in_turn && role == "assistant" {

Calls 6

getMethod · 0.65
collectMethod · 0.45
iterMethod · 0.45
as_strMethod · 0.45
pushMethod · 0.45
is_emptyMethod · 0.45