browser-assistant 2.9.9 → 2.9.10

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.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "manifest_version": 3,
3
3
  "name": "Browser Assistant",
4
- "version": "2.9.9",
4
+ "version": "2.9.10",
5
5
  "description": "Scans webpages for MCQs and coding problems. Powered by Groq (cloud) and Ollama (local fallback).",
6
6
  "permissions": [
7
7
  "activeTab",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "browser-assistant",
3
- "version": "2.9.9",
3
+ "version": "2.9.10",
4
4
  "description": "Chrome extension and local Python backend for exploring webpage questions and coding exercises",
5
5
  "license": "MIT",
6
6
  "repository": {
package/server/main.py CHANGED
@@ -28,7 +28,8 @@ from local_security import install_local_guards, redact, sanitize_data
28
28
  from vision_images import (compress_image_base64, unique_image_blocks, collect_image_evidence,
29
29
  cross_check_visual_answers, relative_percentage_question, RELATIVE_PERCENTAGE_INSTRUCTION,
30
30
  answer_permutation, cross_check_text_answers, solve_matrix_choice, solve_row_parallel_choice,
31
- reading_response_constraints, VisionRateLimitError, VisionUnconfirmedError)
31
+ ordering_question, solve_sorted_list_merge_order, reading_response_constraints,
32
+ VisionRateLimitError, VisionUnconfirmedError)
32
33
 
33
34
  logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
34
35
  logger = logging.getLogger("browser-assistant")
@@ -191,7 +192,7 @@ async def lifespan(_app: FastAPI):
191
192
  client.close()
192
193
 
193
194
 
194
- app = FastAPI(title="Browser Assistant API", version="2.9.9", lifespan=lifespan)
195
+ app = FastAPI(title="Browser Assistant API", version="2.9.10", lifespan=lifespan)
195
196
  install_local_guards(app)
196
197
 
197
198
  @app.exception_handler(HTTPException)
@@ -1278,8 +1279,15 @@ async def solve_questions(content: PageContent):
1278
1279
  except ValueError as exc:
1279
1280
  raise HTTPException(status_code=422, detail=str(exc)) from exc
1280
1281
  system_prompt = build_system_prompt(content.practice_type, language)
1282
+ ordering = ordering_question(trimmed_text)
1281
1283
  reading_constraints = reading_response_constraints(trimmed_text) if content.require_answer_agreement else None
1282
- if reading_constraints:
1284
+ if ordering:
1285
+ system_prompt = (
1286
+ 'Arrange all steps in the order required by the problem. Check prerequisites and use each displayed '
1287
+ 'step number once. Return exactly one JSON array item with type "mcq", the question, a brief '
1288
+ 'reasoning string, and an answer array of the original step numbers in their new order.'
1289
+ )
1290
+ elif reading_constraints:
1283
1291
  _, _, keyword = reading_constraints
1284
1292
  rule = (f'Use two to five words including the unchanged word {keyword}. Preserve the meaning of the original sentence. '
1285
1293
  if keyword else 'Use exactly one word. ')
@@ -1317,6 +1325,11 @@ REPAIR MODE:
1317
1325
  )
1318
1326
  strict_code_review = require_code and (content.review_before_submit or not FAST_CODE_MODE or STRICT_REPAIR_REVIEW)
1319
1327
  verify_answers = not require_code and (content.verify_answer or content.require_answer_agreement or not FAST_SCAN_MODE)
1328
+ if ordering and not require_code:
1329
+ item = solve_sorted_list_merge_order(ordering)
1330
+ if item is not None:
1331
+ return {"results": json.dumps([item], ensure_ascii=False), "provider": "local merge-step check [verified]",
1332
+ "language": language, "answer_verified": True, "code_reviewed": False}
1320
1333
  if not require_code and not content.require_answer_agreement and content.practice_type in {'auto', 'mcq', 'math', 'aptitude'}:
1321
1334
  local_result = local_choice_result(trimmed_text, language)
1322
1335
  if local_result is not None:
@@ -167,6 +167,55 @@ def answer_permutation(value):
167
167
  and sorted(value) == list(range(1, len(value) + 1)))
168
168
 
169
169
 
170
+ def ordering_question(text):
171
+ prompt, separator, block = text.strip().rpartition('\nReorder ALL items.')
172
+ if not separator or not prompt:
173
+ return None
174
+ rows = block.splitlines()[1:]
175
+ if not 2 <= len(rows) <= 30:
176
+ return None
177
+ options = []
178
+ for index, line in enumerate(rows, 1):
179
+ match = re.fullmatch(rf'{index}\.\s+(\S.*)', line.strip())
180
+ if not match:
181
+ return None
182
+ options.append(match[1])
183
+ return prompt, options
184
+
185
+
186
+ def solve_sorted_list_merge_order(ordering):
187
+ if not ordering:
188
+ return None
189
+ prompt, options = ordering
190
+ if prompt.strip().rstrip(':').lower() != 'rearrange the steps to merge two sorted linked lists':
191
+ return None
192
+ steps = (
193
+ 'initialize pointers for the first and second list',
194
+ 'create a dummy node to serve as the head of the merged list',
195
+ 'traverse both lists simultaneously',
196
+ 'compare elements of both lists and link the smaller node to the merged list',
197
+ 'append remaining nodes of either list to the merged list',
198
+ 'return the merged list starting from the next of the dummy node',
199
+ )
200
+ normalized = [' '.join(option.split()).lower().rstrip('.') for option in options]
201
+ if len(normalized) != len(steps) or set(normalized) != set(steps):
202
+ return None
203
+ return {
204
+ 'type': 'mcq', 'question': prompt, 'answer': [normalized.index(step) + 1 for step in steps],
205
+ 'reasoning': 'Initialize both pointers and the dummy head before traversal. Compare and link nodes while both '
206
+ 'lists have items, append the remaining tail, then return the node after the dummy head.',
207
+ }
208
+
209
+
210
+ def ordering_key(value, size):
211
+ if isinstance(value, str):
212
+ try:
213
+ value = json.loads(value)
214
+ except ValueError:
215
+ return None
216
+ return tuple(value) if answer_permutation(value) and len(value) == size else None
217
+
218
+
170
219
  MATRIX_SYNTAX = re.compile(r'\\begin\{(?:[bpBvV]?matrix|smallmatrix)\}')
