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
37 changes: 33 additions & 4 deletions apps/api/src/app/api/trpc/[trpc]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { appRouter } from "@superset/trpc";
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import { createContext } from "@/trpc/context";

const LEADERBOARD_CACHE_SECONDS = 300;
const STATS_CACHE_SECONDS = 3600;

export const maxDuration = 60;

const handler = (req: Request) =>
Expand All @@ -10,11 +13,37 @@ const handler = (req: Request) =>
req,
router: appRouter,
createContext,
// Anonymous leaderboard reads are the only cacheable surface here; every
// other procedure varies by session. Cache only when *every* path in the
// batch is under leaderboard.public, or a session-varying response gets
// served from the CDN to the next caller.
responseMeta: ({ paths, type, errors }) => {
if (type !== "query" || errors.length > 0 || !paths?.length) return {};
if (!paths.every((path) => path.startsWith("leaderboard.public."))) {
return {};
}
const maxAge = paths.every((path) => path === "leaderboard.public.stats")
? STATS_CACHE_SECONDS
: LEADERBOARD_CACHE_SECONDS;
return {
headers: {
"cache-control": `public, s-maxage=${maxAge}, stale-while-revalidate=86400`,
vary: "origin",
},
};
},
onError: ({ path, error }) => {
// Suppress NOT_FOUND only for the known-dead device.heartbeat path
// (removed in #4490). Old desktop clients gated behind UpdateRequiredPage
// still call it; all other NOT_FOUND errors should remain visible.
if (error.code === "NOT_FOUND" && path === "device.heartbeat") return;
// Suppress NOT_FOUND for the known-dead device.heartbeat path (removed in
// #4490, old desktop clients gated behind UpdateRequiredPage still call
// it) and for public profile lookups, where an unknown handle is normal
// crawler traffic. All other NOT_FOUND errors should remain visible.
if (
error.code === "NOT_FOUND" &&
(path === "device.heartbeat" ||
path === "leaderboard.public.participant")
) {
return;
}
console.error(`❌ tRPC error on ${path ?? "<no-path>"}:`, error);
},
});
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export const env = createEnv({
NEXT_PUBLIC_API_URL: z.string().url(),
NEXT_PUBLIC_WEB_URL: z.string().url(),
NEXT_PUBLIC_ADMIN_URL: z.string().url(),
NEXT_PUBLIC_MARKETING_URL: z.string().url(),
NEXT_PUBLIC_DESKTOP_URL: z.string().url().optional(),
NEXT_PUBLIC_POSTHOG_KEY: z.string().min(1),
NEXT_PUBLIC_POSTHOG_HOST: z.string().url(),
Expand All @@ -95,6 +96,7 @@ export const env = createEnv({
NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,
NEXT_PUBLIC_WEB_URL: process.env.NEXT_PUBLIC_WEB_URL,
NEXT_PUBLIC_ADMIN_URL: process.env.NEXT_PUBLIC_ADMIN_URL,
NEXT_PUBLIC_MARKETING_URL: process.env.NEXT_PUBLIC_MARKETING_URL,
NEXT_PUBLIC_DESKTOP_URL: process.env.NEXT_PUBLIC_DESKTOP_URL,
NEXT_PUBLIC_POSTHOG_KEY: process.env.NEXT_PUBLIC_POSTHOG_KEY,
NEXT_PUBLIC_POSTHOG_HOST: process.env.NEXT_PUBLIC_POSTHOG_HOST,
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function getAllowedOrigins(deploymentOrigin: string) {
return [
env.NEXT_PUBLIC_WEB_URL,
env.NEXT_PUBLIC_ADMIN_URL,
env.NEXT_PUBLIC_MARKETING_URL,
env.NEXT_PUBLIC_DESKTOP_URL,
...getTrustedVercelPreviewOrigins(deploymentOrigin),
...desktopDevOrigins,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { Button } from "@superset/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@superset/ui/dialog";
import { Input } from "@superset/ui/input";
import { Label } from "@superset/ui/label";
import { useEffect, useState } from "react";
import { RankTeaser } from "./components/RankTeaser";
import type { LeaderboardPreview } from "./types";

const HANDLE_PATTERN = /^[a-z0-9](?:[a-z0-9]|-(?=[a-z0-9])){1,38}$/;

interface LeaderboardJoinDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
preview: LeaderboardPreview | null;
suggestedHandle: string | null;
isLoading: boolean;
isJoining: boolean;
onConfirm: (handle: string) => void;
}

export function LeaderboardJoinDialog({
open,
onOpenChange,
preview,
suggestedHandle,
isLoading,
isJoining,
onConfirm,
}: LeaderboardJoinDialogProps) {
const [handle, setHandle] = useState("");
const [edited, setEdited] = useState(false);

useEffect(() => {
if (!edited && suggestedHandle) setHandle(suggestedHandle);
}, [edited, suggestedHandle]);

const trimmed = handle.trim().toLowerCase();
const valid = HANDLE_PATTERN.test(trimmed);

return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-md">
<DialogHeader>
<DialogTitle>Join the leaderboard</DialogTitle>
<DialogDescription>
Publishes your token counts and model names, never repo names, file
paths or prompts. You can leave at any time, which deletes
everything you've published.
</DialogDescription>
</DialogHeader>

<div className="space-y-4">
{isLoading ? (
<p className="text-sm text-muted-foreground">
Reading your local usage…
</p>
) : preview ? (
<RankTeaser preview={preview} />
) : null}

<div className="space-y-1.5">
<Label htmlFor="leaderboard-handle">Handle</Label>
<Input
id="leaderboard-handle"
value={handle}
onChange={(event) => {
setEdited(true);
setHandle(event.target.value);
}}
placeholder="pick a name for the board"
autoComplete="off"
spellCheck={false}
/>
<p className="text-xs text-muted-foreground">
Shown publicly alongside your name.
</p>
</div>
</div>

<DialogFooter>
<Button
variant="ghost"
size="sm"
onClick={() => onOpenChange(false)}
disabled={isJoining}
>
Cancel
</Button>
<Button
size="sm"
disabled={!valid || isJoining}
onClick={() => onConfirm(trimmed)}
>
{isJoining ? "Joining…" : "Join"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { formatTokens } from "@superset/shared/format-tokens";
import type { LeaderboardPreview } from "../../types";

const MIN_PARTICIPANTS_FOR_RANK = 50;

export function RankTeaser({ preview }: { preview: LeaderboardPreview }) {
if (preview.tokens === 0) {
return (
<p className="text-sm text-muted-foreground">
No Claude or Codex usage found on this machine yet. Join now and you'll
appear once you've used an agent.
</p>
);
}

return (
<div className="space-y-1">
{preview.total >= MIN_PARTICIPANTS_FOR_RANK ? (
<p className="text-sm">
You'd be{" "}
<span className="font-medium text-foreground">#{preview.rank}</span>{" "}
of {preview.total}.
</p>
) : (
<p className="text-sm">You'd be one of the first on the board.</p>
)}
<p className="text-xs text-muted-foreground">
Based on {formatTokens(preview.tokens)} tokens in the last 30 days,
counting {preview.providers.join(" and ")}.
</p>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { RankTeaser } from "./RankTeaser";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { LeaderboardJoinDialog } from "./LeaderboardJoinDialog";
export type { LeaderboardPreview } from "./types";
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { RouterOutputs } from "@superset/trpc";

export type LeaderboardPreview = Pick<
RouterOutputs["leaderboard"]["previewRank"],
"rank" | "total"
> & {
tokens: number;
providers: string[];
};
65 changes: 65 additions & 0 deletions apps/desktop/src/renderer/lib/leaderboard/askedState.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { afterAll, beforeEach, describe, expect, it } from "bun:test";
import { markLeaderboardAsked, readLeaderboardAsked } from "./askedState";

const originalLocalStorage = Object.getOwnPropertyDescriptor(
globalThis,
"localStorage",
);

function installStorage(): void {
const entries = new Map<string, string>();
Object.defineProperty(globalThis, "localStorage", {
configurable: true,
writable: true,
value: {
getItem: (key: string) => entries.get(key) ?? null,
setItem: (key: string, value: string) => {
entries.set(key, value);
},
removeItem: (key: string) => {
entries.delete(key);
},
clear: () => entries.clear(),
key: (index: number) => [...entries.keys()][index] ?? null,
get length() {
return entries.size;
},
} satisfies Storage,
});
}

beforeEach(installStorage);

afterAll(() => {
if (originalLocalStorage) {
Object.defineProperty(globalThis, "localStorage", originalLocalStorage);
} else {
Reflect.deleteProperty(globalThis, "localStorage");
}
});

describe("leaderboard asked flag", () => {
it("reports not asked on a fresh profile", () => {
expect(readLeaderboardAsked()).toBe(false);
});

it("round-trips through markLeaderboardAsked", () => {
markLeaderboardAsked();
expect(readLeaderboardAsked()).toBe(true);
});

it("treats an unrecognised value as not asked", () => {
localStorage.setItem("leaderboard-asked-v1", "{}");
expect(readLeaderboardAsked()).toBe(false);
});

it("suppresses the prompt when storage is unavailable", () => {
Object.defineProperty(globalThis, "localStorage", {
configurable: true,
get() {
throw new Error("denied");
},
});
expect(readLeaderboardAsked()).toBe(true);
});
});
17 changes: 17 additions & 0 deletions apps/desktop/src/renderer/lib/leaderboard/askedState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const STORAGE_KEY = "leaderboard-asked-v1";

export function readLeaderboardAsked(): boolean {
try {
return localStorage.getItem(STORAGE_KEY) === "true";
} catch {
return true;
}
}

export function markLeaderboardAsked(): void {
try {
localStorage.setItem(STORAGE_KEY, "true");
} catch {
return;
}
}
13 changes: 13 additions & 0 deletions apps/desktop/src/renderer/lib/leaderboard/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export { markLeaderboardAsked, readLeaderboardAsked } from "./askedState";
export type {
LeaderboardFactoryDay,
LeaderboardPayload,
LeaderboardPayloadDay,
} from "./publishUsage";
export {
BACKFILL_DAYS,
buildPayload,
PREVIEW_DAYS,
publishPayload,
publishUsage,
} from "./publishUsage";
53 changes: 53 additions & 0 deletions apps/desktop/src/renderer/lib/leaderboard/publishUsage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { RouterInputs } from "@superset/trpc";
import { PUBLISH_PAYLOAD_VERSION } from "@superset/trpc/leaderboard-schema";
import { apiTrpcClient } from "renderer/lib/api-trpc-client";
import { getHostServiceClientByUrl } from "renderer/lib/host-service-client";

export const BACKFILL_DAYS = 30;

export const PREVIEW_DAYS = 30;

type PublishInput = RouterInputs["leaderboard"]["publish"];

export type LeaderboardPayloadDay = PublishInput["days"][number];
export type LeaderboardFactoryDay = NonNullable<
PublishInput["factoryDays"]
>[number];

export interface LeaderboardPayload {
Comment thread
harshithmullapudi marked this conversation as resolved.
Resolved
days: LeaderboardPayloadDay[];
factoryDays: LeaderboardFactoryDay[];
}

export async function buildPayload(
hostUrl: string,
days: number,
): Promise<LeaderboardPayload> {
return await getHostServiceClientByUrl(
hostUrl,
).usage.leaderboardPayload.query({ days });
}

export async function publishPayload(
machineId: string,
payload: LeaderboardPayload,
): Promise<{ written: number; days: number }> {
if (payload.days.length === 0 && payload.factoryDays.length === 0) {
return { written: 0, days: 0 };
}

return await apiTrpcClient.leaderboard.publish.mutate({
payloadVersion: PUBLISH_PAYLOAD_VERSION,
hostId: machineId,
days: payload.days,
factoryDays: payload.factoryDays,
});
}

export async function publishUsage(
hostUrl: string,
machineId: string,
days: number = BACKFILL_DAYS,
): Promise<{ written: number; days: number }> {
return await publishPayload(machineId, await buildPayload(hostUrl, days));
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ export const PERSISTED_KEY_REGISTRY: ReadonlyArray<
"src/renderer/components/PostHogUserIdentifier/PostHogUserIdentifier.tsx",
["active_organization_id"],
],
[
"src/renderer/routes/_authenticated/components/LeaderboardAutoPublish/hooks/useLeaderboardAutoPublish/autoPublishState.ts",
["leaderboard-auto-publish-v1"],
],
["src/renderer/lib/leaderboard/askedState.ts", ["leaderboard-asked-v1"]],
[
"src/renderer/hooks/useAgentModelPreference/useAgentModelPreference.ts",
["lastSelectedV2WorkspaceCreateModelByPreset"],
Expand Down
Loading
Loading