owenkaplinsky commited on
Commit
959d85a
·
1 Parent(s): 4302df1

Fix input duplication bug

Browse files
Files changed (2) hide show
  1. project/src/blocks/text.js +21 -12
  2. project/src/index.js +31 -19
project/src/blocks/text.js CHANGED
@@ -130,18 +130,19 @@ Blockly.Extensions.registerMutator(
130
  // Check if we need to find existing reference blocks in the workspace
131
  // This happens after deserialization when the blocks exist but aren't tracked
132
  if (this.inputRefBlocks_.size === 0 && this.inputNames_ && this.inputNames_.length > 0) {
133
- const allBlocks = this.workspace.getAllBlocks(false);
134
  for (let i = 0; i < this.inputNames_.length; i++) {
135
  const name = this.inputNames_[i];
136
- const blockType = `input_reference_${name}`;
137
- // Find orphaned reference blocks that belong to this block
138
- const refBlock = allBlocks.find(b =>
139
- b.type === blockType &&
140
- b._ownerBlockId === this.id &&
141
- !b.getParent()
142
- );
143
- if (refBlock) {
144
- this.inputRefBlocks_.set(name, refBlock);
 
 
145
  }
146
  }
147
  }
@@ -309,8 +310,16 @@ Blockly.Extensions.registerMutator(
309
  this.moveInputBefore('X' + j, 'BODY');
310
 
311
  const blockType = createInputRefBlockType(name);
312
- if (!existingRefBlock) {
313
- // Only create a new reference block if none exists
 
 
 
 
 
 
 
 
314
  const refBlock = this.workspace.newBlock(blockType);
315
  refBlock.initSvg();
316
  refBlock.setDeletable(false);
 
130
  // Check if we need to find existing reference blocks in the workspace
131
  // This happens after deserialization when the blocks exist but aren't tracked
132
  if (this.inputRefBlocks_.size === 0 && this.inputNames_ && this.inputNames_.length > 0) {
 
133
  for (let i = 0; i < this.inputNames_.length; i++) {
134
  const name = this.inputNames_[i];
135
+ const input = this.getInput('X' + i);
136
+
137
+ // Check if there's already a connected block in this input
138
+ if (input && input.connection && input.connection.targetBlock()) {
139
+ const connectedBlock = input.connection.targetBlock();
140
+ const expectedType = `input_reference_${name}`;
141
+
142
+ // If this is the expected reference block, track it
143
+ if (connectedBlock.type === expectedType && connectedBlock._ownerBlockId === this.id) {
144
+ this.inputRefBlocks_.set(name, connectedBlock);
145
+ }
146
  }
147
  }
148
  }
 
310
  this.moveInputBefore('X' + j, 'BODY');
311
 
312
  const blockType = createInputRefBlockType(name);
313
+
314
+ // Check if there's already a block connected to this input
315
+ const currentlyConnected = input.connection ? input.connection.targetBlock() : null;
316
+
317
+ if (currentlyConnected && currentlyConnected.type === blockType) {
318
+ // There's already the correct reference block connected, just track it
319
+ currentlyConnected._ownerBlockId = this.id;
320
+ this.inputRefBlocks_.set(name, currentlyConnected);
321
+ } else if (!existingRefBlock) {
322
+ // Only create a new reference block if none exists and nothing is connected
323
  const refBlock = this.workspace.newBlock(blockType);
324
  refBlock.initSvg();
325
  refBlock.setDeletable(false);
project/src/index.js CHANGED
@@ -784,39 +784,51 @@ const updateChatCode = () => {
784
  try {
785
  load(ws);
786
 
787
- // After loading, create the reference blocks that should be in the inputs
788
  setTimeout(() => {
789
  const mutatorBlocks = ws.getAllBlocks(false).filter(b =>
790
  (b.type === 'create_mcp' || b.type === 'func_def')
791
  );
792
 
793
  for (const block of mutatorBlocks) {
 
 
 
 
 
794
  // Create reference blocks for each input if they don't exist
795
  if (block.inputNames_ && block.inputNames_.length > 0) {
796
  for (let i = 0; i < block.inputNames_.length; i++) {
797
  const name = block.inputNames_[i];
798
  const input = block.getInput('X' + i);
799
 
800
- // Only create if input exists AND has no connected block yet
801
- if (input && input.connection && !input.connection.targetBlock()) {
802
- // Create the reference block
803
- const blockType = `input_reference_${name}`;
804
- const refBlock = ws.newBlock(blockType);
805
- refBlock.initSvg();
806
- refBlock.setDeletable(false);
807
- refBlock._ownerBlockId = block.id;
808
- refBlock.render();
809
-
810
- // Connect it
811
- if (input.connection && refBlock.outputConnection) {
812
- input.connection.connect(refBlock.outputConnection);
813
- }
 
 
 
 
 
 
 
 
 
814
 
815
- // Track it
816
- if (!block.inputRefBlocks_) {
817
- block.inputRefBlocks_ = new Map();
818
  }
819
- block.inputRefBlocks_.set(name, refBlock);
820
  }
821
  }
822
  }
 
784
  try {
785
  load(ws);
786
 
787
+ // After loading, ensure reference blocks are properly connected and tracked
788
  setTimeout(() => {
789
  const mutatorBlocks = ws.getAllBlocks(false).filter(b =>
790
  (b.type === 'create_mcp' || b.type === 'func_def')
791
  );
792
 
793
  for (const block of mutatorBlocks) {
794
+ // Initialize the reference block map if needed
795
+ if (!block.inputRefBlocks_) {
796
+ block.inputRefBlocks_ = new Map();
797
+ }
798
+
799
  // Create reference blocks for each input if they don't exist
800
  if (block.inputNames_ && block.inputNames_.length > 0) {
801
  for (let i = 0; i < block.inputNames_.length; i++) {
802
  const name = block.inputNames_[i];
803
  const input = block.getInput('X' + i);
804
 
805
+ if (input && input.connection) {
806
+ const connectedBlock = input.connection.targetBlock();
807
+ const expectedType = `input_reference_${name}`;
808
+
809
+ // If there's already the correct block connected, just track it
810
+ if (connectedBlock && connectedBlock.type === expectedType) {
811
+ connectedBlock._ownerBlockId = block.id;
812
+ connectedBlock.setDeletable(false);
813
+ block.inputRefBlocks_.set(name, connectedBlock);
814
+ }
815
+ // Only create if input exists AND has no connected block yet
816
+ else if (!connectedBlock) {
817
+ // Create the reference block
818
+ const refBlock = ws.newBlock(expectedType);
819
+ refBlock.initSvg();
820
+ refBlock.setDeletable(false);
821
+ refBlock._ownerBlockId = block.id;
822
+ refBlock.render();
823
+
824
+ // Connect it
825
+ if (refBlock.outputConnection) {
826
+ input.connection.connect(refBlock.outputConnection);
827
+ }
828
 
829
+ // Track it
830
+ block.inputRefBlocks_.set(name, refBlock);
 
831
  }
 
832
  }
833
  }
834
  }