mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-10 03:08:10 +02:00
> **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
217 lines
7.1 KiB
TypeScript
217 lines
7.1 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import os from "node:os";
|
|
import type { AdapterModel } from "@paperclipai/adapter-utils";
|
|
import {
|
|
asString,
|
|
ensurePathInEnv,
|
|
runChildProcess,
|
|
} from "@paperclipai/adapter-utils/server-utils";
|
|
import { isValidOpenCodeModelId } from "../index.js";
|
|
|
|
const MODELS_CACHE_TTL_MS = 60_000;
|
|
const MODELS_DISCOVERY_TIMEOUT_MS = 20_000;
|
|
|
|
function resolveOpenCodeCommand(input: unknown): string {
|
|
const envOverride =
|
|
typeof process.env.PAPERCLIP_OPENCODE_COMMAND === "string" &&
|
|
process.env.PAPERCLIP_OPENCODE_COMMAND.trim().length > 0
|
|
? process.env.PAPERCLIP_OPENCODE_COMMAND.trim()
|
|
: "opencode";
|
|
return asString(input, envOverride);
|
|
}
|
|
|
|
const discoveryCache = new Map<string, { expiresAt: number; models: AdapterModel[] }>();
|
|
const VOLATILE_ENV_KEY_PREFIXES = ["PAPERCLIP_", "npm_", "NPM_"] as const;
|
|
const VOLATILE_ENV_KEY_EXACT = new Set(["PWD", "OLDPWD", "SHLVL", "_", "TERM_SESSION_ID", "HOME"]);
|
|
|
|
export function requireOpenCodeModelId(input: unknown): string {
|
|
const model = asString(input, "").trim();
|
|
if (!isValidOpenCodeModelId(model)) {
|
|
throw new Error("OpenCode requires `adapterConfig.model` in provider/model format.");
|
|
}
|
|
return model;
|
|
}
|
|
|
|
function dedupeModels(models: AdapterModel[]): AdapterModel[] {
|
|
const seen = new Set<string>();
|
|
const deduped: AdapterModel[] = [];
|
|
for (const model of models) {
|
|
const id = model.id.trim();
|
|
if (!id || seen.has(id)) continue;
|
|
seen.add(id);
|
|
deduped.push({ id, label: model.label.trim() || id });
|
|
}
|
|
return deduped;
|
|
}
|
|
|
|
function sortModels(models: AdapterModel[]): AdapterModel[] {
|
|
return [...models].sort((a, b) =>
|
|
a.id.localeCompare(b.id, "en", { numeric: true, sensitivity: "base" }),
|
|
);
|
|
}
|
|
|
|
function firstNonEmptyLine(text: string): string {
|
|
return (
|
|
text
|
|
.split(/\r?\n/)
|
|
.map((line) => line.trim())
|
|
.find(Boolean) ?? ""
|
|
);
|
|
}
|
|
|
|
function parseModelsOutput(stdout: string): AdapterModel[] {
|
|
const parsed: AdapterModel[] = [];
|
|
for (const raw of stdout.split(/\r?\n/)) {
|
|
const line = raw.trim();
|
|
if (!line) continue;
|
|
const firstToken = line.split(/\s+/)[0]?.trim() ?? "";
|
|
if (!firstToken.includes("/")) continue;
|
|
const provider = firstToken.slice(0, firstToken.indexOf("/")).trim();
|
|
const model = firstToken.slice(firstToken.indexOf("/") + 1).trim();
|
|
if (!provider || !model) continue;
|
|
parsed.push({ id: `${provider}/${model}`, label: `${provider}/${model}` });
|
|
}
|
|
return dedupeModels(parsed);
|
|
}
|
|
|
|
function normalizeEnv(input: unknown): Record<string, string> {
|
|
const envInput = typeof input === "object" && input !== null && !Array.isArray(input)
|
|
? (input as Record<string, unknown>)
|
|
: {};
|
|
const env: Record<string, string> = {};
|
|
for (const [key, value] of Object.entries(envInput)) {
|
|
if (typeof value === "string") env[key] = value;
|
|
}
|
|
return env;
|
|
}
|
|
|
|
function isVolatileEnvKey(key: string): boolean {
|
|
if (VOLATILE_ENV_KEY_EXACT.has(key)) return true;
|
|
return VOLATILE_ENV_KEY_PREFIXES.some((prefix) => key.startsWith(prefix));
|
|
}
|
|
|
|
function hashValue(value: string): string {
|
|
return createHash("sha256").update(value).digest("hex");
|
|
}
|
|
|
|
function discoveryCacheKey(command: string, cwd: string, env: Record<string, string>) {
|
|
const envKey = Object.entries(env)
|
|
.filter(([key]) => !isVolatileEnvKey(key))
|
|
.sort(([a], [b]) => a.localeCompare(b))
|
|
.map(([key, value]) => `${key}=${hashValue(value)}`)
|
|
.join("\n");
|
|
return `${command}\n${cwd}\n${envKey}`;
|
|
}
|
|
|
|
function pruneExpiredDiscoveryCache(now: number) {
|
|
for (const [key, value] of discoveryCache.entries()) {
|
|
if (value.expiresAt <= now) discoveryCache.delete(key);
|
|
}
|
|
}
|
|
|
|
export async function discoverOpenCodeModels(input: {
|
|
command?: unknown;
|
|
cwd?: unknown;
|
|
env?: unknown;
|
|
} = {}): Promise<AdapterModel[]> {
|
|
const command = resolveOpenCodeCommand(input.command);
|
|
const cwd = asString(input.cwd, process.cwd());
|
|
const env = normalizeEnv(input.env);
|
|
// Ensure HOME points to the actual running user's home directory.
|
|
// When the server is started via `runuser -u <user>`, HOME may still
|
|
// reflect the parent process (e.g. /root), causing OpenCode to miss
|
|
// provider auth credentials stored under the target user's home.
|
|
let resolvedHome: string | undefined;
|
|
try {
|
|
resolvedHome = os.userInfo().homedir || undefined;
|
|
} catch {
|
|
// os.userInfo() throws a SystemError when the current UID has no
|
|
// /etc/passwd entry (e.g. `docker run --user 1234` with a minimal
|
|
// image). Fall back to process.env.HOME.
|
|
}
|
|
// Prevent OpenCode from writing an opencode.json into the working directory.
|
|
const runtimeEnv = normalizeEnv(ensurePathInEnv({ ...process.env, ...env, ...(resolvedHome ? { HOME: resolvedHome } : {}), OPENCODE_DISABLE_PROJECT_CONFIG: "true" }));
|
|
|
|
const result = await runChildProcess(
|
|
`opencode-models-${Date.now()}-${Math.random().toString(16).slice(2)}`,
|
|
command,
|
|
["models"],
|
|
{
|
|
cwd,
|
|
env: runtimeEnv,
|
|
timeoutSec: MODELS_DISCOVERY_TIMEOUT_MS / 1000,
|
|
graceSec: 3,
|
|
onLog: async () => {},
|
|
},
|
|
);
|
|
|
|
if (result.timedOut) {
|
|
throw new Error(`\`opencode models\` timed out after ${MODELS_DISCOVERY_TIMEOUT_MS / 1000}s.`);
|
|
}
|
|
if ((result.exitCode ?? 1) !== 0) {
|
|
const detail = firstNonEmptyLine(result.stderr) || firstNonEmptyLine(result.stdout);
|
|
throw new Error(detail ? `\`opencode models\` failed: ${detail}` : "`opencode models` failed.");
|
|
}
|
|
|
|
return sortModels(parseModelsOutput(result.stdout));
|
|
}
|
|
|
|
export async function discoverOpenCodeModelsCached(input: {
|
|
command?: unknown;
|
|
cwd?: unknown;
|
|
env?: unknown;
|
|
} = {}): Promise<AdapterModel[]> {
|
|
const command = resolveOpenCodeCommand(input.command);
|
|
const cwd = asString(input.cwd, process.cwd());
|
|
const env = normalizeEnv(input.env);
|
|
const key = discoveryCacheKey(command, cwd, env);
|
|
const now = Date.now();
|
|
pruneExpiredDiscoveryCache(now);
|
|
const cached = discoveryCache.get(key);
|
|
if (cached && cached.expiresAt > now) return cached.models;
|
|
|
|
const models = await discoverOpenCodeModels({ command, cwd, env });
|
|
discoveryCache.set(key, { expiresAt: now + MODELS_CACHE_TTL_MS, models });
|
|
return models;
|
|
}
|
|
|
|
export async function ensureOpenCodeModelConfiguredAndAvailable(input: {
|
|
model?: unknown;
|
|
command?: unknown;
|
|
cwd?: unknown;
|
|
env?: unknown;
|
|
}): Promise<AdapterModel[]> {
|
|
const model = requireOpenCodeModelId(input.model);
|
|
|
|
const models = await discoverOpenCodeModelsCached({
|
|
command: input.command,
|
|
cwd: input.cwd,
|
|
env: input.env,
|
|
});
|
|
|
|
if (models.length === 0) {
|
|
throw new Error("OpenCode returned no models. Run `opencode models` and verify provider auth.");
|
|
}
|
|
|
|
if (!models.some((entry) => entry.id === model)) {
|
|
const sample = models.slice(0, 12).map((entry) => entry.id).join(", ");
|
|
throw new Error(
|
|
`Configured OpenCode model is unavailable: ${model}. Available models: ${sample}${models.length > 12 ? ", ..." : ""}`,
|
|
);
|
|
}
|
|
|
|
return models;
|
|
}
|
|
|
|
export async function listOpenCodeModels(): Promise<AdapterModel[]> {
|
|
try {
|
|
return await discoverOpenCodeModelsCached();
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export function resetOpenCodeModelsCacheForTests() {
|
|
discoveryCache.clear();
|
|
}
|