forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace-taxonomy.ts
More file actions
159 lines (142 loc) · 3.97 KB
/
workspace-taxonomy.ts
File metadata and controls
159 lines (142 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import fs from 'fs';
import path from 'path';
import type { TaxonomyCategory, TaxonomyFile } from '@/types';
const TAXONOMY_FILENAME = 'taxonomy.json';
const ASSISTANT_DIR = '.assistant';
export const DEFAULT_TAXONOMY: TaxonomyFile = {
version: 1,
categories: [],
evolutionRules: {
allowAutoCreateCategory: false,
allowAutoArchive: false,
requireConfirmationForMoves: true,
},
};
function getTaxonomyPath(dir: string): string {
return path.join(dir, ASSISTANT_DIR, TAXONOMY_FILENAME);
}
export function loadTaxonomy(dir: string): TaxonomyFile {
const filePath = getTaxonomyPath(dir);
try {
const content = fs.readFileSync(filePath, 'utf-8');
return JSON.parse(content) as TaxonomyFile;
} catch {
return { ...DEFAULT_TAXONOMY, categories: [] };
}
}
export function saveTaxonomy(dir: string, taxonomy: TaxonomyFile): void {
const filePath = getTaxonomyPath(dir);
const assistantDir = path.join(dir, ASSISTANT_DIR);
if (!fs.existsSync(assistantDir)) {
fs.mkdirSync(assistantDir, { recursive: true });
}
fs.writeFileSync(filePath, JSON.stringify(taxonomy, null, 2), 'utf-8');
}
const ROLE_MAP: Record<string, string> = {
notes: 'notes',
note: 'notes',
projects: 'project',
project: 'project',
journal: 'journal',
diary: 'journal',
daily: 'journal',
archive: 'archive',
archives: 'archive',
inbox: 'inbox',
templates: 'template',
template: 'template',
resources: 'resource',
assets: 'resource',
attachments: 'resource',
memory: 'memory',
};
// Names that are an exact canonical form get higher confidence
const EXACT_NAMES = new Set([
'notes',
'projects',
'journal',
'archive',
'archives',
'inbox',
'templates',
'resources',
'assets',
'attachments',
'memory',
'daily',
'diary',
]);
function inferRole(dirName: string): { role: string; confidence: number } {
const lower = dirName.toLowerCase();
const role = ROLE_MAP[lower];
if (role) {
const confidence = EXACT_NAMES.has(lower) ? 0.9 : 0.6;
return { role, confidence };
}
return { role: 'unknown', confidence: 0.3 };
}
export function inferTaxonomyFromDirs(dir: string): TaxonomyCategory[] {
let entries: fs.Dirent[];
try {
entries = fs.readdirSync(dir, { withFileTypes: true });
} catch {
return [];
}
const categories: TaxonomyCategory[] = [];
for (const entry of entries) {
if (!entry.isDirectory()) continue;
if (entry.name.startsWith('.')) continue;
const { role, confidence } = inferRole(entry.name);
categories.push({
id: entry.name.toLowerCase().replace(/[^a-z0-9-]/g, '-'),
label: entry.name,
paths: [entry.name + '/'],
role,
confidence,
source: 'learned',
description: role !== 'unknown'
? `Auto-detected ${role} directory`
: `Unknown directory: ${entry.name}`,
createdBy: 'system',
});
}
return categories;
}
export function classifyPath(
filePath: string,
taxonomy: TaxonomyFile,
): TaxonomyCategory | null {
const normalized = filePath.replace(/\\/g, '/');
let bestMatch: TaxonomyCategory | null = null;
let bestLength = 0;
for (const category of taxonomy.categories) {
for (const catPath of category.paths) {
const normalizedCatPath = catPath.replace(/\\/g, '/');
if (
normalized.startsWith(normalizedCatPath) ||
normalized.startsWith('/' + normalizedCatPath)
) {
if (normalizedCatPath.length > bestLength) {
bestLength = normalizedCatPath.length;
bestMatch = category;
}
}
}
}
return bestMatch;
}
export function suggestNewCategory(dirName: string): TaxonomyCategory {
const { role, confidence } = inferRole(dirName);
return {
id: dirName.toLowerCase().replace(/[^a-z0-9-]/g, '-'),
label: dirName,
paths: [dirName + '/'],
role,
confidence,
source: 'learned',
description: role !== 'unknown'
? `Suggested ${role} category for "${dirName}"`
: `New category for "${dirName}"`,
createdBy: 'system',
};
}