From 1d109c45d0f2512e3b375cbdf1843e0073687737 Mon Sep 17 00:00:00 2001 From: sarthakjdev Date: Sun, 14 Jul 2024 14:10:00 +0530 Subject: [PATCH] fix: build Signed-off-by: sarthakjdev --- apps/wapi-ai-chatbot/src/index.ts | 82 +++++++++++----------- apps/wapi-ai-chatbot/src/utils/cache.ts | 4 +- apps/wapi-ai-chatbot/src/utils/gpt.ts | 32 ++++----- packages/create-wapi-app/src/create-bot.ts | 3 +- 4 files changed, 61 insertions(+), 60 deletions(-) diff --git a/apps/wapi-ai-chatbot/src/index.ts b/apps/wapi-ai-chatbot/src/index.ts index 309e4bc..ae37a9c 100644 --- a/apps/wapi-ai-chatbot/src/index.ts +++ b/apps/wapi-ai-chatbot/src/index.ts @@ -1,55 +1,55 @@ -import { TextMessage, TextMessageEvent } from '@wapijs/wapi.js' +import { TextMessage, type TextMessageEvent } from '@wapijs/wapi.js' import { whatsappClient } from './utils/client' import { askAi } from './utils/gpt' import { getCache, setCache } from './utils/cache' async function init() { - try { - whatsappClient.on('Ready', () => { - console.log('Client is ready') - }) - - whatsappClient.on('Error', error => { - console.error('Error', error.message) - }) - - whatsappClient.on('TextMessage', async (event: TextMessageEvent) => { - const cachedResponse = await getCache(event.text.data.text) - console.log({ cachedResponse }) - let response = 'Sorry, I am not able to understand that.' - if (cachedResponse) { - response = String(cachedResponse) - } else { - const aiResponse = await askAi(event.text.data.text) - response = aiResponse || response - if (aiResponse) { - setCache(event.text.data.text, aiResponse) - } - } - - console.log({ response }) - await event.reply({ - message: new TextMessage({ - text: response - }) - }) - }) - - whatsappClient.initiate() - } catch (error) { - console.error(error) - // ! TODO: you may prefer to send a notification to your slack channel or email here - } + try { + whatsappClient.on('Ready', () => { + console.log('Client is ready') + }) + + whatsappClient.on('Error', error => { + console.error('Error', error.message) + }) + + whatsappClient.on('TextMessage', async (event: TextMessageEvent) => { + const cachedResponse = await getCache(event.text.data.text) + console.log({ cachedResponse }) + let response = 'Sorry, I am not able to understand that.' + if (cachedResponse) { + response = String(cachedResponse) + } else { + const aiResponse = await askAi(event.text.data.text) + response = aiResponse || response + if (aiResponse) { + setCache(event.text.data.text, aiResponse) + } + } + + console.log({ response }) + await event.reply({ + message: new TextMessage({ + text: response + }) + }) + }) + + whatsappClient.initiate() + } catch (error) { + console.error(error) + // ! TODO: you may prefer to send a notification to your slack channel or email here + } } init().catch(error => console.error(error)) process.on('unhandledRejection', error => { - console.error('unhandledRejection', error) - process.exit(1) + console.error('unhandledRejection', error) + process.exit(1) }) process.on('uncaughtException', error => { - console.error('uncaughtException', error) - process.exit(1) + console.error('uncaughtException', error) + process.exit(1) }) diff --git a/apps/wapi-ai-chatbot/src/utils/cache.ts b/apps/wapi-ai-chatbot/src/utils/cache.ts index a11808e..6236ef6 100644 --- a/apps/wapi-ai-chatbot/src/utils/cache.ts +++ b/apps/wapi-ai-chatbot/src/utils/cache.ts @@ -3,9 +3,9 @@ import NodeCache from 'node-cache' const cache = new NodeCache() export const setCache = (key: string, value: any, ttl: number = 3600) => { - cache.set(key, value, ttl) + cache.set(key, value, ttl) } export const getCache = (key: string) => { - return cache.get(key) + return cache.get(key) } diff --git a/apps/wapi-ai-chatbot/src/utils/gpt.ts b/apps/wapi-ai-chatbot/src/utils/gpt.ts index a06e06a..9046fc3 100644 --- a/apps/wapi-ai-chatbot/src/utils/gpt.ts +++ b/apps/wapi-ai-chatbot/src/utils/gpt.ts @@ -1,29 +1,29 @@ -import OpenAI from 'openai' +import { OpenAI } from 'openai' const openAiApiKey = process.env.OPEN_AI_API_KEY if (!openAiApiKey) { - throw new Error('OPEN_AI_API_KEY not defined!') + throw new Error('OPEN_AI_API_KEY not defined!') } const OpenApiClient = new OpenAI({ - apiKey: openAiApiKey, - project: 'proj_viwVq5nzCR5Mj4TnIw4FQoNO', - organization: 'org-wwfaGhYXIA7CBUSg6x8DbKYj' + apiKey: openAiApiKey, + project: 'proj_viwVq5nzCR5Mj4TnIw4FQoNO', + organization: 'org-wwfaGhYXIA7CBUSg6x8DbKYj' }) export async function askAi(message: string): Promise { - try { - const chatCompletion = await OpenApiClient.chat.completions.create({ - messages: [{ role: 'user', content: message }], - model: 'gpt-3.5-turbo' - }) + try { + const chatCompletion = await OpenApiClient.chat.completions.create({ + messages: [{ role: 'user', content: message }], + model: 'gpt-3.5-turbo' + }) - console.log(JSON.stringify({ chatCompletion })) + console.log(JSON.stringify({ chatCompletion })) - return chatCompletion.choices[0].message.content - } catch (error) { - console.log({ error }) - return null - } + return chatCompletion.choices[0].message.content + } catch (error) { + console.log({ error }) + return null + } } diff --git a/packages/create-wapi-app/src/create-bot.ts b/packages/create-wapi-app/src/create-bot.ts index e8b4bf1..c4c22c6 100644 --- a/packages/create-wapi-app/src/create-bot.ts +++ b/packages/create-wapi-app/src/create-bot.ts @@ -74,7 +74,8 @@ export async function createWhatsappBot(options: { if (bun) { await cp( new URL( - `../template/Bun/${isTypescriptEnabled ? "typescript" : "javascript" + `../template/Bun/${ + isTypescriptEnabled ? "typescript" : "javascript" }/package.json`, import.meta.url, ),