@kodelyth/codex 2026.5.40 → 2026.5.42

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.
Files changed (178) hide show
  1. package/dist/client-ChMX13_o.js +642 -0
  2. package/dist/client-factory-D3dIsp4Y.js +9 -0
  3. package/dist/command-formatters-BRW7_Nu7.js +519 -0
  4. package/dist/command-handlers-P2IqtXaZ.js +1462 -0
  5. package/dist/compact-baos5flR.js +329 -0
  6. package/dist/computer-use-VfLvTMaa.js +367 -0
  7. package/dist/config-CezENx_E.js +510 -0
  8. package/dist/doctor-contract-api.js +53 -0
  9. package/dist/harness.js +51 -0
  10. package/dist/index.js +1133 -0
  11. package/dist/media-understanding-provider.js +335 -0
  12. package/dist/models-B9DhrIwD.js +110 -0
  13. package/dist/node-cli-sessions-De4_DuFw.js +1216 -0
  14. package/dist/plugin-activation-BlMuJeXz.js +452 -0
  15. package/dist/prompt-overlay.js +12 -0
  16. package/dist/protocol-C9UWI98H.js +9 -0
  17. package/dist/protocol-validators-BGBspNmF.js +5988 -0
  18. package/dist/provider-catalog.js +84 -0
  19. package/dist/provider-discovery.js +33 -0
  20. package/dist/provider.js +150 -0
  21. package/dist/rate-limit-cache-CHuacE27.js +24 -0
  22. package/dist/request-CTQKUxaa.js +89 -0
  23. package/dist/rolldown-runtime-DUslC3ob.js +14 -0
  24. package/dist/run-attempt-DqV2OU1R.js +5366 -0
  25. package/dist/session-binding-3PzU7ZTW.js +222 -0
  26. package/dist/shared-client-Cnyr9dyT.js +631 -0
  27. package/dist/side-question-CP5XlA0U.js +667 -0
  28. package/dist/test-api.js +45 -0
  29. package/dist/thread-lifecycle-DBJetBuV.js +1561 -0
  30. package/dist/vision-tools-Cl_5a93K.js +1379 -0
  31. package/doctor-contract-api.test.ts +44 -0
  32. package/doctor-contract-api.ts +68 -0
  33. package/harness.ts +72 -0
  34. package/index.test.ts +230 -0
  35. package/index.ts +66 -0
  36. package/klaw.plugin.json +24 -85
  37. package/media-understanding-provider.test.ts +486 -0
  38. package/media-understanding-provider.ts +521 -0
  39. package/package.json +3 -3
  40. package/prompt-overlay-runtime-contract.test.ts +48 -0
  41. package/prompt-overlay.ts +21 -0
  42. package/provider-catalog.ts +83 -0
  43. package/provider-discovery.ts +45 -0
  44. package/provider.test.ts +384 -0
  45. package/provider.ts +243 -0
  46. package/src/app-server/app-inventory-cache.test.ts +176 -0
  47. package/src/app-server/app-inventory-cache.ts +324 -0
  48. package/src/app-server/approval-bridge.test.ts +1471 -0
  49. package/src/app-server/approval-bridge.ts +1211 -0
  50. package/src/app-server/auth-bridge.test.ts +1449 -0
  51. package/src/app-server/auth-bridge.ts +614 -0
  52. package/src/app-server/auth-profile-runtime-contract.test.ts +239 -0
  53. package/src/app-server/capabilities.ts +27 -0
  54. package/src/app-server/client-factory.ts +24 -0
  55. package/src/app-server/client.test.ts +563 -0
  56. package/src/app-server/client.ts +715 -0
  57. package/src/app-server/compact.test.ts +710 -0
  58. package/src/app-server/compact.ts +500 -0
  59. package/src/app-server/computer-use.test.ts +788 -0
  60. package/src/app-server/computer-use.ts +683 -0
  61. package/src/app-server/config.test.ts +879 -0
  62. package/src/app-server/config.ts +1038 -0
  63. package/src/app-server/context-engine-projection.test.ts +252 -0
  64. package/src/app-server/context-engine-projection.ts +403 -0
  65. package/src/app-server/delivery-no-reply-runtime-contract.test.ts +80 -0
  66. package/src/app-server/dynamic-tool-diagnostics.ts +73 -0
  67. package/src/app-server/dynamic-tool-profile.ts +69 -0
  68. package/src/app-server/dynamic-tools.test.ts +1302 -0
  69. package/src/app-server/dynamic-tools.ts +623 -0
  70. package/src/app-server/elicitation-bridge.test.ts +1056 -0
  71. package/src/app-server/elicitation-bridge.ts +783 -0
  72. package/src/app-server/event-projector.test.ts +2668 -0
  73. package/src/app-server/event-projector.ts +2057 -0
  74. package/src/app-server/image-payload-sanitizer.test.ts +49 -0
  75. package/src/app-server/image-payload-sanitizer.ts +167 -0
  76. package/src/app-server/klaw-owned-tool-runtime-contract.test.ts +456 -0
  77. package/src/app-server/local-runtime-attribution.ts +39 -0
  78. package/src/app-server/managed-binary.test.ts +139 -0
  79. package/src/app-server/managed-binary.ts +193 -0
  80. package/src/app-server/models.test.ts +246 -0
  81. package/src/app-server/models.ts +172 -0
  82. package/src/app-server/native-hook-relay.test.ts +271 -0
  83. package/src/app-server/native-hook-relay.ts +150 -0
  84. package/src/app-server/native-subagent-task-mirror.test.ts +573 -0
  85. package/src/app-server/native-subagent-task-mirror.ts +497 -0
  86. package/src/app-server/outcome-fallback-runtime-contract.test.ts +404 -0
  87. package/src/app-server/plugin-activation.test.ts +336 -0
  88. package/src/app-server/plugin-activation.ts +283 -0
  89. package/src/app-server/plugin-app-cache-key.ts +74 -0
  90. package/src/app-server/plugin-approval-roundtrip.ts +122 -0
  91. package/src/app-server/plugin-inventory.test.ts +355 -0
  92. package/src/app-server/plugin-inventory.ts +357 -0
  93. package/src/app-server/plugin-thread-config.test.ts +865 -0
  94. package/src/app-server/plugin-thread-config.ts +455 -0
  95. package/src/app-server/protocol-generated/json/DynamicToolCallParams.json +33 -0
  96. package/src/app-server/protocol-generated/json/v2/ErrorNotification.json +199 -0
  97. package/src/app-server/protocol-generated/json/v2/GetAccountResponse.json +102 -0
  98. package/src/app-server/protocol-generated/json/v2/ModelListResponse.json +227 -0
  99. package/src/app-server/protocol-generated/json/v2/ThreadResumeResponse.json +2630 -0
  100. package/src/app-server/protocol-generated/json/v2/ThreadStartResponse.json +2630 -0
  101. package/src/app-server/protocol-generated/json/v2/TurnCompletedNotification.json +1659 -0
  102. package/src/app-server/protocol-generated/json/v2/TurnStartResponse.json +1655 -0
  103. package/src/app-server/protocol-validators.test.ts +75 -0
  104. package/src/app-server/protocol-validators.ts +203 -0
  105. package/src/app-server/protocol.ts +520 -0
  106. package/src/app-server/rate-limit-cache.ts +48 -0
  107. package/src/app-server/rate-limits.test.ts +202 -0
  108. package/src/app-server/rate-limits.ts +583 -0
  109. package/src/app-server/request.ts +73 -0
  110. package/src/app-server/run-attempt.context-engine.test.ts +1004 -0
  111. package/src/app-server/run-attempt.test.ts +9477 -0
  112. package/src/app-server/run-attempt.ts +4683 -0
  113. package/src/app-server/run-attempt.vision-tools.test.ts +35 -0
  114. package/src/app-server/schema-normalization-runtime-contract.test.ts +206 -0
  115. package/src/app-server/session-binding.test.ts +303 -0
  116. package/src/app-server/session-binding.ts +398 -0
  117. package/src/app-server/session-history.ts +44 -0
  118. package/src/app-server/shared-client.test.ts +589 -0
  119. package/src/app-server/shared-client.ts +289 -0
  120. package/src/app-server/side-question.test.ts +1175 -0
  121. package/src/app-server/side-question.ts +1007 -0
  122. package/src/app-server/test-support.ts +48 -0
  123. package/src/app-server/thread-lifecycle.test.ts +447 -0
  124. package/src/app-server/thread-lifecycle.ts +939 -0
  125. package/src/app-server/thread-lifecycle.user-mcp-servers.test.ts +442 -0
  126. package/src/app-server/timeout.ts +9 -0
  127. package/src/app-server/tool-progress-normalization.ts +77 -0
  128. package/src/app-server/trajectory.test.ts +205 -0
  129. package/src/app-server/trajectory.ts +365 -0
  130. package/src/app-server/transcript-mirror.test.ts +524 -0
  131. package/src/app-server/transcript-mirror.ts +208 -0
  132. package/src/app-server/transcript-repair-runtime-contract.test.ts +44 -0
  133. package/src/app-server/transport-stdio.test.ts +171 -0
  134. package/src/app-server/transport-stdio.ts +107 -0
  135. package/src/app-server/transport-websocket.test.ts +69 -0
  136. package/src/app-server/transport-websocket.ts +90 -0
  137. package/src/app-server/transport.ts +117 -0
  138. package/src/app-server/user-input-bridge.test.ts +249 -0
  139. package/src/app-server/user-input-bridge.ts +316 -0
  140. package/src/app-server/version.ts +4 -0
  141. package/src/app-server/vision-tools.ts +12 -0
  142. package/src/command-account.ts +544 -0
  143. package/src/command-formatters.ts +425 -0
  144. package/src/command-handlers.ts +2004 -0
  145. package/src/command-rpc.test.ts +16 -0
  146. package/src/command-rpc.ts +142 -0
  147. package/src/commands.test.ts +3312 -0
  148. package/src/commands.ts +65 -0
  149. package/src/conversation-binding-data.ts +124 -0
  150. package/src/conversation-binding.test.ts +599 -0
  151. package/src/conversation-binding.ts +561 -0
  152. package/src/conversation-control.test.ts +126 -0
  153. package/src/conversation-control.ts +303 -0
  154. package/src/conversation-turn-collector.test.ts +191 -0
  155. package/src/conversation-turn-collector.ts +186 -0
  156. package/src/conversation-turn-input.test.ts +141 -0
  157. package/src/conversation-turn-input.ts +106 -0
  158. package/src/manifest.test.ts +20 -0
  159. package/src/migration/apply.ts +501 -0
  160. package/src/migration/helpers.ts +55 -0
  161. package/src/migration/plan.ts +461 -0
  162. package/src/migration/provider.test.ts +1741 -0
  163. package/src/migration/provider.ts +41 -0
  164. package/src/migration/source.ts +643 -0
  165. package/src/migration/targets.ts +25 -0
  166. package/src/node-cli-sessions.test.ts +180 -0
  167. package/src/node-cli-sessions.ts +711 -0
  168. package/test-api.ts +82 -0
  169. package/tsconfig.json +16 -0
  170. package/doctor-contract-api.js +0 -7
  171. package/harness.js +0 -7
  172. package/index.js +0 -7
  173. package/media-understanding-provider.js +0 -7
  174. package/prompt-overlay.js +0 -7
  175. package/provider-catalog.js +0 -7
  176. package/provider-discovery.js +0 -7
  177. package/provider.js +0 -7
  178. package/test-api.js +0 -7
