-
Notifications
You must be signed in to change notification settings - Fork 51
fix(bot): align nested tsconfig base paths #482
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
base: master
Are you sure you want to change the base?
Changes from all commits
98f3a2f
f9aa644
abc6791
71750f6
35e047f
af1bcd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "outDir": "./dist", | ||
| "rootDir": "./src" | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } | ||
| { | ||
| "extends": "../../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "outDir": "./dist", | ||
| "rootDir": "./src" | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "outDir": "./dist", | ||
| "rootDir": "./src" | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } | ||
| { | ||
| "extends": "../../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "outDir": "./dist", | ||
| "rootDir": "./src" | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "outDir": "./dist", | ||
| "rootDir": "./src" | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } | ||
| { | ||
| "extends": "../../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "outDir": "./dist", | ||
| "rootDir": "./src" | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "outDir": "./dist", | ||
| "rootDir": "./src" | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } | ||
| { | ||
| "extends": "../../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "outDir": "./dist", | ||
| "rootDir": "./src" | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,128 +1,129 @@ | ||
| import makeBaileysBot, { type WASocket } from "baileys"; | ||
| import { z } from "zod"; | ||
|
|
||
| const configSchema = z.object({ | ||
| botName: z.string().default("Bot"), | ||
| aiProvider: z.enum(["claude-code", "opencode", "qwen"]).default("claude-code"), | ||
| aiCliPath: z.string().default("claude"), | ||
| aiModel: z.string().optional(), | ||
| sessionTimeoutMs: z.number().int().positive().default(30 * 60 * 1000), | ||
| maxOutputLength: z.number().int().positive().default(4000), | ||
| maxConcurrentSessions: z.number().int().positive().default(5), | ||
| allowedUsers: z.array(z.string()).default([]), | ||
| adminUsers: z.array(z.string()).default([]), | ||
| }); | ||
|
|
||
| export type Config = z.infer<typeof configSchema>; | ||
|
|
||
| export interface IncomingMessage { | ||
| source: string; | ||
| sourceName: string; | ||
| text: string; | ||
| timestamp: number; | ||
| chatId: string; | ||
| isGroup: boolean; | ||
| attachments: Array<{ filename: string; url: string }>; | ||
| raw: any; | ||
| } | ||
|
|
||
| type MessageHandler = (msg: IncomingMessage) => void | Promise<void>; | ||
|
|
||
| export class WhatsAppBot { | ||
| private sock: WASocket | null = null; | ||
| private handlers: MessageHandler[] = []; | ||
| private config: Config; | ||
| private running = false; | ||
|
|
||
| constructor(config: Config) { | ||
| this.config = config; | ||
| } | ||
|
|
||
| onMessage(handler: MessageHandler): void { | ||
| this.handlers.push(handler); | ||
| } | ||
|
|
||
| async start(): Promise<void> { | ||
| const { state, saveState } = useMultiFileAuthState("./auth"); | ||
|
|
||
| this.sock = makeBaileysBot(state, { | ||
| print: console.log, | ||
| import makeBaileysBot, { type WASocket } from "baileys"; | ||
| import { z } from "zod"; | ||
| const configSchema = z.object({ | ||
| botName: z.string().default("Bot"), | ||
| aiProvider: z.enum(["claude-code", "opencode", "qwen"]).default("claude-code"), | ||
| aiCliPath: z.string().default("claude"), | ||
| aiModel: z.string().optional(), | ||
| sessionTimeoutMs: z.number().int().positive().default(30 * 60 * 1000), | ||
| maxOutputLength: z.number().int().positive().default(4000), | ||
| maxConcurrentSessions: z.number().int().positive().default(5), | ||
| allowedUsers: z.array(z.string()).default([]), | ||
| adminUsers: z.array(z.string()).default([]), | ||
| }); | ||
| export type Config = z.infer<typeof configSchema>; | ||
| export interface IncomingMessage { | ||
| source: string; | ||
| sourceName: string; | ||
| text: string; | ||
| timestamp: number; | ||
| chatId: string; | ||
| isGroup: boolean; | ||
| attachments: Array<{ filename: string; url: string }>; | ||
| raw: any; | ||
| } | ||
| type MessageHandler = (msg: IncomingMessage) => void | Promise<void>; | ||
| export class WhatsAppBot { | ||
| private sock: WASocket | null = null; | ||
| private handlers: MessageHandler[] = []; | ||
| private config: Config; | ||
| private running = false; | ||
| constructor(config: Config) { | ||
| this.config = config; | ||
| } | ||
| onMessage(handler: MessageHandler): void { | ||
| this.handlers.push(handler); | ||
| } | ||
| async start(): Promise<void> { | ||
| const { state, saveState } = useMultiFileAuthState("./auth"); | ||
| this.sock = makeBaileysBot({ | ||
| auth: state, | ||
| browser: ["sh1pt-bot", "Chrome", "120"], | ||
| }); | ||
| this.sock.ev.on("creds.update", saveState); | ||
|
|
||
| this.sock.ev.on("messages.upsert", async ({ messages }) => { | ||
| for (const msg of messages) { | ||
| if (!msg.message || msg.key.fromMe) continue; | ||
|
|
||
| const chatId = msg.key.remoteJid!; | ||
| const isGroup = chatId.endsWith("@g.us"); | ||
| const text = | ||
| msg.message.conversation || | ||
| msg.message.extendedTextMessage?.text || | ||
| ""; | ||
|
|
||
| const incoming: IncomingMessage = { | ||
| source: chatId, | ||
| sourceName: msg.pushName || "User", | ||
| text, | ||
| timestamp: msg.messageTimestamp * 1000, | ||
| chatId, | ||
| isGroup, | ||
| attachments: [], | ||
| raw: msg, | ||
| }; | ||
|
|
||
| for (const handler of this.handlers) { | ||
| try { | ||
| const result = handler(incoming); | ||
| if (result instanceof Promise) result.catch(console.error); | ||
| } catch (err) { | ||
| console.error("Handler error:", err); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| this.running = true; | ||
| console.log("WhatsApp bot started"); | ||
| } | ||
|
|
||
| async stop(): Promise<void> { | ||
| this.running = false; | ||
| } | ||
|
|
||
| isRunning(): boolean { | ||
| return this.running; | ||
| } | ||
|
|
||
| async reply(msg: IncomingMessage, text: string): Promise<void> { | ||
| await this.sock?.sendMessage(msg.chatId, { text }); | ||
| } | ||
|
|
||
| async send(chatId: string, text: string): Promise<void> { | ||
| await this.sock?.sendMessage(chatId, { text }); | ||
| } | ||
| for (const msg of messages) { | ||
| if (!msg.message || msg.key.fromMe) continue; | ||
|
|
||
| const chatId = msg.key.remoteJid!; | ||
| const isGroup = chatId.endsWith("@g.us"); | ||
| const text = | ||
| msg.message.conversation || | ||
| msg.message.extendedTextMessage?.text || | ||
| ""; | ||
|
|
||
| const incoming: IncomingMessage = { | ||
| source: chatId, | ||
| sourceName: msg.pushName || "User", | ||
| text, | ||
| timestamp: msg.messageTimestamp * 1000, | ||
| chatId, | ||
| isGroup, | ||
| attachments: [], | ||
| raw: msg, | ||
| }; | ||
|
|
||
| for (const handler of this.handlers) { | ||
| try { | ||
| const result = handler(incoming); | ||
| if (result instanceof Promise) result.catch(console.error); | ||
| } catch (err) { | ||
| console.error("Handler error:", err); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| this.running = true; | ||
| console.log("WhatsApp bot started"); | ||
| } | ||
|
|
||
| async stop(): Promise<void> { | ||
| this.running = false; | ||
| } | ||
|
|
||
| isRunning(): boolean { | ||
| return this.running; | ||
| } | ||
|
|
||
| async reply(msg: IncomingMessage, text: string): Promise<void> { | ||
| await this.sock?.sendMessage(msg.chatId, { text }); | ||
| } | ||
|
|
||
| async send(chatId: string, text: string): Promise<void> { | ||
| await this.sock?.sendMessage(chatId, { text }); | ||
| } | ||
| } | ||
|
|
||
| function useMultiFileAuthState( | ||
| dir: string | ||
| ): { state: any; saveState: () => void } { | ||
| return { | ||
| state: {}, | ||
| saveState: () => {}, | ||
| }; | ||
| } | ||
|
Comment on lines
+108
to
+115
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The local stub always returns |
||
|
|
||
| export function loadConfig(env: Record<string, string | undefined>): Config { | ||
| return configSchema.parse({ | ||
| botName: env.BOT_NAME || "Bot", | ||
| aiProvider: (env.AI_PROVIDER as any) || "claude-code", | ||
| aiCliPath: env.AI_CLI_PATH || "claude", | ||
| aiModel: env.AI_MODEL, | ||
| sessionTimeoutMs: parseInt(env.SESSION_TIMEOUT_MS || "1800000", 10), | ||
| maxOutputLength: parseInt(env.MAX_OUTPUT_LENGTH || "4000", 10), | ||
| maxConcurrentSessions: parseInt(env.MAX_CONCURRENT_SESSIONS || "5", 10), | ||
| allowedUsers: env.ALLOWED_USERS?.split(",").map((u) => u.trim()).filter(Boolean) || [], | ||
| adminUsers: env.ADMIN_USERS?.split(",").map((u) => u.trim()).filter(Boolean) || [], | ||
| }); | ||
| } | ||
|
|
||
| function useMultiFileAuthState( | ||
| dir: string | ||
| ): { state: any; saveState: () => void } { | ||
| return { | ||
| state: {}, | ||
| saveState: () => {}, | ||
| }; | ||
| } | ||
|
|
||
| export function loadConfig(env: Record<string, string | undefined>): Config { | ||
| return configSchema.parse({ | ||
| botName: env.BOT_NAME || "Bot", | ||
| aiProvider: (env.AI_PROVIDER as any) || "claude-code", | ||
| aiCliPath: env.AI_CLI_PATH || "claude", | ||
| aiModel: env.AI_MODEL, | ||
| sessionTimeoutMs: parseInt(env.SESSION_TIMEOUT_MS || "1800000", 10), | ||
| maxOutputLength: parseInt(env.MAX_OUTPUT_LENGTH || "4000", 10), | ||
| maxConcurrentSessions: parseInt(env.MAX_CONCURRENT_SESSIONS || "5", 10), | ||
| allowedUsers: env.ALLOWED_USERS?.split(",").map((u) => u.trim()).filter(Boolean) || [], | ||
| adminUsers: env.ADMIN_USERS?.split(",").map((u) => u.trim()).filter(Boolean) || [], | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "outDir": "./dist", | ||
| "rootDir": "./src" | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } | ||
| { | ||
| "extends": "../../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "outDir": "./dist", | ||
| "rootDir": "./src" | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stop()leaves the socket connection openstop()flipsthis.runningbut never callsthis.sock?.end()(or the equivalent Baileys close method). The underlying WebSocket stays connected and event listeners keep firing afterstop()returns, which can cause unexpected behavior or resource leaks ifstart()is called again.