Skip to content

Commit

Permalink
feat: bot setup script, top level catch
Browse files Browse the repository at this point in the history
  • Loading branch information
agazso committed Sep 18, 2023
1 parent 65fac9c commit 586ac9a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 28 deletions.
28 changes: 28 additions & 0 deletions src/lib/adapters/waku/bot-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh

user="0x9B16da459b7EA266787D5beCDB12DD05854A6704"

bot1="0x269a16a015287D660Dc7C0F60dE3e0BF18111550"
bot1_name="WendyBot2"
bot1_avatar="QmTJDir3UkoxEy1Mze1fbSjNz9aapT4A1staTobSa76zCX"

bot2="0xAc0fDf1E1A6bbBaE2E7D332218b3a211C905eBec"
bot2_name="WendyTest"
bot2_avatar="QmYtPe54iHBRZvhRMJFrsmEUKBa7HD7iXpcxWH3975vKVo"

group=cde2cf96d7aaa20a561956fec868bb3cbd737a85b29417fc3642b7f922321c98
group_name="BotParty"
group_avatar="QmUkzzteHqVkU1pV9ncZY1P6ipNH2MQfmhPJoSqEYfCBSV"

pnpm cli waku set-profile "$bot1" "$bot1_name" "$bot1_avatar"
pnpm cli waku set-profile "$bot2" "$bot2_name" "$bot2_avatar"

pnpm cli waku send "$bot1" "$user" "hello from $bot1_name"
pnpm cli waku send "$bot2" "$user" "hello from $bot2_name"

pnpm cli waku set-group-chat "$group" "$group_name" "$group_avatar" "$user" "$bot1" "$bot2"

pnpm cli waku invite "$user" "$bot1" "$group"
pnpm cli waku invite "$user" "$bot2" "$group"

# pnpm cli waku send "$bot1" "$group" "@$bot2 are you there?"
73 changes: 45 additions & 28 deletions src/lib/adapters/waku/bot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// run with pnpm bot <address>

// [ ] admin + commands
// [ ] onlu print nonzero errors

import child_process from 'child_process'
import { PageDirection, type LightNode, type Unsubscribe } from '@waku/interfaces'
import axios from 'axios'
Expand Down Expand Up @@ -54,6 +57,7 @@ const errors = {
numDisconnect: 0,
numRequestError: 0,
numSendError: 0,
numUndefinedResponse: 0,
}
const queuedMessages: QueuedMessage[] = []
let isHandlingMessage = false
Expand All @@ -62,36 +66,45 @@ let isHandlingMessage = false
main().catch(console.error)

async function main() {
if (!BOT_ADDRESS) {
console.error(
'please provide a bot address as argument or in the BOT_ADDRESS environment variable',
try {
if (!BOT_ADDRESS) {
console.error(
'please provide a bot address as argument or in the BOT_ADDRESS environment variable',
)
process.exit(1)
}

process.on('uncaughtException', (error) => log(`⁉️ Uncaught exception`, { error }))
process.on('unhandledRejection', (reason, promise) =>
log(`⁉️ Unhandled rejection`, { reason, promise }),
)
process.exit(1)
}

let waku: LightNode | undefined = undefined
waku = await connectWaku({
onDisconnect: () => {
log('❌ disconnected from waku')
errors.numDisconnect++
},
onConnect: () => {
log('✅ connected to waku')
safeResubscribe(waku)
},
})
let waku: LightNode | undefined = undefined
waku = await connectWaku({
onDisconnect: () => {
log('❌ disconnected from waku')
errors.numDisconnect++
},
onConnect: () => {
log('✅ connected to waku')
safeResubscribe(waku)
},
})

log(`🪪 storing profile as ${BOT_NAME} and avatar as ${BOT_AVATAR}`)
await storeDocument(waku, 'profile', BOT_ADDRESS, botProfile)
log(`🪪 storing profile as ${BOT_NAME} and avatar as ${BOT_AVATAR}`)
await storeDocument(waku, 'profile', BOT_ADDRESS, botProfile)

await subscribe(waku, BOT_ADDRESS, queueMessage)
await subscribe(waku, BOT_ADDRESS, queueMessage)

await loadGroupChats(waku)
for (const groupChatId of groupChats) {
await subscribe(waku, groupChatId, queueMessage)
}
await loadGroupChats(waku)
for (const groupChatId of groupChats) {
await subscribe(waku, groupChatId, queueMessage)
}

setInterval(logStats, 60_000)
setInterval(logStats, 60_000)
} catch (error) {
log(`‼️ Top Level Error: `, { error })
}
}

async function safeResubscribe(waku: LightNode | undefined) {
Expand Down Expand Up @@ -239,6 +252,8 @@ async function requestLLM(text: string, name: string, history?: any) {
history = response?.data?.results?.[0]?.history
const responseText = history?.visible?.slice(-1)?.[0]?.[1]

console.debug({ lastSlice: history?.visible?.slice(-1) })

return {
response,
history,
Expand Down Expand Up @@ -277,12 +292,12 @@ async function handleUserMessage(waku: LightNode, chatMessage: UserMessage, chat
if (isGroupChatId(chatId)) {
// ignore messages from self
if (chatMessage.fromAddress === BOT_ADDRESS) {
log('🙈 ignoring messages from self')
log('🙈 ignoring message from self')
return
}
// ignore messages not addressed to bot
if (!chatMessage.text.startsWith(`@${BOT_ADDRESS}`)) {
log('🙈 ignoring messages not addressed to me')
log('🙈 ignoring message not addressed to me')
return
}
return handleSessionUserMessage(waku, chatMessage, chatId)
Expand All @@ -301,7 +316,8 @@ async function handleSessionUserMessage(waku: LightNode, chatMessage: UserMessag
)

if (!responseText) {
log(`⚠️ undefined response text`, { response })
log(`⚠️ undefined response text`, { response })
errors.numUndefinedResponse++
return
}

Expand Down Expand Up @@ -422,5 +438,6 @@ function sleep(msec: number) {
}

function logStats() {
log(`📊 `, { errors })
const nonZeroErrors = Object.entries(errors).filter(([, value]) => value !== 0)
log(`📊 `, { errors: Object.fromEntries(nonZeroErrors) })
}

0 comments on commit 586ac9a

Please sign in to comment.