feat: posthog telemetry: chat, sessionStart, and slash commands

This commit is contained in:
Dallin Romney 2025-07-25 16:49:40 +07:00
parent 26af132814
commit 9e39d8483c
6 changed files with 124 additions and 4 deletions

14
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "@continuedev/cli",
"version": "1.4.2",
"version": "0.0.0-dev",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@continuedev/cli",
"version": "1.4.2",
"version": "0.0.0-dev",
"license": "Apache-2.0",
"dependencies": {
"@continuedev/config-yaml": "^1.0.96",
@ -38,6 +38,7 @@
"minimatch": "^10.0.3",
"node-fetch": "^3.3.2",
"open": "^10.1.0",
"posthog-node": "^5.6.0",
"react": "^19.1.0",
"readline-sync": "^1.4.10",
"turndown": "^7.2.0",
@ -14123,6 +14124,15 @@
"node": ">=4"
}
},
"node_modules/posthog-node": {
"version": "5.6.0",
"resolved": "https://registry.npmjs.org/posthog-node/-/posthog-node-5.6.0.tgz",
"integrity": "sha512-MVXxKmqAYp2cPBrN1YMhnhYsJYIu6yc6wumbHz1dbo67wZBf2WtMm67Uh+4VCrp07049qierWlxQqz1W5zGDeg==",
"license": "MIT",
"engines": {
"node": ">=20"
}
},
"node_modules/pretty-format": {
"version": "29.7.0",
"resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.7.0.tgz",

View file

@ -60,6 +60,7 @@
"minimatch": "^10.0.3",
"node-fetch": "^3.3.2",
"open": "^10.1.0",
"posthog-node": "^5.6.0",
"react": "^19.1.0",
"readline-sync": "^1.4.10",
"turndown": "^7.2.0",

View file

@ -17,6 +17,7 @@ import { startTUIChat } from "../ui/index.js";
import { safeStdout } from "../util/consoleOverride.js";
import { formatError } from "../util/formatError.js";
import logger from "../util/logger.js";
import { posthogService } from "../telemetry/posthogService.js";
export interface ChatOptions {
headless?: boolean;
@ -149,6 +150,7 @@ export async function chat(prompt?: string, options: ChatOptions = {}) {
try {
// Record session start
telemetryService.recordSessionStart();
posthogService.capture("sessionStart", {});
// Start active time tracking
telemetryService.startActiveTime();

View file

@ -7,6 +7,7 @@ import {
} from "./auth/workos.js";
import { getAllSlashCommands } from "./commands/commands.js";
import { reloadService, SERVICE_NAMES, services } from "./services/index.js";
import { posthogService } from "./telemetry/posthogService.js";
export async function handleSlashCommands(
input: string,
@ -29,14 +30,29 @@ export async function handleSlashCommands(
"Available commands:",
...allCommands.map((cmd) => `/${cmd.name} - ${cmd.description}`),
].join("\n");
posthogService.capture("useSlashCommand", {
name: "help",
});
return { output: helpMessage };
case "clear":
posthogService.capture("useSlashCommand", {
name: "clear",
});
return { clear: true, output: "Chat history cleared" };
case "exit":
posthogService.capture("useSlashCommand", {
name: "exit",
});
return { exit: true, output: "Goodbye!" };
case "config":
posthogService.capture("useSlashCommand", {
name: "config",
});
return { openConfigSelector: true };
case "login":
posthogService.capture("useSlashCommand", {
name: "login",
});
try {
const newAuthState = await services.auth.login();
@ -66,6 +82,9 @@ export async function handleSlashCommands(
}
case "logout":
posthogService.capture("useSlashCommand", {
name: "logout",
});
try {
await services.auth.logout();
@ -82,6 +101,9 @@ export async function handleSlashCommands(
}
case "whoami":
posthogService.capture("useSlashCommand", {
name: "whoami",
});
if (isAuthenticated()) {
const config = loadAuthConfig();
if (config && isAuthenticatedConfig(config)) {
@ -103,6 +125,9 @@ export async function handleSlashCommands(
}
case "org":
posthogService.capture("useSlashCommand", {
name: "org",
});
// Organization switching command
if (args.length === 0) {
return {
@ -156,7 +181,6 @@ export async function handleSlashCommands(
};
}
}
default:
const assistantPrompt = assistant.prompts?.find(
(prompt) => prompt?.name === command
@ -169,4 +193,4 @@ export async function handleSlashCommands(
}
}
return null;
}
}

View file

@ -0,0 +1,69 @@
import os from "node:os";
import type { PostHog as PostHogType } from "posthog-node";
export class PosthogService {
private os: string | undefined;
private uniqueId: string;
constructor() {
this.os = os.platform();
this.uniqueId = "NOT_UNIQUE";
}
get isEnabled() {
return process.env.CONTINUE_CLI_ENABLE_TELEMETRY !== "0";
}
private _client: PostHogType | undefined;
private async getClient() {
if (this.isEnabled) {
if (!this._client) {
const { PostHog } = await import("posthog-node");
this._client = new PostHog(
"phc_JS6XFROuNbhJtVCEdTSYk6gl5ArRrTNMpCcguAXlSPs",
{
host: "https://app.posthog.com",
}
);
}
} else {
this._client = undefined;
}
return this._client;
}
async capture(event: string, properties: { [key: string]: any }) {
const client = await this.getClient();
if (!client) {
return;
}
try {
const augmentedProperties = {
...properties,
os: this.os,
extensionVersion: "", // TODO cn version
ideName: "cn",
ideType: "cli",
};
const payload = {
distinctId: this.uniqueId,
event,
properties: augmentedProperties,
sendFeatureFlags: true,
};
client?.capture(payload);
} catch (e) {
console.error(`Failed to capture event: ${e}`);
}
}
async shutdown() {
const client = await this.getClient();
client?.shutdown();
}
}
export const posthogService = new PosthogService();

View file

@ -17,6 +17,7 @@ import { formatError } from "../../util/formatError.js";
import logger from "../../util/logger.js";
import { DisplayMessage } from "../types.js";
import { posthogService } from "../../telemetry/posthogService.js";
interface UseChatProps {
assistant?: AssistantUnrolled;
@ -257,18 +258,27 @@ export function useChat({
const handleUserMessage = async (message: string) => {
// Special handling for /org command in TUI
if (message.trim() === "/org") {
posthogService.capture("useSlashCommand", {
name: "org",
});
onShowOrgSelector();
return;
}
// Special handling for /config command in TUI
if (message.trim() === "/config") {
posthogService.capture("useSlashCommand", {
name: "config",
});
onShowConfigSelector();
return;
}
// Handle /exit command in remote mode
if (isRemoteMode && remoteUrl && message.trim() === "/exit") {
posthogService.capture("useSlashCommand", {
name: "exit",
});
try {
// Send POST request to /exit endpoint
const response = await fetch(`${remoteUrl}/exit`, {
@ -379,6 +389,10 @@ export function useChat({
// Track user prompt
telemetryService.logUserPrompt(message.length, message);
posthogService.capture("chat", {
model: model?.name,
provider: model?.provider,
});
// Add user message to history and display
let messageContent = message;