aboutsummaryrefslogtreecommitdiff
path: root/apps/web/server/index.ts
blob: 1343be53af769337c734344f8c95ae8f3a5df530 (plain) (blame)
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import { AppLoadContext } from "@remix-run/cloudflare";

import * as cheerio from "cheerio";
import { createOpenAI } from "@ai-sdk/openai";
import { zValidator } from "@hono/zod-validator";
import { getSessionFromRequest } from "@supermemory/authkit-remix-cloudflare/src/session";
import { convertToCoreMessages, generateText, streamText } from "ai";
import { putEncryptedKV } from "encrypt-workers-kv";
import { Hono } from "hono";
import { z } from "zod";

const app = new Hono<{ Bindings: Env }>();

app.onError(async (err, c) => {
	return c.text(err.message, { status: 500 });
});

app.get("/api/metadata", async (c) => {
	const url = c.req.query("url");
	if (!url) {
		return c.text("URL is required", { status: 400 });
	}

	const cacheKey = `metadata:${url}`;

	// Try to get cached metadata
	const cachedMetadata = await c.env.METADATA_KV.get(cacheKey, "json");
	if (cachedMetadata) {
		return c.json(cachedMetadata);
	}

	// If not cached, fetch and parse metadata
	try {
		const response = await fetch(url);
		const html = await response.text();
		const $ = cheerio.load(html);

		// Try multiple image selectors in order of preference
		const image =
			$('meta[property="og:image"]').attr("content") ||
			$('meta[property="twitter:image"]').attr("content") ||
			$('meta[name="thumbnail"]').attr("content") ||
			$('link[rel="image_src"]').attr("href") ||
			$('img[itemprop="image"]').attr("src") ||
			$("img").first().attr("src") ||
			"";

		// Convert relative image URLs to absolute
		const absoluteImage = image ? new URL(image, url).toString() : "";

		const metadata = {
			title: $("title").text() || $('meta[property="og:title"]').attr("content") || "",
			description:
				$('meta[name="description"]').attr("content") ||
				$('meta[property="og:description"]').attr("content") ||
				"",
			image: absoluteImage,
		};

		// Cache the metadata
		await c.env.METADATA_KV.put(cacheKey, JSON.stringify(metadata), {
			expirationTtl: 7 * 24 * 60 * 60,
		}); // 7 days TTL

		return c.json(metadata);
	} catch (error) {
		console.error("Error fetching metadata:", error);
		return c.json({ error: "Failed to fetch metadata" }, 500);
	}
});

app.get("/api/session", async (c) => {
	const fakeContext = {
		cloudflare: {
			env: c.env,
		},
	};
	const session = await getSessionFromRequest(c.req.raw, fakeContext as AppLoadContext);
	if (!session) {
		return c.json({ error: "No session found" }, 401);
	}
	return c.json(session);
});

app.all("/backend/*", async (c) => {
	const backendUrl = c.env.BACKEND_URL ?? "https://supermemory-backend.dhravya.workers.dev";
	const path = c.req.path.replace("/backend", "");
	const searchParams = new URL(c.req.url).searchParams.toString();
	const queryString = searchParams ? `?${searchParams}` : "";
	const url = `${backendUrl}${path}${queryString}`;

	const headers = new Headers(c.req.raw.headers);
	headers.delete("host");

	let body;
	if (c.req.raw.body) {
		try {
			// Use tee() to create a copy of the body stream
			const [stream1, stream2] = c.req.raw.body.tee();
	
			const reader = stream2.getReader();
			const chunks = [];
			let done = false;

			while (!done) {
				const { value, done: isDone } = await reader.read();
				if (value) {
					chunks.push(value);
				}
				done = isDone;
			}

			const bodyText = new TextDecoder().decode(
				chunks.reduce((acc, chunk) => {
					const tmp = new Uint8Array(acc.length + chunk.length);
					tmp.set(acc);
					tmp.set(chunk, acc.length);
					return tmp;
				}, new Uint8Array(0)),
			);

			if (c.req.method === "POST") {
				try {
					const parsedBody = JSON.parse(bodyText);
					body = JSON.stringify(parsedBody);
				} catch (e) {
					console.error("Invalid JSON in request body:", bodyText);
					return c.json({ 
						error: "Invalid JSON in request body",
						details: bodyText.substring(0, 100) + "..." // Show partial body for debugging
					}, 400);
				}
			} else {
				body = bodyText;
			}
		} catch (error) {
			console.error("Error reading request body:", error);
			return c.json({ 
				error: "Failed to process request body",
				details: error instanceof Error ? error.message : "Unknown error",
				path: path
			}, 400);
		}
	}

	const fetchOptions: RequestInit = {
		method: c.req.method,
		headers: headers,
		...(body && { body }),
	};

	if (c.req.method !== "GET" && c.req.method !== "HEAD") {
		(fetchOptions as any).duplex = "half";
	}

	try {
		const response = await fetch(url, fetchOptions);

		const newHeaders = new Headers(response.headers);
		newHeaders.delete("set-cookie");

		const setCookieHeaders = response.headers.get("set-cookie");
		if (setCookieHeaders) {
			setCookieHeaders.split(", ").forEach((cookie: string) => {
				c.header("set-cookie", cookie);
			});
		}

		if (response.headers.get("content-type")?.includes("text/event-stream")) {
			return new Response(response.body, {
				status: response.status,
				statusText: response.statusText,
				headers: {
					...newHeaders,
					"content-type": "text/event-stream",
					"cache-control": "no-cache",
					connection: "keep-alive",
				},
			});
		}

		if (response.body && response.headers.get("content-type")?.includes("text/x-unknown")) {
			return new Response(response.body, {
				status: response.status,
				statusText: response.statusText,
				headers: newHeaders,
			});
		}

		let responseBody;
		try {
			const responseText = await response.text();
			try {
				responseBody = JSON.parse(responseText);
			} catch {
				responseBody = responseText;
			}
		} catch (error) {
			console.error("Error reading response:", error);
			return c.json({ 
				error: "Failed to read backend response",
				details: error instanceof Error ? error.message : "Unknown error",
				path: path
			}, 502);
		}

		return new Response(JSON.stringify(responseBody), {
			status: response.status,
			statusText: response.statusText,
			headers: {
				...newHeaders,
				"content-type": "application/json",
			},
		});
	} catch (error) {
		console.error("Error proxying request:", error);
		return c.json({ 
			error: "Failed to proxy request to backend",
			details: error instanceof Error ? error.message : "Unknown error",
			path: path,
			url: url
		}, 502);
	}
});

