gen-cordis-api.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * Generate the model-facing Cordis API data module from the same event/service
  3. * collector as the documentation catalogs. It emits original declaration
  4. * JSDoc, first-sentence summaries, raw signatures, transitive public type
  5. * shapes, and inherited context entries, without source pointers; output is
  6. * deterministic and `--check` verifies it.
  7. */
  8. import { globSync, readFileSync, writeFileSync } from 'node:fs'
  9. import { resolve } from 'node:path'
  10. import ts from 'typescript'
  11. import { collectEvents, collectServices, INHERITED_SERVICES } from './gen-cordis-catalog.ts'
  12. const root = resolve(import.meta.dirname, '..')
  13. const OUT = 'packages/cordis/tool-cordis/src/api-catalog.ts'
  14. /** Declarations longer than this render as a truncated stub — a shape the model cannot skim teaches nothing. */
  15. const MAX_DECL_CHARS = 1500
  16. /** The first sentence of a (possibly multi-line) JSDoc prose block. */
  17. function firstSentence(doc: string): string {
  18. const line = doc.split('\n', 1)[0] ?? ''
  19. const match = /^(.*?[.!?])(?:\s|$)/.exec(line)
  20. return (match?.[1] ?? line).trim()
  21. }
  22. /** Render a string as a single-quoted, lint-clean TS literal. */
  23. function quote(value: string): string {
  24. return `'${value.replace(/\\/g, '\\\\').replace(/'/g, '\\\'').replace(/\n/g, '\\n')}'`
  25. }
  26. /**
  27. * Reduce an exported class to its type shape: drop method/constructor bodies
  28. * and property initializers so the catalog serves member signatures, not
  29. * implementation.
  30. */
  31. function classShape(node: ts.ClassDeclaration): ts.ClassDeclaration {
  32. const isNonPublic = (member: ts.ClassElement): boolean =>
  33. (ts.canHaveModifiers(member) ? ts.getModifiers(member) : undefined)?.some(m =>
  34. m.kind === ts.SyntaxKind.PrivateKeyword || m.kind === ts.SyntaxKind.ProtectedKeyword) ?? false
  35. const members = node.members.flatMap((member): ts.ClassElement[] => {
  36. if (isNonPublic(member) || (ts.isPropertyDeclaration(member) && ts.isPrivateIdentifier(member.name))) return []
  37. if (ts.isMethodDeclaration(member)) {
  38. return [ts.factory.updateMethodDeclaration(
  39. member, member.modifiers, member.asteriskToken, member.name, member.questionToken,
  40. member.typeParameters, member.parameters, member.type, undefined)]
  41. }
  42. if (ts.isConstructorDeclaration(member)) {
  43. return [ts.factory.updateConstructorDeclaration(member, member.modifiers, member.parameters, undefined)]
  44. }
  45. if (ts.isGetAccessorDeclaration(member)) {
  46. return [ts.factory.updateGetAccessorDeclaration(
  47. member, member.modifiers, member.name, member.parameters, member.type, undefined)]
  48. }
  49. if (ts.isSetAccessorDeclaration(member)) {
  50. return [ts.factory.updateSetAccessorDeclaration(
  51. member, member.modifiers, member.name, member.parameters, undefined)]
  52. }
  53. if (ts.isPropertyDeclaration(member)) {
  54. return [ts.factory.updatePropertyDeclaration(
  55. member, member.modifiers, member.name, member.questionToken ?? member.exclamationToken, member.type, undefined)]
  56. }
  57. return [member]
  58. })
  59. return ts.factory.updateClassDeclaration(
  60. node, node.modifiers, node.name, node.typeParameters, node.heritageClauses, members)
  61. }
  62. /**
  63. * Collect exported interface, type-alias, and body-stripped class shapes; omit
  64. * names declared in multiple packages rather than serve the wrong shape.
  65. */
  66. function collectTypeDecls(scanRoot: string = root): Map<string, string> {
  67. const printer = ts.createPrinter({ removeComments: true })
  68. const decls = new Map<string, string>()
  69. const ambiguous = new Set<string>()
  70. for (const rel of globSync('packages/*/*/src/*.ts', { cwd: scanRoot }).sort()) {
  71. const abs = resolve(scanRoot, rel)
  72. const sf = ts.createSourceFile(abs, readFileSync(abs, 'utf8'), ts.ScriptTarget.Latest, true)
  73. for (const stmt of sf.statements) {
  74. const named = ts.isInterfaceDeclaration(stmt) || ts.isTypeAliasDeclaration(stmt) || ts.isClassDeclaration(stmt)
  75. if (!named || stmt.name === undefined) continue
  76. if (!(stmt.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword) ?? false)) continue
  77. const name = stmt.name.text
  78. if (decls.has(name)) {
  79. ambiguous.add(name)
  80. continue
  81. }
  82. const emit = ts.isClassDeclaration(stmt) ? classShape(stmt) : stmt
  83. const printed = printer.printNode(ts.EmitHint.Unspecified, emit, sf).replace(/\r/g, '')
  84. decls.set(name, printed.length > MAX_DECL_CHARS
  85. ? `${printed.slice(0, MAX_DECL_CHARS)} /* …truncated — full shape in source */`
  86. : printed)
  87. }
  88. }
  89. for (const name of ambiguous) decls.delete(name)
  90. return decls
  91. }
  92. /** Resolve and sort the word-bounded transitive type closure referenced by seed text. */
  93. function referencedTypes(seeds: string[], decls: Map<string, string>): { name: string; declaration: string }[] {
  94. const included = new Map<string, string>()
  95. let frontier = seeds
  96. while (frontier.length > 0) {
  97. const next: string[] = []
  98. for (const [name, declaration] of decls) {
  99. if (included.has(name)) continue
  100. const pattern = new RegExp(`\\b${name}\\b`)
  101. if (frontier.some(text => pattern.test(text))) {
  102. included.set(name, declaration)
  103. next.push(declaration)
  104. }
  105. }
  106. frontier = next
  107. }
  108. return [...included].map(([name, declaration]) => ({ name, declaration })).sort((a, b) => a.name.localeCompare(b.name))
  109. }
  110. /** Render the whole generated module (pure, deterministic given sorted collector output). */
  111. function render(): string {
  112. const services = collectServices()
  113. const events = collectEvents().sort((a, b) => a.name.localeCompare(b.name))
  114. const types = referencedTypes(services.flatMap(service => service.methods.map(method => method.signature)), collectTypeDecls())
  115. const lines: string[] = [
  116. '/**',
  117. ' * Generated by scripts/gen-cordis-api.ts — do not edit by hand; run',
  118. ' * `pnpm run gen-cordis-api` to regenerate (freshness-gated by',
  119. ' * `pnpm run verify-cordis-api` in doc-sync).',
  120. ' *',
  121. ' * The machine-readable cordis API catalog `cordis_inspect` serves to the',
  122. ' * model: harness services (summary + public method signatures/JSDoc),',
  123. ' * harness events (mode + signature/JSDoc), and the inherited `ctx` surface. Produced by',
  124. ' * the same AST walk as docs/cordis-catalog, so this data and the rendered',
  125. ' * docs cannot diverge.',
  126. ' *',
  127. ' * @module @deepseek-ai/dsh-tool-cordis/api-catalog',
  128. ' */',
  129. '',
  130. '/** One public service method and its source-owned contract. */',
  131. 'export interface ServiceApiMethod {',
  132. ' /** Public method signature with its body stripped. */',
  133. ' signature: string',
  134. ' /** Original method JSDoc, with only container indentation removed. */',
  135. ' jsDoc: string',
  136. '}',
  137. '',
  138. '/** One harness `ctx.<key>` service: its one-line summary and public methods. */',
  139. 'export interface ServiceApiEntry {',
  140. ' /** The `ctx.<key>` name, e.g. `tools`. */',
  141. ' key: string',
  142. ' /** First sentence of the service class JSDoc. */',
  143. ' summary: string',
  144. ' /** Public methods, bodies stripped, in source order. */',
  145. ' methods: readonly ServiceApiMethod[]',
  146. '}',
  147. '',
  148. '/** One harness event: its dispatch mode, exact signature, and one-line summary. */',
  149. 'export interface EventApiEntry {',
  150. ' /** The scoped event name, e.g. `agent/status`. */',
  151. ' name: string',
  152. ' /** The dispatch mode from the declaration\'s `@mode` tag. */',
  153. ' mode: string',
  154. ' /** The exact listener signature, whitespace-normalized. */',
  155. ' signature: string',
  156. ' /** Original event JSDoc, with only container indentation removed. */',
  157. ' jsDoc: string',
  158. ' /** First sentence of the event JSDoc. */',
  159. ' summary: string',
  160. '}',
  161. '',
  162. '/** One inherited (cordis core + loader/hmr/timer) `ctx` member group with its summary. */',
  163. 'export interface InheritedApiEntry {',
  164. ' /** The `ctx` member name(s), e.g. `ctx.on / ctx.once`. */',
  165. ' name: string',
  166. ' /** One-line summary of what the member does. */',
  167. ' summary: string',
  168. '}',
  169. '',
  170. '/** One named type shape the service signatures reference. */',
  171. 'export interface TypeApiEntry {',
  172. ' /** The exported type/interface name, e.g. `BashRunResult`. */',
  173. ' name: string',
  174. ' /** The full declaration text, comments stripped. */',
  175. ' declaration: string',
  176. '}',
  177. '',
  178. '/** Every harness `ctx.<key>` service, sorted by key. */',
  179. 'export const SERVICE_API: readonly ServiceApiEntry[] = [',
  180. ]
  181. for (const service of services) {
  182. lines.push(' {')
  183. lines.push(` key: ${quote(service.key)},`)
  184. lines.push(` summary: ${quote(firstSentence(service.doc))},`)
  185. if (service.methods.length === 0) {
  186. lines.push(' methods: [],')
  187. } else {
  188. lines.push(' methods: [')
  189. for (const method of service.methods) {
  190. lines.push(' {')
  191. lines.push(` signature: ${quote(method.signature)},`)
  192. lines.push(` jsDoc: ${quote(method.jsDoc)},`)
  193. lines.push(' },')
  194. }
  195. lines.push(' ],')
  196. }
  197. lines.push(' },')
  198. }
  199. lines.push(
  200. ']',
  201. '',
  202. '/** Every harness event, sorted by name. */',
  203. 'export const EVENT_API: readonly EventApiEntry[] = [',
  204. )
  205. for (const event of events) {
  206. lines.push(' {')
  207. lines.push(` name: ${quote(event.name)},`)
  208. lines.push(` mode: ${quote(event.mode)},`)
  209. lines.push(` signature: ${quote(event.signature)},`)
  210. lines.push(` jsDoc: ${quote(event.jsDoc)},`)
  211. lines.push(` summary: ${quote(firstSentence(event.doc))},`)
  212. lines.push(' },')
  213. }
  214. lines.push(
  215. ']',
  216. '',
  217. '/** Shapes of every exported type the SERVICE_API signatures reference (transitively), sorted by name. */',
  218. 'export const TYPE_API: readonly TypeApiEntry[] = [',
  219. )
  220. for (const type of types) {
  221. lines.push(' {')
  222. lines.push(` name: ${quote(type.name)},`)
  223. lines.push(` declaration: ${quote(type.declaration)},`)
  224. lines.push(' },')
  225. }
  226. lines.push(
  227. ']',
  228. '',
  229. '/** The inherited `ctx` surface (cordis core + loader/hmr/timer), in curated order. */',
  230. 'export const INHERITED_CTX_API: readonly InheritedApiEntry[] = [',
  231. )
  232. for (const inherited of INHERITED_SERVICES) {
  233. lines.push(` { name: ${quote(inherited.name)}, summary: ${quote(inherited.summary)} },`)
  234. }
  235. lines.push(']', '')
  236. return lines.join('\n')
  237. }
  238. /** CLI entry: default writes the artifact, `--check` fails if the committed
  239. * copy is stale. Guarded behind an entry-point check so importing this module
  240. * for tests neither regenerates the committed file nor calls process.exit. */
  241. function main(): void {
  242. const content = render()
  243. if (process.argv.includes('--check')) {
  244. let committed: string | null = null
  245. try {
  246. committed = readFileSync(resolve(root, OUT), 'utf8')
  247. } catch {
  248. // Only ENOENT (not yet generated) is expected; a present-but-unreadable
  249. // file is not a state this repo produces. Either way the remedy is the
  250. // same — regenerate — so treat a read failure as "stale".
  251. committed = null
  252. }
  253. if (committed === content) {
  254. console.log(`gen-cordis-api: ${OUT} is up to date.`)
  255. process.exit(0)
  256. }
  257. console.error(`gen-cordis-api: ${OUT} is stale. Run \`pnpm run gen-cordis-api\` and commit ${OUT}.`)
  258. process.exit(1)
  259. }
  260. writeFileSync(resolve(root, OUT), content)
  261. console.log(`gen-cordis-api: wrote ${OUT}.`)
  262. }
  263. // Run only when invoked as a script, not when imported by a test.
  264. if (process.argv[1] && import.meta.filename === resolve(process.argv[1])) {
  265. main()
  266. }