package history import ( "context" "strings" "ds2api/internal/auth" "ds2api/internal/httpapi/openai/shared" "ds2api/internal/promptcompat" ) type Service struct { Store shared.ConfigReader DS shared.DeepSeekCaller } // Apply is retained for legacy compatibility only. The active split path is // current input file handling in ApplyCurrentInputFile. func (s Service) Apply(ctx context.Context, a *auth.RequestAuth, stdReq promptcompat.StandardRequest) (promptcompat.StandardRequest, error) { return stdReq, nil } func SplitOpenAIHistoryMessages(messages []any, triggerAfterTurns int) ([]any, []any) { if triggerAfterTurns <= 0 { triggerAfterTurns = 1 } lastUserIndex := -1 userTurns := 0 for i, raw := range messages { msg, ok := raw.(map[string]any) if !ok { continue } role := strings.ToLower(strings.TrimSpace(shared.AsString(msg["role"]))) if role != "user" { continue } userTurns++ lastUserIndex = i } if userTurns <= triggerAfterTurns || lastUserIndex < 0 { return messages, nil } promptMessages := make([]any, 0, len(messages)-lastUserIndex) historyMessages := make([]any, 0, lastUserIndex) for i, raw := range messages { msg, ok := raw.(map[string]any) if !ok { if i >= lastUserIndex { promptMessages = append(promptMessages, raw) } else { historyMessages = append(historyMessages, raw) } continue } role := strings.ToLower(strings.TrimSpace(shared.AsString(msg["role"]))) switch role { case "system", "developer": promptMessages = append(promptMessages, raw) default: if i >= lastUserIndex { promptMessages = append(promptMessages, raw) } else { historyMessages = append(historyMessages, raw) } } } if len(promptMessages) == 0 { return messages, nil } return promptMessages, historyMessages } func prependUniqueRefFileID(existing []string, fileID string) []string { fileID = strings.TrimSpace(fileID) if fileID == "" { return existing } out := make([]string, 0, len(existing)+1) out = append(out, fileID) for _, id := range existing { trimmed := strings.TrimSpace(id) if trimmed == "" || strings.EqualFold(trimmed, fileID) { continue } out = append(out, trimmed) } return out }