refactor: adds a message to the conversation history

This commit is contained in:
lilong.129
2025-04-28 20:12:08 +08:00
parent 427cc1dab2
commit 68dbeb368a
5 changed files with 26 additions and 24 deletions

View File

@@ -1 +1 @@
v5.0.0-beta-2504281959
v5.0.0-beta-2504282012

View File

@@ -47,7 +47,7 @@ type UITarsAsserter struct {
model *ark.ChatModel
config *ark.ChatModelConfig
systemPrompt string
history []*schema.Message // conversation history
history ConversationHistory
}
// NewUITarsAsserter creates a new UITarsAsserter instance
@@ -87,7 +87,7 @@ func (a *UITarsAsserter) Assert(opts *AssertOptions) (*AssertionResponse, error)
}
// Reset history for each new assertion
a.history = []*schema.Message{
a.history = ConversationHistory{
{
Role: schema.System,
Content: a.systemPrompt,
@@ -118,7 +118,7 @@ Here is the assertion. Please tell whether it is truthy according to the screens
}
// Append user message to history
appendConversationHistory(&a.history, userMsg)
a.history.Append(userMsg)
// Call model service, generate response
logRequest(a.history)
@@ -138,7 +138,7 @@ Here is the assertion. Please tell whether it is truthy according to the screens
}
// Append assistant message to history
appendConversationHistory(&a.history, &schema.Message{
a.history.Append(&schema.Message{
Role: schema.Assistant,
Content: resp.Content,
})

View File

@@ -131,8 +131,10 @@ func logResponse(resp *schema.Message) {
logger.Msg("log response message")
}
// appendConversationHistory adds a message to the conversation history
func appendConversationHistory(history *[]*schema.Message, msg *schema.Message) {
type ConversationHistory []*schema.Message
// Append adds a message to the conversation history
func (h *ConversationHistory) Append(msg *schema.Message) {
// for user image message:
// - keep at most 4 user image messages
// - delete the oldest user image message when the limit is reached
@@ -142,7 +144,7 @@ func appendConversationHistory(history *[]*schema.Message, msg *schema.Message)
firstUserImgIndex := -1
// calculate the number of user messages and find the index of the first user message
for i, item := range *history {
for i, item := range *h {
if item.Role == schema.User {
userImgCount++
if firstUserImgIndex == -1 {
@@ -154,28 +156,28 @@ func appendConversationHistory(history *[]*schema.Message, msg *schema.Message)
// if there are already 4 user messages, delete the first one before adding the new message
if userImgCount >= 4 && firstUserImgIndex >= 0 {
// delete the first user message
*history = append(
(*history)[:firstUserImgIndex],
(*history)[firstUserImgIndex+1:]...,
*h = append(
(*h)[:firstUserImgIndex],
(*h)[firstUserImgIndex+1:]...,
)
}
// add the new user message to the history
*history = append(*history, msg)
*h = append(*h, msg)
}
// for assistant message:
// - keep at most the last 10 assistant messages
if msg.Role == schema.Assistant {
// add the new assistant message to the history
*history = append(*history, msg)
*h = append(*h, msg)
// if there are more than 10 assistant messages, remove the oldest ones
assistantMsgCount := 0
for i := len(*history) - 1; i >= 0; i-- {
if (*history)[i].Role == schema.Assistant {
for i := len(*h) - 1; i >= 0; i-- {
if (*h)[i].Role == schema.Assistant {
assistantMsgCount++
if assistantMsgCount > 10 {
*history = append((*history)[:i], (*history)[i+1:]...)
*h = append((*h)[:i], (*h)[i+1:]...)
}
}
}

View File

@@ -110,7 +110,7 @@ type Planner struct {
model model.ToolCallingChatModel
config *openai.ChatModelConfig
systemPrompt string
history []*schema.Message // conversation history
history ConversationHistory
}
// Call performs UI planning using Vision Language Model
@@ -124,7 +124,7 @@ func (p *Planner) Call(opts *PlanningOptions) (*PlanningResult, error) {
if len(p.history) == 0 {
// add system message
systemPrompt := uiTarsPlanningPrompt + opts.UserInstruction
p.history = []*schema.Message{
p.history = ConversationHistory{
{
Role: schema.System,
Content: systemPrompt,
@@ -132,7 +132,7 @@ func (p *Planner) Call(opts *PlanningOptions) (*PlanningResult, error) {
}
}
// append user image message
appendConversationHistory(&p.history, opts.Message)
p.history.Append(opts.Message)
// call model service, generate response
logRequest(p.history)
@@ -152,7 +152,7 @@ func (p *Planner) Call(opts *PlanningOptions) (*PlanningResult, error) {
}
// append assistant message
appendConversationHistory(&p.history, &schema.Message{
p.history.Append(&schema.Message{
Role: schema.Assistant,
Content: result.ActionSummary,
})

View File

@@ -123,7 +123,7 @@ type UITarsPlanner struct {
model model.ToolCallingChatModel
config *ark.ChatModelConfig
systemPrompt string
history []*schema.Message // conversation history
history ConversationHistory
}
// Call performs UI planning using Vision Language Model
@@ -137,7 +137,7 @@ func (p *UITarsPlanner) Call(opts *PlanningOptions) (*PlanningResult, error) {
if len(p.history) == 0 {
// add system message
systemPrompt := uiTarsPlanningPrompt + opts.UserInstruction
p.history = []*schema.Message{
p.history = ConversationHistory{
{
Role: schema.System,
Content: systemPrompt,
@@ -145,7 +145,7 @@ func (p *UITarsPlanner) Call(opts *PlanningOptions) (*PlanningResult, error) {
}
}
// append user image message
appendConversationHistory(&p.history, opts.Message)
p.history.Append(opts.Message)
// call model service, generate response
logRequest(p.history)
@@ -165,7 +165,7 @@ func (p *UITarsPlanner) Call(opts *PlanningOptions) (*PlanningResult, error) {
}
// append assistant message
appendConversationHistory(&p.history, &schema.Message{
p.history.Append(&schema.Message{
Role: schema.Assistant,
Content: result.ActionSummary,
})