Skip to content

Commit

Permalink
Merge pull request #677 from Microsoft/johtaylo/user-agent
Browse files Browse the repository at this point in the history
add user agent header to LUIS
  • Loading branch information
johnataylor authored Dec 5, 2018
2 parents 1e6391a + a974e61 commit dd9afb6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
20 changes: 19 additions & 1 deletion libraries/botbuilder-ai/src/luisRecognizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import { LUISRuntimeClient as LuisClient, LUISRuntimeModels as LuisModels } from 'azure-cognitiveservices-luis-runtime';
import { RecognizerResult, TurnContext } from 'botbuilder';
import * as msRest from 'ms-rest';
import * as os from 'os';

const pjson = require('../package.json');

const LUIS_TRACE_TYPE: string = 'https://www.luis.ai/schemas/trace';
const LUIS_TRACE_NAME: string = 'LuisRecognizer';
Expand Down Expand Up @@ -198,7 +201,10 @@ export class LuisRecognizer {
this.application.applicationId, utterance,
{
verbose: this.options.includeAllIntents,
customHeaders: { 'Ocp-Apim-Subscription-Key': this.application.endpointKey },
customHeaders: {
'Ocp-Apim-Subscription-Key': this.application.endpointKey,
'User-Agent': this.getUserAgent()
},
...this.options
}
)
Expand Down Expand Up @@ -233,6 +239,18 @@ export class LuisRecognizer {
return Promise.resolve(cached);
}

private getUserAgent() : string {

// Note when the ms-rest dependency the LuisClient uses has been updated
// this code should be modified to use the client's addUserAgentInfo() function.

const packageUserAgent = `${pjson.name}/${pjson.version}`;
const platformUserAgent = `(${os.arch()}-${os.type()}-${os.release()}; Node.js,Version=${process.version})`;
const userAgent = `${packageUserAgent} ${platformUserAgent}`;

return userAgent;
}

private emitTraceInfo(context: TurnContext, luisResult: LuisModels.LuisResult, recognizerResult: RecognizerResult): Promise<any> {
const traceInfo: LuisTraceInfo = {
recognizerResult: recognizerResult,
Expand Down
14 changes: 14 additions & 0 deletions libraries/botbuilder-ai/tests/luisRecognizer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,18 @@ describe('LuisRecognizer', function () {
done();
})
});

it('should send user-agent header.', done => {
nock.cleanAll();
nock('https://westus.api.cognitive.microsoft.com')
.matchHeader('User-Agent', /botbuilder-ai\/4.*/)
.post(/apps/)
.reply(200, { query: null, intents: [], entities: [] });
const recognizer = new LuisRecognizer({ applicationId: luisAppId, endpointKey: endpointKey }, { includeAllIntents: true }, true);
const context = new TestContext({ text: 'Hello world!' });
recognizer.recognize(context).then(res => {
nock.cleanAll();
done();
});
});
});

0 comments on commit dd9afb6

Please sign in to comment.