Class GuidanceProcessor
The processor scans project files and modules selected by the configured path
matcher. For supported file types, it uses Reviewer implementations
discovered through ServiceLoader to extract mandatory guidance
instructions from source comments. If a default prompt is configured,
matching files without explicit guidance can still be processed by applying
that default prompt.
Guidance comments are identified by the special marker
GUIDANCE_TAG_NAME. Reviewers are responsible for preserving marker
comments in their original source locations while allowing the provider to
update surrounding content. Processing results are collected in
getReport() as relative file paths and provider messages.
Examples
Configurator configurator = ...;
GuidanceProcessor processor = new GuidanceProcessor(new File("."), "my-model", configurator);
processor.process(projectLayout, new File("src/main/java/App.java"), "Ensure documentation is current.");
List<Map<String, Object>> report = processor.getReport();
A supported source file may include a guidance block such as:
/*
* @guidance: Keep this class documented and ensure examples compile.
*/
public class App {
}
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String[]Fully qualified names of the built-in function-tool implementations enabled when a provider has not been configured with any tools.static final StringSpecial comment marker used to identify guidance blocks inside supported files.private static final org.slf4j.LoggerLogger used to record processor initialization and provider output.private final AtomicIntegerNumber of files for which processing has been attempted successfully.(package private) final ResourceBundleResource bundle that supplies the default system instructions and guidance rules used when no explicit instructions are configured.Provider results accumulated during this processor instance's lifetime.Reviewers indexed by normalized, dot-free lower-case file extension.Fields inherited from class org.machanism.machai.gw.processor.AIFileProcessor
CONTINUE_SPECIAL_PROMPT_COMMAND, ENABLED_TOOLS_PARAM_NAME, EXIT_SPECIAL_PROMPT_COMMAND, FILE_INCLUDED_MARKER, functionTools, LOG_OUTPUT_PREFIX, PUBLIC_PROP_GROUP_NAME -
Constructor Summary
ConstructorsConstructorDescriptionGuidanceProcessor(File rootDir, String genai, Configurator configurator) Constructs a newGuidanceProcessorfor processing files with guidance tags. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidapplyTools(String instructions, String[] prompts, ProcessProvider provider, String[] tools) Configures provider tools, expanding the"auto"shortcut to the built-in command, file, and web function-tool classes before delegating to the base processor.private StringdefaultReport(ProjectLayout projectLayout, File file, String perform) Normalizes an empty provider response and increments the processed-file counter.Returns the current base instructions used for processing.intReturns the total number of files processed.Returns the mutable list of processing results collected so far.(package private) ReviewergetReviewerForExtension(String extension) Resolves a reviewer for a given file extension after normalizing the extension to the key format used by the reviewer registry.(package private) voidLoads file reviewers via theServiceLoaderregistry, mapping each supported normalized extension to the first reviewer that declares it.protected booleanmatch(File file, ProjectLayout projectLayout) Applies path matching while preserving default-guidance behavior when no path matcher is configured.(package private) static StringnormalizeExtensionKey(String extension) Normalizes a file extension (with or without a leading dot) into a lower-case lookup key.(package private) StringUses aReviewer(based on file extension) to extract guidance.process(ProjectLayout projectLayout, File file, String guidance) Composes the final prompt, dispatches it to the configured provider, and adds the resulting message to this processor's report.private voidprocessDefaultGuidance(ProjectLayout projectLayout, File projectDir) Processes the project directory with the configured default prompt when the directory matches the active scan criteria.protected voidprocessFile(ProjectLayout projectLayout, File file) Extracts guidance for a file and, when present, performs provider processing.protected voidprocessModule(File projectDir, String module) Processes a module directory after determining whether it is relevant to the configured scan path.protected voidprocessParentFiles(ProjectLayout projectLayout) Processes files and folders under the parent project directory, excluding module directories.voidscanDocuments(File projectDir, String path) Scans the requested project location and logs the number of files processed after the scan completes.Methods inherited from class org.machanism.machai.gw.processor.AIFileProcessor
addTool, getDefaultPrompt, getDirInfoLine, getModel, getProcessInfo, input, isInteractive, parseLines, parsePath, process, processFolder, processModulesMultiThreaded, readFromFilePath, readFromHttpUrl, removeFrontMatterData, setDefaultPrompt, setInstructions, setInteractive, setModel, tryToGetFromReferenceMethods inherited from class org.machanism.machai.gw.processor.AbstractFileProcessor
addMatchingFile, getConfigurator, getExcludes, getModuleThreadTimeoutMinutes, getPath, getPathMatcher, getPatternPath, getRootDir, isModuleDir, isNonRecursive, isPathPattern, listFiles, listFiles, pathDepth, processProjectDir, scanFolder, setExcludes, setModuleThreadTimeoutMinutes, setNonRecursive, setPath, setPathMatcher, setThreads, shouldExcludePath, shouldIncludeInListFiles, shutdownExecutorMethods inherited from class org.machanism.machai.project.ProjectProcessor
getProjectLayout
-
Field Details
-
logger
private static final org.slf4j.Logger loggerLogger used to record processor initialization and provider output. -
GUIDANCE_TAG_NAME
Special comment marker used to identify guidance blocks inside supported files.A guidance block begins with this marker and contains mandatory processing instructions for the AI provider. For example, Java reviewers can extract comments that start with
/*@guidance:and pass their contents to this processor. The marker itself must remain unchanged in processed files so future runs can discover the same guidance.- See Also:
-
DEFAULT_TOOLS
Fully qualified names of the built-in function-tool implementations enabled when a provider has not been configured with any tools. -
promptBundle
Resource bundle that supplies the default system instructions and guidance rules used when no explicit instructions are configured. -
reviewerMap
Reviewers indexed by normalized, dot-free lower-case file extension. -
report
Provider results accumulated during this processor instance's lifetime. Each entry contains the relative file path and the provider message. -
processedFilesCounter
Number of files for which processing has been attempted successfully.
-
-
Constructor Details
-
GuidanceProcessor
Constructs a newGuidanceProcessorfor processing files with guidance tags.Initializes the processor with the specified root directory, GenAI model identifier, and configuration. Logs the root directory and GenAI model (if provided), and loads reviewer information for guidance processing.
- Parameters:
rootDir- the root directory to scan for filesgenai- the GenAI model identifier to use for processing (may benull)configurator- the configuration object for property resolution and runtime settings
-
-
Method Details
-
loadReviewers
void loadReviewers()Loads file reviewers via theServiceLoaderregistry, mapping each supported normalized extension to the first reviewer that declares it. -
normalizeExtensionKey
Normalizes a file extension (with or without a leading dot) into a lower-case lookup key.- Parameters:
extension- the extension to normalize (e.g.,"java"or".java")- Returns:
- normalized key, or
nullif the input is blank
-
match
Applies path matching while preserving default-guidance behavior when no path matcher is configured. In that case, all files are eligible when no default prompt exists; otherwise only the project directory is eligible.- Overrides:
matchin classAbstractFileProcessor- Parameters:
file- candidate file/directoryprojectLayout- current project layout- Returns:
truewhen the candidate should be processed
-
processModule
Processes a module directory after determining whether it is relevant to the configured scan path.When a scan directory or pattern is configured, modules are only processed when the module itself matches or contains the scan directory.
- Overrides:
processModulein classProjectProcessor- Parameters:
projectDir- parent project directorymodule- module relative path- Throws:
IOException- if scanning the module fails
-
processParentFiles
Processes files and folders under the parent project directory, excluding module directories. A matching project directory is additionally processed with the default prompt when one is configured.- Overrides:
processParentFilesin classAbstractFileProcessor- Parameters:
projectLayout- project layout whose parent files are scanned- Throws:
IOException- if listing or processing a child cannot be completed
-
processDefaultGuidance
Processes the project directory with the configured default prompt when the directory matches the active scan criteria.- Parameters:
projectLayout- project layout used for matching and processingprojectDir- project directory to process
-
processFile
Extracts guidance for a file and, when present, performs provider processing. When the file has no guidance, the configured default prompt is used instead.- Overrides:
processFilein classAbstractFileProcessor- Parameters:
projectLayout- project layoutfile- file to process- Throws:
IOException- if reading the file or provider execution fails
-
defaultReport
Normalizes an empty provider response and increments the processed-file counter.- Parameters:
projectLayout- project layout associated with the file; retained for processing-context compatibilityfile- file being counted; retained for processing-context compatibilityperform- provider response- Returns:
- the provider response, or
"OK"when it is blank
-
process
Composes the final prompt, dispatches it to the configured provider, and adds the resulting message to this processor's report.- Overrides:
processin classAIFileProcessor- Parameters:
projectLayout- project layoutfile- file currently being processedguidance- extracted guidance and/or default guidance- Returns:
- provider output, or
nullwhen the provider produces no result
-
getInstructions
Returns the current base instructions used for processing. If no explicit instructions are configured, this method returns the bundled guidance system instructions.- Overrides:
getInstructionsin classAIFileProcessor- Returns:
- the configured instruction text
-
parseFile
Uses aReviewer(based on file extension) to extract guidance.- Parameters:
projectDir- project root directoryfile- file being parsed- Returns:
- guidance text, or
nullif the file type is not supported - Throws:
IOException- if the file cannot be read
-
getReviewerForExtension
Resolves a reviewer for a given file extension after normalizing the extension to the key format used by the reviewer registry.- Parameters:
extension- file extension (with or without a dot)- Returns:
- reviewer, or
nullif none is registered for that extension
-
applyTools
protected void applyTools(String instructions, String[] prompts, ProcessProvider provider, String[] tools) Configures provider tools, expanding the"auto"shortcut to the built-in command, file, and web function-tool classes before delegating to the base processor.- Overrides:
applyToolsin classAIFileProcessor- Parameters:
instructions- provider system instructionsprompts- prompts to provide to the providerprovider- configured GenAI providertools- requested tool class names
-
scanDocuments
Scans the requested project location and logs the number of files processed after the scan completes.- Overrides:
scanDocumentsin classAIFileProcessor- Parameters:
projectDir- project directory to scanpath- optional file or directory path relative to the project directory- Throws:
IOException- if the scan cannot be completed
-
getReport
Returns the mutable list of processing results collected so far.- Returns:
- result entries containing
"file"and"message"values
-
getProcessedFiles
public int getProcessedFiles()Returns the total number of files processed.- Returns:
- the current count of processed files
-