MCPcopy Index your code
hub / github.com/github/copilot-sdk / rewriteSharedDefinitionReferences

Function rewriteSharedDefinitionReferences

scripts/codegen/utils.ts:1401–1449  ·  view source on GitHub ↗
(
    schema: T,
    sharedDefinitionNames: Iterable<string>,
    externalSchemaFile: string,
    preserveDefinitions = false
)

Source from the content-addressed store, hash-verified

1399}
1400
1401export function rewriteSharedDefinitionReferences<T>(
1402 schema: T,
1403 sharedDefinitionNames: Iterable<string>,
1404 externalSchemaFile: string,
1405 preserveDefinitions = false
1406): T {
1407 const sharedNames = new Set(sharedDefinitionNames);
1408 if (sharedNames.size === 0) return cloneSchemaForCodegen(schema);
1409
1410 const rewriteRef = (ref: string): string => {
1411 const localRef = parseLocalDefinitionRef(ref);
1412 return localRef && sharedNames.has(localRef) ? `${externalSchemaFile}#/definitions/${localRef}` : ref;
1413 };
1414
1415 const rewrite = (value: unknown): unknown => {
1416 if (Array.isArray(value)) {
1417 return value.map((item) => rewrite(item));
1418 }
1419
1420 if (!value || typeof value !== "object") {
1421 return value;
1422 }
1423
1424 const source = value as Record<string, unknown>;
1425 const result: Record<string, unknown> = {};
1426 for (const [childKey, childValue] of Object.entries(source)) {
1427 if ((childKey === "definitions" || childKey === "$defs") && childValue && typeof childValue === "object" && !Array.isArray(childValue)) {
1428 const definitions: Record<string, unknown> = {};
1429 for (const [definitionName, definitionValue] of Object.entries(childValue as Record<string, unknown>)) {
1430 if (preserveDefinitions || !sharedNames.has(definitionName)) {
1431 definitions[definitionName] = rewrite(definitionValue);
1432 }
1433 }
1434 result[childKey] = definitions;
1435 continue;
1436 }
1437
1438 result[childKey] = rewrite(childValue);
1439 }
1440
1441 if (typeof result.$ref === "string") {
1442 result.$ref = rewriteRef(result.$ref);
1443 }
1444
1445 return result;
1446 };
1447
1448 return rewrite(schema) as T;
1449}
1450
1451export function inlineExternalSchemaDefinitions<T>(
1452 schema: T,

Callers 6

generateFunction · 0.85
generateRpcFunction · 0.85
generateRpcFunction · 0.85
generateSessionEventsFunction · 0.85
generateRpcFunction · 0.85

Calls 2

cloneSchemaForCodegenFunction · 0.85
rewriteFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…