pi-freeflow 1.3.6 → 1.3.7
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/package.json +1 -1
- package/src/commands.ts +9 -1
- package/src/relay-state.ts +87 -8
- package/src/relay.ts +9 -1
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-freeflow",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.3.
|
|
4
|
+
"version": "1.3.7",
|
|
5
5
|
"description": "Thin provider for OMP/Pi — model list + dumb relay proxy + log; host pi-ai owns thinking/normalization",
|
|
6
6
|
"main": "extensions/index.ts",
|
|
7
7
|
"types": "src/index.ts",
|
package/src/commands.ts
CHANGED
|
@@ -19,6 +19,8 @@ import {
|
|
|
19
19
|
ensureRelay,
|
|
20
20
|
findRelay,
|
|
21
21
|
getActiveRelayState,
|
|
22
|
+
getRelayHealth,
|
|
23
|
+
isRelayHealthy,
|
|
22
24
|
removeRelay,
|
|
23
25
|
saveRelayState,
|
|
24
26
|
setActiveRelayState,
|
|
@@ -254,7 +256,13 @@ export function createCommandSpec(
|
|
|
254
256
|
const star = r.url === relayState.url ? "★" : " ";
|
|
255
257
|
const shortName = r.label ? `[${r.label}]` : `[${shortRelayLabel(r.url, relayState.relays)}]`;
|
|
256
258
|
const paddedName = shortName.padEnd(16, " ");
|
|
257
|
-
|
|
259
|
+
const health = getRelayHealth(r.url);
|
|
260
|
+
const isCooling = health && Date.now() < health.cooldownUntil;
|
|
261
|
+
const remainingSec = isCooling ? Math.ceil((health.cooldownUntil - Date.now()) / 1000) : 0;
|
|
262
|
+
const healthBadge = isCooling
|
|
263
|
+
? ` ⚠️ [cooling ${remainingSec}s: ${health.lastStatus ? `HTTP ${health.lastStatus}` : "error"}]`
|
|
264
|
+
: " ✓";
|
|
265
|
+
return `${star} [${idx + 1}] ${paddedName} → ${r.url}${healthBadge}`;
|
|
258
266
|
});
|
|
259
267
|
const activeLabel = shortRelayLabel(relayState.url, relayState.relays);
|
|
260
268
|
const activeIdx = Math.max(
|
package/src/relay-state.ts
CHANGED
|
@@ -170,6 +170,81 @@ let activeRelayState: RelayState = resolveRelayState();
|
|
|
170
170
|
let roundRobinCounter = 0;
|
|
171
171
|
let activeStatusUi: ExtensionUIContext | null = null;
|
|
172
172
|
let isFreeFlowModelActive = true;
|
|
173
|
+
|
|
174
|
+
export interface RelayHealth {
|
|
175
|
+
consecutiveFailures: number;
|
|
176
|
+
lastFailureTime: number;
|
|
177
|
+
cooldownUntil: number;
|
|
178
|
+
lastStatus?: number;
|
|
179
|
+
lastError?: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const relayHealthMap = new Map<string, RelayHealth>();
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Mark a relay as healthy and active on successful response.
|
|
186
|
+
*/
|
|
187
|
+
export function markRelaySuccess(url: string): void {
|
|
188
|
+
if (!url) return;
|
|
189
|
+
relayHealthMap.delete(url.trim());
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Mark a relay as degraded with temporary cooldown on failure/429/timeout/socket error.
|
|
194
|
+
*/
|
|
195
|
+
export function markRelayFailure(url: string, status?: number, error?: string): void {
|
|
196
|
+
if (!url) return;
|
|
197
|
+
const clean = url.trim();
|
|
198
|
+
const prev = relayHealthMap.get(clean) || {
|
|
199
|
+
consecutiveFailures: 0,
|
|
200
|
+
lastFailureTime: 0,
|
|
201
|
+
cooldownUntil: 0,
|
|
202
|
+
};
|
|
203
|
+
const consecutive = prev.consecutiveFailures + 1;
|
|
204
|
+
const now = Date.now();
|
|
205
|
+
let cooldownMs = 30_000; // 30s default for socket/network/502/503
|
|
206
|
+
|
|
207
|
+
if (status === 429) {
|
|
208
|
+
cooldownMs = 90_000; // 90s cooldown for upstream rate limits
|
|
209
|
+
} else if (status === 504) {
|
|
210
|
+
cooldownMs = 60_000; // 60s cooldown for gateway timeout
|
|
211
|
+
} else if (status && status >= 500) {
|
|
212
|
+
cooldownMs = 45_000; // 45s for 5xx errors
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
relayHealthMap.set(clean, {
|
|
216
|
+
consecutiveFailures: consecutive,
|
|
217
|
+
lastFailureTime: now,
|
|
218
|
+
cooldownUntil: now + cooldownMs,
|
|
219
|
+
lastStatus: status,
|
|
220
|
+
lastError: error,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Check if a relay is currently healthy (not in active cooldown).
|
|
226
|
+
*/
|
|
227
|
+
export function isRelayHealthy(url: string): boolean {
|
|
228
|
+
if (!url) return true;
|
|
229
|
+
const clean = url.trim();
|
|
230
|
+
const health = relayHealthMap.get(clean);
|
|
231
|
+
if (!health) return true;
|
|
232
|
+
return Date.now() >= health.cooldownUntil;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Get current health snapshot for a relay.
|
|
237
|
+
*/
|
|
238
|
+
export function getRelayHealth(url: string): RelayHealth | undefined {
|
|
239
|
+
return relayHealthMap.get(url.trim());
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Reset all in-memory relay health records.
|
|
244
|
+
*/
|
|
245
|
+
export function resetAllRelayHealth(): void {
|
|
246
|
+
relayHealthMap.clear();
|
|
247
|
+
}
|
|
173
248
|
/**
|
|
174
249
|
* Mtime of the on-disk state file at the moment we last read or wrote it.
|
|
175
250
|
* session's master daemon, while never clobbering this process's own
|
|
@@ -280,19 +355,23 @@ export function getOrderedRelayUrls(): string[] {
|
|
|
280
355
|
// Rotate starting point per-request to avoid thundering herd when many
|
|
281
356
|
// subagents hit the shared 127.0.0.1 daemon at once — each request
|
|
282
357
|
// tries a different primary relay, but still rolls seamlessly on 429.
|
|
283
|
-
const
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
|
|
358
|
+
const totalRelays = activeRelayState.relays.length;
|
|
359
|
+
const startIdx = (activeIdx + (roundRobinCounter++ % totalRelays)) % totalRelays;
|
|
360
|
+
const rawOrdered: string[] = [];
|
|
361
|
+
for (let i = 0; i < totalRelays; i++) {
|
|
362
|
+
const r = activeRelayState.relays[(startIdx + i) % totalRelays];
|
|
287
363
|
if (r?.url?.trim()) {
|
|
288
|
-
|
|
364
|
+
rawOrdered.push(r.url.trim());
|
|
289
365
|
}
|
|
290
366
|
}
|
|
367
|
+
|
|
368
|
+
// Partition into healthy candidates first, degraded/cooling candidates at the tail
|
|
369
|
+
const healthy = rawOrdered.filter((u) => isRelayHealthy(u));
|
|
370
|
+
const cooling = rawOrdered.filter((u) => !isRelayHealthy(u));
|
|
371
|
+
const ordered = [...healthy, ...cooling];
|
|
372
|
+
|
|
291
373
|
return ordered.length > 0 ? ordered : [DEFAULT_RELAY_URL];
|
|
292
374
|
}
|
|
293
|
-
if (activeRelayState.url?.trim()) {
|
|
294
|
-
return [activeRelayState.url.trim()];
|
|
295
|
-
}
|
|
296
375
|
return [DEFAULT_RELAY_URL];
|
|
297
376
|
}
|
|
298
377
|
|
package/src/relay.ts
CHANGED
|
@@ -11,6 +11,8 @@ import { isDebugEnabled, log } from "./logger.ts";
|
|
|
11
11
|
import {
|
|
12
12
|
getActiveRelayState,
|
|
13
13
|
getOrderedRelayUrls,
|
|
14
|
+
markRelayFailure,
|
|
15
|
+
markRelaySuccess,
|
|
14
16
|
saveRelayState,
|
|
15
17
|
setActiveRelayState,
|
|
16
18
|
shortRelayLabel,
|
|
@@ -106,6 +108,7 @@ export async function relayFetch(
|
|
|
106
108
|
// Vercel 504 Gateway Timeout on heavy prompts (>50KB or >25s):
|
|
107
109
|
// Fast fallback directly to upstream instead of cycling through multiple 25s timeouts.
|
|
108
110
|
if (res.status === 504) {
|
|
111
|
+
markRelayFailure(targetUrl, 504, "Gateway Timeout (25s exceeded)");
|
|
109
112
|
log(
|
|
110
113
|
"warn",
|
|
111
114
|
`relay ${targetUrl} hit HTTP 504 Gateway Timeout in ${elapsed}s (prompt evaluation exceeded Vercel 25s limit) — fast fallback to direct upstream`,
|
|
@@ -116,6 +119,7 @@ export async function relayFetch(
|
|
|
116
119
|
}
|
|
117
120
|
|
|
118
121
|
if (isRetriableStatus(res.status)) {
|
|
122
|
+
markRelayFailure(targetUrl, res.status);
|
|
119
123
|
lastResponse = res;
|
|
120
124
|
log(
|
|
121
125
|
"warn",
|
|
@@ -126,6 +130,8 @@ export async function relayFetch(
|
|
|
126
130
|
continue;
|
|
127
131
|
}
|
|
128
132
|
|
|
133
|
+
markRelaySuccess(targetUrl);
|
|
134
|
+
|
|
129
135
|
// SUCCESS or non-retriable client error (e.g. 200, 404):
|
|
130
136
|
// If we switched to a different relay because previous failed, update sticky active relay!
|
|
131
137
|
if (relayState.url !== targetUrl) {
|
|
@@ -151,10 +157,12 @@ export async function relayFetch(
|
|
|
151
157
|
} catch (err) {
|
|
152
158
|
const elapsed = ((Date.now() - attemptStart) / 1000).toFixed(1);
|
|
153
159
|
lastError = err;
|
|
160
|
+
const errMsg = (err as Error)?.message || String(err);
|
|
161
|
+
markRelayFailure(targetUrl, 0, errMsg);
|
|
154
162
|
log(
|
|
155
163
|
"warn",
|
|
156
164
|
`relay ${targetUrl} fetch error in ${elapsed}s — rolling to next relay`,
|
|
157
|
-
{ upstream: url, error:
|
|
165
|
+
{ upstream: url, error: errMsg },
|
|
158
166
|
rid,
|
|
159
167
|
);
|
|
160
168
|
continue;
|