tokencut-proxy 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.
- tokencut/__init__.py +0 -0
- tokencut/cli.py +127 -0
- tokencut/contracts.py +148 -0
- tokencut/gateway/__init__.py +0 -0
- tokencut/gateway/__main__.py +14 -0
- tokencut/gateway/adapters/__init__.py +1 -0
- tokencut/gateway/adapters/anthropic.py +119 -0
- tokencut/gateway/adapters/common.py +95 -0
- tokencut/gateway/adapters/gemini.py +107 -0
- tokencut/gateway/adapters/openai.py +82 -0
- tokencut/gateway/app.py +366 -0
- tokencut/gateway/brief.py +101 -0
- tokencut/gateway/ccr.py +83 -0
- tokencut/gateway/classify.py +41 -0
- tokencut/gateway/codeview.py +139 -0
- tokencut/gateway/history.py +433 -0
- tokencut/gateway/live.html +135 -0
- tokencut/gateway/media.py +825 -0
- tokencut/gateway/note.py +71 -0
- tokencut/gateway/prompt_router.py +214 -0
- tokencut/gateway/records.py +312 -0
- tokencut/gateway/routing.py +131 -0
- tokencut/gateway/session.py +76 -0
- tokencut/gateway/task_router.py +157 -0
- tokencut/gateway/textcut.py +91 -0
- tokencut/gateway/trimmer.py +256 -0
- tokencut/gateway/usage.py +120 -0
- tokencut/gateway/virtual.py +273 -0
- tokencut/graph/__init__.py +0 -0
- tokencut/graph/builder.py +307 -0
- tokencut/graph/modules.py +149 -0
- tokencut/graph/queries.py +106 -0
- tokencut/ingestion/__init__.py +0 -0
- tokencut/ingestion/base.py +18 -0
- tokencut/ingestion/parser.py +36 -0
- tokencut/ingestion/python_parser.py +275 -0
- tokencut/ingestion/walker.py +68 -0
- tokencut/launch.py +104 -0
- tokencut/litellm_proxy.py +134 -0
- tokencut/pdftool/__init__.py +9 -0
- tokencut/pdftool/__main__.py +115 -0
- tokencut/pdftool/reader.py +97 -0
- tokencut/repoindex/__init__.py +0 -0
- tokencut/repoindex/__main__.py +67 -0
- tokencut/repoindex/bm25.py +63 -0
- tokencut/repoindex/embed.py +93 -0
- tokencut/repoindex/index.py +455 -0
- tokencut/repoindex/mcp.py +145 -0
- tokencut/tokens.py +19 -0
- tokencut/trimming/__init__.py +0 -0
- tokencut/trimming/packing.py +317 -0
- tokencut/trimming/query.py +259 -0
- tokencut/trimming/render.py +65 -0
- tokencut/trimming/scoring.py +117 -0
- tokencut/trimming/variants.py +61 -0
- tokencut/userconfig.py +107 -0
- tokencut/wizard.py +94 -0
- tokencut_proxy-0.1.0.dist-info/METADATA +267 -0
- tokencut_proxy-0.1.0.dist-info/RECORD +62 -0
- tokencut_proxy-0.1.0.dist-info/WHEEL +5 -0
- tokencut_proxy-0.1.0.dist-info/entry_points.txt +2 -0
- tokencut_proxy-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
"""Split one Python file into MODULE / CLASS / FUNCTION / METHOD chunks with tree-sitter.
|
|
2
|
+
|
|
3
|
+
The exact meaning of each chunk is fixed in docs/IMPLEMENTATION_PLAN.md section 2
|
|
4
|
+
("Precise meaning of a chunk"); this file implements those rules.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import ast
|
|
8
|
+
import hashlib
|
|
9
|
+
import logging
|
|
10
|
+
|
|
11
|
+
import tree_sitter_python as tspython
|
|
12
|
+
from tree_sitter import Language, Node, Parser
|
|
13
|
+
|
|
14
|
+
from tokencut.contracts import Chunk, ChunkKind
|
|
15
|
+
from tokencut.tokens import count_tokens
|
|
16
|
+
|
|
17
|
+
log = logging.getLogger(__name__)
|
|
18
|
+
|
|
19
|
+
PY_LANGUAGE = Language(tspython.language())
|
|
20
|
+
MODULE_NAME = "<module>"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _decode(source: bytes) -> tuple[str, str]:
|
|
24
|
+
"""Return (text, encoding). latin-1 never fails and keeps one char per byte, so lines match."""
|
|
25
|
+
try:
|
|
26
|
+
return source.decode("utf-8"), "utf-8"
|
|
27
|
+
except UnicodeDecodeError:
|
|
28
|
+
return source.decode("latin-1"), "latin-1"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _line_range(node: Node) -> tuple[int, int]:
|
|
32
|
+
"""1-based inclusive line range of a node."""
|
|
33
|
+
start = node.start_point[0] + 1
|
|
34
|
+
end_row, end_col = node.end_point
|
|
35
|
+
# A node that ends exactly at the start of a line does not really occupy that line.
|
|
36
|
+
if end_col == 0 and end_row > node.start_point[0]:
|
|
37
|
+
end_row -= 1
|
|
38
|
+
return start, end_row + 1
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _unwrap(node: Node) -> Node:
|
|
42
|
+
"""decorated_definition -> the function/class it decorates (otherwise the node itself)."""
|
|
43
|
+
if node.type == "decorated_definition":
|
|
44
|
+
inner = node.child_by_field_name("definition")
|
|
45
|
+
if inner is not None:
|
|
46
|
+
return inner
|
|
47
|
+
return node
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _docstring(definition: Node) -> str:
|
|
51
|
+
body = definition.child_by_field_name("body")
|
|
52
|
+
if body is None:
|
|
53
|
+
return ""
|
|
54
|
+
first = None
|
|
55
|
+
for child in body.named_children:
|
|
56
|
+
if child.type != "comment":
|
|
57
|
+
first = child
|
|
58
|
+
break
|
|
59
|
+
if first is None or first.type != "expression_statement" or first.named_child_count != 1:
|
|
60
|
+
return ""
|
|
61
|
+
string = first.named_children[0]
|
|
62
|
+
if string.type != "string":
|
|
63
|
+
return ""
|
|
64
|
+
if any(c.type == "interpolation" for c in string.children):
|
|
65
|
+
return "" # an f-string is not a docstring
|
|
66
|
+
raw = string.text.decode("utf-8", errors="replace")
|
|
67
|
+
try:
|
|
68
|
+
value = ast.literal_eval(raw)
|
|
69
|
+
except (ValueError, SyntaxError):
|
|
70
|
+
value = raw.lstrip("rRbBuU").strip("\"'")
|
|
71
|
+
if isinstance(value, bytes):
|
|
72
|
+
value = value.decode("utf-8", errors="replace")
|
|
73
|
+
return value if isinstance(value, str) else ""
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class PythonParser:
|
|
77
|
+
"""LanguageParser for Python files (DECISIONS #6)."""
|
|
78
|
+
|
|
79
|
+
file_suffixes = (".py",)
|
|
80
|
+
|
|
81
|
+
def __init__(self) -> None:
|
|
82
|
+
self._parser = Parser(PY_LANGUAGE)
|
|
83
|
+
|
|
84
|
+
def parse_file(self, rel_path: str, source: bytes) -> list[Chunk]:
|
|
85
|
+
file_hash = hashlib.sha256(source).hexdigest()
|
|
86
|
+
text, encoding = _decode(source)
|
|
87
|
+
# Strip '\r' so a Windows (CRLF) checkout gives the same chunk code as Linux.
|
|
88
|
+
lines = [line.rstrip("\r") for line in text.split("\n")]
|
|
89
|
+
try:
|
|
90
|
+
return _FileChunker(rel_path, source, encoding, lines, file_hash).run(self._parser)
|
|
91
|
+
except Exception: # never let one bad file stop the whole repo
|
|
92
|
+
log.warning(
|
|
93
|
+
"could not chunk %s; keeping it as one module chunk", rel_path, exc_info=True
|
|
94
|
+
)
|
|
95
|
+
code = "\n".join(lines).strip("\n")
|
|
96
|
+
return [
|
|
97
|
+
Chunk(
|
|
98
|
+
id=f"{rel_path}::{MODULE_NAME}",
|
|
99
|
+
file_path=rel_path,
|
|
100
|
+
qualified_name=MODULE_NAME,
|
|
101
|
+
kind=ChunkKind.MODULE,
|
|
102
|
+
start_line=1,
|
|
103
|
+
end_line=max(1, len(lines)),
|
|
104
|
+
code=code,
|
|
105
|
+
token_count=count_tokens(code),
|
|
106
|
+
file_hash=file_hash,
|
|
107
|
+
)
|
|
108
|
+
]
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
class _FileChunker:
|
|
112
|
+
"""Holds the state needed while chunking one file."""
|
|
113
|
+
|
|
114
|
+
def __init__(
|
|
115
|
+
self, rel_path: str, source: bytes, encoding: str, lines: list[str], file_hash: str
|
|
116
|
+
):
|
|
117
|
+
self.rel_path = rel_path
|
|
118
|
+
self.source = source
|
|
119
|
+
self.encoding = encoding
|
|
120
|
+
self.lines = lines
|
|
121
|
+
self.file_hash = file_hash
|
|
122
|
+
self.name_counts: dict[str, int] = {} # qualified_name -> times seen (for '#2' suffixes)
|
|
123
|
+
|
|
124
|
+
# ---------- helpers ----------
|
|
125
|
+
|
|
126
|
+
def _code(self, line_numbers: list[int]) -> str:
|
|
127
|
+
return "\n".join(self.lines[n - 1] for n in line_numbers if n - 1 < len(self.lines))
|
|
128
|
+
|
|
129
|
+
def _signature(self, outer: Node, definition: Node) -> str:
|
|
130
|
+
"""Decorators + header up to and including the ':' (everything before the body)."""
|
|
131
|
+
body = definition.child_by_field_name("body")
|
|
132
|
+
end = body.start_byte if body is not None else definition.end_byte
|
|
133
|
+
raw = self.source[outer.start_byte : end].decode(self.encoding, errors="replace")
|
|
134
|
+
if body is None:
|
|
135
|
+
raw = raw.split("\n")[0]
|
|
136
|
+
return raw.replace("\r", "").rstrip()
|
|
137
|
+
|
|
138
|
+
def _new_id(self, qualified_name: str) -> str:
|
|
139
|
+
# DECISIONS #2: a repeated qualified_name in the same file gets '#2', '#3', ... in line order.
|
|
140
|
+
count = self.name_counts.get(qualified_name, 0) + 1
|
|
141
|
+
self.name_counts[qualified_name] = count
|
|
142
|
+
suffix = "" if count == 1 else f"#{count}"
|
|
143
|
+
return f"{self.rel_path}::{qualified_name}{suffix}"
|
|
144
|
+
|
|
145
|
+
def _make(
|
|
146
|
+
self,
|
|
147
|
+
kind: ChunkKind,
|
|
148
|
+
qualified_name: str,
|
|
149
|
+
start: int,
|
|
150
|
+
end: int,
|
|
151
|
+
code: str,
|
|
152
|
+
signature: str = "",
|
|
153
|
+
docstring: str = "",
|
|
154
|
+
parent_id: str | None = None,
|
|
155
|
+
chunk_id: str | None = None,
|
|
156
|
+
) -> Chunk:
|
|
157
|
+
return Chunk(
|
|
158
|
+
id=chunk_id or self._new_id(qualified_name),
|
|
159
|
+
file_path=self.rel_path,
|
|
160
|
+
qualified_name=qualified_name,
|
|
161
|
+
kind=kind,
|
|
162
|
+
parent_id=parent_id,
|
|
163
|
+
start_line=start,
|
|
164
|
+
end_line=end,
|
|
165
|
+
code=code,
|
|
166
|
+
signature=signature,
|
|
167
|
+
docstring=docstring,
|
|
168
|
+
token_count=count_tokens(code),
|
|
169
|
+
file_hash=self.file_hash,
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
# ---------- main algorithm ----------
|
|
173
|
+
|
|
174
|
+
def run(self, parser: Parser) -> list[Chunk]:
|
|
175
|
+
tree = parser.parse(self.source)
|
|
176
|
+
chunks: list[Chunk] = []
|
|
177
|
+
module_lines: set[int] = set()
|
|
178
|
+
def_lines: set[int] = set()
|
|
179
|
+
|
|
180
|
+
for node in tree.root_node.children:
|
|
181
|
+
definition = _unwrap(node)
|
|
182
|
+
start, end = _line_range(node) # starts at the decorator, if any
|
|
183
|
+
if definition.type == "function_definition":
|
|
184
|
+
name = self._name(definition)
|
|
185
|
+
chunks.append(
|
|
186
|
+
self._function(node, definition, name, ChunkKind.FUNCTION, start, end)
|
|
187
|
+
)
|
|
188
|
+
def_lines.update(range(start, end + 1))
|
|
189
|
+
elif definition.type == "class_definition":
|
|
190
|
+
chunks.extend(self._class(node, definition, start, end))
|
|
191
|
+
def_lines.update(range(start, end + 1))
|
|
192
|
+
else:
|
|
193
|
+
# imports, assignments, docstring, comments, ERROR regions, stray statements
|
|
194
|
+
module_lines.update(range(start, end + 1))
|
|
195
|
+
|
|
196
|
+
kept = sorted(module_lines - def_lines)
|
|
197
|
+
while kept and not self.lines[kept[-1] - 1].strip():
|
|
198
|
+
kept.pop()
|
|
199
|
+
module_code = self._code(kept)
|
|
200
|
+
module = self._make(
|
|
201
|
+
ChunkKind.MODULE,
|
|
202
|
+
MODULE_NAME,
|
|
203
|
+
kept[0] if kept else 1,
|
|
204
|
+
kept[-1] if kept else 1,
|
|
205
|
+
module_code,
|
|
206
|
+
chunk_id=f"{self.rel_path}::{MODULE_NAME}",
|
|
207
|
+
)
|
|
208
|
+
return [module] + chunks
|
|
209
|
+
|
|
210
|
+
def _name(self, definition: Node) -> str:
|
|
211
|
+
name_node = definition.child_by_field_name("name")
|
|
212
|
+
if name_node is None:
|
|
213
|
+
return "<anonymous>"
|
|
214
|
+
return name_node.text.decode("utf-8", errors="replace")
|
|
215
|
+
|
|
216
|
+
def _function(
|
|
217
|
+
self,
|
|
218
|
+
outer: Node,
|
|
219
|
+
definition: Node,
|
|
220
|
+
qualified_name: str,
|
|
221
|
+
kind: ChunkKind,
|
|
222
|
+
start: int,
|
|
223
|
+
end: int,
|
|
224
|
+
parent_id: str | None = None,
|
|
225
|
+
) -> Chunk:
|
|
226
|
+
return self._make(
|
|
227
|
+
kind,
|
|
228
|
+
qualified_name,
|
|
229
|
+
start,
|
|
230
|
+
end,
|
|
231
|
+
self._code(list(range(start, end + 1))),
|
|
232
|
+
signature=self._signature(outer, definition),
|
|
233
|
+
docstring=_docstring(definition),
|
|
234
|
+
parent_id=parent_id,
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
def _class(self, outer: Node, definition: Node, start: int, end: int) -> list[Chunk]:
|
|
238
|
+
class_name = self._name(definition)
|
|
239
|
+
class_id = self._new_id(class_name)
|
|
240
|
+
|
|
241
|
+
methods: list[tuple[Node, Node, int, int]] = []
|
|
242
|
+
body = definition.child_by_field_name("body")
|
|
243
|
+
if body is not None:
|
|
244
|
+
for child in body.children:
|
|
245
|
+
inner = _unwrap(child)
|
|
246
|
+
if inner.type == "function_definition":
|
|
247
|
+
m_start, m_end = _line_range(child)
|
|
248
|
+
methods.append((child, inner, m_start, m_end))
|
|
249
|
+
|
|
250
|
+
# Class chunk = whole class minus its methods (DECISIONS #2: no double counting).
|
|
251
|
+
method_lines: set[int] = set()
|
|
252
|
+
for _, _, m_start, m_end in methods:
|
|
253
|
+
method_lines.update(range(m_start, m_end + 1))
|
|
254
|
+
class_lines = [n for n in range(start, end + 1) if n not in method_lines]
|
|
255
|
+
while class_lines and not self.lines[class_lines[-1] - 1].strip():
|
|
256
|
+
class_lines.pop()
|
|
257
|
+
|
|
258
|
+
chunks = [
|
|
259
|
+
self._make(
|
|
260
|
+
ChunkKind.CLASS,
|
|
261
|
+
class_name,
|
|
262
|
+
start,
|
|
263
|
+
end,
|
|
264
|
+
self._code(class_lines),
|
|
265
|
+
signature=self._signature(outer, definition),
|
|
266
|
+
docstring=_docstring(definition),
|
|
267
|
+
chunk_id=class_id,
|
|
268
|
+
)
|
|
269
|
+
]
|
|
270
|
+
for child, inner, m_start, m_end in methods:
|
|
271
|
+
qualified = f"{class_name}.{self._name(inner)}"
|
|
272
|
+
chunks.append(
|
|
273
|
+
self._function(child, inner, qualified, ChunkKind.METHOD, m_start, m_end, class_id)
|
|
274
|
+
)
|
|
275
|
+
return chunks
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""Find the Python files of a repository (DECISIONS #12)."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import os
|
|
5
|
+
import subprocess
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from tokencut.contracts import MAX_FILE_BYTES, SKIP_DIR_NAMES
|
|
9
|
+
|
|
10
|
+
log = logging.getLogger(__name__)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _git_files(repo: Path) -> list[str] | None:
|
|
14
|
+
"""Tracked + untracked-but-not-ignored files, relative to repo. None if git is unusable."""
|
|
15
|
+
try:
|
|
16
|
+
result = subprocess.run(
|
|
17
|
+
["git", "ls-files", "-z", "--cached", "--others", "--exclude-standard"],
|
|
18
|
+
cwd=repo,
|
|
19
|
+
capture_output=True,
|
|
20
|
+
check=True,
|
|
21
|
+
timeout=120,
|
|
22
|
+
)
|
|
23
|
+
except (OSError, subprocess.SubprocessError):
|
|
24
|
+
return None
|
|
25
|
+
names = result.stdout.decode("utf-8", errors="surrogateescape").split("\0")
|
|
26
|
+
return [name for name in names if name]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _walk_files(repo: Path) -> list[str]:
|
|
30
|
+
"""Fallback when the folder is not a git repo: plain directory walk."""
|
|
31
|
+
found = []
|
|
32
|
+
for root, dirs, files in os.walk(repo):
|
|
33
|
+
dirs[:] = sorted(d for d in dirs if d not in SKIP_DIR_NAMES)
|
|
34
|
+
for name in sorted(files):
|
|
35
|
+
found.append((Path(root) / name).relative_to(repo).as_posix())
|
|
36
|
+
return found
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _is_binary(path: Path) -> bool:
|
|
40
|
+
with path.open("rb") as f:
|
|
41
|
+
return b"\0" in f.read(8192)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def list_python_files(repo_path: str | Path) -> list[str]:
|
|
45
|
+
"""Return sorted forward-slash paths (relative to repo_path) of the files to parse."""
|
|
46
|
+
repo = Path(repo_path)
|
|
47
|
+
names = _git_files(repo)
|
|
48
|
+
if names is None:
|
|
49
|
+
names = _walk_files(repo)
|
|
50
|
+
|
|
51
|
+
selected = set()
|
|
52
|
+
for name in names:
|
|
53
|
+
rel = Path(name)
|
|
54
|
+
if rel.suffix != ".py":
|
|
55
|
+
continue
|
|
56
|
+
if any(part in SKIP_DIR_NAMES for part in rel.parts[:-1]):
|
|
57
|
+
continue
|
|
58
|
+
path = repo / rel
|
|
59
|
+
if not path.is_file(): # e.g. tracked by git but deleted in the working tree
|
|
60
|
+
continue
|
|
61
|
+
if path.stat().st_size > MAX_FILE_BYTES:
|
|
62
|
+
log.info("skipping large file %s", name)
|
|
63
|
+
continue
|
|
64
|
+
if _is_binary(path):
|
|
65
|
+
log.info("skipping binary file %s", name)
|
|
66
|
+
continue
|
|
67
|
+
selected.add(rel.as_posix())
|
|
68
|
+
return sorted(selected)
|
tokencut/launch.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"""`tokencut` / `tokencut code`: model connector + gateway + Claude Code, in one command.
|
|
2
|
+
|
|
3
|
+
tokencut [claude args...] (python -m tokencut.launch code [claude args...])
|
|
4
|
+
|
|
5
|
+
Reads ~/.tokencut/config.json (runs `tokencut setup` first if there is none), starts what is
|
|
6
|
+
not already running, then opens Claude Code in the current folder wired to the gateway with
|
|
7
|
+
the TokenCut key. The model connector (LiteLLM) stays hidden: it delivers Claude requests
|
|
8
|
+
(with Claude Code's own login) and, if set up, GPT requests (`/model gpt-6.1-sol`).
|
|
9
|
+
Every TOKENCUT_* variable already set in the environment overrides the config.
|
|
10
|
+
Live page: http://127.0.0.1:8787/tokencut/ Processes started here stop when Claude Code exits.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import os
|
|
14
|
+
import shutil
|
|
15
|
+
import subprocess
|
|
16
|
+
import sys
|
|
17
|
+
import time
|
|
18
|
+
import urllib.request
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
from tokencut import litellm_proxy, userconfig
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _up(url: str) -> bool:
|
|
25
|
+
try:
|
|
26
|
+
with urllib.request.urlopen(url, timeout=2) as response:
|
|
27
|
+
return response.status < 500
|
|
28
|
+
except OSError:
|
|
29
|
+
return False
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _wait(url: str, what: str, seconds: int) -> None:
|
|
33
|
+
deadline = time.time() + seconds
|
|
34
|
+
while time.time() < deadline:
|
|
35
|
+
if _up(url):
|
|
36
|
+
return
|
|
37
|
+
time.sleep(0.5)
|
|
38
|
+
log = userconfig.home() / f"{what}.log"
|
|
39
|
+
sys.exit(f"tokencut: the {what} did not start within {seconds}s (see {log})")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _start(cmd: list[str], env: dict[str, str], log_name: str) -> subprocess.Popen:
|
|
43
|
+
userconfig.home().mkdir(parents=True, exist_ok=True)
|
|
44
|
+
log = (userconfig.home() / log_name).open("ab")
|
|
45
|
+
return subprocess.Popen(cmd, env=env, stdout=log, stderr=subprocess.STDOUT)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _start_connector(config: dict, env: dict[str, str],
|
|
49
|
+
started: list[subprocess.Popen]) -> str | None:
|
|
50
|
+
"""The connector's URL once it runs, or None (Claude then goes straight to Anthropic)."""
|
|
51
|
+
if not (config["litellm"]["enabled"] and litellm_proxy.installed()):
|
|
52
|
+
return None
|
|
53
|
+
url = litellm_proxy.url(config)
|
|
54
|
+
if not _up(f"{url}/health/liveliness"):
|
|
55
|
+
started.append(_start(litellm_proxy.command(config), litellm_proxy.process_env(env),
|
|
56
|
+
"connector.log"))
|
|
57
|
+
_wait(f"{url}/health/liveliness", "connector", 120)
|
|
58
|
+
return url
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def main(argv: list[str]) -> int:
|
|
62
|
+
if not argv or argv[0] != "code":
|
|
63
|
+
print(__doc__)
|
|
64
|
+
return 2
|
|
65
|
+
if not userconfig.config_path().exists():
|
|
66
|
+
from tokencut.wizard import run_setup
|
|
67
|
+
|
|
68
|
+
if run_setup() != 0:
|
|
69
|
+
return 1
|
|
70
|
+
config = userconfig.load()
|
|
71
|
+
claude = shutil.which("claude")
|
|
72
|
+
if claude is None:
|
|
73
|
+
sys.exit("tokencut: Claude Code ('claude') not found on PATH; run `tokencut setup`")
|
|
74
|
+
started: list[subprocess.Popen] = []
|
|
75
|
+
env = dict(os.environ)
|
|
76
|
+
try:
|
|
77
|
+
connector = _start_connector(config, env, started)
|
|
78
|
+
genv = userconfig.gateway_env(config, connector, env)
|
|
79
|
+
# Run this package's gateway code, also from a checkout without an install.
|
|
80
|
+
genv["PYTHONPATH"] = os.pathsep.join(
|
|
81
|
+
filter(None, [str(Path(__file__).resolve().parents[1]), env.get("PYTHONPATH")]))
|
|
82
|
+
gateway = f"http://127.0.0.1:{genv['TOKENCUT_PORT']}"
|
|
83
|
+
if _up(f"{gateway}/tokencut/health"):
|
|
84
|
+
print(f"tokencut: already running on {gateway} (its own settings apply)")
|
|
85
|
+
else:
|
|
86
|
+
print("tokencut: starting ...")
|
|
87
|
+
started.append(_start([sys.executable, "-m", "tokencut.gateway"], genv,
|
|
88
|
+
"gateway.log"))
|
|
89
|
+
_wait(f"{gateway}/tokencut/health", "gateway", 180) # first start loads the router
|
|
90
|
+
models = "Claude" + (" + GPT (/model " + config["chatgpt"]["models"][0] + ")"
|
|
91
|
+
if connector and config["chatgpt"]["enabled"] else "")
|
|
92
|
+
print(f"tokencut: ready - {models}; savings at {gateway}/tokencut/\n")
|
|
93
|
+
return subprocess.call([claude, *argv[1:]], env=userconfig.agent_env(config, env))
|
|
94
|
+
finally:
|
|
95
|
+
for proc in reversed(started):
|
|
96
|
+
proc.terminate()
|
|
97
|
+
try:
|
|
98
|
+
proc.wait(timeout=10)
|
|
99
|
+
except subprocess.TimeoutExpired:
|
|
100
|
+
proc.kill()
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
if __name__ == "__main__":
|
|
104
|
+
sys.exit(main(sys.argv[1:]))
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""The hidden model connector: a private LiteLLM install that delivers Claude and GPT requests.
|
|
2
|
+
|
|
3
|
+
The user never sees LiteLLM: `tokencut setup` installs a pinned version into
|
|
4
|
+
~/.tokencut/litellm-venv (its dependencies would clash with TokenCut's), the config is generated
|
|
5
|
+
from ~/.tokencut/config.json, and `tokencut` runs it as a child process on 127.0.0.1.
|
|
6
|
+
Claude requests keep the user's Claude Code login (LiteLLM forwards it to Anthropic); GPT
|
|
7
|
+
requests use LiteLLM's ChatGPT plan login (~/.config/litellm/chatgpt/auth.json).
|
|
8
|
+
If the connector is missing or down, Claude requests go straight to Anthropic (fail open).
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import json
|
|
12
|
+
import os
|
|
13
|
+
import shutil
|
|
14
|
+
import subprocess
|
|
15
|
+
import sys
|
|
16
|
+
from collections.abc import Callable
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
|
|
19
|
+
from tokencut import userconfig
|
|
20
|
+
|
|
21
|
+
LITELLM_VERSION = "1.109.1" # keep equal to deploy/litellm/requirements.txt (tested)
|
|
22
|
+
PYTHON_FOR_UV = "3.11"
|
|
23
|
+
_BIN, _EXE = ("Scripts", ".exe") if os.name == "nt" else ("bin", "")
|
|
24
|
+
CHATGPT_AUTH = Path.home() / ".config" / "litellm" / "chatgpt" / "auth.json"
|
|
25
|
+
Runner = Callable[..., subprocess.CompletedProcess]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def venv_dir() -> Path:
|
|
29
|
+
return userconfig.home() / "litellm-venv"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _python() -> Path:
|
|
33
|
+
return venv_dir() / _BIN / f"python{_EXE}"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def executable() -> Path:
|
|
37
|
+
override = os.environ.get("TOKENCUT_LITELLM")
|
|
38
|
+
return Path(override) if override else venv_dir() / _BIN / f"litellm{_EXE}"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _stamp() -> Path:
|
|
42
|
+
return venv_dir() / "tokencut-litellm-version"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def installed() -> bool:
|
|
46
|
+
"""The pinned version is installed (a TokenCut update with a new pin reinstalls it)."""
|
|
47
|
+
if os.environ.get("TOKENCUT_LITELLM"):
|
|
48
|
+
return executable().exists()
|
|
49
|
+
stamp = _stamp()
|
|
50
|
+
return (executable().exists() and stamp.exists()
|
|
51
|
+
and stamp.read_text(encoding="utf-8").strip() == LITELLM_VERSION)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def install_commands(uv: str | None) -> list[list[str]]:
|
|
55
|
+
target = str(venv_dir())
|
|
56
|
+
package = f"litellm[proxy]=={LITELLM_VERSION}"
|
|
57
|
+
if uv: # much faster, and brings its own Python if the machine has none
|
|
58
|
+
return [[uv, "venv", "--quiet", "--allow-existing", "--python", PYTHON_FOR_UV, target],
|
|
59
|
+
[uv, "pip", "install", "--quiet", "--python", str(_python()), package]]
|
|
60
|
+
return [[sys.executable, "-m", "venv", target],
|
|
61
|
+
[str(_python()), "-m", "pip", "install", "--quiet", "--upgrade", "pip"],
|
|
62
|
+
[str(_python()), "-m", "pip", "install", "--quiet", package]]
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def install(run: Runner = subprocess.run) -> bool:
|
|
66
|
+
"""Install the pinned connector; False (with the reason printed by the tool) on failure."""
|
|
67
|
+
venv_dir().parent.mkdir(parents=True, exist_ok=True)
|
|
68
|
+
for command in install_commands(shutil.which("uv")):
|
|
69
|
+
if run(command).returncode != 0:
|
|
70
|
+
return False
|
|
71
|
+
_stamp().parent.mkdir(parents=True, exist_ok=True)
|
|
72
|
+
_stamp().write_text(LITELLM_VERSION, encoding="utf-8")
|
|
73
|
+
return True
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def config_dict(config: dict) -> dict:
|
|
77
|
+
"""LiteLLM proxy config: Claude always (Claude Code login forwarded), GPT if enabled."""
|
|
78
|
+
models = []
|
|
79
|
+
if config["chatgpt"]["enabled"]:
|
|
80
|
+
models += [{"model_name": name, "model_info": {"mode": "responses"},
|
|
81
|
+
"litellm_params": {"model": f"chatgpt/{name}"}}
|
|
82
|
+
for name in config["chatgpt"]["models"]]
|
|
83
|
+
# Claude Code sends plain names like claude-haiku-5-5.
|
|
84
|
+
models += [{"model_name": "claude-*", "litellm_params": {"model": "anthropic/claude-*"}},
|
|
85
|
+
{"model_name": "anthropic/*", "litellm_params": {"model": "anthropic/*"}}]
|
|
86
|
+
return {
|
|
87
|
+
"model_list": models,
|
|
88
|
+
# Claude Code sends Anthropic-only fields (thinking, cache_control, ...); drop what GPT
|
|
89
|
+
# rejects.
|
|
90
|
+
"litellm_settings": {"drop_params": True},
|
|
91
|
+
"general_settings": {
|
|
92
|
+
"master_key": "os.environ/LITELLM_MASTER_KEY", # never the key itself
|
|
93
|
+
# Forward anthropic-beta and x-* headers (Claude Code's beta flags, e.g. 1h cache).
|
|
94
|
+
"forward_client_headers_to_llm_api": True,
|
|
95
|
+
},
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def write_config(config: dict) -> Path:
|
|
100
|
+
"""Write the generated config (JSON is valid YAML, so no YAML library is needed)."""
|
|
101
|
+
path = userconfig.home() / "litellm_config.yaml"
|
|
102
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
103
|
+
path.write_text(json.dumps(config_dict(config), indent=2) + "\n", encoding="utf-8")
|
|
104
|
+
return path
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def url(config: dict) -> str:
|
|
108
|
+
return f"http://127.0.0.1:{config['litellm']['port']}"
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def command(config: dict) -> list[str]:
|
|
112
|
+
config_file = os.environ.get("TOKENCUT_LITELLM_CONFIG") or str(write_config(config))
|
|
113
|
+
return [str(executable()), "--config", config_file, "--host", "127.0.0.1",
|
|
114
|
+
"--port", str(config["litellm"]["port"])]
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def process_env(environ: dict[str, str]) -> dict[str, str]:
|
|
118
|
+
# UTF-8: LiteLLM's banner crashes a cp1252 Windows console otherwise.
|
|
119
|
+
env = {**environ, "PYTHONUTF8": "1", "PYTHONIOENCODING": "utf-8"}
|
|
120
|
+
master = userconfig.key("litellm", create=False)
|
|
121
|
+
if master:
|
|
122
|
+
env["LITELLM_MASTER_KEY"] = master
|
|
123
|
+
return env
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def chatgpt_logged_in() -> bool:
|
|
127
|
+
return CHATGPT_AUTH.exists()
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def chatgpt_login(run: Runner = subprocess.run) -> bool:
|
|
131
|
+
"""ChatGPT device-code sign-in (prints a link and a code); the token stays in LiteLLM's file."""
|
|
132
|
+
code = ("from litellm.llms.chatgpt.authenticator import Authenticator; "
|
|
133
|
+
"Authenticator().get_access_token()")
|
|
134
|
+
return run([str(_python()), "-c", code], env=process_env(dict(os.environ))).returncode == 0
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""read_pdf: a local PDF reader for the agent (MCP tool), text pages as text.
|
|
2
|
+
|
|
3
|
+
Runs NEXT TO the agent, like the repo index; the gateway still never reads the disk.
|
|
4
|
+
Claude Code with Poppler reads long PDFs as page pictures only, so the gateway's PDF rule
|
|
5
|
+
(attachments step 7) never sees a PDF. This tool applies the same page check where the file
|
|
6
|
+
is: pages with only text come back as text, pages with charts, tables, images or scans come
|
|
7
|
+
back as pictures. A PreToolUse hook (`python -m tokencut.pdftool hook`) sends Claude Code's
|
|
8
|
+
Read of a .pdf to this tool, so the choice does not depend on the model following a prompt.
|
|
9
|
+
"""
|