@gakr-gakr/codex 0.1.0 → 0.1.1

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 (115) hide show
  1. package/dist/client-DNN2uyJW.js +642 -0
  2. package/dist/client-factory-Bu9OClHJ.js +9 -0
  3. package/dist/command-formatters-BpPOTePl.js +520 -0
  4. package/dist/command-handlers-BBs7Vws9.js +1533 -0
  5. package/dist/compact-CDboBy7o.js +329 -0
  6. package/dist/computer-use-DCZB46Sw.js +367 -0
  7. package/dist/config-CLMSw0p2.js +510 -0
  8. package/dist/doctor-contract-api.js +53 -0
  9. package/dist/harness.js +51 -0
  10. package/dist/index.js +1171 -0
  11. package/dist/media-understanding-provider.js +335 -0
  12. package/dist/models-jLA2SIvd.js +110 -0
  13. package/dist/node-cli-sessions-BLRDs_US.js +1216 -0
  14. package/dist/plugin-activation-CEy_oYpx.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-9LxQdE0K.js +24 -0
  22. package/dist/request-DbSPeTcV.js +89 -0
  23. package/dist/rolldown-runtime-DUslC3ob.js +14 -0
  24. package/dist/run-attempt-BoEwzQCv.js +5463 -0
  25. package/dist/session-binding-e2GFp9VH.js +222 -0
  26. package/dist/shared-client-D7Vy0glq.js +631 -0
  27. package/dist/side-question-BDLuEzFP.js +668 -0
  28. package/dist/test-api.js +49 -0
  29. package/dist/thread-lifecycle-Clo0EHMk.js +1565 -0
  30. package/dist/vision-tools-Cofrv35p.js +1379 -0
  31. package/package.json +16 -1
  32. package/doctor-contract-api.ts +0 -68
  33. package/harness.ts +0 -72
  34. package/index.ts +0 -124
  35. package/media-understanding-provider.ts +0 -521
  36. package/prompt-overlay.ts +0 -21
  37. package/provider-catalog.ts +0 -83
  38. package/provider-discovery.ts +0 -45
  39. package/provider.ts +0 -243
  40. package/src/app-server/app-inventory-cache.ts +0 -324
  41. package/src/app-server/approval-bridge.ts +0 -1211
  42. package/src/app-server/auth-bridge.ts +0 -614
  43. package/src/app-server/capabilities.ts +0 -27
  44. package/src/app-server/client-factory.ts +0 -24
  45. package/src/app-server/client.ts +0 -715
  46. package/src/app-server/compact.ts +0 -512
  47. package/src/app-server/computer-use.ts +0 -683
  48. package/src/app-server/config.ts +0 -1038
  49. package/src/app-server/context-engine-projection.ts +0 -403
  50. package/src/app-server/dynamic-tool-diagnostics.ts +0 -73
  51. package/src/app-server/dynamic-tool-profile.ts +0 -70
  52. package/src/app-server/dynamic-tools.ts +0 -623
  53. package/src/app-server/elicitation-bridge.ts +0 -783
  54. package/src/app-server/event-projector.ts +0 -2065
  55. package/src/app-server/image-payload-sanitizer.ts +0 -167
  56. package/src/app-server/local-runtime-attribution.ts +0 -39
  57. package/src/app-server/managed-binary.ts +0 -193
  58. package/src/app-server/models.ts +0 -172
  59. package/src/app-server/native-hook-relay.ts +0 -150
  60. package/src/app-server/native-subagent-task-mirror.ts +0 -497
  61. package/src/app-server/plugin-activation.ts +0 -283
  62. package/src/app-server/plugin-app-cache-key.ts +0 -74
  63. package/src/app-server/plugin-approval-roundtrip.ts +0 -122
  64. package/src/app-server/plugin-inventory.ts +0 -357
  65. package/src/app-server/plugin-thread-config.ts +0 -455
  66. package/src/app-server/protocol-generated/json/DynamicToolCallParams.json +0 -33
  67. package/src/app-server/protocol-generated/json/v2/ErrorNotification.json +0 -199
  68. package/src/app-server/protocol-generated/json/v2/GetAccountResponse.json +0 -102
  69. package/src/app-server/protocol-generated/json/v2/ModelListResponse.json +0 -227
  70. package/src/app-server/protocol-generated/json/v2/ThreadResumeResponse.json +0 -2630
  71. package/src/app-server/protocol-generated/json/v2/ThreadStartResponse.json +0 -2630
  72. package/src/app-server/protocol-generated/json/v2/TurnCompletedNotification.json +0 -1659
  73. package/src/app-server/protocol-generated/json/v2/TurnStartResponse.json +0 -1655
  74. package/src/app-server/protocol-validators.ts +0 -203
  75. package/src/app-server/protocol.ts +0 -520
  76. package/src/app-server/rate-limit-cache.ts +0 -48
  77. package/src/app-server/rate-limits.ts +0 -583
  78. package/src/app-server/request.ts +0 -73
  79. package/src/app-server/run-attempt.ts +0 -4862
  80. package/src/app-server/session-binding.ts +0 -398
  81. package/src/app-server/session-history.ts +0 -44
  82. package/src/app-server/shared-client.ts +0 -289
  83. package/src/app-server/side-question.ts +0 -1009
  84. package/src/app-server/test-support.ts +0 -48
  85. package/src/app-server/thread-lifecycle.ts +0 -959
  86. package/src/app-server/timeout.ts +0 -9
  87. package/src/app-server/tool-progress-normalization.ts +0 -77
  88. package/src/app-server/trajectory.ts +0 -368
  89. package/src/app-server/transcript-mirror.ts +0 -208
  90. package/src/app-server/transport-stdio.ts +0 -107
  91. package/src/app-server/transport-websocket.ts +0 -90
  92. package/src/app-server/transport.ts +0 -117
  93. package/src/app-server/user-input-bridge.ts +0 -316
  94. package/src/app-server/version.ts +0 -4
  95. package/src/app-server/vision-tools.ts +0 -12
  96. package/src/command-account.ts +0 -544
  97. package/src/command-formatters.ts +0 -426
  98. package/src/command-handlers.ts +0 -2021
  99. package/src/command-plugins-management.ts +0 -137
  100. package/src/command-rpc.ts +0 -142
  101. package/src/commands.ts +0 -65
  102. package/src/conversation-binding-data.ts +0 -124
  103. package/src/conversation-binding.ts +0 -561
  104. package/src/conversation-control.ts +0 -303
  105. package/src/conversation-turn-collector.ts +0 -186
  106. package/src/conversation-turn-input.ts +0 -106
  107. package/src/migration/apply.ts +0 -501
  108. package/src/migration/helpers.ts +0 -55
  109. package/src/migration/plan.ts +0 -461
  110. package/src/migration/provider.ts +0 -41
  111. package/src/migration/source.ts +0 -643
  112. package/src/migration/targets.ts +0 -25
  113. package/src/node-cli-sessions.ts +0 -711
  114. package/test-api.ts +0 -95
  115. package/tsconfig.json +0 -16
