paperclip/server/src/routes
Devin Foley bb7d040894
Switch OpenCode to explicit static/local-aware model selection (#5117)
> **Stacked PR (part 4 of 7).** Depends on:
  - PR #5114
  - PR #5115
  - PR #5116
> Diff against `master` includes commits from earlier PRs in the stack —
the new commit in this PR is the topmost one.

## Thinking Path

> - Paperclip orchestrates AI agents for zero-human companies
> - When creating an OpenCode-local agent, Paperclip currently validates
> `adapterConfig.model` against the *Paperclip host's* `opencode models`
output
> - SSH testing surfaced that this blocks creating an OpenCode agent for
an SSH
> environment: the model that exists on the SSH target isn't visible to
the
> host, so creation fails with "OpenCode requires `adapterConfig.model`
in
> provider/model format" even when the operator picked a real remote
model
> - The initial direction was environment-aware model discovery; the
final
> decision was to keep OpenCode on the same explicit-model pattern as
other
> adapters (default + curated list + manual override) and stop blocking
>   creation on host-side discovery
> - This PR does both: the adapter-models endpoint now accepts
`environmentId` and
> probes against the target environment, and the create-time hard gate
is
> replaced by `requireOpenCodeModelId` which validates `provider/model`
*format*
> without requiring host-local discovery. Test/run-time still surfaces
real
>   auth/availability problems
> - The benefit is that operators can create OpenCode agents for remote
> environments without out-of-band setup, and the model picker in the UI
>   reflects the actually-targeted environment

## What Changed

- Added `requireOpenCodeModelId(input)` in
`opencode-local/src/server/models.ts`,
  exported it from the adapter index
- `ensureOpenCodeModelConfiguredAndAvailable` now delegates the format
check to
  `requireOpenCodeModelId`
- `agentsApi.adapterModels(companyId, adapterType, { environmentId })`
now accepts
  an environment ID and passes it as a query parameter
- `queryKeys.agents.adapterModels` now keys on `(companyId, adapterType,
environmentId)`
- `server/src/routes/agents.ts` reads and validates the new query
parameter,
  forwarding it to the adapter's model probe
- `AgentConfigForm.tsx` and `OnboardingWizard.tsx` build the model query
key from
the currently selected default environment ID and disable autodetect for
  `opencode_local` (model selection is explicit)
- `NewAgent.tsx` simplified — no longer special-cases OpenCode
autodetect
- `company-portability.ts` no longer needs OpenCode-specific autodetect
handling
- Tests added/updated:
  `adapter-model-refresh-routes.test.ts`, `adapter-models.test.ts`,
`agent-permissions-routes.test.ts`,
`opencode-local/src/server/models.test.ts`

## Verification

- `pnpm --filter @paperclipai/server test -- adapter-models
adapter-model-refresh agent-permissions`
- `pnpm --filter @paperclipai/adapter-opencode-local test`
- `pnpm --filter @paperclipai/ui test -- AgentConfigForm
OnboardingWizard NewAgent`
- Manual QA in browser:
1. Boot Paperclip on Tailscale-bound port (so it's reachable from
another
machine), create an OpenCode-local agent, switch the default environment
between two installed sandboxes, and confirm the model list refreshes
     per-environment
  2. Submit with a malformed `provider/model` string and verify the new
     `requireOpenCodeModelId` error surfaces
- Before/after screenshots attached for `AgentConfigForm` model picker

## Risks

- Behavioural shift: switching default environment now triggers a model
refetch.
Should be cheap but introduces a new UI loading state for OpenCode
users.
- Removing dynamic autodetect for OpenCode: if any user configured an
agent
without specifying `model` and relied on autodetect populating it, that
agent
will now fail at submit time. Mitigation: validation error is explicit
and
  actionable.
- New query string parameter on `/api/companies/:id/adapter-models` —
older
clients that omit it still work (parameter is optional and defaults to
null).

## Model Used

- OpenAI GPT-5.4 (reasoning effort: high) via Codex CLI
- Provider: OpenAI
- Used to author the code changes in this PR

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] If this change affects the UI, I have included before/after
screenshots
- [ ] I have updated relevant documentation to reflect my changes — N/A
- [x] I have considered and documented any risks above
- [x] I will address all Greptile and reviewer comments before
requesting merge
2026-05-03 13:01:34 -07:00
..
access.ts Fix SSH callback URL selection for LAN and private networks (#4799) 2026-04-29 15:56:17 -07:00
activity.ts [codex] Harden heartbeat scheduling and runtime controls (#4223) 2026-04-21 12:24:11 -05:00
adapters.ts Add cheap model profiles for local adapters (#4881) 2026-04-30 15:32:04 -05:00
agents.ts Switch OpenCode to explicit static/local-aware model selection (#5117) 2026-05-03 13:01:34 -07:00
approvals.ts Add sandbox environment support (#4415) 2026-04-24 12:15:53 -07:00
assets.ts Use attachment-size limit for company logos 2026-03-16 10:13:19 -05:00
auth.ts feat: implement multi-user access and invite flows (#3784) 2026-04-17 09:44:19 -05:00
authz.ts feat: implement multi-user access and invite flows (#3784) 2026-04-17 09:44:19 -05:00
companies.ts Harden API route authorization boundaries (#4122) 2026-04-20 10:56:48 -05:00
company-skills.ts Address Greptile telemetry review comments 2026-04-03 14:11:11 -05:00
costs.ts Add workflow interaction cancellation and issue cost summaries (#4862) 2026-04-30 13:57:25 -05:00
dashboard.ts refactor: rename packages to @paperclipai and CLI binary to paperclipai 2026-03-03 08:45:26 -06:00
environment-selection.ts Add sandbox environment support (#4415) 2026-04-24 12:15:53 -07:00
environments.ts Generalize sandbox provider core for plugin-only providers (#4449) 2026-04-24 18:03:41 -07:00
execution-workspaces.ts [codex] Respect manual workspace runtime controls (#4125) 2026-04-20 10:39:37 -05:00
goals.ts Add versioned telemetry events 2026-04-03 09:25:00 -05:00
health.ts [codex] Add backup endpoint and dev runtime hardening (#4087) 2026-04-20 06:08:55 -05:00
inbox-dismissals.ts Persist non-issue inbox dismissals 2026-04-09 06:16:05 -05:00
index.ts [codex] Add issue subtree pause, cancel, and restore controls (#4332) 2026-04-23 14:51:46 -05:00
instance-database-backups.ts [codex] Add backup endpoint and dev runtime hardening (#4087) 2026-04-20 06:08:55 -05:00
instance-settings.ts [codex] Add configurable liveness auto-recovery controls (#4587) 2026-04-27 08:46:44 -05:00
issue-tree-control.ts [codex] Split backend control-plane QoL slice (#4700) 2026-04-28 16:46:45 -05:00
issues-checkout-wakeup.ts Cut over OpenClaw adapter to strict SSE streaming 2026-03-05 15:54:55 -06:00
issues.ts [codex] Add issue monitor liveness controls (#4988) 2026-05-03 08:58:53 -05:00
llms.ts [codex] Harden create-agent skill governance (#4422) 2026-04-24 14:15:28 -05:00
org-chart-svg.ts Improve generated company org chart assets 2026-03-23 16:58:07 -05:00
plugin-ui-static.ts Add plugin framework and settings UI 2026-03-13 16:22:34 -05:00
plugins.ts Harden API route authorization boundaries (#4122) 2026-04-20 10:56:48 -05:00
projects.ts [codex] Split backend control-plane QoL slice (#4700) 2026-04-28 16:46:45 -05:00
routines.ts [codex] Add workspace routine run tab (#4958) 2026-05-01 11:58:15 -05:00
secrets.ts refactor: rename packages to @paperclipai and CLI binary to paperclipai 2026-03-03 08:45:26 -06:00
sidebar-badges.ts feat: implement multi-user access and invite flows (#3784) 2026-04-17 09:44:19 -05:00
sidebar-preferences.ts [codex] Improve workspace runtime and navigation ergonomics (#3680) 2026-04-14 12:57:11 -05:00
user-profiles.ts [codex] Add access cleanup and user profile page (#4088) 2026-04-20 06:10:20 -05:00
workspace-command-authz.ts Add cheap model profiles for local adapters (#4881) 2026-04-30 15:32:04 -05:00
workspace-runtime-service-authz.ts [codex] harden authenticated routes and issue editor reliability (#3741) 2026-04-15 08:41:15 -05:00