-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest-event-id-gen.ts
More file actions
220 lines (186 loc) · 6.42 KB
/
test-event-id-gen.ts
File metadata and controls
220 lines (186 loc) · 6.42 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
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
/**
* Test different event ID generation strategies
*/
import { connectToSuperhuman, disconnect } from "./src/superhuman-api";
import { getToken, gmailFetch } from "./src/token-api";
import { listAccounts } from "./src/accounts";
const CDP_PORT = 9333;
const SUPERHUMAN_BACKEND_BASE = "https://mail.superhuman.com/~backend";
// Current implementation (random mixed-case alphanumeric)
function generateEventIdV1(): string {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let id = "event_";
for (let i = 0; i < 17; i++) {
id += chars.charAt(Math.floor(Math.random() * chars.length));
}
return id;
}
// V2: Start with "11" like team ID prefix
function generateEventIdV2(): string {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let id = "event_11";
for (let i = 0; i < 15; i++) {
id += chars.charAt(Math.floor(Math.random() * chars.length));
}
return id;
}
// V3: Match the exact pattern - 18 chars like team suffix
function generateEventIdV3(): string {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let id = "event_";
for (let i = 0; i < 18; i++) {
id += chars.charAt(Math.floor(Math.random() * chars.length));
}
return id;
}
// V4: Use crypto for better randomness
function generateEventIdV4(): string {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const bytes = new Uint8Array(18);
crypto.getRandomValues(bytes);
let id = "event_";
for (const byte of bytes) {
id += chars[byte % 62];
}
return id;
}
async function testEventId(eventIdGenerator: () => string, name: string) {
console.log(`\n=== Testing ${name} ===`);
const conn = await connectToSuperhuman(CDP_PORT);
if (!conn) {
console.error("Failed to connect to Superhuman");
return null;
}
const { Runtime } = conn;
// Get account info
const accounts = await listAccounts(conn);
const currentAccount = accounts.find((a) => a.isCurrent);
if (!currentAccount) {
console.error("No current account");
await disconnect(conn);
return null;
}
// Get idToken and googleId
const tokenResult = await Runtime.evaluate({
expression: `
(() => {
const ga = window.GoogleAccount;
const authData = ga?.credential?._authData;
return {
idToken: authData?.idToken,
googleId: authData?.googleId,
};
})()
`,
returnByValue: true,
});
const { idToken, googleId } = tokenResult.result.value;
// Get OAuth token for Gmail
const oauthToken = await getToken(conn, currentAccount.email);
// Get a thread for context
const threadId = "19b4d2a9561472a1";
const thread = await gmailFetch(oauthToken.accessToken, `/threads/${threadId}?format=full`);
if (!thread || !thread.messages) {
console.error("Failed to fetch thread");
await disconnect(conn);
return null;
}
// Build thread messages
const threadMessages = thread.messages.slice(0, 2).map((msg: any) => {
const headers = msg.payload?.headers || [];
const getHeader = (name: string) => headers.find((h: any) => h.name.toLowerCase() === name.toLowerCase())?.value || "";
return {
message_id: msg.id,
subject: getHeader("Subject"),
body: msg.snippet?.substring(0, 500) || "",
};
});
const eventId = eventIdGenerator();
console.log(`Generated event ID: ${eventId}`);
console.log(` Length: ${eventId.length}`);
console.log(` Suffix: ${eventId.replace('event_', '')}`);
console.log(` Suffix length: ${eventId.replace('event_', '').length}`);
const payload = {
session_id: crypto.randomUUID(),
question_event_id: eventId,
query: "what is this about?",
chat_history: [],
user: {
provider_id: googleId,
email: currentAccount.email,
name: "",
company: "",
position: "",
},
local_datetime: new Date().toISOString(),
current_thread_id: threadId,
current_thread_messages: threadMessages,
};
const response = await fetch(`${SUPERHUMAN_BACKEND_BASE}/v3/ai.askAIProxy`, {
method: "POST",
headers: {
Authorization: `Bearer ${idToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
console.log(`Response status: ${response.status}`);
const responseText = await response.text();
let result = null;
try {
result = JSON.parse(responseText);
if (result.error) {
console.log(`Error: ${result.error}`);
} else if (result.response) {
console.log(`SUCCESS! Response: ${result.response.substring(0, 100)}...`);
}
} catch {
console.log(`Response: ${responseText.substring(0, 200)}`);
}
await disconnect(conn);
return { eventId, status: response.status, result };
}
async function main() {
console.log("=== Event ID Format Testing ===\n");
// Show reference formats
console.log("Reference formats:");
console.log(` Team ID: team_11STeHt1wOE5UlznX9`);
console.log(` Team suffix length: 18`);
console.log(` Expected event ID: event_11VNPdc4sKP2pEaKSz`);
console.log(` Expected suffix length: 17`);
// Generate samples
console.log("\nSample IDs from each generator:");
console.log(` V1 (17 chars): ${generateEventIdV1()}`);
console.log(` V2 (11+15=17): ${generateEventIdV2()}`);
console.log(` V3 (18 chars): ${generateEventIdV3()}`);
console.log(` V4 (crypto 18): ${generateEventIdV4()}`);
// Test each generator
const results: any[] = [];
// Test V1
const r1 = await testEventId(generateEventIdV1, "V1: 17 random chars");
if (r1) results.push({ name: "V1", ...r1 });
// Wait between tests
await new Promise(r => setTimeout(r, 1000));
// Test V2
const r2 = await testEventId(generateEventIdV2, "V2: 11 + 15 chars");
if (r2) results.push({ name: "V2", ...r2 });
await new Promise(r => setTimeout(r, 1000));
// Test V3
const r3 = await testEventId(generateEventIdV3, "V3: 18 chars");
if (r3) results.push({ name: "V3", ...r3 });
await new Promise(r => setTimeout(r, 1000));
// Test V4
const r4 = await testEventId(generateEventIdV4, "V4: crypto 18 chars");
if (r4) results.push({ name: "V4", ...r4 });
// Summary
console.log("\n\n=== SUMMARY ===\n");
for (const r of results) {
console.log(`${r.name}: status=${r.status}, eventId=${r.eventId}`);
if (r.result?.error) {
console.log(` Error: ${r.result.error}`);
} else if (r.status === 200) {
console.log(` SUCCESS`);
}
}
}
main().catch(console.error);