@@ -10,6 +10,9 @@ import {
1010} from "@huggingface/tasks" ;
1111import type { InferenceProvider } from "../types" ;
1212import { Template } from "@huggingface/jinja" ;
13+ import fs from "fs" ;
14+ import path from "path" ;
15+ import { existsSync as pathExists } from "node:fs" ;
1316
1417const TOOLS = [ "huggingface_hub" , "requests" , "fal_client" , "openai" ] ;
1518
@@ -26,25 +29,7 @@ interface TemplateParams {
2629}
2730
2831// Helpers to find + load templates
29- function isNode ( ) : boolean {
30- return typeof process !== "undefined" && process . versions != null && process . versions . node != null ;
31- }
32-
33- function loadNodeModules ( ) {
34- if ( ! isNode ( ) ) {
35- throw new Error ( "Inference snippet generation is only available in Node.js environment" ) ;
36- }
37- // eslint-disable-next-line @typescript-eslint/no-var-requires
38- const fs = require ( "fs" ) ;
39- // eslint-disable-next-line @typescript-eslint/no-var-requires
40- const path = require ( "path" ) ;
41- // eslint-disable-next-line @typescript-eslint/no-var-requires
42- const pathExists = require ( "fs" ) . existsSync ;
43- return { fs, path, pathExists } ;
44- }
45-
4632const rootDirFinder = ( ) : string => {
47- const { path, pathExists } = loadNodeModules ( ) ;
4833 let currentPath = path . normalize ( import . meta. url ) . replace ( "file:" , "" ) ;
4934
5035 while ( currentPath !== "/" ) {
@@ -58,17 +43,11 @@ const rootDirFinder = (): string => {
5843 return "/" ;
5944} ;
6045
61- const templatePath = ( tool : string , templateName : string ) : string => {
62- const { path } = loadNodeModules ( ) ;
63- return path . join ( rootDirFinder ( ) , "src" , "snippets" , "templates" , "python" , tool , `${ templateName } .jinja` ) ;
64- } ;
65- const hasTemplate = ( tool : string , templateName : string ) : boolean => {
66- const { pathExists } = loadNodeModules ( ) ;
67- return pathExists ( templatePath ( tool , templateName ) ) ;
68- } ;
46+ const templatePath = ( tool : string , templateName : string ) : string =>
47+ path . join ( rootDirFinder ( ) , "src" , "snippets" , "templates" , "python" , tool , `${ templateName } .jinja` ) ;
48+ const hasTemplate = ( tool : string , templateName : string ) : boolean => pathExists ( templatePath ( tool , templateName ) ) ;
6949
7050const loadTemplate = ( tool : string , templateName : string ) : ( ( data : TemplateParams ) => string ) => {
71- const { fs } = loadNodeModules ( ) ;
7251 const template = fs . readFileSync ( templatePath ( tool , templateName ) , "utf8" ) ;
7352 return ( data : TemplateParams ) => new Template ( template ) . render ( { ...data } ) ;
7453} ;
0 commit comments