From f342fc0c564cc446c7e6ddf605f334546003be15 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 15 Dec 2025 11:30:09 -0300 Subject: [PATCH] feat: support text/ files Signed-off-by: Carlos Alexandro Becker --- providers/anthropic/anthropic.go | 19 +++++++++++-------- providers/openai/language_model_hooks.go | 5 +++++ .../openaicompat/language_model_hooks.go | 5 +++++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/providers/anthropic/anthropic.go b/providers/anthropic/anthropic.go index 39f1c9b603625451fed4cae1f2392f997618014b..037aa04c06bbe85cbf04e4667d95e3fc18c81f65 100644 --- a/providers/anthropic/anthropic.go +++ b/providers/anthropic/anthropic.go @@ -547,16 +547,19 @@ func toPrompt(prompt fantasy.Prompt, sendReasoningData bool) ([]anthropic.TextBl continue } // TODO: handle other file types - if !strings.HasPrefix(file.MediaType, "image/") { - continue + if strings.HasPrefix(file.MediaType, "image/") { + base64Encoded := base64.StdEncoding.EncodeToString(file.Data) + imageBlock := anthropic.NewImageBlockBase64(file.MediaType, base64Encoded) + if cacheControl != nil { + imageBlock.OfImage.CacheControl = anthropic.NewCacheControlEphemeralParam() + } + anthropicContent = append(anthropicContent, imageBlock) } - - base64Encoded := base64.StdEncoding.EncodeToString(file.Data) - imageBlock := anthropic.NewImageBlockBase64(file.MediaType, base64Encoded) - if cacheControl != nil { - imageBlock.OfImage.CacheControl = anthropic.NewCacheControlEphemeralParam() + if strings.HasPrefix(file.MediaType, "text/") { + anthropicContent = append(anthropicContent, anthropic.NewDocumentBlock(anthropic.PlainTextSourceParam{ + Data: string(file.Data), + })) } - anthropicContent = append(anthropicContent, imageBlock) } } } else if msg.Role == fantasy.MessageRoleTool { diff --git a/providers/openai/language_model_hooks.go b/providers/openai/language_model_hooks.go index 9ca2c64f520a1dbf01f95f46ac097689b2e6d045..2b657e1aa36102c6527d72e1af8965a428462c19 100644 --- a/providers/openai/language_model_hooks.go +++ b/providers/openai/language_model_hooks.go @@ -359,6 +359,11 @@ func DefaultToPrompt(prompt fantasy.Prompt, _, _ string) ([]openai.ChatCompletio } switch { + case strings.HasPrefix(filePart.MediaType, "text/"): + base64Encoded := base64.StdEncoding.EncodeToString(filePart.Data) + content = append(content, openai.FileContentPart(openai.ChatCompletionContentPartFileFileParam{ + FileData: param.NewOpt(base64Encoded), + })) case strings.HasPrefix(filePart.MediaType, "image/"): // Handle image files base64Encoded := base64.StdEncoding.EncodeToString(filePart.Data) diff --git a/providers/openaicompat/language_model_hooks.go b/providers/openaicompat/language_model_hooks.go index 2f21f7130e950b1dddee83de45591cbf4a571b1f..f4e094a85f28351b681192a5d098ef45c58d1088 100644 --- a/providers/openaicompat/language_model_hooks.go +++ b/providers/openaicompat/language_model_hooks.go @@ -209,6 +209,11 @@ func ToPromptFunc(prompt fantasy.Prompt, _, _ string) ([]openaisdk.ChatCompletio } switch { + case strings.HasPrefix(filePart.MediaType, "text/"): + base64Encoded := base64.StdEncoding.EncodeToString(filePart.Data) + content = append(content, openaisdk.FileContentPart(openaisdk.ChatCompletionContentPartFileFileParam{ + FileData: param.NewOpt(base64Encoded), + })) case strings.HasPrefix(filePart.MediaType, "image/"): // Handle image files base64Encoded := base64.StdEncoding.EncodeToString(filePart.Data)