@akanjs/devkit 3.3.0-rc.1 → 3.3.0-rc.10
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/CHANGELOG.md +229 -2
- package/README.ko.md +3 -2
- package/README.md +3 -3
- package/akanApp/akanApp.host.test.ts +22 -5
- package/akanApp/akanApp.host.ts +46 -9
- package/akanApp/akanApp.host.wiring.test.ts +206 -2
- package/akanApp/devBackend.ts +3 -1
- package/akanApp/devBuilder.ts +5 -1
- package/akanApp/devStartTimeline.test.ts +115 -0
- package/akanApp/devStartTimeline.ts +69 -0
- package/akanConfig/akanConfig.test.ts +57 -8
- package/akanConfig/akanConfig.ts +12 -65
- package/akanConfig/dockerImage.ts +17 -0
- package/akanConfig/nativeKeys.ts +52 -0
- package/app/applicationBuildRunner.test.ts +49 -1
- package/app/applicationBuildRunner.ts +3 -1
- package/codeAgent/agent/CodeAgent.ts +36 -16
- package/codeAgent/agent/CodeAgentAllowed.ts +35 -0
- package/codeAgent/agent/CodeAgentCommandLine.ts +372 -0
- package/codeAgent/agent/CodeAgentGate.ts +23 -4
- package/codeAgent/agent/CodeAgentRpcHost.ts +1 -1
- package/codeAgent/agent/codeAgent.test.ts +62 -0
- package/codeAgent/agent/codeAgentCommandLine.test.ts +212 -0
- package/codeAgent/agent/codeAgentContract.test.ts +117 -5
- package/codeAgent/skills/akan-agent/SKILL.md +8 -1
- package/codeAgent/skills/akan-code/SKILL.md +38 -3
- package/codeAgent/skills/akan-page/SKILL.md +18 -13
- package/codeAgent/skills/akan-runtime/SKILL.md +21 -16
- package/codeAgent/skills/akan-service/SKILL.md +2 -2
- package/docs/apiReference.test.ts +5 -0
- package/docs/apiReference.ts +7 -3
- package/docs/declarationReader.ts +28 -1
- package/executors/appExecutor.ts +35 -11
- package/executors/executors.test.ts +30 -3
- package/frontendBuild/buildCacheStamp.test.ts +42 -0
- package/frontendBuild/buildCacheStamp.ts +23 -0
- package/frontendBuild/buildRouteClient.test.ts +2 -7
- package/frontendBuild/clientEntryDiscovery.ts +2 -2
- package/frontendBuild/cssCandidateCache.test.ts +70 -1
- package/frontendBuild/cssCandidateCache.ts +17 -10
- package/frontendBuild/cssCompiler.test.ts +112 -0
- package/frontendBuild/cssCompiler.ts +41 -21
- package/frontendBuild/devBarrelImports.test.ts +30 -0
- package/frontendBuild/devBarrelImports.ts +2 -2
- package/frontendBuild/fontOptimizer.test.ts +144 -3
- package/frontendBuild/fontOptimizer.ts +103 -18
- package/frontendBuild/pagesBundleBuilder.ts +3 -1
- package/frontendBuild/plainStylesheet.test.ts +155 -0
- package/frontendBuild/plainStylesheet.ts +197 -0
- package/frontendBuild/rscServerRendererGuard.test.ts +60 -0
- package/frontendBuild/rscServerRendererGuard.ts +68 -0
- package/frontendBuild/ssrBaseArtifactBuilder.test.ts +30 -1
- package/frontendBuild/ssrBaseArtifactBuilder.ts +40 -12
- package/guideline/agentAssets.test.ts +10 -0
- package/guideline/agentAssets.ts +52 -11
- package/incrementalBuilder/buildBatch.proc.ts +11 -10
- package/incrementalBuilder/buildBatchProtocol.ts +2 -1
- package/incrementalBuilder/builderBoot.ts +14 -7
- package/incrementalBuilder/builderChannel.ts +8 -2
- package/incrementalBuilder/builderGraphPayloads.ts +4 -4
- package/incrementalBuilder/builderLanes.ts +2 -2
- package/incrementalBuilder/builderWatch.ts +8 -8
- package/incrementalBuilder/csrRegistry.ts +4 -4
- package/incrementalBuilder/incrementalBuilder.host.test.ts +16 -0
- package/incrementalBuilder/incrementalBuilder.host.ts +7 -1
- package/incrementalBuilder/incrementalBuilder.proc.test.ts +41 -0
- package/incrementalBuilder/incrementalBuilder.proc.ts +9 -8
- package/incrementalBuilder/ssrRegistry.ts +6 -6
- package/index.ts +1 -1
- package/integration/renderAdmissionSoak.test.ts +99 -0
- package/integration/renderAdmissionSoak.ts +609 -0
- package/mobile/nativeConfig.test.ts +100 -0
- package/mobile/nativeConfig.ts +55 -16
- package/mobile/nativePublicEnv.ts +2 -1
- package/package.json +14 -169
- package/packageExports.test.ts +38 -24
- package/transforms/barrelAnalyzer.ts +2 -2
- package/transforms/transforms.test.ts +3 -3
- package/tunnel/createTunnel.test.ts +29 -8
- package/tunnel/createTunnel.ts +18 -11
- package/util/fileEditor.ts +1 -0
- package/util/fileSys.test.ts +13 -0
- package/util/fileSys.ts +2 -2
- package/util/selectModel.ts +1 -0
- package/workspace/getRelatedCnsts.ts +7 -0
package/akanConfig/akanConfig.ts
CHANGED
|
@@ -11,6 +11,8 @@ import { WorkspaceExecutor } from "../executors/workspaceExecutor";
|
|
|
11
11
|
import { ServerEnvProfile } from "../transforms/serverEnvProfile";
|
|
12
12
|
import type { BaseDevEnv, PackageJson } from "../workspace/types";
|
|
13
13
|
import { AkanBin } from "./akanBin";
|
|
14
|
+
import { DEFAULT_DOCKER_IMAGE, DOCKER_RUN_USER, DOCKER_WRITE_DIRS } from "./dockerImage";
|
|
15
|
+
import { nativeKeys } from "./nativeKeys";
|
|
14
16
|
import {
|
|
15
17
|
type AkanApiConfig,
|
|
16
18
|
type AkanAssetsConfig,
|
|
@@ -47,8 +49,6 @@ const DEFAULT_OPTIMIZE_IMPORTS = [
|
|
|
47
49
|
"@material-ui/icons @tabler/icons-react mui-core react-icons/*",
|
|
48
50
|
].flatMap((line) => line.split(" "));
|
|
49
51
|
const WORKSPACE_BARREL_FACETS = ["ui", "webkit", "common", "client", "server"] as const;
|
|
50
|
-
// `1-slim` floats to whichever Bun is newest at build time, one the framework was never tested on.
|
|
51
|
-
const DEFAULT_DOCKER_IMAGE = "oven/bun:1.4.4-slim";
|
|
52
52
|
const SSR_RUNTIME_PACKAGES = ["react", "react-dom", "react-server-dom-webpack"] as const;
|
|
53
53
|
const DEFAULT_AKAN_IMAGE_CONFIG: AkanImageConfig = {
|
|
54
54
|
deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
|
|
@@ -452,56 +452,6 @@ export class AkanAppConfig implements AppConfigResult {
|
|
|
452
452
|
static #isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
453
453
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
454
454
|
}
|
|
455
|
-
static readonly #nativeKeys = {
|
|
456
|
-
native: [
|
|
457
|
-
"basePath",
|
|
458
|
-
"indexPath",
|
|
459
|
-
"appName",
|
|
460
|
-
"appId",
|
|
461
|
-
"fileName",
|
|
462
|
-
"version",
|
|
463
|
-
"buildNum",
|
|
464
|
-
"icon",
|
|
465
|
-
"splash",
|
|
466
|
-
"backgroundColor",
|
|
467
|
-
"permissions",
|
|
468
|
-
"plugins",
|
|
469
|
-
"deepLinks",
|
|
470
|
-
"updates",
|
|
471
|
-
"devtools",
|
|
472
|
-
"csp",
|
|
473
|
-
"openUrls",
|
|
474
|
-
"ios",
|
|
475
|
-
"android",
|
|
476
|
-
"desktop",
|
|
477
|
-
],
|
|
478
|
-
ios: ["indexPath", "teamId", "infoPlist", "entitlements", "privacy", "files"],
|
|
479
|
-
android: [
|
|
480
|
-
"indexPath",
|
|
481
|
-
"sha256CertFingerprints",
|
|
482
|
-
"googleServices",
|
|
483
|
-
"push",
|
|
484
|
-
"autoplay",
|
|
485
|
-
"manifest",
|
|
486
|
-
"application",
|
|
487
|
-
"activity",
|
|
488
|
-
"files",
|
|
489
|
-
],
|
|
490
|
-
desktop: [
|
|
491
|
-
"indexPath",
|
|
492
|
-
"server",
|
|
493
|
-
"recovery",
|
|
494
|
-
"window",
|
|
495
|
-
"screenCapture",
|
|
496
|
-
"webview2",
|
|
497
|
-
"publisher",
|
|
498
|
-
"entitlements",
|
|
499
|
-
"dmg",
|
|
500
|
-
],
|
|
501
|
-
desktopServer: ["omit", "profile", "modules", "env", "restart", "watchdog"],
|
|
502
|
-
dmg: ["background", "background2x", "window", "iconSize", "textSize", "app", "applications"],
|
|
503
|
-
deepLinks: ["schemes", "domains"],
|
|
504
|
-
} as const;
|
|
505
455
|
//* Settings that moved when `mobile` became `native` are named with where they went, instead of "unknown".
|
|
506
456
|
static readonly #movedKeys: Readonly<Record<string, string>> = {
|
|
507
457
|
native: "the platform sections and plugins sit directly in the native section: ios, android, desktop, plugins",
|
|
@@ -526,19 +476,15 @@ export class AkanAppConfig implements AppConfigResult {
|
|
|
526
476
|
);
|
|
527
477
|
}
|
|
528
478
|
};
|
|
529
|
-
check(section,
|
|
479
|
+
check(section, nativeKeys.native, where, "native");
|
|
530
480
|
const settings = section as AkanNativeSettings;
|
|
531
|
-
check(settings.ios,
|
|
532
|
-
check(settings.android,
|
|
533
|
-
check(settings.desktop,
|
|
534
|
-
check(
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
"desktopServer",
|
|
539
|
-
);
|
|
540
|
-
check(settings.desktop?.dmg, AkanAppConfig.#nativeKeys.dmg, `${where}.desktop.dmg`, "dmg");
|
|
541
|
-
check(settings.deepLinks, AkanAppConfig.#nativeKeys.deepLinks, `${where}.deepLinks`, "deepLinks");
|
|
481
|
+
check(settings.ios, nativeKeys.ios, `${where}.ios`, "ios");
|
|
482
|
+
check(settings.android, nativeKeys.android, `${where}.android`, "android");
|
|
483
|
+
check(settings.desktop, nativeKeys.desktop, `${where}.desktop`, "desktop");
|
|
484
|
+
check(settings.desktop?.server, nativeKeys.desktopServer, `${where}.desktop.server`, "desktopServer");
|
|
485
|
+
check(settings.desktop?.dmg, nativeKeys.dmg, `${where}.desktop.dmg`, "dmg");
|
|
486
|
+
check(settings.desktop?.externalView, nativeKeys.externalView, `${where}.desktop.externalView`, "externalView");
|
|
487
|
+
check(settings.deepLinks, nativeKeys.deepLinks, `${where}.deepLinks`, "deepLinks");
|
|
542
488
|
}
|
|
543
489
|
static readonly #androidFilePattern = /^(?:res\/[^/]+\/[^/]+|assets\/.+)$/;
|
|
544
490
|
static #assertFiles(settings: AkanNativeSettings, where: string, configPath: string) {
|
|
@@ -659,13 +605,14 @@ RUN apt-get update && apt-get upgrade -y && apt-get install -y --no-install-reco
|
|
|
659
605
|
RUN ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime
|
|
660
606
|
ARG TARGETARCH
|
|
661
607
|
${preRunScripts.join("\n")}
|
|
662
|
-
RUN mkdir -p /workspace
|
|
663
608
|
WORKDIR /workspace
|
|
664
609
|
${lockfile ? "COPY ./package.json ./bun.lock ./" : "COPY ./package.json ./package.json"}
|
|
665
610
|
RUN bun install --production${lockfile ? " --frozen-lockfile" : ""}
|
|
666
611
|
${postRunScripts.join("\n")}
|
|
667
612
|
COPY . .
|
|
613
|
+
RUN mkdir -p ${DOCKER_WRITE_DIRS.join(" ")} && chown ${DOCKER_RUN_USER} ${DOCKER_WRITE_DIRS.join(" ")}
|
|
668
614
|
${envLines.join("\n")}
|
|
615
|
+
USER ${DOCKER_RUN_USER}
|
|
669
616
|
CMD [${command.map((c) => `"${c}"`).join(",")}]`;
|
|
670
617
|
}
|
|
671
618
|
static #importGeneration = 0;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//? A Bun bump changes both: `docker buildx imagetools inspect oven/bun:<version>-slim` prints the new multi-arch digest.
|
|
2
|
+
export const BUN_IMAGE_PIN = {
|
|
3
|
+
version: "1.4.4",
|
|
4
|
+
digest: "sha256:1846adf22496bd711036aae8be147e57bdebbaea2e5264e3e0b5af1db187d9a4",
|
|
5
|
+
} as const;
|
|
6
|
+
// `1-slim` floats to whichever Bun is newest at build time, one the framework was never tested on.
|
|
7
|
+
export const DEFAULT_DOCKER_IMAGE = `oven/bun:${BUN_IMAGE_PIN.version}-slim@${BUN_IMAGE_PIN.digest}`;
|
|
8
|
+
//? oven/bun's `bun` user, numeric so a kubelet can check `runAsNonRoot` without reading the image's passwd file.
|
|
9
|
+
export const DOCKER_RUN_USER = "1000:1000";
|
|
10
|
+
//? The directories the server writes, its code staying read-only; an empty named volume mounted on one takes its owner.
|
|
11
|
+
export const DOCKER_WRITE_DIRS = [
|
|
12
|
+
"/workspace/sqlite",
|
|
13
|
+
"/workspace/local",
|
|
14
|
+
"/workspace/runtime",
|
|
15
|
+
"/workspace/data",
|
|
16
|
+
"/workspace/.akan/artifact/image-cache",
|
|
17
|
+
];
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export const nativeKeys = {
|
|
2
|
+
native: [
|
|
3
|
+
"basePath",
|
|
4
|
+
"indexPath",
|
|
5
|
+
"appName",
|
|
6
|
+
"appId",
|
|
7
|
+
"fileName",
|
|
8
|
+
"version",
|
|
9
|
+
"buildNum",
|
|
10
|
+
"icon",
|
|
11
|
+
"splash",
|
|
12
|
+
"backgroundColor",
|
|
13
|
+
"permissions",
|
|
14
|
+
"plugins",
|
|
15
|
+
"deepLinks",
|
|
16
|
+
"updates",
|
|
17
|
+
"devtools",
|
|
18
|
+
"csp",
|
|
19
|
+
"openUrls",
|
|
20
|
+
"ios",
|
|
21
|
+
"android",
|
|
22
|
+
"desktop",
|
|
23
|
+
],
|
|
24
|
+
ios: ["indexPath", "teamId", "infoPlist", "entitlements", "privacy", "files", "hideFormAccessoryBar"],
|
|
25
|
+
android: [
|
|
26
|
+
"indexPath",
|
|
27
|
+
"sha256CertFingerprints",
|
|
28
|
+
"googleServices",
|
|
29
|
+
"push",
|
|
30
|
+
"autoplay",
|
|
31
|
+
"manifest",
|
|
32
|
+
"application",
|
|
33
|
+
"activity",
|
|
34
|
+
"files",
|
|
35
|
+
],
|
|
36
|
+
desktop: [
|
|
37
|
+
"indexPath",
|
|
38
|
+
"server",
|
|
39
|
+
"recovery",
|
|
40
|
+
"window",
|
|
41
|
+
"screenCapture",
|
|
42
|
+
"webview2",
|
|
43
|
+
"publisher",
|
|
44
|
+
"entitlements",
|
|
45
|
+
"dmg",
|
|
46
|
+
"externalView",
|
|
47
|
+
],
|
|
48
|
+
externalView: ["allowOrigins", "evaluate", "sendKey"],
|
|
49
|
+
desktopServer: ["omit", "profile", "modules", "env", "restart", "watchdog"],
|
|
50
|
+
dmg: ["background", "background2x", "window", "iconSize", "textSize", "app", "applications"],
|
|
51
|
+
deepLinks: ["schemes", "domains"],
|
|
52
|
+
} as const;
|
|
@@ -439,7 +439,9 @@ console.info(new SampleService().name);
|
|
|
439
439
|
|
|
440
440
|
expect(proc.exitCode).toBe(1);
|
|
441
441
|
expect(proc.stdout.toString()).toBe("");
|
|
442
|
-
expect(proc.stderr.toString()).toContain(
|
|
442
|
+
expect(proc.stderr.toString()).toContain(
|
|
443
|
+
`${path.join("dist", "verify", "portal")} holds the partial output of akan verify, not a build. Run \`akan build portal\` and start dist/apps/portal.`,
|
|
444
|
+
);
|
|
443
445
|
expect(await Bun.file(path.join(app.dist.cwdPath, "akan.build.json")).json()).toMatchObject({
|
|
444
446
|
profile: "verify",
|
|
445
447
|
});
|
|
@@ -452,4 +454,50 @@ console.info(new SampleService().name);
|
|
|
452
454
|
await rm(root, { recursive: true, force: true });
|
|
453
455
|
}
|
|
454
456
|
});
|
|
457
|
+
|
|
458
|
+
test("a verify build writes to dist/verify/<app> and leaves the full build in dist/apps/<app> as it was", async () => {
|
|
459
|
+
const { root, workspace } = await createTempApp("portal");
|
|
460
|
+
const keys = ["AKAN_PUBLIC_ENV", "AKAN_PUBLIC_REPO_NAME", "AKAN_PUBLIC_SERVE_DOMAIN"] as const;
|
|
461
|
+
const shellEnv = Object.fromEntries(keys.map((key) => [key, process.env[key]]));
|
|
462
|
+
const appDir = path.join(root, "apps", "portal");
|
|
463
|
+
await Promise.all(["main.ts", "server.ts"].map((file) => writeFile(path.join(appDir, file), "")));
|
|
464
|
+
await mkdir(path.join(appDir, "public"));
|
|
465
|
+
await writeFile(path.join(appDir, "public", "hero.png"), "image");
|
|
466
|
+
const built = path.join(root, "dist", "apps", "portal");
|
|
467
|
+
const fullBuild = {
|
|
468
|
+
"main.js": 'console.log("booted");\n',
|
|
469
|
+
"akan.build.json": '{ "buildId": "abc123" }\n',
|
|
470
|
+
"akan.env.json": '{ "AKAN_PUBLIC_ENV": "local" }\n',
|
|
471
|
+
"public/hero.png": "image",
|
|
472
|
+
};
|
|
473
|
+
for (const [file, content] of Object.entries(fullBuild)) {
|
|
474
|
+
await mkdir(path.dirname(path.join(built, file)), { recursive: true });
|
|
475
|
+
await writeFile(path.join(built, file), content);
|
|
476
|
+
}
|
|
477
|
+
const app = AppExecutor.from(workspace, "portal");
|
|
478
|
+
const bundle = spyOn(Bun, "build").mockImplementation(
|
|
479
|
+
async () => ({ success: true, outputs: [], logs: [] }) as unknown as Bun.BuildOutput,
|
|
480
|
+
);
|
|
481
|
+
try {
|
|
482
|
+
const { outputDir } = await new ApplicationBuildRunner(app, { verify: true }).build();
|
|
483
|
+
|
|
484
|
+
for (const [file, content] of Object.entries(fullBuild))
|
|
485
|
+
expect(await readFile(path.join(built, file), "utf8").catch(() => null)).toBe(content);
|
|
486
|
+
const verified = path.join(root, "dist", "verify", "portal");
|
|
487
|
+
expect(outputDir).toBe(verified);
|
|
488
|
+
expect(await Bun.file(path.join(verified, "akan.build.json")).json()).toMatchObject({ profile: "verify" });
|
|
489
|
+
expect(await Bun.file(path.join(verified, "console.js")).exists()).toBe(true);
|
|
490
|
+
expect(await Bun.file(path.join(verified, "public", "hero.png")).exists()).toBe(false);
|
|
491
|
+
const outdirs = bundle.mock.calls.map(([config]) => config.outdir);
|
|
492
|
+
expect(outdirs.length).toBeGreaterThan(0);
|
|
493
|
+
expect(outdirs.every((outdir) => outdir === verified)).toBe(true);
|
|
494
|
+
} finally {
|
|
495
|
+
bundle.mockRestore();
|
|
496
|
+
for (const key of keys) {
|
|
497
|
+
if (shellEnv[key] === undefined) delete process.env[key];
|
|
498
|
+
else process.env[key] = shellEnv[key];
|
|
499
|
+
}
|
|
500
|
+
await rm(root, { recursive: true, force: true });
|
|
501
|
+
}
|
|
502
|
+
});
|
|
455
503
|
});
|
|
@@ -164,6 +164,7 @@ export class ApplicationBuildRunner {
|
|
|
164
164
|
async build({ spinner = false }: BuildOptions = {}): Promise<BuildResult> {
|
|
165
165
|
// serial build is needed because of Bun.build is unstable for parallel build
|
|
166
166
|
this.#spinner = spinner;
|
|
167
|
+
if (this.#verify) this.#app.useVerifyDist();
|
|
167
168
|
const { web, assets, baseDevEnv } = await this.#app.getConfig();
|
|
168
169
|
const environment = this.#environment ?? baseDevEnv.env;
|
|
169
170
|
WorkspaceExecutor.assertDeployableEnv(baseDevEnv, environment, "akan build");
|
|
@@ -370,7 +371,8 @@ export class ApplicationBuildRunner {
|
|
|
370
371
|
//? `bun main.js` in dist/apps/<app> is a documented way to run a build, and a verify output has no CSR bundle or
|
|
371
372
|
//? production env file, so its main.js refuses to boot before any app module runs.
|
|
372
373
|
#verifyBanner() {
|
|
373
|
-
const
|
|
374
|
+
const { name, dist, workspace } = this.#app;
|
|
375
|
+
const message = `${path.relative(workspace.workspaceRoot, dist.cwdPath)} holds the partial output of akan verify, not a build. Run \`akan build ${name}\` and start dist/apps/${name}.`;
|
|
374
376
|
return `console.error(${JSON.stringify(message)});process.exit(1);`;
|
|
375
377
|
}
|
|
376
378
|
|
|
@@ -70,6 +70,15 @@ export interface CodeAgentOptions {
|
|
|
70
70
|
resume?: string;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
const textOf = (content: unknown) => {
|
|
74
|
+
if (typeof content === "string") return content.trim();
|
|
75
|
+
if (!Array.isArray(content)) return "";
|
|
76
|
+
return content
|
|
77
|
+
.map((part) => (typeof part === "object" && part && "text" in part ? String(part.text) : ""))
|
|
78
|
+
.join("")
|
|
79
|
+
.trim();
|
|
80
|
+
};
|
|
81
|
+
|
|
73
82
|
export class CodeAgent {
|
|
74
83
|
readonly #profile: CodeAgentProfile;
|
|
75
84
|
readonly #gate: CodeAgentGate;
|
|
@@ -171,6 +180,10 @@ export class CodeAgent {
|
|
|
171
180
|
return this.#profile;
|
|
172
181
|
}
|
|
173
182
|
|
|
183
|
+
get allowed() {
|
|
184
|
+
return this.#gate.allowed;
|
|
185
|
+
}
|
|
186
|
+
|
|
174
187
|
get canSeeImages() {
|
|
175
188
|
const model = this.#session?.model;
|
|
176
189
|
return model ? akanCodeModelSupportsImages(model) : false;
|
|
@@ -269,21 +282,12 @@ export class CodeAgent {
|
|
|
269
282
|
const turnId = `restored:${session.sessionId}`;
|
|
270
283
|
for (const message of session.messages) {
|
|
271
284
|
if (message.role !== "user" && message.role !== "assistant") continue;
|
|
272
|
-
const text =
|
|
285
|
+
const text = textOf(message.content);
|
|
273
286
|
if (text) this.#emit({ type: "message", turnId, role: message.role, text });
|
|
274
287
|
}
|
|
275
288
|
this.#emitContextNow();
|
|
276
289
|
}
|
|
277
290
|
|
|
278
|
-
static #textOf(content: unknown) {
|
|
279
|
-
if (typeof content === "string") return content.trim();
|
|
280
|
-
if (!Array.isArray(content)) return "";
|
|
281
|
-
return content
|
|
282
|
-
.map((part) => (typeof part === "object" && part && "text" in part ? String(part.text) : ""))
|
|
283
|
-
.join("")
|
|
284
|
-
.trim();
|
|
285
|
-
}
|
|
286
|
-
|
|
287
291
|
on(listener: (event: CodeAgentEvent) => void) {
|
|
288
292
|
this.#listeners.add(listener);
|
|
289
293
|
return () => this.#listeners.delete(listener);
|
|
@@ -339,9 +343,13 @@ export class CodeAgent {
|
|
|
339
343
|
return true;
|
|
340
344
|
}
|
|
341
345
|
|
|
342
|
-
async approve(approvalId: string, approved: boolean) {
|
|
346
|
+
async approve(approvalId: string, approved: boolean, remember?: "session") {
|
|
343
347
|
if (this.#asks.resolveApproval(approvalId, approved)) {
|
|
344
|
-
|
|
348
|
+
const remembered = this.allowed.settle(approvalId, approved && remember === "session");
|
|
349
|
+
this.#emit({ type: "approval_resolved", approvalId, approved, ...(remembered.length ? { remembered } : {}) });
|
|
350
|
+
for (const covered of this.allowed.covered())
|
|
351
|
+
if (this.#asks.resolveApproval(covered, true))
|
|
352
|
+
this.#emit({ type: "approval_resolved", approvalId: covered, approved: true });
|
|
345
353
|
return true;
|
|
346
354
|
}
|
|
347
355
|
if (this.#profile.interaction.approval !== "suspend") return false;
|
|
@@ -542,7 +550,12 @@ export class CodeAgent {
|
|
|
542
550
|
event.input,
|
|
543
551
|
"This reaches a host outside the profile's list, which needs the user's approval, and nobody can be asked in this session. Use a listed host, or say what you need fetched.",
|
|
544
552
|
);
|
|
545
|
-
const approved = await this.#requestApproval(
|
|
553
|
+
const approved = await this.#requestApproval(
|
|
554
|
+
event.toolCallId,
|
|
555
|
+
event.toolName,
|
|
556
|
+
verdict.approval,
|
|
557
|
+
verdict.remember,
|
|
558
|
+
);
|
|
546
559
|
if (approved) return undefined;
|
|
547
560
|
return this.#refuse(event.toolCallId, event.toolName, event.input, "The user declined this action.");
|
|
548
561
|
});
|
|
@@ -555,24 +568,31 @@ export class CodeAgent {
|
|
|
555
568
|
return { block: true, reason };
|
|
556
569
|
}
|
|
557
570
|
|
|
558
|
-
async #requestApproval(toolCallId: string, name: string,
|
|
571
|
+
async #requestApproval(toolCallId: string, name: string, summary: string, offered?: string[]) {
|
|
559
572
|
// Nobody to ask means nobody to refuse: a pod would otherwise deny every write it was created to make.
|
|
560
573
|
if (!this.#profile.ui.canPrompt) return true;
|
|
561
574
|
const approvalId = this.#asks.nextId("a");
|
|
575
|
+
const suspends = this.#profile.interaction.approval === "suspend";
|
|
576
|
+
// A suspended approval ends its turn, and the retry is a new call, so there is no session answer to offer.
|
|
577
|
+
const remember = suspends ? undefined : offered;
|
|
562
578
|
const request = {
|
|
563
579
|
approvalId,
|
|
564
580
|
toolCallId,
|
|
565
581
|
name,
|
|
566
582
|
summary: codeAgentClip(summary, codeAgentLabelChars),
|
|
567
583
|
policy: this.#profile.approval,
|
|
584
|
+
...(remember ? { remember } : {}),
|
|
568
585
|
};
|
|
569
586
|
this.#emit({ type: "approval", request });
|
|
570
|
-
if (
|
|
587
|
+
if (suspends) {
|
|
571
588
|
this.#suspended.addApproval(request);
|
|
572
589
|
this.#mapper.noteOutcome("awaiting");
|
|
573
590
|
return false;
|
|
574
591
|
}
|
|
575
|
-
|
|
592
|
+
if (remember) this.allowed.offer(approvalId, remember);
|
|
593
|
+
const approved = await this.#asks.openApproval(approvalId);
|
|
594
|
+
this.allowed.settle(approvalId, false);
|
|
595
|
+
return approved;
|
|
576
596
|
}
|
|
577
597
|
|
|
578
598
|
/** The rendered answer; `undefined` when skipped, suspended, or nobody can be prompted. */
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Kept in this process only: a resumed or reloaded session starts with nothing allowed.
|
|
2
|
+
export class CodeAgentAllowed {
|
|
3
|
+
readonly #names = new Set<string>();
|
|
4
|
+
readonly #offers = new Map<string, string[]>();
|
|
5
|
+
|
|
6
|
+
get names() {
|
|
7
|
+
return [...this.#names].sort();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
missing(names: string[]) {
|
|
11
|
+
return names.filter((name) => !this.#names.has(name));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
offer(approvalId: string, names: string[]) {
|
|
15
|
+
this.#offers.set(approvalId, names);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
settle(approvalId: string, remember: boolean) {
|
|
19
|
+
const names = this.#offers.get(approvalId) ?? [];
|
|
20
|
+
this.#offers.delete(approvalId);
|
|
21
|
+
if (!remember) return [];
|
|
22
|
+
for (const name of names) this.#names.add(name);
|
|
23
|
+
return names;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
covered() {
|
|
27
|
+
return [...this.#offers].filter(([, names]) => !this.missing(names).length).map(([approvalId]) => approvalId);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
clear() {
|
|
31
|
+
const count = this.#names.size;
|
|
32
|
+
this.#names.clear();
|
|
33
|
+
return count;
|
|
34
|
+
}
|
|
35
|
+
}
|