Repository navigation
Expand file tree
/
Copy pathbash-interactive.ts
More file actions
183 lines (157 loc) · 4.8 KB
/
Copy pathbash-interactive.ts
File metadata and controls
183 lines (157 loc) · 4.8 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
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
import { Deferred, Effect, Layer, Context } from "effect"
import { Bus } from "@/bus"
import { BusEvent } from "@/bus/bus-event"
import { InstanceState } from "@/effect"
import { Log } from "@/util"
import z from "zod"
import crypto from "crypto"
const log = Log.create({ service: "bash-interactive" })
// Bus events
export const Event = {
Asked: BusEvent.define(
"bash.interactive.asked",
z.object({
id: z.string(),
command: z.string(),
cwd: z.string(),
env: z.record(z.string(), z.string()).optional(),
description: z.string(),
}),
),
Replied: BusEvent.define(
"bash.interactive.replied",
z.object({
id: z.string(),
output: z.string(),
exitCode: z.number(),
}),
),
}
// Types
export interface InteractiveRequest {
id: string
command: string
cwd: string
env?: Record<string, string>
description: string
}
export interface InteractiveResult {
output: string
exitCode: number
}
export class InteractiveError extends Error {
constructor(message: string) {
super(message)
this.name = "BashInteractiveError"
}
}
// Service
interface PendingEntry {
request: InteractiveRequest
deferred: Deferred.Deferred<InteractiveResult, InteractiveError>
}
interface State {
pending: Map<string, PendingEntry>
}
export interface Interface {
readonly request: (input: {
command: string
cwd: string
env?: Record<string, string>
description: string
}) => Effect.Effect<InteractiveResult, InteractiveError>
readonly reply: (input: { id: string; output: string; exitCode: number }) => Effect.Effect<void>
readonly list: () => Effect.Effect<ReadonlyArray<InteractiveRequest>>
}
export class Service extends Context.Service<Service, Interface>()("@opencode/BashInteractive") {}
export const layer = Layer.effect(
Service,
Effect.gen(function* () {
const bus = yield* Bus.Service
const state = yield* InstanceState.make<State>(
Effect.fn("BashInteractive.state")(function* () {
const state: State = {
pending: new Map(),
}
yield* Effect.addFinalizer(() =>
Effect.gen(function* () {
for (const item of state.pending.values()) {
yield* Deferred.fail(item.deferred, new InteractiveError("Instance disposed"))
}
state.pending.clear()
}),
)
return state
}),
)
const request = Effect.fn("BashInteractive.request")(function* (input: {
command: string
cwd: string
env?: Record<string, string>
description: string
}) {
const pending = (yield* InstanceState.get(state)).pending
const id = crypto.randomUUID()
log.info("requesting interactive", { id, command: input.command })
const deferred = yield* Deferred.make<InteractiveResult, InteractiveError>()
const req: InteractiveRequest = {
id,
command: input.command,
cwd: input.cwd,
env: input.env,
description: input.description,
}
pending.set(id, { request: req, deferred })
yield* bus.publish(Event.Asked, req)
return yield* Effect.ensuring(
Deferred.await(deferred),
Effect.sync(() => {
pending.delete(id)
}),
)
})
const reply = Effect.fn("BashInteractive.reply")(function* (input: {
id: string
output: string
exitCode: number
}) {
const pending = (yield* InstanceState.get(state)).pending
const existing = pending.get(input.id)
if (!existing) {
log.warn("reply for unknown request", { id: input.id })
return
}
pending.delete(input.id)
log.info("replied", { id: input.id, exitCode: input.exitCode })
yield* bus.publish(Event.Replied, {
id: existing.request.id,
output: input.output,
exitCode: input.exitCode,
})
yield* Deferred.succeed(existing.deferred, {
output: input.output,
exitCode: input.exitCode,
})
})
const list = Effect.fn("BashInteractive.list")(function* () {
const pending = (yield* InstanceState.get(state)).pending
return Array.from(pending.values(), (x) => x.request)
})
return Service.of({ request, reply, list })
}),
)
export const defaultLayer = layer.pipe(Layer.provide(Bus.layer))
// Standalone functions (uses the instance-scoped runtime, same pattern as Bus module)
import { makeRuntime } from "@/effect/run-service"
const { runPromise } = makeRuntime(Service, defaultLayer)
export function request(input: {
command: string
cwd: string
env?: Record<string, string>
description: string
}): Promise<InteractiveResult> {
return runPromise((svc) => svc.request(input))
}
export function reply(input: { id: string; output: string; exitCode: number }): Promise<void> {
return runPromise((svc) => svc.reply(input))
}