|
| 1 | +package adapter |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/tidwall/gjson" |
| 6 | + "regexp" |
| 7 | +) |
| 8 | + |
| 9 | +func init() { |
| 10 | + Adapters["claude"] = &ClaudeAdapter{} |
| 11 | +} |
| 12 | + |
| 13 | +type ClaudeAdapter struct { |
| 14 | +} |
| 15 | + |
| 16 | +func (g *ClaudeAdapter) HandleResponse(responseBuffer []byte, done bool) (*AdapterResponse, error) { |
| 17 | + content := "" |
| 18 | + reasoningContent := "" |
| 19 | + pattern := `data:(.*?)\n\n` |
| 20 | + re := regexp.MustCompile(pattern) |
| 21 | + matches := re.FindAllStringSubmatch(string(responseBuffer), -1) |
| 22 | + if len(matches) == 0 { |
| 23 | + return nil, fmt.Errorf("no match data") |
| 24 | + } |
| 25 | + thinkStatus := false |
| 26 | + for i := 0; i < len(matches); i++ { |
| 27 | + match := matches[i] |
| 28 | + if len(match) == 2 { |
| 29 | + c, d := g.getDataContent(match[1], &thinkStatus) |
| 30 | + if !d { |
| 31 | + if thinkStatus { |
| 32 | + reasoningContent = reasoningContent + c |
| 33 | + } else { |
| 34 | + content = content + c |
| 35 | + } |
| 36 | + } else { |
| 37 | + done = true |
| 38 | + break |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return &AdapterResponse{ |
| 44 | + Content: content, |
| 45 | + ReasoningContent: reasoningContent, |
| 46 | + ToolCalls: "", |
| 47 | + Done: done, |
| 48 | + }, nil |
| 49 | + |
| 50 | +} |
| 51 | + |
| 52 | +func (g *ClaudeAdapter) getDataContent(jsonData string, thinkStatus *bool) (string, bool) { |
| 53 | + typeResult := gjson.Get(jsonData, "type") |
| 54 | + if typeResult.Type != gjson.String { |
| 55 | + return "", false |
| 56 | + } |
| 57 | + content := "" |
| 58 | + switch typeResult.String() { |
| 59 | + case "content_block_start": |
| 60 | + blockTypeResult := gjson.Get(jsonData, "content_block.type") |
| 61 | + if blockTypeResult.Type == gjson.String { |
| 62 | + if blockTypeResult.String() == "thinking" { |
| 63 | + *thinkStatus = true |
| 64 | + thinkingResult := gjson.Get(jsonData, "content_block.thinking") |
| 65 | + if thinkingResult.Type == gjson.String { |
| 66 | + content = thinkingResult.String() |
| 67 | + } |
| 68 | + } else if blockTypeResult.String() == "text" { |
| 69 | + *thinkStatus = false |
| 70 | + textResult := gjson.Get(jsonData, "content_block.text") |
| 71 | + if textResult.Type == gjson.String { |
| 72 | + content = textResult.String() |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + case "content_block_delta": |
| 77 | + blockDeltaTypeResult := gjson.Get(jsonData, "delta.type") |
| 78 | + if blockDeltaTypeResult.Type == gjson.String { |
| 79 | + if blockDeltaTypeResult.String() == "thinking_delta" { |
| 80 | + textResult := gjson.Get(jsonData, "delta.thinking") |
| 81 | + if textResult.Type == gjson.String { |
| 82 | + content = textResult.String() |
| 83 | + } |
| 84 | + } else if blockDeltaTypeResult.String() == "text_delta" { |
| 85 | + textResult := gjson.Get(jsonData, "delta.text") |
| 86 | + if textResult.Type == gjson.String { |
| 87 | + content = textResult.String() |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + case "content_block_stop": |
| 92 | + case "message_stop": |
| 93 | + return "", true |
| 94 | + } |
| 95 | + return content, false |
| 96 | +} |
0 commit comments