Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres + drizzleORM #6

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ NM_EMAIL=
NM_PASS=

# 10 digit bot number
SYSTEM_NUMBER=
SYSTEM_NUMBER=

# Database
DATABASE_URL=postgres://insightsjcet:Insightsjcet2024@db:5432/insight
POSTGRES_USER=insightsjcet
POSTGRES_HOST=db
POSTGRES_DB=insight
POSTGRES_PASSWORD=Insightsjcet2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,5 @@ dist

.wwebjs_cache
authContainer/*
localAuth/*
index.js
Binary file modified bun.lockb
Binary file not shown.
193 changes: 193 additions & 0 deletions cache/queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import type { CoreMessage, ToolContent } from "ai";
import { prefixRedis, redisClient } from "./client";
import type WAWebJS from "whatsapp-web.js";
import type { generateTools } from "@/tools";
import type { User } from "./user";
import { extractFromText } from "@/lib/chat";

type Message = WAWebJS.Message
type GetReturn<T extends AsyncAnyFunc> = Awaited<ReturnType<T>>
type Tools = GetReturn<GetReturn<typeof generateTools>>

const getMessageType = (m: Message) => {
const mm = m.body.trim()
if (mm.length !== 0) return mm

if (m.hasMedia) {
switch (m.type) {
case "image": return "** user sent a image **"
case "ptt":
case "audio": return "** user sent an audio message **"
// case "document": return "** user sent a document **"
// case "video": return "** user sent a video **"
}
}

return ""
}


export class MessageQueues {
private key
public coreMessages: CoreMessage[]
public texts: string[] = []

constructor(private number: string, private messages: Message[] = []) {
this.number = number;
this.key = MessageQueues.setKey(number);
this.coreMessages = MessageQueues.MessageToCore(messages)
}

addMessage(messages: Message[]) {
this.coreMessages = this.coreMessages.concat(MessageQueues.MessageToCore(messages))
}

private static setKey(number: string) {
return `${prefixRedis}status:${number}`;
}

static MessageToCore(messages: Message[]): CoreMessage[] {
return messages.map(m => {

if (!m.fromMe) {
const mmm = getMessageType(m)
return {
content: mmm,
role: "user"
} as CoreMessage
}

const { role, content } = extractFromText(m.body)

if (role === "tool")
return {
role,
content: [{
toolName: content.toolName,
result: `invoked tool with args: ${JSON.stringify(content.args)}`,
}] as ToolContent
}

return {
role,
content
} as CoreMessage
})
}

static CoreToMessage(toolResults: Tools["toolResults"]) {
const texts: string[] = []
for (const toolResult of toolResults) {
console.log("A:", toolResult.toolName)
console.log("Arg:", toolResult.args)

texts.push(MessageQueues.getToolText(toolResult.toolName, toolResult.args))

if (typeof toolResult.result === "string") {
texts.push(MessageQueues.getSystemText(toolResult.result));
}
else {
texts.push(
MessageQueues.getSystemText(
JSON.stringify(toolResult.result)
)
);
}
}

return texts
}

static getToolText(toolName: string, args: any = {}) {
return `> \`\`\`${toolName}(${JSON.stringify(args)})\`\`\``
}

static getSystemText(str: string) {
return "`" + str + "`"
}

private async get() {
return await redisClient.get<string>(this.key);
}
async clear() {
await redisClient.del(this.key);
}

async addUserMessage(reply: string) {
const mm = reply.trim()
if (mm.length) {
console.log("A:", mm)
this.coreMessages.push({
role: "user",
content: mm
})
}
}

async sentTools(reply: Tools) {
if (reply.text) {
console.log("A:", reply.text)
this.texts.push(reply.text)
}

if (reply.toolResults)
this.texts = this.texts.concat(MessageQueues.CoreToMessage(reply.toolResults))

// if (reply.toolCalls)
// this.texts = this.texts.concat(MessageQueues.CoreToMessage1(reply.toolCalls))
}
async updateStatus(userData?: User["data"]) {
const status = await this.get()

if (userData) this.coreMessages.concat([
{
content: JSON.stringify(userData),
role: "system"
}
])

if (status) this.coreMessages.concat([
{
content: status,
role: "system"
}
])
}
}

const message = [
{
fromMe: true,
body: "`system`"
},
{
fromMe: true,
body: "llm"
},
{
fromMe: true,
body: '> tools({"email":"HI"})'
},
]

// @ts-ignore
// const x = new MessageQueues("1234567890", message)

// console.log(x.coreMessages)
// // @ts-ignore
// await x.reply({
// toolResults: [{
// args: {
// email: "qwertyui",
// },
// result: "12345678",
// toolCallId: "12345678",
// toolName: "login",
// type: "tool-result"
// }]
// })
// // @ts-ignore
// await x.reply({ text: "hi" })
// // @ts-ignore
// await x.reply({})
// console.log(x.texts)
2 changes: 1 addition & 1 deletion cache/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getDataFromMail } from "@/lib/email";
import { generateOTP } from "@/lib/otp";
import type { AllYears, SJCET, UserOtp } from "@/lib/type";
import { prefixRedis, redisClient } from "./client";
import { sentOTP } from "@/source/mailer";
import { sentOTP } from "@/data/otp-mail";


export const isStudent = (year: AllYears) => {
Expand Down
16 changes: 16 additions & 0 deletions data/certificate.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,21 @@
"certificateType": "participation",
"description": "A project based coding competition. To find the top 20 coders in SJCET Palai. They done a great job building and submitted their quality projects on time",
"link": "https://www.instagram.com/p/C-epC8VSlr6"
},
"sih-24-participation": {
"title": "Smart India Hackathon Prelims 2024",
"organisedBy": "IEDC",
"activityPoints": 5,
"certificateType": "participation",
"description": "A nationwide competition where students from all over India come together to solve real-world problems faced by different government departments and industries",
"link": ""
},
"fossday-24": {
"title": "FOSSDAY 2024",
"organisedBy": "NEXUSSJCET",
"activityPoints": 10,
"certificateType": "participation",
"description": "A day-long meetup & workshop on Free and Open Source Software. To introduce students to the world of open source software and its importance",
"link": "https://nexus.sjcetpalai.ac.in/fossday"
}
}
14 changes: 13 additions & 1 deletion data/otp-mail.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { sentMail, simpleHTMLMail } from "@/source/mailer"

const botNumber = process.env.SYSTEM_NUMBER ?? "0000000000"

export const OtpMailTemplate = (otp: number, number: string) =>
Expand All @@ -23,4 +25,14 @@ export const OtpMailTemplate = (otp: number, number: string) =>
<p>Kottayam</p>
</div>
</div>
</div>`
</div>`

export const sentOTP = async (to: string, otp: number, phone: string) => await sentMail({
to,
subject: "Here is the OTP for verification in Insight Portal",
text: `Use the OTP: "${otp}" to complete your Sign Up procedures`,
html: simpleHTMLMail({
title: "Verification for Insight Portal",
body: OtpMailTemplate(otp, phone)
})
})
6 changes: 6 additions & 0 deletions database/demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { db } from "./index";
import { user } from './schema'

const allUsers = await db.select().from(user);

console.log(allUsers)
7 changes: 7 additions & 0 deletions database/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { drizzle } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'

const connectionString = process.env.DATABASE_URL as string

const client = postgres(connectionString, { max: 1 })
export const db = drizzle(client);
6 changes: 6 additions & 0 deletions database/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { migrate } from "drizzle-orm/postgres-js/migrator";
import { db } from "./index";


await migrate(db, { migrationsFolder: "drizzle" });
process.exit(0)
72 changes: 72 additions & 0 deletions database/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { allClubs, allDepartments, allRoles, allYears, type AllClubs, type AllDepartments, type AllRoles, type AllYears } from "@/lib/type";
import { sql } from "drizzle-orm";
import { pgTable, serial, text, varchar, uuid, pgEnum, integer, uniqueIndex, timestamp } from "drizzle-orm/pg-core";

export const statusEnum = pgEnum('status', ['initiated', 'success', 'failed']);
// export const certificateTypeEnum = pgEnum('certificateType', ['participation', 'winner', 'internship']);

export const roleEnum = pgEnum('role', ['NA', ...(Object.keys(allRoles) as AllRoles[])]);
export const departmentEnum = pgEnum('department', [
'NA',
...(Object.keys(allDepartments) as AllDepartments[]), // ai, cs, eee, mca, etc
]);

export const yearEnum = pgEnum('year', [
'NA',
...(Object.keys(allYears) as AllYears[]), // 2021, 2022, ...2027
]);

export const clubsEnum = pgEnum('club', [
'SJCET',
...(Object.keys(allClubs) as AllClubs[]), // IEDC, IEEE, ...NEXUS
]);

export const user = pgTable('user', {
email: varchar('email', { length: 191 }).primaryKey(),
name: text('name'),
phone: varchar('phone', { length: 256 }).notNull().unique(),

department: departmentEnum('department').default('NA').notNull(),
year: yearEnum('year').default('NA').notNull(),

role: roleEnum('role').default('NA').notNull(),
clubs: clubsEnum("clubs").array().default([]),

createdAt: timestamp('createdAt')
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp('updatedAt'),
}, (table) => ({
phoneIndex: uniqueIndex("phoneIndex").on(table.phone)
}));

export const event = pgTable('event', {
id: text('id').primaryKey(),
title: text('title').notNull(),
description: text('description'),
organisedBy: clubsEnum('organisedBy').array().default(['SJCET']).notNull(),

activityPoints: integer("activityPoints").default(0),
// amount: integer('amount').default(0).notNull(),
remark: text('remark'),
link: text("link"),

createdAt: timestamp('createdAt')
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp('updatedAt'),
createdByEmail: varchar('createdByEmail', { length: 191 })
.notNull()
.references(() => user.email),
});

// export const certificate = pgTable('certificate', {
// id: text('id').primaryKey(),
// title: text('title').notNull(),
// description: text('description'),
// organisedBy: clubsEnum('organisedBy').array().default(['SJCET']).notNull(),
// activityPoints: integer("activityPoints").default(0),
// remark: text('remark'),
// link: text("link"),
// certificateType: certificateTypeEnum('certificateType').default("participation"),
// });
Loading