Skip to content

Commit 567783e

Browse files
fix(codex): the protocol generator runs again on Effect rc.115 (pingdotgg#13480)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent f3cb2a1 commit 567783e

3 files changed

Lines changed: 20514 additions & 19043 deletions

File tree

‎apps/server/src/provider/Layers/CodexSessionRuntime.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const McpElicitationFormField = Schema.Struct({
119119
type: Schema.optionalKey(NullableMcpElicitationString),
120120
title: Schema.optionalKey(NullableMcpElicitationString),
121121
description: Schema.optionalKey(NullableMcpElicitationString),
122-
default: Schema.optionalKey(Schema.Unknown),
122+
default: Schema.optionalKey(Schema.Json),
123123
enum: Schema.optionalKey(Schema.NullOr(Schema.Array(Schema.String))),
124124
enumNames: Schema.optionalKey(Schema.NullOr(Schema.Array(Schema.String))),
125125
oneOf: Schema.optionalKey(
@@ -442,7 +442,7 @@ export function toMcpElicitationResponse(
442442
? "always"
443443
: undefined;
444444
const form = mcpElicitationFormFields(payload);
445-
const content: Record<string, unknown> = {};
445+
const content: Record<string, Schema.Json> = {};
446446

447447
for (const [key, field] of Object.entries(form?.properties ?? {})) {
448448
const options = mcpElicitationFieldOptions(field);

‎packages/effect-codex-app-server/scripts/generate.ts‎

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,13 @@ const ensureGeneratedDir = Effect.fn("ensureGeneratedDir")(function* () {
269269
});
270270

271271
const fetchText = Effect.fn("fetchText")(function* (url: string) {
272+
// Unauthenticated GitHub API calls are capped at 60/hour; set GITHUB_TOKEN to lift that.
273+
const token = process.env.GITHUB_TOKEN;
272274
return yield* HttpClientRequest.get(url).pipe(
273275
HttpClientRequest.setHeader("user-agent", USER_AGENT),
276+
token && url.startsWith("https://api.github.com/")
277+
? HttpClientRequest.bearerToken(token)
278+
: (request) => request,
274279
HttpClient.execute,
275280
Effect.flatMap(HttpClientResponse.filterStatusOk),
276281
Effect.flatMap((okResponse) => okResponse.text),
@@ -387,6 +392,84 @@ function stripNullDefaults(value: Schema.Json): Schema.Json {
387392
) as Schema.Json;
388393
}
389394

395+
type JsonSchemaNode = { readonly [key: string]: Schema.Json };
396+
397+
function isJsonSchemaNode(value: Schema.Json | undefined): value is JsonSchemaNode {
398+
return value !== null && typeof value === "object" && !Array.isArray(value);
399+
}
400+
401+
// Adapts Codex's JSON Schema to Effect's importer, visiting only schema
402+
// positions so a field literally named "properties" is left alone:
403+
// - Effect imports an object without additionalProperties as an open record.
404+
// Codex omits it for plain structs, so close objects that list properties.
405+
// - Effect cannot intersect shared object fields with object alternatives,
406+
// which Codex uses for "one of these keys plus shared fields"
407+
// (image_url | file_id). Fold the shared fields into each alternative.
408+
function adaptSchemaForEffect(value: Schema.Json): Schema.Json {
409+
if (!isJsonSchemaNode(value)) {
410+
return value;
411+
}
412+
const node: Record<string, Schema.Json> = { ...value };
413+
for (const key of ["items", "additionalProperties"]) {
414+
const child = node[key];
415+
if (isJsonSchemaNode(child)) node[key] = adaptSchemaForEffect(child);
416+
}
417+
for (const key of ["anyOf", "oneOf", "allOf"]) {
418+
const child = node[key];
419+
if (Array.isArray(child)) node[key] = child.map(adaptSchemaForEffect);
420+
}
421+
for (const key of ["properties", "definitions"]) {
422+
const child = node[key];
423+
if (isJsonSchemaNode(child)) {
424+
node[key] = Object.fromEntries(
425+
Object.entries(child).map(([name, schema]) => [name, adaptSchemaForEffect(schema)]),
426+
);
427+
}
428+
}
429+
430+
const { properties } = node;
431+
if (
432+
isJsonSchemaNode(properties) &&
433+
Object.keys(properties).length > 0 &&
434+
!("additionalProperties" in node)
435+
) {
436+
node.additionalProperties = false;
437+
}
438+
439+
const alternativesKey = "anyOf" in node ? "anyOf" : "oneOf";
440+
const alternatives = node[alternativesKey];
441+
if (
442+
!isJsonSchemaNode(properties) ||
443+
!Array.isArray(alternatives) ||
444+
!alternatives.every(
445+
(alternative) => isJsonSchemaNode(alternative) && alternative.type === "object",
446+
)
447+
) {
448+
return node;
449+
}
450+
const {
451+
properties: _shared,
452+
required,
453+
type: _type,
454+
additionalProperties: _closed,
455+
...rest
456+
} = node;
457+
const sharedRequired = Array.isArray(required) ? required : [];
458+
return {
459+
...rest,
460+
[alternativesKey]: alternatives.map((alternative) => {
461+
const branch = alternative as JsonSchemaNode;
462+
const branchProperties = isJsonSchemaNode(branch.properties) ? branch.properties : {};
463+
const branchRequired = Array.isArray(branch.required) ? branch.required : [];
464+
return {
465+
...branch,
466+
properties: { ...properties, ...branchProperties },
467+
required: [...new Set([...sharedRequired, ...branchRequired])],
468+
};
469+
}),
470+
};
471+
}
472+
390473
// Codex 0.153 adds async questions to agent messages. Keep older protocol
391474
// fields until the next full refresh, including every thread history namespace.
392475
function addAsyncQuestionFields(value: Schema.Json): Schema.Json {
@@ -649,7 +732,7 @@ function rewriteExternalRefs(
649732
const definitionName = child.slice("#/definitions/".length);
650733
const localRewrite = localDefinitionNames.get(definitionName);
651734
if (localRewrite) {
652-
return [key, `#/definitions/${localRewrite}`];
735+
return [key, `#/components/schemas/${localRewrite}`];
653736
}
654737

655738
const candidates = [
@@ -670,7 +753,7 @@ function rewriteExternalRefs(
670753
throw new Error(`Missing rewritten definition for ref: ${child}`);
671754
}
672755

673-
return [key, `#/definitions/${rewritten}`];
756+
return [key, `#/components/schemas/${rewritten}`];
674757
}
675758

676759
return [
@@ -761,7 +844,7 @@ const generateFiles = Effect.fn("generateFiles")(function* () {
761844
for (const [name, schema] of Object.entries(aggregateSchemas).toSorted(([left], [right]) =>
762845
left.localeCompare(right),
763846
)) {
764-
aggregateSchemas[name] = addAsyncQuestionFields(schema);
847+
aggregateSchemas[name] = adaptSchemaForEffect(addAsyncQuestionFields(schema));
765848
generator.addSchema(name, aggregateSchemas[name] as never);
766849
}
767850

0 commit comments

Comments
 (0)