diff --git a/.env.example b/.env.example index 17a4445..d40544f 100644 --- a/.env.example +++ b/.env.example @@ -2,5 +2,5 @@ # Use the project token from your own PostHog project (Project settings -> Project API key). # A token copied from documentation reports into someone else's project, whose alerts, # insights and notifications you cannot read. -VITE_POSTHOG_PROJECT_TOKEN=phc_your_project_token -VITE_POSTHOG_HOST=https://your-posthog-host +PUBLIC_POSTHOG_PROJECT_TOKEN=phc_your_project_token +PUBLIC_POSTHOG_HOST=https://your-posthog-host diff --git a/src/app.html b/src/app.html index b627c14..59d258c 100644 --- a/src/app.html +++ b/src/app.html @@ -31,72 +31,6 @@ %sveltekit.head% -
%sveltekit.body%
diff --git a/src/hooks.client.ts b/src/hooks.client.ts index 11bd041..4945f07 100644 --- a/src/hooks.client.ts +++ b/src/hooks.client.ts @@ -1,6 +1,7 @@ import type { HandleClientError } from "@sveltejs/kit"; import posthog from "posthog-js"; import { dev } from "$app/environment"; +import { env } from "$env/dynamic/public"; import { captureLog } from "$lib/client/logging"; import { initBrowserTracing, startSpan } from "$lib/client/tracing"; @@ -17,16 +18,43 @@ function describeError(error: unknown): string { return String(error); } -posthog.init("phc_AS2JXQsqEwNNreH5kra5Eefd5TijExE5eiLfwdzXLhPg", { - api_host: "https://lightcms.lightcms.me", - defaults: "2026-05-30", - person_profiles: "always", -}); +export function init() { + const key = env.PUBLIC_POSTHOG_PROJECT_TOKEN; + const host = env.PUBLIC_POSTHOG_HOST; -initBrowserTracing({ - posthogKey: "phc_AS2JXQsqEwNNreH5kra5Eefd5TijExE5eiLfwdzXLhPg", - posthogHost: "https://lightcms.lightcms.me", -}); + if (!key || !host) { + if (dev) { + const variable = !key ? "PUBLIC_POSTHOG_PROJECT_TOKEN" : "PUBLIC_POSTHOG_HOST"; + throw new Error( + `${variable} variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once ${variable} is configured`, + ); + } + return; + } + + posthog.init(key, { + api_host: host, + defaults: "2026-05-30", + person_profiles: "always", + // Include browser identity and session headers on same-origin server requests. + tracing_headers: [window.location.hostname], + capture_exceptions: { + capture_unhandled_errors: true, + capture_unhandled_rejections: true, + capture_console_errors: false, + }, + logs: { + serviceName: "lightcms-web", + environment: dev ? "development" : "production", + }, + }); + posthogInitialized = true; + + initBrowserTracing({ + posthogKey: key, + posthogHost: host, + }); +} export const handleError: HandleClientError = ({ error }) => { if (!posthogInitialized) { diff --git a/src/lib/server/ai-observability.ts b/src/lib/server/ai-observability.ts index 9c0371c..3584f1b 100644 --- a/src/lib/server/ai-observability.ts +++ b/src/lib/server/ai-observability.ts @@ -6,18 +6,24 @@ import { NodeSDK } from "@opentelemetry/sdk-node"; import { PostHogSpanProcessor } from "@posthog/ai/otel"; import { registerTelemetry } from "ai"; -const key = process.env.VITE_POSTHOG_PROJECT_TOKEN; -const host = process.env.VITE_POSTHOG_HOST; +const key = process.env.PUBLIC_POSTHOG_PROJECT_TOKEN; +const host = process.env.PUBLIC_POSTHOG_HOST; let posthogSpanProcessor: PostHogSpanProcessor | undefined; let posthogLogProcessor: BatchLogRecordProcessor | undefined; let posthogLogProvider: LoggerProvider | undefined; let posthogLogger: ReturnType | undefined; -if (!key || !host) { +if (!key) { if (process.env.NODE_ENV !== "production") { console.warn( - "PostHog AI observability is not configured (VITE_POSTHOG_PROJECT_TOKEN / VITE_POSTHOG_HOST). Traces and logs will not be exported.", + "PUBLIC_POSTHOG_PROJECT_TOKEN variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once PUBLIC_POSTHOG_PROJECT_TOKEN is configured", + ); + } +} else if (!host) { + if (process.env.NODE_ENV !== "production") { + console.warn( + "PUBLIC_POSTHOG_HOST variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once PUBLIC_POSTHOG_HOST is configured", ); } } else { diff --git a/src/lib/server/posthog.ts b/src/lib/server/posthog.ts index 7ef4022..550924b 100644 --- a/src/lib/server/posthog.ts +++ b/src/lib/server/posthog.ts @@ -1,5 +1,5 @@ import { dev } from "$app/environment"; -import { env } from "$env/dynamic/private"; +import { env } from "$env/dynamic/public"; import { PostHog } from "posthog-node"; let posthogClient: PostHog | undefined; @@ -26,13 +26,13 @@ const HTTP_METHODS = new Set(["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST function getPostHogClient() { if (posthogClient) return posthogClient; - const key = env.VITE_POSTHOG_PROJECT_TOKEN; - const host = env.VITE_POSTHOG_HOST; + const key = env.PUBLIC_POSTHOG_PROJECT_TOKEN; + const host = env.PUBLIC_POSTHOG_HOST; if (!key) { if (dev) { console.warn( - "VITE_POSTHOG_PROJECT_TOKEN variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once VITE_POSTHOG_PROJECT_TOKEN is configured", + "PUBLIC_POSTHOG_PROJECT_TOKEN variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once PUBLIC_POSTHOG_PROJECT_TOKEN is configured", ); } return; @@ -41,7 +41,7 @@ function getPostHogClient() { if (!host) { if (dev) { console.warn( - "VITE_POSTHOG_HOST variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once VITE_POSTHOG_HOST is configured", + "PUBLIC_POSTHOG_HOST variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once PUBLIC_POSTHOG_HOST is configured", ); } return; diff --git a/src/lib/server/speech-ai-sdk.ts b/src/lib/server/speech-ai-sdk.ts index d6d4712..1c3b5c1 100644 --- a/src/lib/server/speech-ai-sdk.ts +++ b/src/lib/server/speech-ai-sdk.ts @@ -9,6 +9,11 @@ export interface TranscribeAiSdkOptions { language?: string; prompt?: string; temperature?: number; + observability?: { + distinctId?: string; + sessionId?: string; + traceName?: string; + }; } export interface AiSdkTranscriptionResult { @@ -119,8 +124,20 @@ export async function transcribeWithAiSdk( providerOptions: { openai: providerOptions, }, + runtimeContext: options?.observability + ? { + distinctId: options.observability.distinctId, + sessionId: options.observability.sessionId, + traceName: options.observability.traceName, + } + : undefined, telemetry: { functionId: "cms_speech_transcription", + includeRuntimeContext: { + distinctId: true, + sessionId: true, + traceName: true, + }, recordInputs: true, recordOutputs: true, }, diff --git a/src/lib/server/transcription.ts b/src/lib/server/transcription.ts index c8c1f8e..5dfa444 100644 --- a/src/lib/server/transcription.ts +++ b/src/lib/server/transcription.ts @@ -115,6 +115,13 @@ export async function transcribeAudio( language: options?.language, prompt: options?.prompt, temperature: options?.temperature, + observability: options?.observability + ? { + distinctId: options.observability.distinctId, + sessionId: options.observability.sessionId, + traceName: "cms_speech_transcription", + } + : undefined, }); return {