Skip to content

Commit 2a305f1

Browse files
feat(telemetry): add first-run consent dialog with tier-aware UX
- Add DialogTelemetryConsent component: free-tier shows informational 'I Understand' (forced opt-in); paid-tier shows 'Enable'/'No Thanks' two-button confirm/decline with keyboard navigation - Persist consent choice to KV store (telemetry_consent_shown, telemetry_enabled) and propagate to Telemetry.setConsent() - Wire dialog into app.tsx via createEffect that fires before the connect-provider dialog, checking KV for prior consent
1 parent ceaf8ff commit 2a305f1

2 files changed

Lines changed: 167 additions & 0 deletions

File tree

packages/opencode/src/cli/cmd/tui/app.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import { ArgsProvider, useArgs, type Args } from "./context/args"
3636
import open from "open"
3737
import { writeHeapSnapshot } from "v8"
3838
import { PromptRefProvider, usePromptRef } from "./context/prompt"
39+
import { DialogTelemetryConsent, KV_TELEMETRY_CONSENT_SHOWN, KV_TELEMETRY_ENABLED } from "@tui/component/dialog-telemetry-consent"
40+
import { Telemetry } from "@/telemetry"
3941

4042
async function getTerminalBackgroundColor(): Promise<"dark" | "light"> {
4143
// can't set raw mode if not a TTY
@@ -272,6 +274,33 @@ function App() {
272274
}
273275
})
274276