@@ -0,0 +1,39 @@
1
+ import type { EmbeddedRunAttemptParams } from "klaw/plugin-sdk/agent-harness-runtime";
2
+
3
+ const OPENAI_PROVIDER_ID = "openai";
4
+ const OPENAI_RESPONSES_API = "openai-responses";
5
+ const OPENAI_CODEX_PROVIDER_ID = "openai-codex";
6
+ const OPENAI_CODEX_RESPONSES_API = "openai-codex-responses";
7
+
8
+ export type CodexLocalRuntimeAttribution = {
9
+ provider: string;
10
+ api?: string;
11
+ };
12
+
13
+ function normalizeRuntimeId(value: string | undefined): string {
14
+ return value?.trim().toLowerCase() ?? "";
15
+ }
16
+
17
+ export function resolveCodexLocalRuntimeAttribution(
18
+ params: EmbeddedRunAttemptParams,
19
+ ): CodexLocalRuntimeAttribution {
20
+ const authProfileProvider = normalizeRuntimeId(
21
+ params.runtimePlan?.auth?.authProfileProviderForAuth,
22
+ );
23
+ if (
24
+ normalizeRuntimeId(params.runtimePlan?.observability.harnessId) === "codex" &&
25
+ authProfileProvider !== OPENAI_PROVIDER_ID &&
26
+ normalizeRuntimeId(params.model.provider) === OPENAI_PROVIDER_ID &&
27
+ normalizeRuntimeId(params.model.api) === OPENAI_RESPONSES_API
28
+ ) {
29
+ return {
30
+ provider: OPENAI_CODEX_PROVIDER_ID,
31
+ api: OPENAI_CODEX_RESPONSES_API,
32
+ };
33
+ }
34
+
35
+ return {
36
+ provider: params.provider,
37
+ api: params.model.api,
38
+ };
39
+ }
@@ -0,0 +1,139 @@
1
+ import { mkdir, mkdtemp, realpath, writeFile } from "node:fs/promises";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { describe, expect, it, vi } from "vitest";
5
+ import type { CodexAppServerStartOptions } from "./config.js";
6
+ import {
7
+ testing,
8
+ resolveManagedCodexAppServerPaths,
9
+ resolveManagedCodexAppServerStartOptions,
10
+ } from "./managed-binary.js";
11
+
12
+ function startOptions(
13
+ commandSource: CodexAppServerStartOptions["commandSource"],
14
+ ): CodexAppServerStartOptions {
15
+ return {
16
+ transport: "stdio",
17
+ command: "codex",
18
+ commandSource,
19
+ args: ["app-server", "--listen", "stdio://"],
20
+ headers: {},
21
+ };
22
+ }
23
+
24
+ function managedCommandPath(root: string, platform: NodeJS.Platform): string {
25
+ return path.join(root, "node_modules", ".bin", platform === "win32" ? "codex.cmd" : "codex");
26
+ }
27
+
28
+ describe("managed Codex app-server binary", () => {
29
+ it("leaves explicit command overrides unchanged", async () => {
30
+ const explicitOptions = startOptions("config");
31
+ const pathExists = vi.fn(async () => false);
32
+
33
+ await expect(
34
+ resolveManagedCodexAppServerStartOptions(explicitOptions, {
35
+ platform: "darwin",
36
+ pathExists,
37
+ }),
38
+ ).resolves.toBe(explicitOptions);
39
+ expect(pathExists).not.toHaveBeenCalled();
40
+ });
41
+
42
+ it("resolves the plugin-local bundled Codex binary", async () => {
43
+ const pluginRoot = path.join("/tmp", "klaw", "extensions", "codex");
44
+ const paths = resolveManagedCodexAppServerPaths({ platform: "darwin", pluginRoot });
45
+ const pathExists = vi.fn(async (filePath: string) => filePath === paths.commandPath);
46
+
47
+ await expect(
48
+ resolveManagedCodexAppServerStartOptions(startOptions("managed"), {
49
+ platform: "darwin",
50
+ pluginRoot,
51
+ pathExists,
52
+ }),
53
+ ).resolves.toEqual({
54
+ ...startOptions("managed"),
55
+ command: paths.commandPath,
56
+ commandSource: "resolved-managed",
57
+ });
58
+ expect(paths.commandPath).toBe(managedCommandPath(pluginRoot, "darwin"));
59
+ });
60
+
61
+ it("resolves Windows Codex command shims", () => {
62
+ const pluginRoot = path.win32.join("C:\\", "Klaw", "dist", "extensions", "codex");
63
+ const paths = resolveManagedCodexAppServerPaths({ platform: "win32", pluginRoot });
64
+
65
+ expect(paths.commandPath.endsWith(path.win32.join("node_modules", ".bin", "codex.cmd"))).toBe(
66
+ true,
67
+ );
68
+ });
69
+
70
+ it("uses the package root when the resolver is bundled into a dist chunk", () => {
71
+ expect(testing.resolveDefaultCodexPluginRoot("/repo/klaw/dist")).toBe("/repo/klaw");
72
+ expect(testing.resolveDefaultCodexPluginRoot("/repo/klaw/dist-runtime")).toBe("/repo/klaw");
73
+ expect(
74
+ testing.resolveDefaultCodexPluginRoot("/repo/klaw/extensions/codex/src/app-server"),
75
+ ).toBe("/repo/klaw/extensions/codex");
76
+ });
77
+
78
+ it("finds Codex in the package install root used by packaged plugins", async () => {
79
+ const installRoot = path.join("/tmp", "klaw-plugin-package", "codex");
80
+ const pluginRoot = path.join(installRoot, "dist", "extensions", "codex");
81
+ const installedCommand = managedCommandPath(installRoot, "linux");
82
+ const pathExists = vi.fn(async (filePath: string) => filePath === installedCommand);
83
+
84
+ await expect(
85
+ resolveManagedCodexAppServerStartOptions(startOptions("managed"), {
86
+ platform: "linux",
87
+ pluginRoot,
88
+ pathExists,
89
+ }),
90
+ ).resolves.toEqual({
91
+ ...startOptions("managed"),
92
+ command: installedCommand,
93
+ commandSource: "resolved-managed",
94
+ });
95
+ });
96
+
97
+ it("falls back to the resolved Codex package bin when no command shim exists", async () => {
98
+ const installRoot = await mkdtemp(path.join(os.tmpdir(), "klaw-codex-package-"));
99
+ const pluginRoot = path.join(installRoot, "dist", "extensions", "codex");
100
+ const packageRoot = path.join(installRoot, "node_modules", "@openai", "codex");
101
+ const packageBin = path.join(packageRoot, "bin", "codex.js");
102
+ await mkdir(path.dirname(packageBin), { recursive: true });
103
+ await writeFile(
104
+ path.join(packageRoot, "package.json"),
105
+ JSON.stringify({
106
+ name: "@openai/codex",
107
+ bin: {
108
+ codex: "bin/codex.js",
109
+ },
110
+ }),
111
+ );
112
+ await writeFile(packageBin, "#!/usr/bin/env node\n");
113
+ const resolvedPackageBin = await realpath(packageBin);
114
+
115
+ const pathExists = vi.fn(async (filePath: string) => filePath === resolvedPackageBin);
116
+
117
+ await expect(
118
+ resolveManagedCodexAppServerStartOptions(startOptions("managed"), {
119
+ platform: "linux",
120
+ pluginRoot,
121
+ pathExists,
122
+ }),
123
+ ).resolves.toEqual({
124
+ ...startOptions("managed"),
125
+ command: resolvedPackageBin,
126
+ commandSource: "resolved-managed",
127
+ });
128
+ });
129
+
130
+ it("fails clearly when the managed Codex binary is missing", async () => {
131
+ await expect(
132
+ resolveManagedCodexAppServerStartOptions(startOptions("managed"), {
133
+ platform: "darwin",
134
+ pluginRoot: path.join("/tmp", "klaw", "extensions", "codex"),
135
+ pathExists: vi.fn(async () => false),
136
+ }),
137
+ ).rejects.toThrow("Managed Codex app-server binary was not found");
138
+ });
139
+ });
@@ -0,0 +1,193 @@
1
+ import { constants as fsConstants, readFileSync } from "node:fs";
2
+ import { access } from "node:fs/promises";
3
+ import { createRequire } from "node:module";
4
+ import path from "node:path";
5
+ import { fileURLToPath } from "node:url";
6
+ import type { CodexAppServerStartOptions } from "./config.js";
7
+ import { MANAGED_CODEX_APP_SERVER_PACKAGE } from "./version.js";
8
+
9
+ const CODEX_APP_SERVER_MODULE_DIR = path.dirname(fileURLToPath(import.meta.url));
10
+ const CODEX_PLUGIN_ROOT = resolveDefaultCodexPluginRoot(CODEX_APP_SERVER_MODULE_DIR);
11
+
12
+ type ManagedCodexAppServerPaths = {
13
+ commandPath: string;
14
+ candidateCommandPaths: string[];
15
+ };
16
+
17
+ type ResolveManagedCodexAppServerOptions = {
18
+ platform?: NodeJS.Platform;
19
+ pluginRoot?: string;
20
+ pathExists?: (filePath: string, platform: NodeJS.Platform) => Promise<boolean>;
21
+ };
22
+
23
+ export async function resolveManagedCodexAppServerStartOptions(
24
+ startOptions: CodexAppServerStartOptions,
25
+ options: ResolveManagedCodexAppServerOptions = {},
26
+ ): Promise<CodexAppServerStartOptions> {
27
+ if (startOptions.transport !== "stdio" || startOptions.commandSource !== "managed") {
28
+ return startOptions;
29
+ }
30
+
31
+ const platform = options.platform ?? process.platform;
32
+ const paths = resolveManagedCodexAppServerPaths({
33
+ platform,
34
+ pluginRoot: options.pluginRoot,
35
+ });
36
+ const pathExists = options.pathExists ?? commandPathExists;
37
+ const commandPath = await findManagedCodexAppServerCommandPath({
38
+ candidateCommandPaths: paths.candidateCommandPaths,
39
+ pathExists,
40
+ platform,
41
+ });
42
+
43
+ return {
44
+ ...startOptions,
45
+ command: commandPath,
46
+ commandSource: "resolved-managed",
47
+ };
48
+ }
49
+
50
+ export function resolveManagedCodexAppServerPaths(params: {
51
+ platform?: NodeJS.Platform;
52
+ pluginRoot?: string;
53
+ }): ManagedCodexAppServerPaths {
54
+ const platform = params.platform ?? process.platform;
55
+ const candidateCommandPaths = resolveManagedCodexAppServerCommandCandidates(
56
+ params.pluginRoot ?? CODEX_PLUGIN_ROOT,
57
+ platform,
58
+ );
59
+ return {
60
+ commandPath: candidateCommandPaths[0] ?? "",
61
+ candidateCommandPaths,
62
+ };
63
+ }
64
+
65
+ function resolveManagedCodexAppServerCommandCandidates(
66
+ pluginRoot: string,
67
+ platform: NodeJS.Platform,
68
+ ): string[] {
69
+ const pathApi = pathForPlatform(platform);
70
+ const commandName = platform === "win32" ? "codex.cmd" : "codex";
71
+ const roots = resolveManagedCodexAppServerCandidateRoots(pluginRoot, platform);
72
+ return [
73
+ ...new Set([
74
+ ...roots.map((root) => pathApi.join(root, "node_modules", ".bin", commandName)),
75
+ ...resolveManagedCodexPackageBinCandidates(roots, platform),
76
+ ]),
77
+ ];
78
+ }
79
+
80
+ function resolveDefaultCodexPluginRoot(moduleDir: string): string {
81
+ const moduleBaseName = path.basename(moduleDir);
82
+ if (moduleBaseName === "dist" || moduleBaseName === "dist-runtime") {
83
+ return path.dirname(moduleDir);
84
+ }
85
+ return path.resolve(moduleDir, "..", "..");
86
+ }
87
+
88
+ function resolveManagedCodexAppServerCandidateRoots(
89
+ pluginRoot: string,
90
+ platform: NodeJS.Platform,
91
+ ): string[] {
92
+ const pathApi = pathForPlatform(platform);
93
+ return [
94
+ pluginRoot,
95
+ pathApi.dirname(pluginRoot),
96
+ pathApi.dirname(pathApi.dirname(pluginRoot)),
97
+ isDistExtensionRoot(pluginRoot, platform)
98
+ ? pathApi.dirname(pathApi.dirname(pathApi.dirname(pluginRoot)))
99
+ : null,
100
+ ].filter((root): root is string => Boolean(root));
101
+ }
102
+
103
+ function resolveManagedCodexPackageBinCandidates(
104
+ roots: readonly string[],
105
+ platform: NodeJS.Platform,
106
+ ): string[] {
107
+ if (platform === "win32") {
108
+ return [];
109
+ }
110
+
111
+ const candidates: string[] = [];
112
+ for (const root of roots) {
113
+ const candidate = resolveManagedCodexPackageBinCandidate(root);
114
+ if (candidate) {
115
+ candidates.push(candidate);
116
+ }
117
+ }
118
+ return candidates;
119
+ }
120
+
121
+ function resolveManagedCodexPackageBinCandidate(root: string): string | null {
122
+ try {
123
+ const requireFromRoot = createRequire(path.join(root, "package.json"));
124
+ const packageJsonPath = requireFromRoot.resolve(
125
+ `${MANAGED_CODEX_APP_SERVER_PACKAGE}/package.json`,
126
+ );
127
+ const packageRoot = path.dirname(packageJsonPath);
128
+ const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8")) as {
129
+ bin?: unknown;
130
+ };
131
+ const binPath =
132
+ typeof packageJson.bin === "string"
133
+ ? packageJson.bin
134
+ : isRecord(packageJson.bin) && typeof packageJson.bin.codex === "string"
135
+ ? packageJson.bin.codex
136
+ : null;
137
+ return binPath ? path.resolve(packageRoot, binPath) : null;
138
+ } catch {
139
+ return null;
140
+ }
141
+ }
142
+
143
+ function isRecord(value: unknown): value is Record<string, unknown> {
144
+ return typeof value === "object" && value !== null;
145
+ }
146
+
147
+ export const testing = {
148
+ resolveDefaultCodexPluginRoot,
149
+ };
150
+
151
+ function isDistExtensionRoot(pluginRoot: string, platform: NodeJS.Platform): boolean {
152
+ const pathApi = pathForPlatform(platform);
153
+ const extensionsDir = pathApi.dirname(pluginRoot);
154
+ const distDir = pathApi.dirname(extensionsDir);
155
+ return (
156
+ pathApi.basename(extensionsDir) === "extensions" &&
157
+ (pathApi.basename(distDir) === "dist" || pathApi.basename(distDir) === "dist-runtime")
158
+ );
159
+ }
160
+
161
+ function pathForPlatform(platform: NodeJS.Platform): typeof path {
162
+ return platform === "win32" ? path.win32 : path.posix;
163
+ }
164
+
165
+ async function findManagedCodexAppServerCommandPath(params: {
166
+ candidateCommandPaths: readonly string[];
167
+ pathExists: (filePath: string, platform: NodeJS.Platform) => Promise<boolean>;
168
+ platform: NodeJS.Platform;
169
+ }): Promise<string> {
170
+ for (const commandPath of params.candidateCommandPaths) {
171
+ if (await params.pathExists(commandPath, params.platform)) {
172
+ return commandPath;
173
+ }
174
+ }
175
+
176
+ throw new Error(
177
+ [
178
+ `Managed Codex app-server binary was not found for ${MANAGED_CODEX_APP_SERVER_PACKAGE}.`,
179
+ "Reinstall or update Klaw, or run pnpm install in a source checkout.",
180
+ "Set plugins.entries.codex.config.appServer.command or KLAW_CODEX_APP_SERVER_BIN to use a custom Codex binary.",
181
+ ].join(" "),
182
+ );
183
+ }
184
+
185
+ async function commandPathExists(filePath: string, platform: NodeJS.Platform): Promise<boolean> {
186
+ try {
187
+ await access(filePath, platform === "win32" ? fsConstants.F_OK : fsConstants.X_OK);
188
+ return true;
189
+ } catch {
190
+ return false;
191
+ }
192
+ }
193
+ export { testing as __testing };
@@ -0,0 +1,246 @@
1
+ import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
2
+ import { CodexAppServerClient } from "./client.js";
3
+ import { createClientHarness } from "./test-support.js";
4
+
5
+ const mocks = vi.hoisted(() => {
6
+ const authBridge = {
7
+ applyAuthProfile: vi.fn(async () => undefined),
8
+ authProfileId: vi.fn((params?: { authProfileId?: string }) => params?.authProfileId),
9
+ startOptions: vi.fn(async ({ startOptions }) => startOptions),
10
+ };
11
+ const managedBinary = {
12
+ startOptions: vi.fn(async (startOptions) => startOptions),
13
+ };
14
+ const providerAuth = {
15
+ agentDir: vi.fn(() => "/tmp/klaw-agent"),
16
+ };
17
+ return { authBridge, managedBinary, providerAuth };
18
+ });
19
+
20
+ vi.mock("./auth-bridge.js", () => ({
21
+ applyCodexAppServerAuthProfile: mocks.authBridge.applyAuthProfile,
22
+ bridgeCodexAppServerStartOptions: mocks.authBridge.startOptions,
23
+ resolveCodexAppServerAuthProfileIdForAgent: mocks.authBridge.authProfileId,
24
+ }));
25
+
26
+ vi.mock("./managed-binary.js", () => ({
27
+ resolveManagedCodexAppServerStartOptions: mocks.managedBinary.startOptions,
28
+ }));
29
+
30
+ vi.mock("klaw/plugin-sdk/agent-runtime", () => ({
31
+ resolveDefaultAgentDir: mocks.providerAuth.agentDir,
32
+ }));
33
+
34
+ let listCodexAppServerModels: typeof import("./models.js").listCodexAppServerModels;
35
+ let listAllCodexAppServerModels: typeof import("./models.js").listAllCodexAppServerModels;
36
+ let resetSharedCodexAppServerClientForTests: typeof import("./shared-client.js").resetSharedCodexAppServerClientForTests;
37
+
38
+ describe("listCodexAppServerModels", () => {
39
+ beforeAll(async () => {
40
+ ({ listCodexAppServerModels } = await import("./models.js"));
41
+ ({ listAllCodexAppServerModels } = await import("./models.js"));
42
+ ({ resetSharedCodexAppServerClientForTests } = await import("./shared-client.js"));
43
+ });
44
+
45
+ afterEach(() => {
46
+ resetSharedCodexAppServerClientForTests();
47
+ vi.restoreAllMocks();
48
+ mocks.authBridge.applyAuthProfile.mockClear();
49
+ mocks.authBridge.authProfileId.mockClear();
50
+ mocks.authBridge.authProfileId.mockImplementation(
51
+ (params?: { authProfileId?: string }) => params?.authProfileId,
52
+ );
53
+ mocks.authBridge.startOptions.mockClear();
54
+ mocks.managedBinary.startOptions.mockClear();
55
+ mocks.managedBinary.startOptions.mockImplementation(async (startOptions) => startOptions);
56
+ mocks.providerAuth.agentDir.mockClear();
57
+ });
58
+
59
+ it("lists app-server models through the typed helper", async () => {
60
+ const harness = createClientHarness();
61
+ const startSpy = vi.spyOn(CodexAppServerClient, "start").mockReturnValue(harness.client);
62
+
63
+ const listPromise = listCodexAppServerModels({ limit: 12, timeoutMs: 1000 });
64
+ await vi.waitFor(() => expect(harness.writes.length).toBeGreaterThanOrEqual(1));
65
+ const initialize = JSON.parse(harness.writes[0] ?? "{}") as { id?: number };
66
+ harness.send({
67
+ id: initialize.id,
68
+ result: { userAgent: "klaw/0.125.0 (macOS; test)" },
69
+ });
70
+ await vi.waitFor(() => expect(harness.writes.length).toBeGreaterThanOrEqual(3));
71
+ const list = JSON.parse(harness.writes[2] ?? "{}") as { id?: number; method?: string };
72
+ expect(list.method).toBe("model/list");
73
+
74
+ harness.send({
75
+ id: list.id,
76
+ result: {
77
+ data: [
78
+ {
79
+ id: "gpt-5.4",
80
+ model: "gpt-5.4",
81
+ upgrade: null,
82
+ upgradeInfo: null,
83
+ availabilityNux: null,
84
+ displayName: "gpt-5.4",
85
+ description: "GPT-5.4",
86
+ hidden: false,
87
+ inputModalities: ["text", "image"],
88
+ supportedReasoningEfforts: [
89
+ { reasoningEffort: "low", description: "fast" },
90
+ { reasoningEffort: "xhigh", description: "deep" },
91
+ ],
92
+ defaultReasoningEffort: "medium",
93
+ supportsPersonality: false,
94
+ additionalSpeedTiers: [],
95
+ isDefault: true,
96
+ },
97
+ ],
98
+ nextCursor: null,
99
+ },
100
+ });
101
+
102
+ await expect(listPromise).resolves.toEqual({
103
+ models: [
104
+ {
105
+ id: "gpt-5.4",
106
+ model: "gpt-5.4",
107
+ displayName: "gpt-5.4",
108
+ description: "GPT-5.4",
109
+ hidden: false,
110
+ inputModalities: ["text", "image"],
111
+ supportedReasoningEfforts: ["low", "xhigh"],
112
+ defaultReasoningEffort: "medium",
113
+ isDefault: true,
114
+ },
115
+ ],
116
+ });
117
+ harness.client.close();
118
+ startSpy.mockRestore();
119
+ });
120
+
121
+ it("lists all app-server model pages through one client", async () => {
122
+ const harness = createClientHarness();
123
+ const startSpy = vi.spyOn(CodexAppServerClient, "start").mockReturnValue(harness.client);
124
+
125
+ const listPromise = listAllCodexAppServerModels({ limit: 1, timeoutMs: 1000 });
126
+ await vi.waitFor(() => expect(harness.writes.length).toBeGreaterThanOrEqual(1));
127
+ const initialize = JSON.parse(harness.writes[0] ?? "{}") as { id?: number };
128
+ harness.send({
129
+ id: initialize.id,
130
+ result: { userAgent: "klaw/0.125.0 (macOS; test)" },
131
+ });
132
+ await vi.waitFor(() => expect(harness.writes.length).toBeGreaterThanOrEqual(3));
133
+ const firstList = JSON.parse(harness.writes[2] ?? "{}") as {
134
+ id?: number;
135
+ params?: { cursor?: string | null };
136
+ };
137
+ expect(firstList.params?.cursor).toBeNull();
138
+
139
+ harness.send({
140
+ id: firstList.id,
141
+ result: {
142
+ data: [
143
+ {
144
+ id: "gpt-5.4",
145
+ model: "gpt-5.4",
146
+ upgrade: null,
147
+ upgradeInfo: null,
148
+ availabilityNux: null,
149
+ displayName: "gpt-5.4",
150
+ description: "GPT-5.4",
151
+ hidden: false,
152
+ inputModalities: ["text"],
153
+ supportedReasoningEfforts: [],
154
+ defaultReasoningEffort: "medium",
155
+ supportsPersonality: false,
156
+ additionalSpeedTiers: [],
157
+ isDefault: false,
158
+ },
159
+ ],
160
+ nextCursor: "page-2",
161
+ },
162
+ });
163
+ await vi.waitFor(() => expect(harness.writes.length).toBeGreaterThanOrEqual(4));
164
+ const secondList = JSON.parse(harness.writes[3] ?? "{}") as {
165
+ id?: number;
166
+ params?: { cursor?: string | null };
167
+ };
168
+ expect(secondList.params?.cursor).toBe("page-2");
169
+
170
+ harness.send({
171
+ id: secondList.id,
172
+ result: {
173
+ data: [
174
+ {
175
+ id: "gpt-5.2",
176
+ model: "gpt-5.2",
177
+ upgrade: null,
178
+ upgradeInfo: null,
179
+ availabilityNux: null,
180
+ displayName: "gpt-5.2",
181
+ description: "GPT-5.2",
182
+ hidden: false,
183
+ inputModalities: ["text", "image"],
184
+ supportedReasoningEfforts: [],
185
+ defaultReasoningEffort: "medium",
186
+ supportsPersonality: false,
187
+ additionalSpeedTiers: [],
188
+ isDefault: false,
189
+ },
190
+ ],
191
+ nextCursor: null,
192
+ },
193
+ });
194
+
195
+ const list = await listPromise;
196
+ expect(list.models.map((model) => model.id)).toEqual(["gpt-5.4", "gpt-5.2"]);
197
+ harness.client.close();
198
+ startSpy.mockRestore();
199
+ });
200
+
201
+ it("marks all-model listing truncated after the page cap", async () => {
202
+ const harness = createClientHarness();
203
+ const startSpy = vi.spyOn(CodexAppServerClient, "start").mockReturnValue(harness.client);
204
+
205
+ const listPromise = listAllCodexAppServerModels({ limit: 1, timeoutMs: 1000, maxPages: 1 });
206
+ await vi.waitFor(() => expect(harness.writes.length).toBeGreaterThanOrEqual(1));
207
+ const initialize = JSON.parse(harness.writes[0] ?? "{}") as { id?: number };
208
+ harness.send({
209
+ id: initialize.id,
210
+ result: { userAgent: "klaw/0.125.0 (macOS; test)" },
211
+ });
212
+ await vi.waitFor(() => expect(harness.writes.length).toBeGreaterThanOrEqual(3));
213
+ const firstList = JSON.parse(harness.writes[2] ?? "{}") as { id?: number };
214
+ harness.send({
215
+ id: firstList.id,
216
+ result: {
217
+ data: [
218
+ {
219
+ id: "gpt-5.4",
220
+ model: "gpt-5.4",
221
+ upgrade: null,
222
+ upgradeInfo: null,
223
+ availabilityNux: null,
224
+ displayName: "gpt-5.4",
225
+ description: "GPT-5.4",
226
+ hidden: false,
227
+ inputModalities: ["text"],
228
+ supportedReasoningEfforts: [],
229
+ defaultReasoningEffort: "medium",
230
+ supportsPersonality: false,
231
+ additionalSpeedTiers: [],
232
+ isDefault: false,
233
+ },
234
+ ],
235
+ nextCursor: "page-2",
236
+ },
237
+ });
238
+
239
+ const list = await listPromise;
240
+ expect(list.models.map((model) => model.id)).toEqual(["gpt-5.4"]);
241
+ expect(list.nextCursor).toBe("page-2");
242
+ expect(list.truncated).toBe(true);
243
+ harness.client.close();
244
+ startSpy.mockRestore();
245
+ });
246
+ });