(ctx *gin.Context, w http.ResponseWriter, id string, client *openai.Client, conversationCtx *ConversationContext)
| 430 | } |
| 431 | |
| 432 | func (c *AIController) handleAIConversation(ctx *gin.Context, w http.ResponseWriter, id string, client *openai.Client, conversationCtx *ConversationContext) { |
| 433 | maxRounds := 10 |
| 434 | messages := conversationCtx.GetOpenAIMessages() |
| 435 | |
| 436 | for round := range maxRounds { |
| 437 | log.Debugf("AI conversation round: %d", round+1) |
| 438 | |
| 439 | aiReq := openai.ChatCompletionRequest{ |
| 440 | Model: conversationCtx.Model, |
| 441 | Messages: messages, |
| 442 | Tools: c.getMCPTools(), |
| 443 | Stream: true, |
| 444 | } |
| 445 | |
| 446 | toolCalls, newMessages, finished, aiResponse := c.processAIStream(ctx, w, id, conversationCtx.Model, client, aiReq, messages) |
| 447 | messages = newMessages |
| 448 | |
| 449 | log.Debugf("Round %d: toolCalls=%v", round+1, toolCalls) |
| 450 | if aiResponse != "" { |
| 451 | conversationCtx.Messages = append(conversationCtx.Messages, &ai_conversation.ConversationMessage{ |
| 452 | Role: "assistant", |
| 453 | Content: aiResponse, |
| 454 | }) |
| 455 | } |
| 456 | |
| 457 | if finished { |
| 458 | return |
| 459 | } |
| 460 | |
| 461 | if len(toolCalls) > 0 { |
| 462 | messages = c.executeToolCalls(ctx, w, id, conversationCtx.Model, toolCalls, messages) |
| 463 | } else { |
| 464 | return |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | log.Warnf("AI conversation reached maximum rounds limit: %d", maxRounds) |
| 469 | } |
| 470 | |
| 471 | // processAIStream |
| 472 | func (c *AIController) processAIStream( |
no test coverage detected