Skip to content

Open source alternative to Semrush and Ahrefs

1.4 KB
Raw Blame
import { createServerFn } from "@tanstack/react-start";
import { z } from "zod";
import { getOpenRouterAccess } from "@/server/lib/openrouter";
import {
  getWorkersEnv,
  isHostedServerAuthMode,
} from "@/server/lib/runtime-env";
import { requireProjectContext } from "@/serverFunctions/middleware";

const OPENROUTER_KEY_MISSING_MESSAGE =
  "OPENROUTER_API_KEY is not set for this deployment yet. Add it to your environment, restart OpenSEO, then confirm here.";

const projectScopedSchema = z.object({ projectId: z.string().min(1) });

type SamAccessStatus = {
  enabled: boolean;
  errorMessage: string | null;
};

// Gates the in-app AI agent (SAM) on an OpenRouter key being configured, the
// same way backlinks/AI-search gate on their DataForSEO subscriptions. Hosted
// deployments always have the key provisioned, so only self-hosted is checked.
export const getSamAccessSetupStatus = createServerFn({ method: "GET" })
  .middleware(requireProjectContext)
  .validator(projectScopedSchema)
  .handler(async (): Promise<SamAccessStatus> => {
    if (await isHostedServerAuthMode()) {
      return { enabled: true, errorMessage: null };
    }

    // Behind an Every App gateway this is the gateway's binding; the gateway
    // answers 503 until an admin sets its OPENROUTER_API_KEY.
    const enabled = Boolean(getOpenRouterAccess((await getWorkersEnv()) ?? {}));
    return {
      enabled,
      errorMessage: enabled ? null : OPENROUTER_KEY_MISSING_MESSAGE,
    };
  });