Skip to content

Commit

Permalink
Merge branch 'develop' into docs/yoojin/typedocs
Browse files Browse the repository at this point in the history
  • Loading branch information
yoojinko authored Sep 21, 2023
2 parents 852e999 + 40ef701 commit 3696f4b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 30 deletions.
34 changes: 21 additions & 13 deletions src/ainize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,29 @@ export default class Ainize {
* @param {deployConfig} deployConfig Set configuration for container. modelName, billingConfig, etc.
* @returns {Model} Control object of deployed model.
*/
// TODO(yoojin, woojae): Deploy container, advanced.
async deploy({modelName, billingConfig, serviceUrl}: deployConfig): Promise<Model> {
// TODO(yoojin, woojae): Deploy container, advanced.
const deployer = this.ain.getAddress();
if (!billingConfig) {
billingConfig = {
...DEFAULT_BILLING_CONFIG,
depositAddress: deployer,
};
if(!this.ain.isDefaultAccountExist()) {
throw new Error('you should login first');
}
// NOTE(yoojin): For test. We make fixed url on service.
if (!serviceUrl) {
serviceUrl = `https://${modelName}.ainetwork.xyz`;
}

await this.appController.createApp({ appName: modelName, serviceUrl, billingConfig });
// TODO(yoojin, woojae): Add container deploy logic.
const result = await new Promise(async (resolve, reject) => {
const deployer = this.ain.getAddress();
if (!billingConfig) {
billingConfig = {
...DEFAULT_BILLING_CONFIG,
depositAddress: deployer,
};
}
// NOTE(yoojin): For test. We make fixed url on service.
if (!serviceUrl) {
serviceUrl = `https://${modelName}.ainetwork.xyz`;
}
const modelPath = Path.app(modelName).status();
await this.handler.subscribe(modelPath, resolve);
await this.appController.createApp({ appName: modelName, serviceUrl, billingConfig });
});
console.log(`${modelName} deploy success!`);
return this.model(modelName);
}

Expand Down
37 changes: 24 additions & 13 deletions src/controllers/appController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,30 @@ export default class AppController {
setRuleOps.push(ruleOp);
}

const depositParam = this.depositTriggerFunctionConfig(appName, serviceUrl);
const value = this.buildSetFunctionValue(depositParam);
const funcOp = buildSetOperation("SET_FUNCTION", depositParam.ref, value);
setFunctionOps.push(funcOp);
const depositPath = `${Path.app(appName).depositOfUser("$userAddress")}/$transferKey`
const depositUrl = `${serviceUrl}/deposit`;
const depositParam: setTriggerFunctionParm = {
ref: depositPath,
function_id: "deposit-trigger",
function_type: "REST",
function_url: depositUrl
}
const depositValue = this.buildSetFunctionValue(depositParam);
const depositFuncOp = buildSetOperation("SET_FUNCTION", depositParam.ref, depositValue);
setFunctionOps.push(depositFuncOp);

const serviceFuncPath = Path.app(appName).request("$userAddress", "$requestKey")
const serviceFuncUrl = `${serviceUrl}/service`;
const serviceFuncParam: setTriggerFunctionParm = {
ref: serviceFuncPath,
function_id: "service-trigger",
function_type: "REST",
function_url: serviceFuncUrl
}
const serviceFuncValue = this.buildSetFunctionValue(serviceFuncParam);
const serviceFuncOp = buildSetOperation("SET_FUNCTION", serviceFuncParam.ref, serviceFuncValue);
setFunctionOps.push(serviceFuncOp);

const configOp = this.buildSetAppBillingConfigOp(appName, billingConfig);
setBillingConfigOps.push(configOp);

Expand Down Expand Up @@ -169,13 +189,4 @@ export default class AppController {
const value = !isRemoveOp ? null : true;
return buildSetOperation("SET_VALUE", path, value);
}

private depositTriggerFunctionConfig = (appName: string, serviceUrl: string): setTriggerFunctionParm => {
return {
ref: `${Path.app(appName).depositOfUser("$userAddress")}/$transferKey`,
function_type: "REST",
function_id: "deposit-trigger",
function_url: `${serviceUrl}/deposit`,
}
}
}
3 changes: 2 additions & 1 deletion src/controllers/modelController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ export default class ModelController {
const result = await new Promise(async (resolve, reject) => {
const requestKey = Date.now();
const requesterAddress = this.ain.getAddress();
await this.handler.subscribe(requesterAddress, requestKey.toString(), modelName, resolve);
const responsePath = Path.app(modelName).response(requesterAddress, requestKey.toString());
await this.handler.subscribe(responsePath, resolve);
const requestPath = Path.app(modelName).request(requesterAddress, requestKey);
const requestOp = buildSetOperation("SET_VALUE", requestPath, {prompt: requestData});
const txBody = buildTxBody(requestOp);
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ export default class Handler {
}
}

async subscribe(requester:string, recordId:string, appName: string, resolve: any) {
async subscribe(subscribePath: string, resolve: any) {
this.checkEventManager();
const responsePath = Path.app(appName).response(requester, recordId);

const subscribeId = await this.ain.getEventManager().subscribe(
"VALUE_CHANGED",
{
path: responsePath,
path: subscribePath,
event_source: "USER",
},
(valueChangedEvent: any) => {
Expand Down

0 comments on commit 3696f4b

Please sign in to comment.