UNPKG

@intuitionrobotics/google-services

Version:
122 lines 6.85 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DialogFlowApi = void 0; const ts_common_1 = require("@intuitionrobotics/ts-common"); const googleapis_1 = require("googleapis"); const AuthModule_1 = require("./AuthModule"); const consts_1 = require("./consts"); class DialogFlowApi extends ts_common_1.Logger { constructor(authKey) { super(); this.agent = { create: (agentProjectId, name) => __awaiter(this, void 0, void 0, function* () { 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" }; yield this.dialogFlowApi.projects.setAgent({ parent: `projects/${agentProjectId}`, requestBody: newTestAgent }); this.logInfo(`Created agent for project ${agentProjectId} with name: ${name}`); }), train: (agentProjectId) => __awaiter(this, void 0, void 0, function* () { this.logInfo(`Train ${agentProjectId}`); return (yield this.dialogFlowApi.projects.agent.train({ parent: `projects/${agentProjectId}` })).data; }), export: (agentProjectId) => __awaiter(this, void 0, void 0, function* () { var _a; this.logInfo(`Exporting ${agentProjectId}`); return (_a = (yield this.dialogFlowApi.projects.agent.export({ parent: `projects/${agentProjectId}` })).data.response) === null || _a === void 0 ? void 0 : _a.agentContent; }), restore: (agentProjectId, agentContent) => __awaiter(this, void 0, void 0, function* () { this.logInfo(`Restoring ${agentProjectId}`); return (yield this.dialogFlowApi.projects.agent.restore({ parent: `projects/${agentProjectId}`, requestBody: { agentContent } })).data; }), import: (agentProjectId, agentContent) => __awaiter(this, void 0, void 0, function* () { this.logInfo(`Importing ${agentProjectId}`); return (yield this.dialogFlowApi.projects.agent.import({ parent: `projects/${agentProjectId}`, requestBody: { agentContent } })).data; }), copy: (fromAgent, toAgent) => __awaiter(this, void 0, void 0, function* () { this.logInfo(`Merging agent ${fromAgent} => ${toAgent}`); const content = yield this.agent.export(fromAgent); if (content) yield this.agent.import(toAgent, content); }), override: (fromAgent, toAgent) => __awaiter(this, void 0, void 0, function* () { this.logInfo(`Overriding agent ${fromAgent} => ${toAgent}`); const content = yield this.agent.export(fromAgent); if (content) yield this.agent.restore(toAgent, content); }) }; this.intent = { list: (agentProjectId) => __awaiter(this, void 0, void 0, function* () { this.logInfo(`List intents of ${agentProjectId}`); const intentList = []; let counter = 1; let pageToken = undefined; do { const response = yield 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: (agentProjectId, entityId, entityList) => __awaiter(this, void 0, void 0, function* () { const request = { parent: entityId, requestBody: { entities: entityList } }; return (yield this.dialogFlowApi.projects.agent.entityTypes.entities.batchCreate(request)).data; }), createEntityType: (agentProjectId, entityName) => __awaiter(this, void 0, void 0, function* () { var _a, _b; const pathString = `projects/${agentProjectId}/agent`; const entityTypeList = yield 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 (yield this.dialogFlowApi.projects.agent.entityTypes.create(request)).data.name; }), }; this.dialogFlowApi = new googleapis_1.dialogflow_v2.Dialogflow(AuthModule_1.AuthModule.getAuth(authKey, [consts_1.GCPScope.CloudPlatform])); } } exports.DialogFlowApi = DialogFlowApi; //# sourceMappingURL=DialogFlowApi.js.map