Skip to content

Commit 5833cdf

Browse files
lethemanhlethemanh
authored andcommitted
fix: Avoid insert empty api key when creating openrag assistant 🐛
1 parent 89ecca5 commit 5833cdf

File tree

4 files changed

+204
-62
lines changed

4 files changed

+204
-62
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
[cozy-client](../README.md) / [models](../modules/models.md) / [assistant](../modules/models.assistant.md) / Assistant
2+
3+
# Interface: Assistant<>
4+
5+
[models](../modules/models.md).[assistant](../modules/models.assistant.md).Assistant
6+
7+
## Properties
8+
9+
### apiKey
10+
11+
**apiKey**: `string`
12+
13+
API key for authentication
14+
15+
*Defined in*
16+
17+
[packages/cozy-client/src/models/assistant.js:16](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L16)
18+
19+
***
20+
21+
### baseUrl
22+
23+
**baseUrl**: `string`
24+
25+
Provider's base URL
26+
27+
*Defined in*
28+
29+
[packages/cozy-client/src/models/assistant.js:15](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L15)
30+
31+
***
32+
33+
### icon
34+
35+
**icon**: `string`
36+
37+
Optional icon for the assistant
38+
39+
*Defined in*
40+
41+
[packages/cozy-client/src/models/assistant.js:12](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L12)
42+
43+
***
44+
45+
### isCustomModel
46+
47+
**isCustomModel**: `boolean`
48+
49+
Indicates if it's a custom model
50+
51+
*Defined in*
52+
53+
[packages/cozy-client/src/models/assistant.js:13](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L13)
54+
55+
***
56+
57+
### model
58+
59+
**model**: `string`
60+
61+
Model identifier
62+
63+
*Defined in*
64+
65+
[packages/cozy-client/src/models/assistant.js:14](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L14)
66+
67+
***
68+
69+
### name
70+
71+
**name**: `string`
72+
73+
Name of the assistant
74+
75+
*Defined in*
76+
77+
[packages/cozy-client/src/models/assistant.js:10](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L10)
78+
79+
***
80+
81+
### prompt
82+
83+
**prompt**: `string`
84+
85+
Prompt for the assistant
86+
87+
*Defined in*
88+
89+
[packages/cozy-client/src/models/assistant.js:11](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L11)
90+
91+
***
92+
93+
### providerId
94+
95+
**providerId**: `string`
96+
97+
ID of the provider
98+
99+
*Defined in*
100+
101+
[packages/cozy-client/src/models/assistant.js:17](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L17)

docs/api/cozy-client/modules/models.assistant.md

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
[models](models.md).assistant
66

7+
## Interfaces
8+
9+
* [Assistant](../interfaces/models.assistant.Assistant.md)
10+
711
## Functions
812

913
### createAssistant
@@ -19,14 +23,7 @@ Creates a new assistant with the provided data.
1923
| Name | Type | Description |
2024
| :------ | :------ | :------ |
2125
| `client` | [`CozyClient`](../classes/CozyClient.md) | An instance of CozyClient |
22-
| `assistantData` | `Object` | Data for the new assistant |
23-
| `assistantData.apiKey` | `string` | API key for authentication |
24-
| `assistantData.baseUrl` | `string` | Provider's base URL |
25-
| `assistantData.icon` | `string` | - |
26-
| `assistantData.isCustomModel` | `boolean` | Indicates if it's a custom model |
27-
| `assistantData.model` | `string` | Model identifier |
28-
| `assistantData.name` | `string` | Name of the assistant |
29-
| `assistantData.prompt` | `string` | Prompt for the assistant |
26+
| `assistantData` | [`Assistant`](../interfaces/models.assistant.Assistant.md) | Data for the new assistant |
3027

3128
*Returns*
3229

@@ -36,7 +33,7 @@ Creates a new assistant with the provided data.
3633

3734
*Defined in*
3835

39-
[packages/cozy-client/src/models/assistant.js:20](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L20)
36+
[packages/cozy-client/src/models/assistant.js:28](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L28)
4037

4138
***
4239

@@ -63,7 +60,7 @@ Deletes an assistant by its ID.
6360

6461
*Defined in*
6562

66-
[packages/cozy-client/src/models/assistant.js:79](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L79)
63+
[packages/cozy-client/src/models/assistant.js:97](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L97)
6764

6865
***
6966

