Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add user agent header to LUIS #677

Merged
merged 3 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});