@@ -66,37 +66,55 @@ func (c *ChatInstance) CreateChatRequest(props *ChatProps) (string, error) {
66
66
return "" , fmt .Errorf ("claude error: invalid response" )
67
67
}
68
68
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
+
69
98
// CreateStreamChatRequest is the stream request for anthropic claude
70
99
func (c * ChatInstance ) CreateStreamChatRequest (props * ChatProps , hook globals.Hook ) error {
100
+ buf := ""
101
+
71
102
return utils .EventSource (
72
103
"POST" ,
73
104
c .GetChatEndpoint (),
74
105
c .GetChatHeaders (),
75
106
c .GetChatBody (props , true ),
76
107
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
- }
87
108
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 {
94
112
return err
95
113
}
96
- return nil
114
+ } else {
115
+ buf = buf + data
97
116
}
98
117
99
- globals .Warn (fmt .Sprintf ("anthropic error: cannot parse response: %s" , data ))
100
118
return nil
101
119
})
102
120
}
0 commit comments