Skip to content

Commit cb70452

Browse files
committed
fix claude event id buf error
1 parent e9f107b commit cb70452

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

adapter/claude/chat.go

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,37 +66,55 @@ func (c *ChatInstance) CreateChatRequest(props *ChatProps) (string, error) {
6666
return "", fmt.Errorf("claude error: invalid response")
6767
}
6868

69+
func (c *ChatInstance) ProcessLine(buf, data string) (string, error) {
70+
// response example:
71+
//
72+
// event:completion
73+
// data:{"completion":"!","stop_reason":null,"model":"claude-2.0","stop":null,"log_id":"f5f659a5807419c94cfac4a9f2f79a66e95733975714ce7f00e30689dd136b02"}
74+
75+
if !strings.HasPrefix(data, "data:") && strings.HasPrefix(data, "event:") {
76+
return "", nil
77+
} else {
78+
data = strings.TrimSpace(strings.TrimPrefix(data, "data:"))
79+
}
80+
81+
if len(data) == 0 {
82+
return "", nil
83+
}
84+
85+
if form := utils.UnmarshalForm[ChatResponse](data); form != nil {
86+
return form.Completion, nil
87+
}
88+
89+
data = buf + data
90+
if form := utils.UnmarshalForm[ChatResponse](data); form != nil {
91+
return form.Completion, nil
92+
}
93+
94+
globals.Warn(fmt.Sprintf("anthropic error: cannot parse response: %s", data))
95+
return "", fmt.Errorf("claude error: invalid response")
96+
}
97+
6998
// CreateStreamChatRequest is the stream request for anthropic claude
7099
func (c *ChatInstance) CreateStreamChatRequest(props *ChatProps, hook globals.Hook) error {
100+
buf := ""
101+
71102
return utils.EventSource(
72103
"POST",
73104
c.GetChatEndpoint(),
74105
c.GetChatHeaders(),
75106
c.GetChatBody(props, true),
76107
func(data string) error {
77-
// response example:
78-
//
79-
// event:completion
80-
// data:{"completion":"!","stop_reason":null,"model":"claude-2.0","stop":null,"log_id":"f5f659a5807419c94cfac4a9f2f79a66e95733975714ce7f00e30689dd136b02"}
81-
82-
if !strings.HasPrefix(data, "data:") && strings.HasPrefix(data, "event:") {
83-
return nil
84-
} else {
85-
data = strings.TrimSpace(strings.TrimPrefix(data, "data:"))
86-
}
87108

88-
if len(data) == 0 {
89-
return nil
90-
}
91-
92-
if form := utils.UnmarshalForm[ChatResponse](data); form != nil {
93-
if err := hook(form.Completion); err != nil {
109+
if resp, err := c.ProcessLine(buf, data); err == nil && len(resp) > 0 {
110+
buf = ""
111+
if err := hook(resp); err != nil {
94112
return err
95113
}
96-
return nil
114+
} else {
115+
buf = buf + data
97116
}
98117

99-
globals.Warn(fmt.Sprintf("anthropic error: cannot parse response: %s", data))
100118
return nil
101119
})
102120
}

globals/logger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func init() {
3232

3333
Logger.SetOutput(&lumberjack.Logger{
3434
Filename: "logs/chat.log",
35-
MaxSize: 20,
36-
MaxBackups: 20,
35+
MaxSize: 1,
36+
MaxBackups: 500,
3737
MaxAge: 1,
3838
})
3939

0 commit comments

Comments
 (0)