Files
oh-my-opencode/packages/omo-codex/plugin/components/telemetry/src/codex-hook.ts
T
YeonGyu-Kim bd867019f9 feat(omo-codex): wire Codex SessionStart telemetry for DAU/WAU/MAU
Adds a new Codex plugin component `telemetry` that emits a single
omo_codex_daily_active event (reason: session_start) from every Codex
SessionStart hook, with the same UTC-day deduplication, hashed
installation identifier, and four-flag opt-out as the install-time
event. Previously omo-codex telemetry only fired on install_completed,
so DAU/WAU/MAU under-reported real Codex usage.

- New plugin component packages/omo-codex/plugin/components/telemetry/
  mirrors the rules/comment-checker/lsp pattern: own src/, tsc build,
  vitest tests, package.json (posthog-node dep), hooks/hooks.json.
- src/codex-hook.ts wraps createPluginPostHog().trackActive(..., "session_start")
  with safeShutdown so Codex session startup never blocks on telemetry.
- Plugin root hooks.json + workspaces register the new component
  alongside rules and ultrawork on SessionStart.
- Aggregate test expectations updated to include the telemetry directory.
- cross-package-equivalence.test.ts pins product-identity constants and
  shouldDisablePostHog behavior to stay byte-equivalent between the CLI
  installer (src/telemetry/) and the plugin runtime
  (plugin/components/telemetry/src/), so the two PostHog sources never
  drift on event name, distinct_id base, dedup file path, or opt-out
  flags.
- PostHogActivityReason union in the CLI-side posthog.ts gains
  "session_start" so future CLI paths can emit the same reason without
  a type break.
2026-05-28 13:58:11 +09:00

50 lines
1.1 KiB
TypeScript

import {
type PostHogActivityReason,
type PostHogClient,
createPluginPostHog,
getPostHogDistinctId,
} from "./posthog.js";
export type CodexSessionStartInput = {
session_id: string;
transcript_path: string | null;
cwd: string;
hook_event_name: "SessionStart";
model: string;
permission_mode: string;
source: "startup" | "resume" | "clear";
};
export type CodexTelemetryHookOptions = {
createClient?: () => PostHogClient;
getDistinctId?: () => string;
};
const SESSION_START_REASON: PostHogActivityReason = "session_start";
export async function runSessionStartHook(
_input: CodexSessionStartInput,
options: CodexTelemetryHookOptions = {},
): Promise<string> {
const createClient = options.createClient ?? createPluginPostHog;
const getDistinctId = options.getDistinctId ?? getPostHogDistinctId;
const client = createClient();
try {
client.trackActive(getDistinctId(), SESSION_START_REASON);
} catch {
await safeShutdown(client);
return "";
}
await safeShutdown(client);
return "";
}
async function safeShutdown(client: PostHogClient): Promise<void> {
try {
await client.shutdown();
} catch {
return;
}
}