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