Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Identify.ts

104 lines3.1 KB

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