Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

feat: add Spotify account details persistence, display in status, and implement logout functionality

Salman Qureshi2889c4e72b9e
diff --git a/src/core/agent.ts b/src/core/agent.ts
index 345721ff..d19112ea 100644
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@ -391,7 +391,18 @@ export class Agent {
const rawArgs = trimmed.slice('/spotify'.length).trim().toLowerCase();
if (!rawArgs || rawArgs === 'status') {
const auth = this.spotifyClient.isAuthenticated() ? 'Connected' : 'Not connected';
- await channel.send(`Spotify: **${auth}**\nDevice: ${this.spotifyClient.getDeviceId() || 'none selected'}`, msg.channelId);
+ const accountName = this.spotifyClient.getAccountName();
+ const product = this.spotifyClient.getProduct();
+ let status = `Spotify: **${auth}**`;
+ if (accountName) status += `\nAccount: **${accountName}**`;
+ if (product) status += `\nPlan: ${product}`;
+ status += `\nDevice: ${this.spotifyClient.getDeviceId() || 'none selected'}`;
+ await channel.send(status, msg.channelId);
+ return;
+ }
+ if (rawArgs === 'logout') {
+ this.spotifyClient.logout();
+ await channel.send('Spotify disconnected. Run `/spotify auth` to reconnect.', msg.channelId);
return;
}
if (rawArgs === 'now' || rawArgs === 'playing' || rawArgs === 'np') {
@@ -1771,9 +1782,41 @@ Always specify owner and repo parameters on GitHub tools. The user's GitHub user
if (!rawArgs || rawArgs === 'status') {
const auth = this.spotifyClient.isAuthenticated() ? 'Connected' : 'Not connected';
const device = this.spotifyClient.getDeviceId() || 'none';
- const premium = this.spotifyClient.getPremiumStatus();
- const premiumLabel = premium === null ? '' : premium ? ' | Premium' : ' | Free (no playback control)';
- await channel.send(`Spotify: **${auth}**${premiumLabel}\nDevice: ${device !== 'none' ? device : 'none selected'}`, channelId);
+
+ let accountName = this.spotifyClient.getAccountName();
+ let accountId = this.spotifyClient.getAccountId();
+ let product = this.spotifyClient.getProduct();
+ let accountError = '';
+
+ if (!accountName) {
+ try {
+ await this.spotifyClient.saveAccountInfo();
+ accountName = this.spotifyClient.getAccountName();
+ accountId = this.spotifyClient.getAccountId();
+ product = this.spotifyClient.getProduct();
+ } catch (err: any) {
+ accountError = err.message;
+ logger.warn({ err: err.message }, 'Failed to fetch Spotify account info');
+ }
+ }
+
+ let premium = this.spotifyClient.getPremiumStatus();
+ if (premium === null) {
+ premium = await this.spotifyClient.checkPremium();
+ }
+
+ let status = `Spotify: **${auth}**`;
+ if (accountName) status += `\nAccount: **${accountName}**`;
+ if (accountId) status += `\nUser ID: ${accountId}`;
+ if (product) status += `\nPlan: ${product}`;
+ if (premium === true) {
+ status += ' — all features available';
+ } else if (premium === false) {
+ status += ' — playback control requires Premium';
+ }
+ if (accountError) status += `\n⚠ Could not verify account: ${accountError}`;
+ status += `\nDevice: ${device !== 'none' ? device : 'none selected'}`;
+ await channel.send(status, channelId);
return true;
}
@@ -1930,7 +1973,13 @@ Always specify owner and repo parameters on GitHub tools. The user's GitHub user
return true;
}
- await channel.send('Unknown /spotify command. Available: /spotify, /spotify auth, /spotify code <code>, /spotify player, /spotify devices, /spotify device <id>, /spotify now', channelId);
+ if (rawArgs === 'logout') {
+ this.spotifyClient.logout();
+ await channel.send('Spotify disconnected. Run `/spotify auth` to reconnect.', channelId);
+ return true;
+ }
+
+ await channel.send('Unknown /spotify command. Available: /spotify, /spotify auth, /spotify code <code>, /spotify logout, /spotify player, /spotify devices, /spotify device <id>, /spotify now', channelId);
return true;
}
diff --git a/src/index.ts b/src/index.ts
index e2225bb4..e28cb60f 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1203,7 +1203,17 @@ async function runAgent(isDaemon: boolean = false): Promise<void> {
agent.setSpotifyClient(spotifyClient);
if (spotifyClient.isAuthenticated()) {
- logger.info('Spotify connected (token available)');
+ if (!spotifyClient.getAccountName()) {
+ spotifyClient.saveAccountInfo().catch(() => {});
+ }
+ spotifyClient.checkPremium().catch(() => {});
+
+ const accountName = spotifyClient.getAccountName();
+ const label = accountName ? ` as ${accountName}` : '';
+ logger.info(`Spotify connected${label} (token available)`);
+ if (!isDaemon) {
+ console.log(chalk.green(` Spotify: connected${label}`));
+ }
} else if (!isDaemon) {
console.log(chalk.dim(' Spotify: not connected — run /spotify auth to link your account'));
}
@@ -1437,6 +1447,18 @@ program
console.log(` Telegram Access: ${chalk.white(getTelegramAccessSummary(config))}`);
console.log(` Skills: ${skills.length > 0 ? chalk.green(skills.map(s => s.name).join(', ')) : chalk.dim('none')}`);
console.log(` Budget: ${chalk.white(config.tokens.dailyBudget.toLocaleString())} tokens/day`);
+ const spotify = config.spotify;
+ if (spotify.clientId && spotify.clientSecret) {
+ if (spotify.enabled && (spotify.accessToken || spotify.refreshToken)) {
+ const label = spotify.accountName ? ` as ${spotify.accountName}` : '';
+ const plan = spotify.product ? ` (${spotify.product})` : '';
+ console.log(` Spotify: ${chalk.green(`connected${label}`)}${plan}`);
+ } else {
+ console.log(` Spotify: ${chalk.dim('not connected')} — run /spotify auth`);
+ }
+ } else {
+ console.log(` Spotify: ${chalk.dim('not configured')}`);
+ }
console.log(` Setup: ${isSetupComplete() ? chalk.green('complete') : chalk.red('not done')}`);
console.log(` Daemon: ${daemon.running ? chalk.green(`running (PID: ${daemon.pid})`) : chalk.dim('not running')}`);
console.log(` Home: ${chalk.dim(home)}`);
diff --git a/src/spotify/client.ts b/src/spotify/client.ts
index 7281dde6..091160db 100644
--- a/src/spotify/client.ts
+++ b/src/spotify/client.ts
@@ -107,6 +107,7 @@ export class SpotifyClient {
server.close();
logger.info('Spotify authentication successful');
+ this.saveAccountInfo().catch(() => {});
this.checkPremium().then((premium) => {
if (!premium) {
logger.warn('Spotify account is not Premium — playback control will be unavailable');
@@ -173,6 +174,7 @@ export class SpotifyClient {
logger.info('Spotify authentication successful (manual code)');
+ this.saveAccountInfo().catch(() => {});
this.checkPremium().then((premium) => {
if (!premium) {
logger.warn('Spotify account is not Premium — playback control will be unavailable');
@@ -432,14 +434,17 @@ export class SpotifyClient {
return this.apiRequest('/me');
}
- async checkPremium(): Promise<boolean> {
+ async checkPremium(): Promise<boolean | null> {
if (this.isPremium !== null) return this.isPremium;
try {
const me = await this.getMe();
this.isPremium = me.product === 'premium';
+ this.config.spotify.product = me.product || '';
+ saveConfig(this.config);
return this.isPremium;
- } catch {
- return false;
+ } catch (err: any) {
+ logger.warn({ err: err.message }, 'Failed to check Spotify premium status');
+ return null;
}
}
@@ -457,6 +462,50 @@ export class SpotifyClient {
return this.deviceId;
}
+ async saveAccountInfo(): Promise<void> {
+ try {
+ const me = await this.getMe();
+ this.config.spotify.accountName = me.display_name || '';
+ this.config.spotify.accountId = me.id || '';
+ this.config.spotify.product = me.product || '';
+ this.isPremium = me.product === 'premium';
+ saveConfig(this.config);
+ } catch {
+ logger.warn('Could not fetch Spotify account info');
+ }
+ }
+
+ getAccountName(): string {
+ return this.config.spotify.accountName;
+ }
+
+ getAccountId(): string {
+ return this.config.spotify.accountId;
+ }
+
+ getProduct(): string {
+ return this.config.spotify.product;
+ }
+
+ logout(): void {
+ this.accessToken = '';
+ this.refreshToken = '';
+ this.expiresAt = 0;
+ this.isPremium = null;
+ this.deviceId = '';
+ this.config.spotify.accessToken = '';
+ this.config.spotify.refreshToken = '';
+ this.config.spotify.expiresAt = '';
+ this.config.spotify.scopes = [];
+ this.config.spotify.deviceId = '';
+ this.config.spotify.accountName = '';
+ this.config.spotify.accountId = '';
+ this.config.spotify.product = '';
+ this.config.spotify.enabled = false;
+ saveConfig(this.config);
+ logger.info('Spotify logged out');
+ }
+
async getNowPlayingText(): Promise<string> {
try {
const data = await this.getCurrentlyPlaying();
diff --git a/src/utils/config.ts b/src/utils/config.ts
index a219d25c..7fdb9d9c 100644
--- a/src/utils/config.ts
+++ b/src/utils/config.ts
@@ -124,6 +124,9 @@ export interface MercuryConfig {
expiresAt: string;
scopes: string[];
deviceId: string;
+ accountName: string;
+ accountId: string;
+ product: string;
};
}
@@ -266,6 +269,9 @@ export function getDefaultConfig(): MercuryConfig {
expiresAt: '',
scopes: [],
deviceId: '',
+ accountName: '',
+ accountId: '',
+ product: '',
},
};
}
diff --git a/src/utils/manual.ts b/src/utils/manual.ts
index f3e74e86..d1e8aa3b 100644
--- a/src/utils/manual.ts
+++ b/src/utils/manual.ts
@@ -145,6 +145,7 @@ export function getManual(): string {
['/spotify devices', 'List available Spotify devices'],
['/spotify device <id>', 'Set active Spotify device'],
['/spotify now', 'Show what is currently playing'],
+ ['/spotify logout', 'Disconnect Spotify and clear saved tokens'],
['/unpair', 'Reset all Telegram access for this Mercury instance (admins only)'],
];