@justmpm/ai-tool 0.9.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,252 @@
1
+ import {
2
+ cacheSymbolsIndex,
3
+ detectFileAreas,
4
+ findSimilar,
5
+ formatOutput,
6
+ getAllCodeFiles,
7
+ getCachedSymbolsIndex,
8
+ indexProject,
9
+ isCacheValid,
10
+ isFileIgnored,
11
+ parseCommandOptions,
12
+ readConfig,
13
+ updateCacheMeta
14
+ } from "./chunk-LC7H7LGY.js";
15
+
16
+ // src/commands/describe.ts
17
+ var STOPWORDS = /* @__PURE__ */ new Set([
18
+ // PT-BR
19
+ "de",
20
+ "da",
21
+ "do",
22
+ "das",
23
+ "dos",
24
+ "para",
25
+ "com",
26
+ "em",
27
+ "uma",
28
+ "um",
29
+ "o",
30
+ "a",
31
+ "os",
32
+ "as",
33
+ "no",
34
+ "na",
35
+ "nos",
36
+ "nas",
37
+ "pelo",
38
+ "pela",
39
+ "que",
40
+ "e",
41
+ "ou",
42
+ "se",
43
+ "ao",
44
+ "aos",
45
+ // EN
46
+ "the",
47
+ "of",
48
+ "in",
49
+ "for",
50
+ "with",
51
+ "on",
52
+ "at",
53
+ "to",
54
+ "and",
55
+ "or",
56
+ "is",
57
+ "are",
58
+ "was",
59
+ "by",
60
+ "an"
61
+ ]);
62
+ function removeStopwords(words) {
63
+ const filtered = words.filter((w) => !STOPWORDS.has(w) && w.length > 1);
64
+ return filtered.length > 0 ? filtered : words;
65
+ }
66
+ function calculatePartialScore(queryWords, searchableText) {
67
+ if (queryWords.length === 0) return 1;
68
+ const fullQuery = queryWords.join(" ");
69
+ if (searchableText.includes(fullQuery)) return 0;
70
+ let found = 0;
71
+ for (const word of queryWords) {
72
+ if (searchableText.includes(word)) {
73
+ found++;
74
+ }
75
+ }
76
+ return 1 - found / queryWords.length;
77
+ }
78
+ function buildAreaFileMap(allFiles, config) {
79
+ const map = /* @__PURE__ */ new Map();
80
+ for (const filePath of allFiles) {
81
+ if (isFileIgnored(filePath, config)) continue;
82
+ const fileAreas = detectFileAreas(filePath, config);
83
+ for (const areaId of fileAreas) {
84
+ if (!map.has(areaId)) map.set(areaId, []);
85
+ map.get(areaId).push(filePath);
86
+ }
87
+ }
88
+ return map;
89
+ }
90
+ function buildSearchableText(candidate, config, areaFileMap, index) {
91
+ const metadata = `${candidate.id} ${candidate.name} ${candidate.description}`;
92
+ const keywords = config.areas[candidate.id]?.keywords?.join(" ") ?? "";
93
+ const areaFiles = areaFileMap.get(candidate.id) ?? [];
94
+ const fileNames = areaFiles.map((f) => {
95
+ const name = f.split("/").pop() ?? "";
96
+ return name.replace(/\.(tsx?|jsx?|mjs|cjs)$/, "");
97
+ }).join(" ");
98
+ const symbolNames = [];
99
+ for (const filePath of areaFiles) {
100
+ const fileData = index.files[filePath];
101
+ if (fileData?.symbols) {
102
+ for (const symbol of fileData.symbols) {
103
+ if (symbol.isExported) {
104
+ symbolNames.push(symbol.name);
105
+ }
106
+ }
107
+ }
108
+ }
109
+ return `${metadata} ${keywords} ${fileNames} ${symbolNames.join(" ")}`.toLowerCase();
110
+ }
111
+ function findAreaMatches(normalizedQuery, candidates, config, cwd) {
112
+ const allFiles = getAllCodeFiles(cwd);
113
+ let index;
114
+ if (isCacheValid(cwd)) {
115
+ const cached = getCachedSymbolsIndex(cwd);
116
+ if (cached?.files) {
117
+ index = cached;
118
+ } else {
119
+ index = indexProject(cwd);
120
+ cacheSymbolsIndex(cwd, index);
121
+ updateCacheMeta(cwd);
122
+ }
123
+ } else {
124
+ index = indexProject(cwd);
125
+ cacheSymbolsIndex(cwd, index);
126
+ updateCacheMeta(cwd);
127
+ }
128
+ const areaFileMap = buildAreaFileMap(allFiles, config);
129
+ const queryWords = removeStopwords(normalizedQuery.split(/\s+/));
130
+ const matches = [];
131
+ for (const candidate of candidates) {
132
+ const searchableText = buildSearchableText(candidate, config, areaFileMap, index);
133
+ const score = calculatePartialScore(queryWords, searchableText);
134
+ if (score < 0.6) {
135
+ const areaFiles = areaFileMap.get(candidate.id) ?? [];
136
+ matches.push({
137
+ id: candidate.id,
138
+ name: candidate.name,
139
+ description: candidate.description || "Sem descricao",
140
+ files: areaFiles,
141
+ fileCount: areaFiles.length,
142
+ score
143
+ });
144
+ }
145
+ }
146
+ return matches.sort((a, b) => a.score - b.score);
147
+ }
148
+ async function describe(query, options = {}) {
149
+ const { cwd, format } = parseCommandOptions(options);
150
+ if (!query || query.trim().length === 0) {
151
+ throw new Error("Query \xE9 obrigat\xF3ria. Exemplo: ai-tool describe 'autentica\xE7\xE3o'");
152
+ }
153
+ try {
154
+ const config = readConfig(cwd);
155
+ const normalizedQuery = query.toLowerCase().trim();
156
+ const candidates = Object.entries(config.areas).map(([id, area]) => ({
157
+ id,
158
+ name: area.name,
159
+ description: area.description || ""
160
+ }));
161
+ const matches = findAreaMatches(normalizedQuery, candidates, config, cwd);
162
+ const suggestions = [];
163
+ if (matches.length === 0) {
164
+ const similarAreaIds = findSimilar(
165
+ normalizedQuery,
166
+ candidates.map((c) => c.id),
167
+ { maxDistance: 2, limit: 3 }
168
+ );
169
+ const similarNames = findSimilar(
170
+ normalizedQuery,
171
+ candidates.map((c) => c.name),
172
+ { maxDistance: 2, limit: 3 }
173
+ );
174
+ suggestions.push(
175
+ ...similarAreaIds.map((id) => `\u2192 ai-tool describe ${id}`),
176
+ ...similarNames.map((name) => `\u2192 ai-tool describe "${name}"`)
177
+ );
178
+ }
179
+ const result = {
180
+ version: "1.0.0",
181
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
182
+ query,
183
+ areas: matches,
184
+ suggestions: suggestions.length > 0 ? suggestions : void 0
185
+ };
186
+ return formatOutput(result, format, formatDescribeText);
187
+ } catch (error) {
188
+ const message = error instanceof Error ? error.message : String(error);
189
+ throw new Error(`Erro ao executar describe: ${message}`);
190
+ }
191
+ }
192
+ function formatDescribeText(result) {
193
+ let out = "";
194
+ if (result.areas.length === 0) {
195
+ out += `\u274C Nenhuma \xE1rea encontrada para: "${result.query}"
196
+
197
+ `;
198
+ if (result.suggestions && result.suggestions.length > 0) {
199
+ out += `\u{1F4A1} Voc\xEA quis dizer?
200
+ `;
201
+ for (const suggestion of result.suggestions) {
202
+ out += ` ${suggestion}
203
+ `;
204
+ }
205
+ out += `
206
+ `;
207
+ }
208
+ out += `\u{1F4D6} Dica: Use 'ai-tool areas' para listar todas as \xE1reas dispon\xEDveis`;
209
+ return out;
210
+ }
211
+ out += `\u{1F50D} Busca: "${result.query}"
212
+
213
+ `;
214
+ for (const area of result.areas) {
215
+ out += `## ${area.name} (${area.id})
216
+ `;
217
+ out += `${area.description}
218
+ `;
219
+ out += `\u{1F4C1} ${area.fileCount} arquivo(s)
220
+
221
+ `;
222
+ if (area.files.length > 0) {
223
+ const MAX_FILES = 5;
224
+ const filesToShow = area.files.slice(0, MAX_FILES);
225
+ const remaining = area.files.length - filesToShow.length;
226
+ out += `Arquivos:
227
+ `;
228
+ for (const file of filesToShow) {
229
+ out += ` \u2022 ${file}
230
+ `;
231
+ }
232
+ if (remaining > 0) {
233
+ out += ` ... e mais ${remaining} arquivo(s)
234
+ `;
235
+ out += ` \u2192 Use 'ai-tool area ${area.id}' para ver todos
236
+ `;
237
+ }
238
+ out += "\n";
239
+ }
240
+ }
241
+ out += `\u{1F4D6} Pr\xF3ximos passos:
242
+ `;
243
+ out += ` \u2192 ai-tool area <id> - ver detalhes de uma \xE1rea
244
+ `;
245
+ out += ` \u2192 ai-tool context --area=<id> - contexto completo de uma \xE1rea
246
+ `;
247
+ return out;
248
+ }
249
+
250
+ export {
251
+ describe
252
+ };