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/llm/probe.py ADDED
@@ -0,0 +1,229 @@
1
+ """Detect what kind of OpenAI-compatible server sits behind a base URL and what
2
+ it supports (context size, slots, template), so the client can tune requests."""
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ import re
7
+ from dataclasses import dataclass, field
8
+
9
+ import httpx
10
+
11
+
12
+ @dataclass
13
+ class ServerInfo:
14
+ kind: str = "unknown" # llama.cpp | vllm | sglang | ollama | lmstudio | unknown
15
+ reachable: bool = False
16
+ error: str | None = None
17
+ models: list[str] = field(default_factory=list)
18
+ context_window: int | None = None
19
+ total_slots: int | None = None
20
+ chat_template: str | None = None
21
+ build: str | None = None
22
+ vision: bool | None = None
23
+ effort_levels: tuple[str, ...] | None = None # reasoning_effort values the chat template accepts
24
+ loaded_models: list[str] = field(default_factory=list) # Ollama / LM Studio: models in memory now
25
+ model_contexts: dict[str, int] = field(default_factory=dict) # Ollama: runtime context per loaded model
26
+ raw: dict = field(default_factory=dict)
27
+
28
+ def describe(self) -> str:
29
+ bits = [self.kind]
30
+ if self.build:
31
+ bits.append(self.build)
32
+ if self.context_window:
33
+ bits.append(f"ctx {self.context_window:,}")
34
+ if self.total_slots:
35
+ bits.append(f"{self.total_slots} slot{'s' if self.total_slots != 1 else ''}")
36
+ return " · ".join(bits)
37
+
38
+
39
+ def server_root(base_url: str) -> str:
40
+ root = base_url.rstrip("/")
41
+ for suffix in ("/v1", "/api/v1", "/openai/v1"):
42
+ if root.endswith(suffix):
43
+ return root[: -len(suffix)]
44
+ return root
45
+
46
+
47
+ async def _get_json(client: httpx.AsyncClient, url: str) -> tuple[int | None, object | None]:
48
+ try:
49
+ r = await client.get(url)
50
+ except httpx.HTTPError:
51
+ return None, None
52
+ try:
53
+ return r.status_code, r.json()
54
+ except ValueError:
55
+ return r.status_code, None
56
+
57
+
58
+ _EFFORT_LIST_RE = re.compile(r"reasoning_effort\s+in\s+\[([^\]]*)\]")
59
+
60
+
61
+ def template_effort_levels(template: str | None) -> tuple[str, ...] | None:
62
+ """Every reasoning_effort value a Jinja chat template checks for, if it lists any."""
63
+ levels: list[str] = []
64
+ for m in _EFFORT_LIST_RE.finditer(template or ""):
65
+ for v in re.findall(r"['\"]([a-z]+)['\"]", m.group(1)):
66
+ if v not in levels:
67
+ levels.append(v)
68
+ return tuple(levels) or None
69
+
70
+
71
+ def _first_int(*vals) -> int | None:
72
+ for v in vals:
73
+ try:
74
+ if v is not None and int(v) > 0:
75
+ return int(v)
76
+ except (TypeError, ValueError):
77
+ continue
78
+ return None
79
+
80
+
81
+ async def probe_server(base_url: str, api_key: str | None = None, timeout: float = 8.0) -> ServerInfo:
82
+ info = ServerInfo()
83
+ headers = {"Authorization": f"Bearer {api_key}"} if api_key else {}
84
+ root = server_root(base_url)
85
+ async with httpx.AsyncClient(timeout=httpx.Timeout(timeout), headers=headers) as client:
86
+ try:
87
+ r = await client.get(f"{base_url.rstrip('/')}/models")
88
+ info.reachable = True
89
+ models_json = r.json() if r.status_code == 200 else None
90
+ except httpx.ConnectTimeout:
91
+ info.error = "connect timeout (host reachable? firewall blocking the port?)"
92
+ return info
93
+ except httpx.ConnectError as e:
94
+ info.error = f"connection refused/failed: {e}"
95
+ return info
96
+ except httpx.HTTPError as e:
97
+ info.error = f"{type(e).__name__}: {e}"
98
+ return info
99
+ except ValueError:
100
+ models_json = None
101
+
102
+ if isinstance(models_json, dict):
103
+ for m in models_json.get("data") or []:
104
+ if not isinstance(m, dict):
105
+ continue
106
+ if m.get("id"):
107
+ info.models.append(str(m["id"]))
108
+ meta = m.get("meta") or {}
109
+ info.context_window = info.context_window or _first_int(
110
+ m.get("max_model_len"), m.get("context_length"), m.get("max_context_length"),
111
+ meta.get("n_ctx"), meta.get("n_ctx_train"))
112
+ owner = str(m.get("owned_by", "")).lower()
113
+ if owner == "vllm":
114
+ info.kind = "vllm"
115
+ elif owner == "llamacpp":
116
+ info.kind = "llama.cpp"
117
+ elif owner in ("sglang",):
118
+ info.kind = "sglang"
119
+
120
+ # llama.cpp exposes /props (n_ctx is the *runtime* context, better than n_ctx_train).
121
+ props_status, props, slots_status, slots = None, None, None, None
122
+ (props_status, props), (slots_status, slots) = await asyncio.gather(
123
+ _get_json(client, f"{root}/props"), _get_json(client, f"{root}/slots"))
124
+ if props_status == 200 and isinstance(props, dict) and (
125
+ "default_generation_settings" in props or "total_slots" in props or "build_info" in props):
126
+ info.kind = "llama.cpp"
127
+ info.raw["props"] = {k: v for k, v in props.items() if k != "chat_template"}
128
+ dgs = props.get("default_generation_settings") or {}
129
+ n_ctx = _first_int(dgs.get("n_ctx"), props.get("n_ctx"))
130
+ if n_ctx:
131
+ info.context_window = n_ctx
132
+ info.total_slots = _first_int(props.get("total_slots"))
133
+ info.chat_template = props.get("chat_template")
134
+ info.effort_levels = template_effort_levels(info.chat_template)
135
+ info.build = props.get("build_info")
136
+ mods = props.get("modalities") or {}
137
+ if isinstance(mods, dict):
138
+ info.vision = bool(mods.get("vision"))
139
+ if slots_status == 200 and isinstance(slots, list) and slots:
140
+ info.total_slots = info.total_slots or len(slots)
141
+ if not n_ctx:
142
+ info.context_window = _first_int(slots[0].get("n_ctx")) or info.context_window
143
+ return info
144
+
145
+ if info.kind == "unknown":
146
+ st, data = await _get_json(client, f"{root}/get_model_info")
147
+ if st == 200 and isinstance(data, dict) and "model_path" in data:
148
+ info.kind = "sglang"
149
+ if info.kind in ("unknown", "sglang"):
150
+ st, data = await _get_json(client, f"{root}/get_server_info")
151
+ if st == 200 and isinstance(data, dict):
152
+ info.kind = "sglang"
153
+ info.context_window = info.context_window or _first_int(data.get("context_length"))
154
+ if info.kind == "unknown":
155
+ st, data = await _get_json(client, f"{root}/api/version")
156
+ if st == 200 and isinstance(data, dict) and "version" in data:
157
+ info.kind = "ollama"
158
+ info.build = str(data.get("version"))
159
+ st, ps = await _get_json(client, f"{root}/api/ps")
160
+ for m in (ps.get("models") or []) if isinstance(ps, dict) else []:
161
+ name = m.get("name") or m.get("model")
162
+ if name:
163
+ info.loaded_models.append(str(name))
164
+ ctx = _first_int(m.get("context_length"))
165
+ if ctx:
166
+ info.model_contexts[str(name)] = ctx
167
+ if info.kind == "unknown":
168
+ st, data = await _get_json(client, f"{root}/version")
169
+ if st == 200 and isinstance(data, dict) and "version" in data:
170
+ info.kind = "vllm"
171
+ info.build = str(data.get("version"))
172
+ if info.kind == "unknown":
173
+ st, data = await _get_json(client, f"{root}/api/v0/models")
174
+ if st == 200 and isinstance(data, dict) and "data" in data:
175
+ info.kind = "lmstudio"
176
+ for m in data.get("data") or []:
177
+ if m.get("state") == "loaded" and m.get("id"):
178
+ info.loaded_models.append(str(m["id"]))
179
+ info.model_contexts[str(m["id"])] = _first_int(
180
+ m.get("loaded_context_length"), m.get("max_context_length")) or 0
181
+ info.context_window = info.context_window or _first_int(m.get("max_context_length"))
182
+ return info
183
+
184
+
185
+ AUTO = (None, "", "auto")
186
+ LOCAL_SERVERS = (
187
+ ("llama.cpp", "http://127.0.0.1:8080/v1"),
188
+ ("Ollama", "http://127.0.0.1:11434/v1"),
189
+ ("LM Studio", "http://127.0.0.1:1234/v1"),
190
+ ("vLLM/SGLang", "http://127.0.0.1:8000/v1"),
191
+ )
192
+
193
+
194
+ async def discover(provider: dict, timeout: float = 8.0) -> tuple[ServerInfo, str | None]:
195
+ """Probe the configured server, or find a local one when base_url is 'auto', and pick the model
196
+ when model is 'auto'. Fills in provider['base_url'] / provider['model'] in place.
197
+
198
+ Returns the server info and a one-line notice about what was chosen (or why nothing could be)."""
199
+ key = provider.get("api_key")
200
+ notice = None
201
+ if provider.get("base_url") in AUTO:
202
+ infos = await asyncio.gather(*(probe_server(url, key, timeout=2.0) for _, url in LOCAL_SERVERS))
203
+ # A server counts only if it lists models: dev web servers often sit on 8000/8080 too.
204
+ found = [(name, url, i) for (name, url), i in zip(LOCAL_SERVERS, infos) if i.reachable and i.models]
205
+ if not found:
206
+ ports = ", ".join(f"{url.split(':')[2].split('/')[0]} ({name})" for name, url in LOCAL_SERVERS)
207
+ return ServerInfo(error=f"no local model server found on ports {ports}. Start one, or point "
208
+ "rigcode at yours: rigcode --base-url http://HOST:PORT/v1"), None
209
+ name, provider["base_url"], info = found[0]
210
+ notice = f"Found {name} at {provider['base_url']}"
211
+ else:
212
+ info = await probe_server(str(provider["base_url"]), key, timeout=timeout)
213
+ if not info.reachable:
214
+ return info, notice
215
+ if provider.get("model") in AUTO:
216
+ candidates = info.loaded_models or info.models
217
+ if not candidates:
218
+ info.reachable, info.error = False, "the server lists no models; set one with --model NAME"
219
+ return info, notice
220
+ provider["model"] = candidates[0]
221
+ others = [m for m in info.models if m != candidates[0]]
222
+ picked = f"using model {candidates[0]}"
223
+ if others:
224
+ picked += f" (also available: {', '.join(others[:5])}{' …' if len(others) > 5 else ''}; pick with -m)"
225
+ notice = f"{notice}, {picked}" if notice else picked[0].upper() + picked[1:]
226
+ ctx = info.model_contexts.get(str(provider.get("model")))
227
+ if ctx:
228
+ info.context_window = ctx
229
+ return info, notice
rigcode/llm/stream.py ADDED
@@ -0,0 +1,240 @@
1
+ """SSE parsing and assembly of streamed chat-completion chunks."""
2
+ from __future__ import annotations
3
+
4
+ import json
5
+ from dataclasses import dataclass, field
6
+
7
+ from rigcode.llm.types import AssistantTurn, Delta, ToolCall, Usage
8
+
9
+ THINK_TAGS = (("<think>", "</think>"), ("◁think▷", "◁/think▷"))
10
+
11
+
12
+ class SSEDecoder:
13
+ """Incremental Server-Sent-Events decoder (line based)."""
14
+
15
+ def __init__(self) -> None:
16
+ self._data: list[str] = []
17
+ self._event: str | None = None
18
+
19
+ def feed_line(self, line: str) -> tuple[str | None, str] | None:
20
+ """Feed one line (without newline). Returns (event, data) when a frame completes."""
21
+ if line.endswith("\r"):
22
+ line = line[:-1]
23
+ if line == "":
24
+ if not self._data:
25
+ self._event = None
26
+ return None
27
+ data = "\n".join(self._data)
28
+ event = self._event
29
+ self._data, self._event = [], None
30
+ return event, data
31
+ if line.startswith(":"):
32
+ return None
33
+ field_name, _, value = line.partition(":")
34
+ if value.startswith(" "):
35
+ value = value[1:]
36
+ if field_name == "data":
37
+ self._data.append(value)
38
+ elif field_name == "event":
39
+ self._event = value
40
+ return None
41
+
42
+ def flush(self) -> tuple[str | None, str] | None:
43
+ if self._data:
44
+ data = "\n".join(self._data)
45
+ event = self._event
46
+ self._data, self._event = [], None
47
+ return event, data
48
+ return None
49
+
50
+
51
+ class ThinkSplitter:
52
+ """Routes <think>...</think> found at the start of `content` into reasoning.
53
+
54
+ Some servers (or reasoning-format=none) leave the model's thinking inline in
55
+ the content. We only treat a think block at the very start of the message as
56
+ reasoning, so legitimate mentions of the tag later in an answer are untouched.
57
+ """
58
+
59
+ def __init__(self) -> None:
60
+ self.state = "start" # start | think | content
61
+ self.buf = ""
62
+ self.close_tag = ""
63
+
64
+ def feed(self, text: str) -> list[tuple[str, str]]:
65
+ out: list[tuple[str, str]] = []
66
+ self.buf += text
67
+ while self.buf:
68
+ if self.state == "start":
69
+ stripped = self.buf.lstrip()
70
+ matched = False
71
+ for open_tag, close_tag in THINK_TAGS:
72
+ if stripped.startswith(open_tag):
73
+ self.buf = stripped[len(open_tag):]
74
+ self.state, self.close_tag, matched = "think", close_tag, True
75
+ break
76
+ if open_tag.startswith(stripped): # could still become a tag
77
+ return out
78
+ if not matched:
79
+ self.state = "content"
80
+ elif self.state == "think":
81
+ idx = self.buf.find(self.close_tag)
82
+ if idx >= 0:
83
+ if idx:
84
+ out.append(("reasoning", self.buf[:idx]))
85
+ self.buf = self.buf[idx + len(self.close_tag):].lstrip("\n")
86
+ self.state = "content"
87
+ else:
88
+ # Hold back a possible partial closing tag.
89
+ keep = 0
90
+ for k in range(1, len(self.close_tag)):
91
+ if self.buf.endswith(self.close_tag[:k]):
92
+ keep = k
93
+ emit = self.buf[: len(self.buf) - keep]
94
+ if emit:
95
+ out.append(("reasoning", emit))
96
+ self.buf = self.buf[len(self.buf) - keep:]
97
+ return out
98
+ else:
99
+ out.append(("content", self.buf))
100
+ self.buf = ""
101
+ return out
102
+
103
+ def flush(self) -> list[tuple[str, str]]:
104
+ if not self.buf:
105
+ return []
106
+ kind = "reasoning" if self.state == "think" else "content"
107
+ text, self.buf = self.buf, ""
108
+ return [(kind, text)]
109
+
110
+
111
+ @dataclass
112
+ class _ToolAcc:
113
+ id: str | None = None
114
+ name: str = ""
115
+ args: list[str] = field(default_factory=list)
116
+
117
+
118
+ class ChunkAssembler:
119
+ """Turns OpenAI-style chat.completion.chunk dicts into Deltas + a final turn."""
120
+
121
+ def __init__(self) -> None:
122
+ self.reset()
123
+
124
+ def reset(self) -> None:
125
+ self.turn = AssistantTurn()
126
+ self._content: list[str] = []
127
+ self._reasoning: list[str] = []
128
+ self._tools: dict[int, _ToolAcc] = {}
129
+ self._splitter = ThinkSplitter()
130
+ self.saw_reasoning_field = False
131
+
132
+ @property
133
+ def partial_text(self) -> str:
134
+ return "".join(self._content)
135
+
136
+ def ingest(self, chunk: dict) -> list[Delta]:
137
+ deltas: list[Delta] = []
138
+ if chunk.get("usage"):
139
+ self._apply_usage(chunk["usage"])
140
+ if chunk.get("timings"):
141
+ self._apply_timings(chunk["timings"])
142
+ if "prompt_progress" in chunk and isinstance(chunk["prompt_progress"], dict):
143
+ deltas.append(Delta("progress", data=chunk["prompt_progress"]))
144
+ for choice in chunk.get("choices") or []:
145
+ delta = choice.get("delta") or choice.get("message") or {}
146
+ reasoning = delta.get("reasoning_content")
147
+ if reasoning is None:
148
+ reasoning = delta.get("reasoning")
149
+ if isinstance(reasoning, str) and reasoning:
150
+ self.saw_reasoning_field = True
151
+ self._reasoning.append(reasoning)
152
+ deltas.append(Delta("reasoning", reasoning))
153
+ content = delta.get("content")
154
+ if isinstance(content, str) and content:
155
+ for kind, text in self._splitter.feed(content):
156
+ (self._reasoning if kind == "reasoning" else self._content).append(text)
157
+ deltas.append(Delta(kind, text))
158
+ for tc in delta.get("tool_calls") or []:
159
+ idx = tc.get("index")
160
+ if idx is None:
161
+ # Servers that omit `index`: a new id starts a new call,
162
+ # otherwise the chunk continues the latest call.
163
+ tc_id = tc.get("id")
164
+ if tc_id and all(t.id != tc_id for t in self._tools.values()):
165
+ idx = len(self._tools)
166
+ else:
167
+ idx = max(self._tools) if self._tools else 0
168
+ acc = self._tools.get(idx)
169
+ if acc is None:
170
+ acc = self._tools[idx] = _ToolAcc()
171
+ if tc.get("id") and not acc.id:
172
+ acc.id = tc["id"]
173
+ fn = tc.get("function") or {}
174
+ name = fn.get("name")
175
+ if name and name != acc.name:
176
+ # llama.cpp may re-send a partially parsed name longer; overwrite, don't append.
177
+ first = not acc.name
178
+ acc.name = name
179
+ if first:
180
+ deltas.append(Delta("tool_start", index=idx, name=name))
181
+ args = fn.get("arguments")
182
+ if args:
183
+ if isinstance(args, dict):
184
+ args = json.dumps(args)
185
+ acc.args.append(args)
186
+ deltas.append(Delta("tool_args", args, index=idx))
187
+ if choice.get("finish_reason"):
188
+ self.turn.finish_reason = choice["finish_reason"]
189
+ return deltas
190
+
191
+ def _apply_usage(self, u: dict) -> None:
192
+ usage = self.turn.usage
193
+ usage.prompt_tokens = int(u.get("prompt_tokens") or usage.prompt_tokens or 0)
194
+ usage.completion_tokens = int(u.get("completion_tokens") or usage.completion_tokens or 0)
195
+ details = u.get("prompt_tokens_details") or {}
196
+ if details.get("cached_tokens"):
197
+ usage.cached_tokens = int(details["cached_tokens"])
198
+
199
+ def _apply_timings(self, t: dict) -> None:
200
+ usage = self.turn.usage
201
+ if t.get("cache_n") is not None:
202
+ usage.cached_tokens = int(t["cache_n"])
203
+ if t.get("prompt_n") is not None and not usage.prompt_tokens:
204
+ usage.prompt_tokens = int(t["prompt_n"]) + int(t.get("cache_n") or 0)
205
+ if t.get("prompt_ms") is not None:
206
+ usage.prompt_ms = float(t["prompt_ms"])
207
+ if t.get("predicted_n") is not None and not usage.completion_tokens:
208
+ usage.completion_tokens = int(t["predicted_n"])
209
+ if t.get("predicted_ms") is not None:
210
+ usage.gen_ms = float(t["predicted_ms"])
211
+
212
+ def finish(self) -> tuple[AssistantTurn, list[Delta]]:
213
+ deltas: list[Delta] = []
214
+ for kind, text in self._splitter.flush():
215
+ (self._reasoning if kind == "reasoning" else self._content).append(text)
216
+ deltas.append(Delta(kind, text))
217
+ content = "".join(self._content)
218
+ reasoning = "".join(self._reasoning)
219
+ # Unopened </think>: the server's template opened the think block itself.
220
+ if not reasoning and "</think>" in content:
221
+ head, _, tail = content.partition("</think>")
222
+ reasoning, content = head.strip(), tail.lstrip("\n")
223
+ self.turn.content = content
224
+ self.turn.reasoning = reasoning
225
+ calls = []
226
+ for n, idx in enumerate(sorted(self._tools)):
227
+ acc = self._tools[idx]
228
+ if not acc.name:
229
+ continue
230
+ calls.append(ToolCall(id=acc.id or f"call_{n}", name=acc.name, arguments="".join(acc.args)))
231
+ self.turn.tool_calls = calls
232
+ return self.turn, deltas
233
+
234
+
235
+ def parse_usage_only(u: dict | None) -> Usage:
236
+ usage = Usage()
237
+ if u:
238
+ usage.prompt_tokens = int(u.get("prompt_tokens") or 0)
239
+ usage.completion_tokens = int(u.get("completion_tokens") or 0)
240
+ return usage
@@ -0,0 +1,161 @@
1
+ """Text-based tool calling, used when the server lacks native tool support, plus
2
+ lenient recovery of tool calls that leak into `content` in native mode.
3
+
4
+ Recognized formats inside assistant text:
5
+ <tool_call>{"name": "Read", "arguments": {...}}</tool_call> (Hermes / rigcode)
6
+ <function=Read><parameter=file_path>a.py</parameter></function> (Qwen3-Coder XML)
7
+ <|tool_call_begin|>functions.Read:0<|tool_call_argument_begin|>{...}<|tool_call_end|> (Kimi native)
8
+ """
9
+ from __future__ import annotations
10
+
11
+ import json
12
+ import re
13
+
14
+ from rigcode.llm.types import ToolCall
15
+ from rigcode.utils.jsonrepair import loads_lenient
16
+
17
+ _TOOL_CALL_RE = re.compile(r"<tool_call>\s*(.*?)\s*(?:</tool_call>|\Z)", re.S)
18
+ _QWEN_FN_RE = re.compile(r"<function=([\w.\-]+)>\s*(.*?)\s*(?:</function>|\Z)", re.S)
19
+ _QWEN_PARAM_RE = re.compile(r"<parameter=([\w.\-]+)>\s*(.*?)\s*</parameter>", re.S)
20
+ _KIMI_SECTION_RE = re.compile(r"<\|tool_calls_section_begin\|>(.*?)(?:<\|tool_calls_section_end\|>|\Z)", re.S)
21
+ _KIMI_CALL_RE = re.compile(
22
+ r"<\|tool_call_begin\|>\s*(?:functions\.)?([\w.\-]+?)(?::(\d+))?\s*<\|tool_call_argument_begin\|>\s*(.*?)\s*(?:<\|tool_call_end\|>|\Z)",
23
+ re.S,
24
+ )
25
+
26
+
27
+ def render_tools_prompt(tool_schemas: list[dict]) -> str:
28
+ """System-prompt section that teaches the text tool protocol."""
29
+ lines = [
30
+ "# Tool calling protocol",
31
+ "You call tools by writing one or more blocks exactly like this in your reply:",
32
+ "<tool_call>",
33
+ '{"name": "TOOL_NAME", "arguments": {"param": "value"}}',
34
+ "</tool_call>",
35
+ "Rules: the JSON must be valid (escape newlines in strings as \\n). You may emit several "
36
+ "<tool_call> blocks in one reply to run independent calls together. After your tool calls, "
37
+ "stop writing and wait: results arrive in the next message inside <tool_result> tags. "
38
+ "Never write a <tool_result> yourself or guess a result.",
39
+ "",
40
+ "Available tools (JSON Schema):",
41
+ "<tools>",
42
+ ]
43
+ for schema in tool_schemas:
44
+ fn = schema.get("function", schema)
45
+ lines.append(json.dumps({"name": fn["name"], "description": fn.get("description", ""),
46
+ "parameters": fn.get("parameters", {})}, ensure_ascii=False))
47
+ lines.append("</tools>")
48
+ return "\n".join(lines)
49
+
50
+
51
+ def _call_from_obj(obj: object, n: int) -> ToolCall | None:
52
+ if not isinstance(obj, dict):
53
+ return None
54
+ name = obj.get("name") or obj.get("tool") or obj.get("function")
55
+ if isinstance(name, dict): # {"function": {"name":..., "arguments":...}}
56
+ obj = name
57
+ name = obj.get("name")
58
+ if not isinstance(name, str) or not name:
59
+ return None
60
+ args = obj.get("arguments", obj.get("parameters", obj.get("args", {})))
61
+ if isinstance(args, str):
62
+ parsed, err = loads_lenient(args)
63
+ raw = args
64
+ args = parsed if isinstance(parsed, dict) else None
65
+ call = ToolCall(id=f"call_t{n}", name=name, arguments=raw, args=args)
66
+ if err or args is None:
67
+ call.error = f"Could not parse arguments as JSON: {err or 'not an object'}"
68
+ return call
69
+ if not isinstance(args, dict):
70
+ args = {}
71
+ return ToolCall(id=f"call_t{n}", name=name, arguments=json.dumps(args, ensure_ascii=False), args=args)
72
+
73
+
74
+ def _coerce_param(value: str) -> object:
75
+ v = value.strip()
76
+ if v and v[0] in "[{" or v in ("true", "false", "null") or re.fullmatch(r"-?\d+(\.\d+)?", v or "x"):
77
+ try:
78
+ return json.loads(v)
79
+ except json.JSONDecodeError:
80
+ pass
81
+ return value
82
+
83
+
84
+ def parse_text_tool_calls(content: str, start_index: int = 0) -> tuple[str, list[ToolCall]]:
85
+ """Extract tool calls from assistant text. Returns (remaining_text, calls)."""
86
+ calls: list[ToolCall] = []
87
+ text = content
88
+
89
+ def take(pattern: re.Pattern, handler) -> None:
90
+ nonlocal text
91
+ pieces, last = [], 0
92
+ for m in pattern.finditer(text):
93
+ produced = handler(m)
94
+ if produced:
95
+ calls.extend(produced)
96
+ pieces.append(text[last:m.start()])
97
+ last = m.end()
98
+ if pieces or last:
99
+ pieces.append(text[last:])
100
+ text = "".join(pieces)
101
+
102
+ def kimi_section(m: re.Match) -> list[ToolCall]:
103
+ out = []
104
+ for cm in _KIMI_CALL_RE.finditer(m.group(1)):
105
+ name, raw = cm.group(1), cm.group(3)
106
+ parsed, err = loads_lenient(raw)
107
+ call = ToolCall(id=f"call_k{start_index + len(calls) + len(out)}", name=name, arguments=raw,
108
+ args=parsed if isinstance(parsed, dict) else None)
109
+ if call.args is None:
110
+ call.error = f"Could not parse arguments as JSON: {err or 'not an object'}"
111
+ out.append(call)
112
+ return out
113
+
114
+ def hermes(m: re.Match) -> list[ToolCall]:
115
+ body = m.group(1).strip()
116
+ if not body:
117
+ return []
118
+ fn = _QWEN_FN_RE.search(body)
119
+ if fn and not body.startswith("{"):
120
+ return qwen(fn)
121
+ parsed, err = loads_lenient(body)
122
+ if isinstance(parsed, list):
123
+ out = [c for i, o in enumerate(parsed) if (c := _call_from_obj(o, start_index + len(calls) + i))]
124
+ return out
125
+ call = _call_from_obj(parsed, start_index + len(calls))
126
+ if call:
127
+ return [call]
128
+ # Unparseable block: surface it as a broken call so the model can retry.
129
+ name_m = re.search(r'"name"\s*:\s*"([\w.\-]+)"', body)
130
+ if name_m:
131
+ return [ToolCall(id=f"call_t{start_index + len(calls)}", name=name_m.group(1), arguments=body,
132
+ args=None, error=f"Could not parse the tool call JSON: {err}")]
133
+ return []
134
+
135
+ def qwen(m: re.Match) -> list[ToolCall]:
136
+ name, body = m.group(1), m.group(2)
137
+ args = {pm.group(1): _coerce_param(pm.group(2)) for pm in _QWEN_PARAM_RE.finditer(body)}
138
+ return [ToolCall(id=f"call_t{start_index + len(calls)}", name=name,
139
+ arguments=json.dumps(args, ensure_ascii=False), args=args)]
140
+
141
+ take(_KIMI_SECTION_RE, kimi_section)
142
+ if "<tool_call>" in text:
143
+ take(_TOOL_CALL_RE, hermes)
144
+ if "<function=" in text:
145
+ take(_QWEN_FN_RE, qwen)
146
+ # Strip stray protocol tokens.
147
+ text = re.sub(r"<\|tool_calls?_section_(?:begin|end)\|>|</?tool_call>", "", text)
148
+ return text.strip(), calls
149
+
150
+
151
+ def has_text_tool_markup(content: str) -> bool:
152
+ return any(tag in content for tag in ("<tool_call>", "<|tool_call_begin|>", "<function="))
153
+
154
+
155
+ def render_tool_call_text(call: ToolCall) -> str:
156
+ args = call.args if call.args is not None else call.arguments
157
+ return "<tool_call>\n" + json.dumps({"name": call.name, "arguments": args}, ensure_ascii=False) + "\n</tool_call>"
158
+
159
+
160
+ def render_tool_result_text(name: str, call_id: str, content: str) -> str:
161
+ return f'<tool_result name="{name}" id="{call_id}">\n{content}\n</tool_result>'