Files
ds2api/internal/promptcompat/thinking_injection.go
2026-04-27 02:09:11 +08:00

74 lines
2.4 KiB
Go

package promptcompat
import "strings"
const (
ThinkingInjectionMarker = "Reasoning Effort: Absolute maximum with no shortcuts permitted."
DefaultThinkingInjectionPrompt = ThinkingInjectionMarker + "\n" +
"You MUST be very thorough in your thinking and comprehensively decompose the problem to resolve the root cause, rigorously stress-testing your logic against all potential paths, edge cases, and adversarial scenarios.\n" +
"Explicitly write out your entire deliberation process, documenting every intermediate step, considered alternative, and rejected hypothesis to ensure absolutely no assumption is left unchecked."
)
func AppendThinkingInjectionToLatestUser(messages []any) ([]any, bool) {
return AppendThinkingInjectionPromptToLatestUser(messages, "")
}
func AppendThinkingInjectionPromptToLatestUser(messages []any, injectionPrompt string) ([]any, bool) {
if len(messages) == 0 {
return messages, false
}
injectionPrompt = strings.TrimSpace(injectionPrompt)
if injectionPrompt == "" {
injectionPrompt = DefaultThinkingInjectionPrompt
}
for i := len(messages) - 1; i >= 0; i-- {
msg, ok := messages[i].(map[string]any)
if !ok {
continue
}
if strings.ToLower(strings.TrimSpace(asString(msg["role"]))) != "user" {
continue
}
content := msg["content"]
normalizedContent := NormalizeOpenAIContentForPrompt(content)
if strings.Contains(normalizedContent, ThinkingInjectionMarker) || strings.Contains(normalizedContent, injectionPrompt) {
return messages, false
}
updatedContent := appendThinkingInjectionToContent(content, injectionPrompt)
out := append([]any(nil), messages...)
cloned := make(map[string]any, len(msg))
for k, v := range msg {
cloned[k] = v
}
cloned["content"] = updatedContent
out[i] = cloned
return out, true
}
return messages, false
}
func appendThinkingInjectionToContent(content any, injectionPrompt string) any {
switch x := content.(type) {
case string:
return appendTextBlock(x, injectionPrompt)
case []any:
out := append([]any(nil), x...)
out = append(out, map[string]any{
"type": "text",
"text": injectionPrompt,
})
return out
default:
text := NormalizeOpenAIContentForPrompt(content)
return appendTextBlock(text, injectionPrompt)
}
}
func appendTextBlock(base, addition string) string {
base = strings.TrimSpace(base)
if base == "" {
return addition
}
return base + "\n\n" + addition
}