/ migrations / resetAutoModeOptInForDefaultOffer.ts
resetAutoModeOptInForDefaultOffer.ts
 1  import { feature } from 'bun:bundle'
 2  import { logEvent } from 'src/services/analytics/index.js'
 3  import { getGlobalConfig, saveGlobalConfig } from '../utils/config.js'
 4  import { logError } from '../utils/log.js'
 5  import { getAutoModeEnabledState } from '../utils/permissions/permissionSetup.js'
 6  import {
 7    getSettingsForSource,
 8    updateSettingsForSource,
 9  } from '../utils/settings/settings.js'
10  
11  /**
12   * One-shot migration: clear skipAutoPermissionPrompt for users who accepted
13   * the old 2-option AutoModeOptInDialog but don't have auto as their default.
14   * Re-surfaces the dialog so they see the new "make it my default mode" option.
15   * Guard lives in GlobalConfig (~/.claude.json), not settings.json, so it
16   * survives settings resets and doesn't re-arm itself.
17   *
18   * Only runs when tengu_auto_mode_config.enabled === 'enabled'. For 'opt-in'
19   * users, clearing skipAutoPermissionPrompt would remove auto from the carousel
20   * (permissionSetup.ts:988) — the dialog would become unreachable and the
21   * migration would defeat itself. In practice the ~40 target ants are all
22   * 'enabled' (they reached the old dialog via bare Shift+Tab, which requires
23   * 'enabled'), but the guard makes it safe regardless.
24   */
25  export function resetAutoModeOptInForDefaultOffer(): void {
26    if (feature('TRANSCRIPT_CLASSIFIER')) {
27      const config = getGlobalConfig()
28      if (config.hasResetAutoModeOptInForDefaultOffer) return
29      if (getAutoModeEnabledState() !== 'enabled') return
30  
31      try {
32        const user = getSettingsForSource('userSettings')
33        if (
34          user?.skipAutoPermissionPrompt &&
35          user?.permissions?.defaultMode !== 'auto'
36        ) {
37          updateSettingsForSource('userSettings', {
38            skipAutoPermissionPrompt: undefined,
39          })
40          logEvent('tengu_migrate_reset_auto_opt_in_for_default_offer', {})
41        }
42  
43        saveGlobalConfig(c => {
44          if (c.hasResetAutoModeOptInForDefaultOffer) return c
45          return { ...c, hasResetAutoModeOptInForDefaultOffer: true }
46        })
47      } catch (error) {
48        logError(new Error(`Failed to reset auto mode opt-in: ${error}`))
49      }
50    }
51  }