@innovastudio/contentbox 1.6.101 → 1.6.102

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -31396,9 +31396,10 @@ class Util$1 {
31396
31396
  const dom = this.dom;
31397
31397
  pop.style.display = '';
31398
31398
  dom.removeClass(pop, 'active');
31399
- this.builder.doc.activeElement.blur();
31400
- document.activeElement.blur();
31401
- // this.builder.doc.body.focus();
31399
+
31400
+ // this.builder.doc.activeElement.blur();
31401
+ // document.activeElement.blur();
31402
+ this.builder.doc.body.focus();
31402
31403
  pop.setAttribute('aria-hidden', true);
31403
31404
 
31404
31405
  // pop.removeEventListener('keydown', this.handlePopKeyDown);
@@ -76468,6 +76469,7 @@ class Module {
76468
76469
  class Code {
76469
76470
  constructor(builder) {
76470
76471
  this.builder = builder;
76472
+ this.renderTool();
76471
76473
  }
76472
76474
  renderTool() {
76473
76475
  const builderStuff = this.builder.builderStuff;
@@ -78138,6 +78140,8 @@ class RowTool {
78138
78140
  constructor(builder) {
78139
78141
  this.builder = builder;
78140
78142
  this.grid = new Grid(builder); // a must (also for ContentBox's command.js)
78143
+
78144
+ this.renderRowMore();
78141
78145
  }
78142
78146
 
78143
78147
  // Render row & column tools on each row
@@ -80032,6 +80036,7 @@ class ColumnTool {
80032
80036
  });
80033
80037
  });
80034
80038
  }
80039
+ this.renderColMore();
80035
80040
  }
80036
80041
  renderColMore() {
80037
80042
  const builder = this.builder;
@@ -146742,7 +146747,285 @@ class Lib {
146742
146747
  description: 'User requests to add a new section with 4 boxes.'
146743
146748
  }
146744
146749
  },
