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/extensions.py ADDED
@@ -0,0 +1,275 @@
1
+ """Discovery of user extensions: subagents, skills, memory files, slash commands.
2
+
3
+ Search order (later wins on name clashes):
4
+ built-in -> ~/.rigcode/... -> ~/.claude/... (if compat.claude_user_dirs)
5
+ -> <project>/.claude/... (if compat.claude_project_dirs) -> <project>/.rigcode/...
6
+ """
7
+ from __future__ import annotations
8
+
9
+ import hashlib
10
+ import re
11
+ import subprocess
12
+ from dataclasses import dataclass, field
13
+ from importlib import resources
14
+ from pathlib import Path
15
+
16
+ from rigcode.utils.frontmatter import as_list, parse_frontmatter
17
+ from rigcode.utils.paths import rigcode_home
18
+
19
+ EFFORTS = {"low", "medium", "high", "max", "none"}
20
+
21
+
22
+ def _roots(settings, project_root: Path, sub: str) -> list[tuple[str, Path]]:
23
+ roots = [("user", rigcode_home() / sub)]
24
+ if settings.get("compat.claude_user_dirs", False):
25
+ roots.append(("user-claude", Path.home() / ".claude" / sub))
26
+ if settings.get("compat.claude_project_dirs", True):
27
+ roots.append(("project-claude", project_root / ".claude" / sub))
28
+ roots.append(("project", project_root / ".rigcode" / sub))
29
+ return roots
30
+
31
+
32
+ # ---------------------------------------------------------------- subagents
33
+
34
+ @dataclass
35
+ class AgentDef:
36
+ name: str
37
+ description: str
38
+ prompt: str
39
+ tools: list[str] | None = None # allowlist; None = every tool except Task
40
+ read_only: bool = False
41
+ effort: str | None = None # None = same as the main agent (shares the prompt cache)
42
+ max_turns: int | None = None
43
+ model: str | None = None # profile name from settings.profiles
44
+ source: str = "builtin"
45
+
46
+
47
+ def _agent_from_text(text: str, source: str, fallback_name: str) -> AgentDef | None:
48
+ meta, body = parse_frontmatter(text)
49
+ name = str(meta.get("name") or fallback_name).strip()
50
+ desc = str(meta.get("description") or "").strip()
51
+ if not name or not desc:
52
+ return None
53
+ tools = as_list(meta.get("tools")) or None
54
+ if tools and any(t in ("*", "all") for t in tools):
55
+ tools = None
56
+ effort = str(meta.get("effort") or "").strip().lower() or None
57
+ if effort not in EFFORTS:
58
+ effort = None
59
+ model = str(meta.get("model") or "").strip() or None
60
+ if model in ("inherit", "sonnet", "opus", "haiku", "fable"): # Claude Code model names -> inherit
61
+ model = None
62
+ try:
63
+ max_turns = int(meta["max_turns"]) if meta.get("max_turns") else None
64
+ except (TypeError, ValueError):
65
+ max_turns = None
66
+ read_only = meta.get("read_only")
67
+ if read_only is None:
68
+ read_only = bool(tools) and not any(t in ("Edit", "Write", "MultiEdit", "Bash", "NotebookEdit") for t in tools)
69
+ return AgentDef(name=name, description=" ".join(desc.split()), prompt=body.strip(), tools=tools,
70
+ read_only=bool(read_only), effort=effort, max_turns=max_turns, model=model, source=source)
71
+
72
+
73
+ def load_agents(settings, project_root: Path) -> dict[str, AgentDef]:
74
+ agents: dict[str, AgentDef] = {}
75
+ try:
76
+ pkg = resources.files("rigcode") / "prompts" / "agents"
77
+ for entry in sorted(pkg.iterdir(), key=lambda e: e.name):
78
+ if entry.name.endswith(".md"):
79
+ a = _agent_from_text(entry.read_text(encoding="utf-8"), "builtin", entry.name[:-3])
80
+ if a:
81
+ agents[a.name] = a
82
+ except (FileNotFoundError, OSError):
83
+ pass
84
+ for source, root in _roots(settings, project_root, "agents"):
85
+ if not root.is_dir():
86
+ continue
87
+ for path in sorted(root.glob("*.md")):
88
+ try:
89
+ a = _agent_from_text(path.read_text(encoding="utf-8"), source, path.stem)
90
+ except OSError:
91
+ continue
92
+ if a:
93
+ agents[a.name] = a
94
+ disabled = set(settings.get("agents.disabled", []) or [])
95
+ return {k: v for k, v in agents.items() if k not in disabled}
96
+
97
+
98
+ # ---------------------------------------------------------------- skills
99
+
100
+ @dataclass
101
+ class Skill:
102
+ name: str
103
+ description: str
104
+ directory: Path
105
+ path: Path
106
+ source: str
107
+
108
+ def load_body(self) -> str:
109
+ try:
110
+ _, body = parse_frontmatter(self.path.read_text(encoding="utf-8"))
111
+ except OSError as e:
112
+ return f"(could not read skill: {e})"
113
+ return body.strip()
114
+
115
+
116
+ def load_skills(settings, project_root: Path) -> dict[str, Skill]:
117
+ skills: dict[str, Skill] = {}
118
+ for source, root in _roots(settings, project_root, "skills"):
119
+ if not root.is_dir():
120
+ continue
121
+ for skill_md in sorted(root.glob("*/SKILL.md")):
122
+ try:
123
+ meta, _ = parse_frontmatter(skill_md.read_text(encoding="utf-8"))
124
+ except OSError:
125
+ continue
126
+ name = str(meta.get("name") or skill_md.parent.name).strip()
127
+ desc = " ".join(str(meta.get("description") or "").split())
128
+ if name and desc:
129
+ skills[name] = Skill(name, desc, skill_md.parent, skill_md, source)
130
+ return skills
131
+
132
+
133
+ # ---------------------------------------------------------------- memory files
134
+
135
+ @dataclass
136
+ class MemoryFile:
137
+ path: Path
138
+ content: str
139
+ scope: str
140
+
141
+
142
+ _IMPORT_RE = re.compile(r"(?<![\w`])@((?:~|\.{1,2})?[\w./\\-]+\.\w+)")
143
+
144
+
145
+ def _expand_imports(text: str, base: Path, depth: int = 0, seen: set | None = None) -> str:
146
+ if depth >= 3:
147
+ return text
148
+ seen = seen or set()
149
+ in_fence = False
150
+ out = []
151
+ for line in text.split("\n"):
152
+ if line.strip().startswith("```"):
153
+ in_fence = not in_fence
154
+ if in_fence:
155
+ out.append(line)
156
+ continue
157
+
158
+ def repl(m: re.Match) -> str:
159
+ raw = m.group(1)
160
+ p = Path(raw).expanduser()
161
+ if not p.is_absolute():
162
+ p = (base / p)
163
+ try:
164
+ p = p.resolve()
165
+ key = str(p).lower()
166
+ if key in seen or not p.is_file() or p.stat().st_size > 200_000:
167
+ return m.group(0)
168
+ seen.add(key)
169
+ inner = p.read_text(encoding="utf-8", errors="replace")
170
+ except OSError:
171
+ return m.group(0)
172
+ return f"\n<!-- imported from {raw} -->\n" + _expand_imports(inner, p.parent, depth + 1, seen)
173
+
174
+ out.append(_IMPORT_RE.sub(repl, line))
175
+ return "\n".join(out)
176
+
177
+
178
+ def load_memory(settings, project_root: Path, cwd: Path) -> list[MemoryFile]:
179
+ names = list(settings.get("memory_files", ["AGENTS.md", "CLAUDE.md", "RIGCODE.md"]) or [])
180
+ local_names = [n.replace(".md", ".local.md") for n in names]
181
+ candidates: list[tuple[Path, str]] = []
182
+ for n in ("AGENTS.md", "RIGCODE.md"):
183
+ candidates.append((rigcode_home() / n, "user"))
184
+ if settings.get("compat.claude_user_dirs", False):
185
+ candidates.append((Path.home() / ".claude" / "CLAUDE.md", "user"))
186
+ try:
187
+ cwd_r = cwd.resolve()
188
+ except OSError:
189
+ cwd_r = cwd
190
+ chain = list(reversed([cwd_r, *cwd_r.parents]))
191
+ for d in chain:
192
+ scope = "project" if str(d).lower().startswith(str(project_root).lower()) else "parent"
193
+ for n in names + local_names:
194
+ candidates.append((d / n, scope))
195
+ for n in names:
196
+ candidates.append((d / ".rigcode" / n, scope))
197
+ files, seen_hash, seen_path = [], set(), set()
198
+ per_file = 12000
199
+ total_budget = int(settings.get("context.max_memory_chars", 20000))
200
+ used = 0
201
+ for path, scope in candidates:
202
+ key = str(path).lower()
203
+ if key in seen_path or not path.is_file():
204
+ continue
205
+ seen_path.add(key)
206
+ try:
207
+ text = path.read_text(encoding="utf-8", errors="replace")
208
+ except OSError:
209
+ continue
210
+ text = _expand_imports(text, path.parent).strip()
211
+ h = hashlib.sha1(text.encode("utf-8")).hexdigest()
212
+ if not text or h in seen_hash:
213
+ continue
214
+ seen_hash.add(h)
215
+ if len(text) > per_file:
216
+ text = text[:per_file] + "\n[... truncated ...]"
217
+ if used + len(text) > total_budget:
218
+ text = text[: max(0, total_budget - used)] + "\n[... truncated: memory budget reached ...]"
219
+ used += len(text)
220
+ files.append(MemoryFile(path, text, scope))
221
+ if used >= total_budget:
222
+ break
223
+ return files
224
+
225
+
226
+ # ---------------------------------------------------------------- custom slash commands
227
+
228
+ @dataclass
229
+ class CustomCommand:
230
+ name: str
231
+ description: str
232
+ body: str
233
+ path: Path
234
+ source: str
235
+ argument_hint: str | None = None
236
+ allowed_tools: list[str] = field(default_factory=list)
237
+ model: str | None = None
238
+
239
+ def render(self, arguments: str, cwd: Path, shell_argv=None) -> str:
240
+ text = self.body
241
+ args = arguments.split()
242
+ text = text.replace("$ARGUMENTS", arguments)
243
+ for i in range(9, 0, -1):
244
+ text = text.replace(f"${i}", args[i - 1] if len(args) >= i else "")
245
+ if shell_argv and any(t == "Bash" or t.startswith("Bash(") for t in self.allowed_tools):
246
+ def run(m: re.Match) -> str:
247
+ try:
248
+ proc = subprocess.run(shell_argv(m.group(1)), cwd=str(cwd), capture_output=True, timeout=30)
249
+ out = proc.stdout.decode("utf-8", errors="replace") + proc.stderr.decode("utf-8", errors="replace")
250
+ except (OSError, subprocess.SubprocessError) as e:
251
+ out = f"(command failed: {e})"
252
+ return out.strip()
253
+ text = re.sub(r"!`([^`]+)`", run, text)
254
+ return text.strip()
255
+
256
+
257
+ def load_commands(settings, project_root: Path) -> dict[str, CustomCommand]:
258
+ commands: dict[str, CustomCommand] = {}
259
+ for source, root in _roots(settings, project_root, "commands"):
260
+ if not root.is_dir():
261
+ continue
262
+ for path in sorted(root.rglob("*.md")):
263
+ rel = path.relative_to(root).with_suffix("")
264
+ name = ":".join(rel.parts)
265
+ try:
266
+ meta, body = parse_frontmatter(path.read_text(encoding="utf-8"))
267
+ except OSError:
268
+ continue
269
+ desc = str(meta.get("description") or "").strip() or body.strip().split("\n", 1)[0][:80]
270
+ commands[name] = CustomCommand(
271
+ name=name, description=desc, body=body, path=path, source=source,
272
+ argument_hint=meta.get("argument-hint") or meta.get("argument_hint"),
273
+ allowed_tools=as_list(meta.get("allowed-tools") or meta.get("allowed_tools")),
274
+ model=meta.get("model"))
275
+ return commands
rigcode/hooks.py ADDED
@@ -0,0 +1,142 @@
1
+ """User-configured hooks: shell commands run at lifecycle events.
2
+
3
+ Configuration (settings.json), Claude-Code-compatible shape:
4
+ "hooks": {
5
+ "PreToolUse": [{"matcher": "Bash|Edit", "hooks": [{"type": "command", "command": "python check.py", "timeout": 60}]}],
6
+ "PostToolUse": [...], "UserPromptSubmit": [...], "Stop": [...], "SessionStart": [...], ...
7
+ }
8
+ The hook receives a JSON payload on stdin. Exit code 0 = ok (stdout may be JSON
9
+ with decision fields), 2 = block (stderr is fed back to the model), other =
10
+ non-blocking error shown to the user.
11
+ """
12
+ from __future__ import annotations
13
+
14
+ import asyncio
15
+ import json
16
+ import re
17
+ from dataclasses import dataclass, field
18
+ from pathlib import Path
19
+
20
+ from rigcode.tools.shell import ShellSpec, _child_env, _kill_tree, build_invocation
21
+ from rigcode.utils.text import decode_output
22
+
23
+ EVENTS = ("PreToolUse", "PostToolUse", "UserPromptSubmit", "Stop", "SubagentStop", "SessionStart",
24
+ "SessionEnd", "PreCompact", "Notification")
25
+
26
+
27
+ @dataclass
28
+ class HookOutcome:
29
+ block: bool = False
30
+ reason: str | None = None
31
+ permission: str | None = None # allow | deny | ask (PreToolUse)
32
+ updated_input: dict | None = None
33
+ additional_context: list[str] = field(default_factory=list)
34
+ halt: bool = False # "continue": false -> stop the agent
35
+ halt_reason: str | None = None
36
+ notices: list[str] = field(default_factory=list)
37
+
38
+
39
+ class HookRunner:
40
+ def __init__(self, config: dict, shell: ShellSpec, session_id: str, transcript_path: Path | None):
41
+ self.config = config or {}
42
+ self.shell = shell
43
+ self.session_id = session_id
44
+ self.transcript_path = transcript_path
45
+
46
+ def has(self, event: str) -> bool:
47
+ return bool(self.config.get(event))
48
+
49
+ def _matching(self, event: str, match_value: str | None) -> list[dict]:
50
+ out = []
51
+ for group in self.config.get(event) or []:
52
+ if not isinstance(group, dict):
53
+ continue
54
+ matcher = str(group.get("matcher") or "")
55
+ if matcher and matcher != "*" and match_value is not None:
56
+ try:
57
+ if not re.fullmatch(matcher, match_value) and not re.search(rf"^(?:{matcher})$", match_value):
58
+ continue
59
+ except re.error:
60
+ if matcher != match_value:
61
+ continue
62
+ for hook in group.get("hooks") or []:
63
+ if isinstance(hook, dict) and hook.get("type", "command") == "command" and hook.get("command"):
64
+ out.append(hook)
65
+ return out
66
+
67
+ async def run(self, event: str, payload: dict, cwd: Path, match_value: str | None = None) -> HookOutcome:
68
+ outcome = HookOutcome()
69
+ hooks = self._matching(event, match_value)
70
+ if not hooks:
71
+ return outcome
72
+ data = {"session_id": self.session_id, "transcript_path": str(self.transcript_path or ""),
73
+ "cwd": str(cwd), "hook_event_name": event, **payload}
74
+ stdin = json.dumps(data, ensure_ascii=False, default=str).encode("utf-8")
75
+ results = await asyncio.gather(*(self._run_one(h, stdin, cwd) for h in hooks))
76
+ for hook, (code, out, err) in zip(hooks, results):
77
+ name = hook["command"][:60]
78
+ if code is None:
79
+ outcome.notices.append(f"hook '{name}' timed out")
80
+ continue
81
+ if code == 2:
82
+ outcome.block = True
83
+ outcome.reason = "\n".join(x for x in (outcome.reason, err.strip() or out.strip()) if x)
84
+ continue
85
+ if code != 0:
86
+ outcome.notices.append(f"hook '{name}' failed (exit {code}): {(err or out).strip()[:300]}")
87
+ continue
88
+ parsed = None
89
+ text = out.strip()
90
+ if text.startswith("{"):
91
+ try:
92
+ parsed = json.loads(text)
93
+ except json.JSONDecodeError:
94
+ parsed = None
95
+ if parsed is None:
96
+ if text and event in ("UserPromptSubmit", "SessionStart"):
97
+ outcome.additional_context.append(text)
98
+ continue
99
+ self._apply_json(parsed, outcome)
100
+ return outcome
101
+
102
+ def _apply_json(self, data: dict, outcome: HookOutcome) -> None:
103
+ if data.get("continue") is False:
104
+ outcome.halt = True
105
+ outcome.halt_reason = data.get("stopReason")
106
+ decision = str(data.get("decision") or "").lower()
107
+ if decision in ("block", "deny"):
108
+ outcome.block = True
109
+ outcome.reason = data.get("reason") or outcome.reason
110
+ elif decision in ("approve", "allow"):
111
+ outcome.permission = "allow"
112
+ hso = data.get("hookSpecificOutput") or {}
113
+ if isinstance(hso, dict):
114
+ pd = str(hso.get("permissionDecision") or "").lower()
115
+ if pd in ("allow", "deny", "ask"):
116
+ outcome.permission = pd
117
+ if pd == "deny":
118
+ outcome.block = True
119
+ outcome.reason = hso.get("permissionDecisionReason") or outcome.reason
120
+ if isinstance(hso.get("updatedInput"), dict):
121
+ outcome.updated_input = hso["updatedInput"]
122
+ if hso.get("additionalContext"):
123
+ outcome.additional_context.append(str(hso["additionalContext"]))
124
+ if data.get("additionalContext"):
125
+ outcome.additional_context.append(str(data["additionalContext"]))
126
+
127
+ async def _run_one(self, hook: dict, stdin: bytes, cwd: Path) -> tuple[int | None, str, str]:
128
+ timeout = float(hook.get("timeout") or 60)
129
+ argv = build_invocation(self.shell, hook["command"], cwd, track_cwd=False)
130
+ try:
131
+ proc = await asyncio.create_subprocess_exec(
132
+ *argv, cwd=str(cwd), stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE,
133
+ stderr=asyncio.subprocess.PIPE, env={**_child_env(), "RIGCODE_PROJECT_DIR": str(cwd),
134
+ "CLAUDE_PROJECT_DIR": str(cwd)})
135
+ except OSError as e:
136
+ return 1, "", f"could not start hook: {e}"
137
+ try:
138
+ out, err = await asyncio.wait_for(proc.communicate(stdin), timeout=timeout)
139
+ except asyncio.TimeoutError:
140
+ _kill_tree(proc)
141
+ return None, "", ""
142
+ return proc.returncode, decode_output(out), decode_output(err)
File without changes
rigcode/llm/client.py ADDED
@@ -0,0 +1,259 @@
1
+ """Streaming OpenAI-compatible chat client (raw httpx + SSE).
2
+
3
+ Works with llama.cpp's llama-server, vLLM, SGLang, LM Studio, Ollama's /v1 and
4
+ hosted OpenAI-compatible APIs. Server-specific knobs are only sent when the
5
+ server type has been detected (see probe.py).
6
+ """
7
+ from __future__ import annotations
8
+
9
+ import asyncio
10
+ import json
11
+ import random
12
+ import time
13
+ from collections.abc import Callable
14
+ from pathlib import Path
15
+
16
+ import httpx
17
+
18
+ from rigcode.llm.probe import ServerInfo
19
+ from rigcode.llm.stream import ChunkAssembler, SSEDecoder
20
+ from rigcode.llm.types import AssistantTurn, Delta, LLMError
21
+
22
+ DeltaCallback = Callable[[Delta], None]
23
+
24
+
25
+ def _extract_error_message(body: str) -> str:
26
+ try:
27
+ data = json.loads(body)
28
+ except (json.JSONDecodeError, TypeError):
29
+ return body.strip()[:500]
30
+ if isinstance(data, dict):
31
+ err = data.get("error", data)
32
+ if isinstance(err, dict):
33
+ return str(err.get("message") or err.get("detail") or err)[:800]
34
+ if isinstance(err, str):
35
+ return err[:800]
36
+ if "detail" in data:
37
+ return str(data["detail"])[:800]
38
+ if "message" in data:
39
+ return str(data["message"])[:800]
40
+ return body.strip()[:500]
41
+
42
+
43
+ def classify_error(status: int | None, body: str) -> LLMError:
44
+ msg = _extract_error_message(body)
45
+ low = (body or "").lower()
46
+ kind, retryable = "bad_request", False
47
+ if status in (401, 403):
48
+ kind = "auth"
49
+ elif ("exceed" in low and "context" in low) or any(k in low for k in (
50
+ "context length", "context_length", "maximum context", "context size", "too many tokens",
51
+ "prompt is too long", "exceeds the available context", "exceed_context_size")):
52
+ kind = "context_overflow"
53
+ elif "max_tokens" in low or "max_completion_tokens" in low:
54
+ kind = "max_tokens"
55
+ elif "tool" in low and any(k in low for k in ("jinja", "not support", "unsupported", "does not support", "tool_choice")):
56
+ kind = "tools_unsupported"
57
+ elif status == 404:
58
+ kind = "not_found"
59
+ elif status in (408, 409, 425, 429, 500, 502, 503, 504) or status is None:
60
+ kind, retryable = "server", True
61
+ return LLMError(f"HTTP {status}: {msg}" if status else msg, status=status, kind=kind, body=body, retryable=retryable)
62
+
63
+
64
+ class LLMClient:
65
+ def __init__(self, provider: dict, server: ServerInfo | None = None, log_path: Path | None = None):
66
+ self.provider = provider
67
+ self.server = server or ServerInfo()
68
+ self.log_path = log_path
69
+ self._http: httpx.AsyncClient | None = None
70
+ mode = provider.get("tool_mode", "auto")
71
+ self.tool_mode = "text" if mode == "text" else "native"
72
+
73
+ # ------------------------------------------------------------------ setup
74
+ @property
75
+ def base_url(self) -> str:
76
+ return str(self.provider.get("base_url", "")).rstrip("/")
77
+
78
+ @property
79
+ def model(self) -> str:
80
+ return str(self.provider.get("model", ""))
81
+
82
+ def _client(self) -> httpx.AsyncClient:
83
+ if self._http is None:
84
+ timeout = httpx.Timeout(
85
+ connect=float(self.provider.get("connect_timeout", 15)),
86
+ read=float(self.provider.get("request_timeout", 3600)),
87
+ write=120.0,
88
+ pool=60.0,
89
+ )
90
+ headers = {"Content-Type": "application/json", "Accept": "text/event-stream, application/json"}
91
+ key = self.provider.get("api_key")
92
+ if key:
93
+ headers["Authorization"] = f"Bearer {key}"
94
+ headers.update(self.provider.get("extra_headers") or {})
95
+ self._http = httpx.AsyncClient(timeout=timeout, headers=headers)
96
+ return self._http
97
+
98
+ async def aclose(self) -> None:
99
+ if self._http is not None:
100
+ await self._http.aclose()
101
+ self._http = None
102
+
103
+ # ------------------------------------------------------------------ request
104
+ def build_body(self, messages: list[dict], tools: list[dict] | None, *, effort: str | None,
105
+ max_tokens: int | None, slot: int | None = None, temperature: float | None = None,
106
+ extra: dict | None = None) -> dict:
107
+ p = self.provider
108
+ body: dict = {
109
+ "model": self.model,
110
+ "messages": messages,
111
+ "stream": True,
112
+ "stream_options": {"include_usage": True},
113
+ }
114
+ if tools and self.tool_mode == "native":
115
+ body["tools"] = tools
116
+ body["parallel_tool_calls"] = True
117
+ if effort:
118
+ levels = self.server.effort_levels
119
+ if effort == "medium" and levels and "medium" not in levels and "high" in levels:
120
+ effort = "high" # e.g. Kimi K3's stock template treats an unknown level as max
121
+ body["reasoning_effort"] = effort
122
+ if effort == "none": # K3-style templates read either flag to turn thinking off
123
+ body["chat_template_kwargs"] = {**(p.get("extra_body") or {}).get("chat_template_kwargs", {}),
124
+ "enable_thinking": False}
125
+ if max_tokens:
126
+ body["max_tokens"] = int(max_tokens)
127
+ temp = temperature if temperature is not None else p.get("temperature")
128
+ if temp is not None:
129
+ body["temperature"] = temp
130
+ if p.get("top_p") is not None:
131
+ body["top_p"] = p["top_p"]
132
+ if p.get("reasoning_budget_tokens"):
133
+ body["reasoning_budget_tokens"] = int(p["reasoning_budget_tokens"])
134
+ if self.server.kind == "llama.cpp":
135
+ lc = p.get("llama_cpp") or {}
136
+ if lc.get("cache_prompt", True):
137
+ body["cache_prompt"] = True
138
+ if slot is not None and lc.get("slot_pinning", False):
139
+ body["id_slot"] = slot
140
+ if lc.get("return_progress", True):
141
+ body["return_progress"] = True
142
+ body.update(p.get("extra_body") or {})
143
+ if extra:
144
+ body.update(extra)
145
+ return body
146
+
147
+ async def stream_chat(self, messages: list[dict], tools: list[dict] | None, *, on_delta: DeltaCallback,
148
+ assembler: ChunkAssembler, effort: str | None = None, max_tokens: int | None = None,
149
+ slot: int | None = None, temperature: float | None = None,
150
+ extra: dict | None = None) -> AssistantTurn:
151
+ """Stream one completion. The caller owns `assembler` so it can recover the
152
+ partial turn if this coroutine is cancelled (Ctrl+C)."""
153
+ max_retries = int(self.provider.get("max_retries", 3))
154
+ body = self.build_body(messages, tools, effort=effort, max_tokens=max_tokens, slot=slot,
155
+ temperature=temperature, extra=extra)
156
+ attempt = 0
157
+ while True:
158
+ started = time.monotonic()
159
+ try:
160
+ turn = await self._stream_once(body, assembler, on_delta, started)
161
+ turn.elapsed = time.monotonic() - started
162
+ self._log({"event": "response", "finish": turn.finish_reason, "tool_calls": len(turn.tool_calls),
163
+ "usage": vars(turn.usage), "elapsed": round(turn.elapsed, 2)})
164
+ return turn
165
+ except LLMError as e:
166
+ self._log({"event": "error", "kind": e.kind, "status": e.status, "message": str(e)})
167
+ if e.kind == "max_tokens" and "max_tokens" in body:
168
+ body.pop("max_tokens") # let the server pick a valid limit
169
+ assembler.reset()
170
+ continue
171
+ if not e.retryable or attempt >= max_retries:
172
+ raise
173
+ attempt += 1
174
+ delay = min(30.0, (2 ** attempt) + random.random())
175
+ on_delta(Delta("reset", text=f"{e} - retrying in {delay:.0f}s (attempt {attempt}/{max_retries})"))
176
+ assembler.reset()
177
+ await asyncio.sleep(delay)
178
+
179
+ async def _stream_once(self, body: dict, asm: ChunkAssembler, on_delta: DeltaCallback, started: float) -> AssistantTurn:
180
+ url = f"{self.base_url}/chat/completions"
181
+ self._log({"event": "request", "url": url, "n_messages": len(body["messages"]),
182
+ "tools": len(body.get("tools") or []), "params": {k: v for k, v in body.items() if k not in ("messages", "tools")},
183
+ "tail": body["messages"][-2:]})
184
+ try:
185
+ async with self._client().stream("POST", url, json=body) as resp:
186
+ if resp.status_code >= 400:
187
+ raw = (await resp.aread()).decode("utf-8", errors="replace")
188
+ raise classify_error(resp.status_code, raw)
189
+ ctype = resp.headers.get("content-type", "")
190
+ if "text/event-stream" not in ctype and "application/json" in ctype:
191
+ data = json.loads((await resp.aread()).decode("utf-8", errors="replace"))
192
+ if isinstance(data, dict) and data.get("error") and not data.get("choices"):
193
+ raise classify_error(resp.status_code, json.dumps(data))
194
+ for d in asm.ingest(data):
195
+ on_delta(d)
196
+ else:
197
+ decoder = SSEDecoder()
198
+ async for line in resp.aiter_lines():
199
+ frame = decoder.feed_line(line)
200
+ if frame is not None and self._handle_frame(frame, asm, on_delta, started):
201
+ break
202
+ frame = decoder.flush()
203
+ if frame is not None:
204
+ self._handle_frame(frame, asm, on_delta, started)
205
+ except httpx.ConnectError as e:
206
+ raise LLMError(f"Cannot connect to {self.base_url} ({e}). Is the server running and the port open?",
207
+ kind="connection", retryable=True) from e
208
+ except httpx.ConnectTimeout as e:
209
+ raise LLMError(f"Timed out connecting to {self.base_url}. A firewall may be blocking the port.",
210
+ kind="connection", retryable=True) from e
211
+ except httpx.ReadTimeout as e:
212
+ # Never auto-retry: disconnecting cancels the server's work and a retry starts from scratch.
213
+ raise LLMError("Timed out waiting for the model (no data within provider.request_timeout).",
214
+ kind="timeout", retryable=False) from e
215
+ except (httpx.RemoteProtocolError, httpx.ReadError, httpx.WriteError) as e:
216
+ raise LLMError(f"Connection to the server was lost: {e}", kind="connection", retryable=True) from e
217
+ turn, deltas = asm.finish()
218
+ for d in deltas:
219
+ on_delta(d)
220
+ return turn
221
+
222
+ def _handle_frame(self, frame, asm: ChunkAssembler, on_delta: DeltaCallback, started: float) -> bool:
223
+ """Process one SSE frame; returns True at end of stream."""
224
+ _event, data = frame
225
+ if data.strip() == "[DONE]":
226
+ return True
227
+ try:
228
+ chunk = json.loads(data)
229
+ except json.JSONDecodeError:
230
+ return False
231
+ if not isinstance(chunk, dict):
232
+ return False
233
+ if chunk.get("error") and not chunk.get("choices"):
234
+ raise classify_error(None if not isinstance(chunk["error"], dict) else chunk["error"].get("code"), data)
235
+ for d in asm.ingest(chunk):
236
+ if asm.turn.first_token_latency is None and d.kind in ("reasoning", "content", "tool_start"):
237
+ asm.turn.first_token_latency = time.monotonic() - started
238
+ on_delta(Delta("first_token"))
239
+ on_delta(d)
240
+ return False
241
+
242
+ def _log(self, record: dict) -> None:
243
+ if not self.log_path:
244
+ return
245
+ try:
246
+ record = {"ts": time.strftime("%Y-%m-%dT%H:%M:%S"), **record}
247
+ with open(self.log_path, "a", encoding="utf-8") as fh:
248
+ fh.write(json.dumps(record, ensure_ascii=False, default=str) + "\n")
249
+ except OSError:
250
+ pass
251
+
252
+ # ------------------------------------------------------------------ simple one-shot
253
+ async def complete(self, messages: list[dict], *, effort: str | None = "low", max_tokens: int | None = None,
254
+ on_delta: DeltaCallback | None = None, tools: list[dict] | None = None,
255
+ slot: int | None = None) -> AssistantTurn:
256
+ """Convenience: one completion without the agent loop (warm-up, probes)."""
257
+ asm = ChunkAssembler()
258
+ return await self.stream_chat(messages, tools, on_delta=on_delta or (lambda d: None), assembler=asm,
259
+ effort=effort, max_tokens=max_tokens, slot=slot)