pi-cliproxyapi 0.3.2 → 0.3.3
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/index.ts +26 -2
- package/package.json +1 -1
- package/src/cache.ts +58 -0
- package/src/fetch-models.ts +4 -1
package/index.ts
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
17
17
|
|
|
18
18
|
import { applyAll } from "./src/apply.ts";
|
|
19
|
+
import { readDiscoveryCache } from "./src/cache.ts";
|
|
19
20
|
import { registerCommands } from "./src/commands.ts";
|
|
20
21
|
import { loadConfig, resolveConfigValue } from "./src/config.ts";
|
|
21
22
|
import { detectConflicts } from "./src/conflicts.ts";
|
|
@@ -39,8 +40,31 @@ export default async function cliproxyapi(pi: ExtensionAPI): Promise<void> {
|
|
|
39
40
|
for (const c of conflicts) log.warn(`conflict (${c.kind}): ${c.detail}`);
|
|
40
41
|
|
|
41
42
|
try {
|
|
42
|
-
const
|
|
43
|
-
|
|
43
|
+
const cached = readDiscoveryCache();
|
|
44
|
+
if (cached) {
|
|
45
|
+
// Serve the last good discovery instantly so Pi startup never blocks on
|
|
46
|
+
// the ~5s proxy round-trip, then revalidate over the network in the
|
|
47
|
+
// background (applyAll is idempotent — it just re-registers providers).
|
|
48
|
+
log.info(
|
|
49
|
+
`discovery from cache (age ${Math.round(cached.ageMs / 1000)}s): ${cached.discovery.builtinProviders.length} builtin, ${cached.discovery.customPool.length} custom`,
|
|
50
|
+
);
|
|
51
|
+
await applyAll(pi, cfg, cached.discovery);
|
|
52
|
+
void (async () => {
|
|
53
|
+
try {
|
|
54
|
+
const fresh = await fetchDiscovery(cfg, resolvedKey);
|
|
55
|
+
await applyAll(pi, cfg, fresh);
|
|
56
|
+
log.debug("discovery revalidated from network");
|
|
57
|
+
} catch (e) {
|
|
58
|
+
log.warn(
|
|
59
|
+
"background discovery revalidate failed:",
|
|
60
|
+
(e as Error).message,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
})();
|
|
64
|
+
} else {
|
|
65
|
+
const discovery = await fetchDiscovery(cfg, resolvedKey);
|
|
66
|
+
await applyAll(pi, cfg, discovery);
|
|
67
|
+
}
|
|
44
68
|
} catch (err) {
|
|
45
69
|
log.error("initial apply failed:", (err as Error).message);
|
|
46
70
|
// Commands stay registered; user can open /cliproxy and press r to refresh.
|
package/package.json
CHANGED
package/src/cache.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Disk cache for Discovery — stale-while-revalidate.
|
|
2
|
+
//
|
|
3
|
+
// The well-known/v1-models round-trip costs ~5s on every Pi startup (the proxy
|
|
4
|
+
// answers /.well-known/pi slowly), and Pi re-spawns the agent for every
|
|
5
|
+
// provider-feature probe, so that cost is paid constantly. We persist the last
|
|
6
|
+
// good Discovery and serve it instantly on boot, then revalidate over the
|
|
7
|
+
// network in the background.
|
|
8
|
+
|
|
9
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
|
|
12
|
+
import { CONFIG_DIR } from "./config.ts";
|
|
13
|
+
import type { Discovery } from "./fetch-models.ts";
|
|
14
|
+
import { log } from "./log.ts";
|
|
15
|
+
|
|
16
|
+
export const DISCOVERY_CACHE_PATH = join(CONFIG_DIR, "discovery-cache.json");
|
|
17
|
+
|
|
18
|
+
interface CacheEnvelope {
|
|
19
|
+
savedAt: number;
|
|
20
|
+
discovery: Discovery;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CachedDiscovery {
|
|
24
|
+
discovery: Discovery;
|
|
25
|
+
ageMs: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function readDiscoveryCache(): CachedDiscovery | null {
|
|
29
|
+
if (!existsSync(DISCOVERY_CACHE_PATH)) return null;
|
|
30
|
+
try {
|
|
31
|
+
const env = JSON.parse(
|
|
32
|
+
readFileSync(DISCOVERY_CACHE_PATH, "utf8"),
|
|
33
|
+
) as CacheEnvelope;
|
|
34
|
+
if (
|
|
35
|
+
!env ||
|
|
36
|
+
typeof env.savedAt !== "number" ||
|
|
37
|
+
!env.discovery ||
|
|
38
|
+
!Array.isArray(env.discovery.builtinProviders) ||
|
|
39
|
+
!Array.isArray(env.discovery.customPool)
|
|
40
|
+
) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
return { discovery: env.discovery, ageMs: Date.now() - env.savedAt };
|
|
44
|
+
} catch (err) {
|
|
45
|
+
log.warn("failed to read discovery cache:", (err as Error).message);
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function writeDiscoveryCache(discovery: Discovery): void {
|
|
51
|
+
try {
|
|
52
|
+
mkdirSync(dirname(DISCOVERY_CACHE_PATH), { recursive: true });
|
|
53
|
+
const env: CacheEnvelope = { savedAt: Date.now(), discovery };
|
|
54
|
+
writeFileSync(DISCOVERY_CACHE_PATH, JSON.stringify(env), "utf8");
|
|
55
|
+
} catch (err) {
|
|
56
|
+
log.warn("failed to write discovery cache:", (err as Error).message);
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/fetch-models.ts
CHANGED
|
@@ -17,10 +17,11 @@ import {
|
|
|
17
17
|
normalizeSuggestedProvider,
|
|
18
18
|
reasoningFromId,
|
|
19
19
|
} from "./compat.ts";
|
|
20
|
+
import { writeDiscoveryCache } from "./cache.ts";
|
|
20
21
|
import type { ProxyConfig } from "./config.ts";
|
|
21
22
|
import { log } from "./log.ts";
|
|
22
23
|
|
|
23
|
-
export const PLUGIN_USER_AGENT = "pi-cliproxyapi/0.3.
|
|
24
|
+
export const PLUGIN_USER_AGENT = "pi-cliproxyapi/0.3.3";
|
|
24
25
|
const REQUEST_TIMEOUT_MS = 5_000;
|
|
25
26
|
|
|
26
27
|
export interface DiscoveryModelEntry {
|
|
@@ -308,6 +309,7 @@ export async function fetchDiscovery(
|
|
|
308
309
|
log.info(
|
|
309
310
|
`discovery via /.well-known/pi: ${wk.builtinProviders.length} builtin, ${wk.customPool.length} custom`,
|
|
310
311
|
);
|
|
312
|
+
writeDiscoveryCache(wk);
|
|
311
313
|
return wk;
|
|
312
314
|
}
|
|
313
315
|
if (!resolvedKey) {
|
|
@@ -320,6 +322,7 @@ export async function fetchDiscovery(
|
|
|
320
322
|
log.info(
|
|
321
323
|
`discovery via /v1/models: ${d.builtinProviders.length} builtin, ${d.customPool.length} custom`,
|
|
322
324
|
);
|
|
325
|
+
writeDiscoveryCache(d);
|
|
323
326
|
return d;
|
|
324
327
|
}
|
|
325
328
|
|