Skip to content

Commit f4ba887

Browse files
committed
feat: add multi-directory file resolution for agent files
When running `mdflow test.md`, now checks multiple locations in order: 1. As-is (absolute or relative path) 2. ./.mdflow/ (project agents) 3. ~/.mdflow/ (user agents) 4. PATH directories This applies to both the CLI runner and the shell alias setup script.
1 parent 64cfd0a commit f4ba887

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

src/cli-runner.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import {
2424
getImportLogger, getCurrentLogPath,
2525
} from "./logger";
2626
import { isDomainTrusted, promptForTrust, addTrustedDomain, extractDomain } from "./trust";
27-
import { dirname, resolve } from "path";
27+
import { dirname, resolve, join } from "path";
28+
import { homedir } from "os";
2829
import { input } from "@inquirer/prompts";
2930
import { exceedsLimit, StdinSizeLimitError } from "./limits";
3031
import { countTokens } from "./tokenizer";
@@ -90,6 +91,48 @@ export class CliRunner {
9091
if (logPath) this.writeStderr(` Detailed logs: ${logPath}`);
9192
}
9293

94+
/**
95+
* Resolve file path by checking multiple locations in order:
96+
* 1. As-is (absolute path or relative to cwd)
97+
* 2. Project agents: ./.mdflow/<filename>
98+
* 3. User agents: ~/.mdflow/<filename>
99+
* 4. PATH directories (for files without path separators)
100+
*/
101+
private async resolveFilePath(filePath: string): Promise<string> {
102+
// 1. Try as-is (could be absolute or relative from cwd)
103+
if (await this.env.fs.exists(filePath)) {
104+
return filePath;
105+
}
106+
107+
// Only search directories for simple filenames (no path separators)
108+
if (!filePath.includes("/")) {
109+
// 2. Try ./.mdflow/
110+
const projectPath = join(this.cwd, ".mdflow", filePath);
111+
if (await this.env.fs.exists(projectPath)) {
112+
return projectPath;
113+
}
114+
115+
// 3. Try ~/.mdflow/
116+
const userPath = join(homedir(), ".mdflow", filePath);
117+
if (await this.env.fs.exists(userPath)) {
118+
return userPath;
119+
}
120+
121+
// 4. Try $PATH directories
122+
const pathDirs = (this.processEnv.PATH || "").split(":");
123+
for (const dir of pathDirs) {
124+
if (!dir) continue;
125+
const pathFilePath = join(dir, filePath);
126+
if (await this.env.fs.exists(pathFilePath)) {
127+
return pathFilePath;
128+
}
129+
}
130+
}
131+
132+
// Not found anywhere - return original for error message
133+
return filePath;
134+
}
135+
93136
async run(argv: string[]): Promise<CliRunResult> {
94137
let logPath: string | null = null;
95138
try {
@@ -182,6 +225,9 @@ export class CliRunner {
182225
}
183226
localFilePath = remoteResult.localPath!;
184227
isRemote = true;
228+
} else {
229+
// Resolve local file path by checking multiple directories
230+
localFilePath = await this.resolveFilePath(filePath);
185231
}
186232

187233
// Signal handling

src/setup.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@ _handle_md() {
1010
local input="$1"
1111
shift
1212
13-
# Resolve input: URLs pass through, files check current dir then PATH
13+
# Resolve input: URLs pass through, files check multiple locations
14+
# Priority: 1) as-is, 2) ./.mdflow/, 3) ~/.mdflow/, 4) PATH
1415
local resolved=""
1516
if [[ "$input" =~ ^https?:// ]]; then
1617
# URL - pass through as-is
1718
resolved="$input"
1819
elif [[ -f "$input" ]]; then
20+
# Found as-is (absolute or relative path)
1921
resolved="$input"
22+
elif [[ -f ".mdflow/$input" ]]; then
23+
# Found in project .mdflow/ directory
24+
resolved=".mdflow/$input"
25+
elif [[ -f "$HOME/.mdflow/$input" ]]; then
26+
# Found in user ~/.mdflow/ directory
27+
resolved="$HOME/.mdflow/$input"
2028
else
2129
# Search PATH for the .md file
2230
local dir
@@ -29,7 +37,7 @@ _handle_md() {
2937
fi
3038
3139
if [[ -z "$resolved" ]]; then
32-
echo "File not found: $input (checked current dir and PATH)"
40+
echo "File not found: $input (checked cwd, .mdflow/, ~/.mdflow/, and PATH)"
3341
return 1
3442
fi
3543

0 commit comments

Comments
 (0)