Files
cline-multi-mirror/src/services/logging/distinctId.ts
T
Ara a85acf8898 feat(logging): replace HostProvider with node-machine-id for machine identification (#6680)
* feat(logging): replace HostProvider with node-machine-id for machine identification

Replace custom HostProvider.env.getMachineId() implementation with the
node-machine-id npm package for retrieving machine identifiers. This
simplifies the codebase by using a well-maintained library instead of
custom host provider logic.

Changes:
- Add node-machine-id dependency (^1.1.12)
- Remove dotenv dev dependency (no longer needed)
- Update distinctId service to use node-machine-id directly
- Refactor tests to stub node-machine-id instead of HostProvider
- Remove HostProvider mock utilities from tests

This change improves maintainability and reduces custom code while
maintaining the same functionality for generating stable machine IDs.

* Removing dead code
2025-10-07 13:51:42 -07:00

75 lines
2.3 KiB
TypeScript

import { machineId } from "node-machine-id"
import { v4 as uuidv4 } from "uuid"
import { ExtensionContext } from "vscode"
/*
* Unique identifier for the current installation.
*/
let _distinctId: string = ""
/**
* Some environments don't return a value for the machine ID. For these situations we generated
* a unique ID and store it locally.
*/
export const _GENERATED_MACHINE_ID_KEY = "cline.generatedMachineId"
export async function initializeDistinctId(context: ExtensionContext, uuid: () => string = uuidv4) {
// Try to read the ID from storage.
let distinctId = context.globalState.get<string>(_GENERATED_MACHINE_ID_KEY)
if (!distinctId) {
// Get the ID from the host environment.
distinctId = await getMachineId()
}
if (!distinctId) {
// Fallback to generating a unique ID and keeping in global storage.
console.warn("No machine ID found for telemetry, generating UUID")
// Add a prefix to the UUID so we can see in the telemetry how many clients are don't have a machine ID.
distinctId = "cl-" + uuid()
context.globalState.update(_GENERATED_MACHINE_ID_KEY, distinctId)
}
setDistinctId(distinctId)
console.log("Telemetry distinct ID initialized:", distinctId)
}
/*
* Get machine ID using node-machine-id package
* This works across all platforms (VS Code, JetBrains, CLI)
*/
async function getMachineId(): Promise<string | undefined> {
try {
// Get the machine ID using node-machine-id package
// This provides a deterministic ID across different operating systems
const id = await machineId()
return id
} catch (error) {
console.log("Failed to get machine ID from node-machine-id", error)
return undefined
}
}
/*
* Set the distinct ID for logging and telemetry.
* This is updated to Cline User ID when authenticated.
*/
export function setDistinctId(newId: string) {
if (_distinctId && _distinctId !== newId) {
console.log(`Changing telemetry ID from ${_distinctId} to ${newId}.`)
}
_distinctId = newId
}
/*
* Unique identifier for the current user
* If authenticated, this will be the Cline User ID.
* Else, this will be the machine ID, or the anonymous ID as a fallback.
*/
export function getDistinctId() {
if (!_distinctId) {
console.error("Telemetry ID is not initialized. Call initializeDistinctId() first.")
}
return _distinctId
}