gen-website-api.ts 33 KB

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