@@ -6,7 +6,9 @@ package openai
66import (
77 "bytes"
88 "context"
9+ "crypto/sha256"
910 "encoding/base64"
11+ "encoding/hex"
1012 "encoding/json"
1113 "errors"
1214 "fmt"
@@ -58,6 +60,16 @@ func extractXmlAttribute(tag, attrName string) (string, bool) {
5860 return value , true
5961}
6062
63+ // generateDeterministicSuffix creates an 8-character hash from input strings
64+ func generateDeterministicSuffix (inputs ... string ) string {
65+ hasher := sha256 .New ()
66+ for _ , input := range inputs {
67+ hasher .Write ([]byte (input ))
68+ }
69+ hash := hasher .Sum (nil )
70+ return hex .EncodeToString (hash )[:8 ]
71+ }
72+
6173// ---------- OpenAI Request Types ----------
6274
6375type StreamOptionsType struct {
@@ -388,8 +400,8 @@ func convertFileAIMessagePart(part uctypes.AIMessagePart) (*OpenAIMessageContent
388400 encodedFileName := strings .ReplaceAll (fileName , `"` , """ )
389401 quotedFileName := strconv .Quote (encodedFileName )
390402
391- randomSuffix := uuid . New (). String ()[ 0 : 8 ]
392- formattedText := fmt .Sprintf ("<AttachedTextFile_%s file_name=%s>\n %s\n </AttachedTextFile_%s>" , randomSuffix , quotedFileName , textContent , randomSuffix )
403+ deterministicSuffix := generateDeterministicSuffix ( textContent , fileName )
404+ formattedText := fmt .Sprintf ("<AttachedTextFile_%s file_name=%s>\n %s\n </AttachedTextFile_%s>" , deterministicSuffix , quotedFileName , textContent , deterministicSuffix )
393405
394406 return & OpenAIMessageContent {
395407 Type : "input_text" ,
@@ -412,8 +424,8 @@ func convertFileAIMessagePart(part uctypes.AIMessagePart) (*OpenAIMessageContent
412424 encodedDirName := strings .ReplaceAll (directoryName , `"` , """ )
413425 quotedDirName := strconv .Quote (encodedDirName )
414426
415- randomSuffix := uuid . New (). String ()[ 0 : 8 ]
416- formattedText := fmt .Sprintf ("<AttachedDirectoryListing_%s directory_name=%s>\n %s\n </AttachedDirectoryListing_%s>" , randomSuffix , quotedDirName , jsonContent , randomSuffix )
427+ deterministicSuffix := generateDeterministicSuffix ( jsonContent , directoryName )
428+ formattedText := fmt .Sprintf ("<AttachedDirectoryListing_%s directory_name=%s>\n %s\n </AttachedDirectoryListing_%s>" , deterministicSuffix , quotedDirName , jsonContent , deterministicSuffix )
417429
418430 return & OpenAIMessageContent {
419431 Type : "input_text" ,
0 commit comments