146745
- required: ['image', 'article']
146750
+ required: []
146751
+ }
146752
+ }];
146753
+ }
146754
+
146755
+ getFunctionsInitial() {
146756
+ return [{
146757
+ name: 'get_intent',
146758
+ description: 'analyze user command to modify or create section/content',
146759
+ parameters: {
146760
+ type: 'object',
146761
+ properties: {
146762
+ new_section: {
146763
+ type: 'boolean',
146764
+ description: 'User requests to add a new section and not updating current section.'
146765
+ } // edit_section: {
146766
+ // type: 'boolean',
146767
+ // description: 'User provides section to edit, such as improve content or rewrite.',
146768
+ // },
146769
+
146770
+ },
146771
+ required: []
146772
+ }
146773
+ }];
146774
+ }
146775
+
146776
+ getFunctionsMore() {
146777
+ return [{
146778
+ name: 'get_intent',
146779
+ description: 'analyze user command to modify or create section/content',
146780
+ parameters: {
146781
+ type: 'object',
146782
+ properties: {
146783
+ asking_info: {
146784
+ type: 'boolean',
146785
+ description: 'User request an information.'
146786
+ },
146787
+ add_info: {
146788
+ // Without this, adding info will be considered as asking_info. Tha add_info should be the same as change_text
146789
+ type: 'boolean',
146790
+ description: 'User request an information to be added to the current content.'
146791
+ },
146792
+ // Add new Column or Block (in General Mode)
146793
+ add_column_or_block: {
146794
+ type: 'boolean',
146795
+ description: 'User requests to add a column or a block.'
146796
+ },
146797
+ change_text: {
146798
+ type: 'boolean',
146799
+ description: 'User explicitely requests to update text of the current content, rewrite, paraphrase, translate or change text.'
146800
+ },
146801
+ write_based_on_the_current_content: {
146802
+ type: 'boolean',
146803
+ description: 'User request to create new content based on the current content, such as to summarize, expand, make short, create a tweet or social media post, etc'
146804
+ },
146805
+ add_headline: {
146806
+ type: 'boolean',
146807
+ description: 'If requested to add a headline.'
146808
+ },
146809
+ add_paragraph: {
146810
+ type: 'boolean',
146811
+ description: 'If requested to add a paragraph.'
146812
+ },
146813
+ add_space: {
146814
+ type: 'boolean',
146815
+ description: 'If requested to add a space.'
146816
+ },
146817
+ add_button: {
146818
+ type: 'boolean',
146819
+ description: 'If requested to add a button.'
146820
+ },
146821
+ add_youtube: {
146822
+ type: 'boolean',
146823
+ description: 'If requested to add a youtube/vimeo video.'
146824
+ },
146825
+ add_video: {
146826
+ type: 'boolean',
146827
+ description: 'If requested to add a video.'
146828
+ },
146829
+ add_audio: {
146830
+ type: 'boolean',
146831
+ description: 'If requested to add an audio.'
146832
+ },
146833
+ add_map: {
146834
+ type: 'boolean',
146835
+ description: 'If requested to add a google map.'
146836
+ },
146837
+ add_code: {
146838
+ type: 'boolean',
146839
+ description: 'If requested to add a custom code block.'
146840
+ },
146841
+ add_article: {
146842
+ type: 'boolean',
146843
+ description: 'If requested to add an article.'
146844
+ },
146845
+ add_image: {
146846
+ type: 'string',
146847
+ description: 'User request to add image or gallery. Possible value: image, gallery'
146848
+ },
146849
+ add_list: {
146850
+ type: 'boolean',
146851
+ description: 'If requested to add a list (eg. features, benefits, or list of items to explain).'
146852
+ },
146853
+ add_list_with_image: {
146854
+ type: 'boolean',
146855
+ description: 'If requested to add a list with an image for each list item.'
146856
+ },
146857
+ add_list_with_icon: {
146858
+ type: 'boolean',
146859
+ description: 'If requested to add a list with an icon.'
146860
+ },
146861
+ add_list_with_number: {
146862
+ type: 'boolean',
146863
+ description: 'If requested to add a list with numbering.'
146864
+ },
146865
+ // freeform
146866
+ new_content: {
146867
+ type: 'boolean',
146868
+ description: 'User requests to add a new content.'
146869
+ },
146870
+ add_block: {
146871
+ type: 'boolean',
146872
+ description: 'User requests to add a block.'
146873
+ },
146874
+
146875
+ /*
146876
+ To make 2 columns not mixed with 2 boxes, each has to be identified separately
146877
+ */
146878
+ redesign_layout: {
146879
+ type: 'boolean',
146880
+ description: 'If redesigning the layout is requested.'
146881
+ },
146882
+ change_style: {
146883
+ type: 'boolean',
146884
+ description: 'If changing element style is requested.'
146885
+ },
146886
+ change_font: {
146887
+ type: 'boolean',
146888
+ description: 'User requests to change font of the current section.'
146889
+ },
146890
+ change_section_height: {
146891
+ type: 'number',
146892
+ description: 'Requested height (percentage) of the section. Possible value: 10, 15, 20, 25, 30, 40, 50, 60, 70, 75, 80, 85, 90, 100'
146893
+ },
146894
+ section_background: {
146895
+ type: 'string',
146896
+ description: 'User request to add or remove section background that can be a background image, video or slider. Possible value: image, video, slider, remove'
146897
+ }
146898
+ },
146899
+ required: []
146900
+ }
146901
+ }];
146902
+ }
146903
+
146904
+ getFunctionsFinal() {
146905
+ return [{
146906
+ name: 'get_intent',
146907
+ description: 'categorize user intent',
146908
+ parameters: {
146909
+ type: 'object',
146910
+ properties: {
146911
+ change_color_individual_text: {
146912
+ type: 'boolean',
146913
+ description: 'User requests to change the color of a headline, paragraph of specific element/text.'
146914
+ },
146915
+ change_content_dark: {
146916
+ type: 'boolean',
146917
+ description: 'If requested to change text/content color to dark/black.'
146918
+ },
146919
+ change_content_light: {
146920
+ type: 'boolean',
146921
+ description: 'If requested to change text/content color to light/white.'
146922
+ },
146923
+ align_individual_text: {
146924
+ type: 'boolean',
146925
+ description: 'If requested to align an individual text/element, such as a headline, a paragraph, buttons, etc to left, center or right'
146926
+ },
146927
+ align_whole_content: {
146928
+ type: 'string',
146929
+ description: 'User requests to align the whole text/content. Possible value: left, center or right'
146930
+ },
146931
+
146932
+ /*
146933
+ // Block Mode (in General Mode)
146934
+ block_task: {
146935
+ type: 'string',
146936
+ description: 'User requests to move, duplicate, or delete block/row. Possible values: move-up, move-down, duplicate, remove'
146937
+ },
146938
+ column_task: {
146939
+ type: 'string',
146940
+ description: 'User requests to duplicate column, or delete column. Possible values: duplicate, remove'
146941
+ },
146942
+ move_column: {
146943
+ type: 'string',
146944
+ description: 'User requests to move column. Possible values: move-up, move-down, move-left, move-right'
146945
+ },
146946
+ increase_decrease_column: {
146947
+ type: 'string',
146948
+ description: 'User requests to increase/enlarge or decrease/reduce column size/width. Possible values: increase, decrease'
146949
+ },
146950
+ move_content_left: {
146951
+ type: 'boolean',
146952
+ description: 'Move content to the left',
146953
+ },
146954
+ move_content_center: {
146955
+ type: 'boolean',
146956
+ description: 'Move content to the center',
146957
+ },
146958
+ move_content_right: {
146959
+ type: 'boolean',
146960
+ description: 'Move content to the right',
146961
+ },
146962
+ move_content_top: {
146963
+ type: 'boolean',
146964
+ description: 'Move content to the top',
146965
+ },
146966
+ move_content_bottom: {
146967
+ type: 'boolean',
146968
+ description: 'Move content to the bottom',
146969
+ },
146970
+ */
146971
+ content_width: {
146972
+ type: 'string',
146973
+ description: 'User requests to increase or decrease content width/size. Possible value: increase, decrease'
146974
+ },
146975
+ line_height_paragraph: {
146976
+ type: 'string',
146977
+ description: 'User requests to increase/decrease the line height of the paragraph. Possible value: increase, decrease'
146978
+ },
146979
+ line_height_individual_text: {
146980
+ type: 'boolean',
146981
+ description: 'User requests to increase/decrease the line height of an individual text/element (eg. headline).'
146982
+ },
146983
+ font_size_paragraph: {
146984
+ type: 'string',
146985
+ description: 'User requests to increase/decrease the font size of the paragraph. Possible value: increase, decrease'
146986
+ },
146987
+ font_size_individual_text: {
146988
+ type: 'boolean',
146989
+ description: 'User requests to increase/decrease the font size of an individual text/element (eg. headline).'
146990
+ }
146991
+ /*
146992
+ animation: {
146993
+ type: 'string',
146994
+ description: 'User requests to add animation. Possible value: fade-in, slide-up, zoom-in, remove'
146995
+ },
146996
+ section_task: {
146997
+ type: 'string',
146998
+ description: 'User requests to move, duplicate, or delet section. Possible values: move-up, move-top, move-down, move-bottom, duplicate, remove'
146999
+ },
147000
+ select_section: {
147001
+ type: 'string',
147002
+ description: 'User requests to select a section. Value can be: current, next, previous, first, last.'
147003
+ },
147004
+ select_block: {
147005
+ type: 'string',
147006
+ description: 'User requests to select a block. Value can be: block, headline, title, paragraph, image, next, previous, first, last'
147007
+ },
147008
+ unselect_block: {
147009
+ type: 'boolean',
147010
+ description: 'User requests to unselect block'
147011
+ },
147012
+ */
147013
+ // Must be placed on the last for more precise
147014
+ // new_section_two_boxes: {
147015
+ // type: 'boolean',
147016
+ // description: 'User requests to add a new section with 2 boxes.'
147017
+ // },
147018
+ // new_section_three_boxes: {
147019
+ // type: 'boolean',
147020
+ // description: 'User requests to add a new section with 3 boxes.'
147021
+ // },
147022
+ // new_section_four_boxes: {
147023
+ // type: 'boolean',
147024
+ // description: 'User requests to add a new section with 4 boxes.'
147025
+ // },
147026
+
147027
+ },
147028
+ required: []
146746
147029
  }
146747
147030
  }];
146748
147031
  }
