1use std::sync::Arc;
14
15use agent_client_protocol::schema::v1::{
16 ContentBlock, ContentChunk, EmbeddedResourceResource, PlanEntryStatus, ToolCallStatus, ToolKind,
17};
18use anyhow::{Result, bail};
19use serde::{Deserialize, Serialize};
20
21pub const SESSION_RESTART_TEXT: &str = "[session restarted]";
22pub const SESSION_RESTART_ITEM_PREFIX: &str = "system:session-restarted:";
23pub const WORK_INTERRUPTED_ITEM_PREFIX: &str = "system:work-interrupted:";
24pub const HARNESS_TURN_TEXT: &str = "Agent continued on its own";
26pub const HARNESS_TURN_ITEM_PREFIX: &str = "harness-turn:";
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
30#[serde(rename_all = "snake_case")]
31pub enum ToolSummarySourceKind {
32 RawInput,
33 RawOutput,
34 Title,
35}
36
37#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
39pub struct ToolCallPresentation {
40 pub summary: String,
41 pub source: String,
42 pub source_kind: ToolSummarySourceKind,
43 pub tool_kind: ToolKind,
44 #[serde(default)]
47 pub summary_version: u8,
48}
49
50#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
54#[serde(tag = "kind", rename_all = "snake_case")]
55pub enum TranscriptBody {
56 User {
57 content: Vec<serde_json::Value>,
58 },
59 Agent {
60 chunks: Vec<serde_json::Value>,
63 streaming: bool,
64 },
65 Thought {
66 chunks: Vec<serde_json::Value>,
69 streaming: bool,
70 },
71 Tool {
72 call: serde_json::Value,
75 #[serde(default, skip_serializing_if = "Vec::is_empty")]
80 terminal_outputs: Vec<TerminalOutputRecord>,
81 #[serde(default, skip_serializing_if = "Vec::is_empty")]
86 terminal_refs: Vec<String>,
87 #[serde(default, skip_serializing_if = "Option::is_none")]
90 presentation: Option<Box<ToolCallPresentation>>,
91 },
92 TerminalOutput {
97 record: TerminalOutputRecord,
98 },
99 Plan {
100 plan: serde_json::Value,
103 },
104 PlanProposal {
110 proposal_id: String,
112 plan: String,
114 },
115 System {
116 text: String,
117 },
118}
119
120pub fn push_content_chunk(chunks: &mut Vec<serde_json::Value>, chunk: serde_json::Value) {
135 if chunks
136 .last()
137 .is_some_and(|last| text_chunks_mergeable(last, &chunk))
138 {
139 let addition = chunk
140 .get("content")
141 .and_then(|content| content.get("text"))
142 .and_then(serde_json::Value::as_str)
143 .unwrap_or_default()
144 .to_owned();
145 if let Some(serde_json::Value::Object(last)) = chunks.last_mut()
146 && let Some(serde_json::Value::Object(content)) = last.get_mut("content")
147 && let Some(serde_json::Value::String(text)) = content.get_mut("text")
148 {
149 text.push_str(&addition);
150 return;
151 }
152 }
153 chunks.push(chunk);
154}
155
156pub fn coalesce_content_chunks(chunks: &mut Vec<serde_json::Value>) {
162 if chunks.len() < 2 {
163 return;
164 }
165 let mut merged = Vec::with_capacity(chunks.len());
166 for chunk in std::mem::take(chunks) {
167 push_content_chunk(&mut merged, chunk);
168 }
169 merged.shrink_to_fit();
170 *chunks = merged;
171}
172
173fn text_chunks_mergeable(last: &serde_json::Value, next: &serde_json::Value) -> bool {
177 let (serde_json::Value::Object(last), serde_json::Value::Object(next)) = (last, next) else {
178 return false;
179 };
180 let (
181 Some(serde_json::Value::Object(last_content)),
182 Some(serde_json::Value::Object(next_content)),
183 ) = (last.get("content"), next.get("content"))
184 else {
185 return false;
186 };
187 let is_text = |content: &serde_json::Map<String, serde_json::Value>| {
188 content.get("type").and_then(serde_json::Value::as_str) == Some("text")
189 && content
190 .get("text")
191 .is_some_and(serde_json::Value::is_string)
192 };
193 if !is_text(last_content) || !is_text(next_content) {
194 return false;
195 }
196 if last.len() != next.len()
198 || !last
199 .iter()
200 .all(|(key, value)| key == "content" || next.get(key) == Some(value))
201 {
202 return false;
203 }
204 last_content.len() == next_content.len()
206 && last_content
207 .iter()
208 .all(|(key, value)| key == "text" || next_content.get(key) == Some(value))
209}
210
211#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
215pub struct TerminalOutputRecord {
216 pub terminal_id: String,
217 pub output: String,
218 #[serde(default, skip_serializing_if = "is_false")]
219 pub truncated: bool,
220 #[serde(default, skip_serializing_if = "Option::is_none")]
221 pub exit_code: Option<u32>,
222 #[serde(default, skip_serializing_if = "Option::is_none")]
223 pub signal: Option<String>,
224}
225
226impl TerminalOutputRecord {
227 pub fn exited_cleanly(&self) -> bool {
232 self.exit_code == Some(0) && self.signal.is_none()
233 }
234
235 pub fn matches_tool_raw_result(&self, call: &serde_json::Value) -> bool {
240 if !matches!(
241 call.get("status").and_then(serde_json::Value::as_str),
242 Some("completed" | "failed")
243 ) {
244 return false;
245 }
246 let Some(raw) = call.get("rawOutput") else {
247 return false;
248 };
249 let Some(exit_code) = raw
250 .get("exit_code")
251 .and_then(serde_json::Value::as_u64)
252 .and_then(|code| u32::try_from(code).ok())
253 else {
254 return false;
255 };
256 if self.exit_code != Some(exit_code) || self.signal.is_some() {
257 return false;
258 }
259 match raw.get("output") {
260 Some(serde_json::Value::Array(bytes)) => {
261 bytes.len() == self.output.len()
262 && bytes
263 .iter()
264 .zip(self.output.as_bytes())
265 .all(|(value, byte)| value.as_u64() == Some(u64::from(*byte)))
266 }
267 Some(serde_json::Value::String(output)) => output == &self.output,
268 _ => false,
269 }
270 }
271}
272
273#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
274#[serde(deny_unknown_fields)]
275pub struct TranscriptItem {
276 pub stable_id: String,
277 pub position: u64,
279 pub latest_content_event_ordinal: Option<u64>,
282 pub created_at_ms: i64,
283 pub last_changed_at_ms: i64,
284 pub body: TranscriptBody,
285}
286
287impl TranscriptItem {
288 pub fn is_work_interruption(&self) -> bool {
289 self.stable_id.starts_with(WORK_INTERRUPTED_ITEM_PREFIX)
290 }
291
292 pub fn is_session_restart(&self) -> bool {
293 self.stable_id.starts_with(SESSION_RESTART_ITEM_PREFIX)
294 }
295
296 pub fn seq(&self) -> u64 {
304 self.latest_content_event_ordinal.unwrap_or(self.position)
305 }
306
307 pub fn is_turn_start(&self) -> bool {
311 matches!(self.body, TranscriptBody::User { .. })
312 || self.stable_id.starts_with(HARNESS_TURN_ITEM_PREFIX)
313 }
314
315 pub fn is_nonempty_agent_message(&self) -> bool {
316 let TranscriptBody::Agent { chunks, .. } = &self.body else {
317 return false;
318 };
319 chunks.iter().any(|chunk| {
320 let Some(content) = chunk.get("content") else {
321 return false;
322 };
323 match content.get("type").and_then(serde_json::Value::as_str) {
324 Some("text") => content
325 .get("text")
326 .and_then(serde_json::Value::as_str)
327 .is_some_and(|text| !text.trim().is_empty()),
328 Some(_) => true,
329 None => false,
330 }
331 })
332 }
333
334 pub fn validate(&self, through: u64) -> Result<()> {
335 if self.stable_id.trim().is_empty() {
336 bail!("materialized transcript item has an empty stable id");
337 }
338 if self.position == 0 || self.position > through {
339 bail!(
340 "materialized transcript item {:?} has invalid position {} at frontier {through}",
341 self.stable_id,
342 self.position
343 );
344 }
345 match (&self.body, self.latest_content_event_ordinal) {
346 (TranscriptBody::Agent { .. }, Some(ordinal))
347 if ordinal >= self.position && ordinal <= through => {}
348 (TranscriptBody::Agent { .. }, Some(ordinal)) => bail!(
349 "materialized agent message {:?} has invalid latest content ordinal {ordinal} at position {} and frontier {through}",
350 self.stable_id,
351 self.position
352 ),
353 (TranscriptBody::Agent { .. }, None) => bail!(
354 "materialized agent message {:?} has no latest content ordinal",
355 self.stable_id
356 ),
357 (_, Some(ordinal)) => bail!(
358 "non-agent transcript item {:?} has latest content ordinal {ordinal}",
359 self.stable_id
360 ),
361 (_, None) => {}
362 }
363 if self.last_changed_at_ms < self.created_at_ms {
364 bail!(
365 "materialized transcript item {:?} changed before it was created",
366 self.stable_id
367 );
368 }
369 Ok(())
370 }
371}
372
373#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
374pub enum ChatRole {
375 User,
376 Agent,
377 Thought,
379 Tool,
381 Plan,
383 PlanProposal,
385 System,
386}
387
388#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
389pub struct ChatEntry {
390 #[serde(default)]
391 pub start_seq: u64,
392 pub seq: u64,
393 pub role: ChatRole,
394 pub text: String,
395 pub recorded_at_ms: Option<i64>,
396 pub revision: u64,
397 pub message_id: Option<String>,
398 pub tool_call_id: Option<String>,
399 pub tool_status: Option<ToolStatus>,
400 #[serde(default, skip_serializing_if = "Option::is_none")]
403 pub tool_summary: Option<String>,
404 #[serde(default, skip_serializing_if = "Option::is_none")]
407 pub tool_presentation: Option<ToolCallPresentation>,
408 pub tool_content: Vec<String>,
409 pub tool_diffstats: Vec<String>,
410 pub tool_locations: Vec<String>,
411 pub plan: Vec<PlanLine>,
412 #[serde(default, skip_serializing_if = "is_false")]
413 pub leading_omitted: bool,
414 #[serde(default, skip_serializing_if = "is_false")]
418 pub raw_only: bool,
419 #[serde(skip)]
423 pub source: TranscriptSource,
424}
425
426#[derive(Debug, Clone, Default)]
433pub struct TranscriptSource(pub Option<Arc<TranscriptItem>>);
434
435impl TranscriptSource {
436 pub fn is(&self, item: &Arc<TranscriptItem>) -> bool {
437 self.0
438 .as_ref()
439 .is_some_and(|source| Arc::ptr_eq(source, item))
440 }
441}
442
443impl PartialEq for TranscriptSource {
444 fn eq(&self, _other: &Self) -> bool {
445 true
446 }
447}
448
449impl Eq for TranscriptSource {}
450
451impl ChatEntry {
452 pub fn is_session_restart(&self) -> bool {
457 self.source
458 .0
459 .as_ref()
460 .is_some_and(|item| item.is_session_restart())
461 || (self.role == ChatRole::System && self.text == SESSION_RESTART_TEXT)
462 }
463
464 pub fn plan(seq: u64, plan: Vec<PlanLine>) -> Self {
465 Self {
466 start_seq: seq,
467 seq,
468 role: ChatRole::Plan,
469 text: String::new(),
470 recorded_at_ms: None,
471 revision: 0,
472 message_id: None,
473 tool_call_id: None,
474 tool_status: None,
475 tool_summary: None,
476 tool_presentation: None,
477 tool_content: Vec::new(),
478 tool_diffstats: Vec::new(),
479 tool_locations: Vec::new(),
480 plan,
481 leading_omitted: false,
482 raw_only: false,
483 source: TranscriptSource::default(),
484 }
485 }
486
487 pub fn touch(&mut self, seq: u64) {
488 self.seq = seq;
489 self.revision = self.revision.wrapping_add(1);
490 }
491
492 #[doc(hidden)]
498 pub fn bounded_for_dashboard(mut self) -> Self {
499 self.bound_dashboard_content();
500 self
501 }
502
503 fn bound_dashboard_content(&mut self) {
504 const TEXT_BYTES: usize = 64 * 1024;
505 const DETAIL_BYTES: usize = 2 * 1024;
506 const DETAIL_COUNT: usize = 8;
507
508 self.leading_omitted |= truncate_string_start(&mut self.text, TEXT_BYTES);
509 for values in [
510 &mut self.tool_content,
511 &mut self.tool_diffstats,
512 &mut self.tool_locations,
513 ] {
514 values.truncate(DETAIL_COUNT);
515 for value in values {
516 truncate_string_start(value, DETAIL_BYTES);
517 }
518 }
519 if let Some(summary) = &mut self.tool_summary {
520 truncate_string_start(summary, DETAIL_BYTES);
521 }
522 if let Some(presentation) = &mut self.tool_presentation {
523 truncate_string_start(&mut presentation.summary, DETAIL_BYTES);
524 truncate_string_start(&mut presentation.source, TEXT_BYTES);
525 }
526 self.plan.truncate(DETAIL_COUNT);
527 for line in &mut self.plan {
528 truncate_string_start(&mut line.text, DETAIL_BYTES);
529 }
530 }
531
532 pub fn with_recorded_at(mut self, recorded_at_ms: Option<i64>) -> Self {
533 self.recorded_at_ms = recorded_at_ms;
534 self
535 }
536}
537
538impl ChatEntry {
541 pub fn plain(seq: u64, role: ChatRole, text: impl Into<String>) -> Self {
542 Self {
543 start_seq: seq,
544 seq,
545 role,
546 text: sanitize_terminal_text(&text.into()),
547 recorded_at_ms: None,
548 revision: 0,
549 message_id: None,
550 tool_call_id: None,
551 tool_status: None,
552 tool_summary: None,
553 tool_presentation: None,
554 tool_content: Vec::new(),
555 tool_diffstats: Vec::new(),
556 tool_locations: Vec::new(),
557 plan: Vec::new(),
558 leading_omitted: false,
559 raw_only: false,
560 source: TranscriptSource::default(),
561 }
562 }
563
564 pub fn tool(
565 seq: u64,
566 title: impl Into<String>,
567 tool_call_id: Option<String>,
568 tool_status: ToolStatus,
569 ) -> Self {
570 Self {
571 start_seq: seq,
572 seq,
573 role: ChatRole::Tool,
574 text: sanitize_terminal_text(&title.into()),
575 recorded_at_ms: None,
576 revision: 0,
577 message_id: None,
578 tool_call_id,
579 tool_status: Some(tool_status),
580 tool_summary: None,
581 tool_presentation: None,
582 tool_content: Vec::new(),
583 tool_diffstats: Vec::new(),
584 tool_locations: Vec::new(),
585 plan: Vec::new(),
586 leading_omitted: false,
587 raw_only: false,
588 source: TranscriptSource::default(),
589 }
590 }
591}
592
593pub(crate) fn is_false(value: &bool) -> bool {
594 !*value
595}
596
597pub fn plan_status(status: &PlanEntryStatus) -> PlanStatus {
598 match status {
599 PlanEntryStatus::InProgress => PlanStatus::Running,
600 PlanEntryStatus::Completed => PlanStatus::Completed,
601 _ => PlanStatus::Pending,
602 }
603}
604
605pub fn sanitize_terminal_text(text: &str) -> String {
607 let mut sanitized = String::with_capacity(text.len());
608 let mut chars = text.chars().peekable();
609 while let Some(ch) = chars.next() {
610 if ch == '\x1b' {
611 while consume_escape_body(&mut chars) {}
615 } else if ch == '\r' {
616 if chars.peek() != Some(&'\n') {
617 sanitized.push('\n');
618 }
619 } else if matches!(ch, '\n' | '\t') || !ch.is_control() {
620 sanitized.push(ch);
621 }
622 }
623 sanitized
624}
625
626fn consume_escape_body(chars: &mut std::iter::Peekable<std::str::Chars<'_>>) -> bool {
633 match chars.next() {
634 Some('[') => {
636 let _ = chars.find(|ch| ('@'..='~').contains(ch));
637 false
638 }
639 Some(']' | 'P' | 'X' | '^' | '_') => consume_string_body(chars),
641 Some('(' | ')' | '*' | '+' | '-' | '.' | '/' | '#' | '%' | ' ') => {
643 chars.next();
644 false
645 }
646 _ => false,
649 }
650}
651
652fn consume_string_body(chars: &mut std::iter::Peekable<std::str::Chars<'_>>) -> bool {
656 while let Some(&ch) = chars.peek() {
657 match ch {
658 '\n' | '\r' | '\x18' | '\x1a' => return false,
659 '\x07' => {
660 chars.next();
661 return false;
662 }
663 '\x1b' => {
664 chars.next();
665 return true;
666 }
667 _ => {
668 chars.next();
669 }
670 }
671 }
672 false
673}
674
675pub fn materialized_content_text(content: &[serde_json::Value]) -> String {
676 let text = content
677 .iter()
678 .map(materialized_value_text)
679 .filter(|text| !text.is_empty())
680 .collect::<Vec<_>>()
681 .join("\n");
682 crate::relay::strip_hidden_prompt_context(&text).to_owned()
683}
684
685#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
691#[serde(rename_all = "snake_case")]
692pub enum TranscriptRole {
693 User,
694 Agent,
695 Thought,
696 Tool,
697 Terminal,
698 Plan,
699 PlanProposal,
700 System,
701}
702
703impl TranscriptRole {
704 pub fn as_str(self) -> &'static str {
705 match self {
706 Self::User => "user",
707 Self::Agent => "agent",
708 Self::Thought => "thought",
709 Self::Tool => "tool",
710 Self::Terminal => "terminal",
711 Self::Plan => "plan",
712 Self::PlanProposal => "plan_proposal",
713 Self::System => "system",
714 }
715 }
716 pub fn storage_kind(self) -> &'static str {
717 match self {
718 Self::Terminal => "terminal_output",
719 other => other.as_str(),
720 }
721 }
722}
723
724pub fn transcript_item_role(body: &TranscriptBody) -> &'static str {
725 let role = match body {
726 TranscriptBody::User { .. } => TranscriptRole::User,
727 TranscriptBody::Agent { .. } => TranscriptRole::Agent,
728 TranscriptBody::Thought { .. } => TranscriptRole::Thought,
729 TranscriptBody::Tool { .. } => TranscriptRole::Tool,
730 TranscriptBody::TerminalOutput { .. } => TranscriptRole::Terminal,
731 TranscriptBody::Plan { .. } => TranscriptRole::Plan,
732 TranscriptBody::PlanProposal { .. } => TranscriptRole::PlanProposal,
733 TranscriptBody::System { .. } => TranscriptRole::System,
734 };
735 role.as_str()
736}
737
738pub fn materialized_chunks_text(chunks: &[serde_json::Value]) -> String {
739 chunks
740 .iter()
741 .filter_map(|value| match ContentChunk::deserialize(value) {
742 Ok(chunk) => Some(chunk),
743 Err(error) => {
744 tracing::warn!(%error, "could not decode a stored content chunk");
745 None
746 }
747 })
748 .filter_map(|chunk| content_block_text(&chunk.content))
749 .map(|text| sanitize_terminal_text(&text))
750 .collect::<Vec<_>>()
751 .join("")
752}
753
754fn materialized_value_text(value: &serde_json::Value) -> String {
755 if let Ok(block) = ContentBlock::deserialize(value)
756 && let Some(text) = content_block_text(&block)
757 {
758 return sanitize_terminal_text(&text);
759 }
760 if let Some(text) = value.as_str() {
761 return sanitize_terminal_text(text);
762 }
763 sanitize_terminal_text(&serde_json::to_string(value).unwrap_or_else(|_| "[content]".into()))
764}
765
766pub fn tool_status(status: &ToolCallStatus) -> ToolStatus {
767 match status {
768 ToolCallStatus::InProgress => ToolStatus::Running,
769 ToolCallStatus::Completed => ToolStatus::Completed,
770 ToolCallStatus::Failed => ToolStatus::Failed,
771 _ => ToolStatus::Pending,
772 }
773}
774
775pub fn content_block_text(content: &ContentBlock) -> Option<String> {
776 match content {
777 ContentBlock::Text(text) => Some(text.text.clone()),
778 ContentBlock::Image(_) => Some("[image]".into()),
779 ContentBlock::Audio(_) => Some("[audio]".into()),
780 ContentBlock::ResourceLink(link) => Some(format!("[{}]({})", link.name, link.uri)),
781 ContentBlock::Resource(resource) => Some(match &resource.resource {
782 EmbeddedResourceResource::TextResourceContents(resource) => resource.text.clone(),
783 EmbeddedResourceResource::BlobResourceContents(resource) => {
784 format!("[embedded resource: {}]", resource.uri)
785 }
786 _ => "[embedded resource]".into(),
787 }),
788 _ => None,
789 }
790}
791
792pub fn truncate_string_start(value: &mut String, maximum_bytes: usize) -> bool {
793 if value.len() <= maximum_bytes {
794 return false;
795 }
796 let mut start = value.len() - maximum_bytes;
797 while !value.is_char_boundary(start) {
798 start += 1;
799 }
800 value.drain(..start);
801 true
802}
803
804#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
806pub enum ToolStatus {
807 Pending,
808 Running,
809 Completed,
810 Failed,
811}
812
813#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
814pub enum PlanStatus {
815 Pending,
816 Running,
817 Completed,
818}
819
820#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
821pub struct PlanLine {
822 pub text: String,
823 pub status: PlanStatus,
824}
825
826#[cfg(test)]
827mod tests {
828 use super::*;
829 use serde_json::json;
830
831 fn text_chunk(text: &str, message_id: Option<&str>) -> serde_json::Value {
832 match message_id {
833 Some(id) => json!({"content": {"type": "text", "text": text}, "messageId": id}),
834 None => json!({"content": {"type": "text", "text": text}}),
835 }
836 }
837
838 #[test]
839 fn push_content_chunk_merges_adjacent_text_for_the_same_message_id() {
840 let mut chunks = vec![text_chunk("The", Some("m1"))];
841 push_content_chunk(&mut chunks, text_chunk(" quick", Some("m1")));
842 push_content_chunk(&mut chunks, text_chunk(" fox", Some("m1")));
843 assert_eq!(chunks, vec![text_chunk("The quick fox", Some("m1"))]);
844 }
845
846 #[test]
847 fn push_content_chunk_merges_adjacent_text_without_message_ids() {
848 let mut chunks = vec![text_chunk("one", None)];
849 push_content_chunk(&mut chunks, text_chunk(" two", None));
850 assert_eq!(chunks, vec![text_chunk("one two", None)]);
851 }
852
853 #[test]
854 fn push_content_chunk_keeps_chunks_from_different_message_ids_apart() {
855 let mut chunks = vec![text_chunk("first", Some("m1"))];
856 push_content_chunk(&mut chunks, text_chunk("second", Some("m2")));
857 push_content_chunk(&mut chunks, text_chunk("third", None));
858 assert_eq!(
859 chunks,
860 vec![
861 text_chunk("first", Some("m1")),
862 text_chunk("second", Some("m2")),
863 text_chunk("third", None),
864 ]
865 );
866 }
867
868 #[test]
869 fn push_content_chunk_keeps_non_text_content_separate() {
870 let image = json!({"content": {"type": "image", "data": "abc", "mimeType": "image/png"}});
871 let mut chunks = vec![text_chunk("before", None)];
872 push_content_chunk(&mut chunks, image.clone());
873 push_content_chunk(&mut chunks, image.clone());
874 push_content_chunk(&mut chunks, text_chunk("after", None));
875 assert_eq!(
876 chunks,
877 vec![
878 text_chunk("before", None),
879 image.clone(),
880 image,
881 text_chunk("after", None),
882 ]
883 );
884 }
885
886 #[test]
887 fn push_content_chunk_keeps_chunks_with_differing_metadata_apart() {
888 let mut chunks =
889 vec![json!({"content": {"type": "text", "text": "a"}, "meta": {"source": "one"}})];
890 push_content_chunk(
891 &mut chunks,
892 json!({"content": {"type": "text", "text": "b"}, "meta": {"source": "two"}}),
893 );
894 push_content_chunk(
895 &mut chunks,
896 json!({"content": {"type": "text", "text": "c"}, "meta": {"source": "two"}}),
897 );
898 assert_eq!(
899 chunks,
900 vec![
901 json!({"content": {"type": "text", "text": "a"}, "meta": {"source": "one"}}),
902 json!({"content": {"type": "text", "text": "bc"}, "meta": {"source": "two"}}),
903 ]
904 );
905 }
906
907 #[test]
908 fn push_content_chunk_keeps_chunks_with_differing_annotations_apart() {
909 let mut chunks = vec![
910 json!({"content": {"type": "text", "text": "a", "annotations": {"audience": ["user"]}}}),
911 ];
912 push_content_chunk(
913 &mut chunks,
914 json!({"content": {"type": "text", "text": "b"}}),
915 );
916 assert_eq!(
917 chunks,
918 vec![
919 json!({"content": {"type": "text", "text": "a", "annotations": {"audience": ["user"]}}}),
920 json!({"content": {"type": "text", "text": "b"}}),
921 ]
922 );
923 }
924
925 #[test]
926 fn coalesce_content_chunks_collapses_runs_and_keeps_segment_boundaries() {
927 let mut chunks = vec![
928 text_chunk("He", Some("m1")),
929 text_chunk("llo", Some("m1")),
930 text_chunk("!", Some("m1")),
931 text_chunk("next", Some("m2")),
932 text_chunk(" turn", Some("m2")),
933 ];
934 coalesce_content_chunks(&mut chunks);
935 assert_eq!(
936 chunks,
937 vec![
938 text_chunk("Hello!", Some("m1")),
939 text_chunk("next turn", Some("m2")),
940 ]
941 );
942 }
943
944 #[test]
945 fn coalesce_content_chunks_leaves_unmergeable_chunks_alone() {
946 let original = vec![
947 text_chunk("a", Some("m1")),
948 text_chunk("b", Some("m2")),
949 json!({"content": {"type": "image", "data": "x", "mimeType": "image/png"}}),
950 ];
951 let mut chunks = original.clone();
952 coalesce_content_chunks(&mut chunks);
953 assert_eq!(chunks, original);
954 }
955}