From ba8d5f03c33237bca48d67761b2478a811f5257a Mon Sep 17 00:00:00 2001 From: Antigravity Date: Sat, 13 Jun 2026 16:21:18 -0400 Subject: [PATCH] Fix proxy routing for whatsapp and pod startup for gmail-agent --- agents/k8s/gmail-agent-deployment.yaml | 2 ++ gmail-agent/index.js | 46 ++++++++++++++------------ whatsapp-gateway/public/app.js | 12 +++---- whatsapp-gateway/public/index.html | 1 + 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/agents/k8s/gmail-agent-deployment.yaml b/agents/k8s/gmail-agent-deployment.yaml index f886282..724082a 100644 --- a/agents/k8s/gmail-agent-deployment.yaml +++ b/agents/k8s/gmail-agent-deployment.yaml @@ -68,12 +68,14 @@ spec: - name: credentials-volume secret: secretName: gmail-agent-auth + optional: true items: - key: credentials.json path: credentials.json - name: tokens-volume secret: secretName: gmail-agent-tokens + optional: true --- apiVersion: v1 diff --git a/gmail-agent/index.js b/gmail-agent/index.js index 3db3e34..cc1499a 100644 --- a/gmail-agent/index.js +++ b/gmail-agent/index.js @@ -18,33 +18,37 @@ let accounts = []; function loadAuth() { if (!fs.existsSync(CREDENTIALS_PATH)) { - console.error('Missing credentials.json. Please download it from Google Cloud Console and place it in the project directory.'); - process.exit(1); + 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)) { - console.error('Missing tokens directory. Please run "node auth.js " to generate an account token.'); - process.exit(1); + fs.mkdirSync(TOKENS_DIR, { recursive: true }); + console.log('Created empty tokens directory.'); } - const creds = JSON.parse(fs.readFileSync(CREDENTIALS_PATH)); - 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' }; - - const tokenFiles = fs.readdirSync(TOKENS_DIR).filter(file => file.endsWith('.json')); - if (tokenFiles.length === 0) { - console.error('No tokens found in tokens/. Please run "node auth.js " to generate one.'); - process.exit(1); - } - - for (const file of tokenFiles) { - const accountName = file.replace('.json', ''); - const tokenContent = JSON.parse(fs.readFileSync(path.join(TOKENS_DIR, file))); + try { + const creds = JSON.parse(fs.readFileSync(CREDENTIALS_PATH)); + 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' }; - const client = new google.auth.OAuth2(baseOAuth2Client.client_id, baseOAuth2Client.client_secret, baseOAuth2Client.redirect_uri); - client.setCredentials(tokenContent); + const tokenFiles = fs.readdirSync(TOKENS_DIR).filter(file => file.endsWith('.json')); + if (tokenFiles.length === 0) { + console.log('ℹ️ No token files found in tokens/. Waiting for accounts to be added via UI.'); + return; + } - accounts.push({ name: accountName, client }); - console.log(`Loaded credentials for account: ${accountName}`); + for (const file of tokenFiles) { + const accountName = file.replace('.json', ''); + const tokenContent = JSON.parse(fs.readFileSync(path.join(TOKENS_DIR, file))); + + const client = new google.auth.OAuth2(baseOAuth2Client.client_id, baseOAuth2Client.client_secret, baseOAuth2Client.redirect_uri); + client.setCredentials(tokenContent); + + accounts.push({ name: accountName, client }); + console.log(`✅ Loaded credentials for account: ${accountName}`); + } + } catch (err) { + console.error('❌ Error loading auth files:', err.message); } } diff --git a/whatsapp-gateway/public/app.js b/whatsapp-gateway/public/app.js index 6ce8efd..5be6824 100644 --- a/whatsapp-gateway/public/app.js +++ b/whatsapp-gateway/public/app.js @@ -1,4 +1,4 @@ -const socket = io(); +const socket = io({ path: '/proxy/whatsapp/socket.io' }); // UI Elements const statusBadge = document.getElementById('status-badge'); @@ -84,7 +84,7 @@ function renderQR(qrText) { async function fetchGroups() { groupsTableBody.innerHTML = 'Loading...'; try { - const res = await fetch('/api/groups'); + const res = await fetch('api/groups'); const data = await res.json(); if (!data.success) { @@ -121,7 +121,7 @@ document.getElementById('btn-refresh-groups').addEventListener('click', fetchGro async function fetchApps() { appsTableBody.innerHTML = 'Loading...'; try { - const res = await fetch('/api/apps'); + const res = await fetch('api/apps'); const data = await res.json(); if (!data.success) { @@ -164,7 +164,7 @@ document.getElementById('create-group-form').addEventListener('submit', async (e const participant = document.getElementById('new-group-participant').value; try { - const res = await fetch('/api/groups/create', { + const res = await fetch('api/groups/create', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title, initialParticipants: [participant] }) @@ -189,7 +189,7 @@ document.getElementById('create-group-form').addEventListener('submit', async (e // Invite Link Modal window.getInviteLink = async (groupId) => { try { - const res = await fetch('/api/groups/invite', { + const res = await fetch('api/groups/invite', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ groupId }) @@ -230,7 +230,7 @@ document.getElementById('test-msg-form').addEventListener('submit', async (e) => const message = document.getElementById('test-body').value; try { - const res = await fetch('/api/send-message', { + const res = await fetch('api/send-message', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ to, message }) diff --git a/whatsapp-gateway/public/index.html b/whatsapp-gateway/public/index.html index 5bf6bef..e6e560c 100644 --- a/whatsapp-gateway/public/index.html +++ b/whatsapp-gateway/public/index.html @@ -4,6 +4,7 @@ WhatsApp Gateway Dashboard +