@@ -2,34 +2,49 @@ import CozyClient from '../CozyClient'
22import { Q } from '../queries/dsl'
33import 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 */
2028export 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) => {
7997export 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 */
120131export 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