-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathforge-worktree.ts
More file actions
257 lines (230 loc) · 12.2 KB
/
forge-worktree.ts
File metadata and controls
257 lines (230 loc) · 12.2 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* Forge workspace helpers using opencode's experimental workspace API.
*
* The recommended entry point is {@link createBuiltinWorktreeWorkspace}, which creates
* a Forge workspace with `type: 'forge'` through opencode's experimental adapter,
* then registers it via syncList so the TUI can show it as connected (green dot).
*
* Workspaces are created with `type: 'forge'` (not `type: 'worktree'`) because
* Forge uses its own adapter registered in the experimental workspace API.
*/
import type { OpencodeClient } from '@opencode-ai/sdk/v2'
import type { WorkspaceStatusRegistry } from '../utils/workspace-status-registry'
export interface ForgeWorkspaceEntry {
id: string
name?: string
type?: string
branch?: string | null
directory?: string | null
extra?: Record<string, unknown> | null
}
export function getForgeWorkspaceLoopName(entry: Pick<ForgeWorkspaceEntry, 'extra'>): string | undefined {
const loopName = entry.extra?.loopName
return typeof loopName === 'string' && loopName.length > 0 ? loopName : undefined
}
/**
* Look up existing forge workspaces by loop name.
*/
export async function findExistingForgeWorkspaces(
client: OpencodeClient,
loopName: string,
logger?: { log: (msg: string, ...args: unknown[]) => void; error: (msg: string, ...args: unknown[]) => void },
): Promise<ForgeWorkspaceEntry[]> {
const workspaceApi = client.experimental?.workspace
if (!workspaceApi || typeof workspaceApi.list !== 'function') return []
try {
const result = await workspaceApi.list()
const entries = ((result as { data?: unknown[] } | undefined)?.data ?? []) as ForgeWorkspaceEntry[]
const matches = entries.filter((entry) => entry.id && workspaceMatchesLoop(entry, loopName))
if (matches.length > 0) {
(logger ?? console).log?.(`findExistingForgeWorkspaces: found ${matches.length} existing workspace(s) for loop ${loopName}`)
}
return matches
} catch (err) {
(logger ?? console).error('findExistingForgeWorkspaces: workspace.list threw', err)
return []
}
}
export function workspaceMatchesLoop(entry: ForgeWorkspaceEntry, loopName: string): boolean {
if (entry.type !== 'forge') return false
if (entry.name === loopName) return true
return getForgeWorkspaceLoopName(entry) === loopName
}
export async function removeExistingForgeLoopWorkspaces(
client: OpencodeClient,
loopName: string,
logger?: { log: (msg: string, ...args: unknown[]) => void; error: (msg: string, ...args: unknown[]) => void },
): Promise<void> {
const workspaceApi = client.experimental?.workspace
if (!workspaceApi || typeof workspaceApi.remove !== 'function') return
const matches = await findExistingForgeWorkspaces(client, loopName, logger)
for (const match of matches) {
await workspaceApi.remove({ id: match.id })
;(logger ?? console).log?.(`removeExistingForgeLoopWorkspaces: removed old workspace ${match.id} for loop ${loopName}`)
}
}
/**
* Creates a Forge workspace via opencode's experimental workspace API with the `forge` adapter.
*
* Uses `experimental.workspace.create({ type: 'forge', branch: null })` so the
* workspace appears as fully connected (green dot) in the TUI.
*
* After a successful create, also issues a best-effort `experimental.workspace.syncList()`
* so the new workspace is registered in the Warp picker, not just reachable from the session list.
*
* @returns `{ workspaceId, directory, branch }` or `null` on failure.
*/
export async function createBuiltinWorktreeWorkspace(
client: OpencodeClient,
options: {
loopName: string
directory: string
},
logger?: { log: (msg: string, ...args: unknown[]) => void; error: (msg: string, ...args: unknown[]) => void },
statusRegistry?: WorkspaceStatusRegistry,
): Promise<{ workspaceId: string; directory: string; branch: string } | null> {
const workspaceApi = client.experimental?.workspace
if (!workspaceApi || typeof workspaceApi.create !== 'function') {
(logger ?? console).log?.('createBuiltinWorktreeWorkspace: experimental.workspace API not available')
return null
}
if (!options.directory) {
(logger ?? console).error('createBuiltinWorktreeWorkspace: options.directory is required')
return null
}
try {
const _wsStart = Date.now()
;(logger ?? console).log?.(`[warp] workspace.create.start loopName=${options.loopName}`)
const createParams: { type: string; branch: string | null; extra: { loopName: string; projectDirectory: string; workspaceCreatedAt: number } } = {
type: 'forge',
branch: null,
extra: { loopName: options.loopName, projectDirectory: options.directory, workspaceCreatedAt: Date.now() },
}
const result = await workspaceApi.create(createParams)
if ('error' in result && result.error) {
(logger ?? console).error('createBuiltinWorktreeWorkspace: workspace.create returned error', result.error)
return null
}
const rawResult = result as unknown
const workspaceData = 'data' in result ? result.data as unknown : rawResult
const wsId =
rawResult && typeof rawResult === 'object' && 'id' in rawResult && typeof rawResult.id === 'string'
? rawResult.id
: null
const id = typeof workspaceData === 'string'
? workspaceData
: workspaceData && typeof workspaceData === 'object' && 'id' in workspaceData && typeof workspaceData.id === 'string'
? workspaceData.id
: wsId
const directory = workspaceData && typeof workspaceData === 'object' && 'directory' in workspaceData
? String((workspaceData as Record<string, unknown>).directory ?? '')
: ''
const branch = workspaceData && typeof workspaceData === 'object' && 'branch' in workspaceData
? String((workspaceData as Record<string, unknown>).branch ?? '')
: ''
if (!id) {
(logger ?? console).error('createBuiltinWorktreeWorkspace: workspace.create returned no workspace id', workspaceData)
return null
}
// opencode awaits the connected event internally before returning,
// see opencode source workspace.ts (Event.Status loop). The response should
// not reach us until the worktree is ready or errored — verify directory is populated.
if (!directory) {
(logger ?? console).error('createBuiltinWorktreeWorkspace: workspace.create returned empty directory', workspaceData)
return null
}
(logger ?? console).log?.(`createBuiltinWorktreeWorkspace: workspace ${id} created for ${options.loopName}`)
;(logger ?? console).log?.(`[warp] workspace.create.complete loopName=${options.loopName} workspaceId=${id} elapsedMs=${Date.now() - _wsStart}`)
if (typeof workspaceApi.syncList === 'function') {
try {
await workspaceApi.syncList()
;(logger ?? console).log?.(`createBuiltinWorktreeWorkspace: workspace ${id} registered via syncList`)
;(logger ?? console).log?.(`[warp] syncList.complete loopName=${options.loopName} workspaceId=${id} elapsedMs=${Date.now() - _wsStart}`)
} catch (err) {
;(logger ?? console).error('createBuiltinWorktreeWorkspace: syncList after create failed; workspace may be reachable via session list but not visible in Warp picker', err)
}
} else {
;(logger ?? console).log?.('createBuiltinWorktreeWorkspace: syncList not available on SDK, skipping registration')
}
if (typeof client.sync?.start === 'function') {
try {
await client.sync.start()
;(logger ?? console).log?.(`createBuiltinWorktreeWorkspace: workspace sync started for ${id}`)
;(logger ?? console).log?.(`[warp] sync.start.complete loopName=${options.loopName} workspaceId=${id} elapsedMs=${Date.now() - _wsStart}`)
} catch (err) {
;(logger ?? console).error('createBuiltinWorktreeWorkspace: sync.start after create failed; workspace status may remain unavailable in the TUI', err)
}
}
try {
const [listResult, statusResult] = await Promise.all([
typeof workspaceApi.list === 'function' ? workspaceApi.list() : Promise.resolve(undefined),
typeof workspaceApi.status === 'function' ? workspaceApi.status() : Promise.resolve(undefined),
])
const listed = ((listResult as { data?: Array<{ id?: string }> } | undefined)?.data ?? []).some((workspace) => workspace.id === id)
const statusData = ((statusResult as { data?: Array<{ workspaceID?: string; status?: string }> } | undefined)?.data ?? [])
const status = statusData.find((entry) => entry.workspaceID === id)?.status
statusRegistry?.primeFromSnapshot(statusData.map((entry) => ({ workspaceID: entry.workspaceID ?? '', status: entry.status ?? '' })))
;(logger ?? console).log?.(`createBuiltinWorktreeWorkspace: workspace ${id} visibility listed=${listed} status=${status ?? 'unknown'}`)
} catch (err) {
;(logger ?? console).error('createBuiltinWorktreeWorkspace: post-create workspace visibility check failed', err)
}
return { workspaceId: id, directory, branch }
} catch (err) {
(logger ?? console).error('createBuiltinWorktreeWorkspace: workspace.create threw', err)
return null
}
}
/**
* Binds a session to a workspace by calling the warp API.
*/
export async function bindSessionToWorkspace(
client: OpencodeClient,
workspaceId: string,
sessionId: string,
logger?: { log: (msg: string, ...args: unknown[]) => void; error: (msg: string, ...args: unknown[]) => void },
options?: { copyChanges?: boolean; loopName?: string },
statusRegistry?: WorkspaceStatusRegistry,
): Promise<void> {
const workspaceApi = client.experimental?.workspace
if (!workspaceApi || typeof workspaceApi.warp !== 'function') {
(logger ?? console).log?.('bindSessionToWorkspace: experimental.workspace.warp not available')
throw new Error('experimental.workspace.warp not available on this host')
}
const warpParams: { id: string; sessionID: string; copyChanges?: boolean } = {
id: workspaceId,
sessionID: sessionId,
}
if (typeof options?.copyChanges === 'boolean') warpParams.copyChanges = options.copyChanges
const _warpStart = Date.now()
;(logger ?? console).log?.(`[warp] warp.start loopName=${options?.loopName ?? 'unknown'} workspaceId=${workspaceId} sessionId=${sessionId}`)
const result = await workspaceApi.warp(warpParams)
if ('error' in result && result.error) {
const _warpError = String(result.error)
;(logger ?? console).error(`[warp] warp.failed loopName=${options?.loopName ?? 'unknown'} workspaceId=${workspaceId} sessionId=${sessionId} elapsedMs=${Date.now() - _warpStart} error="${_warpError}"`)
;(logger ?? console).error(`bindSessionToWorkspace: warp failed for workspace=${workspaceId} session=${sessionId}`, result.error)
throw new Error(`Session warp failed: ${JSON.stringify(result.error)}`)
}
;(logger ?? console).log?.(`[warp] warp.complete loopName=${options?.loopName ?? 'unknown'} workspaceId=${workspaceId} sessionId=${sessionId} elapsedMs=${Date.now() - _warpStart}`)
if (typeof client.sync?.start === 'function') {
try {
await client.sync.start()
;(logger ?? console).log?.(`bindSessionToWorkspace: workspace sync started for workspace=${workspaceId} session=${sessionId}`)
} catch (err) {
;(logger ?? console).error('bindSessionToWorkspace: sync.start after warp failed; workspace status may remain unavailable in the TUI', err)
}
}
try {
const [listResult, statusResult] = await Promise.all([
typeof workspaceApi.list === 'function' ? workspaceApi.list() : Promise.resolve(undefined),
typeof workspaceApi.status === 'function' ? workspaceApi.status() : Promise.resolve(undefined),
])
const listed = ((listResult as { data?: Array<{ id?: string }> } | undefined)?.data ?? [])
.some((workspace) => workspace.id === workspaceId)
const statusData = ((statusResult as { data?: Array<{ workspaceID?: string; status?: string }> } | undefined)?.data ?? [])
const status = statusData.find((entry) => entry.workspaceID === workspaceId)?.status
statusRegistry?.primeFromSnapshot(statusData.map((entry) => ({ workspaceID: entry.workspaceID ?? '', status: entry.status ?? '' })))
;(logger ?? console).log?.(`bindSessionToWorkspace: workspace ${workspaceId} visibility after warp listed=${listed} status=${status ?? 'unknown'}`)
} catch (err) {
;(logger ?? console).error('bindSessionToWorkspace: post-warp workspace visibility check failed', err)
}
}