app.post("/api/ai/command", async (c) => {
	const { apiKey: key, messages, model = "gpt-4o-mini", system } = await c.req.json();

	const apiKey = key || c.env.OPENAI_API_KEY;

	if (!apiKey) {
		return c.json({ error: "Missing OpenAI API key." }, 401);
	}

	const openai = createOpenAI({ apiKey });

	try {
		const result = await streamText({
			maxTokens: 2048,
			messages: convertToCoreMessages(messages),
			model: openai(model),
			system: system,
		});

		return result.toDataStreamResponse();
	} catch (error) {
		console.error("Failed to process AI request:", error);
		return c.json({ error: "Failed to process AI request" }, 500);
	}
});

app.post("/api/ai/copilot", async (c) => {
	const { apiKey: key, model = "gpt-4o-mini", prompt, system } = await c.req.json();

	const apiKey = key || c.env.OPENAI_API_KEY;

	if (!apiKey) {
		return c.json({ error: "Missing OpenAI API key." }, 401);
	}

	const openai = createOpenAI({ apiKey });

	try {
		const result = await generateText({
			maxTokens: 50,
			model: openai(model),
			prompt: prompt,
			system,
			temperature: 0.7,
		});

		return c.json(result);
	} catch (error: any) {
		if (error.name === "AbortError") {
			return c.json(null, { status: 408 });
		}

		console.error("Failed to process AI request:", error);
		return c.json({ error: "Failed to process AI request" }, 500);
	}
});

app.all("/auth/notion/callback", zValidator("query", z.object({ code: z.string() })), async (c) => {
	const { code } = c.req.valid("query");

	const notionCredentials = btoa(`${c.env.NOTION_CLIENT_ID}:${c.env.NOTION_CLIENT_SECRET}`);

	const response = await fetch("https://api.notion.com/v1/oauth/token", {
		method: "POST",
		headers: {
			Authorization: `Basic ${notionCredentials}`,
			"Content-Type": "application/json",
		},
		body: JSON.stringify({
			grant_type: "authorization_code",
			code: code,
			redirect_uri:
				c.env.NODE_ENV === "production"
					? "https://supermemory.ai/auth/notion/callback"
					: "http://localhost:3000/auth/notion/callback",
		}),
	});

	const data = await response.json();

	const fakeContext = {
		cloudflare: {
			env: c.env,
		},
	};

	const currentUser = await getSessionFromRequest(c.req.raw, fakeContext as AppLoadContext);

	console.log(currentUser?.user.id);
	const success = !(data as any).error;

	if (!success) {
		return c.redirect(`/?error=${(data as any).error}`);
	}

	const accessToken = (data as any).access_token;

	// const key = await crypto.subtle.importKey(
	// 	"raw",
	// 	new TextEncoder().encode(c.env.WORKOS_COOKIE_PASSWORD),
	// 	{ name: "AES-GCM" },
	// 	false,
	// 	["encrypt", "decrypt"],
	// );

	// const encrypted = await crypto.subtle.encrypt(
	// 	{ name: "AES-GCM", iv: new Uint8Array(20) },
	// 	key,
	// 	new TextEncoder().encode(accessToken),
	// );

	// const encryptedString = btoa(String(encrypted));

	// await c.env.ENCRYPTED_TOKENS.put(`${currentUser?.user.id}-notion`, encryptedString);

	await putEncryptedKV(
		c.env.ENCRYPTED_TOKENS,
		`${currentUser?.user.id}-notion`,
		accessToken,
		`${c.env.WORKOS_COOKIE_PASSWORD}-${currentUser?.user.id}`,
	);

	return c.redirect(`/?success=${success}&integration=notion`);
});

export default app;