UNPKG

@nu-art/google-services

Version:
113 lines (112 loc) 5.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ModuleBE_DialogFlow = void 0; const ts_common_1 = require("@nu-art/ts-common"); const googleapis_1 = require("googleapis"); const ModuleBE_Auth_1 = require("./ModuleBE_Auth"); const consts_1 = require("./consts"); class ModuleBE_DialogFlow extends ts_common_1.Logger { constructor(authKey) { super(); this.agent = { create: async (agentProjectId, name) => { this.logInfo(`Creating agent for project ${agentProjectId}`); const newTestAgent = { "parent": `projects/${agentProjectId}`, "displayName": name, "defaultLanguageCode": "en", "timeZone": "America/New_York", "enableLogging": true, "matchMode": "MATCH_MODE_HYBRID", "classificationThreshold": 0.7, "apiVersion": "API_VERSION_V2", "tier": "TIER_STANDARD" }; await this.dialogFlowApi.projects.setAgent({ parent: `projects/${agentProjectId}`, requestBody: newTestAgent }); this.logInfo(`Created agent for project ${agentProjectId} with name: ${name}`); }, train: async (agentProjectId) => { this.logInfo(`Train ${agentProjectId}`); return (await this.dialogFlowApi.projects.agent.train({ parent: `projects/${agentProjectId}` })).data; }, export: async (agentProjectId) => { var _a; this.logInfo(`Exporting ${agentProjectId}`); return (_a = (await this.dialogFlowApi.projects.agent.export({ parent: `projects/${agentProjectId}` })).data.response) === null || _a === void 0 ? void 0 : _a.agentContent; }, restore: async (agentProjectId, agentContent) => { this.logInfo(`Restoring ${agentProjectId}`); return (await this.dialogFlowApi.projects.agent.restore({ parent: `projects/${agentProjectId}`, requestBody: { agentContent } })).data; }, import: async (agentProjectId, agentContent) => { this.logInfo(`Importing ${agentProjectId}`); return (await this.dialogFlowApi.projects.agent.import({ parent: `projects/${agentProjectId}`, requestBody: { agentContent } })).data; }, copy: async (fromAgent, toAgent) => { this.logInfo(`Merging agent ${fromAgent} => ${toAgent}`); const content = await this.agent.export(fromAgent); if (content) await this.agent.import(toAgent, content); }, override: async (fromAgent, toAgent) => { this.logInfo(`Overriding agent ${fromAgent} => ${toAgent}`); const content = await this.agent.export(fromAgent); if (content) await this.agent.restore(toAgent, content); } }; this.intent = { list: async (agentProjectId) => { this.logInfo(`List intents of ${agentProjectId}`); const intentList = []; let counter = 1; let pageToken = undefined; do { const response = await this.dialogFlowApi.projects.agent.intents.list({ parent: `projects/${agentProjectId}/agent`, pageToken, pageSize: 1000 }); if (!response.data.intents) break; pageToken = response.data.nextPageToken || undefined; intentList.push(...response.data.intents); counter++; if (counter > 10) throw new ts_common_1.ThisShouldNotHappenException('Too many calls to DialogFlow API'); } while (pageToken); return intentList; }, }; this.entity = { createEntities: async (agentProjectId, entityId, entityList) => { const request = { parent: entityId, requestBody: { entities: entityList } }; return (await this.dialogFlowApi.projects.agent.entityTypes.entities.batchCreate(request)).data; }, createEntityType: async (agentProjectId, entityName) => { var _a, _b; const pathString = `projects/${agentProjectId}/agent`; const entityTypeList = await this.dialogFlowApi.projects.agent.entityTypes.list({ parent: pathString }); const foundType = (_b = (_a = entityTypeList.data) === null || _a === void 0 ? void 0 : _a.entityTypes) === null || _b === void 0 ? void 0 : _b.find(entityType => entityType.displayName === entityName); if (foundType) return foundType.name; const request = { parent: pathString, requestBody: { "displayName": `${entityName}`, "enableFuzzyExtraction": false, "kind": "KIND_MAP", } }; return (await this.dialogFlowApi.projects.agent.entityTypes.create(request)).data.name; }, }; this.dialogFlowApi = new googleapis_1.dialogflow_v2.Dialogflow(ModuleBE_Auth_1.ModuleBE_Auth.getAuth(authKey, [consts_1.GCPScope.CloudPlatform])); } } exports.ModuleBE_DialogFlow = ModuleBE_DialogFlow;