| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- import { createRoot, createSignal, getOwner, onCleanup, runWithOwner, type Owner } from "solid-js"
- import { createStore, type SetStoreFunction, type Store } from "solid-js/store"
- import { Persist, persisted } from "@/utils/persist"
- import type { VcsInfo } from "@opencode-ai/sdk/v2/client"
- import {
- DIR_IDLE_TTL_MS,
- MAX_DIR_STORES,
- type ChildOptions,
- type DirState,
- type IconCache,
- type MetaCache,
- type ProjectMeta,
- type State,
- type VcsCache,
- } from "./types"
- import { canDisposeDirectory, pickDirectoriesToEvict } from "./eviction"
- import { useQuery } from "@tanstack/solid-query"
- import { QueryOptionsApi } from "../server-sync"
- import { directoryKey, type DirectoryKey } from "./utils"
- import { NormalizedProviderListResponse } from "@opencode-ai/ui/context"
- export function createChildStoreManager(input: {
- owner: Owner
- isBooting: (directory: string) => boolean
- isLoadingSessions: (directory: string) => boolean
- onBootstrap: (directory: string) => void
- onMcp: (directory: string, setStore: SetStoreFunction<State>) => void
- onDispose: (directory: string) => void
- translate: (key: string, vars?: Record<string, string | number>) => string
- queryOptions: QueryOptionsApi
- global: {
- provider: NormalizedProviderListResponse
- }
- }) {
- const children: Record<string, [Store<State>, SetStoreFunction<State>]> = {}
- const vcsCache = new Map<string, VcsCache>()
- const metaCache = new Map<string, MetaCache>()
- const iconCache = new Map<string, IconCache>()
- const lifecycle = new Map<string, DirState>()
- const pins = new Map<string, number>()
- const ownerPins = new WeakMap<object, Set<string>>()
- const disposers = new Map<string, () => void>()
- const mcpDirectories = new Set<string>()
- const mcpToggles = new Map<string, (enabled: boolean) => void>()
- const markKey = (key: DirectoryKey) => {
- if (!key) return
- lifecycle.set(key, { lastAccessAt: Date.now() })
- runEviction(key)
- }
- const mark = (directory: string) => {
- const key = directoryKey(directory)
- markKey(key)
- }
- const pin = (directory: string) => {
- const key = directoryKey(directory)
- if (!key) return
- pins.set(key, (pins.get(key) ?? 0) + 1)
- markKey(key)
- }
- const unpin = (directory: string) => {
- const key = directoryKey(directory)
- if (!key) return
- const next = (pins.get(key) ?? 0) - 1
- if (next > 0) {
- pins.set(key, next)
- return
- }
- pins.delete(key)
- runEviction()
- }
- const pinned = (directory: string) => (pins.get(directoryKey(directory)) ?? 0) > 0
- const pinForOwner = (directory: string) => {
- const current = getOwner()
- if (!current) return
- if (current === input.owner) return
- const key = current as object
- const set = ownerPins.get(key)
- if (set?.has(directory)) return
- if (set) set.add(directory)
- if (!set) ownerPins.set(key, new Set([directory]))
- pin(directory)
- onCleanup(() => {
- const set = ownerPins.get(key)
- if (set) {
- set.delete(directory)
- if (set.size === 0) ownerPins.delete(key)
- }
- unpin(directory)
- })
- }
- function disposeDirectory(directory: DirectoryKey) {
- const key = directory
- if (
- !canDisposeDirectory({
- directory: key,
- hasStore: !!children[key],
- pinned: pinned(key),
- booting: input.isBooting(key),
- loadingSessions: input.isLoadingSessions(key),
- })
- ) {
- return false
- }
- vcsCache.delete(key)
- metaCache.delete(key)
- iconCache.delete(key)
- lifecycle.delete(key)
- mcpDirectories.delete(key)
- mcpToggles.delete(key)
- const dispose = disposers.get(key)
- if (dispose) {
- dispose()
- disposers.delete(key)
- }
- delete children[key]
- input.onDispose(key)
- return true
- }
- function runEviction(skip?: string) {
- const stores = Object.keys(children)
- if (stores.length === 0) return
- const list = pickDirectoriesToEvict({
- stores,
- state: lifecycle,
- pins: new Set(stores.filter(pinned)),
- max: MAX_DIR_STORES,
- ttl: DIR_IDLE_TTL_MS,
- now: Date.now(),
- }).filter((directory) => directory !== skip)
- if (list.length === 0) return
- for (const directory of list) {
- if (!disposeDirectory(directoryKey(directory))) continue
- }
- }
- function ensureChild(directory: string) {
- const key = directoryKey(directory)
- if (!key) console.error("No directory provided")
- if (!children[key]) {
- const vcs = runWithOwner(input.owner, () =>
- persisted(
- Persist.workspace(directory, "vcs", ["vcs.v1"]),
- createStore({ value: undefined as VcsInfo | undefined }),
- ),
- )
- if (!vcs) throw new Error(input.translate("error.childStore.persistedCacheCreateFailed"))
- const vcsStore = vcs[0]
- vcsCache.set(key, { store: vcsStore, setStore: vcs[1], ready: vcs[3] })
- const meta = runWithOwner(input.owner, () =>
- persisted(
- Persist.workspace(directory, "project", ["project.v1"]),
- createStore({ value: undefined as ProjectMeta | undefined }),
- ),
- )
- if (!meta) throw new Error(input.translate("error.childStore.persistedProjectMetadataCreateFailed"))
- metaCache.set(key, { store: meta[0], setStore: meta[1], ready: meta[3] })
- const icon = runWithOwner(input.owner, () =>
- persisted(
- Persist.workspace(directory, "icon", ["icon.v1"]),
- createStore({ value: undefined as string | undefined }),
- ),
- )
- if (!icon) throw new Error(input.translate("error.childStore.persistedProjectIconCreateFailed"))
- iconCache.set(key, { store: icon[0], setStore: icon[1], ready: icon[3] })
- const init = () =>
- createRoot((dispose) => {
- const initialMeta = meta[0].value
- const initialIcon = icon[0].value
- const [mcpEnabled, setMcpEnabled] = createSignal(false)
- const pathQuery = useQuery(() => input.queryOptions.path(key))
- const mcpQuery = useQuery(() => ({ ...input.queryOptions.mcp(key), enabled: mcpEnabled() }))
- const lspQuery = useQuery(() => input.queryOptions.lsp(key))
- const providerQuery = useQuery(() => input.queryOptions.providers(key))
- const child = createStore<State>({
- project: "",
- projectMeta: initialMeta,
- icon: initialIcon,
- get provider_ready() {
- return !providerQuery.isLoading
- },
- get provider() {
- const EMPTY = { all: new Map(), connected: [], default: {} }
- if (providerQuery.isLoading) return EMPTY
- if (providerQuery.data?.all.size === 0 && input.global.provider.all.size > 0) return input.global.provider
- return providerQuery.data ?? EMPTY
- },
- config: {},
- get path() {
- if (pathQuery.data) return pathQuery.data
- return { state: "", config: "", worktree: "", directory, home: "" }
- },
- status: "loading" as const,
- agent: [],
- command: [],
- session: [],
- sessionTotal: 0,
- session_status: {},
- session_working(id: string) {
- const type = this.session_status[id]?.type
- return (type ?? "idle") !== "idle"
- },
- session_diff: {},
- todo: {},
- permission: {},
- question: {},
- get mcp_ready() {
- return !mcpQuery.isLoading
- },
- get mcp() {
- return mcpQuery.isLoading ? {} : (mcpQuery.data ?? {})
- },
- get lsp_ready() {
- return !lspQuery.isLoading
- },
- get lsp() {
- return lspQuery.isLoading ? [] : (lspQuery.data ?? [])
- },
- vcs: vcsStore.value,
- limit: 5,
- message: {},
- part: {},
- part_text_accum_delta: {},
- })
- children[key] = child
- disposers.set(key, dispose)
- mcpToggles.set(key, setMcpEnabled)
- const onPersistedInit = (init: Promise<string> | string | null, run: () => void) => {
- if (!(init instanceof Promise)) return
- void init.then(() => {
- if (children[key] !== child) return
- run()
- })
- }
- onPersistedInit(vcs[2], () => {
- const cached = vcsStore.value
- if (!cached?.branch) return
- child[1]("vcs", (value) => value ?? cached)
- })
- onPersistedInit(meta[2], () => {
- if (child[0].projectMeta !== initialMeta) return
- child[1]("projectMeta", meta[0].value)
- })
- onPersistedInit(icon[2], () => {
- if (child[0].icon !== initialIcon) return
- child[1]("icon", icon[0].value)
- })
- })
- runWithOwner(input.owner, init)
- }
- markKey(key)
- const childStore = children[key]
- if (!childStore) throw new Error(input.translate("error.childStore.storeCreateFailed"))
- return childStore
- }
- function child(directory: string, options: ChildOptions = {}) {
- const key = directoryKey(directory)
- const childStore = ensureChild(directory)
- pinForOwner(key)
- if (options.mcp) enableMcp(directory, key, childStore)
- const shouldBootstrap = options.bootstrap ?? true
- if (shouldBootstrap && childStore[0].status === "loading") {
- input.onBootstrap(directory)
- }
- return childStore
- }
- function peek(directory: string, options: ChildOptions = {}) {
- const key = directoryKey(directory)
- const childStore = ensureChild(directory)
- if (options.mcp) enableMcp(directory, key, childStore)
- const shouldBootstrap = options.bootstrap ?? true
- if (shouldBootstrap && childStore[0].status === "loading") {
- input.onBootstrap(directory)
- }
- return childStore
- }
- function enableMcp(directory: string, key: DirectoryKey, childStore: [Store<State>, SetStoreFunction<State>]) {
- if (mcpDirectories.has(key)) return
- mcpDirectories.add(key)
- mcpToggles.get(key)?.(true)
- if (childStore[0].status !== "loading") input.onMcp(directory, childStore[1])
- }
- function disableMcp(directory: string) {
- const key = directoryKey(directory)
- if (!mcpDirectories.delete(key)) return
- mcpToggles.get(key)?.(false)
- }
- function projectMeta(directory: string, patch: ProjectMeta) {
- const key = directoryKey(directory)
- const [store, setStore] = ensureChild(directory)
- const cached = metaCache.get(key)
- if (!cached) return
- const previous = store.projectMeta ?? {}
- const icon = patch.icon ? { ...previous.icon, ...patch.icon } : previous.icon
- const commands = patch.commands ? { ...previous.commands, ...patch.commands } : previous.commands
- const next = {
- ...previous,
- ...patch,
- icon,
- commands,
- }
- cached.setStore("value", next)
- setStore("projectMeta", next)
- }
- function projectIcon(directory: string, value: string | undefined) {
- const key = directoryKey(directory)
- const [store, setStore] = ensureChild(directory)
- const cached = iconCache.get(key)
- if (!cached) return
- if (store.icon === value) return
- cached.setStore("value", value)
- setStore("icon", value)
- }
- return {
- children,
- ensureChild,
- child,
- peek,
- projectMeta,
- projectIcon,
- mark,
- pin,
- unpin,
- pinned,
- mcp: (directory: string) => mcpDirectories.has(directoryKey(directory)),
- disableMcp,
- disposeDirectory,
- runEviction,
- vcsCache,
- metaCache,
- iconCache,
- }
- }
|