Skip to content

Commit

Permalink
Add room title generation and update functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuaki640 committed Feb 21, 2024
1 parent f8b4779 commit c50206c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getAllRooms,
getRoom,
insertRoom,
updateRoom,
} from "./repositories/room-repository";
import { Messages } from "./schema";
import type { AppEnv } from "./types";
Expand Down Expand Up @@ -89,6 +90,17 @@ app.post("/chats/:roomId", async (c) => {
content: m.message,
}));

// 題名の生成
if (messageHistory.length === 0) {
const completion = await fetchCompletion(
c.var.openai,
`次の質問に対して、短くわかりやすい題名をつけてください。質問文: ${newMessage}`,
);
const roomTitle = completion.choices[0].message.content;
await updateRoom(db, roomId, { roomId, roomTitle });
}

// メッセージの生成と挿入
const completion = await fetchCompletion(
c.var.openai,
newMessage,
Expand Down
8 changes: 8 additions & 0 deletions src/repositories/room-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ export const getRoom = async (db: DrizzleD1Database, roomId: string) => {
export const getAllRooms = async (db: DrizzleD1Database) => {
return db.select().from(Rooms).orderBy(desc(Rooms.roomUpdated)).all();
};

export const updateRoom = async (
db: DrizzleD1Database,
roomId: string,
room: typeof Rooms.$inferInsert,
) => {
return db.update(Rooms).set(room).where(eq(Rooms.roomId, roomId)).execute();
};
4 changes: 2 additions & 2 deletions src/views/Room.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ export const Room: FC<{ props: Props }> = ({ props }) => (
<table>
<thead>
<tr>
<th>ID</th>
<th>{props.room.roomTitle ? "Title" : "ID"}</th>
<th>Created</th>
<th>Updated</th>
</tr>
</thead>
<tbody>
<tr>
<td>{props.room.roomId}</td>
<td>{props.room.roomTitle ?? props.room.roomId}</td>
<td>{props.room.roomCreated}</td>
<td>{props.room.roomUpdated}</td>
</tr>
Expand Down

0 comments on commit c50206c

Please sign in to comment.