@gakr-gakr/codex 0.1.0

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 (86) hide show
  1. package/autobot.plugin.json +374 -0
  2. package/doctor-contract-api.ts +68 -0
  3. package/harness.ts +72 -0
  4. package/index.ts +124 -0
  5. package/media-understanding-provider.ts +521 -0
  6. package/package.json +40 -0
  7. package/prompt-overlay.ts +21 -0
  8. package/provider-catalog.ts +83 -0
  9. package/provider-discovery.ts +45 -0
  10. package/provider.ts +243 -0
  11. package/src/app-server/app-inventory-cache.ts +324 -0
  12. package/src/app-server/approval-bridge.ts +1211 -0
  13. package/src/app-server/auth-bridge.ts +614 -0
  14. package/src/app-server/capabilities.ts +27 -0
  15. package/src/app-server/client-factory.ts +24 -0
  16. package/src/app-server/client.ts +715 -0
  17. package/src/app-server/compact.ts +512 -0
  18. package/src/app-server/computer-use.ts +683 -0
  19. package/src/app-server/config.ts +1038 -0
  20. package/src/app-server/context-engine-projection.ts +403 -0
  21. package/src/app-server/dynamic-tool-diagnostics.ts +73 -0
  22. package/src/app-server/dynamic-tool-profile.ts +70 -0
  23. package/src/app-server/dynamic-tools.ts +623 -0
  24. package/src/app-server/elicitation-bridge.ts +783 -0
  25. package/src/app-server/event-projector.ts +2065 -0
  26. package/src/app-server/image-payload-sanitizer.ts +167 -0
  27. package/src/app-server/local-runtime-attribution.ts +39 -0
  28. package/src/app-server/managed-binary.ts +193 -0
  29. package/src/app-server/models.ts +172 -0
  30. package/src/app-server/native-hook-relay.ts +150 -0
  31. package/src/app-server/native-subagent-task-mirror.ts +497 -0
  32. package/src/app-server/plugin-activation.ts +283 -0
  33. package/src/app-server/plugin-app-cache-key.ts +74 -0
  34. package/src/app-server/plugin-approval-roundtrip.ts +122 -0
  35. package/src/app-server/plugin-inventory.ts +357 -0
  36. package/src/app-server/plugin-thread-config.ts +455 -0
  37. package/src/app-server/protocol-generated/json/DynamicToolCallParams.json +33 -0
  38. package/src/app-server/protocol-generated/json/v2/ErrorNotification.json +199 -0
  39. package/src/app-server/protocol-generated/json/v2/GetAccountResponse.json +102 -0
  40. package/src/app-server/protocol-generated/json/v2/ModelListResponse.json +227 -0
  41. package/src/app-server/protocol-generated/json/v2/ThreadResumeResponse.json +2630 -0
  42. package/src/app-server/protocol-generated/json/v2/ThreadStartResponse.json +2630 -0
  43. package/src/app-server/protocol-generated/json/v2/TurnCompletedNotification.json +1659 -0
  44. package/src/app-server/protocol-generated/json/v2/TurnStartResponse.json +1655 -0
  45. package/src/app-server/protocol-validators.ts +203 -0
  46. package/src/app-server/protocol.ts +520 -0
  47. package/src/app-server/rate-limit-cache.ts +48 -0
  48. package/src/app-server/rate-limits.ts +583 -0
  49. package/src/app-server/request.ts +73 -0
  50. package/src/app-server/run-attempt.ts +4862 -0
  51. package/src/app-server/session-binding.ts +398 -0
  52. package/src/app-server/session-history.ts +44 -0
  53. package/src/app-server/shared-client.ts +289 -0
  54. package/src/app-server/side-question.ts +1009 -0
  55. package/src/app-server/test-support.ts +48 -0
  56. package/src/app-server/thread-lifecycle.ts +959 -0
  57. package/src/app-server/timeout.ts +9 -0
  58. package/src/app-server/tool-progress-normalization.ts +77 -0
  59. package/src/app-server/trajectory.ts +368 -0
  60. package/src/app-server/transcript-mirror.ts +208 -0
  61. package/src/app-server/transport-stdio.ts +107 -0
  62. package/src/app-server/transport-websocket.ts +90 -0
  63. package/src/app-server/transport.ts +117 -0
  64. package/src/app-server/user-input-bridge.ts +316 -0
  65. package/src/app-server/version.ts +4 -0
  66. package/src/app-server/vision-tools.ts +12 -0
  67. package/src/command-account.ts +544 -0
  68. package/src/command-formatters.ts +426 -0
  69. package/src/command-handlers.ts +2021 -0
  70. package/src/command-plugins-management.ts +137 -0
  71. package/src/command-rpc.ts +142 -0
  72. package/src/commands.ts +65 -0
  73. package/src/conversation-binding-data.ts +124 -0
  74. package/src/conversation-binding.ts +561 -0
  75. package/src/conversation-control.ts +303 -0
  76. package/src/conversation-turn-collector.ts +186 -0
  77. package/src/conversation-turn-input.ts +106 -0
  78. package/src/migration/apply.ts +501 -0
  79. package/src/migration/helpers.ts +55 -0
  80. package/src/migration/plan.ts +461 -0
  81. package/src/migration/provider.ts +41 -0
  82. package/src/migration/source.ts +643 -0
  83. package/src/migration/targets.ts +25 -0
  84. package/src/node-cli-sessions.ts +711 -0
  85. package/test-api.ts +95 -0
  86. package/tsconfig.json +16 -0
