Skip to content

Commit

Permalink
#13 Database changes for the audit record are in place
Browse files Browse the repository at this point in the history
  • Loading branch information
santthosh committed Mar 17, 2024
1 parent 5e2e0af commit e582bd2
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- CreateTable
CREATE TABLE "Thread" (
"id" TEXT NOT NULL,
"object" JSONB,
"assistantId" TEXT,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,

CONSTRAINT "Thread_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Message" (
"id" TEXT NOT NULL,
"object" JSONB,
"threadId" TEXT,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,

CONSTRAINT "Message_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "Thread" ADD CONSTRAINT "Thread_assistantId_fkey" FOREIGN KEY ("assistantId") REFERENCES "Assistant"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Message" ADD CONSTRAINT "Message_threadId_fkey" FOREIGN KEY ("threadId") REFERENCES "Thread"("id") ON DELETE SET NULL ON UPDATE CASCADE;
20 changes: 20 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,24 @@ model Assistant {
account Account? @relation(fields: [accountOwner, accountOwnerType], references: [owner, ownerType])
created_at DateTime @default(now())
updated_at DateTime @updatedAt
Thread Thread[]
}

model Thread {
id String @id
object Json?
assistantId String?
assistant Assistant? @relation(fields: [assistantId], references: [id])
created_at DateTime @default(now())
updated_at DateTime @updatedAt
Message Message[]
}

model Message {
id String @id
object Json?
threadId String?
thread Thread? @relation(fields: [threadId], references: [id])
created_at DateTime @default(now())
updated_at DateTime @updatedAt
}
70 changes: 59 additions & 11 deletions src/app/api/openai/threads/[id]/messages/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,38 @@ export async function GET(req: NextRequest, res: NextResponse) {
let after = req.nextUrl.searchParams.get('after');
const openai = (await getOpenAIObjectForAssistant(req, prisma)) as OpenAI;

// @ts-ignore
let messagesResponse = await openai.beta.threads.messages.list(threadId, {
order: 'asc',
after: after,
});
return Response.json(messagesResponse, { status: 200 });
try {
// @ts-ignore
let messagesResponse = await openai.beta.threads.messages.list(threadId, {
order: 'asc',
after: after,
});

// If new messages are being pulled and presented then lets sync them up with the database
for (let i = 0; i < messagesResponse.data.length; i++) {
let message = messagesResponse.data[i];
await prisma.message.upsert({
where: {
id: message.id,
},
update: {
id: message.id,
threadId: threadId,
object: message as any,
},
create: {
id: message.id,
threadId: threadId,
object: message as any,
},
});
}

return Response.json(messagesResponse, { status: 200 });
} catch (err: any) {
console.log(err);
return Response.json({ message: err.message }, { status: err.status });
}
} catch (err: any) {
console.log(err);
return Response.json({ message: err.message }, { status: err.status });
Expand All @@ -39,11 +65,33 @@ export async function POST(req: NextRequest, res: NextResponse) {
content: body.message.content[0].text.value,
};

let createMessageResponse = await openai.beta.threads.messages.create(
threadId,
message
);
return Response.json(createMessageResponse, { status: 201 });
try {
let createMessageResponse = await openai.beta.threads.messages.create(
threadId,
message
);

await prisma.message.upsert({
where: {
id: createMessageResponse.id,
},
update: {
id: createMessageResponse.id,
threadId: threadId,
object: createMessageResponse as any,
},
create: {
id: createMessageResponse.id,
threadId: threadId,
object: createMessageResponse as any,
},
});

return Response.json(createMessageResponse, { status: 201 });
} catch (err: any) {
console.log(err);
return Response.json({ message: err.message }, { status: err.status });
}
} catch (err: any) {
console.log(err);
return Response.json({ message: err.message }, { status: err.status });
Expand Down
32 changes: 28 additions & 4 deletions src/app/api/openai/threads/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,40 @@ export async function POST(req: NextRequest, res: NextResponse) {
try {
const openai = (await getOpenAIObjectForAssistant(req, prisma)) as OpenAI;

let assistantId = req.headers.get('X-Assistant-Id');

let metadata: any = {};
let fingerprint = req.headers.get('X-Fingerprint');
if (fingerprint) {
metadata['fingerprint'] = fingerprint;
}

let createThreadResponse = await openai.beta.threads.create({
metadata: metadata,
});
return Response.json(createThreadResponse, { status: 201 });
try {
let createThreadResponse = await openai.beta.threads.create({
metadata: metadata,
});

await prisma.thread.upsert({
where: {
id: createThreadResponse.id,
},
update: {
id: createThreadResponse.id,
assistantId: assistantId,
object: createThreadResponse as any,
},
create: {
id: createThreadResponse.id,
assistantId: assistantId,
object: createThreadResponse as any,
},
});

return Response.json(createThreadResponse, { status: 201 });
} catch (err: any) {
console.log(err);
return Response.json({ message: err.message }, { status: err.status });
}
} catch (err: any) {
console.log(err);
return Response.json({ message: err.message }, { status: err.status });
Expand Down
1 change: 0 additions & 1 deletion src/app/assistants/[id]/chat/ChatPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ export default function ChatPopup(props: ChatProps) {
props.assistant.id,
fingerprint
);
console.log(threadResponse);
thread = threadResponse.id;
setCurrentThread(threadResponse.id);
}
Expand Down

0 comments on commit e582bd2

Please sign in to comment.