@@ -7,6 +7,7 @@ import { PROVIDERS_OR_POLICIES } from "@huggingface/inference";
77import { Agent } from "@huggingface/mcp-client" ;
88import { version as packageVersion } from "../package.json" ;
99import { ServerConfigSchema } from "./lib/types" ;
10+ import { ANSI , debug , error } from "./lib/utils" ;
1011
1112const USAGE_HELP = `
1213Usage:
@@ -32,17 +33,28 @@ const FILENAME_CONFIG = "agent.json";
3233// eslint-disable-next-line @typescript-eslint/no-unused-vars
3334const 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 }
0 commit comments