Open History to see the last commit
1import { Effect, FileSystem, Path, Random, Schema } from "effect";
2import * as Crypto from "node:crypto";
3import { homedir } from "node:os";
4import { ServerConfig } from "../config";
5
6const CodexAuthJsonSchema = Schema.Struct({
7 tokens: Schema.Struct({
8 account_id: Schema.String,
9 }),
10});
11
12const ClaudeJsonSchema = Schema.Struct({
13 userID: Schema.String,
14});
15
16class IdentifyUserError extends Schema.TaggedErrorClass<IdentifyUserError>()("IdentifyUserError", {
17 message: Schema.String,
18 cause: Schema.optional(Schema.Defect),
19}) {}
20
21const hash = (value: string) =>
22 Effect.try({
23 try: () => Crypto.createHash("sha256").update(value).digest("hex"),
24 catch: (error) =>
25 new IdentifyUserError({
26 message: "Failed to hash identifier",
27 cause: error,
28 }),
29 });
30
31const getCodexAccountId = Effect.gen(function* () {
32 const fileSystem = yield* FileSystem.FileSystem;
33 const path = yield* Path.Path;
34
35 const authJsonPath = path.join(homedir(), ".codex", "auth.json");
36 const authJson = yield* Effect.flatMap(
37 fileSystem.readFileString(authJsonPath),
38 Schema.decodeEffect(Schema.fromJsonString(CodexAuthJsonSchema)),
39 );
40
41 return authJson.tokens.account_id;
42});
43
44const getClaudeUserId = Effect.gen(function* () {
45 const fileSystem = yield* FileSystem.FileSystem;
46 const path = yield* Path.Path;
47
48 const claudeJsonPath = path.join(homedir(), ".claude.json");
49 const claudeJson = yield* Effect.flatMap(
50 fileSystem.readFileString(claudeJsonPath),
51 Schema.decodeEffect(Schema.fromJsonString(ClaudeJsonSchema)),
52 );
53
54 return claudeJson.userID;
55});
56
57const upsertAnonymousId = Effect.gen(function* () {
58 const fileSystem = yield* FileSystem.FileSystem;
59 const { anonymousIdPath } = yield* ServerConfig;
60
61 const anonymousId = yield* fileSystem.readFileString(anonymousIdPath).pipe(
62 Effect.catch(() =>
63 Effect.gen(function* () {
64 const randomId = yield* Random.nextUUIDv4;
65 yield* fileSystem.writeFileString(anonymousIdPath, randomId);
66 return randomId;
67 }),
68 ),
69 );
70
71 return anonymousId;
72});
73
74/**
75 * getTelemetryIdentifier - Users are "identified" by finding the first match of the following, then hashing the value.
76 * 1. ~/.codex/auth.json tokens.account_id
77 * 2. ~/.claude.json userID
78 * 3. ~/.t3/telemetry/anonymous-id
79 */
80export const getTelemetryIdentifier = Effect.gen(function* () {
81 const codexAccountId = yield* Effect.result(getCodexAccountId);
82 if (codexAccountId._tag === "Success") {
83 return yield* hash(codexAccountId.success);
84 }
85
86 const claudeUserId = yield* Effect.result(getClaudeUserId);
87 if (claudeUserId._tag === "Success") {
88 return yield* hash(claudeUserId.success);
89 }
90
91 const anonymousId = yield* Effect.result(upsertAnonymousId);
92 if (anonymousId._tag === "Success") {
93 return yield* hash(anonymousId.success);
94 }
95
96 return null;
97}).pipe(
98 Effect.tapError((error) => Effect.logWarning("Failed to get identifier", { cause: error })),
99 Effect.orElseSucceed(() => null),
100);