build.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #!/usr/bin/env bun
  2. import { fileURLToPath } from "url"
  3. const dir = fileURLToPath(new URL("..", import.meta.url))
  4. process.chdir(dir)
  5. import { $ } from "bun"
  6. import path from "path"
  7. import { createClient } from "@hey-api/openapi-ts"
  8. const opencode = path.resolve(dir, "../../opencode")
  9. const client = path.resolve(dir, "../../client")
  10. await $`bun dev generate > ${dir}/openapi.json`.cwd(opencode)
  11. await $`bun -e ${`
  12. import { OpenApi } from "effect/unstable/httpapi"
  13. import { ClientApi } from "@opencode-ai/protocol/client"
  14. const output = process.argv.at(-1)
  15. if (!output) throw new Error("Missing OpenAPI output path")
  16. await Bun.write(output, JSON.stringify(OpenApi.fromApi(ClientApi)))
  17. `} ${path.join(dir, "openapi-v2.json")}`.cwd(client)
  18. type OpenApiDocument = {
  19. components?: { schemas?: Record<string, unknown> }
  20. paths?: Record<string, unknown>
  21. [key: string]: unknown
  22. }
  23. const document = (await Bun.file("./openapi.json").json()) as OpenApiDocument
  24. const v2Document = (await Bun.file("./openapi-v2.json").json()) as OpenApiDocument
  25. normalizeComponentNames(v2Document)
  26. deduplicateEquivalentComponent(v2Document, "Shell", "Shell1")
  27. renameCollidingComponents(document, v2Document)
  28. document.paths = { ...document.paths, ...v2Document.paths }
  29. document.components = {
  30. ...document.components,
  31. schemas: { ...document.components?.schemas, ...v2Document.components?.schemas },
  32. }
  33. inlineTypedAllOfConstraints(document)
  34. const schemas = document.components?.schemas
  35. if (schemas) {
  36. const reachable = new Set<string>()
  37. const visit = (value: unknown) => {
  38. if (Array.isArray(value)) {
  39. value.forEach(visit)
  40. return
  41. }
  42. if (typeof value !== "object" || value === null) return
  43. for (const [key, child] of Object.entries(value)) {
  44. if (key === "$ref" && typeof child === "string" && child.startsWith("#/components/schemas/")) {
  45. const name = child.slice("#/components/schemas/".length)
  46. if (reachable.has(name)) continue
  47. reachable.add(name)
  48. visit(schemas[name])
  49. } else {
  50. visit(child)
  51. }
  52. }
  53. }
  54. visit({ ...document, components: { ...document.components, schemas: undefined } })
  55. for (const name of Object.keys(schemas)) {
  56. if (
  57. /^(SessionAgentSelected|SessionModelSelected|SessionMoved|SessionRenamed|SessionForked|SessionInputPromoted|SessionInputAdmitted|SessionExecutionStarted|SessionExecutionSucceeded|SessionExecutionFailed|SessionExecutionInterrupted|SessionInstructionsUpdated|SessionSynthetic|SessionSkillActivated|SessionShellStarted|SessionShellEnded|SessionStepStarted|SessionStepEnded|SessionStepFailed|SessionTextStarted|SessionTextDelta|SessionTextEnded|SessionToolInputStarted|SessionToolInputDelta|SessionToolInputEnded|SessionToolCalled|SessionToolProgress|SessionToolSuccess|SessionToolFailed|SessionRetryScheduled|SessionCompactionStarted|SessionCompactionDelta|SessionCompactionEnded|SessionRevertStaged|SessionRevertCleared|SessionRevertCommitted)\d+$/.test(
  58. name,
  59. ) &&
  60. !reachable.has(name)
  61. )
  62. delete schemas[name]
  63. }
  64. await Bun.write("./openapi.json", JSON.stringify(document))
  65. }
  66. await createClient({
  67. input: "./openapi.json",
  68. output: {
  69. path: "./src/v2/gen",
  70. tsConfigPath: path.join(dir, "tsconfig.json"),
  71. clean: true,
  72. },
  73. plugins: [
  74. {
  75. name: "@hey-api/typescript",
  76. exportFromIndex: false,
  77. },
  78. {
  79. name: "@hey-api/sdk",
  80. instance: "OpencodeClient",
  81. exportFromIndex: false,
  82. auth: false,
  83. paramsStructure: "flat",
  84. },
  85. {
  86. name: "@hey-api/client-fetch",
  87. exportFromIndex: false,
  88. baseUrl: "http://localhost:4096",
  89. },
  90. ],
  91. })
  92. const generatedTypesPath = "./src/v2/gen/types.gen.ts"
  93. const generatedTypes = await Bun.file(generatedTypesPath).text()
  94. if (
  95. /export type (SessionAgentSelected|SessionModelSelected|SessionMoved|SessionRenamed|SessionForked|SessionInputPromoted|SessionInputAdmitted|SessionExecutionStarted|SessionExecutionSucceeded|SessionExecutionFailed|SessionExecutionInterrupted|SessionInstructionsUpdated|SessionSynthetic|SessionSkillActivated|SessionShellStarted|SessionShellEnded|SessionStepStarted|SessionStepEnded|SessionStepFailed|SessionTextStarted|SessionTextDelta|SessionTextEnded|SessionToolInputStarted|SessionToolInputDelta|SessionToolInputEnded|SessionToolCalled|SessionToolProgress|SessionToolSuccess|SessionToolFailed|SessionRetryScheduled|SessionCompactionStarted|SessionCompactionDelta|SessionCompactionEnded|SessionRevertStaged|SessionRevertCleared|SessionRevertCommitted)\d+ =/.test(
  96. generatedTypes,
  97. )
  98. ) {
  99. throw new Error("Session history generated duplicate Session event variants")
  100. }
  101. const sessionErrorTypesPatched = deduplicateEquivalentGeneratedTypes(
  102. generatedTypes,
  103. "SessionStructuredError",
  104. /^SessionStructuredError\d+$/,
  105. )
  106. const obsoleteSessionNext = [...sessionErrorTypesPatched.matchAll(/export type (SessionNext\w*) =/g)].map(
  107. (match) => match[1],
  108. )
  109. if (obsoleteSessionNext.length > 0) {
  110. throw new Error(`Obsolete SessionNext generated type noise reintroduced: ${obsoleteSessionNext.join(", ")}`)
  111. }
  112. const logTypesPatched = sessionErrorTypesPatched.replace(
  113. /(export type V2SessionLogData = \{[\s\S]*?query\?: \{\s*after\?: )string/,
  114. "$1number",
  115. )
  116. if (logTypesPatched === sessionErrorTypesPatched) {
  117. throw new Error("Session log numeric query patch did not apply")
  118. }
  119. const sessionListTypesPatched = logTypesPatched.replace(
  120. /(export type V2SessionListData = \{[\s\S]*?query\?: \{[\s\S]*?limit\?: )string( \| null)/,
  121. "$1number$2",
  122. )
  123. if (sessionListTypesPatched === logTypesPatched) {
  124. throw new Error("Session list numeric query patch did not apply")
  125. }
  126. const sessionMessagesTypesPatched = sessionListTypesPatched.replace(
  127. /(export type V2MessageListData = \{[\s\S]*?query\?: \{[\s\S]*?limit\?: )string( \| null)/,
  128. "$1number$2",
  129. )
  130. if (sessionMessagesTypesPatched === sessionListTypesPatched) {
  131. throw new Error("Session messages numeric query patch did not apply")
  132. }
  133. const eventSubscribeTypesPatched = sessionMessagesTypesPatched.replace(
  134. /(export type V2EventSubscribeResponses = \{\s*\/\*\*[\s\S]*?\*\/\s*200: )\{\s*id: string \| null;?\s*event: string;?\s*data: V2EventStream(?:V2)?;?\s*\};?/,
  135. "$1V2Event",
  136. )
  137. if (eventSubscribeTypesPatched === sessionMessagesTypesPatched) {
  138. throw new Error("Event subscribe response patch did not apply")
  139. }
  140. if (/SessionStructuredError\d/.test(eventSubscribeTypesPatched)) {
  141. throw new Error("Session structured error generated a name-mangled duplicate")
  142. }
  143. if (/\bSessionNext\w*\b/.test(eventSubscribeTypesPatched)) {
  144. throw new Error("Obsolete SessionNext generated type noise reintroduced")
  145. }
  146. if (/export type Shell\d+V2 =/.test(eventSubscribeTypesPatched)) {
  147. throw new Error("Shell generated a name-mangled duplicate")
  148. }
  149. await Bun.write(generatedTypesPath, eventSubscribeTypesPatched)
  150. const querySerializerPath = "./src/v2/gen/client/utils.gen.ts"
  151. const querySerializerSource = await Bun.file(querySerializerPath).text()
  152. const querySerializerPatched = querySerializerSource.replace(
  153. /if \(value === undefined \|\| value === null\) \{\s*continue;?\s*\}/,
  154. "if (value === undefined) {\n continue;\n }\n\n if (value === null) {\n search.push(`${name}=null`);\n continue;\n }",
  155. )
  156. if (querySerializerPatched === querySerializerSource) {
  157. throw new Error(
  158. `Query serializer null patch did not apply; @hey-api/openapi-ts output may have changed (${querySerializerPath})`,
  159. )
  160. }
  161. await Bun.write(querySerializerPath, querySerializerPatched)
  162. const generatedSdkPath = "./src/v2/gen/sdk.gen.ts"
  163. const generatedSdk = await Bun.file(generatedSdkPath).text()
  164. const logSdkPatched = generatedSdk.replace(
  165. /(Read the session log[\s\S]*?parameters: \{[\s\S]*?after\?: )string(\s*\|\s*null)?/,
  166. "$1number$2",
  167. )
  168. if (logSdkPatched === generatedSdk) {
  169. throw new Error("Session log numeric SDK patch did not apply")
  170. }
  171. const sessionListSdkPatched = logSdkPatched.replace(
  172. /(List sessions[\s\S]*?parameters\?: \{[\s\S]*?limit\?: )string( \| null)/,
  173. "$1number$2",
  174. )
  175. if (sessionListSdkPatched === logSdkPatched) {
  176. throw new Error("Session list numeric SDK patch did not apply")
  177. }
  178. const sessionMessagesSdkPatched = sessionListSdkPatched.replace(
  179. /(Get session messages[\s\S]*?parameters: \{[\s\S]*?limit\?: )string( \| null)/,
  180. "$1number$2",
  181. )
  182. if (sessionMessagesSdkPatched === sessionListSdkPatched) {
  183. throw new Error("Session messages numeric SDK patch did not apply")
  184. }
  185. await Bun.write(generatedSdkPath, sessionMessagesSdkPatched)
  186. // Patch a @hey-api/openapi-ts codegen bug: SseFn incorrectly passes the
  187. // endpoint's TError into the second generic of ServerSentEventsResult, which
  188. // is the AsyncGenerator's TReturn slot. Iterator return values have nothing
  189. // to do with HTTP errors, and any consumer that calls `.return()` or returns
  190. // from a mock generator gets type-checked against the wrong shape. Drop the
  191. // arg so TReturn defaults to void.
  192. const sseTypesPath = "./src/v2/gen/client/types.gen.ts"
  193. const sseTypesFile = Bun.file(sseTypesPath)
  194. const sseTypesSource = await sseTypesFile.text()
  195. const sseTypesPatched = sseTypesSource.replace(
  196. "=> Promise<ServerSentEventsResult<TData, TError>>",
  197. "=> Promise<ServerSentEventsResult<TData>>",
  198. )
  199. if (sseTypesPatched === sseTypesSource) {
  200. throw new Error(`SseFn patch did not apply; @hey-api/openapi-ts output may have changed (${sseTypesPath})`)
  201. }
  202. await Bun.write(sseTypesPath, sseTypesPatched)
  203. await $`bun prettier --write src/gen`
  204. await $`bun prettier --write src/v2`
  205. await $`rm -rf dist`
  206. await $`bun tsc`
  207. await $`rm openapi.json openapi-v2.json`
  208. function renameCollidingComponents(target: OpenApiDocument, source: OpenApiDocument) {
  209. const targetSchemas = target.components?.schemas
  210. const sourceSchemas = source.components?.schemas
  211. if (!targetSchemas || !sourceSchemas) return
  212. const renames = new Map<string, string>()
  213. for (const name of Object.keys(sourceSchemas)) {
  214. if (!Object.hasOwn(targetSchemas, name)) continue
  215. if (JSON.stringify(normalizeSchema(sourceSchemas[name])) === JSON.stringify(normalizeSchema(targetSchemas[name]))) {
  216. delete sourceSchemas[name]
  217. continue
  218. }
  219. let renamed = `${name}V2`
  220. let index = 2
  221. while (Object.hasOwn(targetSchemas, renamed) || Object.hasOwn(sourceSchemas, renamed)) {
  222. renamed = `${name}V2${index}`
  223. index++
  224. }
  225. renames.set(name, renamed)
  226. }
  227. if (renames.size === 0) return
  228. source.components = {
  229. ...source.components,
  230. schemas: Object.fromEntries(
  231. Object.entries(sourceSchemas).map(([name, schema]) => [renames.get(name) ?? name, rewriteRefs(schema, renames)]),
  232. ),
  233. }
  234. source.paths = rewriteRefs(source.paths, renames) as Record<string, unknown> | undefined
  235. }
  236. function normalizeComponentNames(document: OpenApiDocument) {
  237. const schemas = document.components?.schemas
  238. if (!schemas) return
  239. const canonical = new Map(Object.entries(schemas))
  240. const renames = new Map<string, string>()
  241. for (const name of Object.keys(schemas)) {
  242. const next = componentTypeName(name)
  243. if (next === name) continue
  244. const existing = canonical.get(next)
  245. if (existing !== undefined) {
  246. if (JSON.stringify(normalizeSchema(schemas[name])) !== JSON.stringify(normalizeSchema(existing))) continue
  247. renames.set(name, next)
  248. continue
  249. }
  250. renames.set(name, next)
  251. canonical.set(next, schemas[name])
  252. }
  253. if (renames.size === 0) return
  254. const renamed = new Set<string>()
  255. document.components = {
  256. ...document.components,
  257. schemas: Object.fromEntries(
  258. [
  259. ...Object.entries(schemas).filter(([name]) => !renames.has(name)),
  260. ...Object.entries(schemas).flatMap(([name, schema]) => {
  261. const next = renames.get(name)
  262. if (!next || Object.hasOwn(schemas, next) || renamed.has(next)) return []
  263. renamed.add(next)
  264. return [[next, schema] as const]
  265. }),
  266. ].map(([name, schema]) => [name, rewriteRefs(schema, renames)]),
  267. ),
  268. }
  269. document.paths = rewriteRefs(document.paths, renames) as Record<string, unknown> | undefined
  270. }
  271. function componentTypeName(name: string) {
  272. if (!name.includes(".")) return name
  273. return name
  274. .split(".")
  275. .filter((part) => !/^\d+$/.test(part))
  276. .map((part) => part.slice(0, 1).toUpperCase() + part.slice(1))
  277. .join("")
  278. }
  279. function deduplicateEquivalentComponent(document: OpenApiDocument, canonical: string, duplicate: string) {
  280. const schemas = document.components?.schemas
  281. if (!schemas?.[canonical] || !schemas[duplicate]) return
  282. if (JSON.stringify(normalizeSchema(schemas[canonical])) !== JSON.stringify(normalizeSchema(schemas[duplicate]))) {
  283. throw new Error(`${duplicate} no longer has the same wire shape as ${canonical}`)
  284. }
  285. const renames = new Map([[duplicate, canonical]])
  286. const rewritten = rewriteRefs(schemas, renames) as Record<string, unknown>
  287. delete rewritten[duplicate]
  288. document.components = { ...document.components, schemas: rewritten }
  289. document.paths = rewriteRefs(document.paths, renames) as Record<string, unknown> | undefined
  290. }
  291. function deduplicateEquivalentGeneratedTypes(source: string, canonical: string, duplicates: RegExp) {
  292. const canonicalType = generatedType(source, canonical)
  293. if (!canonicalType) throw new Error(`Generated canonical type missing: ${canonical}`)
  294. const names = [...source.matchAll(/export type (\w+) =/g)]
  295. .map((match) => match[1])
  296. .filter((name): name is string => name !== undefined && duplicates.test(name))
  297. return names.reduce((patched, name) => {
  298. const duplicate = generatedType(patched, name)
  299. const currentCanonical = generatedType(patched, canonical)
  300. if (!duplicate || !currentCanonical) throw new Error(`Generated type declaration missing while comparing ${name}`)
  301. if (normalizeGeneratedType(currentCanonical.shape) !== normalizeGeneratedType(duplicate.shape)) {
  302. throw new Error(`${name} no longer has the same generated type shape as ${canonical}`)
  303. }
  304. return (patched.slice(0, duplicate.start) + patched.slice(duplicate.end)).replaceAll(name, canonical)
  305. }, source)
  306. }
  307. function generatedType(source: string, name: string) {
  308. const start = source.indexOf(`export type ${name} =`)
  309. if (start === -1) return undefined
  310. const next = source.indexOf("\n\nexport type ", start + 1)
  311. const shapeEnd = next === -1 ? source.length : next
  312. return {
  313. start,
  314. end: next === -1 ? source.length : next + 2,
  315. shape: source.slice(source.indexOf("=", start) + 1, shapeEnd),
  316. }
  317. }
  318. function normalizeGeneratedType(shape: string) {
  319. return shape.replaceAll(/\s/g, "")
  320. }
  321. function normalizeSchema(value: unknown, key?: string): unknown {
  322. if (Array.isArray(value)) {
  323. const flattened =
  324. key === "anyOf"
  325. ? value.flatMap((item) =>
  326. typeof item === "object" && item !== null && Object.keys(item).length === 1 && "anyOf" in item
  327. ? Array.isArray(item.anyOf)
  328. ? item.anyOf
  329. : [item]
  330. : [item],
  331. )
  332. : value
  333. const expanded =
  334. key === "anyOf"
  335. ? flattened.flatMap((item) => {
  336. if (typeof item !== "object" || item === null || !("type" in item) || !("enum" in item)) return [item]
  337. if (Object.keys(item).some((property) => property !== "type" && property !== "enum")) return [item]
  338. if (!Array.isArray(item.enum)) return [item]
  339. return item.enum.map((member) => ({ type: item.type, enum: [member] }))
  340. })
  341. : flattened
  342. const normalized = expanded.map((item) => normalizeSchema(item))
  343. if (key !== "anyOf" && key !== "required" && key !== "enum") return normalized
  344. return [...new Map(normalized.map((item) => [JSON.stringify(item), item])).values()].sort((a, b) =>
  345. JSON.stringify(a).localeCompare(JSON.stringify(b)),
  346. )
  347. }
  348. if (typeof value !== "object" || value === null) return value
  349. return Object.fromEntries(
  350. Object.entries(value)
  351. .sort(([left], [right]) => left.localeCompare(right))
  352. .map(([property, child]) => [property, normalizeSchema(child, property)]),
  353. )
  354. }
  355. function rewriteRefs(value: unknown, renames: Map<string, string>): unknown {
  356. if (Array.isArray(value)) return value.map((item) => rewriteRefs(item, renames))
  357. if (typeof value !== "object" || value === null) return value
  358. return Object.fromEntries(
  359. Object.entries(value).map(([key, child]) => {
  360. if (key !== "$ref" || typeof child !== "string") return [key, rewriteRefs(child, renames)]
  361. const prefix = "#/components/schemas/"
  362. if (!child.startsWith(prefix)) return [key, child]
  363. return [key, `${prefix}${renames.get(child.slice(prefix.length)) ?? child.slice(prefix.length)}`]
  364. }),
  365. )
  366. }
  367. function inlineTypedAllOfConstraints(value: unknown): void {
  368. if (Array.isArray(value)) {
  369. value.forEach(inlineTypedAllOfConstraints)
  370. return
  371. }
  372. if (typeof value !== "object" || value === null) return
  373. const schema = value as { allOf?: unknown; type?: unknown; [key: string]: unknown }
  374. if (typeof schema.type === "string" && Array.isArray(schema.allOf) && schema.allOf.every(isConstraintSchema)) {
  375. for (const item of schema.allOf) Object.assign(schema, item)
  376. delete schema.allOf
  377. }
  378. Object.values(schema).forEach(inlineTypedAllOfConstraints)
  379. }
  380. function isConstraintSchema(value: unknown): value is Record<string, unknown> {
  381. if (typeof value !== "object" || value === null || Array.isArray(value)) return false
  382. return !Object.keys(value).some(
  383. (key) => key === "$ref" || key === "type" || key === "allOf" || key === "anyOf" || key === "oneOf",
  384. )
  385. }