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

FeatureUsage.ts

93 lines4.3 KB

1import {
2 ORCHESTRATION_V2_WS_METHODS,
3 type OrchestrationV2Command,
4 WS_METHODS,
5} from "@t3tools/contracts";
6​
7export interface FeatureUsage {
8 readonly feature: string;
9 readonly variant?: string;
11​
12type Payload = Readonly<Record<string, unknown>>;
13​
14const literal = (payload: Payload, key: string) =>
15 typeof payload[key] === "string" ? (payload[key] as string) : undefined;
16​
17const use = (feature: string, variant?: string): FeatureUsage =>
18 variant === undefined ? { feature } : { feature, variant };
19​
20/**
21 * User commands sent through `orchestration.dispatchCommand` that represent a
22 * feature choice. Housekeeping commands (visits, reorders, acknowledgements)
23 * and `message.dispatch`, which `client.turn.requested` already counts, are
24 * left out.
25 */
26const COMMAND_FEATURES: Partial<
27 Record<OrchestrationV2Command["type"], (command: Payload) => FeatureUsage>
28> = {
29 "checkpoint.rollback": (command) => ({
30 feature: "checkpoint.rollback",
31 variant: command.restoreFiles === false ? "conversation_only" : "with_files",
32 }),
33 "thread.fork": () => ({ feature: "thread.fork" }),
34 "thread.merge_back": () => ({ feature: "thread.merge_back" }),
35 "provider.switch": () => ({ feature: "provider.switch" }),
36 "thread.archive": () => ({ feature: "thread.archive" }),
37 "thread.snooze": () => ({ feature: "thread.snooze" }),
38 "thread.pin": () => ({ feature: "thread.pin" }),
39 "thread.settle": () => ({ feature: "thread.settle" }),
40 "thread.pull-request.link": () => ({ feature: "thread.pull_request.link" }),
41 "thread.pull-request.watch": () => ({ feature: "thread.pull_request.watch" }),
42 "thread.runtime-mode.set": (command) =>
43 use("thread.runtime_mode.set", literal(command, "runtimeMode")),
44 "thread.interaction-mode.set": (command) =>
45 use("thread.interaction_mode.set", literal(command, "interactionMode")),
46 "run.interrupt": () => ({ feature: "run.interrupt" }),
47 "queued-message.promote-to-steer": () => ({ feature: "queue.steer" }),
48 "queued-run.edit": () => ({ feature: "queue.edit" }),
49};
50​
51/**
52 * RPCs that represent a user starting a feature. Variants come only from
53 * closed enums in the contracts, so no path, id, or free text can reach analytics.
54 */
55const METHOD_FEATURES: Readonly<Record<string, (payload: Payload) => FeatureUsage>> = {
56 [ORCHESTRATION_V2_WS_METHODS.launchThread]: (payload) => {
57 const strategy = payload.workspaceStrategy;
58 return use(
59 "thread.launch",
60 typeof strategy === "object" && strategy !== null
61 ? literal(strategy as Payload, "type")
62 : undefined,
63 );
64 },
65 [WS_METHODS.terminalOpen]: () => ({ feature: "terminal.open" }),
66 [WS_METHODS.previewOpen]: () => ({ feature: "preview.open" }),
67 [WS_METHODS.deviceOpen]: () => ({ feature: "device.open" }),
68 [WS_METHODS.shellOpenInEditor]: (payload) => use("editor.open", literal(payload, "editor")),
69 [WS_METHODS.vcsCreateWorktree]: () => ({ feature: "worktree.create" }),
70 [WS_METHODS.gitRunStackedAction]: (payload) =>
71 use("git.stacked_action", literal(payload, "action")),
72 [WS_METHODS.pullRequestsRunAction]: (payload) =>
73 use("pull_request.action", literal(payload, "action")),
74 [WS_METHODS.pullRequestsSubmitReview]: () => ({ feature: "pull_request.review" }),
75 [WS_METHODS.pullRequestsComment]: () => ({ feature: "pull_request.comment" }),
76 [WS_METHODS.scheduledTasksUpsert]: () => ({ feature: "scheduled_task.save" }),
77 [WS_METHODS.scheduledTasksRunNow]: () => ({ feature: "scheduled_task.run_now" }),
78 [WS_METHODS.agentSessionsImport]: () => ({ feature: "agent_session.import" }),
79 [WS_METHODS.projectCloneStart]: () => ({ feature: "project.clone" }),
80 [WS_METHODS.sourceControlPublishRepository]: () => ({ feature: "repository.publish" }),
81 [WS_METHODS.serverUpsertKeybinding]: () => ({ feature: "keybinding.customize" }),
82};
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 const input = payload as Payload;
88 if (method === ORCHESTRATION_V2_WS_METHODS.dispatchCommand) {
89 const type = literal(input, "type") as OrchestrationV2Command["type"] | undefined;
90 return type === undefined ? undefined : COMMAND_FEATURES[type]?.(input);
91 }
92 return METHOD_FEATURES[method]?.(input);