chenbhao commited on
Commit
e91c819
·
1 Parent(s): 8083f17

fix: /login default enter and black pre-config

Browse files
src/components/LocalLoginFlow.tsx CHANGED
@@ -1,6 +1,6 @@
1
  import * as React from 'react'
2
- import { useState } from 'react'
3
- import { saveLocalModelConfig } from '../utils/auth.js'
4
  import { Box, Text } from '../ink.js'
5
  import TextInput from './TextInput.js'
6
 
@@ -14,8 +14,23 @@ export function LocalLoginFlow({ onDone, startingMessage }: Props) {
14
  const [modelName, setModelName] = useState<string>('')
15
  const [cursorOffset, setCursorOffset] = useState(0)
16
  const [isAnalyzing, setIsAnalyzing] = useState(false)
 
 
17
  const defaultUrl = 'http://127.0.0.1:8001'
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  // 分析 URL 提取模型名
20
  const analyzeUrl = (inputUrl: string) => {
21
  setIsAnalyzing(true)
@@ -71,15 +86,18 @@ export function LocalLoginFlow({ onDone, startingMessage }: Props) {
71
  cursorOffset={cursorOffset}
72
  onChangeCursorOffset={setCursorOffset}
73
  columns={72}
74
- placeholder={defaultUrl}
75
  focus={true}
76
  />
77
  </Box>
78
 
79
- {modelName && (
80
  <Box flexDirection="column" gap={1}>
81
  <Text dimColor={true}>
82
- Detected model: <Text bold={true}>{modelName}</Text>
 
 
 
83
  </Text>
84
  </Box>
85
  )}
 
1
  import * as React from 'react'
2
+ import { useState, useEffect } from 'react'
3
+ import { saveLocalModelConfig, getLocalBaseUrl, getLocalModelName } from '../utils/auth.js'
4
  import { Box, Text } from '../ink.js'
5
  import TextInput from './TextInput.js'
6
 
 
14
  const [modelName, setModelName] = useState<string>('')
15
  const [cursorOffset, setCursorOffset] = useState(0)
16
  const [isAnalyzing, setIsAnalyzing] = useState(false)
17
+ const [existingBaseUrl, setExistingBaseUrl] = useState<string | null>(null)
18
+ const [existingModelName, setExistingModelName] = useState<string | null>(null)
19
  const defaultUrl = 'http://127.0.0.1:8001'
20
 
21
+ useEffect(() => {
22
+ const baseUrl = getLocalBaseUrl()
23
+ const model = getLocalModelName()
24
+ if (baseUrl) {
25
+ setExistingBaseUrl(baseUrl)
26
+ setUrl(baseUrl)
27
+ }
28
+ if (model) {
29
+ setExistingModelName(model)
30
+ setModelName(model)
31
+ }
32
+ }, [])
33
+
34
  // 分析 URL 提取模型名
35
  const analyzeUrl = (inputUrl: string) => {
36
  setIsAnalyzing(true)
 
86
  cursorOffset={cursorOffset}
87
  onChangeCursorOffset={setCursorOffset}
88
  columns={72}
89
+ placeholder={existingBaseUrl || defaultUrl}
90
  focus={true}
91
  />
92
  </Box>
93
 
94
+ {(modelName || existingModelName) && (
95
  <Box flexDirection="column" gap={1}>
96
  <Text dimColor={true}>
97
+ {modelName ?
98
+ `Detected model: <Text bold={true}>{modelName}</Text>` :
99
+ `Current model: <Text bold={true}>{existingModelName}</Text>`
100
+ }
101
  </Text>
102
  </Box>
103
  )}
src/components/OpenAILoginFlow.tsx CHANGED
@@ -1,11 +1,12 @@
1
  import * as React from 'react'
2
- import { useState } from 'react'
3
  import { Box, Text } from '../ink.js'
4
  import {
5
  importOpenAIAuthFromCodexCache,
6
  runCodexLogin,
7
  saveOpenAIApiKey,
8
  saveOpenAIAuthTokens,
 
9
  } from '../utils/auth.js'
10
  import { Select } from './CustomSelect/select.js'
11
  import TextInput from './TextInput.js'
@@ -27,6 +28,14 @@ export function OpenAILoginFlow({
27
  const [status, setStatus] = useState<string | null>(null)
28
  const [inputValue, setInputValue] = useState('')
29
  const [cursorOffset, setCursorOffset] = useState(0)
 
 
 
 
 
 
 
 
30
 
31
  const menuOptions = [
32
  {
@@ -116,12 +125,14 @@ export function OpenAILoginFlow({
116
  }
117
 
118
  async function handleSubmit(value?: string): Promise<void> {
119
- if (!value) {
120
  return
121
  }
122
 
123
- const trimmed = value.trim()
124
- if (!trimmed) {
 
 
125
  return
126
  }
127
 
@@ -129,9 +140,9 @@ export function OpenAILoginFlow({
129
  setStatus(null)
130
  try {
131
  if (mode === 'api_key') {
132
- await saveOpenAIApiKey(trimmed)
133
  } else {
134
- saveOpenAIAuthTokens({ accessToken: trimmed })
135
  }
136
  onDone()
137
  } catch (error) {
@@ -185,6 +196,7 @@ export function OpenAILoginFlow({
185
  columns={72}
186
  mask="*"
187
  focus={true}
 
188
  />
189
  </Box>
190
  {status ? <Text color="error">{status}</Text> : null}
 
1
  import * as React from 'react'
2
+ import { useState, useEffect } from 'react'
3
  import { Box, Text } from '../ink.js'
4
  import {
5
  importOpenAIAuthFromCodexCache,
6
  runCodexLogin,
7
  saveOpenAIApiKey,
8
  saveOpenAIAuthTokens,
9
+ getOpenAIApiKey,
10
  } from '../utils/auth.js'
11
  import { Select } from './CustomSelect/select.js'
12
  import TextInput from './TextInput.js'
 
28
  const [status, setStatus] = useState<string | null>(null)
29
  const [inputValue, setInputValue] = useState('')
30
  const [cursorOffset, setCursorOffset] = useState(0)
31
+ const [existingApiKey, setExistingApiKey] = useState<string | null>(null)
32
+
33
+ useEffect(() => {
34
+ const key = getOpenAIApiKey()
35
+ if (key) {
36
+ setExistingApiKey(key)
37
+ }
38
+ }, [])
39
 
40
  const menuOptions = [
41
  {
 
125
  }
126
 
127
  async function handleSubmit(value?: string): Promise<void> {
128
+ if (!value && mode === 'api_key' && !existingApiKey) {
129
  return
130
  }
131
 
132
+ const trimmed = value?.trim() || ''
133
+ const keyToSave = mode === 'api_key' ? (trimmed || existingApiKey) : trimmed
134
+
135
+ if (!keyToSave) {
136
  return
137
  }
138
 
 
140
  setStatus(null)
141
  try {
142
  if (mode === 'api_key') {
143
+ await saveOpenAIApiKey(keyToSave)
144
  } else {
145
+ saveOpenAIAuthTokens({ accessToken: keyToSave })
146
  }
147
  onDone()
148
  } catch (error) {
 
196
  columns={72}
197
  mask="*"
198
  focus={true}
199
+ placeholder={mode === 'api_key' ? existingApiKey || undefined : undefined}
200
  />
201
  </Box>
202
  {status ? <Text color="error">{status}</Text> : null}
src/components/OpenRouterLoginFlow.tsx CHANGED
@@ -1,7 +1,7 @@
1
  import * as React from 'react'
2
- import { useState } from 'react'
3
  import { Box, Text } from '../ink.js'
4
- import { saveOpenRouterApiKey } from '../utils/auth.js'
5
  import { Spinner } from './Spinner.js'
6
  import TextInput from './TextInput.js'
7
 
@@ -18,21 +18,31 @@ export function OpenRouterLoginFlow({
18
  const [status, setStatus] = useState<string | null>(null)
19
  const [inputValue, setInputValue] = useState('')
20
  const [cursorOffset, setCursorOffset] = useState(0)
 
 
 
 
 
 
 
 
21
 
22
  async function handleSubmit(value?: string): Promise<void> {
23
- if (!value) {
24
  return
25
  }
26
 
27
- const trimmed = value.trim()
28
- if (!trimmed) {
 
 
29
  return
30
  }
31
 
32
  setIsBusy(true)
33
  setStatus(null)
34
  try {
35
- await saveOpenRouterApiKey(trimmed)
36
  onDone()
37
  } catch (error) {
38
  setStatus(error instanceof Error ? error.message : String(error))
@@ -81,6 +91,7 @@ export function OpenRouterLoginFlow({
81
  columns={72}
82
  mask="*"
83
  focus={true}
 
84
  />
85
  </Box>
86
  {status ? <Text color="error">{status}</Text> : null}
 
1
  import * as React from 'react'
2
+ import { useState, useEffect } from 'react'
3
  import { Box, Text } from '../ink.js'
4
+ import { saveOpenRouterApiKey, getOpenRouterApiKey } from '../utils/auth.js'
5
  import { Spinner } from './Spinner.js'
6
  import TextInput from './TextInput.js'
7
 
 
18
  const [status, setStatus] = useState<string | null>(null)
19
  const [inputValue, setInputValue] = useState('')
20
  const [cursorOffset, setCursorOffset] = useState(0)
21
+ const [existingKey, setExistingKey] = useState<string | null>(null)
22
+
23
+ useEffect(() => {
24
+ const key = getOpenRouterApiKey()
25
+ if (key) {
26
+ setExistingKey(key)
27
+ }
28
+ }, [])
29
 
30
  async function handleSubmit(value?: string): Promise<void> {
31
+ if (!value && !existingKey) {
32
  return
33
  }
34
 
35
+ const trimmed = value?.trim() || ''
36
+ const keyToSave = trimmed || existingKey
37
+
38
+ if (!keyToSave) {
39
  return
40
  }
41
 
42
  setIsBusy(true)
43
  setStatus(null)
44
  try {
45
+ await saveOpenRouterApiKey(keyToSave)
46
  onDone()
47
  } catch (error) {
48
  setStatus(error instanceof Error ? error.message : String(error))
 
91
  columns={72}
92
  mask="*"
93
  focus={true}
94
+ placeholder={existingKey || undefined}
95
  />
96
  </Box>
97
  {status ? <Text color="error">{status}</Text> : null}