mirror of
https://github.com/CopilotKit/CopilotKit.git
synced 2026-09-14 16:26:20 +08:00
e1b3e363fa
Mirrors upstream #4643 + #4646 follow-ups. New trackCommandCopy helper
infers install_type from the leading token (covers npx/pnpm/pip/uv/
docker/curl/brew/helm/kubectl/make/bash/sh, falls back to "code"), and
the CopyTracker provider monkey-patches navigator.clipboard.writeText
once at app boot so every programmatic copy fires cli_command_copied
without per-component instrumentation. Preserves upstream's chained-
wrapper pattern so it coexists with Reo's writeText patch.
Final event shape is { install_type, location? } — the command body
and product props from the original draft were dropped upstream
(commit 0b7b3c77c) before merge.
51 lines
1.0 KiB
TypeScript
51 lines
1.0 KiB
TypeScript
import type { PostHog } from "posthog-js";
|
|
|
|
const KNOWN_INSTALL_TYPES = [
|
|
"npx",
|
|
"npm",
|
|
"pnpm",
|
|
"yarn",
|
|
"bun",
|
|
"pip",
|
|
"uv",
|
|
"poetry",
|
|
"cargo",
|
|
"go",
|
|
"docker",
|
|
"curl",
|
|
"brew",
|
|
"helm",
|
|
"kubectl",
|
|
"make",
|
|
"bash",
|
|
"sh",
|
|
] as const;
|
|
|
|
export type InstallType = (typeof KNOWN_INSTALL_TYPES)[number] | "code";
|
|
|
|
function inferInstallType(command: string): InstallType {
|
|
const firstToken = command.trim().split(/\s+/)[0]?.toLowerCase();
|
|
if (!firstToken) return "code";
|
|
return (KNOWN_INSTALL_TYPES as readonly string[]).includes(firstToken)
|
|
? (firstToken as InstallType)
|
|
: "code";
|
|
}
|
|
|
|
export type TrackCommandCopyArgs = {
|
|
command: string;
|
|
location?: string;
|
|
};
|
|
|
|
export function trackCommandCopy(
|
|
posthog: PostHog | undefined,
|
|
{ command, location }: TrackCommandCopyArgs,
|
|
) {
|
|
if (!posthog) return;
|
|
const trimmed = command.trim();
|
|
if (!trimmed) return;
|
|
posthog.capture("cli_command_copied", {
|
|
install_type: inferInstallType(trimmed),
|
|
...(location ? { location } : {}),
|
|
});
|
|
}
|