@@ -81,14 +78,7 @@ Edit assistant with the provided data.
8178
| :------ | :------ | :------ |
8279
| `client` | [`CozyClient`](../classes/CozyClient.md) | An instance of CozyClient |
8380
| `assistantId` | `string` | ID of existed assistant |
84-
| `assistantData` | `Object` | Data for the new assistant |
85-
| `assistantData.apiKey` | `string` | - |
86-
| `assistantData.baseUrl` | `string` | Provider's base URL |
87-
| `assistantData.icon` | `string` | - |
88-
| `assistantData.isCustomModel` | `boolean` | Indicates if it's a custom model |
89-
| `assistantData.model` | `string` | Model identifier |
90-
| `assistantData.name` | `string` | Name of the assistant |
91-
| `assistantData.prompt` | `string` | Prompt for the assistant |
81+
| `assistantData` | [`Assistant`](../interfaces/models.assistant.Assistant.md) | Data for the editted assistant |
9282

9383
*Returns*
9484

@@ -98,4 +88,4 @@ Edit assistant with the provided data.
9888

9989
*Defined in*
10090

101-
[packages/cozy-client/src/models/assistant.js:120](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L120)
91+
[packages/cozy-client/src/models/assistant.js:131](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L131)

packages/cozy-client/src/models/assistant.js

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,49 @@ import CozyClient from '../CozyClient'
22
import { Q } from '../queries/dsl'
33
import logger from '../logger'
44

