browser-assistant 2.9.43 → 2.9.44
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.
- package/extension/manifest.json +1 -1
- package/package.json +1 -1
- package/server/code_contract.py +42 -3
- package/server/main.py +4 -4
package/extension/manifest.json
CHANGED
package/package.json
CHANGED
package/server/code_contract.py
CHANGED
|
@@ -70,12 +70,51 @@ def function_header(node, data, language):
|
|
|
70
70
|
for start, stop in sorted(ignored, reverse=True):
|
|
71
71
|
header = header[:start-node.start_byte] + header[stop-node.start_byte:]
|
|
72
72
|
header = header.decode('utf-8')
|
|
73
|
+
elif language == 'python':
|
|
74
|
+
# Python comments between ':' and the first statement are not API.
|
|
75
|
+
spans = []
|
|
76
|
+
def comments(child):
|
|
77
|
+
if child.type == 'comment' and child.start_byte < end:
|
|
78
|
+
spans.append((child.start_byte, min(child.end_byte, end)))
|
|
79
|
+
for part in child.named_children:
|
|
80
|
+
comments(part)
|
|
81
|
+
comments(node)
|
|
82
|
+
for start, stop in sorted(spans, reverse=True):
|
|
83
|
+
header = header[:start-node.start_byte] + header[stop-node.start_byte:]
|
|
84
|
+
header = header.decode('utf-8')
|
|
73
85
|
else:
|
|
74
86
|
header = re.sub(r'/\*.*?\*/|//[^\n]*', '', header.decode('utf-8'), flags=re.DOTALL)
|
|
75
87
|
return re.sub(r'\s+', '', header)
|
|
76
88
|
|
|
77
89
|
|
|
78
|
-
def
|
|
90
|
+
def prepare_starter(source, language):
|
|
91
|
+
"""Recover a C++ portal scaffold without repairing generated output."""
|
|
92
|
+
if language != 'cpp':
|
|
93
|
+
return source
|
|
94
|
+
# An accidentally pasted Python program is not part of a C++ interface.
|
|
95
|
+
if re.search(r'\b(?:class|struct)\s+\w+\s*\{', source):
|
|
96
|
+
source = re.split(r'(?m)^def\s+\w+\s*\(', source, maxsplit=1)[0]
|
|
97
|
+
data = source.encode('utf-8')
|
|
98
|
+
root = get_parser('cpp').parse(data).root_node
|
|
99
|
+
balance = 0
|
|
100
|
+
def braces(node):
|
|
101
|
+
nonlocal balance
|
|
102
|
+
if node.type == '{' and not node.is_missing:
|
|
103
|
+
balance += 1
|
|
104
|
+
elif node.type == '}' and not node.is_missing:
|
|
105
|
+
balance -= 1
|
|
106
|
+
else:
|
|
107
|
+
for child in node.children:
|
|
108
|
+
braces(child)
|
|
109
|
+
braces(root)
|
|
110
|
+
if 0 < balance <= 8:
|
|
111
|
+
source += '\n' + '\n'.join('}' for _ in range(balance)) + ';\n'
|
|
112
|
+
return source
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def interface(source, language, *, starter=False):
|
|
116
|
+
if starter:
|
|
117
|
+
source = prepare_starter(source, language)
|
|
79
118
|
data = source.encode('utf-8')
|
|
80
119
|
root = get_parser(language).parse(data).root_node
|
|
81
120
|
classes, functions = set(), set()
|
|
@@ -114,7 +153,7 @@ def interface(source, language):
|
|
|
114
153
|
def validate_starter_interface(code, starter, language):
|
|
115
154
|
if not starter or language not in SUPPORTED:
|
|
116
155
|
return
|
|
117
|
-
classes, functions = interface(starter, language)
|
|
156
|
+
classes, functions = interface(starter, language, starter=True)
|
|
118
157
|
actual_classes, actual_functions = interface(code, language)
|
|
119
158
|
missing_classes = classes - actual_classes
|
|
120
159
|
if missing_classes:
|
|
@@ -139,7 +178,7 @@ def normalize_starter_code(code, starter, language):
|
|
|
139
178
|
"""Remove one added C++ driver only when the complete required interface remains."""
|
|
140
179
|
if language != 'cpp' or not starter:
|
|
141
180
|
return code
|
|
142
|
-
classes, functions = interface(starter, language)
|
|
181
|
+
classes, functions = interface(starter, language, starter=True)
|
|
143
182
|
if not (classes or functions) or any(not owner and re.search(r'^(?:int|signed(?:int)?|auto)main\(', header)
|
|
144
183
|
for owner, header, _access in functions):
|
|
145
184
|
return code
|
package/server/main.py
CHANGED
|
@@ -20,7 +20,7 @@ import sqlite3
|
|
|
20
20
|
from contextlib import asynccontextmanager, contextmanager
|
|
21
21
|
from datetime import datetime, timezone
|
|
22
22
|
from pathlib import Path
|
|
23
|
-
from code_contract import coding_samples, validate_starter_interface, normalize_starter_code
|
|
23
|
+
from code_contract import coding_samples, validate_starter_interface, normalize_starter_code, prepare_starter
|
|
24
24
|
from fastapi import Query, status
|
|
25
25
|
from fastapi.responses import FileResponse
|
|
26
26
|
from fastapi.exception_handlers import http_exception_handler
|
|
@@ -201,7 +201,7 @@ async def lifespan(_app: FastAPI):
|
|
|
201
201
|
client.close()
|
|
202
202
|
|
|
203
203
|
|
|
204
|
-
app = FastAPI(title="Browser Assistant API", version="2.9.
|
|
204
|
+
app = FastAPI(title="Browser Assistant API", version="2.9.44", lifespan=lifespan)
|
|
205
205
|
install_local_guards(app)
|
|
206
206
|
|
|
207
207
|
@app.exception_handler(HTTPException)
|
|
@@ -998,7 +998,7 @@ def build_ollama_code_messages(text: str, url: str, language: str, *,
|
|
|
998
998
|
starter_code: str | None = None, repair_attempt: int = 0) -> list:
|
|
999
999
|
"""Keep all coding evidence intact within the local fallback's input budget."""
|
|
1000
1000
|
if starter_code:
|
|
1001
|
-
text += '\n\nOriginal portal starter interface:\n' + starter_code
|
|
1001
|
+
text += '\n\nOriginal portal starter interface:\n' + prepare_starter(starter_code, language)
|
|
1002
1002
|
if len(text) > 18_000:
|
|
1003
1003
|
raise ValueError(
|
|
1004
1004
|
'Local Ollama fallback needs the full problem, code and feedback within 18000 characters. '
|
|
@@ -1105,7 +1105,7 @@ def starter_interface_instructions(starter_code: str | None, language: str) -> s
|
|
|
1105
1105
|
return ''
|
|
1106
1106
|
instructions = ('\nORIGINAL PORTAL STARTER CODE (immutable interface; preserve exact class and function '
|
|
1107
1107
|
'names, case, return types, parameter types, reference qualifiers, parameter order and access):\n'
|
|
1108
|
-
+ starter_code)
|
|
1108
|
+
+ prepare_starter(starter_code, language))
|
|
1109
1109
|
if language == 'cpp':
|
|
1110
1110
|
instructions += ('\nFor C++: complete the supplied function body inside its original class. '
|
|
1111
1111
|
'Copy the original function declaration and access exactly. '
|