gen-website-api.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /**
  2. * Generate (and verify) the website API reference under `website/zh-CN/api/`.
  3. *
  4. * The website's API section is FULLY GENERATED from source — never hand-edit
  5. * it. The hand-written hub `api/index.md` sits OUTSIDE the generated subdirs
  6. * (`api/cordis/`, `api/harness/`), so the orphan sweep never touches it. Two tiers:
  7. *
  8. * - `api/cordis/*` — the vendored cordis framework surface (Context, Events,
  9. * Fiber, Registry, Service), driven by the CORDIS_PAGES manifest below.
  10. * Members come from the real class declarations and the `declare module
  11. * './context.ts'` interface merges (the typed `ctx.*` surface a plugin
  12. * author actually sees).
  13. * - `api/harness/*` — one page per `ctx.<key>` harness service (walked from
  14. * every `declare module 'cordis'` Context merge under `packages/<group>/<pkg>/src`),
  15. * plus `events.md` listing every harness event grouped by scope.
  16. *
  17. * Prose comes from the JSDoc; the generator HARD-ERRORS (aggregated) when a
  18. * rendered member lacks a summary, a parameter lacks `@param`, or a non-void
  19. * annotated return lacks `@returns` — so a vendor sync or a new service method
  20. * cannot land undocumented without CI going red. Pages are English (the
  21. * planned zh translation flow arrives separately; see docs/i18n/README.md).
  22. *
  23. * Signature fences use the ` ```ts website-api ` info string: doc-typecheck
  24. * only processes its known info strings, so these bare (non-compilable)
  25. * signature fragments are skipped there, while VitePress still highlights the
  26. * `ts` token. The sidebar fragment `website/.vitepress/config/api-sidebar.json`
  27. * is generated alongside so navigation can never drift from the page set.
  28. *
  29. * `tsx scripts/gen-website-api.ts` → write pages + sidebar
  30. * `tsx scripts/gen-website-api.ts --check` → exit 1 if committed copies are
  31. * stale (doc-sync / CI gate)
  32. */
  33. import { globSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
  34. import { dirname, resolve } from 'node:path'
  35. import ts from 'typescript'
  36. import { checkParams, checkReturns, parseJsDoc, parseTags, pointer, rawJsDoc, reportViolations, type Mode } from './jsdoc.ts'
  37. const root = resolve(import.meta.dirname, '..')
  38. /** Output roots: generated pages and the generated sidebar fragment. */
  39. const PAGES_DIR = 'website/zh-CN/api'
  40. const SIDEBAR_OUT = 'website/.vitepress/config/api-sidebar.json'
  41. /** GitHub blob base for source links on the public site (repo-relative paths
  42. * do not resolve on the built site, unlike the in-repo catalogs). */
  43. const GITHUB = 'https://github.com/deepseek-harness/deepseek-harness/blob/master'
  44. /** Signature-fence info string (skipped by doc-typecheck, highlighted as ts). */
  45. const FENCE = 'ts website-api'
  46. /** One rendered member: a method/property plus its parsed JSDoc. */
  47. interface MemberDoc {
  48. /** Display name, e.g. `on` or `agent/pre-step`. */
  49. name: string
  50. /** Heading suffix with parameter names, e.g. `(name, listener, options?)`;
  51. * empty for properties. */
  52. heading: string
  53. /** All overload signature lines (bodies stripped). */
  54. signatures: string[]
  55. /** Description prose, one paragraph per line. */
  56. doc: string
  57. /** Parameter name → `@param` text, in declaration order. */
  58. params: { name: string; text: string }[]
  59. /** `@returns` text, or null for void/undocumented. */
  60. returns: string | null
  61. /** Repo-relative `file:line` of the (first) declaration. */
  62. source: string
  63. }
  64. /** A cordis-page section: which declarations it renders. */
  65. type Section =
  66. | { kind: 'class'; file: string; symbol: string; prefix?: string }
  67. | { kind: 'context-merge'; file: string }
  68. | { kind: 'decl'; file: string; symbol: string }
  69. /** One generated cordis page. */
  70. interface CordisPage {
  71. out: string
  72. title: string
  73. intro: string
  74. sections: Section[]
  75. }
  76. /**
  77. * The cordis tier manifest. Deliberately explicit (not a blind walk): the
  78. * vendor `Context` mixes true plugin-author surface with internals, and page
  79. * grouping is an editorial choice — but every member listed here is still
  80. * EXTRACTED, never transcribed, so signatures and docs cannot drift.
  81. */
  82. const CORDIS_PAGES: CordisPage[] = [
  83. {
  84. out: 'cordis/context.md',
  85. title: 'Context',
  86. intro: 'The context is the core cordis object: every service, event, and lifecycle API is reached through `ctx`. Event methods (`ctx.on`, `ctx.emit`, …) are documented on [Events](./events.md); `ctx.effect` and `ctx.fiber` on [Fiber](./fiber.md); `ctx.plugin` and `ctx.inject` on [Registry](./registry.md).',
  87. sections: [
  88. { kind: 'class', file: 'vendor/cordis/src/context.ts', symbol: 'Context', prefix: 'ctx.' },
  89. { kind: 'context-merge', file: 'vendor/cordis/src/reflect.ts' },
  90. ],
  91. },
  92. {
  93. out: 'cordis/events.md',
  94. title: 'Events',
  95. intro: 'The event system mixed into every context. Harness-defined events are cataloged on [Harness events](../harness/events.md).',
  96. sections: [
  97. { kind: 'context-merge', file: 'vendor/cordis/src/events.ts' },
  98. { kind: 'decl', file: 'vendor/cordis/src/events.ts', symbol: 'EventOptions' },
  99. { kind: 'decl', file: 'vendor/cordis/src/events.ts', symbol: 'DispatchMode' },
  100. ],
  101. },
  102. {
  103. out: 'cordis/fiber.md',
  104. title: 'Fiber',
  105. intro: 'A fiber is one loaded plugin instance: its lifecycle state, validated config, and registered effects. `ctx.fiber` is the current fiber; `ctx.effect()` delegates to it.',
  106. sections: [
  107. { kind: 'context-merge', file: 'vendor/cordis/src/fiber.ts' },
  108. { kind: 'class', file: 'vendor/cordis/src/fiber.ts', symbol: 'Fiber' },
  109. { kind: 'decl', file: 'vendor/cordis/src/fiber.ts', symbol: 'Effect' },
  110. { kind: 'decl', file: 'vendor/cordis/src/fiber.ts', symbol: 'Disposable' },
  111. { kind: 'decl', file: 'vendor/cordis/src/fiber.ts', symbol: 'EffectMeta' },
  112. { kind: 'decl', file: 'vendor/cordis/src/fiber.ts', symbol: 'CordisError' },
  113. { kind: 'decl', file: 'vendor/cordis/src/fiber.ts', symbol: 'ValidationError' },
  114. ],
  115. },
  116. {
  117. out: 'cordis/registry.md',
  118. title: 'Registry',
  119. intro: 'Plugin loading and dependency injection.',
  120. sections: [
  121. { kind: 'context-merge', file: 'vendor/cordis/src/registry.ts' },
  122. { kind: 'decl', file: 'vendor/cordis/src/registry.ts', symbol: 'Plugin' },
  123. { kind: 'decl', file: 'vendor/cordis/src/registry.ts', symbol: 'Inject' },
  124. ],
  125. },
  126. {
  127. out: 'cordis/service.md',
  128. title: 'Service',
  129. intro: 'Base class for context services: subclass it and load the subclass as a plugin to register `ctx.<name>`.',
  130. sections: [
  131. { kind: 'class', file: 'vendor/cordis/src/service.ts', symbol: 'Service' },
  132. ],
  133. },
  134. ]
  135. // ---------------------------------------------------------------------------
  136. // Extraction
  137. // ---------------------------------------------------------------------------
  138. const sfCache = new Map<string, { sf: ts.SourceFile; text: string }>()
  139. /** Parse (and cache) one repo-relative source file. */
  140. function load(rel: string): { sf: ts.SourceFile; text: string } {
  141. const cached = sfCache.get(rel)
  142. if (cached) return cached
  143. const text = readFileSync(resolve(root, rel), 'utf8')
  144. const sf = ts.createSourceFile(rel, text, ts.ScriptTarget.Latest, true)
  145. const entry = { sf, text }
  146. sfCache.set(rel, entry)
  147. return entry
  148. }
  149. /** The body of a `declare module './context.ts'` / `declare module 'cordis'`
  150. * block, or null. */
  151. function moduleBody(sf: ts.SourceFile): ts.ModuleBlock | null {
  152. for (const stmt of sf.statements) {
  153. if (!ts.isModuleDeclaration(stmt) || !ts.isStringLiteral(stmt.name)) continue
  154. if (stmt.name.text !== './context.ts' && stmt.name.text !== 'cordis') continue
  155. if (stmt.body && ts.isModuleBlock(stmt.body)) return stmt.body
  156. }
  157. return null
  158. }
  159. /** Signature text of a member: full text minus body/initializer, whitespace
  160. * collapsed, trailing semicolon stripped. */
  161. function signatureOf(member: ts.Node, sf: ts.SourceFile): string {
  162. const full = member.getText(sf)
  163. const tail = (member as { body?: ts.Node; initializer?: ts.Node }).body
  164. ?? (member as { initializer?: ts.Node }).initializer
  165. const sig = tail ? full.slice(0, full.length - tail.getText(sf).length).replace(/[=\s]+$/, '') : full
  166. return sig.replace(/\s*;?\s*$/, '').replace(/\s+/g, ' ').trim()
  167. }
  168. /** `(a, b?, ...rest)` heading suffix from a parameter list, `this` dropped. */
  169. function headingParams(parameters: readonly ts.ParameterDeclaration[], sf: ts.SourceFile): string {
  170. const names = parameters
  171. .filter(p => !(ts.isIdentifier(p.name) && p.name.text === 'this'))
  172. .map((p) => {
  173. const dots = p.dotDotDotToken ? '...' : ''
  174. const opt = p.questionToken || p.initializer ? '?' : ''
  175. return `${dots}${p.name.getText(sf)}${opt}`
  176. })
  177. return `(${names.join(', ')})`
  178. }
  179. /** Whether a class member is renderable public API (non-static half). */
  180. function isPublicInstance(member: ts.ClassElement): boolean {
  181. const mods = ts.getCombinedModifierFlags(member)
  182. if (mods & (ts.ModifierFlags.Private | ts.ModifierFlags.Protected | ts.ModifierFlags.Static)) return false
  183. if (!member.name) return false
  184. if (ts.isComputedPropertyName(member.name) || ts.isPrivateIdentifier(member.name)) return false
  185. return !member.name.getText().startsWith('_')
  186. }
  187. /** Whether a class member is renderable public STATIC API. */
  188. function isPublicStatic(member: ts.ClassElement): boolean {
  189. const mods = ts.getCombinedModifierFlags(member)
  190. if (mods & (ts.ModifierFlags.Private | ts.ModifierFlags.Protected)) return false
  191. if (!(mods & ts.ModifierFlags.Static)) return false
  192. if (!member.name || ts.isComputedPropertyName(member.name) || ts.isPrivateIdentifier(member.name)) return false
  193. return !member.name.getText().startsWith('_')
  194. }
  195. /** Build a MemberDoc from a declaration group (overloads share one entry),
  196. * collecting completeness violations for everything rendered. */
  197. function memberDoc(
  198. where: string,
  199. name: string,
  200. group: (ts.MethodDeclaration | ts.MethodSignature | ts.PropertyDeclaration | ts.PropertySignature | ts.GetAccessorDeclaration)[],
  201. rel: string,
  202. violations: string[],
  203. ): MemberDoc {
  204. const { sf, text } = load(rel)
  205. const first = group[0]
  206. if (!first) throw new Error(`gen-website-api: empty member group for ${name}`)
  207. // Doc from the first overload that carries JSDoc prose.
  208. const rawDocs = group.map(m => rawJsDoc(text, m))
  209. const docIndex = rawDocs.findIndex(r => parseJsDoc(r).doc !== '')
  210. const raw = docIndex === -1 ? '' : (rawDocs[docIndex] ?? '')
  211. const doc = parseJsDoc(raw).doc
  212. if (!doc) violations.push(`${where} has no JSDoc prose.`)
  213. const { params: tags, returns } = parseTags(raw)
  214. const params: { name: string; text: string }[] = []
  215. let returnsText: string | null = null
  216. const funcLike = group.filter((m): m is ts.MethodDeclaration | ts.MethodSignature => ts.isMethodDeclaration(m) || ts.isMethodSignature(m))
  217. const docCarrier = funcLike[docIndex === -1 ? 0 : docIndex]
  218. if (docCarrier) {
  219. checkParams(where, 'website-api', docCarrier.parameters, tags, sf,
  220. p => ts.isIdentifier(p.name) && p.name.text === 'this', violations)
  221. if (docCarrier.type) {
  222. checkReturns(where, docCarrier.type, returns, sf, violations)
  223. } else if (!returns && ts.isMethodDeclaration(docCarrier)) {
  224. // Comment-only vendor policy: we cannot add a return type annotation to
  225. // pinned upstream source, so an unannotated rendered method must carry
  226. // an explicit @returns describing the result instead.
  227. violations.push(`${where} has no return type annotation; document the result with @returns.`)
  228. }
  229. for (const p of docCarrier.parameters) {
  230. if (ts.isIdentifier(p.name) && p.name.text === 'this') continue
  231. const pname = p.name.getText(sf)
  232. const tag = tags.get(pname)
  233. if (tag) params.push({ name: pname, text: tag })
  234. }
  235. returnsText = returns
  236. }
  237. const headingSource = docCarrier ?? funcLike[0]
  238. return {
  239. name,
  240. heading: headingSource ? headingParams(headingSource.parameters, sf) : '',
  241. signatures: (ts.isMethodDeclaration(first) && funcLike.length > 1
  242. ? funcLike.filter(m => ts.isMethodDeclaration(m) && !m.body)
  243. : group).map(m => signatureOf(m, sf)),
  244. doc,
  245. params,
  246. returns: returnsText,
  247. source: pointer(rel, sf, first),
  248. }
  249. }
  250. /** Members of the `interface Context` merge in `rel`, overloads grouped. */
  251. function contextMergeMembers(rel: string, violations: string[]): MemberDoc[] {
  252. const { sf } = load(rel)
  253. const body = moduleBody(sf)
  254. if (!body) throw new Error(`gen-website-api: ${rel} has no context module merge`)
  255. const groups = new Map<string, (ts.MethodSignature | ts.PropertySignature)[]>()
  256. for (const stmt of body.statements) {
  257. if (!ts.isInterfaceDeclaration(stmt) || stmt.name.text !== 'Context') continue
  258. for (const member of stmt.members) {
  259. if (!ts.isMethodSignature(member) && !ts.isPropertySignature(member)) continue
  260. if (ts.isComputedPropertyName(member.name)) continue
  261. const name = member.name.getText(sf)
  262. const group = groups.get(name) ?? []
  263. group.push(member)
  264. groups.set(name, group)
  265. }
  266. }
  267. return [...groups.entries()].map(([name, group]) =>
  268. memberDoc(`ctx.${name} (${rel})`, name, group, rel, violations))
  269. }
  270. /** Instance + static members of one class, as two rendered lists. */
  271. function classMembers(rel: string, className: string, violations: string[]): {
  272. doc: string
  273. instance: MemberDoc[]
  274. statics: MemberDoc[]
  275. source: string
  276. } {
  277. const { sf, text } = load(rel)
  278. const cls = sf.statements.find(
  279. (s): s is ts.ClassDeclaration => ts.isClassDeclaration(s) && s.name?.text === className,
  280. )
  281. if (!cls) throw new Error(`gen-website-api: class ${className} not found in ${rel}`)
  282. const clsDoc = parseJsDoc(rawJsDoc(text, cls)).doc
  283. if (!clsDoc) violations.push(`class ${className} (${pointer(rel, sf, cls)}) has no JSDoc.`)
  284. const instance = new Map<string, (ts.MethodDeclaration | ts.PropertyDeclaration | ts.GetAccessorDeclaration)[]>()
  285. const statics = new Map<string, (ts.MethodDeclaration | ts.PropertyDeclaration)[]>()
  286. for (const member of cls.members) {
  287. const renderable = ts.isMethodDeclaration(member) || ts.isPropertyDeclaration(member) || ts.isGetAccessorDeclaration(member)
  288. if (!renderable) continue
  289. const name = member.name.getText(sf)
  290. if (isPublicInstance(member)) {
  291. const group = instance.get(name) ?? []
  292. group.push(member)
  293. instance.set(name, group)
  294. } else if (isPublicStatic(member) && !ts.isGetAccessorDeclaration(member)) {
  295. const group = statics.get(name) ?? []
  296. group.push(member)
  297. statics.set(name, group)
  298. }
  299. }
  300. type Renderable = ts.MethodDeclaration | ts.PropertyDeclaration | ts.GetAccessorDeclaration
  301. const toDocs = (groups: Map<string, Renderable[]>, prefix: string): MemberDoc[] =>
  302. [...groups.entries()].map(([name, group]) =>
  303. memberDoc(`${prefix}${name} (${rel})`, name, group, rel, violations))
  304. return {
  305. doc: clsDoc,
  306. instance: toDocs(instance, `${className}#`),
  307. statics: toDocs(statics, `${className}.`),
  308. source: pointer(rel, sf, cls),
  309. }
  310. }
  311. /** Splice every function-like BODY out of a declaration's text, leaving the
  312. * signature (`) {` → `)`). A reference paste shows shapes, not implementation;
  313. * property initializers (e.g. an `as const` code table) are data and stay. */
  314. function stripBodies(node: ts.Node, sf: ts.SourceFile): string {
  315. const cuts: { start: number; end: number }[] = []
  316. const visit = (n: ts.Node): void => {
  317. const funcLike = ts.isMethodDeclaration(n) || ts.isConstructorDeclaration(n)
  318. || ts.isFunctionDeclaration(n) || ts.isGetAccessorDeclaration(n) || ts.isSetAccessorDeclaration(n)
  319. if (funcLike && n.body) {
  320. // Cut from just after the parameter close (or return-type end) through
  321. // the body, so `foo(a: string) { … }` renders as `foo(a: string)`.
  322. const sigEnd = (n.type ?? n.parameters[n.parameters.length - 1] ?? n).getEnd()
  323. // Find the `)` (and optional `: Type`) boundary: body start is exact.
  324. cuts.push({ start: sigEnd, end: n.body.getEnd() })
  325. return // nothing renderable inside the body
  326. }
  327. n.forEachChild(visit)
  328. }
  329. visit(node)
  330. const base = node.getStart(sf)
  331. let out = node.getText(sf)
  332. for (const cut of cuts.sort((a, b) => b.start - a.start)) {
  333. const head = out.slice(0, cut.start - base)
  334. // Keep everything of the signature up to the closing paren / return type,
  335. // drop ` { … }`. The head may end mid-signature (last param), so retain
  336. // the source between sigEnd and the body's `{` MINUS trailing space.
  337. const between = out.slice(cut.start - base, cut.end - base)
  338. const bodyBrace = between.indexOf('{')
  339. out = head + between.slice(0, bodyBrace).trimEnd() + out.slice(cut.end - base)
  340. }
  341. return out
  342. }
  343. /** Verbatim declaration paste: every top-level statement named `symbol`
  344. * (class + merged namespace both), with leading JSDoc prose extracted and
  345. * function bodies stripped (a reference shows shapes, not implementation). */
  346. function declPaste(rel: string, symbol: string): { doc: string; code: string; source: string } {
  347. const { sf, text } = load(rel)
  348. const matches = sf.statements.filter((s) => {
  349. const named = ts.isInterfaceDeclaration(s) || ts.isTypeAliasDeclaration(s)
  350. || ts.isClassDeclaration(s) || ts.isEnumDeclaration(s) || ts.isModuleDeclaration(s)
  351. return named && s.name?.getText(sf) === symbol
  352. })
  353. if (matches.length === 0) throw new Error(`gen-website-api: declaration ${symbol} not found in ${rel}`)
  354. const first = matches[0]
  355. if (!first) throw new Error(`gen-website-api: declaration ${symbol} not found in ${rel}`)
  356. const doc = parseJsDoc(rawJsDoc(text, first)).doc
  357. const code = matches.map(s => stripBodies(s, sf).replace(/^export\s+(default\s+)?/, '')).join('\n\n')
  358. return { doc, code, source: pointer(rel, sf, first) }
  359. }
  360. /** One harness service with member-level detail. */
  361. interface HarnessService {
  362. key: string
  363. type: string
  364. abstract: boolean
  365. doc: string
  366. members: MemberDoc[]
  367. source: string
  368. /** Owning npm package name (from the package.json beside the entry). */
  369. pkg: string
  370. }
  371. /** Walk every harness `declare module 'cordis'` Context merge → services. */
  372. function collectHarnessServices(violations: string[]): HarnessService[] {
  373. const services: HarnessService[] = []
  374. for (const rel of globSync('packages/*/*/src/index.ts', { cwd: root }).sort()) {
  375. const { sf, text } = load(rel)
  376. if (!text.includes('interface Context')) continue
  377. const body = moduleBody(sf)
  378. if (!body) continue
  379. const keyToType = new Map<string, string>()
  380. for (const stmt of body.statements) {
  381. if (!ts.isInterfaceDeclaration(stmt) || stmt.name.text !== 'Context') continue
  382. for (const member of stmt.members) {
  383. if (!ts.isPropertySignature(member) || !member.type) continue
  384. keyToType.set(member.name.getText(sf), member.type.getText(sf))
  385. }
  386. }
  387. const pkgJson = rel.replace(/src\/index\.ts$/, 'package.json')
  388. const pkg = (JSON.parse(readFileSync(resolve(root, pkgJson), 'utf8')) as { name: string }).name
  389. for (const [key, type] of keyToType) {
  390. const cls = sf.statements.find(
  391. (s): s is ts.ClassDeclaration => ts.isClassDeclaration(s) && s.name?.text === type,
  392. )
  393. if (!cls) continue // a Pick-mixin member, not a class here
  394. const abstract = cls.modifiers?.some(m => m.kind === ts.SyntaxKind.AbstractKeyword) ?? false
  395. const clsDoc = parseJsDoc(rawJsDoc(text, cls)).doc
  396. if (!clsDoc) violations.push(`service ctx.${key} (${pointer(rel, sf, cls)}): class ${type} has no JSDoc.`)
  397. const groups = new Map<string, ts.MethodDeclaration[]>()
  398. for (const member of cls.members) {
  399. if (!ts.isMethodDeclaration(member)) continue
  400. if (!isPublicInstance(member)) continue
  401. const name = member.name.getText(sf)
  402. const group = groups.get(name) ?? []
  403. group.push(member)
  404. groups.set(name, group)
  405. }
  406. const members = [...groups.entries()].map(([name, group]) =>
  407. memberDoc(`ctx.${key}.${name} (${rel})`, name, group, rel, violations))
  408. services.push({ key, type, abstract, doc: clsDoc, members, source: pointer(rel, sf, cls), pkg })
  409. }
  410. }
  411. return services.sort((a, b) => a.key.localeCompare(b.key))
  412. }
  413. /** One harness event with member-level detail. */
  414. interface HarnessEvent {
  415. name: string
  416. scope: string
  417. mode: Mode | null
  418. signature: string
  419. doc: string
  420. params: { name: string; text: string }[]
  421. source: string
  422. }
  423. /** Walk every harness `interface Events` merge → events. */
  424. function collectHarnessEvents(violations: string[]): HarnessEvent[] {
  425. const events: HarnessEvent[] = []
  426. for (const rel of globSync('packages/*/*/src/*.ts', { cwd: root }).sort()) {
  427. const { sf, text } = load(rel)
  428. if (!text.includes('interface Events')) continue
  429. const body = moduleBody(sf)
  430. if (!body) continue
  431. for (const stmt of body.statements) {
  432. if (!ts.isInterfaceDeclaration(stmt) || stmt.name.text !== 'Events') continue
  433. for (const member of stmt.members) {
  434. if (!ts.isMethodSignature(member)) continue
  435. const name = ts.isStringLiteral(member.name) ? member.name.text : member.name.getText(sf)
  436. const raw = rawJsDoc(text, member)
  437. const { doc, mode } = parseJsDoc(raw)
  438. if (!mode) violations.push(`event '${name}' (${pointer(rel, sf, member)}) is missing @mode.`)
  439. if (!doc) violations.push(`event '${name}' (${pointer(rel, sf, member)}) has no JSDoc prose.`)
  440. const { params: tags } = parseTags(raw)
  441. const last = member.parameters.at(-1)
  442. const hasNext = !!last && last.name.getText(sf) === 'next'
  443. checkParams(`event '${name}' (${pointer(rel, sf, member)})`, 'website-api', member.parameters, tags, sf,
  444. p => (ts.isIdentifier(p.name) && p.name.text === 'this') || (hasNext && p === last), violations)
  445. const params: { name: string; text: string }[] = []
  446. for (const p of member.parameters) {
  447. const pname = p.name.getText(sf)
  448. const tag = tags.get(pname)
  449. if (tag) params.push({ name: pname, text: tag })
  450. }
  451. events.push({ name, scope: name.split('/')[0] ?? name, mode, signature: signatureOf(member, sf), doc, params, source: pointer(rel, sf, member) })
  452. }
  453. }
  454. }
  455. return events.sort((a, b) => a.name.localeCompare(b.name))
  456. }
  457. // ---------------------------------------------------------------------------
  458. // Rendering
  459. // ---------------------------------------------------------------------------
  460. const BANNER = '<!-- Generated by scripts/gen-website-api.ts — do not edit by hand. Run `pnpm run gen-website-api` to regenerate. -->'
  461. /** GitHub source link for a `file:line` pointer. */
  462. function sourceLink(source: string): string {
  463. const [file, line] = source.split(':')
  464. return `[Source](${GITHUB}/${file}#L${line})`
  465. }
  466. /** Render prose paragraphs (one per line of `doc`). */
  467. function prose(doc: string): string[] {
  468. return doc.split('\n').filter(l => l.trim() !== '')
  469. }
  470. /** Render one member section at heading depth 3. */
  471. function renderMember(prefix: string, m: MemberDoc): string[] {
  472. const lines: string[] = []
  473. const call = m.heading === '' ? '' : m.heading
  474. lines.push(`### ${prefix}${m.name}${call}`, '')
  475. lines.push('```' + FENCE)
  476. for (const sig of m.signatures) lines.push(sig)
  477. lines.push('```', '')
  478. lines.push(...prose(m.doc), '')
  479. if (m.params.length > 0) {
  480. for (const p of m.params) lines.push(`- \`${p.name}\` — ${p.text}`)
  481. lines.push('')
  482. }
  483. if (m.returns) lines.push(`**Returns** ${m.returns}`, '')
  484. lines.push(sourceLink(m.source), '')
  485. return lines
  486. }
  487. /** Render one cordis-tier page from its manifest entry. */
  488. function renderCordisPage(page: CordisPage, violations: string[]): string {
  489. const lines: string[] = [BANNER, '', `# ${page.title}`, '', page.intro, '']
  490. for (const section of page.sections) {
  491. if (section.kind === 'context-merge') {
  492. for (const m of contextMergeMembers(section.file, violations)) {
  493. lines.push(...renderMember('ctx.', m))
  494. }
  495. } else if (section.kind === 'class') {
  496. const cls = classMembers(section.file, section.symbol, violations)
  497. lines.push(...prose(cls.doc), '', sourceLink(cls.source), '')
  498. const instancePrefix = section.prefix ?? `${section.symbol.toLowerCase()}.`
  499. for (const m of cls.instance) lines.push(...renderMember(instancePrefix, m))
  500. if (cls.statics.length > 0) {
  501. lines.push('## Static members', '')
  502. for (const m of cls.statics) lines.push(...renderMember(`${section.symbol}.`, m))
  503. }
  504. } else {
  505. const decl = declPaste(section.file, section.symbol)
  506. lines.push(`## ${section.symbol}`, '')
  507. if (decl.doc) lines.push(...prose(decl.doc), '')
  508. lines.push('```' + FENCE, decl.code, '```', '', sourceLink(decl.source), '')
  509. }
  510. }
  511. return `${lines.join('\n').replace(/\n{3,}/g, '\n\n').trimEnd()}\n`
  512. }
  513. /** kebab-case a ctx key: `agentLoop` → `agent-loop`. */
  514. function kebab(key: string): string {
  515. return key.replace(/[A-Z]/g, c => `-${c.toLowerCase()}`)
  516. }
  517. /** Render one harness service page. */
  518. function renderServicePage(svc: HarnessService): string {
  519. const seam = svc.abstract ? ' (abstract seam)' : ''
  520. const lines: string[] = [
  521. BANNER, '',
  522. `# ctx.${svc.key}`, '',
  523. `\`${svc.type}\`${seam} — provided by \`${svc.pkg}\`.`, '',
  524. ...prose(svc.doc), '',
  525. sourceLink(svc.source), '',
  526. ]
  527. for (const m of svc.members) lines.push(...renderMember(`ctx.${svc.key}.`, m))
  528. return `${lines.join('\n').replace(/\n{3,}/g, '\n\n').trimEnd()}\n`
  529. }
  530. /** Render the harness events page, grouped by scope. */
  531. function renderEventsPage(events: HarnessEvent[]): string {
  532. const lines: string[] = [
  533. BANNER, '',
  534. '# Harness events', '',
  535. `Every event the harness packages declare on the cordis event bus (${events.length} total), grouped by scope. The **mode** is the dispatch semantics (\`emit\` fire-and-forget, \`parallel\` awaited, \`serial\` first-bail, \`waterfall\` veto-chain — a waterfall listener MUST call \`next()\` to delegate).`, '',
  536. ]
  537. const scopes = [...new Set(events.map(e => e.scope))].sort()
  538. for (const scope of scopes) {
  539. lines.push(`## ${scope}/*`, '')
  540. for (const e of events.filter(ev => ev.scope === scope)) {
  541. lines.push(`### ${e.name}`, '')
  542. lines.push(`**Mode:** \`${e.mode ?? 'unknown'}\``, '')
  543. lines.push('```' + FENCE, e.signature, '```', '')
  544. lines.push(...prose(e.doc), '')
  545. if (e.params.length > 0) {
  546. for (const p of e.params) lines.push(`- \`${p.name}\` — ${p.text}`)
  547. lines.push('')
  548. }
  549. lines.push(sourceLink(e.source), '')
  550. }
  551. }
  552. return `${lines.join('\n').replace(/\n{3,}/g, '\n\n').trimEnd()}\n`
  553. }
  554. // ---------------------------------------------------------------------------
  555. // Assembly + CLI
  556. // ---------------------------------------------------------------------------
  557. /** Build every generated file as `relPath → content`. */
  558. export function generate(): Map<string, string> {
  559. const violations: string[] = []
  560. const files = new Map<string, string>()
  561. for (const page of CORDIS_PAGES) {
  562. files.set(`${PAGES_DIR}/${page.out}`, renderCordisPage(page, violations))
  563. }
  564. const services = collectHarnessServices(violations)
  565. for (const svc of services) {
  566. files.set(`${PAGES_DIR}/harness/${kebab(svc.key)}.md`, renderServicePage(svc))
  567. }
  568. const events = collectHarnessEvents(violations)
  569. files.set(`${PAGES_DIR}/harness/events.md`, renderEventsPage(events))
  570. reportViolations('gen-website-api', violations)
  571. const sidebar = {
  572. cordis: CORDIS_PAGES.map(p => ({
  573. text: p.title,
  574. link: `/zh-CN/api/${p.out.replace(/\.md$/, '')}`,
  575. })),
  576. harness: [
  577. ...services.map(s => ({ text: `ctx.${s.key}`, link: `/zh-CN/api/harness/${kebab(s.key)}` })),
  578. { text: 'Events', link: '/zh-CN/api/harness/events' },
  579. ],
  580. }
  581. files.set(SIDEBAR_OUT, `${JSON.stringify(sidebar, null, 2)}\n`)
  582. return files
  583. }
  584. /** CLI entry: default writes, `--check` fails on stale/orphan files. Guarded
  585. * behind an entry-point check so tests can import `generate()`. */
  586. function main(): void {
  587. const check = process.argv.includes('--check')
  588. const files = generate()
  589. // Orphan detection: a generated-dir page that generate() no longer emits
  590. // (e.g. a service was renamed) must be deleted, not left to rot.
  591. const expected = new Set([...files.keys()])
  592. // Orphans live in the generated subdirs only; the hand-written api/index.md
  593. // is one level up and never matches this glob.
  594. const onDisk = globSync(`${PAGES_DIR}/{cordis,harness}/*.md`, { cwd: root }).sort()
  595. const orphans = onDisk.filter(rel => !expected.has(rel))
  596. if (check) {
  597. const stale: string[] = []
  598. for (const [rel, content] of files) {
  599. let current: string | null = null
  600. try {
  601. current = readFileSync(resolve(root, rel), 'utf8')
  602. } catch {
  603. // Missing file: reported as stale below; readFileSync is the probe.
  604. }
  605. if (current !== content) stale.push(rel)
  606. }
  607. if (stale.length > 0 || orphans.length > 0) {
  608. console.error('gen-website-api: website API reference is stale. Run `pnpm run gen-website-api` and commit the result.')
  609. for (const rel of stale) console.error(` stale: ${rel}`)
  610. for (const rel of orphans) console.error(` orphan (delete): ${rel}`)
  611. process.exit(1)
  612. }
  613. console.log(`gen-website-api: ${files.size} generated file(s) fresh.`)
  614. return
  615. }
  616. for (const [rel, content] of files) {
  617. const abs = resolve(root, rel)
  618. mkdirSync(dirname(abs), { recursive: true })
  619. writeFileSync(abs, content)
  620. }
  621. for (const rel of orphans) {
  622. console.log(`gen-website-api: orphan page ${rel} — delete it (no longer generated).`)
  623. }
  624. console.log(`gen-website-api: wrote ${files.size} file(s).`)
  625. }
  626. // Run only when invoked as a script, not when imported by a test.
  627. if (process.argv[1] && import.meta.filename === resolve(process.argv[1])) {
  628. main()
  629. }