Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 22c9d9c

Browse files
authored
Fix for luis import (#663)
1 parent 8318274 commit 22c9d9c

File tree

8 files changed

+135
-28
lines changed

8 files changed

+135
-28
lines changed

common/config/rush/pnpm-lock.yaml

Lines changed: 52 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/lu/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"dependencies": {
3737
"@azure/cognitiveservices-luis-authoring": "4.0.0-preview.1",
3838
"@azure/ms-rest-azure-js": "2.0.1",
39+
"@types/node-fetch": "~2.5.5",
3940
"antlr4": "^4.7.2",
4041
"chalk": "2.4.1",
4142
"console-stream": "^0.1.1",
@@ -46,7 +47,7 @@
4647
"globby": "^10.0.1",
4748
"intercept-stdout": "^0.1.2",
4849
"lodash": "^4.17.15",
49-
"node-fetch": "^2.1.2",
50+
"node-fetch": "~2.6.0",
5051
"semver": "^5.5.1",
5152
"tslib": "^1.10.0"
5253
},

packages/lu/src/parser/lubuild/builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ export class Builder {
309309
this.handler(`Creating LUIS.ai application: ${currentApp.name} version:${currentApp.versionId}\n`)
310310
await delay(delayDuration)
311311
const response = await luBuildCore.importApplication(currentApp)
312-
recognizer.setAppId(typeof response.body === 'string' ? response.body : response.body[Object.keys(response.body)[0]])
312+
recognizer.setAppId(typeof response === 'string' ? response : response[Object.keys(response)[0]])
313313
return true
314314
}
315315

packages/lu/src/parser/lubuild/core.ts

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,21 @@ import {isEqual, differenceWith} from 'lodash'
1010
import {CognitiveServicesCredentials} from '@azure/ms-rest-azure-js'
1111
import {LUISAuthoringClient} from '@azure/cognitiveservices-luis-authoring'
1212
import * as path from 'path'
13+
import fetch from 'node-fetch'
14+
const retCode = require('./../utils/enums/CLI-errors')
15+
const exception = require('./../utils/exception')
1316
const Content = require('./../lu/lu')
1417
const LUOptions = require('./../lu/luOptions')
1518

1619
export class LuBuildCore {
1720
private readonly client: any
21+
private readonly subscriptionKey: string
22+
private readonly endpoint: string
1823

19-
constructor(authoringKey: string, endpoint: string) {
20-
// new luis api client
21-
const creds = new CognitiveServicesCredentials(authoringKey)
24+
constructor(subscriptionKey: string, endpoint: string) {
25+
this.subscriptionKey = subscriptionKey
26+
this.endpoint = endpoint
27+
const creds = new CognitiveServicesCredentials(subscriptionKey)
2228
this.client = new LUISAuthoringClient(creds, endpoint)
2329
}
2430

@@ -35,9 +41,23 @@ export class LuBuildCore {
3541
}
3642

3743
public async importApplication(currentApp: any): Promise<any> {
38-
let response = await this.client.apps.importMethod(currentApp)
44+
// let response = await this.client.apps.importMethod(currentApp)
3945

40-
return response
46+
const name = `?appName=${currentApp.name}`
47+
const url = this.endpoint + '/luis/authoring/v3.0-preview/apps/import' + name
48+
const headers = {
49+
'Content-Type': 'application/json',
50+
'Ocp-Apim-Subscription-Key': this.subscriptionKey
51+
}
52+
53+
const response = await fetch(url, {method: 'POST', headers, body: JSON.stringify(currentApp)})
54+
const messageData = await response.json()
55+
56+
if (messageData.error) {
57+
throw (new exception(retCode.errorCode.LUIS_API_CALL_FAILED, messageData.error.message))
58+
}
59+
60+
return messageData
4161
}
4262

4363
public async exportApplication(appId: string, versionId: string) {
@@ -91,7 +111,23 @@ export class LuBuildCore {
91111
}
92112

93113
public async importNewVersion(appId: string, app: any, options: any) {
94-
await this.client.versions.importMethod(appId, app, options)
114+
// await this.client.versions.importMethod(appId, app, options)
115+
116+
const versionId = `?versionId=${options.versionId}`
117+
let url = this.endpoint + '/luis/authoring/v3.0-preview/apps/' + appId + '/versions/import' + versionId
118+
const headers = {
119+
'Content-Type': 'application/json',
120+
'Ocp-Apim-Subscription-Key': this.subscriptionKey
121+
}
122+
123+
const response = await fetch(url, {method: 'POST', headers, body: JSON.stringify(app)})
124+
const messageData = await response.json()
125+
126+
if (messageData.error) {
127+
throw (new exception(retCode.errorCode.LUIS_API_CALL_FAILED, messageData.error.message))
128+
}
129+
130+
return messageData
95131
}
96132

97133
public async listApplicationVersions(appId: string) {

packages/lu/src/parser/utils/enums/CLI-errors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports = {
2828
INVALID_URI: 21,
2929
INVALID_REGEX_ENTITY: 22,
3030
INVALID_COMPOSITE_ENTITY: 23,
31+
LUIS_API_CALL_FAILED: 24,
3132
UNKNOWN_ERROR: 99,
3233
BOUNDARY_INTENTS: 501,
3334
BOUNDARY_PATTERNANYENTITY: 502,

packages/luis/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@
4646
"@oclif/command": "~1.5.19",
4747
"@oclif/config": "~1.13.3",
4848
"@oclif/errors": "~1.2.2",
49+
"@types/node-fetch": "~2.5.5",
4950
"@types/sinon": "^7.5.0",
5051
"cli-ux": "~5.3.3",
5152
"fs-extra": "^8.1.0",
5253
"lodash": "^4.17.15",
54+
"node-fetch": "~2.6.0",
5355
"tslib": "^1.10.0",
5456
"username": "^4.1.0"
5557
},

packages/luis/src/commands/luis/application/import.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import {CLIError, Command, flags} from '@microsoft/bf-cli-command'
7+
import fetch from 'node-fetch'
78

89
const utils = require('../../../utils/index')
910

@@ -41,15 +42,28 @@ export default class LuisApplicationImport extends Command {
4142
const appJSON = stdin ? stdin : await utils.getInputFromFile(inVal)
4243
if (!appJSON) throw new CLIError('No import data found - please provide input through stdin or the --in flag')
4344

44-
const client = utils.getLUISClient(subscriptionKey, endpoint)
45+
// const client = utils.getLUISClient(subscriptionKey, endpoint)
4546

4647
const options: any = {}
4748
if (name) options.appName = name
4849

4950
try {
50-
const response = await client.apps.importMethod(JSON.parse(appJSON), options)
51+
// const response = await client.apps.importMethod(JSON.parse(appJSON), options)
5152

52-
const output: string = flags.json ? JSON.stringify({Status: 'Success', id: response.body}, null, 2) : `App successfully imported with id ${response.body}.`
53+
name = name ? '?appName=' + name : ''
54+
let url = endpoint + '/luis/authoring/v3.0-preview/apps/import' + name
55+
const headers = {
56+
'Content-Type': 'application/json',
57+
'Ocp-Apim-Subscription-Key': subscriptionKey
58+
}
59+
const response = await fetch(url, {method: 'POST', headers, body: appJSON})
60+
const messageData = await response.json()
61+
62+
if (messageData.error) {
63+
throw new CLIError(messageData.error.message)
64+
}
65+
66+
const output: string = flags.json ? JSON.stringify({Status: 'Success', id: messageData}, null, 2) : `App successfully imported with id ${messageData}.`
5367
this.log(output)
5468

5569
if (flags.save) {

0 commit comments

Comments
 (0)