api.ts 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. // API client for stable-diffusion REST API
  2. // Type for server config injected by the server
  3. declare global {
  4. interface Window {
  5. __SERVER_CONFIG__?: {
  6. apiUrl: string;
  7. apiBasePath: string;
  8. host: string;
  9. port: number;
  10. authMethod: "none" | "unix" | "jwt";
  11. authEnabled: boolean;
  12. };
  13. }
  14. }
  15. // Request throttling to prevent excessive API calls
  16. class RequestThrottler {
  17. private requests: Map<string, { count: number; resetTime: number }> =
  18. new Map();
  19. private maxRequests: number = 10; // Max requests per time window
  20. private timeWindow: number = 1000; // Time window in milliseconds
  21. canMakeRequest(key: string): boolean {
  22. const now = Date.now();
  23. const request = this.requests.get(key);
  24. if (!request || now >= request.resetTime) {
  25. this.requests.set(key, { count: 1, resetTime: now + this.timeWindow });
  26. return true;
  27. }
  28. if (request.count >= this.maxRequests) {
  29. return false;
  30. }
  31. request.count++;
  32. return true;
  33. }
  34. getWaitTime(key: string): number {
  35. const request = this.requests.get(key);
  36. if (!request) return 0;
  37. const now = Date.now();
  38. if (now >= request.resetTime) return 0;
  39. return request.resetTime - now;
  40. }
  41. }
  42. // Global throttler instance
  43. const throttler = new RequestThrottler();
  44. // Debounce utility for frequent calls
  45. function _debounce<T extends (...args: unknown[]) => unknown>(
  46. func: T,
  47. wait: number,
  48. immediate?: boolean,
  49. ): (...args: Parameters<T>) => void {
  50. let timeout: NodeJS.Timeout | null = null;
  51. return function executedFunction(...args: Parameters<T>) {
  52. const later = () => {
  53. timeout = null;
  54. if (!immediate) func(...args);
  55. };
  56. const callNow = immediate && !timeout;
  57. if (timeout) clearTimeout(timeout);
  58. timeout = setTimeout(later, wait);
  59. if (callNow) func(...args);
  60. };
  61. }
  62. // Cache for API responses to reduce redundant calls
  63. class ApiCache {
  64. private cache: Map<string, { data: unknown; timestamp: number; ttl: number }> =
  65. new Map();
  66. private defaultTtl: number = 5000; // 5 seconds default TTL
  67. set(key: string, data: unknown, ttl?: number): void {
  68. this.cache.set(key, {
  69. data,
  70. timestamp: Date.now(),
  71. ttl: ttl || this.defaultTtl,
  72. });
  73. }
  74. get(key: string): unknown | null {
  75. const cached = this.cache.get(key);
  76. if (!cached) return null;
  77. if (Date.now() - cached.timestamp > cached.ttl) {
  78. this.cache.delete(key);
  79. return null;
  80. }
  81. return cached.data;
  82. }
  83. clear(): void {
  84. this.cache.clear();
  85. }
  86. delete(key: string): void {
  87. this.cache.delete(key);
  88. }
  89. }
  90. const cache = new ApiCache();
  91. // Get configuration from server-injected config or fallback to environment/defaults
  92. // This function is called at runtime to ensure __SERVER_CONFIG__ is available
  93. function getApiConfig() {
  94. if (typeof window !== "undefined" && window.__SERVER_CONFIG__) {
  95. let apiUrl = window.__SERVER_CONFIG__.apiUrl;
  96. // Fix 0.0.0.0 host to use actual browser host
  97. if (apiUrl && apiUrl.includes("0.0.0.0")) {
  98. const protocol = window.location.protocol;
  99. const host = window.location.hostname;
  100. const port = window.location.port;
  101. apiUrl = `${protocol}//${host}${port ? ":" + port : ""}`;
  102. }
  103. return {
  104. apiUrl,
  105. apiBase: window.__SERVER_CONFIG__.apiBasePath,
  106. };
  107. }
  108. // Fallback for development mode - use current window location
  109. if (typeof window !== "undefined") {
  110. const protocol = window.location.protocol;
  111. const host = window.location.hostname;
  112. const port = window.location.port;
  113. return {
  114. apiUrl: `${protocol}//${host}:${port}`,
  115. apiBase: "/api",
  116. };
  117. }
  118. // Server-side fallback
  119. return {
  120. apiUrl: process.env.NEXT_PUBLIC_API_URL || "http://localhost:8081",
  121. apiBase: process.env.NEXT_PUBLIC_API_BASE_PATH || "/api",
  122. };
  123. }
  124. export interface GenerationRequest {
  125. model?: string;
  126. prompt: string;
  127. negative_prompt?: string;
  128. width?: number;
  129. height?: number;
  130. steps?: number;
  131. cfg_scale?: number;
  132. seed?: string;
  133. sampling_method?: string;
  134. scheduler?: string;
  135. batch_count?: number;
  136. clip_skip?: number;
  137. strength?: number;
  138. control_strength?: number;
  139. }
  140. export interface JobInfo {
  141. id?: string;
  142. request_id?: string;
  143. status:
  144. | "pending"
  145. | "loading"
  146. | "processing"
  147. | "completed"
  148. | "failed"
  149. | "cancelled"
  150. | "queued";
  151. progress?: number;
  152. model_load_progress?: number;
  153. generation_progress?: number;
  154. overall_progress?: number;
  155. result?: {
  156. images: string[];
  157. };
  158. outputs?: Array<{
  159. filename: string;
  160. url: string;
  161. path: string;
  162. }>;
  163. error?: string;
  164. created_at?: string;
  165. updated_at?: string;
  166. message?: string;
  167. queue_position?: number;
  168. prompt?: string;
  169. end_time?: number;
  170. start_time?: number;
  171. queued_time?: number;
  172. error_message?: string;
  173. position?: number;
  174. }
  175. // API response wrapper for job details
  176. export interface JobDetailsResponse {
  177. job: JobInfo;
  178. }
  179. export interface ModelInfo {
  180. id?: string;
  181. name: string;
  182. path?: string;
  183. type: string;
  184. size?: number;
  185. file_size?: number;
  186. file_size_mb?: number;
  187. sha256?: string | null;
  188. sha256_short?: string | null;
  189. loaded?: boolean;
  190. architecture?: string;
  191. required_models?: RequiredModelInfo[];
  192. recommended_vae?: RecommendedModelInfo;
  193. recommended_textual_inversions?: RecommendedModelInfo[];
  194. recommended_loras?: RecommendedModelInfo[];
  195. metadata?: Record<string, unknown>;
  196. }
  197. export interface RequiredModelInfo {
  198. type: string;
  199. name?: string;
  200. description?: string;
  201. optional?: boolean;
  202. priority?: number;
  203. }
  204. export interface RecommendedModelInfo {
  205. type: string;
  206. name?: string;
  207. description?: string;
  208. reason?: string;
  209. }
  210. export interface AutoSelectionState {
  211. selectedModels: Record<string, string>; // modelType -> modelName
  212. autoSelectedModels: Record<string, string>; // modelType -> modelName
  213. missingModels: string[]; // modelType names
  214. warnings: string[];
  215. errors: string[];
  216. isAutoSelecting: boolean;
  217. }
  218. export interface EnhancedModelsResponse {
  219. models: ModelInfo[];
  220. pagination: {
  221. page: number;
  222. limit: number;
  223. total_count: number;
  224. total_pages: number;
  225. has_next: boolean;
  226. has_prev: boolean;
  227. };
  228. statistics: Record<string, unknown>;
  229. auto_selection?: AutoSelectionState;
  230. }
  231. export interface QueueStatus {
  232. active_generations: number;
  233. jobs: JobInfo[];
  234. running: boolean;
  235. size: number;
  236. }
  237. export interface HealthStatus {
  238. status: "ok" | "error" | "degraded";
  239. message: string;
  240. timestamp: string;
  241. uptime?: number;
  242. version?: string;
  243. }
  244. export interface VersionInfo {
  245. version: string;
  246. type: string;
  247. commit: {
  248. short: string;
  249. full: string;
  250. };
  251. branch: string;
  252. clean: boolean;
  253. build_time: string;
  254. }
  255. class ApiClient {
  256. private baseUrl: string = "";
  257. private isInitialized: boolean = false;
  258. // Initialize base URL
  259. private initBaseUrl(): string {
  260. if (!this.isInitialized) {
  261. const config = getApiConfig();
  262. this.baseUrl = `${config.apiUrl}${config.apiBase}`;
  263. this.isInitialized = true;
  264. }
  265. return this.baseUrl;
  266. }
  267. // Get base URL dynamically at runtime to ensure server config is loaded
  268. private getBaseUrl(): string {
  269. return this.initBaseUrl();
  270. }
  271. private async request<T>(
  272. endpoint: string,
  273. options: RequestInit = {},
  274. ): Promise<T> {
  275. const url = `${this.getBaseUrl()}${endpoint}`;
  276. // Check request throttling for certain endpoints
  277. const needsThrottling =
  278. endpoint.includes("/queue/status") || endpoint.includes("/health");
  279. if (needsThrottling) {
  280. const waitTime = throttler.getWaitTime(endpoint);
  281. if (waitTime > 0) {
  282. // Wait before making the request
  283. await new Promise((resolve) => setTimeout(resolve, waitTime));
  284. }
  285. if (!throttler.canMakeRequest(endpoint)) {
  286. throw new Error(
  287. "Too many requests. Please wait before making another request.",
  288. );
  289. }
  290. }
  291. // Get authentication method from server config
  292. const authMethod =
  293. typeof window !== "undefined" && window.__SERVER_CONFIG__
  294. ? window.__SERVER_CONFIG__.authMethod
  295. : "jwt";
  296. // Add auth token or Unix user header based on auth method
  297. const token =
  298. typeof window !== "undefined" ? localStorage.getItem("auth_token") : null;
  299. const unixUser =
  300. typeof window !== "undefined" ? localStorage.getItem("unix_user") : null;
  301. const headers: Record<string, string> = {
  302. "Content-Type": "application/json",
  303. ...(options.headers as Record<string, string>),
  304. };
  305. if (authMethod === "unix" && unixUser) {
  306. // For Unix auth, send username in X-Unix-User header
  307. headers["X-Unix-User"] = unixUser;
  308. } else if (token) {
  309. // For JWT auth, send the token in Authorization header
  310. headers["Authorization"] = `Bearer ${token}`;
  311. }
  312. const response = await fetch(url, {
  313. ...options,
  314. headers,
  315. });
  316. if (!response.ok) {
  317. const errorData = await response.json().catch(() => ({
  318. error: { message: response.statusText },
  319. }));
  320. // Handle nested error structure: { error: { message: "..." } }
  321. const errorMessage =
  322. errorData.error?.message ||
  323. errorData.message ||
  324. errorData.error ||
  325. "API request failed";
  326. throw new Error(errorMessage);
  327. }
  328. return response.json();
  329. }
  330. // Enhanced health check with caching and better error handling
  331. async checkHealth(): Promise<HealthStatus> {
  332. const cacheKey = "health_check";
  333. const cachedResult = cache.get(cacheKey) as HealthStatus | null;
  334. if (cachedResult) {
  335. return cachedResult;
  336. }
  337. const endpoints = ["/queue/status", "/health", "/status", "/"];
  338. for (const endpoint of endpoints) {
  339. try {
  340. const response = await fetch(`${this.getBaseUrl()}${endpoint}`, {
  341. method: "GET",
  342. headers: {
  343. "Content-Type": "application/json",
  344. },
  345. // Add timeout to prevent hanging requests
  346. signal: AbortSignal.timeout(3000), // Reduced timeout
  347. });
  348. if (response.ok) {
  349. const data = await response.json();
  350. // For queue status, consider it healthy if it returns valid structure
  351. if (endpoint === "/queue/status" && data.queue) {
  352. const result = {
  353. status: "ok" as const,
  354. message: "API is running and queue is accessible",
  355. timestamp: new Date().toISOString(),
  356. };
  357. cache.set(cacheKey, result, 10000); // Cache for 10 seconds
  358. return result;
  359. }
  360. // For other health endpoints
  361. const healthStatus: HealthStatus = {
  362. status: "ok",
  363. message: "API is running",
  364. timestamp: new Date().toISOString(),
  365. uptime: data.uptime,
  366. version: data.version || data.build || data.git_version,
  367. };
  368. // Handle different response formats
  369. if (data.status) {
  370. if (
  371. data.status === "healthy" ||
  372. data.status === "running" ||
  373. data.status === "ok"
  374. ) {
  375. healthStatus.status = "ok";
  376. healthStatus.message = data.message || "API is running";
  377. } else if (data.status === "degraded") {
  378. healthStatus.status = "degraded";
  379. healthStatus.message =
  380. data.message || "API is running in degraded mode";
  381. } else {
  382. healthStatus.status = "error";
  383. healthStatus.message = data.message || "API status is unknown";
  384. }
  385. }
  386. cache.set(cacheKey, healthStatus, 10000); // Cache for 10 seconds
  387. return healthStatus;
  388. }
  389. } catch (error) {
  390. // Continue to next endpoint if this one fails
  391. console.warn(`Health check failed for endpoint ${endpoint}:`, error);
  392. continue;
  393. }
  394. }
  395. // If all endpoints fail
  396. throw new Error("All health check endpoints are unavailable");
  397. }
  398. // Alternative simple connectivity check with caching
  399. async checkConnectivity(): Promise<boolean> {
  400. const cacheKey = "connectivity_check";
  401. const cachedResult = cache.get(cacheKey) as boolean | undefined;
  402. if (cachedResult !== undefined) {
  403. return cachedResult;
  404. }
  405. try {
  406. const response = await fetch(`${this.getBaseUrl()}`, {
  407. method: "HEAD",
  408. signal: AbortSignal.timeout(2000), // Reduced timeout
  409. });
  410. const result = response.ok || response.status < 500;
  411. cache.set(cacheKey, result, 5000); // Cache for 5 seconds
  412. return result;
  413. } catch {
  414. cache.set(cacheKey, false, 5000); // Cache failure for 5 seconds
  415. return false;
  416. }
  417. }
  418. // Generation endpoints
  419. async generateImage(params: GenerationRequest): Promise<JobInfo> {
  420. return this.request<JobInfo>("/generate/text2img", {
  421. method: "POST",
  422. body: JSON.stringify(params),
  423. });
  424. }
  425. async text2img(params: GenerationRequest): Promise<JobInfo> {
  426. return this.request<JobInfo>("/generate/text2img", {
  427. method: "POST",
  428. body: JSON.stringify(params),
  429. });
  430. }
  431. async img2img(
  432. params: GenerationRequest & { image: string },
  433. ): Promise<JobInfo> {
  434. // Convert frontend field name to backend field name
  435. const backendParams = {
  436. ...params,
  437. init_image: params.image,
  438. image: undefined, // Remove the frontend field
  439. };
  440. return this.request<JobInfo>("/generate/img2img", {
  441. method: "POST",
  442. body: JSON.stringify(backendParams),
  443. });
  444. }
  445. async inpainting(
  446. params: GenerationRequest & { source_image: string; mask_image: string },
  447. ): Promise<JobInfo> {
  448. return this.request<JobInfo>("/generate/inpainting", {
  449. method: "POST",
  450. body: JSON.stringify(params),
  451. });
  452. }
  453. async upscale(
  454. params: {
  455. image: string;
  456. model: string;
  457. upscale_factor: number;
  458. }
  459. ): Promise<JobInfo> {
  460. // Map 'model' to 'esrgan_model' as expected by the API
  461. const apiParams = {
  462. image: params.image,
  463. esrgan_model: params.model,
  464. upscale_factor: params.upscale_factor,
  465. };
  466. return this.request<JobInfo>("/generate/upscale", {
  467. method: "POST",
  468. body: JSON.stringify(apiParams),
  469. });
  470. }
  471. // Job management with caching for status checks
  472. async getJobStatus(jobId: string): Promise<JobDetailsResponse> {
  473. const cacheKey = `job_status_${jobId}`;
  474. const cachedResult = cache.get(cacheKey) as JobDetailsResponse | null;
  475. if (cachedResult) {
  476. return cachedResult;
  477. }
  478. const result = await this.request<JobDetailsResponse>(
  479. `/queue/job/${jobId}`,
  480. );
  481. // Cache job status for a short time
  482. if (result.job.status === "processing" || result.job.status === "queued") {
  483. cache.set(cacheKey, result, 2000); // Cache for 2 seconds for active jobs
  484. } else {
  485. cache.set(cacheKey, result, 10000); // Cache for 10 seconds for completed jobs
  486. }
  487. return result;
  488. }
  489. // Get authenticated image URL with cache-busting
  490. getImageUrl(jobId: string, filename: string): string {
  491. const baseUrl = this.getBaseUrl();
  492. // Add cache-busting timestamp
  493. const timestamp = Date.now();
  494. const url = `${baseUrl}/queue/job/${jobId}/output/${filename}?t=${timestamp}`;
  495. return url;
  496. }
  497. // Download image with authentication
  498. async downloadImage(jobId: string, filename: string): Promise<Blob> {
  499. const url = this.getImageUrl(jobId, filename);
  500. // Get authentication method from server config
  501. const authMethod =
  502. typeof window !== "undefined" && window.__SERVER_CONFIG__
  503. ? window.__SERVER_CONFIG__.authMethod
  504. : "jwt";
  505. // Add auth token or Unix user header based on auth method
  506. const token =
  507. typeof window !== "undefined" ? localStorage.getItem("auth_token") : null;
  508. const unixUser =
  509. typeof window !== "undefined" ? localStorage.getItem("unix_user") : null;
  510. const headers: Record<string, string> = {};
  511. if (authMethod === "unix" && unixUser) {
  512. // For Unix auth, send the username in X-Unix-User header
  513. headers["X-Unix-User"] = unixUser;
  514. } else if (token) {
  515. // For JWT auth, send the token in Authorization header
  516. headers["Authorization"] = `Bearer ${token}`;
  517. }
  518. const response = await fetch(url, {
  519. headers,
  520. });
  521. if (!response.ok) {
  522. const errorData = await response.json().catch(() => ({
  523. error: { message: response.statusText },
  524. }));
  525. // Handle nested error structure: { error: { message: "..." } }
  526. const errorMessage =
  527. errorData.error?.message ||
  528. errorData.message ||
  529. errorData.error ||
  530. "Failed to download image";
  531. throw new Error(errorMessage);
  532. }
  533. return response.blob();
  534. }
  535. // Download image from URL with server-side proxy to avoid CORS issues
  536. async downloadImageFromUrl(url: string): Promise<{
  537. mimeType: string;
  538. filename: string;
  539. base64Data: string;
  540. tempUrl?: string;
  541. tempFilename?: string;
  542. }> {
  543. const apiUrl = `${this.getBaseUrl()}/image/download?url=${encodeURIComponent(url)}`;
  544. const response = await fetch(apiUrl);
  545. if (!response.ok) {
  546. const errorData = await response.json().catch(() => ({
  547. error: { message: response.statusText },
  548. }));
  549. // Handle nested error structure: { error: { message: "..." } }
  550. const errorMessage =
  551. errorData.error?.message ||
  552. errorData.message ||
  553. errorData.error ||
  554. "Failed to download image from URL";
  555. throw new Error(errorMessage);
  556. }
  557. const result = await response.json();
  558. return {
  559. mimeType: result.mime_type,
  560. filename: result.filename,
  561. base64Data: result.base64_data,
  562. tempUrl: result.temp_url,
  563. tempFilename: result.temp_filename,
  564. };
  565. }
  566. async cancelJob(jobId: string): Promise<void> {
  567. // Clear job status cache when cancelling
  568. cache.delete(`job_status_${jobId}`);
  569. return this.request<void>("/queue/cancel", {
  570. method: "POST",
  571. body: JSON.stringify({ job_id: jobId }),
  572. });
  573. }
  574. // Get queue status with caching and throttling
  575. async getQueueStatus(): Promise<QueueStatus> {
  576. const cacheKey = "queue_status";
  577. const cachedResult = cache.get(cacheKey) as QueueStatus | null;
  578. if (cachedResult) {
  579. return cachedResult;
  580. }
  581. const response = await this.request<{ queue: QueueStatus }>(
  582. "/queue/status",
  583. );
  584. // Cache queue status based on current activity
  585. const hasActiveJobs = response.queue.jobs.some(
  586. (job) => job.status === "processing" || job.status === "queued",
  587. );
  588. // Cache for shorter time if there are active jobs
  589. const cacheTime = hasActiveJobs ? 1000 : 5000; // 1 second for active, 5 seconds for idle
  590. cache.set(cacheKey, response.queue, cacheTime);
  591. return response.queue;
  592. }
  593. async clearQueue(): Promise<void> {
  594. // Clear all related caches
  595. cache.delete("queue_status");
  596. return this.request<void>("/queue/clear", {
  597. method: "POST",
  598. });
  599. }
  600. // Model management
  601. async getModels(
  602. type?: string,
  603. loaded?: boolean,
  604. page: number = 1,
  605. limit: number = -1,
  606. search?: string,
  607. ): Promise<EnhancedModelsResponse> {
  608. const cacheKey = `models_${type || "all"}_${loaded ? "loaded" : "all"}_${page}_${limit}_${search || "all"}`;
  609. const cachedResult = cache.get(cacheKey) as EnhancedModelsResponse | null;
  610. if (cachedResult) {
  611. return cachedResult;
  612. }
  613. let endpoint = "/models";
  614. const params = [];
  615. if (type && type !== "loaded") params.push(`type=${type}`);
  616. if (type === "loaded" || loaded) params.push("loaded=true");
  617. // Only add page parameter if we're using pagination (limit > 0)
  618. if (limit > 0) {
  619. params.push(`page=${page}`);
  620. params.push(`limit=${limit}`);
  621. } else {
  622. // When limit is 0 (default), we want all models, so add limit=0 to disable pagination
  623. params.push("limit=0");
  624. }
  625. if (search) params.push(`search=${encodeURIComponent(search)}`);
  626. // Add include_metadata for additional information
  627. params.push("include_metadata=true");
  628. if (params.length > 0) endpoint += "?" + params.join("&");
  629. const response = await this.request<EnhancedModelsResponse>(endpoint);
  630. const models = response.models.map((model) => ({
  631. ...model,
  632. id: model.sha256_short || model.sha256 || model.id || model.name,
  633. size: model.file_size || model.size,
  634. path: model.path || model.name,
  635. }));
  636. const result = {
  637. ...response,
  638. models,
  639. };
  640. // Cache models for 30 seconds as they don't change frequently
  641. cache.set(cacheKey, result, 30000);
  642. return result;
  643. }
  644. // Get models with automatic selection information
  645. async getModelsForAutoSelection(
  646. checkpointModel?: string,
  647. ): Promise<EnhancedModelsResponse> {
  648. const cacheKey = `models_auto_selection_${checkpointModel || "none"}`;
  649. const cachedResult = cache.get(cacheKey) as EnhancedModelsResponse | null;
  650. if (cachedResult) {
  651. return cachedResult;
  652. }
  653. let endpoint = "/models";
  654. const params = [];
  655. params.push("include_metadata=true");
  656. params.push("include_requirements=true");
  657. if (checkpointModel) {
  658. params.push(`checkpoint=${encodeURIComponent(checkpointModel)}`);
  659. }
  660. params.push("limit=0"); // Get all models
  661. if (params.length > 0) endpoint += "?" + params.join("&");
  662. const response = await this.request<EnhancedModelsResponse>(endpoint);
  663. const models = response.models.map((model) => ({
  664. ...model,
  665. id: model.sha256_short || model.sha256 || model.id || model.name,
  666. size: model.file_size || model.size,
  667. path: model.path || model.name,
  668. }));
  669. const result = {
  670. ...response,
  671. models,
  672. };
  673. // Cache for 30 seconds
  674. cache.set(cacheKey, result, 30000);
  675. return result;
  676. }
  677. // Utility function to get models by type
  678. getModelsByType(models: ModelInfo[], type: string): ModelInfo[] {
  679. return models.filter(
  680. (model) => model.type.toLowerCase() === type.toLowerCase(),
  681. );
  682. }
  683. // Utility function to find models by name pattern
  684. findModelsByName(models: ModelInfo[], namePattern: string): ModelInfo[] {
  685. const pattern = namePattern.toLowerCase();
  686. return models.filter((model) => model.name.toLowerCase().includes(pattern));
  687. }
  688. // Utility function to get loaded models by type
  689. getLoadedModelsByType(models: ModelInfo[], type: string): ModelInfo[] {
  690. return this.getModelsByType(models, type).filter((model) => model.loaded);
  691. }
  692. // Get all models (for backward compatibility)
  693. async getAllModels(type?: string, loaded?: boolean): Promise<ModelInfo[]> {
  694. const allModels: ModelInfo[] = [];
  695. let page = 1;
  696. const limit = 100;
  697. while (true) {
  698. const response = await this.getModels(type, loaded, page, limit);
  699. allModels.push(...response.models);
  700. if (!response.pagination.has_next) {
  701. break;
  702. }
  703. page++;
  704. }
  705. return allModels;
  706. }
  707. async getModelInfo(modelId: string): Promise<ModelInfo> {
  708. const cacheKey = `model_info_${modelId}`;
  709. const cachedResult = cache.get(cacheKey) as ModelInfo | null;
  710. if (cachedResult) {
  711. return cachedResult;
  712. }
  713. const result = await this.request<ModelInfo>(`/models/${modelId}`);
  714. cache.set(cacheKey, result, 30000); // Cache for 30 seconds
  715. return result;
  716. }
  717. async loadModel(modelId: string): Promise<void> {
  718. // Clear model cache when loading
  719. cache.delete(`model_info_${modelId}`);
  720. try {
  721. return await this.request<void>(`/models/${modelId}/load`, {
  722. method: "POST",
  723. });
  724. } catch (error) {
  725. // Enhance error message for hash validation failures
  726. const errorMessage = error instanceof Error ? error.message : "Failed to load model";
  727. if (errorMessage.includes("INVALID_MODEL_IDENTIFIER") || errorMessage.includes("MODEL_NOT_FOUND")) {
  728. throw new Error(`Hash validation failed: ${errorMessage}. Please ensure you're using a valid model hash instead of a name.`);
  729. }
  730. throw error;
  731. }
  732. }
  733. async unloadModel(modelId: string): Promise<void> {
  734. // Clear model cache when unloading
  735. cache.delete(`models_*`);
  736. try {
  737. return await this.request<void>(`/models/${modelId}/unload`, {
  738. method: "POST",
  739. });
  740. } catch (error) {
  741. // Enhance error message for hash validation failures
  742. const errorMessage = error instanceof Error ? error.message : "Failed to unload model";
  743. if (errorMessage.includes("INVALID_MODEL_IDENTIFIER") || errorMessage.includes("MODEL_NOT_FOUND")) {
  744. throw new Error(`Hash validation failed: ${errorMessage}. Please ensure you're using a valid model hash instead of a name.`);
  745. }
  746. throw error;
  747. }
  748. }
  749. async computeModelHash(modelId: string): Promise<{request_id: string, status: string, message: string}> {
  750. // Clear model cache when computing hash
  751. cache.delete(`models_*`);
  752. return this.request<{request_id: string, status: string, message: string}>(`/models/hash`, {
  753. method: "POST",
  754. body: JSON.stringify({ models: [modelId] }),
  755. });
  756. }
  757. async scanModels(): Promise<void> {
  758. // Clear all model caches when scanning
  759. cache.clear();
  760. return this.request<void>("/models/refresh", {
  761. method: "POST",
  762. });
  763. }
  764. async getModelTypes(): Promise<
  765. Array<{
  766. type: string;
  767. description: string;
  768. extensions: string[];
  769. capabilities: string[];
  770. requires?: string[];
  771. recommended_for: string;
  772. }>
  773. > {
  774. const cacheKey = "model_types";
  775. const cachedResult = cache.get(cacheKey) as Array<{
  776. type: string;
  777. description: string;
  778. extensions: string[];
  779. capabilities: string[];
  780. requires?: string[];
  781. recommended_for: string;
  782. }> | null;
  783. if (cachedResult) {
  784. return cachedResult;
  785. }
  786. const response = await this.request<{
  787. model_types: Array<{
  788. type: string;
  789. description: string;
  790. extensions: string[];
  791. capabilities: string[];
  792. requires?: string[];
  793. recommended_for: string;
  794. }>;
  795. }>("/models/types");
  796. cache.set(cacheKey, response.model_types, 60000); // Cache for 1 minute
  797. return response.model_types;
  798. }
  799. async convertModel(
  800. modelName: string,
  801. quantizationType: string,
  802. outputPath?: string,
  803. ): Promise<{ request_id: string; message: string }> {
  804. return this.request<{ request_id: string; message: string }>(
  805. "/models/convert",
  806. {
  807. method: "POST",
  808. body: JSON.stringify({
  809. model_name: modelName,
  810. quantization_type: quantizationType,
  811. output_path: outputPath,
  812. }),
  813. },
  814. );
  815. }
  816. // System endpoints
  817. async getHealth(): Promise<{ status: string }> {
  818. return this.request<{ status: string }>("/health");
  819. }
  820. async getStatus(): Promise<Record<string, unknown>> {
  821. return this.request<Record<string, unknown>>("/status");
  822. }
  823. async getSystemInfo(): Promise<Record<string, unknown>> {
  824. return this.request<Record<string, unknown>>("/system");
  825. }
  826. async restartServer(): Promise<{ message: string }> {
  827. return this.request<{ message: string }>("/system/restart", {
  828. method: "POST",
  829. body: JSON.stringify({}),
  830. });
  831. }
  832. // Image manipulation endpoints
  833. async resizeImage(
  834. image: string,
  835. width: number,
  836. height: number,
  837. ): Promise<{ image: string }> {
  838. return this.request<{ image: string }>("/image/resize", {
  839. method: "POST",
  840. body: JSON.stringify({
  841. image,
  842. width,
  843. height,
  844. }),
  845. });
  846. }
  847. async cropImage(
  848. image: string,
  849. x: number,
  850. y: number,
  851. width: number,
  852. height: number,
  853. ): Promise<{ image: string }> {
  854. return this.request<{ image: string }>("/image/crop", {
  855. method: "POST",
  856. body: JSON.stringify({
  857. image,
  858. x,
  859. y,
  860. width,
  861. height,
  862. }),
  863. });
  864. }
  865. // Configuration endpoints with caching
  866. async getSamplers(): Promise<
  867. Array<{ name: string; description: string; recommended_steps: number }>
  868. > {
  869. const cacheKey = "samplers";
  870. const cachedResult = cache.get(cacheKey) as Array<{ name: string; description: string; recommended_steps: number }> | null;
  871. if (cachedResult) {
  872. return cachedResult;
  873. }
  874. const response = await this.request<{
  875. samplers: Array<{
  876. name: string;
  877. description: string;
  878. recommended_steps: number;
  879. }>;
  880. }>("/samplers");
  881. cache.set(cacheKey, response.samplers, 60000); // Cache for 1 minute
  882. return response.samplers;
  883. }
  884. async getSchedulers(): Promise<Array<{ name: string; description: string }>> {
  885. const cacheKey = "schedulers";
  886. const cachedResult = cache.get(cacheKey) as Array<{ name: string; description: string }> | null;
  887. if (cachedResult) {
  888. return cachedResult;
  889. }
  890. const response = await this.request<{
  891. schedulers: Array<{ name: string; description: string }>;
  892. }>("/schedulers");
  893. cache.set(cacheKey, response.schedulers, 60000); // Cache for 1 minute
  894. return response.schedulers;
  895. }
  896. // Cache management methods
  897. clearCache(): void {
  898. cache.clear();
  899. }
  900. clearCacheByPrefix(prefix: string): void {
  901. const keysToDelete: string[] = [];
  902. // Access the private cache property through type assertion for cleanup
  903. const cacheInstance = cache as unknown as {
  904. cache: Map<string, { data: unknown; timestamp: number; ttl: number }>
  905. };
  906. cacheInstance.cache.forEach((_: unknown, key: string) => {
  907. if (key.startsWith(prefix)) {
  908. keysToDelete.push(key);
  909. }
  910. });
  911. keysToDelete.forEach((key) => cache.delete(key));
  912. }
  913. }
  914. // Generic API request function for authentication
  915. export async function apiRequest(
  916. endpoint: string,
  917. options: RequestInit = {},
  918. ): Promise<Response> {
  919. const { apiUrl, apiBase } = getApiConfig();
  920. const url = `${apiUrl}${apiBase}${endpoint}`;
  921. // For both Unix and JWT auth, send username and password
  922. // The server will handle whether password is required based on PAM availability
  923. // Get authentication method from server config
  924. const authMethod =
  925. typeof window !== "undefined" && window.__SERVER_CONFIG__
  926. ? window.__SERVER_CONFIG__.authMethod
  927. : "jwt";
  928. // Add auth token or Unix user header based on auth method
  929. const token =
  930. typeof window !== "undefined" ? localStorage.getItem("auth_token") : null;
  931. const unixUser =
  932. typeof window !== "undefined" ? localStorage.getItem("unix_user") : null;
  933. const headers: Record<string, string> = {
  934. "Content-Type": "application/json",
  935. ...(options.headers as Record<string, string>),
  936. };
  937. if (authMethod === "unix" && unixUser) {
  938. // For Unix auth, send the username in X-Unix-User header
  939. headers["X-Unix-User"] = unixUser;
  940. } else if (token) {
  941. // For JWT auth, send the token in Authorization header
  942. headers["Authorization"] = `Bearer ${token}`;
  943. }
  944. return fetch(url, {
  945. ...options,
  946. headers,
  947. });
  948. }
  949. // Authentication API endpoints
  950. export const authApi = {
  951. async login(username: string, password?: string) {
  952. // Get authentication method from server config
  953. const authMethod =
  954. typeof window !== "undefined" && window.__SERVER_CONFIG__
  955. ? window.__SERVER_CONFIG__.authMethod
  956. : "jwt";
  957. // For both Unix and JWT auth, send username and password
  958. // The server will handle whether password is required based on PAM availability
  959. const response = await apiRequest("/auth/login", {
  960. method: "POST",
  961. body: JSON.stringify({ username, password }),
  962. });
  963. if (!response.ok) {
  964. const error = await response
  965. .json()
  966. .catch(() => ({ message: "Login failed" }));
  967. throw new Error(error.message || "Login failed");
  968. }
  969. return response.json();
  970. },
  971. async validateToken(token: string) {
  972. const response = await apiRequest("/auth/validate", {
  973. headers: { Authorization: `Bearer ${token}` },
  974. });
  975. if (!response.ok) {
  976. throw new Error("Token validation failed");
  977. }
  978. return response.json();
  979. },
  980. async refreshToken() {
  981. const response = await apiRequest("/auth/refresh", {
  982. method: "POST",
  983. });
  984. if (!response.ok) {
  985. throw new Error("Token refresh failed");
  986. }
  987. return response.json();
  988. },
  989. async logout() {
  990. await apiRequest("/auth/logout", {
  991. method: "POST",
  992. });
  993. },
  994. async getCurrentUser() {
  995. const response = await apiRequest("/auth/me");
  996. if (!response.ok) {
  997. throw new Error("Failed to get current user");
  998. }
  999. return response.json();
  1000. },
  1001. async changePassword(oldPassword: string, newPassword: string) {
  1002. const response = await apiRequest("/auth/change-password", {
  1003. method: "POST",
  1004. body: JSON.stringify({
  1005. old_password: oldPassword,
  1006. new_password: newPassword,
  1007. }),
  1008. });
  1009. if (!response.ok) {
  1010. const error = await response
  1011. .json()
  1012. .catch(() => ({ message: "Password change failed" }));
  1013. throw new Error(error.message || "Password change failed");
  1014. }
  1015. return response.json();
  1016. },
  1017. // API Key management
  1018. async getApiKeys() {
  1019. const response = await apiRequest("/auth/api-keys");
  1020. if (!response.ok) {
  1021. throw new Error("Failed to get API keys");
  1022. }
  1023. return response.json();
  1024. },
  1025. async createApiKey(name: string, scopes?: string[]) {
  1026. const response = await apiRequest("/auth/api-keys", {
  1027. method: "POST",
  1028. body: JSON.stringify({ name, scopes }),
  1029. });
  1030. if (!response.ok) {
  1031. const error = await response
  1032. .json()
  1033. .catch(() => ({ message: "Failed to create API key" }));
  1034. throw new Error(error.message || "Failed to create API key");
  1035. }
  1036. return response.json();
  1037. },
  1038. async deleteApiKey(keyId: string) {
  1039. const response = await apiRequest(`/auth/api-keys/${keyId}`, {
  1040. method: "DELETE",
  1041. });
  1042. if (!response.ok) {
  1043. throw new Error("Failed to delete API key");
  1044. }
  1045. return response.json();
  1046. },
  1047. // User management (admin only)
  1048. async getUsers() {
  1049. const response = await apiRequest("/auth/users");
  1050. if (!response.ok) {
  1051. throw new Error("Failed to get users");
  1052. }
  1053. return response.json();
  1054. },
  1055. async createUser(userData: {
  1056. username: string;
  1057. email?: string;
  1058. password: string;
  1059. role?: string;
  1060. }) {
  1061. const response = await apiRequest("/auth/users", {
  1062. method: "POST",
  1063. body: JSON.stringify(userData),
  1064. });
  1065. if (!response.ok) {
  1066. const error = await response
  1067. .json()
  1068. .catch(() => ({ message: "Failed to create user" }));
  1069. throw new Error(error.message || "Failed to create user");
  1070. }
  1071. return response.json();
  1072. },
  1073. async updateUser(
  1074. userId: string,
  1075. userData: { email?: string; role?: string; active?: boolean },
  1076. ) {
  1077. const response = await apiRequest(`/auth/users/${userId}`, {
  1078. method: "PUT",
  1079. body: JSON.stringify(userData),
  1080. });
  1081. if (!response.ok) {
  1082. const error = await response
  1083. .json()
  1084. .catch(() => ({ message: "Failed to update user" }));
  1085. throw new Error(error.message || "Failed to update user");
  1086. }
  1087. return response.json();
  1088. },
  1089. async deleteUser(userId: string) {
  1090. const response = await apiRequest(`/auth/users/${userId}`, {
  1091. method: "DELETE",
  1092. });
  1093. if (!response.ok) {
  1094. throw new Error("Failed to delete user");
  1095. }
  1096. return response.json();
  1097. },
  1098. };
  1099. // Version API
  1100. export async function getVersion(): Promise<VersionInfo> {
  1101. const response = await apiRequest("/version");
  1102. if (!response.ok) {
  1103. throw new Error("Failed to get version information");
  1104. }
  1105. return response.json();
  1106. }
  1107. export const apiClient = new ApiClient();