diff --git a/README.md b/README.md index 3d7d0a8..8373db3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# ainize-sdk +# ainize-js -A Typescript SDK for the Ainize, a system for running AI services on the AI Network. +A Typescript JS for the Ainize, a system for running AI services on the AI Network. ## Requirements node >= 16 @@ -8,14 +8,14 @@ node >= 16 ## usage ### Install ```bash -npm install @ainize-team/ainize-sdk +npm install @ainize-team/ainize-js -yarn install @ainize-team/ainize-sdk +yarn install @ainize-team/ainize-js ``` ### Import ```typescript -import Ainize from '@ainize-team/ainize-sdk' +import Ainize from '@ainize-team/ainize-js' const ainize = new Ainize(); ``` @@ -49,9 +49,9 @@ service.run(); ``` ### Using Service -You can use a service using `ainize.service()`. +You can use a service using `ainize.getService()`. ```typescript -const service = await ainize.service(); +const service = await ainize.getService(); ``` You should deposit AIN to credit before using service. @@ -62,5 +62,5 @@ const balance = await service.getCreditBalance(); If you have enough credit, you can use the service. ```typescript -const result = await service.use(); +const result = await service.request(); ``` diff --git a/package.json b/package.json index 124a460..bb1727b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "@ainize-team/ainize-sdk", - "version": "1.0.3", + "name": "@ainize-team/ainize-js", + "version": "1.0.4", "main": "dist/ainize.js", "types": "dist/ainize.d.ts", "scripts": { @@ -14,7 +14,7 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/ainize-team/ainize-sdk.git" + "url": "git+https://github.com/ainize-team/ainize-js.git" }, "keywords": [ "ainize", diff --git a/src/constants.ts b/src/constants.ts index 9921d87..7552df4 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -31,14 +31,6 @@ export const Path = { export const defaultAppRules = (appName: string): { [type: string]: { ref: string, value: object } } => { const rootRef = Path.app(appName).root(); return { - root: { - ref: rootRef, - value: { - ".rule": { - write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true" - }, - }, - }, deposit: { ref: `${Path.app(appName).depositOfUser("$userAddress")}/$transferKey`, value: { @@ -56,7 +48,7 @@ export const defaultAppRules = (appName: string): { [type: string]: { ref: strin }, }, balanceHistory: { - ref: `${rootRef}/balance/$userAddress/history/$timestamp_and_type`, + ref: `${rootRef}/balance/$userAddress/history/$timestamp`, value: { ".rule": { write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true && util.isDict(newData) && util.isNumber(newData.amount) && (newData.type === 'DEPOSIT' || newData.type === 'USAGE')" @@ -68,7 +60,8 @@ export const defaultAppRules = (appName: string): { [type: string]: { ref: strin value: { ".rule": { write: - "auth.addr === $userAddress && getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) !== null && " + + "auth.addr === $userAddress && " + + "(getValue(`/apps/" + `${appName}` + "/billingConfig/minCost`) === 0 || " + "(getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) >= getValue(`/apps/" + `${appName}` + "/billingConfig/minCost`))" }, }, @@ -85,9 +78,12 @@ export const defaultAppRules = (appName: string): { [type: string]: { ref: strin ref: Path.app(appName).billingConfig(), value: { ".rule": { - write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true && util.isDict(newData) && util.isString(newData.depositAddress) && " + - "util.isDict(newData) && util.isNumber(newData.costPerToken) && util.isNumber(newData.minCost) && " + - "util.isEmpty(newData.maxCost) || (util.isNumber(newData.maxCost) && newData.maxCost >= newData.minCost)", + write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true && " + + "util.isDict(newData) && " + + "util.isString(newData.depositAddress) && " + + "util.isNumber(newData.costPerToken) && " + + "util.isNumber(newData.minCost) && newData.minCost >= 0 &&" + + "(util.isEmpty(newData.maxCost) || (util.isNumber(newData.maxCost) && newData.maxCost >= newData.minCost))", }, }, }, diff --git a/src/controllers/serviceController.ts b/src/controllers/serviceController.ts index a21c0d0..f5bbfbe 100644 --- a/src/controllers/serviceController.ts +++ b/src/controllers/serviceController.ts @@ -34,7 +34,8 @@ export default class ServiceController { return await 'information of service'; } - async calculateCost(serviceName: string, requestData: string): Promise { + // FIXME(yoojin): Temporary deprecated. Need new pricing rules. + private async calculateCost(serviceName: string, requestData: string): Promise { const billingConfig = await this.ain.getValue(Path.app(serviceName).billingConfig()); const token = requestData.split(' ').length; let cost = token * billingConfig.costPerToken; @@ -76,7 +77,7 @@ export default class ServiceController { return await this.ain.getValue(creditHistoryPath) as creditHistories; } - async request(serviceName: string, requestData: string) : Promise { + async request(serviceName: string, requestData: any) : Promise { this.checkRunning(serviceName); const result = await new Promise(async (resolve, reject) => { const requestKey = Date.now(); diff --git a/src/service.ts b/src/service.ts index 3a21d6e..24239da 100644 --- a/src/service.ts +++ b/src/service.ts @@ -31,7 +31,9 @@ export default class Service { * @returns {number} Estimated cost. */ async calculateCost (requestData: string) { - return await this.serviceController.calculateCost(this.serviceName, requestData); + // FIXME(yoojin): Temporary deprecated. Need new pricing rules. + // return await this.serviceController.calculateCost(this.serviceName, requestData); + return 0; } /** @@ -77,7 +79,7 @@ export default class Service { * @param {string} requestData String data for request to service. * @returns {string} Response data from service. */ - async request(requestData: string) { + async request(requestData: any) { this.isLoggedIn(); return await this.serviceController.request(this.serviceName, requestData); }