@@ -1,167 +0,0 @@
1
- const DATA_URL_PREFIX = "data:";
2
- const IMAGE_OMITTED_TEXT = "omitted image payload: invalid inline image data";
3
-
4
- function startsWithDataUrl(value: string): boolean {
5
- return value.slice(0, DATA_URL_PREFIX.length).toLowerCase() === DATA_URL_PREFIX;
6
- }
7
-
8
- function canonicalizeBase64(base64: string): string | undefined {
9
- let cleaned = "";
10
- let padding = 0;
11
- let sawPadding = false;
12
- for (let i = 0; i < base64.length; i += 1) {
13
- const code = base64.charCodeAt(i);
14
- if (code <= 0x20) {
15
- continue;
16
- }
17
- if (code === 0x3d) {
18
- padding += 1;
19
- if (padding > 2) {
20
- return undefined;
21
- }
22
- sawPadding = true;
23
- cleaned += "=";
24
- continue;
25
- }
26
- const isBase64DataChar =
27
- (code >= 0x41 && code <= 0x5a) ||
28
- (code >= 0x61 && code <= 0x7a) ||
29
- (code >= 0x30 && code <= 0x39) ||
30
- code === 0x2b ||
31
- code === 0x2f;
32
- if (sawPadding || !isBase64DataChar) {
33
- return undefined;
34
- }
35
- cleaned += base64[i];
36
- }
37
- if (!cleaned || cleaned.length % 4 !== 0) {
38
- return undefined;
39
- }
40
- return cleaned;
41
- }
42
-
43
- function sniffImageMime(buffer: Buffer): string | undefined {
44
- if (
45
- buffer.length >= 8 &&
46
- buffer[0] === 0x89 &&
47
- buffer[1] === 0x50 &&
48
- buffer[2] === 0x4e &&
49
- buffer[3] === 0x47 &&
50
- buffer[4] === 0x0d &&
51
- buffer[5] === 0x0a &&
52
- buffer[6] === 0x1a &&
53
- buffer[7] === 0x0a
54
- ) {
55
- return "image/png";
56
- }
57
- if (buffer.length >= 3 && buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) {
58
- return "image/jpeg";
59
- }
60
- if (
61
- buffer.length >= 12 &&
62
- buffer.subarray(0, 4).toString("ascii") === "RIFF" &&
63
- buffer.subarray(8, 12).toString("ascii") === "WEBP"
64
- ) {
65
- return "image/webp";
66
- }
67
- if (
68
- buffer.length >= 6 &&
69
- (buffer.subarray(0, 6).toString("ascii") === "GIF87a" ||
70
- buffer.subarray(0, 6).toString("ascii") === "GIF89a")
71
- ) {
72
- return "image/gif";
73
- }
74
- return undefined;
75
- }
76
-
77
- export function sanitizeInlineImageDataUrl(imageUrl: string): string | undefined {
78
- if (!startsWithDataUrl(imageUrl)) {
79
- return imageUrl;
80
- }
81
- const commaIndex = imageUrl.indexOf(",");
82
- if (commaIndex < 0) {
83
- return undefined;
84
- }
85
-
86
- const metadata = imageUrl.slice(DATA_URL_PREFIX.length, commaIndex);
87
- const payload = imageUrl.slice(commaIndex + 1);
88
- const metadataParts = metadata.split(";").map((part) => part.trim());
89
- const declaredMimeType = metadataParts[0]?.toLowerCase();
90
- if (!declaredMimeType?.startsWith("image/")) {
91
- return undefined;
92
- }
93
- if (!metadataParts.slice(1).some((part) => part.toLowerCase() === "base64")) {
94
- return undefined;
95
- }
96
-
97
- const canonicalPayload = canonicalizeBase64(payload);
98
- if (!canonicalPayload) {
99
- return undefined;
100
- }
101
- const sniffedMimeType = sniffImageMime(Buffer.from(canonicalPayload, "base64"));
102
- if (!sniffedMimeType) {
103
- return undefined;
104
- }
105
- return `data:${sniffedMimeType};base64,${canonicalPayload}`;
106
- }
107
-
108
- export function invalidInlineImageText(label: string): string {
109
- return `[${label}] ${IMAGE_OMITTED_TEXT}`;
110
- }
111
-
112
- function isRecord(value: unknown): value is Record<string, unknown> {
113
- return Boolean(value && typeof value === "object" && !Array.isArray(value));
114
- }
115
-
116
- function sanitizeImageContentRecord(
117
- record: Record<string, unknown>,
118
- label: string,
119
- ): Record<string, unknown> | undefined {
120
- if (record.type === "image" && typeof record.data === "string") {
121
- const mimeType = typeof record.mimeType === "string" ? record.mimeType : "image/png";
122
- const imageUrl = sanitizeInlineImageDataUrl(`data:${mimeType};base64,${record.data}`);
123
- if (!imageUrl) {
124
- return { type: "text", text: invalidInlineImageText(label) };
125
- }
126
- const commaIndex = imageUrl.indexOf(",");
127
- const metadata = imageUrl.slice(DATA_URL_PREFIX.length, commaIndex);
128
- const mime = metadata.split(";")[0] ?? mimeType;
129
- return { ...record, mimeType: mime, data: imageUrl.slice(commaIndex + 1) };
130
- }
131
-
132
- if (record.type === "inputImage" && typeof record.imageUrl === "string") {
133
- const imageUrl = sanitizeInlineImageDataUrl(record.imageUrl);
134
- return imageUrl
135
- ? { ...record, imageUrl }
136
- : { type: "inputText", text: invalidInlineImageText(label) };
137
- }
138
-
139
- if (record.type === "input_image" && typeof record.image_url === "string") {
140
- const imageUrl = sanitizeInlineImageDataUrl(record.image_url);
141
- return imageUrl
142
- ? { ...record, image_url: imageUrl }
143
- : { type: "input_text", text: invalidInlineImageText(label) };
144
- }
145
-
146
- return undefined;
147
- }
148
-
149
- export function sanitizeCodexHistoryImagePayloads<T>(value: T, label: string): T {
150
- if (Array.isArray(value)) {
151
- return value.map((entry) => sanitizeCodexHistoryImagePayloads(entry, label)) as T;
152
- }
153
- if (!isRecord(value)) {
154
- return value;
155
- }
156
-
157
- const imageRecord = sanitizeImageContentRecord(value, label);
158
- if (imageRecord) {
159
- return imageRecord as T;
160
- }
161
-
162
- const next: Record<string, unknown> = {};
163
- for (const [key, child] of Object.entries(value)) {
164
- next[key] = sanitizeCodexHistoryImagePayloads(child, label);
165
- }
166
- return next as T;
167
- }
@@ -1,39 +0,0 @@
1
- import type { EmbeddedRunAttemptParams } from "autobot/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
- }
@@ -1,193 +0,0 @@
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 AutoBot, or run pnpm install in a source checkout.",
180
- "Set plugins.entries.codex.config.appServer.command or AUTOBOT_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 };
@@ -1,172 +0,0 @@
1
- import type { resolveCodexAppServerAuthProfileIdForAgent } from "./auth-bridge.js";
2
- import type { CodexAppServerClient } from "./client.js";
3
- import type { CodexAppServerStartOptions } from "./config.js";
4
- import { readCodexModelListResponse } from "./protocol-validators.js";
5
- import type { CodexModel, CodexReasoningEffortOption } from "./protocol.js";
6
-
7
- export type CodexAppServerModel = {
8
- id: string;
9
- model: string;
10
- displayName?: string;
11
- description?: string;
12
- hidden?: boolean;
13
- isDefault?: boolean;
14
- inputModalities: string[];
15
- supportedReasoningEfforts: string[];
16
- defaultReasoningEffort?: string;
17
- };
18
-
19
- export type CodexAppServerModelListResult = {
20
- models: CodexAppServerModel[];
21
- nextCursor?: string;
22
- truncated?: boolean;
23
- };
24
-
25
- export type CodexAppServerListModelsOptions = {
26
- limit?: number;
27
- cursor?: string;
28
- includeHidden?: boolean;
29
- timeoutMs?: number;
30
- startOptions?: CodexAppServerStartOptions;
31
- authProfileId?: string;
32
- agentDir?: string;
33
- config?: Parameters<typeof resolveCodexAppServerAuthProfileIdForAgent>[0]["config"];
34
- sharedClient?: boolean;
35
- };
36
-
37
- export async function listCodexAppServerModels(
38
- options: CodexAppServerListModelsOptions = {},
39
- ): Promise<CodexAppServerModelListResult> {
40
- return await withCodexAppServerModelClient(options, async ({ client, timeoutMs }) =>
41
- requestModelListPage(client, { ...options, timeoutMs }),
42
- );
43
- }
44
-
45
- export async function listAllCodexAppServerModels(
46
- options: CodexAppServerListModelsOptions & { maxPages?: number } = {},
47
- ): Promise<CodexAppServerModelListResult> {
48
- const maxPages = normalizeMaxPages(options.maxPages);
49
- return await withCodexAppServerModelClient(options, async ({ client, timeoutMs }) => {
50
- const models: CodexAppServerModel[] = [];
51
- let cursor = options.cursor;
52
- let nextCursor: string | undefined;
53
- for (let page = 0; page < maxPages; page += 1) {
54
- const result = await requestModelListPage(client, {
55
- ...options,
56
- timeoutMs,
57
- cursor,
58
- });
59
- models.push(...result.models);
60
- nextCursor = result.nextCursor;
61
- if (!nextCursor) {
62
- return { models };
63
- }
64
- cursor = nextCursor;
65
- }
66
- return { models, nextCursor, truncated: true };
67
- });
68
- }
69
-
70
- async function withCodexAppServerModelClient<T>(
71
- options: CodexAppServerListModelsOptions,
72
- run: (params: { client: CodexAppServerClient; timeoutMs: number }) => Promise<T>,
73
- ): Promise<T> {
74
- const timeoutMs = options.timeoutMs ?? 2500;
75
- const useSharedClient = options.sharedClient !== false;
76
- const { createIsolatedCodexAppServerClient, getSharedCodexAppServerClient } =
77
- await import("./shared-client.js");
78
- const client = useSharedClient
79
- ? await getSharedCodexAppServerClient({
80
- startOptions: options.startOptions,
81
- timeoutMs,
82
- authProfileId: options.authProfileId,
83
- agentDir: options.agentDir,
84
- config: options.config,
85
- })
86
- : await createIsolatedCodexAppServerClient({
87
- startOptions: options.startOptions,
88
- timeoutMs,
89
- authProfileId: options.authProfileId,
90
- agentDir: options.agentDir,
91
- config: options.config,
92
- });
93
- try {
94
- return await run({ client, timeoutMs });
95
- } finally {
96
- if (!useSharedClient) {
97
- client.close();
98
- }
99
- }
100
- }
101
-
102
- async function requestModelListPage(
103
- client: CodexAppServerClient,
104
- options: CodexAppServerListModelsOptions & { timeoutMs: number },
105
- ): Promise<CodexAppServerModelListResult> {
106
- const response = await client.request(
107
- "model/list",
108
- {
109
- limit: options.limit ?? null,
110
- cursor: options.cursor ?? null,
111
- includeHidden: options.includeHidden ?? null,
112
- },
113
- { timeoutMs: options.timeoutMs },
114
- );
115
- return readModelListResult(response);
116
- }
117
-
118
- export function readModelListResult(value: unknown): CodexAppServerModelListResult {
119
- const response = readCodexModelListResponse(value);
120
- if (!response) {
121
- return { models: [] };
122
- }
123
- const models = response.data
124
- .map((entry) => readCodexModel(entry))
125
- .filter((entry): entry is CodexAppServerModel => entry !== undefined);
126
- const nextCursor = response.nextCursor ?? undefined;
127
- return { models, ...(nextCursor ? { nextCursor } : {}) };
128
- }
129
-
130
- function readCodexModel(value: CodexModel): CodexAppServerModel | undefined {
131
- const id = readNonEmptyString(value.id);
132
- const model = readNonEmptyString(value.model) ?? id;
133
- if (!id || !model) {
134
- return undefined;
135
- }
136
- return {
137
- id,
138
- model,
139
- ...(readNonEmptyString(value.displayName)
140
- ? { displayName: readNonEmptyString(value.displayName) }
141
- : {}),
142
- ...(readNonEmptyString(value.description)
143
- ? { description: readNonEmptyString(value.description) }
144
- : {}),
145
- hidden: value.hidden,
146
- isDefault: value.isDefault,
147
- inputModalities: value.inputModalities,
148
- supportedReasoningEfforts: readReasoningEfforts(value.supportedReasoningEfforts),
149
- ...(readNonEmptyString(value.defaultReasoningEffort)
150
- ? { defaultReasoningEffort: readNonEmptyString(value.defaultReasoningEffort) }
151
- : {}),
152
- };
153
- }
154
-
155
- function readReasoningEfforts(value: CodexReasoningEffortOption[]): string[] {
156
- const efforts = value
157
- .map((entry) => readNonEmptyString(entry.reasoningEffort))
158
- .filter((entry): entry is string => entry !== undefined);
159
- return [...new Set(efforts)];
160
- }
161
-
162
- function readNonEmptyString(value: unknown): string | undefined {
163
- if (typeof value !== "string") {
164
- return undefined;
165
- }
166
- const trimmed = value.trim();
167
- return trimmed || undefined;
168
- }
169
-
170
- function normalizeMaxPages(value: unknown): number {
171
- return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.floor(value) : 20;
172
- }