Spaces:
Running
Running
File size: 18,852 Bytes
98b7f49 ebdc25f d84050d ebdc25f d84050d ebdc25f d84050d ebdc25f d84050d ebdc25f d84050d ebdc25f d84050d ebdc25f 98b7f49 a4a6500 98b7f49 5db8e53 98b7f49 4d53e2b 5db8e53 4d53e2b 5db8e53 4d53e2b 5db8e53 4d53e2b 98b7f49 ba1db2b 98b7f49 a23307a ba1db2b e6104d9 ba1db2b e6104d9 ba1db2b a23307a ba1db2b e6104d9 ba1db2b e6104d9 98b7f49 a23307a ba1db2b e6104d9 ba1db2b e6104d9 ba1db2b a23307a ba1db2b e6104d9 ba1db2b e6104d9 98b7f49 a4a6500 aa0bba7 a4a6500 98b7f49 aa0bba7 98b7f49 53813c1 98b7f49 cd2780c 98b7f49 ebdc25f 53813c1 5db8e53 53813c1 3c247ba 53813c1 98b7f49 53813c1 98b7f49 53813c1 cd2780c 53813c1 98b7f49 53813c1 98b7f49 ebdc25f 98b7f49 35169fd 98b7f49 3eb00a5 0827363 2b0cf6a 98b7f49 739ee1e 98b7f49 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
// API client for AnyCoder backend
import axios, { AxiosInstance } from 'axios';
import type {
Model,
AuthStatus,
CodeGenerationRequest,
DeploymentRequest,
DeploymentResponse,
Language,
} from '@/types';
// Use relative URLs in production (Next.js rewrites will proxy to backend)
// In local dev, use localhost:8000 for direct backend access
const getApiUrl = () => {
// If explicitly set via env var, use it (for development)
if (process.env.NEXT_PUBLIC_API_URL) {
console.log('[API Client] Using explicit API URL:', process.env.NEXT_PUBLIC_API_URL);
return process.env.NEXT_PUBLIC_API_URL;
}
// For server-side rendering, always use relative URLs
if (typeof window === 'undefined') {
console.log('[API Client] SSR mode: using relative URLs');
return '';
}
// On localhost (dev mode), use direct backend URL
const hostname = window.location.hostname;
if (hostname === 'localhost' || hostname === '127.0.0.1') {
console.log('[API Client] Localhost dev mode: using http://localhost:8000');
return 'http://localhost:8000';
}
// In production (HF Space), use relative URLs (Next.js proxies to backend)
console.log('[API Client] Production mode: using relative URLs (proxied by Next.js)');
return '';
};
const API_URL = getApiUrl();
class ApiClient {
private client: AxiosInstance;
private token: string | null = null;
constructor() {
this.client = axios.create({
baseURL: API_URL,
headers: {
'Content-Type': 'application/json',
},
timeout: 10000, // 10 second timeout to prevent hanging connections
});
// Add auth token to requests if available
this.client.interceptors.request.use((config) => {
// ALWAYS use OAuth token primarily, session token is for backend tracking only
if (this.token) {
config.headers.Authorization = `Bearer ${this.token}`;
}
return config;
});
// Add response interceptor to handle authentication errors
this.client.interceptors.response.use(
(response) => response,
(error) => {
// Handle 401 errors (expired/invalid authentication)
// ONLY log out on specific auth errors, not all 401s
if (error.response && error.response.status === 401) {
const errorData = error.response.data;
const errorMessage = errorData?.detail || errorData?.message || '';
// Only log out if it's an authentication/session issue
// Don't log out for permission errors on specific resources
const shouldLogout =
errorMessage.includes('Authentication required') ||
errorMessage.includes('Invalid token') ||
errorMessage.includes('Token expired') ||
errorMessage.includes('Session expired') ||
error.config?.url?.includes('/auth/');
if (shouldLogout && typeof window !== 'undefined') {
// Clear ALL authentication data including session token
localStorage.removeItem('hf_oauth_token');
localStorage.removeItem('hf_session_token');
localStorage.removeItem('hf_user_info');
this.token = null;
// Dispatch custom event to notify UI components
window.dispatchEvent(new CustomEvent('auth-expired', {
detail: { message: 'Your session has expired. Please sign in again.' }
}));
}
}
return Promise.reject(error);
}
);
// Load token from localStorage on client side
if (typeof window !== 'undefined') {
this.token = localStorage.getItem('hf_oauth_token');
}
}
setToken(token: string | null) {
this.token = token;
// Note: OAuth token is stored by auth.ts, not here
// We just keep it in memory for API calls
}
getToken(): string | null {
return this.token;
}
// Cache helpers
private getCachedData<T>(key: string, maxAgeMs: number): T | null {
if (typeof window === 'undefined') return null;
try {
const cached = localStorage.getItem(key);
if (!cached) return null;
const { data, timestamp } = JSON.parse(cached);
const age = Date.now() - timestamp;
if (age > maxAgeMs) {
localStorage.removeItem(key);
return null;
}
return data;
} catch (error) {
console.error(`Failed to get cached data for ${key}:`, error);
return null;
}
}
private setCachedData<T>(key: string, data: T): void {
if (typeof window === 'undefined') return;
try {
localStorage.setItem(key, JSON.stringify({
data,
timestamp: Date.now()
}));
} catch (error) {
console.error(`Failed to cache data for ${key}:`, error);
}
}
async getModels(): Promise<Model[]> {
// Check cache first (24 hour TTL - cache once per day)
const cached = this.getCachedData<Model[]>('anycoder_models', 24 * 60 * 60 * 1000);
if (cached) {
console.log('Using cached models:', cached.length, 'models');
return cached;
}
try {
console.log('Fetching models from API...');
const response = await this.client.get<Model[]>('/api/models');
const models = response.data;
// Cache the successful response
if (models && models.length > 0) {
this.setCachedData('anycoder_models', models);
console.log('Cached', models.length, 'models (valid for 24 hours)');
}
return models;
} catch (error: any) {
// Handle connection errors gracefully
const isConnectionError =
error.code === 'ECONNABORTED' ||
error.code === 'ECONNRESET' ||
error.code === 'ECONNREFUSED' ||
error.message?.includes('socket hang up') ||
error.message?.includes('timeout') ||
error.message?.includes('Network Error') ||
error.response?.status === 503 ||
error.response?.status === 502;
if (isConnectionError) {
// Try to return stale cache if available
const staleCache = this.getCachedData<Model[]>('anycoder_models', Infinity);
if (staleCache && staleCache.length > 0) {
console.warn('Backend not available, using stale cached models');
return staleCache;
}
console.warn('Backend not available, cannot load models');
return [];
}
// Re-throw other errors
throw error;
}
}
async getLanguages(): Promise<{ languages: Language[] }> {
// Check cache first (24 hour TTL - cache once per day)
const cached = this.getCachedData<Language[]>('anycoder_languages', 24 * 60 * 60 * 1000);
if (cached) {
console.log('Using cached languages:', cached.length, 'languages');
return { languages: cached };
}
try {
console.log('Fetching languages from API...');
const response = await this.client.get<{ languages: Language[] }>('/api/languages');
const languages = response.data.languages;
// Cache the successful response
if (languages && languages.length > 0) {
this.setCachedData('anycoder_languages', languages);
console.log('Cached', languages.length, 'languages (valid for 24 hours)');
}
return response.data;
} catch (error: any) {
// Handle connection errors gracefully
const isConnectionError =
error.code === 'ECONNABORTED' ||
error.code === 'ECONNRESET' ||
error.code === 'ECONNREFUSED' ||
error.message?.includes('socket hang up') ||
error.message?.includes('timeout') ||
error.message?.includes('Network Error') ||
error.response?.status === 503 ||
error.response?.status === 502;
if (isConnectionError) {
// Try to return stale cache if available
const staleCache = this.getCachedData<Language[]>('anycoder_languages', Infinity);
if (staleCache && staleCache.length > 0) {
console.warn('Backend not available, using stale cached languages');
return { languages: staleCache };
}
// Fall back to default languages
console.warn('Backend not available, using default languages');
return { languages: ['html', 'gradio', 'transformers.js', 'streamlit', 'comfyui', 'react'] };
}
// Re-throw other errors
throw error;
}
}
async getAuthStatus(): Promise<AuthStatus> {
try {
const response = await this.client.get<AuthStatus>('/api/auth/status');
return response.data;
} catch (error: any) {
// Silently handle connection errors - don't spam console
if (error.code === 'ECONNABORTED' || error.code === 'ECONNRESET' || error.message?.includes('socket hang up')) {
// Connection error - backend may not be ready
return {
authenticated: false,
username: undefined,
message: 'Connection error',
};
}
// For other errors, return not authenticated
return {
authenticated: false,
username: undefined,
message: 'Not authenticated',
};
}
}
// Stream-based code generation using Fetch API with streaming (supports POST)
generateCodeStream(
request: CodeGenerationRequest,
onChunk: (content: string) => void,
onComplete: (code: string) => void,
onError: (error: string) => void,
onDeploying?: (message: string) => void,
onDeployed?: (message: string, spaceUrl: string) => void,
onDeployError?: (message: string) => void
): () => void {
// Build the URL correctly whether we have a base URL or not
const baseUrl = API_URL || window.location.origin;
const url = new URL('/api/generate', baseUrl);
let abortController = new AbortController();
let accumulatedCode = '';
let buffer = ''; // Buffer for incomplete SSE lines
// Use fetch with POST to support large payloads
fetch(url.toString(), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...(this.token ? { 'Authorization': `Bearer ${this.token}` } : {}),
},
body: JSON.stringify(request),
signal: abortController.signal,
})
.then(async (response) => {
// Handle rate limit errors before parsing response
if (response.status === 429) {
onError('⏱️ Rate limit exceeded. Free tier allows up to 20 requests per minute. Please wait a moment and try again.');
return;
}
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
if (!response.body) {
throw new Error('Response body is null');
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) {
console.log('[Stream] Stream ended, total code length:', accumulatedCode.length);
if (accumulatedCode) {
onComplete(accumulatedCode);
}
break;
}
// Decode chunk and add to buffer
buffer += decoder.decode(value, { stream: true });
// Process complete SSE messages (ending with \n\n)
const messages = buffer.split('\n\n');
// Keep the last incomplete message in the buffer
buffer = messages.pop() || '';
// Process each complete message
for (const message of messages) {
if (!message.trim()) continue;
// Parse SSE format: "data: {...}"
const lines = message.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
try {
const jsonStr = line.substring(6);
const data = JSON.parse(jsonStr);
console.log('[Stream] Received event:', data.type, data.content?.substring(0, 50));
if (data.type === 'chunk' && data.content) {
accumulatedCode += data.content;
onChunk(data.content);
} else if (data.type === 'complete') {
console.log('[Stream] Generation complete, total code length:', data.code?.length || accumulatedCode.length);
// Use the complete code from the message if available, otherwise use accumulated
const finalCode = data.code || accumulatedCode;
onComplete(finalCode);
// Don't return yet - might have deployment events coming
} else if (data.type === 'deploying') {
console.log('[Stream] Deployment started:', data.message);
if (onDeploying) {
onDeploying(data.message || 'Deploying...');
}
} else if (data.type === 'deployed') {
console.log('[Stream] Deployment successful:', data.space_url);
if (onDeployed) {
onDeployed(data.message || 'Deployed!', data.space_url);
}
} else if (data.type === 'deploy_error') {
console.log('[Stream] Deployment error:', data.message);
if (onDeployError) {
onDeployError(data.message || 'Deployment failed');
}
} else if (data.type === 'error') {
console.error('[Stream] Error:', data.message);
onError(data.message || 'Unknown error occurred');
return; // Exit the processing loop
}
} catch (error) {
console.error('Error parsing SSE data:', error, 'Line:', line);
}
}
}
}
}
})
.catch((error) => {
if (error.name === 'AbortError') {
console.log('[Stream] Request aborted');
return;
}
console.error('[Stream] Fetch error:', error);
onError(error.message || 'Connection error occurred');
});
// Return cleanup function
return () => {
abortController.abort();
};
}
// Alternative: WebSocket-based generation
generateCodeWebSocket(
request: CodeGenerationRequest,
onChunk: (content: string) => void,
onComplete: (code: string) => void,
onError: (error: string) => void
): WebSocket {
// Build WebSocket URL correctly for both dev and production
const baseUrl = API_URL || window.location.origin;
const wsUrl = baseUrl.replace('http', 'ws') + '/ws/generate';
const ws = new WebSocket(wsUrl);
ws.onopen = () => {
ws.send(JSON.stringify(request));
};
ws.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
if (data.type === 'chunk' && data.content) {
onChunk(data.content);
} else if (data.type === 'complete' && data.code) {
onComplete(data.code);
ws.close();
} else if (data.type === 'error') {
onError(data.message || 'Unknown error occurred');
ws.close();
}
} catch (error) {
console.error('Error parsing WebSocket data:', error);
}
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
onError('Connection error occurred');
};
return ws;
}
async deploy(request: DeploymentRequest): Promise<DeploymentResponse> {
console.log('[API Client] Deploy request:', {
endpoint: '/api/deploy',
method: 'POST',
baseURL: API_URL,
hasToken: !!this.token,
language: request.language,
code_length: request.code?.length,
space_name: request.space_name,
existing_repo_id: request.existing_repo_id,
});
try {
const response = await this.client.post<DeploymentResponse>('/api/deploy', request);
console.log('[API Client] Deploy response:', response.status, response.data);
return response.data;
} catch (error: any) {
console.error('[API Client] Deploy error:', {
status: error.response?.status,
statusText: error.response?.statusText,
data: error.response?.data,
message: error.message,
});
throw error;
}
}
async importProject(url: string, preferLocal: boolean = false): Promise<any> {
const response = await this.client.post('/api/import', { url, prefer_local: preferLocal });
return response.data;
}
async importSpace(username: string, spaceName: string): Promise<any> {
const response = await this.client.get(`/api/import/space/${username}/${spaceName}`);
return response.data;
}
async importModel(modelId: string, preferLocal: boolean = false): Promise<any> {
const response = await this.client.get(`/api/import/model/${modelId}`, {
params: { prefer_local: preferLocal }
});
return response.data;
}
async importGithub(owner: string, repo: string): Promise<any> {
const response = await this.client.get(`/api/import/github/${owner}/${repo}`);
return response.data;
}
async createPullRequest(repoId: string, code: string, language: string, prTitle?: string, prDescription?: string): Promise<any> {
const response = await this.client.post('/api/create-pr', {
repo_id: repoId,
code,
language,
pr_title: prTitle,
pr_description: prDescription
});
return response.data;
}
async duplicateSpace(fromSpaceId: string, toSpaceName?: string, isPrivate: boolean = false): Promise<any> {
const response = await this.client.post('/api/duplicate-space', {
from_space_id: fromSpaceId,
to_space_name: toSpaceName,
private: isPrivate
});
return response.data;
}
logout() {
this.token = null;
}
async getTrendingAnycoderApps(): Promise<any[]> {
try {
// Fetch from HuggingFace API directly
const response = await axios.get('https://huggingface.co/api/spaces', {
timeout: 5000,
});
// Filter for apps with 'anycoder' tag and sort by trendingScore
const anycoderApps = response.data
.filter((space: any) => space.tags && space.tags.includes('anycoder'))
.sort((a: any, b: any) => (b.trendingScore || 0) - (a.trendingScore || 0))
.slice(0, 6);
return anycoderApps;
} catch (error) {
console.error('Failed to fetch trending anycoder apps:', error);
return [];
}
}
}
// Export singleton instance
export const apiClient = new ApiClient();
|