server.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import { createSimpleContext } from "@opencode-ai/ui/context"
  2. import { batch, createEffect, createMemo, onCleanup } from "solid-js"
  3. import { createStore } from "solid-js/store"
  4. import { usePlatform } from "@/context/platform"
  5. import { Persist, persisted } from "@/utils/persist"
  6. import { checkServerHealth } from "@/utils/server-health"
  7. type StoredProject = { worktree: string; expanded: boolean }
  8. export function normalizeServerUrl(input: string) {
  9. const trimmed = input.trim()
  10. if (!trimmed) return
  11. const withProtocol = /^https?:\/\//.test(trimmed) ? trimmed : `http://${trimmed}`
  12. return withProtocol.replace(/\/+$/, "")
  13. }
  14. export function serverDisplayName(url: string) {
  15. if (!url) return ""
  16. return url.replace(/^https?:\/\//, "").replace(/\/+$/, "")
  17. }
  18. function projectsKey(url: string) {
  19. if (!url) return ""
  20. const host = url.replace(/^https?:\/\//, "").split(":")[0]
  21. if (host === "localhost" || host === "127.0.0.1") return "local"
  22. return url
  23. }
  24. export const { use: useServer, provider: ServerProvider } = createSimpleContext({
  25. name: "Server",
  26. init: (props: { defaultUrl: string }) => {
  27. const platform = usePlatform()
  28. const [store, setStore, _, ready] = persisted(
  29. Persist.global("server", ["server.v3"]),
  30. createStore({
  31. list: [] as string[],
  32. projects: {} as Record<string, StoredProject[]>,
  33. lastProject: {} as Record<string, string>,
  34. }),
  35. )
  36. const [state, setState] = createStore({
  37. active: "",
  38. healthy: undefined as boolean | undefined,
  39. })
  40. const healthy = () => state.healthy
  41. function setActive(input: string) {
  42. const url = normalizeServerUrl(input)
  43. if (!url) return
  44. setState("active", url)
  45. }
  46. function add(input: string) {
  47. const url = normalizeServerUrl(input)
  48. if (!url) return
  49. const fallback = normalizeServerUrl(props.defaultUrl)
  50. if (fallback && url === fallback) {
  51. setState("active", url)
  52. return
  53. }
  54. batch(() => {
  55. if (!store.list.includes(url)) {
  56. setStore("list", store.list.length, url)
  57. }
  58. setState("active", url)
  59. })
  60. }
  61. function remove(input: string) {
  62. const url = normalizeServerUrl(input)
  63. if (!url) return
  64. const list = store.list.filter((x) => x !== url)
  65. const next = state.active === url ? (list[0] ?? normalizeServerUrl(props.defaultUrl) ?? "") : state.active
  66. batch(() => {
  67. setStore("list", list)
  68. setState("active", next)
  69. })
  70. }
  71. createEffect(() => {
  72. if (!ready()) return
  73. if (state.active) return
  74. const url = normalizeServerUrl(props.defaultUrl)
  75. if (!url) return
  76. setState("active", url)
  77. })
  78. const isReady = createMemo(() => ready() && !!state.active)
  79. const fetcher = platform.fetch ?? globalThis.fetch
  80. const check = (url: string) => checkServerHealth(url, fetcher).then((x) => x.healthy)
  81. createEffect(() => {
  82. const url = state.active
  83. if (!url) return
  84. setState("healthy", undefined)
  85. let alive = true
  86. let busy = false
  87. const run = () => {
  88. if (busy) return
  89. busy = true
  90. void check(url)
  91. .then((next) => {
  92. if (!alive) return
  93. setState("healthy", next)
  94. })
  95. .finally(() => {
  96. busy = false
  97. })
  98. }
  99. run()
  100. const interval = setInterval(run, 10_000)
  101. onCleanup(() => {
  102. alive = false
  103. clearInterval(interval)
  104. })
  105. })
  106. const origin = createMemo(() => projectsKey(state.active))
  107. const projectsList = createMemo(() => store.projects[origin()] ?? [])
  108. const isLocal = createMemo(() => origin() === "local")
  109. return {
  110. ready: isReady,
  111. healthy,
  112. isLocal,
  113. get url() {
  114. return state.active
  115. },
  116. get name() {
  117. return serverDisplayName(state.active)
  118. },
  119. get list() {
  120. return store.list
  121. },
  122. setActive,
  123. add,
  124. remove,
  125. projects: {
  126. list: projectsList,
  127. open(directory: string) {
  128. const key = origin()
  129. if (!key) return
  130. const current = store.projects[key] ?? []
  131. if (current.find((x) => x.worktree === directory)) return
  132. setStore("projects", key, [{ worktree: directory, expanded: true }, ...current])
  133. },
  134. close(directory: string) {
  135. const key = origin()
  136. if (!key) return
  137. const current = store.projects[key] ?? []
  138. setStore(
  139. "projects",
  140. key,
  141. current.filter((x) => x.worktree !== directory),
  142. )
  143. },
  144. expand(directory: string) {
  145. const key = origin()
  146. if (!key) return
  147. const current = store.projects[key] ?? []
  148. const index = current.findIndex((x) => x.worktree === directory)
  149. if (index !== -1) setStore("projects", key, index, "expanded", true)
  150. },
  151. collapse(directory: string) {
  152. const key = origin()
  153. if (!key) return
  154. const current = store.projects[key] ?? []
  155. const index = current.findIndex((x) => x.worktree === directory)
  156. if (index !== -1) setStore("projects", key, index, "expanded", false)
  157. },
  158. move(directory: string, toIndex: number) {
  159. const key = origin()
  160. if (!key) return
  161. const current = store.projects[key] ?? []
  162. const fromIndex = current.findIndex((x) => x.worktree === directory)
  163. if (fromIndex === -1 || fromIndex === toIndex) return
  164. const result = [...current]
  165. const [item] = result.splice(fromIndex, 1)
  166. result.splice(toIndex, 0, item)
  167. setStore("projects", key, result)
  168. },
  169. last() {
  170. const key = origin()
  171. if (!key) return
  172. return store.lastProject[key]
  173. },
  174. touch(directory: string) {
  175. const key = origin()
  176. if (!key) return
  177. setStore("lastProject", key, directory)
  178. },
  179. },
  180. }
  181. },
  182. })