server.cpp 177 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536
  1. #include "server.h"
  2. #include "model_manager.h"
  3. #include "generation_queue.h"
  4. #include "utils.h"
  5. #include "auth_middleware.h"
  6. #include "user_manager.h"
  7. #include <httplib.h>
  8. #include <nlohmann/json.hpp>
  9. #include <iostream>
  10. #include <sstream>
  11. #include <fstream>
  12. #include <chrono>
  13. #include <random>
  14. #include <iomanip>
  15. #include <algorithm>
  16. #include <thread>
  17. #include <filesystem>
  18. // Include stb_image for loading images (implementation is in generation_queue.cpp)
  19. #include "../stable-diffusion.cpp-src/thirdparty/stb_image.h"
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22. #include <unistd.h>
  23. #include <arpa/inet.h>
  24. using json = nlohmann::json;
  25. Server::Server(ModelManager* modelManager, GenerationQueue* generationQueue, const std::string& outputDir, const std::string& uiDir)
  26. : m_modelManager(modelManager)
  27. , m_generationQueue(generationQueue)
  28. , m_isRunning(false)
  29. , m_startupFailed(false)
  30. , m_port(8080)
  31. , m_outputDir(outputDir)
  32. , m_uiDir(uiDir)
  33. , m_userManager(nullptr)
  34. , m_authMiddleware(nullptr)
  35. {
  36. m_httpServer = std::make_unique<httplib::Server>();
  37. }
  38. Server::~Server() {
  39. stop();
  40. }
  41. bool Server::start(const std::string& host, int port) {
  42. if (m_isRunning.load()) {
  43. return false;
  44. }
  45. m_host = host;
  46. m_port = port;
  47. // Validate host and port
  48. if (host.empty() || (port < 1 || port > 65535)) {
  49. return false;
  50. }
  51. // Set up CORS headers
  52. setupCORS();
  53. // Register API endpoints
  54. registerEndpoints();
  55. // Reset startup flags
  56. m_startupFailed.store(false);
  57. // Start server in a separate thread
  58. m_serverThread = std::thread(&Server::serverThreadFunction, this, host, port);
  59. // Wait for server to actually start and bind to the port
  60. // Give more time for server to actually start and bind
  61. for (int i = 0; i < 100; i++) { // Wait up to 10 seconds
  62. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  63. // Check if startup failed early
  64. if (m_startupFailed.load()) {
  65. if (m_serverThread.joinable()) {
  66. m_serverThread.join();
  67. }
  68. return false;
  69. }
  70. if (m_isRunning.load()) {
  71. // Give it a moment more to ensure server is fully started
  72. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  73. if (m_isRunning.load()) {
  74. return true;
  75. }
  76. }
  77. }
  78. if (m_isRunning.load()) {
  79. return true;
  80. } else {
  81. if (m_serverThread.joinable()) {
  82. m_serverThread.join();
  83. }
  84. return false;
  85. }
  86. }
  87. void Server::stop() {
  88. // Use atomic check to ensure thread safety
  89. bool wasRunning = m_isRunning.exchange(false);
  90. if (!wasRunning) {
  91. return; // Already stopped
  92. }
  93. if (m_httpServer) {
  94. m_httpServer->stop();
  95. // Give the server a moment to stop the blocking listen call
  96. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  97. // If server thread is still running, try to force unblock the listen call
  98. // by making a quick connection to the server port
  99. if (m_serverThread.joinable()) {
  100. try {
  101. // Create a quick connection to interrupt the blocking listen
  102. httplib::Client client("127.0.0.1", m_port);
  103. client.set_connection_timeout(0, 500000); // 0.5 seconds
  104. client.set_read_timeout(0, 500000); // 0.5 seconds
  105. client.set_write_timeout(0, 500000); // 0.5 seconds
  106. auto res = client.Get("/api/health");
  107. // We don't care about the response, just trying to unblock
  108. } catch (...) {
  109. // Ignore any connection errors - we're just trying to unblock
  110. }
  111. }
  112. }
  113. if (m_serverThread.joinable()) {
  114. m_serverThread.join();
  115. }
  116. }
  117. bool Server::isRunning() const {
  118. return m_isRunning.load();
  119. }
  120. void Server::waitForStop() {
  121. if (m_serverThread.joinable()) {
  122. m_serverThread.join();
  123. }
  124. }
  125. void Server::registerEndpoints() {
  126. // Register authentication endpoints first (before applying middleware)
  127. registerAuthEndpoints();
  128. // Health check endpoint (public)
  129. m_httpServer->Get("/api/health", [this](const httplib::Request& req, httplib::Response& res) {
  130. handleHealthCheck(req, res);
  131. });
  132. // API status endpoint (public)
  133. m_httpServer->Get("/api/status", [this](const httplib::Request& req, httplib::Response& res) {
  134. handleApiStatus(req, res);
  135. });
  136. // Apply authentication middleware to protected endpoints
  137. auto withAuth = [this](std::function<void(const httplib::Request&, httplib::Response&)> handler) {
  138. return [this, handler](const httplib::Request& req, httplib::Response& res) {
  139. if (m_authMiddleware) {
  140. AuthContext authContext = m_authMiddleware->authenticate(req, res);
  141. if (!authContext.authenticated) {
  142. m_authMiddleware->sendAuthError(res, authContext.errorMessage, authContext.errorCode);
  143. return;
  144. }
  145. }
  146. handler(req, res);
  147. };
  148. };
  149. // Specialized generation endpoints (protected)
  150. m_httpServer->Post("/api/generate/text2img", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  151. handleText2Img(req, res);
  152. }));
  153. m_httpServer->Post("/api/generate/img2img", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  154. handleImg2Img(req, res);
  155. }));
  156. m_httpServer->Post("/api/generate/controlnet", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  157. handleControlNet(req, res);
  158. }));
  159. m_httpServer->Post("/api/generate/upscale", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  160. handleUpscale(req, res);
  161. }));
  162. m_httpServer->Post("/api/generate/inpainting", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  163. handleInpainting(req, res);
  164. }));
  165. // Utility endpoints (now protected - require authentication)
  166. m_httpServer->Get("/api/samplers", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  167. handleSamplers(req, res);
  168. }));
  169. m_httpServer->Get("/api/schedulers", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  170. handleSchedulers(req, res);
  171. }));
  172. m_httpServer->Get("/api/parameters", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  173. handleParameters(req, res);
  174. }));
  175. m_httpServer->Post("/api/validate", [this](const httplib::Request& req, httplib::Response& res) {
  176. handleValidate(req, res);
  177. });
  178. m_httpServer->Post("/api/estimate", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  179. handleEstimate(req, res);
  180. }));
  181. m_httpServer->Get("/api/config", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  182. handleConfig(req, res);
  183. }));
  184. m_httpServer->Get("/api/system", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  185. handleSystem(req, res);
  186. }));
  187. m_httpServer->Post("/api/system/restart", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  188. handleSystemRestart(req, res);
  189. }));
  190. // Models list endpoint (now protected - require authentication)
  191. m_httpServer->Get("/api/models", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  192. handleModelsList(req, res);
  193. }));
  194. // Model-specific endpoints
  195. m_httpServer->Get("/api/models/(.*)", [this](const httplib::Request& req, httplib::Response& res) {
  196. handleModelInfo(req, res);
  197. });
  198. m_httpServer->Post("/api/models/(.*)/load", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  199. handleLoadModelById(req, res);
  200. }));
  201. m_httpServer->Post("/api/models/(.*)/unload", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  202. handleUnloadModelById(req, res);
  203. }));
  204. // Model management endpoints (now protected - require authentication)
  205. m_httpServer->Get("/api/models/types", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  206. handleModelTypes(req, res);
  207. }));
  208. m_httpServer->Get("/api/models/directories", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  209. handleModelDirectories(req, res);
  210. }));
  211. m_httpServer->Post("/api/models/refresh", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  212. handleRefreshModels(req, res);
  213. }));
  214. m_httpServer->Post("/api/models/hash", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  215. handleHashModels(req, res);
  216. }));
  217. m_httpServer->Post("/api/models/convert", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  218. handleConvertModel(req, res);
  219. }));
  220. m_httpServer->Get("/api/models/stats", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  221. handleModelStats(req, res);
  222. }));
  223. m_httpServer->Post("/api/models/batch", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  224. handleBatchModels(req, res);
  225. }));
  226. // Model validation endpoints (already protected with withAuth)
  227. m_httpServer->Post("/api/models/validate", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  228. handleValidateModel(req, res);
  229. }));
  230. m_httpServer->Post("/api/models/compatible", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  231. handleCheckCompatibility(req, res);
  232. }));
  233. m_httpServer->Post("/api/models/requirements", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  234. handleModelRequirements(req, res);
  235. }));
  236. // Queue status endpoint (now protected - require authentication)
  237. m_httpServer->Get("/api/queue/status", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  238. handleQueueStatus(req, res);
  239. }));
  240. // Download job output file endpoint (must be before job status endpoint to match more specific pattern first)
  241. // Note: This endpoint is public to allow frontend to display generated images without authentication
  242. m_httpServer->Get("/api/queue/job/(.*)/output/(.*)", [this](const httplib::Request& req, httplib::Response& res) {
  243. handleDownloadOutput(req, res);
  244. });
  245. // Job status endpoint (now protected - require authentication)
  246. m_httpServer->Get("/api/queue/job/(.*)", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  247. handleJobStatus(req, res);
  248. }));
  249. // Cancel job endpoint (protected)
  250. m_httpServer->Post("/api/queue/cancel", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  251. handleCancelJob(req, res);
  252. }));
  253. // Clear queue endpoint (protected)
  254. m_httpServer->Post("/api/queue/clear", withAuth([this](const httplib::Request& req, httplib::Response& res) {
  255. handleClearQueue(req, res);
  256. }));
  257. // Serve static web UI files if uiDir is configured
  258. if (!m_uiDir.empty() && std::filesystem::exists(m_uiDir)) {
  259. std::cout << "Serving static UI files from: " << m_uiDir << " at /ui" << std::endl;
  260. // Read UI version from version.json if available
  261. std::string uiVersion = "unknown";
  262. std::string versionFilePath = m_uiDir + "/version.json";
  263. if (std::filesystem::exists(versionFilePath)) {
  264. try {
  265. std::ifstream versionFile(versionFilePath);
  266. if (versionFile.is_open()) {
  267. nlohmann::json versionData = nlohmann::json::parse(versionFile);
  268. if (versionData.contains("version")) {
  269. uiVersion = versionData["version"].get<std::string>();
  270. }
  271. versionFile.close();
  272. }
  273. } catch (const std::exception& e) {
  274. std::cerr << "Failed to read UI version: " << e.what() << std::endl;
  275. }
  276. }
  277. std::cout << "UI version: " << uiVersion << std::endl;
  278. // Serve dynamic config.js that provides runtime configuration to the web UI
  279. m_httpServer->Get("/ui/config.js", [this, uiVersion](const httplib::Request& req, httplib::Response& res) {
  280. // Generate JavaScript configuration with current server settings
  281. std::ostringstream configJs;
  282. configJs << "// Auto-generated configuration\n"
  283. << "window.__SERVER_CONFIG__ = {\n"
  284. << " apiUrl: 'http://" << m_host << ":" << m_port << "',\n"
  285. << " apiBasePath: '/api',\n"
  286. << " host: '" << m_host << "',\n"
  287. << " port: " << m_port << ",\n"
  288. << " uiVersion: '" << uiVersion << "',\n";
  289. // Add authentication method information
  290. if (m_authMiddleware) {
  291. auto authConfig = m_authMiddleware->getConfig();
  292. std::string authMethod = "none";
  293. switch (authConfig.authMethod) {
  294. case AuthMethod::UNIX:
  295. authMethod = "unix";
  296. break;
  297. case AuthMethod::JWT:
  298. authMethod = "jwt";
  299. break;
  300. default:
  301. authMethod = "none";
  302. break;
  303. }
  304. configJs << " authMethod: '" << authMethod << "',\n"
  305. << " authEnabled: " << (authConfig.authMethod != AuthMethod::NONE ? "true" : "false") << "\n";
  306. } else {
  307. configJs << " authMethod: 'none',\n"
  308. << " authEnabled: false\n";
  309. }
  310. configJs << "};\n";
  311. // No cache for config.js - always fetch fresh
  312. res.set_header("Cache-Control", "no-cache, no-store, must-revalidate");
  313. res.set_header("Pragma", "no-cache");
  314. res.set_header("Expires", "0");
  315. res.set_content(configJs.str(), "application/javascript");
  316. });
  317. // Set up file request handler for caching static assets
  318. m_httpServer->set_file_request_handler([uiVersion](const httplib::Request& req, httplib::Response& res) {
  319. // Add cache headers based on file type and version
  320. std::string path = req.path;
  321. // For versioned static assets (.js, .css, images), use long cache
  322. if (path.find("/_next/") != std::string::npos ||
  323. path.find(".js") != std::string::npos ||
  324. path.find(".css") != std::string::npos ||
  325. path.find(".png") != std::string::npos ||
  326. path.find(".jpg") != std::string::npos ||
  327. path.find(".svg") != std::string::npos ||
  328. path.find(".ico") != std::string::npos ||
  329. path.find(".woff") != std::string::npos ||
  330. path.find(".woff2") != std::string::npos ||
  331. path.find(".ttf") != std::string::npos) {
  332. // Long cache (1 year) for static assets
  333. res.set_header("Cache-Control", "public, max-age=31536000, immutable");
  334. // Add ETag based on UI version for cache validation
  335. res.set_header("ETag", "\"" + uiVersion + "\"");
  336. // Check If-None-Match for conditional requests
  337. if (req.has_header("If-None-Match")) {
  338. std::string clientETag = req.get_header_value("If-None-Match");
  339. if (clientETag == "\"" + uiVersion + "\"") {
  340. res.status = 304; // Not Modified
  341. return;
  342. }
  343. }
  344. } else if (path.find(".html") != std::string::npos || path == "/ui/" || path == "/ui") {
  345. // HTML files should revalidate but can be cached briefly
  346. res.set_header("Cache-Control", "public, max-age=0, must-revalidate");
  347. res.set_header("ETag", "\"" + uiVersion + "\"");
  348. }
  349. });
  350. // Create a handler for UI routes with authentication check
  351. auto uiHandler = [this](const httplib::Request& req, httplib::Response& res) {
  352. // Check if authentication is enabled
  353. if (m_authMiddleware) {
  354. auto authConfig = m_authMiddleware->getConfig();
  355. if (authConfig.authMethod != AuthMethod::NONE) {
  356. // Authentication is enabled, check if user is authenticated
  357. AuthContext authContext = m_authMiddleware->authenticate(req, res);
  358. // For Unix auth, we need to check if the user is authenticated
  359. // The authenticateUnix function will return a guest context for UI requests
  360. // when no Authorization header is present, but we still need to show the login page
  361. if (!authContext.authenticated) {
  362. // Check if this is a request for a static asset (JS, CSS, images)
  363. // These should be served even without authentication to allow the login page to work
  364. bool isStaticAsset = false;
  365. std::string path = req.path;
  366. if (path.find(".js") != std::string::npos ||
  367. path.find(".css") != std::string::npos ||
  368. path.find(".png") != std::string::npos ||
  369. path.find(".jpg") != std::string::npos ||
  370. path.find(".jpeg") != std::string::npos ||
  371. path.find(".svg") != std::string::npos ||
  372. path.find(".ico") != std::string::npos ||
  373. path.find("/_next/") != std::string::npos) {
  374. isStaticAsset = true;
  375. }
  376. // For static assets, allow them to be served without authentication
  377. if (isStaticAsset) {
  378. // Continue to serve the file
  379. } else {
  380. // For HTML requests, redirect to login page
  381. if (req.path.find(".html") != std::string::npos ||
  382. req.path == "/ui/" || req.path == "/ui") {
  383. // Serve the login page instead of the requested page
  384. std::string loginPagePath = m_uiDir + "/login.html";
  385. if (std::filesystem::exists(loginPagePath)) {
  386. std::ifstream loginFile(loginPagePath);
  387. if (loginFile.is_open()) {
  388. std::string content((std::istreambuf_iterator<char>(loginFile)),
  389. std::istreambuf_iterator<char>());
  390. res.set_content(content, "text/html");
  391. return;
  392. }
  393. }
  394. // If login.html doesn't exist, serve a simple login page
  395. std::string simpleLoginPage = R"(
  396. <!DOCTYPE html>
  397. <html>
  398. <head>
  399. <title>Login Required</title>
  400. <style>
  401. body { font-family: Arial, sans-serif; max-width: 500px; margin: 100px auto; padding: 20px; }
  402. .form-group { margin-bottom: 15px; }
  403. label { display: block; margin-bottom: 5px; }
  404. input { width: 100%; padding: 8px; box-sizing: border-box; }
  405. button { background-color: #007bff; color: white; padding: 10px 15px; border: none; cursor: pointer; }
  406. .error { color: red; margin-top: 10px; }
  407. </style>
  408. </head>
  409. <body>
  410. <h1>Login Required</h1>
  411. <p>Please enter your username to continue.</p>
  412. <form id="loginForm">
  413. <div class="form-group">
  414. <label for="username">Username:</label>
  415. <input type="text" id="username" name="username" required>
  416. </div>
  417. <button type="submit">Login</button>
  418. </form>
  419. <div id="error" class="error"></div>
  420. <script>
  421. document.getElementById('loginForm').addEventListener('submit', async (e) => {
  422. e.preventDefault();
  423. const username = document.getElementById('username').value;
  424. const errorDiv = document.getElementById('error');
  425. try {
  426. const response = await fetch('/api/auth/login', {
  427. method: 'POST',
  428. headers: { 'Content-Type': 'application/json' },
  429. body: JSON.stringify({ username })
  430. });
  431. if (response.ok) {
  432. const data = await response.json();
  433. localStorage.setItem('auth_token', data.token);
  434. localStorage.setItem('unix_user', username);
  435. window.location.reload();
  436. } else {
  437. const error = await response.json();
  438. errorDiv.textContent = error.message || 'Login failed';
  439. }
  440. } catch (err) {
  441. errorDiv.textContent = 'Login failed: ' + err.message;
  442. }
  443. });
  444. </script>
  445. </body>
  446. </html>
  447. )";
  448. res.set_content(simpleLoginPage, "text/html");
  449. return;
  450. } else {
  451. // For non-HTML files, return unauthorized
  452. m_authMiddleware->sendAuthError(res, "Authentication required", "AUTH_REQUIRED");
  453. return;
  454. }
  455. }
  456. }
  457. }
  458. }
  459. // If we get here, either auth is disabled or user is authenticated
  460. // Serve the requested file
  461. std::string filePath = req.path.substr(3); // Remove "/ui" prefix
  462. if (filePath.empty() || filePath == "/") {
  463. filePath = "/index.html";
  464. }
  465. std::string fullPath = m_uiDir + filePath;
  466. if (std::filesystem::exists(fullPath) && std::filesystem::is_regular_file(fullPath)) {
  467. std::ifstream file(fullPath, std::ios::binary);
  468. if (file.is_open()) {
  469. std::string content((std::istreambuf_iterator<char>(file)),
  470. std::istreambuf_iterator<char>());
  471. // Determine content type based on file extension
  472. std::string contentType = "text/plain";
  473. if (filePath.find(".html") != std::string::npos) {
  474. contentType = "text/html";
  475. } else if (filePath.find(".js") != std::string::npos) {
  476. contentType = "application/javascript";
  477. } else if (filePath.find(".css") != std::string::npos) {
  478. contentType = "text/css";
  479. } else if (filePath.find(".png") != std::string::npos) {
  480. contentType = "image/png";
  481. } else if (filePath.find(".jpg") != std::string::npos || filePath.find(".jpeg") != std::string::npos) {
  482. contentType = "image/jpeg";
  483. } else if (filePath.find(".svg") != std::string::npos) {
  484. contentType = "image/svg+xml";
  485. }
  486. res.set_content(content, contentType);
  487. } else {
  488. res.status = 404;
  489. res.set_content("File not found", "text/plain");
  490. }
  491. } else {
  492. // For SPA routing, if the file doesn't exist, serve index.html
  493. // This allows Next.js to handle client-side routing
  494. std::string indexPath = m_uiDir + "/index.html";
  495. if (std::filesystem::exists(indexPath)) {
  496. std::ifstream indexFile(indexPath, std::ios::binary);
  497. if (indexFile.is_open()) {
  498. std::string content((std::istreambuf_iterator<char>(indexFile)),
  499. std::istreambuf_iterator<char>());
  500. res.set_content(content, "text/html");
  501. } else {
  502. res.status = 404;
  503. res.set_content("File not found", "text/plain");
  504. }
  505. } else {
  506. res.status = 404;
  507. res.set_content("File not found", "text/plain");
  508. }
  509. }
  510. };
  511. // Set up UI routes with authentication
  512. m_httpServer->Get("/ui/.*", uiHandler);
  513. // Redirect /ui to /ui/ to ensure proper routing
  514. m_httpServer->Get("/ui", [](const httplib::Request& req, httplib::Response& res) {
  515. res.set_redirect("/ui/");
  516. });
  517. }
  518. }
  519. void Server::setAuthComponents(std::shared_ptr<UserManager> userManager, std::shared_ptr<AuthMiddleware> authMiddleware) {
  520. m_userManager = userManager;
  521. m_authMiddleware = authMiddleware;
  522. }
  523. void Server::registerAuthEndpoints() {
  524. // Login endpoint
  525. m_httpServer->Post("/api/auth/login", [this](const httplib::Request& req, httplib::Response& res) {
  526. handleLogin(req, res);
  527. });
  528. // Logout endpoint
  529. m_httpServer->Post("/api/auth/logout", [this](const httplib::Request& req, httplib::Response& res) {
  530. handleLogout(req, res);
  531. });
  532. // Token validation endpoint
  533. m_httpServer->Get("/api/auth/validate", [this](const httplib::Request& req, httplib::Response& res) {
  534. handleValidateToken(req, res);
  535. });
  536. // Refresh token endpoint
  537. m_httpServer->Post("/api/auth/refresh", [this](const httplib::Request& req, httplib::Response& res) {
  538. handleRefreshToken(req, res);
  539. });
  540. // Get current user endpoint
  541. m_httpServer->Get("/api/auth/me", [this](const httplib::Request& req, httplib::Response& res) {
  542. handleGetCurrentUser(req, res);
  543. });
  544. }
  545. void Server::handleLogin(const httplib::Request& req, httplib::Response& res) {
  546. std::string requestId = generateRequestId();
  547. try {
  548. if (!m_userManager || !m_authMiddleware) {
  549. sendErrorResponse(res, "Authentication system not available", 500, "AUTH_UNAVAILABLE", requestId);
  550. return;
  551. }
  552. // Parse request body
  553. json requestJson;
  554. try {
  555. requestJson = json::parse(req.body);
  556. } catch (const json::parse_error& e) {
  557. sendErrorResponse(res, std::string("Invalid JSON: ") + e.what(), 400, "JSON_PARSE_ERROR", requestId);
  558. return;
  559. }
  560. // Check if using Unix authentication
  561. if (m_authMiddleware->getConfig().authMethod == AuthMethod::UNIX) {
  562. // For Unix auth, get username and password from request body
  563. std::string username = requestJson.value("username", "");
  564. std::string password = requestJson.value("password", "");
  565. if (username.empty()) {
  566. sendErrorResponse(res, "Missing username", 400, "MISSING_USERNAME", requestId);
  567. return;
  568. }
  569. // Check if PAM is enabled - if so, password is required
  570. if (m_userManager->isPamAuthEnabled() && password.empty()) {
  571. sendErrorResponse(res, "Password is required for Unix authentication", 400, "MISSING_PASSWORD", requestId);
  572. return;
  573. }
  574. // Authenticate Unix user (with or without password depending on PAM)
  575. auto result = m_userManager->authenticateUnix(username, password);
  576. if (!result.success) {
  577. sendErrorResponse(res, result.errorMessage, 401, "UNIX_AUTH_FAILED", requestId);
  578. return;
  579. }
  580. // Generate simple token for Unix auth
  581. std::string token = "unix_token_" + std::to_string(std::chrono::duration_cast<std::chrono::seconds>(
  582. std::chrono::system_clock::now().time_since_epoch()).count()) + "_" + username;
  583. json response = {
  584. {"token", token},
  585. {"user", {
  586. {"id", result.userId},
  587. {"username", result.username},
  588. {"role", result.role},
  589. {"permissions", result.permissions}
  590. }},
  591. {"message", "Unix authentication successful"}
  592. };
  593. sendJsonResponse(res, response);
  594. return;
  595. }
  596. // For non-Unix auth, validate required fields
  597. if (!requestJson.contains("username") || !requestJson.contains("password")) {
  598. sendErrorResponse(res, "Missing username or password", 400, "MISSING_CREDENTIALS", requestId);
  599. return;
  600. }
  601. std::string username = requestJson["username"];
  602. std::string password = requestJson["password"];
  603. // Authenticate user
  604. auto result = m_userManager->authenticateUser(username, password);
  605. if (!result.success) {
  606. sendErrorResponse(res, result.errorMessage, 401, "INVALID_CREDENTIALS", requestId);
  607. return;
  608. }
  609. // Generate JWT token if using JWT auth
  610. std::string token;
  611. if (m_authMiddleware->getConfig().authMethod == AuthMethod::JWT) {
  612. // For now, create a simple token (in a real implementation, use JWT)
  613. token = "token_" + std::to_string(std::chrono::duration_cast<std::chrono::seconds>(
  614. std::chrono::system_clock::now().time_since_epoch()).count()) + "_" + username;
  615. }
  616. json response = {
  617. {"token", token},
  618. {"user", {
  619. {"id", result.userId},
  620. {"username", result.username},
  621. {"role", result.role},
  622. {"permissions", result.permissions}
  623. }},
  624. {"message", "Login successful"}
  625. };
  626. sendJsonResponse(res, response);
  627. } catch (const std::exception& e) {
  628. sendErrorResponse(res, std::string("Login failed: ") + e.what(), 500, "LOGIN_ERROR", requestId);
  629. }
  630. }
  631. void Server::handleLogout(const httplib::Request& req, httplib::Response& res) {
  632. std::string requestId = generateRequestId();
  633. try {
  634. // For now, just return success (in a real implementation, invalidate the token)
  635. json response = {
  636. {"message", "Logout successful"}
  637. };
  638. sendJsonResponse(res, response);
  639. } catch (const std::exception& e) {
  640. sendErrorResponse(res, std::string("Logout failed: ") + e.what(), 500, "LOGOUT_ERROR", requestId);
  641. }
  642. }
  643. void Server::handleValidateToken(const httplib::Request& req, httplib::Response& res) {
  644. std::string requestId = generateRequestId();
  645. try {
  646. if (!m_userManager || !m_authMiddleware) {
  647. sendErrorResponse(res, "Authentication system not available", 500, "AUTH_UNAVAILABLE", requestId);
  648. return;
  649. }
  650. // Extract token from header
  651. std::string authHeader = req.get_header_value("Authorization");
  652. if (authHeader.empty()) {
  653. sendErrorResponse(res, "Missing authorization token", 401, "MISSING_TOKEN", requestId);
  654. return;
  655. }
  656. // Simple token validation (in a real implementation, validate JWT)
  657. // For now, just check if it starts with "token_"
  658. if (authHeader.find("Bearer ") != 0) {
  659. sendErrorResponse(res, "Invalid authorization header format", 401, "INVALID_HEADER", requestId);
  660. return;
  661. }
  662. std::string token = authHeader.substr(7); // Remove "Bearer "
  663. if (token.find("token_") != 0) {
  664. sendErrorResponse(res, "Invalid token", 401, "INVALID_TOKEN", requestId);
  665. return;
  666. }
  667. // Extract username from token (simple format: token_timestamp_username)
  668. size_t last_underscore = token.find_last_of('_');
  669. if (last_underscore == std::string::npos) {
  670. sendErrorResponse(res, "Invalid token format", 401, "INVALID_TOKEN", requestId);
  671. return;
  672. }
  673. std::string username = token.substr(last_underscore + 1);
  674. // Get user info
  675. auto userInfo = m_userManager->getUserInfoByUsername(username);
  676. if (userInfo.id.empty()) {
  677. sendErrorResponse(res, "User not found", 401, "USER_NOT_FOUND", requestId);
  678. return;
  679. }
  680. json response = {
  681. {"user", {
  682. {"id", userInfo.id},
  683. {"username", userInfo.username},
  684. {"role", userInfo.role},
  685. {"permissions", userInfo.permissions}
  686. }},
  687. {"valid", true}
  688. };
  689. sendJsonResponse(res, response);
  690. } catch (const std::exception& e) {
  691. sendErrorResponse(res, std::string("Token validation failed: ") + e.what(), 500, "VALIDATION_ERROR", requestId);
  692. }
  693. }
  694. void Server::handleRefreshToken(const httplib::Request& req, httplib::Response& res) {
  695. std::string requestId = generateRequestId();
  696. try {
  697. // For now, just return a new token (in a real implementation, refresh JWT)
  698. json response = {
  699. {"token", "new_token_" + std::to_string(std::chrono::duration_cast<std::chrono::seconds>(
  700. std::chrono::system_clock::now().time_since_epoch()).count())},
  701. {"message", "Token refreshed successfully"}
  702. };
  703. sendJsonResponse(res, response);
  704. } catch (const std::exception& e) {
  705. sendErrorResponse(res, std::string("Token refresh failed: ") + e.what(), 500, "REFRESH_ERROR", requestId);
  706. }
  707. }
  708. void Server::handleGetCurrentUser(const httplib::Request& req, httplib::Response& res) {
  709. std::string requestId = generateRequestId();
  710. try {
  711. if (!m_userManager || !m_authMiddleware) {
  712. sendErrorResponse(res, "Authentication system not available", 500, "AUTH_UNAVAILABLE", requestId);
  713. return;
  714. }
  715. // Authenticate the request
  716. AuthContext authContext = m_authMiddleware->authenticate(req, res);
  717. if (!authContext.authenticated) {
  718. sendErrorResponse(res, "Authentication required", 401, "AUTH_REQUIRED", requestId);
  719. return;
  720. }
  721. json response = {
  722. {"user", {
  723. {"id", authContext.userId},
  724. {"username", authContext.username},
  725. {"role", authContext.role},
  726. {"permissions", authContext.permissions}
  727. }}
  728. };
  729. sendJsonResponse(res, response);
  730. } catch (const std::exception& e) {
  731. sendErrorResponse(res, std::string("Get current user failed: ") + e.what(), 500, "USER_ERROR", requestId);
  732. }
  733. }
  734. void Server::setupCORS() {
  735. // Use post-routing handler to set CORS headers after the response is generated
  736. // This ensures we don't duplicate headers that may be set by other handlers
  737. m_httpServer->set_post_routing_handler([](const httplib::Request& req, httplib::Response& res) {
  738. // Only add CORS headers if they haven't been set already
  739. if (!res.has_header("Access-Control-Allow-Origin")) {
  740. res.set_header("Access-Control-Allow-Origin", "*");
  741. }
  742. if (!res.has_header("Access-Control-Allow-Methods")) {
  743. res.set_header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
  744. }
  745. if (!res.has_header("Access-Control-Allow-Headers")) {
  746. res.set_header("Access-Control-Allow-Headers", "Content-Type, Authorization");
  747. }
  748. });
  749. // Handle OPTIONS requests for CORS preflight (API endpoints only)
  750. m_httpServer->Options("/api/.*", [](const httplib::Request&, httplib::Response& res) {
  751. res.set_header("Access-Control-Allow-Origin", "*");
  752. res.set_header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
  753. res.set_header("Access-Control-Allow-Headers", "Content-Type, Authorization");
  754. res.status = 200;
  755. });
  756. }
  757. void Server::handleHealthCheck(const httplib::Request& req, httplib::Response& res) {
  758. try {
  759. json response = {
  760. {"status", "healthy"},
  761. {"timestamp", std::chrono::duration_cast<std::chrono::seconds>(
  762. std::chrono::system_clock::now().time_since_epoch()).count()},
  763. {"version", "1.0.0"}
  764. };
  765. sendJsonResponse(res, response);
  766. } catch (const std::exception& e) {
  767. sendErrorResponse(res, std::string("Health check failed: ") + e.what(), 500);
  768. }
  769. }
  770. void Server::handleApiStatus(const httplib::Request& req, httplib::Response& res) {
  771. try {
  772. json response = {
  773. {"server", {
  774. {"running", m_isRunning.load()},
  775. {"host", m_host},
  776. {"port", m_port}
  777. }},
  778. {"generation_queue", {
  779. {"running", m_generationQueue ? m_generationQueue->isRunning() : false},
  780. {"queue_size", m_generationQueue ? m_generationQueue->getQueueSize() : 0},
  781. {"active_generations", m_generationQueue ? m_generationQueue->getActiveGenerations() : 0}
  782. }},
  783. {"models", {
  784. {"loaded_count", m_modelManager ? m_modelManager->getLoadedModelsCount() : 0},
  785. {"available_count", m_modelManager ? m_modelManager->getAvailableModelsCount() : 0}
  786. }}
  787. };
  788. sendJsonResponse(res, response);
  789. } catch (const std::exception& e) {
  790. sendErrorResponse(res, std::string("Status check failed: ") + e.what(), 500);
  791. }
  792. }
  793. void Server::handleModelsList(const httplib::Request& req, httplib::Response& res) {
  794. std::string requestId = generateRequestId();
  795. try {
  796. if (!m_modelManager) {
  797. sendErrorResponse(res, "Model manager not available", 500, "MODEL_MANAGER_UNAVAILABLE", requestId);
  798. return;
  799. }
  800. // Parse query parameters for enhanced filtering
  801. std::string typeFilter = req.get_param_value("type");
  802. std::string searchQuery = req.get_param_value("search");
  803. std::string sortBy = req.get_param_value("sort_by");
  804. std::string sortOrder = req.get_param_value("sort_order");
  805. std::string dateFilter = req.get_param_value("date");
  806. std::string sizeFilter = req.get_param_value("size");
  807. // Pagination parameters
  808. int page = 1;
  809. int limit = 50;
  810. try {
  811. if (!req.get_param_value("page").empty()) {
  812. page = std::stoi(req.get_param_value("page"));
  813. if (page < 1) page = 1;
  814. }
  815. if (!req.get_param_value("limit").empty()) {
  816. limit = std::stoi(req.get_param_value("limit"));
  817. if (limit < 1) limit = 1;
  818. if (limit > 200) limit = 200; // Max limit to prevent performance issues
  819. }
  820. } catch (const std::exception& e) {
  821. sendErrorResponse(res, "Invalid pagination parameters", 400, "INVALID_PAGINATION", requestId);
  822. return;
  823. }
  824. // Filter parameters
  825. bool includeLoaded = req.get_param_value("loaded") == "true";
  826. bool includeUnloaded = req.get_param_value("unloaded") == "true";
  827. bool includeMetadata = req.get_param_value("include_metadata") == "true";
  828. bool includeThumbnails = req.get_param_value("include_thumbnails") == "true";
  829. // Get all models
  830. auto allModels = m_modelManager->getAllModels();
  831. json models = json::array();
  832. // Apply filters and build response
  833. for (const auto& pair : allModels) {
  834. const auto& modelInfo = pair.second;
  835. // Apply type filter
  836. if (!typeFilter.empty()) {
  837. ModelType filterType = ModelManager::stringToModelType(typeFilter);
  838. if (modelInfo.type != filterType) continue;
  839. }
  840. // Apply loaded/unloaded filters
  841. if (includeLoaded && !modelInfo.isLoaded) continue;
  842. if (includeUnloaded && modelInfo.isLoaded) continue;
  843. // Apply search filter (case-insensitive search in name and description)
  844. if (!searchQuery.empty()) {
  845. std::string searchLower = searchQuery;
  846. std::transform(searchLower.begin(), searchLower.end(), searchLower.begin(), ::tolower);
  847. std::string nameLower = modelInfo.name;
  848. std::transform(nameLower.begin(), nameLower.end(), nameLower.begin(), ::tolower);
  849. std::string descLower = modelInfo.description;
  850. std::transform(descLower.begin(), descLower.end(), descLower.begin(), ::tolower);
  851. if (nameLower.find(searchLower) == std::string::npos &&
  852. descLower.find(searchLower) == std::string::npos) {
  853. continue;
  854. }
  855. }
  856. // Apply date filter (simplified - expects "recent", "old", or YYYY-MM-DD)
  857. if (!dateFilter.empty()) {
  858. auto now = std::filesystem::file_time_type::clock::now();
  859. auto modelTime = modelInfo.modifiedAt;
  860. auto duration = std::chrono::duration_cast<std::chrono::hours>(now - modelTime).count();
  861. if (dateFilter == "recent" && duration > 24 * 7) continue; // Older than 1 week
  862. if (dateFilter == "old" && duration < 24 * 30) continue; // Newer than 1 month
  863. }
  864. // Apply size filter (expects "small", "medium", "large", or size in MB)
  865. if (!sizeFilter.empty()) {
  866. double sizeMB = modelInfo.fileSize / (1024.0 * 1024.0);
  867. if (sizeFilter == "small" && sizeMB > 1024) continue; // > 1GB
  868. if (sizeFilter == "medium" && (sizeMB < 1024 || sizeMB > 4096)) continue; // < 1GB or > 4GB
  869. if (sizeFilter == "large" && sizeMB < 4096) continue; // < 4GB
  870. // Try to parse as specific size in MB
  871. try {
  872. double maxSizeMB = std::stod(sizeFilter);
  873. if (sizeMB > maxSizeMB) continue;
  874. } catch (...) {
  875. // Ignore if parsing fails
  876. }
  877. }
  878. // Build model JSON with only essential information
  879. json modelJson = {
  880. {"name", modelInfo.name},
  881. {"type", ModelManager::modelTypeToString(modelInfo.type)},
  882. {"file_size", modelInfo.fileSize},
  883. {"file_size_mb", modelInfo.fileSize / (1024.0 * 1024.0)},
  884. {"sha256", modelInfo.sha256.empty() ? nullptr : json(modelInfo.sha256)},
  885. {"sha256_short", (modelInfo.sha256.empty() || modelInfo.sha256.length() < 10) ? nullptr : json(modelInfo.sha256.substr(0, 10))}
  886. };
  887. // Add architecture information if available (checkpoints only)
  888. if (!modelInfo.architecture.empty()) {
  889. modelJson["architecture"] = modelInfo.architecture;
  890. modelJson["recommended_vae"] = modelInfo.recommendedVAE.empty() ? nullptr : json(modelInfo.recommendedVAE);
  891. if (modelInfo.recommendedWidth > 0) {
  892. modelJson["recommended_width"] = modelInfo.recommendedWidth;
  893. }
  894. if (modelInfo.recommendedHeight > 0) {
  895. modelJson["recommended_height"] = modelInfo.recommendedHeight;
  896. }
  897. if (modelInfo.recommendedSteps > 0) {
  898. modelJson["recommended_steps"] = modelInfo.recommendedSteps;
  899. }
  900. if (!modelInfo.recommendedSampler.empty()) {
  901. modelJson["recommended_sampler"] = modelInfo.recommendedSampler;
  902. }
  903. if (!modelInfo.requiredModels.empty()) {
  904. modelJson["required_models"] = modelInfo.requiredModels;
  905. }
  906. if (!modelInfo.missingModels.empty()) {
  907. modelJson["missing_models"] = modelInfo.missingModels;
  908. modelJson["has_missing_dependencies"] = true;
  909. } else {
  910. modelJson["has_missing_dependencies"] = false;
  911. }
  912. }
  913. models.push_back(modelJson);
  914. }
  915. // Apply sorting
  916. if (!sortBy.empty()) {
  917. std::sort(models.begin(), models.end(), [&sortBy, &sortOrder](const json& a, const json& b) {
  918. bool ascending = sortOrder != "desc";
  919. if (sortBy == "name") {
  920. return ascending ? a["name"] < b["name"] : a["name"] > b["name"];
  921. } else if (sortBy == "size") {
  922. return ascending ? a["file_size"] < b["file_size"] : a["file_size"] > b["file_size"];
  923. } else if (sortBy == "date") {
  924. return ascending ? a["last_modified"] < b["last_modified"] : a["last_modified"] > b["last_modified"];
  925. } else if (sortBy == "type") {
  926. return ascending ? a["type"] < b["type"] : a["type"] > b["type"];
  927. } else if (sortBy == "loaded") {
  928. return ascending ? a["is_loaded"] < b["is_loaded"] : a["is_loaded"] > b["is_loaded"];
  929. }
  930. return false;
  931. });
  932. }
  933. // Apply pagination
  934. int totalCount = models.size();
  935. int totalPages = (totalCount + limit - 1) / limit;
  936. int startIndex = (page - 1) * limit;
  937. int endIndex = std::min(startIndex + limit, totalCount);
  938. json paginatedModels = json::array();
  939. for (int i = startIndex; i < endIndex; ++i) {
  940. paginatedModels.push_back(models[i]);
  941. }
  942. // Build comprehensive response
  943. json response = {
  944. {"models", paginatedModels},
  945. {"pagination", {
  946. {"page", page},
  947. {"limit", limit},
  948. {"total_count", totalCount},
  949. {"total_pages", totalPages},
  950. {"has_next", page < totalPages},
  951. {"has_prev", page > 1}
  952. }},
  953. {"filters_applied", {
  954. {"type", typeFilter.empty() ? json(nullptr) : json(typeFilter)},
  955. {"search", searchQuery.empty() ? json(nullptr) : json(searchQuery)},
  956. {"date", dateFilter.empty() ? json(nullptr) : json(dateFilter)},
  957. {"size", sizeFilter.empty() ? json(nullptr) : json(sizeFilter)},
  958. {"loaded", includeLoaded ? json(true) : json(nullptr)},
  959. {"unloaded", includeUnloaded ? json(true) : json(nullptr)}
  960. }},
  961. {"sorting", {
  962. {"sort_by", sortBy.empty() ? "name" : json(sortBy)},
  963. {"sort_order", sortOrder.empty() ? "asc" : json(sortOrder)}
  964. }},
  965. {"statistics", {
  966. {"loaded_count", m_modelManager->getLoadedModelsCount()},
  967. {"available_count", m_modelManager->getAvailableModelsCount()}
  968. }},
  969. {"request_id", requestId}
  970. };
  971. sendJsonResponse(res, response);
  972. } catch (const std::exception& e) {
  973. sendErrorResponse(res, std::string("Failed to list models: ") + e.what(), 500, "MODEL_LIST_ERROR", requestId);
  974. }
  975. }
  976. void Server::handleQueueStatus(const httplib::Request& req, httplib::Response& res) {
  977. try {
  978. if (!m_generationQueue) {
  979. sendErrorResponse(res, "Generation queue not available", 500);
  980. return;
  981. }
  982. // Get detailed queue status
  983. auto jobs = m_generationQueue->getQueueStatus();
  984. // Convert jobs to JSON
  985. json jobsJson = json::array();
  986. for (const auto& job : jobs) {
  987. std::string statusStr;
  988. switch (job.status) {
  989. case GenerationStatus::QUEUED: statusStr = "queued"; break;
  990. case GenerationStatus::PROCESSING: statusStr = "processing"; break;
  991. case GenerationStatus::COMPLETED: statusStr = "completed"; break;
  992. case GenerationStatus::FAILED: statusStr = "failed"; break;
  993. }
  994. // Convert time points to timestamps
  995. auto queuedTime = std::chrono::duration_cast<std::chrono::milliseconds>(
  996. job.queuedTime.time_since_epoch()).count();
  997. auto startTime = std::chrono::duration_cast<std::chrono::milliseconds>(
  998. job.startTime.time_since_epoch()).count();
  999. auto endTime = std::chrono::duration_cast<std::chrono::milliseconds>(
  1000. job.endTime.time_since_epoch()).count();
  1001. jobsJson.push_back({
  1002. {"id", job.id},
  1003. {"status", statusStr},
  1004. {"prompt", job.prompt},
  1005. {"queued_time", queuedTime},
  1006. {"start_time", startTime > 0 ? json(startTime) : json(nullptr)},
  1007. {"end_time", endTime > 0 ? json(endTime) : json(nullptr)},
  1008. {"position", job.position},
  1009. {"progress", job.progress}
  1010. });
  1011. }
  1012. json response = {
  1013. {"queue", {
  1014. {"size", m_generationQueue->getQueueSize()},
  1015. {"active_generations", m_generationQueue->getActiveGenerations()},
  1016. {"running", m_generationQueue->isRunning()},
  1017. {"jobs", jobsJson}
  1018. }}
  1019. };
  1020. sendJsonResponse(res, response);
  1021. } catch (const std::exception& e) {
  1022. sendErrorResponse(res, std::string("Queue status check failed: ") + e.what(), 500);
  1023. }
  1024. }
  1025. void Server::handleJobStatus(const httplib::Request& req, httplib::Response& res) {
  1026. try {
  1027. if (!m_generationQueue) {
  1028. sendErrorResponse(res, "Generation queue not available", 500);
  1029. return;
  1030. }
  1031. // Extract job ID from URL path
  1032. std::string jobId = req.matches[1].str();
  1033. if (jobId.empty()) {
  1034. sendErrorResponse(res, "Missing job ID", 400);
  1035. return;
  1036. }
  1037. // Get job information
  1038. auto jobInfo = m_generationQueue->getJobInfo(jobId);
  1039. if (jobInfo.id.empty()) {
  1040. sendErrorResponse(res, "Job not found", 404);
  1041. return;
  1042. }
  1043. // Convert status to string
  1044. std::string statusStr;
  1045. switch (jobInfo.status) {
  1046. case GenerationStatus::QUEUED: statusStr = "queued"; break;
  1047. case GenerationStatus::PROCESSING: statusStr = "processing"; break;
  1048. case GenerationStatus::COMPLETED: statusStr = "completed"; break;
  1049. case GenerationStatus::FAILED: statusStr = "failed"; break;
  1050. }
  1051. // Convert time points to timestamps
  1052. auto queuedTime = std::chrono::duration_cast<std::chrono::milliseconds>(
  1053. jobInfo.queuedTime.time_since_epoch()).count();
  1054. auto startTime = std::chrono::duration_cast<std::chrono::milliseconds>(
  1055. jobInfo.startTime.time_since_epoch()).count();
  1056. auto endTime = std::chrono::duration_cast<std::chrono::milliseconds>(
  1057. jobInfo.endTime.time_since_epoch()).count();
  1058. // Create download URLs for output files
  1059. json outputUrls = json::array();
  1060. for (const auto& filePath : jobInfo.outputFiles) {
  1061. // Extract filename from full path
  1062. std::filesystem::path p(filePath);
  1063. std::string filename = p.filename().string();
  1064. // Create download URL
  1065. std::string url = "/api/queue/job/" + jobInfo.id + "/output/" + filename;
  1066. json fileInfo = {
  1067. {"filename", filename},
  1068. {"url", url},
  1069. {"path", filePath}
  1070. };
  1071. outputUrls.push_back(fileInfo);
  1072. }
  1073. json response = {
  1074. {"job", {
  1075. {"id", jobInfo.id},
  1076. {"status", statusStr},
  1077. {"prompt", jobInfo.prompt},
  1078. {"queued_time", queuedTime},
  1079. {"start_time", startTime > 0 ? json(startTime) : json(nullptr)},
  1080. {"end_time", endTime > 0 ? json(endTime) : json(nullptr)},
  1081. {"position", jobInfo.position},
  1082. {"outputs", outputUrls},
  1083. {"error_message", jobInfo.errorMessage},
  1084. {"progress", jobInfo.progress}
  1085. }}
  1086. };
  1087. sendJsonResponse(res, response);
  1088. } catch (const std::exception& e) {
  1089. sendErrorResponse(res, std::string("Job status check failed: ") + e.what(), 500);
  1090. }
  1091. }
  1092. void Server::handleCancelJob(const httplib::Request& req, httplib::Response& res) {
  1093. try {
  1094. if (!m_generationQueue) {
  1095. sendErrorResponse(res, "Generation queue not available", 500);
  1096. return;
  1097. }
  1098. // Parse JSON request body
  1099. json requestJson = json::parse(req.body);
  1100. // Validate required fields
  1101. if (!requestJson.contains("job_id") || !requestJson["job_id"].is_string()) {
  1102. sendErrorResponse(res, "Missing or invalid 'job_id' field", 400);
  1103. return;
  1104. }
  1105. std::string jobId = requestJson["job_id"];
  1106. // Try to cancel the job
  1107. bool cancelled = m_generationQueue->cancelJob(jobId);
  1108. if (cancelled) {
  1109. json response = {
  1110. {"status", "success"},
  1111. {"message", "Job cancelled successfully"},
  1112. {"job_id", jobId}
  1113. };
  1114. sendJsonResponse(res, response);
  1115. } else {
  1116. json response = {
  1117. {"status", "error"},
  1118. {"message", "Job not found or already processing"},
  1119. {"job_id", jobId}
  1120. };
  1121. sendJsonResponse(res, response, 404);
  1122. }
  1123. } catch (const json::parse_error& e) {
  1124. sendErrorResponse(res, std::string("Invalid JSON: ") + e.what(), 400);
  1125. } catch (const std::exception& e) {
  1126. sendErrorResponse(res, std::string("Job cancellation failed: ") + e.what(), 500);
  1127. }
  1128. }
  1129. void Server::handleClearQueue(const httplib::Request& req, httplib::Response& res) {
  1130. try {
  1131. if (!m_generationQueue) {
  1132. sendErrorResponse(res, "Generation queue not available", 500);
  1133. return;
  1134. }
  1135. // Clear the queue
  1136. m_generationQueue->clearQueue();
  1137. json response = {
  1138. {"status", "success"},
  1139. {"message", "Queue cleared successfully"}
  1140. };
  1141. sendJsonResponse(res, response);
  1142. } catch (const std::exception& e) {
  1143. sendErrorResponse(res, std::string("Queue clear failed: ") + e.what(), 500);
  1144. }
  1145. }
  1146. void Server::handleDownloadOutput(const httplib::Request& req, httplib::Response& res) {
  1147. try {
  1148. // Extract job ID and filename from URL path
  1149. if (req.matches.size() < 3) {
  1150. sendErrorResponse(res, "Invalid request: job ID and filename required", 400, "INVALID_REQUEST", "");
  1151. return;
  1152. }
  1153. std::string jobId = req.matches[1];
  1154. std::string filename = req.matches[2];
  1155. // Validate inputs
  1156. if (jobId.empty() || filename.empty()) {
  1157. sendErrorResponse(res, "Job ID and filename cannot be empty", 400, "INVALID_PARAMETERS", "");
  1158. return;
  1159. }
  1160. // Construct absolute file path using the same logic as when saving:
  1161. // {outputDir}/{jobId}/{filename}
  1162. std::string fullPath = std::filesystem::absolute(m_outputDir + "/" + jobId + "/" + filename).string();
  1163. // Log the request for debugging
  1164. std::cout << "Image download request: jobId=" << jobId << ", filename=" << filename
  1165. << ", fullPath=" << fullPath << std::endl;
  1166. // Check if file exists
  1167. if (!std::filesystem::exists(fullPath)) {
  1168. std::cerr << "Output file not found: " << fullPath << std::endl;
  1169. sendErrorResponse(res, "Output file not found: " + filename, 404, "FILE_NOT_FOUND", "");
  1170. return;
  1171. }
  1172. // Check file size to detect zero-byte files
  1173. auto fileSize = std::filesystem::file_size(fullPath);
  1174. if (fileSize == 0) {
  1175. std::cerr << "Output file is zero bytes: " << fullPath << std::endl;
  1176. sendErrorResponse(res, "Output file is empty (corrupted generation)", 500, "EMPTY_FILE", "");
  1177. return;
  1178. }
  1179. // Check if file is accessible
  1180. std::ifstream file(fullPath, std::ios::binary);
  1181. if (!file.is_open()) {
  1182. std::cerr << "Failed to open output file: " << fullPath << std::endl;
  1183. sendErrorResponse(res, "Output file not accessible", 500, "FILE_ACCESS_ERROR", "");
  1184. return;
  1185. }
  1186. // Read file contents
  1187. std::string fileContent;
  1188. try {
  1189. fileContent = std::string(
  1190. std::istreambuf_iterator<char>(file),
  1191. std::istreambuf_iterator<char>()
  1192. );
  1193. file.close();
  1194. } catch (const std::exception& e) {
  1195. std::cerr << "Failed to read file content: " << e.what() << std::endl;
  1196. sendErrorResponse(res, "Failed to read file content", 500, "FILE_READ_ERROR", "");
  1197. return;
  1198. }
  1199. // Verify we actually read data
  1200. if (fileContent.empty()) {
  1201. std::cerr << "File content is empty after read: " << fullPath << std::endl;
  1202. sendErrorResponse(res, "File content is empty after read", 500, "EMPTY_CONTENT", "");
  1203. return;
  1204. }
  1205. // Determine content type based on file extension
  1206. std::string contentType = "application/octet-stream";
  1207. if (Utils::endsWith(filename, ".png")) {
  1208. contentType = "image/png";
  1209. } else if (Utils::endsWith(filename, ".jpg") || Utils::endsWith(filename, ".jpeg")) {
  1210. contentType = "image/jpeg";
  1211. } else if (Utils::endsWith(filename, ".mp4")) {
  1212. contentType = "video/mp4";
  1213. } else if (Utils::endsWith(filename, ".gif")) {
  1214. contentType = "image/gif";
  1215. } else if (Utils::endsWith(filename, ".webp")) {
  1216. contentType = "image/webp";
  1217. }
  1218. // Set response headers for proper browser handling
  1219. res.set_header("Content-Type", contentType);
  1220. res.set_header("Content-Length", std::to_string(fileContent.length()));
  1221. res.set_header("Cache-Control", "public, max-age=3600"); // Cache for 1 hour
  1222. res.set_header("Access-Control-Allow-Origin", "*"); // CORS for image access
  1223. // Uncomment if you want to force download instead of inline display:
  1224. // res.set_header("Content-Disposition", "attachment; filename=\"" + filename + "\"");
  1225. // Set the content
  1226. res.set_content(fileContent, contentType);
  1227. res.status = 200;
  1228. std::cout << "Successfully served image: " << filename << " (" << fileContent.length() << " bytes)" << std::endl;
  1229. } catch (const std::exception& e) {
  1230. std::cerr << "Exception in handleDownloadOutput: " << e.what() << std::endl;
  1231. sendErrorResponse(res, std::string("Failed to download file: ") + e.what(), 500, "DOWNLOAD_ERROR", "");
  1232. }
  1233. }
  1234. void Server::sendJsonResponse(httplib::Response& res, const nlohmann::json& json, int status_code) {
  1235. res.set_header("Content-Type", "application/json");
  1236. res.status = status_code;
  1237. res.body = json.dump();
  1238. }
  1239. void Server::sendErrorResponse(httplib::Response& res, const std::string& message, int status_code,
  1240. const std::string& error_code, const std::string& request_id) {
  1241. json errorResponse = {
  1242. {"error", {
  1243. {"message", message},
  1244. {"status_code", status_code},
  1245. {"error_code", error_code},
  1246. {"request_id", request_id},
  1247. {"timestamp", std::chrono::duration_cast<std::chrono::seconds>(
  1248. std::chrono::system_clock::now().time_since_epoch()).count()}
  1249. }}
  1250. };
  1251. sendJsonResponse(res, errorResponse, status_code);
  1252. }
  1253. std::pair<bool, std::string> Server::validateGenerationParameters(const nlohmann::json& params) {
  1254. // Validate required fields
  1255. if (!params.contains("prompt") || !params["prompt"].is_string()) {
  1256. return {false, "Missing or invalid 'prompt' field"};
  1257. }
  1258. const std::string& prompt = params["prompt"];
  1259. if (prompt.empty()) {
  1260. return {false, "Prompt cannot be empty"};
  1261. }
  1262. if (prompt.length() > 10000) {
  1263. return {false, "Prompt too long (max 10000 characters)"};
  1264. }
  1265. // Validate negative prompt if present
  1266. if (params.contains("negative_prompt")) {
  1267. if (!params["negative_prompt"].is_string()) {
  1268. return {false, "Invalid 'negative_prompt' field, must be string"};
  1269. }
  1270. if (params["negative_prompt"].get<std::string>().length() > 10000) {
  1271. return {false, "Negative prompt too long (max 10000 characters)"};
  1272. }
  1273. }
  1274. // Validate width
  1275. if (params.contains("width")) {
  1276. if (!params["width"].is_number_integer()) {
  1277. return {false, "Invalid 'width' field, must be integer"};
  1278. }
  1279. int width = params["width"];
  1280. if (width < 64 || width > 2048 || width % 64 != 0) {
  1281. return {false, "Width must be between 64 and 2048 and divisible by 64"};
  1282. }
  1283. }
  1284. // Validate height
  1285. if (params.contains("height")) {
  1286. if (!params["height"].is_number_integer()) {
  1287. return {false, "Invalid 'height' field, must be integer"};
  1288. }
  1289. int height = params["height"];
  1290. if (height < 64 || height > 2048 || height % 64 != 0) {
  1291. return {false, "Height must be between 64 and 2048 and divisible by 64"};
  1292. }
  1293. }
  1294. // Validate batch count
  1295. if (params.contains("batch_count")) {
  1296. if (!params["batch_count"].is_number_integer()) {
  1297. return {false, "Invalid 'batch_count' field, must be integer"};
  1298. }
  1299. int batchCount = params["batch_count"];
  1300. if (batchCount < 1 || batchCount > 100) {
  1301. return {false, "Batch count must be between 1 and 100"};
  1302. }
  1303. }
  1304. // Validate steps
  1305. if (params.contains("steps")) {
  1306. if (!params["steps"].is_number_integer()) {
  1307. return {false, "Invalid 'steps' field, must be integer"};
  1308. }
  1309. int steps = params["steps"];
  1310. if (steps < 1 || steps > 150) {
  1311. return {false, "Steps must be between 1 and 150"};
  1312. }
  1313. }
  1314. // Validate CFG scale
  1315. if (params.contains("cfg_scale")) {
  1316. if (!params["cfg_scale"].is_number()) {
  1317. return {false, "Invalid 'cfg_scale' field, must be number"};
  1318. }
  1319. float cfgScale = params["cfg_scale"];
  1320. if (cfgScale < 1.0f || cfgScale > 30.0f) {
  1321. return {false, "CFG scale must be between 1.0 and 30.0"};
  1322. }
  1323. }
  1324. // Validate seed
  1325. if (params.contains("seed")) {
  1326. if (!params["seed"].is_string() && !params["seed"].is_number_integer()) {
  1327. return {false, "Invalid 'seed' field, must be string or integer"};
  1328. }
  1329. }
  1330. // Validate sampling method
  1331. if (params.contains("sampling_method")) {
  1332. if (!params["sampling_method"].is_string()) {
  1333. return {false, "Invalid 'sampling_method' field, must be string"};
  1334. }
  1335. std::string method = params["sampling_method"];
  1336. std::vector<std::string> validMethods = {
  1337. "euler", "euler_a", "heun", "dpm2", "dpm++2s_a", "dpm++2m",
  1338. "dpm++2mv2", "ipndm", "ipndm_v", "lcm", "ddim_trailing", "tcd", "default"
  1339. };
  1340. if (std::find(validMethods.begin(), validMethods.end(), method) == validMethods.end()) {
  1341. return {false, "Invalid sampling method"};
  1342. }
  1343. }
  1344. // Validate scheduler
  1345. if (params.contains("scheduler")) {
  1346. if (!params["scheduler"].is_string()) {
  1347. return {false, "Invalid 'scheduler' field, must be string"};
  1348. }
  1349. std::string scheduler = params["scheduler"];
  1350. std::vector<std::string> validSchedulers = {
  1351. "discrete", "karras", "exponential", "ays", "gits",
  1352. "smoothstep", "sgm_uniform", "simple", "default"
  1353. };
  1354. if (std::find(validSchedulers.begin(), validSchedulers.end(), scheduler) == validSchedulers.end()) {
  1355. return {false, "Invalid scheduler"};
  1356. }
  1357. }
  1358. // Validate strength
  1359. if (params.contains("strength")) {
  1360. if (!params["strength"].is_number()) {
  1361. return {false, "Invalid 'strength' field, must be number"};
  1362. }
  1363. float strength = params["strength"];
  1364. if (strength < 0.0f || strength > 1.0f) {
  1365. return {false, "Strength must be between 0.0 and 1.0"};
  1366. }
  1367. }
  1368. // Validate control strength
  1369. if (params.contains("control_strength")) {
  1370. if (!params["control_strength"].is_number()) {
  1371. return {false, "Invalid 'control_strength' field, must be number"};
  1372. }
  1373. float controlStrength = params["control_strength"];
  1374. if (controlStrength < 0.0f || controlStrength > 1.0f) {
  1375. return {false, "Control strength must be between 0.0 and 1.0"};
  1376. }
  1377. }
  1378. // Validate clip skip
  1379. if (params.contains("clip_skip")) {
  1380. if (!params["clip_skip"].is_number_integer()) {
  1381. return {false, "Invalid 'clip_skip' field, must be integer"};
  1382. }
  1383. int clipSkip = params["clip_skip"];
  1384. if (clipSkip < -1 || clipSkip > 12) {
  1385. return {false, "Clip skip must be between -1 and 12"};
  1386. }
  1387. }
  1388. // Validate threads
  1389. if (params.contains("threads")) {
  1390. if (!params["threads"].is_number_integer()) {
  1391. return {false, "Invalid 'threads' field, must be integer"};
  1392. }
  1393. int threads = params["threads"];
  1394. if (threads < -1 || threads > 32) {
  1395. return {false, "Threads must be between -1 (auto) and 32"};
  1396. }
  1397. }
  1398. return {true, ""};
  1399. }
  1400. SamplingMethod Server::parseSamplingMethod(const std::string& method) {
  1401. if (method == "euler") return SamplingMethod::EULER;
  1402. else if (method == "euler_a") return SamplingMethod::EULER_A;
  1403. else if (method == "heun") return SamplingMethod::HEUN;
  1404. else if (method == "dpm2") return SamplingMethod::DPM2;
  1405. else if (method == "dpm++2s_a") return SamplingMethod::DPMPP2S_A;
  1406. else if (method == "dpm++2m") return SamplingMethod::DPMPP2M;
  1407. else if (method == "dpm++2mv2") return SamplingMethod::DPMPP2MV2;
  1408. else if (method == "ipndm") return SamplingMethod::IPNDM;
  1409. else if (method == "ipndm_v") return SamplingMethod::IPNDM_V;
  1410. else if (method == "lcm") return SamplingMethod::LCM;
  1411. else if (method == "ddim_trailing") return SamplingMethod::DDIM_TRAILING;
  1412. else if (method == "tcd") return SamplingMethod::TCD;
  1413. else return SamplingMethod::DEFAULT;
  1414. }
  1415. Scheduler Server::parseScheduler(const std::string& scheduler) {
  1416. if (scheduler == "discrete") return Scheduler::DISCRETE;
  1417. else if (scheduler == "karras") return Scheduler::KARRAS;
  1418. else if (scheduler == "exponential") return Scheduler::EXPONENTIAL;
  1419. else if (scheduler == "ays") return Scheduler::AYS;
  1420. else if (scheduler == "gits") return Scheduler::GITS;
  1421. else if (scheduler == "smoothstep") return Scheduler::SMOOTHSTEP;
  1422. else if (scheduler == "sgm_uniform") return Scheduler::SGM_UNIFORM;
  1423. else if (scheduler == "simple") return Scheduler::SIMPLE;
  1424. else return Scheduler::DEFAULT;
  1425. }
  1426. std::string Server::generateRequestId() {
  1427. std::random_device rd;
  1428. std::mt19937 gen(rd());
  1429. std::uniform_int_distribution<> dis(100000, 999999);
  1430. return "req_" + std::to_string(dis(gen));
  1431. }
  1432. std::tuple<std::vector<uint8_t>, int, int, int, bool, std::string>
  1433. Server::loadImageFromInput(const std::string& input) {
  1434. std::vector<uint8_t> imageData;
  1435. int width = 0, height = 0, channels = 0;
  1436. // Auto-detect input source type
  1437. // 1. Check if input is a URL (starts with http:// or https://)
  1438. if (Utils::startsWith(input, "http://") || Utils::startsWith(input, "https://")) {
  1439. // Parse URL to extract host and path
  1440. std::string url = input;
  1441. std::string scheme, host, path;
  1442. int port = 80;
  1443. // Determine scheme and port
  1444. if (Utils::startsWith(url, "https://")) {
  1445. scheme = "https";
  1446. port = 443;
  1447. url = url.substr(8); // Remove "https://"
  1448. } else {
  1449. scheme = "http";
  1450. port = 80;
  1451. url = url.substr(7); // Remove "http://"
  1452. }
  1453. // Extract host and path
  1454. size_t slashPos = url.find('/');
  1455. if (slashPos != std::string::npos) {
  1456. host = url.substr(0, slashPos);
  1457. path = url.substr(slashPos);
  1458. } else {
  1459. host = url;
  1460. path = "/";
  1461. }
  1462. // Check for custom port
  1463. size_t colonPos = host.find(':');
  1464. if (colonPos != std::string::npos) {
  1465. try {
  1466. port = std::stoi(host.substr(colonPos + 1));
  1467. host = host.substr(0, colonPos);
  1468. } catch (...) {
  1469. return {imageData, 0, 0, 0, false, "Invalid port in URL"};
  1470. }
  1471. }
  1472. // Download image using httplib
  1473. try {
  1474. httplib::Result res;
  1475. if (scheme == "https") {
  1476. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1477. httplib::SSLClient client(host, port);
  1478. client.set_follow_location(true);
  1479. client.set_connection_timeout(30, 0); // 30 seconds
  1480. client.set_read_timeout(60, 0); // 60 seconds
  1481. res = client.Get(path.c_str());
  1482. #else
  1483. return {imageData, 0, 0, 0, false, "HTTPS not supported (OpenSSL not available)"};
  1484. #endif
  1485. } else {
  1486. httplib::Client client(host, port);
  1487. client.set_follow_location(true);
  1488. client.set_connection_timeout(30, 0); // 30 seconds
  1489. client.set_read_timeout(60, 0); // 60 seconds
  1490. res = client.Get(path.c_str());
  1491. }
  1492. if (!res) {
  1493. return {imageData, 0, 0, 0, false, "Failed to download image from URL: Connection error"};
  1494. }
  1495. if (res->status != 200) {
  1496. return {imageData, 0, 0, 0, false, "Failed to download image from URL: HTTP " + std::to_string(res->status)};
  1497. }
  1498. // Convert response body to vector
  1499. std::vector<uint8_t> downloadedData(res->body.begin(), res->body.end());
  1500. // Load image from memory
  1501. int w, h, c;
  1502. unsigned char* pixels = stbi_load_from_memory(
  1503. downloadedData.data(),
  1504. downloadedData.size(),
  1505. &w, &h, &c,
  1506. 3 // Force RGB
  1507. );
  1508. if (!pixels) {
  1509. return {imageData, 0, 0, 0, false, "Failed to decode image from URL"};
  1510. }
  1511. width = w;
  1512. height = h;
  1513. channels = 3;
  1514. size_t dataSize = width * height * channels;
  1515. imageData.resize(dataSize);
  1516. std::memcpy(imageData.data(), pixels, dataSize);
  1517. stbi_image_free(pixels);
  1518. } catch (const std::exception& e) {
  1519. return {imageData, 0, 0, 0, false, "Failed to download image from URL: " + std::string(e.what())};
  1520. }
  1521. }
  1522. // 2. Check if input is base64 encoded data URI (starts with "data:image")
  1523. else if (Utils::startsWith(input, "data:image")) {
  1524. // Extract base64 data after the comma
  1525. size_t commaPos = input.find(',');
  1526. if (commaPos == std::string::npos) {
  1527. return {imageData, 0, 0, 0, false, "Invalid data URI format"};
  1528. }
  1529. std::string base64Data = input.substr(commaPos + 1);
  1530. std::vector<uint8_t> decodedData = Utils::base64Decode(base64Data);
  1531. // Load image from memory using stb_image
  1532. int w, h, c;
  1533. unsigned char* pixels = stbi_load_from_memory(
  1534. decodedData.data(),
  1535. decodedData.size(),
  1536. &w, &h, &c,
  1537. 3 // Force RGB
  1538. );
  1539. if (!pixels) {
  1540. return {imageData, 0, 0, 0, false, "Failed to decode image from base64 data URI"};
  1541. }
  1542. width = w;
  1543. height = h;
  1544. channels = 3; // We forced RGB
  1545. // Copy pixel data
  1546. size_t dataSize = width * height * channels;
  1547. imageData.resize(dataSize);
  1548. std::memcpy(imageData.data(), pixels, dataSize);
  1549. stbi_image_free(pixels);
  1550. }
  1551. // 3. Check if input is raw base64 (long string without slashes, likely base64)
  1552. else if (input.length() > 100 && input.find('/') == std::string::npos && input.find('.') == std::string::npos) {
  1553. // Likely raw base64 without data URI prefix
  1554. std::vector<uint8_t> decodedData = Utils::base64Decode(input);
  1555. int w, h, c;
  1556. unsigned char* pixels = stbi_load_from_memory(
  1557. decodedData.data(),
  1558. decodedData.size(),
  1559. &w, &h, &c,
  1560. 3 // Force RGB
  1561. );
  1562. if (!pixels) {
  1563. return {imageData, 0, 0, 0, false, "Failed to decode image from base64"};
  1564. }
  1565. width = w;
  1566. height = h;
  1567. channels = 3;
  1568. size_t dataSize = width * height * channels;
  1569. imageData.resize(dataSize);
  1570. std::memcpy(imageData.data(), pixels, dataSize);
  1571. stbi_image_free(pixels);
  1572. }
  1573. // 4. Treat as local file path
  1574. else {
  1575. int w, h, c;
  1576. unsigned char* pixels = stbi_load(input.c_str(), &w, &h, &c, 3);
  1577. if (!pixels) {
  1578. return {imageData, 0, 0, 0, false, "Failed to load image from file: " + input};
  1579. }
  1580. width = w;
  1581. height = h;
  1582. channels = 3;
  1583. size_t dataSize = width * height * channels;
  1584. imageData.resize(dataSize);
  1585. std::memcpy(imageData.data(), pixels, dataSize);
  1586. stbi_image_free(pixels);
  1587. }
  1588. return {imageData, width, height, channels, true, ""};
  1589. }
  1590. std::string Server::samplingMethodToString(SamplingMethod method) {
  1591. switch (method) {
  1592. case SamplingMethod::EULER: return "euler";
  1593. case SamplingMethod::EULER_A: return "euler_a";
  1594. case SamplingMethod::HEUN: return "heun";
  1595. case SamplingMethod::DPM2: return "dpm2";
  1596. case SamplingMethod::DPMPP2S_A: return "dpm++2s_a";
  1597. case SamplingMethod::DPMPP2M: return "dpm++2m";
  1598. case SamplingMethod::DPMPP2MV2: return "dpm++2mv2";
  1599. case SamplingMethod::IPNDM: return "ipndm";
  1600. case SamplingMethod::IPNDM_V: return "ipndm_v";
  1601. case SamplingMethod::LCM: return "lcm";
  1602. case SamplingMethod::DDIM_TRAILING: return "ddim_trailing";
  1603. case SamplingMethod::TCD: return "tcd";
  1604. default: return "default";
  1605. }
  1606. }
  1607. std::string Server::schedulerToString(Scheduler scheduler) {
  1608. switch (scheduler) {
  1609. case Scheduler::DISCRETE: return "discrete";
  1610. case Scheduler::KARRAS: return "karras";
  1611. case Scheduler::EXPONENTIAL: return "exponential";
  1612. case Scheduler::AYS: return "ays";
  1613. case Scheduler::GITS: return "gits";
  1614. case Scheduler::SMOOTHSTEP: return "smoothstep";
  1615. case Scheduler::SGM_UNIFORM: return "sgm_uniform";
  1616. case Scheduler::SIMPLE: return "simple";
  1617. default: return "default";
  1618. }
  1619. }
  1620. uint64_t Server::estimateGenerationTime(const GenerationRequest& request) {
  1621. // Basic estimation based on parameters
  1622. uint64_t baseTime = 1000; // 1 second base time
  1623. // Factor in steps
  1624. baseTime *= request.steps;
  1625. // Factor in resolution
  1626. double resolutionFactor = (request.width * request.height) / (512.0 * 512.0);
  1627. baseTime = static_cast<uint64_t>(baseTime * resolutionFactor);
  1628. // Factor in batch count
  1629. baseTime *= request.batchCount;
  1630. // Adjust for sampling method (some are faster than others)
  1631. switch (request.samplingMethod) {
  1632. case SamplingMethod::LCM:
  1633. baseTime /= 4; // LCM is much faster
  1634. break;
  1635. case SamplingMethod::EULER:
  1636. case SamplingMethod::EULER_A:
  1637. baseTime *= 0.8; // Euler methods are faster
  1638. break;
  1639. case SamplingMethod::DPM2:
  1640. case SamplingMethod::DPMPP2S_A:
  1641. baseTime *= 1.2; // DPM methods are slower
  1642. break;
  1643. default:
  1644. break;
  1645. }
  1646. return baseTime;
  1647. }
  1648. size_t Server::estimateMemoryUsage(const GenerationRequest& request) {
  1649. // Basic memory estimation in bytes
  1650. size_t baseMemory = 1024 * 1024 * 1024; // 1GB base
  1651. // Factor in resolution
  1652. double resolutionFactor = (request.width * request.height) / (512.0 * 512.0);
  1653. baseMemory = static_cast<size_t>(baseMemory * resolutionFactor);
  1654. // Factor in batch count
  1655. baseMemory *= request.batchCount;
  1656. // Additional memory for certain features
  1657. if (request.diffusionFlashAttn) {
  1658. baseMemory += 512 * 1024 * 1024; // Extra 512MB for flash attention
  1659. }
  1660. if (!request.controlNetPath.empty()) {
  1661. baseMemory += 1024 * 1024 * 1024; // Extra 1GB for ControlNet
  1662. }
  1663. return baseMemory;
  1664. }
  1665. // Specialized generation endpoints
  1666. void Server::handleText2Img(const httplib::Request& req, httplib::Response& res) {
  1667. std::string requestId = generateRequestId();
  1668. try {
  1669. if (!m_generationQueue) {
  1670. sendErrorResponse(res, "Generation queue not available", 500, "QUEUE_UNAVAILABLE", requestId);
  1671. return;
  1672. }
  1673. json requestJson = json::parse(req.body);
  1674. // Validate required fields for text2img
  1675. if (!requestJson.contains("prompt") || !requestJson["prompt"].is_string()) {
  1676. sendErrorResponse(res, "Missing or invalid 'prompt' field", 400, "INVALID_PARAMETERS", requestId);
  1677. return;
  1678. }
  1679. // Validate all parameters
  1680. auto [isValid, errorMessage] = validateGenerationParameters(requestJson);
  1681. if (!isValid) {
  1682. sendErrorResponse(res, errorMessage, 400, "INVALID_PARAMETERS", requestId);
  1683. return;
  1684. }
  1685. // Check if any model is loaded
  1686. if (!m_modelManager) {
  1687. sendErrorResponse(res, "Model manager not available", 500, "MODEL_MANAGER_UNAVAILABLE", requestId);
  1688. return;
  1689. }
  1690. // Get currently loaded checkpoint model
  1691. auto allModels = m_modelManager->getAllModels();
  1692. std::string loadedModelName;
  1693. for (const auto& [modelName, modelInfo] : allModels) {
  1694. if (modelInfo.type == ModelType::CHECKPOINT && modelInfo.isLoaded) {
  1695. loadedModelName = modelName;
  1696. break;
  1697. }
  1698. }
  1699. if (loadedModelName.empty()) {
  1700. sendErrorResponse(res, "No checkpoint model loaded. Please load a checkpoint model first using POST /api/models/{hash}/load", 400, "NO_CHECKPOINT_LOADED", requestId);
  1701. return;
  1702. }
  1703. // Create generation request specifically for text2img
  1704. GenerationRequest genRequest;
  1705. genRequest.id = requestId;
  1706. genRequest.modelName = loadedModelName; // Use the currently loaded model
  1707. genRequest.prompt = requestJson["prompt"];
  1708. genRequest.negativePrompt = requestJson.value("negative_prompt", "");
  1709. genRequest.width = requestJson.value("width", 512);
  1710. genRequest.height = requestJson.value("height", 512);
  1711. genRequest.batchCount = requestJson.value("batch_count", 1);
  1712. genRequest.steps = requestJson.value("steps", 20);
  1713. genRequest.cfgScale = requestJson.value("cfg_scale", 7.5f);
  1714. genRequest.seed = requestJson.value("seed", "random");
  1715. // Parse optional parameters
  1716. if (requestJson.contains("sampling_method")) {
  1717. genRequest.samplingMethod = parseSamplingMethod(requestJson["sampling_method"]);
  1718. }
  1719. if (requestJson.contains("scheduler")) {
  1720. genRequest.scheduler = parseScheduler(requestJson["scheduler"]);
  1721. }
  1722. // Set text2img specific defaults
  1723. genRequest.strength = 1.0f; // Full strength for text2img
  1724. // Optional VAE model
  1725. if (requestJson.contains("vae_model") && requestJson["vae_model"].is_string()) {
  1726. std::string vaeModelId = requestJson["vae_model"];
  1727. if (!vaeModelId.empty()) {
  1728. auto vaeInfo = m_modelManager->getModelInfo(vaeModelId);
  1729. if (!vaeInfo.name.empty() && vaeInfo.type == ModelType::VAE) {
  1730. genRequest.vaePath = vaeInfo.path;
  1731. } else {
  1732. sendErrorResponse(res, "VAE model not found or invalid: " + vaeModelId, 400, "INVALID_VAE_MODEL", requestId);
  1733. return;
  1734. }
  1735. }
  1736. }
  1737. // Optional TAESD model
  1738. if (requestJson.contains("taesd_model") && requestJson["taesd_model"].is_string()) {
  1739. std::string taesdModelId = requestJson["taesd_model"];
  1740. if (!taesdModelId.empty()) {
  1741. auto taesdInfo = m_modelManager->getModelInfo(taesdModelId);
  1742. if (!taesdInfo.name.empty() && taesdInfo.type == ModelType::TAESD) {
  1743. genRequest.taesdPath = taesdInfo.path;
  1744. } else {
  1745. sendErrorResponse(res, "TAESD model not found or invalid: " + taesdModelId, 400, "INVALID_TAESD_MODEL", requestId);
  1746. return;
  1747. }
  1748. }
  1749. }
  1750. // Enqueue request
  1751. auto future = m_generationQueue->enqueueRequest(genRequest);
  1752. json params = {
  1753. {"prompt", genRequest.prompt},
  1754. {"negative_prompt", genRequest.negativePrompt},
  1755. {"model", genRequest.modelName},
  1756. {"width", genRequest.width},
  1757. {"height", genRequest.height},
  1758. {"batch_count", genRequest.batchCount},
  1759. {"steps", genRequest.steps},
  1760. {"cfg_scale", genRequest.cfgScale},
  1761. {"seed", genRequest.seed},
  1762. {"sampling_method", samplingMethodToString(genRequest.samplingMethod)},
  1763. {"scheduler", schedulerToString(genRequest.scheduler)}
  1764. };
  1765. // Add VAE/TAESD if specified
  1766. if (!genRequest.vaePath.empty()) {
  1767. params["vae_model"] = requestJson.value("vae_model", "");
  1768. }
  1769. if (!genRequest.taesdPath.empty()) {
  1770. params["taesd_model"] = requestJson.value("taesd_model", "");
  1771. }
  1772. json response = {
  1773. {"request_id", requestId},
  1774. {"status", "queued"},
  1775. {"message", "Text-to-image generation request queued successfully"},
  1776. {"queue_position", m_generationQueue->getQueueSize()},
  1777. {"estimated_time_seconds", estimateGenerationTime(genRequest) / 1000},
  1778. {"estimated_memory_mb", estimateMemoryUsage(genRequest) / (1024 * 1024)},
  1779. {"type", "text2img"},
  1780. {"parameters", params}
  1781. };
  1782. sendJsonResponse(res, response, 202);
  1783. } catch (const json::parse_error& e) {
  1784. sendErrorResponse(res, std::string("Invalid JSON: ") + e.what(), 400, "JSON_PARSE_ERROR", requestId);
  1785. } catch (const std::exception& e) {
  1786. sendErrorResponse(res, std::string("Text-to-image request failed: ") + e.what(), 500, "INTERNAL_ERROR", requestId);
  1787. }
  1788. }
  1789. void Server::handleImg2Img(const httplib::Request& req, httplib::Response& res) {
  1790. std::string requestId = generateRequestId();
  1791. try {
  1792. if (!m_generationQueue) {
  1793. sendErrorResponse(res, "Generation queue not available", 500, "QUEUE_UNAVAILABLE", requestId);
  1794. return;
  1795. }
  1796. json requestJson = json::parse(req.body);
  1797. // Validate required fields for img2img
  1798. if (!requestJson.contains("prompt") || !requestJson["prompt"].is_string()) {
  1799. sendErrorResponse(res, "Missing or invalid 'prompt' field", 400, "INVALID_PARAMETERS", requestId);
  1800. return;
  1801. }
  1802. if (!requestJson.contains("init_image") || !requestJson["init_image"].is_string()) {
  1803. sendErrorResponse(res, "Missing or invalid 'init_image' field", 400, "INVALID_PARAMETERS", requestId);
  1804. return;
  1805. }
  1806. // Validate all parameters
  1807. auto [isValid, errorMessage] = validateGenerationParameters(requestJson);
  1808. if (!isValid) {
  1809. sendErrorResponse(res, errorMessage, 400, "INVALID_PARAMETERS", requestId);
  1810. return;
  1811. }
  1812. // Check if any model is loaded
  1813. if (!m_modelManager) {
  1814. sendErrorResponse(res, "Model manager not available", 500, "MODEL_MANAGER_UNAVAILABLE", requestId);
  1815. return;
  1816. }
  1817. // Get currently loaded checkpoint model
  1818. auto allModels = m_modelManager->getAllModels();
  1819. std::string loadedModelName;
  1820. for (const auto& [modelName, modelInfo] : allModels) {
  1821. if (modelInfo.type == ModelType::CHECKPOINT && modelInfo.isLoaded) {
  1822. loadedModelName = modelName;
  1823. break;
  1824. }
  1825. }
  1826. if (loadedModelName.empty()) {
  1827. sendErrorResponse(res, "No checkpoint model loaded. Please load a checkpoint model first using POST /api/models/{hash}/load", 400, "NO_CHECKPOINT_LOADED", requestId);
  1828. return;
  1829. }
  1830. // Load the init image
  1831. std::string initImageInput = requestJson["init_image"];
  1832. auto [imageData, imgWidth, imgHeight, imgChannels, success, loadError] = loadImageFromInput(initImageInput);
  1833. if (!success) {
  1834. sendErrorResponse(res, "Failed to load init image: " + loadError, 400, "IMAGE_LOAD_ERROR", requestId);
  1835. return;
  1836. }
  1837. // Create generation request specifically for img2img
  1838. GenerationRequest genRequest;
  1839. genRequest.id = requestId;
  1840. genRequest.requestType = GenerationRequest::RequestType::IMG2IMG;
  1841. genRequest.modelName = loadedModelName; // Use the currently loaded model
  1842. genRequest.prompt = requestJson["prompt"];
  1843. genRequest.negativePrompt = requestJson.value("negative_prompt", "");
  1844. genRequest.width = requestJson.value("width", imgWidth); // Default to input image dimensions
  1845. genRequest.height = requestJson.value("height", imgHeight);
  1846. genRequest.batchCount = requestJson.value("batch_count", 1);
  1847. genRequest.steps = requestJson.value("steps", 20);
  1848. genRequest.cfgScale = requestJson.value("cfg_scale", 7.5f);
  1849. genRequest.seed = requestJson.value("seed", "random");
  1850. genRequest.strength = requestJson.value("strength", 0.75f);
  1851. // Set init image data
  1852. genRequest.initImageData = imageData;
  1853. genRequest.initImageWidth = imgWidth;
  1854. genRequest.initImageHeight = imgHeight;
  1855. genRequest.initImageChannels = imgChannels;
  1856. // Parse optional parameters
  1857. if (requestJson.contains("sampling_method")) {
  1858. genRequest.samplingMethod = parseSamplingMethod(requestJson["sampling_method"]);
  1859. }
  1860. if (requestJson.contains("scheduler")) {
  1861. genRequest.scheduler = parseScheduler(requestJson["scheduler"]);
  1862. }
  1863. // Optional VAE model
  1864. if (requestJson.contains("vae_model") && requestJson["vae_model"].is_string()) {
  1865. std::string vaeModelId = requestJson["vae_model"];
  1866. if (!vaeModelId.empty()) {
  1867. auto vaeInfo = m_modelManager->getModelInfo(vaeModelId);
  1868. if (!vaeInfo.name.empty() && vaeInfo.type == ModelType::VAE) {
  1869. genRequest.vaePath = vaeInfo.path;
  1870. } else {
  1871. sendErrorResponse(res, "VAE model not found or invalid: " + vaeModelId, 400, "INVALID_VAE_MODEL", requestId);
  1872. return;
  1873. }
  1874. }
  1875. }
  1876. // Optional TAESD model
  1877. if (requestJson.contains("taesd_model") && requestJson["taesd_model"].is_string()) {
  1878. std::string taesdModelId = requestJson["taesd_model"];
  1879. if (!taesdModelId.empty()) {
  1880. auto taesdInfo = m_modelManager->getModelInfo(taesdModelId);
  1881. if (!taesdInfo.name.empty() && taesdInfo.type == ModelType::TAESD) {
  1882. genRequest.taesdPath = taesdInfo.path;
  1883. } else {
  1884. sendErrorResponse(res, "TAESD model not found or invalid: " + taesdModelId, 400, "INVALID_TAESD_MODEL", requestId);
  1885. return;
  1886. }
  1887. }
  1888. }
  1889. // Enqueue request
  1890. auto future = m_generationQueue->enqueueRequest(genRequest);
  1891. json params = {
  1892. {"prompt", genRequest.prompt},
  1893. {"negative_prompt", genRequest.negativePrompt},
  1894. {"init_image", requestJson["init_image"]},
  1895. {"model", genRequest.modelName},
  1896. {"width", genRequest.width},
  1897. {"height", genRequest.height},
  1898. {"batch_count", genRequest.batchCount},
  1899. {"steps", genRequest.steps},
  1900. {"cfg_scale", genRequest.cfgScale},
  1901. {"seed", genRequest.seed},
  1902. {"strength", genRequest.strength},
  1903. {"sampling_method", samplingMethodToString(genRequest.samplingMethod)},
  1904. {"scheduler", schedulerToString(genRequest.scheduler)}
  1905. };
  1906. // Add VAE/TAESD if specified
  1907. if (!genRequest.vaePath.empty()) {
  1908. params["vae_model"] = requestJson.value("vae_model", "");
  1909. }
  1910. if (!genRequest.taesdPath.empty()) {
  1911. params["taesd_model"] = requestJson.value("taesd_model", "");
  1912. }
  1913. json response = {
  1914. {"request_id", requestId},
  1915. {"status", "queued"},
  1916. {"message", "Image-to-image generation request queued successfully"},
  1917. {"queue_position", m_generationQueue->getQueueSize()},
  1918. {"estimated_time_seconds", estimateGenerationTime(genRequest) / 1000},
  1919. {"estimated_memory_mb", estimateMemoryUsage(genRequest) / (1024 * 1024)},
  1920. {"type", "img2img"},
  1921. {"parameters", params}
  1922. };
  1923. sendJsonResponse(res, response, 202);
  1924. } catch (const json::parse_error& e) {
  1925. sendErrorResponse(res, std::string("Invalid JSON: ") + e.what(), 400, "JSON_PARSE_ERROR", requestId);
  1926. } catch (const std::exception& e) {
  1927. sendErrorResponse(res, std::string("Image-to-image request failed: ") + e.what(), 500, "INTERNAL_ERROR", requestId);
  1928. }
  1929. }
  1930. void Server::handleControlNet(const httplib::Request& req, httplib::Response& res) {
  1931. std::string requestId = generateRequestId();
  1932. try {
  1933. if (!m_generationQueue) {
  1934. sendErrorResponse(res, "Generation queue not available", 500, "QUEUE_UNAVAILABLE", requestId);
  1935. return;
  1936. }
  1937. json requestJson = json::parse(req.body);
  1938. // Validate required fields for ControlNet
  1939. if (!requestJson.contains("prompt") || !requestJson["prompt"].is_string()) {
  1940. sendErrorResponse(res, "Missing or invalid 'prompt' field", 400, "INVALID_PARAMETERS", requestId);
  1941. return;
  1942. }
  1943. if (!requestJson.contains("control_image") || !requestJson["control_image"].is_string()) {
  1944. sendErrorResponse(res, "Missing or invalid 'control_image' field", 400, "INVALID_PARAMETERS", requestId);
  1945. return;
  1946. }
  1947. // Validate all parameters
  1948. auto [isValid, errorMessage] = validateGenerationParameters(requestJson);
  1949. if (!isValid) {
  1950. sendErrorResponse(res, errorMessage, 400, "INVALID_PARAMETERS", requestId);
  1951. return;
  1952. }
  1953. // Check if any model is loaded
  1954. if (!m_modelManager) {
  1955. sendErrorResponse(res, "Model manager not available", 500, "MODEL_MANAGER_UNAVAILABLE", requestId);
  1956. return;
  1957. }
  1958. // Get currently loaded checkpoint model
  1959. auto allModels = m_modelManager->getAllModels();
  1960. std::string loadedModelName;
  1961. for (const auto& [modelName, modelInfo] : allModels) {
  1962. if (modelInfo.type == ModelType::CHECKPOINT && modelInfo.isLoaded) {
  1963. loadedModelName = modelName;
  1964. break;
  1965. }
  1966. }
  1967. if (loadedModelName.empty()) {
  1968. sendErrorResponse(res, "No checkpoint model loaded. Please load a checkpoint model first using POST /api/models/{hash}/load", 400, "NO_CHECKPOINT_LOADED", requestId);
  1969. return;
  1970. }
  1971. // Create generation request specifically for ControlNet
  1972. GenerationRequest genRequest;
  1973. genRequest.id = requestId;
  1974. genRequest.modelName = loadedModelName; // Use the currently loaded model
  1975. genRequest.prompt = requestJson["prompt"];
  1976. genRequest.negativePrompt = requestJson.value("negative_prompt", "");
  1977. genRequest.width = requestJson.value("width", 512);
  1978. genRequest.height = requestJson.value("height", 512);
  1979. genRequest.batchCount = requestJson.value("batch_count", 1);
  1980. genRequest.steps = requestJson.value("steps", 20);
  1981. genRequest.cfgScale = requestJson.value("cfg_scale", 7.5f);
  1982. genRequest.seed = requestJson.value("seed", "random");
  1983. genRequest.controlStrength = requestJson.value("control_strength", 0.9f);
  1984. genRequest.controlNetPath = requestJson.value("control_net_model", "");
  1985. // Parse optional parameters
  1986. if (requestJson.contains("sampling_method")) {
  1987. genRequest.samplingMethod = parseSamplingMethod(requestJson["sampling_method"]);
  1988. }
  1989. if (requestJson.contains("scheduler")) {
  1990. genRequest.scheduler = parseScheduler(requestJson["scheduler"]);
  1991. }
  1992. // Optional VAE model
  1993. if (requestJson.contains("vae_model") && requestJson["vae_model"].is_string()) {
  1994. std::string vaeModelId = requestJson["vae_model"];
  1995. if (!vaeModelId.empty()) {
  1996. auto vaeInfo = m_modelManager->getModelInfo(vaeModelId);
  1997. if (!vaeInfo.name.empty() && vaeInfo.type == ModelType::VAE) {
  1998. genRequest.vaePath = vaeInfo.path;
  1999. } else {
  2000. sendErrorResponse(res, "VAE model not found or invalid: " + vaeModelId, 400, "INVALID_VAE_MODEL", requestId);
  2001. return;
  2002. }
  2003. }
  2004. }
  2005. // Optional TAESD model
  2006. if (requestJson.contains("taesd_model") && requestJson["taesd_model"].is_string()) {
  2007. std::string taesdModelId = requestJson["taesd_model"];
  2008. if (!taesdModelId.empty()) {
  2009. auto taesdInfo = m_modelManager->getModelInfo(taesdModelId);
  2010. if (!taesdInfo.name.empty() && taesdInfo.type == ModelType::TAESD) {
  2011. genRequest.taesdPath = taesdInfo.path;
  2012. } else {
  2013. sendErrorResponse(res, "TAESD model not found or invalid: " + taesdModelId, 400, "INVALID_TAESD_MODEL", requestId);
  2014. return;
  2015. }
  2016. }
  2017. }
  2018. // Store control image path (would be handled in actual implementation)
  2019. genRequest.outputPath = requestJson.value("control_image", "");
  2020. // Enqueue request
  2021. auto future = m_generationQueue->enqueueRequest(genRequest);
  2022. json params = {
  2023. {"prompt", genRequest.prompt},
  2024. {"negative_prompt", genRequest.negativePrompt},
  2025. {"control_image", requestJson["control_image"]},
  2026. {"control_net_model", genRequest.controlNetPath},
  2027. {"model", genRequest.modelName},
  2028. {"width", genRequest.width},
  2029. {"height", genRequest.height},
  2030. {"batch_count", genRequest.batchCount},
  2031. {"steps", genRequest.steps},
  2032. {"cfg_scale", genRequest.cfgScale},
  2033. {"seed", genRequest.seed},
  2034. {"control_strength", genRequest.controlStrength},
  2035. {"sampling_method", samplingMethodToString(genRequest.samplingMethod)},
  2036. {"scheduler", schedulerToString(genRequest.scheduler)}
  2037. };
  2038. // Add VAE/TAESD if specified
  2039. if (!genRequest.vaePath.empty()) {
  2040. params["vae_model"] = requestJson.value("vae_model", "");
  2041. }
  2042. if (!genRequest.taesdPath.empty()) {
  2043. params["taesd_model"] = requestJson.value("taesd_model", "");
  2044. }
  2045. json response = {
  2046. {"request_id", requestId},
  2047. {"status", "queued"},
  2048. {"message", "ControlNet generation request queued successfully"},
  2049. {"queue_position", m_generationQueue->getQueueSize()},
  2050. {"estimated_time_seconds", estimateGenerationTime(genRequest) / 1000},
  2051. {"estimated_memory_mb", estimateMemoryUsage(genRequest) / (1024 * 1024)},
  2052. {"type", "controlnet"},
  2053. {"parameters", params}
  2054. };
  2055. sendJsonResponse(res, response, 202);
  2056. } catch (const json::parse_error& e) {
  2057. sendErrorResponse(res, std::string("Invalid JSON: ") + e.what(), 400, "JSON_PARSE_ERROR", requestId);
  2058. } catch (const std::exception& e) {
  2059. sendErrorResponse(res, std::string("ControlNet request failed: ") + e.what(), 500, "INTERNAL_ERROR", requestId);
  2060. }
  2061. }
  2062. void Server::handleUpscale(const httplib::Request& req, httplib::Response& res) {
  2063. std::string requestId = generateRequestId();
  2064. try {
  2065. if (!m_generationQueue) {
  2066. sendErrorResponse(res, "Generation queue not available", 500, "QUEUE_UNAVAILABLE", requestId);
  2067. return;
  2068. }
  2069. json requestJson = json::parse(req.body);
  2070. // Validate required fields for upscaler
  2071. if (!requestJson.contains("image") || !requestJson["image"].is_string()) {
  2072. sendErrorResponse(res, "Missing or invalid 'image' field", 400, "INVALID_PARAMETERS", requestId);
  2073. return;
  2074. }
  2075. if (!requestJson.contains("esrgan_model") || !requestJson["esrgan_model"].is_string()) {
  2076. sendErrorResponse(res, "Missing or invalid 'esrgan_model' field (model hash or name)", 400, "INVALID_PARAMETERS", requestId);
  2077. return;
  2078. }
  2079. // Check if model manager is available
  2080. if (!m_modelManager) {
  2081. sendErrorResponse(res, "Model manager not available", 500, "MODEL_MANAGER_UNAVAILABLE", requestId);
  2082. return;
  2083. }
  2084. // Get the ESRGAN/upscaler model
  2085. std::string esrganModelId = requestJson["esrgan_model"];
  2086. auto modelInfo = m_modelManager->getModelInfo(esrganModelId);
  2087. if (modelInfo.name.empty()) {
  2088. sendErrorResponse(res, "ESRGAN model not found: " + esrganModelId, 404, "MODEL_NOT_FOUND", requestId);
  2089. return;
  2090. }
  2091. if (modelInfo.type != ModelType::ESRGAN && modelInfo.type != ModelType::UPSCALER) {
  2092. sendErrorResponse(res, "Model is not an ESRGAN/upscaler model", 400, "INVALID_MODEL_TYPE", requestId);
  2093. return;
  2094. }
  2095. // Load the input image
  2096. std::string imageInput = requestJson["image"];
  2097. auto [imageData, imgWidth, imgHeight, imgChannels, success, loadError] = loadImageFromInput(imageInput);
  2098. if (!success) {
  2099. sendErrorResponse(res, "Failed to load image: " + loadError, 400, "IMAGE_LOAD_ERROR", requestId);
  2100. return;
  2101. }
  2102. // Create upscaler request
  2103. GenerationRequest genRequest;
  2104. genRequest.id = requestId;
  2105. genRequest.requestType = GenerationRequest::RequestType::UPSCALER;
  2106. genRequest.esrganPath = modelInfo.path;
  2107. genRequest.upscaleFactor = requestJson.value("upscale_factor", 4);
  2108. genRequest.nThreads = requestJson.value("threads", -1);
  2109. genRequest.offloadParamsToCpu = requestJson.value("offload_to_cpu", false);
  2110. genRequest.diffusionConvDirect = requestJson.value("direct", false);
  2111. // Set input image data
  2112. genRequest.initImageData = imageData;
  2113. genRequest.initImageWidth = imgWidth;
  2114. genRequest.initImageHeight = imgHeight;
  2115. genRequest.initImageChannels = imgChannels;
  2116. // Enqueue request
  2117. auto future = m_generationQueue->enqueueRequest(genRequest);
  2118. json response = {
  2119. {"request_id", requestId},
  2120. {"status", "queued"},
  2121. {"message", "Upscale request queued successfully"},
  2122. {"queue_position", m_generationQueue->getQueueSize()},
  2123. {"type", "upscale"},
  2124. {"parameters", {
  2125. {"esrgan_model", esrganModelId},
  2126. {"upscale_factor", genRequest.upscaleFactor},
  2127. {"input_width", imgWidth},
  2128. {"input_height", imgHeight},
  2129. {"output_width", imgWidth * genRequest.upscaleFactor},
  2130. {"output_height", imgHeight * genRequest.upscaleFactor}
  2131. }}
  2132. };
  2133. sendJsonResponse(res, response, 202);
  2134. } catch (const json::parse_error& e) {
  2135. sendErrorResponse(res, std::string("Invalid JSON: ") + e.what(), 400, "JSON_PARSE_ERROR", requestId);
  2136. } catch (const std::exception& e) {
  2137. sendErrorResponse(res, std::string("Upscale request failed: ") + e.what(), 500, "INTERNAL_ERROR", requestId);
  2138. }
  2139. }
  2140. void Server::handleInpainting(const httplib::Request& req, httplib::Response& res) {
  2141. std::string requestId = generateRequestId();
  2142. try {
  2143. if (!m_generationQueue) {
  2144. sendErrorResponse(res, "Generation queue not available", 500, "QUEUE_UNAVAILABLE", requestId);
  2145. return;
  2146. }
  2147. json requestJson = json::parse(req.body);
  2148. // Validate required fields for inpainting
  2149. if (!requestJson.contains("prompt") || !requestJson["prompt"].is_string()) {
  2150. sendErrorResponse(res, "Missing or invalid 'prompt' field", 400, "INVALID_PARAMETERS", requestId);
  2151. return;
  2152. }
  2153. if (!requestJson.contains("source_image") || !requestJson["source_image"].is_string()) {
  2154. sendErrorResponse(res, "Missing or invalid 'source_image' field", 400, "INVALID_PARAMETERS", requestId);
  2155. return;
  2156. }
  2157. if (!requestJson.contains("mask_image") || !requestJson["mask_image"].is_string()) {
  2158. sendErrorResponse(res, "Missing or invalid 'mask_image' field", 400, "INVALID_PARAMETERS", requestId);
  2159. return;
  2160. }
  2161. // Validate all parameters
  2162. auto [isValid, errorMessage] = validateGenerationParameters(requestJson);
  2163. if (!isValid) {
  2164. sendErrorResponse(res, errorMessage, 400, "INVALID_PARAMETERS", requestId);
  2165. return;
  2166. }
  2167. // Check if any model is loaded
  2168. if (!m_modelManager) {
  2169. sendErrorResponse(res, "Model manager not available", 500, "MODEL_MANAGER_UNAVAILABLE", requestId);
  2170. return;
  2171. }
  2172. // Get currently loaded checkpoint model
  2173. auto allModels = m_modelManager->getAllModels();
  2174. std::string loadedModelName;
  2175. for (const auto& [modelName, modelInfo] : allModels) {
  2176. if (modelInfo.type == ModelType::CHECKPOINT && modelInfo.isLoaded) {
  2177. loadedModelName = modelName;
  2178. break;
  2179. }
  2180. }
  2181. if (loadedModelName.empty()) {
  2182. sendErrorResponse(res, "No checkpoint model loaded. Please load a checkpoint model first using POST /api/models/{hash}/load", 400, "NO_CHECKPOINT_LOADED", requestId);
  2183. return;
  2184. }
  2185. // Load the source image
  2186. std::string sourceImageInput = requestJson["source_image"];
  2187. auto [sourceImageData, sourceImgWidth, sourceImgHeight, sourceImgChannels, sourceSuccess, sourceLoadError] = loadImageFromInput(sourceImageInput);
  2188. if (!sourceSuccess) {
  2189. sendErrorResponse(res, "Failed to load source image: " + sourceLoadError, 400, "IMAGE_LOAD_ERROR", requestId);
  2190. return;
  2191. }
  2192. // Load the mask image
  2193. std::string maskImageInput = requestJson["mask_image"];
  2194. auto [maskImageData, maskImgWidth, maskImgHeight, maskImgChannels, maskSuccess, maskLoadError] = loadImageFromInput(maskImageInput);
  2195. if (!maskSuccess) {
  2196. sendErrorResponse(res, "Failed to load mask image: " + maskLoadError, 400, "MASK_LOAD_ERROR", requestId);
  2197. return;
  2198. }
  2199. // Validate that source and mask images have compatible dimensions
  2200. if (sourceImgWidth != maskImgWidth || sourceImgHeight != maskImgHeight) {
  2201. sendErrorResponse(res, "Source and mask images must have the same dimensions", 400, "DIMENSION_MISMATCH", requestId);
  2202. return;
  2203. }
  2204. // Create generation request specifically for inpainting
  2205. GenerationRequest genRequest;
  2206. genRequest.id = requestId;
  2207. genRequest.requestType = GenerationRequest::RequestType::INPAINTING;
  2208. genRequest.modelName = loadedModelName; // Use the currently loaded model
  2209. genRequest.prompt = requestJson["prompt"];
  2210. genRequest.negativePrompt = requestJson.value("negative_prompt", "");
  2211. genRequest.width = requestJson.value("width", sourceImgWidth); // Default to input image dimensions
  2212. genRequest.height = requestJson.value("height", sourceImgHeight);
  2213. genRequest.batchCount = requestJson.value("batch_count", 1);
  2214. genRequest.steps = requestJson.value("steps", 20);
  2215. genRequest.cfgScale = requestJson.value("cfg_scale", 7.5f);
  2216. genRequest.seed = requestJson.value("seed", "random");
  2217. genRequest.strength = requestJson.value("strength", 0.75f);
  2218. // Set source image data
  2219. genRequest.initImageData = sourceImageData;
  2220. genRequest.initImageWidth = sourceImgWidth;
  2221. genRequest.initImageHeight = sourceImgHeight;
  2222. genRequest.initImageChannels = sourceImgChannels;
  2223. // Set mask image data
  2224. genRequest.maskImageData = maskImageData;
  2225. genRequest.maskImageWidth = maskImgWidth;
  2226. genRequest.maskImageHeight = maskImgHeight;
  2227. genRequest.maskImageChannels = maskImgChannels;
  2228. // Parse optional parameters
  2229. if (requestJson.contains("sampling_method")) {
  2230. genRequest.samplingMethod = parseSamplingMethod(requestJson["sampling_method"]);
  2231. }
  2232. if (requestJson.contains("scheduler")) {
  2233. genRequest.scheduler = parseScheduler(requestJson["scheduler"]);
  2234. }
  2235. // Optional VAE model
  2236. if (requestJson.contains("vae_model") && requestJson["vae_model"].is_string()) {
  2237. std::string vaeModelId = requestJson["vae_model"];
  2238. if (!vaeModelId.empty()) {
  2239. auto vaeInfo = m_modelManager->getModelInfo(vaeModelId);
  2240. if (!vaeInfo.name.empty() && vaeInfo.type == ModelType::VAE) {
  2241. genRequest.vaePath = vaeInfo.path;
  2242. } else {
  2243. sendErrorResponse(res, "VAE model not found or invalid: " + vaeModelId, 400, "INVALID_VAE_MODEL", requestId);
  2244. return;
  2245. }
  2246. }
  2247. }
  2248. // Optional TAESD model
  2249. if (requestJson.contains("taesd_model") && requestJson["taesd_model"].is_string()) {
  2250. std::string taesdModelId = requestJson["taesd_model"];
  2251. if (!taesdModelId.empty()) {
  2252. auto taesdInfo = m_modelManager->getModelInfo(taesdModelId);
  2253. if (!taesdInfo.name.empty() && taesdInfo.type == ModelType::TAESD) {
  2254. genRequest.taesdPath = taesdInfo.path;
  2255. } else {
  2256. sendErrorResponse(res, "TAESD model not found or invalid: " + taesdModelId, 400, "INVALID_TAESD_MODEL", requestId);
  2257. return;
  2258. }
  2259. }
  2260. }
  2261. // Enqueue request
  2262. auto future = m_generationQueue->enqueueRequest(genRequest);
  2263. json params = {
  2264. {"prompt", genRequest.prompt},
  2265. {"negative_prompt", genRequest.negativePrompt},
  2266. {"source_image", requestJson["source_image"]},
  2267. {"mask_image", requestJson["mask_image"]},
  2268. {"model", genRequest.modelName},
  2269. {"width", genRequest.width},
  2270. {"height", genRequest.height},
  2271. {"batch_count", genRequest.batchCount},
  2272. {"steps", genRequest.steps},
  2273. {"cfg_scale", genRequest.cfgScale},
  2274. {"seed", genRequest.seed},
  2275. {"strength", genRequest.strength},
  2276. {"sampling_method", samplingMethodToString(genRequest.samplingMethod)},
  2277. {"scheduler", schedulerToString(genRequest.scheduler)}
  2278. };
  2279. // Add VAE/TAESD if specified
  2280. if (!genRequest.vaePath.empty()) {
  2281. params["vae_model"] = requestJson.value("vae_model", "");
  2282. }
  2283. if (!genRequest.taesdPath.empty()) {
  2284. params["taesd_model"] = requestJson.value("taesd_model", "");
  2285. }
  2286. json response = {
  2287. {"request_id", requestId},
  2288. {"status", "queued"},
  2289. {"message", "Inpainting generation request queued successfully"},
  2290. {"queue_position", m_generationQueue->getQueueSize()},
  2291. {"estimated_time_seconds", estimateGenerationTime(genRequest) / 1000},
  2292. {"estimated_memory_mb", estimateMemoryUsage(genRequest) / (1024 * 1024)},
  2293. {"type", "inpainting"},
  2294. {"parameters", params}
  2295. };
  2296. sendJsonResponse(res, response, 202);
  2297. } catch (const json::parse_error& e) {
  2298. sendErrorResponse(res, std::string("Invalid JSON: ") + e.what(), 400, "JSON_PARSE_ERROR", requestId);
  2299. } catch (const std::exception& e) {
  2300. sendErrorResponse(res, std::string("Inpainting request failed: ") + e.what(), 500, "INTERNAL_ERROR", requestId);
  2301. }
  2302. }
  2303. // Utility endpoints
  2304. void Server::handleSamplers(const httplib::Request& req, httplib::Response& res) {
  2305. try {
  2306. json samplers = {
  2307. {"samplers", {
  2308. {
  2309. {"name", "euler"},
  2310. {"description", "Euler sampler - fast and simple"},
  2311. {"recommended_steps", 20}
  2312. },
  2313. {
  2314. {"name", "euler_a"},
  2315. {"description", "Euler ancestral sampler - adds randomness"},
  2316. {"recommended_steps", 20}
  2317. },
  2318. {
  2319. {"name", "heun"},
  2320. {"description", "Heun sampler - more accurate but slower"},
  2321. {"recommended_steps", 20}
  2322. },
  2323. {
  2324. {"name", "dpm2"},
  2325. {"description", "DPM2 sampler - second-order DPM"},
  2326. {"recommended_steps", 20}
  2327. },
  2328. {
  2329. {"name", "dpm++2s_a"},
  2330. {"description", "DPM++ 2s ancestral sampler"},
  2331. {"recommended_steps", 20}
  2332. },
  2333. {
  2334. {"name", "dpm++2m"},
  2335. {"description", "DPM++ 2m sampler - multistep"},
  2336. {"recommended_steps", 20}
  2337. },
  2338. {
  2339. {"name", "dpm++2mv2"},
  2340. {"description", "DPM++ 2m v2 sampler - improved multistep"},
  2341. {"recommended_steps", 20}
  2342. },
  2343. {
  2344. {"name", "ipndm"},
  2345. {"description", "IPNDM sampler - improved noise prediction"},
  2346. {"recommended_steps", 20}
  2347. },
  2348. {
  2349. {"name", "ipndm_v"},
  2350. {"description", "IPNDM v sampler - variant of IPNDM"},
  2351. {"recommended_steps", 20}
  2352. },
  2353. {
  2354. {"name", "lcm"},
  2355. {"description", "LCM sampler - Latent Consistency Model, very fast"},
  2356. {"recommended_steps", 4}
  2357. },
  2358. {
  2359. {"name", "ddim_trailing"},
  2360. {"description", "DDIM trailing sampler - deterministic"},
  2361. {"recommended_steps", 20}
  2362. },
  2363. {
  2364. {"name", "tcd"},
  2365. {"description", "TCD sampler - Trajectory Consistency Distillation"},
  2366. {"recommended_steps", 8}
  2367. },
  2368. {
  2369. {"name", "default"},
  2370. {"description", "Use model's default sampler"},
  2371. {"recommended_steps", 20}
  2372. }
  2373. }}
  2374. };
  2375. sendJsonResponse(res, samplers);
  2376. } catch (const std::exception& e) {
  2377. sendErrorResponse(res, std::string("Failed to get samplers: ") + e.what(), 500);
  2378. }
  2379. }
  2380. void Server::handleSchedulers(const httplib::Request& req, httplib::Response& res) {
  2381. try {
  2382. json schedulers = {
  2383. {"schedulers", {
  2384. {
  2385. {"name", "discrete"},
  2386. {"description", "Discrete scheduler - standard noise schedule"}
  2387. },
  2388. {
  2389. {"name", "karras"},
  2390. {"description", "Karras scheduler - improved noise schedule"}
  2391. },
  2392. {
  2393. {"name", "exponential"},
  2394. {"description", "Exponential scheduler - exponential noise decay"}
  2395. },
  2396. {
  2397. {"name", "ays"},
  2398. {"description", "AYS scheduler - Adaptive Your Scheduler"}
  2399. },
  2400. {
  2401. {"name", "gits"},
  2402. {"description", "GITS scheduler - Generalized Iterative Time Steps"}
  2403. },
  2404. {
  2405. {"name", "smoothstep"},
  2406. {"description", "Smoothstep scheduler - smooth transition function"}
  2407. },
  2408. {
  2409. {"name", "sgm_uniform"},
  2410. {"description", "SGM uniform scheduler - uniform noise schedule"}
  2411. },
  2412. {
  2413. {"name", "simple"},
  2414. {"description", "Simple scheduler - basic linear schedule"}
  2415. },
  2416. {
  2417. {"name", "default"},
  2418. {"description", "Use model's default scheduler"}
  2419. }
  2420. }}
  2421. };
  2422. sendJsonResponse(res, schedulers);
  2423. } catch (const std::exception& e) {
  2424. sendErrorResponse(res, std::string("Failed to get schedulers: ") + e.what(), 500);
  2425. }
  2426. }
  2427. void Server::handleParameters(const httplib::Request& req, httplib::Response& res) {
  2428. try {
  2429. json parameters = {
  2430. {"parameters", {
  2431. {
  2432. {"name", "prompt"},
  2433. {"type", "string"},
  2434. {"required", true},
  2435. {"description", "Text prompt for image generation"},
  2436. {"min_length", 1},
  2437. {"max_length", 10000},
  2438. {"example", "a beautiful landscape with mountains"}
  2439. },
  2440. {
  2441. {"name", "negative_prompt"},
  2442. {"type", "string"},
  2443. {"required", false},
  2444. {"description", "Negative prompt to guide generation away from"},
  2445. {"min_length", 0},
  2446. {"max_length", 10000},
  2447. {"example", "blurry, low quality, distorted"}
  2448. },
  2449. {
  2450. {"name", "width"},
  2451. {"type", "integer"},
  2452. {"required", false},
  2453. {"description", "Image width in pixels"},
  2454. {"min", 64},
  2455. {"max", 2048},
  2456. {"multiple_of", 64},
  2457. {"default", 512}
  2458. },
  2459. {
  2460. {"name", "height"},
  2461. {"type", "integer"},
  2462. {"required", false},
  2463. {"description", "Image height in pixels"},
  2464. {"min", 64},
  2465. {"max", 2048},
  2466. {"multiple_of", 64},
  2467. {"default", 512}
  2468. },
  2469. {
  2470. {"name", "steps"},
  2471. {"type", "integer"},
  2472. {"required", false},
  2473. {"description", "Number of diffusion steps"},
  2474. {"min", 1},
  2475. {"max", 150},
  2476. {"default", 20}
  2477. },
  2478. {
  2479. {"name", "cfg_scale"},
  2480. {"type", "number"},
  2481. {"required", false},
  2482. {"description", "Classifier-Free Guidance scale"},
  2483. {"min", 1.0},
  2484. {"max", 30.0},
  2485. {"default", 7.5}
  2486. },
  2487. {
  2488. {"name", "seed"},
  2489. {"type", "string|integer"},
  2490. {"required", false},
  2491. {"description", "Seed for generation (use 'random' for random seed)"},
  2492. {"example", "42"}
  2493. },
  2494. {
  2495. {"name", "sampling_method"},
  2496. {"type", "string"},
  2497. {"required", false},
  2498. {"description", "Sampling method to use"},
  2499. {"enum", {"euler", "euler_a", "heun", "dpm2", "dpm++2s_a", "dpm++2m", "dpm++2mv2", "ipndm", "ipndm_v", "lcm", "ddim_trailing", "tcd", "default"}},
  2500. {"default", "default"}
  2501. },
  2502. {
  2503. {"name", "scheduler"},
  2504. {"type", "string"},
  2505. {"required", false},
  2506. {"description", "Scheduler to use"},
  2507. {"enum", {"discrete", "karras", "exponential", "ays", "gits", "smoothstep", "sgm_uniform", "simple", "default"}},
  2508. {"default", "default"}
  2509. },
  2510. {
  2511. {"name", "batch_count"},
  2512. {"type", "integer"},
  2513. {"required", false},
  2514. {"description", "Number of images to generate"},
  2515. {"min", 1},
  2516. {"max", 100},
  2517. {"default", 1}
  2518. },
  2519. {
  2520. {"name", "strength"},
  2521. {"type", "number"},
  2522. {"required", false},
  2523. {"description", "Strength for img2img (0.0-1.0)"},
  2524. {"min", 0.0},
  2525. {"max", 1.0},
  2526. {"default", 0.75}
  2527. },
  2528. {
  2529. {"name", "control_strength"},
  2530. {"type", "number"},
  2531. {"required", false},
  2532. {"description", "ControlNet strength (0.0-1.0)"},
  2533. {"min", 0.0},
  2534. {"max", 1.0},
  2535. {"default", 0.9}
  2536. }
  2537. }},
  2538. {"openapi", {
  2539. {"version", "3.0.0"},
  2540. {"info", {
  2541. {"title", "Stable Diffusion REST API"},
  2542. {"version", "1.0.0"},
  2543. {"description", "Comprehensive REST API for stable-diffusion.cpp functionality"}
  2544. }},
  2545. {"components", {
  2546. {"schemas", {
  2547. {"GenerationRequest", {
  2548. {"type", "object"},
  2549. {"required", {"prompt"}},
  2550. {"properties", {
  2551. {"prompt", {{"type", "string"}, {"description", "Text prompt for generation"}}},
  2552. {"negative_prompt", {{"type", "string"}, {"description", "Negative prompt"}}},
  2553. {"width", {{"type", "integer"}, {"minimum", 64}, {"maximum", 2048}, {"default", 512}}},
  2554. {"height", {{"type", "integer"}, {"minimum", 64}, {"maximum", 2048}, {"default", 512}}},
  2555. {"steps", {{"type", "integer"}, {"minimum", 1}, {"maximum", 150}, {"default", 20}}},
  2556. {"cfg_scale", {{"type", "number"}, {"minimum", 1.0}, {"maximum", 30.0}, {"default", 7.5}}}
  2557. }}
  2558. }}
  2559. }}
  2560. }}
  2561. }}
  2562. };
  2563. sendJsonResponse(res, parameters);
  2564. } catch (const std::exception& e) {
  2565. sendErrorResponse(res, std::string("Failed to get parameters: ") + e.what(), 500);
  2566. }
  2567. }
  2568. void Server::handleValidate(const httplib::Request& req, httplib::Response& res) {
  2569. std::string requestId = generateRequestId();
  2570. try {
  2571. json requestJson = json::parse(req.body);
  2572. // Validate parameters
  2573. auto [isValid, errorMessage] = validateGenerationParameters(requestJson);
  2574. json response = {
  2575. {"request_id", requestId},
  2576. {"valid", isValid},
  2577. {"message", isValid ? "Parameters are valid" : errorMessage},
  2578. {"errors", isValid ? json::array() : json::array({errorMessage})}
  2579. };
  2580. sendJsonResponse(res, response, isValid ? 200 : 400);
  2581. } catch (const json::parse_error& e) {
  2582. sendErrorResponse(res, std::string("Invalid JSON: ") + e.what(), 400, "JSON_PARSE_ERROR", requestId);
  2583. } catch (const std::exception& e) {
  2584. sendErrorResponse(res, std::string("Validation failed: ") + e.what(), 500, "INTERNAL_ERROR", requestId);
  2585. }
  2586. }
  2587. void Server::handleEstimate(const httplib::Request& req, httplib::Response& res) {
  2588. std::string requestId = generateRequestId();
  2589. try {
  2590. json requestJson = json::parse(req.body);
  2591. // Validate parameters first
  2592. auto [isValid, errorMessage] = validateGenerationParameters(requestJson);
  2593. if (!isValid) {
  2594. sendErrorResponse(res, errorMessage, 400, "INVALID_PARAMETERS", requestId);
  2595. return;
  2596. }
  2597. // Create a temporary request to estimate
  2598. GenerationRequest genRequest;
  2599. genRequest.prompt = requestJson["prompt"];
  2600. genRequest.width = requestJson.value("width", 512);
  2601. genRequest.height = requestJson.value("height", 512);
  2602. genRequest.batchCount = requestJson.value("batch_count", 1);
  2603. genRequest.steps = requestJson.value("steps", 20);
  2604. genRequest.diffusionFlashAttn = requestJson.value("diffusion_flash_attn", false);
  2605. genRequest.controlNetPath = requestJson.value("control_net_path", "");
  2606. if (requestJson.contains("sampling_method")) {
  2607. genRequest.samplingMethod = parseSamplingMethod(requestJson["sampling_method"]);
  2608. }
  2609. // Calculate estimates
  2610. uint64_t estimatedTime = estimateGenerationTime(genRequest);
  2611. size_t estimatedMemory = estimateMemoryUsage(genRequest);
  2612. json response = {
  2613. {"request_id", requestId},
  2614. {"estimated_time_seconds", estimatedTime / 1000},
  2615. {"estimated_memory_mb", estimatedMemory / (1024 * 1024)},
  2616. {"parameters", {
  2617. {"resolution", std::to_string(genRequest.width) + "x" + std::to_string(genRequest.height)},
  2618. {"steps", genRequest.steps},
  2619. {"batch_count", genRequest.batchCount},
  2620. {"sampling_method", samplingMethodToString(genRequest.samplingMethod)}
  2621. }}
  2622. };
  2623. sendJsonResponse(res, response);
  2624. } catch (const json::parse_error& e) {
  2625. sendErrorResponse(res, std::string("Invalid JSON: ") + e.what(), 400, "JSON_PARSE_ERROR", requestId);
  2626. } catch (const std::exception& e) {
  2627. sendErrorResponse(res, std::string("Estimation failed: ") + e.what(), 500, "INTERNAL_ERROR", requestId);
  2628. }
  2629. }
  2630. void Server::handleConfig(const httplib::Request& req, httplib::Response& res) {
  2631. std::string requestId = generateRequestId();
  2632. try {
  2633. // Get current configuration
  2634. json config = {
  2635. {"request_id", requestId},
  2636. {"config", {
  2637. {"server", {
  2638. {"host", m_host},
  2639. {"port", m_port},
  2640. {"max_concurrent_generations", 1}
  2641. }},
  2642. {"generation", {
  2643. {"default_width", 512},
  2644. {"default_height", 512},
  2645. {"default_steps", 20},
  2646. {"default_cfg_scale", 7.5},
  2647. {"max_batch_count", 100},
  2648. {"max_steps", 150},
  2649. {"max_resolution", 2048}
  2650. }},
  2651. {"rate_limiting", {
  2652. {"requests_per_minute", 60},
  2653. {"enabled", true}
  2654. }}
  2655. }}
  2656. };
  2657. sendJsonResponse(res, config);
  2658. } catch (const std::exception& e) {
  2659. sendErrorResponse(res, std::string("Config operation failed: ") + e.what(), 500, "INTERNAL_ERROR", requestId);
  2660. }
  2661. }
  2662. void Server::handleSystem(const httplib::Request& req, httplib::Response& res) {
  2663. try {
  2664. json system = {
  2665. {"system", {
  2666. {"version", "1.0.0"},
  2667. {"build", "stable-diffusion.cpp-rest"},
  2668. {"uptime", std::chrono::duration_cast<std::chrono::seconds>(
  2669. std::chrono::steady_clock::now().time_since_epoch()).count()},
  2670. {"capabilities", {
  2671. {"text2img", true},
  2672. {"img2img", true},
  2673. {"controlnet", true},
  2674. {"batch_generation", true},
  2675. {"parameter_validation", true},
  2676. {"estimation", true}
  2677. }},
  2678. {"supported_formats", {
  2679. {"input", {"png", "jpg", "jpeg", "webp"}},
  2680. {"output", {"png", "jpg", "jpeg", "webp"}}
  2681. }},
  2682. {"limits", {
  2683. {"max_resolution", 2048},
  2684. {"max_steps", 150},
  2685. {"max_batch_count", 100},
  2686. {"max_prompt_length", 10000}
  2687. }}
  2688. }},
  2689. {"hardware", {
  2690. {"cpu_threads", std::thread::hardware_concurrency()}
  2691. }}
  2692. };
  2693. sendJsonResponse(res, system);
  2694. } catch (const std::exception& e) {
  2695. sendErrorResponse(res, std::string("System info failed: ") + e.what(), 500);
  2696. }
  2697. }
  2698. void Server::handleSystemRestart(const httplib::Request& req, httplib::Response& res) {
  2699. try {
  2700. json response = {
  2701. {"message", "Server restart initiated. The server will shut down gracefully and exit. Please use a process manager to automatically restart it."},
  2702. {"status", "restarting"}
  2703. };
  2704. sendJsonResponse(res, response);
  2705. // Schedule server stop after response is sent
  2706. // Using a separate thread to allow the response to be sent first
  2707. std::thread([this]() {
  2708. std::this_thread::sleep_for(std::chrono::seconds(1));
  2709. this->stop();
  2710. // Exit with code 42 to signal restart intent to process manager
  2711. std::exit(42);
  2712. }).detach();
  2713. } catch (const std::exception& e) {
  2714. sendErrorResponse(res, std::string("Restart failed: ") + e.what(), 500);
  2715. }
  2716. }
  2717. // Helper methods for model management
  2718. json Server::getModelCapabilities(ModelType type) {
  2719. json capabilities = json::object();
  2720. switch (type) {
  2721. case ModelType::CHECKPOINT:
  2722. capabilities = {
  2723. {"text2img", true},
  2724. {"img2img", true},
  2725. {"inpainting", true},
  2726. {"outpainting", true},
  2727. {"controlnet", true},
  2728. {"lora", true},
  2729. {"vae", true},
  2730. {"sampling_methods", {"euler", "euler_a", "heun", "dpm2", "dpm++2s_a", "dpm++2m", "dpm++2mv2", "ipndm", "ipndm_v", "lcm", "ddim_trailing", "tcd"}},
  2731. {"schedulers", {"discrete", "karras", "exponential", "ays", "gits", "smoothstep", "sgm_uniform", "simple"}},
  2732. {"recommended_resolution", "512x512"},
  2733. {"max_resolution", "2048x2048"},
  2734. {"supports_batch", true}
  2735. };
  2736. break;
  2737. case ModelType::LORA:
  2738. capabilities = {
  2739. {"text2img", true},
  2740. {"img2img", true},
  2741. {"inpainting", true},
  2742. {"controlnet", false},
  2743. {"lora", true},
  2744. {"vae", false},
  2745. {"requires_checkpoint", true},
  2746. {"strength_range", {0.0, 2.0}},
  2747. {"recommended_strength", 1.0}
  2748. };
  2749. break;
  2750. case ModelType::CONTROLNET:
  2751. capabilities = {
  2752. {"text2img", false},
  2753. {"img2img", true},
  2754. {"inpainting", true},
  2755. {"controlnet", true},
  2756. {"requires_checkpoint", true},
  2757. {"control_modes", {"canny", "depth", "pose", "scribble", "hed", "mlsd", "normal", "seg"}},
  2758. {"strength_range", {0.0, 1.0}},
  2759. {"recommended_strength", 0.9}
  2760. };
  2761. break;
  2762. case ModelType::VAE:
  2763. capabilities = {
  2764. {"text2img", false},
  2765. {"img2img", false},
  2766. {"inpainting", false},
  2767. {"vae", true},
  2768. {"requires_checkpoint", true},
  2769. {"encoding", true},
  2770. {"decoding", true},
  2771. {"precision", {"fp16", "fp32"}}
  2772. };
  2773. break;
  2774. case ModelType::EMBEDDING:
  2775. capabilities = {
  2776. {"text2img", true},
  2777. {"img2img", true},
  2778. {"inpainting", true},
  2779. {"embedding", true},
  2780. {"requires_checkpoint", true},
  2781. {"token_count", 1},
  2782. {"compatible_with", {"checkpoint", "lora"}}
  2783. };
  2784. break;
  2785. case ModelType::TAESD:
  2786. capabilities = {
  2787. {"text2img", false},
  2788. {"img2img", false},
  2789. {"inpainting", false},
  2790. {"vae", true},
  2791. {"requires_checkpoint", true},
  2792. {"fast_decoding", true},
  2793. {"real_time", true},
  2794. {"precision", {"fp16", "fp32"}}
  2795. };
  2796. break;
  2797. case ModelType::ESRGAN:
  2798. capabilities = {
  2799. {"text2img", false},
  2800. {"img2img", false},
  2801. {"inpainting", false},
  2802. {"upscaling", true},
  2803. {"scale_factors", {2, 4}},
  2804. {"models", {"ESRGAN", "RealESRGAN", "SwinIR"}},
  2805. {"supports_alpha", false}
  2806. };
  2807. break;
  2808. default:
  2809. capabilities = {
  2810. {"text2img", false},
  2811. {"img2img", false},
  2812. {"inpainting", false},
  2813. {"capabilities", {}}
  2814. };
  2815. break;
  2816. }
  2817. return capabilities;
  2818. }
  2819. json Server::getModelTypeStatistics() {
  2820. if (!m_modelManager) return json::object();
  2821. json stats = json::object();
  2822. auto allModels = m_modelManager->getAllModels();
  2823. // Initialize counters for each type
  2824. std::map<ModelType, int> typeCounts;
  2825. std::map<ModelType, int> loadedCounts;
  2826. std::map<ModelType, size_t> sizeByType;
  2827. for (const auto& pair : allModels) {
  2828. ModelType type = pair.second.type;
  2829. typeCounts[type]++;
  2830. if (pair.second.isLoaded) {
  2831. loadedCounts[type]++;
  2832. }
  2833. sizeByType[type] += pair.second.fileSize;
  2834. }
  2835. // Build statistics JSON
  2836. for (const auto& count : typeCounts) {
  2837. std::string typeName = ModelManager::modelTypeToString(count.first);
  2838. stats[typeName] = {
  2839. {"total_count", count.second},
  2840. {"loaded_count", loadedCounts[count.first]},
  2841. {"total_size_bytes", sizeByType[count.first]},
  2842. {"total_size_mb", sizeByType[count.first] / (1024.0 * 1024.0)},
  2843. {"average_size_mb", count.second > 0 ? (sizeByType[count.first] / (1024.0 * 1024.0)) / count.second : 0.0}
  2844. };
  2845. }
  2846. return stats;
  2847. }
  2848. // Additional helper methods for model management
  2849. json Server::getModelCompatibility(const ModelManager::ModelInfo& modelInfo) {
  2850. json compatibility = {
  2851. {"is_compatible", true},
  2852. {"compatibility_score", 100},
  2853. {"issues", json::array()},
  2854. {"warnings", json::array()},
  2855. {"requirements", {
  2856. {"min_memory_mb", 1024},
  2857. {"recommended_memory_mb", 2048},
  2858. {"supported_formats", {"safetensors", "ckpt", "gguf"}},
  2859. {"required_dependencies", {}}
  2860. }}
  2861. };
  2862. // Check for specific compatibility issues based on model type
  2863. if (modelInfo.type == ModelType::LORA) {
  2864. compatibility["requirements"]["required_dependencies"] = {"checkpoint"};
  2865. } else if (modelInfo.type == ModelType::CONTROLNET) {
  2866. compatibility["requirements"]["required_dependencies"] = {"checkpoint"};
  2867. } else if (modelInfo.type == ModelType::VAE) {
  2868. compatibility["requirements"]["required_dependencies"] = {"checkpoint"};
  2869. }
  2870. return compatibility;
  2871. }
  2872. json Server::getModelRequirements(ModelType type) {
  2873. json requirements = {
  2874. {"min_memory_mb", 1024},
  2875. {"recommended_memory_mb", 2048},
  2876. {"min_disk_space_mb", 1024},
  2877. {"supported_formats", {"safetensors", "ckpt", "gguf"}},
  2878. {"required_dependencies", json::array()},
  2879. {"optional_dependencies", json::array()},
  2880. {"system_requirements", {
  2881. {"cpu_cores", 4},
  2882. {"cpu_architecture", "x86_64"},
  2883. {"os", "Linux/Windows/macOS"},
  2884. {"gpu_memory_mb", 2048},
  2885. {"gpu_compute_capability", "3.5+"}
  2886. }}
  2887. };
  2888. switch (type) {
  2889. case ModelType::CHECKPOINT:
  2890. requirements["min_memory_mb"] = 2048;
  2891. requirements["recommended_memory_mb"] = 4096;
  2892. requirements["min_disk_space_mb"] = 2048;
  2893. requirements["supported_formats"] = {"safetensors", "ckpt", "gguf"};
  2894. break;
  2895. case ModelType::LORA:
  2896. requirements["min_memory_mb"] = 512;
  2897. requirements["recommended_memory_mb"] = 1024;
  2898. requirements["min_disk_space_mb"] = 100;
  2899. requirements["supported_formats"] = {"safetensors", "ckpt"};
  2900. requirements["required_dependencies"] = {"checkpoint"};
  2901. break;
  2902. case ModelType::CONTROLNET:
  2903. requirements["min_memory_mb"] = 1024;
  2904. requirements["recommended_memory_mb"] = 2048;
  2905. requirements["min_disk_space_mb"] = 500;
  2906. requirements["supported_formats"] = {"safetensors", "pth"};
  2907. requirements["required_dependencies"] = {"checkpoint"};
  2908. break;
  2909. case ModelType::VAE:
  2910. requirements["min_memory_mb"] = 512;
  2911. requirements["recommended_memory_mb"] = 1024;
  2912. requirements["min_disk_space_mb"] = 200;
  2913. requirements["supported_formats"] = {"safetensors", "pt", "ckpt", "gguf"};
  2914. requirements["required_dependencies"] = {"checkpoint"};
  2915. break;
  2916. case ModelType::EMBEDDING:
  2917. requirements["min_memory_mb"] = 64;
  2918. requirements["recommended_memory_mb"] = 256;
  2919. requirements["min_disk_space_mb"] = 10;
  2920. requirements["supported_formats"] = {"safetensors", "pt"};
  2921. requirements["required_dependencies"] = {"checkpoint"};
  2922. break;
  2923. case ModelType::TAESD:
  2924. requirements["min_memory_mb"] = 256;
  2925. requirements["recommended_memory_mb"] = 512;
  2926. requirements["min_disk_space_mb"] = 100;
  2927. requirements["supported_formats"] = {"safetensors", "pth", "gguf"};
  2928. requirements["required_dependencies"] = {"checkpoint"};
  2929. break;
  2930. case ModelType::ESRGAN:
  2931. requirements["min_memory_mb"] = 1024;
  2932. requirements["recommended_memory_mb"] = 2048;
  2933. requirements["min_disk_space_mb"] = 500;
  2934. requirements["supported_formats"] = {"pth", "pt"};
  2935. requirements["optional_dependencies"] = {"checkpoint"};
  2936. break;
  2937. default:
  2938. break;
  2939. }
  2940. return requirements;
  2941. }
  2942. json Server::getRecommendedUsage(ModelType type) {
  2943. json usage = {
  2944. {"text2img", false},
  2945. {"img2img", false},
  2946. {"inpainting", false},
  2947. {"controlnet", false},
  2948. {"lora", false},
  2949. {"vae", false},
  2950. {"recommended_resolution", "512x512"},
  2951. {"recommended_steps", 20},
  2952. {"recommended_cfg_scale", 7.5},
  2953. {"recommended_batch_size", 1}
  2954. };
  2955. switch (type) {
  2956. case ModelType::CHECKPOINT:
  2957. usage = {
  2958. {"text2img", true},
  2959. {"img2img", true},
  2960. {"inpainting", true},
  2961. {"controlnet", true},
  2962. {"lora", true},
  2963. {"vae", true},
  2964. {"recommended_resolution", "512x512"},
  2965. {"recommended_steps", 20},
  2966. {"recommended_cfg_scale", 7.5},
  2967. {"recommended_batch_size", 1}
  2968. };
  2969. break;
  2970. case ModelType::LORA:
  2971. usage = {
  2972. {"text2img", true},
  2973. {"img2img", true},
  2974. {"inpainting", true},
  2975. {"controlnet", false},
  2976. {"lora", true},
  2977. {"vae", false},
  2978. {"recommended_strength", 1.0},
  2979. {"recommended_usage", "Style transfer, character customization"}
  2980. };
  2981. break;
  2982. case ModelType::CONTROLNET:
  2983. usage = {
  2984. {"text2img", false},
  2985. {"img2img", true},
  2986. {"inpainting", true},
  2987. {"controlnet", true},
  2988. {"lora", false},
  2989. {"vae", false},
  2990. {"recommended_strength", 0.9},
  2991. {"recommended_usage", "Precise control over output"}
  2992. };
  2993. break;
  2994. case ModelType::VAE:
  2995. usage = {
  2996. {"text2img", false},
  2997. {"img2img", false},
  2998. {"inpainting", false},
  2999. {"controlnet", false},
  3000. {"lora", false},
  3001. {"vae", true},
  3002. {"recommended_usage", "Improved encoding/decoding quality"}
  3003. };
  3004. break;
  3005. case ModelType::EMBEDDING:
  3006. usage = {
  3007. {"text2img", true},
  3008. {"img2img", true},
  3009. {"inpainting", true},
  3010. {"controlnet", false},
  3011. {"lora", false},
  3012. {"vae", false},
  3013. {"embedding", true},
  3014. {"recommended_usage", "Concept control, style words"}
  3015. };
  3016. break;
  3017. case ModelType::TAESD:
  3018. usage = {
  3019. {"text2img", false},
  3020. {"img2img", false},
  3021. {"inpainting", false},
  3022. {"controlnet", false},
  3023. {"lora", false},
  3024. {"vae", true},
  3025. {"recommended_usage", "Real-time decoding"}
  3026. };
  3027. break;
  3028. case ModelType::ESRGAN:
  3029. usage = {
  3030. {"text2img", false},
  3031. {"img2img", false},
  3032. {"inpainting", false},
  3033. {"controlnet", false},
  3034. {"lora", false},
  3035. {"vae", false},
  3036. {"upscaling", true},
  3037. {"recommended_usage", "Image upscaling and quality enhancement"}
  3038. };
  3039. break;
  3040. default:
  3041. break;
  3042. }
  3043. return usage;
  3044. }
  3045. std::string Server::getModelTypeFromDirectoryName(const std::string& dirName) {
  3046. if (dirName == "stable-diffusion" || dirName == "checkpoints") {
  3047. return "checkpoint";
  3048. } else if (dirName == "lora") {
  3049. return "lora";
  3050. } else if (dirName == "controlnet") {
  3051. return "controlnet";
  3052. } else if (dirName == "vae") {
  3053. return "vae";
  3054. } else if (dirName == "taesd") {
  3055. return "taesd";
  3056. } else if (dirName == "esrgan" || dirName == "upscaler") {
  3057. return "esrgan";
  3058. } else if (dirName == "embeddings" || dirName == "textual-inversion") {
  3059. return "embedding";
  3060. } else {
  3061. return "unknown";
  3062. }
  3063. }
  3064. std::string Server::getDirectoryDescription(const std::string& dirName) {
  3065. if (dirName == "stable-diffusion" || dirName == "checkpoints") {
  3066. return "Main stable diffusion model files";
  3067. } else if (dirName == "lora") {
  3068. return "LoRA adapter models for style transfer";
  3069. } else if (dirName == "controlnet") {
  3070. return "ControlNet models for precise control";
  3071. } else if (dirName == "vae") {
  3072. return "VAE models for improved encoding/decoding";
  3073. } else if (dirName == "taesd") {
  3074. return "TAESD models for real-time decoding";
  3075. } else if (dirName == "esrgan" || dirName == "upscaler") {
  3076. return "ESRGAN models for image upscaling";
  3077. } else if (dirName == "embeddings" || dirName == "textual-inversion") {
  3078. return "Text embeddings for concept control";
  3079. } else {
  3080. return "Unknown model directory";
  3081. }
  3082. }
  3083. json Server::getDirectoryContents(const std::string& dirPath) {
  3084. json contents = json::array();
  3085. try {
  3086. if (std::filesystem::exists(dirPath) && std::filesystem::is_directory(dirPath)) {
  3087. for (const auto& entry : std::filesystem::directory_iterator(dirPath)) {
  3088. if (entry.is_regular_file()) {
  3089. json file = {
  3090. {"name", entry.path().filename().string()},
  3091. {"path", entry.path().string()},
  3092. {"size", std::filesystem::file_size(entry.path())},
  3093. {"size_mb", std::filesystem::file_size(entry.path()) / (1024.0 * 1024.0)},
  3094. {"last_modified", std::chrono::duration_cast<std::chrono::seconds>(
  3095. std::filesystem::last_write_time(entry.path()).time_since_epoch()).count()}
  3096. };
  3097. contents.push_back(file);
  3098. }
  3099. }
  3100. }
  3101. } catch (const std::exception& e) {
  3102. // Return empty array if directory access fails
  3103. }
  3104. return contents;
  3105. }
  3106. json Server::getLargestModel(const std::map<std::string, ModelManager::ModelInfo>& allModels) {
  3107. json largest = json::object();
  3108. size_t maxSize = 0;
  3109. std::string largestName;
  3110. for (const auto& pair : allModels) {
  3111. if (pair.second.fileSize > maxSize) {
  3112. maxSize = pair.second.fileSize;
  3113. largestName = pair.second.name;
  3114. }
  3115. }
  3116. if (!largestName.empty()) {
  3117. largest = {
  3118. {"name", largestName},
  3119. {"size", maxSize},
  3120. {"size_mb", maxSize / (1024.0 * 1024.0)},
  3121. {"type", ModelManager::modelTypeToString(allModels.at(largestName).type)}
  3122. };
  3123. }
  3124. return largest;
  3125. }
  3126. json Server::getSmallestModel(const std::map<std::string, ModelManager::ModelInfo>& allModels) {
  3127. json smallest = json::object();
  3128. size_t minSize = SIZE_MAX;
  3129. std::string smallestName;
  3130. for (const auto& pair : allModels) {
  3131. if (pair.second.fileSize < minSize) {
  3132. minSize = pair.second.fileSize;
  3133. smallestName = pair.second.name;
  3134. }
  3135. }
  3136. if (!smallestName.empty()) {
  3137. smallest = {
  3138. {"name", smallestName},
  3139. {"size", minSize},
  3140. {"size_mb", minSize / (1024.0 * 1024.0)},
  3141. {"type", ModelManager::modelTypeToString(allModels.at(smallestName).type)}
  3142. };
  3143. }
  3144. return smallest;
  3145. }
  3146. json Server::validateModelFile(const std::string& modelPath, const std::string& modelType) {
  3147. json validation = {
  3148. {"is_valid", false},
  3149. {"errors", json::array()},
  3150. {"warnings", json::array()},
  3151. {"file_info", json::object()},
  3152. {"compatibility", json::object()},
  3153. {"recommendations", json::array()}
  3154. };
  3155. try {
  3156. if (!std::filesystem::exists(modelPath)) {
  3157. validation["errors"].push_back("File does not exist");
  3158. return validation;
  3159. }
  3160. if (!std::filesystem::is_regular_file(modelPath)) {
  3161. validation["errors"].push_back("Path is not a regular file");
  3162. return validation;
  3163. }
  3164. // Check file extension
  3165. std::string extension = std::filesystem::path(modelPath).extension().string();
  3166. if (extension.empty()) {
  3167. validation["errors"].push_back("Missing file extension");
  3168. return validation;
  3169. }
  3170. // Remove dot and convert to lowercase
  3171. if (extension[0] == '.') {
  3172. extension = extension.substr(1);
  3173. }
  3174. std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
  3175. // Validate extension based on model type
  3176. ModelType type = ModelManager::stringToModelType(modelType);
  3177. bool validExtension = false;
  3178. switch (type) {
  3179. case ModelType::CHECKPOINT:
  3180. validExtension = (extension == "safetensors" || extension == "ckpt" || extension == "gguf");
  3181. break;
  3182. case ModelType::LORA:
  3183. validExtension = (extension == "safetensors" || extension == "ckpt");
  3184. break;
  3185. case ModelType::CONTROLNET:
  3186. validExtension = (extension == "safetensors" || extension == "pth");
  3187. break;
  3188. case ModelType::VAE:
  3189. validExtension = (extension == "safetensors" || extension == "pt" || extension == "ckpt" || extension == "gguf");
  3190. break;
  3191. case ModelType::EMBEDDING:
  3192. validExtension = (extension == "safetensors" || extension == "pt");
  3193. break;
  3194. case ModelType::TAESD:
  3195. validExtension = (extension == "safetensors" || extension == "pth" || extension == "gguf");
  3196. break;
  3197. case ModelType::ESRGAN:
  3198. validExtension = (extension == "pth" || extension == "pt");
  3199. break;
  3200. default:
  3201. break;
  3202. }
  3203. if (!validExtension) {
  3204. validation["errors"].push_back("Invalid file extension for model type: " + extension);
  3205. }
  3206. // Check file size
  3207. size_t fileSize = std::filesystem::file_size(modelPath);
  3208. if (fileSize == 0) {
  3209. validation["errors"].push_back("File is empty");
  3210. } else if (fileSize > 8ULL * 1024 * 1024 * 1024) { // 8GB
  3211. validation["warnings"].push_back("Very large file may cause performance issues");
  3212. }
  3213. // Build file info
  3214. validation["file_info"] = {
  3215. {"path", modelPath},
  3216. {"size", fileSize},
  3217. {"size_mb", fileSize / (1024.0 * 1024.0)},
  3218. {"extension", extension},
  3219. {"last_modified", std::chrono::duration_cast<std::chrono::seconds>(
  3220. std::filesystem::last_write_time(modelPath).time_since_epoch()).count()}
  3221. };
  3222. // Check compatibility
  3223. validation["compatibility"] = {
  3224. {"extension_valid", validExtension},
  3225. {"size_appropriate", fileSize <= 4ULL * 1024 * 1024 * 1024}, // 4GB
  3226. {"recommended_format", "safetensors"}
  3227. };
  3228. // Add recommendations
  3229. if (!validExtension) {
  3230. validation["recommendations"].push_back("Convert to SafeTensors format for better security and performance");
  3231. }
  3232. if (fileSize > 2ULL * 1024 * 1024 * 1024) { // 2GB
  3233. validation["recommendations"].push_back("Consider using a smaller model for better performance");
  3234. }
  3235. // If no errors found, mark as valid
  3236. if (validation["errors"].empty()) {
  3237. validation["is_valid"] = true;
  3238. }
  3239. } catch (const std::exception& e) {
  3240. validation["errors"].push_back("Validation failed: " + std::string(e.what()));
  3241. }
  3242. return validation;
  3243. }
  3244. json Server::checkModelCompatibility(const ModelManager::ModelInfo& modelInfo, const std::string& systemInfo) {
  3245. json compatibility = {
  3246. {"is_compatible", true},
  3247. {"compatibility_score", 100},
  3248. {"issues", json::array()},
  3249. {"warnings", json::array()},
  3250. {"requirements", json::object()},
  3251. {"recommendations", json::array()},
  3252. {"system_info", json::object()}
  3253. };
  3254. // Check system compatibility
  3255. if (systemInfo == "auto") {
  3256. compatibility["system_info"] = {
  3257. {"cpu_cores", std::thread::hardware_concurrency()}
  3258. };
  3259. }
  3260. // Check model-specific compatibility issues
  3261. if (modelInfo.type == ModelType::CHECKPOINT) {
  3262. if (modelInfo.fileSize > 4ULL * 1024 * 1024 * 1024) { // 4GB
  3263. compatibility["warnings"].push_back("Large checkpoint model may require significant memory");
  3264. compatibility["compatibility_score"] = 80;
  3265. }
  3266. if (modelInfo.fileSize < 500 * 1024 * 1024) { // 500MB
  3267. compatibility["warnings"].push_back("Small checkpoint model may have limited capabilities");
  3268. compatibility["compatibility_score"] = 85;
  3269. }
  3270. } else if (modelInfo.type == ModelType::LORA) {
  3271. if (modelInfo.fileSize > 500 * 1024 * 1024) { // 500MB
  3272. compatibility["warnings"].push_back("Large LoRA may impact performance");
  3273. compatibility["compatibility_score"] = 75;
  3274. }
  3275. }
  3276. return compatibility;
  3277. }
  3278. json Server::calculateSpecificRequirements(const std::string& modelType, const std::string& resolution, const std::string& batchSize) {
  3279. json specific = {
  3280. {"memory_requirements", json::object()},
  3281. {"performance_impact", json::object()},
  3282. {"quality_expectations", json::object()}
  3283. };
  3284. // Parse resolution
  3285. int width = 512, height = 512;
  3286. try {
  3287. size_t xPos = resolution.find('x');
  3288. if (xPos != std::string::npos) {
  3289. width = std::stoi(resolution.substr(0, xPos));
  3290. height = std::stoi(resolution.substr(xPos + 1));
  3291. }
  3292. } catch (...) {
  3293. // Use defaults if parsing fails
  3294. }
  3295. // Parse batch size
  3296. int batch = 1;
  3297. try {
  3298. batch = std::stoi(batchSize);
  3299. } catch (...) {
  3300. // Use default if parsing fails
  3301. }
  3302. // Calculate memory requirements based on resolution and batch
  3303. size_t pixels = width * height;
  3304. size_t baseMemory = 1024 * 1024 * 1024; // 1GB base
  3305. size_t resolutionMemory = (pixels * 4) / (512 * 512); // Scale based on 512x512
  3306. size_t batchMemory = (batch - 1) * baseMemory * 0.5; // Additional memory for batch
  3307. specific["memory_requirements"] = {
  3308. {"base_memory_mb", baseMemory / (1024 * 1024)},
  3309. {"resolution_memory_mb", resolutionMemory / (1024 * 1024)},
  3310. {"batch_memory_mb", batchMemory / (1024 * 1024)},
  3311. {"total_memory_mb", (baseMemory + resolutionMemory + batchMemory) / (1024 * 1024)}
  3312. };
  3313. // Calculate performance impact
  3314. double performanceFactor = 1.0;
  3315. if (pixels > 512 * 512) {
  3316. performanceFactor = 1.5;
  3317. }
  3318. if (batch > 1) {
  3319. performanceFactor *= 1.2;
  3320. }
  3321. specific["performance_impact"] = {
  3322. {"resolution_factor", pixels > 512 * 512 ? 1.5 : 1.0},
  3323. {"batch_factor", batch > 1 ? 1.2 : 1.0},
  3324. {"overall_factor", performanceFactor}
  3325. };
  3326. return specific;
  3327. }
  3328. // Enhanced model management endpoint implementations
  3329. void Server::handleModelInfo(const httplib::Request& req, httplib::Response& res) {
  3330. std::string requestId = generateRequestId();
  3331. try {
  3332. if (!m_modelManager) {
  3333. sendErrorResponse(res, "Model manager not available", 500, "MODEL_MANAGER_UNAVAILABLE", requestId);
  3334. return;
  3335. }
  3336. // Extract model ID from URL path
  3337. std::string modelId = req.matches[1].str();
  3338. if (modelId.empty()) {
  3339. sendErrorResponse(res, "Missing model ID", 400, "MISSING_MODEL_ID", requestId);
  3340. return;
  3341. }
  3342. // Get model information
  3343. auto modelInfo = m_modelManager->getModelInfo(modelId);
  3344. if (modelInfo.name.empty()) {
  3345. sendErrorResponse(res, "Model not found", 404, "MODEL_NOT_FOUND", requestId);
  3346. return;
  3347. }
  3348. // Build comprehensive model information
  3349. json response = {
  3350. {"model", {
  3351. {"name", modelInfo.name},
  3352. {"path", modelInfo.path},
  3353. {"type", ModelManager::modelTypeToString(modelInfo.type)},
  3354. {"is_loaded", modelInfo.isLoaded},
  3355. {"file_size", modelInfo.fileSize},
  3356. {"file_size_mb", modelInfo.fileSize / (1024.0 * 1024.0)},
  3357. {"description", modelInfo.description},
  3358. {"metadata", modelInfo.metadata},
  3359. {"capabilities", getModelCapabilities(modelInfo.type)},
  3360. {"compatibility", getModelCompatibility(modelInfo)},
  3361. {"requirements", getModelRequirements(modelInfo.type)},
  3362. {"recommended_usage", getRecommendedUsage(modelInfo.type)},
  3363. {"last_modified", std::chrono::duration_cast<std::chrono::seconds>(
  3364. modelInfo.modifiedAt.time_since_epoch()).count()}
  3365. }},
  3366. {"request_id", requestId}
  3367. };
  3368. sendJsonResponse(res, response);
  3369. } catch (const std::exception& e) {
  3370. sendErrorResponse(res, std::string("Failed to get model info: ") + e.what(), 500, "MODEL_INFO_ERROR", requestId);
  3371. }
  3372. }
  3373. void Server::handleLoadModelById(const httplib::Request& req, httplib::Response& res) {
  3374. std::string requestId = generateRequestId();
  3375. try {
  3376. if (!m_modelManager) {
  3377. sendErrorResponse(res, "Model manager not available", 500, "MODEL_MANAGER_UNAVAILABLE", requestId);
  3378. return;
  3379. }
  3380. // Extract model ID from URL path (could be hash or name)
  3381. std::string modelIdentifier = req.matches[1].str();
  3382. if (modelIdentifier.empty()) {
  3383. sendErrorResponse(res, "Missing model identifier", 400, "MISSING_MODEL_ID", requestId);
  3384. return;
  3385. }
  3386. // Try to find by hash first (if it looks like a hash - 10+ hex chars)
  3387. std::string modelId = modelIdentifier;
  3388. if (modelIdentifier.length() >= 10 &&
  3389. std::all_of(modelIdentifier.begin(), modelIdentifier.end(),
  3390. [](char c) { return std::isxdigit(c); })) {
  3391. std::string foundName = m_modelManager->findModelByHash(modelIdentifier);
  3392. if (!foundName.empty()) {
  3393. modelId = foundName;
  3394. std::cout << "Resolved hash " << modelIdentifier << " to model: " << modelId << std::endl;
  3395. }
  3396. }
  3397. // Parse optional parameters from request body
  3398. json requestJson;
  3399. if (!req.body.empty()) {
  3400. try {
  3401. requestJson = json::parse(req.body);
  3402. } catch (const json::parse_error& e) {
  3403. sendErrorResponse(res, std::string("Invalid JSON: ") + e.what(), 400, "JSON_PARSE_ERROR", requestId);
  3404. return;
  3405. }
  3406. }
  3407. // Unload previous model if one is loaded
  3408. std::string previousModel;
  3409. {
  3410. std::lock_guard<std::mutex> lock(m_currentModelMutex);
  3411. previousModel = m_currentlyLoadedModel;
  3412. }
  3413. if (!previousModel.empty() && previousModel != modelId) {
  3414. std::cout << "Unloading previous model: " << previousModel << std::endl;
  3415. m_modelManager->unloadModel(previousModel);
  3416. }
  3417. // Load model
  3418. bool success = m_modelManager->loadModel(modelId);
  3419. if (success) {
  3420. // Update currently loaded model
  3421. {
  3422. std::lock_guard<std::mutex> lock(m_currentModelMutex);
  3423. m_currentlyLoadedModel = modelId;
  3424. }
  3425. auto modelInfo = m_modelManager->getModelInfo(modelId);
  3426. json response = {
  3427. {"status", "success"},
  3428. {"model", {
  3429. {"name", modelInfo.name},
  3430. {"path", modelInfo.path},
  3431. {"type", ModelManager::modelTypeToString(modelInfo.type)},
  3432. {"is_loaded", modelInfo.isLoaded}
  3433. }},
  3434. {"request_id", requestId}
  3435. };
  3436. sendJsonResponse(res, response);
  3437. } else {
  3438. sendErrorResponse(res, "Failed to load model", 400, "MODEL_LOAD_FAILED", requestId);
  3439. }
  3440. } catch (const std::exception& e) {
  3441. sendErrorResponse(res, std::string("Model load failed: ") + e.what(), 500, "MODEL_LOAD_ERROR", requestId);
  3442. }
  3443. }
  3444. void Server::handleUnloadModelById(const httplib::Request& req, httplib::Response& res) {
  3445. std::string requestId = generateRequestId();
  3446. try {
  3447. if (!m_modelManager) {
  3448. sendErrorResponse(res, "Model manager not available", 500, "MODEL_MANAGER_UNAVAILABLE", requestId);
  3449. return;
  3450. }
  3451. // Extract model ID from URL path
  3452. std::string modelId = req.matches[1].str();
  3453. if (modelId.empty()) {
  3454. sendErrorResponse(res, "Missing model ID", 400, "MISSING_MODEL_ID", requestId);
  3455. return;
  3456. }
  3457. // Unload model
  3458. bool success = m_modelManager->unloadModel(modelId);
  3459. if (success) {
  3460. // Clear currently loaded model if it matches
  3461. {
  3462. std::lock_guard<std::mutex> lock(m_currentModelMutex);
  3463. if (m_currentlyLoadedModel == modelId) {
  3464. m_currentlyLoadedModel = "";
  3465. }
  3466. }
  3467. json response = {
  3468. {"status", "success"},
  3469. {"model", {
  3470. {"name", modelId},
  3471. {"is_loaded", false}
  3472. }},
  3473. {"request_id", requestId}
  3474. };
  3475. sendJsonResponse(res, response);
  3476. } else {
  3477. sendErrorResponse(res, "Failed to unload model or model not found", 404, "MODEL_UNLOAD_FAILED", requestId);
  3478. }
  3479. } catch (const std::exception& e) {
  3480. sendErrorResponse(res, std::string("Model unload failed: ") + e.what(), 500, "MODEL_UNLOAD_ERROR", requestId);
  3481. }
  3482. }
  3483. void Server::handleModelTypes(const httplib::Request& req, httplib::Response& res) {
  3484. std::string requestId = generateRequestId();
  3485. try {
  3486. json types = {
  3487. {"model_types", {
  3488. {
  3489. {"type", "checkpoint"},
  3490. {"description", "Main stable diffusion model files for text-to-image, image-to-image, and inpainting"},
  3491. {"extensions", {"safetensors", "ckpt", "gguf"}},
  3492. {"capabilities", {"text2img", "img2img", "inpainting", "controlnet", "lora", "vae"}},
  3493. {"recommended_for", "General purpose image generation"}
  3494. },
  3495. {
  3496. {"type", "lora"},
  3497. {"description", "LoRA adapter models for style transfer and character customization"},
  3498. {"extensions", {"safetensors", "ckpt"}},
  3499. {"capabilities", {"style_transfer", "character_customization"}},
  3500. {"requires", {"checkpoint"}},
  3501. {"recommended_for", "Style modification and character-specific generation"}
  3502. },
  3503. {
  3504. {"type", "controlnet"},
  3505. {"description", "ControlNet models for precise control over output composition"},
  3506. {"extensions", {"safetensors", "pth"}},
  3507. {"capabilities", {"precise_control", "composition_control"}},
  3508. {"requires", {"checkpoint"}},
  3509. {"recommended_for", "Precise control over image generation"}
  3510. },
  3511. {
  3512. {"type", "vae"},
  3513. {"description", "VAE models for improved encoding and decoding quality"},
  3514. {"extensions", {"safetensors", "pt", "ckpt", "gguf"}},
  3515. {"capabilities", {"encoding", "decoding", "quality_improvement"}},
  3516. {"requires", {"checkpoint"}},
  3517. {"recommended_for", "Improved image quality and encoding"}
  3518. },
  3519. {
  3520. {"type", "embedding"},
  3521. {"description", "Text embeddings for concept control and style words"},
  3522. {"extensions", {"safetensors", "pt"}},
  3523. {"capabilities", {"concept_control", "style_words"}},
  3524. {"requires", {"checkpoint"}},
  3525. {"recommended_for", "Concept control and specific styles"}
  3526. },
  3527. {
  3528. {"type", "taesd"},
  3529. {"description", "TAESD models for real-time decoding"},
  3530. {"extensions", {"safetensors", "pth", "gguf"}},
  3531. {"capabilities", {"real_time_decoding", "fast_preview"}},
  3532. {"requires", {"checkpoint"}},
  3533. {"recommended_for", "Real-time applications and fast previews"}
  3534. },
  3535. {
  3536. {"type", "esrgan"},
  3537. {"description", "ESRGAN models for image upscaling and enhancement"},
  3538. {"extensions", {"pth", "pt"}},
  3539. {"capabilities", {"upscaling", "enhancement", "quality_improvement"}},
  3540. {"recommended_for", "Image upscaling and quality enhancement"}
  3541. }
  3542. }},
  3543. {"request_id", requestId}
  3544. };
  3545. sendJsonResponse(res, types);
  3546. } catch (const std::exception& e) {
  3547. sendErrorResponse(res, std::string("Failed to get model types: ") + e.what(), 500, "MODEL_TYPES_ERROR", requestId);
  3548. }
  3549. }
  3550. void Server::handleModelDirectories(const httplib::Request& req, httplib::Response& res) {
  3551. std::string requestId = generateRequestId();
  3552. try {
  3553. if (!m_modelManager) {
  3554. sendErrorResponse(res, "Model manager not available", 500, "MODEL_MANAGER_UNAVAILABLE", requestId);
  3555. return;
  3556. }
  3557. std::string modelsDir = m_modelManager->getModelsDirectory();
  3558. json directories = json::array();
  3559. // Define expected model directories
  3560. std::vector<std::string> modelDirs = {
  3561. "stable-diffusion", "checkpoints", "lora", "controlnet",
  3562. "vae", "taesd", "esrgan", "embeddings"
  3563. };
  3564. for (const auto& dirName : modelDirs) {
  3565. std::string dirPath = modelsDir + "/" + dirName;
  3566. std::string type = getModelTypeFromDirectoryName(dirName);
  3567. std::string description = getDirectoryDescription(dirName);
  3568. json dirInfo = {
  3569. {"name", dirName},
  3570. {"path", dirPath},
  3571. {"type", type},
  3572. {"description", description},
  3573. {"exists", std::filesystem::exists(dirPath) && std::filesystem::is_directory(dirPath)},
  3574. {"contents", getDirectoryContents(dirPath)}
  3575. };
  3576. directories.push_back(dirInfo);
  3577. }
  3578. json response = {
  3579. {"models_directory", modelsDir},
  3580. {"directories", directories},
  3581. {"request_id", requestId}
  3582. };
  3583. sendJsonResponse(res, response);
  3584. } catch (const std::exception& e) {
  3585. sendErrorResponse(res, std::string("Failed to get model directories: ") + e.what(), 500, "MODEL_DIRECTORIES_ERROR", requestId);
  3586. }
  3587. }
  3588. void Server::handleRefreshModels(const httplib::Request& req, httplib::Response& res) {
  3589. std::string requestId = generateRequestId();
  3590. try {
  3591. if (!m_modelManager) {
  3592. sendErrorResponse(res, "Model manager not available", 500, "MODEL_MANAGER_UNAVAILABLE", requestId);
  3593. return;
  3594. }
  3595. // Force refresh of model cache
  3596. bool success = m_modelManager->scanModelsDirectory();
  3597. if (success) {
  3598. json response = {
  3599. {"status", "success"},
  3600. {"message", "Model cache refreshed successfully"},
  3601. {"models_found", m_modelManager->getAvailableModelsCount()},
  3602. {"models_loaded", m_modelManager->getLoadedModelsCount()},
  3603. {"models_directory", m_modelManager->getModelsDirectory()},
  3604. {"request_id", requestId}
  3605. };
  3606. sendJsonResponse(res, response);
  3607. } else {
  3608. sendErrorResponse(res, "Failed to refresh model cache", 500, "MODEL_REFRESH_FAILED", requestId);
  3609. }
  3610. } catch (const std::exception& e) {
  3611. sendErrorResponse(res, std::string("Model refresh failed: ") + e.what(), 500, "MODEL_REFRESH_ERROR", requestId);
  3612. }
  3613. }
  3614. void Server::handleHashModels(const httplib::Request& req, httplib::Response& res) {
  3615. std::string requestId = generateRequestId();
  3616. try {
  3617. if (!m_generationQueue || !m_modelManager) {
  3618. sendErrorResponse(res, "Services not available", 500, "SERVICE_UNAVAILABLE", requestId);
  3619. return;
  3620. }
  3621. // Parse request body
  3622. json requestJson;
  3623. if (!req.body.empty()) {
  3624. requestJson = json::parse(req.body);
  3625. }
  3626. HashRequest hashReq;
  3627. hashReq.id = requestId;
  3628. hashReq.forceRehash = requestJson.value("force_rehash", false);
  3629. if (requestJson.contains("models") && requestJson["models"].is_array()) {
  3630. for (const auto& model : requestJson["models"]) {
  3631. hashReq.modelNames.push_back(model.get<std::string>());
  3632. }
  3633. }
  3634. // Enqueue hash request
  3635. auto future = m_generationQueue->enqueueHashRequest(hashReq);
  3636. json response = {
  3637. {"request_id", requestId},
  3638. {"status", "queued"},
  3639. {"message", "Hash job queued successfully"},
  3640. {"models_to_hash", hashReq.modelNames.empty() ? "all_unhashed" : std::to_string(hashReq.modelNames.size())}
  3641. };
  3642. sendJsonResponse(res, response, 202);
  3643. } catch (const json::parse_error& e) {
  3644. sendErrorResponse(res, std::string("Invalid JSON: ") + e.what(), 400, "JSON_PARSE_ERROR", requestId);
  3645. } catch (const std::exception& e) {
  3646. sendErrorResponse(res, std::string("Hash request failed: ") + e.what(), 500, "INTERNAL_ERROR", requestId);
  3647. }
  3648. }
  3649. void Server::handleConvertModel(const httplib::Request& req, httplib::Response& res) {
  3650. std::string requestId = generateRequestId();
  3651. try {
  3652. if (!m_generationQueue || !m_modelManager) {
  3653. sendErrorResponse(res, "Services not available", 500, "SERVICE_UNAVAILABLE", requestId);
  3654. return;
  3655. }
  3656. // Parse request body
  3657. json requestJson;
  3658. try {
  3659. requestJson = json::parse(req.body);
  3660. } catch (const json::parse_error& e) {
  3661. sendErrorResponse(res, std::string("Invalid JSON: ") + e.what(), 400, "JSON_PARSE_ERROR", requestId);
  3662. return;
  3663. }
  3664. // Validate required fields
  3665. if (!requestJson.contains("model_name")) {
  3666. sendErrorResponse(res, "Missing required field: model_name", 400, "MISSING_FIELD", requestId);
  3667. return;
  3668. }
  3669. if (!requestJson.contains("quantization_type")) {
  3670. sendErrorResponse(res, "Missing required field: quantization_type", 400, "MISSING_FIELD", requestId);
  3671. return;
  3672. }
  3673. std::string modelName = requestJson["model_name"].get<std::string>();
  3674. std::string quantizationType = requestJson["quantization_type"].get<std::string>();
  3675. // Validate quantization type
  3676. const std::vector<std::string> validTypes = {"f32", "f16", "q4_0", "q4_1", "q5_0", "q5_1", "q8_0", "q2_K", "q3_K", "q4_K"};
  3677. if (std::find(validTypes.begin(), validTypes.end(), quantizationType) == validTypes.end()) {
  3678. sendErrorResponse(res, "Invalid quantization_type. Valid types: f32, f16, q4_0, q4_1, q5_0, q5_1, q8_0, q2_K, q3_K, q4_K",
  3679. 400, "INVALID_QUANTIZATION_TYPE", requestId);
  3680. return;
  3681. }
  3682. // Get model info to find the full path
  3683. auto modelInfo = m_modelManager->getModelInfo(modelName);
  3684. if (modelInfo.name.empty()) {
  3685. sendErrorResponse(res, "Model not found: " + modelName, 404, "MODEL_NOT_FOUND", requestId);
  3686. return;
  3687. }
  3688. // Check if model is already GGUF
  3689. if (modelInfo.fullPath.find(".gguf") != std::string::npos) {
  3690. sendErrorResponse(res, "Model is already in GGUF format. Cannot convert GGUF to GGUF.",
  3691. 400, "ALREADY_GGUF", requestId);
  3692. return;
  3693. }
  3694. // Build output path
  3695. std::string outputPath = requestJson.value("output_path", "");
  3696. if (outputPath.empty()) {
  3697. // Generate default output path: model_name_quantization.gguf
  3698. namespace fs = std::filesystem;
  3699. fs::path inputPath(modelInfo.fullPath);
  3700. std::string baseName = inputPath.stem().string();
  3701. std::string outputDir = inputPath.parent_path().string();
  3702. outputPath = outputDir + "/" + baseName + "_" + quantizationType + ".gguf";
  3703. }
  3704. // Create conversion request
  3705. ConversionRequest convReq;
  3706. convReq.id = requestId;
  3707. convReq.modelName = modelName;
  3708. convReq.modelPath = modelInfo.fullPath;
  3709. convReq.outputPath = outputPath;
  3710. convReq.quantizationType = quantizationType;
  3711. // Enqueue conversion request
  3712. auto future = m_generationQueue->enqueueConversionRequest(convReq);
  3713. json response = {
  3714. {"request_id", requestId},
  3715. {"status", "queued"},
  3716. {"message", "Model conversion queued successfully"},
  3717. {"model_name", modelName},
  3718. {"input_path", modelInfo.fullPath},
  3719. {"output_path", outputPath},
  3720. {"quantization_type", quantizationType}
  3721. };
  3722. sendJsonResponse(res, response, 202);
  3723. } catch (const std::exception& e) {
  3724. sendErrorResponse(res, std::string("Conversion request failed: ") + e.what(), 500, "INTERNAL_ERROR", requestId);
  3725. }
  3726. }
  3727. void Server::handleModelStats(const httplib::Request& req, httplib::Response& res) {
  3728. std::string requestId = generateRequestId();
  3729. try {
  3730. if (!m_modelManager) {
  3731. sendErrorResponse(res, "Model manager not available", 500, "MODEL_MANAGER_UNAVAILABLE", requestId);
  3732. return;
  3733. }
  3734. auto allModels = m_modelManager->getAllModels();
  3735. json response = {
  3736. {"statistics", {
  3737. {"total_models", allModels.size()},
  3738. {"loaded_models", m_modelManager->getLoadedModelsCount()},
  3739. {"available_models", m_modelManager->getAvailableModelsCount()},
  3740. {"model_types", getModelTypeStatistics()},
  3741. {"largest_model", getLargestModel(allModels)},
  3742. {"smallest_model", getSmallestModel(allModels)}
  3743. }},
  3744. {"request_id", requestId}
  3745. };
  3746. sendJsonResponse(res, response);
  3747. } catch (const std::exception& e) {
  3748. sendErrorResponse(res, std::string("Failed to get model stats: ") + e.what(), 500, "MODEL_STATS_ERROR", requestId);
  3749. }
  3750. }
  3751. void Server::handleBatchModels(const httplib::Request& req, httplib::Response& res) {
  3752. std::string requestId = generateRequestId();
  3753. try {
  3754. if (!m_modelManager) {
  3755. sendErrorResponse(res, "Model manager not available", 500, "MODEL_MANAGER_UNAVAILABLE", requestId);
  3756. return;
  3757. }
  3758. // Parse JSON request body
  3759. json requestJson = json::parse(req.body);
  3760. if (!requestJson.contains("operation") || !requestJson["operation"].is_string()) {
  3761. sendErrorResponse(res, "Missing or invalid 'operation' field", 400, "INVALID_OPERATION", requestId);
  3762. return;
  3763. }
  3764. if (!requestJson.contains("models") || !requestJson["models"].is_array()) {
  3765. sendErrorResponse(res, "Missing or invalid 'models' field", 400, "INVALID_MODELS", requestId);
  3766. return;
  3767. }
  3768. std::string operation = requestJson["operation"];
  3769. json models = requestJson["models"];
  3770. json results = json::array();
  3771. for (const auto& model : models) {
  3772. if (!model.is_string()) {
  3773. results.push_back({
  3774. {"model", model},
  3775. {"success", false},
  3776. {"error", "Invalid model name"}
  3777. });
  3778. continue;
  3779. }
  3780. std::string modelName = model;
  3781. bool success = false;
  3782. std::string error = "";
  3783. if (operation == "load") {
  3784. success = m_modelManager->loadModel(modelName);
  3785. if (!success) error = "Failed to load model";
  3786. } else if (operation == "unload") {
  3787. success = m_modelManager->unloadModel(modelName);
  3788. if (!success) error = "Failed to unload model";
  3789. } else {
  3790. error = "Unsupported operation";
  3791. }
  3792. results.push_back({
  3793. {"model", modelName},
  3794. {"success", success},
  3795. {"error", error.empty() ? json(nullptr) : json(error)}
  3796. });
  3797. }
  3798. json response = {
  3799. {"operation", operation},
  3800. {"results", results},
  3801. {"successful_count", std::count_if(results.begin(), results.end(),
  3802. [](const json& result) { return result["success"].get<bool>(); })},
  3803. {"failed_count", std::count_if(results.begin(), results.end(),
  3804. [](const json& result) { return !result["success"].get<bool>(); })},
  3805. {"request_id", requestId}
  3806. };
  3807. sendJsonResponse(res, response);
  3808. } catch (const json::parse_error& e) {
  3809. sendErrorResponse(res, std::string("Invalid JSON: ") + e.what(), 400, "JSON_PARSE_ERROR", requestId);
  3810. } catch (const std::exception& e) {
  3811. sendErrorResponse(res, std::string("Batch operation failed: ") + e.what(), 500, "BATCH_OPERATION_ERROR", requestId);
  3812. }
  3813. }
  3814. void Server::handleValidateModel(const httplib::Request& req, httplib::Response& res) {
  3815. std::string requestId = generateRequestId();
  3816. try {
  3817. // Parse JSON request body
  3818. json requestJson = json::parse(req.body);
  3819. if (!requestJson.contains("model_path") || !requestJson["model_path"].is_string()) {
  3820. sendErrorResponse(res, "Missing or invalid 'model_path' field", 400, "INVALID_MODEL_PATH", requestId);
  3821. return;
  3822. }
  3823. std::string modelPath = requestJson["model_path"];
  3824. std::string modelType = requestJson.value("model_type", "checkpoint");
  3825. // Validate model file
  3826. json validation = validateModelFile(modelPath, modelType);
  3827. json response = {
  3828. {"validation", validation},
  3829. {"request_id", requestId}
  3830. };
  3831. sendJsonResponse(res, response);
  3832. } catch (const json::parse_error& e) {
  3833. sendErrorResponse(res, std::string("Invalid JSON: ") + e.what(), 400, "JSON_PARSE_ERROR", requestId);
  3834. } catch (const std::exception& e) {
  3835. sendErrorResponse(res, std::string("Model validation failed: ") + e.what(), 500, "MODEL_VALIDATION_ERROR", requestId);
  3836. }
  3837. }
  3838. void Server::handleCheckCompatibility(const httplib::Request& req, httplib::Response& res) {
  3839. std::string requestId = generateRequestId();
  3840. try {
  3841. if (!m_modelManager) {
  3842. sendErrorResponse(res, "Model manager not available", 500, "MODEL_MANAGER_UNAVAILABLE", requestId);
  3843. return;
  3844. }
  3845. // Parse JSON request body
  3846. json requestJson = json::parse(req.body);
  3847. if (!requestJson.contains("model_name") || !requestJson["model_name"].is_string()) {
  3848. sendErrorResponse(res, "Missing or invalid 'model_name' field", 400, "INVALID_MODEL_NAME", requestId);
  3849. return;
  3850. }
  3851. std::string modelName = requestJson["model_name"];
  3852. std::string systemInfo = requestJson.value("system_info", "auto");
  3853. // Get model information
  3854. auto modelInfo = m_modelManager->getModelInfo(modelName);
  3855. if (modelInfo.name.empty()) {
  3856. sendErrorResponse(res, "Model not found", 404, "MODEL_NOT_FOUND", requestId);
  3857. return;
  3858. }
  3859. // Check compatibility
  3860. json compatibility = checkModelCompatibility(modelInfo, systemInfo);
  3861. json response = {
  3862. {"model", modelName},
  3863. {"compatibility", compatibility},
  3864. {"request_id", requestId}
  3865. };
  3866. sendJsonResponse(res, response);
  3867. } catch (const json::parse_error& e) {
  3868. sendErrorResponse(res, std::string("Invalid JSON: ") + e.what(), 400, "JSON_PARSE_ERROR", requestId);
  3869. } catch (const std::exception& e) {
  3870. sendErrorResponse(res, std::string("Compatibility check failed: ") + e.what(), 500, "COMPATIBILITY_CHECK_ERROR", requestId);
  3871. }
  3872. }
  3873. void Server::handleModelRequirements(const httplib::Request& req, httplib::Response& res) {
  3874. std::string requestId = generateRequestId();
  3875. try {
  3876. // Parse JSON request body
  3877. json requestJson = json::parse(req.body);
  3878. std::string modelType = requestJson.value("model_type", "checkpoint");
  3879. std::string resolution = requestJson.value("resolution", "512x512");
  3880. std::string batchSize = requestJson.value("batch_size", "1");
  3881. // Calculate specific requirements
  3882. json requirements = calculateSpecificRequirements(modelType, resolution, batchSize);
  3883. // Get general requirements for model type
  3884. ModelType type = ModelManager::stringToModelType(modelType);
  3885. json generalRequirements = getModelRequirements(type);
  3886. json response = {
  3887. {"model_type", modelType},
  3888. {"configuration", {
  3889. {"resolution", resolution},
  3890. {"batch_size", batchSize}
  3891. }},
  3892. {"specific_requirements", requirements},
  3893. {"general_requirements", generalRequirements},
  3894. {"request_id", requestId}
  3895. };
  3896. sendJsonResponse(res, response);
  3897. } catch (const json::parse_error& e) {
  3898. sendErrorResponse(res, std::string("Invalid JSON: ") + e.what(), 400, "JSON_PARSE_ERROR", requestId);
  3899. } catch (const std::exception& e) {
  3900. sendErrorResponse(res, std::string("Requirements calculation failed: ") + e.what(), 500, "REQUIREMENTS_ERROR", requestId);
  3901. }
  3902. }
  3903. void Server::serverThreadFunction(const std::string& host, int port) {
  3904. try {
  3905. std::cout << "Server thread starting, attempting to bind to " << host << ":" << port << std::endl;
  3906. // Check if port is available before attempting to bind
  3907. std::cout << "Checking if port " << port << " is available..." << std::endl;
  3908. // Try to create a test socket to check if port is in use
  3909. int test_socket = socket(AF_INET, SOCK_STREAM, 0);
  3910. if (test_socket >= 0) {
  3911. // Set SO_REUSEADDR to avoid TIME_WAIT issues
  3912. int opt = 1;
  3913. setsockopt(test_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  3914. struct sockaddr_in addr;
  3915. addr.sin_family = AF_INET;
  3916. addr.sin_port = htons(port);
  3917. addr.sin_addr.s_addr = INADDR_ANY;
  3918. // Try to bind to the port
  3919. if (bind(test_socket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
  3920. close(test_socket);
  3921. std::cerr << "ERROR: Port " << port << " is already in use! Cannot start server." << std::endl;
  3922. std::cerr << "Please stop the existing instance or use a different port." << std::endl;
  3923. m_isRunning.store(false);
  3924. m_startupFailed.store(true);
  3925. return;
  3926. }
  3927. close(test_socket);
  3928. }
  3929. std::cout << "Port " << port << " is available, proceeding with server startup..." << std::endl;
  3930. std::cout << "Calling listen()..." << std::endl;
  3931. // Set up a flag to track if listen started successfully
  3932. std::atomic<bool> listenStarted{false};
  3933. // We need to set m_isRunning after successful bind but before blocking
  3934. // cpp-httplib doesn't provide a callback, so we set it optimistically
  3935. // and clear it if listen() returns false
  3936. m_isRunning.store(true);
  3937. bool listenResult = m_httpServer->listen(host.c_str(), port);
  3938. std::cout << "listen() returned: " << (listenResult ? "true" : "false") << std::endl;
  3939. // If we reach here, server has stopped (either normally or due to error)
  3940. m_isRunning.store(false);
  3941. if (!listenResult) {
  3942. std::cerr << "Server listen failed! This usually means port is in use or permission denied." << std::endl;
  3943. }
  3944. } catch (const std::exception& e) {
  3945. std::cerr << "Exception in server thread: " << e.what() << std::endl;
  3946. m_isRunning.store(false);
  3947. }
  3948. }