File size: 2,853 Bytes
dcd5e1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Test script to verify smart HTML context reduction
function getSmartHtmlContext(html, selectedElementHtml) {
  // If no selected element, use the original HTML but truncate if too large
  if (!selectedElementHtml) {
    return html.length > 10000 ? 
      html.substring(0, 10000) + "...\n<!-- HTML truncated for context efficiency -->" : 
      html;
  }

  // If there's a selected element, provide minimal context around it
  const selectedIndex = html.indexOf(selectedElementHtml);
  if (selectedIndex === -1) {
    // Fallback: if selected element not found, use truncated HTML
    return html.length > 8000 ? 
      html.substring(0, 8000) + "...\n<!-- HTML truncated for context efficiency -->" : 
      html;
  }

  // Provide context around the selected element
  const contextSize = 2000; // Characters before and after
  const start = Math.max(0, selectedIndex - contextSize);
  const end = Math.min(html.length, selectedIndex + selectedElementHtml.length + contextSize);
  
  let contextHtml = html.substring(start, end);
  
  // Add markers if we truncated
  if (start > 0) {
    contextHtml = "...\n<!-- Context starts here -->\n" + contextHtml;
  }
  if (end < html.length) {
    contextHtml = contextHtml + "\n<!-- Context ends here -->\n...";
  }
  
  return contextHtml;
}

console.log("Testing smart HTML context reduction:\n");

// Test 1: Large HTML without selected element
const largeHtml = "<html><body>" + "x".repeat(15000) + "</body></html>";
const result1 = getSmartHtmlContext(largeHtml);
console.log("1. Large HTML without selection:");
console.log(`   Original: ${largeHtml.length} chars`);
console.log(`   Reduced:  ${result1.length} chars`);
console.log(`   Savings:  ${largeHtml.length - result1.length} chars\n`);

// Test 2: Large HTML with selected element
const htmlWithElement = "<html><head><title>Test</title></head><body><div>Start</div>" + "x".repeat(10000) + "<button id='test'>Click me</button>" + "y".repeat(10000) + "</body></html>";
const selectedElement = "<button id='test'>Click me</button>";
const result2 = getSmartHtmlContext(htmlWithElement, selectedElement);
console.log("2. Large HTML with selected element:");
console.log(`   Original: ${htmlWithElement.length} chars`);
console.log(`   Reduced:  ${result2.length} chars`);
console.log(`   Savings:  ${htmlWithElement.length - result2.length} chars`);
console.log(`   Context includes selected element: ${result2.includes(selectedElement)}\n`);

// Test 3: Small HTML (should not be truncated)
const smallHtml = "<div>Small content</div>";
const result3 = getSmartHtmlContext(smallHtml);
console.log("3. Small HTML:");
console.log(`   Original: ${smallHtml.length} chars`);
console.log(`   Reduced:  ${result3.length} chars`);
console.log(`   No truncation: ${smallHtml === result3}\n`);

console.log("✅ All tests completed successfully!");