Skip to content

Commit 35a469b

Browse files
committed
Actually add the file back...
1 parent 8829363 commit 35a469b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

typescript/generate.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env node
2+
3+
import { compile } from "json-schema-to-typescript";
4+
import { generate } from "ts-to-zod";
5+
import fs from "fs";
6+
7+
const jsonSchema = JSON.parse(fs.readFileSync("./schema/schema.json", "utf8"));
8+
const metadata = JSON.parse(fs.readFileSync("./schema/meta.json", "utf8"));
9+
10+
const tsSrc = await compile(jsonSchema, "Agent Client Protocol", {
11+
additionalProperties: false,
12+
bannerComment: false,
13+
});
14+
15+
const zodGenerator = generate({
16+
sourceText: tsSrc,
17+
bannerComment: false,
18+
keepComments: true,
19+
});
20+
const zodSchemas = zodGenerator.getZodSchemasFile();
21+
const zodInfer = zodGenerator.getInferredTypes("./zod");
22+
23+
const schemaTs = `
24+
export const AGENT_METHODS = ${JSON.stringify(metadata.agentMethods, null, 2)};
25+
26+
export const CLIENT_METHODS = ${JSON.stringify(metadata.clientMethods, null, 2)};
27+
28+
export const PROTOCOL_VERSION = ${metadata.version};
29+
30+
import { z } from "zod";
31+
32+
${fixGeneratedZod(zodInfer)}
33+
34+
${fixGeneratedZod(zodSchemas)}
35+
`;
36+
37+
function fixGeneratedZod(src) {
38+
return src
39+
.replace(`// Generated by ts-to-zod\nimport { z } from "zod";\n`, "")
40+
.replace(`import * as generated from "./zod";\n`, "")
41+
.replace(/typeof generated./g, "typeof ");
42+
}
43+
44+
fs.writeFileSync("typescript/schema.ts", schemaTs, "utf8");

0 commit comments

Comments
 (0)