index.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import { BusEvent } from "@/bus/bus-event"
  2. import path from "path"
  3. import { $ } from "bun"
  4. import z from "zod"
  5. import { NamedError } from "@opencode-ai/util/error"
  6. import { Log } from "../util/log"
  7. import { iife } from "@/util/iife"
  8. import { Flag } from "../flag/flag"
  9. declare global {
  10. const OPENCODE_VERSION: string
  11. const OPENCODE_CHANNEL: string
  12. }
  13. export namespace Installation {
  14. const log = Log.create({ service: "installation" })
  15. export type Method = Awaited<ReturnType<typeof method>>
  16. export const Event = {
  17. Updated: BusEvent.define(
  18. "installation.updated",
  19. z.object({
  20. version: z.string(),
  21. }),
  22. ),
  23. UpdateAvailable: BusEvent.define(
  24. "installation.update-available",
  25. z.object({
  26. version: z.string(),
  27. }),
  28. ),
  29. }
  30. export const Info = z
  31. .object({
  32. version: z.string(),
  33. latest: z.string(),
  34. })
  35. .meta({
  36. ref: "InstallationInfo",
  37. })
  38. export type Info = z.infer<typeof Info>
  39. export async function info() {
  40. return {
  41. version: VERSION,
  42. latest: await latest(),
  43. }
  44. }
  45. export function isPreview() {
  46. return CHANNEL !== "latest"
  47. }
  48. export function isLocal() {
  49. return CHANNEL === "local"
  50. }
  51. export async function method() {
  52. if (process.execPath.includes(path.join(".opencode", "bin"))) return "curl"
  53. if (process.execPath.includes(path.join(".local", "bin"))) return "curl"
  54. const exec = process.execPath.toLowerCase()
  55. const checks = [
  56. {
  57. name: "npm" as const,
  58. command: () => $`npm list -g --depth=0`.throws(false).quiet().text(),
  59. },
  60. {
  61. name: "yarn" as const,
  62. command: () => $`yarn global list`.throws(false).quiet().text(),
  63. },
  64. {
  65. name: "pnpm" as const,
  66. command: () => $`pnpm list -g --depth=0`.throws(false).quiet().text(),
  67. },
  68. {
  69. name: "bun" as const,
  70. command: () => $`bun pm ls -g`.throws(false).quiet().text(),
  71. },
  72. {
  73. name: "brew" as const,
  74. command: () => $`brew list --formula opencode`.throws(false).quiet().text(),
  75. },
  76. {
  77. name: "scoop" as const,
  78. command: () => $`scoop list opencode`.throws(false).quiet().text(),
  79. },
  80. {
  81. name: "choco" as const,
  82. command: () => $`choco list --limit-output opencode`.throws(false).quiet().text(),
  83. },
  84. ]
  85. checks.sort((a, b) => {
  86. const aMatches = exec.includes(a.name)
  87. const bMatches = exec.includes(b.name)
  88. if (aMatches && !bMatches) return -1
  89. if (!aMatches && bMatches) return 1
  90. return 0
  91. })
  92. for (const check of checks) {
  93. const output = await check.command()
  94. const installedName =
  95. check.name === "brew" || check.name === "choco" || check.name === "scoop" ? "opencode" : "opencode-ai"
  96. if (output.includes(installedName)) {
  97. return check.name
  98. }
  99. }
  100. return "unknown"
  101. }
  102. export const UpgradeFailedError = NamedError.create(
  103. "UpgradeFailedError",
  104. z.object({
  105. stderr: z.string(),
  106. }),
  107. )
  108. async function getBrewFormula() {
  109. const tapFormula = await $`brew list --formula anomalyco/tap/opencode`.throws(false).quiet().text()
  110. if (tapFormula.includes("opencode")) return "anomalyco/tap/opencode"
  111. const coreFormula = await $`brew list --formula opencode`.throws(false).quiet().text()
  112. if (coreFormula.includes("opencode")) return "opencode"
  113. return "opencode"
  114. }
  115. export async function upgrade(method: Method, target: string) {
  116. let cmd
  117. switch (method) {
  118. case "curl":
  119. cmd = $`curl -fsSL https://opencode.ai/install | bash`.env({
  120. ...process.env,
  121. VERSION: target,
  122. })
  123. break
  124. case "npm":
  125. cmd = $`npm install -g opencode-ai@${target}`
  126. break
  127. case "pnpm":
  128. cmd = $`pnpm install -g opencode-ai@${target}`
  129. break
  130. case "bun":
  131. cmd = $`bun install -g opencode-ai@${target}`
  132. break
  133. case "brew": {
  134. const formula = await getBrewFormula()
  135. cmd = $`brew upgrade ${formula}`.env({
  136. HOMEBREW_NO_AUTO_UPDATE: "1",
  137. ...process.env,
  138. })
  139. break
  140. }
  141. case "choco":
  142. cmd = $`echo Y | choco upgrade opencode --version=${target}`
  143. break
  144. case "scoop":
  145. cmd = $`scoop install opencode@${target}`
  146. break
  147. default:
  148. throw new Error(`Unknown method: ${method}`)
  149. }
  150. const result = await cmd.quiet().throws(false)
  151. if (result.exitCode !== 0) {
  152. const stderr = method === "choco" ? "not running from an elevated command shell" : result.stderr.toString("utf8")
  153. throw new UpgradeFailedError({
  154. stderr: stderr,
  155. })
  156. }
  157. log.info("upgraded", {
  158. method,
  159. target,
  160. stdout: result.stdout.toString(),
  161. stderr: result.stderr.toString(),
  162. })
  163. await $`${process.execPath} --version`.nothrow().quiet().text()
  164. }
  165. export const VERSION = typeof OPENCODE_VERSION === "string" ? OPENCODE_VERSION : "local"
  166. export const CHANNEL = typeof OPENCODE_CHANNEL === "string" ? OPENCODE_CHANNEL : "local"
  167. export const USER_AGENT = `opencode/${CHANNEL}/${VERSION}/${Flag.OPENCODE_CLIENT}`
  168. export async function latest(installMethod?: Method) {
  169. const detectedMethod = installMethod || (await method())
  170. if (detectedMethod === "brew") {
  171. const formula = await getBrewFormula()
  172. if (formula === "opencode") {
  173. return fetch("https://formulae.brew.sh/api/formula/opencode.json")
  174. .then((res) => {
  175. if (!res.ok) throw new Error(res.statusText)
  176. return res.json()
  177. })
  178. .then((data: any) => data.versions.stable)
  179. }
  180. }
  181. if (detectedMethod === "npm" || detectedMethod === "bun" || detectedMethod === "pnpm") {
  182. const registry = await iife(async () => {
  183. const r = (await $`npm config get registry`.quiet().nothrow().text()).trim()
  184. const reg = r || "https://registry.npmjs.org"
  185. return reg.endsWith("/") ? reg.slice(0, -1) : reg
  186. })
  187. const channel = CHANNEL
  188. return fetch(`${registry}/opencode-ai/${channel}`)
  189. .then((res) => {
  190. if (!res.ok) throw new Error(res.statusText)
  191. return res.json()
  192. })
  193. .then((data: any) => data.version)
  194. }
  195. if (detectedMethod === "choco") {
  196. return fetch(
  197. "https://community.chocolatey.org/api/v2/Packages?$filter=Id%20eq%20%27opencode%27%20and%20IsLatestVersion&$select=Version",
  198. { headers: { Accept: "application/json;odata=verbose" } },
  199. )
  200. .then((res) => {
  201. if (!res.ok) throw new Error(res.statusText)
  202. return res.json()
  203. })
  204. .then((data: any) => data.d.results[0].Version)
  205. }
  206. if (detectedMethod === "scoop") {
  207. return fetch("https://raw.githubusercontent.com/ScoopInstaller/Main/master/bucket/opencode.json", {
  208. headers: { Accept: "application/json" },
  209. })
  210. .then((res) => {
  211. if (!res.ok) throw new Error(res.statusText)
  212. return res.json()
  213. })
  214. .then((data: any) => data.version)
  215. }
  216. return fetch("https://api.github.com/repos/anomalyco/opencode/releases/latest")
  217. .then((res) => {
  218. if (!res.ok) throw new Error(res.statusText)
  219. return res.json()
  220. })
  221. .then((data: any) => data.tag_name.replace(/^v/, ""))
  222. }
  223. }