diff --git a/src/ai/ai.ts b/src/ai/ai.ts index 3acf907c..3b7593d1 100644 --- a/src/ai/ai.ts +++ b/src/ai/ai.ts @@ -28,7 +28,7 @@ export class Ai extends FactoryBase { * Sets up ai configuration for an AINFT object. * @param {string} objectId - The ID of the AINFT object. * @param {string} serviceName - The name of Ainize service. - * @returns {Promise} Returns a promise that resolves with both the transaction result and the configuration info. + * @returns {Promise} A promise that resolves with both the transaction result and the configuration info. */ async configure( objectId: string, @@ -49,7 +49,7 @@ export class Ai extends FactoryBase { /** * Retrieves the credit balance for a service. * @param {string} serviceName - The name of Ainize service. - * @returns {Promise} Returns a promise that resolves with the credit balance. + * @returns {Promise} A promise that resolves with the credit balance. */ async getCredit(serviceName: string): Promise { const address = await this.ain.signer.getAddress(); @@ -80,7 +80,7 @@ export class Ai extends FactoryBase { * Deposits a credits for a service. * @param {string} serviceName - The name of Ainize service. * @param {number} amount - The amount of credits to deposit. - * @returns {Promise} Returns a promise that resolves with the deposit transaction info. + * @returns {Promise} A promise that resolves with the deposit transaction info. */ /* async depositCredit(serviceName: string, amount: number): Promise { diff --git a/src/ai/assistant.ts b/src/ai/assistant.ts index 69cff0ed..c5c4e696 100644 --- a/src/ai/assistant.ts +++ b/src/ai/assistant.ts @@ -58,7 +58,7 @@ export class Assistants extends FactoryBase { * @param {string} tokenId - The ID of AINFT token. * @param {AssistantCreateParams} AssistantCreateParams - The parameters to create assistant. * @param {AssistantCreateOptions} AssistantCreateOptions - The creation options. - * @returns Returns a promise that resolves with both the transaction result and the created assistant. + * @returns A promise that resolves with both the transaction result and the created assistant. */ async create( objectId: string, @@ -117,7 +117,7 @@ export class Assistants extends FactoryBase { * @param {string} tokenId - The ID of AINFT token. * @param {string} assistantId - The ID of assistant. * @param {AssistantUpdateParams} AssistantUpdateParams - The parameters to update assistant. - * @returns Returns a promise that resolves with both the transaction result and the updated assistant. + * @returns A promise that resolves with both the transaction result and the updated assistant. */ async update( objectId: string, @@ -175,7 +175,7 @@ export class Assistants extends FactoryBase { * @param {string} objectId - The ID of AINFT object. * @param {string} tokenId - The ID of AINFT token. * @param {string} assistantId - The ID of assistant. - * @returns Returns a promise that resolves with both the transaction result and the deleted assistant. + * @returns A promise that resolves with both the transaction result and the deleted assistant. */ async delete( objectId: string, @@ -220,7 +220,7 @@ export class Assistants extends FactoryBase { * @param {string} objectId - The ID of AINFT object. * @param {string} tokenId - The ID of AINFT token. * @param {string} assistantId - The ID of assistant. - * @returns Returns a promise that resolves with the assistant. + * @returns A promise that resolves with the assistant. */ async get(objectId: string, tokenId: string, assistantId: string): Promise { const appId = AinftObject.getAppId(objectId); @@ -249,18 +249,18 @@ export class Assistants extends FactoryBase { /** * Retrieves a list of assistants. * @param {string} objectId - The ID of AINFT object. - * @param {string} address - The checksum address of account. - * @param {QueryParams} QueryParams - The parameters for querying items. - * @returns Returns a promise that resolves with the list of assistants. + * @param {string} [address] - (Optional) The checksum address of account. + * @param {QueryParams} [queryParams={}] - The parameters for querying items. + * @returns A promise that resolves with the list of assistants. */ async list( objectId: string, - address: string, + address?: string | null, { limit = 20, offset = 0, order = 'desc' }: QueryParams = {} ) { await validateObject(this.ain, objectId); - const tokens = await this.getTokensByAddress(objectId, address); + const tokens = await this.getTokens(objectId, address); const assistants = this.getAssistantsFromTokens(tokens); const sorted = this.sortAssistants(assistants, order); @@ -393,12 +393,12 @@ export class Assistants extends FactoryBase { ); } - private async getTokensByAddress(objectId: string, address: string) { + private async getTokens(objectId: string, address?: string | null) { const appId = AinftObject.getAppId(objectId); const tokensPath = Path.app(appId).tokens().value(); const tokens: NftTokens = (await this.ain.db.ref(tokensPath).getValue()) || {}; return Object.entries(tokens).reduce((acc, [id, token]) => { - if (token.owner === address) { + if (!address || token.owner === address) { acc.push({ tokenId: id, ...token }); } return acc; @@ -408,13 +408,13 @@ export class Assistants extends FactoryBase { private getAssistantsFromTokens(tokens: NftToken[]) { return tokens.reduce((acc, token) => { if (token.ai) { - acc.push(this.toAssistant(token)); + acc.push(this.toAssistant(token, this.countThreads(token.ai.history))); } return acc; }, []); } - private toAssistant(data: any): Assistant { + private toAssistant(data: any, numThreads: number): Assistant { return { id: data.ai.id, tokenId: data.tokenId, @@ -425,6 +425,7 @@ export class Assistants extends FactoryBase { description: data.ai.config.description || null, metadata: data.ai.config.metadata || {}, created_at: data.ai.createdAt, + metric: { numThreads }, }; } @@ -435,4 +436,17 @@ export class Assistants extends FactoryBase { return 0; }); } + + private countThreads(items: any) { + if (typeof items !== 'object' || !items) { + return 0; + } + return Object.values(items).reduce((sum: number, item: any) => { + const count = + item.threads && typeof item.threads === 'object' + ? Object.keys(item.threads).length + : 0; + return sum + count; + }, 0); + } } diff --git a/src/ai/message.ts b/src/ai/message.ts index 10dc1990..93785df8 100644 --- a/src/ai/message.ts +++ b/src/ai/message.ts @@ -34,7 +34,7 @@ export class Messages extends FactoryBase { * @param {string} tokenId - The ID of AINFT token. * @param {string} threadId - The ID of thread. * @param {MessageCreateParams} MessageCreateParams - The parameters to create message. - * @returns Returns a promise that resolves with both the transaction result and a list including the new message. + * @returns A promise that resolves with both the transaction result and a list including the new message. */ async create( objectId: string, @@ -76,7 +76,7 @@ export class Messages extends FactoryBase { * @param {string} threadId - The ID of thread. * @param {string} messageId - The ID of message. * @param {MessageUpdateParams} MessageUpdateParams - The parameters to update message. - * @returns Returns a promise that resolves with both the transaction result and the updated message. + * @returns A promise that resolves with both the transaction result and the updated message. */ async update( objectId: string, @@ -122,7 +122,7 @@ export class Messages extends FactoryBase { * @param {string} threadId - The ID of thread. * @param {string} messageId - The ID of message. * @param {string} address - The checksum address of account. - * @returns Returns a promise that resolves with the message. + * @returns A promise that resolves with the message. */ async get( objectId: string, @@ -156,7 +156,7 @@ export class Messages extends FactoryBase { * @param {string} tokenId - The ID of AINFT token. * @param {string} threadId - The ID of thread. * @param {string} address - The checksum address of account. - * @returns Returns a promise that resolves with the list of messages. + * @returns A promise that resolves with the list of messages. */ async list( objectId: string, diff --git a/src/ai/thread.ts b/src/ai/thread.ts index 70a1140a..4fc39879 100644 --- a/src/ai/thread.ts +++ b/src/ai/thread.ts @@ -35,7 +35,7 @@ export class Threads extends FactoryBase { * @param {string} objectId - The ID of AINFT object. * @param {string} tokenId - The ID of AINFT token. * @param {ThreadCreateParams} ThreadCreateParams - The parameters to create thread. - * @returns Returns a promise that resolves with both the transaction result and the created thread. + * @returns A promise that resolves with both the transaction result and the created thread. */ async create( objectId: string, @@ -88,7 +88,7 @@ export class Threads extends FactoryBase { * @param {string} tokenId - The ID of AINFT token. * @param {string} threadId - The ID of thread. * @param {ThreadUpdateParams} ThreadUpdateParams - The parameters to update thread. - * @returns Returns a promise that resolves with both the transaction result and the updated thread. + * @returns A promise that resolves with both the transaction result and the updated thread. */ async update( objectId: string, @@ -129,7 +129,7 @@ export class Threads extends FactoryBase { * @param {string} objectId - The ID of AINFT object. * @param {string} tokenId - The ID of AINFT token. * @param {string} threadId - The ID of thread. - * @returns Returns a promise that resolves with both the transaction result and the deleted thread. + * @returns A promise that resolves with both the transaction result and the deleted thread. */ async delete( objectId: string, @@ -167,7 +167,7 @@ export class Threads extends FactoryBase { * @param {string} tokenId - The ID of AINFT token. * @param {string} threadId - The ID of thread. * @param {string} address - The checksum address of account. - * @returns Returns a promise that resolves with the thread. + * @returns A promise that resolves with the thread. */ async get(objectId: string, tokenId: string, threadId: string, address: string): Promise { await validateObject(this.ain, objectId); @@ -198,7 +198,7 @@ export class Threads extends FactoryBase { * @param {string | null} [tokenId] - The ID of AINFT token. * @param {string | null} [address] - The checksum address of account. * @param {QueryParams} QueryParams - The parameters for querying items. - * @returns Returns a promise that resolves with the list of threads. + * @returns A promise that resolves with the list of threads. */ async list( objectId: string, diff --git a/src/ainft721Object.ts b/src/ainft721Object.ts index 5a225a7c..a5fd1a79 100644 --- a/src/ainft721Object.ts +++ b/src/ainft721Object.ts @@ -17,8 +17,6 @@ export default class Ainft721Object extends FactoryBase { readonly owner: string; /** The ID of app in AIN blockchain. */ readonly appId: string; - /** The description of AINFT object. */ - readonly description?: string | null; /** The metadata of AINFT object. */ readonly metadata?: Metadata; @@ -29,7 +27,8 @@ export default class Ainft721Object extends FactoryBase { * @param objectInfo.id The ID of AINFT object. * @param objectInfo.name The name of AINFT object. * @param objectInfo.symbol The symbol of AINFT object. - * @param objectInfo.owner Owner of AINFT object. + * @param objectInfo.owner The owner of AINFT object. + * @param objectInfo.metadata The metadata of AINFT object. * @param ain Ain instance to sign and send transaction to AIN blockchain. * @param baseUrl The base url to request api to AINFT factory server. */ @@ -39,7 +38,6 @@ export default class Ainft721Object extends FactoryBase { name: string; symbol: string; owner: string; - description?: string; metadata?: Metadata; }, ain: Ain, @@ -51,7 +49,6 @@ export default class Ainft721Object extends FactoryBase { this.symbol = objectInfo.symbol; this.owner = objectInfo.owner; this.appId = Ainft721Object.getAppId(objectInfo.id); - this.description = objectInfo.description || null; this.metadata = objectInfo.metadata || {}; } diff --git a/src/nft.ts b/src/nft.ts index c0bbd73d..7eb955f1 100644 --- a/src/nft.ts +++ b/src/nft.ts @@ -41,14 +41,13 @@ export default class Nft extends FactoryBase { * }); * ``` */ - async create({ name, symbol, description, metadata }: AinftObjectCreateParams): Promise<{ txHash: string; ainftObject: Ainft721Object }> { + async create({ name, symbol, metadata }: AinftObjectCreateParams): Promise<{ txHash: string; ainftObject: Ainft721Object }> { const address = await this.ain.signer.getAddress(); const body = { address, name, symbol, - ...(description && { description }), ...(metadata && !_.isEmpty(metadata) && { metadata }), }; const trailingUrl = 'native'; diff --git a/src/types.ts b/src/types.ts index 379251ca..fa53e634 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1125,10 +1125,8 @@ export interface AinftObjectCreateParams { name: string; /** The symbol of the AINFT object. */ symbol: string; - /** The description of the AINFT object. */ - description?: string | null; /** The metadata of the AINFT object. */ - metadata?: Metadata | null; + metadata?: Metadata; } export enum ServiceType { @@ -1242,6 +1240,8 @@ export interface Assistant { * with keys limited to 64 characters and values to 512 characters. */ metadata: object | null; + /** The metric of the assistant. */ + metric?: { [key: string]: number } | null; /** The UNIX timestamp in seconds. */ created_at: number; } diff --git a/test/ai/assistant.test.ts b/test/ai/assistant.test.ts index 46dec5e5..c2ffb90e 100644 --- a/test/ai/assistant.test.ts +++ b/test/ai/assistant.test.ts @@ -50,7 +50,7 @@ describe.skip('assistant', () => { }); it('should list assistants', async () => { - const result = await ainft.assistant.list(objectId, address); + const result = await ainft.assistant.list(objectId, null); expect(result.total).toBeDefined(); expect(result.items).toBeDefined(); diff --git a/test/nft.test.ts b/test/nft.test.ts index fc1b245d..633b8603 100644 --- a/test/nft.test.ts +++ b/test/nft.test.ts @@ -2,7 +2,7 @@ import AinftJs from '../src/ainft'; import { AINFT_OBJECT_REGEX, TX_HASH_REGEX } from './constants'; import { address, privateKey } from './test_data'; -describe('nft', () => { +describe.skip('nft', () => { const ainft = new AinftJs({ privateKey, baseUrl: 'https://ainft-api-dev.ainetwork.ai', @@ -14,12 +14,12 @@ describe('nft', () => { const result = await ainft.nft.create({ name: 'name', symbol: 'symbol', - description: 'description', metadata: { author: { address: address, username: 'username', }, + description: 'description', logoImage: 'https://picsum.photos/200/200', bannerImage: 'https://picsum.photos/1400/264', externalLink: 'https://example.com', @@ -31,12 +31,12 @@ describe('nft', () => { expect(result.ainftObject.name).toBe('name'); expect(result.ainftObject.symbol).toBe('symbol'); expect(result.ainftObject.owner).toBe(address); - expect(result.ainftObject.description).toBe('description'); expect(result.ainftObject.metadata).toEqual({ author: { address: address, username: 'username', }, + description: 'description', logoImage: 'https://picsum.photos/200/200', bannerImage: 'https://picsum.photos/1400/264', externalLink: 'https://example.com',