fix(onboarding): make ConnectMachineStep actually work for new customers

The connect step instructed new users to run
`npx @cockpit/agent init --token ck_...`, but:

  • `@cockpit/agent` is NOT published to npm — `npm view` returns 404
  • The repo is private, so raw GitHub fallback URLs are also gated
  • Result: every new customer hit a dead end at step 3 of onboarding

The CLI itself is fully implemented at packages/agent/bin/cockpit-agent.js
(144 lines, no external deps — only Node builtins + global fetch). It just
isn't reachable from outside this repo.

Three-piece fix that ships value today without needing npm publish:

  1. New route /api/agent/install (Node runtime) reads the script body
     and serves it as application/javascript. Public, cached 5 min.

  2. next.config.ts outputFileTracingIncludes pins packages/agent/bin/**
     to that route so Vercel's tracer ships the file in the bundle
     (otherwise tree-shaken — the route reads it via fs at runtime).

  3. src/proxy.ts matcher exception adds api/agent/install so the
     curl request reaches the route without a session cookie.

  4. ConnectMachineStep init command swaps to:
       curl -fsSL ${APP_URL}/api/agent/install | node - init --token ...
     The npx form is left in a comment for the eventual swap-back when
     @cockpit/agent ships to npm publicly.

What this still does NOT fix (out of scope for one commit): the daemon
script (`./scripts/cockpit-daemon.sh`) still requires a cockpit repo
clone — the agent CLI only writes the token env file. That's the next
layer of the "no public install path" gap.
This commit is contained in:
g-but committed 2026-05-24 23:55:32 +02:00
1 parent 047e29ceb6
commit 422873a21d
4 files changed
+54 -3

No files matched your search

+6
View File
@@ -5,6 +5,12 @@ const nextConfig: NextConfig = {
outputFileTracingExcludes: {
"/api/agent/launch": ["./next.config.ts"],
},
outputFileTracingIncludes: {
// Serves the @cockpit/agent CLI script to new customers (the package
// isn't published to npm yet and the repo is private). Without explicit
// tracing, Vercel would tree-shake the file out of the deployment bundle.
"/api/agent/install": ["./packages/agent/bin/**"],
},
};
export default nextConfig;
+39
View File
@@ -0,0 +1,39 @@
import { NextResponse } from "next/server";
import { readFile } from "node:fs/promises";
import path from "node:path";
/**
* Serves the @cockpit/agent CLI script body so a new customer can install
* it from the cloud without needing the package to be published on npm.
*
* The package exists at packages/agent/bin/cockpit-agent.js but is NOT
* published — `npm view @cockpit/agent` returns 404 — and the repo itself
* is private, so the raw GitHub URL is also gated. Until the package
* ships to npm publicly, this endpoint is the only working install path.
*
* Public (no auth) — matches the matcher exception in src/proxy.ts.
* Designed to be piped: curl -fsSL ${APP_URL}/api/agent/install | node - init --token ck_…
*
* next.config.ts includes packages/agent/bin/** in this route's
* outputFileTracingIncludes so the file lands in the Vercel deployment
* bundle (it would otherwise be tree-shaken out as not imported).
*/
export async function GET() {
try {
const filePath = path.join(process.cwd(), "packages", "agent", "bin", "cockpit-agent.js");
const body = await readFile(filePath, "utf8");
return new NextResponse(body, {
status: 200,
headers: {
"Content-Type": "application/javascript; charset=utf-8",
"Cache-Control": "public, max-age=300, s-maxage=300",
},
});
} catch (e) {
console.error("[agent/install] failed to read agent script:", (e as Error)?.message);
return NextResponse.json(
{ error: "Agent script unavailable" },
{ status: 500 },
);
}
}
@@ -3,7 +3,7 @@
import { useState, useEffect, useCallback } from "react";
import { Check, Copy, Loader2, Terminal, Wifi } from "lucide-react";
import { getJson, postJson } from "@/lib/api/fetch";
import { APP_NAME, APP_SLUG, APP_URL } from "@/config/brand";
import { APP_NAME, APP_URL } from "@/config/brand";
type Props = {
saving: boolean;
@@ -60,8 +60,12 @@ export function ConnectMachineStep({ saving, onComplete, onSkip }: Props) {
setTimeout(() => setCopied(null), 2000);
}
// @cockpit/agent isn't on npm yet and the repo is private, so the
// npx form doesn't work for new customers. The cloud serves the CLI
// itself from /api/agent/install — pipe it into node. When the npm
// package ships, swap this back to the npx form.
const initCommand = token
? `npx @${APP_SLUG}/agent init --token ${token} --base-url ${APP_URL}`
? `curl -fsSL ${APP_URL}/api/agent/install | node - init --token ${token} --base-url ${APP_URL}`
: null;
const connected = status?.daemonConnected ?? false;
+3 -1
View File
@@ -30,12 +30,14 @@ export const config = {
* u/ – public user profiles (/u/[username])
* beacon – public beacon page
* api/auth – NextAuth internal endpoints
* api/agent/install – serves the @cockpit/agent CLI for curl|node install
* (public so new customers can run it before they sign in)
* api/health, api/setup – infrastructure endpoints (pre-auth)
* api/crons, api/system – GET excluded for daemon/monitoring; write methods enforce auth in-handler
* api/invitations/ – token-scoped invitation routes (GET validate, POST accept);
* trailing slash keeps GET /api/invitations (list) protected
* api/stripe/webhook – Stripe webhook; verifies its own Stripe-Signature header
*/
"/((?!_next/static|_next/image|favicon\\.ico|icon\\.svg|manifest\\.json|opengraph-image|twitter-image|robots\\.txt|sitemap\\.xml|sign-in|sign-up|forgot-password|reset-password|verify-email|setup|invite|whitepaper|thoughts|u/|beacon|api/auth|api/health|api/setup|api/crons|api/system|api/beacon|api/invitations/|api/stripe/webhook).+)",
"/((?!_next/static|_next/image|favicon\\.ico|icon\\.svg|manifest\\.json|opengraph-image|twitter-image|robots\\.txt|sitemap\\.xml|sign-in|sign-up|forgot-password|reset-password|verify-email|setup|invite|whitepaper|thoughts|u/|beacon|api/auth|api/agent/install|api/health|api/setup|api/crons|api/system|api/beacon|api/invitations/|api/stripe/webhook).+)",
],
};