feat: add auto-load CLAUDE.md functionality

- Introduced a new setting to enable automatic loading of CLAUDE.md files from project-specific directories.
- Updated relevant services and components to support the new setting, including the AgentService and AutoModeService.
- Added UI controls for managing the auto-load setting in the settings view.
- Enhanced SDK options to incorporate settingSources for CLAUDE.md loading.
- Updated global and project settings interfaces to include autoLoadClaudeMd property.
This commit is contained in:
Kacper
2025-12-24 22:05:50 +01:00
parent 8a0226512d
commit 07bcb6b767
12 changed files with 306 additions and 15 deletions

View File

@@ -16,6 +16,7 @@ import {
import { ProviderFactory } from '../providers/provider-factory.js';
import { createChatOptions, validateWorkingDirectory } from '../lib/sdk-options.js';
import { PathNotAllowedError } from '@automaker/platform';
import type { SettingsService } from './settings-service.js';
interface Message {
id: string;
@@ -57,11 +58,13 @@ export class AgentService {
private stateDir: string;
private metadataFile: string;
private events: EventEmitter;
private settingsService: SettingsService | null = null;
constructor(dataDir: string, events: EventEmitter) {
constructor(dataDir: string, events: EventEmitter, settingsService?: SettingsService) {
this.stateDir = path.join(dataDir, 'agent-sessions');
this.metadataFile = path.join(dataDir, 'sessions-metadata.json');
this.events = events;
this.settingsService = settingsService ?? null;
}
async initialize(): Promise<void> {
@@ -186,7 +189,11 @@ export class AgentService {
// Determine the effective working directory for context loading
const effectiveWorkDir = workingDirectory || session.workingDirectory;
// Load autoLoadClaudeMd setting (project setting takes precedence over global)
const autoLoadClaudeMd = await this.getAutoLoadClaudeMdSetting(effectiveWorkDir);
// Load project context files (CLAUDE.md, CODE_QUALITY.md, etc.)
// Note: When autoLoadClaudeMd is enabled, SDK handles CLAUDE.md loading via settingSources
const { formattedPrompt: contextFilesPrompt } = await loadContextFiles({
projectPath: effectiveWorkDir,
fsModule: secureFs as Parameters<typeof loadContextFiles>[0]['fsModule'],
@@ -205,6 +212,7 @@ export class AgentService {
sessionModel: session.model,
systemPrompt: combinedSystemPrompt,
abortController: session.abortController!,
autoLoadClaudeMd,
});
// Extract model, maxTurns, and allowedTools from SDK options
@@ -224,11 +232,12 @@ export class AgentService {
prompt: '', // Will be set below based on images
model: effectiveModel,
cwd: effectiveWorkDir,
systemPrompt: combinedSystemPrompt,
systemPrompt: sdkOptions.systemPrompt,
maxTurns: maxTurns,
allowedTools: allowedTools,
abortController: session.abortController!,
conversationHistory: conversationHistory.length > 0 ? conversationHistory : undefined,
settingSources: sdkOptions.settingSources,
sdkSessionId: session.sdkSessionId, // Pass SDK session ID for resuming
};
@@ -595,6 +604,37 @@ You have full access to the codebase and can:
- Execute tests and builds`;
}
/**
* Get the autoLoadClaudeMd setting, with project settings taking precedence over global.
* Returns false if settings service is not available.
*/
private async getAutoLoadClaudeMdSetting(projectPath: string): Promise<boolean> {
if (!this.settingsService) {
console.log('[AgentService] SettingsService not available, autoLoadClaudeMd disabled');
return false;
}
try {
// Check project settings first (takes precedence)
const projectSettings = await this.settingsService.getProjectSettings(projectPath);
if (projectSettings.autoLoadClaudeMd !== undefined) {
console.log(
`[AgentService] autoLoadClaudeMd from project settings: ${projectSettings.autoLoadClaudeMd}`
);
return projectSettings.autoLoadClaudeMd;
}
// Fall back to global settings
const globalSettings = await this.settingsService.getGlobalSettings();
const result = globalSettings.autoLoadClaudeMd ?? false;
console.log(`[AgentService] autoLoadClaudeMd from global settings: ${result}`);
return result;
} catch (error) {
console.error('[AgentService] Failed to load autoLoadClaudeMd setting:', error);
return false;
}
}
private generateId(): string {
return `msg_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
}

View File

@@ -27,6 +27,7 @@ import * as secureFs from '../lib/secure-fs.js';
import type { EventEmitter } from '../lib/events.js';
import { createAutoModeOptions, validateWorkingDirectory } from '../lib/sdk-options.js';
import { FeatureLoader } from './feature-loader.js';
import type { SettingsService } from './settings-service.js';
const execAsync = promisify(exec);
@@ -341,9 +342,11 @@ export class AutoModeService {
private autoLoopAbortController: AbortController | null = null;
private config: AutoModeConfig | null = null;
private pendingApprovals = new Map<string, PendingApproval>();
private settingsService: SettingsService | null = null;
constructor(events: EventEmitter) {
constructor(events: EventEmitter, settingsService?: SettingsService) {
this.events = events;
this.settingsService = settingsService ?? null;
}
/**
@@ -1780,11 +1783,15 @@ This mock response was generated because AUTOMAKER_MOCK_AGENT=true was set.
return;
}
// Load autoLoadClaudeMd setting (project setting takes precedence over global)
const autoLoadClaudeMd = await this.getAutoLoadClaudeMdSetting(finalProjectPath);
// Build SDK options using centralized configuration for feature implementation
const sdkOptions = createAutoModeOptions({
cwd: workDir,
model: model,
abortController,
autoLoadClaudeMd,
});
// Extract model, maxTurns, and allowedTools from SDK options
@@ -1823,7 +1830,8 @@ This mock response was generated because AUTOMAKER_MOCK_AGENT=true was set.
cwd: workDir,
allowedTools: allowedTools,
abortController,
systemPrompt: options?.systemPrompt,
systemPrompt: sdkOptions.systemPrompt,
settingSources: sdkOptions.settingSources,
};
// Execute via provider
@@ -2494,4 +2502,35 @@ Begin implementing task ${task.id} now.`;
}
});
}
/**
* Get the autoLoadClaudeMd setting, with project settings taking precedence over global.
* Returns false if settings service is not available.
*/
private async getAutoLoadClaudeMdSetting(projectPath: string): Promise<boolean> {
if (!this.settingsService) {
console.log('[AutoMode] SettingsService not available, autoLoadClaudeMd disabled');
return false;
}
try {
// Check project settings first (takes precedence)
const projectSettings = await this.settingsService.getProjectSettings(projectPath);
if (projectSettings.autoLoadClaudeMd !== undefined) {
console.log(
`[AutoMode] autoLoadClaudeMd from project settings: ${projectSettings.autoLoadClaudeMd}`
);
return projectSettings.autoLoadClaudeMd;
}
// Fall back to global settings
const globalSettings = await this.settingsService.getGlobalSettings();
const result = globalSettings.autoLoadClaudeMd ?? false;
console.log(`[AutoMode] autoLoadClaudeMd from global settings: ${result}`);
return result;
} catch (error) {
console.error('[AutoMode] Failed to load autoLoadClaudeMd setting:', error);
return false;
}
}
}