@@ -0,0 +1,398 @@
1
+ import fs from "node:fs/promises";
2
+ import { embeddedAgentLog } from "autobot/plugin-sdk/agent-harness-runtime";
3
+ import {
4
+ ensureAuthProfileStore,
5
+ resolveDefaultAgentDir,
6
+ resolveProviderIdForAuth,
7
+ type AuthProfileStore,
8
+ } from "autobot/plugin-sdk/agent-runtime";
9
+ import {
10
+ CODEX_PLUGINS_MARKETPLACE_NAME,
11
+ normalizeCodexServiceTier,
12
+ type CodexAppServerApprovalPolicy,
13
+ type CodexAppServerSandboxMode,
14
+ } from "./config.js";
15
+ import type { PluginAppPolicyContext } from "./plugin-thread-config.js";
16
+ import type { CodexServiceTier } from "./protocol.js";
17
+
18
+ const CODEX_APP_SERVER_NATIVE_AUTH_PROVIDER = "openai-codex";
19
+ const PUBLIC_OPENAI_MODEL_PROVIDER = "openai";
20
+
21
+ type ProviderAuthAliasLookupParams = Parameters<typeof resolveProviderIdForAuth>[1];
22
+ type ProviderAuthAliasConfig = NonNullable<ProviderAuthAliasLookupParams>["config"];
23
+
24
+ export type CodexAppServerAuthProfileLookup = {
25
+ authProfileId?: string;
26
+ authProfileStore?: AuthProfileStore;
27
+ agentDir?: string;
28
+ config?: ProviderAuthAliasConfig;
29
+ };
30
+
31
+ export type CodexAppServerThreadBinding = {
32
+ schemaVersion: 1;
33
+ threadId: string;
34
+ sessionFile: string;
35
+ cwd: string;
36
+ authProfileId?: string;
37
+ model?: string;
38
+ modelProvider?: string;
39
+ approvalPolicy?: CodexAppServerApprovalPolicy;
40
+ sandbox?: CodexAppServerSandboxMode;
41
+ serviceTier?: CodexServiceTier;
42
+ dynamicToolsFingerprint?: string;
43
+ userMcpServersFingerprint?: string;
44
+ mcpServersFingerprint?: string;
45
+ pluginAppsFingerprint?: string;
46
+ pluginAppsInputFingerprint?: string;
47
+ pluginAppPolicyContext?: PluginAppPolicyContext;
48
+ contextEngine?: CodexAppServerContextEngineBinding;
49
+ createdAt: string;
50
+ updatedAt: string;
51
+ };
52
+
53
+ export type CodexAppServerContextEngineBinding = {
54
+ schemaVersion: 1;
55
+ engineId: string;
56
+ policyFingerprint: string;
57
+ projection?: CodexAppServerContextEngineProjectionBinding;
58
+ };
59
+
60
+ export type CodexAppServerContextEngineProjectionBinding = {
61
+ schemaVersion: 1;
62
+ mode: "thread_bootstrap";
63
+ epoch: string;
64
+ fingerprint?: string;
65
+ };
66
+
67
+ export function resolveCodexAppServerBindingPath(sessionFile: string): string {
68
+ return `${sessionFile}.codex-app-server.json`;
69
+ }
70
+
71
+ export async function readCodexAppServerBinding(
72
+ sessionFile: string,
73
+ lookup: Omit<CodexAppServerAuthProfileLookup, "authProfileId"> = {},
74
+ ): Promise<CodexAppServerThreadBinding | undefined> {
75
+ const path = resolveCodexAppServerBindingPath(sessionFile);
76
+ let raw: string;
77
+ try {
78
+ raw = await fs.readFile(path, "utf8");
79
+ } catch (error) {
80
+ if (isNotFound(error)) {
81
+ return undefined;
82
+ }
83
+ embeddedAgentLog.warn("failed to read codex app-server binding", { path, error });
84
+ return undefined;
85
+ }
86
+ try {
87
+ const parsed = JSON.parse(raw) as Partial<CodexAppServerThreadBinding>;
88
+ if (parsed.schemaVersion !== 1 || typeof parsed.threadId !== "string") {
89
+ return undefined;
90
+ }
91
+ const authProfileId =
92
+ typeof parsed.authProfileId === "string" ? parsed.authProfileId : undefined;
93
+ return {
94
+ schemaVersion: 1,
95
+ threadId: parsed.threadId,
96
+ sessionFile,
97
+ cwd: typeof parsed.cwd === "string" ? parsed.cwd : "",
98
+ authProfileId,
99
+ model: typeof parsed.model === "string" ? parsed.model : undefined,
100
+ modelProvider: normalizeCodexAppServerBindingModelProvider({
101
+ ...lookup,
102
+ authProfileId,
103
+ modelProvider: typeof parsed.modelProvider === "string" ? parsed.modelProvider : undefined,
104
+ }),
105
+ approvalPolicy: readApprovalPolicy(parsed.approvalPolicy),
106
+ sandbox: readSandboxMode(parsed.sandbox),
107
+ serviceTier: readServiceTier(parsed.serviceTier),
108
+ dynamicToolsFingerprint:
109
+ typeof parsed.dynamicToolsFingerprint === "string"
110
+ ? parsed.dynamicToolsFingerprint
111
+ : undefined,
112
+ userMcpServersFingerprint:
113
+ typeof parsed.userMcpServersFingerprint === "string"
114
+ ? parsed.userMcpServersFingerprint
115
+ : undefined,
116
+ mcpServersFingerprint:
117
+ typeof parsed.mcpServersFingerprint === "string" ? parsed.mcpServersFingerprint : undefined,
118
+ pluginAppsFingerprint:
119
+ typeof parsed.pluginAppsFingerprint === "string" ? parsed.pluginAppsFingerprint : undefined,
120
+ pluginAppsInputFingerprint:
121
+ typeof parsed.pluginAppsInputFingerprint === "string"
122
+ ? parsed.pluginAppsInputFingerprint
123
+ : undefined,
124
+ pluginAppPolicyContext: readPluginAppPolicyContext(parsed.pluginAppPolicyContext),
125
+ contextEngine: readContextEngineBinding(parsed.contextEngine),
126
+ createdAt: typeof parsed.createdAt === "string" ? parsed.createdAt : new Date().toISOString(),
127
+ updatedAt: typeof parsed.updatedAt === "string" ? parsed.updatedAt : new Date().toISOString(),
128
+ };
129
+ } catch (error) {
130
+ embeddedAgentLog.warn("failed to parse codex app-server binding", { path, error });
131
+ return undefined;
132
+ }
133
+ }
134
+
135
+ export async function writeCodexAppServerBinding(
136
+ sessionFile: string,
137
+ binding: Omit<
138
+ CodexAppServerThreadBinding,
139
+ "schemaVersion" | "sessionFile" | "createdAt" | "updatedAt"
140
+ > & {
141
+ createdAt?: string;
142
+ },
143
+ lookup: Omit<CodexAppServerAuthProfileLookup, "authProfileId"> = {},
144
+ ): Promise<void> {
145
+ const now = new Date().toISOString();
146
+ const payload: CodexAppServerThreadBinding = {
147
+ schemaVersion: 1,
148
+ sessionFile,
149
+ threadId: binding.threadId,
150
+ cwd: binding.cwd,
151
+ authProfileId: binding.authProfileId,
152
+ model: binding.model,
153
+ modelProvider: normalizeCodexAppServerBindingModelProvider({
154
+ ...lookup,
155
+ authProfileId: binding.authProfileId,
156
+ modelProvider: binding.modelProvider,
157
+ }),
158
+ approvalPolicy: binding.approvalPolicy,
159
+ sandbox: binding.sandbox,
160
+ serviceTier: binding.serviceTier,
161
+ dynamicToolsFingerprint: binding.dynamicToolsFingerprint,
162
+ userMcpServersFingerprint: binding.userMcpServersFingerprint,
163
+ mcpServersFingerprint: binding.mcpServersFingerprint,
164
+ pluginAppsFingerprint: binding.pluginAppsFingerprint,
165
+ pluginAppsInputFingerprint: binding.pluginAppsInputFingerprint,
166
+ pluginAppPolicyContext: binding.pluginAppPolicyContext,
167
+ contextEngine: binding.contextEngine,
168
+ createdAt: binding.createdAt ?? now,
169
+ updatedAt: now,
170
+ };
171
+ await fs.writeFile(
172
+ resolveCodexAppServerBindingPath(sessionFile),
173
+ `${JSON.stringify(payload, null, 2)}\n`,
174
+ );
175
+ }
176
+
177
+ function readContextEngineBinding(value: unknown): CodexAppServerContextEngineBinding | undefined {
178
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
179
+ return undefined;
180
+ }
181
+ const record = value as Record<string, unknown>;
182
+ if (
183
+ record.schemaVersion !== 1 ||
184
+ typeof record.engineId !== "string" ||
185
+ typeof record.policyFingerprint !== "string"
186
+ ) {
187
+ return undefined;
188
+ }
189
+ return {
190
+ schemaVersion: 1,
191
+ engineId: record.engineId,
192
+ policyFingerprint: record.policyFingerprint,
193
+ projection: readContextEngineProjectionBinding(record.projection),
194
+ };
195
+ }
196
+
197
+ function readContextEngineProjectionBinding(
198
+ value: unknown,
199
+ ): CodexAppServerContextEngineProjectionBinding | undefined {
200
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
201
+ return undefined;
202
+ }
203
+ const record = value as Record<string, unknown>;
204
+ if (
205
+ record.schemaVersion !== 1 ||
206
+ record.mode !== "thread_bootstrap" ||
207
+ typeof record.epoch !== "string" ||
208
+ !record.epoch.trim()
209
+ ) {
210
+ return undefined;
211
+ }
212
+ return {
213
+ schemaVersion: 1,
214
+ mode: "thread_bootstrap",
215
+ epoch: record.epoch,
216
+ fingerprint: typeof record.fingerprint === "string" ? record.fingerprint : undefined,
217
+ };
218
+ }
219
+
220
+ function readPluginAppPolicyContext(value: unknown): PluginAppPolicyContext | undefined {
221
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
222
+ return undefined;
223
+ }
224
+ const record = value as Record<string, unknown>;
225
+ if (typeof record.fingerprint !== "string") {
226
+ return undefined;
227
+ }
228
+ const apps = record.apps;
229
+ if (!apps || typeof apps !== "object" || Array.isArray(apps)) {
230
+ return undefined;
231
+ }
232
+ const parsedApps: PluginAppPolicyContext["apps"] = {};
233
+ for (const [appId, rawEntry] of Object.entries(apps)) {
234
+ if (!rawEntry || typeof rawEntry !== "object" || Array.isArray(rawEntry)) {
235
+ return undefined;
236
+ }
237
+ const entry = rawEntry as Record<string, unknown>;
238
+ if (
239
+ "appId" in entry ||
240
+ typeof entry.configKey !== "string" ||
241
+ entry.marketplaceName !== CODEX_PLUGINS_MARKETPLACE_NAME ||
242
+ typeof entry.pluginName !== "string" ||
243
+ typeof entry.allowDestructiveActions !== "boolean" ||
244
+ !Array.isArray(entry.mcpServerNames) ||
245
+ entry.mcpServerNames.some((serverName) => typeof serverName !== "string")
246
+ ) {
247
+ return undefined;
248
+ }
249
+ parsedApps[appId] = {
250
+ configKey: entry.configKey,
251
+ marketplaceName: entry.marketplaceName,
252
+ pluginName: entry.pluginName,
253
+ allowDestructiveActions: entry.allowDestructiveActions,
254
+ mcpServerNames: entry.mcpServerNames,
255
+ };
256
+ }
257
+ const parsedPluginAppIds: PluginAppPolicyContext["pluginAppIds"] = {};
258
+ const rawPluginAppIds = record.pluginAppIds;
259
+ if (rawPluginAppIds && (typeof rawPluginAppIds !== "object" || Array.isArray(rawPluginAppIds))) {
260
+ return undefined;
261
+ }
262
+ if (rawPluginAppIds && typeof rawPluginAppIds === "object") {
263
+ for (const [configKey, appIds] of Object.entries(rawPluginAppIds)) {
264
+ if (!Array.isArray(appIds) || appIds.some((appId) => typeof appId !== "string")) {
265
+ return undefined;
266
+ }
267
+ parsedPluginAppIds[configKey] = appIds;
268
+ }
269
+ }
270
+ return {
271
+ fingerprint: record.fingerprint,
272
+ apps: parsedApps,
273
+ pluginAppIds: parsedPluginAppIds,
274
+ };
275
+ }
276
+
277
+ export async function clearCodexAppServerBinding(sessionFile: string): Promise<void> {
278
+ try {
279
+ await fs.unlink(resolveCodexAppServerBindingPath(sessionFile));
280
+ } catch (error) {
281
+ if (!isNotFound(error)) {
282
+ embeddedAgentLog.warn("failed to clear codex app-server binding", { sessionFile, error });
283
+ }
284
+ }
285
+ }
286
+
287
+ function isNotFound(error: unknown): boolean {
288
+ return Boolean(error && typeof error === "object" && "code" in error && error.code === "ENOENT");
289
+ }
290
+
291
+ export function isCodexAppServerNativeAuthProfile(
292
+ lookup: CodexAppServerAuthProfileLookup,
293
+ ): boolean {
294
+ const authProfileId = lookup.authProfileId?.trim();
295
+ if (!authProfileId) {
296
+ return false;
297
+ }
298
+ try {
299
+ const credential = resolveCodexAppServerAuthProfileCredential({
300
+ ...lookup,
301
+ authProfileId,
302
+ });
303
+ return isCodexAppServerNativeAuthProvider({
304
+ provider: credential?.provider,
305
+ config: lookup.config,
306
+ });
307
+ } catch (error) {
308
+ embeddedAgentLog.debug("failed to resolve codex app-server auth profile provider", {
309
+ authProfileId,
310
+ error,
311
+ });
312
+ return false;
313
+ }
314
+ }
315
+
316
+ export function normalizeCodexAppServerBindingModelProvider(params: {
317
+ authProfileId?: string;
318
+ modelProvider?: string;
319
+ authProfileStore?: AuthProfileStore;
320
+ agentDir?: string;
321
+ config?: ProviderAuthAliasConfig;
322
+ }): string | undefined {
323
+ const modelProvider = params.modelProvider?.trim();
324
+ if (!modelProvider) {
325
+ return undefined;
326
+ }
327
+ if (
328
+ isCodexAppServerNativeAuthProfile(params) &&
329
+ modelProvider.toLowerCase() === PUBLIC_OPENAI_MODEL_PROVIDER
330
+ ) {
331
+ return undefined;
332
+ }
333
+ return modelProvider;
334
+ }
335
+
336
+ function resolveCodexAppServerAuthProfileCredential(
337
+ lookup: CodexAppServerAuthProfileLookup,
338
+ ): AuthProfileStore["profiles"][string] | undefined {
339
+ const authProfileId = lookup.authProfileId?.trim();
340
+ if (!authProfileId) {
341
+ return undefined;
342
+ }
343
+ const store =
344
+ lookup.authProfileStore ??
345
+ loadCodexAppServerAuthProfileStore({
346
+ agentDir: lookup.agentDir,
347
+ authProfileId,
348
+ config: lookup.config,
349
+ });
350
+ return store.profiles[authProfileId];
351
+ }
352
+
353
+ function loadCodexAppServerAuthProfileStore(params: {
354
+ agentDir: string | undefined;
355
+ authProfileId: string;
356
+ config?: ProviderAuthAliasConfig;
357
+ }): AuthProfileStore {
358
+ return ensureAuthProfileStore(
359
+ params.agentDir?.trim() || resolveDefaultAgentDir(params.config ?? {}),
360
+ {
361
+ allowKeychainPrompt: false,
362
+ config: params.config,
363
+ externalCliProviderIds: [CODEX_APP_SERVER_NATIVE_AUTH_PROVIDER],
364
+ externalCliProfileIds: [params.authProfileId],
365
+ },
366
+ );
367
+ }
368
+
369
+ function isCodexAppServerNativeAuthProvider(params: {
370
+ provider?: string;
371
+ config?: ProviderAuthAliasConfig;
372
+ }): boolean {
373
+ const provider = params.provider?.trim();
374
+ return Boolean(
375
+ provider &&
376
+ resolveProviderIdForAuth(provider, { config: params.config }) ===
377
+ CODEX_APP_SERVER_NATIVE_AUTH_PROVIDER,
378
+ );
379
+ }
380
+
381
+ function readApprovalPolicy(value: unknown): CodexAppServerApprovalPolicy | undefined {
382
+ return value === "never" ||
383
+ value === "on-request" ||
384
+ value === "on-failure" ||
385
+ value === "untrusted"
386
+ ? value
387
+ : undefined;
388
+ }
389
+
390
+ function readSandboxMode(value: unknown): CodexAppServerSandboxMode | undefined {
391
+ return value === "read-only" || value === "workspace-write" || value === "danger-full-access"
392
+ ? value
393
+ : undefined;
394
+ }
395
+
396
+ function readServiceTier(value: unknown): CodexServiceTier | undefined {
397
+ return normalizeCodexServiceTier(value);
398
+ }
@@ -0,0 +1,44 @@
1
+ import fs from "node:fs/promises";
2
+ import type { SessionEntry } from "@earendil-works/pi-coding-agent";
3
+ import {
4
+ buildSessionContext,
5
+ migrateSessionEntries,
6
+ parseSessionEntries,
7
+ } from "@earendil-works/pi-coding-agent";
8
+ import type { AgentMessage } from "autobot/plugin-sdk/agent-harness-runtime";
9
+ import { sanitizeCodexHistoryImagePayloads } from "./image-payload-sanitizer.js";
10
+
11
+ function isMissingFileError(error: unknown): boolean {
12
+ return Boolean(
13
+ error &&
14
+ typeof error === "object" &&
15
+ "code" in error &&
16
+ (error as { code?: unknown }).code === "ENOENT",
17
+ );
18
+ }
19
+
20
+ export async function readCodexMirroredSessionHistoryMessages(
21
+ sessionFile: string,
22
+ ): Promise<AgentMessage[] | undefined> {
23
+ try {
24
+ const raw = await fs.readFile(sessionFile, "utf-8");
25
+ const entries = parseSessionEntries(raw);
26
+ const firstEntry = entries[0] as { type?: unknown; id?: unknown } | undefined;
27
+ if (firstEntry?.type !== "session" || typeof firstEntry.id !== "string") {
28
+ return undefined;
29
+ }
30
+ migrateSessionEntries(entries);
31
+ const sessionEntries = entries.filter(
32
+ (entry): entry is SessionEntry => entry.type !== "session",
33
+ );
34
+ return sanitizeCodexHistoryImagePayloads(
35
+ buildSessionContext(sessionEntries).messages,
36
+ "codex mirrored history",
37
+ );
38
+ } catch (error) {
39
+ if (isMissingFileError(error)) {
40
+ return [];
41
+ }
42
+ return undefined;
43
+ }
44
+ }