import { fetchSchemaManifest } from './client.js'; import { ApiCliError } from './errors.js'; import { FACTORY_API_ROUTE_SCHEMAS } from './factory-route-metadata.generated.js'; import { FEEDBACK_SCORE_SCHEMAS } from './feedback-score-schemas.generated.js'; import { LEARNING_ROUTE_SCHEMAS } from './learning-route-metadata.js'; import type { ResolvedTarget } from './target.js'; import type { ApiCommandDescriptor, ApiCommandExample } from './types.js'; type BundledRouteSchema = { pathParamSchema?: unknown; queryParamSchema?: unknown; bodySchema?: unknown; responseSchema?: unknown; }; const FACTORY_BUNDLED_ROUTE_SCHEMAS = Object.fromEntries( Object.entries(FACTORY_API_ROUTE_SCHEMAS).map(([routeKey, schemas]) => [ routeKey, { pathParamSchema: 'path' in schemas ? schemas.path : undefined, queryParamSchema: 'query' in schemas ? schemas.query : undefined, bodySchema: 'body' in schemas ? schemas.body : undefined, responseSchema: schemas.response, }, ]), ); export const BUNDLED_ROUTE_SCHEMAS: Readonly> = { ...FACTORY_BUNDLED_ROUTE_SCHEMAS, ...FEEDBACK_SCORE_SCHEMAS, ...LEARNING_ROUTE_SCHEMAS, }; export async function getCommandSchema(descriptor: ApiCommandDescriptor, target: ResolvedTarget): Promise { if (!descriptor.acceptsInput) { throw new ApiCliError('SCHEMA_UNAVAILABLE', 'This command does not accept JSON input'); } const bundledSchema = BUNDLED_ROUTE_SCHEMAS[`${descriptor.method} ${descriptor.path}`]; if (bundledSchema) { return buildRouteSchema(descriptor, bundledSchema); } const manifest = await fetchSchemaManifest(target.baseUrl, target.headers, target.timeoutMs, target.apiPrefix); if (!manifest || typeof manifest !== 'object' || !Array.isArray((manifest as { routes?: unknown }).routes)) { throw new ApiCliError('SCHEMA_UNAVAILABLE', 'Target server returned an invalid schema manifest', { reason: 'invalid_manifest', }); } const route = manifest.routes.find( (candidate: any) => candidate.method === descriptor.method && candidate.path === descriptor.path, ); if (!route) { throw new ApiCliError('SCHEMA_UNAVAILABLE', 'Target server did not expose a schema for this command', { method: descriptor.method, path: descriptor.path, }); } return buildRouteSchema(descriptor, route); } function buildRouteSchema( descriptor: ApiCommandDescriptor, route: { pathParamSchema?: any; queryParamSchema?: any; bodySchema?: any; responseSchema?: any }, ): unknown { const source = descriptor.method === 'GET' ? 'query' : route.queryParamSchema ? 'query+body' : 'body'; const inputSchema = descriptor.method === 'GET' ? route.queryParamSchema : mergeObjectSchemas(route.queryParamSchema, route.bodySchema); return { command: buildCommandUsage(descriptor), description: descriptor.description, method: descriptor.method, path: descriptor.path, positionals: buildPositionals(descriptor, route.pathParamSchema), examples: buildCommandExamples(descriptor), input: { required: descriptor.inputRequired, source, schema: inputSchema, }, schemas: { pathParams: route.pathParamSchema, query: route.queryParamSchema, body: route.bodySchema, }, response: { list: descriptor.list, shape: descriptor.responseShape, schema: route.responseSchema, }, }; } export function buildCommandUsage(descriptor: ApiCommandDescriptor): string { const positionals = descriptor.positionals.map(name => `<${name}>`).join(' '); const input = descriptor.acceptsInput ? (descriptor.inputRequired ? '' : '[input]') : ''; return ['mastra api', descriptor.name, positionals, input].filter(Boolean).join(' '); } function mergeObjectSchemas(querySchema: any, bodySchema: any): any { if (!querySchema) return bodySchema; if (!bodySchema) return querySchema; return { type: 'object', properties: { ...(querySchema.properties ?? {}), ...(bodySchema.properties ?? {}), }, required: [...new Set([...(querySchema.required ?? []), ...(bodySchema.required ?? [])])], additionalProperties: bodySchema.additionalProperties ?? querySchema.additionalProperties, }; } function buildPositionals(descriptor: ApiCommandDescriptor, pathParamSchema: any): Array> { const properties = pathParamSchema?.properties ?? {}; const required = new Set(Array.isArray(pathParamSchema?.required) ? pathParamSchema.required : []); return descriptor.positionals.map(name => ({ name, required: required.has(name) || descriptor.path.includes(`:${name}`), description: properties[name]?.description, schema: properties[name], })); } export function buildCommandExamples(descriptor: ApiCommandDescriptor): ApiCommandExample[] { if (descriptor.examples && descriptor.examples.length > 0) { return descriptor.examples; } return buildGenericExamples(descriptor, `mastra api ${descriptor.name}`); } function buildGenericExamples(descriptor: ApiCommandDescriptor, command: string): ApiCommandExample[] { if (descriptor.list) { const positionalSamples = samplePositionals(descriptor); return [ { description: descriptor.description, command: [command, ...positionalSamples, descriptor.acceptsInput ? `'{"page":0,"perPage":50}'` : undefined] .filter(Boolean) .join(' '), }, ]; } if (!descriptor.acceptsInput) { return [{ description: descriptor.description, command: [command, ...samplePositionals(descriptor)].join(' ') }]; } const sampleInput = descriptor.method === 'GET' && descriptor.inputRequired ? sampleInputWithPathParams(descriptor) : '{}'; return [{ description: descriptor.description, command: `${command} '${sampleInput}'` }]; } function samplePositionals(descriptor: ApiCommandDescriptor): string[] { return descriptor.positionals.map(name => `${name}_123`); } function sampleInputWithPathParams(descriptor: ApiCommandDescriptor): string { const pathParams = [...descriptor.path.matchAll(/:([A-Za-z0-9_]+)/g)].flatMap(match => (match[1] ? [match[1]] : [])); const inputOnlyParams = pathParams.filter(param => !descriptor.positionals.includes(param)); if (inputOnlyParams.length === 0) return '{}'; return JSON.stringify(Object.fromEntries(inputOnlyParams.map(param => [param, `${param}_123`]))); }