handleNextChunk processa o próximo chunk de arquivos com tratamento de falhas
(ctx context.Context)
| 599 | |
| 600 | // handleNextChunk processa o próximo chunk de arquivos com tratamento de falhas |
| 601 | func (cli *ChatCLI) handleNextChunk(ctx context.Context) bool { |
| 602 | if len(cli.fileChunks) == 0 { |
| 603 | if len(cli.failedChunks) > 0 { |
| 604 | fmt.Println(i18n.T("chunk.no_more_pending_but_failed", len(cli.failedChunks))) |
| 605 | } else { |
| 606 | fmt.Println(i18n.T("chunk.no_more_chunks")) |
| 607 | } |
| 608 | return false |
| 609 | } |
| 610 | |
| 611 | nextChunk := cli.fileChunks[0] |
| 612 | totalChunks := nextChunk.Total |
| 613 | currentChunk := nextChunk.Index |
| 614 | remainingChunks := len(cli.fileChunks) - 1 |
| 615 | |
| 616 | fmt.Println(i18n.T("chunk.sending", currentChunk, totalChunks, remainingChunks)) |
| 617 | |
| 618 | progressInfo := fmt.Sprintf(i18n.T("chunk.progress_header", currentChunk, totalChunks)+"\n"+ |
| 619 | "=============================\n"+ |
| 620 | i18n.T("chunk.progress_processed", currentChunk-1)+"\n"+ |
| 621 | i18n.T("chunk.progress_pending", remainingChunks)+"\n"+ |
| 622 | i18n.T("chunk.progress_failed", len(cli.failedChunks))+"\n"+ |
| 623 | i18n.T("chunk.progress_usage")+"\n\n"+ |
| 624 | "=============================\n\n", |
| 625 | currentChunk, totalChunks, currentChunk-1, remainingChunks, len(cli.failedChunks)) |
| 626 | |
| 627 | prompt := i18n.T("chunk.prompt", currentChunk, totalChunks) |
| 628 | |
| 629 | cli.history = append(cli.history, models.Message{ |
| 630 | Role: "user", |
| 631 | Content: prompt + "\n\n" + progressInfo + nextChunk.Content, |
| 632 | }) |
| 633 | |
| 634 | cli.animation.ShowThinkingAnimation(cli.Client.GetModelName()) |
| 635 | ctx, cancel := context.WithTimeout(ctx, 5*time.Minute) |
| 636 | defer cancel() |
| 637 | aiResponse, err := cli.Client.SendPrompt(ctx, prompt+"\n\n"+nextChunk.Content, cli.history, 0) |
| 638 | cli.animation.StopThinkingAnimation() |
| 639 | |
| 640 | if err != nil { |
| 641 | cli.logger.Error("Erro ao processar chunk com a LLM", zap.Int("chunkIndex", nextChunk.Index), zap.Int("totalChunks", nextChunk.Total), zap.Error(err)) |
| 642 | cli.lastFailedChunk = &cli.fileChunks[0] |
| 643 | cli.failedChunks = append(cli.failedChunks, cli.fileChunks[0]) |
| 644 | cli.fileChunks = cli.fileChunks[1:] |
| 645 | |
| 646 | fmt.Println(i18n.T("chunk.error_processing", currentChunk, totalChunks, err.Error())) |
| 647 | fmt.Println(i18n.T("chunk.moved_to_failed_queue")) |
| 648 | fmt.Println(i18n.T("chunk.retry_or_next")) |
| 649 | return false |
| 650 | } |
| 651 | |
| 652 | cli.history = append(cli.history, models.Message{Role: "assistant", Content: aiResponse}) |
| 653 | renderedResponse := cli.renderMarkdown(aiResponse) |
| 654 | cli.typewriterEffect(fmt.Sprintf("\n%s:\n%s\n", cli.Client.GetModelName(), renderedResponse), 2*time.Millisecond) |
| 655 | cli.fileChunks = cli.fileChunks[1:] |
| 656 | |
| 657 | if len(cli.fileChunks) > 0 || len(cli.failedChunks) > 0 { |
| 658 | cli.printChunkStatus() |
no test coverage detected