-
Notifications
You must be signed in to change notification settings - Fork 14
fix: Avoid insert empty api key when creating openrag assistant 🐛 #1674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,34 +2,48 @@ 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<void>} - A promise that resolves when the assistant is created | ||||||
| * @throws {Error} - Throws an error if the creation fails | ||||||
| */ | ||||||
| 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 | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix typo: "editted" → "edited". This typo will propagate to the auto-generated API documentation. Proposed fix- * `@param` {Assistant} assistantData - Data for the editted assistant
+ * `@param` {Assistant} assistantData - Data for the edited assistant📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| * @returns {Promise<void>} - 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) { | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<void>; | ||
| export function createAssistant(client: CozyClient, assistantData: Assistant): Promise<void>; | ||
| export function deleteAssistant(client: CozyClient, assistantId: string): Promise<void>; | ||
| export function editAssistant(client: CozyClient, assistantId: string, assistantData: { | ||
| export function editAssistant(client: CozyClient, assistantId: string, assistantData: Assistant): Promise<void>; | ||
| 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<void>; | ||
| /** | ||
| * - API key for authentication | ||
| */ | ||
| apiKey?: string; | ||
| /** | ||
| * - ID of the provider | ||
| */ | ||
| providerId: string; | ||
| }; | ||
| import CozyClient from "../CozyClient"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add the providerId ? Like this:
account.account_type = providerIdSo we can identify which account it is
See https://github.com/cozy/cozy-doctypes/blob/master/docs/io.cozy.accounts.md
(the "konnector" concept does not apply here)