refactor: Rename BeginAt and EndAt fields for consistency in message handling
- Change BeginAt and EndAt fields to Begin and End in Message struct for improved clarity. - Update related methods and parameters across the codebase to reflect the new naming convention. - Ensure consistency in timestamp handling within the ScanTokens function and message processing methods.
This commit is contained in:
parent
712c8ade62
commit
f1de4c07f8
4 changed files with 29 additions and 29 deletions
|
|
@ -372,7 +372,7 @@ func (ast *Assistant) streamChat(
|
||||||
toolsCount := 0
|
toolsCount := 0
|
||||||
currentMessageID := ""
|
currentMessageID := ""
|
||||||
tokenID := ""
|
tokenID := ""
|
||||||
beginAt := int64(0)
|
beganAt := int64(0)
|
||||||
var retry error = nil
|
var retry error = nil
|
||||||
var result interface{} = nil // To save the result
|
var result interface{} = nil // To save the result
|
||||||
var content string = "" // To save the content
|
var content string = "" // To save the content
|
||||||
|
|
@ -417,7 +417,7 @@ func (ast *Assistant) streamChat(
|
||||||
// for api reasoning_content response
|
// for api reasoning_content response
|
||||||
if msg.Type == "think" {
|
if msg.Type == "think" {
|
||||||
if isFirstThink {
|
if isFirstThink {
|
||||||
msg.BeginAt = time.Now().UnixNano()
|
msg.Begin = time.Now().UnixNano()
|
||||||
msg.Text = "<think>\n" + msg.Text // add the think begin tag
|
msg.Text = "<think>\n" + msg.Text // add the think begin tag
|
||||||
isFirstThink = false
|
isFirstThink = false
|
||||||
isThinking = true
|
isThinking = true
|
||||||
|
|
@ -431,8 +431,8 @@ func (ast *Assistant) streamChat(
|
||||||
end.ID = currentMessageID
|
end.ID = currentMessageID
|
||||||
end.Retry = ctx.Retry
|
end.Retry = ctx.Retry
|
||||||
end.Silent = ctx.Silent
|
end.Silent = ctx.Silent
|
||||||
end.EndAt = time.Now().UnixNano()
|
end.End = time.Now().UnixNano()
|
||||||
end.BeginAt = beginAt
|
end.Begin = beganAt
|
||||||
|
|
||||||
end.Callback(cb).Write(c.Writer)
|
end.Callback(cb).Write(c.Writer)
|
||||||
end.AppendTo(contents)
|
end.AppendTo(contents)
|
||||||
|
|
@ -444,7 +444,7 @@ func (ast *Assistant) streamChat(
|
||||||
|
|
||||||
// Clear the token
|
// Clear the token
|
||||||
contents.ClearToken(tokenID)
|
contents.ClearToken(tokenID)
|
||||||
beginAt = 0
|
beganAt = 0
|
||||||
tokenID = ""
|
tokenID = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -470,12 +470,12 @@ func (ast *Assistant) streamChat(
|
||||||
}
|
}
|
||||||
|
|
||||||
toolsCount++
|
toolsCount++
|
||||||
msg.BeginAt = time.Now().UnixNano()
|
msg.Begin = time.Now().UnixNano()
|
||||||
}
|
}
|
||||||
|
|
||||||
if msg.IsEndTool {
|
if msg.IsEndTool {
|
||||||
msg.Text = msg.Text + "\n</tool>\n" // add the tool_calls close tag
|
msg.Text = msg.Text + "\n</tool>\n" // add the tool_calls close tag
|
||||||
msg.EndAt = time.Now().UnixNano()
|
msg.End = time.Now().UnixNano()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -487,25 +487,25 @@ func (ast *Assistant) streamChat(
|
||||||
msg.AppendTo(contents) // Append content
|
msg.AppendTo(contents) // Append content
|
||||||
|
|
||||||
// Scan the tokens
|
// Scan the tokens
|
||||||
contents.ScanTokens(currentMessageID, tokenID, beginAt, func(params message.ScanCallbackParams) {
|
contents.ScanTokens(currentMessageID, tokenID, beganAt, func(params message.ScanCallbackParams) {
|
||||||
currentMessageID = params.MessageID
|
currentMessageID = params.MessageID
|
||||||
msg.ID = params.MessageID
|
msg.ID = params.MessageID
|
||||||
msg.Type = params.Token
|
msg.Type = params.Token
|
||||||
msg.Text = "" // clear the text
|
msg.Text = "" // clear the text
|
||||||
msg.Props = map[string]interface{}{"text": params.Text} // Update props
|
msg.Props = map[string]interface{}{"text": params.Text} // Update props
|
||||||
msg.BeginAt = params.BeginAt
|
msg.Begin = params.BeganAt
|
||||||
msg.EndAt = params.EndAt
|
msg.End = params.EndAt
|
||||||
|
|
||||||
// End of the token clear the text
|
// End of the token clear the text
|
||||||
if params.Begin {
|
if params.Begin {
|
||||||
tokenID = params.TokenID
|
tokenID = params.TokenID
|
||||||
beginAt = params.BeginAt
|
beganAt = params.BeganAt
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.End {
|
if params.End {
|
||||||
tokenID = ""
|
tokenID = ""
|
||||||
beginAt = 0
|
beganAt = 0
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -568,8 +568,8 @@ func (ast *Assistant) streamChat(
|
||||||
}
|
}
|
||||||
|
|
||||||
if msg.Type == "think" || msg.Type == "tool" {
|
if msg.Type == "think" || msg.Type == "tool" {
|
||||||
output.BeginAt = msg.BeginAt
|
output.Begin = msg.Begin
|
||||||
output.EndAt = msg.EndAt
|
output.End = msg.End
|
||||||
}
|
}
|
||||||
|
|
||||||
output.Callback(cb).Write(c.Writer)
|
output.Callback(cb).Write(c.Writer)
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ type ScanCallbackParams struct {
|
||||||
Token string
|
Token string
|
||||||
MessageID string
|
MessageID string
|
||||||
TokenID string
|
TokenID string
|
||||||
BeginAt int64
|
BeganAt int64
|
||||||
EndAt int64
|
EndAt int64
|
||||||
Begin bool
|
Begin bool
|
||||||
End bool
|
End bool
|
||||||
|
|
@ -73,7 +73,7 @@ func NewContents() *Contents {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScanTokens scan the tokens
|
// ScanTokens scan the tokens
|
||||||
func (c *Contents) ScanTokens(messageID string, tokenID string, beginAt int64, cb func(params ScanCallbackParams)) {
|
func (c *Contents) ScanTokens(messageID string, tokenID string, beganAt int64, cb func(params ScanCallbackParams)) {
|
||||||
|
|
||||||
text := strings.TrimSpace(c.Text())
|
text := strings.TrimSpace(c.Text())
|
||||||
|
|
||||||
|
|
@ -96,13 +96,13 @@ func (c *Contents) ScanTokens(messageID string, tokenID string, beginAt int64, c
|
||||||
|
|
||||||
c.UpdateType(tokenType, map[string]interface{}{"text": text}, extra)
|
c.UpdateType(tokenType, map[string]interface{}{"text": text}, extra)
|
||||||
c.NewText([]byte(tails), extra) // Create new text with the tails
|
c.NewText([]byte(tails), extra) // Create new text with the tails
|
||||||
cb(ScanCallbackParams{Token: tokenType, MessageID: c.id, TokenID: tokenID, BeginAt: beginAt, Begin: false, End: true, Text: text, Tails: tails, EndAt: extra.End})
|
cb(ScanCallbackParams{Token: tokenType, MessageID: c.id, TokenID: tokenID, BeganAt: beganAt, Begin: false, End: true, Text: text, Tails: tails, EndAt: extra.End})
|
||||||
c.ClearToken(c.token) // clear the token
|
c.ClearToken(c.token) // clear the token
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// call the callback for the scanning of the token
|
// call the callback for the scanning of the token
|
||||||
cb(ScanCallbackParams{Token: tokenType, MessageID: c.id, TokenID: tokenID, BeginAt: beginAt, Begin: false, End: false, Text: text, Tails: "", EndAt: 0})
|
cb(ScanCallbackParams{Token: tokenType, MessageID: c.id, TokenID: tokenID, BeganAt: beganAt, Begin: false, End: false, Text: text, Tails: "", EndAt: 0})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -124,14 +124,14 @@ func (c *Contents) ScanTokens(messageID string, tokenID string, beginAt int64, c
|
||||||
// First time scanning the token, generate the token ID and begin time
|
// First time scanning the token, generate the token ID and begin time
|
||||||
if tokenID == "" || tokenType != name {
|
if tokenID == "" || tokenType != name {
|
||||||
tokenID = GenerateNumericID("T")
|
tokenID = GenerateNumericID("T")
|
||||||
beginAt = time.Now().UnixNano()
|
beganAt = time.Now().UnixNano()
|
||||||
begin = true
|
begin = true
|
||||||
c.token = tokenID
|
c.token = tokenID
|
||||||
c.AppendToken(tokenID, name)
|
c.AppendToken(tokenID, name)
|
||||||
c.UpdateType(name, map[string]interface{}{"text": text, "id": tokenID}, Extra{ID: c.id, Begin: beginAt, End: beginAt})
|
c.UpdateType(name, map[string]interface{}{"text": text, "id": tokenID}, Extra{ID: c.id, Begin: beganAt, End: beganAt})
|
||||||
}
|
}
|
||||||
|
|
||||||
cb(ScanCallbackParams{Token: name, MessageID: c.id, TokenID: tokenID, BeginAt: beginAt, Begin: begin, End: false, Text: text, Tails: "", EndAt: 0}) // call the callback
|
cb(ScanCallbackParams{Token: name, MessageID: c.id, TokenID: tokenID, BeganAt: beganAt, Begin: begin, End: false, Text: text, Tails: "", EndAt: 0}) // call the callback
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,8 +44,8 @@ type Message struct {
|
||||||
IsBeginTool bool `json:"-"` // is new tool for the message for native tool_calls
|
IsBeginTool bool `json:"-"` // is new tool for the message for native tool_calls
|
||||||
IsEndTool bool `json:"-"` // is end tool for the message for native tool_calls
|
IsEndTool bool `json:"-"` // is end tool for the message for native tool_calls
|
||||||
Result any `json:"result,omitempty"` // result for the message
|
Result any `json:"result,omitempty"` // result for the message
|
||||||
BeginAt int64 `json:"begin_at,omitempty"` // begin at for the message // timestamp
|
Begin int64 `json:"begin,omitempty"` // begin at for the message // timestamp
|
||||||
EndAt int64 `json:"end_at,omitempty"` // end at for the message // timestamp
|
End int64 `json:"end,omitempty"` // end at for the message // timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mention represents a mention
|
// Mention represents a mention
|
||||||
|
|
@ -423,10 +423,10 @@ func (m *Message) AppendTo(contents *Contents) *Message {
|
||||||
case "text", "think", "tool", "tool_calls_native":
|
case "text", "think", "tool", "tool_calls_native":
|
||||||
if m.Text != "" {
|
if m.Text != "" {
|
||||||
if m.IsNew {
|
if m.IsNew {
|
||||||
contents.NewText([]byte(m.Text), Extra{ID: m.ID, Begin: m.BeginAt, End: m.EndAt})
|
contents.NewText([]byte(m.Text), Extra{ID: m.ID, Begin: m.Begin, End: m.End})
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
contents.AppendText([]byte(m.Text), Extra{ID: m.ID, Begin: m.BeginAt, End: m.EndAt})
|
contents.AppendText([]byte(m.Text), Extra{ID: m.ID, Begin: m.Begin, End: m.End})
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
|
|
|
||||||
|
|
@ -129,7 +129,7 @@ func (neo *DSL) GenerateWithAI(ctx chatctx.Context, input string, messageType st
|
||||||
isThinking := false
|
isThinking := false
|
||||||
currentMessageID := ""
|
currentMessageID := ""
|
||||||
tokenID := ""
|
tokenID := ""
|
||||||
beginAt := int64(0)
|
beganAt := int64(0)
|
||||||
err := ast.Chat(c.Request.Context(), msgList, neo.Option, func(data []byte) int {
|
err := ast.Chat(c.Request.Context(), msgList, neo.Option, func(data []byte) int {
|
||||||
select {
|
select {
|
||||||
case <-clientBreak:
|
case <-clientBreak:
|
||||||
|
|
@ -179,7 +179,7 @@ func (neo *DSL) GenerateWithAI(ctx chatctx.Context, input string, messageType st
|
||||||
msg.AppendTo(contents)
|
msg.AppendTo(contents)
|
||||||
|
|
||||||
// Scan the tokens
|
// Scan the tokens
|
||||||
contents.ScanTokens(currentMessageID, tokenID, beginAt, func(params message.ScanCallbackParams) {
|
contents.ScanTokens(currentMessageID, tokenID, beganAt, func(params message.ScanCallbackParams) {
|
||||||
currentMessageID = params.MessageID
|
currentMessageID = params.MessageID
|
||||||
msg.ID = params.MessageID
|
msg.ID = params.MessageID
|
||||||
msg.Type = params.Token
|
msg.Type = params.Token
|
||||||
|
|
@ -188,13 +188,13 @@ func (neo *DSL) GenerateWithAI(ctx chatctx.Context, input string, messageType st
|
||||||
|
|
||||||
// End of the token clear the text
|
// End of the token clear the text
|
||||||
if params.Begin {
|
if params.Begin {
|
||||||
msg.BeginAt = beginAt
|
msg.Begin = beganAt
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// End of the token clear the text
|
// End of the token clear the text
|
||||||
if params.End {
|
if params.End {
|
||||||
msg.EndAt = params.EndAt
|
msg.End = params.EndAt
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue