Skip to content

Commit 72fc0f6

Browse files
committed
Revert "Merge pull request #71 from ut-code/auth"
This reverts commit b74201a, reversing changes made to d8ce0f4.
1 parent b74201a commit 72fc0f6

21 files changed

+112
-1294
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,3 @@ yarn-error.log*
4242
# typescript
4343
*.tsbuildinfo
4444
next-env.d.ts
45-
46-
/app/generated/prisma

README.md

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,11 @@ https://my-code.utcode.net
77
npm ci
88
```
99

10-
ルートディレクトリに .env.local という名前のファイルを作成し、以下の内容を記述
10+
ルートディレクトリに .env.local という名前のファイルを作成し、Gemini APIキーを設定してください
1111
```dotenv
12-
API_KEY=GeminiAPIキー
13-
BETTER_AUTH_URL=http://localhost:3000
12+
API_KEY="XXXXXXXX"
1413
```
1514

16-
prismaの開発環境を起動
17-
(.env にDATABASE_URLが自動的に追加される)
18-
```bash
19-
npx prisma dev
20-
```
21-
別ターミナルで
22-
```bash
23-
npx prisma db push
24-
```
25-
26-
### 本番環境の場合
27-
28-
上記の環境変数以外に、
29-
* BETTER_AUTH_SECRET に任意の文字列
30-
* DATABASE_URL に本番用のPostgreSQLデータベースURL
31-
* GOOGLE_CLIENT_IDとGOOGLE_CLIENT_SECRETにGoogle OAuthのクライアントIDとシークレット https://www.better-auth.com/docs/authentication/google
32-
* GITHUB_CLIENT_IDとGITHUB_CLIENT_SECRETにGitHub OAuthのクライアントIDとシークレット https://www.better-auth.com/docs/authentication/github
33-
34-
3515
## 開発環境
3616

3717
```bash

app/[docs_id]/chatForm.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use client";
22

33
import { useState, FormEvent, useEffect } from "react";
4+
import { askAI } from "@/app/actions/chatActions";
45
import useSWR from "swr";
56
import {
67
getQuestionExample,
@@ -9,8 +10,7 @@ import {
910
import { getLanguageName } from "../pagesList";
1011
import { DynamicMarkdownSection } from "./pageContent";
1112
import { useEmbedContext } from "../terminal/embedContext";
12-
import { useChatHistoryContext } from "./chatHistory";
13-
import { askAI } from "@/actions/chatActions";
13+
import { ChatMessage, useChatHistoryContext } from "./chatHistory";
1414

1515
interface ChatFormProps {
1616
docs_id: string;
@@ -71,6 +71,8 @@ export function ChatForm({
7171
setIsLoading(true);
7272
setErrorMessage(null); // Clear previous error message
7373

74+
const userMessage: ChatMessage = { sender: "user", text: inputValue };
75+
7476
let userQuestion = inputValue;
7577
if (!userQuestion && exampleData) {
7678
// 質問が空欄なら、質問例を使用
@@ -81,19 +83,19 @@ export function ChatForm({
8183

8284
const result = await askAI({
8385
userQuestion,
84-
docsId: docs_id,
8586
documentContent,
8687
sectionContent,
8788
replOutputs,
8889
files,
8990
execResults,
9091
});
9192

92-
if (result.error !== null) {
93+
if (result.error) {
9394
setErrorMessage(result.error);
9495
console.log(result.error);
9596
} else {
96-
addChat(result.chat);
97+
const aiMessage: ChatMessage = { sender: "ai", text: result.response };
98+
const chatId = addChat(result.targetSectionId, [userMessage, aiMessage]);
9799
// TODO: chatIdが指す対象の回答にフォーカス
98100
setInputValue("");
99101
close();

app/[docs_id]/chatHistory.tsx

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use client";
22

3-
import { ChatWithMessages } from "@/lib/chatHistory";
43
import {
54
createContext,
65
ReactNode,
@@ -9,10 +8,15 @@ import {
98
useState,
109
} from "react";
1110

11+
export interface ChatMessage {
12+
sender: "user" | "ai" | "error";
13+
text: string;
14+
}
15+
1216
export interface IChatHistoryContext {
13-
chatHistories: ChatWithMessages[];
14-
addChat: (chat: ChatWithMessages) => void;
15-
// updateChat: (sectionId: string, chatId: string, message: ChatMessage) => void;
17+
chatHistories: Record<string, Record<string, ChatMessage[]>>;
18+
addChat: (sectionId: string, messages: ChatMessage[]) => string;
19+
updateChat: (sectionId: string, chatId: string, message: ChatMessage) => void;
1620
}
1721
const ChatHistoryContext = createContext<IChatHistoryContext | null>(null);
1822
export function useChatHistoryContext() {
@@ -25,26 +29,65 @@ export function useChatHistoryContext() {
2529
return context;
2630
}
2731

28-
export function ChatHistoryProvider({
29-
children,
30-
initialChatHistories,
31-
}: {
32-
children: ReactNode;
33-
initialChatHistories: ChatWithMessages[];
34-
}) {
35-
const [chatHistories, setChatHistories] =
36-
useState<ChatWithMessages[]>(initialChatHistories);
32+
export function ChatHistoryProvider({ children }: { children: ReactNode }) {
33+
const [chatHistories, setChatHistories] = useState<
34+
Record<string, Record<string, ChatMessage[]>>
35+
>({});
3736
useEffect(() => {
38-
setChatHistories(initialChatHistories);
39-
}, [initialChatHistories]);
37+
// Load chat histories from localStorage on mount
38+
const chatHistories: Record<string, Record<string, ChatMessage[]>> = {};
39+
for (let i = 0; i < localStorage.length; i++) {
40+
const key = localStorage.key(i);
41+
if (key && key.startsWith("chat/") && key.split("/").length === 3) {
42+
const savedHistory = localStorage.getItem(key);
43+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
44+
const [_, sectionId, chatId] = key.split("/");
45+
if (savedHistory) {
46+
if (!chatHistories[sectionId]) {
47+
chatHistories[sectionId] = {};
48+
}
49+
chatHistories[sectionId][chatId] = JSON.parse(savedHistory);
50+
}
51+
}
52+
}
53+
setChatHistories(chatHistories);
54+
}, []);
4055

41-
const addChat = (chat: ChatWithMessages) => {
42-
// サーバー側で追加された新しいchatをクライアント側にも反映する
43-
setChatHistories([...chatHistories, chat]);
56+
const addChat = (sectionId: string, messages: ChatMessage[]): string => {
57+
const chatId = Date.now().toString();
58+
const newChatHistories = { ...chatHistories };
59+
if (!newChatHistories[sectionId]) {
60+
newChatHistories[sectionId] = {};
61+
}
62+
newChatHistories[sectionId][chatId] = messages;
63+
setChatHistories(newChatHistories);
64+
localStorage.setItem(
65+
`chat/${sectionId}/${chatId}`,
66+
JSON.stringify(messages)
67+
);
68+
return chatId;
69+
};
70+
const updateChat = (
71+
sectionId: string,
72+
chatId: string,
73+
message: ChatMessage
74+
) => {
75+
const newChatHistories = { ...chatHistories };
76+
if (newChatHistories[sectionId] && newChatHistories[sectionId][chatId]) {
77+
newChatHistories[sectionId][chatId] = [
78+
...newChatHistories[sectionId][chatId],
79+
message,
80+
];
81+
setChatHistories(newChatHistories);
82+
localStorage.setItem(
83+
`chat/${sectionId}/${chatId}`,
84+
JSON.stringify(newChatHistories[sectionId][chatId])
85+
);
86+
}
4487
};
4588

4689
return (
47-
<ChatHistoryContext.Provider value={{ chatHistories, addChat }}>
90+
<ChatHistoryContext.Provider value={{ chatHistories, addChat, updateChat }}>
4891
{children}
4992
</ChatHistoryContext.Provider>
5093
);

app/[docs_id]/page.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { MarkdownSection, splitMarkdown } from "./splitMarkdown";
66
import pyodideLock from "pyodide/pyodide-lock.json";
77
import { PageContent } from "./pageContent";
88
import { ChatHistoryProvider } from "./chatHistory";
9-
import { getChat } from "@/lib/chatHistory";
109

1110
export default async function Page({
1211
params,
@@ -45,10 +44,8 @@ export default async function Page({
4544

4645
const splitMdContent: MarkdownSection[] = splitMarkdown(mdContent);
4746

48-
const initialChatHistories = await getChat(docs_id);
49-
5047
return (
51-
<ChatHistoryProvider initialChatHistories={initialChatHistories}>
48+
<ChatHistoryProvider>
5249
<PageContent
5350
documentContent={mdContent}
5451
splitMdContent={splitMdContent}

app/[docs_id]/pageContent.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ export function PageContent(props: PageContentProps) {
9494
</div>
9595
<div key={`${index}-chat`}>
9696
{/* 右側に表示するチャット履歴欄 */}
97-
{chatHistories.filter((c) => c.sectionId === section.sectionId).map(
98-
({chatId, messages}) => (
97+
{Object.entries(chatHistories[section.sectionId] ?? {}).map(
98+
([chatId, messages]) => (
9999
<div
100100
key={chatId}
101101
className="max-w-xs mb-2 p-2 text-sm border border-base-content/10 rounded-sm shadow-sm bg-base-100"
@@ -104,20 +104,20 @@ export function PageContent(props: PageContentProps) {
104104
{messages.map((msg, index) => (
105105
<div
106106
key={index}
107-
className={`chat ${msg.role === "user" ? "chat-end" : "chat-start"}`}
107+
className={`chat ${msg.sender === "user" ? "chat-end" : "chat-start"}`}
108108
>
109109
<div
110110
className={clsx(
111111
"chat-bubble p-1!",
112-
msg.role === "user" &&
112+
msg.sender === "user" &&
113113
"bg-primary text-primary-content",
114-
msg.role === "ai" &&
114+
msg.sender === "ai" &&
115115
"bg-secondary-content dark:bg-neutral text-black dark:text-white",
116-
msg.role === "error" && "chat-bubble-error"
116+
msg.sender === "error" && "chat-bubble-error"
117117
)}
118118
style={{ maxWidth: "100%", wordBreak: "break-word" }}
119119
>
120-
<StyledMarkdown content={msg.content} />
120+
<StyledMarkdown content={msg.text} />
121121
</div>
122122
</div>
123123
))}

app/accountMenu.tsx

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)