Skip to content

Commit d0eec7b

Browse files
xiaozhenliu-gg5alexander-fenster
authored andcommitted
feat!: regenerate library based on dialogflow/v2 proto changes (#569)
* sample-test should fail * fix resource name * lint * test * update copyright * pick up v2/ proto change * fix sample-test for proto change * new resource name * test * test * test * test * proper resource path * all green now * fix & gts lint * remove createDocument test * lint Co-authored-by: Alexander Fenster <[email protected]>
1 parent 7f3f4ca commit d0eec7b

13 files changed

+171
-135
lines changed

dialogflow/detect-intent-TTS-response.v2.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function main(
2222
) {
2323
// [START dialogflow_detect_intent_with_texttospeech_response]
2424
// Imports the Dialogflow client library
25-
const dialogflow = require('dialogflow').v2;
25+
const dialogflow = require('@google-cloud/dialogflow').v2;
2626

2727
// Instantiate a DialogFlow client.
2828
const sessionClient = new dialogflow.SessionsClient();
@@ -37,7 +37,10 @@ async function main(
3737
// const outputFile = `path for audio output file, e.g. ./resources/myOutput.wav`;
3838

3939
// Define session path
40-
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
40+
const sessionPath = sessionClient.projectAgentSessionPath(
41+
projectId,
42+
sessionId
43+
);
4144
const fs = require('fs');
4245
const util = require('util');
4346

dialogflow/detect-intent-sentiment.v2.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async function main(
2121
) {
2222
// [START dialogflow_detect_intent_with_sentiment_analysis]
2323
// Imports the Dialogflow client library
24-
const dialogflow = require('dialogflow').v2;
24+
const dialogflow = require('@google-cloud/dialogflow').v2;
2525

2626
// Instantiate a DialogFlow client.
2727
const sessionClient = new dialogflow.SessionsClient();
@@ -35,7 +35,10 @@ async function main(
3535
// const languageCode = 'BCP-47 language code, e.g. en-US';
3636

3737
// Define session path
38-
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
38+
const sessionPath = sessionClient.projectAgentSessionPath(
39+
projectId,
40+
sessionId
41+
);
3942

4043
async function detectIntentandSentiment() {
4144
// The text query request.

dialogflow/detect.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function detectTextIntent(projectId, sessionId, queries, languageCode) {
3434
// const languageCode = 'en';
3535

3636
// Imports the Dialogflow library
37-
const dialogflow = require('dialogflow');
37+
const dialogflow = require('@google-cloud/dialogflow');
3838

3939
// Instantiates a session client
4040
const sessionClient = new dialogflow.SessionsClient();
@@ -47,7 +47,10 @@ function detectTextIntent(projectId, sessionId, queries, languageCode) {
4747
languageCode
4848
) {
4949
// The path to identify the agent that owns the created intent.
50-
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
50+
const sessionPath = sessionClient.projectAgentSessionPath(
51+
projectId,
52+
sessionId
53+
);
5154

5255
// The text query request.
5356
const request = {
@@ -108,13 +111,16 @@ async function detectEventIntent(
108111
const {struct} = require('pb-util');
109112

110113
// Imports the Dialogflow library
111-
const dialogflow = require('dialogflow');
114+
const dialogflow = require('@google-cloud/dialogflow');
112115

113116
// Instantiates a session client
114117
const sessionClient = new dialogflow.SessionsClient();
115118

116119
// The path to identify the agent that owns the created intent.
117-
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
120+
const sessionPath = sessionClient.projectAgentSessionPath(
121+
projectId,
122+
sessionId
123+
);
118124

119125
// The text query request.
120126
const request = {
@@ -146,7 +152,9 @@ async function detectEventIntent(
146152
if (result.outputContexts && result.outputContexts.length) {
147153
console.log(' Output contexts:');
148154
result.outputContexts.forEach(context => {
149-
const contextId = contextClient.matchContextFromContextName(context.name);
155+
const contextId = contextClient.matchContextFromProjectAgentSessionContextName(
156+
context.name
157+
);
150158
const contextParameters = JSON.stringify(
151159
struct.decode(context.parameters)
152160
);
@@ -170,13 +178,16 @@ async function detectAudioIntent(
170178
const util = require('util');
171179
const {struct} = require('pb-util');
172180
// Imports the Dialogflow library
173-
const dialogflow = require('dialogflow');
181+
const dialogflow = require('@google-cloud/dialogflow');
174182

175183
// Instantiates a session client
176184
const sessionClient = new dialogflow.SessionsClient();
177185

178186
// The path to identify the agent that owns the created intent.
179-
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
187+
const sessionPath = sessionClient.projectAgentSessionPath(
188+
projectId,
189+
sessionId
190+
);
180191

181192
// Read the content of the audio file and send it as part of the request.
182193
const readFile = util.promisify(fs.readFile);
@@ -213,7 +224,9 @@ async function detectAudioIntent(
213224
if (result.outputContexts && result.outputContexts.length) {
214225
console.log(' Output contexts:');
215226
result.outputContexts.forEach(context => {
216-
const contextId = contextClient.matchContextFromContextName(context.name);
227+
const contextId = contextClient.matchContextFromProjectAgentSessionContextName(
228+
context.name
229+
);
217230
const contextParameters = JSON.stringify(
218231
struct.decode(context.parameters)
219232
);
@@ -241,7 +254,7 @@ async function streamingDetectIntent(
241254

242255
const pump = util.promisify(pipeline);
243256
// Imports the Dialogflow library
244-
const dialogflow = require('dialogflow');
257+
const dialogflow = require('@google-cloud/dialogflow');
245258

246259
// Instantiates a session client
247260
const sessionClient = new dialogflow.SessionsClient();
@@ -257,7 +270,10 @@ async function streamingDetectIntent(
257270

258271
// The BCP-47 language code to use, e.g. 'en-US'
259272
// const languageCode = 'en-US';
260-
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
273+
const sessionPath = sessionClient.projectAgentSessionPath(
274+
projectId,
275+
sessionId
276+
);
261277

262278
const initialStreamRequest = {
263279
session: sessionPath,
@@ -299,7 +315,7 @@ async function streamingDetectIntent(
299315
if (result.outputContexts && result.outputContexts.length) {
300316
console.log(' Output contexts:');
301317
result.outputContexts.forEach(context => {
302-
const contextId = contextClient.matchContextFromContextName(
318+
const contextId = contextClient.matchContextFromProjectAgentSessionContextName(
303319
context.name
304320
);
305321
const contextParameters = JSON.stringify(

dialogflow/detect.v2beta1.js

Lines changed: 89 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const util = require('util');
2020
async function createKnowledgeBase(projectId, displayName) {
2121
// [START dialogflow_create_knowledge_base]
2222
// Imports the Dialogflow client library
23-
const dialogflow = require('dialogflow').v2beta1;
23+
const dialogflow = require('@google-cloud/dialogflow').v2beta1;
2424

2525
// Instantiate a DialogFlow client.
2626
const client = new dialogflow.KnowledgeBasesClient();
@@ -31,7 +31,7 @@ async function createKnowledgeBase(projectId, displayName) {
3131
// const projectId = 'ID of GCP project associated with your Dialogflow agent';
3232
// const displayName = `your knowledge base display name, e.g. myKnowledgeBase`;
3333

34-
const formattedParent = client.projectPath(projectId);
34+
const formattedParent = 'projects/' + projectId;
3535
const knowledgeBase = {
3636
displayName: displayName,
3737
};
@@ -47,55 +47,59 @@ async function createKnowledgeBase(projectId, displayName) {
4747
// [END dialogflow_create_knowledge_base]
4848
}
4949

50-
async function createDocument(
51-
projectId,
52-
knowledgeBaseFullName,
53-
documentPath,
54-
documentName,
55-
knowledgeTypes,
56-
mimeType
57-
) {
58-
// [START dialogflow_create_document]
59-
// Imports the Dialogflow client library
60-
const dialogflow = require('dialogflow').v2beta1;
61-
62-
// Instantiate a DialogFlow Documents client.
63-
const client = new dialogflow.DocumentsClient({
64-
projectId: projectId,
65-
});
66-
67-
/**
68-
* TODO(developer): Uncomment the following lines before running the sample.
69-
*/
70-
// const projectId = 'ID of GCP project associated with your Dialogflow agent';
71-
// const knowledgeBaseFullName = `the full path of your knowledge base, e.g my-Gcloud-project/myKnowledgeBase`;
72-
// const documentPath = `path of the document you'd like to add, e.g. https://dialogflow.com/docs/knowledge-connectors`;
73-
// const documentName = `displayed name of your document in knowledge base, e.g. myDoc`;
74-
// const knowledgeTypes = `The Knowledge type of the Document. e.g. FAQ`;
75-
// const mimeType = `The mime_type of the Document. e.g. text/csv, text/html,text/plain, text/pdf etc.`;
76-
77-
const request = {
78-
parent: knowledgeBaseFullName,
79-
document: {
80-
knowledgeTypes: [knowledgeTypes],
81-
displayName: documentName,
82-
contentUri: documentPath,
83-
source: 'contentUri',
84-
mimeType: mimeType,
85-
},
86-
};
87-
88-
const [operation] = await client.createDocument(request);
89-
const [response] = await operation.promise();
90-
console.log('Document created');
91-
console.log(`Content URI...${response.contentUri}`);
92-
console.log(`displayName...${response.displayName}`);
93-
console.log(`mimeType...${response.mimeType}`);
94-
console.log(`name...${response.name}`);
95-
console.log(`source...${response.source}`);
96-
97-
// [END dialogflow_create_document]
98-
}
50+
/*
51+
*This test is commented until the proto change for dialogflow/v2beta1 is finished.
52+
*/
53+
// async function createDocument(
54+
// projectId,
55+
// knowledgeBaseFullName,
56+
// documentPath,
57+
// documentName,
58+
// knowledgeTypes,
59+
// mimeType
60+
// ) {
61+
// // [START dialogflow_create_document]
62+
// // Imports the Dialogflow client library
63+
// const dialogflow = require('@google-cloud/dialogflow').v2beta1;
64+
65+
// // Instantiate a DialogFlow Documents client.
66+
// const client = new dialogflow.DocumentsClient({
67+
// projectId: projectId,
68+
// });
69+
70+
// /**
71+
// * TODO(developer): Uncomment the following lines before running the sample.
72+
// */
73+
// // const projectId = 'ID of GCP project associated with your Dialogflow agent';
74+
// // const knowledgeBaseFullName = `the full path of your knowledge base, e.g my-Gcloud-project/myKnowledgeBase`;
75+
// // const documentPath = `path of the document you'd like to add, e.g. https://dialogflow.com/docs/knowledge-connectors`;
76+
// // const documentName = `displayed name of your document in knowledge base, e.g. myDoc`;
77+
// // const knowledgeTypes = `The Knowledge type of the Document. e.g. FAQ`;
78+
// // const mimeType = `The mime_type of the Document. e.g. text/csv, text/html,text/plain, text/pdf etc.`;
79+
80+
// const request = {
81+
// parent: knowledgeBaseFullName,
82+
// document: {
83+
// knowledgeTypes: [knowledgeTypes],
84+
// displayName: documentName,
85+
// contentUri: documentPath,
86+
// source: 'contentUri',
87+
// mimeType: mimeType,
88+
// },
89+
// };
90+
91+
// const [operation] = await client.createDocument(request);
92+
// const [response] = await operation.promise();
93+
94+
// console.log('Document created');
95+
// console.log(`Content URI...${response.contentUri}`);
96+
// console.log(`displayName...${response.displayName}`);
97+
// console.log(`mimeType...${response.mimeType}`);
98+
// console.log(`name...${response.name}`);
99+
// console.log(`source...${response.source}`);
100+
101+
// // [END dialogflow_create_document]
102+
// }
99103

100104
async function detectIntentandSentiment(
101105
projectId,
@@ -105,8 +109,7 @@ async function detectIntentandSentiment(
105109
) {
106110
// [START dialogflow_detect_intent_with_sentiment_analysis]
107111
// Imports the Dialogflow client library
108-
const dialogflow = require('dialogflow').v2beta1;
109-
112+
const dialogflow = require('@google-cloud/dialogflow').v2beta1;
110113
// Instantiate a DialogFlow client.
111114
const sessionClient = new dialogflow.SessionsClient();
112115

@@ -119,7 +122,10 @@ async function detectIntentandSentiment(
119122
// const languageCode = 'BCP-47 language code, e.g. en-US';
120123

121124
// Define session path
122-
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
125+
const sessionPath = sessionClient.projectAgentSessionPath(
126+
projectId,
127+
sessionId
128+
);
123129

124130
// The text query request.
125131
const request = {
@@ -171,8 +177,7 @@ async function detectIntentwithTexttoSpeechResponse(
171177
) {
172178
// [START dialogflow_detect_intent_with_texttospeech_response]
173179
// Imports the Dialogflow client library
174-
const dialogflow = require('dialogflow').v2beta1;
175-
180+
const dialogflow = require('@google-cloud/dialogflow').v2beta1;
176181
// Instantiate a DialogFlow client.
177182
const sessionClient = new dialogflow.SessionsClient();
178183

@@ -186,7 +191,10 @@ async function detectIntentwithTexttoSpeechResponse(
186191
// const outputFile = `path for audio output file, e.g. ./resources/myOutput.wav`;
187192

188193
// Define session path
189-
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
194+
const sessionPath = sessionClient.projectAgentSessionPath(
195+
projectId,
196+
sessionId
197+
);
190198
const fs = require('fs');
191199

192200
// The audio query request
@@ -220,8 +228,7 @@ async function detectIntentKnowledge(
220228
) {
221229
// [START dialogflow_detect_intent_knowledge]
222230
// Imports the Dialogflow client library
223-
const dialogflow = require('dialogflow').v2beta1;
224-
231+
const dialogflow = require('@google-cloud/dialogflow').v2beta1;
225232
// Instantiate a DialogFlow client.
226233
const sessionClient = new dialogflow.SessionsClient();
227234

@@ -235,12 +242,12 @@ async function detectIntentKnowledge(
235242
// const query = `phrase(s) to pass to detect, e.g. I'd like to reserve a room for six people`;
236243

237244
// Define session path
238-
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
239-
const knowbase = new dialogflow.KnowledgeBasesClient();
240-
const knowledgeBasePath = knowbase.knowledgeBasePath(
245+
const sessionPath = sessionClient.projectAgentSessionPath(
241246
projectId,
242-
knowledgeBaseId
247+
sessionId
243248
);
249+
const knowledgeBasePath =
250+
'projects/' + projectId + '/knowledgeBases/' + knowledgeBaseId + '';
244251

245252
// The audio query request
246253
const request = {
@@ -367,20 +374,25 @@ const cli = require('yargs')
367374
.command('createKnowledgeBase', 'Creates a new knowledge base', {}, opts =>
368375
createKnowledgeBase(opts.projectId, opts.knowledgeBaseName)
369376
)
370-
.command(
371-
'createDocument',
372-
'Creates a new document for this knowledge base',
373-
{},
374-
opts =>
375-
createDocument(
376-
opts.projectId,
377-
opts.knowledgeBaseFullName,
378-
opts.documentPath,
379-
opts.documentName,
380-
opts.knowledgeTypes,
381-
opts.mimeType
382-
)
383-
)
377+
/**
378+
* TODO(developer): Uncomment the following lines until proto updates for dialogflow/v2beta1 is complete.
379+
* This method should be annotated with (google.longrunning.operationInfo) to generate LRO methods.
380+
* Now it's a simple method, without proper LRO response, so it fails because `promise() is not a function`.
381+
*/
382+
// .command(
383+
// 'createDocument',
384+
// 'Creates a new document for this knowledge base',
385+
// {},
386+
// opts =>
387+
// createDocument(
388+
// opts.projectId,
389+
// opts.knowledgeBaseFullName,
390+
// opts.documentPath,
391+
// opts.documentName,
392+
// opts.knowledgeTypes,
393+
// opts.mimeType
394+
// )
395+
// )
384396
.command(
385397
'detectIntentwithTexttoSpeechResponse',
386398
'Detects the intent of text input, outputs .wav file to target location',

dialogflow/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"test": "mocha system-test --timeout=600000"
1616
},
1717
"dependencies": {
18-
"dialogflow": "^1.2.0",
18+
"@google-cloud/dialogflow": "^1.2.0",
1919
"pb-util": "^0.1.0",
2020
"uuid": "^3.3.2",
2121
"yargs": "^15.0.0"

0 commit comments

Comments
 (0)