Skip to content

Commit f9458a7

Browse files
Consume standardized schema titles
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent add04df commit f9458a7

10 files changed

Lines changed: 51 additions & 52 deletions

File tree

dotnet/src/Generated/Rpc.cs

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/rpc/generated_rpc.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nodejs/src/generated/rpc.ts

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nodejs/src/generated/session-events.ts

Lines changed: 1 addition & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/copilot/generated/rpc.py

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/codegen/csharp.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import path from "path";
1212
import { promisify } from "util";
1313
import type { JSONSchema7 } from "json-schema";
1414
import {
15-
applyTitleSuggestions,
15+
cloneSchemaForCodegen,
1616
getApiSchemaPath,
1717
getRpcSchemaTypeName,
1818
getSessionEventsSchemaPath,
@@ -584,7 +584,7 @@ namespace GitHub.Copilot.SDK;
584584
export async function generateSessionEvents(schemaPath?: string): Promise<void> {
585585
console.log("C#: generating session-events...");
586586
const resolvedPath = schemaPath ?? (await getSessionEventsSchemaPath());
587-
const schema = applyTitleSuggestions(JSON.parse(await fs.readFile(resolvedPath, "utf-8")) as JSONSchema7);
587+
const schema = cloneSchemaForCodegen(JSON.parse(await fs.readFile(resolvedPath, "utf-8")) as JSONSchema7);
588588
const code = generateSessionEventsCode(schema);
589589
const outPath = await writeGeneratedFile("dotnet/src/Generated/SessionEvents.cs", code);
590590
console.log(` ✓ ${outPath}`);
@@ -1138,7 +1138,7 @@ internal static class Diagnostics
11381138
export async function generateRpc(schemaPath?: string): Promise<void> {
11391139
console.log("C#: generating RPC types...");
11401140
const resolvedPath = schemaPath ?? (await getApiSchemaPath());
1141-
const schema = applyTitleSuggestions(JSON.parse(await fs.readFile(resolvedPath, "utf-8")) as ApiSchema);
1141+
const schema = cloneSchemaForCodegen(JSON.parse(await fs.readFile(resolvedPath, "utf-8")) as ApiSchema);
11421142
const code = generateRpcCode(schema);
11431143
const outPath = await writeGeneratedFile("dotnet/src/Generated/Rpc.cs", code);
11441144
console.log(` ✓ ${outPath}`);

scripts/codegen/go.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { JSONSchema7 } from "json-schema";
1212
import { FetchingJSONSchemaStore, InputData, JSONSchemaInput, quicktype } from "quicktype-core";
1313
import { promisify } from "util";
1414
import {
15-
applyTitleSuggestions,
15+
cloneSchemaForCodegen,
1616
getApiSchemaPath,
1717
getRpcSchemaTypeName,
1818
getSessionEventsSchemaPath,
@@ -842,7 +842,7 @@ async function generateSessionEvents(schemaPath?: string): Promise<void> {
842842
console.log("Go: generating session-events...");
843843

844844
const resolvedPath = schemaPath ?? (await getSessionEventsSchemaPath());
845-
const schema = applyTitleSuggestions(JSON.parse(await fs.readFile(resolvedPath, "utf-8")) as JSONSchema7);
845+
const schema = cloneSchemaForCodegen(JSON.parse(await fs.readFile(resolvedPath, "utf-8")) as JSONSchema7);
846846
const processed = postProcessSchema(schema);
847847

848848
const code = generateGoSessionEventsCode(processed);
@@ -859,7 +859,7 @@ async function generateRpc(schemaPath?: string): Promise<void> {
859859
console.log("Go: generating RPC types...");
860860

861861
const resolvedPath = schemaPath ?? (await getApiSchemaPath());
862-
const schema = applyTitleSuggestions(JSON.parse(await fs.readFile(resolvedPath, "utf-8")) as ApiSchema);
862+
const schema = cloneSchemaForCodegen(JSON.parse(await fs.readFile(resolvedPath, "utf-8")) as ApiSchema);
863863

864864
const allMethods = [
865865
...collectRpcMethods(schema.server || {}),

scripts/codegen/python.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import fs from "fs/promises";
1010
import type { JSONSchema7 } from "json-schema";
1111
import { FetchingJSONSchemaStore, InputData, JSONSchemaInput, quicktype } from "quicktype-core";
1212
import {
13-
applyTitleSuggestions,
13+
cloneSchemaForCodegen,
1414
getApiSchemaPath,
1515
getRpcSchemaTypeName,
1616
getSessionEventsSchemaPath,
@@ -213,7 +213,7 @@ async function generateSessionEvents(schemaPath?: string): Promise<void> {
213213
console.log("Python: generating session-events...");
214214

215215
const resolvedPath = schemaPath ?? (await getSessionEventsSchemaPath());
216-
const schema = applyTitleSuggestions(JSON.parse(await fs.readFile(resolvedPath, "utf-8")) as JSONSchema7);
216+
const schema = cloneSchemaForCodegen(JSON.parse(await fs.readFile(resolvedPath, "utf-8")) as JSONSchema7);
217217
const resolvedSchema = (schema.definitions?.SessionEvent as JSONSchema7) || schema;
218218
const processed = postProcessSchema(resolvedSchema);
219219

@@ -269,7 +269,7 @@ async function generateRpc(schemaPath?: string): Promise<void> {
269269
console.log("Python: generating RPC types...");
270270

271271
const resolvedPath = schemaPath ?? (await getApiSchemaPath());
272-
const schema = applyTitleSuggestions(JSON.parse(await fs.readFile(resolvedPath, "utf-8")) as ApiSchema);
272+
const schema = cloneSchemaForCodegen(JSON.parse(await fs.readFile(resolvedPath, "utf-8")) as ApiSchema);
273273

274274
const allMethods = [
275275
...collectRpcMethods(schema.server || {}),

scripts/codegen/typescript.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
isNodeFullyExperimental,
1717
isRpcMethod,
1818
postProcessSchema,
19+
stripNonAnnotationTitles,
1920
writeGeneratedFile,
2021
type ApiSchema,
2122
type RpcMethod,
@@ -130,7 +131,7 @@ async function generateSessionEvents(schemaPath?: string): Promise<void> {
130131

131132
const resolvedPath = schemaPath ?? (await getSessionEventsSchemaPath());
132133
const schema = JSON.parse(await fs.readFile(resolvedPath, "utf-8")) as JSONSchema7;
133-
const processed = postProcessSchema(schema);
134+
const processed = postProcessSchema(stripNonAnnotationTitles(schema));
134135

135136
const ts = await compile(processed, "SessionEvent", {
136137
bannerComment: `/**
@@ -176,7 +177,7 @@ import type { MessageConnection } from "vscode-jsonrpc/node.js";
176177

177178
for (const method of [...allMethods, ...clientSessionMethods]) {
178179
if (method.result) {
179-
const compiled = await compile(method.result, resultTypeName(method), {
180+
const compiled = await compile(stripNonAnnotationTitles(method.result), resultTypeName(method), {
180181
bannerComment: "",
181182
additionalProperties: false,
182183
});
@@ -188,7 +189,7 @@ import type { MessageConnection } from "vscode-jsonrpc/node.js";
188189
}
189190

190191
if (method.params?.properties && Object.keys(method.params.properties).length > 0) {
191-
const paramsCompiled = await compile(method.params, paramsTypeName(method), {
192+
const paramsCompiled = await compile(stripNonAnnotationTitles(method.params), paramsTypeName(method), {
192193
bannerComment: "",
193194
additionalProperties: false,
194195
});

scripts/codegen/utils.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,28 +118,47 @@ export interface RpcMethod {
118118
}
119119

120120
export function getRpcSchemaTypeName(schema: JSONSchema7 | null | undefined, fallback: string): string {
121-
const titleSuggestion = (schema as JSONSchema7 & { titleSuggestion?: unknown } | null | undefined)?.titleSuggestion;
122121
if (typeof schema?.title === "string") return schema.title;
123-
return typeof titleSuggestion === "string" ? titleSuggestion : fallback;
122+
return fallback;
124123
}
125124

126-
export function applyTitleSuggestions<T>(value: T): T {
125+
export function cloneSchemaForCodegen<T>(value: T): T {
127126
if (Array.isArray(value)) {
128-
return value.map((item) => applyTitleSuggestions(item)) as T;
127+
return value.map((item) => cloneSchemaForCodegen(item)) as T;
129128
}
130129

131130
if (value && typeof value === "object") {
132131
const result: Record<string, unknown> = {};
133-
const source = value as Record<string, unknown>;
134-
for (const [key, child] of Object.entries(source)) {
135-
if (key === "titleSuggestion") {
132+
for (const [key, child] of Object.entries(value as Record<string, unknown>)) {
133+
if (key === "titleSource") {
136134
continue;
137135
}
138-
result[key] = applyTitleSuggestions(child);
136+
result[key] = cloneSchemaForCodegen(child);
139137
}
140138

141-
if (typeof source.title !== "string" && typeof source.titleSuggestion === "string") {
142-
result.title = source.titleSuggestion;
139+
return result as T;
140+
}
141+
142+
return value;
143+
}
144+
145+
export function stripNonAnnotationTitles<T>(value: T): T {
146+
if (Array.isArray(value)) {
147+
return value.map((item) => stripNonAnnotationTitles(item)) as T;
148+
}
149+
150+
if (value && typeof value === "object") {
151+
const result: Record<string, unknown> = {};
152+
const source = value as Record<string, unknown>;
153+
const keepTitle = typeof source.title === "string" && source.titleSource === "annotation";
154+
for (const [key, child] of Object.entries(source)) {
155+
if (key === "titleSource") {
156+
continue;
157+
}
158+
if (key === "title" && !keepTitle) {
159+
continue;
160+
}
161+
result[key] = stripNonAnnotationTitles(child);
143162
}
144163

145164
return result as T;
@@ -232,6 +251,7 @@ function sortJsonValue(value: unknown): unknown {
232251
if (value && typeof value === "object") {
233252
return Object.fromEntries(
234253
Object.entries(value as Record<string, unknown>)
254+
.filter(([key]) => key !== "description" && key !== "titleSource")
235255
.sort(([left], [right]) => left.localeCompare(right))
236256
.map(([key, child]) => [key, sortJsonValue(child)])
237257
);

0 commit comments

Comments
 (0)