5+
const ASSISTANT_DOCTYPE = 'io.cozy.ai.chat.assistants'
6+
const ACCOUNT_DOCTYPE = 'io.cozy.accounts'
7+
8+
/**
9+
* @typedef {object} Assistant
10+
* @property {string} name - Name of the assistant
11+
* @property {string} prompt - Prompt for the assistant
12+
* @property {string} [icon] - Optional icon for the assistant
13+
* @property {boolean} isCustomModel - Indicates if it's a custom model
14+
* @property {string} model - Model identifier
15+
* @property {string} baseUrl - Provider's base URL
16+
* @property {string} [apiKey] - API key for authentication
17+
* @property {string} providerId - ID of the provider
18+
*/
19+
520
/**
621
* Creates a new assistant with the provided data.
722
*
823
* @param {CozyClient} client - An instance of CozyClient
9-
* @param {object} assistantData - Data for the new assistant
10-
* @param {string} assistantData.name - Name of the assistant
11-
* @param {string} assistantData.prompt - Prompt for the assistant
12-
* @param {string} [assistantData.icon] - Optional icon for the assistant
13-
* @param {boolean} assistantData.isCustomModel - Indicates if it's a custom model
14-
* @param {string} assistantData.model - Model identifier
15-
* @param {string} assistantData.baseUrl - Provider's base URL
16-
* @param {string} assistantData.apiKey - API key for authentication
24+
* @param {Assistant} assistantData - Data for the new assistant
1725
* @returns {Promise<void>} - A promise that resolves when the assistant is created
1826
* @throws {Error} - Throws an error if the creation fails
1927
*/
2028
export const createAssistant = async (client, assistantData) => {
2129
let createdAccountId = null
2230
try {
2331
const account = {
24-
_type: 'io.cozy.accounts',
32+
_type: ACCOUNT_DOCTYPE,
2533
auth: {
26-
login: assistantData.model,
27-
password: assistantData.apiKey
34+
login: assistantData.model
2835
},
29-
data: {
36+
account_type: assistantData.providerId
37+
}
38+
39+
if (assistantData.baseUrl) {
40+
account.data = {
3041
baseUrl: assistantData.baseUrl
3142
}
3243
}
44+
45+
if (assistantData.apiKey) {
46+
account.auth.password = assistantData.apiKey
47+
}
3348
const response = await client.save(account)
3449

3550
if (!response.data || !response.data._id) {
@@ -38,16 +53,19 @@ export const createAssistant = async (client, assistantData) => {
3853
createdAccountId = response.data._id
3954

4055
const assistant = {
41-
_type: 'io.cozy.ai.chat.assistants',
56+
_type: ASSISTANT_DOCTYPE,
4257
name: assistantData.name,
4358
prompt: assistantData.prompt,
4459
icon: assistantData.icon || null,
4560
isCustomModel: assistantData.isCustomModel,
4661
relationships: {
4762
provider: {
4863
data: {
49-
_type: 'io.cozy.accounts',
50-
_id: createdAccountId
64+
_type: ACCOUNT_DOCTYPE,
65+
_id: createdAccountId,
66+
metadata: {
67+
providerId: assistantData.providerId
68+
}
5169
}
5270
}
5371
}
@@ -79,7 +97,7 @@ export const createAssistant = async (client, assistantData) => {
7997
export const deleteAssistant = async (client, assistantId) => {
8098
try {
8199
const existedAssistant = await client.query(
82-
Q('io.cozy.ai.chat.assistants')
100+
Q(ASSISTANT_DOCTYPE)
83101
.getById(assistantId)
84102
.include(['provider'])
85103
)
@@ -88,12 +106,12 @@ export const deleteAssistant = async (client, assistantId) => {
88106
const provider = existedAssistant.included?.[0]
89107

90108
await client.stackClient
91-
.collection('io.cozy.ai.chat.assistants')
109+
.collection(ASSISTANT_DOCTYPE)
92110
.destroy({ _id: assistantId, _rev: assistantInstance._rev })
93111

94112
if (provider?._id && provider?._rev) {
95113
await client.stackClient
96-
.collection('io.cozy.accounts')
114+
.collection(ACCOUNT_DOCTYPE)
97115
.destroy({ _id: provider._id, _rev: provider._rev })
98116
}
99117
} catch (error) {
@@ -106,21 +124,14 @@ export const deleteAssistant = async (client, assistantId) => {
106124
*
107125
* @param {CozyClient} client - An instance of CozyClient
108126
* @param {string} assistantId - ID of existed assistant
109-
* @param {object} assistantData - Data for the new assistant
110-
* @param {string} assistantData.name - Name of the assistant
111-
* @param {string} assistantData.prompt - Prompt for the assistant
112-
* @param {string} [assistantData.icon] - Optional icon for the assistant
113-
* @param {boolean} assistantData.isCustomModel - Indicates if it's a custom model
114-
* @param {string} assistantData.model - Model identifier
115-
* @param {string} assistantData.baseUrl - Provider's base URL
116-
* @param {string} [assistantData.apiKey] - API key for authentication
127+
* @param {Assistant} assistantData - Data for the editted assistant
117128
* @returns {Promise<void>} - A promise that resolves when the assistant is edited
118129
* @throws {Error} - Throws an error if the edition fails
119130
*/
120131
export const editAssistant = async (client, assistantId, assistantData) => {
121132
try {
122133
const existedAssistant = await client.query(
123-
Q('io.cozy.ai.chat.assistants')
134+
Q(ASSISTANT_DOCTYPE)
124135
.getById(assistantId)
125136
.include(['provider'])
126137
)
@@ -142,13 +153,23 @@ export const editAssistant = async (client, assistantId, assistantData) => {
142153
...(provider.auth || {}),
143154
login: assistantData.model
144155
},
156+
account_type: assistantData.providerId,
145157
data: {
146-
...(provider.data || {}),
147-
baseUrl: assistantData.baseUrl
158+
...(provider.data || {})
148159
}
149160
}
161+
162+
if (assistantData.baseUrl) {
163+
account.data.baseUrl = assistantData.baseUrl
164+
} else {
165+
delete account.data?.baseUrl
166+
}
167+
168+
// Only update the password if a new API key is explicitly provided
150169
if (assistantData.apiKey) {
151170
account.auth.password = assistantData.apiKey
171+
} else if (!assistantData.baseUrl) {
172+
delete account.auth?.password
152173
}
153174
const response = await client.save(account)
154175

@@ -161,7 +182,19 @@ export const editAssistant = async (client, assistantId, assistantData) => {
161182
name: assistantData.name,
162183
prompt: assistantData.prompt,
163184
icon: assistantData.icon || null,
164-
isCustomModel: assistantData.isCustomModel
185+
isCustomModel: assistantData.isCustomModel,
186+
relationships: {
187+
provider: {
188+
data: {
189+
...(existedAssistantData?.relationships?.provider?.data || {}),
190+
metadata: {
191+
...(existedAssistantData?.relationships?.provider?.data
192+
?.metadata || {}),
193+
providerId: assistantData.providerId
194+
}
195+
}
196+
}
197+
}
165198
}
166199
await client.save(assistant)
167200
} catch (error) {

0 commit comments

Comments
 (0)