File size: 6,535 Bytes
bf04727
 
b3a788a
 
bf04727
7004388
 
 
 
 
 
bf04727
7004388
 
 
bf04727
 
 
 
 
 
 
7004388
bf04727
 
 
 
 
 
b5e8a53
7004388
 
b5e8a53
 
7004388
bf04727
 
 
 
831ce9c
 
 
 
 
 
 
 
 
bf04727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5e8a53
 
 
 
 
 
 
 
bf04727
7004388
 
bf04727
7004388
 
bf04727
7004388
 
bf04727
 
 
 
b5e8a53
 
bf04727
b5e8a53
bf04727
b5e8a53
bf04727
 
b5e8a53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8aba1e2
 
 
a304e30
 
 
 
 
 
 
 
 
9a86182
 
 
831ce9c
0ff500c
831ce9c
0ff500c
831ce9c
 
172aa06
 
 
7004388
bf04727
 
7004388
 
 
 
 
 
 
bf04727
 
7004388
bf04727
 
 
 
 
7004388
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf04727
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
// API client for InnerVoice backend

// In production/HuggingFace, we want empty string to use relative /api paths which hits the Next.js rewrite proxy.
const API_URL = process.env.NEXT_PUBLIC_API_URL || "";

function getAuthHeader(): Record<string, string> {
  if (typeof window === "undefined") return {};
  const token = localStorage.getItem("token");
  return token ? { Authorization: `Bearer ${token}` } : {};
}

export async function apiGet<T>(path: string): Promise<T> {
  const res = await fetch(`${API_URL}${path}`, {
    headers: { ...getAuthHeader() },
  });
  if (!res.ok) throw new Error(`API Error ${res.status}: ${await res.text()}`);
  return res.json();
}

export async function apiPost<T>(path: string, body: unknown): Promise<T> {
  const res = await fetch(`${API_URL}${path}`, {
    method: "POST",
    headers: { "Content-Type": "application/json", ...getAuthHeader() },
    body: JSON.stringify(body),
  });
  if (!res.ok) throw new Error(`API Error ${res.status}: ${await res.text()}`);
  return res.json();
}

export async function apiPut<T>(path: string, body?: unknown): Promise<T> {
  const res = await fetch(`${API_URL}${path}`, {
    method: "PUT",
    headers: { "Content-Type": "application/json", ...getAuthHeader() },
    body: body ? JSON.stringify(body) : undefined,
  });
  if (!res.ok) throw new Error(`API Error ${res.status}: ${await res.text()}`);
  return res.json();
}

export async function apiDelete<T>(path: string): Promise<T> {
  const res = await fetch(`${API_URL}${path}`, {
    method: "DELETE",
    headers: { ...getAuthHeader() },
  });
  if (!res.ok) throw new Error(`API Error ${res.status}: ${await res.text()}`);
  return res.json();
}

// ── Typed API functions ───────────────────────────────────────────────────────

export interface VoiceEntry {
  id: string;
  created_at: string;
  primary_emotion: string;
  emotion_confidence: number;
  energy_score: number;
  calmness_score: number;
  mood_score: number;
  clarity_score: number;
  transcription: string;
  duration_seconds: number;
  pitch_mean: number;
  speech_rate: number;
  pause_count: number;
  filler_rate: number;
}

export interface MoodAlert {
  id: string;
  created_at: string;
  alert_type: string;
  severity: string;
  message: string;
  suggested_action: string | null;
  is_read: boolean;
}

export interface TrendsData {
  entries_count: number;
  streak: number;
  most_common_emotion: string | null;
  this_week: Record<string, number | null>;
  last_week: Record<string, number | null>;
  insights: string[];
}

export interface AnalyzeResult {
  entry_id: string;
  emotion: string;
  confidence: number;
  mood_scores: { energy: number; calmness: number; mood: number; clarity: number };
  transcription: string;
  features: Record<string, unknown>;
  insight: string;
  new_alerts: MoodAlert[];
}

