11#!/usr/bin/env node
2- import { join } from "node:path" ;
32import { parseArgs } from "node:util" ;
4- import { lstat , readFile } from "node:fs/promises" ;
53import { z } from "zod" ;
6- import { downloadFileToCacheDir , type RepoDesignation } from "@huggingface/hub" ;
74import { PROVIDERS_OR_POLICIES } from "@huggingface/inference" ;
85import { Agent } from "@huggingface/mcp-client" ;
96import { version as packageVersion } from "../package.json" ;
107import { ServerConfigSchema } from "./lib/types" ;
118import { debug , error } from "./lib/utils" ;
129import { mainCliLoop } from "./lib/mainCliLoop" ;
10+ import { loadConfigFrom } from "./lib/loadConfigFrom" ;
1311
1412const USAGE_HELP = `
1513Usage:
@@ -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-
3427const CLI_COMMANDS = [ "run" , "serve" ] as const ;
3528function 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-
13932async function main ( ) {
14033 const {
14134 values : { help, version } ,
0 commit comments