File size: 8,797 Bytes
9e89154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * API client for Quantum Portfolio backend β€” parity with `frontend/src/services/api.js`.
 * Uses `NEXT_PUBLIC_API_URL` (empty = same-origin; dev often proxies to Flask :5000).
 *
 * Secrets: only `NEXT_PUBLIC_*` vars are embedded in the client bundle; never import
 * server-only keys here (Phase 3 / Checkpoint 3).
 */
import axios, { type AxiosError, type InternalAxiosRequestConfig } from "axios";
import { toast } from "sonner";

import { extractApiErrorMessage } from "./apiError";

const API_BASE = process.env.NEXT_PUBLIC_API_URL ?? "";
const API_KEY = process.env.NEXT_PUBLIC_API_KEY ?? "";

const defaultHeaders: Record<string, string> = { "Content-Type": "application/json" };
if (API_KEY) {
  defaultHeaders["X-API-Key"] = API_KEY;
}

type RetriableConfig = InternalAxiosRequestConfig & { _retried?: boolean };

const api = axios.create({
  baseURL: API_BASE,
  headers: defaultHeaders,
  timeout: 60000,
});

/** When set (e.g. admin static API key), sent as X-Tenant-Id for per-enterprise IBM / integrations. */
export const INTEGRATION_TENANT_STORAGE_KEY = "ql_active_tenant";

export function setActiveIntegrationTenant(tenantId: string | null): void {
  if (typeof window === "undefined") return;
  if (tenantId) {
    localStorage.setItem(INTEGRATION_TENANT_STORAGE_KEY, tenantId);
  } else {
    localStorage.removeItem(INTEGRATION_TENANT_STORAGE_KEY);
  }
}

export function getActiveIntegrationTenant(): string | null {
  if (typeof window === "undefined") return null;
  return localStorage.getItem(INTEGRATION_TENANT_STORAGE_KEY);
}

api.interceptors.request.use((config) => {
  if (typeof window !== "undefined") {
    const t = localStorage.getItem(INTEGRATION_TENANT_STORAGE_KEY);
    if (t) {
      config.headers = config.headers ?? {};
      (config.headers as Record<string, string>)["X-Tenant-Id"] = t;
    }
  }
  return config;
});

api.interceptors.response.use(
  (response) => {
    const d = response.data;
    if (d && typeof d === "object" && "data" in d && "meta" in d) {
      response.data = (d as { data: unknown }).data;
    }
    return response;
  },
  async (error: AxiosError) => {
    const status = error.response?.status;
    const config = error.config as RetriableConfig | undefined;

    if (status === 401) {
      toast.error("Unauthorized (401). Check your API key configuration.");
    }
    if (status === 429) {
      toast.warning("Rate limited (429). Please slow down requests.");
    }
    if (status && status >= 500 && config && !config._retried) {
      config._retried = true;
      toast.info("Server error β€” retrying once...");
      return api(config);
    }

    const respData = error.response?.data;
    const message = extractApiErrorMessage(error, respData);

    return Promise.reject(new Error(message));
  }
);

export async function fetchMarketData(
  tickers: string | string[],
  startDate: string | null = null,
  endDate: string | null = null
) {
  const res = await api.post("/api/market-data", {
    tickers: Array.isArray(tickers)
      ? tickers
      : tickers.split(",").map((t) => t.trim()).filter(Boolean),
    start_date: startDate,
    end_date: endDate,
  });
  return res.data;
}

export async function optimizePortfolio(params: Record<string, unknown>) {
  const res = await api.post("/api/portfolio/optimize", params);
  return res.data;
}

export async function runBacktest(params: Record<string, unknown>) {
  const res = await api.post("/api/portfolio/backtest", params);
  return res.data;
}

export async function runBacktestBatch(
  requests: unknown[],
  stopOnError = false
) {
  const res = await api.post("/api/portfolio/backtest/batch", {
    requests,
    stop_on_error: stopOnError,
  });
  return res.data;
}

export async function optimizeBatch(requests: unknown[], stopOnError = false) {
  const res = await api.post("/api/portfolio/optimize/batch", {
    requests,
    stop_on_error: stopOnError,
  });
  return res.data;
}

export async function getObjectives() {
  const res = await api.get("/api/config/objectives");
  return res.data;
}

export async function getPresets() {
  const res = await api.get("/api/config/presets");
  return res.data;
}

