Skip to content

Commit

Permalink
fix: handling messages not having ids
Browse files Browse the repository at this point in the history
  • Loading branch information
PrinceBaghel258025 committed Nov 18, 2023
1 parent 7ea93a1 commit 967f5b4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/app/[uid]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export default function LoggedInLayout({
audioRef.current.load();
audioRef.current.play();
}
if (!store.audioSrc) {
audioRef.current?.pause();
}
}, [store.audioSrc]);

return (
Expand Down
36 changes: 27 additions & 9 deletions src/app/api/tts/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { env } from "@/app/env.mjs";
import { db } from "@/lib/db";
import { chats, Chat as ChatSchema } from "@/lib/db/schema";
import { ChatLog } from "@/lib/types";
import { ChatEntry, ChatLog } from "@/lib/types";
import { saveAudio } from "@/utils/apiHelper";
import { nanoid } from "ai";
import { eq } from "drizzle-orm";
import { NextResponse } from "next/server";
import OpenAI from "openai";
Expand All @@ -11,16 +12,19 @@ export const maxDuration = 60;

const bodyobj = z.object({
text: z.string().min(1),
messageId: z.string().min(1),
messageId: z.string().min(1).optional(),
orgId: z.string().min(1),
chatId: z.string().min(1),
index: z.coerce.number().min(0),
});

export async function POST(request: Request) {
const body = bodyobj.parse(await request.json());
const b = await request.json();
console.log("b", b);
const body = bodyobj.parse(b);

const text = body.text;
const messageId = body.messageId;
let messageId = body.messageId;
const orgId = body.orgId;
const chatId = body.chatId;
console.log("id of the message", body.messageId);
Expand Down Expand Up @@ -52,12 +56,26 @@ export async function POST(request: Request) {
chatlog = JSON.parse(msg as string) as ChatLog;
}

// finding the message with the given id if not found return last message from log
let message = chatlog.log.find((msg) => msg.id === messageId);
if (!message) {
message = chatlog.log[chatlog.log.length - 1];
message.id = messageId;
// find the message according to the index if no messageId is given
let message: ChatEntry | undefined;
if (!messageId) {
message = chatlog.log[body.index];
// update all the messages of chatlog.log to have an id using nanoid()
chatlog.log = chatlog.log.map((msg) => {
msg.id = msg.id ? msg.id : nanoid();
return msg;
});
} else {
// finding the message with the given id if not found return last message from log
message = chatlog.log.find((msg) => msg.id === messageId);
if (!message) {
message = chatlog.log[chatlog.log.length - 1];
message.id = messageId;
}
}

messageId = messageId ? messageId : chatlog.log[body.index].id;

// adding the audio to the message
const audioUrl = await saveAudio({ buffer, chatId, messageId });
message.audio = audioUrl;
Expand Down
2 changes: 2 additions & 0 deletions src/components/chatmessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ const ChatMessage = (props: ChatMessageProps) => {
store.setAudioSrc(audioSrc);
return;
}
store.setAudioSrc(undefined);
const text = props.chat.content;
setIsFetchingAudioBuffer(true);
try {
Expand All @@ -144,6 +145,7 @@ const ChatMessage = (props: ChatMessageProps) => {
body: JSON.stringify({
text: text,
messageId: id,
index: props.messageIndex,
orgId: props.orgId,
chatId: props.chatId,
voice: "en-US",
Expand Down
6 changes: 3 additions & 3 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { create } from "zustand";
import { persist } from "zustand/middleware";

type Store = {
audioSrc: string;
setAudioSrc: (src: string) => void;
audioSrc: string | undefined;
setAudioSrc: (src: string | undefined) => void;
reset: () => void;
};

Expand All @@ -15,7 +15,7 @@ export const useStore = create<Store>()(
audioSrc: "",
setAudioSrc: (src) => set((state) => ({ ...state, audioSrc: src })),
reset: () => {
set({ audioSrc: "" });
set({ audioSrc: undefined });
},
}),
{
Expand Down

0 comments on commit 967f5b4

Please sign in to comment.