feat(metadata): add stripThinkingBlocks function and related tests

This commit is contained in:
2026-03-27 00:05:41 +02:00
parent a5c7b90f49
commit 6af02a2ba1
2 changed files with 114 additions and 0 deletions

View File

@@ -189,6 +189,7 @@ func (c *Client) extractMetadataWithModel(ctx context.Context, input, model stri
}
metadataText := strings.TrimSpace(resp.Choices[0].Message.Content)
metadataText = stripThinkingBlocks(metadataText)
metadataText = stripCodeFence(metadataText)
metadataText = extractJSONObject(metadataText)
if metadataText == "" {
@@ -320,6 +321,30 @@ func extractJSONObject(s string) string {
return s[start : end+1]
}
// stripThinkingBlocks removes <think>...</think> and <thinking>...</thinking>
// blocks produced by reasoning models (DeepSeek R1, QwQ, etc.) so that the
// remaining text can be parsed as JSON without interference from thinking content
// that may itself contain braces.
func stripThinkingBlocks(s string) string {
for _, tag := range []string{"think", "thinking"} {
open := "<" + tag + ">"
close := "</" + tag + ">"
for {
start := strings.Index(s, open)
if start == -1 {
break
}
end := strings.Index(s[start:], close)
if end == -1 {
s = s[:start]
break
}
s = s[:start] + s[start+end+len(close):]
}
}
return strings.TrimSpace(s)
}
func stripCodeFence(value string) string {
value = strings.TrimSpace(value)
if !strings.HasPrefix(value, "```") {