Commit 86b9dc63 by CalciumIon

refactor: Update Message methods to use pointer receivers

- Refactored ParseToolCalls, SetToolCalls, IsStringContent, and ParseContent methods in the Message struct to use pointer receivers, improving efficiency and consistency in handling mutable state.
- Enhanced code readability and maintainability by ensuring all relevant methods operate on the pointer receiver, aligning with Go best practices.
parent 18ed3795
...@@ -93,22 +93,6 @@ type Message struct { ...@@ -93,22 +93,6 @@ type Message struct {
ToolCallId string `json:"tool_call_id,omitempty"` ToolCallId string `json:"tool_call_id,omitempty"`
} }
func (m Message) ParseToolCalls() []ToolCall {
if m.ToolCalls == nil {
return nil
}
var toolCalls []ToolCall
if err := json.Unmarshal(m.ToolCalls, &toolCalls); err == nil {
return toolCalls
}
return toolCalls
}
func (m *Message) SetToolCalls(toolCalls any) {
toolCallsJson, _ := json.Marshal(toolCalls)
m.ToolCalls = toolCallsJson
}
type MediaContent struct { type MediaContent struct {
Type string `json:"type"` Type string `json:"type"`
Text string `json:"text"` Text string `json:"text"`
...@@ -132,7 +116,23 @@ const ( ...@@ -132,7 +116,23 @@ const (
ContentTypeInputAudio = "input_audio" ContentTypeInputAudio = "input_audio"
) )
func (m Message) StringContent() string { func (m *Message) ParseToolCalls() []ToolCall {
if m.ToolCalls == nil {
return nil
}
var toolCalls []ToolCall
if err := json.Unmarshal(m.ToolCalls, &toolCalls); err == nil {
return toolCalls
}
return toolCalls
}
func (m *Message) SetToolCalls(toolCalls any) {
toolCallsJson, _ := json.Marshal(toolCalls)
m.ToolCalls = toolCallsJson
}
func (m *Message) StringContent() string {
var stringContent string var stringContent string
if err := json.Unmarshal(m.Content, &stringContent); err == nil { if err := json.Unmarshal(m.Content, &stringContent); err == nil {
return stringContent return stringContent
...@@ -145,7 +145,7 @@ func (m *Message) SetStringContent(content string) { ...@@ -145,7 +145,7 @@ func (m *Message) SetStringContent(content string) {
m.Content = jsonContent m.Content = jsonContent
} }
func (m Message) IsStringContent() bool { func (m *Message) IsStringContent() bool {
var stringContent string var stringContent string
if err := json.Unmarshal(m.Content, &stringContent); err == nil { if err := json.Unmarshal(m.Content, &stringContent); err == nil {
return true return true
...@@ -153,7 +153,7 @@ func (m Message) IsStringContent() bool { ...@@ -153,7 +153,7 @@ func (m Message) IsStringContent() bool {
return false return false
} }
func (m Message) ParseContent() []MediaContent { func (m *Message) ParseContent() []MediaContent {
var contentList []MediaContent var contentList []MediaContent
var stringContent string var stringContent string
if err := json.Unmarshal(m.Content, &stringContent); err == nil { if err := json.Unmarshal(m.Content, &stringContent); err == nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment