index.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // @refresh reload
  2. import {
  3. AppBaseProviders,
  4. AppInterface,
  5. handleNotificationClick,
  6. type Platform,
  7. PlatformProvider,
  8. ServerConnection,
  9. useCommand,
  10. } from "@opencode-ai/app"
  11. import { Splash } from "@opencode-ai/ui/logo"
  12. import type { AsyncStorage } from "@solid-primitives/storage"
  13. import { getCurrentWindow } from "@tauri-apps/api/window"
  14. import { readImage } from "@tauri-apps/plugin-clipboard-manager"
  15. import { getCurrent, onOpenUrl } from "@tauri-apps/plugin-deep-link"
  16. import { open, save } from "@tauri-apps/plugin-dialog"
  17. import { fetch as tauriFetch } from "@tauri-apps/plugin-http"
  18. import { isPermissionGranted, requestPermission } from "@tauri-apps/plugin-notification"
  19. import { type as ostype } from "@tauri-apps/plugin-os"
  20. import { relaunch } from "@tauri-apps/plugin-process"
  21. import { open as shellOpen } from "@tauri-apps/plugin-shell"
  22. import { Store } from "@tauri-apps/plugin-store"
  23. import { check, type Update } from "@tauri-apps/plugin-updater"
  24. import { createResource, type JSX, onCleanup, onMount, Show } from "solid-js"
  25. import { render } from "solid-js/web"
  26. import pkg from "../package.json"
  27. import { initI18n, t } from "./i18n"
  28. import { UPDATER_ENABLED } from "./updater"
  29. import { webviewZoom } from "./webview-zoom"
  30. import "./styles.css"
  31. import { Channel } from "@tauri-apps/api/core"
  32. import { commands, ServerReadyData, type InitStep } from "./bindings"
  33. import { createMenu } from "./menu"
  34. const root = document.getElementById("root")
  35. if (import.meta.env.DEV && !(root instanceof HTMLElement)) {
  36. throw new Error(t("error.dev.rootNotFound"))
  37. }
  38. void initI18n()
  39. let update: Update | null = null
  40. const deepLinkEvent = "opencode:deep-link"
  41. const emitDeepLinks = (urls: string[]) => {
  42. if (urls.length === 0) return
  43. window.__OPENCODE__ ??= {}
  44. const pending = window.__OPENCODE__.deepLinks ?? []
  45. window.__OPENCODE__.deepLinks = [...pending, ...urls]
  46. window.dispatchEvent(new CustomEvent(deepLinkEvent, { detail: { urls } }))
  47. }
  48. const listenForDeepLinks = async () => {
  49. const startUrls = await getCurrent().catch(() => null)
  50. if (startUrls?.length) emitDeepLinks(startUrls)
  51. await onOpenUrl((urls) => emitDeepLinks(urls)).catch(() => undefined)
  52. }
  53. const createPlatform = (): Platform => {
  54. const os = (() => {
  55. const type = ostype()
  56. if (type === "macos" || type === "windows" || type === "linux") return type
  57. return undefined
  58. })()
  59. const wslHome = async () => {
  60. if (os !== "windows" || !window.__OPENCODE__?.wsl) return undefined
  61. return commands.wslPath("~", "windows").catch(() => undefined)
  62. }
  63. const handleWslPicker = async <T extends string | string[]>(result: T | null): Promise<T | null> => {
  64. if (!result || !window.__OPENCODE__?.wsl) return result
  65. if (Array.isArray(result)) {
  66. return Promise.all(result.map((path) => commands.wslPath(path, "linux").catch(() => path))) as any
  67. }
  68. return commands.wslPath(result, "linux").catch(() => result) as any
  69. }
  70. return {
  71. platform: "desktop",
  72. os,
  73. version: pkg.version,
  74. async openDirectoryPickerDialog(opts) {
  75. const defaultPath = await wslHome()
  76. const result = await open({
  77. directory: true,
  78. multiple: opts?.multiple ?? false,
  79. title: opts?.title ?? t("desktop.dialog.chooseFolder"),
  80. defaultPath,
  81. })
  82. return await handleWslPicker(result)
  83. },
  84. async openFilePickerDialog(opts) {
  85. const result = await open({
  86. directory: false,
  87. multiple: opts?.multiple ?? false,
  88. title: opts?.title ?? t("desktop.dialog.chooseFile"),
  89. })
  90. return handleWslPicker(result)
  91. },
  92. async saveFilePickerDialog(opts) {
  93. const result = await save({
  94. title: opts?.title ?? t("desktop.dialog.saveFile"),
  95. defaultPath: opts?.defaultPath,
  96. })
  97. return handleWslPicker(result)
  98. },
  99. openLink(url: string) {
  100. void shellOpen(url).catch(() => undefined)
  101. },
  102. async openPath(path: string, app?: string) {
  103. await commands.openPath(path, app ?? null)
  104. },
  105. back() {
  106. window.history.back()
  107. },
  108. forward() {
  109. window.history.forward()
  110. },
  111. storage: (() => {
  112. type StoreLike = {
  113. get(key: string): Promise<string | null | undefined>
  114. set(key: string, value: string): Promise<unknown>
  115. delete(key: string): Promise<unknown>
  116. clear(): Promise<unknown>
  117. keys(): Promise<string[]>
  118. length(): Promise<number>
  119. }
  120. const WRITE_DEBOUNCE_MS = 250
  121. const storeCache = new Map<string, Promise<StoreLike>>()
  122. const apiCache = new Map<string, AsyncStorage & { flush: () => Promise<void> }>()
  123. const memoryCache = new Map<string, StoreLike>()
  124. const flushAll = async () => {
  125. const apis = Array.from(apiCache.values())
  126. await Promise.all(apis.map((api) => api.flush().catch(() => undefined)))
  127. }
  128. if ("addEventListener" in globalThis) {
  129. const handleVisibility = () => {
  130. if (document.visibilityState !== "hidden") return
  131. void flushAll()
  132. }
  133. window.addEventListener("pagehide", () => void flushAll())
  134. document.addEventListener("visibilitychange", handleVisibility)
  135. }
  136. const createMemoryStore = () => {
  137. const data = new Map<string, string>()
  138. const store: StoreLike = {
  139. get: async (key) => data.get(key),
  140. set: async (key, value) => {
  141. data.set(key, value)
  142. },
  143. delete: async (key) => {
  144. data.delete(key)
  145. },
  146. clear: async () => {
  147. data.clear()
  148. },
  149. keys: async () => Array.from(data.keys()),
  150. length: async () => data.size,
  151. }
  152. return store
  153. }
  154. const getStore = (name: string) => {
  155. const cached = storeCache.get(name)
  156. if (cached) return cached
  157. const store = Store.load(name).catch(() => {
  158. const cached = memoryCache.get(name)
  159. if (cached) return cached
  160. const memory = createMemoryStore()
  161. memoryCache.set(name, memory)
  162. return memory
  163. })
  164. storeCache.set(name, store)
  165. return store
  166. }
  167. const createStorage = (name: string) => {
  168. const pending = new Map<string, string | null>()
  169. let timer: ReturnType<typeof setTimeout> | undefined
  170. let flushing: Promise<void> | undefined
  171. const flush = async () => {
  172. if (flushing) return flushing
  173. flushing = (async () => {
  174. const store = await getStore(name)
  175. while (pending.size > 0) {
  176. const batch = Array.from(pending.entries())
  177. pending.clear()
  178. for (const [key, value] of batch) {
  179. if (value === null) {
  180. await store.delete(key).catch(() => undefined)
  181. } else {
  182. await store.set(key, value).catch(() => undefined)
  183. }
  184. }
  185. }
  186. })().finally(() => {
  187. flushing = undefined
  188. })
  189. return flushing
  190. }
  191. const schedule = () => {
  192. if (timer) return
  193. timer = setTimeout(() => {
  194. timer = undefined
  195. void flush()
  196. }, WRITE_DEBOUNCE_MS)
  197. }
  198. const api: AsyncStorage & { flush: () => Promise<void> } = {
  199. flush,
  200. getItem: async (key: string) => {
  201. const next = pending.get(key)
  202. if (next !== undefined) return next
  203. const store = await getStore(name)
  204. const value = await store.get(key).catch(() => null)
  205. if (value === undefined) return null
  206. return value
  207. },
  208. setItem: async (key: string, value: string) => {
  209. pending.set(key, value)
  210. schedule()
  211. },
  212. removeItem: async (key: string) => {
  213. pending.set(key, null)
  214. schedule()
  215. },
  216. clear: async () => {
  217. pending.clear()
  218. const store = await getStore(name)
  219. await store.clear().catch(() => undefined)
  220. },
  221. key: async (index: number) => {
  222. const store = await getStore(name)
  223. return (await store.keys().catch(() => []))[index]
  224. },
  225. getLength: async () => {
  226. const store = await getStore(name)
  227. return await store.length().catch(() => 0)
  228. },
  229. get length() {
  230. return api.getLength()
  231. },
  232. }
  233. return api
  234. }
  235. return (name = "default.dat") => {
  236. const cached = apiCache.get(name)
  237. if (cached) return cached
  238. const api = createStorage(name)
  239. apiCache.set(name, api)
  240. return api
  241. }
  242. })(),
  243. checkUpdate: async () => {
  244. if (!UPDATER_ENABLED) return { updateAvailable: false }
  245. const next = await check().catch(() => null)
  246. if (!next) return { updateAvailable: false }
  247. const ok = await next
  248. .download()
  249. .then(() => true)
  250. .catch(() => false)
  251. if (!ok) return { updateAvailable: false }
  252. update = next
  253. return { updateAvailable: true, version: next.version }
  254. },
  255. update: async () => {
  256. if (!UPDATER_ENABLED || !update) return
  257. if (ostype() === "windows") await commands.killSidecar().catch(() => undefined)
  258. await update.install().catch(() => undefined)
  259. },
  260. restart: async () => {
  261. await commands.killSidecar().catch(() => undefined)
  262. await relaunch()
  263. },
  264. notify: async (title, description, href) => {
  265. const granted = await isPermissionGranted().catch(() => false)
  266. const permission = granted ? "granted" : await requestPermission().catch(() => "denied")
  267. if (permission !== "granted") return
  268. const win = getCurrentWindow()
  269. const focused = await win.isFocused().catch(() => document.hasFocus())
  270. if (focused) return
  271. await Promise.resolve()
  272. .then(() => {
  273. const notification = new Notification(title, {
  274. body: description ?? "",
  275. icon: "https://opencode.ai/favicon-96x96-v3.png",
  276. })
  277. notification.onclick = () => {
  278. const win = getCurrentWindow()
  279. void win.show().catch(() => undefined)
  280. void win.unminimize().catch(() => undefined)
  281. void win.setFocus().catch(() => undefined)
  282. handleNotificationClick(href)
  283. notification.close()
  284. }
  285. })
  286. .catch(() => undefined)
  287. },
  288. fetch: (input, init) => {
  289. if (input instanceof Request) {
  290. return tauriFetch(input)
  291. } else {
  292. return tauriFetch(input, init)
  293. }
  294. },
  295. getWslEnabled: async () => {
  296. const next = await commands.getWslConfig().catch(() => null)
  297. if (next) return next.enabled
  298. return window.__OPENCODE__!.wsl ?? false
  299. },
  300. setWslEnabled: async (enabled) => {
  301. await commands.setWslConfig({ enabled })
  302. },
  303. getDefaultServerUrl: async () => {
  304. const result = await commands.getDefaultServerUrl().catch(() => null)
  305. return result
  306. },
  307. setDefaultServerUrl: async (url: string | null) => {
  308. await commands.setDefaultServerUrl(url)
  309. },
  310. getDisplayBackend: async () => {
  311. const result = await commands.getDisplayBackend().catch(() => null)
  312. return result
  313. },
  314. setDisplayBackend: async (backend) => {
  315. await commands.setDisplayBackend(backend)
  316. },
  317. parseMarkdown: (markdown: string) => commands.parseMarkdownCommand(markdown),
  318. webviewZoom,
  319. checkAppExists: async (appName: string) => {
  320. return commands.checkAppExists(appName)
  321. },
  322. async readClipboardImage() {
  323. const image = await readImage().catch(() => null)
  324. if (!image) return null
  325. const bytes = await image.rgba().catch(() => null)
  326. if (!bytes || bytes.length === 0) return null
  327. const size = await image.size().catch(() => null)
  328. if (!size) return null
  329. const canvas = document.createElement("canvas")
  330. canvas.width = size.width
  331. canvas.height = size.height
  332. const ctx = canvas.getContext("2d")
  333. if (!ctx) return null
  334. const imageData = ctx.createImageData(size.width, size.height)
  335. imageData.data.set(bytes)
  336. ctx.putImageData(imageData, 0, 0)
  337. return new Promise<File | null>((resolve) => {
  338. canvas.toBlob((blob) => {
  339. if (!blob) return resolve(null)
  340. resolve(
  341. new File([blob], `pasted-image-${Date.now()}.png`, {
  342. type: "image/png",
  343. }),
  344. )
  345. }, "image/png")
  346. })
  347. },
  348. }
  349. }
  350. let menuTrigger = null as null | ((id: string) => void)
  351. createMenu((id) => {
  352. menuTrigger?.(id)
  353. })
  354. void listenForDeepLinks()
  355. render(() => {
  356. const platform = createPlatform()
  357. const [defaultServer] = createResource(() =>
  358. platform.getDefaultServerUrl?.().then((url) => {
  359. if (url) return ServerConnection.key({ type: "http", http: { url } })
  360. }),
  361. )
  362. function handleClick(e: MouseEvent) {
  363. const link = (e.target as HTMLElement).closest("a.external-link") as HTMLAnchorElement | null
  364. if (link?.href) {
  365. e.preventDefault()
  366. platform.openLink(link.href)
  367. }
  368. }
  369. onMount(() => {
  370. document.addEventListener("click", handleClick)
  371. onCleanup(() => {
  372. document.removeEventListener("click", handleClick)
  373. })
  374. })
  375. return (
  376. <PlatformProvider value={platform}>
  377. <AppBaseProviders>
  378. <ServerGate>
  379. {(data) => {
  380. const http = {
  381. url: data.url,
  382. username: data.username ?? undefined,
  383. password: data.password ?? undefined,
  384. }
  385. const server: ServerConnection.Any = data.is_sidecar
  386. ? {
  387. displayName: t("desktop.server.local"),
  388. type: "sidecar",
  389. variant: "base",
  390. http,
  391. }
  392. : { type: "http", http }
  393. function Inner() {
  394. const cmd = useCommand()
  395. menuTrigger = (id) => cmd.trigger(id)
  396. return null
  397. }
  398. return (
  399. <Show when={!defaultServer.loading}>
  400. <AppInterface defaultServer={defaultServer.latest ?? ServerConnection.key(server)} servers={[server]}>
  401. <Inner />
  402. </AppInterface>
  403. </Show>
  404. )
  405. }}
  406. </ServerGate>
  407. </AppBaseProviders>
  408. </PlatformProvider>
  409. )
  410. }, root!)
  411. // Gate component that waits for the server to be ready
  412. function ServerGate(props: { children: (data: ServerReadyData) => JSX.Element }) {
  413. const [serverData] = createResource(() => commands.awaitInitialization(new Channel<InitStep>() as any))
  414. if (serverData.state === "errored") throw serverData.error
  415. return (
  416. <Show
  417. when={serverData.state !== "pending" && serverData()}
  418. fallback={
  419. <div class="h-screen w-screen flex flex-col items-center justify-center bg-background-base">
  420. <Splash class="w-16 h-20 opacity-50 animate-pulse" />
  421. <div data-tauri-decorum-tb class="flex flex-row absolute top-0 right-0 z-10 h-10" />
  422. </div>
  423. }
  424. >
  425. {(data) => props.children(data())}
  426. </Show>
  427. )
  428. }