Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/api/src/billing.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// https://github.com/t3dotgg/stripe-recommendations/blob/main/README.md
import Stripe from "stripe";

import { stripe } from "./stripe";
import { supabaseAdmin } from "./supabase";
import { stripe } from "./integration/stripe";
import { supabaseAdmin } from "./integration/supabase";

const HANDLED_EVENTS: Stripe.Event.Type[] = [
"checkout.session.completed",
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/hono-bindings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type * as Sentry from "@sentry/bun";
import type Stripe from "stripe";

export type AppBindings = {
Variables: {
stripeEvent: Stripe.Event;
sentrySpan: Sentry.Span;
};
};
16 changes: 9 additions & 7 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import { logger } from "hono/logger";

import { env } from "./env";
import type { AppBindings } from "./hono-bindings";
import { loadTestOverride } from "./load-test-auth";
import {
loadTestOverride,
sentryMiddleware,
supabaseAuthMiddleware,
verifyStripeWebhook,
} from "./middleware";
import { API_TAGS, routes } from "./routes";
import { sentryMiddleware } from "./sentry/middleware";
import { verifyStripeWebhook } from "./stripe";
import { requireSupabaseAuth } from "./supabase";

const app = new Hono<AppBindings>();

Expand Down Expand Up @@ -44,12 +46,12 @@ app.use("*", (c, next) => {
return corsMiddleware(c, next);
});

app.use("/chat/completions", loadTestOverride, requireSupabaseAuth);
app.use("/chat/completions", loadTestOverride, supabaseAuthMiddleware);
app.use("/webhook/stripe", verifyStripeWebhook);

if (env.NODE_ENV !== "development") {
app.use("/listen", loadTestOverride, requireSupabaseAuth);
app.use("/transcribe", loadTestOverride, requireSupabaseAuth);
app.use("/listen", loadTestOverride, supabaseAuthMiddleware);
app.use("/transcribe", loadTestOverride, supabaseAuthMiddleware);
}

app.route("/", routes);
Expand Down
4 changes: 4 additions & 0 deletions apps/api/src/integration/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./supabase";
export * from "./stripe";
export * from "./openrouter";
export * from "./posthog";
23 changes: 23 additions & 0 deletions apps/api/src/integration/openrouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { OpenAI as PostHogOpenAI } from "@posthog/ai";

import { env } from "../env";
import { posthog } from "./posthog";

export const openai = new PostHogOpenAI({
baseURL: "https://openrouter.ai/api/v1",
apiKey: env.OPENROUTER_API_KEY,
posthog,
});

const MODELS = {
toolCalling: [
"moonshotai/kimi-k2-0905:exacto",
"anthropic/claude-haiku-4.5",
"openai/gpt-oss-120b:exacto",
],
default: ["moonshotai/kimi-k2-0905", "openai/gpt-5.1-chat"],
} as const;

export function getModels(needsToolCalling: boolean): string[] {
return needsToolCalling ? [...MODELS.toolCalling] : [...MODELS.default];
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PostHog } from "posthog-node";

import { env } from "./env";
import { env } from "../env";

export const posthog = new PostHog(env.POSTHOG_API_KEY, {
host: "https://us.i.posthog.com",
Expand Down
7 changes: 7 additions & 0 deletions apps/api/src/integration/stripe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Stripe from "stripe";

import { env } from "../env";

export const stripe = new Stripe(env.STRIPE_SECRET_KEY, {
apiVersion: "2025-10-29.clover",
});
8 changes: 8 additions & 0 deletions apps/api/src/integration/supabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createClient } from "@supabase/supabase-js";

import { env } from "../env";

export const supabaseAdmin = createClient(
env.SUPABASE_URL,
env.SUPABASE_SERVICE_ROLE_KEY,
);
2 changes: 1 addition & 1 deletion apps/api/src/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Sentry from "@sentry/bun";
import type { Handler } from "hono";
import { upgradeWebSocket } from "hono/bun";

import { Metrics } from "./sentry/metrics";
import { Metrics } from "./metrics";
import {
createProxyFromRequest,
normalizeWsData,
Expand Down
24 changes: 16 additions & 8 deletions apps/api/src/sentry/metrics.ts → apps/api/src/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
import * as Sentry from "@sentry/bun";

export const Metrics = {
const billing = {
billingSync: (success: boolean, eventType: string) => {
Sentry.metrics.count("billing.sync", 1, {
attributes: { success: String(success), event_type: eventType },
});
},
};

const stt = {
websocketConnected: (provider: string) => {
Sentry.metrics.count("websocket.connected", 1, {
attributes: { provider },
});
},

websocketDisconnected: (provider: string, durationMs: number) => {
Sentry.metrics.distribution("websocket.duration", durationMs, {
unit: "millisecond",
attributes: { provider },
});
},
};

billingSync: (success: boolean, eventType: string) => {
Sentry.metrics.count("billing.sync", 1, {
attributes: { success: String(success), event_type: eventType },
});
},

const llm = {
chatCompletion: (streaming: boolean, statusCode: number) => {
Sentry.metrics.count("chat.completion", 1, {
attributes: { streaming: String(streaming), status: String(statusCode) },
});
},
};

export const Metrics = {
...stt,
...llm,
upstreamLatency: (provider: string, durationMs: number) => {
Sentry.metrics.distribution("upstream.latency", durationMs, {
unit: "millisecond",
attributes: { provider },
});
},
...billing,
};
4 changes: 4 additions & 0 deletions apps/api/src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./load-test-auth";
export * from "./sentry";
export * from "./supabase";
export * from "./stripe";
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createMiddleware } from "hono/factory";

import { env } from "./env";
import { env } from "../env";

export const loadTestOverride = createMiddleware<{
Variables: { supabaseUserId: string };
Expand Down
29 changes: 29 additions & 0 deletions apps/api/src/middleware/sentry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as Sentry from "@sentry/bun";
import { createMiddleware } from "hono/factory";

import type { AppBindings } from "../hono-bindings";

export const sentryMiddleware = createMiddleware<AppBindings>(
async (c, next) => {
const sentryTrace = c.req.header("sentry-trace");
const baggage = c.req.header("baggage");

return Sentry.continueTrace({ sentryTrace, baggage }, async () => {
return Sentry.startSpan(
{
name: `${c.req.method} ${c.req.path}`,
op: "http.server",
attributes: {
"http.method": c.req.method,
"http.url": c.req.url,
},
},
async (span) => {
c.set("sentrySpan", span);
await next();
span.setAttribute("http.status_code", c.res.status);
},
);
});
},
);
7 changes: 2 additions & 5 deletions apps/api/src/stripe.ts → apps/api/src/middleware/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import * as Sentry from "@sentry/bun";
import { createMiddleware } from "hono/factory";
import Stripe from "stripe";

import { env } from "./env";

export const stripe = new Stripe(env.STRIPE_SECRET_KEY, {
apiVersion: "2025-10-29.clover",
});
import { env } from "../env";
import { stripe } from "../integration/stripe";

const cryptoProvider = Stripe.createSubtleCryptoProvider();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { createClient } from "@supabase/supabase-js";
import { createMiddleware } from "hono/factory";

import { env } from "./env";
import { env } from "../env";

export const supabaseAdmin = createClient(
env.SUPABASE_URL,
env.SUPABASE_SERVICE_ROLE_KEY,
);

export const requireSupabaseAuth = createMiddleware<{
export const supabaseAuthMiddleware = createMiddleware<{
Variables: { supabaseUserId: string };
}>(async (c, next) => {
const authHeader = c.req.header("Authorization");
Expand Down
Loading