sub2api/backend/internal/service/model_not_found_error.go

45 lines
1.1 KiB
Go

package service
import (
"net/http"
"strings"
)
var upstreamModelNotFoundKeywords = []string{"model not found", "unknown model", "not found"}
func isUpstreamModelNotFoundError(statusCode int, body []byte) bool {
if statusCode != http.StatusNotFound {
return false
}
normalized := normalizeModelNotFoundBody(body)
if normalized == "" || !strings.Contains(normalized, "model") {
return false
}
return containsModelNotFoundKeyword(normalized)
}
func isModelNotFoundError(statusCode int, body []byte) bool {
return isUpstreamModelNotFoundError(statusCode, body) || statusCode == http.StatusNotFound
}
func containsModelNotFoundKeyword(normalizedBody string) bool {
if normalizedBody == "" {
return false
}
for _, keyword := range upstreamModelNotFoundKeywords {
if strings.Contains(normalizedBody, keyword) {
return true
}
}
return false
}
func normalizeModelNotFoundBody(body []byte) string {
if len(body) == 0 {
return ""
}
normalized := strings.ToLower(string(body))
normalized = strings.NewReplacer("_", " ", "-", " ", "\n", " ", "\r", " ", "\t", " ").Replace(normalized)
return strings.Join(strings.Fields(normalized), " ")
}