Skip to content

Commit 5fd833a

Browse files
authored
refactor: standardize InstanceState variable name to state (#20267)
1 parent 44f8301 commit 5fd833a

8 files changed

Lines changed: 74 additions & 74 deletions

File tree

packages/opencode/src/bus/index.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export namespace Bus {
4646
export const layer = Layer.effect(
4747
Service,
4848
Effect.gen(function* () {
49-
const cache = yield* InstanceState.make<State>(
49+
const state = yield* InstanceState.make<State>(
5050
Effect.fn("Bus.state")(function* (ctx) {
5151
const wildcard = yield* PubSub.unbounded<Payload>()
5252
const typed = new Map<string, PubSub.PubSub<Payload>>()
@@ -82,13 +82,13 @@ export namespace Bus {
8282

8383
function publish<D extends BusEvent.Definition>(def: D, properties: z.output<D["properties"]>) {
8484
return Effect.gen(function* () {
85-
const state = yield* InstanceState.get(cache)
85+
const s = yield* InstanceState.get(state)
8686
const payload: Payload = { type: def.type, properties }
8787
log.info("publishing", { type: def.type })
8888

89-
const ps = state.typed.get(def.type)
89+
const ps = s.typed.get(def.type)
9090
if (ps) yield* PubSub.publish(ps, payload)
91-
yield* PubSub.publish(state.wildcard, payload)
91+
yield* PubSub.publish(s.wildcard, payload)
9292

9393
const dir = yield* InstanceState.directory
9494
GlobalBus.emit("event", {
@@ -102,8 +102,8 @@ export namespace Bus {
102102
log.info("subscribing", { type: def.type })
103103
return Stream.unwrap(
104104
Effect.gen(function* () {
105-
const state = yield* InstanceState.get(cache)
106-
const ps = yield* getOrCreate(state, def)
105+
const s = yield* InstanceState.get(state)
106+
const ps = yield* getOrCreate(s, def)
107107
return Stream.fromPubSub(ps)
108108
}),
109109
).pipe(Stream.ensuring(Effect.sync(() => log.info("unsubscribing", { type: def.type }))))
@@ -113,8 +113,8 @@ export namespace Bus {
113113
log.info("subscribing", { type: "*" })
114114
return Stream.unwrap(
115115
Effect.gen(function* () {
116-
const state = yield* InstanceState.get(cache)
117-
return Stream.fromPubSub(state.wildcard)
116+
const s = yield* InstanceState.get(state)
117+
return Stream.fromPubSub(s.wildcard)
118118
}),
119119
).pipe(Stream.ensuring(Effect.sync(() => log.info("unsubscribing", { type: "*" }))))
120120
}
@@ -150,14 +150,14 @@ export namespace Bus {
150150
def: D,
151151
callback: (event: Payload<D>) => unknown,
152152
) {
153-
const state = yield* InstanceState.get(cache)
154-
const ps = yield* getOrCreate(state, def)
153+
const s = yield* InstanceState.get(state)
154+
const ps = yield* getOrCreate(s, def)
155155
return yield* on(ps, def.type, callback)
156156
})
157157

158158
const subscribeAllCallback = Effect.fn("Bus.subscribeAllCallback")(function* (callback: (event: any) => unknown) {
159-
const state = yield* InstanceState.get(cache)
160-
return yield* on(state.wildcard, "*", callback)
159+
const s = yield* InstanceState.get(state)
160+
return yield* on(s.wildcard, "*", callback)
161161
})
162162

163163
return Service.of({ publish, subscribe, subscribeAll, subscribeCallback, subscribeAllCallback })

packages/opencode/src/command/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,16 @@ export namespace Command {
161161
}
162162
})
163163

164-
const cache = yield* InstanceState.make<State>((ctx) => init(ctx))
164+
const state = yield* InstanceState.make<State>((ctx) => init(ctx))
165165

166166
const get = Effect.fn("Command.get")(function* (name: string) {
167-
const state = yield* InstanceState.get(cache)
168-
return state.commands[name]
167+
const s = yield* InstanceState.get(state)
168+
return s.commands[name]
169169
})
170170

171171
const list = Effect.fn("Command.list")(function* () {
172-
const state = yield* InstanceState.get(cache)
173-
return Object.values(state.commands)
172+
const s = yield* InstanceState.get(state)
173+
return Object.values(s.commands)
174174
})
175175

176176
return Service.of({ get, list })

packages/opencode/src/mcp/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ export namespace MCP {
477477
})
478478
}
479479

480-
const cache = yield* InstanceState.make<State>(
480+
const state = yield* InstanceState.make<State>(
481481
Effect.fn("MCP.state")(function* () {
482482
const cfg = yield* cfgSvc.get()
483483
const config = cfg.mcp ?? {}
@@ -549,7 +549,7 @@ export namespace MCP {
549549
}
550550

551551
const status = Effect.fn("MCP.status")(function* () {
552-
const s = yield* InstanceState.get(cache)
552+
const s = yield* InstanceState.get(state)
553553

554554
const cfg = yield* cfgSvc.get()
555555
const config = cfg.mcp ?? {}
@@ -564,12 +564,12 @@ export namespace MCP {
564564
})
565565

566566
const clients = Effect.fn("MCP.clients")(function* () {
567-
const s = yield* InstanceState.get(cache)
567+
const s = yield* InstanceState.get(state)
568568
return s.clients
569569
})
570570

571571
const createAndStore = Effect.fn("MCP.createAndStore")(function* (name: string, mcp: Config.Mcp) {
572-
const s = yield* InstanceState.get(cache)
572+
const s = yield* InstanceState.get(state)
573573
const result = yield* create(name, mcp)
574574

575575
s.status[name] = result.status
@@ -588,7 +588,7 @@ export namespace MCP {
588588

589589
const add = Effect.fn("MCP.add")(function* (name: string, mcp: Config.Mcp) {
590590
yield* createAndStore(name, mcp)
591-
const s = yield* InstanceState.get(cache)
591+
const s = yield* InstanceState.get(state)
592592
return { status: s.status }
593593
})
594594

@@ -602,15 +602,15 @@ export namespace MCP {
602602
})
603603

604604
const disconnect = Effect.fn("MCP.disconnect")(function* (name: string) {
605-
const s = yield* InstanceState.get(cache)
605+
const s = yield* InstanceState.get(state)
606606
yield* closeClient(s, name)
607607
delete s.clients[name]
608608
s.status[name] = { status: "disabled" }
609609
})
610610

611611
const tools = Effect.fn("MCP.tools")(function* () {
612612
const result: Record<string, Tool> = {}
613-
const s = yield* InstanceState.get(cache)
613+
const s = yield* InstanceState.get(state)
614614

615615
const cfg = yield* cfgSvc.get()
616616
const config = cfg.mcp ?? {}
@@ -657,12 +657,12 @@ export namespace MCP {
657657
}
658658

659659
const prompts = Effect.fn("MCP.prompts")(function* () {
660-
const s = yield* InstanceState.get(cache)
660+
const s = yield* InstanceState.get(state)
661661
return yield* collectFromConnected(s, (c) => c.listPrompts().then((r) => r.prompts), "prompts")
662662
})
663663

664664
const resources = Effect.fn("MCP.resources")(function* () {
665-
const s = yield* InstanceState.get(cache)
665+
const s = yield* InstanceState.get(state)
666666
return yield* collectFromConnected(s, (c) => c.listResources().then((r) => r.resources), "resources")
667667
})
668668

@@ -672,7 +672,7 @@ export namespace MCP {
672672
label: string,
673673
meta?: Record<string, unknown>,
674674
) {
675-
const s = yield* InstanceState.get(cache)
675+
const s = yield* InstanceState.get(state)
676676
const client = s.clients[clientName]
677677
if (!client) {
678678
log.warn(`client not found for ${label}`, { clientName })

packages/opencode/src/plugin/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export namespace Plugin {
103103
const bus = yield* Bus.Service
104104
const config = yield* Config.Service
105105

106-
const cache = yield* InstanceState.make<State>(
106+
const state = yield* InstanceState.make<State>(
107107
Effect.fn("Plugin.state")(function* (ctx) {
108108
const hooks: Hooks[] = []
109109

@@ -279,8 +279,8 @@ export namespace Plugin {
279279
Output = Parameters<Required<Hooks>[Name]>[1],
280280
>(name: Name, input: Input, output: Output) {
281281
if (!name) return output
282-
const state = yield* InstanceState.get(cache)
283-
for (const hook of state.hooks) {
282+
const s = yield* InstanceState.get(state)
283+
for (const hook of s.hooks) {
284284
const fn = hook[name] as any
285285
if (!fn) continue
286286
yield* Effect.promise(async () => fn(input, output))
@@ -289,12 +289,12 @@ export namespace Plugin {
289289
})
290290

291291
const list = Effect.fn("Plugin.list")(function* () {
292-
const state = yield* InstanceState.get(cache)
293-
return state.hooks
292+
const s = yield* InstanceState.get(state)
293+
return s.hooks
294294
})
295295

296296
const init = Effect.fn("Plugin.init")(function* () {
297-
yield* InstanceState.get(cache)
297+
yield* InstanceState.get(state)
298298
})
299299

300300
return Service.of({ trigger, list, init })

packages/opencode/src/provider/provider.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ export namespace Provider {
967967
const config = yield* Config.Service
968968
const auth = yield* Auth.Service
969969

970-
const cache = yield* InstanceState.make<State>(() =>
970+
const state = yield* InstanceState.make<State>(() =>
971971
Effect.gen(function* () {
972972
using _ = log.time("state")
973973
const cfg = yield* config.get()
@@ -1247,7 +1247,7 @@ export namespace Provider {
12471247
}),
12481248
)
12491249

1250-
const list = Effect.fn("Provider.list")(() => InstanceState.use(cache, (s) => s.providers))
1250+
const list = Effect.fn("Provider.list")(() => InstanceState.use(state, (s) => s.providers))
12511251

12521252
async function resolveSDK(model: Model, s: State) {
12531253
try {
@@ -1385,11 +1385,11 @@ export namespace Provider {
13851385
}
13861386

13871387
const getProvider = Effect.fn("Provider.getProvider")((providerID: ProviderID) =>
1388-
InstanceState.use(cache, (s) => s.providers[providerID]),
1388+
InstanceState.use(state, (s) => s.providers[providerID]),
13891389
)
13901390

13911391
const getModel = Effect.fn("Provider.getModel")(function* (providerID: ProviderID, modelID: ModelID) {
1392-
const s = yield* InstanceState.get(cache)
1392+
const s = yield* InstanceState.get(state)
13931393
const provider = s.providers[providerID]
13941394
if (!provider) {
13951395
const available = Object.keys(s.providers)
@@ -1407,7 +1407,7 @@ export namespace Provider {
14071407
})
14081408

14091409
const getLanguage = Effect.fn("Provider.getLanguage")(function* (model: Model) {
1410-
const s = yield* InstanceState.get(cache)
1410+
const s = yield* InstanceState.get(state)
14111411
const key = `${model.providerID}/${model.id}`
14121412
if (s.models.has(key)) return s.models.get(key)!
14131413

@@ -1439,7 +1439,7 @@ export namespace Provider {
14391439
})
14401440

14411441
const closest = Effect.fn("Provider.closest")(function* (providerID: ProviderID, query: string[]) {
1442-
const s = yield* InstanceState.get(cache)
1442+
const s = yield* InstanceState.get(state)
14431443
const provider = s.providers[providerID]
14441444
if (!provider) return undefined
14451445
for (const item of query) {
@@ -1458,7 +1458,7 @@ export namespace Provider {
14581458
return yield* getModel(parsed.providerID, parsed.modelID)
14591459
}
14601460

1461-
const s = yield* InstanceState.get(cache)
1461+
const s = yield* InstanceState.get(state)
14621462
const provider = s.providers[providerID]
14631463
if (!provider) return undefined
14641464

@@ -1510,7 +1510,7 @@ export namespace Provider {
15101510
const cfg = yield* config.get()
15111511
if (cfg.model) return parseModel(cfg.model)
15121512

1513-
const s = yield* InstanceState.get(cache)
1513+
const s = yield* InstanceState.get(state)
15141514
const recent = yield* Effect.promise(() =>
15151515
Filesystem.readJson<{
15161516
recent?: { providerID: ProviderID; modelID: ModelID }[]

packages/opencode/src/pty/index.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export namespace Pty {
130130
session.subscribers.clear()
131131
}
132132

133-
const cache = yield* InstanceState.make<State>(
133+
const state = yield* InstanceState.make<State>(
134134
Effect.fn("Pty.state")(function* (ctx) {
135135
const state = {
136136
dir: ctx.directory,
@@ -151,27 +151,27 @@ export namespace Pty {
151151
)
152152

153153
const remove = Effect.fn("Pty.remove")(function* (id: PtyID) {
154-
const state = yield* InstanceState.get(cache)
155-
const session = state.sessions.get(id)
154+
const s = yield* InstanceState.get(state)
155+
const session = s.sessions.get(id)
156156
if (!session) return
157-
state.sessions.delete(id)
157+
s.sessions.delete(id)
158158
log.info("removing session", { id })
159159
teardown(session)
160160
void Bus.publish(Event.Deleted, { id: session.info.id })
161161
})
162162

163163
const list = Effect.fn("Pty.list")(function* () {
164-
const state = yield* InstanceState.get(cache)
165-
return Array.from(state.sessions.values()).map((session) => session.info)
164+
const s = yield* InstanceState.get(state)
165+
return Array.from(s.sessions.values()).map((session) => session.info)
166166
})
167167

168168
const get = Effect.fn("Pty.get")(function* (id: PtyID) {
169-
const state = yield* InstanceState.get(cache)
170-
return state.sessions.get(id)?.info
169+
const s = yield* InstanceState.get(state)
170+
return s.sessions.get(id)?.info
171171
})
172172

173173
const create = Effect.fn("Pty.create")(function* (input: CreateInput) {
174-
const state = yield* InstanceState.get(cache)
174+
const s = yield* InstanceState.get(state)
175175
return yield* Effect.promise(async () => {
176176
const id = PtyID.ascending()
177177
const command = input.command || Shell.preferred()
@@ -180,7 +180,7 @@ export namespace Pty {
180180
args.push("-l")
181181
}
182182

183-
const cwd = input.cwd || state.dir
183+
const cwd = input.cwd || s.dir
184184
const shellEnv = await Plugin.trigger("shell.env", { cwd }, { env: {} })
185185
const env = {
186186
...process.env,
@@ -221,7 +221,7 @@ export namespace Pty {
221221
cursor: 0,
222222
subscribers: new Map(),
223223
}
224-
state.sessions.set(id, session)
224+
s.sessions.set(id, session)
225225
proc.onData(
226226
Instance.bind((chunk) => {
227227
session.cursor += chunk.length
@@ -264,8 +264,8 @@ export namespace Pty {
264264
})
265265

266266
const update = Effect.fn("Pty.update")(function* (id: PtyID, input: UpdateInput) {
267-
const state = yield* InstanceState.get(cache)
268-
const session = state.sessions.get(id)
267+
const s = yield* InstanceState.get(state)
268+
const session = s.sessions.get(id)
269269
if (!session) return
270270
if (input.title) {
271271
session.info.title = input.title
@@ -278,24 +278,24 @@ export namespace Pty {
278278
})
279279

280280
const resize = Effect.fn("Pty.resize")(function* (id: PtyID, cols: number, rows: number) {
281-
const state = yield* InstanceState.get(cache)
282-
const session = state.sessions.get(id)
281+
const s = yield* InstanceState.get(state)
282+
const session = s.sessions.get(id)
283283
if (session && session.info.status === "running") {
284284
session.process.resize(cols, rows)
285285
}
286286
})
287287

288288
const write = Effect.fn("Pty.write")(function* (id: PtyID, data: string) {
289-
const state = yield* InstanceState.get(cache)
290-
const session = state.sessions.get(id)
289+
const s = yield* InstanceState.get(state)
290+
const session = s.sessions.get(id)
291291
if (session && session.info.status === "running") {
292292
session.process.write(data)
293293
}
294294
})
295295

296296
const connect = Effect.fn("Pty.connect")(function* (id: PtyID, ws: Socket, cursor?: number) {
297-
const state = yield* InstanceState.get(cache)
298-
const session = state.sessions.get(id)
297+
const s = yield* InstanceState.get(state)
298+
const session = s.sessions.get(id)
299299
if (!session) {
300300
ws.close()
301301
return

0 commit comments

Comments
 (0)