forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeedback.ts
More file actions
176 lines (160 loc) · 4.66 KB
/
Copy pathfeedback.ts
File metadata and controls
176 lines (160 loc) · 4.66 KB
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
/**
* Server-only client for the backend's feedback-report admin API.
*
* The daily thumbs-down report cannot be read straight out of Firestore the
* way CSAT stats are, because the conversation text it points at is encrypted
* per user with a key the admin app does not hold (`ENCRYPTION_SECRET` lives
* only in the backend). So this dashboard reads pointers and asks the backend
* to decrypt one event's window at a time.
*
* Both hops are authorized: the route handler checks the browser's Firebase
* token against `adminData/{uid}` first, then this module adds `X-Admin-Key`,
* which only exists in Cloud Run's runtime secrets. Neither key ever reaches
* the browser.
*/
const OMI_API_BASE_URL = process.env.NEXT_PUBLIC_OMI_API_URL;
export class FeedbackApiError extends Error {
constructor(message: string, public status: number) {
super(message);
this.name = "FeedbackApiError";
}
}
async function feedbackApi<T>(
endpoint: string,
adminUid: string,
init: RequestInit = {}
): Promise<T> {
const adminKey = process.env.OMI_API_SECRET_KEY;
if (!adminKey) {
throw new FeedbackApiError("OMI_API_SECRET_KEY is not configured", 500);
}
if (!OMI_API_BASE_URL) {
throw new FeedbackApiError(
"NEXT_PUBLIC_OMI_API_URL is not configured",
500
);
}
const response = await fetch(`${OMI_API_BASE_URL}${endpoint}`, {
...init,
headers: {
"Content-Type": "application/json",
"X-Admin-Key": adminKey,
// Who is asking. The key is shared across the deployment, so it alone
// cannot tell the backend's audit log which admin read a user's chat.
// This uid has already been checked against `adminData/{uid}` by the
// route handler; it travels server-side only.
"X-Admin-User": adminUid,
...init.headers,
},
cache: "no-store",
});
if (!response.ok) {
let detail = `${response.status} ${response.statusText}`;
try {
const body = await response.json();
if (body?.detail) detail = String(body.detail);
} catch {
// Non-JSON error body; the status line is all we have.
}
throw new FeedbackApiError(detail, response.status);
}
return (await response.json()) as T;
}
export type FeedbackEvent = {
id: string;
uid: string;
surface: string;
target_kind: string;
target_id: string;
value: number;
created_at: string;
reason?: string | null;
comment?: string | null;
platform?: string | null;
app_version?: string | null;
app_id?: string | null;
chat_session_id?: string | null;
prompt_name?: string | null;
prompt_commit?: string | null;
};
export type FeedbackContextTurn = {
message_id: string;
sender: string;
created_at: string;
position: "before" | "rated" | "after";
seconds_from_rated: number;
};
export type FeedbackContextPointer = {
event_id: string;
uid: string;
target_kind: string;
target_id: string;
turns: FeedbackContextTurn[];
follow_up_count: number;
follow_up_window_seconds: number;
truncated_before: boolean;
truncated_after: boolean;
resolution_error?: string | null;
};
export type FeedbackReportEntry = {
event: FeedbackEvent;
context: FeedbackContextPointer;
};
export type FeedbackReport = {
date: string;
generated_at: string;
total_negative: number;
counts_by_surface: Record<string, number>;
counts_by_reason: Record<string, number>;
counts_by_platform: Record<string, number>;
entries: FeedbackReportEntry[];
truncated: boolean;
};
export type FeedbackContextHydratedTurn = FeedbackContextTurn & {
text: string;
};
export type FeedbackContextHydrated = {
event_id: string;
target_kind: string;
target_id: string;
turns: FeedbackContextHydratedTurn[];
unavailable: string[];
};
export function listReportDates(
adminUid: string,
limit = 30
): Promise<{ dates: string[] }> {
return feedbackApi(`/v1/admin/feedback/reports?limit=${limit}`, adminUid);
}
export function getReport(
adminUid: string,
date: string
): Promise<FeedbackReport> {
return feedbackApi(
`/v1/admin/feedback/reports/${encodeURIComponent(date)}`,
adminUid
);
}
export function generateReport(
adminUid: string,
date: string
): Promise<{ date: string; total_negative: number; truncated: boolean }> {
return feedbackApi(
`/v1/admin/feedback/reports/${encodeURIComponent(date)}/generate`,
adminUid,
{ method: "POST" }
);
}
export function getEventContext(
adminUid: string,
eventId: string,
reportDate?: string
): Promise<FeedbackContextHydrated> {
const query = reportDate
? `?report_date=${encodeURIComponent(reportDate)}`
: "";
return feedbackApi(
`/v1/admin/feedback/events/${encodeURIComponent(eventId)}/context${query}`,
adminUid
);
}