Skip to content

Commit d532a69

Browse files
committed
move to src/lib/loadConfigFrom.ts
1 parent 51d4223 commit d532a69

File tree

3 files changed

+111
-108
lines changed

3 files changed

+111
-108
lines changed

packages/tiny-agents/src/cli.ts

Lines changed: 1 addition & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
#!/usr/bin/env node
2-
import { join } from "node:path";
32
import { parseArgs } from "node:util";
4-
import { lstat, readFile } from "node:fs/promises";
53
import { z } from "zod";
6-
import { downloadFileToCacheDir, type RepoDesignation } from "@huggingface/hub";
74
import { PROVIDERS_OR_POLICIES } from "@huggingface/inference";
85
import { Agent } from "@huggingface/mcp-client";
96
import { version as packageVersion } from "../package.json";
107
import { ServerConfigSchema } from "./lib/types";
118
import { debug, error } from "./lib/utils";
129
import { mainCliLoop } from "./lib/mainCliLoop";
10+
import { loadConfigFrom } from "./lib/loadConfigFrom";
1311

1412
const USAGE_HELP = `
1513
Usage:
@@ -26,116 +24,11 @@ Flags:
2624
-v, --version Show version information
2725
`.trim();
2826

29-
interface TinyAgentConfig {
30-
configJson: string;
31-
prompt?: string;
32-
}
33-
3427
const CLI_COMMANDS = ["run", "serve"] as const;
3528
function isValidCommand(command: string): command is (typeof CLI_COMMANDS)[number] {
3629
return (CLI_COMMANDS as unknown as string[]).includes(command);
3730
}
3831

