mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
Merge forge/terminal-dev-loop into beta
feat: integrated terminal, project actions and dev server preview (OM-17, OM-18, OM-19) Integration with #380 and #379: - Adopt the git-config identity model: drop the branch's identity stubs (getShellEnv, getAssignedGitIdentityEnv, repo identity ids, duplicate createGitService and getMainCheckoutPath); terminals receive only the GitHub token env and take their commit identity from git config. - Add RepoWorkspaceService as the single owner of OpenCode workspace side effects: worktree setup commands on create and terminal cleanup on remove, used by the workspace routes, the session launcher (ocm tool and multi-run) and multi-run discard, with one worktree-sibling matcher. - Run repo-delete terminal cleanup inside the worktree branch deletion flow. - Combine the CreateWorktreeDialog tests and update docs for the identity model and setup command coverage.
This commit is contained in:
commit
b13707b5b3
135 files changed
+12726
-492
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import path from 'path'
|
||||
import { promises as fs, mkdirSync, accessSync, constants, realpathSync } from 'node:fs'
|
||||
import { promises as fs, mkdirSync, accessSync, constants, realpathSync, writeFileSync, renameSync, rmSync } from 'node:fs'
|
||||
|
||||
interface MkdirSafeOptions {
|
||||
mode?: number
|
||||
@@ -39,6 +39,19 @@ export async function writeFileAtomic(filePath: string, content: string, options
|
||||
}
|
||||
}
|
||||
|
||||
export function writeFileAtomicSync(filePath: string, content: string, options: { mode?: number } = {}): void {
|
||||
const dir = path.dirname(filePath)
|
||||
mkdirSyncSafe(dir)
|
||||
const tempPath = path.join(dir, `.${path.basename(filePath)}.ocm-tmp-${process.pid}-${Date.now()}`)
|
||||
try {
|
||||
writeFileSync(tempPath, content, { encoding: 'utf-8', mode: options.mode ?? 0o600 })
|
||||
renameSync(tempPath, filePath)
|
||||
} catch (error) {
|
||||
rmSync(tempPath, { force: true })
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export async function existingFileMode(filePath: string): Promise<number | undefined> {
|
||||
try {
|
||||
return (await fs.stat(filePath)).mode & 0o777
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
const HOP_BY_HOP_HEADERS = new Set([
|
||||
'connection',
|
||||
'keep-alive',
|
||||
'proxy-authenticate',
|
||||
'proxy-authorization',
|
||||
'te',
|
||||
'trailers',
|
||||
'upgrade',
|
||||
'transfer-encoding',
|
||||
'content-length',
|
||||
'content-encoding',
|
||||
'host',
|
||||
'authorization',
|
||||
])
|
||||
|
||||
export function filterProxyHeaders(
|
||||
headers: Headers,
|
||||
extraExcluded: ReadonlySet<string> = new Set(),
|
||||
): Record<string, string> {
|
||||
const result: Record<string, string> = {}
|
||||
headers.forEach((value, key) => {
|
||||
const lowerKey = key.toLowerCase()
|
||||
if (HOP_BY_HOP_HEADERS.has(lowerKey) || extraExcluded.has(lowerKey)) return
|
||||
result[key] = value
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
export function buildProxyResponseHeaders(
|
||||
source: Headers,
|
||||
extraExcluded: ReadonlySet<string> = new Set(),
|
||||
): Headers {
|
||||
const excluded = new Set(extraExcluded)
|
||||
excluded.add('set-cookie')
|
||||
const headers = new Headers(filterProxyHeaders(source, excluded))
|
||||
for (const cookie of source.getSetCookie()) {
|
||||
headers.append('set-cookie', cookie)
|
||||
}
|
||||
return headers
|
||||
}
|
||||
@@ -27,6 +27,8 @@ interface ServiceError {
|
||||
message: string
|
||||
statusCode?: number
|
||||
status?: number
|
||||
code?: string
|
||||
details?: unknown
|
||||
}
|
||||
|
||||
type ServiceErrorConstructor = new (message: string, statusOrStatusCode: number) => ServiceError
|
||||
@@ -38,8 +40,16 @@ export function handleServiceError(
|
||||
ErrorClass: ServiceErrorConstructor,
|
||||
) {
|
||||
if (error instanceof ErrorClass) {
|
||||
const status = (error as ServiceError).statusCode ?? (error as ServiceError).status ?? 500
|
||||
return c.json({ error: error.message }, status as ContentfulStatusCode)
|
||||
const serviceError = error as ServiceError
|
||||
const status = serviceError.statusCode ?? serviceError.status ?? 500
|
||||
return c.json(
|
||||
{
|
||||
error: error.message,
|
||||
...(serviceError.code !== undefined ? { code: serviceError.code } : {}),
|
||||
...(serviceError.details !== undefined ? { details: serviceError.details } : {}),
|
||||
},
|
||||
status as ContentfulStatusCode,
|
||||
)
|
||||
}
|
||||
logger.error(fallback, error)
|
||||
return c.json({ error: getErrorMessage(error) }, 500)
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
export interface WebSocketPeer {
|
||||
send(data: string | Uint8Array<ArrayBuffer>): void
|
||||
close(code?: number, reason?: string): void
|
||||
bufferedAmount(): number
|
||||
}
|
||||
|
||||
export interface WebSocketBridge {
|
||||
send(data: string | ArrayBuffer | Uint8Array<ArrayBuffer>): void
|
||||
close(): void
|
||||
}
|
||||
|
||||
export const MAX_PEER_BUFFERED_BYTES = 4 * 1024 * 1024
|
||||
|
||||
export function peerBufferedAmount(peer: { raw?: unknown }): number {
|
||||
const raw = peer.raw as { bufferedAmount?: number } | undefined
|
||||
return typeof raw?.bufferedAmount === 'number' ? raw.bufferedAmount : 0
|
||||
}
|
||||
|
||||
export async function forwardPeerMessage(
|
||||
bridge: WebSocketBridge,
|
||||
data: string | Blob | ArrayBufferLike,
|
||||
): Promise<void> {
|
||||
if (typeof data === 'string') {
|
||||
bridge.send(data)
|
||||
return
|
||||
}
|
||||
if (data instanceof Blob) {
|
||||
bridge.send(await data.arrayBuffer())
|
||||
return
|
||||
}
|
||||
if (data instanceof ArrayBuffer) {
|
||||
bridge.send(data)
|
||||
}
|
||||
}
|
||||
|
||||
function defaultMapCloseCode(code: number): number {
|
||||
if (code === 1000) return 1000
|
||||
if (code >= 4000 && code <= 4999) return code
|
||||
return 1011
|
||||
}
|
||||
|
||||
function toBytes(data: ArrayBuffer | Uint8Array): Uint8Array<ArrayBuffer> {
|
||||
return data instanceof Uint8Array ? (data as Uint8Array<ArrayBuffer>) : new Uint8Array(data)
|
||||
}
|
||||
|
||||
export function bridgeWebSocket(upstream: WebSocket, peer: WebSocketPeer): WebSocketBridge {
|
||||
upstream.binaryType = 'arraybuffer'
|
||||
|
||||
const queue: Array<string | Uint8Array> = []
|
||||
let upstreamOpen = false
|
||||
let finished = false
|
||||
|
||||
const finish = (): void => {
|
||||
finished = true
|
||||
}
|
||||
|
||||
upstream.addEventListener('open', () => {
|
||||
upstreamOpen = true
|
||||
while (queue.length > 0) {
|
||||
upstream.send(queue.shift()!)
|
||||
}
|
||||
})
|
||||
|
||||
upstream.addEventListener('message', (event) => {
|
||||
if (finished) return
|
||||
const data = (event as MessageEvent).data
|
||||
if (typeof data === 'string') {
|
||||
peer.send(data)
|
||||
} else if (data instanceof ArrayBuffer || data instanceof Uint8Array) {
|
||||
peer.send(toBytes(data))
|
||||
}
|
||||
if (peer.bufferedAmount() > MAX_PEER_BUFFERED_BYTES) {
|
||||
finish()
|
||||
upstream.close()
|
||||
peer.close(1013, 'Client too slow')
|
||||
}
|
||||
})
|
||||
|
||||
upstream.addEventListener('close', (event) => {
|
||||
if (finished) return
|
||||
finish()
|
||||
peer.close(defaultMapCloseCode((event as CloseEvent).code))
|
||||
})
|
||||
|
||||
upstream.addEventListener('error', () => {
|
||||
if (finished) return
|
||||
finish()
|
||||
peer.close(1011)
|
||||
})
|
||||
|
||||
return {
|
||||
send(data) {
|
||||
if (finished) return
|
||||
const payload = typeof data === 'string' ? data : toBytes(data)
|
||||
if (upstreamOpen) {
|
||||
upstream.send(payload)
|
||||
} else {
|
||||
queue.push(payload)
|
||||
}
|
||||
},
|
||||
close() {
|
||||
if (finished) return
|
||||
finish()
|
||||
upstream.close()
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user