File size: 6,737 Bytes
bfdb3c0
 
 
 
 
 
4e7af9b
bfdb3c0
e40073c
bfdb3c0
 
4e7af9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfdb3c0
 
 
 
4e7af9b
bfdb3c0
 
 
 
 
 
 
 
4e7af9b
 
 
 
 
 
 
 
 
 
bfdb3c0
 
e40073c
4e7af9b
 
 
 
 
 
 
 
 
bfdb3c0
 
 
 
 
e40073c
4e7af9b
 
 
 
 
 
 
 
 
bfdb3c0
 
 
 
 
e40073c
4e7af9b
 
 
 
 
 
 
 
 
bfdb3c0
 
 
 
 
 
e40073c
4e7af9b
 
 
 
 
 
 
 
 
bfdb3c0
 
 
 
 
 
e40073c
4e7af9b
 
 
 
 
 
 
 
 
 
 
 
 
 
bfdb3c0
 
 
 
 
 
e40073c
bfdb3c0
 
 
 
 
 
4e7af9b
 
 
 
 
 
 
 
bfdb3c0
 
 
 
 
 
 
 
 
 
 
 
 
 
e40073c
bfdb3c0
 
 
 
 
 
 
 
 
 
 
4e7af9b
 
 
 
bfdb3c0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
 * Desktop auth store that reads/writes ~/.claude.json directly via Rust Tauri commands.
 * Mirrors TUI's src/utils/auth.ts save* functions.
 *
 * This removes the dependency on cc-haha's /api/cli-auth HTTP endpoint.
 */
import { create } from 'zustand'
import { getTuiConfig, saveTuiConfigPatch, clearConfigCache } from '../api/config'
import { useSettingsStore } from './settingsStore'

type AuthProvider = 'anthropic' | 'openai' | 'openrouter' | 'local' | 'opencode' | 'nvidia'

type CliAuthStore = {
  authProvider: AuthProvider | null
  nvidiaApiKey: string | null
  openRouterApiKey: string | null
  openAiApiKey: string | null
  openCodeApiKey: string | null
  openCodeModelName: string | null
  localBaseUrl: string | null
  localModelName: string | null
  isLoading: boolean
  error: string | null

  fetchAuth: () => Promise<void>
  setAuthProvider: (provider: AuthProvider) => Promise<void>
  saveNvidiaApiKey: (apiKey: string) => Promise<void>
  saveOpenRouterApiKey: (apiKey: string) => Promise<void>
  saveOpenAIApiKey: (apiKey: string) => Promise<void>
  saveOpenCodeApiKey: (apiKey: string, modelName?: string) => Promise<void>
  saveLocalModelConfig: (baseUrl: string, modelName: string) => Promise<void>
  clearAuth: () => Promise<void>
}

