Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 1 addition & 59 deletions src/main/broker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
type PendingRelayMessage
} from '@agent-relay/harness-driver'
import { AgentRelay, RelayPlacementError, type RelayMessage } from '@agent-relay/sdk'
import { getAccessToken, getApiUrl } from './auth'
import { assertDirectory } from './path-utils'
import { toErrorMessage } from './errors'
import { isRecord } from './guards'
Expand Down Expand Up @@ -2008,8 +2007,7 @@ export class BrokerManager {

/**
* Attach to an already-provisioned cloud sandbox (used by CloudAgentManager
* which warms the box via the cloud-agents/{id}/box endpoint). connectCloud
* is the legacy ad-hoc path that creates a sandbox here.
* which warms the box via the cloud-agents/{id}/box endpoint).
*/
async attachCloudSandbox(
projectId: string,
Expand Down Expand Up @@ -2149,62 +2147,6 @@ export class BrokerManager {
}
}

/**
* Connect to a broker running in a remote Daytona sandbox.
* Creates an ad-hoc sandbox via the cloud API, then attaches through the
* same SDK path used by CloudAgentManager-provisioned sandboxes.
*/
async connectCloud(projectId: string, win: BrowserWindow): Promise<string> {
const normalizedProjectId = projectId.trim()
if (!normalizedProjectId) {
throw new Error('Project id is required')
}

// Provisioning errors (sandbox create / terminal fetch) are handled here;
// attachCloudSandbox handles its own error reporting (console.error +
// broker:status). Splitting the try/catch keeps the two paths from
// double-logging the same failure to the renderer.
let sandboxId: string
let httpUrl: string
let apiKey: string
try {
const token = await getAccessToken()
if (!token) throw new Error('Not logged in — sign in first')

const apiUrl = getApiUrl()

// 1. Create sandbox with broker
console.log('[broker] Creating cloud sandbox...')
const createRes = await fetch(`${apiUrl}/api/v1/sandboxes`, {
method: 'POST',
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
body: JSON.stringify({})
})
if (!createRes.ok) {
const err = await createRes.json().catch(() => ({ error: createRes.statusText }))
throw new Error(`Failed to create sandbox: ${(err as { error: string }).error}`)
}
;({ sandboxId } = await createRes.json() as { sandboxId: string })
console.log('[broker] Sandbox created:', sandboxId)

// 2. Get terminal connection info
const termRes = await fetch(`${apiUrl}/api/v1/sandboxes/${sandboxId}/terminal`, {
headers: { Authorization: `Bearer ${token}` }
})
if (!termRes.ok) {
throw new Error('Failed to get terminal connection info')
}
;({ httpUrl, apiKey } = await termRes.json() as { httpUrl: string; apiKey: string })
} catch (err) {
console.error(`[broker] Failed to connect cloud broker for project ${normalizedProjectId}:`, err)
this.sendStatusToWindow(win, normalizedProjectId, 'error', String(err))
throw err
}

// attachCloudSandbox owns its own error reporting; let its errors propagate.
return this.attachCloudSandbox(normalizedProjectId, { sandboxId, execUrl: httpUrl, apiKey }, win)
}

// Local session first, then the cloud session — local stays the default
// target for project-scoped operations when both brokers are running.
private sessionsForProject(projectId: string): BrokerSession[] {
Expand Down
1 change: 0 additions & 1 deletion src/main/ipc-handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const mock = vi.hoisted(() => {
shutdown: vi.fn(async () => undefined),
syncChannels: vi.fn(async () => undefined),
autoFixRuntime: vi.fn(),
connectCloud: vi.fn(),
spawnAgent: vi.fn(),
listPersonas: vi.fn(),
spawnPersona: vi.fn(),
Expand Down
6 changes: 0 additions & 6 deletions src/main/ipc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,12 +829,6 @@ export function registerIpcHandlers(): void {
return brokerManager.attachTerminal(input.projectId, input)
})

ipcMain.handle('broker:connect-cloud', async (event) => {
const win = BrowserWindow.fromWebContents(event.sender)
if (!win) throw new Error('No window')
return brokerManager.connectCloud('cloud', win)
})

ipcMain.handle('broker:send-input', async (_, projectId: string | undefined, name: string, data: string) => {
return brokerManager.sendInput(projectId, name, data)
})
Expand Down
1 change: 0 additions & 1 deletion src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ const api = {
channels,
errorMessage
),
connectCloud: () => invoke<string>('broker:connect-cloud'),
spawnAgent: (projectId: string, input: BrokerSpawnAgentInput) =>
invoke<BrokerSpawnAgentResult>('broker:spawn-agent', projectId, input),
placeAgent: (projectId: string, input: BrokerPlaceAgentInput) =>
Expand Down
1 change: 0 additions & 1 deletion src/renderer/src/lib/ipc-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,6 @@ export const pearMock: PearAPI = {
id: `mock-observer-token-${projectId}`
}),
autoFixRuntime: async () => ({ removed: [] }),
connectCloud: async () => 'mock-cloud',
spawnAgent: async (projectId: string, input: BrokerSpawnAgentInput): Promise<BrokerSpawnAgentResult> => {
const agent = upsertAgent({ ...input, projectId, runtime: 'mock', current_state: 'idle' })
handleInjectedBrokerEvent({
Expand Down
1 change: 0 additions & 1 deletion src/shared/types/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,6 @@ export interface PearAPI {
channels?: string[],
errorMessage?: string
) => Promise<{ removed: string[] }>
connectCloud: () => Promise<string>
spawnAgent: (projectId: string, input: BrokerSpawnAgentInput) => Promise<BrokerSpawnAgentResult>
placeAgent: (projectId: string, input: BrokerPlaceAgentInput) => Promise<BrokerPlaceAgentOutcome>
listNodes: (projectId: string, capability?: string) => Promise<BrokerNodeSummary[]>
Expand Down
Loading