Repository navigation
Expand file tree
/
Copy pathreference.ts
More file actions
129 lines (115 loc) · 4.13 KB
/
Copy pathreference.ts
File metadata and controls
129 lines (115 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
export * as Reference from "./reference.js"
import { makeLocationNode } from "@opencode/util/effect/app-node"
import { Context, Effect, Layer, Scope } from "effect"
import { Reference } from "@opencode/schema/reference"
import { Global } from "@opencode/util/global"
import { Bus } from "./bus.js"
import { Repository } from "./repository.js"
import { RepositoryCache } from "./repository-cache.js"
import { AbsolutePath } from "./schema.js"
import { State } from "./state.js"
export const LocalSource = Reference.LocalSource
export type LocalSource = Reference.LocalSource
export const GitSource = Reference.GitSource
export type GitSource = Reference.GitSource
export const Source = Reference.Source
export type Source = Reference.Source
export { Event } from "@opencode/schema/reference"
export const Info = Reference.Info
export type Info = Reference.Info
type Data = {
sources: Map<string, Source>
}
type Editor = {
add(name: string, source: Source): void
remove(name: string): void
list(): readonly [string, Source][]
get(name: string): Source | undefined
}
export interface Interface extends State.Transformable<Editor> {
readonly list: () => Effect.Effect<Info[]>
}
export class Service extends Context.Service<Service, Interface>()("@opencode/Reference") {}
const layer = Layer.effect(
Service,
Effect.gen(function* () {
const global = yield* Global.Service
const bus = yield* Bus.Service
const cache = yield* RepositoryCache.Service
const scope = yield* Scope.Scope
const list = (data: Data = state.get()): Info[] =>
Array.from(data.sources).flatMap(([name, source]) => {
const info = {
name,
source,
...(source.description === undefined ? {} : { description: source.description }),
...(source.hidden === undefined ? {} : { hidden: source.hidden }),
}
if (source.type === "local") return [Info.make({ ...info, path: source.path })]
const repository = Repository.parse(source.repository)
if (!repository || !Repository.isRemote(repository)) return []
if (source.branch) {
try {
Repository.validateBranch(source.branch)
} catch {
return []
}
}
return [
Info.make({
...info,
path: AbsolutePath.make(Repository.cachePath(global.repos, repository, source.branch)),
}),
]
})
const refresh = Effect.fn("Reference.refresh")(function* () {
yield* Effect.forEach(
list(),
(reference) =>
Effect.gen(function* () {
if (reference.source.type !== "git") return
yield* cache.ensure({
reference: Repository.parseRemote(reference.source.repository),
branch: reference.source.branch,
refresh: "daily",
})
}).pipe(
Effect.catchCause((cause) =>
Effect.logWarning("failed to materialize reference", { name: reference.name, cause }),
),
),
{ concurrency: 4, discard: true },
)
})
const state = State.create<Data, Editor>({
name: "reference",
initial: () => ({ sources: new Map() }),
editor: (editor) => ({
add: (name, source) => editor.sources.set(name, source),
remove: (name) => editor.sources.delete(name),
list: () => Array.from(editor.sources),
get: (name) => editor.sources.get(name),
}),
notify: State.changes(list, () =>
Effect.gen(function* () {
yield* refresh().pipe(Effect.forkIn(scope))
yield* bus.publish(Reference.Event.Updated, {})
}),
),
})
// Check independently of session activity; the shared cache throttles Git work daily.
yield* Effect.sleep("1 hour").pipe(Effect.andThen(refresh()), Effect.forever, Effect.forkScoped)
return Service.of({
transform: state.transform,
reload: state.reload,
list: Effect.fn("Reference.list")(function* () {
return list()
}),
})
}),
)
export const node = makeLocationNode({
service: Service,
layer,
deps: [Global.node, Bus.node, RepositoryCache.node],
})