Files
root ba4db85241 fix(logic): ignore reference directives when checking for prompt text
Strip inline image tokens and reference directive URLs before deciding
whether a prompt carries real user text, so a reference-only prompt is
still treated as empty while text around references counts.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 16:17:59 +08:00

47 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package logic
import (
"fmt"
"regexp"
"strings"
"img_infinite_canvas/internal/domain/design"
)
var (
promptReferenceDirectivePattern = regexp.MustCompile(`(?i)(?:参考图|reference image)\s*\d*\s*(?:[(][^)\n]+[)])?\s*[:]\s*(?:https?://[^\s\],。]+|data:image/[^\s\],。]+|/[^\s\],。]+)`)
promptImageTokenPattern = regexp.MustCompile(`\[@image:#\d+:[^:\]]+:(?:\[[^\]]+\]\((?:[^)]+)\)|[^\]]+)\]`)
)
func validateRequiredPrompt(prompt string, allowEmpty bool) error {
if allowEmpty {
return nil
}
if hasRequiredPromptText(prompt) {
return nil
}
return fmt.Errorf("%w: prompt is required", design.ErrInvalidInput)
}
func hasRequiredPromptText(prompt string) bool {
prompt = promptImageTokenPattern.ReplaceAllString(prompt, " ")
prompt = promptReferenceDirectivePattern.ReplaceAllString(prompt, " ")
for _, line := range strings.Split(prompt, "\n") {
line = strings.TrimSpace(strings.ReplaceAll(line, "\u00a0", " "))
if line == "" || isPromptReferenceDirective(line) {
continue
}
return true
}
return false
}
func isPromptReferenceDirective(line string) bool {
lower := strings.ToLower(line)
hasURL := strings.Contains(lower, "http://") || strings.Contains(lower, "https://")
if !hasURL {
return false
}
return strings.HasPrefix(line, "参考图") || strings.HasPrefix(lower, "reference image")
}