voicekit.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Colour blocks for Blockly.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Blocks.voicekit');
  26. goog.require('Blockly.Blocks');
  27. /**
  28. * Common HSV hue for all blocks in this category.
  29. */
  30. Blockly.Blocks.voicekit.HUE = "#3382f5";
  31. Blockly.Blocks["voicekit_from"] = {
  32. init: function() {
  33. this.setColour(Blockly.Blocks.voicekit.HUE);
  34. this.appendValueInput("INPUT")
  35. .appendField(Blockly.Msg.VOICEKIT_FROM)
  36. .appendField(new Blockly.FieldDropdown([
  37. ["aiy.voice","aiy.vocie"],
  38. ["aiy.assistant","aiy.assistant"],
  39. ["aiy.assistant.library","aiy.assistant.library"],
  40. ["google.assistant.library.event","google.assistant.library.event"]
  41. ]),"FROM")
  42. this.setPreviousStatement(true);
  43. this.setNextStatement(true);
  44. }
  45. }
  46. Blockly.Blocks["voicekit_import"] = {
  47. init:function() {
  48. this.setColour(Blockly.Blocks.voicekit.HUE);
  49. this.appendDummyInput()
  50. .appendField(Blockly.Msg.VOICEKIT_IMPORT_AUTH)
  51. this.setOutput(true);
  52. }
  53. }
  54. Blockly.Blocks["voicekit_import_tts"] = {
  55. init:function() {
  56. this.setColour(Blockly.Blocks.voicekit.HUE);
  57. this.appendDummyInput()
  58. .appendField(Blockly.Msg.VOICEKIT_IMPORT_TTS)
  59. // this.setOutput(true);
  60. this.setPreviousStatement(true);
  61. this.setNextStatement(true);
  62. }
  63. }
  64. Blockly.Blocks["voicekit_tts_say"] = {
  65. init:function() {
  66. this.setColour(Blockly.Blocks.voicekit.HUE);
  67. this.appendValueInput("TEXT")
  68. .appendField(Blockly.Msg.VOICEKIT_IMPORT_TTS_SAY)
  69. this.setInputsInline(true)
  70. this.setPreviousStatement(true);
  71. this.setNextStatement(true);
  72. }
  73. }
  74. Blockly.Blocks["voicekit_set_credential"] = {
  75. init:function() {
  76. this.setColour(Blockly.Blocks.voicekit.HUE);
  77. this.appendValueInput("INPUT")
  78. .appendField(Blockly.Msg.VOICEKIT_SET_CREDENTIAL)
  79. .appendField(new Blockly.FieldTextInput("assistant"),"ASSISTANT")
  80. .appendField(Blockly.Msg.VOICEKIT_SET_FROM)
  81. this.appendStatementInput("STACK")
  82. .appendField(Blockly.Msg.VOICEKIT_SET_DO)
  83. this.setPreviousStatement(true);
  84. this.setNextStatement(true);
  85. }
  86. }
  87. Blockly.Blocks['voicekit_get_credential'] = {
  88. init:function() {
  89. this.setColour(Blockly.Blocks.voicekit.HUE);
  90. this.appendDummyInput()
  91. .appendField(Blockly.Msg.VOICEKIT_GET_CREDENTIAL)
  92. this.setOutput(true);
  93. }
  94. }
  95. Blockly.Blocks['voicekit_import_assistant'] = {
  96. init:function() {
  97. this.setColour(Blockly.Blocks.voicekit.HUE);
  98. this.appendDummyInput()
  99. .appendField(Blockly.Msg.VOICEKIT_IMPORT_ASSISTANT)
  100. this.setOutput(true);
  101. }
  102. }
  103. Blockly.Blocks['voicekit_assistant_preNext'] = {
  104. init:function() {
  105. this.setColour(Blockly.Blocks.voicekit.HUE)
  106. this.appendValueInput("INPUT")
  107. .appendField(Blockly.Msg.VOICEKIT_ASSISTANT)
  108. this.appendDummyInput()
  109. .appendField(new Blockly.FieldDropdown([
  110. ["stop conversation","stop_conversation()"],
  111. ["start conversation","start_conversation()"]
  112. ]),"OPERATION")
  113. this.setInputsInline(true);
  114. this.setPreviousStatement(true)
  115. this.setNextStatement(true);
  116. }
  117. }
  118. Blockly.Blocks['voicekit_assistant_output'] = {
  119. init:function() {
  120. this.setColour(Blockly.Blocks.voicekit.HUE)
  121. this.appendValueInput("INPUT")
  122. .appendField(Blockly.Msg.VOICEKIT_ASSISTANT)
  123. this.appendDummyInput()
  124. .appendField(new Blockly.FieldDropdown([
  125. ["stop conversation","stop_conversation()"],
  126. ["start conversation","start_conversation()"]
  127. ]),"OPERATION")
  128. this.setInputsInline(true);
  129. // this.setPreviousStatement(true)
  130. // this.setNextStatement(true);
  131. this.setOutput(true);
  132. }
  133. }
  134. Blockly.Blocks['voicekit_EventType_import'] = {
  135. init:function() {
  136. this.setColour(Blockly.Blocks.voicekit.HUE)
  137. this.appendDummyInput()
  138. .appendField(Blockly.Msg.VOICEKIT_EVENTTYPE_IMPORT)
  139. // this.setInputsInline(true);
  140. // this.setPreviousStatement(true)
  141. // this.setNextStatement(true);
  142. this.setOutput(true);
  143. }
  144. }
  145. Blockly.Blocks['voicekit_event_of'] = {
  146. init:function() {
  147. this.setColour(Blockly.Blocks.voicekit.HUE);
  148. this.appendValueInput("INPUT")
  149. .appendField(Blockly.Msg.VOICEKIT_EVENT_TYPE_OF)
  150. this.setInputsInline(true);
  151. this.setOutput(true);
  152. }
  153. }
  154. Blockly.Blocks['voicekit_run_task'] = {
  155. init:function(){
  156. this.setColour(Blockly.Blocks.voicekit.HUE)
  157. this.appendDummyInput()
  158. .appendField("_run_task")
  159. this.setOutput(true)
  160. }
  161. }
  162. Blockly.Blocks['voicekit_process_event'] = {
  163. init:function() {
  164. this.setColour(Blockly.Blocks.voicekit.HUE)
  165. this.appendDummyInput()
  166. .appendField("_process_event")
  167. this.setPreviousStatement(true);
  168. this.setNextStatement(true);
  169. }
  170. }
  171. Blockly.Blocks['voicekit_assistant_start_conversation'] = {
  172. init:function() {
  173. this.setColour(Blockly.Blocks.voicekit.HUE)
  174. this.appendDummyInput()
  175. .appendField("self_assistant.start_conversation()")
  176. this.setPreviousStatement(true);
  177. this.setNextStatement(true);
  178. }
  179. }
  180. Blockly.Blocks['voicekit_send_instructions'] = {
  181. init:function() {
  182. this.setColour(Blockly.Blocks.voicekit.HUE)
  183. this.appendDummyInput()
  184. .appendField("send_instructions")
  185. this.appendValueInput("TEXT")
  186. .appendField("text")
  187. .setAlign(Blockly.ALIGN_RIGHT)
  188. this.setPreviousStatement(true);
  189. this.setNextStatement(true);
  190. }
  191. }
  192. Blockly.Blocks['voicekit_on_button_pressed'] = {
  193. init:function() {
  194. this.setColour(Blockly.Blocks.voicekit.HUE)
  195. this.appendDummyInput()
  196. .appendField("on_button_pressed")
  197. this.setOutput(true)
  198. }
  199. }
  200. /**************************
  201. * simplify *
  202. ***************************/
  203. /**
  204. * voice kit
  205. * import EventType / auth_helpers / Assistant
  206. */
  207. Blockly.Blocks['voicekit_import_aiy'] = {
  208. init:function() {
  209. this.setColour(Blockly.Blocks.voicekit.HUE)
  210. this.appendDummyInput()
  211. .appendField("import AIY Voice Moudles")
  212. this.setPreviousStatement(true);
  213. this.setNextStatement(true);
  214. },
  215. }
  216. /**
  217. * voice kit init
  218. *
  219. */
  220. Blockly.Blocks['voicekit_init'] = {
  221. init:function() {
  222. this.setColour(Blockly.Blocks.voicekit.HUE)
  223. this.appendDummyInput()
  224. .appendField("Voicekit Kit")
  225. this.appendDummyInput()
  226. .appendField("Assistant Initialization")
  227. this.itemCount_ = 1;
  228. this.updateShape_();
  229. this.setMutator(new Blockly.Mutator(['voicekit_init_item']));
  230. this.setPreviousStatement(true);
  231. this.setNextStatement(true);
  232. },
  233. /**
  234. * Create XML to represent print inputs.
  235. * @return {Element} XML storage element.
  236. * @this Blockly.Block
  237. */
  238. mutationToDom: function(workspace) {
  239. var container = document.createElement('mutation');
  240. container.setAttribute('items', this.itemCount_);
  241. return container;
  242. },
  243. /**
  244. * Parse XML to restore the list inputs.
  245. * @param {!Element} xmlElement XML storage element.
  246. * @this Blockly.Block
  247. */
  248. domToMutation: function(xmlElement) {
  249. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  250. this.updateShape_();
  251. },
  252. /**
  253. * Populate the mutator's dialog with this block's components.
  254. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  255. * @return {!Blockly.Block} Root block in mutator.
  256. * @this Blockly.Block
  257. */
  258. decompose: function(workspace) {
  259. var containerBlock = Blockly.Block.obtain(workspace,
  260. 'voicekit_init_container');
  261. containerBlock.initSvg();
  262. var connection = containerBlock.getInput('STACK').connection;
  263. for (var x = 0; x < this.itemCount_; x++) {
  264. var itemBlock = Blockly.Block.obtain(workspace, 'voicekit_init_item');
  265. itemBlock.initSvg();
  266. connection.connect(itemBlock.previousConnection);
  267. connection = itemBlock.nextConnection;
  268. }
  269. return containerBlock;
  270. },
  271. /**
  272. * Reconfigure this block based on the mutator dialog's components.
  273. * @param {!Blockly.Block} containerBlock Root block in mutator.
  274. * @this Blockly.Block
  275. */
  276. compose: function(containerBlock) {
  277. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  278. // Count number of inputs.
  279. var connections = [];
  280. var i = 0;
  281. while (itemBlock) {
  282. connections[i] = itemBlock.valueConnection_;
  283. itemBlock = itemBlock.nextConnection &&
  284. itemBlock.nextConnection.targetBlock();
  285. i++;
  286. }
  287. this.itemCount_ = i;
  288. this.updateShape_();
  289. // Reconnect any child blocks.
  290. for (var i = 0; i < this.itemCount_; i++) {
  291. if (connections[i]) {
  292. this.getInput('ADD' + i).connection.connect(connections[i]);
  293. }
  294. }
  295. },
  296. /**
  297. * Store pointers to any connected child blocks.
  298. * @param {!Blockly.Block} containerBlock Root block in mutator.
  299. * @this Blockly.Block
  300. */
  301. saveConnections: function(containerBlock) {
  302. // Store a pointer to any connected child blocks.
  303. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  304. var x = 0;
  305. while (itemBlock) {
  306. var input = this.getInput('ADD' + x);
  307. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  308. x++;
  309. itemBlock = itemBlock.nextConnection &&
  310. itemBlock.nextConnection.targetBlock();
  311. }
  312. },
  313. /**
  314. * Modify this block to have the correct number of inputs.
  315. * @private
  316. * @this Blockly.Block
  317. */
  318. updateShape_: function() {
  319. // Delete everything.
  320. if (this.getInput('EMPTY')) {
  321. this.removeInput('EMPTY');
  322. } else {
  323. var i = 0;
  324. while (this.getInput('ADD' + i)) {
  325. this.removeInput('ADD' + i);
  326. i++;
  327. }
  328. }
  329. // Rebuild block.
  330. for (var i = 0; i < this.itemCount_; i++) {
  331. var input = this.appendValueInput('ADD' + i);
  332. }
  333. }
  334. };
  335. Blockly.Blocks['voicekit_init_container'] = {
  336. // Container.
  337. init: function() {
  338. this.setColour(Blockly.Blocks.voicekit.HUE);
  339. this.appendDummyInput()
  340. .appendField('field');
  341. this.appendStatementInput('STACK');
  342. this.contextMenu = false;
  343. }
  344. };
  345. Blockly.Blocks['voicekit_init_item'] = {
  346. // Add items.
  347. init: function() {
  348. this.setColour(Blockly.Blocks.voicekit.HUE);
  349. this.appendDummyInput()
  350. .appendField('item');
  351. this.setInputsInline(true);
  352. this.setPreviousStatement(true);
  353. this.setNextStatement(true);
  354. this.contextMenu = false;
  355. }
  356. };
  357. Blockly.Blocks['voicekit_task'] = {
  358. init:function() {
  359. this.setColour(Blockly.Blocks.voicekit.HUE)
  360. this.appendDummyInput()
  361. .appendField("Task Tread")
  362. this.setOutput(true)
  363. }
  364. }
  365. Blockly.Blocks['voicekit_can_start'] = {
  366. init:function() {
  367. this.setColour(Blockly.Blocks.voicekit.HUE)
  368. this.appendDummyInput()
  369. .appendField("Converstation")
  370. this.setOutput(true)
  371. }
  372. }
  373. Blockly.Blocks['voicekit_asssistant'] = {
  374. init:function() {
  375. this.setColour(Blockly.Blocks.voicekit.HUE)
  376. this.appendDummyInput()
  377. .appendField("Assistant")
  378. this.setOutput(true)
  379. }
  380. }
  381. /**
  382. * voice kit
  383. * start thread
  384. */
  385. Blockly.Blocks['voicekit_start_thread'] = {
  386. init: function() {
  387. this.setColour(Blockly.Blocks.voicekit.HUE)
  388. this.appendDummyInput()
  389. .appendField("Start task")
  390. this.setPreviousStatement(true);
  391. this.setNextStatement(true);
  392. }
  393. }
  394. /**
  395. * voice kit
  396. * run task
  397. */
  398. Blockly.Blocks['voicekit_runTask'] = {
  399. init: function() {
  400. this.setColour(Blockly.Blocks.voicekit.HUE)
  401. this.appendDummyInput()
  402. .appendField("Run task")
  403. this.setPreviousStatement(true);
  404. this.setNextStatement(true);
  405. }
  406. }
  407. Blockly.Blocks['voicekit_logging_config'] = {
  408. init: function() {
  409. this.setColour(Blockly.Blocks.voicekit.HUE)
  410. this.appendDummyInput()
  411. .appendField("logging.basicConfig(level = logging.INFO)")
  412. this.setPreviousStatement(true);
  413. this.setNextStatement(true);
  414. }
  415. }
  416. Blockly.Blocks['voicekit_myassistant_start'] = {
  417. init:function() {
  418. this.setColour(230)
  419. this.appendDummyInput()
  420. .appendField(new Blockly.FieldTextInput("MyAssistant"),"INPUT")
  421. .appendField(".start()")
  422. this.setPreviousStatement(true);
  423. this.setNextStatement(true);
  424. }
  425. }
  426. Blockly.Blocks['voicekit_process_event'] = {
  427. init: function() {
  428. this.setColour(Blockly.Blocks.voicekit.HUE)
  429. this.appendDummyInput()
  430. .appendField("Process Event")
  431. this.appendStatementInput("STACK");
  432. this.setPreviousStatement(true);
  433. this.setNextStatement(true);
  434. }
  435. }
  436. Blockly.Blocks['voicekit_process_eventType'] = {
  437. init: function() {
  438. this.setColour(Blockly.Blocks.voicekit.HUE)
  439. this.appendDummyInput()
  440. .appendField("Event.type")
  441. this.setOutput(true);
  442. }
  443. }
  444. Blockly.Blocks['voicekit_EventType_select'] = {
  445. init: function() {
  446. this.setColour(Blockly.Blocks.voicekit.HUE)
  447. this.appendDummyInput()
  448. .appendField(Blockly.Msg.VOICEKIT_EVENTTYPE_ASSISTANT)
  449. this.appendDummyInput()
  450. .appendField(new Blockly.FieldDropdown([
  451. ["ON_START_FINISHED","ON_START_FINISHED"],
  452. ["ON_CONVERSATION_TURN_STARTED","ON_CONVERSATION_TURN_STARTED"],
  453. ["ON_RECOGNIZING_SPEECH_FINISHED","ON_RECOGNIZING_SPEECH_FINISHED"],
  454. ["ON_END_OF_UTTERANCE","ON_END_OF_UTTERANCE"],
  455. ["ON_CONVERSATION_TURN_FINISHED","ON_CONVERSATION_TURN_FINISHED"],
  456. ["ON_ASSISTANT_ERROR","ON_ASSISTANT_ERROR"],
  457. ["ON_CONVERSATION_TURN_TIMEOUT","ON_CONVERSATION_TURN_TIMEOUT"],
  458. ["ON_NO_RESPONSE","ON_NO_RESPONSE"],
  459. ]),"TYPE")
  460. this.setOutput(true)
  461. }
  462. }
  463. Blockly.Blocks['voicekit_can_start_conversation_self'] = {
  464. init: function() {
  465. this.setColour("#ff6381")
  466. this.appendDummyInput()
  467. .appendField("Can start conversation")
  468. this.setOutput(true);
  469. }
  470. }
  471. Blockly.Blocks['voicekit_can_start_conversation'] = {
  472. init: function() {
  473. this.setColour("#ff6381")
  474. this.appendDummyInput()
  475. .appendField("Can start conversation =")
  476. .appendField(new Blockly.FieldDropdown([
  477. ["True","True"],
  478. ["False","False"],
  479. ]),"SETTING")
  480. this.setPreviousStatement(true);
  481. this.setNextStatement(true)
  482. }
  483. }
  484. Blockly.Blocks['voicekit_assistant_conversation'] = {
  485. init: function() {
  486. this.setColour("#ff6381")
  487. this.appendDummyInput()
  488. .appendField("Assistant.")
  489. .appendField(new Blockly.FieldDropdown([
  490. ["stop_conversation","stop_conversation()"],
  491. ["start_conversation","start_conversation()"]
  492. ]),"SETTING")
  493. this.setPreviousStatement(true);
  494. this.setNextStatement(true)
  495. }
  496. }
  497. Blockly.Blocks['voicekit_event_args'] = {
  498. init:function() {
  499. this.setColour(Blockly.Blocks.voicekit.HUE);
  500. this.appendDummyInput()
  501. .appendField(Blockly.Msg.VOICEKIT_EVENT_ARGS)
  502. this.setInputsInline(true);
  503. this.setOutput(true)
  504. }
  505. }
  506. Blockly.Blocks['voicekit_event_args_of'] ={
  507. init:function() {
  508. this.setColour(Blockly.Blocks.voicekit.HUE)
  509. this.appendDummyInput()
  510. .appendField(Blockly.Msg.VOICEKIT_EVENT_ARGS)
  511. .appendField(Blockly.Msg.VOICEKIT_EVENT_OF)
  512. .appendField(new Blockly.FieldTextInput("text"),"TEXT")
  513. this.setInputsInline(true);
  514. this.setOutput(true)
  515. }
  516. }