-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmanager.ts
More file actions
205 lines (174 loc) · 5.74 KB
/
Copy pathmanager.ts
File metadata and controls
205 lines (174 loc) · 5.74 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import type { OpencodeClient } from '@opencode-ai/sdk'
import { semver } from 'bun'
import { Terminal } from 'bun-pty'
import { version as bunPtyVersion } from 'bun-pty/package.json'
import { NotificationManager } from './notification-manager.ts'
import { OutputManager } from './output-manager.ts'
import { SessionLifecycleManager } from './session-lifecycle.ts'
import type { SnapshotDiff, WaitCondition, WaitResult } from './snapshot.ts'
import type {
PTYSessionInfo,
PTYStatus,
ReadResult,
SearchResult,
SnapshotResult,
SpawnOptions,
} from './types.ts'
import { withSession } from './utils.ts'
// Monkey-patch bun-pty to fix race condition in _startReadLoop
// Temporary workaround until https://github.com/sursaone/bun-pty/pull/37 is merged
if (semver.order(bunPtyVersion, '0.4.8') > 0) {
throw new Error(
`bun-pty version ${bunPtyVersion} is too new for patching; remove the workaround.`
)
}
const proto = Terminal.prototype as unknown as { _startReadLoop?: (...args: unknown[]) => unknown }
const original = proto._startReadLoop
if (typeof original === 'function') {
proto._startReadLoop = async function (this: InstanceType<typeof Terminal>, ...args: unknown[]) {
await Promise.resolve() // Yield to allow event handlers to be registered
return original.apply(this, args)
}
}
type SessionUpdateCallback = (session: PTYSessionInfo) => void
export const sessionUpdateCallbacks: SessionUpdateCallback[] = []
export function registerSessionUpdateCallback(callback: SessionUpdateCallback) {
sessionUpdateCallbacks.push(callback)
}
export function removeSessionUpdateCallback(callback: SessionUpdateCallback) {
const index = sessionUpdateCallbacks.indexOf(callback)
if (index !== -1) {
sessionUpdateCallbacks.splice(index, 1)
}
}
function notifySessionUpdate(session: PTYSessionInfo) {
for (const callback of sessionUpdateCallbacks) {
try {
callback(session)
} catch {
// Ignore callback errors
}
}
}
type RawOutputCallback = (session: PTYSessionInfo, rawData: string) => void
export const rawOutputCallbacks: RawOutputCallback[] = []
export function registerRawOutputCallback(callback: RawOutputCallback): void {
rawOutputCallbacks.push(callback)
}
export function removeRawOutputCallback(callback: RawOutputCallback): void {
const index = rawOutputCallbacks.indexOf(callback)
if (index !== -1) {
rawOutputCallbacks.splice(index, 1)
}
}
function notifyRawOutput(session: PTYSessionInfo, rawData: string): void {
for (const callback of rawOutputCallbacks) {
try {
callback(session, rawData)
} catch {
// Ignore callback errors
}
}
}
class PTYManager {
private lifecycleManager = new SessionLifecycleManager()
private outputManager = new OutputManager()
private notificationManager = new NotificationManager()
init(client: OpencodeClient): void {
this.notificationManager.init(client)
}
clearAllSessions(): void {
this.lifecycleManager.clearAllSessions()
}
spawn(opts: SpawnOptions): PTYSessionInfo {
const session = this.lifecycleManager.spawn(
opts,
(session, data) => {
notifyRawOutput(this.lifecycleManager.toInfo(session), data)
},
async (session, exitCode) => {
notifySessionUpdate(this.lifecycleManager.toInfo(session))
if (session?.notifyOnExit) {
await this.notificationManager.sendExitNotification(session, exitCode || 0)
}
}
)
notifySessionUpdate(session)
return session
}
write(id: string, data: string): boolean {
return withSession(
this.lifecycleManager,
id,
(session) => this.outputManager.write(session, data),
false
)
}
read(id: string, offset: number = 0, limit?: number): ReadResult | null {
return withSession(
this.lifecycleManager,
id,
(session) => this.outputManager.read(session, offset, limit),
null
)
}
search(id: string, pattern: RegExp, offset: number = 0, limit?: number): SearchResult | null {
return withSession(
this.lifecycleManager,
id,
(session) => this.outputManager.search(session, pattern, offset, limit),
null
)
}
list(): PTYSessionInfo[] {
return this.lifecycleManager.listSessions().map((s) => this.lifecycleManager.toInfo(s))
}
get(id: string): PTYSessionInfo | null {
return withSession(
this.lifecycleManager,
id,
(session) => this.lifecycleManager.toInfo(session),
null
)
}
getRawBuffer(id: string): { raw: string; byteLength: number } | null {
return withSession(
this.lifecycleManager,
id,
(session) => ({
raw: session.buffer.readRaw(),
byteLength: session.buffer.byteLength,
}),
null
)
}
snapshot(id: string): SnapshotResult | null {
return withSession(this.lifecycleManager, id, (session) => this.outputManager.snapshot(session), null)
}
snapshotDiff(id: string, sinceSeq: number): (SnapshotDiff & { id: string; status: PTYStatus }) | null {
return withSession(
this.lifecycleManager,
id,
(session) => this.outputManager.snapshotDiff(session, sinceSeq),
null
)
}
async snapshotWait(
id: string,
condition: WaitCondition
): Promise<(WaitResult & { id: string; status: string }) | null> {
const session = this.lifecycleManager.getSession(id)
if (!session) return null
return this.outputManager.snapshotWait(session, condition)
}
kill(id: string, cleanup: boolean = false): boolean {
return this.lifecycleManager.kill(id, cleanup)
}
cleanupBySession(parentSessionId: string): void {
this.lifecycleManager.cleanupBySession(parentSessionId)
}
}
export const manager = new PTYManager()
export function initManager(opcClient: OpencodeClient): void {
manager.init(opcClient)
}