Skip to content

Commit aa2c777

Browse files
committed
Refactor content block types in SSEWriter and types package for improved clarity and structure
1 parent 0426e62 commit aa2c777

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

internal/claude/sse.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"encoding/json"
55
"fmt"
66

7-
"github.com/gin-gonic/gin"
87
"puter2api/internal/types"
8+
9+
"github.com/gin-gonic/gin"
910
)
1011

1112
// SSEWriter SSE 写入器
@@ -47,7 +48,7 @@ func (w *SSEWriter) SendTextBlockStart(index int) {
4748
w.SendEvent("content_block_start", types.ContentBlockStartEvent{
4849
Type: "content_block_start",
4950
Index: index,
50-
ContentBlock: types.ContentBlock{Type: "text", Text: ""},
51+
ContentBlock: types.TextContentBlock{Type: "text", Text: ""},
5152
})
5253
}
5354

@@ -65,7 +66,7 @@ func (w *SSEWriter) SendToolUseBlockStart(index int, id, name string) {
6566
w.SendEvent("content_block_start", types.ContentBlockStartEvent{
6667
Type: "content_block_start",
6768
Index: index,
68-
ContentBlock: types.ContentBlock{
69+
ContentBlock: types.ToolUseContentBlock{
6970
Type: "tool_use",
7071
ID: id,
7172
Name: name,
@@ -104,4 +105,3 @@ func (w *SSEWriter) SendMessageDelta(stopReason string, outputTokens int) {
104105
func (w *SSEWriter) SendMessageStop() {
105106
w.SendEvent("message_stop", types.MessageStopEvent{Type: "message_stop"})
106107
}
107-

internal/types/types.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ type ContentBlock struct {
3030
Content json.RawMessage `json:"content,omitempty"`
3131
}
3232

33+
// TextContentBlock 文本内容块(text 字段始终存在)
34+
type TextContentBlock struct {
35+
Type string `json:"type"`
36+
Text string `json:"text"`
37+
}
38+
39+
// ToolUseContentBlock 工具使用内容块
40+
type ToolUseContentBlock struct {
41+
Type string `json:"type"`
42+
ID string `json:"id"`
43+
Name string `json:"name"`
44+
Input json.RawMessage `json:"input"`
45+
}
46+
3347
// Usage token 使用量
3448
type Usage struct {
3549
InputTokens int `json:"input_tokens"`
@@ -77,9 +91,9 @@ type MessageStartDetail struct {
7791

7892
// ContentBlockStartEvent content_block_start 事件
7993
type ContentBlockStartEvent struct {
80-
Type string `json:"type"`
81-
Index int `json:"index"`
82-
ContentBlock ContentBlock `json:"content_block"`
94+
Type string `json:"type"`
95+
Index int `json:"index"`
96+
ContentBlock interface{} `json:"content_block"`
8397
}
8498

8599
// ContentBlockDeltaEvent content_block_delta 事件

0 commit comments

Comments
 (0)