Skip to content

Commit bb2f36e

Browse files
committed
Fetch prompt.md from disk
cc @gary149
1 parent 3127f31 commit bb2f36e

File tree

2 files changed

+64
-31
lines changed

2 files changed

+64
-31
lines changed

packages/tiny-agents/src/cli.ts

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { PROVIDERS_OR_POLICIES } from "@huggingface/inference";
77
import { Agent } from "@huggingface/mcp-client";
88
import { version as packageVersion } from "../package.json";
99
import { ServerConfigSchema } from "./lib/types";
10+
import { ANSI, debug, error } from "./lib/utils";
1011

1112
const USAGE_HELP = `
1213
Usage:
@@ -32,17 +33,28 @@ const FILENAME_CONFIG = "agent.json";
3233
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3334
const FILENAME_PROMPT = "PROMPT.md";
3435

35-
async function loadConfigFrom(loadFrom: string): Promise<string> {
36+
async function loadConfigFrom(loadFrom: string): Promise<{ configJson: string; prompt?: string }> {
3637
try {
3738
/// First try it as a local directory path, then we will try as a path inside the repo itself
38-
return await readFile(loadFrom, { encoding: "utf8" });
39+
return {
40+
configJson: await readFile(loadFrom, { encoding: "utf8" }),
41+
};
3942
} catch {
4043
const srcDir = dirname(__filename);
41-
const configPath = join(srcDir, "agents", loadFrom, FILENAME_CONFIG);
44+
const configDir = join(srcDir, "agents", loadFrom);
4245
try {
43-
return await readFile(configPath, { encoding: "utf8" });
46+
let prompt: string | undefined;
47+
try {
48+
prompt = await readFile(join(configDir, FILENAME_PROMPT), { encoding: "utf8" });
49+
} catch {
50+
debug(`PROMPT.md not found in ${configDir}, continuing without prompt template`);
51+
}
52+
return {
53+
configJson: await readFile(join(configDir, FILENAME_CONFIG), { encoding: "utf8" }),
54+
prompt,
55+
};
4456
} catch {
45-
console.error(`Config file not found! Loading from the HF Hub is not implemented yet`);
57+
error(`Config file not found! Loading from the HF Hub is not implemented yet`);
4658
process.exit(1);
4759
}
4860
}
@@ -76,39 +88,41 @@ async function main() {
7688
process.exit(0);
7789
}
7890
if (positionals.length !== 2 || !isValidCommand(command)) {
79-
console.error(`You need to call run or serve, followed by an agent id (local path or Hub identifier).`);
91+
error(`You need to call run or serve, followed by an agent id (local path or Hub identifier).`);
8092
console.log(USAGE_HELP);
8193
process.exit(1);
8294
}
8395

84-
if (command === "serve") {
85-
console.error(`Serve is not implemented yet, coming soon!`);
86-
process.exit(1);
87-
} else {
88-
const configJson = await loadConfigFrom(loadFrom);
96+
const { configJson, prompt } = await loadConfigFrom(loadFrom);
8997

90-
const ConfigSchema = z.object({
91-
model: z.string(),
92-
provider: z.enum(PROVIDERS_OR_POLICIES),
93-
servers: z.array(ServerConfigSchema),
94-
});
98+
const ConfigSchema = z.object({
99+
model: z.string(),
100+
provider: z.enum(PROVIDERS_OR_POLICIES),
101+
servers: z.array(ServerConfigSchema),
102+
});
95103

96-
let config: z.infer<typeof ConfigSchema>;
97-
try {
98-
const parsedConfig = JSON.parse(configJson);
99-
config = ConfigSchema.parse(parsedConfig);
100-
} catch (error) {
101-
console.error("Invalid configuration file:", error instanceof Error ? error.message : error);
102-
process.exit(1);
103-
}
104-
const agent = new Agent({
105-
provider: config.provider,
106-
model: config.model,
107-
apiKey: process.env.HF_TOKEN ?? "",
108-
servers: config.servers,
109-
});
104+
let config: z.infer<typeof ConfigSchema>;
105+
try {
106+
const parsedConfig = JSON.parse(configJson);
107+
config = ConfigSchema.parse(parsedConfig);
108+
} catch (err) {
109+
error("Invalid configuration file:", err instanceof Error ? err.message : err);
110+
process.exit(1);
111+
}
112+
113+
const agent = new Agent({
114+
provider: config.provider,
115+
model: config.model,
116+
apiKey: process.env.HF_TOKEN ?? "",
117+
servers: config.servers,
118+
prompt,
119+
});
110120

111-
console.log(agent);
121+
if (command === "serve") {
122+
error(`Serve is not implemented yet, coming soon!`);
123+
process.exit(1);
124+
} else {
125+
console.debug(agent);
112126

113127
// TODO: hook main loop from mcp-client/cli.ts
114128
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { inspect } from "util";
2+
3+
export function debug(...args: unknown[]): void {
4+
if (process.env.DEBUG) {
5+
console.debug(inspect(args, { depth: Infinity, colors: true }));
6+
}
7+
}
8+
9+
export function error(...args: unknown[]): void {
10+
console.error(ANSI.RED + args.join("") + ANSI.RESET);
11+
}
12+
13+
export const ANSI = {
14+
BLUE: "\x1b[34m",
15+
GRAY: "\x1b[90m",
16+
GREEN: "\x1b[32m",
17+
RED: "\x1b[31m",
18+
RESET: "\x1b[0m",
19+
};

0 commit comments

Comments
 (0)