Skip to content

Commit 3f96de6

Browse files
authored
Merge pull request #172 from APIParkLab/feature/ai-balance
fix: ai event handler read event error
2 parents a8bb0c2 + e869997 commit 3f96de6

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

app/ai-event-handler/nsq.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"log"
78
"strings"
89
"time"
@@ -27,8 +28,8 @@ func init() {
2728
}
2829

2930
type NSQConfig struct {
30-
Addr string `json:"addr"`
31-
TopicPrefix string `json:"topic_prefix"`
31+
Addr string `json:"addr" yaml:"addr"`
32+
TopicPrefix string `json:"topic_prefix" yaml:"topic_prefix"`
3233
}
3334

3435
// 定义 NSQ 消息结构
@@ -78,6 +79,11 @@ func convertInt(value interface{}) int {
7879
}
7980
}
8081

82+
func genAIKey(key string, provider string) string {
83+
keys := strings.Split(key, "@")
84+
return strings.TrimSuffix(keys[0], fmt.Sprintf("-%s", provider))
85+
}
86+
8187
// HandleMessage 处理从 NSQ 读取的消息
8288
func (h *NSQHandler) HandleMessage(message *nsq.Message) error {
8389
log.Printf("Received message: %s", string(message.Body))
@@ -104,8 +110,7 @@ func (h *NSQHandler) HandleMessage(message *nsq.Message) error {
104110
finalStatus := &AIProviderStatus{}
105111
for _, s := range data.AI.ProviderStats {
106112
status := ToKeyStatus(s.Status).Int()
107-
keys := strings.Split(s.Key, "@")
108-
key := keys[0]
113+
key := genAIKey(s.Key, s.Provider)
109114
err = h.aiKeyService.Save(ctx, key, &ai_key.Edit{
110115
Status: &status,
111116
})
@@ -128,8 +133,9 @@ func (h *NSQHandler) HandleMessage(message *nsq.Message) error {
128133
finalStatus = &s
129134
}
130135
if finalStatus != nil {
131-
keys := strings.Split(finalStatus.Key, "@")
132-
err = h.aiKeyService.IncrUseToken(ctx, keys[0], convertInt(data.AI.TotalToken))
136+
//keys := strings.Split(finalStatus.Key, "@")
137+
key := genAIKey(finalStatus.Key, finalStatus.Provider)
138+
err = h.aiKeyService.IncrUseToken(ctx, key, convertInt(data.AI.TotalToken))
133139
if err != nil {
134140
log.Printf("Failed to increment AI key token: %v", err)
135141
return err

0 commit comments

Comments
 (0)