UNPKG

@intuitionrobotics/google-services

Version:
108 lines 5.02 kB
import { Logger, ThisShouldNotHappenException } from "@intuitionrobotics/ts-common"; import { dialogflow_v2 } from "googleapis"; import { AuthModule } from "./AuthModule.js"; import { GCPScope } from "./consts.js"; export class DialogFlowApi extends Logger { dialogFlowApi; constructor(authKey) { super(); this.dialogFlowApi = new dialogflow_v2.Dialogflow(AuthModule.getAuth(authKey, [GCPScope.CloudPlatform])); } 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) => { this.logInfo(`Exporting ${agentProjectId}`); return (await this.dialogFlowApi.projects.agent.export({ parent: `projects/${agentProjectId}` })).data.response?.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); } }; 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 ThisShouldNotHappenException('Too many calls to DialogFlow API'); } while (pageToken); return intentList; }, }; 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) => { const pathString = `projects/${agentProjectId}/agent`; const entityTypeList = await this.dialogFlowApi.projects.agent.entityTypes.list({ parent: pathString }); const foundType = entityTypeList.data?.entityTypes?.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; }, }; } //# sourceMappingURL=DialogFlowApi.js.map