CreateContext cria um novo contexto a partir de caminhos de arquivos/diretórios
(ctx context.Context, name, description string, paths []string, mode ProcessingMode, tags []string, force bool)
| 87 | |
| 88 | // CreateContext cria um novo contexto a partir de caminhos de arquivos/diretórios |
| 89 | func (m *Manager) CreateContext(ctx context.Context, name, description string, paths []string, mode ProcessingMode, tags []string, force bool) (*FileContext, error) { |
| 90 | m.mu.Lock() |
| 91 | defer m.mu.Unlock() |
| 92 | |
| 93 | // Validar nome |
| 94 | if err := m.validator.ValidateName(name); err != nil { |
| 95 | return nil, err |
| 96 | } |
| 97 | |
| 98 | // Verificar se já existe |
| 99 | if m.contextExistsByName(name) { |
| 100 | if !force { |
| 101 | return nil, fmt.Errorf("já existe um contexto com o nome '%s'. Use --force caso queira sobrescrever", name) |
| 102 | } |
| 103 | |
| 104 | // Se force=true, deletar o existente primeiro |
| 105 | for id, ctx := range m.contexts { |
| 106 | if ctx.Name == name { |
| 107 | if err := m.Storage.DeleteContext(id); err != nil { |
| 108 | return nil, fmt.Errorf("erro ao remover contexto existente: %w", err) |
| 109 | } |
| 110 | delete(m.contexts, id) |
| 111 | break |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Processar arquivos baseado no modo. Em knowledge mode, arquivos .jsonl |
| 117 | // são corpora docs-flatten e entram pelo parser nativo (um FileInfo por |
| 118 | // chunk, preservando source/título/proveniência); demais caminhos seguem |
| 119 | // pelo scanner normal, então diretórios de docs também viram knowledge. |
| 120 | knowledgeMeta := map[string]string{} |
| 121 | var files []utils.FileInfo |
| 122 | var scanPaths []string |
| 123 | if mode == ModeKnowledge { |
| 124 | for _, p := range paths { |
| 125 | if !isJSONLPath(p) { |
| 126 | scanPaths = append(scanPaths, p) |
| 127 | continue |
| 128 | } |
| 129 | expanded, expandErr := utils.ExpandPath(p) |
| 130 | if expandErr != nil { |
| 131 | expanded = p |
| 132 | } |
| 133 | kfiles, kmeta, ingestErr := ingestKnowledgeJSONL(expanded, m.logger) |
| 134 | if ingestErr != nil { |
| 135 | return nil, ingestErr |
| 136 | } |
| 137 | files = append(files, kfiles...) |
| 138 | for k, v := range kmeta { |
| 139 | knowledgeMeta[k] = v |
| 140 | } |
| 141 | } |
| 142 | } else { |
| 143 | scanPaths = paths |
| 144 | } |
| 145 | |
| 146 | scanOpts := utils.DefaultDirectoryScanOptions(m.logger) |