export const useCliAuthStore = create<CliAuthStore>((set) => ({
  authProvider: null,
  nvidiaApiKey: null,
  openRouterApiKey: null,
  openAiApiKey: null,
  openCodeApiKey: null,
  openCodeModelName: null,
  localBaseUrl: null,
  localModelName: null,
  isLoading: false,
  error: null,

  fetchAuth: async () => {
    set({ isLoading: true, error: null })
    try {
      const config = await getTuiConfig()
      // Map TUI's authProvider names to our store's names
      // TUI uses 'anthropic' → our 'anthropic' means firstParty
      const authProvider = config.authProvider ?? null
      set({
        authProvider,
        nvidiaApiKey: config.nvidiaApiKey ?? null,
        openRouterApiKey: config.openRouterApiKey ?? null,
        openAiApiKey: (config.openAiApiKey || config.openAiAccessToken) ?? null,
        openCodeApiKey: config.openCodeApiKey ?? null,
        openCodeModelName: config.openCodeModelName ?? null,
        localBaseUrl: config.localBaseUrl ?? null,
        localModelName: config.localModelName ?? null,
        isLoading: false,
      })
    } catch (err) {
      set({ isLoading: false, error: err instanceof Error ? err.message : String(err) })
    }
  },

  setAuthProvider: async (provider: AuthProvider) => {
    set({ isLoading: true, error: null })
    try {
      await saveTuiConfigPatch({ authProvider: provider })
      clearConfigCache()
      await useSettingsStore.getState().syncFromConfig()
      set({ authProvider: provider, isLoading: false })
    } catch (err) {
      set({ isLoading: false, error: err instanceof Error ? err.message : String(err) })
    }
  },

  saveNvidiaApiKey: async (apiKey: string) => {
    set({ isLoading: true, error: null })
    try {
      await saveTuiConfigPatch({
        authProvider: 'nvidia',
        nvidiaApiKey: apiKey,
      })
      clearConfigCache()
      await useSettingsStore.getState().syncFromConfig()
      set({ authProvider: 'nvidia', nvidiaApiKey: apiKey, isLoading: false })
    } catch (err) {
      set({ isLoading: false, error: err instanceof Error ? err.message : String(err) })
    }
  },

  saveOpenRouterApiKey: async (apiKey: string) => {
    set({ isLoading: true, error: null })
    try {
      await saveTuiConfigPatch({
        authProvider: 'openrouter',
        openRouterApiKey: apiKey,
      })
      clearConfigCache()
      await useSettingsStore.getState().syncFromConfig()
      set({ authProvider: 'openrouter', openRouterApiKey: apiKey, isLoading: false })
    } catch (err) {
      set({ isLoading: false, error: err instanceof Error ? err.message : String(err) })
    }
  },

  saveOpenAIApiKey: async (apiKey: string) => {
    set({ isLoading: true, error: null })
    try {
      await saveTuiConfigPatch({
        authProvider: 'openai',
        openAiApiKey: apiKey,
        openAiAccessToken: undefined, // Clear OAuth token when switching to API key
      })
      clearConfigCache()
      await useSettingsStore.getState().syncFromConfig()
      set({ authProvider: 'openai', openAiApiKey: apiKey, isLoading: false })
    } catch (err) {
      set({ isLoading: false, error: err instanceof Error ? err.message : String(err) })
    }
  },

  saveOpenCodeApiKey: async (apiKey: string, modelName?: string) => {
    set({ isLoading: true, error: null })
    try {
      await saveTuiConfigPatch({
        authProvider: 'opencode',
        openCodeApiKey: apiKey || undefined,
        openCodeModelName: modelName || undefined,
      })
      clearConfigCache()
      await useSettingsStore.getState().syncFromConfig()
      set({
        authProvider: 'opencode',
        openCodeApiKey: apiKey,
        ...(modelName ? { openCodeModelName: modelName } : {}),
        isLoading: false,
      })
    } catch (err) {
      set({ isLoading: false, error: err instanceof Error ? err.message : String(err) })
    }
  },

  saveLocalModelConfig: async (baseUrl: string, modelName: string) => {
    set({ isLoading: true, error: null })
    try {
      await saveTuiConfigPatch({
        authProvider: 'local',
        localBaseUrl: baseUrl,
        localModelName: modelName,
      })
      clearConfigCache()
      await useSettingsStore.getState().syncFromConfig()
      set({
        authProvider: 'local',
        localBaseUrl: baseUrl,
        localModelName: modelName,
        isLoading: false,
      })
    } catch (err) {
      set({ isLoading: false, error: err instanceof Error ? err.message : String(err) })
    }
  },

  clearAuth: async () => {
    set({ isLoading: true, error: null })
    try {
      // Clear by writing null values for all auth fields
      await saveTuiConfigPatch({
        authProvider: null,
        primaryApiKey: undefined,
        openAiApiKey: undefined,
        openAiAccessToken: undefined,
        openRouterApiKey: undefined,
        nvidiaApiKey: undefined,
        openCodeApiKey: undefined,
        openCodeModelName: undefined,
        localBaseUrl: undefined,
        localModelName: undefined,
      })
      clearConfigCache()
      await useSettingsStore.getState().syncFromConfig()
      set({
        authProvider: null,
        nvidiaApiKey: null,
        openRouterApiKey: null,
        openAiApiKey: null,
        openCodeApiKey: null,
        openCodeModelName: null,
        localBaseUrl: null,
        localModelName: null,
        isLoading: false,
      })
    } catch (err) {
      set({ isLoading: false, error: err instanceof Error ? err.message : String(err) })
    }
  },
}))