@1presence/bridge 0.38.0 → 0.39.0

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.
package/dist/auth.js CHANGED
@@ -86,26 +86,32 @@ function runBrowserAuthFlow(gatewayUrl, pwaUrl) {
86
86
  res.setHeader('Access-Control-Allow-Origin', pwaOrigin);
87
87
  }
88
88
  res.setHeader('Vary', 'Origin');
89
- res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
89
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
90
90
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
91
91
  if (req.method === 'OPTIONS') {
92
92
  res.writeHead(204);
93
93
  res.end();
94
94
  return;
95
95
  }
96
- if (req.method !== 'POST') {
97
- res.writeHead(405);
98
- res.end();
99
- return;
100
- }
101
96
  const reqUrl = new URL(req.url ?? '/', 'http://localhost');
102
97
  const path = reqUrl.pathname;
103
- // Every POST must carry the launch-specific nonce.
98
+ // Every request must carry the launch-specific nonce.
104
99
  if (!checkNonce(reqUrl.searchParams.get('nonce'))) {
105
100
  res.writeHead(403);
106
101
  res.end();
107
102
  return;
108
103
  }
104
+ // Lets the PWA verify the bridge is still listening before POSTing the token.
105
+ if (req.method === 'GET' && (path === '/' || path === '')) {
106
+ res.writeHead(200, { 'Content-Type': 'text/plain' });
107
+ res.end('1Presence bridge waiting for sign-in');
108
+ return;
109
+ }
110
+ if (req.method !== 'POST') {
111
+ res.writeHead(405);
112
+ res.end();
113
+ return;
114
+ }
109
115
  // Status beacon from the PWA — used so we exit early when the user closes
110
116
  // the auth tab before signing in (sendBeacon path).
111
117
  if (path === '/status') {
package/dist/index.js CHANGED
@@ -60,19 +60,43 @@ async function fetchSystemPrompt(token, agentSlug) {
60
60
  // default 1Presence. Without it, every Local Mode turn was the generalist.
61
61
  const agentParam = agentSlug ? `&agent=${encodeURIComponent(agentSlug)}` : '';
62
62
  const url = `${GATEWAY_HTTP}/system-prompt-for-bridge?timezone=${encodeURIComponent(tz)}${agentParam}`;
63
- let res;
64
- try {
65
- res = await fetch(url, {
66
- headers: { Authorization: `Bearer ${token}` },
67
- });
68
- }
69
- catch (err) {
70
- throw new Error(`fetch failed for ${url}: ${err.message}`);
71
- }
72
- if (!res.ok) {
73
- const body = await res.text().catch(() => '<unreadable body>');
74
- throw new Error(`/system-prompt-for-bridge returned ${res.status} ${res.statusText}: ${body.slice(0, 500)}`);
63
+ const headers = { Authorization: `Bearer ${token}` };
64
+ const maxAttempts = 8;
65
+ let res = null;
66
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
67
+ try {
68
+ res = await fetch(url, { headers });
69
+ }
70
+ catch (err) {
71
+ if (attempt === maxAttempts) {
72
+ throw new Error(`fetch failed for ${url}: ${err.message}`);
73
+ }
74
+ await new Promise((r) => setTimeout(r, 2_000));
75
+ continue;
76
+ }
77
+ if (res.status === 503 || res.status === 502) {
78
+ const body = await res.text().catch(() => '');
79
+ let retryable = true;
80
+ try {
81
+ const parsed = JSON.parse(body);
82
+ retryable = parsed.error === 'agent_waking' || parsed.error === 'agent_unreachable';
83
+ }
84
+ catch { /* use default */ }
85
+ if (retryable && attempt < maxAttempts) {
86
+ const delayMs = Math.min(2_000 * attempt, 10_000);
87
+ console.log(`[bridge] agent pod waking (${res.status}), retrying system prompt in ${delayMs / 1000}s…`);
88
+ await new Promise((r) => setTimeout(r, delayMs));
89
+ continue;
90
+ }
91
+ }
92
+ if (!res.ok) {
93
+ const body = await res.text().catch(() => '<unreadable body>');
94
+ throw new Error(`/system-prompt-for-bridge returned ${res.status} ${res.statusText}: ${body.slice(0, 500)}`);
95
+ }
96
+ break;
75
97
  }
98
+ if (!res)
99
+ throw new Error(`/system-prompt-for-bridge failed after ${maxAttempts} attempts`);
76
100
  let data;
77
101
  try {
78
102
  data = await res.json();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1presence/bridge",
3
- "version": "0.38.0",
3
+ "version": "0.39.0",
4
4
  "description": "Run 1Presence on your Mac and use your Claude.ai Pro subscription from any device",
5
5
  "bin": {
6
6
  "1presence-bridge": "dist/index.js"