export interface ChatChannel {
  id: string;
  title: string;
  created_at: string;
  updated_at: string;
  message_count: number;
}

export const api = {
  getEntries: (days = 30) =>
    apiGet<VoiceEntry[]>(`/api/entries?days=${days}`),

  getTrends: () =>
    apiGet<TrendsData>(`/api/trends`),

  getAlerts: () =>
    apiGet<MoodAlert[]>(`/api/alerts`),

  markAlertRead: (alertId: string) =>
    apiPut<{ success: boolean }>(`/api/alerts/${alertId}/read`),

  chat: (message: string, channelId: string) =>
    apiPost<{ response: string; message_id: string }>("/api/chat", { message, channel_id: channelId }),

  getChatHistory: (channelId: string) =>
    apiGet<Array<{ id: string; role: string; content: string; created_at: string }>>(
      `/api/chat/history?channel_id=${channelId}`
    ),

  getChannels: () =>
    apiGet<ChatChannel[]>("/api/chat/channels"),

  createChannel: (title = "New Chat") =>
    apiPost<ChatChannel>("/api/chat/channels", { title }),

  renameChannel: (channelId: string, title: string) =>
    apiPut<{ success: boolean; title: string }>(`/api/chat/channels/${channelId}`, { title }),

  deleteChannel: (channelId: string) =>
    fetch(`${API_URL}/api/chat/channels/${channelId}`, {
      method: "DELETE",
      headers: { ...getAuthHeader() },
    }).then(r => r.json()),

  getWeeklyReport: () =>
    apiGet<Record<string, unknown>>("/api/weekly-report"),

  getDailyPrompt: () =>
    apiGet<{ prompt: string; context: string }>("/api/daily-prompt"),

  logSleep: (entryId: string, sleepHours: number) =>
    apiPost<{ success: boolean; sleep_hours: number }>(`/api/entries/${entryId}/sleep`, { sleep_hours: sleepHours }),

  getSleepCorrelation: () =>
    apiGet<Array<{ date: string; sleep_hours: number; mood_score: number; energy_score: number; emotion: string }>>("/api/sleep-correlation"),

  inviteTrustedMember: (email: string) =>
    apiPost<{ success: boolean; message: string }>("/api/trusted-circle/invite", { email }),

  getTrustedMembers: () =>
    apiGet<{ id: string; email: string; joined_at: string }[]>("/api/trusted-circle"),

  removeTrustedMember: (id: string) =>
    apiDelete<{ success: boolean; message: string }>(`/api/trusted-circle/${id}`),

  broadcastWeeklyReport: () =>
    apiPost<{ success: boolean; sent_count: number; errors?: string[] }>("/api/weekly-report/broadcast", {}),

  analyzeAudio: async (audioBlob: Blob): Promise<AnalyzeResult> => {
    const formData = new FormData();
    formData.append("audio", audioBlob, "recording.webm");
    
    const headers: Record<string, string> = {};
    if (typeof window !== "undefined") {
      const token = localStorage.getItem("token");
      if (token) headers["Authorization"] = `Bearer ${token}`;
    }

    const res = await fetch(`${API_URL}/api/analyze`, {
      method: "POST",
      headers,
      body: formData,
    });
    if (!res.ok) throw new Error(`Analyze error ${res.status}`);
    return res.json();
  },

  setBaseline: async (audioBlob: Blob): Promise<{msg: string, has_baseline: boolean}> => {
    const formData = new FormData();
    formData.append("audio", audioBlob, "calibration.webm");
    
    const headers: Record<string, string> = {};
    if (typeof window !== "undefined") {
      const token = localStorage.getItem("token");
      if (token) headers["Authorization"] = `Bearer ${token}`;
    }

    const res = await fetch(`${API_URL}/api/auth/baseline`, {
      method: "POST",
      headers,
      body: formData,
    });
    if (!res.ok) throw new Error(`Baseline error ${res.status}`);
    return res.json();
  }
};