kittycode 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.
- kittycode/__init__.py +10 -0
- kittycode/__main__.py +3 -0
- kittycode/agent.py +125 -0
- kittycode/cli.py +367 -0
- kittycode/config.py +67 -0
- kittycode/context.py +170 -0
- kittycode/llm.py +325 -0
- kittycode/prompt.py +51 -0
- kittycode/session.py +66 -0
- kittycode/skills.py +125 -0
- kittycode/tools/__init__.py +27 -0
- kittycode/tools/agent.py +47 -0
- kittycode/tools/base.py +25 -0
- kittycode/tools/bash.py +100 -0
- kittycode/tools/edit.py +74 -0
- kittycode/tools/glob_tool.py +42 -0
- kittycode/tools/grep.py +70 -0
- kittycode/tools/read.py +49 -0
- kittycode/tools/write.py +37 -0
- kittycode-0.1.0.dist-info/METADATA +13 -0
- kittycode-0.1.0.dist-info/RECORD +24 -0
- kittycode-0.1.0.dist-info/WHEEL +4 -0
- kittycode-0.1.0.dist-info/entry_points.txt +2 -0
- kittycode-0.1.0.dist-info/licenses/LICENSE +21 -0
kittycode/skills.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"""Local skill discovery for KittyCode."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
import re
|
|
8
|
+
|
|
9
|
+
SKILLS_DIR = Path.home() / ".kittycode" / "skills"
|
|
10
|
+
|
|
11
|
+
_NAME_PATTERN = re.compile(r'^\s*name:\s*["\']?(.*?)["\']?\s*$')
|
|
12
|
+
_DESCRIPTION_PATTERN = re.compile(r'^\s*description:\s*["\']?(.*?)["\']?\s*$')
|
|
13
|
+
|
|
14
|
+
_cached_skills: tuple["SkillDefinition", ...] | None = None
|
|
15
|
+
_cached_root: Path | None = None
|
|
16
|
+
_cached_signature: tuple | None = None
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True)
|
|
20
|
+
class SkillDefinition:
|
|
21
|
+
name: str
|
|
22
|
+
description: str
|
|
23
|
+
path: str
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def load_skills(skills_dir: Path | str | None = None, force_reload: bool = False) -> list[SkillDefinition]:
|
|
27
|
+
"""Load skill metadata from ~/.kittycode/skills into process memory."""
|
|
28
|
+
global _cached_root
|
|
29
|
+
global _cached_skills
|
|
30
|
+
global _cached_signature
|
|
31
|
+
|
|
32
|
+
root = Path(skills_dir).expanduser() if skills_dir is not None else SKILLS_DIR
|
|
33
|
+
root = root.resolve()
|
|
34
|
+
signature = _build_signature(root)
|
|
35
|
+
|
|
36
|
+
if (
|
|
37
|
+
not force_reload
|
|
38
|
+
and _cached_skills is not None
|
|
39
|
+
and _cached_root == root
|
|
40
|
+
and _cached_signature == signature
|
|
41
|
+
):
|
|
42
|
+
return list(_cached_skills)
|
|
43
|
+
|
|
44
|
+
if not root.exists() or not root.is_dir():
|
|
45
|
+
_cached_root = root
|
|
46
|
+
_cached_skills = ()
|
|
47
|
+
_cached_signature = signature
|
|
48
|
+
return []
|
|
49
|
+
|
|
50
|
+
skills: list[SkillDefinition] = []
|
|
51
|
+
for entry in sorted(root.iterdir()):
|
|
52
|
+
if not entry.is_dir():
|
|
53
|
+
continue
|
|
54
|
+
skill_doc = entry / "SKILL.md"
|
|
55
|
+
if not skill_doc.is_file():
|
|
56
|
+
continue
|
|
57
|
+
skills.append(_read_skill(entry, skill_doc))
|
|
58
|
+
|
|
59
|
+
_cached_root = root
|
|
60
|
+
_cached_skills = tuple(skills)
|
|
61
|
+
_cached_signature = signature
|
|
62
|
+
return list(_cached_skills)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _build_signature(root: Path) -> tuple:
|
|
66
|
+
if not root.exists() or not root.is_dir():
|
|
67
|
+
return ("missing", str(root))
|
|
68
|
+
|
|
69
|
+
items = []
|
|
70
|
+
for entry in sorted(root.iterdir()):
|
|
71
|
+
if not entry.is_dir():
|
|
72
|
+
continue
|
|
73
|
+
skill_doc = entry / "SKILL.md"
|
|
74
|
+
if not skill_doc.is_file():
|
|
75
|
+
continue
|
|
76
|
+
stat = skill_doc.stat()
|
|
77
|
+
items.append((entry.name, stat.st_mtime_ns, stat.st_size))
|
|
78
|
+
return tuple(items)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _read_skill(skill_dir: Path, skill_doc: Path) -> SkillDefinition:
|
|
82
|
+
name, description = _parse_skill_header(skill_doc.read_text(errors="replace"))
|
|
83
|
+
return SkillDefinition(
|
|
84
|
+
name=name or skill_dir.name,
|
|
85
|
+
description=description or "No description provided.",
|
|
86
|
+
path=str(skill_dir.resolve()),
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _parse_skill_header(text: str) -> tuple[str, str]:
|
|
91
|
+
lines = text.splitlines()
|
|
92
|
+
header_lines = lines[:40]
|
|
93
|
+
|
|
94
|
+
name = ""
|
|
95
|
+
description = ""
|
|
96
|
+
|
|
97
|
+
for line in header_lines:
|
|
98
|
+
if not name:
|
|
99
|
+
match = _NAME_PATTERN.match(line)
|
|
100
|
+
if match:
|
|
101
|
+
name = match.group(1).strip()
|
|
102
|
+
continue
|
|
103
|
+
if not description:
|
|
104
|
+
match = _DESCRIPTION_PATTERN.match(line)
|
|
105
|
+
if match:
|
|
106
|
+
description = match.group(1).strip()
|
|
107
|
+
|
|
108
|
+
if name and description:
|
|
109
|
+
return name, description
|
|
110
|
+
|
|
111
|
+
for line in lines[:20]:
|
|
112
|
+
stripped = line.strip()
|
|
113
|
+
if stripped.startswith("# "):
|
|
114
|
+
name = name or stripped[2:].strip()
|
|
115
|
+
break
|
|
116
|
+
|
|
117
|
+
if not description:
|
|
118
|
+
for line in lines[:40]:
|
|
119
|
+
stripped = line.strip()
|
|
120
|
+
if not stripped or stripped.startswith("#") or stripped == "---":
|
|
121
|
+
continue
|
|
122
|
+
description = stripped
|
|
123
|
+
break
|
|
124
|
+
|
|
125
|
+
return name, description
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Tool registry."""
|
|
2
|
+
|
|
3
|
+
from .agent import AgentTool
|
|
4
|
+
from .bash import BashTool
|
|
5
|
+
from .edit import EditFileTool
|
|
6
|
+
from .glob_tool import GlobTool
|
|
7
|
+
from .grep import GrepTool
|
|
8
|
+
from .read import ReadFileTool
|
|
9
|
+
from .write import WriteFileTool
|
|
10
|
+
|
|
11
|
+
ALL_TOOLS = [
|
|
12
|
+
BashTool(),
|
|
13
|
+
ReadFileTool(),
|
|
14
|
+
WriteFileTool(),
|
|
15
|
+
EditFileTool(),
|
|
16
|
+
GlobTool(),
|
|
17
|
+
GrepTool(),
|
|
18
|
+
AgentTool(),
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def get_tool(name: str):
|
|
23
|
+
"""Look up a tool by name."""
|
|
24
|
+
for tool in ALL_TOOLS:
|
|
25
|
+
if tool.name == name:
|
|
26
|
+
return tool
|
|
27
|
+
return None
|
kittycode/tools/agent.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Sub-agent spawning."""
|
|
2
|
+
|
|
3
|
+
from .base import Tool
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class AgentTool(Tool):
|
|
7
|
+
name = "agent"
|
|
8
|
+
description = (
|
|
9
|
+
"Spawn a sub-agent to handle a complex sub-task independently. "
|
|
10
|
+
"The sub-agent has its own context and tool access. Use this for: "
|
|
11
|
+
"researching a codebase, implementing a multi-step change in isolation, "
|
|
12
|
+
"or any task that would benefit from a fresh context window."
|
|
13
|
+
)
|
|
14
|
+
parameters = {
|
|
15
|
+
"type": "object",
|
|
16
|
+
"properties": {
|
|
17
|
+
"task": {
|
|
18
|
+
"type": "string",
|
|
19
|
+
"description": "What the sub-agent should accomplish",
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
"required": ["task"],
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
_parent_agent = None
|
|
26
|
+
|
|
27
|
+
def execute(self, task: str) -> str:
|
|
28
|
+
if self._parent_agent is None:
|
|
29
|
+
return "Error: agent tool not initialized (no parent agent)"
|
|
30
|
+
|
|
31
|
+
from ..agent import Agent
|
|
32
|
+
|
|
33
|
+
parent = self._parent_agent
|
|
34
|
+
sub_agent = Agent(
|
|
35
|
+
llm=parent.llm,
|
|
36
|
+
tools=[tool for tool in parent.tools if tool.name != "agent"],
|
|
37
|
+
max_context_tokens=parent.context.max_tokens,
|
|
38
|
+
max_rounds=20,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
try:
|
|
42
|
+
result = sub_agent.chat(task)
|
|
43
|
+
if len(result) > 5000:
|
|
44
|
+
result = result[:4500] + "\n... (sub-agent output truncated)"
|
|
45
|
+
return f"[Sub-agent completed]\n{result}"
|
|
46
|
+
except Exception as exc:
|
|
47
|
+
return f"Sub-agent error: {exc}"
|
kittycode/tools/base.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Base class for all tools."""
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Tool(ABC):
|
|
7
|
+
"""Minimal tool interface."""
|
|
8
|
+
|
|
9
|
+
name: str
|
|
10
|
+
description: str
|
|
11
|
+
parameters: dict
|
|
12
|
+
|
|
13
|
+
@abstractmethod
|
|
14
|
+
def execute(self, **kwargs) -> str:
|
|
15
|
+
"""Run the tool and return a text result."""
|
|
16
|
+
|
|
17
|
+
def schema(self) -> dict:
|
|
18
|
+
return {
|
|
19
|
+
"type": "function",
|
|
20
|
+
"function": {
|
|
21
|
+
"name": self.name,
|
|
22
|
+
"description": self.description,
|
|
23
|
+
"parameters": self.parameters,
|
|
24
|
+
},
|
|
25
|
+
}
|
kittycode/tools/bash.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"""Shell command execution with basic safety checks."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import re
|
|
5
|
+
import subprocess
|
|
6
|
+
|
|
7
|
+
from .base import Tool
|
|
8
|
+
|
|
9
|
+
_cwd: str | None = None
|
|
10
|
+
|
|
11
|
+
_DANGEROUS_PATTERNS = [
|
|
12
|
+
(r"\brm\s+(-\w*)?-r\w*\s+(/|~|\$HOME)", "recursive delete on home/root"),
|
|
13
|
+
(r"\brm\s+(-\w*)?-rf\s", "force recursive delete"),
|
|
14
|
+
(r"\bmkfs\b", "format filesystem"),
|
|
15
|
+
(r"\bdd\s+.*of=/dev/", "raw disk write"),
|
|
16
|
+
(r">\s*/dev/sd[a-z]", "overwrite block device"),
|
|
17
|
+
(r"\bchmod\s+(-R\s+)?777\s+/", "chmod 777 on root"),
|
|
18
|
+
(r":\(\)\s*\{.*:\|:.*\}", "fork bomb"),
|
|
19
|
+
(r"\bcurl\b.*\|\s*(sudo\s+)?bash", "pipe curl to bash"),
|
|
20
|
+
(r"\bwget\b.*\|\s*(sudo\s+)?bash", "pipe wget to bash"),
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class BashTool(Tool):
|
|
25
|
+
name = "bash"
|
|
26
|
+
description = (
|
|
27
|
+
"Execute a shell command. Returns stdout, stderr, and exit code. "
|
|
28
|
+
"Use this for running tests, installing packages, git operations, and similar tasks."
|
|
29
|
+
)
|
|
30
|
+
parameters = {
|
|
31
|
+
"type": "object",
|
|
32
|
+
"properties": {
|
|
33
|
+
"command": {
|
|
34
|
+
"type": "string",
|
|
35
|
+
"description": "The shell command to run",
|
|
36
|
+
},
|
|
37
|
+
"timeout": {
|
|
38
|
+
"type": "integer",
|
|
39
|
+
"description": "Timeout in seconds (default 120)",
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
"required": ["command"],
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
def execute(self, command: str, timeout: int = 120) -> str:
|
|
46
|
+
global _cwd
|
|
47
|
+
|
|
48
|
+
warning = _check_dangerous(command)
|
|
49
|
+
if warning:
|
|
50
|
+
return f"Blocked: {warning}\nCommand: {command}\nIf intentional, modify the command to be more specific."
|
|
51
|
+
|
|
52
|
+
cwd = _cwd or os.getcwd()
|
|
53
|
+
|
|
54
|
+
try:
|
|
55
|
+
process = subprocess.run(
|
|
56
|
+
command,
|
|
57
|
+
shell=True,
|
|
58
|
+
capture_output=True,
|
|
59
|
+
text=True,
|
|
60
|
+
timeout=timeout,
|
|
61
|
+
cwd=cwd,
|
|
62
|
+
)
|
|
63
|
+
except subprocess.TimeoutExpired:
|
|
64
|
+
return f"Error: timed out after {timeout}s"
|
|
65
|
+
except Exception as exc:
|
|
66
|
+
return f"Error running command: {exc}"
|
|
67
|
+
|
|
68
|
+
if process.returncode == 0:
|
|
69
|
+
_update_cwd(command, cwd)
|
|
70
|
+
|
|
71
|
+
output = process.stdout
|
|
72
|
+
if process.stderr:
|
|
73
|
+
output += f"\n[stderr]\n{process.stderr}"
|
|
74
|
+
if process.returncode != 0:
|
|
75
|
+
output += f"\n[exit code: {process.returncode}]"
|
|
76
|
+
if len(output) > 15_000:
|
|
77
|
+
output = output[:6000] + f"\n\n... truncated ({len(output)} chars total) ...\n\n" + output[-3000:]
|
|
78
|
+
return output.strip() or "(no output)"
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _check_dangerous(command: str) -> str | None:
|
|
82
|
+
for pattern, reason in _DANGEROUS_PATTERNS:
|
|
83
|
+
if re.search(pattern, command):
|
|
84
|
+
return reason
|
|
85
|
+
return None
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _update_cwd(command: str, current_cwd: str):
|
|
89
|
+
global _cwd
|
|
90
|
+
|
|
91
|
+
for part in command.split("&&"):
|
|
92
|
+
part = part.strip()
|
|
93
|
+
if not part.startswith("cd "):
|
|
94
|
+
continue
|
|
95
|
+
target = part[3:].strip().strip("'\"")
|
|
96
|
+
if not target:
|
|
97
|
+
continue
|
|
98
|
+
new_dir = os.path.normpath(os.path.join(current_cwd, os.path.expanduser(target)))
|
|
99
|
+
if os.path.isdir(new_dir):
|
|
100
|
+
_cwd = new_dir
|
kittycode/tools/edit.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""Search-and-replace file editing."""
|
|
2
|
+
|
|
3
|
+
import difflib
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from .base import Tool
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class EditFileTool(Tool):
|
|
10
|
+
name = "edit_file"
|
|
11
|
+
description = (
|
|
12
|
+
"Edit a file by replacing an exact string match. "
|
|
13
|
+
"old_string must appear exactly once in the file for safety. "
|
|
14
|
+
"Include enough surrounding context to ensure uniqueness."
|
|
15
|
+
)
|
|
16
|
+
parameters = {
|
|
17
|
+
"type": "object",
|
|
18
|
+
"properties": {
|
|
19
|
+
"file_path": {
|
|
20
|
+
"type": "string",
|
|
21
|
+
"description": "Path to the file to edit",
|
|
22
|
+
},
|
|
23
|
+
"old_string": {
|
|
24
|
+
"type": "string",
|
|
25
|
+
"description": "Exact text to find (must be unique in file)",
|
|
26
|
+
},
|
|
27
|
+
"new_string": {
|
|
28
|
+
"type": "string",
|
|
29
|
+
"description": "Replacement text",
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
"required": ["file_path", "old_string", "new_string"],
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
def execute(self, file_path: str, old_string: str, new_string: str) -> str:
|
|
36
|
+
try:
|
|
37
|
+
path = Path(file_path).expanduser().resolve()
|
|
38
|
+
if not path.exists():
|
|
39
|
+
return f"Error: {file_path} not found"
|
|
40
|
+
|
|
41
|
+
content = path.read_text()
|
|
42
|
+
occurrences = content.count(old_string)
|
|
43
|
+
|
|
44
|
+
if occurrences == 0:
|
|
45
|
+
preview = content[:500] + ("..." if len(content) > 500 else "")
|
|
46
|
+
return f"Error: old_string not found in {file_path}.\nFile starts with:\n{preview}"
|
|
47
|
+
if occurrences > 1:
|
|
48
|
+
return (
|
|
49
|
+
f"Error: old_string appears {occurrences} times in {file_path}. "
|
|
50
|
+
"Include more surrounding lines to make it unique."
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
new_content = content.replace(old_string, new_string, 1)
|
|
54
|
+
path.write_text(new_content)
|
|
55
|
+
diff = _unified_diff(content, new_content, str(path))
|
|
56
|
+
return f"Edited {file_path}\n{diff}"
|
|
57
|
+
except Exception as exc:
|
|
58
|
+
return f"Error: {exc}"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _unified_diff(old: str, new: str, filename: str, context: int = 3) -> str:
|
|
62
|
+
old_lines = old.splitlines(keepends=True)
|
|
63
|
+
new_lines = new.splitlines(keepends=True)
|
|
64
|
+
diff = difflib.unified_diff(
|
|
65
|
+
old_lines,
|
|
66
|
+
new_lines,
|
|
67
|
+
fromfile=f"a/{filename}",
|
|
68
|
+
tofile=f"b/{filename}",
|
|
69
|
+
n=context,
|
|
70
|
+
)
|
|
71
|
+
result = "".join(diff)
|
|
72
|
+
if len(result) > 3000:
|
|
73
|
+
result = result[:2500] + "\n... (diff truncated)\n"
|
|
74
|
+
return result
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""File pattern matching."""
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from .base import Tool
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class GlobTool(Tool):
|
|
9
|
+
name = "glob"
|
|
10
|
+
description = "Find files matching a glob pattern. Supports ** for recursive matching."
|
|
11
|
+
parameters = {
|
|
12
|
+
"type": "object",
|
|
13
|
+
"properties": {
|
|
14
|
+
"pattern": {
|
|
15
|
+
"type": "string",
|
|
16
|
+
"description": "Glob pattern, for example '**/*.py' or 'src/**/*.ts'",
|
|
17
|
+
},
|
|
18
|
+
"path": {
|
|
19
|
+
"type": "string",
|
|
20
|
+
"description": "Directory to search in (default: cwd)",
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
"required": ["pattern"],
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
def execute(self, pattern: str, path: str = ".") -> str:
|
|
27
|
+
try:
|
|
28
|
+
base = Path(path).expanduser().resolve()
|
|
29
|
+
if not base.is_dir():
|
|
30
|
+
return f"Error: {path} is not a directory"
|
|
31
|
+
|
|
32
|
+
hits = list(base.glob(pattern))
|
|
33
|
+
hits.sort(key=lambda item: item.stat().st_mtime if item.exists() else 0, reverse=True)
|
|
34
|
+
|
|
35
|
+
total = len(hits)
|
|
36
|
+
shown = hits[:100]
|
|
37
|
+
result = "\n".join(str(item) for item in shown)
|
|
38
|
+
if total > 100:
|
|
39
|
+
result += f"\n... ({total} matches, showing first 100)"
|
|
40
|
+
return result or "No files matched."
|
|
41
|
+
except Exception as exc:
|
|
42
|
+
return f"Error: {exc}"
|
kittycode/tools/grep.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""Content search with regex support."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from .base import Tool
|
|
7
|
+
|
|
8
|
+
_SKIP_DIRS = {".git", "node_modules", "__pycache__", ".venv", "venv", ".tox", "dist", "build"}
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class GrepTool(Tool):
|
|
12
|
+
name = "grep"
|
|
13
|
+
description = "Search file contents with regex. Returns matching lines with file path and line number."
|
|
14
|
+
parameters = {
|
|
15
|
+
"type": "object",
|
|
16
|
+
"properties": {
|
|
17
|
+
"pattern": {
|
|
18
|
+
"type": "string",
|
|
19
|
+
"description": "Regex pattern to search for",
|
|
20
|
+
},
|
|
21
|
+
"path": {
|
|
22
|
+
"type": "string",
|
|
23
|
+
"description": "File or directory to search (default: cwd)",
|
|
24
|
+
},
|
|
25
|
+
"include": {
|
|
26
|
+
"type": "string",
|
|
27
|
+
"description": "Only search files matching this glob (for example '*.py')",
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
"required": ["pattern"],
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
def execute(self, pattern: str, path: str = ".", include: str | None = None) -> str:
|
|
34
|
+
try:
|
|
35
|
+
regex = re.compile(pattern)
|
|
36
|
+
except re.error as exc:
|
|
37
|
+
return f"Invalid regex: {exc}"
|
|
38
|
+
|
|
39
|
+
base = Path(path).expanduser().resolve()
|
|
40
|
+
if not base.exists():
|
|
41
|
+
return f"Error: {path} not found"
|
|
42
|
+
|
|
43
|
+
files = [base] if base.is_file() else self._walk(base, include)
|
|
44
|
+
matches = []
|
|
45
|
+
|
|
46
|
+
for file_path in files:
|
|
47
|
+
try:
|
|
48
|
+
text = file_path.read_text(errors="ignore")
|
|
49
|
+
except OSError:
|
|
50
|
+
continue
|
|
51
|
+
for line_number, line in enumerate(text.splitlines(), 1):
|
|
52
|
+
if regex.search(line):
|
|
53
|
+
matches.append(f"{file_path}:{line_number}: {line.rstrip()}")
|
|
54
|
+
if len(matches) >= 200:
|
|
55
|
+
matches.append("... (200 match limit reached)")
|
|
56
|
+
return "\n".join(matches)
|
|
57
|
+
|
|
58
|
+
return "\n".join(matches) if matches else "No matches found."
|
|
59
|
+
|
|
60
|
+
@staticmethod
|
|
61
|
+
def _walk(root: Path, include: str | None) -> list[Path]:
|
|
62
|
+
results = []
|
|
63
|
+
for item in root.rglob(include or "*"):
|
|
64
|
+
if any(part in _SKIP_DIRS for part in item.parts):
|
|
65
|
+
continue
|
|
66
|
+
if item.is_file():
|
|
67
|
+
results.append(item)
|
|
68
|
+
if len(results) >= 5000:
|
|
69
|
+
break
|
|
70
|
+
return results
|
kittycode/tools/read.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""File reading with line numbers."""
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from .base import Tool
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ReadFileTool(Tool):
|
|
9
|
+
name = "read_file"
|
|
10
|
+
description = "Read a file's contents with line numbers. Always read a file before editing it."
|
|
11
|
+
parameters = {
|
|
12
|
+
"type": "object",
|
|
13
|
+
"properties": {
|
|
14
|
+
"file_path": {
|
|
15
|
+
"type": "string",
|
|
16
|
+
"description": "Path to the file",
|
|
17
|
+
},
|
|
18
|
+
"offset": {
|
|
19
|
+
"type": "integer",
|
|
20
|
+
"description": "Start line (1-based). Default 1.",
|
|
21
|
+
},
|
|
22
|
+
"limit": {
|
|
23
|
+
"type": "integer",
|
|
24
|
+
"description": "Max lines to read. Default 2000.",
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
"required": ["file_path"],
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
def execute(self, file_path: str, offset: int = 1, limit: int = 2000) -> str:
|
|
31
|
+
try:
|
|
32
|
+
path = Path(file_path).expanduser().resolve()
|
|
33
|
+
if not path.exists():
|
|
34
|
+
return f"Error: {file_path} not found"
|
|
35
|
+
if not path.is_file():
|
|
36
|
+
return f"Error: {file_path} is a directory, not a file"
|
|
37
|
+
|
|
38
|
+
text = path.read_text(errors="replace")
|
|
39
|
+
lines = text.splitlines()
|
|
40
|
+
total = len(lines)
|
|
41
|
+
start = max(0, offset - 1)
|
|
42
|
+
chunk = lines[start : start + limit]
|
|
43
|
+
result = "\n".join(f"{start + index + 1}\t{line}" for index, line in enumerate(chunk))
|
|
44
|
+
|
|
45
|
+
if total > start + limit:
|
|
46
|
+
result += f"\n... ({total} lines total, showing {start + 1}-{start + len(chunk)})"
|
|
47
|
+
return result or "(empty file)"
|
|
48
|
+
except Exception as exc:
|
|
49
|
+
return f"Error: {exc}"
|
kittycode/tools/write.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""File creation and overwrite."""
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from .base import Tool
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class WriteFileTool(Tool):
|
|
9
|
+
name = "write_file"
|
|
10
|
+
description = (
|
|
11
|
+
"Create a new file or completely overwrite an existing one. "
|
|
12
|
+
"For small edits to existing files, prefer edit_file instead."
|
|
13
|
+
)
|
|
14
|
+
parameters = {
|
|
15
|
+
"type": "object",
|
|
16
|
+
"properties": {
|
|
17
|
+
"file_path": {
|
|
18
|
+
"type": "string",
|
|
19
|
+
"description": "Path for the file",
|
|
20
|
+
},
|
|
21
|
+
"content": {
|
|
22
|
+
"type": "string",
|
|
23
|
+
"description": "Full file content to write",
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
"required": ["file_path", "content"],
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
def execute(self, file_path: str, content: str) -> str:
|
|
30
|
+
try:
|
|
31
|
+
path = Path(file_path).expanduser().resolve()
|
|
32
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
33
|
+
path.write_text(content)
|
|
34
|
+
line_count = content.count("\n") + (1 if content and not content.endswith("\n") else 0)
|
|
35
|
+
return f"Wrote {line_count} lines to {file_path}"
|
|
36
|
+
except Exception as exc:
|
|
37
|
+
return f"Error: {exc}"
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: kittycode
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Minimal AI coding agent with a simple tool loop.
|
|
5
|
+
License: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Requires-Dist: anthropic>=0.84.0
|
|
9
|
+
Requires-Dist: openai>=1.0
|
|
10
|
+
Requires-Dist: prompt-toolkit>=3.0
|
|
11
|
+
Requires-Dist: rich>=13.0
|
|
12
|
+
Provides-Extra: dev
|
|
13
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
kittycode/__init__.py,sha256=KNsr0hwd0oqenca60YTaP58jSUxJXJnS-uK0-kAI004,270
|
|
2
|
+
kittycode/__main__.py,sha256=IRrpi8LZaa6mIX0XE4L_YpJXRx71ROB2YFRvWa0lkAE,38
|
|
3
|
+
kittycode/agent.py,sha256=XQAs9M7VU2tBXV2wdbRDg_3vtEj9FKaY_2-gaD2tmwk,4535
|
|
4
|
+
kittycode/cli.py,sha256=jdQngnbNWckbB-nt6QmSGHqc8J6yUcZiS72NMj8Gn40,12949
|
|
5
|
+
kittycode/config.py,sha256=2QqicFzv52DsKpaVaqXozPVjo4bhIYd7Emo2piCD-T8,2240
|
|
6
|
+
kittycode/context.py,sha256=OAGbsRocuux0ESwyEoU-Pvm1A-2wF10e_ImXx57CRkM,5674
|
|
7
|
+
kittycode/llm.py,sha256=R_KG52LzgKuIdS5mcQjs8VGFpLOCaRWYujgXjY3030w,10805
|
|
8
|
+
kittycode/prompt.py,sha256=hFxpKWH5lF9hyHsjsoidi6rz-NHUrz4q2U9_9YuneeM,1934
|
|
9
|
+
kittycode/session.py,sha256=yccMoo1utJr1Z0qpwXPp1ekRXeyeQrdruf5y7Sg-pDg,1874
|
|
10
|
+
kittycode/skills.py,sha256=cLEVw7BgIxqOnresLcbwcPRWoFHIDsicaLRCQRMnJPI,3552
|
|
11
|
+
kittycode/tools/__init__.py,sha256=MsDYpDJ_uvEBXcyrnGpQGVc0iamTzb4DFI3NeMNnfuc,532
|
|
12
|
+
kittycode/tools/agent.py,sha256=qlCI9_-SjilI47W6ZO9oPvKOID9MOU4-Eu6JupOJlvw,1456
|
|
13
|
+
kittycode/tools/base.py,sha256=r6dLpKXrtIhWXgjkh19dawP46ND7hW9px3r_Sv5R1Sc,554
|
|
14
|
+
kittycode/tools/bash.py,sha256=_QlJNclhm_NQr9n3jqrNLLIfYuP9lmwA8gU_4ZuB608,3118
|
|
15
|
+
kittycode/tools/edit.py,sha256=XkrTwtTMCRq1xPGnAeny0D6sugZF0JUaFuSfm3yQgBE,2485
|
|
16
|
+
kittycode/tools/glob_tool.py,sha256=O-ZDA6p5ct05vOjJRG7BCZ1snVKcmoDwE_v64INAJxg,1339
|
|
17
|
+
kittycode/tools/grep.py,sha256=WsGTikXS63Ikymj1N_4TWGU6fpRgQ-MUf4OVpwnCTzc,2332
|
|
18
|
+
kittycode/tools/read.py,sha256=JsLTVhWU2frFHJ6ZvuQXsknj551QnzHxf7eKxhA3aAQ,1676
|
|
19
|
+
kittycode/tools/write.py,sha256=xE7fwR_fzy4ICjtIqWuxNsspit46Zuc6QsQPmzubw8c,1157
|
|
20
|
+
kittycode-0.1.0.dist-info/METADATA,sha256=fEKFzouoWjtUm7hViF3rZqhObip1vGvDF_Z9DFfm00Q,354
|
|
21
|
+
kittycode-0.1.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
22
|
+
kittycode-0.1.0.dist-info/entry_points.txt,sha256=iEq5GUG9tFU05NFy_ZaspTGTYx8K1soh6VJyXkp7D7g,49
|
|
23
|
+
kittycode-0.1.0.dist-info/licenses/LICENSE,sha256=gcy4YmZ3vpRS_rc2H8uzH6YN_RLKPQS5nojKRsdZlXo,1065
|
|
24
|
+
kittycode-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jimmy Ye
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|