39-
const FILENAME_CONFIG = "agent.json";
40-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
41-
const FILENAME_PROMPT = "PROMPT.md";
42-
43-
const TINY_AGENTS_HUB_REPO: RepoDesignation = {
44-
name: "tiny-agents/tiny-agents",
45-
type: "dataset",
46-
};
47-
48-
async function tryLoadFromFile(filePath: string): Promise<TinyAgentConfig | undefined> {
49-
try {
50-
const configJson = await readFile(filePath, { encoding: "utf8" });
51-
return { configJson };
52-
} catch {
53-
return undefined;
54-
}
55-
}
56-
57-
async function tryLoadFromDirectory(dirPath: string): Promise<TinyAgentConfig | undefined> {
58-
const stats = await lstat(dirPath).catch(() => undefined);
59-
if (!stats?.isDirectory()) {
60-
return undefined;
61-
}
62-
63-
let prompt: string | undefined;
64-
try {
65-
prompt = await readFile(join(dirPath, FILENAME_PROMPT), { encoding: "utf8" });
66-
} catch {
67-
debug(`PROMPT.md not found in ${dirPath}, continuing without prompt template`);
68-
}
69-
70-
try {
71-
return {
72-
configJson: await readFile(join(dirPath, FILENAME_CONFIG), { encoding: "utf8" }),
73-
prompt,
74-
};
75-
} catch {
76-
error(`Config file not found in specified local directory.`);
77-
process.exit(1);
78-
}
79-
}
80-
81-
async function tryLoadFromHub(agentId: string): Promise<TinyAgentConfig | undefined> {
82-
let configJson: string;
83-
try {
84-
const configPath = await downloadFileToCacheDir({
85-
repo: TINY_AGENTS_HUB_REPO,
86-
path: `${agentId}/${FILENAME_CONFIG}`,
87-
accessToken: process.env.HF_TOKEN,
88-
});
89-
configJson = await readFile(configPath, { encoding: "utf8" });
90-
} catch {
91-
return undefined;
92-
}
93-
94-
let prompt: string | undefined;
95-
try {
96-
const promptPath = await downloadFileToCacheDir({
97-
repo: TINY_AGENTS_HUB_REPO,
98-
path: `${agentId}/${FILENAME_PROMPT}`,
99-
accessToken: process.env.HF_TOKEN,
100-
});
101-
prompt = await readFile(promptPath, { encoding: "utf8" });
102-
} catch {
103-
debug(
104-
`PROMPT.md not found in https://huggingface.co/datasets/tiny-agents/tiny-agents/tree/main/${agentId}, continuing without prompt template`
105-
);
106-
}
107-
108-
return {
109-
configJson,
110-
prompt,
111-
};
112-
}
113-
114-
async function loadConfigFrom(loadFrom: string): Promise<TinyAgentConfig> {
115-
// First try as a local file
116-
const fileConfig = await tryLoadFromFile(loadFrom);
117-
if (fileConfig) {
118-
return fileConfig;
119-
}
120-
121-
// Then try as a local directory
122-
const dirConfig = await tryLoadFromDirectory(loadFrom);
123-
if (dirConfig) {
124-
return dirConfig;
125-
}
126-
127-
// Finally try loading from repo
128-
const repoConfig = await tryLoadFromHub(loadFrom);
129-
if (repoConfig) {
130-
return repoConfig;
131-
}
132-
133-
error(
134-
`Config file not found in tiny-agents! Please make sure it exists locally or in https://huggingface.co/datasets/tiny-agents/tiny-agents.`
135-
);
136-
process.exit(1);
137-
}
138-
13932
async function main() {
14033
const {
14134
values: { help, version },
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env node
2+
import { join } from "node:path";
3+
import { lstat, readFile } from "node:fs/promises";
4+
import { downloadFileToCacheDir, type RepoDesignation } from "@huggingface/hub";
5+
import type { TinyAgentConfig } from "./types";
6+
import { debug, error } from "./utils";
7+
8+
const FILENAME_CONFIG = "agent.json";
9+
const FILENAME_PROMPT = "PROMPT.md";
10+
11+
const TINY_AGENTS_HUB_REPO: RepoDesignation = {
12+
name: "tiny-agents/tiny-agents",
13+
type: "dataset",
14+
};
15+
16+
async function tryLoadFromFile(filePath: string): Promise<TinyAgentConfig | undefined> {
17+
try {
18+
const configJson = await readFile(filePath, { encoding: "utf8" });
19+
return { configJson };
20+
} catch {
21+
return undefined;
22+
}
23+
}
24+
25+
async function tryLoadFromDirectory(dirPath: string): Promise<TinyAgentConfig | undefined> {
26+
const stats = await lstat(dirPath).catch(() => undefined);
27+
if (!stats?.isDirectory()) {
28+
return undefined;
29+
}
30+
31+
let prompt: string | undefined;
32+
try {
33+
prompt = await readFile(join(dirPath, FILENAME_PROMPT), { encoding: "utf8" });
34+
} catch {
35+
debug(`PROMPT.md not found in ${dirPath}, continuing without prompt template`);
36+
}
37+
38+
try {
39+
return {
40+
configJson: await readFile(join(dirPath, FILENAME_CONFIG), { encoding: "utf8" }),
41+
prompt,
42+
};
43+
} catch {
44+
error(`Config file not found in specified local directory.`);
45+
process.exit(1);
46+
}
47+
}
48+
49+
async function tryLoadFromHub(agentId: string): Promise<TinyAgentConfig | undefined> {
50+
let configJson: string;
51+
try {
52+
const configPath = await downloadFileToCacheDir({
53+
repo: TINY_AGENTS_HUB_REPO,
54+
path: `${agentId}/${FILENAME_CONFIG}`,
55+
accessToken: process.env.HF_TOKEN,
56+
});
57+
configJson = await readFile(configPath, { encoding: "utf8" });
58+
} catch {
59+
return undefined;
60+
}
61+
62+
let prompt: string | undefined;
63+
try {
64+
const promptPath = await downloadFileToCacheDir({
65+
repo: TINY_AGENTS_HUB_REPO,
66+
path: `${agentId}/${FILENAME_PROMPT}`,
67+
accessToken: process.env.HF_TOKEN,
68+
});
69+
prompt = await readFile(promptPath, { encoding: "utf8" });
70+
} catch {
71+
debug(
72+
`PROMPT.md not found in https://huggingface.co/datasets/tiny-agents/tiny-agents/tree/main/${agentId}, continuing without prompt template`
73+
);
74+
}
75+
76+
return {
77+
configJson,
78+
prompt,
79+
};
80+
}
81+
82+
export async function loadConfigFrom(loadFrom: string): Promise<TinyAgentConfig> {
83+
// First try as a local file
84+
const fileConfig = await tryLoadFromFile(loadFrom);
85+
if (fileConfig) {
86+
return fileConfig;
87+
}
88+
89+
// Then try as a local directory
90+
const dirConfig = await tryLoadFromDirectory(loadFrom);
91+
if (dirConfig) {
92+
return dirConfig;
93+
}
94+
95+
// Finally try loading from repo
96+
const repoConfig = await tryLoadFromHub(loadFrom);
97+
if (repoConfig) {
98+
return repoConfig;
99+
}
100+
101+
error(
102+
`Config file not found in tiny-agents! Please make sure it exists locally or in https://huggingface.co/datasets/tiny-agents/tiny-agents.`
103+
);
104+
process.exit(1);
105+
}

packages/tiny-agents/src/lib/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { z } from "zod";
22

3+
export interface TinyAgentConfig {
4+
configJson: string;
5+
prompt?: string;
6+
}
7+
38
export const ServerConfigSchema = z.discriminatedUnion("type", [
49
z.object({
510
type: z.literal("stdio"),

0 commit comments

Comments
 (0)