mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix(terminal-dev-loop): address branch review findings
Security: - Reject same-site and cross-site state-changing requests on cookie-authenticated routes unless the origin is trusted - Drop Manager and ocm_preview Set-Cookie headers from preview responses - Show and hash the full executable content of .ocm/project.json at trust time - Restrict action URLs to http(s) and refuse symlinked repo config paths - Keep the preview start redirect on the gateway origin Lifecycle: - Stop terminals in OpenCode workspace siblings when a repo is deleted - Report preview as unavailable instead of crashing when the gateway port cannot be bound Performance: - Skip branch lookups when validating terminal and project-config directories - Stop the /proc scan once all listening sockets are mapped and pause port polling while a preview is open - Close slow WebSocket clients with 1013 so they reconnect with replay - Lazy-load the xterm terminal view and skip unchanged resize requests - Request uncompressed responses from preview targets Cleanup: - Share one repo directory resolver, trusted-origins parser, cookie prefix, WebSocket frame forwarder, header filter, icon map and dialog-param helper - Remove unused props, options and exports Docs: update terminal, preview, project actions, sandboxing, internal API and environment pages
This commit is contained in:
1 parent
16b91ccf02
commit
c60a9b35f2
72 files changed
+2039
-596
No files matched your search
@@ -6,15 +6,34 @@ import { canonicalPathSync } from '../utils/fs-safe'
|
||||
import { listListeningPorts, isReservedPreviewPort, type PreviewPortEntry } from '../services/preview/ports'
|
||||
import { PreviewSessionStore } from '../services/preview/gateway'
|
||||
|
||||
export interface PreviewAvailability {
|
||||
isEnabled: () => boolean
|
||||
markAvailable: () => void
|
||||
markUnavailable: () => void
|
||||
}
|
||||
|
||||
/**
|
||||
* Tracks whether the preview gateway is usable at request time. It starts in
|
||||
* the given state and is flipped by the server lifecycle: available once the
|
||||
* port is bound, unavailable after a bind error.
|
||||
*/
|
||||
export function createPreviewAvailability(initialEnabled: boolean): PreviewAvailability {
|
||||
let enabled = initialEnabled
|
||||
return {
|
||||
isEnabled: () => enabled,
|
||||
markAvailable: () => { enabled = true },
|
||||
markUnavailable: () => { enabled = false },
|
||||
}
|
||||
}
|
||||
|
||||
export interface PreviewRoutesDeps {
|
||||
enabled: boolean
|
||||
isEnabled: () => boolean
|
||||
listPorts: () => Promise<PreviewPortEntry[]>
|
||||
store: PreviewSessionStore
|
||||
}
|
||||
|
||||
function isInsideDirectory(cwd: string | null, canonicalDirectory: string | null): boolean {
|
||||
if (!cwd || !canonicalDirectory) return false
|
||||
const canonicalCwd = canonicalPathSync(cwd)
|
||||
function isInsideDirectory(canonicalCwd: string | null, canonicalDirectory: string | null): boolean {
|
||||
if (!canonicalCwd || !canonicalDirectory) return false
|
||||
if (canonicalCwd === canonicalDirectory) return true
|
||||
const prefix = canonicalDirectory.endsWith(path.sep) ? canonicalDirectory : `${canonicalDirectory}${path.sep}`
|
||||
return canonicalCwd.startsWith(prefix)
|
||||
@@ -22,7 +41,7 @@ function isInsideDirectory(cwd: string | null, canonicalDirectory: string | null
|
||||
|
||||
export function createPreviewRoutes(deps: Partial<PreviewRoutesDeps> = {}) {
|
||||
const resolvedDeps: PreviewRoutesDeps = {
|
||||
enabled: deps.enabled ?? ENV.PREVIEW.PORT > 0,
|
||||
isEnabled: deps.isEnabled ?? (() => ENV.PREVIEW.PORT > 0),
|
||||
listPorts: deps.listPorts ?? (() => listListeningPorts()),
|
||||
store: deps.store ?? new PreviewSessionStore(),
|
||||
}
|
||||
@@ -32,13 +51,15 @@ export function createPreviewRoutes(deps: Partial<PreviewRoutesDeps> = {}) {
|
||||
const directory = c.req.query('directory')
|
||||
const canonicalDirectory = directory ? canonicalPathSync(directory) : null
|
||||
const ports = await resolvedDeps.listPorts()
|
||||
const sorted = [...ports].sort((left, right) => {
|
||||
const leftMatch = isInsideDirectory(left.cwd, canonicalDirectory)
|
||||
const rightMatch = isInsideDirectory(right.cwd, canonicalDirectory)
|
||||
if (leftMatch !== rightMatch) return leftMatch ? -1 : 1
|
||||
const decorated = ports.map((entry) => ({
|
||||
...entry,
|
||||
inDirectory: isInsideDirectory(entry.cwd ? canonicalPathSync(entry.cwd) : null, canonicalDirectory),
|
||||
}))
|
||||
decorated.sort((left, right) => {
|
||||
if (left.inDirectory !== right.inDirectory) return left.inDirectory ? -1 : 1
|
||||
return left.port - right.port
|
||||
})
|
||||
return c.json({ enabled: resolvedDeps.enabled, ports: sorted })
|
||||
return c.json({ enabled: resolvedDeps.isEnabled(), ports: decorated })
|
||||
})
|
||||
|
||||
app.post('/sessions', async (c) => {
|
||||
@@ -48,7 +69,7 @@ export function createPreviewRoutes(deps: Partial<PreviewRoutesDeps> = {}) {
|
||||
return c.json({ error: 'Invalid request' }, 400)
|
||||
}
|
||||
|
||||
if (!resolvedDeps.enabled) {
|
||||
if (!resolvedDeps.isEnabled()) {
|
||||
return c.json({ error: 'Preview is disabled' }, 503)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import type { Context } from 'hono'
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import type { Repo } from '@opencode-manager/shared/types'
|
||||
import { getSiblingRepos, resolveRepoOrAssistant, resolveRepoWorkingDirectory } from '../services/repo'
|
||||
import { getRepoById } from '../db/queries'
|
||||
import type { GitAuthService } from '../services/git-auth'
|
||||
import type { OpenCodeClient } from '../services/opencode/client'
|
||||
|
||||
export interface RepoDirectoryDeps {
|
||||
database: Database
|
||||
gitAuthService: GitAuthService
|
||||
openCodeClient: OpenCodeClient
|
||||
}
|
||||
|
||||
export interface RepoDirectoryOptions {
|
||||
allowAssistant: boolean
|
||||
}
|
||||
|
||||
export async function resolveRepoRequestDirectory(
|
||||
c: Context,
|
||||
deps: RepoDirectoryDeps,
|
||||
repoIdParam: string,
|
||||
directory: string | undefined,
|
||||
options: RepoDirectoryOptions,
|
||||
): Promise<{ repo: Repo; directory: string } | Response> {
|
||||
const id = Number.parseInt(repoIdParam, 10)
|
||||
if (Number.isNaN(id)) {
|
||||
return c.json({ error: 'Invalid repo id' }, 400)
|
||||
}
|
||||
|
||||
const repo = options.allowAssistant
|
||||
? resolveRepoOrAssistant(deps.database, id)
|
||||
: getRepoById(deps.database, id)
|
||||
if (!repo || repo.cloneStatus !== 'ready') {
|
||||
return c.json({ error: 'Repo not found' }, 404)
|
||||
}
|
||||
|
||||
const resolved = await resolveRepoWorkingDirectory(repo, directory, () =>
|
||||
getSiblingRepos(
|
||||
deps.database,
|
||||
repo.id,
|
||||
deps.gitAuthService.getGitEnvironment(),
|
||||
deps.openCodeClient,
|
||||
{ includeBranch: false },
|
||||
),
|
||||
)
|
||||
|
||||
if (!resolved) {
|
||||
return c.json({ error: 'Directory is not part of this repository' }, 400)
|
||||
}
|
||||
|
||||
return { repo, directory: resolved }
|
||||
}
|
||||
@@ -1,18 +1,12 @@
|
||||
import { Hono } from 'hono'
|
||||
import type { Context } from 'hono'
|
||||
import type { ContentfulStatusCode } from 'hono/utils/http-status'
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import type { Repo } from '@opencode-manager/shared/types'
|
||||
import { UpdateProjectActionsRequestSchema, UpdateWorktreeSetupRequestSchema, TrustRepoConfigRequestSchema, MoveProjectItemRequestSchema, RunProjectActionRequestSchema } from '@opencode-manager/shared/schemas'
|
||||
import * as repoService from '../services/repo'
|
||||
import { resolveRepoWorkingDirectory } from '../services/repo'
|
||||
import { getRepoById } from '../db/queries'
|
||||
import type { GitAuthService } from '../services/git-auth'
|
||||
import type { OpenCodeClient } from '../services/opencode/client'
|
||||
import type { TerminalService } from '../services/terminal'
|
||||
import { ProjectConfigError, type ProjectConfigService } from '../services/project-config'
|
||||
import { logger } from '../utils/logger'
|
||||
import { getErrorMessage } from '../utils/error-utils'
|
||||
import { handleServiceError } from '../utils/route-helpers'
|
||||
import { resolveRepoRequestDirectory } from './repo-directory'
|
||||
|
||||
export interface RepoProjectConfigDeps {
|
||||
database: Database
|
||||
@@ -22,17 +16,6 @@ export interface RepoProjectConfigDeps {
|
||||
terminalService: TerminalService
|
||||
}
|
||||
|
||||
function handleProjectConfigError(c: Context, error: unknown): Response {
|
||||
if (error instanceof ProjectConfigError) {
|
||||
return c.json(
|
||||
{ error: error.message, code: error.code, details: error.details },
|
||||
error.status as ContentfulStatusCode,
|
||||
)
|
||||
}
|
||||
logger.error('Project config request failed:', error)
|
||||
return c.json({ error: getErrorMessage(error) }, 500)
|
||||
}
|
||||
|
||||
export function createRepoProjectConfigRoutes(
|
||||
database: Database,
|
||||
gitAuthService: GitAuthService,
|
||||
@@ -49,45 +32,16 @@ export function createRepoProjectConfigRoutes(
|
||||
}
|
||||
const app = new Hono()
|
||||
|
||||
async function resolveRepoDirectory(
|
||||
c: Context,
|
||||
repoIdParam: string,
|
||||
directory: string | undefined,
|
||||
): Promise<{ repo: Repo; directory: string } | Response> {
|
||||
const id = Number.parseInt(repoIdParam, 10)
|
||||
if (Number.isNaN(id)) {
|
||||
return c.json({ error: 'Invalid repo id' }, 400)
|
||||
}
|
||||
|
||||
const repo = getRepoById(deps.database, id)
|
||||
if (!repo || repo.cloneStatus !== 'ready') {
|
||||
return c.json({ error: 'Repo not found' }, 404)
|
||||
}
|
||||
|
||||
const resolved = await resolveRepoWorkingDirectory(repo, directory, () =>
|
||||
repoService.getSiblingRepos(
|
||||
deps.database,
|
||||
repo.id,
|
||||
deps.gitAuthService.getGitEnvironment(),
|
||||
deps.openCodeClient,
|
||||
),
|
||||
)
|
||||
|
||||
if (!resolved) {
|
||||
return c.json({ error: 'Directory is not part of this repository' }, 400)
|
||||
}
|
||||
|
||||
return { repo, directory: resolved }
|
||||
}
|
||||
|
||||
app.get('/:id/project-config', async (c) => {
|
||||
const resolved = await resolveRepoDirectory(c, c.req.param('id'), c.req.query('directory'))
|
||||
const resolved = await resolveRepoRequestDirectory(c, deps, c.req.param('id'), c.req.query('directory'), {
|
||||
allowAssistant: false,
|
||||
})
|
||||
if (resolved instanceof Response) return resolved
|
||||
|
||||
try {
|
||||
return c.json(await deps.projectConfigService.getConfig(resolved.repo, resolved.directory))
|
||||
} catch (error: unknown) {
|
||||
return handleProjectConfigError(c, error)
|
||||
return handleServiceError(c, error, 'Project config request failed', ProjectConfigError)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -98,7 +52,9 @@ export function createRepoProjectConfigRoutes(
|
||||
return c.json({ error: 'Invalid request', details: parsed.error.flatten() }, 400)
|
||||
}
|
||||
|
||||
const resolved = await resolveRepoDirectory(c, c.req.param('id'), c.req.query('directory'))
|
||||
const resolved = await resolveRepoRequestDirectory(c, deps, c.req.param('id'), c.req.query('directory'), {
|
||||
allowAssistant: false,
|
||||
})
|
||||
if (resolved instanceof Response) return resolved
|
||||
|
||||
try {
|
||||
@@ -106,7 +62,7 @@ export function createRepoProjectConfigRoutes(
|
||||
deps.projectConfigService.setPersonalActions(projectRepo, parsed.data.actions)
|
||||
return c.json(await deps.projectConfigService.getConfig(resolved.repo, resolved.directory))
|
||||
} catch (error: unknown) {
|
||||
return handleProjectConfigError(c, error)
|
||||
return handleServiceError(c, error, 'Project config request failed', ProjectConfigError)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -117,7 +73,9 @@ export function createRepoProjectConfigRoutes(
|
||||
return c.json({ error: 'Invalid request', details: parsed.error.flatten() }, 400)
|
||||
}
|
||||
|
||||
const resolved = await resolveRepoDirectory(c, c.req.param('id'), c.req.query('directory'))
|
||||
const resolved = await resolveRepoRequestDirectory(c, deps, c.req.param('id'), c.req.query('directory'), {
|
||||
allowAssistant: false,
|
||||
})
|
||||
if (resolved instanceof Response) return resolved
|
||||
|
||||
try {
|
||||
@@ -125,7 +83,7 @@ export function createRepoProjectConfigRoutes(
|
||||
deps.projectConfigService.setPersonalSetup(projectRepo, parsed.data.commands)
|
||||
return c.json(await deps.projectConfigService.getConfig(resolved.repo, resolved.directory))
|
||||
} catch (error: unknown) {
|
||||
return handleProjectConfigError(c, error)
|
||||
return handleServiceError(c, error, 'Project config request failed', ProjectConfigError)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -136,14 +94,16 @@ export function createRepoProjectConfigRoutes(
|
||||
return c.json({ error: 'Invalid request', details: parsed.error.flatten() }, 400)
|
||||
}
|
||||
|
||||
const resolved = await resolveRepoDirectory(c, c.req.param('id'), parsed.data.directory)
|
||||
const resolved = await resolveRepoRequestDirectory(c, deps, c.req.param('id'), parsed.data.directory, {
|
||||
allowAssistant: false,
|
||||
})
|
||||
if (resolved instanceof Response) return resolved
|
||||
|
||||
try {
|
||||
await deps.projectConfigService.trustRepoFile(resolved.repo, resolved.directory, parsed.data.hash)
|
||||
return c.json(await deps.projectConfigService.getConfig(resolved.repo, resolved.directory))
|
||||
} catch (error: unknown) {
|
||||
return handleProjectConfigError(c, error)
|
||||
return handleServiceError(c, error, 'Project config request failed', ProjectConfigError)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -154,14 +114,16 @@ export function createRepoProjectConfigRoutes(
|
||||
return c.json({ error: 'Invalid request', details: parsed.error.flatten() }, 400)
|
||||
}
|
||||
|
||||
const resolved = await resolveRepoDirectory(c, c.req.param('id'), parsed.data.directory)
|
||||
const resolved = await resolveRepoRequestDirectory(c, deps, c.req.param('id'), parsed.data.directory, {
|
||||
allowAssistant: false,
|
||||
})
|
||||
if (resolved instanceof Response) return resolved
|
||||
|
||||
try {
|
||||
await deps.projectConfigService.moveItem(resolved.repo, resolved.directory, parsed.data)
|
||||
return c.json(await deps.projectConfigService.getConfig(resolved.repo, resolved.directory))
|
||||
} catch (error: unknown) {
|
||||
return handleProjectConfigError(c, error)
|
||||
return handleServiceError(c, error, 'Project config request failed', ProjectConfigError)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -172,7 +134,9 @@ export function createRepoProjectConfigRoutes(
|
||||
return c.json({ error: 'Invalid request', details: parsed.error.flatten() }, 400)
|
||||
}
|
||||
|
||||
const resolved = await resolveRepoDirectory(c, c.req.param('id'), parsed.data.directory)
|
||||
const resolved = await resolveRepoRequestDirectory(c, deps, c.req.param('id'), parsed.data.directory, {
|
||||
allowAssistant: false,
|
||||
})
|
||||
if (resolved instanceof Response) return resolved
|
||||
|
||||
try {
|
||||
@@ -184,7 +148,7 @@ export function createRepoProjectConfigRoutes(
|
||||
)
|
||||
return c.json(response)
|
||||
} catch (error: unknown) {
|
||||
return handleProjectConfigError(c, error)
|
||||
return handleServiceError(c, error, 'Project config request failed', ProjectConfigError)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -2,25 +2,21 @@ import { Hono } from 'hono'
|
||||
import type { MiddlewareHandler } from 'hono'
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import type { UpgradeWebSocket } from 'hono/ws'
|
||||
import { ENV } from '@opencode-manager/shared/config/env'
|
||||
import { getTrustedOrigins } from '@opencode-manager/shared/config/env'
|
||||
import type { GitAuthService } from '../services/git-auth'
|
||||
import type { OpenCodeClient } from '../services/opencode/client'
|
||||
import type { TerminalService } from '../services/terminal'
|
||||
import type { WebSocketBridge } from '../utils/websocket-bridge'
|
||||
import { forwardPeerMessage, peerBufferedAmount, type WebSocketBridge } from '../utils/websocket-bridge'
|
||||
import { getErrorMessage } from '../utils/error-utils'
|
||||
import { resolveTerminalDirectory } from './repo-terminals'
|
||||
import { resolveRepoRequestDirectory } from './repo-directory'
|
||||
|
||||
export interface RepoTerminalSocketVariables {
|
||||
interface RepoTerminalSocketVariables {
|
||||
terminalDirectory: string
|
||||
terminalCursor: number | undefined
|
||||
}
|
||||
|
||||
const MAX_CLOSE_REASON_BYTES = 120
|
||||
|
||||
const DEFAULT_TRUSTED_ORIGINS = ENV.AUTH.TRUSTED_ORIGINS.split(',')
|
||||
.map((origin) => origin.trim())
|
||||
.filter(Boolean)
|
||||
|
||||
function isAllowedUpgradeOrigin(
|
||||
origin: string | undefined,
|
||||
trustedOrigins: readonly string[],
|
||||
@@ -50,7 +46,7 @@ export function createRepoTerminalSocketRoutes(
|
||||
openCodeClient: OpenCodeClient,
|
||||
terminalService: TerminalService,
|
||||
upgradeWebSocket: UpgradeWebSocket,
|
||||
trustedOrigins: readonly string[] = DEFAULT_TRUSTED_ORIGINS,
|
||||
trustedOrigins: readonly string[] = getTrustedOrigins(),
|
||||
) {
|
||||
const app = new Hono<{ Variables: RepoTerminalSocketVariables }>()
|
||||
const deps = { database, gitAuthService, openCodeClient }
|
||||
@@ -65,7 +61,9 @@ export function createRepoTerminalSocketRoutes(
|
||||
return c.json({ error: 'Invalid cursor' }, 400)
|
||||
}
|
||||
|
||||
const resolved = await resolveTerminalDirectory(c, deps, c.req.param('id') ?? '', c.req.query('directory'))
|
||||
const resolved = await resolveRepoRequestDirectory(c, deps, c.req.param('id') ?? '', c.req.query('directory'), {
|
||||
allowAssistant: true,
|
||||
})
|
||||
if (resolved instanceof Response) return resolved
|
||||
|
||||
c.set('terminalDirectory', resolved.directory)
|
||||
@@ -91,6 +89,7 @@ export function createRepoTerminalSocketRoutes(
|
||||
.connect(directory, ptyID, cursor, {
|
||||
send: (data) => ws.send(data),
|
||||
close: (code, reason) => ws.close(code, reason),
|
||||
bufferedAmount: () => peerBufferedAmount(ws),
|
||||
})
|
||||
.then((connected) => {
|
||||
if (closed) {
|
||||
@@ -107,19 +106,7 @@ export function createRepoTerminalSocketRoutes(
|
||||
async onMessage(event) {
|
||||
await connectPromise
|
||||
if (!bridge) return
|
||||
|
||||
const data = event.data
|
||||
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)
|
||||
}
|
||||
await forwardPeerMessage(bridge, event.data)
|
||||
},
|
||||
onClose() {
|
||||
closed = true
|
||||
|
||||
@@ -1,57 +1,16 @@
|
||||
import { Hono } from 'hono'
|
||||
import type { Context } from 'hono'
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import type { Repo } from '@opencode-manager/shared/types'
|
||||
import { CreateTerminalRequestSchema, ResizeTerminalRequestSchema } from '@opencode-manager/shared/schemas'
|
||||
import * as repoService from '../services/repo'
|
||||
import { resolveRepoOrAssistant, resolveRepoWorkingDirectory } from '../services/repo'
|
||||
import type { GitAuthService } from '../services/git-auth'
|
||||
import type { OpenCodeClient } from '../services/opencode/client'
|
||||
import { TerminalService, TerminalNotFoundError } from '../services/terminal'
|
||||
import { handleOpenCodeError } from '../utils/route-helpers'
|
||||
import { resolveRepoRequestDirectory, type RepoDirectoryDeps } from './repo-directory'
|
||||
|
||||
export interface TerminalDirectoryDeps {
|
||||
database: Database
|
||||
gitAuthService: GitAuthService
|
||||
openCodeClient: OpenCodeClient
|
||||
}
|
||||
|
||||
export interface TerminalRoutesDeps extends TerminalDirectoryDeps {
|
||||
export interface TerminalRoutesDeps extends RepoDirectoryDeps {
|
||||
terminalService: TerminalService
|
||||
}
|
||||
|
||||
export async function resolveTerminalDirectory(
|
||||
c: Context,
|
||||
deps: TerminalDirectoryDeps,
|
||||
repoIdParam: string,
|
||||
directory: string | undefined,
|
||||
): Promise<{ repo: Repo; directory: string } | Response> {
|
||||
const id = Number.parseInt(repoIdParam, 10)
|
||||
if (Number.isNaN(id)) {
|
||||
return c.json({ error: 'Invalid repo id' }, 400)
|
||||
}
|
||||
|
||||
const repo = resolveRepoOrAssistant(deps.database, id)
|
||||
if (!repo || repo.cloneStatus !== 'ready') {
|
||||
return c.json({ error: 'Repo not found' }, 404)
|
||||
}
|
||||
|
||||
const resolved = await resolveRepoWorkingDirectory(repo, directory, () =>
|
||||
repoService.getSiblingRepos(
|
||||
deps.database,
|
||||
repo.id,
|
||||
deps.gitAuthService.getGitEnvironment(),
|
||||
deps.openCodeClient,
|
||||
),
|
||||
)
|
||||
|
||||
if (!resolved) {
|
||||
return c.json({ error: 'Directory is not part of this repository' }, 400)
|
||||
}
|
||||
|
||||
return { repo, directory: resolved }
|
||||
}
|
||||
|
||||
export function createRepoTerminalRoutes(
|
||||
database: Database,
|
||||
gitAuthService: GitAuthService,
|
||||
@@ -62,7 +21,9 @@ export function createRepoTerminalRoutes(
|
||||
const app = new Hono()
|
||||
|
||||
app.get('/:id/terminals', async (c) => {
|
||||
const resolved = await resolveTerminalDirectory(c, deps, c.req.param('id'), c.req.query('directory'))
|
||||
const resolved = await resolveRepoRequestDirectory(c, deps, c.req.param('id'), c.req.query('directory'), {
|
||||
allowAssistant: true,
|
||||
})
|
||||
if (resolved instanceof Response) return resolved
|
||||
|
||||
try {
|
||||
@@ -80,7 +41,9 @@ export function createRepoTerminalRoutes(
|
||||
return c.json({ error: 'Invalid request' }, 400)
|
||||
}
|
||||
|
||||
const resolved = await resolveTerminalDirectory(c, deps, c.req.param('id'), parsed.data.directory)
|
||||
const resolved = await resolveRepoRequestDirectory(c, deps, c.req.param('id'), parsed.data.directory, {
|
||||
allowAssistant: true,
|
||||
})
|
||||
if (resolved instanceof Response) return resolved
|
||||
|
||||
try {
|
||||
@@ -101,7 +64,9 @@ export function createRepoTerminalRoutes(
|
||||
return c.json({ error: 'Invalid request' }, 400)
|
||||
}
|
||||
|
||||
const resolved = await resolveTerminalDirectory(c, deps, c.req.param('id'), parsed.data.directory)
|
||||
const resolved = await resolveRepoRequestDirectory(c, deps, c.req.param('id'), parsed.data.directory, {
|
||||
allowAssistant: true,
|
||||
})
|
||||
if (resolved instanceof Response) return resolved
|
||||
|
||||
const ptyID = c.req.param('ptyID')
|
||||
@@ -119,7 +84,9 @@ export function createRepoTerminalRoutes(
|
||||
})
|
||||
|
||||
app.delete('/:id/terminals/:ptyID', async (c) => {
|
||||
const resolved = await resolveTerminalDirectory(c, deps, c.req.param('id'), c.req.query('directory'))
|
||||
const resolved = await resolveRepoRequestDirectory(c, deps, c.req.param('id'), c.req.query('directory'), {
|
||||
allowAssistant: true,
|
||||
})
|
||||
if (resolved instanceof Response) return resolved
|
||||
|
||||
const ptyID = c.req.param('ptyID')
|
||||
|
||||
@@ -45,6 +45,39 @@ async function removeDirectoryTerminals(terminalService: TerminalService, direct
|
||||
}
|
||||
}
|
||||
|
||||
async function listWorkspaceSiblings(
|
||||
database: Database,
|
||||
repoId: number,
|
||||
gitAuthService: GitAuthService,
|
||||
openCodeClient: OpenCodeClient,
|
||||
): Promise<Array<Repo & { worktreeStrategy?: string }>> {
|
||||
const siblings = await repoService.getSiblingRepos(
|
||||
database,
|
||||
repoId,
|
||||
gitAuthService.getGitEnvironment(),
|
||||
openCodeClient,
|
||||
{ includeBranch: false },
|
||||
)
|
||||
return siblings.filter(isWorktreeSibling)
|
||||
}
|
||||
|
||||
async function removeWorkspaceSiblingTerminals(
|
||||
database: Database,
|
||||
repoId: number,
|
||||
gitAuthService: GitAuthService,
|
||||
openCodeClient: OpenCodeClient,
|
||||
terminalService: TerminalService,
|
||||
): Promise<void> {
|
||||
let siblings: Array<Repo & { worktreeStrategy?: string }>
|
||||
try {
|
||||
siblings = await listWorkspaceSiblings(database, repoId, gitAuthService, openCodeClient)
|
||||
} catch (error: unknown) {
|
||||
logger.warn(`Failed to list OpenCode workspace siblings for repo ${repoId}:`, error)
|
||||
return
|
||||
}
|
||||
await Promise.all(siblings.map((sibling) => removeDirectoryTerminals(terminalService, sibling.fullPath)))
|
||||
}
|
||||
|
||||
export function createRepoRoutes(
|
||||
database: Database,
|
||||
gitAuthService: GitAuthService,
|
||||
@@ -293,8 +326,8 @@ app.get('/', async (c) => {
|
||||
if (!parsed.success) return c.json({ error: 'directory is required' }, 400)
|
||||
const directory = parsed.data.directory
|
||||
|
||||
const siblings = await repoService.getSiblingRepos(database, id, gitAuthService.getGitEnvironment(), openCodeClient)
|
||||
const worktree = repoService.findSiblingByDirectory(siblings.filter(isWorktreeSibling), directory)
|
||||
const siblings = await listWorkspaceSiblings(database, id, gitAuthService, openCodeClient)
|
||||
const worktree = repoService.findSiblingByDirectory(siblings, directory)
|
||||
if (!worktree) return c.json({ error: 'Not a deletable worktree of this repo' }, 400)
|
||||
|
||||
await removeDirectoryTerminals(terminalService, worktree.fullPath)
|
||||
@@ -362,6 +395,8 @@ app.get('/', async (c) => {
|
||||
|
||||
await removeDirectoryTerminals(terminalService, repo.fullPath)
|
||||
|
||||
await removeWorkspaceSiblingTerminals(database, id, gitAuthService, openCodeClient, terminalService)
|
||||
|
||||
await repoService.deleteRepoFiles(database, id)
|
||||
|
||||
return c.json({ success: true })
|
||||
|
||||
Reference in new issue
Block a user