route.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { NextResponse } from "next/server";
  2. import { getBrivenAiAgentMe, isAiAgentConfigured } from "@/lib/briven-ai";
  3. import {
  4. brivenProjectInfoWithM2m,
  5. getBrivenM2mToken,
  6. isM2mConfigured,
  7. } from "@/lib/briven-m2m";
  8. /**
  9. * Server-only health for machine credentials (M2M + AI agent).
  10. * Never returns secrets.
  11. */
  12. export async function GET() {
  13. const projectId =
  14. process.env.BRIVEN_PROJECT_ID?.trim() ||
  15. process.env.NEXT_PUBLIC_BRIVEN_PROJECT_ID?.trim() ||
  16. null;
  17. const userAuthKeySet = Boolean(
  18. (
  19. process.env.NEXT_PUBLIC_BRIVEN_AUTH_KEY ||
  20. process.env.BRIVEN_AUTH_PUBLIC_KEY ||
  21. ""
  22. ).startsWith("pk_briven_auth_"),
  23. );
  24. // AI agent
  25. let ai:
  26. | { configured: false }
  27. | ({ configured: true } & Awaited<ReturnType<typeof getBrivenAiAgentMe>>) = {
  28. configured: false,
  29. };
  30. if (isAiAgentConfigured()) {
  31. const me = await getBrivenAiAgentMe();
  32. ai = { configured: true, ...me };
  33. }
  34. // M2M
  35. const m2m: {
  36. configured: boolean;
  37. ok?: boolean;
  38. tokenPreview?: string;
  39. projectInfo?: unknown;
  40. error?: string;
  41. } = { configured: isM2mConfigured() };
  42. if (m2m.configured) {
  43. try {
  44. const token = await getBrivenM2mToken();
  45. m2m.ok = true;
  46. m2m.tokenPreview = `${token.slice(0, 12)}…`;
  47. try {
  48. m2m.projectInfo = await brivenProjectInfoWithM2m();
  49. } catch (e) {
  50. m2m.error =
  51. e instanceof Error
  52. ? `token ok; project info: ${e.message}`
  53. : "token ok; project info failed";
  54. }
  55. } catch (e) {
  56. m2m.ok = false;
  57. m2m.error = e instanceof Error ? e.message : "m2m failed";
  58. // Platform 404 usually means api.briven.tech route not live yet — not bad local keys.
  59. if (
  60. m2m.error.includes("404") &&
  61. m2m.error.toLowerCase().includes("not found")
  62. ) {
  63. m2m.error = `${m2m.error} — credentials look set (m2m_/m2ms_); Briven API may be down or this path not deployed. Keys stay in .env.local.`;
  64. }
  65. }
  66. }
  67. return NextResponse.json({
  68. projectId,
  69. userAuth: {
  70. configured: userAuthKeySet,
  71. note: "Browser login uses pk_briven_auth_… (OTP/magic/Konnos). Email delivery needs sender domain on Briven.",
  72. },
  73. aiAgent: ai,
  74. m2m: {
  75. ...m2m,
  76. note: m2m.configured
  77. ? m2m.ok
  78. ? "Machine client credentials present and token mint works."
  79. : "Machine client credentials present in .env.local — token call failed (see error)."
  80. : "Create machine client in Auth → Keys (M2M), then set BRIVEN_M2M_CLIENT_ID + BRIVEN_M2M_CLIENT_SECRET in .env.local",
  81. },
  82. });
  83. }