rigcode 0.1.0__py3-none-any.whl
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.
- rigcode/__init__.py +4 -0
- rigcode/__main__.py +4 -0
- rigcode/agent/__init__.py +0 -0
- rigcode/agent/loop.py +627 -0
- rigcode/agent/mentions.py +70 -0
- rigcode/agent/prompts.py +195 -0
- rigcode/agent/runtime.py +535 -0
- rigcode/agent/session.py +165 -0
- rigcode/agent/state.py +161 -0
- rigcode/cli.py +283 -0
- rigcode/config.py +284 -0
- rigcode/doctor.py +158 -0
- rigcode/extensions.py +275 -0
- rigcode/hooks.py +142 -0
- rigcode/llm/__init__.py +0 -0
- rigcode/llm/client.py +259 -0
- rigcode/llm/probe.py +229 -0
- rigcode/llm/stream.py +240 -0
- rigcode/llm/textmode.py +161 -0
- rigcode/llm/types.py +92 -0
- rigcode/llm/wire.py +76 -0
- rigcode/mcp.py +329 -0
- rigcode/permissions.py +221 -0
- rigcode/prompts/agents/coder.md +13 -0
- rigcode/prompts/agents/debugger.md +14 -0
- rigcode/prompts/agents/explore.md +22 -0
- rigcode/prompts/agents/plan.md +21 -0
- rigcode/prompts/agents/researcher.md +14 -0
- rigcode/prompts/agents/reviewer.md +18 -0
- rigcode/prompts/agents/verifier.md +15 -0
- rigcode/prompts/commit.md +5 -0
- rigcode/prompts/compact.md +24 -0
- rigcode/prompts/init.md +10 -0
- rigcode/prompts/system.md +27 -0
- rigcode/tools/__init__.py +0 -0
- rigcode/tools/base.py +245 -0
- rigcode/tools/bashsafety.py +262 -0
- rigcode/tools/editmatch.py +367 -0
- rigcode/tools/files.py +534 -0
- rigcode/tools/interact.py +69 -0
- rigcode/tools/search.py +309 -0
- rigcode/tools/shell.py +424 -0
- rigcode/tools/skill.py +37 -0
- rigcode/tools/task.py +54 -0
- rigcode/tools/todo.py +62 -0
- rigcode/tools/web.py +346 -0
- rigcode/ui/__init__.py +0 -0
- rigcode/ui/base.py +69 -0
- rigcode/ui/commands.py +581 -0
- rigcode/ui/plain.py +321 -0
- rigcode/ui/render.py +207 -0
- rigcode/ui/terminal.py +1154 -0
- rigcode/utils/__init__.py +0 -0
- rigcode/utils/files.py +176 -0
- rigcode/utils/frontmatter.py +38 -0
- rigcode/utils/globmatch.py +91 -0
- rigcode/utils/jsonrepair.py +110 -0
- rigcode/utils/paths.py +83 -0
- rigcode/utils/text.py +105 -0
- rigcode-0.1.0.dist-info/METADATA +203 -0
- rigcode-0.1.0.dist-info/RECORD +64 -0
- rigcode-0.1.0.dist-info/WHEEL +4 -0
- rigcode-0.1.0.dist-info/entry_points.txt +2 -0
- rigcode-0.1.0.dist-info/licenses/LICENSE +21 -0
|
File without changes
|
rigcode/utils/files.py
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"""File helpers: project file listing (gitignore-aware), text IO that preserves
|
|
2
|
+
encoding and line endings, binary detection."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import locale
|
|
6
|
+
import os
|
|
7
|
+
import re
|
|
8
|
+
import subprocess
|
|
9
|
+
import tempfile
|
|
10
|
+
from bisect import bisect_left, bisect_right
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from rigcode.utils.text import detect_bom_encoding
|
|
15
|
+
|
|
16
|
+
DEFAULT_IGNORED_DIRS = {
|
|
17
|
+
".git", ".hg", ".svn", "node_modules", "__pycache__", ".venv", "venv", "env",
|
|
18
|
+
".tox", ".nox", ".mypy_cache", ".pytest_cache", ".ruff_cache", ".cache",
|
|
19
|
+
".next", ".nuxt", ".svelte-kit", ".turbo", ".parcel-cache", ".gradle", ".idea",
|
|
20
|
+
"target", "dist", "build", "out", ".eggs", "coverage", ".terraform",
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
BINARY_EXTENSIONS = {
|
|
24
|
+
".png", ".jpg", ".jpeg", ".gif", ".bmp", ".ico", ".webp", ".tif", ".tiff", ".psd",
|
|
25
|
+
".pdf", ".zip", ".gz", ".tgz", ".bz2", ".xz", ".7z", ".rar", ".tar", ".jar", ".war",
|
|
26
|
+
".exe", ".dll", ".so", ".dylib", ".bin", ".o", ".a", ".lib", ".obj", ".class",
|
|
27
|
+
".pyc", ".pyo", ".whl", ".mp3", ".mp4", ".mov", ".avi", ".mkv", ".wav", ".flac",
|
|
28
|
+
".ogg", ".woff", ".woff2", ".ttf", ".otf", ".eot", ".sqlite", ".db", ".npy", ".npz",
|
|
29
|
+
".pkl", ".pt", ".pth", ".onnx", ".safetensors", ".gguf", ".h5", ".parquet", ".xlsx",
|
|
30
|
+
".xls", ".docx", ".pptx", ".iso", ".dmg",
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp"}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def git_ls_files(root: Path, timeout: float = 20) -> list[str] | None:
|
|
37
|
+
"""Tracked + untracked-but-not-ignored files under root, relative POSIX paths.
|
|
38
|
+
Returns None if root is not inside a git work tree."""
|
|
39
|
+
try:
|
|
40
|
+
proc = subprocess.run(
|
|
41
|
+
["git", "-C", str(root), "ls-files", "-z", "--cached", "--others", "--exclude-standard"],
|
|
42
|
+
capture_output=True, timeout=timeout,
|
|
43
|
+
)
|
|
44
|
+
except (OSError, subprocess.SubprocessError):
|
|
45
|
+
return None
|
|
46
|
+
if proc.returncode != 0:
|
|
47
|
+
return None
|
|
48
|
+
out = proc.stdout.decode("utf-8", errors="replace")
|
|
49
|
+
seen, files = set(), []
|
|
50
|
+
for f in out.split("\0"):
|
|
51
|
+
if f and f not in seen:
|
|
52
|
+
seen.add(f)
|
|
53
|
+
files.append(f)
|
|
54
|
+
return files
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def walk_files(root: Path, max_files: int = 100_000, ignored_dirs: set[str] | None = None) -> list[str]:
|
|
58
|
+
ignored = DEFAULT_IGNORED_DIRS if ignored_dirs is None else ignored_dirs
|
|
59
|
+
files: list[str] = []
|
|
60
|
+
root_str = str(root)
|
|
61
|
+
for dirpath, dirnames, filenames in os.walk(root_str):
|
|
62
|
+
dirnames[:] = sorted(d for d in dirnames if d not in ignored and not d.endswith(".egg-info"))
|
|
63
|
+
rel_dir = os.path.relpath(dirpath, root_str)
|
|
64
|
+
for name in sorted(filenames):
|
|
65
|
+
rel = name if rel_dir == "." else os.path.join(rel_dir, name)
|
|
66
|
+
files.append(rel.replace("\\", "/"))
|
|
67
|
+
if len(files) >= max_files:
|
|
68
|
+
return files
|
|
69
|
+
return files
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def list_project_files(root: Path, max_files: int = 100_000) -> list[str]:
|
|
73
|
+
"""Gitignore-aware listing when possible, falling back to a pruned walk."""
|
|
74
|
+
files = git_ls_files(root)
|
|
75
|
+
if files is None:
|
|
76
|
+
return walk_files(root, max_files)
|
|
77
|
+
# git lists deleted-but-tracked files too; filter lazily-cheap here.
|
|
78
|
+
out = []
|
|
79
|
+
for f in files:
|
|
80
|
+
if (root / f).exists():
|
|
81
|
+
out.append(f)
|
|
82
|
+
if len(out) >= max_files:
|
|
83
|
+
break
|
|
84
|
+
return out
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def is_binary_file(path: Path, sniff: int = 8192) -> bool:
|
|
88
|
+
if path.suffix.lower() in BINARY_EXTENSIONS:
|
|
89
|
+
return True
|
|
90
|
+
try:
|
|
91
|
+
with open(path, "rb") as fh:
|
|
92
|
+
chunk = fh.read(sniff)
|
|
93
|
+
except OSError:
|
|
94
|
+
return False
|
|
95
|
+
if not chunk:
|
|
96
|
+
return False
|
|
97
|
+
if detect_bom_encoding(chunk):
|
|
98
|
+
return False
|
|
99
|
+
if b"\0" in chunk:
|
|
100
|
+
return True
|
|
101
|
+
# Lots of control characters -> binary.
|
|
102
|
+
ctrl = sum(1 for b in chunk if b < 9 or (13 < b < 32))
|
|
103
|
+
return ctrl / len(chunk) > 0.3
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
@dataclass
|
|
107
|
+
class TextFile:
|
|
108
|
+
raw: str # decoded text exactly as on disk (original line endings)
|
|
109
|
+
text: str # LF view: CRLF normalized to \n
|
|
110
|
+
encoding: str
|
|
111
|
+
newline: str # dominant line ending: "\n" or "\r\n"
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def read_text_file(path: Path) -> TextFile:
|
|
115
|
+
data = path.read_bytes()
|
|
116
|
+
enc = detect_bom_encoding(data)
|
|
117
|
+
raw = None
|
|
118
|
+
if enc:
|
|
119
|
+
raw = data.decode(enc)
|
|
120
|
+
else:
|
|
121
|
+
for candidate in ("utf-8", locale.getpreferredencoding(False), "cp932", "latin-1"):
|
|
122
|
+
try:
|
|
123
|
+
raw = data.decode(candidate)
|
|
124
|
+
enc = candidate
|
|
125
|
+
break
|
|
126
|
+
except (UnicodeDecodeError, LookupError):
|
|
127
|
+
continue
|
|
128
|
+
assert raw is not None and enc is not None
|
|
129
|
+
crlf = raw.count("\r\n")
|
|
130
|
+
lf = raw.count("\n") - crlf
|
|
131
|
+
newline = "\r\n" if crlf > lf else "\n"
|
|
132
|
+
text = raw.replace("\r\n", "\n") if crlf else raw
|
|
133
|
+
return TextFile(raw=raw, text=text, encoding=enc, newline=newline)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
class LFMap:
|
|
137
|
+
"""Maps offsets in the LF view of a text back to the raw (CRLF-containing) text."""
|
|
138
|
+
|
|
139
|
+
def __init__(self, raw: str) -> None:
|
|
140
|
+
cr = [m.start() for m in re.finditer("\r\n", raw)]
|
|
141
|
+
self._cr_lf = [p - j for j, p in enumerate(cr)] # LF-view index of each '\n' that had a '\r'
|
|
142
|
+
|
|
143
|
+
def start(self, q: int) -> int:
|
|
144
|
+
return q + bisect_left(self._cr_lf, q)
|
|
145
|
+
|
|
146
|
+
def end(self, q: int) -> int:
|
|
147
|
+
return q + bisect_right(self._cr_lf, q - 1) if q > 0 else 0
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def write_text_file(path: Path, text: str, encoding: str = "utf-8", newline: str | None = "\n") -> None:
|
|
151
|
+
"""Atomic-ish write: temp file in the same directory, then replace.
|
|
152
|
+
newline="\\r\\n" converts LF to CRLF; None writes the text untouched."""
|
|
153
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
154
|
+
if newline == "\r\n":
|
|
155
|
+
text = text.replace("\r\n", "\n").replace("\n", "\r\n")
|
|
156
|
+
data = text.encode(encoding, errors="strict")
|
|
157
|
+
fd, tmp = tempfile.mkstemp(prefix=".rigcode-", suffix=".tmp", dir=str(path.parent))
|
|
158
|
+
try:
|
|
159
|
+
with os.fdopen(fd, "wb") as fh:
|
|
160
|
+
fh.write(data)
|
|
161
|
+
try:
|
|
162
|
+
mode = path.stat().st_mode
|
|
163
|
+
os.chmod(tmp, mode)
|
|
164
|
+
except OSError:
|
|
165
|
+
pass
|
|
166
|
+
os.replace(tmp, path)
|
|
167
|
+
except BaseException:
|
|
168
|
+
try:
|
|
169
|
+
os.unlink(tmp)
|
|
170
|
+
except OSError:
|
|
171
|
+
pass
|
|
172
|
+
raise
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def atomic_write_text(path: Path, text: str) -> None:
|
|
176
|
+
write_text_file(path, text, "utf-8", "\n")
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""Minimal YAML front-matter parsing for agent, skill and command files."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import re
|
|
5
|
+
|
|
6
|
+
import yaml
|
|
7
|
+
|
|
8
|
+
_FM_RE = re.compile(r"\A?---[ \t]*\r?\n(.*?)\r?\n---[ \t]*(?:\r?\n|\Z)(.*)\Z", re.S)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def parse_frontmatter(text: str) -> tuple[dict, str]:
|
|
12
|
+
"""Split '---\\nkey: value\\n---\\nbody' into (meta, body). Never raises."""
|
|
13
|
+
m = _FM_RE.match(text)
|
|
14
|
+
if not m:
|
|
15
|
+
return {}, text
|
|
16
|
+
raw, body = m.group(1), m.group(2)
|
|
17
|
+
try:
|
|
18
|
+
meta = yaml.safe_load(raw) or {}
|
|
19
|
+
if not isinstance(meta, dict):
|
|
20
|
+
meta = {}
|
|
21
|
+
except yaml.YAMLError:
|
|
22
|
+
meta = {}
|
|
23
|
+
for line in raw.splitlines():
|
|
24
|
+
if ":" in line and not line.startswith((" ", "\t")):
|
|
25
|
+
k, v = line.split(":", 1)
|
|
26
|
+
meta[k.strip()] = v.strip().strip('"').strip("'")
|
|
27
|
+
return meta, body.lstrip("\r\n")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def as_list(value) -> list[str]:
|
|
31
|
+
"""Accept 'a, b', ['a', 'b'] or None."""
|
|
32
|
+
if value is None:
|
|
33
|
+
return []
|
|
34
|
+
if isinstance(value, str):
|
|
35
|
+
return [v.strip() for v in value.split(",") if v.strip()]
|
|
36
|
+
if isinstance(value, (list, tuple)):
|
|
37
|
+
return [str(v).strip() for v in value if str(v).strip()]
|
|
38
|
+
return [str(value)]
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"""Glob pattern -> regex translation with **, {a,b}, [abc] support."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import functools
|
|
5
|
+
import os
|
|
6
|
+
import re
|
|
7
|
+
|
|
8
|
+
_CASE_INSENSITIVE = os.name == "nt"
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _translate(pat: str) -> str:
|
|
12
|
+
i, n = 0, len(pat)
|
|
13
|
+
res: list[str] = []
|
|
14
|
+
while i < n:
|
|
15
|
+
c = pat[i]
|
|
16
|
+
if c == "*":
|
|
17
|
+
if pat.startswith("**", i):
|
|
18
|
+
if pat.startswith("**/", i):
|
|
19
|
+
res.append("(?:.*/)?")
|
|
20
|
+
i += 3
|
|
21
|
+
continue
|
|
22
|
+
res.append(".*")
|
|
23
|
+
i += 2
|
|
24
|
+
continue
|
|
25
|
+
res.append("[^/]*")
|
|
26
|
+
elif c == "?":
|
|
27
|
+
res.append("[^/]")
|
|
28
|
+
elif c == "[":
|
|
29
|
+
j = pat.find("]", i + 1)
|
|
30
|
+
if j == -1:
|
|
31
|
+
res.append(re.escape(c))
|
|
32
|
+
else:
|
|
33
|
+
body = pat[i + 1 : j]
|
|
34
|
+
if body.startswith("!"):
|
|
35
|
+
body = "^" + body[1:]
|
|
36
|
+
res.append("[" + body.replace("\\", "\\\\") + "]")
|
|
37
|
+
i = j
|
|
38
|
+
elif c == "{":
|
|
39
|
+
depth, j = 0, i
|
|
40
|
+
while j < n:
|
|
41
|
+
if pat[j] == "{":
|
|
42
|
+
depth += 1
|
|
43
|
+
elif pat[j] == "}":
|
|
44
|
+
depth -= 1
|
|
45
|
+
if depth == 0:
|
|
46
|
+
break
|
|
47
|
+
j += 1
|
|
48
|
+
if j < n:
|
|
49
|
+
alts = _split_top_level(pat[i + 1 : j])
|
|
50
|
+
res.append("(?:" + "|".join(_translate(a) for a in alts) + ")")
|
|
51
|
+
i = j
|
|
52
|
+
else:
|
|
53
|
+
res.append(re.escape(c))
|
|
54
|
+
else:
|
|
55
|
+
res.append(re.escape(c))
|
|
56
|
+
i += 1
|
|
57
|
+
return "".join(res)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _split_top_level(s: str) -> list[str]:
|
|
61
|
+
parts, depth, cur = [], 0, []
|
|
62
|
+
for ch in s:
|
|
63
|
+
if ch == "{":
|
|
64
|
+
depth += 1
|
|
65
|
+
elif ch == "}":
|
|
66
|
+
depth -= 1
|
|
67
|
+
if ch == "," and depth == 0:
|
|
68
|
+
parts.append("".join(cur))
|
|
69
|
+
cur = []
|
|
70
|
+
else:
|
|
71
|
+
cur.append(ch)
|
|
72
|
+
parts.append("".join(cur))
|
|
73
|
+
return parts
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@functools.lru_cache(maxsize=512)
|
|
77
|
+
def compile_glob(pattern: str) -> re.Pattern:
|
|
78
|
+
pattern = pattern.replace("\\", "/")
|
|
79
|
+
if pattern.startswith("./"):
|
|
80
|
+
pattern = pattern[2:]
|
|
81
|
+
flags = re.IGNORECASE if _CASE_INSENSITIVE else 0
|
|
82
|
+
return re.compile("^" + _translate(pattern) + "$", flags)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def glob_match(pattern: str, rel_path: str) -> bool:
|
|
86
|
+
return bool(compile_glob(pattern).match(rel_path.replace("\\", "/")))
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def basename_match(pattern: str, rel_path: str) -> bool:
|
|
90
|
+
"""Match a slash-less pattern against the file name only (gitignore style)."""
|
|
91
|
+
return bool(compile_glob(pattern).match(rel_path.replace("\\", "/").rsplit("/", 1)[-1]))
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""Lenient JSON parsing for model-generated tool arguments.
|
|
2
|
+
|
|
3
|
+
Open-weight models occasionally emit almost-JSON: code fences, trailing commas,
|
|
4
|
+
raw newlines inside strings, Python literals, or a missing closing brace.
|
|
5
|
+
We try progressively more aggressive repairs before giving up.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import ast
|
|
10
|
+
import json
|
|
11
|
+
import re
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _strip_fences(s: str) -> str:
|
|
15
|
+
s = s.strip()
|
|
16
|
+
m = re.match(r"^```[a-zA-Z0-9_-]*\s*\n?(.*?)\n?```$", s, re.S)
|
|
17
|
+
return m.group(1).strip() if m else s
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _escape_control_chars_in_strings(s: str) -> str:
|
|
21
|
+
"""Escape raw newlines/tabs that appear inside JSON string literals."""
|
|
22
|
+
out = []
|
|
23
|
+
in_str = False
|
|
24
|
+
esc = False
|
|
25
|
+
for ch in s:
|
|
26
|
+
if in_str:
|
|
27
|
+
if esc:
|
|
28
|
+
esc = False
|
|
29
|
+
out.append(ch)
|
|
30
|
+
elif ch == "\\":
|
|
31
|
+
esc = True
|
|
32
|
+
out.append(ch)
|
|
33
|
+
elif ch == '"':
|
|
34
|
+
in_str = False
|
|
35
|
+
out.append(ch)
|
|
36
|
+
elif ch == "\n":
|
|
37
|
+
out.append("\\n")
|
|
38
|
+
elif ch == "\r":
|
|
39
|
+
out.append("\\r")
|
|
40
|
+
elif ch == "\t":
|
|
41
|
+
out.append("\\t")
|
|
42
|
+
elif ord(ch) < 0x20:
|
|
43
|
+
out.append(f"\\u{ord(ch):04x}")
|
|
44
|
+
else:
|
|
45
|
+
out.append(ch)
|
|
46
|
+
else:
|
|
47
|
+
if ch == '"':
|
|
48
|
+
in_str = True
|
|
49
|
+
out.append(ch)
|
|
50
|
+
return "".join(out)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _close_brackets(s: str) -> str:
|
|
54
|
+
"""Append missing closing quotes/brackets for truncated JSON."""
|
|
55
|
+
stack = []
|
|
56
|
+
in_str = False
|
|
57
|
+
esc = False
|
|
58
|
+
for ch in s:
|
|
59
|
+
if in_str:
|
|
60
|
+
if esc:
|
|
61
|
+
esc = False
|
|
62
|
+
elif ch == "\\":
|
|
63
|
+
esc = True
|
|
64
|
+
elif ch == '"':
|
|
65
|
+
in_str = False
|
|
66
|
+
elif ch == '"':
|
|
67
|
+
in_str = True
|
|
68
|
+
elif ch in "{[":
|
|
69
|
+
stack.append("}" if ch == "{" else "]")
|
|
70
|
+
elif ch in "}]" and stack:
|
|
71
|
+
stack.pop()
|
|
72
|
+
tail = '"' if in_str else ""
|
|
73
|
+
return s + tail + "".join(reversed(stack))
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def loads_lenient(raw: str | None) -> tuple[object | None, str | None]:
|
|
77
|
+
"""Return (value, None) on success or (None, error_message)."""
|
|
78
|
+
if raw is None:
|
|
79
|
+
return {}, None
|
|
80
|
+
s = raw.strip()
|
|
81
|
+
if not s:
|
|
82
|
+
return {}, None
|
|
83
|
+
first_err = None
|
|
84
|
+
candidates = []
|
|
85
|
+
s1 = _strip_fences(s)
|
|
86
|
+
candidates.append(s1)
|
|
87
|
+
# Extract the outermost {...} if there is surrounding prose.
|
|
88
|
+
start, end = s1.find("{"), s1.rfind("}")
|
|
89
|
+
if start > 0 or (end != -1 and end < len(s1) - 1):
|
|
90
|
+
if start != -1 and end > start:
|
|
91
|
+
candidates.append(s1[start : end + 1])
|
|
92
|
+
for c in list(candidates):
|
|
93
|
+
fixed = _escape_control_chars_in_strings(c)
|
|
94
|
+
fixed = re.sub(r",\s*([}\]])", r"\1", fixed)
|
|
95
|
+
candidates.append(fixed)
|
|
96
|
+
candidates.append(_close_brackets(fixed))
|
|
97
|
+
for c in candidates:
|
|
98
|
+
try:
|
|
99
|
+
return json.loads(c), None
|
|
100
|
+
except json.JSONDecodeError as e:
|
|
101
|
+
first_err = first_err or e
|
|
102
|
+
# Python-literal style dicts: {'a': True, 'b': None}
|
|
103
|
+
try:
|
|
104
|
+
val = ast.literal_eval(s1)
|
|
105
|
+
if isinstance(val, (dict, list)):
|
|
106
|
+
return json.loads(json.dumps(val)), None
|
|
107
|
+
except (ValueError, SyntaxError, TypeError, MemoryError, RecursionError):
|
|
108
|
+
pass
|
|
109
|
+
msg = f"{first_err.msg} at line {first_err.lineno} column {first_err.colno}" if first_err else "unparseable"
|
|
110
|
+
return None, msg
|
rigcode/utils/paths.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""Path helpers: resolving model-supplied paths, display paths, rigcode home."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import hashlib
|
|
5
|
+
import os
|
|
6
|
+
import re
|
|
7
|
+
import subprocess
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
IS_WINDOWS = os.name == "nt"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def rigcode_home() -> Path:
|
|
14
|
+
"""Global config/data directory (~/.rigcode, overridable with RIGCODE_HOME)."""
|
|
15
|
+
env = os.environ.get("RIGCODE_HOME")
|
|
16
|
+
return Path(env).expanduser() if env else Path.home() / ".rigcode"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def resolve_path(raw: str, cwd: Path) -> Path:
|
|
20
|
+
"""Turn a model-supplied path into an absolute, normalized Path.
|
|
21
|
+
|
|
22
|
+
Accepts absolute or cwd-relative paths, ~, quotes, and on Windows also
|
|
23
|
+
Git-Bash (/d/foo) and WSL (/mnt/d/foo) style paths.
|
|
24
|
+
"""
|
|
25
|
+
p = (raw or "").strip().strip('"').strip("'")
|
|
26
|
+
if IS_WINDOWS:
|
|
27
|
+
m = re.match(r"^/mnt/([a-zA-Z])(/.*)?$", p) or re.match(r"^/([a-zA-Z])(/.*)?$", p)
|
|
28
|
+
if m:
|
|
29
|
+
p = f"{m.group(1).upper()}:{m.group(2) or '/'}"
|
|
30
|
+
p = os.path.expanduser(p)
|
|
31
|
+
path = Path(p)
|
|
32
|
+
if not path.is_absolute():
|
|
33
|
+
path = cwd / path
|
|
34
|
+
return Path(os.path.normpath(str(path)))
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def is_within(path: Path, root: Path) -> bool:
|
|
38
|
+
try:
|
|
39
|
+
a = os.path.normcase(os.path.abspath(str(path)))
|
|
40
|
+
b = os.path.normcase(os.path.abspath(str(root)))
|
|
41
|
+
return a == b or a.startswith(b.rstrip("\\/") + os.sep)
|
|
42
|
+
except (ValueError, OSError):
|
|
43
|
+
return False
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def is_within_any(path: Path, roots: list[Path]) -> bool:
|
|
47
|
+
return any(is_within(path, r) for r in roots)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def display_path(path: Path | str, cwd: Path) -> str:
|
|
51
|
+
"""Short path for display: relative to cwd when inside it, else absolute."""
|
|
52
|
+
path = Path(path)
|
|
53
|
+
if is_within(path, cwd):
|
|
54
|
+
try:
|
|
55
|
+
rel = os.path.relpath(str(path), str(cwd))
|
|
56
|
+
return rel.replace("\\", "/") if rel != "." else "."
|
|
57
|
+
except ValueError:
|
|
58
|
+
pass
|
|
59
|
+
return str(path).replace("\\", "/")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def find_git_root(start: Path) -> Path | None:
|
|
63
|
+
cur = start.resolve()
|
|
64
|
+
for parent in [cur, *cur.parents]:
|
|
65
|
+
if (parent / ".git").exists():
|
|
66
|
+
return parent
|
|
67
|
+
return None
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def git_available() -> bool:
|
|
71
|
+
try:
|
|
72
|
+
subprocess.run(["git", "--version"], capture_output=True, timeout=5)
|
|
73
|
+
return True
|
|
74
|
+
except (OSError, subprocess.SubprocessError):
|
|
75
|
+
return False
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def project_slug(path: Path) -> str:
|
|
79
|
+
"""Stable, filesystem-safe directory name for per-project data."""
|
|
80
|
+
norm = os.path.normcase(str(path.resolve()))
|
|
81
|
+
name = re.sub(r"[^A-Za-z0-9._-]+", "-", path.name or "root").strip("-")[:40] or "root"
|
|
82
|
+
digest = hashlib.sha1(norm.encode("utf-8")).hexdigest()[:10]
|
|
83
|
+
return f"{name}-{digest}"
|
rigcode/utils/text.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"""Text helpers: truncation, ANSI stripping, decoding, token estimates."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import codecs
|
|
5
|
+
import locale
|
|
6
|
+
import re
|
|
7
|
+
|
|
8
|
+
_ANSI_RE = re.compile(r"\x1b\[[0-9;?]*[ -/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b[@-Z\\-_]")
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def strip_ansi(text: str) -> str:
|
|
12
|
+
return _ANSI_RE.sub("", text)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def decode_output(data: bytes) -> str:
|
|
16
|
+
"""Decode subprocess output: UTF-8 first, then the console/locale codepage."""
|
|
17
|
+
if not data:
|
|
18
|
+
return ""
|
|
19
|
+
try:
|
|
20
|
+
return data.decode("utf-8")
|
|
21
|
+
except UnicodeDecodeError:
|
|
22
|
+
pass
|
|
23
|
+
for enc in (locale.getpreferredencoding(False), "cp932", "cp1252"):
|
|
24
|
+
try:
|
|
25
|
+
return data.decode(enc)
|
|
26
|
+
except (UnicodeDecodeError, LookupError):
|
|
27
|
+
continue
|
|
28
|
+
return data.decode("utf-8", errors="replace")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def truncate_middle(text: str, max_chars: int, head_ratio: float = 0.35) -> tuple[str, bool]:
|
|
32
|
+
"""Keep the head and tail of long text, eliding the middle on a line boundary."""
|
|
33
|
+
if max_chars <= 0 or len(text) <= max_chars:
|
|
34
|
+
return text, False
|
|
35
|
+
head_len = int(max_chars * head_ratio)
|
|
36
|
+
tail_len = max_chars - head_len
|
|
37
|
+
head = text[:head_len]
|
|
38
|
+
tail = text[-tail_len:]
|
|
39
|
+
nl = head.rfind("\n")
|
|
40
|
+
if nl > head_len * 0.6:
|
|
41
|
+
head = head[: nl + 1]
|
|
42
|
+
nl = tail.find("\n")
|
|
43
|
+
if 0 <= nl < tail_len * 0.4:
|
|
44
|
+
tail = tail[nl + 1 :]
|
|
45
|
+
omitted = len(text) - len(head) - len(tail)
|
|
46
|
+
omitted_lines = text.count("\n", len(head), len(text) - len(tail))
|
|
47
|
+
marker = f"\n... [{omitted} characters / ~{omitted_lines} lines omitted] ...\n"
|
|
48
|
+
return head + marker + tail, True
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def truncate_line(line: str, max_len: int) -> str:
|
|
52
|
+
if len(line) <= max_len:
|
|
53
|
+
return line
|
|
54
|
+
return line[:max_len] + f"... [line truncated, {len(line) - max_len} more chars]"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def one_line(text: str, max_len: int = 80) -> str:
|
|
58
|
+
s = " ".join((text or "").split())
|
|
59
|
+
return s if len(s) <= max_len else s[: max_len - 1] + "…"
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def est_tokens(text: str) -> int:
|
|
63
|
+
"""Rough token estimate: ~4 chars/token for ASCII, ~1 token per CJK char."""
|
|
64
|
+
if not text:
|
|
65
|
+
return 0
|
|
66
|
+
non_ascii = sum(1 for ch in text if ord(ch) > 127)
|
|
67
|
+
return int((len(text) - non_ascii) / 3.8 + non_ascii * 0.9) + 1
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def number_lines(lines: list[str], start: int = 1, max_line_len: int = 2000) -> str:
|
|
71
|
+
"""cat -n style: right-aligned line number, tab, content."""
|
|
72
|
+
out = []
|
|
73
|
+
for i, line in enumerate(lines, start):
|
|
74
|
+
out.append(f"{i:>6}\t{truncate_line(line, max_line_len)}")
|
|
75
|
+
return "\n".join(out)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def detect_bom_encoding(raw: bytes) -> str | None:
|
|
79
|
+
if raw.startswith(codecs.BOM_UTF8):
|
|
80
|
+
return "utf-8-sig"
|
|
81
|
+
if raw.startswith(codecs.BOM_UTF16_LE) or raw.startswith(codecs.BOM_UTF16_BE):
|
|
82
|
+
return "utf-16"
|
|
83
|
+
return None
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def fmt_duration(seconds: float) -> str:
|
|
87
|
+
seconds = max(0.0, float(seconds))
|
|
88
|
+
if seconds < 60:
|
|
89
|
+
return f"{seconds:.1f}s" if seconds < 10 else f"{seconds:.0f}s"
|
|
90
|
+
m, s = divmod(int(round(seconds)), 60)
|
|
91
|
+
if m < 60:
|
|
92
|
+
return f"{m}m {s:02d}s"
|
|
93
|
+
h, m = divmod(m, 60)
|
|
94
|
+
return f"{h}h {m:02d}m"
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def fmt_tokens(n: int | float | None) -> str:
|
|
98
|
+
if not n:
|
|
99
|
+
return "0"
|
|
100
|
+
n = float(n)
|
|
101
|
+
if n < 1000:
|
|
102
|
+
return f"{n:.0f}"
|
|
103
|
+
if n < 100_000:
|
|
104
|
+
return f"{n / 1000:.1f}k"
|
|
105
|
+
return f"{n / 1000:.0f}k"
|