MCPcopy Index your code
hub / github.com/apache/answer / handleAIConversation

Method handleAIConversation

internal/controller/ai_controller.go:432–469  ·  view source on GitHub ↗
(ctx *gin.Context, w http.ResponseWriter, id string, client *openai.Client, conversationCtx *ConversationContext)

Source from the content-addressed store, hash-verified

430}
431
432func (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
472func (c *AIController) processAIStream(

Callers 1

redirectRequestToAIMethod · 0.95

Calls 4

getMCPToolsMethod · 0.95
processAIStreamMethod · 0.95
executeToolCallsMethod · 0.95
GetOpenAIMessagesMethod · 0.80

Tested by

no test coverage detected