From dcb6f952cc048d4185a2f77db600874d62791255 Mon Sep 17 00:00:00 2001 From: Antigravity Date: Sat, 13 Jun 2026 22:53:04 -0400 Subject: [PATCH] Fix EISDIR error when credentials.json is an empty directory due to missing k8s secret --- gmail-agent/index.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/gmail-agent/index.js b/gmail-agent/index.js index 8c464ed..c88615d 100644 --- a/gmail-agent/index.js +++ b/gmail-agent/index.js @@ -7,6 +7,13 @@ const { google } = require('googleapis'); const TOKENS_DIR = path.join(__dirname, 'tokens'); const CREDENTIALS_PATH = path.join(__dirname, 'credentials.json'); +function getCredentials() { + if (!fs.existsSync(CREDENTIALS_PATH) || fs.statSync(CREDENTIALS_PATH).isDirectory()) { + throw new Error('Google OAuth credentials.json is missing. Please create the gmail-agent-auth Kubernetes Secret with your Google Cloud OAuth credentials.'); + } + return JSON.parse(fs.readFileSync(CREDENTIALS_PATH)); +} + const OLLAMA_URL = process.env.OLLAMA_URL || 'http://ollama.ai-agents.svc.cluster.local:11434/api/generate'; const WHATSAPP_GATEWAY_URL = process.env.WHATSAPP_GATEWAY_URL || 'http://whatsapp-gateway.ai-agents.svc.cluster.local:5001/api/send-message'; const WHATSAPP_TO = process.env.WHATSAPP_TO || '+14085505485'; @@ -16,17 +23,18 @@ let baseOAuth2Client; let accounts = []; function loadAuth() { - if (!fs.existsSync(CREDENTIALS_PATH)) { - console.warn('⚠️ Missing credentials.json. Gmail Agent will not be able to process emails or authenticate new accounts until this file is provided.'); - return; - } + try { + if (!fs.existsSync(CREDENTIALS_PATH) || fs.statSync(CREDENTIALS_PATH).isDirectory()) { + console.warn('⚠️ Missing credentials.json. Gmail Agent will not be able to process emails or authenticate new accounts until this file is provided.'); + return; + } if (!fs.existsSync(TOKENS_DIR)) { fs.mkdirSync(TOKENS_DIR, { recursive: true }); console.log('Created empty tokens directory.'); } try { - const creds = JSON.parse(fs.readFileSync(CREDENTIALS_PATH)); + const creds = getCredentials(); const {client_secret, client_id, redirect_uris} = creds.installed || creds.web; baseOAuth2Client = { client_id, client_secret, redirect_uri: redirect_uris[0] || 'urn:ietf:wg:oauth:2.0:oob' }; @@ -204,7 +212,7 @@ app.post('/api/auth/url', (req, res) => { if (!accountName) return res.status(400).json({ success: false, error: 'accountName is required' }); try { - const creds = JSON.parse(fs.readFileSync(CREDENTIALS_PATH)); + const creds = getCredentials(); const {client_secret, client_id, redirect_uris} = creds.installed || creds.web; const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0] || 'urn:ietf:wg:oauth:2.0:oob'); @@ -226,7 +234,7 @@ app.post('/api/auth/token', (req, res) => { if (!accountName || !code) return res.status(400).json({ success: false, error: 'accountName and code are required' }); try { - const creds = JSON.parse(fs.readFileSync(CREDENTIALS_PATH)); + const creds = getCredentials(); const {client_secret, client_id, redirect_uris} = creds.installed || creds.web; const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0] || 'urn:ietf:wg:oauth:2.0:oob');