File size: 10,271 Bytes
24f95f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type {
  AnalyzeRequest,
  ScamGuardianResponse,
  CaseRecord,
  ConfigStatusResponse,
  DeepHealthResponse,
  HealthResponse,
  PromptInfo,
  SimulationRecord,
  SimulationRequest,
} from './types';

export function getApiBaseUrl(): string {
  if (process.env.NEXT_PUBLIC_API_URL !== undefined) {
    return process.env.NEXT_PUBLIC_API_URL;
  }

  if (typeof window !== 'undefined') {
    return window.location.origin;
  }

  return 'http://localhost:7860';
}

export class MiroOrgClient {
  private baseUrl?: string;

  constructor(baseUrl?: string) {
    this.baseUrl = baseUrl;
  }

  private getBaseUrl(): string {
    return this.baseUrl ?? getApiBaseUrl();
  }

  // Health endpoints
  async getHealth(): Promise<HealthResponse> {
    const response = await fetch(`${this.getBaseUrl()}/health`);
    if (!response.ok) throw new Error('Health check failed');
    return response.json();
  }

  async getDeepHealth(): Promise<DeepHealthResponse> {
    const response = await fetch(`${this.getBaseUrl()}/health/deep`);
    if (!response.ok) throw new Error('Deep health check failed');
    return response.json();
  }

  async getConfigStatus(): Promise<ConfigStatusResponse> {
    const response = await fetch(`${this.getBaseUrl()}/config/status`);
    if (!response.ok) throw new Error('Config status check failed');
    return response.json();
  }

  // AI Assistant endpoints
  async run(userInput: string, context?: any): Promise<any> {
    const response = await fetch(`${this.getBaseUrl()}/run`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ user_input: userInput, context }),
    });
    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.detail || "AI query failed");
    }
    return response.json();
  }

  // Backward compatibility / General analysis
  async analyze(request: AnalyzeRequest): Promise<ScamGuardianResponse> {
    const response = await fetch(`${this.getBaseUrl()}/analyze`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        text: request.user_input,
        url: request.url,
        image_base64: request.image_base64,
        source: request.source || 'janus-client',
      }),
    });
    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.detail || 'Analysis failed');
    }
    return response.json();
  }

  // Case endpoints
  async getCases(): Promise<CaseRecord[]> {
    const response = await fetch(`${this.getBaseUrl()}/cases`);
    if (!response.ok) throw new Error('Failed to fetch cases');
    const data = await response.json();
    return Array.isArray(data) ? data : (data.cases ?? []);
  }

  async getCase(caseId: string): Promise<CaseRecord> {
    const response = await fetch(`${this.getBaseUrl()}/cases/${caseId}`);
    if (!response.ok) throw new Error('Failed to fetch case');
    return response.json();
  }

  // Simulation endpoints
  async createSimulation(request: SimulationRequest): Promise<SimulationRecord> {
    const response = await fetch(`${this.getBaseUrl()}/simulation/run`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(request),
    });
    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.detail || 'Simulation creation failed');
    }
    return response.json();
  }

  async getSimulations(): Promise<SimulationRecord[]> {
    const response = await fetch(`${this.getBaseUrl()}/simulation/list`);
    if (!response.ok) throw new Error('Failed to fetch simulations');
    return response.json();
  }

  async getSimulation(simulationId: string): Promise<SimulationRecord> {
    const response = await fetch(`${this.getBaseUrl()}/simulation/${simulationId}`);
    if (!response.ok) throw new Error('Failed to fetch simulation');
    return response.json();
  }

  async getSimulationStatus(simulationId: string): Promise<{ status: string }> {
    const response = await fetch(`${this.getBaseUrl()}/simulation/${simulationId}`);
    if (!response.ok) throw new Error('Failed to fetch simulation status');
    const data = await response.json();
    return { status: data.status };
  }

  async getSimulationReport(simulationId: string): Promise<{ report: string }> {
    const response = await fetch(`${this.getBaseUrl()}/simulation/${simulationId}/report`);
    if (!response.ok) throw new Error('Failed to fetch simulation report');
    const data = await response.json();
    return { report: data.report };
  }

  async runNativeSimulation(userInput: string, context?: any): Promise<any> {
    const response = await fetch(`${this.getBaseUrl()}/simulation/run`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ user_input: userInput, context }),
    });
    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.detail || 'Simulation failed');
    }
    return response.json();
  }

  async chatWithSimulation(simulationId: string, message: string): Promise<any> {
    const response = await fetch(`${this.getBaseUrl()}/simulation/${simulationId}/chat`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ message }),
    });
    if (!response.ok) throw new Error('Simulation chat failed');
    return response.json();
  }

  // Sentinel endpoints
  async getSentinelStatus(): Promise<any> {
    const r = await fetch(`${this.getBaseUrl()}/sentinel/status`);
    if (!r.ok) return null;
    return r.json();
  }

  async getSentinelAlerts(limit = 20): Promise<any[]> {
    const r = await fetch(`${this.getBaseUrl()}/sentinel/alerts?limit=${limit}`);
    if (!r.ok) return [];
    return r.json();
  }

  async getSentinelCapability(): Promise<any> {
    const r = await fetch(`${this.getBaseUrl()}/sentinel/capability/current`);
    if (!r.ok) return null;
    return r.json();
  }

  // Prompt endpoints
  async getPrompts(): Promise<PromptInfo[]> {
    const response = await fetch(`${this.getBaseUrl()}/prompts`);
    if (!response.ok) throw new Error('Failed to fetch prompts');
    return response.json();
  }

  async getPrompt(name: string): Promise<PromptInfo> {
    const response = await fetch(`${this.getBaseUrl()}/prompts/${name}`);
    if (!response.ok) throw new Error('Failed to fetch prompt');
    return response.json();
  }

  async updatePrompt(name: string, content: string): Promise<{ message: string }> {
    const response = await fetch(`${this.getBaseUrl()}/prompts/${name}`, {
      method: 'PUT',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ content }),
    });
    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.detail || 'Prompt update failed');
    }
    return response.json();
  }
}

