Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions docs/api/cozy-client/interfaces/models.assistant.Assistant.md
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)
28 changes: 9 additions & 19 deletions docs/api/cozy-client/modules/models.assistant.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

[models](models.md).assistant

## Interfaces

* [Assistant](../interfaces/models.assistant.Assistant.md)

## Functions

### createAssistant
Expand All @@ -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*

Expand All @@ -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)

***

Expand All @@ -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)

***

Expand All @@ -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*

Expand All @@ -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)
92 changes: 61 additions & 31 deletions packages/cozy-client/src/models/assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Copy link
Copy Markdown
Contributor

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 = providerId
So 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)


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) {
Expand All @@ -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
}
}
}
}
Expand Down Expand Up @@ -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'])
)
Expand All @@ -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) {
Expand All @@ -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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
* @param {Assistant} assistantData - Data for the editted assistant
* `@param` {Assistant} assistantData - Data for the edited assistant
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/cozy-client/src/models/assistant.js` at line 129, Update the JSDoc
comment for the parameter in packages/cozy-client/src/models/assistant.js by
correcting the typo in the `@param` description: change "editted" to "edited" for
the {Assistant} assistantData parameter so generated API docs use the correct
spelling.

* @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'])
)
Expand All @@ -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)

Expand All @@ -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) {
Expand Down
42 changes: 28 additions & 14 deletions packages/cozy-client/types/models/assistant.d.ts
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";
Loading