@@ -147009,6 +147292,7 @@ Button Example:
147009
147292
 
147010
147293
  isCanvasMode() {
147011
147294
  let activeBox = this.builder.activeBox;
147295
+ if (!activeBox) return false;
147012
147296
  let canvasMode = activeBox.classList.contains('box-canvas');
147013
147297
  return canvasMode;
147014
147298
  }
@@ -148355,7 +148639,8 @@ ${blockContent}
148355
148639
 
148356
148640
  if (activeCol) {
148357
148641
  // && numberOfIntents<=2) {
148358
- // Update Column or Block (in General Mode)
148642
+ if (this.builder.consoleLog) console.log('branch block'); // Update Column or Block (in General Mode)
148643
+
148359
148644
  if (args.align_individual_text || args.font_size_individual_text || args.line_height_individual_text) {
148360
148645
  sendUpdateBlockCommand(question, args);
148361
148646
  return;
@@ -148393,6 +148678,8 @@ ${blockContent}
148393
148678
  if (container) currentContent = this.builder.editor.readHtml(container, false, false);
148394
148679
 
148395
148680
  if (args.add_block || args.new_content || args.new_section || currentContent.trim() === '') {
148681
+ if (this.builder.consoleLog) console.log('branch 1');
148682
+
148396
148683
  if (args.add_image === 'gallery') {
148397
148684
  context = cl.new_section_with_gallery;
148398
148685
  } else if (args.add_list_with_image) {
@@ -148422,6 +148709,7 @@ ${blockContent}
148422
148709
  }
148423
148710
  } else {
148424
148711
  //if(args.redesign_layout || args.change_text || args.change_style || args.add_info) {
148712
+ if (this.builder.consoleLog) console.log('branch 2');
148425
148713
  let currentHtml = '<div class="is-container">' + currentContent + '</div>';
148426
148714
  let redesign_request = '';
148427
148715
 
@@ -148564,10 +148852,30 @@ ${currentHtml}
148564
148852
  }
148565
148853
  } // if(this.builder.sendCommandUrl==='') return;
148566
148854
 
148855
+ /*
148856
+ let response = await this.sendCommandSync('analyze',
148857
+ `This is the command: "${question}"`,
148858
+ '',
148859
+ 'You will analyze the user\'s command and return what the user needs.',
148860
+ this.lib.getFunctionsInitial());
148861
+ const args1 = JSON.parse(response);
148862
+ response = await this.sendCommandSync('analyze',
148863
+ `This is the command: "${question}"`,
148864
+ '',
148865
+ 'You will analyze the user\'s command and return what the user needs.',
148866
+ this.lib.getFunctionsMore());
148867
+ const args2 = JSON.parse(response);
148868
+ response = await this.sendCommandSync('analyze',
148869
+ `This is the command: "${question}"`,
148870
+ '',
148871
+ 'You will analyze the user\'s command and return what the user needs.',
148872
+ this.lib.getFunctionsFinal());
148873
+ const args3 = JSON.parse(response);
148874
+ const args = { ...args1, ...args2, ...args3 };
148875
+ */
148567
148876
 
148568
- this.sendCommand('analyze', `This is the command: "${question}"`, '', 'You will analyze the user\'s command and return what the user needs.', this.lib.getFunctions(), response => {
148569
- const args = JSON.parse(response);
148570
- if (this.builder.consoleLog) console.log(args);
148877
+
148878
+ const processGeneralCommand = args => {
148571
148879
  const canvasMode = this.isCanvasMode();
148572
148880
 
148573
148881
  if (args.add_block && canvasMode) ; else {
@@ -148591,14 +148899,17 @@ ${currentHtml}
148591
148899
 
148592
148900
  if (activeCol) {
148593
148901
  // && numberOfIntents<=2) {
148594
- // Update Column or Block (in General Mode)
148902
+ if (this.builder.consoleLog) console.log('branch block'); // Update Column or Block (in General Mode)
148903
+
148595
148904
  if (args.align_individual_text || args.font_size_individual_text || args.line_height_individual_text) {
148905
+ if (this.builder.consoleLog) console.log('sendUpdateBlockCommand');
148596
148906
  sendUpdateBlockCommand(question, args);
148597
148907
  return;
148598
148908
  } // Add new Column or Block (in General Mode)
148599
148909
 
148600
148910
 
148601
148911
  if (args.add_column_or_block) {
148912
+ if (this.builder.consoleLog) console.log('sendBlockCommand');
148602
148913
  sendBlockCommand(question);
148603
148914
  return;
148604
148915
  }
@@ -148606,7 +148917,8 @@ ${currentHtml}
148606
148917
 
148607
148918
 
148608
148919
  if (activeBox && numberOfIntents === 1) {
148609
- // Block Mode (in General Mode)
148920
+ if (this.builder.consoleLog) console.log('single intent'); // Block Mode (in General Mode)
148921
+
148610
148922
  let result = this.blockAction(args);
148611
148923
 
148612
148924
  if (result) {
@@ -148621,7 +148933,7 @@ ${currentHtml}
148621
148933
  activeBox.setAttribute('data-selectbox',1);
148622
148934
  this.section.sectionDuplicate();
148623
148935
  activeBox.removeAttribute('data-selectbox');
148624
- this.builder.sectionStack();
148936
+ this.builder.sectionStack();
148625
148937
  const nextBox = this.builder.wrapperEl.querySelector('[data-selectbox]');
148626
148938
  if(nextBox) {
148627
148939
  nextBox.removeAttribute('data-selectbox');
@@ -148892,6 +149204,7 @@ ${currentHtml}
148892
149204
  let context = '';
148893
149205
 
148894
149206
  if (args.add_block && canvasMode) {
149207
+ if (this.builder.consoleLog) console.log('branch 1');
148895
149208
  cl = cl2;
148896
149209
 
148897
149210
  if (args.add_image === 'gallery') {
@@ -148934,7 +149247,11 @@ ${currentHtml}
148934
149247
  context = context.replaceAll('{{_IMAGEURL}}', imageUrl);
148935
149248
  }
148936
149249
  } else if (args.new_section || args.new_section_two_boxes || args.new_section_three_boxes || args.new_section_four_boxes) {
149250
+ if (this.builder.consoleLog) console.log('branch 2');
149251
+
148937
149252
  if (args.new_section) {
149253
+ if (this.builder.consoleLog) console.log('branch 2.1');
149254
+
148938
149255
  if (args.add_image === 'gallery') {
148939
149256
  context = cl.new_section_with_gallery;
148940
149257
  } else if (args.add_list_with_image) {
@@ -148975,6 +149292,8 @@ ${currentHtml}
148975
149292
  context = context.replaceAll('{{_IMAGEURL}}', imageUrl);
148976
149293
  }
148977
149294
  } else if (args.new_section_two_boxes || args.new_section_three_boxes || args.new_section_four_boxes) {
149295
+ if (this.builder.consoleLog) console.log('branch 2.2');
149296
+
148978
149297
  if (args.new_section_two_boxes) {
148979
149298
  context = cl.new_section_two_boxes;
148980
149299
  } else if (args.new_section_three_boxes) {
@@ -149010,10 +149329,13 @@ ${currentHtml}
149010
149329
  context = context.replaceAll('{{_IMAGEURL}}', imageUrl);
149011
149330
  }
149012
149331
  } else {
149332
+ if (this.builder.consoleLog) console.log('branch 2.3');
149013
149333
  context = this.chooseContext(cl.new_section_article); // Or use: context = cl.new_section_others;
149014
149334
  }
149015
149335
  } else {
149016
149336
  //if(args.redesign_layout || args.change_text || args.change_style || args.add_info) {
149337
+ if (this.builder.consoleLog) console.log('branch 3');
149338
+
149017
149339
  if (!this.checkSelectedContent(activeBox)) {
149018
149340
  this.dictation.finish(true);
149019
149341
  return;
@@ -149077,13 +149399,16 @@ ${currentHtml}
149077
149399
 
149078
149400
 
149079
149401
  question = `${redesign_request}${question}. Modify my section according to this instruction and returns the modified section HTML.
149402
+ Do not replace existing content unless requested.
149080
149403
 
149081
149404
  This is my section:
149082
-
149405
+
149083
149406
  ${currentHtml}
149084
149407
  `;
149085
149408
 
149086
149409
  if (args.change_text) ; else {
149410
+ if (this.builder.consoleLog) console.log('branch 4');
149411
+
149087
149412
  if ((args.add_headline || args.add_paragraph) && args.add_image === 'image') {
149088
149413
  context += '\n' + cl.update_section_with_image;
149089
149414
  } else if (args.add_image === 'gallery') {
@@ -149133,6 +149458,7 @@ ${currentHtml}
149133
149458
 
149134
149459
  if (!(args.add_block && canvasMode || args.new_section || args.new_section_two_boxes || args.new_section_three_boxes || args.new_section_four_boxes)) {
149135
149460
  // Redesign & Editing (text or style)
149461
+ if (this.builder.consoleLog) console.log('callback 1');
149136
149462
  html = this.fixButtons(html);
149137
149463
  html = this.fixCustomCode(html);
149138
149464
  html = this.analyzeTitle(html, context); // Merge div.is-overlay-content
@@ -149176,7 +149502,10 @@ ${currentHtml}
149176
149502
  this.renderResult(html);
149177
149503
  } else {
149178
149504
  // New Section
149505
+ if (this.builder.consoleLog) console.log('callback 2');
149506
+
149179
149507
  if (args.add_block && canvasMode) {
149508
+ if (this.builder.consoleLog) console.log('callback 2.1');
149180
149509
  html = this.fixHtml(html);
149181
149510
 
149182
149511
  if (!html) {
@@ -149190,6 +149519,7 @@ ${currentHtml}
149190
149519
 
149191
149520
  this.renderResult_Block(html);
149192
149521
  } else {
149522
+ if (this.builder.consoleLog) console.log('callback 2.2');
149193
149523
  html = this.fixButtons(html);
149194
149524
  html = this.fixBgImage(html); // only if box has inline bg image, which is incorrect
149195
149525
 
@@ -149246,7 +149576,14 @@ ${currentHtml}
149246
149576
 
149247
149577
 
149248
149578
  this.sendCommand('html', question, context, systemCmd, [], callback);
149249
- });
149579
+ };
149580
+
149581
+ this.sendCommand('analyze', `This is the command: "${question}"`, '', 'You will analyze the user\'s command and return what the user needs.', this.lib.getFunctions(), response => {
149582
+ const args = JSON.parse(response);
149583
+ if (this.builder.consoleLog) console.log(args);
149584
+ processGeneralCommand(args);
149585
+ }); // console.log(args);
149586
+ // processGeneralCommand(args);
149250
149587
  };
149251
149588
 
149252
149589
  this.dictation.opts.send = question => {
@@ -149271,15 +149608,19 @@ ${currentHtml}
149271
149608
  const activeBlock = this.activeBlock();
149272
149609
 
149273
149610
  if (activeBlock) {
149274
- // Special: If current box is a canvas and there is a selected block
149611
+ if (this.builder.consoleLog) console.log('sendGeneralCommand_CanvasMode'); // Special: If current box is a canvas and there is a selected block
149612
+
149275
149613
  sendGeneralCommand_CanvasMode(question);
149276
149614
  } else {
149277
149615
  // Normal
149616
+ if (this.builder.consoleLog) console.log('sendGeneralCommand');
149278
149617
  sendGeneralCommand(question);
149279
149618
  }
149280
149619
  } else if (this.builder.editor.assistantMode === 'others') {
149620
+ if (this.builder.consoleLog) console.log('sendOtherCommand');
149281
149621
  sendOtherCommand(question);
149282
149622
  } else if (this.builder.editor.assistantMode === 'block') {
149623
+ if (this.builder.consoleLog) console.log('sendBlockCommand');
149283
149624
  sendBlockCommand(question);
149284
149625
  }
149285
149626
  };
@@ -150476,6 +150817,8 @@ ${currentHtml}
150476
150817
 
150477
150818
  chooseContext(list, num) {
150478
150819
  // random, but will be different from two previous results.
150820
+ console.log(list);
150821
+
150479
150822
  if (typeof num !== 'undefined') {
150480
150823
  return list[num];
150481
150824
  }
@@ -151580,8 +151923,143 @@ ${currentHtml}
151580
151923
  }
151581
151924
  }
151582
151925
 
151926
+ async sendCommandSync(mode, question, context, system, functs) {
151927
+ this.dictation.startSending(); // Shows sending status & Abort button
151928
+
151929
+ this.controller = new AbortController(); // Create a new AbortController
151930
+
151931
+ this.signal = this.controller.signal; // Get a new signal object
151932
+
151933
+ let temperature = localStorage.getItem('_temp') || this.builder.temperature;
151934
+ let topP = localStorage.getItem('_top_p') || this.builder.topP;
151935
+ let num = 1;
151936
+ const messages = {
151937
+ question,
151938
+ context,
151939
+ system,
151940
+ functs,
151941
+ temperature,
151942
+ topP,
151943
+ num
151944
+ };
151945
+
151946
+ try {
151947
+ /*
151948
+ const response = await fetch(this.builder.sendCommandUrl, {
151949
+ signal: this.signal, // Abort
151950
+ method: 'POST',
151951
+ headers: {
151952
+ 'Content-Type': 'application/json',
151953
+ },
151954
+ body: JSON.stringify(messages),
151955
+ });
151956
+ const data = await response.json();
151957
+ */
151958
+ let data;
151959
+
151960
+ if (this.builder.sendCommand) {
151961
+ data = await this.builder.sendCommand(messages);
151962
+ } else {
151963
+ let headers = { ...this.builder.headers,
151964
+ ...this.builder.defaultHeaders
151965
+ };
151966
+ const response = await fetch(this.builder.sendCommandUrl, {
151967
+ signal: this.signal,
151968
+ // Abort
151969
+ method: 'POST',
151970
+ headers,
151971
+ body: JSON.stringify(messages)
151972
+ });
151973
+ data = await response.json();
151974
+ }
151975
+
151976
+ if (data.error) {
151977
+ // Error response from OpenAI goes here.
151978
+ console.log('Error:\n' + data.error); // ex. wrong API Key => Request failed with status code 401
151979
+
151980
+ this.builder.showMessage(out('Request failed.'));
151981
+ this.dictation.finish(); // Must be called after finished
151982
+
151983
+ return;
151984
+ }
151985
+
151986
+ if (mode === 'html') {
151987
+ // console.log(data.answer.usage);
151988
+ data.answer.choices.forEach(item => {
151989
+ let html = item.message.content;
151990
+ html = this.getHtmlResult(html);
151991
+
151992
+ if (!html) {
151993
+ this.builder.showMessage(out('Oops! The response received is insufficient. Please try again.'));
151994
+ this.dictation.finish(); // Must be called after finished
151995
+
151996
+ return;
151997
+ } // callback(html);
151998
+
151999
+
152000
+ return html;
152001
+ });
152002
+ } else if (mode === 'assistant') {
152003
+ data.answer.choices.forEach(item => {
152004
+ let answer = item.message.content; // callback(answer);
152005
+
152006
+ return answer;
152007
+ });
152008
+ } else {
152009
+ // mode===analyze
152010
+
152011
+ /*
152012
+ Handle Unrelated Request
152013
+ If OpenAI Function Call stopped (when user is asking unrelated things), it returns:
152014
+ {
152015
+ role: 'assistant',
152016
+ content: 'The user is asking for information.'
152017
+ }
152018
+ */
152019
+ if (data.answer.role && data.answer.content) {
152020
+ // callback(JSON.stringify({new_section: true, others: true})); // Just create a new section
152021
+ // return;
152022
+ return JSON.stringify({
152023
+ new_section: true,
152024
+ others: true
152025
+ });
152026
+ } // callback(data.answer);
152027
+
152028
+
152029
+ return data.answer;
152030
+ }
152031
+ } catch (error) {
152032
+ if (error.name === 'AbortError') ; else {
152033
+ // CORS or code errors goes here
152034
+ console.error('Error:', error); // console.log('Error:\n'+error);
152035
+
152036
+ this.dictation.finish(); // Must be called after finished
152037
+ // Debug
152038
+
152039
+ if (this.builder.consoleLog) {
152040
+ if (!(error + '').includes('Failed to fetch')) {
152041
+ // Only if not CORS error
152042
+ let headers = { ...this.builder.headers,
152043
+ ...this.builder.defaultHeaders
152044
+ };
152045
+ const response = await fetch(this.builder.sendCommandUrl, {
152046
+ method: 'POST',
152047
+ headers,
152048
+ body: JSON.stringify(messages)
152049
+ });
152050
+ const data = await response.text();
152051
+ console.log('Response:\n' + data); // Shows code error
152052
+ }
152053
+ }
152054
+
152055
+ this.builder.showMessage(out('Service Unavailable.'));
152056
+ }
152057
+ }
152058
+ }
152059
+
151583
152060
  isCanvasMode() {
151584
152061
  let activeBox = this.builder.activeBox;
152062
+ if (!activeBox) return false;
151585
152063
  let canvasMode = activeBox.classList.contains('box-canvas');
151586
152064
  return canvasMode;
151587
152065
  }