Skip to content
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

[feat] add assistant metric #303

Merged
merged 4 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions src/ai/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AiConfigurationTransactionResult>} Returns a promise that resolves with both the transaction result and the configuration info.
* @returns {Promise<AiConfigurationTransactionResult>} A promise that resolves with both the transaction result and the configuration info.
*/
async configure(
objectId: string,
Expand All @@ -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<number|null>} Returns a promise that resolves with the credit balance.
* @returns {Promise<number|null>} A promise that resolves with the credit balance.
*/
async getCredit(serviceName: string): Promise<number> {
const address = await this.ain.signer.getAddress();
Expand Down Expand Up @@ -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<CreditTransactionResult>} Returns a promise that resolves with the deposit transaction info.
* @returns {Promise<CreditTransactionResult>} A promise that resolves with the deposit transaction info.
*/
/*
async depositCredit(serviceName: string, amount: number): Promise<CreditTransactionResult> {
Expand Down
36 changes: 23 additions & 13 deletions src/ai/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<Assistant> {
const appId = AinftObject.getAppId(objectId);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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<NftToken[]>((acc, [id, token]) => {
if (token.owner === address) {
if (!address || token.owner === address) {
acc.push({ tokenId: id, ...token });
}
return acc;
Expand All @@ -408,13 +408,13 @@ export class Assistants extends FactoryBase {
private getAssistantsFromTokens(tokens: NftToken[]) {
return tokens.reduce<Assistant[]>((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,
Expand All @@ -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 },
};
}

Expand All @@ -435,4 +436,13 @@ export class Assistants extends FactoryBase {
return 0;
});
}

private countThreads(items: any) {
if (typeof items === 'boolean' || !items) {
jiyoung-an marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}
return _.sumBy(_.values(items), (item) => {
jiyoung-an marked this conversation as resolved.
Show resolved Hide resolved
return _.isObject(item.threads) ? _.keys(item.threads).length : 0;
});
}
}
8 changes: 4 additions & 4 deletions src/ai/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions src/ai/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<Thread> {
await validateObject(this.ain, objectId);
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 2 additions & 5 deletions src/ainft721Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
*/
Expand All @@ -39,7 +38,6 @@ export default class Ainft721Object extends FactoryBase {
name: string;
symbol: string;
owner: string;
description?: string;
metadata?: Metadata;
},
ain: Ain,
Expand All @@ -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 || {};
}

Expand Down
3 changes: 1 addition & 2 deletions src/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion test/ai/assistant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions test/nft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down
Loading