fix: improve migration logic and reduce code duplication

- Preserve user's configured model during config migration (issue #5)
- Simplify ExtractProtocol using strings.Cut
- Extract NormalizeToolCall to shared utility, removing ~70 lines of duplicate code
- Clean up unused fields in providerMigrationConfig struct

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
yinwm
2026-02-19 09:22:39 +08:00
parent 09a0d19119
commit ec86b21d3f
6 changed files with 600 additions and 285 deletions
+1 -40
View File
@@ -607,7 +607,7 @@ func (al *AgentLoop) runLLMIteration(ctx context.Context, messages []providers.M
normalizedToolCalls := make([]providers.ToolCall, 0, len(response.ToolCalls))
for _, tc := range response.ToolCalls {
normalizedToolCalls = append(normalizedToolCalls, normalizeProviderToolCall(tc))
normalizedToolCalls = append(normalizedToolCalls, providers.NormalizeToolCall(tc))
}
// Log tool calls
@@ -715,45 +715,6 @@ func (al *AgentLoop) runLLMIteration(ctx context.Context, messages []providers.M
return finalContent, iteration, nil
}
func normalizeProviderToolCall(tc providers.ToolCall) providers.ToolCall {
normalized := tc
if normalized.Name == "" && normalized.Function != nil {
normalized.Name = normalized.Function.Name
}
if normalized.Arguments == nil {
normalized.Arguments = map[string]interface{}{}
}
if len(normalized.Arguments) == 0 && normalized.Function != nil && normalized.Function.Arguments != "" {
var parsed map[string]interface{}
if err := json.Unmarshal([]byte(normalized.Function.Arguments), &parsed); err == nil && parsed != nil {
normalized.Arguments = parsed
}
}
argsJSON, _ := json.Marshal(normalized.Arguments)
if normalized.Function == nil {
normalized.Function = &providers.FunctionCall{
Name: normalized.Name,
Arguments: string(argsJSON),
}
} else {
if normalized.Function.Name == "" {
normalized.Function.Name = normalized.Name
}
if normalized.Name == "" {
normalized.Name = normalized.Function.Name
}
if normalized.Function.Arguments == "" {
normalized.Function.Arguments = string(argsJSON)
}
}
return normalized
}
// updateToolContexts updates the context for tools that need channel/chatID info.
func (al *AgentLoop) updateToolContexts(channel, chatID string) {
// Use ContextualTool interface instead of type assertions