polydev-ai 1.9.13 → 1.9.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/mcp/stdio-wrapper.js +122 -17
  2. package/package.json +1 -1
@@ -585,7 +585,16 @@ To re-login: npx polydev-ai`
585
585
  // if this process dies, the new one can resume polling with same session)
586
586
  this.startLoginPolling(sessionId);
587
587
 
588
- // Return immediately with accurate status
588
+ // Wait for authentication to complete (blocks up to 120s)
589
+ console.error('[Polydev] Waiting for browser authentication to complete...');
590
+ const authenticated = await this.waitForAuthCompletion();
591
+
592
+ if (authenticated) {
593
+ console.error('[Polydev] Login successful, returning status...');
594
+ return await this.fetchAuthStatusResponse(id);
595
+ }
596
+
597
+ // Timeout - return fallback message
589
598
  return {
590
599
  jsonrpc: '2.0',
591
600
  id,
@@ -595,20 +604,12 @@ To re-login: npx polydev-ai`
595
604
  text: `AUTHENTICATION STARTED
596
605
  ======================
597
606
 
598
- A browser window is opening for you to sign in.
607
+ A browser window was opened for you to sign in.
599
608
 
600
- If it doesn't open, visit:
609
+ If it didn't open, visit:
601
610
  ${authUrl}
602
611
 
603
- Once you sign in, your token will be saved automatically to:
604
- ~/.polydev.env
605
- ~/.zshrc
606
-
607
- After login completes, you can:
608
- /polydev:ask Query multiple AI models
609
- /polydev:auth Check status & credits
610
-
611
- Dashboard: https://polydev.ai/dashboard`
612
+ Still waiting for login. Use /polydev:auth to check status after signing in.`
612
613
  }]
613
614
  }
614
615
  };
@@ -658,8 +659,102 @@ Dashboard: https://polydev.ai/dashboard`
658
659
  }
659
660
  }
660
661
 
662
+ /**
663
+ * Wait for authentication to complete by polling this.isAuthenticated
664
+ * Used to block tool calls until login is detected by startLoginPolling
665
+ * @param {number} timeoutMs - Max time to wait (default 120s)
666
+ * @returns {boolean} true if authenticated, false if timed out
667
+ */
668
+ async waitForAuthCompletion(timeoutMs = 120000) {
669
+ const pollInterval = 3000; // Check every 3 seconds
670
+ const maxPolls = Math.floor(timeoutMs / pollInterval);
671
+
672
+ for (let i = 0; i < maxPolls; i++) {
673
+ await new Promise(r => setTimeout(r, pollInterval));
674
+ if (this.isAuthenticated && this.userToken) {
675
+ return true;
676
+ }
677
+ }
678
+ return false;
679
+ }
680
+
681
+ /**
682
+ * Fetch account status and return formatted response
683
+ * Standalone helper that doesn't trigger re-auth (avoids circular calls)
684
+ * @param {*} id - JSON-RPC request id
685
+ * @returns {object} JSON-RPC response with account status
686
+ */
687
+ async fetchAuthStatusResponse(id) {
688
+ try {
689
+ const accountResponse = await fetch('https://www.polydev.ai/api/auth/status', {
690
+ method: 'POST',
691
+ headers: {
692
+ 'Content-Type': 'application/json',
693
+ 'Authorization': `Bearer ${this.userToken}`,
694
+ 'User-Agent': 'polydev-stdio-wrapper/1.0.0'
695
+ }
696
+ });
697
+
698
+ if (accountResponse.ok) {
699
+ const data = await accountResponse.json();
700
+ const credits = data.credits_remaining?.toLocaleString() || 0;
701
+ const messages = data.messages_remaining?.toLocaleString() || 0;
702
+ const tier = data.subscription_tier || 'Free';
703
+ const email = data.email || 'Connected';
704
+
705
+ return {
706
+ jsonrpc: '2.0',
707
+ id,
708
+ result: {
709
+ content: [{
710
+ type: 'text',
711
+ text: `LOGIN SUCCESSFUL
712
+ ================
713
+
714
+ Authentication: Connected
715
+ Account: ${email}
716
+ Credits: ${credits}
717
+ Messages: ${messages}
718
+ Tier: ${tier}
719
+
720
+ You can now use:
721
+ /polydev:ask Query multiple AI models
722
+ /polydev:auth Check full status & credits
723
+
724
+ Dashboard: https://polydev.ai/dashboard`
725
+ }]
726
+ }
727
+ };
728
+ }
729
+ } catch (e) {
730
+ console.error('[Polydev] Post-login status fetch failed:', e.message);
731
+ }
732
+
733
+ // Fallback if status fetch fails
734
+ return {
735
+ jsonrpc: '2.0',
736
+ id,
737
+ result: {
738
+ content: [{
739
+ type: 'text',
740
+ text: `LOGIN SUCCESSFUL
741
+ ================
742
+
743
+ Your token has been saved and is active.
744
+
745
+ You can now use:
746
+ /polydev:ask Query multiple AI models
747
+ /polydev:auth Check full status & credits
748
+
749
+ Dashboard: https://polydev.ai/dashboard`
750
+ }]
751
+ }
752
+ };
753
+ }
754
+
661
755
  /**
662
756
  * Trigger re-authentication by opening browser and starting polling
757
+ * Waits for auth to complete inline (up to 120s) then returns full status
663
758
  * Used when token is detected as invalid/expired
664
759
  * @param {*} id - JSON-RPC request id
665
760
  * @param {string} reason - Human-readable reason for re-auth
@@ -703,23 +798,33 @@ Dashboard: https://polydev.ai/dashboard`
703
798
  this.savePendingSession(sessionId);
704
799
  this.startLoginPolling(sessionId);
705
800
 
801
+ // Wait for authentication to complete (blocks up to 120s)
802
+ console.error('[Polydev] Waiting for browser authentication to complete...');
803
+ const authenticated = await this.waitForAuthCompletion();
804
+
805
+ if (authenticated) {
806
+ console.error('[Polydev] Re-authentication successful, returning status...');
807
+ return await this.fetchAuthStatusResponse(id);
808
+ }
809
+
810
+ // Timeout - return fallback message
706
811
  return {
707
812
  jsonrpc: '2.0',
708
813
  id,
709
814
  result: {
710
815
  content: [{
711
816
  type: 'text',
712
- text: `RE-AUTHENTICATION REQUIRED
713
- ==========================
817
+ text: `RE-AUTHENTICATION STARTED
818
+ =========================
714
819
 
715
820
  ${reason}
716
821
 
717
- A browser window is opening to re-authenticate.
822
+ A browser window was opened for re-authentication.
718
823
 
719
- If it doesn't open, visit:
824
+ If it didn't open, visit:
720
825
  ${authUrl}
721
826
 
722
- Once you sign in, your token will be updated automatically.`
827
+ Still waiting for login. Use /polydev:auth to check status after signing in.`
723
828
  }]
724
829
  }
725
830
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polydev-ai",
3
- "version": "1.9.13",
3
+ "version": "1.9.14",
4
4
  "engines": {
5
5
  "node": ">=20.x <=22.x"
6
6
  },