gen-website-api.ts 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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; heading?: string }
  67. | { kind: 'context-merge'; file: string; heading?: 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', heading: 'Service store and mixins' },
  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', heading: 'The Fiber class' },
  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. /** Resolve an `extends Pick<Class, 'a' | 'b'>` heritage clause on the Context
  251. * merge to the named members of `Class` declared in the same file — the fiber
  252. * merge (`interface Context extends Pick<Fiber, 'effect'>`) is the motivating
  253. * case: without this, `ctx.effect` had no documented signature anywhere. */
  254. function heritageMembers(
  255. stmt: ts.InterfaceDeclaration,
  256. sf: ts.SourceFile,
  257. groups: Map<string, (ts.MethodSignature | ts.PropertySignature | ts.MethodDeclaration)[]>,
  258. ): void {
  259. for (const clause of stmt.heritageClauses ?? []) {
  260. for (const type of clause.types) {
  261. if (!ts.isIdentifier(type.expression) || type.expression.text !== 'Pick') continue
  262. const [target, keys] = type.typeArguments ?? []
  263. if (!target || !keys || !ts.isTypeReferenceNode(target)) continue
  264. const targetName = target.typeName.getText(sf)
  265. const cls = sf.statements.find(
  266. (s): s is ts.ClassDeclaration => ts.isClassDeclaration(s) && s.name?.text === targetName,
  267. )
  268. if (!cls) continue
  269. const picked = new Set<string>()
  270. const collect = (node: ts.TypeNode): void => {
  271. if (ts.isLiteralTypeNode(node) && ts.isStringLiteral(node.literal)) picked.add(node.literal.text)
  272. if (ts.isUnionTypeNode(node)) node.types.forEach(collect)
  273. }
  274. collect(keys)
  275. for (const member of cls.members) {
  276. if (!ts.isMethodDeclaration(member)) continue
  277. const name = member.name.getText(sf)
  278. if (!picked.has(name)) continue
  279. const group = groups.get(name) ?? []
  280. group.push(member)
  281. groups.set(name, group)
  282. }
  283. }
  284. }
  285. }
  286. /** Members of the `interface Context` merge in `rel`, overloads grouped;
  287. * `Pick<…>` heritage resolved to the picked class members. */
  288. function contextMergeMembers(rel: string, violations: string[]): MemberDoc[] {
  289. const { sf } = load(rel)
  290. const body = moduleBody(sf)
  291. if (!body) throw new Error(`gen-website-api: ${rel} has no context module merge`)
  292. const groups = new Map<string, (ts.MethodSignature | ts.PropertySignature | ts.MethodDeclaration)[]>()
  293. for (const stmt of body.statements) {
  294. if (!ts.isInterfaceDeclaration(stmt) || stmt.name.text !== 'Context') continue
  295. heritageMembers(stmt, sf, groups)
  296. for (const member of stmt.members) {
  297. if (!ts.isMethodSignature(member) && !ts.isPropertySignature(member)) continue
  298. if (ts.isComputedPropertyName(member.name)) continue
  299. const name = member.name.getText(sf)
  300. const group = groups.get(name) ?? []
  301. group.push(member)
  302. groups.set(name, group)
  303. }
  304. }
  305. return [...groups.entries()].map(([name, group]) =>
  306. memberDoc(`ctx.${name} (${rel})`, name, group, rel, violations))
  307. }
  308. /** Instance + static members of one class, as two rendered lists. The class's
  309. * same-named top-level interface half (declaration merging — vendor Context
  310. * declares `root`/`events`/`logger`/… on the interface) is folded into the
  311. * instance list, so neither half of a merged symbol goes undocumented. */
  312. function classMembers(rel: string, className: string, violations: string[]): {
  313. doc: string
  314. instance: MemberDoc[]
  315. statics: MemberDoc[]
  316. source: string
  317. } {
  318. const { sf, text } = load(rel)
  319. const cls = sf.statements.find(
  320. (s): s is ts.ClassDeclaration => ts.isClassDeclaration(s) && s.name?.text === className,
  321. )
  322. if (!cls) throw new Error(`gen-website-api: class ${className} not found in ${rel}`)
  323. const clsDoc = parseJsDoc(rawJsDoc(text, cls)).doc
  324. if (!clsDoc) violations.push(`class ${className} (${pointer(rel, sf, cls)}) has no JSDoc.`)
  325. type Renderable = ts.MethodDeclaration | ts.PropertyDeclaration | ts.GetAccessorDeclaration | ts.PropertySignature
  326. const instance = new Map<string, Renderable[]>()
  327. const statics = new Map<string, (ts.MethodDeclaration | ts.PropertyDeclaration)[]>()
  328. for (const member of cls.members) {
  329. const renderable = ts.isMethodDeclaration(member) || ts.isPropertyDeclaration(member) || ts.isGetAccessorDeclaration(member)
  330. if (!renderable) continue
  331. const name = member.name.getText(sf)
  332. if (isPublicInstance(member)) {
  333. const group = instance.get(name) ?? []
  334. group.push(member)
  335. instance.set(name, group)
  336. } else if (isPublicStatic(member) && !ts.isGetAccessorDeclaration(member)) {
  337. const group = statics.get(name) ?? []
  338. group.push(member)
  339. statics.set(name, group)
  340. }
  341. }
  342. const iface = sf.statements.find(
  343. (s): s is ts.InterfaceDeclaration => ts.isInterfaceDeclaration(s) && s.name.text === className,
  344. )
  345. for (const member of iface?.members ?? []) {
  346. if (!ts.isPropertySignature(member)) continue
  347. if (ts.isComputedPropertyName(member.name)) continue
  348. const name = member.name.getText(sf)
  349. const group = instance.get(name) ?? []
  350. group.push(member)
  351. instance.set(name, group)
  352. }
  353. const toDocs = (groups: Map<string, Renderable[]>, prefix: string): MemberDoc[] =>
  354. [...groups.entries()].map(([name, group]) =>
  355. memberDoc(`${prefix}${name} (${rel})`, name, group, rel, violations))
  356. return {
  357. doc: clsDoc,
  358. instance: toDocs(instance, `${className}#`),
  359. statics: toDocs(statics, `${className}.`),
  360. source: pointer(rel, sf, cls),
  361. }
  362. }
  363. /** Splice every function-like BODY out of a declaration's text, leaving the
  364. * signature (`) {` → `)`). A reference paste shows shapes, not implementation;
  365. * property initializers (e.g. an `as const` code table) are data and stay. */
  366. function stripBodies(node: ts.Node, sf: ts.SourceFile): string {
  367. const cuts: { start: number; end: number }[] = []
  368. const visit = (n: ts.Node): void => {
  369. const funcLike = ts.isMethodDeclaration(n) || ts.isConstructorDeclaration(n)
  370. || ts.isFunctionDeclaration(n) || ts.isGetAccessorDeclaration(n) || ts.isSetAccessorDeclaration(n)
  371. if (funcLike && n.body) {
  372. // Cut from just after the parameter close (or return-type end) through
  373. // the body, so `foo(a: string) { … }` renders as `foo(a: string)`.
  374. const sigEnd = (n.type ?? n.parameters[n.parameters.length - 1] ?? n).getEnd()
  375. // Find the `)` (and optional `: Type`) boundary: body start is exact.
  376. cuts.push({ start: sigEnd, end: n.body.getEnd() })
  377. return // nothing renderable inside the body
  378. }
  379. n.forEachChild(visit)
  380. }
  381. visit(node)
  382. const base = node.getStart(sf)
  383. let out = node.getText(sf)
  384. for (const cut of cuts.sort((a, b) => b.start - a.start)) {
  385. const head = out.slice(0, cut.start - base)
  386. // Keep everything of the signature up to the closing paren / return type,
  387. // drop ` { … }`. The head may end mid-signature (last param), so retain
  388. // the source between sigEnd and the body's `{` MINUS trailing space.
  389. const between = out.slice(cut.start - base, cut.end - base)
  390. const bodyBrace = between.indexOf('{')
  391. out = head + between.slice(0, bodyBrace).trimEnd() + out.slice(cut.end - base)
  392. }
  393. return out
  394. }
  395. /** Verbatim declaration paste: every top-level statement named `symbol`
  396. * (class + merged namespace both), with leading JSDoc prose extracted and
  397. * function bodies stripped (a reference shows shapes, not implementation). */
  398. function declPaste(rel: string, symbol: string): { doc: string; code: string; source: string } {
  399. const { sf, text } = load(rel)
  400. const matches = sf.statements.filter((s) => {
  401. const named = ts.isInterfaceDeclaration(s) || ts.isTypeAliasDeclaration(s)
  402. || ts.isClassDeclaration(s) || ts.isEnumDeclaration(s) || ts.isModuleDeclaration(s)
  403. return named && s.name?.getText(sf) === symbol
  404. })
  405. if (matches.length === 0) throw new Error(`gen-website-api: declaration ${symbol} not found in ${rel}`)
  406. const first = matches[0]
  407. if (!first) throw new Error(`gen-website-api: declaration ${symbol} not found in ${rel}`)
  408. const doc = parseJsDoc(rawJsDoc(text, first)).doc
  409. const code = matches.map(s => stripBodies(s, sf).replace(/^export\s+(default\s+)?/, '')).join('\n\n')
  410. return { doc, code, source: pointer(rel, sf, first) }
  411. }
  412. /** One harness service with member-level detail. */
  413. interface HarnessService {
  414. key: string
  415. type: string
  416. abstract: boolean
  417. doc: string
  418. members: MemberDoc[]
  419. source: string
  420. /** Owning npm package name (from the package.json beside the entry). */
  421. pkg: string
  422. }
  423. /** Walk every harness `declare module 'cordis'` Context merge → services. */
  424. function collectHarnessServices(violations: string[]): HarnessService[] {
  425. const services: HarnessService[] = []
  426. for (const rel of globSync('packages/*/*/src/index.ts', { cwd: root }).sort()) {
  427. const { sf, text } = load(rel)
  428. if (!text.includes('interface Context')) continue
  429. const body = moduleBody(sf)
  430. if (!body) continue
  431. const keyToType = new Map<string, string>()
  432. for (const stmt of body.statements) {
  433. if (!ts.isInterfaceDeclaration(stmt) || stmt.name.text !== 'Context') continue
  434. for (const member of stmt.members) {
  435. if (!ts.isPropertySignature(member) || !member.type) continue
  436. keyToType.set(member.name.getText(sf), member.type.getText(sf))
  437. }
  438. }
  439. const pkgJson = rel.replace(/src\/index\.ts$/, 'package.json')
  440. const pkg = (JSON.parse(readFileSync(resolve(root, pkgJson), 'utf8')) as { name: string }).name
  441. for (const [key, type] of keyToType) {
  442. const cls = sf.statements.find(
  443. (s): s is ts.ClassDeclaration => ts.isClassDeclaration(s) && s.name?.text === type,
  444. )
  445. if (!cls) continue // a Pick-mixin member, not a class here
  446. const abstract = cls.modifiers?.some(m => m.kind === ts.SyntaxKind.AbstractKeyword) ?? false
  447. const clsDoc = parseJsDoc(rawJsDoc(text, cls)).doc
  448. if (!clsDoc) violations.push(`service ctx.${key} (${pointer(rel, sf, cls)}): class ${type} has no JSDoc.`)
  449. const groups = new Map<string, (ts.MethodDeclaration | ts.PropertyDeclaration | ts.GetAccessorDeclaration)[]>()
  450. for (const member of cls.members) {
  451. // Public properties are API too: ctx.codeRuntime.language/isolation
  452. // are readonly descriptors consumers key presentation off.
  453. const renderable = ts.isMethodDeclaration(member) || ts.isPropertyDeclaration(member) || ts.isGetAccessorDeclaration(member)
  454. if (!renderable) continue
  455. if (!isPublicInstance(member)) continue
  456. const name = member.name.getText(sf)
  457. const group = groups.get(name) ?? []
  458. group.push(member)
  459. groups.set(name, group)
  460. }
  461. const members = [...groups.entries()].map(([name, group]) =>
  462. memberDoc(`ctx.${key}.${name} (${rel})`, name, group, rel, violations))
  463. services.push({ key, type, abstract, doc: clsDoc, members, source: pointer(rel, sf, cls), pkg })
  464. }
  465. }
  466. return services.sort((a, b) => a.key.localeCompare(b.key))
  467. }
  468. /** One harness event with member-level detail. */
  469. interface HarnessEvent {
  470. name: string
  471. scope: string
  472. mode: Mode | null
  473. signature: string
  474. doc: string
  475. params: { name: string; text: string }[]
  476. source: string
  477. }
  478. /** Walk every harness `interface Events` merge → events. */
  479. function collectHarnessEvents(violations: string[]): HarnessEvent[] {
  480. const events: HarnessEvent[] = []
  481. for (const rel of globSync('packages/*/*/src/*.ts', { cwd: root }).sort()) {
  482. const { sf, text } = load(rel)
  483. if (!text.includes('interface Events')) continue
  484. const body = moduleBody(sf)
  485. if (!body) continue
  486. for (const stmt of body.statements) {
  487. if (!ts.isInterfaceDeclaration(stmt) || stmt.name.text !== 'Events') continue
  488. for (const member of stmt.members) {
  489. if (!ts.isMethodSignature(member)) continue
  490. const name = ts.isStringLiteral(member.name) ? member.name.text : member.name.getText(sf)
  491. const raw = rawJsDoc(text, member)
  492. const { doc, mode } = parseJsDoc(raw)
  493. if (!mode) violations.push(`event '${name}' (${pointer(rel, sf, member)}) is missing @mode.`)
  494. if (!doc) violations.push(`event '${name}' (${pointer(rel, sf, member)}) has no JSDoc prose.`)
  495. const { params: tags } = parseTags(raw)
  496. const last = member.parameters.at(-1)
  497. const hasNext = !!last && last.name.getText(sf) === 'next'
  498. checkParams(`event '${name}' (${pointer(rel, sf, member)})`, 'website-api', member.parameters, tags, sf,
  499. p => (ts.isIdentifier(p.name) && p.name.text === 'this') || (hasNext && p === last), violations)
  500. const params: { name: string; text: string }[] = []
  501. for (const p of member.parameters) {
  502. const pname = p.name.getText(sf)
  503. const tag = tags.get(pname)
  504. if (tag) params.push({ name: pname, text: tag })
  505. }
  506. events.push({ name, scope: name.split('/')[0] ?? name, mode, signature: signatureOf(member, sf), doc, params, source: pointer(rel, sf, member) })
  507. }
  508. }
  509. }
  510. return events.sort((a, b) => a.name.localeCompare(b.name))
  511. }
  512. // ---------------------------------------------------------------------------
  513. // Rendering
  514. // ---------------------------------------------------------------------------
  515. const BANNER = '<!-- Generated by scripts/gen-website-api.ts — do not edit by hand. Run `pnpm run gen-website-api` to regenerate. -->'
  516. /** GitHub source link for a `file:line` pointer. */
  517. function sourceLink(source: string): string {
  518. const [file, line] = source.split(':')
  519. return `[Source](${GITHUB}/${file}#L${line})`
  520. }
  521. /** Normalize JSDoc inline `{@link X}` / `{@link X|label}` / `{@link X label}`
  522. * tags to plain Markdown code spans — left verbatim they leak into the built
  523. * page as literal `{@link …}` text. */
  524. function unlink(text: string): string {
  525. return text.replace(/\{@link\s+([^}|\s]+)\s*(?:[|\s]\s*([^}]*))?\}/g, (_m, target: string, label?: string) => {
  526. const name = label?.trim()
  527. return name && name !== '' ? name : `\`${target}\``
  528. })
  529. }
  530. /** Render prose paragraphs (one per line of `doc`), JSDoc links normalized. */
  531. function prose(doc: string): string[] {
  532. return unlink(doc).split('\n').filter(l => l.trim() !== '')
  533. }
  534. /** Render one member section at heading depth 3. */
  535. function renderMember(prefix: string, m: MemberDoc): string[] {
  536. const lines: string[] = []
  537. const call = m.heading === '' ? '' : m.heading
  538. lines.push(`### ${prefix}${m.name}${call}`, '')
  539. lines.push('```' + FENCE)
  540. for (const sig of m.signatures) lines.push(sig)
  541. lines.push('```', '')
  542. lines.push(...prose(m.doc), '')
  543. if (m.params.length > 0) {
  544. for (const p of m.params) lines.push(`- \`${p.name}\` — ${unlink(p.text)}`)
  545. lines.push('')
  546. }
  547. if (m.returns) lines.push(`**Returns** ${unlink(m.returns)}`, '')
  548. lines.push(sourceLink(m.source), '')
  549. return lines
  550. }
  551. /** Render one cordis-tier page from its manifest entry. */
  552. function renderCordisPage(page: CordisPage, violations: string[]): string {
  553. const lines: string[] = [BANNER, '', `# ${page.title}`, '', page.intro, '']
  554. for (const section of page.sections) {
  555. if (section.kind !== 'decl' && section.heading) lines.push(`## ${section.heading}`, '')
  556. if (section.kind === 'context-merge') {
  557. for (const m of contextMergeMembers(section.file, violations)) {
  558. lines.push(...renderMember('ctx.', m))
  559. }
  560. } else if (section.kind === 'class') {
  561. const cls = classMembers(section.file, section.symbol, violations)
  562. lines.push(...prose(cls.doc), '', sourceLink(cls.source), '')
  563. const instancePrefix = section.prefix ?? `${section.symbol.toLowerCase()}.`
  564. for (const m of cls.instance) lines.push(...renderMember(instancePrefix, m))
  565. if (cls.statics.length > 0) {
  566. lines.push('## Static members', '')
  567. for (const m of cls.statics) lines.push(...renderMember(`${section.symbol}.`, m))
  568. }
  569. } else {
  570. const decl = declPaste(section.file, section.symbol)
  571. lines.push(`## ${section.symbol}`, '')
  572. if (decl.doc) lines.push(...prose(decl.doc), '')
  573. lines.push('```' + FENCE, decl.code, '```', '', sourceLink(decl.source), '')
  574. }
  575. }
  576. return `${lines.join('\n').replace(/\n{3,}/g, '\n\n').trimEnd()}\n`
  577. }
  578. /** kebab-case a ctx key: `agentLoop` → `agent-loop`. */
  579. function kebab(key: string): string {
  580. return key.replace(/[A-Z]/g, c => `-${c.toLowerCase()}`)
  581. }
  582. /** Render one harness service page. */
  583. function renderServicePage(svc: HarnessService): string {
  584. const seam = svc.abstract ? ' (abstract seam)' : ''
  585. const lines: string[] = [
  586. BANNER, '',
  587. `# ctx.${svc.key}`, '',
  588. `\`${svc.type}\`${seam} — provided by \`${svc.pkg}\`.`, '',
  589. ...prose(svc.doc), '',
  590. sourceLink(svc.source), '',
  591. ]
  592. for (const m of svc.members) lines.push(...renderMember(`ctx.${svc.key}.`, m))
  593. return `${lines.join('\n').replace(/\n{3,}/g, '\n\n').trimEnd()}\n`
  594. }
  595. /** Render the harness events page, grouped by scope. */
  596. function renderEventsPage(events: HarnessEvent[]): string {
  597. const lines: string[] = [
  598. BANNER, '',
  599. '# Harness events', '',
  600. `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).`, '',
  601. ]
  602. const scopes = [...new Set(events.map(e => e.scope))].sort()
  603. for (const scope of scopes) {
  604. lines.push(`## ${scope}/*`, '')
  605. for (const e of events.filter(ev => ev.scope === scope)) {
  606. lines.push(`### ${e.name}`, '')
  607. lines.push(`**Mode:** \`${e.mode ?? 'unknown'}\``, '')
  608. lines.push('```' + FENCE, e.signature, '```', '')
  609. lines.push(...prose(e.doc), '')
  610. if (e.params.length > 0) {
  611. for (const p of e.params) lines.push(`- \`${p.name}\` — ${unlink(p.text)}`)
  612. lines.push('')
  613. }
  614. lines.push(sourceLink(e.source), '')
  615. }
  616. }
  617. return `${lines.join('\n').replace(/\n{3,}/g, '\n\n').trimEnd()}\n`
  618. }
  619. // ---------------------------------------------------------------------------
  620. // Assembly + CLI
  621. // ---------------------------------------------------------------------------
  622. /** Build every generated file as `relPath → content`. */
  623. export function generate(): Map<string, string> {
  624. const violations: string[] = []
  625. const files = new Map<string, string>()
  626. for (const page of CORDIS_PAGES) {
  627. files.set(`${PAGES_DIR}/${page.out}`, renderCordisPage(page, violations))
  628. }
  629. const services = collectHarnessServices(violations)
  630. for (const svc of services) {
  631. files.set(`${PAGES_DIR}/harness/${kebab(svc.key)}.md`, renderServicePage(svc))
  632. }
  633. const events = collectHarnessEvents(violations)
  634. files.set(`${PAGES_DIR}/harness/events.md`, renderEventsPage(events))
  635. reportViolations('gen-website-api', violations)
  636. const sidebar = {
  637. cordis: CORDIS_PAGES.map(p => ({
  638. text: p.title,
  639. link: `/zh-CN/api/${p.out.replace(/\.md$/, '')}`,
  640. })),
  641. harness: [
  642. ...services.map(s => ({ text: `ctx.${s.key}`, link: `/zh-CN/api/harness/${kebab(s.key)}` })),
  643. { text: 'Events', link: '/zh-CN/api/harness/events' },
  644. ],
  645. }
  646. files.set(SIDEBAR_OUT, `${JSON.stringify(sidebar, null, 2)}\n`)
  647. return files
  648. }
  649. /** CLI entry: default writes, `--check` fails on stale/orphan files. Guarded
  650. * behind an entry-point check so tests can import `generate()`. */
  651. function main(): void {
  652. const check = process.argv.includes('--check')
  653. const files = generate()
  654. // Orphan detection: a generated-dir page that generate() no longer emits
  655. // (e.g. a service was renamed) must be deleted, not left to rot.
  656. const expected = new Set([...files.keys()])
  657. // Orphans live in the generated subdirs only; the hand-written api/index.md
  658. // is one level up and never matches this glob.
  659. const onDisk = globSync(`${PAGES_DIR}/{cordis,harness}/*.md`, { cwd: root }).sort()
  660. const orphans = onDisk.filter(rel => !expected.has(rel))
  661. if (check) {
  662. const stale: string[] = []
  663. for (const [rel, content] of files) {
  664. let current: string | null = null
  665. try {
  666. current = readFileSync(resolve(root, rel), 'utf8')
  667. } catch {
  668. // Missing file: reported as stale below; readFileSync is the probe.
  669. }
  670. if (current !== content) stale.push(rel)
  671. }
  672. if (stale.length > 0 || orphans.length > 0) {
  673. console.error('gen-website-api: website API reference is stale. Run `pnpm run gen-website-api` and commit the result.')
  674. for (const rel of stale) console.error(` stale: ${rel}`)
  675. for (const rel of orphans) console.error(` orphan (delete): ${rel}`)
  676. process.exit(1)
  677. }
  678. console.log(`gen-website-api: ${files.size} generated file(s) fresh.`)
  679. return
  680. }
  681. for (const [rel, content] of files) {
  682. const abs = resolve(root, rel)
  683. mkdirSync(dirname(abs), { recursive: true })
  684. writeFileSync(abs, content)
  685. }
  686. for (const rel of orphans) {
  687. console.log(`gen-website-api: orphan page ${rel} — delete it (no longer generated).`)
  688. }
  689. console.log(`gen-website-api: wrote ${files.size} file(s).`)
  690. }
  691. // Run only when invoked as a script, not when imported by a test.
  692. if (process.argv[1] && import.meta.filename === resolve(process.argv[1])) {
  693. main()
  694. }