forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassistant-workspace.ts
More file actions
626 lines (530 loc) · 20.2 KB
/
assistant-workspace.ts
File metadata and controls
626 lines (530 loc) · 20.2 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
import fs from 'fs';
import path from 'path';
import type { AssistantWorkspaceState, AssistantWorkspaceFiles, AssistantWorkspaceFilesV2, SearchResult } from '@/types';
import { getLocalDateString } from '@/lib/utils';
const DEFAULT_STATE: AssistantWorkspaceState = {
onboardingComplete: false,
lastCheckInDate: null,
schemaVersion: 3,
};
const STATE_DIR = '.assistant';
const STATE_FILE = 'state.json';
const MEMORY_DAILY_DIR = 'memory/daily';
// Canonical filenames — lowercase preferred, uppercase fallback
const FILE_MAP: Record<keyof AssistantWorkspaceFiles, string[]> = {
claude: ['claude.md', 'Claude.md', 'CLAUDE.md', 'AGENTS.md'],
soul: ['soul.md', 'Soul.md', 'SOUL.md'],
user: ['user.md', 'User.md', 'USER.md', 'PROFILE.md'],
memory: ['memory.md', 'Memory.md', 'MEMORY.md'],
};
// Templates for initialization
const FILE_TEMPLATES: Record<keyof AssistantWorkspaceFiles, string> = {
claude: '# Rules\n\n<!-- Assistant execution rules go here -->\n',
soul: '# Soul\n\n<!-- Assistant personality and style go here -->\n',
user: '# User Profile\n\n<!-- User preferences and information go here -->\n',
memory: '# Memory\n\n<!-- Long-term facts and notes go here -->\n',
};
const PER_FILE_LIMIT = 8000;
const HEAD_SIZE = 6000;
const TAIL_SIZE = 1800;
const TOTAL_PROMPT_LIMIT = 40000;
const DAILY_MEMORY_LIMIT = 4000;
const ROOT_DOC_LIMIT = 2000;
const RETRIEVAL_RESULT_LIMIT = 3000;
const MAX_RETRIEVAL_RESULTS = 5;
function resolveFile(dir: string, key: keyof AssistantWorkspaceFiles): { filePath: string; exists: boolean } {
for (const variant of FILE_MAP[key]) {
const filePath = path.join(dir, variant);
if (fs.existsSync(filePath)) {
return { filePath, exists: true };
}
}
return { filePath: path.join(dir, FILE_MAP[key][0]), exists: false };
}
// ==========================================
// Daily Memory
// ==========================================
export function ensureDailyDir(dir: string): string {
const dailyDir = path.join(dir, MEMORY_DAILY_DIR);
if (!fs.existsSync(dailyDir)) {
fs.mkdirSync(dailyDir, { recursive: true });
}
return dailyDir;
}
export function writeDailyMemory(dir: string, date: string, content: string): string {
const dailyDir = ensureDailyDir(dir);
const filePath = path.join(dailyDir, `${date}.md`);
fs.writeFileSync(filePath, content, 'utf-8');
return filePath;
}
export function loadDailyMemories(dir: string, count = 2): Array<{ date: string; content: string }> {
const dailyDir = path.join(dir, MEMORY_DAILY_DIR);
if (!fs.existsSync(dailyDir)) return [];
try {
const files = fs.readdirSync(dailyDir)
.filter(f => /^\d{4}-\d{2}-\d{2}\.md$/.test(f))
.sort()
.reverse()
.slice(0, count);
return files.map(f => ({
date: f.replace('.md', ''),
content: fs.readFileSync(path.join(dailyDir, f), 'utf-8'),
}));
} catch {
return [];
}
}
// ==========================================
// State Migration
// ==========================================
export function migrateStateV1ToV2(dir: string): void {
// Read state directly (not via loadState, which triggers migration recursively)
let state: AssistantWorkspaceState;
try {
const statePath = path.join(dir, STATE_DIR, STATE_FILE);
const raw = fs.readFileSync(statePath, 'utf-8');
state = JSON.parse(raw) as AssistantWorkspaceState;
} catch {
return; // No state file, nothing to migrate
}
if (state.schemaVersion >= 2) return;
// Create daily memory directory
ensureDailyDir(dir);
// Create Inbox if not exists
const inboxDir = path.join(dir, 'Inbox');
if (!fs.existsSync(inboxDir)) {
fs.mkdirSync(inboxDir, { recursive: true });
}
// Update schema version
state.schemaVersion = 2;
saveState(dir, state);
}
/**
* Migrate schema v2 → v3: normalize lastCheckInDate from UTC to local.
*
* Before v3, lastCheckInDate was written as `new Date().toISOString().slice(0, 10)`
* which is a UTC date. After v3 it's written via `getLocalDateString()`.
*
* For users east/west of UTC, the old UTC date can differ from the local date
* by ±1 day. We only rewrite the stored date if it matches today's UTC date —
* meaning the user checked in "today" under the old semantics and the value
* just needs normalizing to local. Clearly-past dates are left as-is so the
* user correctly receives their next check-in.
*
* Edge cases that the migration cannot resolve (e.g. check-in written during
* the UTC/local day-boundary mismatch window, but migration runs after UTC
* midnight) are handled by a runtime compat fallback in needsDailyCheckIn.
*/
export function migrateStateV2ToV3(dir: string): void {
let state: AssistantWorkspaceState;
try {
const statePath = path.join(dir, STATE_DIR, STATE_FILE);
const raw = fs.readFileSync(statePath, 'utf-8');
state = JSON.parse(raw) as AssistantWorkspaceState;
} catch {
return;
}
if (state.schemaVersion >= 3) return;
if (state.lastCheckInDate) {
const utcToday = new Date().toISOString().slice(0, 10);
// Only normalize if the stored UTC date is "today" — the ambiguous case
// where the user checked in today but the UTC/local date may differ.
// Past dates are left untouched so needsDailyCheckIn triggers correctly.
if (state.lastCheckInDate === utcToday) {
state.lastCheckInDate = getLocalDateString();
}
}
state.schemaVersion = 3;
saveState(dir, state);
}
// ==========================================
// Root Docs
// ==========================================
export function generateRootDocs(dir: string): string[] {
const generated: string[] = [];
// Scan top-level entries
let entries: fs.Dirent[];
try {
entries = fs.readdirSync(dir, { withFileTypes: true });
} catch {
return generated;
}
const subdirs = entries.filter(e => e.isDirectory() && !e.name.startsWith('.'));
const files = entries.filter(e => e.isFile());
const startMarker = '<!-- AI_GENERATED_START -->';
const endMarker = '<!-- AI_GENERATED_END -->';
// README.ai.md — workspace purpose + categories + structure
const dirList = subdirs.map(d => `- **${d.name}/** — directory`).join('\n');
const fileList = files
.filter(f => !f.name.startsWith('.'))
.map(f => `- ${f.name}`)
.join('\n');
const readmeContent = `${startMarker}
# Workspace Overview
## Directories
${dirList || '(none)'}
## Root Files
${fileList || '(none)'}
## Structure
This workspace contains ${subdirs.length} directories and ${files.length} root files.
${endMarker}`;
const readmePath = path.join(dir, 'README.ai.md');
writeAiDoc(readmePath, readmeContent, startMarker, endMarker);
generated.push(readmePath);
// PATH.ai.md — directory tree + role map
const treeLines = [
`${startMarker}`,
`# Workspace Path Index`,
'',
`Base: \`${dir}\``,
'',
];
for (const d of subdirs) {
treeLines.push(`- \`${d.name}/\``);
}
for (const f of files.filter(f => !f.name.startsWith('.'))) {
treeLines.push(`- \`${f.name}\``);
}
treeLines.push(endMarker);
const pathFilePath = path.join(dir, 'PATH.ai.md');
writeAiDoc(pathFilePath, treeLines.join('\n'), startMarker, endMarker);
generated.push(pathFilePath);
return generated;
}
function writeAiDoc(filePath: string, content: string, startMarker: string, endMarker: string): void {
if (fs.existsSync(filePath)) {
const existing = fs.readFileSync(filePath, 'utf-8');
const startIdx = existing.indexOf(startMarker);
const endIdx = existing.indexOf(endMarker);
if (startIdx !== -1 && endIdx !== -1) {
const updated = existing.slice(0, startIdx) + content + existing.slice(endIdx + endMarker.length);
fs.writeFileSync(filePath, updated, 'utf-8');
} else {
fs.writeFileSync(filePath, content + '\n', 'utf-8');
}
} else {
fs.writeFileSync(filePath, content + '\n', 'utf-8');
}
}
// ==========================================
// Validation & Initialization
// ==========================================
export function validateWorkspace(dir: string): {
exists: boolean;
files: Record<keyof AssistantWorkspaceFiles, { exists: boolean; path: string | null; size: number }>;
} {
const dirExists = fs.existsSync(dir);
const keys = Object.keys(FILE_MAP) as Array<keyof AssistantWorkspaceFiles>;
const files = {} as Record<keyof AssistantWorkspaceFiles, { exists: boolean; path: string | null; size: number }>;
for (const key of keys) {
if (!dirExists) {
files[key] = { exists: false, path: null, size: 0 };
continue;
}
const resolved = resolveFile(dir, key);
if (resolved.exists) {
const stat = fs.statSync(resolved.filePath);
files[key] = { exists: true, path: resolved.filePath, size: stat.size };
} else {
files[key] = { exists: false, path: null, size: 0 };
}
}
return { exists: dirExists, files };
}
export function initializeWorkspace(dir: string): string[] {
const stateDir = path.join(dir, STATE_DIR);
if (!fs.existsSync(stateDir)) {
fs.mkdirSync(stateDir, { recursive: true });
}
const created: string[] = [];
// Detect if this is an existing directory with content
let existingEntries: fs.Dirent[] = [];
try {
existingEntries = fs.readdirSync(dir, { withFileTypes: true });
} catch { /* empty */ }
const hasExistingContent = existingEntries.some(
e => !e.name.startsWith('.') && e.name !== 'Inbox' && e.name !== 'memory'
);
// Create core workspace files (only if missing)
const keys = Object.keys(FILE_MAP) as Array<keyof AssistantWorkspaceFiles>;
for (const key of keys) {
const resolved = resolveFile(dir, key);
if (!resolved.exists) {
const canonicalPath = path.join(dir, FILE_MAP[key][0]);
fs.writeFileSync(canonicalPath, FILE_TEMPLATES[key], 'utf-8');
created.push(canonicalPath);
}
}
// Create V2 directories
ensureDailyDir(dir);
const inboxDir = path.join(dir, 'Inbox');
if (!fs.existsSync(inboxDir)) {
fs.mkdirSync(inboxDir, { recursive: true });
}
// State file
const statePath = path.join(stateDir, STATE_FILE);
if (!fs.existsSync(statePath)) {
saveState(dir, { ...DEFAULT_STATE });
} else {
// Migrate existing state through all schema versions
migrateStateV1ToV2(dir);
migrateStateV2ToV3(dir);
}
// For existing directories, generate root docs and infer taxonomy
if (hasExistingContent) {
generateRootDocs(dir);
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { inferTaxonomyFromDirs, loadTaxonomy, saveTaxonomy } = require('@/lib/workspace-taxonomy');
const taxonomy = loadTaxonomy(dir);
if (taxonomy.categories.length === 0) {
const inferred = inferTaxonomyFromDirs(dir);
if (inferred.length > 0) {
taxonomy.categories = inferred;
saveTaxonomy(dir, taxonomy);
}
}
} catch {
// taxonomy module not available, skip
}
}
return created;
}
// ==========================================
// File Loading
// ==========================================
export function truncateContent(content: string, limit: number): string {
if (content.length <= limit) return content;
const headSize = Math.min(HEAD_SIZE, Math.floor(limit * 0.75));
const tailSize = Math.min(TAIL_SIZE, limit - headSize - 30);
return content.slice(0, headSize) + '\n\n[...truncated...]\n\n' + content.slice(-tailSize);
}
export function loadWorkspaceFiles(dir: string): AssistantWorkspaceFilesV2 {
const result: AssistantWorkspaceFilesV2 = {};
const keys = Object.keys(FILE_MAP) as Array<keyof AssistantWorkspaceFiles>;
for (const key of keys) {
const resolved = resolveFile(dir, key);
if (resolved.exists) {
const content = fs.readFileSync(resolved.filePath, 'utf-8');
result[key] = truncateContent(content, PER_FILE_LIMIT);
}
}
// Load daily memories (today + yesterday)
const dailyMemories = loadDailyMemories(dir, 2);
if (dailyMemories.length > 0) {
result.dailyMemories = dailyMemories.map(m =>
truncateContent(`## ${m.date}\n${m.content}`, DAILY_MEMORY_LIMIT)
);
}
// Load root docs
const readmePath = path.join(dir, 'README.ai.md');
if (fs.existsSync(readmePath)) {
result.rootReadme = truncateContent(
fs.readFileSync(readmePath, 'utf-8'),
ROOT_DOC_LIMIT
);
}
const pathFilePath = path.join(dir, 'PATH.ai.md');
if (fs.existsSync(pathFilePath)) {
result.rootPath = truncateContent(
fs.readFileSync(pathFilePath, 'utf-8'),
ROOT_DOC_LIMIT
);
}
result.rootDir = dir;
return result;
}
// ==========================================
// Budget-Aware Prompt Assembly (V2)
// ==========================================
interface PromptSection {
tag: string;
content: string;
priority: number; // lower = higher priority
maxSize: number;
}
export function assembleWorkspacePrompt(files: AssistantWorkspaceFilesV2, retrievalResults?: SearchResult[]): string {
const sections: PromptSection[] = [];
// Priority 1: Identity (claude + soul + user) — never drop claude
if (files.claude) {
sections.push({ tag: 'claude', content: files.claude, priority: 1, maxSize: PER_FILE_LIMIT });
}
if (files.soul) {
sections.push({ tag: 'soul', content: files.soul, priority: 1, maxSize: PER_FILE_LIMIT });
}
if (files.user) {
sections.push({ tag: 'user', content: files.user, priority: 1, maxSize: PER_FILE_LIMIT });
}
// Priority 2: Long-term memory
if (files.memory) {
sections.push({ tag: 'memory', content: files.memory, priority: 2, maxSize: PER_FILE_LIMIT });
}
// Priority 3: Daily memories
if (files.dailyMemories && files.dailyMemories.length > 0) {
for (let i = 0; i < files.dailyMemories.length; i++) {
sections.push({
tag: `daily-memory-${i}`,
content: files.dailyMemories[i],
priority: 3,
maxSize: DAILY_MEMORY_LIMIT,
});
}
}
// Priority 4: Root docs
if (files.rootReadme) {
sections.push({ tag: 'workspace-readme', content: files.rootReadme, priority: 4, maxSize: ROOT_DOC_LIMIT });
}
if (files.rootPath) {
sections.push({ tag: 'workspace-path', content: files.rootPath, priority: 4, maxSize: ROOT_DOC_LIMIT });
}
// Priority 5: Retrieval results
if (retrievalResults && retrievalResults.length > 0) {
const results = retrievalResults.slice(0, MAX_RETRIEVAL_RESULTS);
for (const result of results) {
const content = `**${result.path}** (${result.source}, score: ${result.score.toFixed(1)})\n${result.heading ? `### ${result.heading}\n` : ''}${result.snippet}`;
sections.push({
tag: 'retrieval-result',
content: truncateContent(content, RETRIEVAL_RESULT_LIMIT),
priority: 5,
maxSize: RETRIEVAL_RESULT_LIMIT,
});
}
}
if (sections.length === 0) return '';
// Sort by priority
sections.sort((a, b) => a.priority - b.priority);
// Budget-aware assembly
let totalSize = 0;
const included: string[] = [];
const wrapperOverhead = 50; // <assistant-workspace> tags
for (const section of sections) {
const sectionContent = truncateContent(section.content, section.maxSize);
const sectionSize = sectionContent.length + section.tag.length * 2 + 10; // tag overhead
if (totalSize + sectionSize + wrapperOverhead > TOTAL_PROMPT_LIMIT) {
// Drop low-priority content on overflow, but never drop claude.md
if (section.tag === 'claude') {
// Force include claude.md even on overflow
included.push(`<${section.tag}>\n${sectionContent}\n</${section.tag}>`);
totalSize += sectionSize;
}
// Skip other sections that don't fit
continue;
}
included.push(`<${section.tag}>\n${sectionContent}\n</${section.tag}>`);
totalSize += sectionSize;
}
if (included.length === 0) return '';
return `<assistant-workspace>\n${included.join('\n\n')}\n</assistant-workspace>`;
}
// ==========================================
// State Management
// ==========================================
export function loadState(dir: string): AssistantWorkspaceState {
try {
const statePath = path.join(dir, STATE_DIR, STATE_FILE);
const raw = fs.readFileSync(statePath, 'utf-8');
const state = JSON.parse(raw) as AssistantWorkspaceState;
// Auto-migrate on load
let migrated = false;
if (state.schemaVersion < 2) {
migrateStateV1ToV2(dir);
migrated = true;
}
if (state.schemaVersion < 3) {
migrateStateV2ToV3(dir);
migrated = true;
}
if (migrated) {
return loadState(dir); // Reload after migration
}
return state;
} catch {
return { ...DEFAULT_STATE };
}
}
export function saveState(dir: string, state: AssistantWorkspaceState): void {
const stateDir = path.join(dir, STATE_DIR);
if (!fs.existsSync(stateDir)) {
fs.mkdirSync(stateDir, { recursive: true });
}
const statePath = path.join(stateDir, STATE_FILE);
const tmpPath = statePath + '.tmp';
fs.writeFileSync(tmpPath, JSON.stringify(state, null, 2), 'utf-8');
fs.renameSync(tmpPath, statePath);
}
export function needsDailyCheckIn(state: AssistantWorkspaceState, now?: Date): boolean {
if (!state.onboardingComplete) return false;
if (state.dailyCheckInEnabled === false) return false;
const d = now ?? new Date();
const localToday = getLocalDateString(d);
if (state.lastCheckInDate === localToday) return false;
// Compat: before schema v3, lastCheckInDate was stored as a UTC date.
// If the stored value matches today's UTC date, the user checked in "today"
// under the old semantics. Accept it to avoid a spurious re-trigger.
// Once the next check-in writes a local date + v3, this path becomes inert.
const utcToday = d.toISOString().slice(0, 10);
if (state.lastCheckInDate === utcToday) return false;
return true;
}
// ==========================================
// Directory Docs (legacy — kept for backward compatibility)
// ==========================================
export function generateDirectoryDocs(dir: string): string[] {
const generated: string[] = [];
let entries: fs.Dirent[];
try {
entries = fs.readdirSync(dir, { withFileTypes: true });
} catch {
return generated;
}
const subdirs = entries.filter(e => e.isDirectory() && !e.name.startsWith('.'));
for (const subdir of subdirs) {
const subdirPath = path.join(dir, subdir.name);
let subEntries: fs.Dirent[];
try {
subEntries = fs.readdirSync(subdirPath, { withFileTypes: true });
} catch {
continue;
}
const fileList = subEntries
.map(e => `- ${e.name}${e.isDirectory() ? '/' : ''}`)
.sort()
.join('\n');
const startMarker = '<!-- AI_GENERATED_START -->';
const endMarker = '<!-- AI_GENERATED_END -->';
const generatedBlock = `${startMarker}\n# ${subdir.name}\n\n${fileList}\n${endMarker}`;
const readmePath = path.join(subdirPath, 'README.ai.md');
if (fs.existsSync(readmePath)) {
const existing = fs.readFileSync(readmePath, 'utf-8');
const startIdx = existing.indexOf(startMarker);
const endIdx = existing.indexOf(endMarker);
if (startIdx !== -1 && endIdx !== -1) {
const updated = existing.slice(0, startIdx) + generatedBlock + existing.slice(endIdx + endMarker.length);
fs.writeFileSync(readmePath, updated, 'utf-8');
} else {
fs.writeFileSync(readmePath, generatedBlock + '\n', 'utf-8');
}
} else {
fs.writeFileSync(readmePath, generatedBlock + '\n', 'utf-8');
}
generated.push(readmePath);
// Generate PATH.ai.md containing full path index
const pathContent = `${startMarker}\n# ${subdir.name} — Path Index\n\nBase: \`${subdirPath}\`\n\n${subEntries.map(e => `- \`${path.join(subdirPath, e.name)}${e.isDirectory() ? '/' : ''}\``).sort().join('\n')}\n${endMarker}`;
const pathFilePath = path.join(subdirPath, 'PATH.ai.md');
if (fs.existsSync(pathFilePath)) {
const existing = fs.readFileSync(pathFilePath, 'utf-8');
const startIdx = existing.indexOf(startMarker);
const endIdx = existing.indexOf(endMarker);
if (startIdx !== -1 && endIdx !== -1) {
const updated = existing.slice(0, startIdx) + pathContent + existing.slice(endIdx + endMarker.length);
fs.writeFileSync(pathFilePath, updated, 'utf-8');
} else {
fs.writeFileSync(pathFilePath, pathContent + '\n', 'utf-8');
}
} else {
fs.writeFileSync(pathFilePath, pathContent + '\n', 'utf-8');
}
generated.push(pathFilePath);
}
return generated;
}