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: 35 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@types/express": "^5.0.0",
"@types/node": "^18.19.54",
"@types/prompts": "^2.4.9",
"@types/uuid": "^10.0.0",
"@types/yargs": "^17.0.33",
"nodemon": "^3.1.7",
"pkg": "^5.8.1",
Expand All @@ -73,6 +74,7 @@
"vitest": "^2.1.1"
},
"dependencies": {
"axios": "^1.7.7",
"dependency-tree": "^11.0.1",
"express": "^4.21.1",
"http-proxy-middleware": "^3.0.3",
Expand All @@ -81,6 +83,7 @@
"tree-sitter": "^0.21.1",
"tree-sitter-javascript": "^0.23.0",
"tree-sitter-typescript": "^0.23.0",
"uuid": "^10.0.0",
"yargs": "^17.7.2",
"zod": "^3.23.8"
}
Expand Down
40 changes: 23 additions & 17 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { getApi } from "./api";
import annotateOpenAICommandHandler from "./commands/annotate";
import splitCommandHandler from "./commands/split";
import { findAvailablePort, openInBrowser } from "./helper/server";
import initCommandHandler from "./commands/init";
import splitCommandHandler from "./commands/split";
import { getConfigFromWorkDir, getOpenaiApiKeyFromConfig } from "./config";
import { findAvailablePort, openInBrowser } from "./helper/server";
import { TelemetryEvents, trackEvent } from "./telemetry";

// remove all warning.
// We need this because of some depreciation warning we have with 3rd party libraries
Expand All @@ -17,8 +18,11 @@ if (process.env.NODE_ENV !== "development") {
process.removeAllListeners("warning");
}

trackEvent(TelemetryEvents.APP_START, {
message: "Napi started with Telemetry enabled",
});

yargs(hideBin(process.argv))
// Global options, used for all commands
.options({
workdir: {
type: "string",
Expand All @@ -27,30 +31,32 @@ yargs(hideBin(process.argv))
description: "working directory",
},
})
// Init command
.command(
"init",
"initialize a nanoapi project",
(yargs) => yargs,
(argv) => {
trackEvent(TelemetryEvents.INIT_COMMAND, { message: "Init command" });
initCommandHandler(argv.workdir);
},
)
// Annotate openai command
.command(
"annotate openai [entrypoint]",
"Annotate a program, needed for splitting",
(yargs) => {
return yargs.options({
(yargs) =>
yargs.options({
apiKey: {
type: "string",
default: "",
alias: "k",
description: "OpenAI API key",
},
});
},
}),
(argv) => {
trackEvent(TelemetryEvents.ANNOTATE_COMMAND, {
message: "Annotate command",
});
const napiConfig = getConfigFromWorkDir(argv.workdir);

if (!napiConfig) {
Expand Down Expand Up @@ -79,10 +85,11 @@ yargs(hideBin(process.argv))
.command(
"split [entrypoint]",
"Split a program into multiple ones",
(yargs) => {
return yargs;
},
(yargs) => yargs,
(argv) => {
trackEvent(TelemetryEvents.SPLIT_COMMAND, {
message: "Split command",
});
const napiConfig = getConfigFromWorkDir(argv.workdir);

if (!napiConfig) {
Expand All @@ -97,10 +104,12 @@ yargs(hideBin(process.argv))
.command(
"ui",
"open the NanoAPI UI",
(yargs) => {
return yargs;
},
(yargs) => yargs,
async (argv) => {
trackEvent(TelemetryEvents.UI_OPEN, {
message: "UI command",
});

const napiConfig = getConfigFromWorkDir(argv.workdir);

if (!napiConfig) {
Expand All @@ -109,15 +118,12 @@ yargs(hideBin(process.argv))
}

const app = express();

const api = getApi(napiConfig);

app.use(api);

if (process.env.NODE_ENV === "development") {
const targetServiceUrl =
process.env.APP_SERVICE_URL || "http://localhost:3001";

app.use(
"/",
createProxyMiddleware({
Expand Down
81 changes: 81 additions & 0 deletions packages/cli/src/telemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import axios from "axios";
import { EventEmitter } from "events";
import { existsSync, readFileSync, writeFileSync } from "fs";
import { join } from "path";
import { v4 as uuidv4 } from "uuid";

export enum TelemetryEvents {
APP_START = "app_start",
INIT_COMMAND = "init_command",
ANNOTATE_COMMAND = "annotate_command",
SPLIT_COMMAND = "split_command",
UI_OPEN = "ui_open",
SERVER_STARTED = "server_started",
API_REQUEST = "api_request",
ERROR_OCCURRED = "error_occurred",
USER_ACTION = "user_action",
}

export interface TelemetryEvent {
sessionId: string;
eventId: TelemetryEvents;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data: any;
timestamp: string;
}

const telemetry = new EventEmitter();
const TELEMETRY_ENDPOINT =
process.env.TELEMETRY_ENDPOINT ||
"https://napi-watchdog-api-gateway-33ge7a49.nw.gateway.dev/telemetryHandler";
const SESSION_FILE_PATH = join("/tmp", "napi_session_id");

// getSessionId generates a new session ID and cache it in SESSION_FILE_PATH
const getSessionId = (): string => {
if (existsSync(SESSION_FILE_PATH)) {
const fileContent = readFileSync(SESSION_FILE_PATH, "utf-8");
const [storedDate, sessionId] = fileContent.split(":");
const today = new Date().toISOString().slice(0, 10);

if (storedDate === today) {
return sessionId;
}
}

const newSessionId = uuidv4();
const today = new Date().toISOString().slice(0, 10);
writeFileSync(SESSION_FILE_PATH, `${today}:${newSessionId}`);
return newSessionId;
};

const SESSION_ID = getSessionId();

telemetry.on("event", (data) => {
sendTelemetryData(data);
});

const sendTelemetryData = async (data: TelemetryEvent) => {
try {
await axios.post(TELEMETRY_ENDPOINT, data, {
headers: {
"Content-Type": "application/json",
"User-Agent": "napi",
},
timeout: 5000,
});
} catch (error) {
console.error(`Failed to send telemetry data: ${error}`);
}
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const trackEvent = (eventId: TelemetryEvents, eventData: any) => {
const telemetryPayload: TelemetryEvent = {
sessionId: SESSION_ID,
eventId,
data: eventData,
timestamp: new Date().toISOString(),
};

telemetry.emit("event", telemetryPayload);
};
Loading