MCPcopy Create free account
hub / github.com/53AI/53AIHub / Collect

Method Collect

api/controller/response_handler.go:92–125  ·  view source on GitHub ↗
(chunk []byte)

Source from the content-addressed store, hash-verified

90}
91
92func (c *StreamResponseCollector) Collect(chunk []byte) {
93 data := string(chunk)
94 lines := strings.Split(data, "\n")
95
96 for _, line := range lines {
97 if strings.HasPrefix(line, "data: ") {
98 dataContent := strings.TrimPrefix(line, "data: ")
99 if dataContent == "[DONE]" {
100 continue
101 }
102
103 var streamResp struct {
104 Choices []struct {
105 Delta struct {
106 Content *string `json:"content"`
107 ReasoningContent *string `json:"reasoning_content"`
108 } `json:"delta"`
109 } `json:"choices"`
110 }
111
112 if err := json.Unmarshal([]byte(dataContent), &streamResp); err == nil {
113 if len(streamResp.Choices) > 0 {
114 delta := streamResp.Choices[0].Delta
115 if delta.Content != nil && *delta.Content != "" {
116 c.content.WriteString(*delta.Content)
117 }
118 if delta.ReasoningContent != nil && *delta.ReasoningContent != "" {
119 c.reasoningContent.WriteString(*delta.ReasoningContent)
120 }
121 }
122 }
123 }
124 }
125}
126
127func (c *StreamResponseCollector) GetContent() (string, string) {
128 return c.content.String(), c.reasoningContent.String()

Callers 1

WriteMethod · 0.45

Calls 1

WriteStringMethod · 0.80

Tested by

no test coverage detected