Skip to content

Commit 44afcf3

Browse files
committed
fix(ai): Fix the issue where thinking model tags sometimes fail to parse
- Add goquery v1.10.0 as a new dependency - Upgrade generative-ai-go from v0.18.0 to v0.19.0 - Refactor GoogleProvider model names for clarity - Enhance processThinking function to handle leading newlines in input - Improve AI command tests for better coverage of thinking tag processing
1 parent 67fe566 commit 44afcf3

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

wox.core/ai/provider_google.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ func (g *GoogleProvider) ChatStream(ctx context.Context, model Model, conversati
4444
func (g *GoogleProvider) Models(ctx context.Context) ([]Model, error) {
4545
return []Model{
4646
{
47-
Name: "gemini-exp-1206",
48-
Provider: ProviderNameGoogle,
49-
},
50-
{
51-
Name: "gemini-2.0-flash-exp",
47+
Name: "gemini-2.0-flash",
5248
Provider: ProviderNameGoogle,
5349
},
5450
}, nil

wox.core/go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.23.0
44

55
require (
66
github.com/Masterminds/semver/v3 v3.3.0
7+
github.com/PuerkitoBio/goquery v1.10.0
78
github.com/cdfmlr/ellipsis v0.0.1
89
github.com/disintegration/imaging v1.6.2
910
github.com/djherbis/buffer v1.2.0
@@ -12,7 +13,7 @@ require (
1213
github.com/fsnotify/fsnotify v1.8.0
1314
github.com/go-resty/resty/v2 v2.15.3
1415
github.com/godbus/dbus/v5 v5.1.0
15-
github.com/google/generative-ai-go v0.18.0
16+
github.com/google/generative-ai-go v0.19.0
1617
github.com/google/uuid v1.6.0
1718
github.com/googleapis/gax-go/v2 v2.13.0
1819
github.com/gorilla/websocket v1.5.3
@@ -56,7 +57,6 @@ require (
5657
cloud.google.com/go/auth/oauth2adapt v0.2.5 // indirect
5758
cloud.google.com/go/compute/metadata v0.5.2 // indirect
5859
cloud.google.com/go/longrunning v0.6.2 // indirect
59-
github.com/PuerkitoBio/goquery v1.10.0 // indirect
6060
github.com/andybalholm/cascadia v1.3.2 // indirect
6161
github.com/davecgh/go-spew v1.1.1 // indirect
6262
github.com/dlclark/regexp2 v1.11.4 // indirect

wox.core/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek
7272
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
7373
github.com/google/generative-ai-go v0.18.0 h1:6ybg9vOCLcI/UpBBYXOTVgvKmcUKFRNj+2Cj3GnebSo=
7474
github.com/google/generative-ai-go v0.18.0/go.mod h1:JYolL13VG7j79kM5BtHz4qwONHkeJQzOCkKXnpqtS/E=
75+
github.com/google/generative-ai-go v0.19.0 h1:R71szggh8wHMCUlEMsW2A/3T+5LdEIkiaHSYgSpUgdg=
76+
github.com/google/generative-ai-go v0.19.0/go.mod h1:JYolL13VG7j79kM5BtHz4qwONHkeJQzOCkKXnpqtS/E=
7577
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
7678
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
7779
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=

wox.core/plugin/system/ai_command.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -456,20 +456,26 @@ func (c *Plugin) processThinking(text string) (thinking string, content string)
456456
const thinkStart = "<think>"
457457
const thinkEnd = "</think>"
458458

459-
// Check if the text starts with the thinking tag
460-
if len(text) >= len(thinkStart) && text[:len(thinkStart)] == thinkStart {
461-
// Find the end tag
459+
// Trim leading newlines for tag detection
460+
trimmedText := strings.TrimLeft(text, "\n")
461+
462+
// Check if the text starts with the thinking tag after trimming newlines
463+
if len(trimmedText) >= len(thinkStart) && trimmedText[:len(thinkStart)] == thinkStart {
464+
// Calculate the offset to maintain original indices
465+
offset := len(text) - len(trimmedText)
466+
467+
// Find the end tag in the original text
462468
endIndex := strings.Index(text, thinkEnd)
463469
if endIndex != -1 {
464470
// Extract thinking content (without the tags)
465-
thinking = text[len(thinkStart):endIndex]
471+
thinking = text[offset+len(thinkStart) : endIndex]
466472
// Extract the remaining content after the thinking tag
467473
if endIndex+len(thinkEnd) < len(text) {
468474
content = text[endIndex+len(thinkEnd):]
469475
}
470476
} else {
471477
// If there's no end tag, the entire text is considered thinking
472-
thinking = text[len(thinkStart):]
478+
thinking = text[offset+len(thinkStart):]
473479
}
474480
} else {
475481
// If there's no thinking tag at the beginning, the entire text is content

wox.core/plugin/system/ai_command_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ func TestAICommandParseThinking(t *testing.T) {
1414
assert.Equal(t, " hello world", thinking)
1515
assert.Equal(t, "this is content", content)
1616

17+
thinking, content = plugin.processThinking("\n<think> hello world</think>this is content")
18+
assert.Equal(t, " hello world", thinking)
19+
assert.Equal(t, "this is content", content)
20+
1721
thinking, content = plugin.processThinking("hello world")
1822
assert.Equal(t, "", thinking)
1923
assert.Equal(t, "hello world", content)

0 commit comments

Comments
 (0)