277+
// --- First-run telemetry consent dialog ---
278+
// Fires once when sync is complete and user hasn't seen the consent dialog yet.
279+
// Must fire *before* the provider connect dialog so consent is captured first.
280+
createEffect(
281+
on(
282+
() => sync.status === "complete" && kv.get(KV_TELEMETRY_CONSENT_SHOWN) === undefined,
283+
(needsConsent, prev) => {
284+
if (!needsConsent || prev) return
285+
286+
// Load any existing consent value into the telemetry module
287+
const existing = kv.get(KV_TELEMETRY_ENABLED)
288+
if (existing !== undefined) {
289+
Telemetry.loadConsent(existing as boolean)
290+
// Already consented in a previous session, skip dialog
291+
kv.set(KV_TELEMETRY_CONSENT_SHOWN, true)
292+
return
293+
}
294+
295+
// Show the consent dialog. Free tier = informational only.
296+
// TODO: Determine actual tier from auth/consent service. Default to "free".
297+
dialog.replace(() => (
298+
<DialogTelemetryConsent tier="free" onResult={() => {}} />
299+
))
300+
},
301+
),
302+
)
303+
275304
createEffect(
276305
on(
277306
() => sync.status === "complete" && sync.data.provider.length === 0,
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
* First-run telemetry consent dialog.
3+
*
4+
* - Free-tier users: informational only — telemetry is required, single "I Understand" button.
5+
* - Paid/unknown users: genuine opt-in with "Enable" / "No Thanks" buttons.
6+
*
7+
* The consent choice is persisted to the KV store and loaded into the
8+
* Telemetry consent module on startup.
9+
*/
10+
11+
import { TextAttributes } from "@opentui/core"
12+
import { useTheme } from "@tui/context/theme"
13+
import { useDialog, type DialogContext } from "@tui/ui/dialog"
14+
import { useKV } from "@tui/context/kv"
15+
import { createStore } from "solid-js/store"
16+
import { For, Show } from "solid-js"
17+
import { useKeyboard } from "@opentui/solid"
18+
import { Telemetry } from "@/telemetry"
19+
20+
/** KV keys used by the consent dialog */
21+
export const KV_TELEMETRY_CONSENT_SHOWN = "telemetry_consent_shown"
22+
export const KV_TELEMETRY_ENABLED = "telemetry_enabled"
23+
24+
type Tier = "free" | "paid"
25+
26+
export type DialogTelemetryConsentProps = {
27+
tier: Tier
28+
onResult: (accepted: boolean) => void
29+
}
30+
31+
export function DialogTelemetryConsent(props: DialogTelemetryConsentProps) {
32+
const dialog = useDialog()
33+
const { theme } = useTheme()
34+
const kv = useKV()
35+
36+
const isFree = () => props.tier === "free"
37+
38+
const title = () => isFree() ? "Usage Data Collection" : "Enable Usage Telemetry?"
39+
40+
const message = () =>
41+
isFree()
42+
? "CodeQ collects anonymous usage telemetry to improve the product.\n" +
43+
"This includes session metrics (token counts, tool usage, latency)\n" +
44+
"and is required for free-tier accounts. No source code or secrets\n" +
45+
"are collected. You can review our privacy policy at qbraid.com/privacy."
46+
: "CodeQ can collect anonymous usage telemetry to help us improve\n" +
47+
"the product. This includes session metrics like token counts,\n" +
48+
"tool usage, and latency. No source code or secrets are collected.\n\n" +
49+
"You can change this anytime in your config:\n" +
50+
' qbraid.telemetry.enabled: true | false'
51+
52+
// Free tier: single button. Paid tier: two-button confirm/decline.
53+
const buttons = () =>
54+
isFree() ? ["understand"] as const : ["decline", "enable"] as const
55+
56+
const [store, setStore] = createStore({
57+
active: isFree() ? "understand" : "enable",
58+
})
59+
60+
const labels: Record<string, string> = {
61+
understand: "I Understand",
62+
enable: "Enable",
63+
decline: "No Thanks",
64+
}
65+
66+
const handleSelect = (key: string) => {
67+
const accepted = key === "understand" || key === "enable"
68+
kv.set(KV_TELEMETRY_CONSENT_SHOWN, true)
69+
kv.set(KV_TELEMETRY_ENABLED, accepted)
70+
Telemetry.setConsent(accepted)
71+
props.onResult(accepted)
72+
dialog.clear()
73+
}
74+
75+
useKeyboard((evt) => {
76+
if (evt.name === "return") {
77+
handleSelect(store.active)
78+
return
79+
}
80+
81+
if (!isFree() && (evt.name === "left" || evt.name === "right")) {
82+
setStore("active", store.active === "enable" ? "decline" : "enable")
83+
}
84+
})
85+
86+
return (
87+
<box paddingLeft={2} paddingRight={2} gap={1}>
88+
<box flexDirection="row" justifyContent="space-between">
89+
<text attributes={TextAttributes.BOLD} fg={theme.text}>
90+
{title()}
91+
</text>
92+
<Show when={!isFree()}>
93+
<text fg={theme.textMuted}>esc</text>
94+
</Show>
95+
</box>
96+
<box paddingBottom={1}>
97+
<text fg={theme.textMuted}>{message()}</text>
98+
</box>
99+
<box flexDirection="row" justifyContent="flex-end" paddingBottom={1} gap={1}>
100+
<For each={[...buttons()]}>
101+
{(key) => (
102+
<box
103+
paddingLeft={2}
104+
paddingRight={2}
105+
backgroundColor={key === store.active ? theme.primary : undefined}
106+
onMouseUp={() => handleSelect(key)}
107+
>
108+
<text fg={key === store.active ? theme.selectedListItemText : theme.textMuted}>
109+
{labels[key]}
110+
</text>
111+
</box>
112+
)}
113+
</For>
114+
</box>
115+
</box>
116+
)
117+
}
118+
119+
/**
120+
* Show the consent dialog and return a promise that resolves with the user's choice.
121+
*/
122+
DialogTelemetryConsent.show = (
123+
dialog: DialogContext,
124+
tier: Tier,
125+
): Promise<boolean> => {
126+
return new Promise<boolean>((resolve) => {
127+
dialog.replace(
128+
() => (
129+
<DialogTelemetryConsent
130+
tier={tier}
131+
onResult={(accepted) => resolve(accepted)}
132+
/>
133+
),
134+
// If user presses Esc on paid tier, treat as decline
135+
() => resolve(false),
136+
)
137+
})
138+
}

0 commit comments

Comments
 (0)