Skip to content

Commit 0d08e1f

Browse files
lethemanhlethemanh
authored andcommitted
feat: Change flow select provider and model ✨
1 parent ddbbb1b commit 0d08e1f

12 files changed

Lines changed: 75 additions & 55 deletions

File tree

packages/cozy-search/src/components/CreateAssistantSteps/ApiKeyStep.jsx

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
1-
import React, { useMemo, useState } from 'react'
2-
import { Link } from 'react-router-dom'
1+
import cx from 'classnames'
2+
import React, { useState } from 'react'
33
import { useI18n } from 'twake-i18n'
44

55
import Card from 'cozy-ui/transpiled/react/Card'
66
import Icon from 'cozy-ui/transpiled/react/Icon'
77
import IconButton from 'cozy-ui/transpiled/react/IconButton'
88
import EyeIcon from 'cozy-ui/transpiled/react/Icons/Eye'
9-
import SelectBox from 'cozy-ui/transpiled/react/SelectBox'
109
import TextField from 'cozy-ui/transpiled/react/TextField'
1110
import Typography from 'cozy-ui/transpiled/react/Typography'
11+
import { useCozyTheme } from 'cozy-ui-plus/dist/providers/CozyTheme'
1212

13-
const ApiKeyStep = ({ apiKey, selectedProvider, onChange, onModelSelect }) => {
13+
const ApiKeyStep = ({ apiKey, selectedProvider, onChange }) => {
14+
const { type: theme } = useCozyTheme()
1415
const [showPassword, setShowPassword] = useState(false)
1516

1617
const { t } = useI18n()
17-
const {
18-
models,
19-
id,
20-
name: providerName,
21-
model,
22-
baseUrl
23-
} = selectedProvider || {}
18+
const { id, name: providerName, model, baseUrl } = selectedProvider || {}
2419

2520
const isCustomModel = id === 'custom'
2621

27-
const mappedModels = useMemo(
28-
() =>
29-
models?.map(model => ({
30-
label: model,
31-
value: model
32-
})),
33-
[models]
34-
)
35-
3622
return (
3723
<div className="u-flex u-flex-column u-gap-1">
3824
<Typography variant="body1" className="u-mb-1 u-c-text-secondary">
@@ -84,10 +70,15 @@ const ApiKeyStep = ({ apiKey, selectedProvider, onChange, onModelSelect }) => {
8470
<Typography variant="h6" className="u-mb-half">
8571
{t('assistant_create.steps.configuration.model.label')}
8672
</Typography>
87-
<SelectBox
88-
options={mappedModels}
89-
value={{ label: model, value: model }}
90-
onChange={selectedModel => onModelSelect(selectedModel.value)}
73+
<TextField
74+
fullWidth
75+
placeholder={t(
76+
'assistant_create.steps.configuration.model.placeholder'
77+
)}
78+
value={model}
79+
onChange={onChange('model')}
80+
variant="outlined"
81+
type="text"
9182
/>
9283
</div>
9384
)}
@@ -120,7 +111,12 @@ const ApiKeyStep = ({ apiKey, selectedProvider, onChange, onModelSelect }) => {
120111
/>
121112
</div>
122113

123-
<Card className="u-bg-paleGrey u-p-1 u-bdw-0">
114+
<Card
115+
className={cx(' u-p-1 u-bdw-0', {
116+
'u-bg-primaryBackgroundLight': theme === 'light',
117+
'u-bg-coolGrey': theme === 'dark'
118+
})}
119+
>
124120
<Typography variant="h6" className="u-mb-half">
125121
{t('assistant_create.steps.configuration.no_key')}
126122
</Typography>
@@ -132,9 +128,6 @@ const ApiKeyStep = ({ apiKey, selectedProvider, onChange, onModelSelect }) => {
132128
: t('assistant_create.steps.configuration.custom_provider')
133129
})}
134130
</span>
135-
<Link underline="hover" to="/" className="u-primaryColor">
136-
{t('assistant_create.steps.configuration.read_docs')}
137-
</Link>
138131
</Typography>
139132
</Card>
140133
</div>

packages/cozy-search/src/components/CreateAssistantSteps/Provider.jsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Icon from 'cozy-ui/transpiled/react/Icon'
66
import CheckIcon from 'cozy-ui/transpiled/react/Icons/Check'
77
import PlusSmallIcon from 'cozy-ui/transpiled/react/Icons/PlusSmall'
88
import Typography from 'cozy-ui/transpiled/react/Typography'
9+
import { useCozyTheme } from 'cozy-ui-plus/dist/providers/CozyTheme'
910

1011
import styles from './styles.styl'
1112
import AnthropicLogo from '../../assets/anthropic.svg'
@@ -27,17 +28,21 @@ const ICONS = {
2728

2829
const Provider = ({ selectedProvider, provider, onSelect }) => {
2930
const { t } = useI18n()
31+
const { type: theme } = useCozyTheme()
3032
const isSelected = selectedProvider?.id === provider.id
3133
const isOpenRag = provider.id === 'openrag'
3234

3335
return (
3436
<button
3537
type="button"
36-
className={`u-p-1 u-c-pointer u-flex u-flex-row u-flex-items-center ${
37-
styles['model-card']
38-
} ${isSelected ? styles.selected : ''} ${
39-
isOpenRag ? styles.openrag : ''
40-
}`}
38+
className={cx(
39+
'u-p-1 u-c-pointer u-flex u-flex-row u-flex-items-center',
40+
styles['model-card'],
41+
{
42+
[styles['model-card--selected']]: isSelected,
43+
[styles[`model-card--openrag--${theme}`]]: isOpenRag
44+
}
45+
)}
4146
onClick={() => onSelect(provider)}
4247
>
4348
<div

packages/cozy-search/src/components/CreateAssistantSteps/helpers.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import providers from './providers.json'
33
/**
44
* Get the provider corresponding to a given model name.
55
* If no provider matches, return the custom provider.
6-
* @param {string} modelName - The name of the model.
6+
* @param {string} providerId - The ID of the provider.
77
* @returns {object} The provider object.
88
*/
9-
export const getSelectedProviderByModel = modelName => {
9+
export const getSelectedProviderById = providerId => {
1010
return (
11-
providers.find(provider => provider.models?.includes(modelName)) || {
12-
...providers.find(provider => provider.id === 'custom'),
13-
name: modelName
11+
providers.find(provider => provider.id === providerId) || {
12+
...providers.find(provider => provider.id === 'custom')
1413
}
1514
)
1615
}

packages/cozy-search/src/components/CreateAssistantSteps/providers.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"id": "openrag",
4-
"name": "LINAGORA",
4+
"name": "OPENRAG",
55
"models": ["openrag"],
66
"description": "assistant_create.steps.provider_selection.openrag.description",
77
"icon": "OpenRagLogo",

packages/cozy-search/src/components/CreateAssistantSteps/styles.styl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@
2525
text-align left
2626
background inherit
2727

28-
&.selected
28+
&--selected
2929
border 1px solid var(--primaryColor)
3030

31-
&.openrag
32-
background linear-gradient(90deg, #EFF6FF 0%, #FAF5FF 100%)
31+
&--openrag
32+
&--light
33+
background linear-gradient(90deg, #EFF6FF 0%, #FAF5FF 100%)
34+
35+
&--dark
36+
background linear-gradient(90deg, #3A2899 0%, #0C091B 100%)
3337

3438
& .icon-container
3539
width 53px

packages/cozy-search/src/components/CreateAssistantSteps/useAssistantDialog.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { useMemo, useState } from 'react'
22
import { useI18n, useExtendI18n } from 'twake-i18n'
33

4+
import Minilog from 'cozy-minilog'
45
import { useAlert } from 'cozy-ui/transpiled/react/providers/Alert'
56

67
import { locales } from '../../locales'
78

9+
const log = Minilog('[AssistantDialog]')
10+
811
export const STEPS = {
912
BASIC_INFO: 0,
1013
MODEL_SELECTION: 1,
@@ -66,7 +69,8 @@ export const useAssistantDialog = ({ onClose, initialData = {} }) => {
6669
...prev,
6770
baseUrl: provider.baseUrl,
6871
isCustomModel: provider.id === 'custom',
69-
model: provider.id === 'openrag' ? provider.models[0] : prev.model
72+
model: provider.id === 'openrag' ? provider.models[0] : prev.model,
73+
providerId: provider.id
7074
}))
7175
setSelectedProvider({
7276
...provider,
@@ -95,6 +99,7 @@ export const useAssistantDialog = ({ onClose, initialData = {} }) => {
9599
setStep(prev => prev + 1)
96100
}
97101
} catch (error) {
102+
log.error('Error in handleNext:', error)
98103
showAlert({ message: t('assistant.default_error'), severity: 'error' })
99104
}
100105
}

packages/cozy-search/src/components/Views/CreateAssistantDialog.jsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ const CreateAssistantDialog = ({ open, onClose }) => {
4141
handleAvatarChange,
4242
isNextDisabled,
4343
handleChangeModel
44-
} = useAssistantDialog({ onClose })
44+
} = useAssistantDialog({
45+
onClose,
46+
initialData: { selectedProvider: { id: 'openrag' } }
47+
})
4548

4649
const getTitle = () => {
4750
if (step === STEPS.API_KEY) {
@@ -58,7 +61,8 @@ const CreateAssistantDialog = ({ open, onClose }) => {
5861
model: formData.model,
5962
apiKey: formData.apiKey,
6063
baseUrl: formData.baseUrl,
61-
isCustomModel: formData.isCustomModel
64+
isCustomModel: formData.isCustomModel,
65+
providerId: selectedProvider.id
6266
})
6367
showAlert({ message: t('assistant_create.success'), severity: 'success' })
6468
}

packages/cozy-search/src/components/Views/EditAssistantDialog.jsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { useAlert } from 'cozy-ui/transpiled/react/providers/Alert'
1818
import { locales } from '../../locales'
1919
import { useAssistant } from '../AssistantProvider'
2020
import AssistantDialogContent from '../CreateAssistantSteps/AssistantDialogContent'
21-
import { getSelectedProviderByModel } from '../CreateAssistantSteps/helpers'
21+
import { getSelectedProviderById } from '../CreateAssistantSteps/helpers'
2222
import styles from '../CreateAssistantSteps/styles.styl'
2323
import {
2424
useAssistantDialog,
@@ -67,16 +67,21 @@ const EditAssistantDialog = ({ open, onClose }) => {
6767
model: provider?.auth?.login || '',
6868
baseUrl: provider?.data?.baseUrl || '',
6969
apiKey: provider?.auth?.apiKey || '',
70-
encryptedApiKey: provider?.auth?.credentials_encrypted || ''
70+
encryptedApiKey: provider?.auth?.credentials_encrypted || '',
71+
providerId: assistant?.relationships?.provider?.cozyMetadata?.providerId
7172
})
7273

73-
const selectProviderDefault = getSelectedProviderByModel(
74-
provider?.auth?.login
74+
const selectProviderDefault = getSelectedProviderById(
75+
assistant?.relationships?.provider?.cozyMetadata?.providerId
7576
)
7677
setSelectedProvider({
7778
...selectProviderDefault,
7879
model: provider?.auth?.login,
79-
baseUrl: provider?.data?.baseUrl
80+
baseUrl: provider?.data?.baseUrl,
81+
name:
82+
selectProviderDefault.id === 'custom'
83+
? provider?.auth?.login
84+
: selectProviderDefault.name
8085
})
8186
}
8287
fetchAssistant()
@@ -97,7 +102,8 @@ const EditAssistantDialog = ({ open, onClose }) => {
97102
model: formData.model,
98103
apiKey: formData.apiKey,
99104
baseUrl: formData.baseUrl,
100-
isCustomModel: formData.isCustomModel
105+
isCustomModel: formData.isCustomModel,
106+
providerId: selectedProvider.id
101107
})
102108
showAlert({ message: t('assistant_edit.success'), severity: 'success' })
103109
}

packages/cozy-search/src/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@
127127
"description": "Enter provider URL, model and your API key to configure the new assistant. Your key will be stored securely.",
128128
"custom_provider_description": "Configure your custom AI provider settings",
129129
"model": {
130-
"label": "Model"
130+
"label": "Model",
131+
"placeholder": "Enter the model name of the provider"
131132
},
132133
"provider": {
133134
"label": "Provider name",

packages/cozy-search/src/locales/fr.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@
127127
"description": "Saisissez l'URL du fournisseur, le modèle et votre clé API pour configurer le nouvel assistant. Votre clé sera stockée de manière sécurisée.",
128128
"custom_provider_description": "Configurez les paramètres de votre fournisseur d'IA personnalisé",
129129
"model": {
130-
"label": "Modèle"
130+
"label": "Modèle",
131+
"placeholder": "Entrez le nom du modèle du fournisseur"
131132
},
132133
"provider": {
133134
"label": "Nom du fournisseur",

0 commit comments

Comments
 (0)