Skip to content

Commit 0c41558

Browse files
authored
Merge pull request #1140 from trungutt/feat/save-tool-definitions-with-messages
Save tool definitions with assistant messages
2 parents 5533313 + e7cc49d commit 0c41558

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pkg/chat/chat.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ type Message struct {
5252
// For Role=assistant prompts this may be set to the tool calls generated by the model, such as function calls.
5353
ToolCalls []tools.ToolCall `json:"tool_calls,omitempty"`
5454

55+
// ToolDefinitions contains the definitions of tools referenced in ToolCalls.
56+
// This is used to provide tool metadata (name, description, category) when loading historical sessions.
57+
ToolDefinitions []tools.Tool `json:"tool_definitions,omitempty"`
58+
5559
// For Role=tool prompts this should be set to the ID given in the assistant's prior request to call a tool.
5660
ToolCallID string `json:"tool_call_id,omitempty"`
5761

pkg/runtime/runtime.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,13 +704,28 @@ func (r *LocalRuntime) RunStream(ctx context.Context, sess *session.Session) <-c
704704
// Add assistant message to conversation history, but skip empty assistant messages
705705
// Providers reject assistant messages that have neither content nor tool calls.
706706
if strings.TrimSpace(res.Content) != "" || len(res.Calls) > 0 {
707+
// Build tool definitions for the tool calls
708+
var toolDefs []tools.Tool
709+
if len(res.Calls) > 0 {
710+
toolMap := make(map[string]tools.Tool, len(agentTools))
711+
for _, t := range agentTools {
712+
toolMap[t.Name] = t
713+
}
714+
for _, call := range res.Calls {
715+
if def, ok := toolMap[call.Function.Name]; ok {
716+
toolDefs = append(toolDefs, def)
717+
}
718+
}
719+
}
720+
707721
assistantMessage := chat.Message{
708722
Role: chat.MessageRoleAssistant,
709723
Content: res.Content,
710724
ReasoningContent: res.ReasoningContent,
711725
ThinkingSignature: res.ThinkingSignature,
712726
ThoughtSignature: res.ThoughtSignature,
713727
ToolCalls: res.Calls,
728+
ToolDefinitions: toolDefs,
714729
CreatedAt: time.Now().Format(time.RFC3339),
715730
}
716731

0 commit comments

Comments
 (0)