pi-ext
bugabingas pi extensions
owned by admin
fix: share lazy imports across concurrent extension calls
e2061b3ef0f1410128099808207769a21154ff54
Oliver Jan Krylow <oliver@bugabinga.net> ·
SSH signature · not checked
Browse filesCompare with HEAD
Changed files (8)
extensions/angel/index.ts
Modified 378 378 return normalized.
length <= 72 ? normalized
: `${normalized.slice(0, 69)}...`;
384 +let advisorModule
: Promise<
typeof import(
"./advisor.ts")>
| undefined;
385 +const loadAdvisor = ()
: Promise<
typeof import(
"./advisor.ts")>
=> {
386 + advisorModule
??= import(
"./advisor.ts");
387 + return advisorModule;
381 390 async function realRunConsultation(
382 391 ctx
: ExtensionContext,
383 392 request
: ConsultationRequest,
384 393 options
: RunConsultationOptions,
385 394 )
: Promise<
ConsultationResult> {
386 - const implementation
= await import(
"./advisor.ts");
395 + const implementation
= await loadAdvisor();
387 396 return implementation.
runConsultation(ctx, request, options);
extensions/git-safe/index.ts
Modified 36 36 import { isMarkerPresent }
from "./marker.js";
37 37 import { createMarkers, wrapUntrusted }
from "./spotlight.js";
42 +let cloneModule
: Promise<
typeof import(
"./clone.js")>
| undefined;
43 +const loadClone = ()
: Promise<
typeof import(
"./clone.js")>
=> {
44 + cloneModule
??= import(
"./clone.js");
41 50 const GIT_PREAMBLE
= [
285 - const { safeClone }
= await import(
"./clone.js");
294 + const { safeClone }
= await loadClone();
286 295 const result
= await safeClone(params.url, params.path, {
287 296 branch: params.branch,
288 297 sparse: params.sparse,
extensions/strata/implementation.ts
Modified 29 29 const emptyDraft = ()
: Draft => ({ reviewed: [], findings: [], notes:
"" });
34 +let githubModule
: Promise<
typeof import(
"./github.js")>
| undefined;
35 +const loadGithub = ()
: Promise<
typeof import(
"./github.js")>
=> {
36 + githubModule
??= import(
"./github.js");
37 + return githubModule;
31 40 export class Strata {
32 41 private controller
?: AbortController;
33 42 private server
?: ReviewServer;
73 82 let forge
: ForgeReview | undefined;
74 83 let source
: Source;
75 84 if (request.kind
=== "pr") {
76 - github
= await import(
"./github.js");
85 + github
= await loadGithub();
77 86 forge
= await github.
openGitHubReview(
79 88 request.identifier,
extensions/strata/index.ts
Modified 8 8 let runtime
: Strata | undefined;
10 10 const openings
= new Set<
Promise<
void>>();
14 + let implementationModule
: 15 + | Promise<
typeof import(
"./implementation.js")>
17 + const loadImplementation = ()
: Promise<
18 + typeof import(
"./implementation.js")
20 + implementationModule
??= import(
"./implementation.js");
21 + return implementationModule;
11 23 pi.
on(
"session_start", (_event, ctx)
=> {
12 24 const version
= ++generation;
13 25 void hasGit().
then((available)
=> {
37 49 const version
= generation;
38 - const { Strata }
= await import(
"./implementation.js");
50 + const { Strata }
= await loadImplementation();
39 51 if (generation
!== version)
return;
40 52 runtime
??= new Strata(pi);
41 53 const opening
= runtime.
open(args, ctx);
extensions/usage/index.ts
Modified1 1 import type { ExtensionAPI }
from "@earendil-works/pi-coding-agent";
6 +let implementationModule
: 7 + | Promise<
typeof import(
"./implementation.js")>
9 +const loadImplementation = ()
: Promise<
10 + typeof import(
"./implementation.js")
12 + implementationModule
??= import(
"./implementation.js");
13 + return implementationModule;
3 16 export default function usageExtension(pi
: ExtensionAPI)
: void {
4 17 pi.
registerCommand(
"usage", {
5 18 description:
"Show usage statistics dashboard",
6 19 handler:
async (args, ctx)
=> 7 - (
await import(
"./implementation.js")).
showUsage(args, ctx),
20 + (
await loadImplementation()).
showUsage(args, ctx),
extensions/web/__tests__/lazy.test.ts
Addednew file mode 100644 1 +import { describe, expect, it }
from "vitest";
2 +import { lazy }
from "../lazy";
4 +describe(
"lazy", ()
=> {
5 + it(
"shares one evaluation among concurrent callers",
async ()
=> {
7 + let resolveLoad: (value
: { ok
: boolean })
=> void = ()
=> {};
8 + const load
= lazy(()
=> {
10 + return new Promise<{ ok
: boolean }>((resolve)
=> {
11 + resolveLoad
= resolve;
15 + const first
= load();
16 + const second
= load();
17 + resolveLoad({ ok:
true });
19 + await expect(first).resolves.
toEqual({ ok:
true });
20 + await expect(second).resolves.
toEqual({ ok:
true });
21 + expect(calls).
toBe(
1);
24 + it(
"re-evaluates after a failed load",
async ()
=> {
26 + const load
= lazy(
async ()
=> {
28 + if (calls
=== 1)
throw new Error(
"boom");
32 + await expect(
load()).rejects.
toThrow(
"boom");
33 + await expect(
load()).resolves.
toBe(
2);
extensions/web/index.ts
Modified 32 32 DEFAULT_TIMEOUT_MS,
34 34 } from
"./constants.js";
35 +import { lazy }
from "./lazy.js";
35 36 import { checkToolAvailability, spawnToText, TOOL_HINTS }
from "./spawn.js";
40 41 } from
"./spotlight.js";
47 +const loadFetch
= lazy(()
=> import(
"./fetch.js"));
48 +const loadExtract
= lazy(()
=> import(
"./extract.js"));
49 +const loadPandoc
= lazy(()
=> import(
"./pandoc.js"));
50 +const loadStore
= lazy(()
=> import(
"./store.js"));
51 +const loadLineMatch
= lazy(()
=> import(
"./line-match.js"));
52 +const loadMap
= lazy(()
=> import(
"./map.js"));
53 +const loadSearch
= lazy(()
=> import(
"./search.js"));
44 57 function fmtSize(bytes
: number)
: string {
216 229 const dir
= await createTempDir(
"pi-web-");
217 230 const tmpPath
= join(dir,
`input.${fromFormat}`);
218 231 await writeFile(tmpPath, data);
219 - const { runPandocFile }
= await import(
"./pandoc.js");
232 + const { runPandocFile }
= await loadPandoc();
220 233 const text
= await runPandocFile(tmpPath, fromFormat,
"plain");
221 234 return { text, tmpPath };
581 594 if (hint)
throw new Error(hint);
583 596 const [{ extractMainContent }, { runPandoc }]
= await Promise.
all([
584 - import(
"./extract.js"),
585 - import(
"./pandoc.js"),
587 600 const extracted
= extractMainContent(body.
toString(
"utf-8"));
1029 1042 let contentType
: string;
1030 1043 let finalUrl
= params.url;
1032 - const { fetchUrlStream }
= await import(
"./fetch.js");
1045 + const { fetchUrlStream }
= await loadFetch();
1033 1046 const result
= await fetchUrlStream(params.url, onProgress, signal, {
1034 1047 maxBytes: params.maxBytes,
1035 1048 timeoutMs: params.timeout,
1077 1090 const fullOutput
= processed.output;
1078 1091 const [{ saveJsonResult }, { filterLineMatches }]
= await Promise.
all([
1079 - import(
"./store.js"),
1080 - import(
"./line-match.js"),
1082 1095 const stored
= await saveJsonResult(
"web", ctx?.cwd
?? process.
cwd(), {
1083 1096 url: params.url,
1229 1242 const [{ discoverSiteMap }, { saveJsonResult }]
= await Promise.
all([
1230 - import(
"./map.js"),
1231 - import(
"./store.js"),
1233 1246 const map
= await discoverSiteMap(
1369 - const { searchWeb }
= await import(
"./search.js");
1382 + const { searchWeb }
= await loadSearch();
1370 1383 const { results, source }
= await searchWeb({
1371 1384 query: params.query,
extensions/web/lazy.ts
Addednew file mode 100644 14 +export function lazy<
T>(
load: ()
=> Promise<
T>)
: ()
=> Promise<
T> {
15 + let promise
: Promise<
T>
| undefined;
17 + (promise
??= load().
catch((error
: unknown)
=> {
18 + promise
= undefined;