Integrate Codex app-server support
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
038560e580
commit
90d0f40790
68 files changed
+17389
-19730
No files matched your search
@@ -3,21 +3,10 @@
|
||||
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { make as makeJsonSchemaGenerator } from "@effect/openapi-generator/JsonSchemaGenerator";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Logger from "effect/Logger";
|
||||
import * as Path from "effect/Path";
|
||||
import * as Schema from "effect/Schema";
|
||||
import {
|
||||
FetchHttpClient,
|
||||
HttpClient,
|
||||
HttpClientRequest,
|
||||
HttpClientResponse,
|
||||
} from "effect/unstable/http";
|
||||
import { Effect, FileSystem, Layer, Logger, Path, Schema } from "effect";
|
||||
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
|
||||
|
||||
const UPSTREAM_REF = "678157acaa819d5510adfe359abb5d0392cfe461";
|
||||
const UPSTREAM_REF = "dbfe855f4fd0f5dcdf079882652a8efe622b0595";
|
||||
const USER_AGENT = "effect-codex-app-server-generator";
|
||||
const GITHUB_API_BASE =
|
||||
"https://api.github.com/repos/openai/codex/contents/codex-rs/app-server-protocol";
|
||||
@@ -32,15 +21,6 @@ const GithubContentEntries = Schema.Array(
|
||||
);
|
||||
type GithubContentEntry = (typeof GithubContentEntries.Type)[number];
|
||||
|
||||
const JsonSchemaDocument = Schema.StructWithRest(
|
||||
Schema.Struct({
|
||||
definitions: Schema.optionalKey(Schema.Record(Schema.String, Schema.Json)),
|
||||
}),
|
||||
[Schema.Record(Schema.String, Schema.Json)],
|
||||
);
|
||||
const decodeGithubContentEntries = Schema.decodeEffect(Schema.fromJsonString(GithubContentEntries));
|
||||
const decodeJsonSchemaDocument = Schema.decodeEffect(Schema.fromJsonString(JsonSchemaDocument));
|
||||
|
||||
interface GeneratedPaths {
|
||||
readonly generatedDir: string;
|
||||
readonly schemaOutputPath: string;
|
||||
@@ -63,14 +43,14 @@ interface JsonSchemaFile {
|
||||
|
||||
class GeneratorError extends Schema.TaggedErrorClass<GeneratorError>()("GeneratorError", {
|
||||
detail: Schema.String,
|
||||
cause: Schema.optional(Schema.Defect()),
|
||||
cause: Schema.optional(Schema.Defect),
|
||||
}) {
|
||||
override get message() {
|
||||
return this.detail;
|
||||
}
|
||||
}
|
||||
|
||||
const ManualSchemas: Record<string, Schema.Json> = {
|
||||
const ManualSchemas: Record<string, typeof Schema.Json.Type> = {
|
||||
GetAuthStatusParams: {
|
||||
type: "object",
|
||||
title: "GetAuthStatusParams",
|
||||
@@ -163,24 +143,45 @@ const ensureGeneratedDir = Effect.fn("ensureGeneratedDir")(function* () {
|
||||
});
|
||||
|
||||
const fetchText = Effect.fn("fetchText")(function* (url: string) {
|
||||
return yield* HttpClientRequest.get(url).pipe(
|
||||
HttpClientRequest.setHeader("user-agent", USER_AGENT),
|
||||
HttpClient.execute,
|
||||
Effect.flatMap(HttpClientResponse.filterStatusOk),
|
||||
Effect.flatMap((okResponse) => okResponse.text),
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new GeneratorError({
|
||||
detail: `Failed to fetch ${url}`,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const response = yield* Effect.tryPromise({
|
||||
try: () =>
|
||||
fetch(url, {
|
||||
headers: {
|
||||
"user-agent": USER_AGENT,
|
||||
},
|
||||
}),
|
||||
catch: (cause) =>
|
||||
new GeneratorError({
|
||||
detail: `Failed to fetch ${url}`,
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const detail = yield* Effect.tryPromise({
|
||||
try: () => response.text(),
|
||||
catch: () => "",
|
||||
});
|
||||
return yield* Effect.fail(
|
||||
new GeneratorError({
|
||||
detail: `Failed to download ${url}: ${response.status} ${detail}`,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
return yield* Effect.tryPromise({
|
||||
try: () => response.text(),
|
||||
catch: (cause) =>
|
||||
new GeneratorError({
|
||||
detail: `Failed to read response body for ${url}`,
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
const fetchDirectoryEntries = Effect.fn("fetchDirectoryEntries")(function* (path: string) {
|
||||
const raw = yield* fetchText(`${GITHUB_API_BASE}/${path}?ref=${UPSTREAM_REF}`);
|
||||
return yield* decodeGithubContentEntries(raw);
|
||||
return yield* Schema.decodeEffect(Schema.fromJsonString(GithubContentEntries))(raw);
|
||||
});
|
||||
|
||||
function collectSchemaEntries(
|
||||
@@ -218,7 +219,7 @@ function collectSchemaEntries(
|
||||
return entries;
|
||||
}
|
||||
|
||||
function normalizeNullableTypes(value: Schema.Json): Schema.Json {
|
||||
function normalizeNullableTypes(value: typeof Schema.Json.Type): typeof Schema.Json.Type {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(normalizeNullableTypes);
|
||||
}
|
||||
@@ -230,7 +231,10 @@ function normalizeNullableTypes(value: Schema.Json): Schema.Json {
|
||||
key,
|
||||
normalizeNullableTypes(child),
|
||||
]);
|
||||
const normalizedObject = Object.fromEntries(normalizedEntries) as Record<string, Schema.Json>;
|
||||
const normalizedObject = Object.fromEntries(normalizedEntries) as Record<
|
||||
string,
|
||||
typeof Schema.Json.Type
|
||||
>;
|
||||
const typeValue = normalizedObject.type;
|
||||
|
||||
if (!Array.isArray(typeValue)) {
|
||||
@@ -248,7 +252,7 @@ function normalizeNullableTypes(value: Schema.Json): Schema.Json {
|
||||
}
|
||||
const nonNullType = nonNullTypes[0]!;
|
||||
|
||||
const nextObject: Record<string, Schema.Json> = {};
|
||||
const nextObject: Record<string, typeof Schema.Json.Type> = {};
|
||||
for (const [key, child] of Object.entries(normalizedObject)) {
|
||||
if (key !== "type") {
|
||||
nextObject[key] = child;
|
||||
@@ -266,7 +270,7 @@ function normalizeNullableTypes(value: Schema.Json): Schema.Json {
|
||||
};
|
||||
}
|
||||
|
||||
function stripNullDefaults(value: Schema.Json): Schema.Json {
|
||||
function stripNullDefaults(value: typeof Schema.Json.Type): typeof Schema.Json.Type {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(stripNullDefaults);
|
||||
}
|
||||
@@ -278,7 +282,7 @@ function stripNullDefaults(value: Schema.Json): Schema.Json {
|
||||
Object.entries(value)
|
||||
.filter(([key, child]) => !(key === "default" && child === null))
|
||||
.map(([key, child]) => [key, stripNullDefaults(child)]),
|
||||
) as Schema.Json;
|
||||
) as typeof Schema.Json.Type;
|
||||
}
|
||||
|
||||
function toPascalCaseMethod(method: string) {
|
||||
@@ -348,13 +352,10 @@ function resolveResponseTypeName(
|
||||
const overrides: Record<string, string> = {
|
||||
"account/logout": "LogoutAccountResponse",
|
||||
"account/rateLimits/read": "GetAccountRateLimitsResponse",
|
||||
"account/usage/read": "GetAccountTokenUsageResponse",
|
||||
"account/workspaceMessages/read": "GetWorkspaceMessagesResponse",
|
||||
"config/batchWrite": "ConfigWriteResponse",
|
||||
"config/mcpServer/reload": "McpServerRefreshResponse",
|
||||
"config/value/write": "ConfigWriteResponse",
|
||||
"configRequirements/read": "ConfigRequirementsReadResponse",
|
||||
"externalAgentConfig/import/readHistories": "ExternalAgentConfigImportHistoriesReadResponse",
|
||||
};
|
||||
|
||||
const override = overrides[method];
|
||||
@@ -417,7 +418,7 @@ function renderSchemaMap(
|
||||
}
|
||||
|
||||
function renderSchemaTypeReference(schemaName: string) {
|
||||
return schemaName === "undefined" ? "undefined" : `CodexSchema.${schemaName}`;
|
||||
return schemaName === "undefined" ? "undefined" : `typeof CodexSchema.${schemaName}.Type`;
|
||||
}
|
||||
|
||||
function exportNameForPath(filePath: string): string {
|
||||
@@ -468,11 +469,11 @@ function buildJsonSchemaFiles(
|
||||
}
|
||||
|
||||
function rewriteExternalRefs(
|
||||
value: Schema.Json,
|
||||
value: typeof Schema.Json.Type,
|
||||
localDefinitionNames: ReadonlyMap<string, string>,
|
||||
currentNamespace: string | undefined,
|
||||
exportNameByQualifiedName: ReadonlyMap<string, string>,
|
||||
): Schema.Json {
|
||||
): typeof Schema.Json.Type {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((entry) =>
|
||||
rewriteExternalRefs(entry, localDefinitionNames, currentNamespace, exportNameByQualifiedName),
|
||||
@@ -522,7 +523,7 @@ function rewriteExternalRefs(
|
||||
),
|
||||
];
|
||||
}),
|
||||
) as Schema.Json;
|
||||
) as typeof Schema.Json.Type;
|
||||
}
|
||||
|
||||
const generateFiles = Effect.fn("generateFiles")(function* () {
|
||||
@@ -543,11 +544,13 @@ const generateFiles = Effect.fn("generateFiles")(function* () {
|
||||
const exportNameByQualifiedName = new Map(
|
||||
jsonSchemaFiles.map((file) => [file.qualifiedName, file.exportName]),
|
||||
);
|
||||
const aggregateSchemas: Record<string, Schema.Json> = {};
|
||||
const aggregateSchemas: Record<string, typeof Schema.Json.Type> = {};
|
||||
|
||||
for (const file of jsonSchemaFiles) {
|
||||
const raw = yield* fetchText(file.downloadUrl);
|
||||
const parsed = yield* decodeJsonSchemaDocument(raw);
|
||||
const parsed = JSON.parse(raw) as {
|
||||
readonly definitions?: Record<string, typeof Schema.Json.Type>;
|
||||
} & Record<string, typeof Schema.Json.Type>;
|
||||
const localDefinitionNames = new Map(
|
||||
Object.keys(parsed.definitions ?? {}).map((definitionName) => [
|
||||
definitionName,
|
||||
@@ -568,7 +571,7 @@ const generateFiles = Effect.fn("generateFiles")(function* () {
|
||||
);
|
||||
}
|
||||
|
||||
const topLevelSchema: Record<string, Schema.Json> = {};
|
||||
const topLevelSchema: Record<string, typeof Schema.Json.Type> = {};
|
||||
for (const [key, value] of Object.entries(parsed)) {
|
||||
if (key !== "definitions") {
|
||||
topLevelSchema[key] = value;
|
||||
@@ -748,16 +751,14 @@ const generateFiles = Effect.fn("generateFiles")(function* () {
|
||||
yield* Effect.log(`Generated Codex App Server schemas from ${UPSTREAM_REF}`);
|
||||
|
||||
yield* Effect.service(ChildProcessSpawner.ChildProcessSpawner).pipe(
|
||||
Effect.flatMap((spawner) =>
|
||||
spawner.spawn(ChildProcess.make("vp", ["fmt", generatedDir, "--write"])),
|
||||
),
|
||||
Effect.flatMap((spawner) => spawner.spawn(ChildProcess.make("bun", ["oxfmt", generatedDir]))),
|
||||
Effect.flatMap((child) => child.exitCode),
|
||||
Effect.tap((code) =>
|
||||
code === 0
|
||||
? Effect.void
|
||||
: Effect.fail(
|
||||
new GeneratorError({
|
||||
detail: `vp fmt failed with exit code ${code}`,
|
||||
detail: `oxfmt failed with exit code ${code}`,
|
||||
}),
|
||||
),
|
||||
),
|
||||
@@ -766,12 +767,6 @@ const generateFiles = Effect.fn("generateFiles")(function* () {
|
||||
|
||||
generateFiles().pipe(
|
||||
Effect.scoped,
|
||||
Effect.provide(
|
||||
Layer.mergeAll(
|
||||
Logger.layer([Logger.consolePretty()]),
|
||||
NodeServices.layer,
|
||||
FetchHttpClient.layer,
|
||||
),
|
||||
),
|
||||
Effect.provide(Layer.mergeAll(Logger.layer([Logger.consolePretty()]), NodeServices.layer)),
|
||||
NodeRuntime.runMain,
|
||||
);
|
||||
Reference in new issue
Block a user