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

FeatureUsage.ts

88 lines4.4 KB

1import { type OrchestrationV2Command, WS_METHODS } from "@t3tools/contracts";
2​
3export interface FeatureUsage {
4 readonly feature: string;
5 readonly variant?: string;
6}
7​
8type Payload = Readonly<Record<string, unknown>>;
9​
10const literal = (payload: Payload, key: string) =>
11 typeof payload[key] === "string" ? (payload[key] as string) : undefined;
12​
13const use = (feature: string, variant?: string): FeatureUsage =>
14 variant === undefined ? { feature } : { feature, variant };
15​
16/**
17 * User commands sent through `orchestration.dispatchCommand` that represent a
18 * feature choice. Housekeeping commands (visits, reorders, acknowledgements)
19 * and `message.dispatch`, which `client.turn.requested` already counts, are
20 * left out. The dispatch handler records these, not the RPC middleware, so a
21 * retried command id that changed nothing is not counted again.
22 */
23const COMMAND_FEATURES: {
24 readonly [Type in OrchestrationV2Command["type"]]?: (
25 command: Extract<OrchestrationV2Command, { readonly type: Type }>,
26 ) => FeatureUsage;
27} = {
28 "checkpoint.rollback": (command) => ({
29 feature: "checkpoint.rollback",
30 variant: command.restoreFiles === false ? "conversation_only" : "with_files",
31 }),
32 "thread.fork": () => ({ feature: "thread.fork" }),
33 "thread.merge_back": () => ({ feature: "thread.merge_back" }),
34 "provider.switch": () => ({ feature: "provider.switch" }),
35 "thread.archive": () => ({ feature: "thread.archive" }),
36 "thread.snooze": () => ({ feature: "thread.snooze" }),
37 "thread.pin": () => ({ feature: "thread.pin" }),
38 "thread.settle": () => ({ feature: "thread.settle" }),
39 "thread.pull-request.link": () => ({ feature: "thread.pull_request.link" }),
40 "thread.pull-request.watch": (command) =>
41 use("thread.pull_request.watch", command.watching ? "on" : "off"),
42 "thread.runtime-mode.set": (command) => use("thread.runtime_mode.set", command.runtimeMode),
43 "thread.interaction-mode.set": (command) =>
44 use("thread.interaction_mode.set", command.interactionMode),
45 "run.interrupt": () => ({ feature: "run.interrupt" }),
46 "queued-message.promote-to-steer": () => ({ feature: "queue.steer" }),
47 "queued-run.edit": () => ({ feature: "queue.edit" }),
48};
49​
50/**
51 * RPCs that represent a user starting a feature. Variants come only from
52 * closed enums in the contracts, so no path, id, or free text can reach analytics.
53 * Thread launches are left out because `client.thread.started` counts them.
54 */
55const METHOD_FEATURES: Readonly<Record<string, (payload: Payload) => FeatureUsage>> = {
56 [WS_METHODS.terminalOpen]: () => ({ feature: "terminal.open" }),
57 [WS_METHODS.previewOpen]: () => ({ feature: "preview.open" }),
58 [WS_METHODS.deviceOpen]: () => ({ feature: "device.open" }),
59 [WS_METHODS.shellOpenInEditor]: (payload) => use("editor.open", literal(payload, "editor")),
60 [WS_METHODS.vcsCreateWorktree]: () => ({ feature: "worktree.create" }),
61 [WS_METHODS.gitRunStackedAction]: (payload) =>
62 use("git.stacked_action", literal(payload, "action")),
63 [WS_METHODS.pullRequestsRunAction]: (payload) =>
64 use("pull_request.action", literal(payload, "action")),
65 [WS_METHODS.pullRequestsSubmitReview]: () => ({ feature: "pull_request.review" }),
66 [WS_METHODS.pullRequestsComment]: () => ({ feature: "pull_request.comment" }),
67 [WS_METHODS.scheduledTasksUpsert]: () => ({ feature: "scheduled_task.save" }),
68 [WS_METHODS.scheduledTasksRunNow]: () => ({ feature: "scheduled_task.run_now" }),
69 [WS_METHODS.agentSessionsImport]: () => ({ feature: "agent_session.import" }),
70 [WS_METHODS.projectCloneStart]: () => ({ feature: "project.clone" }),
71 [WS_METHODS.sourceControlPublishRepository]: () => ({ feature: "repository.publish" }),
72 [WS_METHODS.serverUpsertKeybinding]: () => ({ feature: "keybinding.customize" }),
73};
74​
75/** The feature a dispatched command uses, or undefined when it is not a tracked feature. */
76export function commandFeatureUsage(command: OrchestrationV2Command): FeatureUsage | undefined {
77 // TypeScript cannot pair a union's tag with its own entry, so widen once here.
78 const mapping = COMMAND_FEATURES[command.type] as
79 | ((command: OrchestrationV2Command) => FeatureUsage)
80 | undefined;
81 return mapping?.(command);
83​
84/** The feature a WebSocket request uses, or undefined when it is not a tracked feature. */
85export function featureUsage(method: string, payload: unknown): FeatureUsage | undefined {
86 if (typeof payload !== "object" || payload === null) return undefined;
87 return METHOD_FEATURES[method]?.(payload as Payload);