@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 +13 -7
- package/dist/index.js +36 -12
- package/package.json +1 -1
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
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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();
|