gen-website-api.ts 32 KB

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