Skip to content

Commit b7f61d3

Browse files
committed
handle local directory
1 parent b60cfed commit b7f61d3

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

packages/tiny-agents/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ touch my-agent/agent.json
5555
}
5656
```
5757

58-
`servers` is a list of MCP servers (we support Stdio, SSE, and HTTP servers).
58+
Where `servers` is a list of MCP servers (we support Stdio, SSE, and HTTP servers).
5959

6060
Optionally, you can add a `PROMPT.md` file to override the default Agent prompt.
6161

@@ -67,6 +67,9 @@ npx @huggingface/tiny-agents run ./my-agent
6767

6868
Voilà! 🔥
6969

70+
> [!NOTE]
71+
> Note: you can open a PR in the huggingface.js repo to share your agent with the community, just upload it inside the `src/agents/` directory.
72+
7073
### Advanced: Programmatic Usage
7174

7275
```typescript

packages/tiny-agents/src/cli.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22
import { dirname, join } from "node:path";
33
import { parseArgs } from "node:util";
4-
import { readFile } from "node:fs/promises";
4+
import { lstat, readFile } from "node:fs/promises";
55
import { z } from "zod";
66
import { PROVIDERS_OR_POLICIES } from "@huggingface/inference";
77
import { Agent } from "@huggingface/mcp-client";
@@ -36,11 +36,29 @@ const FILENAME_PROMPT = "PROMPT.md";
3636

3737
async function loadConfigFrom(loadFrom: string): Promise<{ configJson: string; prompt?: string }> {
3838
try {
39-
/// First try it as a local directory path, then we will try as a path inside the repo itself
39+
/// First try it as a local file path, then as a local directory, then we will try as a path inside the repo itself
4040
return {
4141
configJson: await readFile(loadFrom, { encoding: "utf8" }),
4242
};
4343
} catch {
44+
if ((await lstat(loadFrom)).isDirectory()) {
45+
/// local directory
46+
try {
47+
let prompt: string | undefined;
48+
try {
49+
prompt = await readFile(join(loadFrom, FILENAME_PROMPT), { encoding: "utf8" });
50+
} catch {
51+
debug(`PROMPT.md not found in ${loadFrom}, continuing without prompt template`);
52+
}
53+
return {
54+
configJson: await readFile(join(loadFrom, FILENAME_CONFIG), { encoding: "utf8" }),
55+
prompt,
56+
};
57+
} catch {
58+
error(`Config file not found in specified local directory.`);
59+
process.exit(1);
60+
}
61+
}
4462
const srcDir = dirname(__filename);
4563
const configDir = join(srcDir, "agents", loadFrom);
4664
try {
@@ -55,7 +73,7 @@ async function loadConfigFrom(loadFrom: string): Promise<{ configJson: string; p
5573
prompt,
5674
};
5775
} catch {
58-
error(`Config file not found! Loading from the HF Hub is not implemented yet`);
76+
error(`Config file not found in tiny-agents repo! Loading from the HF Hub is not implemented yet`);
5977
process.exit(1);
6078
}
6179
}

0 commit comments

Comments
 (0)