diff --git a/docs/api/cozy-client/interfaces/models.assistant.Assistant.md b/docs/api/cozy-client/interfaces/models.assistant.Assistant.md new file mode 100644 index 000000000..bfa295fc9 --- /dev/null +++ b/docs/api/cozy-client/interfaces/models.assistant.Assistant.md @@ -0,0 +1,89 @@ +[cozy-client](../README.md) / [models](../modules/models.md) / [assistant](../modules/models.assistant.md) / Assistant + +# Interface: Assistant<> + +[models](../modules/models.md).[assistant](../modules/models.assistant.md).Assistant + +## Properties + +### apiKey + +• **apiKey**: `string` + +API key for authentication + +*Defined in* + +[packages/cozy-client/src/models/assistant.js:15](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L15) + +*** + +### baseUrl + +• **baseUrl**: `string` + +Provider's base URL + +*Defined in* + +[packages/cozy-client/src/models/assistant.js:14](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L14) + +*** + +### icon + +• **icon**: `string` + +Optional icon for the assistant + +*Defined in* + +[packages/cozy-client/src/models/assistant.js:12](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L12) + +*** + +### model + +• **model**: `string` + +Model identifier + +*Defined in* + +[packages/cozy-client/src/models/assistant.js:13](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L13) + +*** + +### name + +• **name**: `string` + +Name of the assistant + +*Defined in* + +[packages/cozy-client/src/models/assistant.js:10](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L10) + +*** + +### prompt + +• **prompt**: `string` + +Prompt for the assistant + +*Defined in* + +[packages/cozy-client/src/models/assistant.js:11](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L11) + +*** + +### providerId + +• **providerId**: `string` + +ID of the provider + +*Defined in* + +[packages/cozy-client/src/models/assistant.js:16](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L16) diff --git a/docs/api/cozy-client/modules/models.assistant.md b/docs/api/cozy-client/modules/models.assistant.md index d20749df7..38e5d96eb 100644 --- a/docs/api/cozy-client/modules/models.assistant.md +++ b/docs/api/cozy-client/modules/models.assistant.md @@ -4,6 +4,10 @@ [models](models.md).assistant +## Interfaces + +* [Assistant](../interfaces/models.assistant.Assistant.md) + ## Functions ### createAssistant @@ -19,14 +23,7 @@ Creates a new assistant with the provided data. | Name | Type | Description | | :------ | :------ | :------ | | `client` | [`CozyClient`](../classes/CozyClient.md) | An instance of CozyClient | -| `assistantData` | `Object` | Data for the new assistant | -| `assistantData.apiKey` | `string` | API key for authentication | -| `assistantData.baseUrl` | `string` | Provider's base URL | -| `assistantData.icon` | `string` | - | -| `assistantData.isCustomModel` | `boolean` | Indicates if it's a custom model | -| `assistantData.model` | `string` | Model identifier | -| `assistantData.name` | `string` | Name of the assistant | -| `assistantData.prompt` | `string` | Prompt for the assistant | +| `assistantData` | [`Assistant`](../interfaces/models.assistant.Assistant.md) | Data for the new assistant | *Returns* @@ -36,7 +33,7 @@ Creates a new assistant with the provided data. *Defined in* -[packages/cozy-client/src/models/assistant.js:20](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L20) +[packages/cozy-client/src/models/assistant.js:27](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L27) *** @@ -63,7 +60,7 @@ Deletes an assistant by its ID. *Defined in* -[packages/cozy-client/src/models/assistant.js:79](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L79) +[packages/cozy-client/src/models/assistant.js:95](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L95) *** @@ -81,14 +78,7 @@ Edit assistant with the provided data. | :------ | :------ | :------ | | `client` | [`CozyClient`](../classes/CozyClient.md) | An instance of CozyClient | | `assistantId` | `string` | ID of existed assistant | -| `assistantData` | `Object` | Data for the new assistant | -| `assistantData.apiKey` | `string` | - | -| `assistantData.baseUrl` | `string` | Provider's base URL | -| `assistantData.icon` | `string` | - | -| `assistantData.isCustomModel` | `boolean` | Indicates if it's a custom model | -| `assistantData.model` | `string` | Model identifier | -| `assistantData.name` | `string` | Name of the assistant | -| `assistantData.prompt` | `string` | Prompt for the assistant | +| `assistantData` | [`Assistant`](../interfaces/models.assistant.Assistant.md) | Data for the editted assistant | *Returns* @@ -98,4 +88,4 @@ Edit assistant with the provided data. *Defined in* -[packages/cozy-client/src/models/assistant.js:120](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L120) +[packages/cozy-client/src/models/assistant.js:129](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/assistant.js#L129) diff --git a/packages/cozy-client/src/models/assistant.js b/packages/cozy-client/src/models/assistant.js index e71858d9b..c802f8d45 100644 --- a/packages/cozy-client/src/models/assistant.js +++ b/packages/cozy-client/src/models/assistant.js @@ -2,18 +2,25 @@ import CozyClient from '../CozyClient' import { Q } from '../queries/dsl' import logger from '../logger' +const ASSISTANT_DOCTYPE = 'io.cozy.ai.chat.assistants' +const ACCOUNT_DOCTYPE = 'io.cozy.accounts' + +/** + * @typedef {object} Assistant + * @property {string} name - Name of the assistant + * @property {string} prompt - Prompt for the assistant + * @property {string} [icon] - Optional icon for the assistant + * @property {string} model - Model identifier + * @property {string} baseUrl - Provider's base URL + * @property {string} [apiKey] - API key for authentication + * @property {string} providerId - ID of the provider + */ + /** * Creates a new assistant with the provided data. * * @param {CozyClient} client - An instance of CozyClient - * @param {object} assistantData - Data for the new assistant - * @param {string} assistantData.name - Name of the assistant - * @param {string} assistantData.prompt - Prompt for the assistant - * @param {string} [assistantData.icon] - Optional icon for the assistant - * @param {boolean} assistantData.isCustomModel - Indicates if it's a custom model - * @param {string} assistantData.model - Model identifier - * @param {string} assistantData.baseUrl - Provider's base URL - * @param {string} assistantData.apiKey - API key for authentication + * @param {Assistant} assistantData - Data for the new assistant * @returns {Promise} - A promise that resolves when the assistant is created * @throws {Error} - Throws an error if the creation fails */ @@ -21,15 +28,22 @@ export const createAssistant = async (client, assistantData) => { let createdAccountId = null try { const account = { - _type: 'io.cozy.accounts', + _type: ACCOUNT_DOCTYPE, auth: { - login: assistantData.model, - password: assistantData.apiKey + login: assistantData.model }, - data: { + account_type: assistantData.providerId + } + + if (assistantData.baseUrl) { + account.data = { baseUrl: assistantData.baseUrl } } + + if (assistantData.apiKey) { + account.auth.password = assistantData.apiKey + } const response = await client.save(account) if (!response.data || !response.data._id) { @@ -38,16 +52,18 @@ export const createAssistant = async (client, assistantData) => { createdAccountId = response.data._id const assistant = { - _type: 'io.cozy.ai.chat.assistants', + _type: ASSISTANT_DOCTYPE, name: assistantData.name, prompt: assistantData.prompt, icon: assistantData.icon || null, - isCustomModel: assistantData.isCustomModel, relationships: { provider: { data: { - _type: 'io.cozy.accounts', - _id: createdAccountId + _type: ACCOUNT_DOCTYPE, + _id: createdAccountId, + metadata: { + providerId: assistantData.providerId + } } } } @@ -79,7 +95,7 @@ export const createAssistant = async (client, assistantData) => { export const deleteAssistant = async (client, assistantId) => { try { const existedAssistant = await client.query( - Q('io.cozy.ai.chat.assistants') + Q(ASSISTANT_DOCTYPE) .getById(assistantId) .include(['provider']) ) @@ -88,12 +104,12 @@ export const deleteAssistant = async (client, assistantId) => { const provider = existedAssistant.included?.[0] await client.stackClient - .collection('io.cozy.ai.chat.assistants') + .collection(ASSISTANT_DOCTYPE) .destroy({ _id: assistantId, _rev: assistantInstance._rev }) if (provider?._id && provider?._rev) { await client.stackClient - .collection('io.cozy.accounts') + .collection(ACCOUNT_DOCTYPE) .destroy({ _id: provider._id, _rev: provider._rev }) } } catch (error) { @@ -106,21 +122,14 @@ export const deleteAssistant = async (client, assistantId) => { * * @param {CozyClient} client - An instance of CozyClient * @param {string} assistantId - ID of existed assistant - * @param {object} assistantData - Data for the new assistant - * @param {string} assistantData.name - Name of the assistant - * @param {string} assistantData.prompt - Prompt for the assistant - * @param {string} [assistantData.icon] - Optional icon for the assistant - * @param {boolean} assistantData.isCustomModel - Indicates if it's a custom model - * @param {string} assistantData.model - Model identifier - * @param {string} assistantData.baseUrl - Provider's base URL - * @param {string} [assistantData.apiKey] - API key for authentication + * @param {Assistant} assistantData - Data for the editted assistant * @returns {Promise} - A promise that resolves when the assistant is edited * @throws {Error} - Throws an error if the edition fails */ export const editAssistant = async (client, assistantId, assistantData) => { try { const existedAssistant = await client.query( - Q('io.cozy.ai.chat.assistants') + Q(ASSISTANT_DOCTYPE) .getById(assistantId) .include(['provider']) ) @@ -142,13 +151,23 @@ export const editAssistant = async (client, assistantId, assistantData) => { ...(provider.auth || {}), login: assistantData.model }, + account_type: assistantData.providerId, data: { - ...(provider.data || {}), - baseUrl: assistantData.baseUrl + ...(provider.data || {}) } } + + if (assistantData.baseUrl) { + account.data.baseUrl = assistantData.baseUrl + } else { + delete account.data?.baseUrl + } + + // Only update the password if a new API key is explicitly provided if (assistantData.apiKey) { account.auth.password = assistantData.apiKey + } else if (!assistantData.baseUrl) { + delete account.auth?.password } const response = await client.save(account) @@ -161,7 +180,18 @@ export const editAssistant = async (client, assistantId, assistantData) => { name: assistantData.name, prompt: assistantData.prompt, icon: assistantData.icon || null, - isCustomModel: assistantData.isCustomModel + relationships: { + provider: { + data: { + ...(existedAssistantData?.relationships?.provider?.data || {}), + metadata: { + ...(existedAssistantData?.relationships?.provider?.data + ?.metadata || {}), + providerId: assistantData.providerId + } + } + } + } } await client.save(assistant) } catch (error) { diff --git a/packages/cozy-client/types/models/assistant.d.ts b/packages/cozy-client/types/models/assistant.d.ts index e7f84194d..71ac0072b 100644 --- a/packages/cozy-client/types/models/assistant.d.ts +++ b/packages/cozy-client/types/models/assistant.d.ts @@ -1,20 +1,34 @@ -export function createAssistant(client: CozyClient, assistantData: { - name: string; - prompt: string; - icon: string; - isCustomModel: boolean; - model: string; - baseUrl: string; - apiKey: string; -}): Promise; +export function createAssistant(client: CozyClient, assistantData: Assistant): Promise; export function deleteAssistant(client: CozyClient, assistantId: string): Promise; -export function editAssistant(client: CozyClient, assistantId: string, assistantData: { +export function editAssistant(client: CozyClient, assistantId: string, assistantData: Assistant): Promise; +export type Assistant = { + /** + * - Name of the assistant + */ name: string; + /** + * - Prompt for the assistant + */ prompt: string; - icon: string; - isCustomModel: boolean; + /** + * - Optional icon for the assistant + */ + icon?: string; + /** + * - Model identifier + */ model: string; + /** + * - Provider's base URL + */ baseUrl: string; - apiKey: string; -}): Promise; + /** + * - API key for authentication + */ + apiKey?: string; + /** + * - ID of the provider + */ + providerId: string; +}; import CozyClient from "../CozyClient";