processDirectoryChunked processa um diretório e divide o conteúdo em chunks
(ctx context.Context, path string, tokenEstimator func(string) int, maxTokens int)
| 514 | |
| 515 | // processDirectoryChunked processa um diretório e divide o conteúdo em chunks |
| 516 | func (cli *ChatCLI) processDirectoryChunked(ctx context.Context, path string, tokenEstimator func(string) int, maxTokens int) ([]FileChunk, error) { |
| 517 | path, err := utils.ExpandPath(path) |
| 518 | if err != nil { |
| 519 | return nil, fmt.Errorf("erro ao expandir o caminho: %w", err) |
| 520 | } |
| 521 | |
| 522 | // Configurar opções de processamento de diretório |
| 523 | scanOptions := utils.DefaultDirectoryScanOptions(cli.logger) |
| 524 | scanOptions.OnFileProcessed = func(info utils.FileInfo) { |
| 525 | cli.animation.UpdateMessage(fmt.Sprintf("Processando %s", info.Path)) |
| 526 | } |
| 527 | |
| 528 | // Sem limite de tamanho, vamos coletar tudo e depois dividir |
| 529 | files, err := utils.ProcessDirectory(ctx, path, scanOptions) |
| 530 | if err != nil { |
| 531 | return nil, err |
| 532 | } |
| 533 | |
| 534 | if len(files) == 0 { |
| 535 | return nil, fmt.Errorf("nenhum arquivo relevante encontrado em '%s'", path) |
| 536 | } |
| 537 | |
| 538 | cli.animation.UpdateMessage(fmt.Sprintf("Analisando e dividindo projeto em chunks (%d arquivos encontrados)", len(files))) |
| 539 | |
| 540 | // Dividir os arquivos em chunks |
| 541 | var chunks []FileChunk |
| 542 | var currentChunk strings.Builder |
| 543 | filesInCurrentChunk := []utils.FileInfo{} |
| 544 | |
| 545 | // Função para finalizar o chunk atual |
| 546 | finishCurrentChunk := func() { |
| 547 | if currentChunk.Len() > 0 { |
| 548 | formattedContent := utils.FormatDirectoryContent(filesInCurrentChunk, int64(currentChunk.Len())) |
| 549 | chunks = append(chunks, FileChunk{ |
| 550 | Index: len(chunks) + 1, |
| 551 | Content: formattedContent, |
| 552 | }) |
| 553 | |
| 554 | // Resetar para o próximo chunk |
| 555 | currentChunk.Reset() |
| 556 | filesInCurrentChunk = []utils.FileInfo{} |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | // Processar cada arquivo |
| 561 | for _, file := range files { |
| 562 | // Estimar tokens do conteúdo do arquivo |
| 563 | fileTokens := tokenEstimator(file.Content) |
| 564 | |
| 565 | // Se o arquivo for maior que metade do limite, criar um chunk só para ele |
| 566 | if fileTokens > maxTokens/2 { |
| 567 | // Finalizar chunk anterior se existir |
| 568 | finishCurrentChunk() |
| 569 | |
| 570 | // Criar um chunk separado só para este arquivo grande |
| 571 | chunks = append(chunks, FileChunk{ |
| 572 | Index: len(chunks) + 1, |
| 573 | Content: utils.FormatDirectoryContent([]utils.FileInfo{file}, file.Size), |
no test coverage detected