FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
opencode/packages/opencode/src/project/instance-store.ts at dev · argszero/opencode · GitHub
argszero
/
opencode
Public
forked from
anomalyco/opencode
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
opencode
/
packages
/
opencode
/
src
/
project
/
instance-store.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
213 lines (187 loc) · 8.4 KB
Breadcrumbs
opencode
/
packages
/
opencode
/
src
/
project
/
instance-store.ts
Copy path
File metadata and controls
213 lines (187 loc) · 8.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import
{
LayerNode
}
from
"@opencode-ai/core/effect/layer-node"
import
{
makeGlobalNode
,
Node
}
from
"@opencode-ai/core/effect/app-node"
import
{
GlobalBus
}
from
"@/bus/global"
import
{
serviceUse
}
from
"@opencode-ai/core/effect/service-use"
import
{
WorkspaceContext
}
from
"@/control-plane/workspace-context"
import
{
InstanceRef
}
from
"@/effect/instance-ref"
import
{
disposeInstance
as
runDisposers
}
from
"@/effect/instance-registry"
import
{
FSUtil
}
from
"@opencode-ai/core/fs-util"
import
{
Context
,
Deferred
,
Duration
,
Effect
,
Exit
,
Layer
,
Scope
}
from
"effect"
import
{
type
InstanceContext
}
from
"./instance-context"
import
{
InstanceBootstrap
}
from
"./bootstrap-service"
import
*
as
Project
from
"./project"
export
interface
LoadInput
{
directory
:
string
worktree
?:
string
project
?:
Project
.
Info
}
export
interface
Interface
{
readonly
load
:
(
input
:
LoadInput
)
=>
Effect
.
Effect
<
InstanceContext
>
readonly
reload
:
(
input
:
LoadInput
)
=>
Effect
.
Effect
<
InstanceContext
>
readonly
dispose
:
(
ctx
:
InstanceContext
)
=>
Effect
.
Effect
<
void
>
readonly
disposeDirectory
:
(
directory
:
string
)
=>
Effect
.
Effect
<
void
>
readonly
disposeAll
:
(
)
=>
Effect
.
Effect
<
void
>
readonly
provide
:
<
A
,
E
,
R
>
(
input
:
LoadInput
,
effect
:
Effect
.
Effect
<
A
,
E
,
R
>
)
=>
Effect
.
Effect
<
A
,
E
,
R
>
}
export
class
Service
extends
Context
.
Service
<
Service
,
Interface
>
(
)
(
"@opencode/InstanceStore"
)
{
}
export
const
use
=
serviceUse
(
Service
)
interface
Entry
{
readonly
deferred
:
Deferred
.
Deferred
<
InstanceContext
>
}
const
layer
:
Layer
.
Layer
<
Service
,
never
,
Project
.
Service
|
InstanceBootstrap
.
Service
>
=
Layer
.
effect
(
Service
,
Effect
.
gen
(
function
*
(
)
{
const
project
=
yield
*
Project
.
Service
const
bootstrap
=
yield
*
InstanceBootstrap
.
Service
const
scope
=
yield
*
Scope
.
Scope
const
cache
=
new
Map
<
string
,
Entry
>
(
)
const
boot
=
(
input
:
LoadInput
&
{
directory
:
string
}
)
=>
Effect
.
gen
(
function
*
(
)
{
const
ctx
:
InstanceContext
=
input
.
project
&&
input
.
worktree
?
{
directory
:
input
.
directory
,
worktree
:
input
.
worktree
,
project
:
input
.
project
,
}
:
yield
*
project
.
fromDirectory
(
input
.
directory
)
.
pipe
(
Effect
.
map
(
(
result
)
=>
(
{
directory
:
input
.
directory
,
worktree
:
result
.
sandbox
,
project
:
result
.
project
,
}
)
)
,
)
yield
*
bootstrap
.
run
.
pipe
(
Effect
.
provideService
(
InstanceRef
,
ctx
)
)
return
ctx
}
)
.
pipe
(
Effect
.
withSpan
(
"InstanceStore.boot"
)
)
const
removeEntry
=
(
directory
:
string
,
entry
:
Entry
)
=>
Effect
.
sync
(
(
)
=>
{
if
(
cache
.
get
(
directory
)
!==
entry
)
return
false
cache
.
delete
(
directory
)
return
true
}
)
const
completeLoad
=
(
directory
:
string
,
input
:
LoadInput
,
entry
:
Entry
)
=>
Effect
.
gen
(
function
*
(
)
{
const
exit
=
yield
*
Effect
.
exit
(
boot
(
{
...
input
,
directory
}
)
)
if
(
Exit
.
isFailure
(
exit
)
)
yield
*
removeEntry
(
directory
,
entry
)
yield
*
Deferred
.
done
(
entry
.
deferred
,
exit
)
.
pipe
(
Effect
.
asVoid
)
}
)
const
emitDisposed
=
(
input
:
{
directory
:
string
;
project
?:
string
}
)
=>
Effect
.
sync
(
(
)
=>
GlobalBus
.
emit
(
"event"
,
{
directory
:
input
.
directory
,
project
:
input
.
project
,
workspace
:
WorkspaceContext
.
workspaceID
,
payload
:
{
type
:
"server.instance.disposed"
,
properties
:
{
directory
:
input
.
directory
,
}
,
}
,
}
)
,
)
const
disposeContext
=
Effect
.
fn
(
"InstanceStore.disposeContext"
)
(
function
*
(
ctx
:
InstanceContext
)
{
yield
*
Effect
.
logInfo
(
"disposing instance"
,
{
directory
:
ctx
.
directory
}
)
yield
*
Effect
.
promise
(
(
)
=>
runDisposers
(
ctx
.
directory
)
)
yield
*
emitDisposed
(
{
directory
:
ctx
.
directory
,
project
:
ctx
.
project
.
id
}
)
}
)
const
disposeEntry
=
Effect
.
fnUntraced
(
function
*
(
directory
:
string
,
entry
:
Entry
,
ctx
:
InstanceContext
)
{
if
(
cache
.
get
(
directory
)
!==
entry
)
return
false
yield
*
disposeContext
(
ctx
)
if
(
cache
.
get
(
directory
)
!==
entry
)
return
false
cache
.
delete
(
directory
)
return
true
}
)
const
load
=
(
input
:
LoadInput
)
:
Effect
.
Effect
<
InstanceContext
>
=>
{
const
directory
=
FSUtil
.
resolve
(
input
.
directory
)
return
Effect
.
uninterruptibleMask
(
(
restore
)
=>
Effect
.
gen
(
function
*
(
)
{
const
existing
=
cache
.
get
(
directory
)
if
(
existing
)
return
yield
*
restore
(
Deferred
.
await
(
existing
.
deferred
)
)
const
entry
:
Entry
=
{
deferred
:
Deferred
.
makeUnsafe
<
InstanceContext
>
(
)
}
cache
.
set
(
directory
,
entry
)
yield
*
Effect
.
gen
(
function
*
(
)
{
yield
*
Effect
.
logInfo
(
"creating instance"
,
{
directory
:
directory
}
)
yield
*
completeLoad
(
directory
,
input
,
entry
)
}
)
.
pipe
(
Effect
.
forkIn
(
scope
,
{
startImmediately
:
true
}
)
)
return
yield
*
restore
(
Deferred
.
await
(
entry
.
deferred
)
)
}
)
,
)
.
pipe
(
Effect
.
withSpan
(
"InstanceStore.load"
)
)
}
const
reload
=
(
input
:
LoadInput
)
:
Effect
.
Effect
<
InstanceContext
>
=>
{
const
directory
=
FSUtil
.
resolve
(
input
.
directory
)
return
Effect
.
uninterruptibleMask
(
(
restore
)
=>
Effect
.
gen
(
function
*
(
)
{
const
previous
=
cache
.
get
(
directory
)
const
entry
:
Entry
=
{
deferred
:
Deferred
.
makeUnsafe
<
InstanceContext
>
(
)
}
cache
.
set
(
directory
,
entry
)
yield
*
Effect
.
gen
(
function
*
(
)
{
yield
*
Effect
.
logInfo
(
"reloading instance"
,
{
directory
:
directory
}
)
if
(
previous
)
{
yield
*
Deferred
.
await
(
previous
.
deferred
)
.
pipe
(
Effect
.
ignore
)
yield
*
Effect
.
promise
(
(
)
=>
runDisposers
(
directory
)
)
yield
*
emitDisposed
(
{
directory
,
project
:
input
.
project
?.
id
}
)
}
yield
*
completeLoad
(
directory
,
input
,
entry
)
}
)
.
pipe
(
Effect
.
forkIn
(
scope
,
{
startImmediately
:
true
}
)
)
return
yield
*
restore
(
Deferred
.
await
(
entry
.
deferred
)
)
}
)
,
)
.
pipe
(
Effect
.
withSpan
(
"InstanceStore.reload"
)
)
}
const
dispose
=
Effect
.
fn
(
"InstanceStore.dispose"
)
(
function
*
(
ctx
:
InstanceContext
)
{
const
entry
=
cache
.
get
(
ctx
.
directory
)
if
(
!
entry
)
return
yield
*
disposeContext
(
ctx
)
const
exit
=
yield
*
Deferred
.
await
(
entry
.
deferred
)
.
pipe
(
Effect
.
exit
)
if
(
Exit
.
isFailure
(
exit
)
)
return
yield
*
removeEntry
(
ctx
.
directory
,
entry
)
.
pipe
(
Effect
.
asVoid
)
if
(
exit
.
value
!==
ctx
)
return
yield
*
disposeEntry
(
ctx
.
directory
,
entry
,
ctx
)
.
pipe
(
Effect
.
asVoid
)
}
)
const
disposeDirectory
=
Effect
.
fn
(
"InstanceStore.disposeDirectory"
)
(
function
*
(
input
:
string
)
{
const
directory
=
FSUtil
.
resolve
(
input
)
const
entry
=
cache
.
get
(
directory
)
if
(
!
entry
)
return
const
exit
=
yield
*
Deferred
.
await
(
entry
.
deferred
)
.
pipe
(
Effect
.
exit
)
if
(
Exit
.
isFailure
(
exit
)
)
return
yield
*
removeEntry
(
directory
,
entry
)
.
pipe
(
Effect
.
asVoid
)
yield
*
disposeEntry
(
directory
,
entry
,
exit
.
value
)
.
pipe
(
Effect
.
asVoid
)
}
)
const
disposeAllOnce
=
Effect
.
fnUntraced
(
function
*
(
)
{
yield
*
Effect
.
logInfo
(
"disposing all instances"
)
yield
*
Effect
.
forEach
(
[
...
cache
.
entries
(
)
]
,
(
item
)
=>
Effect
.
gen
(
function
*
(
)
{
const
exit
=
yield
*
Deferred
.
await
(
item
[
1
]
.
deferred
)
.
pipe
(
Effect
.
exit
)
if
(
Exit
.
isFailure
(
exit
)
)
{
yield
*
Effect
.
logWarning
(
"instance dispose failed"
,
{
key
:
item
[
0
]
,
cause
:
exit
.
cause
}
)
yield
*
removeEntry
(
item
[
0
]
,
item
[
1
]
)
return
}
yield
*
disposeEntry
(
item
[
0
]
,
item
[
1
]
,
exit
.
value
)
}
)
,
{
discard
:
true
}
,
)
}
)
const
cachedDisposeAll
=
yield
*
Effect
.
cachedWithTTL
(
disposeAllOnce
(
)
,
Duration
.
zero
)
const
disposeAll
=
Effect
.
fn
(
"InstanceStore.disposeAll"
)
(
function
*
(
)
{
return
yield
*
cachedDisposeAll
}
)
const
provide
=
<
A
,
E
,
R
>
(
input
:
LoadInput
,
effect
:
Effect
.
Effect
<
A
,
E
,
R
>
)
:
Effect
.
Effect
<
A
,
E
,
R
>
=>
load
(
input
)
.
pipe
(
Effect
.
flatMap
(
(
ctx
)
=>
effect
.
pipe
(
Effect
.
provideService
(
InstanceRef
,
ctx
)
)
)
)
yield
*
Effect
.
addFinalizer
(
(
)
=>
disposeAll
(
)
.
pipe
(
Effect
.
ignore
)
)
return
Service
.
of
(
{
load
,
reload
,
dispose
,
disposeDirectory
,
disposeAll
,
provide
,
}
)
}
)
,
)
export
const
bootstrapNode
=
LayerNode
.
unbound
(
InstanceBootstrap
.
Service
,
Node
.
tags
.
values
.
global
)
export
const
node
=
makeGlobalNode
(
{
service
:
Service
,
layer
:
layer
,
deps
:
[
Project
.
node
,
bootstrapNode
]
,
}
)
export
*
as
InstanceStore
from
"./instance-store"
Back
|
FazBrowse Home
|
New Git URL