Skip to content

Commit dcd335d

Browse files
trunguttdgageot
authored andcommitted
Add createdAt for chat.Message
Signed-off-by: Trung Nguyen <trungutt@users.noreply.github.com>
1 parent 3668ea4 commit dcd335d

4 files changed

Lines changed: 31 additions & 13 deletions

File tree

cmd/root/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ func createUserMessageWithAttachment(agentFilename, userContent, attachmentPath
578578
Message: chat.Message{
579579
Role: chat.MessageRoleUser,
580580
MultiContent: multiContent,
581+
CreatedAt: time.Now(),
581582
},
582583
}
583584
}

pkg/chat/chat.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package chat
22

3-
import "github.com/docker/cagent/pkg/tools"
3+
import (
4+
"time"
5+
6+
"github.com/docker/cagent/pkg/tools"
7+
)
48

59
type MessageRole string
610

@@ -56,6 +60,8 @@ type Message struct {
5660

5761
// For Role=tool prompts this should be set to the ID given in the assistant's prior request to call a tool.
5862
ToolCallID string `json:"tool_call_id,omitempty"`
63+
64+
CreatedAt time.Time `json:"created_at"`
5965
}
6066

6167
type MessagePart struct {

pkg/runtime/runtime.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"log/slog"
1010
"strings"
11+
"time"
1112

1213
"github.com/docker/cagent/pkg/agent"
1314
"github.com/docker/cagent/pkg/chat"
@@ -223,6 +224,7 @@ func (r *Runtime) RunStream(ctx context.Context, sess *session.Session) <-chan E
223224
Role: chat.MessageRoleAssistant,
224225
Content: content,
225226
ToolCalls: calls,
227+
CreatedAt: time.Now(),
226228
}
227229

228230
sess.AddMessage(session.NewAgentMessage(a, &assistantMessage))
@@ -544,6 +546,7 @@ func (r *Runtime) runTool(ctx context.Context, tool tools.Tool, toolCall tools.T
544546
Role: chat.MessageRoleTool,
545547
Content: res.Output,
546548
ToolCallID: toolCall.ID,
549+
CreatedAt: time.Now(),
547550
}
548551
sess.AddMessage(session.NewAgentMessage(a, &toolResponseMsg))
549552
}
@@ -584,6 +587,7 @@ func (r *Runtime) runAgentTool(ctx context.Context, handler ToolHandler, sess *s
584587
Role: chat.MessageRoleTool,
585588
Content: output,
586589
ToolCallID: toolCall.ID,
590+
CreatedAt: time.Now(),
587591
}
588592
sess.AddMessage(session.NewAgentMessage(a, &toolResponseMsg))
589593
}
@@ -614,6 +618,7 @@ func (r *Runtime) addToolCancelledResponse(sess *session.Session, toolCall tools
614618
Role: chat.MessageRoleTool,
615619
Content: result,
616620
ToolCallID: toolCall.ID,
621+
CreatedAt: time.Now(),
617622
}
618623
sess.AddMessage(session.NewAgentMessage(a, &toolResponseMsg))
619624
}

pkg/session/session.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ func UserMessage(agentFilename, content string) *Message {
7474
AgentFilename: agentFilename,
7575
AgentName: "",
7676
Message: chat.Message{
77-
Role: chat.MessageRoleUser,
78-
Content: content,
77+
Role: chat.MessageRoleUser,
78+
Content: content,
79+
CreatedAt: time.Now(),
7980
},
8081
}
8182
}
@@ -93,8 +94,9 @@ func SystemMessage(content string) *Message {
9394
AgentFilename: "",
9495
AgentName: "",
9596
Message: chat.Message{
96-
Role: chat.MessageRoleSystem,
97-
Content: content,
97+
Role: chat.MessageRoleSystem,
98+
Content: content,
99+
CreatedAt: time.Now(),
98100
},
99101
}
100102
}
@@ -199,8 +201,9 @@ func (s *Session) GetMessages(a *agent.Agent) []chat.Message {
199201
}
200202

201203
messages = append(messages, chat.Message{
202-
Role: "system",
203-
Content: "You are a multi-agent system, make sure to answer the user query in the most helpful way possible. You have access to these sub-agents:\n" + subAgentsStr + "\nIMPORTANT: You can ONLY transfer tasks to the agents listed above using their ID. The valid agent IDs are: " + strings.Join(validAgentIDs, ", ") + ". You MUST NOT attempt to transfer to any other agent IDs - doing so will cause system errors.\n\nIf you are the best to answer the question according to your description, you can answer it.\n\nIf another agent is better for answering the question according to its description, call `transfer_task` function to transfer the question to that agent using the agent's ID. When transferring, do not generate any text other than the function call.\n\n",
204+
Role: "system",
205+
Content: "You are a multi-agent system, make sure to answer the user query in the most helpful way possible. You have access to these sub-agents:\n" + subAgentsStr + "\nIMPORTANT: You can ONLY transfer tasks to the agents listed above using their ID. The valid agent IDs are: " + strings.Join(validAgentIDs, ", ") + ". You MUST NOT attempt to transfer to any other agent IDs - doing so will cause system errors.\n\nIf you are the best to answer the question according to your description, you can answer it.\n\nIf another agent is better for answering the question according to its description, call `transfer_task` function to transfer the question to that agent using the agent's ID. When transferring, do not generate any text other than the function call.\n\n",
206+
CreatedAt: time.Now(),
204207
})
205208
}
206209

@@ -210,15 +213,17 @@ func (s *Session) GetMessages(a *agent.Agent) []chat.Message {
210213
}
211214

212215
messages = append(messages, chat.Message{
213-
Role: chat.MessageRoleSystem,
214-
Content: a.Instruction() + "\n\n" + date,
216+
Role: chat.MessageRoleSystem,
217+
Content: a.Instruction() + "\n\n" + date,
218+
CreatedAt: time.Now(),
215219
})
216220

217221
for _, tool := range a.ToolSets() {
218222
if tool.Instructions() != "" {
219223
messages = append(messages, chat.Message{
220-
Role: chat.MessageRoleSystem,
221-
Content: tool.Instructions(),
224+
Role: chat.MessageRoleSystem,
225+
Content: tool.Instructions(),
226+
CreatedAt: time.Now(),
222227
})
223228
}
224229
}
@@ -233,8 +238,9 @@ func (s *Session) GetMessages(a *agent.Agent) []chat.Message {
233238

234239
if lastSummaryIndex != -1 {
235240
messages = append(messages, chat.Message{
236-
Role: chat.MessageRoleSystem,
237-
Content: "Session Summary: " + s.Messages[lastSummaryIndex].Summary,
241+
Role: chat.MessageRoleSystem,
242+
Content: "Session Summary: " + s.Messages[lastSummaryIndex].Summary,
243+
CreatedAt: time.Now(),
238244
})
239245
}
240246

0 commit comments

Comments
 (0)