persist.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. import { Platform, usePlatform } from "@/context/platform"
  2. import { makePersisted, type AsyncStorage, type SyncStorage } from "@solid-primitives/storage"
  3. import { checksum } from "@opencode-ai/core/util/encode"
  4. import { createResource, type Accessor } from "solid-js"
  5. import type { SetStoreFunction, Store } from "solid-js/store"
  6. import { pathKey } from "@/utils/path-key"
  7. import { ScopedKey, ServerScope, type ServerScope as ServerScopeValue } from "@/utils/server-scope"
  8. type InitType = Promise<string> | string | null
  9. type PersistedWithReady<T> = [
  10. Store<T>,
  11. SetStoreFunction<T>,
  12. InitType,
  13. Accessor<boolean> & { promise: undefined | Promise<any> },
  14. ]
  15. type PersistTarget = {
  16. storage?: string
  17. scope?: "window"
  18. legacyStorageNames?: string[]
  19. key: string
  20. legacy?: string[]
  21. migrate?: (value: unknown) => unknown
  22. }
  23. const LEGACY_STORAGE = "default.dat"
  24. const GLOBAL_STORAGE = "opencode.global.dat"
  25. const WINDOW_STORAGE = "opencode.window"
  26. const LOCAL_PREFIX = "opencode."
  27. const fallback = new Map<string, boolean>()
  28. const CACHE_MAX_ENTRIES = 500
  29. const CACHE_MAX_BYTES = 8 * 1024 * 1024
  30. type CacheEntry = { value: string; bytes: number }
  31. const cache = new Map<string, CacheEntry>()
  32. const cacheTotal = { bytes: 0 }
  33. function cacheDelete(key: string) {
  34. const entry = cache.get(key)
  35. if (!entry) return
  36. cacheTotal.bytes -= entry.bytes
  37. cache.delete(key)
  38. }
  39. function cachePrune() {
  40. for (;;) {
  41. if (cache.size <= CACHE_MAX_ENTRIES && cacheTotal.bytes <= CACHE_MAX_BYTES) return
  42. const oldest = cache.keys().next().value as string | undefined
  43. if (!oldest) return
  44. cacheDelete(oldest)
  45. }
  46. }
  47. function cacheSet(key: string, value: string) {
  48. const bytes = value.length * 2
  49. if (bytes > CACHE_MAX_BYTES) {
  50. cacheDelete(key)
  51. return
  52. }
  53. const entry = cache.get(key)
  54. if (entry) cacheTotal.bytes -= entry.bytes
  55. cache.delete(key)
  56. cache.set(key, { value, bytes })
  57. cacheTotal.bytes += bytes
  58. cachePrune()
  59. }
  60. function cacheGet(key: string) {
  61. const entry = cache.get(key)
  62. if (!entry) return
  63. cache.delete(key)
  64. cache.set(key, entry)
  65. return entry.value
  66. }
  67. function fallbackDisabled(scope: string) {
  68. return fallback.get(scope) === true
  69. }
  70. function fallbackSet(scope: string) {
  71. fallback.set(scope, true)
  72. }
  73. function quota(error: unknown) {
  74. if (error instanceof DOMException) {
  75. if (error.name === "QuotaExceededError") return true
  76. if (error.name === "NS_ERROR_DOM_QUOTA_REACHED") return true
  77. if (error.name === "QUOTA_EXCEEDED_ERR") return true
  78. if (error.code === 22 || error.code === 1014) return true
  79. return false
  80. }
  81. if (!error || typeof error !== "object") return false
  82. const name = (error as { name?: string }).name
  83. if (name === "QuotaExceededError" || name === "NS_ERROR_DOM_QUOTA_REACHED") return true
  84. if (name && /quota/i.test(name)) return true
  85. const code = (error as { code?: number }).code
  86. if (code === 22 || code === 1014) return true
  87. const message = (error as { message?: string }).message
  88. if (typeof message !== "string") return false
  89. if (/quota/i.test(message)) return true
  90. return false
  91. }
  92. type Evict = { key: string; size: number }
  93. function evict(storage: Storage, keep: string, value: string) {
  94. const total = storage.length
  95. const indexes = Array.from({ length: total }, (_, index) => index)
  96. const items: Evict[] = []
  97. for (const index of indexes) {
  98. const name = storage.key(index)
  99. if (!name) continue
  100. if (!name.startsWith(LOCAL_PREFIX)) continue
  101. if (name === keep) continue
  102. const stored = storage.getItem(name)
  103. items.push({ key: name, size: stored?.length ?? 0 })
  104. }
  105. items.sort((a, b) => b.size - a.size)
  106. for (const item of items) {
  107. storage.removeItem(item.key)
  108. cacheDelete(item.key)
  109. try {
  110. storage.setItem(keep, value)
  111. cacheSet(keep, value)
  112. return true
  113. } catch (error) {
  114. if (!quota(error)) throw error
  115. }
  116. }
  117. return false
  118. }
  119. function write(storage: Storage, key: string, value: string) {
  120. try {
  121. storage.setItem(key, value)
  122. cacheSet(key, value)
  123. return true
  124. } catch (error) {
  125. if (!quota(error)) throw error
  126. }
  127. try {
  128. storage.removeItem(key)
  129. cacheDelete(key)
  130. storage.setItem(key, value)
  131. cacheSet(key, value)
  132. return true
  133. } catch (error) {
  134. if (!quota(error)) throw error
  135. }
  136. const ok = evict(storage, key, value)
  137. return ok
  138. }
  139. function snapshot(value: unknown) {
  140. return JSON.parse(JSON.stringify(value)) as unknown
  141. }
  142. function isRecord(value: unknown): value is Record<string, unknown> {
  143. return typeof value === "object" && value !== null && !Array.isArray(value)
  144. }
  145. function merge(defaults: unknown, value: unknown): unknown {
  146. if (value === undefined) return defaults
  147. if (value === null) return value
  148. if (Array.isArray(defaults)) {
  149. if (Array.isArray(value)) return value
  150. return defaults
  151. }
  152. if (isRecord(defaults)) {
  153. if (!isRecord(value)) return defaults
  154. const result: Record<string, unknown> = { ...defaults }
  155. for (const key of Object.keys(value)) {
  156. if (key in defaults) {
  157. result[key] = merge((defaults as Record<string, unknown>)[key], (value as Record<string, unknown>)[key])
  158. } else {
  159. result[key] = (value as Record<string, unknown>)[key]
  160. }
  161. }
  162. return result
  163. }
  164. return value
  165. }
  166. function parse(value: string) {
  167. try {
  168. return JSON.parse(value) as unknown
  169. } catch {
  170. return undefined
  171. }
  172. }
  173. function normalize(defaults: unknown, raw: string, migrate?: (value: unknown) => unknown) {
  174. const parsed = parse(raw)
  175. if (parsed === undefined) return
  176. const migrated = migrate ? migrate(parsed) : parsed
  177. const merged = merge(defaults, migrated)
  178. return JSON.stringify(merged)
  179. }
  180. function readCurrent(input: {
  181. storage: SyncStorage
  182. key: string
  183. defaults: unknown
  184. migrate?: (value: unknown) => unknown
  185. }) {
  186. const raw = input.storage.getItem(input.key)
  187. if (raw === null) return
  188. const next = normalize(input.defaults, raw, input.migrate)
  189. if (next === undefined) {
  190. input.storage.removeItem(input.key)
  191. return null
  192. }
  193. if (raw !== next) input.storage.setItem(input.key, next)
  194. return next
  195. }
  196. function migrateLegacy(input: {
  197. current: SyncStorage
  198. legacyStore?: SyncStorage
  199. stores: SyncStorage[]
  200. keys: string[]
  201. key: string
  202. defaults: unknown
  203. migrate?: (value: unknown) => unknown
  204. }) {
  205. for (const store of input.stores) {
  206. const raw = store.getItem(input.key)
  207. if (raw === null) continue
  208. const next = normalize(input.defaults, raw, input.migrate)
  209. if (next === undefined) {
  210. store.removeItem(input.key)
  211. continue
  212. }
  213. input.current.setItem(input.key, next)
  214. store.removeItem(input.key)
  215. return next
  216. }
  217. if (!input.legacyStore) return null
  218. for (const key of input.keys) {
  219. const raw = input.legacyStore.getItem(key)
  220. if (raw === null) continue
  221. const next = normalize(input.defaults, raw, input.migrate)
  222. if (next === undefined) {
  223. input.legacyStore.removeItem(key)
  224. continue
  225. }
  226. input.current.setItem(input.key, next)
  227. input.legacyStore.removeItem(key)
  228. return next
  229. }
  230. return null
  231. }
  232. async function readCurrentAsync(input: {
  233. storage: AsyncStorage
  234. key: string
  235. defaults: unknown
  236. migrate?: (value: unknown) => unknown
  237. }) {
  238. const raw = await input.storage.getItem(input.key)
  239. if (raw === null) return
  240. const next = normalize(input.defaults, raw, input.migrate)
  241. if (next === undefined) {
  242. await input.storage.removeItem(input.key).catch(() => undefined)
  243. return null
  244. }
  245. if (raw !== next) await input.storage.setItem(input.key, next)
  246. return next
  247. }
  248. async function removeAsync(storage: AsyncStorage, key: string) {
  249. try {
  250. await storage.removeItem(key)
  251. } catch {}
  252. }
  253. async function migrateLegacyAsync(input: {
  254. current: AsyncStorage
  255. legacyStore?: AsyncStorage
  256. stores: AsyncStorage[]
  257. keys: string[]
  258. key: string
  259. defaults: unknown
  260. migrate?: (value: unknown) => unknown
  261. }) {
  262. for (const store of input.stores) {
  263. const raw = await store.getItem(input.key)
  264. if (raw === null) continue
  265. const next = normalize(input.defaults, raw, input.migrate)
  266. if (next === undefined) {
  267. await removeAsync(store, input.key)
  268. continue
  269. }
  270. await input.current.setItem(input.key, next)
  271. await store.removeItem(input.key)
  272. return next
  273. }
  274. if (!input.legacyStore) return null
  275. for (const key of input.keys) {
  276. const raw = await input.legacyStore.getItem(key)
  277. if (raw === null) continue
  278. const next = normalize(input.defaults, raw, input.migrate)
  279. if (next === undefined) {
  280. await removeAsync(input.legacyStore, key)
  281. continue
  282. }
  283. await input.current.setItem(input.key, next)
  284. await input.legacyStore.removeItem(key)
  285. return next
  286. }
  287. return null
  288. }
  289. function workspaceStorage(dir: string) {
  290. const head = (dir.slice(0, 12) || "workspace").replace(/[^a-zA-Z0-9._-]/g, "-")
  291. const sum = checksum(dir) ?? "0"
  292. return `opencode.workspace.${head}.${sum}.dat`
  293. }
  294. function draftStorage(draftID: string) {
  295. const head = (draftID.slice(0, 12) || "draft").replace(/[^a-zA-Z0-9._-]/g, "-")
  296. const sum = checksum(draftID) ?? "0"
  297. return `opencode.draft.${head}.${sum}.dat`
  298. }
  299. function windowStorage(windowID: string) {
  300. const safe = (windowID || "browser").replace(/[^a-zA-Z0-9._-]/g, "-")
  301. return `${WINDOW_STORAGE}.${safe}.dat`
  302. }
  303. function legacyWorkspaceStorage(dir: string) {
  304. const storage = workspaceStorage(pathKey(dir))
  305. const result = new Set<string>()
  306. const raw = workspaceStorage(dir)
  307. if (raw !== storage) result.add(raw)
  308. const key = pathKey(dir)
  309. const drive = key.length >= 3 && key[1] === ":" && key[2] === "/"
  310. if (drive) {
  311. const backslash = workspaceStorage(key.replaceAll("/", "\\"))
  312. if (backslash !== storage) result.add(backslash)
  313. }
  314. if (result.size === 0) return
  315. return [...result]
  316. }
  317. function serverWorkspaceTarget(scope: ServerScopeValue, dir: string, key: string, legacy?: string[]): PersistTarget {
  318. if (scope !== ServerScope.local) return { storage: workspaceStorage(ScopedKey.from(scope, pathKey(dir))), key }
  319. return { storage: workspaceStorage(pathKey(dir)), legacyStorageNames: legacyWorkspaceStorage(dir), key, legacy }
  320. }
  321. function localStorageWithPrefix(prefix: string): SyncStorage {
  322. const base = `${prefix}:`
  323. const scope = `prefix:${prefix}`
  324. const item = (key: string) => base + key
  325. return {
  326. getItem: (key) => {
  327. const name = item(key)
  328. const cached = cacheGet(name)
  329. if (fallbackDisabled(scope)) return cached ?? null
  330. const stored = (() => {
  331. try {
  332. return localStorage.getItem(name)
  333. } catch {
  334. fallbackSet(scope)
  335. return null
  336. }
  337. })()
  338. if (stored === null) return cached ?? null
  339. cacheSet(name, stored)
  340. return stored
  341. },
  342. setItem: (key, value) => {
  343. const name = item(key)
  344. if (fallbackDisabled(scope)) return
  345. try {
  346. if (write(localStorage, name, value)) return
  347. } catch {
  348. fallbackSet(scope)
  349. return
  350. }
  351. fallbackSet(scope)
  352. },
  353. removeItem: (key) => {
  354. const name = item(key)
  355. cacheDelete(name)
  356. if (fallbackDisabled(scope)) return
  357. try {
  358. localStorage.removeItem(name)
  359. } catch {
  360. fallbackSet(scope)
  361. }
  362. },
  363. }
  364. }
  365. function localStorageDirect(): SyncStorage {
  366. const scope = "direct"
  367. return {
  368. getItem: (key) => {
  369. const cached = cacheGet(key)
  370. if (fallbackDisabled(scope)) return cached ?? null
  371. const stored = (() => {
  372. try {
  373. return localStorage.getItem(key)
  374. } catch {
  375. fallbackSet(scope)
  376. return null
  377. }
  378. })()
  379. if (stored === null) return cached ?? null
  380. cacheSet(key, stored)
  381. return stored
  382. },
  383. setItem: (key, value) => {
  384. if (fallbackDisabled(scope)) return
  385. try {
  386. if (write(localStorage, key, value)) return
  387. } catch {
  388. fallbackSet(scope)
  389. return
  390. }
  391. fallbackSet(scope)
  392. },
  393. removeItem: (key) => {
  394. cacheDelete(key)
  395. if (fallbackDisabled(scope)) return
  396. try {
  397. localStorage.removeItem(key)
  398. } catch {
  399. fallbackSet(scope)
  400. }
  401. },
  402. }
  403. }
  404. const DRAFT_PERSISTED_KEYS = ["prompt", "comments", "model-selection", "file-view", "layout"]
  405. export function draftPersistedKeys() {
  406. return DRAFT_PERSISTED_KEYS
  407. }
  408. export const PersistTesting = {
  409. localStorageDirect,
  410. localStorageWithPrefix,
  411. migrateLegacy,
  412. normalize,
  413. resolveTarget,
  414. windowStorage,
  415. workspaceStorage,
  416. }
  417. export const Persist = {
  418. global(key: string, legacy?: string[]): PersistTarget {
  419. return { storage: GLOBAL_STORAGE, key, legacy }
  420. },
  421. window(key: string, legacy?: string[]): PersistTarget {
  422. return { scope: "window", key, legacy }
  423. },
  424. draft(draftID: string, key: string, legacy?: string[]): PersistTarget {
  425. return { storage: draftStorage(draftID), key: `draft:${key}`, legacy }
  426. },
  427. serverGlobal(scope: ServerScopeValue, key: string, legacy?: string[]): PersistTarget {
  428. if (scope === ServerScope.local) return Persist.global(key, legacy)
  429. return { storage: GLOBAL_STORAGE, key: ScopedKey.from(scope, key) }
  430. },
  431. workspace(dir: string, key: string, legacy?: string[]): PersistTarget {
  432. return serverWorkspaceTarget(ServerScope.local, dir, `workspace:${key}`, legacy)
  433. },
  434. serverWorkspace(scope: ServerScopeValue, dir: string, key: string, legacy?: string[]): PersistTarget {
  435. return serverWorkspaceTarget(scope, dir, `workspace:${key}`, legacy)
  436. },
  437. session(dir: string, session: string, key: string, legacy?: string[]): PersistTarget {
  438. return serverWorkspaceTarget(ServerScope.local, dir, `session:${session}:${key}`, legacy)
  439. },
  440. serverSession(scope: ServerScopeValue, dir: string, session: string, key: string, legacy?: string[]): PersistTarget {
  441. return serverWorkspaceTarget(scope, dir, `session:${session}:${key}`, legacy)
  442. },
  443. scoped(dir: string, session: string | undefined, key: string, legacy?: string[]): PersistTarget {
  444. if (session) return Persist.session(dir, session, key, legacy)
  445. return Persist.workspace(dir, key, legacy)
  446. },
  447. serverScoped(scope: ServerScopeValue, dir: string, session: string | undefined, key: string, legacy?: string[]) {
  448. if (session) return Persist.serverSession(scope, dir, session, key, legacy)
  449. return Persist.serverWorkspace(scope, dir, key, legacy)
  450. },
  451. }
  452. function resolveTarget(target: PersistTarget, platform: Platform): PersistTarget {
  453. if (target.scope !== "window") return target
  454. if (platform.platform === "desktop" && !platform.windowID) return { ...target, storage: GLOBAL_STORAGE }
  455. const windowID = platform.platform === "desktop" ? (platform.windowID ?? "browser") : "browser"
  456. return {
  457. ...target,
  458. storage: windowStorage(windowID),
  459. }
  460. }
  461. export function removePersisted(
  462. target: { storage?: string; legacyStorageNames?: string[]; key: string },
  463. platform?: Platform,
  464. ) {
  465. const isDesktop = platform?.platform === "desktop" && !!platform.storage
  466. if (isDesktop) {
  467. void platform.storage?.(target.storage)?.removeItem(target.key)
  468. for (const storage of target.legacyStorageNames ?? []) {
  469. void platform.storage?.(storage)?.removeItem(target.key)
  470. }
  471. return
  472. }
  473. if (!target.storage) {
  474. localStorageDirect().removeItem(target.key)
  475. return
  476. }
  477. localStorageWithPrefix(target.storage).removeItem(target.key)
  478. for (const storage of target.legacyStorageNames ?? []) {
  479. localStorageWithPrefix(storage).removeItem(target.key)
  480. }
  481. }
  482. export function persisted<T>(
  483. target: string | PersistTarget,
  484. store: [Store<T>, SetStoreFunction<T>],
  485. ): PersistedWithReady<T> {
  486. const platform = usePlatform()
  487. const config = resolveTarget(typeof target === "string" ? { key: target } : target, platform)
  488. const defaults = snapshot(store[0])
  489. const legacy = config.legacy ?? []
  490. const isDesktop = platform.platform === "desktop" && !!platform.storage
  491. const currentStorage = (() => {
  492. if (isDesktop) return platform.storage?.(config.storage)
  493. if (!config.storage) return localStorageDirect()
  494. return localStorageWithPrefix(config.storage)
  495. })()
  496. const legacyStorage = (() => {
  497. if (!isDesktop) return localStorageDirect()
  498. if (!config.storage) return platform.storage?.()
  499. return platform.storage?.(LEGACY_STORAGE)
  500. })()
  501. const legacyStorageNames = config.legacyStorageNames ?? []
  502. const storage = (() => {
  503. if (!isDesktop) {
  504. const current = currentStorage as SyncStorage
  505. const legacyStore = legacyStorage as SyncStorage
  506. const legacyStores = legacyStorageNames.map(localStorageWithPrefix)
  507. const api: SyncStorage = {
  508. getItem: (key) => {
  509. const value = readCurrent({ storage: current, key, defaults, migrate: config.migrate })
  510. if (value !== undefined) return value
  511. return migrateLegacy({
  512. current,
  513. legacyStore,
  514. stores: legacyStores,
  515. keys: legacy,
  516. key,
  517. defaults,
  518. migrate: config.migrate,
  519. })
  520. },
  521. setItem: (key, value) => {
  522. current.setItem(key, value)
  523. },
  524. removeItem: (key) => {
  525. current.removeItem(key)
  526. },
  527. }
  528. return api
  529. }
  530. const current = currentStorage as AsyncStorage
  531. const legacyStore = legacyStorage as AsyncStorage | undefined
  532. const legacyStores = legacyStorageNames
  533. .map((name) => platform.storage?.(name) as AsyncStorage | undefined)
  534. .filter((x) => !!x)
  535. const api: AsyncStorage = {
  536. getItem: async (key) => {
  537. const value = await readCurrentAsync({ storage: current, key, defaults, migrate: config.migrate })
  538. if (value !== undefined) return value
  539. return migrateLegacyAsync({
  540. current,
  541. legacyStore,
  542. stores: legacyStores,
  543. keys: legacy,
  544. key,
  545. defaults,
  546. migrate: config.migrate,
  547. })
  548. },
  549. setItem: async (key, value) => {
  550. await current.setItem(key, value)
  551. },
  552. removeItem: async (key) => {
  553. await current.removeItem(key)
  554. },
  555. }
  556. return api
  557. })()
  558. const [state, setState, init] = makePersisted(store, { name: config.key, storage })
  559. const isAsync = init instanceof Promise
  560. const [ready] = createResource(
  561. () => init,
  562. async (initValue) => {
  563. if (initValue instanceof Promise) await initValue
  564. return true
  565. },
  566. { initialValue: !isAsync },
  567. )
  568. return [
  569. state,
  570. setState,
  571. init,
  572. Object.assign(() => (ready.loading ? false : ready.latest === true), {
  573. promise: init instanceof Promise ? init : undefined,
  574. }),
  575. ]
  576. }