171
220
 
172
221
 
@@ -449,11 +498,21 @@ async def cross_check_text_answers(messages, results, complete, *, question_text
449
498
  )
450
499
  check_messages = [{'role': 'system', 'content': instruction}, *messages[1:]]
451
500
  matrix_math = matrix_question(messages)
501
+ ordering = ordering_question(question_text)
502
+ if ordering and len(items) != 1:
503
+ raise VisionUnconfirmedError('Expected one answer containing all numbered steps.')
504
+ step_count = len(ordering[1]) if ordering else 0
452
505
  choices = displayed_single_choices(question_text) if len(items) == 1 else {}
453
506
  constraints = reading_response_constraints(question_text) if require_agreement and len(items) == 1 else None
454
- if require_agreement and not (choices or constraints):
507
+ if require_agreement and not (choices or constraints or step_count):
455
508
  raise VisionUnconfirmedError('Expected one linked answer with displayed options or a reading response format.')
456
- if constraints:
509
+ if step_count:
510
+ check_messages[0]['content'] = (
511
+ 'Independently order the displayed steps. Use every original item number once, check all prerequisite '
512
+ f'relationships, and return only JSON with readable=true and one answers entry with id=1. '
513
+ f'The answer value must be a JSON array of all {step_count} original item numbers in the correct order.'
514
+ )
515
+ elif constraints:
457
516
  _, _, keyword = constraints
458
517
  rule = (f'Use two to five words, including the unchanged word {keyword}. Preserve the meaning of the original sentence.'
459
518
  if keyword else 'Return exactly one word.')
@@ -461,6 +520,8 @@ async def cross_check_text_answers(messages, results, complete, *, question_text
461
520
  check_messages[0]['content'] = instruction
462
521
 
463
522
  def answer_key(entries):
523
+ if step_count:
524
+ return ordering_key(entries[0].get('answer'), step_count)
464
525
  if choices:
465
526
  return bind_displayed_choice(entries[0].get('answer'), choices, matrix_math=matrix_math)
466
527
  if constraints:
@@ -486,6 +547,11 @@ async def cross_check_text_answers(messages, results, complete, *, question_text
486
547
  except ValueError:
487
548
  current = None
488
549
  if current is not None and (current == first or current == previous):
550
+ if step_count:
551
+ if current != first:
552
+ items[0].pop('reasoning', None)
553
+ items[0]['answer'] = list(current)
554
+ return json.dumps(items, ensure_ascii=False)
489
555
  if choices:
490
556
  if current != first:
491
557
  items[0].pop('reasoning', None)
@@ -497,7 +563,7 @@ async def cross_check_text_answers(messages, results, complete, *, question_text
497
563
  items[0]['answer'] = ' '.join((items[0] if current == first else verified[0])['answer'].split())
498
564
  return json.dumps(items, ensure_ascii=False)
499
565
  return results
500
- if not (choices or constraints) and current is not None:
566
+ if not (choices or constraints or step_count) and current is not None:
501
567
  break
502
568
  if not attempt:
503
569
  previous = current
@@ -509,16 +575,18 @@ async def cross_check_text_answers(messages, results, complete, *, question_text
509
575
  # primary answer over blocking the automation entirely. The primary solve was
510
576
  # validated and the verification provider returned unusable JSON — a parsing
511
577
  # failure, not a disagreement — so a hard 422 is disproportionate.
512
- if not require_agreement and current is None and previous is None:
578
+ if not require_agreement and not step_count and current is None and previous is None:
513
579
  return results
514
580
  # For multi-answer questions (fill-in-the-blank), accept the primary answers
515
581
  # when the majority of individual answers agree with any independent solve.
516
- if not require_agreement and not choices and first is not None and len(first) > 1:
582
+ if not require_agreement and not choices and not step_count and first is not None and len(first) > 1:
517
583
  best = current or previous
518
584
  if best is not None:
519
585
  agreed = sum(a == b for a, b in zip(first, best))
520
586
  if agreed > len(first) // 2:
521
587
  return results
588
+ if step_count:
589
+ raise VisionUnconfirmedError('The independent checks could not confirm a complete order for the displayed steps.')
522
590
  raise VisionUnconfirmedError('The independent solves could not agree on the answer for this blank.' if constraints
523
591
  else 'The independent solves could not agree on the displayed answer.')
524
592