MCPcopy Index your code
hub / github.com/microsoft/SkillOpt / _top_level_brace_objects

Function _top_level_brace_objects

skillopt/utils/json_utils.py:9–71  ·  view source on GitHub ↗

Return every balanced *top-level* ``{...}`` span in ``text``. Fully string/escape aware: braces inside quoted strings are ignored both when scanning for an object start AND while tracking depth inside one, so a ``{`` that appears in prose (e.g. ``'set it to {x}'``) is never mistaken for

(text: str)

Source from the content-addressed store, hash-verified

7
8
9def _top_level_brace_objects(text: str) -> list[str]:
10 """Return every balanced *top-level* ``{...}`` span in ``text``.
11
12 Fully string/escape aware: braces inside quoted strings are ignored both
13 when scanning for an object start AND while tracking depth inside one, so a
14 ``{`` that appears in prose (e.g. ``'set it to {x}'``) is never mistaken for
15 the start of a JSON object. Used to detect ambiguity: when a response carries
16 more than one top-level object we must not let a repair pass silently pick
17 one — it may pick the wrong (discarded) edit, strictly worse than None.
18 """
19 spans: list[str] = []
20 i, n = 0, len(text)
21 outer_in_str = False
22 outer_esc = False
23 while i < n:
24 ch = text[i]
25 # Skip over braces that live *inside* a quoted string before any object
26 # has started — otherwise a `{` in prose like '"set it to {x}"' is wrongly
27 # treated as an object start, and the repair pass below turns non-JSON
28 # prose into a bogus dict (strictly worse than returning None).
29 if outer_in_str:
30 if outer_esc:
31 outer_esc = False
32 elif ch == "\\":
33 outer_esc = True
34 elif ch == '"':
35 outer_in_str = False
36 i += 1
37 continue
38 if ch == '"':
39 outer_in_str = True
40 i += 1
41 continue
42 if ch != "{":
43 i += 1
44 continue
45 depth = 0
46 in_str = False
47 esc = False
48 start = i
49 while i < n:
50 ch = text[i]
51 if in_str:
52 if esc:
53 esc = False
54 elif ch == "\\":
55 esc = True
56 elif ch == '"':
57 in_str = False
58 elif ch == '"':
59 in_str = True
60 elif ch == "{":
61 depth += 1
62 elif ch == "}":
63 depth -= 1
64 if depth == 0:
65 spans.append(text[start:i + 1])
66 i += 1

Calls

no outgoing calls