// Export singleton instance
export const apiClient = new MiroOrgClient();

// Finance Intelligence endpoints
export class FinanceClient {
  private baseUrl?: string;

  constructor(baseUrl?: string) {
    this.baseUrl = baseUrl;
  }

  private getBaseUrl(): string {
    return this.baseUrl ?? getApiBaseUrl();
  }

  async analyzeText(text: string, sources: string[] = []) {
    const r = await fetch(`${this.getBaseUrl()}/finance/analyze/text`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ text, sources }),
    });
    if (!r.ok) throw new Error('Text analysis failed');
    return r.json();
  }

  async getTickerIntelligence(symbol: string) {
    const r = await fetch(`${this.getBaseUrl()}/finance/ticker/${symbol}`);
    if (!r.ok) throw new Error(`Failed to fetch ${symbol}`);
    return r.json();
  }

  async searchTicker(query: string) {
    const r = await fetch(`${this.getBaseUrl()}/finance/search/${encodeURIComponent(query)}`);
    if (!r.ok) return [];
    return r.json();
  }

  async analyzeNews(query: string, limit = 8) {
    const r = await fetch(`${this.getBaseUrl()}/finance/news/analyze`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ query, limit }),
    });
    if (!r.ok) throw new Error('News analysis failed');
    return r.json();
  }

  async getHeadlines() {
    const r = await fetch(`${this.getBaseUrl()}/finance/headlines`);
    if (!r.ok) return [];
    return r.json();
  }
}


export const financeClient = new FinanceClient();

// Scam Guardian endpoints
export class GuardianClient {
  private baseUrl?: string;

  constructor(baseUrl?: string) {
    this.baseUrl = baseUrl;
  }

  private getBaseUrl(): string {
    return this.baseUrl ?? getApiBaseUrl();
  }

  async analyze(payload: { text?: string; url?: string; image_base64?: string; source?: string }): Promise<ScamGuardianResponse> {
    const res = await fetch(`${this.getBaseUrl()}/analyze`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(payload),
    });
    if (!res.ok) throw new Error("Analysis failed");
    return res.json();
  }

  async getHistory(): Promise<ScamGuardianResponse[]> {
    const res = await fetch(`${this.getBaseUrl()}/history`);
    if (!res.ok) throw new Error("Failed to fetch history");
    return res.json();
  }

  async getEvent(id: string): Promise<ScamGuardianResponse> {
    const res = await fetch(`${this.getBaseUrl()}/history/${id}`);
    if (!res.ok) throw new Error("Failed to fetch event detail");
    return res.json();
  }

  async submitFeedback(payload: { analyze_id: string; is_scam: boolean; correct_category?: string; notes?: string }) {
    const res = await fetch(`${this.getBaseUrl()}/feedback`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(payload),
    });
    if (!res.ok) throw new Error("Feedback submission failed");
    return res.json();
  }

  async getGuardianStatus() {
    const res = await fetch(`${this.getBaseUrl()}/guardian/status`, {
      headers: { "Content-Type": "application/json" },
    });
    if (!res.ok) return null;
    return res.json();
  }
}

export const guardianClient = new GuardianClient();