Skip to content

Fetch gateway models directly from Electron instead of spawning preview sessions #1363

@charlesvien

Description

@charlesvien

Problem

To populate the model selector dropdown, we currently spawn a lightweight "preview session" (__preview__ task ID) via the agent package, wait for it to initialize, and extract the available models from its configOptions response. This means:

  • Unnecessary latency — full agent session startup just to get a model list
  • Unnecessary complexity — preview session lifecycle management, cleanup logic, the __preview__ task ID convention
  • Fragility — if the agent fails to start, no models appear in the selector

Current flow

TaskInput mount
  → usePreviewSession() hook
  → sessionService.startPreviewSession({ adapter })
  → trpcClient.agent.start.mutate({ taskId: "__preview__", ... })
  → Agent initializes → Claude adapter → fetchGatewayModels()
  → configOptions returned with model list
  → Model selector populated

Proposed change

Call fetchGatewayModels() directly from a main process service and expose it via a tRPC query. The main process already has the gateway URL and API credentials — no agent session needed.

A tRPC endpoint (getGatewayModels) already exists in apps/code/src/main/trpc/routers/agent.ts that does exactly this. We should:

  1. Have the renderer call this tRPC query directly to populate the model selector
  2. Remove the preview session machinery (usePreviewSession, startPreviewSession, cancelPreviewSession, PREVIEW_TASK_ID)
  3. Keep the 10-min TTL cache in packages/agent/src/gateway-models.ts (still used by real sessions)

Key files

  • packages/agent/src/gateway-models.tsfetchGatewayModels() with 10-min cache
  • apps/code/src/main/trpc/routers/agent.ts — existing getGatewayModels tRPC query (~line 197)
  • apps/code/src/main/services/agent/service.tsgetGatewayModels() (~line 1675)
  • apps/code/src/renderer/features/task-detail/hooks/usePreviewSession.ts — preview session hook (to remove)
  • apps/code/src/renderer/features/sessions/service/service.tsstartPreviewSession() (to remove)
  • apps/code/src/renderer/features/sessions/components/ModelSelector.tsx — needs to use tRPC query instead

Context

From Josh: "for the model select issue thingy we've had open for a while, iirc we spawn some kind of preview session and then use the agent package to load the available models — why do we do that vs calling the gateway directly from electron?"

Answer: no good reason. The direct approach is simpler, faster and already partially wired up.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions