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.
Files changed (64) hide show
  1. rigcode/__init__.py +4 -0
  2. rigcode/__main__.py +4 -0
  3. rigcode/agent/__init__.py +0 -0
  4. rigcode/agent/loop.py +627 -0
  5. rigcode/agent/mentions.py +70 -0
  6. rigcode/agent/prompts.py +195 -0
  7. rigcode/agent/runtime.py +535 -0
  8. rigcode/agent/session.py +165 -0
  9. rigcode/agent/state.py +161 -0
  10. rigcode/cli.py +283 -0
  11. rigcode/config.py +284 -0
  12. rigcode/doctor.py +158 -0
  13. rigcode/extensions.py +275 -0
  14. rigcode/hooks.py +142 -0
  15. rigcode/llm/__init__.py +0 -0
  16. rigcode/llm/client.py +259 -0
  17. rigcode/llm/probe.py +229 -0
  18. rigcode/llm/stream.py +240 -0
  19. rigcode/llm/textmode.py +161 -0
  20. rigcode/llm/types.py +92 -0
  21. rigcode/llm/wire.py +76 -0
  22. rigcode/mcp.py +329 -0
  23. rigcode/permissions.py +221 -0
  24. rigcode/prompts/agents/coder.md +13 -0
  25. rigcode/prompts/agents/debugger.md +14 -0
  26. rigcode/prompts/agents/explore.md +22 -0
  27. rigcode/prompts/agents/plan.md +21 -0
  28. rigcode/prompts/agents/researcher.md +14 -0
  29. rigcode/prompts/agents/reviewer.md +18 -0
  30. rigcode/prompts/agents/verifier.md +15 -0
  31. rigcode/prompts/commit.md +5 -0
  32. rigcode/prompts/compact.md +24 -0
  33. rigcode/prompts/init.md +10 -0
  34. rigcode/prompts/system.md +27 -0
  35. rigcode/tools/__init__.py +0 -0
  36. rigcode/tools/base.py +245 -0
  37. rigcode/tools/bashsafety.py +262 -0
  38. rigcode/tools/editmatch.py +367 -0
  39. rigcode/tools/files.py +534 -0
  40. rigcode/tools/interact.py +69 -0
  41. rigcode/tools/search.py +309 -0
  42. rigcode/tools/shell.py +424 -0
  43. rigcode/tools/skill.py +37 -0
  44. rigcode/tools/task.py +54 -0
  45. rigcode/tools/todo.py +62 -0
  46. rigcode/tools/web.py +346 -0
  47. rigcode/ui/__init__.py +0 -0
  48. rigcode/ui/base.py +69 -0
  49. rigcode/ui/commands.py +581 -0
  50. rigcode/ui/plain.py +321 -0
  51. rigcode/ui/render.py +207 -0
  52. rigcode/ui/terminal.py +1154 -0
  53. rigcode/utils/__init__.py +0 -0
  54. rigcode/utils/files.py +176 -0
  55. rigcode/utils/frontmatter.py +38 -0
  56. rigcode/utils/globmatch.py +91 -0
  57. rigcode/utils/jsonrepair.py +110 -0
  58. rigcode/utils/paths.py +83 -0
  59. rigcode/utils/text.py +105 -0
  60. rigcode-0.1.0.dist-info/METADATA +203 -0
  61. rigcode-0.1.0.dist-info/RECORD +64 -0
  62. rigcode-0.1.0.dist-info/WHEEL +4 -0
  63. rigcode-0.1.0.dist-info/entry_points.txt +2 -0
  64. rigcode-0.1.0.dist-info/licenses/LICENSE +21 -0
rigcode/tools/web.py ADDED
@@ -0,0 +1,346 @@
1
+ """Web tools: WebFetch (URL -> markdown) and WebSearch."""
2
+ from __future__ import annotations
3
+
4
+ import html
5
+ import re
6
+ from html.parser import HTMLParser
7
+ from urllib.parse import parse_qs, quote_plus, unquote, urljoin, urlparse
8
+
9
+ import httpx
10
+
11
+ from rigcode.tools.base import Tool, ToolContext, ToolResult
12
+ from rigcode.utils.text import one_line
13
+
14
+ MAX_DOWNLOAD = 5 * 1024 * 1024
15
+ SKIP_TAGS = {"script", "style", "noscript", "svg", "template", "iframe", "canvas", "head", "form", "button", "select"}
16
+ BLOCK_TAGS = {"p", "div", "section", "article", "header", "footer", "main", "aside", "nav", "table", "tr",
17
+ "ul", "ol", "dl", "dt", "dd", "figure", "figcaption", "details", "summary", "address", "hr"}
18
+
19
+
20
+ class _HTMLToMarkdown(HTMLParser):
21
+ def __init__(self, base_url: str):
22
+ super().__init__(convert_charrefs=True)
23
+ self.base = base_url
24
+ self.out: list[str] = []
25
+ self.skip = 0
26
+ self.pre = 0
27
+ self.lists: list[list] = [] # stack of [type, counter]
28
+ self.link: str | None = None
29
+ self.link_text: list[str] = []
30
+ self.title = ""
31
+ self._in_title = False
32
+ self.cell = 0
33
+
34
+ def _emit(self, text: str) -> None:
35
+ if self.link is not None:
36
+ self.link_text.append(text)
37
+ else:
38
+ self.out.append(text)
39
+
40
+ def _nl(self, n: int = 1) -> None:
41
+ if self.link is None:
42
+ self.out.append("\n" * n)
43
+
44
+ def handle_starttag(self, tag, attrs):
45
+ a = dict(attrs)
46
+ if tag == "title":
47
+ self._in_title = True
48
+ if tag in SKIP_TAGS:
49
+ self.skip += 1
50
+ return
51
+ if self.skip:
52
+ return
53
+ if tag in ("h1", "h2", "h3", "h4", "h5", "h6"):
54
+ self._nl(2)
55
+ self._emit("#" * int(tag[1]) + " ")
56
+ elif tag == "br":
57
+ self._emit("\n")
58
+ elif tag == "pre":
59
+ self.pre += 1
60
+ self._nl(2)
61
+ self._emit("```\n")
62
+ elif tag == "code" and not self.pre:
63
+ self._emit("`")
64
+ elif tag in ("strong", "b"):
65
+ self._emit("**")
66
+ elif tag in ("em", "i"):
67
+ self._emit("*")
68
+ elif tag == "blockquote":
69
+ self._nl(2)
70
+ self._emit("> ")
71
+ elif tag in ("ul", "ol"):
72
+ self.lists.append([tag, 0])
73
+ self._nl()
74
+ elif tag == "li":
75
+ self._nl()
76
+ depth = max(0, len(self.lists) - 1)
77
+ if self.lists and self.lists[-1][0] == "ol":
78
+ self.lists[-1][1] += 1
79
+ self._emit(" " * depth + f"{self.lists[-1][1]}. ")
80
+ else:
81
+ self._emit(" " * depth + "- ")
82
+ elif tag == "a":
83
+ href = a.get("href") or ""
84
+ if href and not href.startswith(("javascript:", "#", "mailto:")):
85
+ self.link = urljoin(self.base, href)
86
+ self.link_text = []
87
+ elif tag == "img":
88
+ alt = (a.get("alt") or "").strip()
89
+ if alt:
90
+ self._emit(f"[image: {alt}]")
91
+ elif tag in ("td", "th"):
92
+ self._emit(" | " if self.cell else "| ")
93
+ self.cell += 1
94
+ elif tag in BLOCK_TAGS:
95
+ self._nl(2 if tag in ("p", "table") else 1)
96
+
97
+ def handle_endtag(self, tag):
98
+ if tag == "title":
99
+ self._in_title = False
100
+ if tag in SKIP_TAGS:
101
+ self.skip = max(0, self.skip - 1)
102
+ return
103
+ if self.skip:
104
+ return
105
+ if tag in ("h1", "h2", "h3", "h4", "h5", "h6"):
106
+ self._nl(2)
107
+ elif tag == "pre":
108
+ self.pre = max(0, self.pre - 1)
109
+ self._emit("\n```")
110
+ self._nl(2)
111
+ elif tag == "code" and not self.pre:
112
+ self._emit("`")
113
+ elif tag in ("strong", "b"):
114
+ self._emit("**")
115
+ elif tag in ("em", "i"):
116
+ self._emit("*")
117
+ elif tag in ("ul", "ol"):
118
+ if self.lists:
119
+ self.lists.pop()
120
+ self._nl()
121
+ elif tag == "a" and self.link is not None:
122
+ text = " ".join("".join(self.link_text).split())
123
+ link, self.link = self.link, None
124
+ if text:
125
+ self.out.append(f"[{text}]({link})")
126
+ elif tag == "tr":
127
+ if self.cell:
128
+ self.out.append(" |")
129
+ self.cell = 0
130
+ self._nl()
131
+ elif tag in BLOCK_TAGS:
132
+ self._nl(2 if tag in ("p", "table") else 1)
133
+
134
+ def handle_data(self, data):
135
+ if self._in_title:
136
+ self.title += data
137
+ if self.skip:
138
+ return
139
+ if self.pre:
140
+ self._emit(data)
141
+ else:
142
+ text = re.sub(r"\s+", " ", data)
143
+ if text.strip() or (text == " " and self.out and not self.out[-1].endswith((" ", "\n"))):
144
+ self._emit(text)
145
+
146
+ def markdown(self) -> str:
147
+ text = "".join(self.out)
148
+ text = re.sub(r"[ \t]+\n", "\n", text)
149
+ text = re.sub(r"\n{3,}", "\n\n", text)
150
+ return text.strip()
151
+
152
+
153
+ def _main_html(doc: str) -> str:
154
+ for tag in ("main", "article"):
155
+ m = re.search(rf"<{tag}\b[^>]*>(.*?)</{tag}>", doc, re.S | re.I)
156
+ if m and len(re.sub(r"<[^>]+>", "", m.group(1))) > 1500:
157
+ return m.group(1)
158
+ m = re.search(r"<body\b[^>]*>(.*)</body>", doc, re.S | re.I)
159
+ return m.group(1) if m else doc
160
+
161
+
162
+ def html_to_markdown(doc: str, base_url: str = "") -> tuple[str, str]:
163
+ title_m = re.search(r"<title[^>]*>(.*?)</title>", doc, re.S | re.I)
164
+ title = html.unescape(title_m.group(1)).strip() if title_m else ""
165
+ parser = _HTMLToMarkdown(base_url)
166
+ try:
167
+ parser.feed(_main_html(doc))
168
+ parser.close()
169
+ except Exception: # noqa: BLE001 - malformed HTML; return what we have
170
+ pass
171
+ return title, parser.markdown()
172
+
173
+
174
+ def select_relevant(text: str, query: str, budget: int) -> str:
175
+ """Pick the sections most relevant to `query` (keyword overlap), in page order."""
176
+ if len(text) <= budget:
177
+ return text
178
+ words = {w for w in re.findall(r"[a-zA-Z0-9_]{3,}", (query or "").lower())}
179
+ sections = re.split(r"\n(?=#{1,6} )|\n\n", text)
180
+ if not words:
181
+ return text[:budget] + "\n\n[... page truncated ...]"
182
+ scored = []
183
+ for i, sec in enumerate(sections):
184
+ low = sec.lower()
185
+ score = sum(low.count(w) for w in words) / (1 + len(sec) / 2000)
186
+ scored.append((score, i, sec))
187
+ keep, used = set(), 0
188
+ for score, i, sec in sorted(scored, key=lambda t: -t[0]):
189
+ if score <= 0 and keep:
190
+ break
191
+ if used + len(sec) > budget:
192
+ continue
193
+ keep.add(i)
194
+ used += len(sec) + 2
195
+ head = sections[0][:600]
196
+ out = [head] if 0 not in keep else []
197
+ out += [sec for i, sec in enumerate(sections) if i in keep]
198
+ return "\n\n".join(out) + f"\n\n[Showing the {len(keep)} sections most relevant to the prompt; page truncated.]"
199
+
200
+
201
+ class WebFetchTool(Tool):
202
+ name = "WebFetch"
203
+ kind = "web"
204
+ read_only = True
205
+ description = (
206
+ "Fetch a URL and return its content as Markdown (HTML is converted; JSON/text returned as-is). Pass "
207
+ "prompt to say what you are looking for; long pages are cut down to the most relevant sections."
208
+ )
209
+ parameters = {
210
+ "type": "object",
211
+ "properties": {
212
+ "url": {"type": "string", "description": "Full http(s) URL"},
213
+ "prompt": {"type": "string", "description": "What information you need from the page"},
214
+ },
215
+ "required": ["url"],
216
+ }
217
+ aliases = {"link": "url", "uri": "url", "query": "prompt", "question": "prompt"}
218
+
219
+ def permission_target(self, args: dict) -> str:
220
+ return str(args.get("url", ""))
221
+
222
+ async def run(self, args: dict, ctx: ToolContext) -> ToolResult:
223
+ url = str(args["url"]).strip()
224
+ if not re.match(r"^https?://", url):
225
+ url = "https://" + url
226
+ ua = ctx.rt.settings.get("web.user_agent") or "Mozilla/5.0 (compatible; rigcode)"
227
+ try:
228
+ async with httpx.AsyncClient(follow_redirects=True, timeout=30, headers={
229
+ "User-Agent": ua, "Accept": "text/html,application/xhtml+xml,application/json,text/plain,*/*"}) as c:
230
+ async with c.stream("GET", url) as r:
231
+ chunks, size = [], 0
232
+ async for chunk in r.aiter_bytes():
233
+ chunks.append(chunk)
234
+ size += len(chunk)
235
+ if size > MAX_DOWNLOAD:
236
+ break
237
+ status, ctype, final_url = r.status_code, r.headers.get("content-type", ""), str(r.url)
238
+ encoding = r.encoding or "utf-8"
239
+ except httpx.HTTPError as e:
240
+ return ToolResult.error(f"Fetch failed for {url}: {type(e).__name__}: {e}")
241
+ data = b"".join(chunks)
242
+ if status >= 400:
243
+ return ToolResult.error(f"HTTP {status} fetching {final_url}")
244
+ budget = int(ctx.rt.settings.get("context.max_tool_output_chars", 12000))
245
+ if "pdf" in ctype:
246
+ return ToolResult.error(f"{final_url} is a PDF ({len(data):,} bytes); download it with Bash (curl -o) and Read it.")
247
+ try:
248
+ text = data.decode(encoding, errors="replace")
249
+ except LookupError:
250
+ text = data.decode("utf-8", errors="replace")
251
+ title = ""
252
+ if "html" in ctype or text.lstrip()[:15].lower().startswith(("<!doctype", "<html")):
253
+ title, text = html_to_markdown(text, final_url)
254
+ text = select_relevant(text, str(args.get("prompt") or ""), budget)
255
+ header = f"# {title}\n" if title else ""
256
+ redirect = f"(redirected to {final_url})\n" if final_url.rstrip("/") != url.rstrip("/") else ""
257
+ return ToolResult(content=f"{header}{redirect}Source: {final_url}\n\n{text}",
258
+ summary=f"Fetched {len(text):,} chars from {urlparse(final_url).netloc}")
259
+
260
+
261
+ class WebSearchTool(Tool):
262
+ name = "WebSearch"
263
+ kind = "web"
264
+ read_only = True
265
+ description = (
266
+ "Search the web. Returns titles, URLs and snippets; follow up with WebFetch for details. Use it for "
267
+ "current information, documentation and error messages."
268
+ )
269
+ parameters = {
270
+ "type": "object",
271
+ "properties": {
272
+ "query": {"type": "string", "description": "Search query"},
273
+ "max_results": {"type": "integer", "description": "Number of results (default 8)"},
274
+ },
275
+ "required": ["query"],
276
+ }
277
+ aliases = {"q": "query", "search": "query", "num_results": "max_results", "count": "max_results"}
278
+
279
+ def permission_target(self, args: dict) -> str:
280
+ return str(args.get("query", ""))
281
+
282
+ async def run(self, args: dict, ctx: ToolContext) -> ToolResult:
283
+ query = str(args["query"]).strip()
284
+ n = max(1, min(int(args.get("max_results") or 8), 20))
285
+ s = ctx.rt.settings
286
+ provider = (s.get("web.search_provider") or "auto").lower()
287
+ try:
288
+ if provider in ("searxng",) or (provider == "auto" and s.get("web.searxng_url")):
289
+ results = await self._searxng(s.get("web.searxng_url"), query, n)
290
+ elif provider == "brave" or (provider == "auto" and s.get("web.brave_api_key")):
291
+ results = await self._brave(s.get("web.brave_api_key"), query, n)
292
+ elif provider == "tavily" or (provider == "auto" and s.get("web.tavily_api_key")):
293
+ results = await self._tavily(s.get("web.tavily_api_key"), query, n)
294
+ else:
295
+ results = await self._duckduckgo(query, n, s.get("web.user_agent"))
296
+ except (httpx.HTTPError, ValueError, KeyError) as e:
297
+ return ToolResult.error(f"Search failed: {type(e).__name__}: {e}")
298
+ if not results:
299
+ return ToolResult(content=f"No results for: {query}", summary="No results")
300
+ lines = []
301
+ for i, (title, url, snippet) in enumerate(results[:n], 1):
302
+ lines.append(f"{i}. {one_line(title, 120)}\n {url}\n {one_line(snippet, 300)}")
303
+ return ToolResult(content="\n".join(lines), summary=f"{len(results[:n])} results")
304
+
305
+ async def _duckduckgo(self, query: str, n: int, ua: str | None) -> list[tuple[str, str, str]]:
306
+ async with httpx.AsyncClient(timeout=20, follow_redirects=True,
307
+ headers={"User-Agent": ua or "Mozilla/5.0"}) as c:
308
+ r = await c.post("https://html.duckduckgo.com/html/", data={"q": query})
309
+ r.raise_for_status()
310
+ page = r.text
311
+ results = []
312
+ for m in re.finditer(r'<a[^>]+class="result__a"[^>]+href="([^"]+)"[^>]*>(.*?)</a>(.*?)(?=<a[^>]+class="result__a"|$)',
313
+ page, re.S):
314
+ href, title_html, rest = m.group(1), m.group(2), m.group(3)
315
+ if "uddg=" in href:
316
+ href = unquote(parse_qs(urlparse(html.unescape(href)).query).get("uddg", [href])[0])
317
+ if "duckduckgo.com/y.js" in href:
318
+ continue # ads
319
+ snip = re.search(r'class="result__snippet"[^>]*>(.*?)</a>', rest, re.S)
320
+ clean = lambda s: html.unescape(re.sub(r"<[^>]+>", "", s or "")).strip() # noqa: E731
321
+ results.append((clean(title_html), href, clean(snip.group(1) if snip else "")))
322
+ if len(results) >= n:
323
+ break
324
+ return results
325
+
326
+ async def _searxng(self, base: str, query: str, n: int) -> list[tuple[str, str, str]]:
327
+ async with httpx.AsyncClient(timeout=20) as c:
328
+ r = await c.get(f"{base.rstrip('/')}/search?q={quote_plus(query)}&format=json")
329
+ r.raise_for_status()
330
+ data = r.json()
331
+ return [(x.get("title", ""), x.get("url", ""), x.get("content", "")) for x in data.get("results", [])[:n]]
332
+
333
+ async def _brave(self, key: str, query: str, n: int) -> list[tuple[str, str, str]]:
334
+ async with httpx.AsyncClient(timeout=20, headers={"X-Subscription-Token": key, "Accept": "application/json"}) as c:
335
+ r = await c.get("https://api.search.brave.com/res/v1/web/search", params={"q": query, "count": n})
336
+ r.raise_for_status()
337
+ data = r.json()
338
+ return [(x.get("title", ""), x.get("url", ""), x.get("description", ""))
339
+ for x in (data.get("web") or {}).get("results", [])[:n]]
340
+
341
+ async def _tavily(self, key: str, query: str, n: int) -> list[tuple[str, str, str]]:
342
+ async with httpx.AsyncClient(timeout=30) as c:
343
+ r = await c.post("https://api.tavily.com/search", json={"api_key": key, "query": query, "max_results": n})
344
+ r.raise_for_status()
345
+ data = r.json()
346
+ return [(x.get("title", ""), x.get("url", ""), x.get("content", "")) for x in data.get("results", [])[:n]]
rigcode/ui/__init__.py ADDED
File without changes
rigcode/ui/base.py ADDED
@@ -0,0 +1,69 @@
1
+ """The interface between the engine (Runtime/Agent) and a front end.
2
+
3
+ Every method has a harmless default, so a front end only overrides what it shows.
4
+ Methods returning None from the interactive calls mean "nobody can answer".
5
+ """
6
+ from __future__ import annotations
7
+
8
+
9
+ class BaseUI:
10
+ interactive = False
11
+
12
+ # ---------------------------------------------------------------- notices and model output
13
+ def on_notice(self, level: str, text: str) -> None: # level: info | warn | error
14
+ pass
15
+
16
+ def on_model_start(self, agent, info: dict) -> None:
17
+ pass
18
+
19
+ def on_delta(self, agent, delta) -> None:
20
+ pass
21
+
22
+ def on_model_end(self, agent, turn) -> None: # turn is None when the request failed
23
+ pass
24
+
25
+ def on_usage(self, agent, usage) -> None:
26
+ pass
27
+
28
+ # ---------------------------------------------------------------- tools
29
+ def on_tool_start(self, agent, prepared, args: dict) -> None:
30
+ pass
31
+
32
+ def on_tool_output(self, agent, call_id: str, text: str) -> None:
33
+ pass
34
+
35
+ def on_tool_end(self, agent, prepared, result) -> None:
36
+ pass
37
+
38
+ # ---------------------------------------------------------------- questions for the user
39
+ async def request_permission(self, req, agent):
40
+ return None
41
+
42
+ async def ask_user(self, question: str, options: list[str]) -> str | None:
43
+ return None
44
+
45
+ async def review_plan(self, plan: str):
46
+ return None
47
+
48
+ def take_queued_input(self) -> str | None:
49
+ """Text the user typed while the agent was busy, delivered at the next step."""
50
+ return None
51
+
52
+ # ---------------------------------------------------------------- lifecycle
53
+ def on_compact_start(self, agent, before: int, reason: str) -> None:
54
+ pass
55
+
56
+ def on_compact_delta(self, agent, delta) -> None:
57
+ pass
58
+
59
+ def on_compact_end(self, agent, before: int, after: int, summary: str | None) -> None:
60
+ pass
61
+
62
+ def on_subagent_start(self, agent, description: str) -> None:
63
+ pass
64
+
65
+ def on_subagent_end(self, agent, stats) -> None:
66
+ pass
67
+
68
+ def on_warmup(self, state: str, data: dict | None = None) -> None: # start | progress | done | failed
69
+ pass