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
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ JEV_MODEL=jev-latest
# LANGGRAPH_DEPLOYMENT_URL=http://localhost:8123
# MCP_SERVER_URL=http://localhost:3100/mcp

# Telemetry (docs/telemetry.md): anonymous install counts, on by default.
# Set either to turn it off.
# DO_NOT_TRACK=1
# COPILOTKIT_TELEMETRY_DISABLED=1
# OIUI_DISTRIBUTION=self_hosted
# Optional browser page analytics; leave unset unless you want them.
# NEXT_PUBLIC_POSTHOG_KEY=

# Rate limiting (per IP) — disabled by default
RATE_LIMIT_ENABLED=false
RATE_LIMIT_WINDOW_MS=60000
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ bun.lockb
# Turbo
.turbo

# Telemetry installation id (docs/telemetry.md)
.open-intelligent-ui

# Tools
.claude

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ Built with CopilotKit. The `openGenerativeUI` runtime API and `generateSandboxed
- [Documentation index](docs/README.md)
- [Bring these patterns to your app](docs/bring-to-your-app.md)
- [Source repository](https://github.com/CopilotKit/OpenIntelligentUI)
- [Telemetry and how to turn it off](docs/telemetry.md)

## Contributing

Expand Down
1 change: 1 addition & 0 deletions apps/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@jetbrains/websandbox": "1.2.1",
"@repo/design-system": "workspace:*",
"next": "16.1.6",
"posthog-js": "^1.438.8",
"qrcode.react": "^4.2.0",
"react": "^19.2.4",
"react-dom": "^19.2.4",
Expand Down
39 changes: 39 additions & 0 deletions apps/app/src/app/__tests__/posthog-analytics.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { render } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";

const posthog = vi.hoisted(() => ({ __loaded: false, init: vi.fn(), register: vi.fn() }));
vi.mock("posthog-js", () => ({ default: posthog }));

import { PostHogAnalytics } from "../posthog-analytics";

afterEach(() => {
vi.unstubAllEnvs();
posthog.init.mockReset();
posthog.register.mockReset();
posthog.__loaded = false;
});

describe("PostHogAnalytics", () => {
it("loads nothing without a PostHog key", () => {
vi.stubEnv("NEXT_PUBLIC_POSTHOG_KEY", "");
render(<PostHogAnalytics />);
expect(posthog.init).not.toHaveBeenCalled();
});

it("starts PostHog with session recording off when a key is set", () => {
vi.stubEnv("NEXT_PUBLIC_POSTHOG_KEY", "phc_test");
render(<PostHogAnalytics />);
expect(posthog.init).toHaveBeenCalledWith(
"phc_test",
expect.objectContaining({ api_host: "https://eu.i.posthog.com", disable_session_recording: true }),
);
expect(posthog.register).toHaveBeenCalledWith({ app: "open-intelligent-ui" });
});

it("does not start twice", () => {
vi.stubEnv("NEXT_PUBLIC_POSTHOG_KEY", "phc_test");
posthog.__loaded = true;
render(<PostHogAnalytics />);
expect(posthog.init).not.toHaveBeenCalled();
});
});
17 changes: 15 additions & 2 deletions apps/app/src/app/api/copilotkit/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {
ExperimentalEmptyAdapter,
copilotRuntimeNextJSAppRouterEndpoint,
} from "@copilotkit/runtime";
import { NextRequest } from "next/server";
import { NextRequest, after } from "next/server";
import { readProviderHeaders, PROVIDER_KEY_INPUT_ERROR } from "@/lib/provider-keys";
import { buildRuntimeOptions } from "@/lib/copilotkit-runtime-options";
import { getLaunchTelemetry, observeRunResult, runtimeMethod } from "@/lib/launch-telemetry";

// Simple in-memory sliding-window rate limiter (per IP)
// Enable via RATE_LIMIT_ENABLED=true — off by default.
Expand Down Expand Up @@ -55,11 +56,23 @@ export const POST = async (req: NextRequest) => {
providerHeaders,
});

// Install counting (docs/telemetry.md). Only the first run's request body is inspected.
const telemetry = getLaunchTelemetry();
const telemetryProperties = telemetry.runtimeProperties();
const watchRun = !telemetry.activated && (await runtimeMethod(req)) === "agent/run";

const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
endpoint: "/api/copilotkit",
serviceAdapter: new ExperimentalEmptyAdapter(),
runtime: new CopilotRuntime(runtimeOptions),
...(telemetryProperties && { properties: { _copilotkit: telemetryProperties } }),
});

return handleRequest(req);
const response = await handleRequest(req);
after(telemetry.recordOpened());
if (!watchRun || !response.body) return response;

const [client, observer] = response.body.tee();
after(observeRunResult(observer).then((ok) => (ok ? telemetry.recordActivated() : undefined)));
return new Response(client, { status: response.status, statusText: response.statusText, headers: response.headers });
};
23 changes: 23 additions & 0 deletions apps/app/src/app/posthog-analytics.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use client";

import { useEffect } from "react";
import posthog from "posthog-js";

// Page analytics for the hosted demo only. Nothing loads unless
// NEXT_PUBLIC_POSTHOG_KEY is set at build time, so clones of this template
// don't report page views. See docs/telemetry.md.
export function PostHogAnalytics() {
useEffect(() => {
const key = process.env.NEXT_PUBLIC_POSTHOG_KEY;
if (!key || posthog.__loaded) return;
posthog.init(key, {
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST || "https://eu.i.posthog.com",
defaults: "2025-05-24",
person_profiles: "identified_only",
// Visitors type provider API keys into this app; never record sessions.
disable_session_recording: true,
});
posthog.register({ app: "open-intelligent-ui" });
}, []);
return null;
}
2 changes: 2 additions & 0 deletions apps/app/src/app/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { OpenGenUIPromptBridge } from "@/lib/sandbox/prompt-bridge";
import { tableCatalog } from "@/components/generative-ui/table-catalog";
import { ProviderKeysProvider, useProviderKeys } from "@/components/chat/provider-keys";
import { ThemeProvider } from "@/hooks/use-theme";
import { PostHogAnalytics } from "./posthog-analytics";

const a2ui = { catalog: tableCatalog };

Expand All @@ -21,6 +22,7 @@ const openGenerativeUI = {
export function Providers({ children }: { children: React.ReactNode }) {
return (
<ThemeProvider>
<PostHogAnalytics />
<ProviderKeysProvider>
<ChatProviders>{children}</ChatProviders>
</ProviderKeysProvider>
Expand Down
Loading
Loading