export async function getConstraintsSchema() {
  const res = await api.get("/api/config/constraints");
  return res.data;
}

export async function getEfficientFrontier(
  tickers: string | string[],
  startDate: string | null,
  endDate: string | null,
  nPoints = 15
) {
  const res = await api.post("/api/portfolio/efficient-frontier", {
    tickers: Array.isArray(tickers)
      ? tickers
      : tickers.split(",").map((t) => t.trim()).filter(Boolean),
    start_date: startDate,
    end_date: endDate,
    n_points: nPoints,
  });
  return res.data;
}

export async function setIbmQuantumToken(token: string) {
  const res = await api.post("/api/config/ibm-quantum", { token });
  return res.data;
}

export async function clearIbmQuantumToken() {
  const res = await api.delete("/api/config/ibm-quantum");
  return res.data;
}

export async function getIbmQuantumStatus() {
  const res = await api.get("/api/config/ibm-quantum/status");
  return res.data;
}

/** Row shape from GET /api/config/ibm-quantum/workloads (IBM Runtime job list). */
export interface IbmWorkloadRow {
  job_id: string | null;
  status: string | null;
  status_error?: string;
  backend?: string | null;
  created?: string | null;
  usage_seconds?: number | null;
  instance?: string | null;
  program_id?: string | null;
}

/** Recent IBM Quantum Runtime jobs for the tenant (requires API key; same as token POST). */
export async function getIbmQuantumWorkloads(limit = 20) {
  const res = await api.get("/api/config/ibm-quantum/workloads", {
    params: { limit },
  });
  return res.data as {
    ok: boolean;
    configured: boolean;
    workloads: IbmWorkloadRow[];
    tenant_id?: string;
    error?: string;
  };
}

export async function getIntegrationTenants() {
  const res = await api.get("/api/config/tenants");
  return res.data as { tenants: { id: string; label: string }[] };
}

export async function getIntegrationsCatalog() {
  const res = await api.get("/api/config/integrations");
  return res.data as {
    tenant_id: string;
    providers: Array<Record<string, unknown>>;
  };
}

export async function healthCheck() {
  const res = await api.get("/api/health");
  return res.data;
}

/** Async job queue β€” see `api.py` `/api/jobs/*`. Body uses `payload` for optimize/backtest params. */
export async function submitOptimizeJob(payload: Record<string, unknown>) {
  const res = await api.post("/api/jobs/optimize", { payload });
  return res.data as { job_id: string; status: string };
}

export async function submitBacktestJob(payload: Record<string, unknown>) {
  const res = await api.post("/api/jobs/backtest", { payload });
  return res.data as { job_id: string; status: string };
}

export async function getJobStatus(jobId: string) {
  const res = await api.get(`/api/jobs/${encodeURIComponent(jobId)}`);
  return res.data as {
    job_id: string;
    job_type: string;
    status: string;
    error?: string | null;
    result?: unknown;
    created_at?: string | null;
    started_at?: string | null;
    finished_at?: string | null;
  };
}

// ─── Lab Runs (durable experiment registry) ─────────────────────────────────

export interface LabRunSpec {
  objective: string;
  weight_min: number;
  weight_max: number;
  seed: number;
  data_mode?: string;
  regime?: string;
  tickers?: string[] | null;
  n_assets?: number | null;
  K?: number | null;
  K_screen?: number | null;
  K_select?: number | null;
  backend_name?: string | null;
  ibm_backend_mode?: string;
}

export interface LabRun {
  id: string;
  tenant_id: string;
  status: "queued" | "running" | "completed" | "failed";
  execution_kind: string;
  spec: LabRunSpec | null;
  result: Record<string, unknown> | null;
  error: string | null;
  external_job_id: string | null;
  created_at: string;
  started_at: string | null;
  finished_at: string | null;
}

export async function createLabRun(
  payload: Record<string, unknown>,
  options?: { execution_kind?: string }
) {
  const body: Record<string, unknown> = { payload };
  if (options?.execution_kind) {
    body.execution_kind = options.execution_kind;
  }
  const res = await api.post("/api/runs", body);
  return res.data as { run_id: string; status: string };
}

export async function getLabRun(runId: string) {
  const res = await api.get(`/api/runs/${encodeURIComponent(runId)}`);
  return res.data as LabRun;
}

export async function listLabRuns(limit = 20) {
  const res = await api.get("/api/runs", { params: { limit } });
  return res.data as { runs: LabRun[]; count: number };
}

export default api;