mirror of
https://github.com/stablyai/orca.git
synced 2026-09-23 08:02:31 +00:00
#14397 split `shared/types.ts` into 46 per-domain modules but kept the path as a re-export barrel so the import sites did not have to change. This removes the barrel: every consumer now imports from the module that actually declares the type, and `src/shared/types.ts` is deleted. Barrels hide where a type lives, make every consumer look like it depends on the whole domain, and let an unrelated edit invalidate a module that ~2,000 files transitively import. 2,323 import declarations across 2,321 files. Rewritten mechanically: each specifier was resolved to an absolute path via the TypeScript AST and recomputed, rather than string-substituted, so alias forms (`@/../../shared/ types`) and per-specifier `type` modifiers survive. Four cases the mechanical pass had to handle, each found by a gate rather than by reading the diff: - Modules inside `src/shared` import the barrel as `./types`, not `shared/types`. A pre-filter on the latter string skipped 176 of them and left imports dangling at a deleted file, which surfaced as confusing `Property 'x' is optional in type 'Repo' but required in Pick<Repo, ...>` errors rather than "module not found". - The barrel RENAMED one type on the way through (`WorkspaceSource as WorkspaceCreateTelemetrySource`), so the original name in the owning module has to be re-aliased at each consumer. - Three test files put `;(globalThis as ...)` on the line after the import. TypeScript parses that `;` as the import statement's terminator, so replacing through `statement.getEnd()` deletes it and breaks ASI. The rewrite now stops at the module specifier. - A file that already imported directly from a module got a SECOND import from it, because the barrel re-exported those same names — which trips `import/no-duplicates` under `--deny-warnings`. A post-pass merges declarations sharing a specifier and type-only-ness; the `import type` plus `import` pair from one module is left alone, since that form is allowed. Splitting one barrel import into several genuinely adds lines, which pushed `terminal-layout-pty-ownership.ts` to 301 counted lines: its 107-character import must wrap, and neither local type collapses onto one line (101 and 116 characters). Rather than contort a type declaration to fit a line budget, `collectLeafIds` and `pruneLeaves` move to `terminal-pane-layout-tree.ts` — they are pure structural operations on the layout tree and independent of PTY ownership. `visible-worktrees.ts` similarly loses its own mini-barrel re-export of `isDefaultBranchWorkspace`, with the four real consumers repointed at the declaring module. No `max-lines` bypass added. Verified: cold `tsc --noEmit` green on node, cli, and web (buildinfo deleted first — these projects are `composite: true` and reuse stale caches); the full `pnpm lint` green, not just bare oxlint — the narrower local check is what let the duplicate imports reach CI; max-lines ratchet OK at 344.
302 lines
9.0 KiB
TypeScript
302 lines
9.0 KiB
TypeScript
import type { WebSocket } from 'ws'
|
|
import {
|
|
DESKTOP_PROTOCOL_VERSION,
|
|
MIN_COMPATIBLE_MOBILE_VERSION
|
|
} from '../../src/shared/protocol-version'
|
|
import {
|
|
applyTerminalQuickCommandMutation,
|
|
type TerminalQuickCommandMutation
|
|
} from '../../src/shared/terminal-quick-commands'
|
|
import type { TerminalQuickCommand } from '../../src/shared/terminal-quick-command-types'
|
|
import { handleMockFilePreviewRequest } from './mock-server-file-preview-data'
|
|
import { handleMockGitRequest } from './mock-server-git-state'
|
|
import { handleMockAccountRequest } from './mock-server-account-rpc'
|
|
import { handleMockNativeChatRequest } from './mock-server-native-chat-scenario'
|
|
import { handleMockSessionTabsRequest } from './mock-server-session-tabs-fixture'
|
|
import { handleMockTerminalRequest } from './mock-server-terminal-stream'
|
|
import { createMockRepos, createMockWorktrees, readScenarioNumber } from './mobile-lag-scenario'
|
|
|
|
const MOCK_REPO_COUNT = readScenarioNumber('MOCK_REPO_COUNT', 2)
|
|
const MOCK_WORKTREE_COUNT = readScenarioNumber('MOCK_WORKTREE_COUNT', 2)
|
|
const MOCK_RPC_DELAY_MS = readScenarioNumber('MOCK_RPC_DELAY_MS', 0)
|
|
|
|
const FAKE_REPOS = createMockRepos(MOCK_REPO_COUNT)
|
|
let fakeWorktrees = createMockWorktrees(FAKE_REPOS, MOCK_WORKTREE_COUNT)
|
|
|
|
// Mutable quick-command list so the mobile Quick Commands sheet can add/edit/
|
|
// delete against the mock the same way it does a paired desktop.
|
|
let fakeQuickCommands: TerminalQuickCommand[] = [
|
|
{
|
|
id: 'qc-codex-review',
|
|
label: 'codex review',
|
|
action: 'agent-prompt',
|
|
agent: 'codex',
|
|
prompt: 'please review this diff for correctness and edge cases.',
|
|
scope: { type: 'global' }
|
|
},
|
|
{
|
|
id: 'qc-dev-server',
|
|
label: 'dev server',
|
|
action: 'terminal-command',
|
|
command: 'pnpm dev',
|
|
appendEnter: true,
|
|
scope: { type: 'global' }
|
|
}
|
|
]
|
|
|
|
export type RpcRequest = {
|
|
id: string
|
|
method: string
|
|
deviceToken?: string
|
|
params?: Record<string, unknown>
|
|
}
|
|
|
|
export type RpcResponse = {
|
|
id: string
|
|
ok: boolean
|
|
result?: unknown
|
|
error?: { code: string; message: string }
|
|
streaming?: true
|
|
_meta: { runtimeId: string }
|
|
}
|
|
|
|
export type RpcRespond = (response: RpcResponse, shouldSend?: () => boolean) => void
|
|
|
|
export const mockScenarioSummary = {
|
|
repoCount: FAKE_REPOS.length,
|
|
worktreeCount: fakeWorktrees.length,
|
|
rpcDelayMs: MOCK_RPC_DELAY_MS
|
|
}
|
|
|
|
export function success(id: string, result: unknown, streaming?: boolean): RpcResponse {
|
|
const resp: RpcResponse = { id, ok: true, result, _meta: { runtimeId: 'mock-runtime' } }
|
|
if (streaming) {
|
|
resp.streaming = true
|
|
}
|
|
return resp
|
|
}
|
|
|
|
export function error(id: string, code: string, message: string): RpcResponse {
|
|
return { id, ok: false, error: { code, message }, _meta: { runtimeId: 'mock-runtime' } }
|
|
}
|
|
|
|
function responseDelayFor(method: string): number {
|
|
const methodOverride =
|
|
process.env[`MOCK_RPC_DELAY_${method.replace(/\W/g, '_').toUpperCase()}_MS`]
|
|
if (!methodOverride) {
|
|
return MOCK_RPC_DELAY_MS
|
|
}
|
|
const parsed = Number(methodOverride)
|
|
return Number.isFinite(parsed) && parsed >= 0 ? Math.floor(parsed) : MOCK_RPC_DELAY_MS
|
|
}
|
|
|
|
function repoSelectorToId(repoSelector: unknown): string | null {
|
|
if (typeof repoSelector !== 'string') {
|
|
return null
|
|
}
|
|
return repoSelector.startsWith('id:') ? repoSelector.slice(3) : repoSelector
|
|
}
|
|
|
|
function terminalListWorktreeId(worktreeSelector: unknown): string | undefined {
|
|
if (typeof worktreeSelector === 'string' && worktreeSelector.length > 0) {
|
|
return worktreeSelector.startsWith('id:') ? worktreeSelector.slice(3) : worktreeSelector
|
|
}
|
|
return fakeWorktrees.find((worktree) => worktree.isActive)?.worktreeId
|
|
}
|
|
|
|
export function handleRequest(
|
|
request: RpcRequest,
|
|
send: (response: RpcResponse) => void,
|
|
ws: WebSocket
|
|
): void {
|
|
const respond: RpcRespond = (response, shouldSend) => {
|
|
const deliver = () => {
|
|
if (shouldSend?.() !== false) {
|
|
send(response)
|
|
}
|
|
}
|
|
const delay = responseDelayFor(request.method)
|
|
if (delay > 0) {
|
|
setTimeout(deliver, delay)
|
|
return
|
|
}
|
|
deliver()
|
|
}
|
|
|
|
// Each returns false for methods it does not own; first owner wins.
|
|
if (
|
|
handleMockGitRequest(request, respond, success) ||
|
|
handleMockFilePreviewRequest(request, respond, success, error) ||
|
|
handleMockAccountRequest(request, respond, success, error) ||
|
|
handleMockNativeChatRequest(request, respond, success, error, ws) ||
|
|
handleMockSessionTabsRequest(request, respond, success, terminalListWorktreeId) ||
|
|
handleMockTerminalRequest(request, respond, success, ws, terminalListWorktreeId)
|
|
) {
|
|
return
|
|
}
|
|
|
|
switch (request.method) {
|
|
case 'status.get':
|
|
respond(
|
|
success(request.id, {
|
|
runtimeId: 'mock-runtime',
|
|
protocolVersion: DESKTOP_PROTOCOL_VERSION,
|
|
minCompatibleMobileVersion: MIN_COMPATIBLE_MOBILE_VERSION,
|
|
capabilities: ['accounts.codex-reset-credit.v1'],
|
|
graphStatus: 'ready',
|
|
windowCount: 1,
|
|
tabCount: 2,
|
|
terminalCount: 2
|
|
})
|
|
)
|
|
break
|
|
|
|
case 'worktree.ps':
|
|
respond(
|
|
success(request.id, {
|
|
worktrees: fakeWorktrees,
|
|
totalCount: fakeWorktrees.length,
|
|
truncated: false
|
|
})
|
|
)
|
|
break
|
|
|
|
case 'repo.list':
|
|
respond(success(request.id, { repos: FAKE_REPOS }))
|
|
break
|
|
|
|
case 'settings.get':
|
|
respond(
|
|
success(request.id, {
|
|
settings: {
|
|
defaultTuiAgent: 'codex',
|
|
disabledTuiAgents: [],
|
|
agentCmdOverrides: {}
|
|
}
|
|
})
|
|
)
|
|
break
|
|
|
|
case 'settings.getTerminalQuickCommands':
|
|
respond(success(request.id, { terminalQuickCommands: fakeQuickCommands }))
|
|
break
|
|
|
|
case 'settings.updateTerminalQuickCommands': {
|
|
const updates = (request.params ?? {}) as { mutation?: TerminalQuickCommandMutation }
|
|
if (updates.mutation) {
|
|
fakeQuickCommands = applyTerminalQuickCommandMutation(fakeQuickCommands, updates.mutation)
|
|
}
|
|
respond(success(request.id, { terminalQuickCommands: fakeQuickCommands }))
|
|
break
|
|
}
|
|
|
|
case 'ui.get':
|
|
respond(
|
|
success(request.id, {
|
|
ui: {
|
|
groupBy: 'repo',
|
|
sortBy: 'recent',
|
|
hideSleepingWorkspaces: false,
|
|
hideDefaultBranchWorkspace: false,
|
|
filterRepoIds: [],
|
|
collapsedGroups: [],
|
|
trustedOrcaHooks: {}
|
|
}
|
|
})
|
|
)
|
|
break
|
|
|
|
case 'ui.set':
|
|
respond(success(request.id, { ok: true }))
|
|
break
|
|
|
|
case 'repo.hooks':
|
|
respond(
|
|
success(request.id, {
|
|
hooks: null,
|
|
source: null,
|
|
setupRunPolicy: 'run-by-default',
|
|
setupTrust: null
|
|
})
|
|
)
|
|
break
|
|
|
|
case 'preflight.detectAgents':
|
|
case 'preflight.detectRemoteAgents':
|
|
respond(success(request.id, ['claude', 'codex', 'gemini']))
|
|
break
|
|
|
|
case 'ssh.getState':
|
|
case 'ssh.connect': {
|
|
const targetId = String(request.params?.targetId ?? '')
|
|
respond(
|
|
success(request.id, {
|
|
state: {
|
|
targetId,
|
|
status: 'connected',
|
|
error: null,
|
|
reconnectAttempt: 0
|
|
}
|
|
})
|
|
)
|
|
break
|
|
}
|
|
|
|
case 'worktree.create': {
|
|
const repoId = repoSelectorToId(request.params?.repo) ?? FAKE_REPOS[0]?.id ?? 'repo-1'
|
|
const repo = FAKE_REPOS.find((candidate) => candidate.id === repoId) ?? FAKE_REPOS[0]
|
|
const name = String(request.params?.name ?? `mock-${fakeWorktrees.length + 1}`)
|
|
const created = createMockWorktrees(repo ? [repo] : FAKE_REPOS, 1)[0]
|
|
const next =
|
|
created && repo
|
|
? {
|
|
...created,
|
|
worktreeId: `${repo.id}::${repo.path}/worktrees/${name}`,
|
|
repoId: repo.id,
|
|
repo: repo.displayName,
|
|
path: `${repo.path}/worktrees/${name}`,
|
|
branch: `feature/${name}`,
|
|
displayName: name,
|
|
isActive: true
|
|
}
|
|
: null
|
|
if (next) {
|
|
fakeWorktrees = [next, ...fakeWorktrees.map((w) => ({ ...w, isActive: false }))]
|
|
mockScenarioSummary.worktreeCount = fakeWorktrees.length
|
|
}
|
|
respond(
|
|
success(request.id, {
|
|
worktree: {
|
|
id: next?.worktreeId ?? `repo-1::/tmp/orca-mobile-repro/${name}`,
|
|
worktreeId: next?.worktreeId
|
|
}
|
|
})
|
|
)
|
|
break
|
|
}
|
|
|
|
case 'worktree.activate': {
|
|
const selector = String(request.params?.worktree ?? '')
|
|
const id = selector.startsWith('id:') ? selector.slice(3) : selector
|
|
fakeWorktrees = fakeWorktrees.map((w) => ({ ...w, isActive: w.worktreeId === id }))
|
|
respond(success(request.id, { ok: true }))
|
|
break
|
|
}
|
|
|
|
case 'files.open':
|
|
case 'files.openDiff':
|
|
respond(
|
|
success(request.id, {
|
|
worktree: request.params?.worktree ?? 'id:mock',
|
|
relativePath: request.params?.relativePath ?? '',
|
|
kind: 'text',
|
|
opened: true
|
|
})
|
|
)
|
|
break
|
|
|
|
default:
|
|
respond(error(request.id, 'method_not_found', `Unknown method: ${request.method}`))
|
|
}
|
|
}
|