gen-website-api.ts 32 KB

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