Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions app/lib/chatHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const CACHE_KEY_BASE = "https://my-code.utcode.net/chatHistory";
interface Context {
drizzle: Awaited<ReturnType<typeof getDrizzle>>;
auth: Auth;
userId: string;
userId?: string;
}
/**
* drizzleが初期化されてなければ初期化し、
Expand All @@ -40,10 +40,9 @@ async function initAll(ctx?: Partial<Context>): Promise<Context> {
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;
}
Expand All @@ -64,7 +63,9 @@ export async function addChat(
context?: Partial<Context>
) {
const { drizzle, userId } = await initAll(context);

if (!userId) {
throw new Error("Not authenticated");
}
const [newChat] = await drizzle
.insert(chat)
.values({
Expand Down Expand Up @@ -106,6 +107,9 @@ export async function getChat(
context?: Partial<Context>
): Promise<ChatWithMessages[]> {
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)),
Expand All @@ -131,6 +135,9 @@ export async function getChatFromCache(
context?: Partial<Context>
) {
const { drizzle, auth, userId } = await initAll(context);
if (!userId) {
return [];
}

const cache = await getCache();
const cachedResponse = await cache.match(
Expand Down
15 changes: 10 additions & 5 deletions wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down