Skip to content

Commit

Permalink
feat: add proxy enable settings
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu-xiaowei committed Jan 24, 2025
1 parent e00dddb commit f67eb0c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 20 deletions.
4 changes: 2 additions & 2 deletions react-native/src/api/open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
getApiUrl,
getDeepSeekApiKey,
getOpenAIApiKey,
getServerProxyEnabled,
getOpenAIProxyEnabled,
getTextModel,
} from '../storage/StorageUtils.ts';
import {
Expand Down Expand Up @@ -260,7 +260,7 @@ function getApiURL(): string {
if (getTextModel().modelId.includes('deepseek')) {
return 'https://api.deepseek.com/chat/completions';
} else {
if (getServerProxyEnabled()) {
if (getOpenAIProxyEnabled()) {
return (isDev ? 'http://localhost:8080' : getApiUrl()) + '/api/gpt';
} else {
return 'https://api.openai.com/v1/chat/completions';
Expand Down
57 changes: 50 additions & 7 deletions react-native/src/settings/SettingsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
getModelUsage,
getOllamaApiUrl,
getOpenAIApiKey,
getOpenAIProxyEnabled,
getRegion,
getTextModel,
isNewStabilityImageModel,
Expand All @@ -37,6 +38,7 @@ import {
saveKeys,
saveOllamaApiURL,
saveOpenAIApiKey,
saveOpenAIProxyEnabled,
saveRegion,
saveTextModel,
} from '../storage/StorageUtils.ts';
Expand Down Expand Up @@ -73,6 +75,9 @@ function SettingsScreen(): React.JSX.Element {
const [ollamaApiUrl, setOllamaApiUrl] = useState(getOllamaApiUrl);
const [deepSeekApiKey, setDeepSeekApiKey] = useState(getDeepSeekApiKey);
const [openAIApiKey, setOpenAIApiKey] = useState(getOpenAIApiKey);
const [openAIProxyEnabled, setOpenAIProxyEnabled] = useState(
getOpenAIProxyEnabled
);
const [region, setRegion] = useState(getRegion);
const [imageSize, setImageSize] = useState(getImageSize);
const [hapticEnabled, setHapticEnabled] = useState(getHapticEnabled);
Expand Down Expand Up @@ -238,6 +243,11 @@ function SettingsScreen(): React.JSX.Element {
value: size,
}));

const toggleOpenAIProxy = (value: boolean) => {
setOpenAIProxyEnabled(value);
saveOpenAIProxyEnabled(value);
};

return (
<SafeAreaView style={styles.safeArea}>
<ScrollView style={styles.container}>
Expand Down Expand Up @@ -277,20 +287,32 @@ function SettingsScreen(): React.JSX.Element {
onChangeText={setOllamaApiUrl}
placeholder="Enter Ollama API URL"
/>
<CustomTextInput
label="OpenAI API Key"
value={openAIApiKey}
onChangeText={setOpenAIApiKey}
placeholder="Enter OpenAI API Key"
secureTextEntry={true}
/>
<CustomTextInput
label="DeepSeek API Key"
value={deepSeekApiKey}
onChangeText={setDeepSeekApiKey}
placeholder="Enter Deep Seek API Key"
secureTextEntry={true}
/>
<View style={styles.apiKeyContainer}>
<View style={styles.apiKeyInputContainer}>
<CustomTextInput
label="OpenAI API Key"
value={openAIApiKey}
onChangeText={setOpenAIApiKey}
placeholder="Enter OpenAI API Key"
secureTextEntry={true}
/>
</View>
<View
style={[styles.proxyContainer, isMac && styles.proxyMacContainer]}>
<Text style={styles.proxyLabel}>Proxy</Text>
<Switch
value={openAIProxyEnabled}
onValueChange={toggleOpenAIProxy}
/>
</View>
</View>
<Text style={[styles.label, styles.middleLabel]}>Select Model</Text>
<CustomDropdown
label="Text Model"
Expand Down Expand Up @@ -489,6 +511,27 @@ const styles = StyleSheet.create({
marginVertical: 10,
paddingBottom: 60,
},
apiKeyContainer: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
},
apiKeyInputContainer: {
flex: 1,
marginRight: 10,
},
proxyContainer: {
marginBottom: 12,
},
proxyMacContainer: {
marginTop: 10,
},
proxyLabel: {
fontSize: 12,
color: 'black',
fontWeight: '500',
marginBottom: 6,
},
});

export default SettingsScreen;
22 changes: 11 additions & 11 deletions react-native/src/storage/StorageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const modelUsageKey = keyPrefix + 'modelUsageKey';
const systemPromptsKey = keyPrefix + 'systemPromptsKey';
const currentSystemPromptKey = keyPrefix + 'currentSystemPromptKey';
const currentPromptIdKey = keyPrefix + 'currentPromptIdKey';
const serverProxyEnabledKey = keyPrefix + 'serverProxyEnabledKey';
const openAIProxyEnabledKey = keyPrefix + 'openAIProxyEnabledKey';

let currentApiUrl: string | undefined;
let currentApiKey: string | undefined;
Expand All @@ -64,7 +64,7 @@ let currentRegion: string | undefined;
let currentImageModel: Model | undefined;
let currentTextModel: Model | undefined;
let currentSystemPrompts: SystemPrompt[] | undefined;
let currentServerProxyEnabled: boolean | undefined;
let currentOpenAIProxyEnabled: boolean | undefined;

export function saveMessages(
sessionId: number,
Expand Down Expand Up @@ -376,17 +376,17 @@ export function savePromptId(promptId: number) {
storage.set(currentPromptIdKey, promptId);
}

export function saveServerProxyEnabled(enabled: boolean) {
currentServerProxyEnabled = enabled;
storage.set(serverProxyEnabledKey, enabled);
export function saveOpenAIProxyEnabled(enabled: boolean) {
currentOpenAIProxyEnabled = enabled;
storage.set(openAIProxyEnabledKey, enabled);
}

export function getServerProxyEnabled() {
if (currentServerProxyEnabled !== undefined) {
return currentServerProxyEnabled;
export function getOpenAIProxyEnabled() {
if (currentOpenAIProxyEnabled !== undefined) {
return currentOpenAIProxyEnabled;
} else {
currentServerProxyEnabled =
storage.getBoolean(serverProxyEnabledKey) ?? false;
return currentServerProxyEnabled;
currentOpenAIProxyEnabled =
storage.getBoolean(openAIProxyEnabledKey) ?? true;
return currentOpenAIProxyEnabled;
}
}

0 comments on commit f67eb0c

Please sign in to comment.