Skip to content

Commit b86bfc3

Browse files
ochafikclaude
andcommitted
refactor: derive capability schemas and types from parent schemas
Schema refactoring: - ClientTasksCapabilitySchema now extracted from ClientCapabilitiesSchema.shape.tasks.unwrap() - ServerTasksCapabilitySchema now extracted from ServerCapabilitiesSchema.shape.tasks.unwrap() - ElicitationCapabilitySchema now extracted from ClientCapabilitiesSchema.shape.elicitation.unwrap() - Inline task and elicitation definitions in parent Capabilities schemas Type generation: - Add DERIVED_CAPABILITY_TYPES config in generate-schemas.ts - Inject ClientTasksCapability and ServerTasksCapability type aliases during pre-processing - Types derived as NonNullable<ParentType["property"]> This makes the parent Capabilities schemas the single source of truth, with sub-schemas and types derived from them. Types.ts: ~1261 lines (down from ~2600, ~51% reduction total) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent fbb88e6 commit b86bfc3

File tree

3 files changed

+1510
-1428
lines changed

3 files changed

+1510
-1428
lines changed

scripts/generate-schemas.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ const DISCRIMINATED_UNIONS: Record<string, string> = {
110110
'ContentBlockSchema': 'type',
111111
};
112112

113+
/**
114+
* Derived capability types to add during pre-processing.
115+
* These are extracted from parent capability interfaces for convenience.
116+
* Format: { typeName: 'ParentType["property"]' }
117+
*/
118+
const DERIVED_CAPABILITY_TYPES: Record<string, string> = {
119+
'ClientTasksCapability': 'ClientCapabilities["tasks"]',
120+
'ServerTasksCapability': 'ServerCapabilities["tasks"]',
121+
};
122+
113123
// =============================================================================
114124
// Pre-processing: Transform spec types to SDK-compatible hierarchy
115125
// =============================================================================
@@ -145,6 +155,9 @@ function preProcessTypes(content: string): string {
145155
// Transform 4: Update Request.params to use RequestParams
146156
updateRequestParamsType(sourceFile);
147157

158+
// Transform 5: Add derived capability types
159+
injectDerivedCapabilityTypes(sourceFile);
160+
148161
return sourceFile.getFullText();
149162
}
150163

@@ -266,6 +279,31 @@ function updateRequestParamsType(sourceFile: SourceFile): void {
266279
}
267280
}
268281

282+
/**
283+
* Add derived capability types that extract nested capabilities from parent types.
284+
* This allows reusing types like ClientCapabilities["tasks"] as standalone types.
285+
*
286+
* Adds type aliases like:
287+
* export type ClientTasksCapability = NonNullable<ClientCapabilities["tasks"]>;
288+
*/
289+
function injectDerivedCapabilityTypes(sourceFile: SourceFile): void {
290+
for (const [typeName, sourceExpression] of Object.entries(DERIVED_CAPABILITY_TYPES)) {
291+
// Check if already exists
292+
if (sourceFile.getTypeAlias(typeName)) {
293+
console.log(` - ${typeName} already exists`);
294+
continue;
295+
}
296+
297+
sourceFile.addTypeAlias({
298+
name: typeName,
299+
isExported: true,
300+
type: `NonNullable<${sourceExpression}>`,
301+
docs: [`Derived from ${sourceExpression} for convenience.`]
302+
});
303+
console.log(` ✓ Added derived type: ${typeName}`);
304+
}
305+
}
306+
269307
// =============================================================================
270308
// Post-processing: AST-based transforms using ts-morph
271309
// =============================================================================

0 commit comments

Comments
 (0)