MCPcopy Create free account
hub / github.com/ChrisFeldmeier/OpenCodeRust / to_model_messages

Function to_model_messages

crates/opencode-session/src/message_v2.rs:834–1122  ·  view source on GitHub ↗

Convert a sequence of [`MessageWithParts`] into provider-level [`ProviderMessage`]s suitable for sending to an LLM API. This is the Rust equivalent of the TS `MessageV2.toModelMessages()`. The function: - Filters ignored text parts and non-media file parts from user messages. - Converts assistant text / reasoning / tool parts into the provider format. - Handles tool state (completed, error, pend

(input: &[MessageWithParts], model: &ModelContext)

Source from the content-addressed store, hash-verified

832/// media, injecting them as separate user messages.
833/// - Skips assistant messages that have errors (unless aborted with real parts).
834pub fn to_model_messages(input: &[MessageWithParts], model: &ModelContext) -> Vec<ProviderMessage> {
835 let mut result: Vec<ProviderMessage> = Vec::new();
836 let supports_media = supports_media_in_tool_results(&model.api_npm, &model.api_id);
837 let model_key = format!("{}/{}", model.provider_id, model.model_id);
838
839 for msg in input {
840 if msg.parts.is_empty() {
841 continue;
842 }
843
844 match &msg.info {
845 // ---------------------------------------------------------------
846 // User messages
847 // ---------------------------------------------------------------
848 MessageInfo::User { .. } => {
849 let mut parts: Vec<ContentPart> = Vec::new();
850
851 for part in &msg.parts {
852 match part {
853 Part::Text { text, ignored, .. } => {
854 if ignored != &Some(true) {
855 parts.push(ContentPart {
856 content_type: "text".to_string(),
857 text: Some(text.clone()),
858 ..Default::default()
859 });
860 }
861 }
862 Part::File(fp) => {
863 if fp.mime != "text/plain" && fp.mime != "application/x-directory" {
864 parts.push(ContentPart {
865 content_type: "file".to_string(),
866 image_url: Some(ImageUrl {
867 url: fp.url.clone(),
868 }),
869 media_type: Some(fp.mime.clone()),
870 filename: fp.filename.clone(),
871 ..Default::default()
872 });
873 }
874 }
875 Part::Compaction(_) => {
876 parts.push(ContentPart {
877 content_type: "text".to_string(),
878 text: Some("What did we do so far?".to_string()),
879 ..Default::default()
880 });
881 }
882 Part::Subtask(_) => {
883 parts.push(ContentPart {
884 content_type: "text".to_string(),
885 text: Some(
886 "The following tool was executed by the user".to_string(),
887 ),
888 ..Default::default()
889 });
890 }
891 _ => {}

Callers

nothing calls this directly

Calls 6

newFunction · 0.85
is_emptyMethod · 0.80
cloneMethod · 0.45
filterMethod · 0.45

Tested by

no test coverage detected