spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / mods / telemetry / hooks / context / identity / identity-of.ts
54 lines · 1.7 KBRaw
1import type { ConfigLocation } from '../../config-location'
2import Entries from '../../entries'
3import { globalConfigPath } from './global-config-path'
4import type { Identity } from './identity'
5import { plausibleUuid } from './plausible-uuid'
6
7/**
8 * Reads the identity the CLI's global config records: the device id it
9 * minted for this install, and the account it is signed in to.
10 *
11 * A config that is missing, unreadable or not JSON yields an identity with
12 * nothing in it; the address is kept on an internal build alone.
13 *
14 * @param read reads a file as text
15 * @param location where the config is
16 * @param isInternal whether this is an internal build
17 * @returns the identity, each field undefined unless the config has it
18 */
19export async function identityOf(
20 read: (path: string) => Promise<string>,
21 location: ConfigLocation,
22 isInternal: boolean,
23): Promise<Identity> {
24 const path = globalConfigPath(location)
25
26 let config: unknown
27
28 try {
29 config = path === undefined ? undefined : JSON.parse(await read(path))
30 } catch {
31 config = undefined
32 }
33
34 const record = Entries.isRecord(config) ? config : {}
35
36 const account = Entries.isRecord(record.oauthAccount)
37 ? record.oauthAccount
38 : {}
39
40 const deviceId = record.userID
41 const email = account.emailAddress
42
43 const isDeviceId =
44 typeof deviceId === 'string' && /^[A-Za-z0-9_-]{8,128}$/.test(deviceId)
45
46 const isEmailKept = isInternal && typeof email === 'string' && email !== ''
47
48 return {
49 deviceId: isDeviceId ? deviceId : undefined,
50 accountUuid: plausibleUuid(account.accountUuid),
51 organizationUuid: plausibleUuid(account.organizationUuid),
52 email: isEmailKept ? email : undefined,
53 }
54}