Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -986,17 +986,36 @@ func (a *agent) validateToolCall(toolCall ToolCallContent, availableTools []Agen
}

func (a *agent) createPrompt(system, prompt string, messages []Message, files ...FilePart) (Prompt, error) {
if prompt == "" {
if prompt == "" && len(messages) < 1 {
return nil, &Error{Title: "invalid argument", Message: "prompt can't be empty"}
}

var preparedPrompt Prompt

var newsystem string
// Check first message for system role
if len(messages) > 0 && messages[0].Role == MessageRoleSystem {
if len(messages[0].Content) > 0 {
Comment on lines +997 to +998
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably iterate over all messages and collect system role messages, + all message content instead of just the first message.

if tp, ok := messages[0].Content[0].(TextPart); ok {
newsystem = tp.Text
}
}
}
// Rewrite system role with main role of agent if it exist
if system != "" {
preparedPrompt = append(preparedPrompt, NewSystemMessage(system))
newsystem = system
}

if newsystem != "" {
preparedPrompt = append(preparedPrompt, NewSystemMessage(newsystem))
}

preparedPrompt = append(preparedPrompt, messages...)
preparedPrompt = append(preparedPrompt, NewUserMessage(prompt, files...))

if prompt != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if prompt is empty we should guard that the last message in the message list is a user message, or a tool response.

I think its not expected that we send a message with the last message being an assistant message.

preparedPrompt = append(preparedPrompt, NewUserMessage(prompt, files...))
}

return preparedPrompt, nil
}

Expand Down