diff --git a/app/lib/chatHistory.ts b/app/lib/chatHistory.ts index 915f435..1025ed6 100644 --- a/app/lib/chatHistory.ts +++ b/app/lib/chatHistory.ts @@ -19,7 +19,7 @@ const CACHE_KEY_BASE = "https://my-code.utcode.net/chatHistory"; interface Context { drizzle: Awaited>; auth: Auth; - userId: string; + userId?: string; } /** * drizzleが初期化されてなければ初期化し、 @@ -40,10 +40,9 @@ async function initAll(ctx?: Partial): Promise { const session = await ctx.auth.api.getSession({ headers: await headers(), }); - if (!session) { - throw new Error("Not authenticated"); + if (session) { + ctx.userId = session.user.id; } - ctx.userId = session.user.id; } return ctx as Context; } @@ -64,7 +63,9 @@ export async function addChat( context?: Partial ) { const { drizzle, userId } = await initAll(context); - + if (!userId) { + throw new Error("Not authenticated"); + } const [newChat] = await drizzle .insert(chat) .values({ @@ -106,6 +107,9 @@ export async function getChat( context?: Partial ): Promise { const { drizzle, userId } = await initAll(context); + if (!userId) { + return []; + } const chats = await drizzle.query.chat.findMany({ where: and(eq(chat.userId, userId), eq(chat.docsId, docsId)), @@ -131,6 +135,9 @@ export async function getChatFromCache( context?: Partial ) { const { drizzle, auth, userId } = await initAll(context); + if (!userId) { + return []; + } const cache = await getCache(); const cachedResponse = await cache.match( diff --git a/wrangler.jsonc b/wrangler.jsonc index 2b5b387..85b8217 100644 --- a/wrangler.jsonc +++ b/wrangler.jsonc @@ -2,13 +2,18 @@ "main": ".open-next/worker.js", "name": "my-code", "compatibility_date": "2025-03-25", - "compatibility_flags": [ - "nodejs_compat", - "global_fetch_strictly_public", - ], + "compatibility_flags": ["nodejs_compat", "global_fetch_strictly_public"], + "observability": { + "logs": { + "enabled": true, + "head_sampling_rate": 1, + "invocation_logs": true, + "persist": true, + }, + }, "assets": { "directory": ".open-next/assets", - "binding": "ASSETS" + "binding": "ASSETS", }, "services": [ {