File size: 4,753 Bytes
b3869eb
2cfda00
b3869eb
 
 
 
 
 
 
 
 
778e84b
b3869eb
778e84b
 
 
 
 
 
b3869eb
778e84b
 
 
 
 
b3869eb
 
 
d242052
b3869eb
d242052
 
 
 
 
 
b3869eb
 
 
d242052
 
 
 
b3869eb
 
 
05251fc
 
 
 
 
 
 
 
 
 
b3869eb
05251fc
 
b3869eb
05251fc
b3869eb
 
 
 
 
 
05251fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3869eb
05251fc
b3869eb
 
 
 
 
05251fc
 
 
 
 
 
 
 
b3869eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Centralized API configuration for the ARIA frontend.
 *
 * Every backend request must go through this module so the base URL is defined
 * in exactly one place. In development the FastAPI backend runs on a different
 * origin (port 8001) than the Astro dev server (port 4321), so requests are
 * issued as absolute URLs and rely on the backend's permissive CORS policy.
 *
 * Override the target by setting `PUBLIC_API_URL` in `frontend/.env`.
 */

const DEFAULT_DEV_API_BASE_URL = 'http://127.0.0.1:8001';

/**
 * Resolved backend origin.
 * - In development mode (`npm run dev`), defaults to `http://127.0.0.1:8001`.
 * - In production mode (`npm run build` / Docker), defaults to `""` (same-origin relative URLs).
 * - Can be explicitly overridden via `PUBLIC_API_URL` environment variable.
 */
export const API_BASE_URL: string = (
  typeof import.meta.env.PUBLIC_API_URL === 'string' && import.meta.env.PUBLIC_API_URL.trim() !== ''
    ? import.meta.env.PUBLIC_API_URL.trim()
    : import.meta.env.DEV
      ? DEFAULT_DEV_API_BASE_URL
      : ''
).replace(/\/$/, '');

/**
 * Build a fully-qualified backend URL from an API path.
 *
 * Call sites should pass the canonical versioned path (e.g. `/api/v1/analyze`).
 * Un-versioned `/api/...` paths are still upgraded to `/api/v1/...` here as a
 * safety net so a stray legacy path never reaches the backend's deprecated
 * redirect, but new code should not rely on that.
 *
 * @param path - A path beginning with `/` (e.g. `/api/v1/analyze`). A leading
 *   slash is added if missing.
 */
export function apiUrl(path: string): string {
  let normalized = path.startsWith('/') ? path : `/${path}`;
  if (normalized.startsWith('/api/') && !normalized.startsWith('/api/v1/')) {
    normalized = `/api/v1${normalized.substring(4)}`;
  }
  return `${API_BASE_URL}${normalized}`;
}

export type EngineState = 'ready' | 'analyzing' | 'indexed' | 'degraded' | 'offline';

export interface EngineStatusResult {
  state: EngineState;
  label: string;
  uptimeSeconds?: number;
  provider?: string;
  model?: string;
}

/**
 * Contextual backend engine status probe.
 * Returns a rich status object distinguishing between ready, indexed, degraded, and offline.
 */
export async function getEngineStatus(timeoutMs = 4000): Promise<EngineStatusResult> {
  const controller = new AbortController();
  const timer = setTimeout(() => controller.abort(), timeoutMs);
  try {
    const response = await fetch(apiUrl('/health'), {
      signal: controller.signal,
    });
    if (!response.ok) {
      return { state: 'degraded', label: 'DEGRADED' };
    }
    const data = await response.json().catch(() => ({}));
    
    // Check if active repository is in localStorage
    const hasActiveRepo = typeof window !== 'undefined' && Boolean(localStorage.getItem('activeRepo'));
    
    return {
      state: hasActiveRepo ? 'indexed' : 'ready',
      label: hasActiveRepo ? 'INDEXED' : 'ENGINE READY',
      uptimeSeconds: data.uptime_seconds,
      provider: data.llm_provider,
      model: data.llm_model,
    };
  } catch {
    return { state: 'offline', label: 'OFFLINE' };
  } finally {
    clearTimeout(timer);
  }
}

/**
 * Lightweight backend liveness probe used by backward-compatible callers.
 */
export async function checkBackendHealth(timeoutMs = 4000): Promise<boolean> {
  const status = await getEngineStatus(timeoutMs);
  return status.state !== 'offline';
}

/**
 * Safely extracts a user-friendly error message from any given error object/response.
 * Conforms to Priority 3: handles FastAPI detail arrays, object fields, HTTP status codes,
 * and falls back gracefully so that `[object Object]` never appears in the UI.
 */
export function extractErrorMessage(error: any): string {
  if (!error) return 'An unknown error occurred.';
  if (typeof error === 'string') return error;

  // Handle FastAPI detail array or object
  if (error.detail) {
    if (typeof error.detail === 'string') {
      return error.detail;
    }
    if (Array.isArray(error.detail)) {
      return error.detail
        .map((e: any) => {
          const locStr = e.loc ? e.loc.join('.') : '';
          return `${locStr ? locStr + ': ' : ''}${e.msg || JSON.stringify(e)}`;
        })
        .join('; ');
    }
    if (typeof error.detail === 'object') {
      return error.detail.message || JSON.stringify(error.detail);
    }
  }

  // Handle standard JS Error message or custom response messages
  if (error.message && typeof error.message === 'string') {
    return error.message;
  }

  // Handle generic object payload
  try {
    const stringified = JSON.stringify(error);
    if (stringified === '{}') {
      return String(error);
    }
    return stringified;
  } catch {
    return String(error);
  }
}