k0ntrol-cli 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.
- k0ntrol/__init__.py +1 -0
- k0ntrol/agents.py +998 -0
- k0ntrol/cli.py +190 -0
- k0ntrol/config.py +330 -0
- k0ntrol/credentials.py +78 -0
- k0ntrol/defaults/config.yaml +23 -0
- k0ntrol/events.py +838 -0
- k0ntrol/frontends/__init__.py +1 -0
- k0ntrol/frontends/ask.py +218 -0
- k0ntrol/frontends/badges.py +228 -0
- k0ntrol/frontends/banner.py +128 -0
- k0ntrol/frontends/diffview.py +174 -0
- k0ntrol/frontends/editor.py +701 -0
- k0ntrol/frontends/klog.html +740 -0
- k0ntrol/frontends/klog.py +403 -0
- k0ntrol/frontends/logo.py +12 -0
- k0ntrol/frontends/mentions.py +29 -0
- k0ntrol/frontends/prompt.py +340 -0
- k0ntrol/frontends/render.py +485 -0
- k0ntrol/frontends/session.py +717 -0
- k0ntrol/frontends/setup.py +442 -0
- k0ntrol/frontends/terminal.py +396 -0
- k0ntrol/frontends/theme.py +24 -0
- k0ntrol/helpers.py +108 -0
- k0ntrol/init_flow.py +362 -0
- k0ntrol/langfuse.py +173 -0
- k0ntrol/memory/__init__.py +0 -0
- k0ntrol/memory/index.py +235 -0
- k0ntrol/memory/layout.py +66 -0
- k0ntrol/memory/python_extract.py +189 -0
- k0ntrol/memory/reflect.py +366 -0
- k0ntrol/memory/refresh.py +21 -0
- k0ntrol/memory/scan.py +538 -0
- k0ntrol/memory/session.py +145 -0
- k0ntrol/otel.py +429 -0
- k0ntrol/pricing.py +29 -0
- k0ntrol/prompts/chatty.md +17 -0
- k0ntrol/prompts/implementer.md +14 -0
- k0ntrol/prompts/planner.md +42 -0
- k0ntrol/prompts/researcher.md +20 -0
- k0ntrol/prompts/reviewer.md +34 -0
- k0ntrol/providers.py +718 -0
- k0ntrol/search.py +133 -0
- k0ntrol/tools/__init__.py +0 -0
- k0ntrol/tools/bulk_read.py +93 -0
- k0ntrol/tools/errors.py +50 -0
- k0ntrol/tools/fetch_url.py +212 -0
- k0ntrol/tools/net.py +317 -0
- k0ntrol/tools/path_guard.py +45 -0
- k0ntrol/tools/read.py +174 -0
- k0ntrol/tools/run_tests.py +66 -0
- k0ntrol/tools/runner.py +177 -0
- k0ntrol/tools/search_memory.py +32 -0
- k0ntrol/tools/slice_guard.py +166 -0
- k0ntrol/tools/slice_journal.py +481 -0
- k0ntrol/tools/tracker.py +115 -0
- k0ntrol/tools/web_search.py +199 -0
- k0ntrol/tools/write.py +123 -0
- k0ntrol/tracker/__init__.py +31 -0
- k0ntrol/tracker/github.py +371 -0
- k0ntrol/tracker/local_files.py +241 -0
- k0ntrol/tracker/protocol.py +210 -0
- k0ntrol/workflow/__init__.py +29 -0
- k0ntrol/workflow/change.py +605 -0
- k0ntrol/workflow/ci_gate.py +96 -0
- k0ntrol/workflow/context.py +137 -0
- k0ntrol/workflow/implement.py +1150 -0
- k0ntrol/workflow/nodes.py +331 -0
- k0ntrol/workflow/plan.py +675 -0
- k0ntrol/workflow/qa.py +98 -0
- k0ntrol/workflow/research.py +533 -0
- k0ntrol/workflow/routers.py +273 -0
- k0ntrol/workflow/run.py +1165 -0
- k0ntrol/workflow/state.py +146 -0
- k0ntrol/workflow/triage.py +269 -0
- k0ntrol_cli-0.1.0.dist-info/METADATA +154 -0
- k0ntrol_cli-0.1.0.dist-info/RECORD +80 -0
- k0ntrol_cli-0.1.0.dist-info/WHEEL +4 -0
- k0ntrol_cli-0.1.0.dist-info/entry_points.txt +2 -0
- k0ntrol_cli-0.1.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
k0ntrol/frontends/ask.py
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"""``/ask <question>``: CHATTY answers with its read-only tools.
|
|
2
|
+
|
|
3
|
+
``/ask`` runs no graph: the free doctor checks for CHATTY and the
|
|
4
|
+
``bulk_read`` helper gate the call, project memory is refreshed once
|
|
5
|
+
(rescan + one index rebuild), the answer streams to the REPL, and every
|
|
6
|
+
model and tool call writes a ``chat``-scoped event (never ``run``) —
|
|
7
|
+
including the rescan's helper calls. ``@path`` mentions pass relative
|
|
8
|
+
paths only — CHATTY reads the files itself; file contents are never
|
|
9
|
+
inlined into the prompt.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from collections.abc import Callable
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from langchain.chat_models import init_chat_model
|
|
17
|
+
from langchain_core.messages import HumanMessage
|
|
18
|
+
|
|
19
|
+
from k0ntrol import __version__
|
|
20
|
+
from k0ntrol.agents import AgentResult, agent_tools, build_agent
|
|
21
|
+
from k0ntrol.events import (
|
|
22
|
+
EventWriter,
|
|
23
|
+
K0EventHandler,
|
|
24
|
+
ProgressRenderer,
|
|
25
|
+
events_path,
|
|
26
|
+
new_chat_id,
|
|
27
|
+
)
|
|
28
|
+
from k0ntrol.frontends.mentions import MentionError, mention_paths
|
|
29
|
+
from k0ntrol.helpers import helper_invoker
|
|
30
|
+
from k0ntrol.memory.refresh import refresh_memory
|
|
31
|
+
from k0ntrol.otel import build_run_trace, trace_callbacks
|
|
32
|
+
from k0ntrol.providers import error_lines, free_checks, has_errors
|
|
33
|
+
from k0ntrol.workflow.change import current_branch
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def answer_ask(
|
|
37
|
+
question: str,
|
|
38
|
+
*,
|
|
39
|
+
root: Path,
|
|
40
|
+
config: dict,
|
|
41
|
+
write: Callable[[str], None],
|
|
42
|
+
on_text: Callable[[str], None] | None = None,
|
|
43
|
+
init: Callable = init_chat_model,
|
|
44
|
+
check: Callable = free_checks,
|
|
45
|
+
refresh: Callable = refresh_memory,
|
|
46
|
+
tracing: Callable = build_run_trace,
|
|
47
|
+
history: str = "",
|
|
48
|
+
session: str | None = None,
|
|
49
|
+
) -> AgentResult | None:
|
|
50
|
+
"""Answer ``question`` with CHATTY.
|
|
51
|
+
|
|
52
|
+
``history`` is the conversation so far, as text: each answer is still a
|
|
53
|
+
fresh agent run, it just reads what was said before.
|
|
54
|
+
|
|
55
|
+
``session`` is the REPL sitting's id: the Langfuse session that groups
|
|
56
|
+
this chat's trace with the rest of the sitting.
|
|
57
|
+
|
|
58
|
+
``write`` takes progress lines and errors; ``on_text`` takes the
|
|
59
|
+
streamed answer deltas (defaults to ``write``). Returns the agent
|
|
60
|
+
result, or ``None`` when the doctor checks, a bad ``@mention`` or the
|
|
61
|
+
memory refresh refuse the run.
|
|
62
|
+
"""
|
|
63
|
+
prompt = _prepare(question, root, config, write, check)
|
|
64
|
+
if prompt is None:
|
|
65
|
+
return None
|
|
66
|
+
prompt = f"{history}\n\n{prompt}" if history else prompt
|
|
67
|
+
writer, handler, chat_id = _chat_events(root, write)
|
|
68
|
+
trace = tracing(chat_id) # each /ask is its own trace (#49)
|
|
69
|
+
if not _refresh(root, config, init, handler, write, refresh, trace, session):
|
|
70
|
+
_finish_ask(trace, "failed", root, session)
|
|
71
|
+
return None
|
|
72
|
+
agent = build_agent(
|
|
73
|
+
"chatty",
|
|
74
|
+
config,
|
|
75
|
+
agent_tools("chatty", root=root, cfg=config, init=init),
|
|
76
|
+
root=root,
|
|
77
|
+
init=init,
|
|
78
|
+
)
|
|
79
|
+
try:
|
|
80
|
+
result = _run_chatty(
|
|
81
|
+
agent, prompt, handler, on_text or write, trace, session
|
|
82
|
+
)
|
|
83
|
+
except Exception as error:
|
|
84
|
+
write(f"CHATTY failed: {error}")
|
|
85
|
+
_finish_ask(trace, "failed", root, session)
|
|
86
|
+
return None
|
|
87
|
+
if result.limit_hit:
|
|
88
|
+
write(
|
|
89
|
+
f"CHATTY hit its {agent.model_calls} model-call limit; "
|
|
90
|
+
"ask again or narrow the question."
|
|
91
|
+
)
|
|
92
|
+
_finish_ask(trace, "done", root, session)
|
|
93
|
+
return result
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _prepare(
|
|
97
|
+
question: str,
|
|
98
|
+
root: Path,
|
|
99
|
+
config: dict,
|
|
100
|
+
write: Callable[[str], None],
|
|
101
|
+
check: Callable,
|
|
102
|
+
) -> str | None:
|
|
103
|
+
"""Usage check, the free doctor gate, then the prompt; None = refuse."""
|
|
104
|
+
if not question.strip():
|
|
105
|
+
write("usage: /ask <question>")
|
|
106
|
+
return None
|
|
107
|
+
checks = check(
|
|
108
|
+
config,
|
|
109
|
+
names=("agents.chatty", "helpers.bulk_read"),
|
|
110
|
+
root=root,
|
|
111
|
+
services=(), # local path: no GitHub auth, remote or search key
|
|
112
|
+
)
|
|
113
|
+
if has_errors(checks):
|
|
114
|
+
for line in error_lines(checks):
|
|
115
|
+
write(line)
|
|
116
|
+
return None
|
|
117
|
+
try:
|
|
118
|
+
return _build_prompt(question, root)
|
|
119
|
+
except MentionError as error:
|
|
120
|
+
write(str(error))
|
|
121
|
+
return None
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _build_prompt(question: str, root: Path) -> str:
|
|
125
|
+
"""The question plus the mentioned files as root-relative paths."""
|
|
126
|
+
mentions = mention_paths(question, root)
|
|
127
|
+
if not mentions:
|
|
128
|
+
return question
|
|
129
|
+
relative = [
|
|
130
|
+
path.relative_to(root.resolve()).as_posix() for path in mentions
|
|
131
|
+
]
|
|
132
|
+
return (
|
|
133
|
+
question
|
|
134
|
+
+ "\n\nMentioned files (read them with your tools):\n"
|
|
135
|
+
+ "\n".join(relative)
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _chat_events(root: Path, write: Callable[[str], None]):
|
|
140
|
+
"""One chat-scoped writer + handler shared by the refresh and CHATTY."""
|
|
141
|
+
path = events_path(root)
|
|
142
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
143
|
+
chat_id = new_chat_id()
|
|
144
|
+
writer = EventWriter(
|
|
145
|
+
path, chat=chat_id, progress=ProgressRenderer(write)
|
|
146
|
+
)
|
|
147
|
+
return writer, K0EventHandler(writer), chat_id
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _callbacks(handler: K0EventHandler, trace) -> list:
|
|
151
|
+
"""The events handler plus the optional Langfuse and OTel ones (#49)."""
|
|
152
|
+
return [handler, *trace_callbacks(trace)]
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _finish_ask(trace, outcome: str, root: Path, session: str | None) -> None:
|
|
156
|
+
trace.finish(
|
|
157
|
+
kind="ask",
|
|
158
|
+
outcome=outcome,
|
|
159
|
+
session_id=session,
|
|
160
|
+
metadata=_ask_metadata(root),
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _langfuse_metadata(session: str | None) -> dict:
|
|
165
|
+
"""Root-run metadata the Langfuse handler reads: ask tag + sitting."""
|
|
166
|
+
metadata: dict[str, Any] = {"langfuse_tags": ["ask"]}
|
|
167
|
+
if session:
|
|
168
|
+
metadata["langfuse_session_id"] = session
|
|
169
|
+
return metadata
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def _ask_metadata(root: Path) -> dict:
|
|
173
|
+
"""The ask trace's metadata (#49): branch and k0 version."""
|
|
174
|
+
metadata = {"k0_version": __version__}
|
|
175
|
+
branch = current_branch(root)
|
|
176
|
+
if branch:
|
|
177
|
+
metadata["branch"] = branch
|
|
178
|
+
return metadata
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def _refresh(
|
|
182
|
+
root: Path,
|
|
183
|
+
config: dict,
|
|
184
|
+
init: Callable,
|
|
185
|
+
handler: K0EventHandler,
|
|
186
|
+
write: Callable[[str], None],
|
|
187
|
+
refresh: Callable,
|
|
188
|
+
trace,
|
|
189
|
+
session: str | None = None,
|
|
190
|
+
) -> bool:
|
|
191
|
+
"""Rescan + one index rebuild; the rescan's helper calls are
|
|
192
|
+
chat-scoped ``model`` events. Returns False when refresh fails."""
|
|
193
|
+
invoke = helper_invoker(
|
|
194
|
+
config,
|
|
195
|
+
"bulk_read",
|
|
196
|
+
init=init,
|
|
197
|
+
config={
|
|
198
|
+
"callbacks": _callbacks(handler, trace),
|
|
199
|
+
"metadata": _langfuse_metadata(session),
|
|
200
|
+
},
|
|
201
|
+
)
|
|
202
|
+
try:
|
|
203
|
+
refresh(root, config=config, invoke=invoke)
|
|
204
|
+
except Exception as error:
|
|
205
|
+
write(f"memory refresh failed: {error}")
|
|
206
|
+
return False
|
|
207
|
+
return True
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def _run_chatty(agent, prompt, handler, on_text, trace, session=None) -> AgentResult:
|
|
211
|
+
return agent.stream(
|
|
212
|
+
[HumanMessage(prompt)],
|
|
213
|
+
config={
|
|
214
|
+
"callbacks": _callbacks(handler, trace),
|
|
215
|
+
"metadata": _langfuse_metadata(session),
|
|
216
|
+
},
|
|
217
|
+
on_text=on_text,
|
|
218
|
+
)
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"""8-bit badges for the services a project uses, drawn in the welcome banner.
|
|
2
|
+
|
|
3
|
+
Each icon is 8 pixels tall; half blocks put two pixel rows in one text row,
|
|
4
|
+
so an icon is 4 text rows, with the service name and its role (issues,
|
|
5
|
+
code, models) under it. The art is k0's own pixel take on each service in
|
|
6
|
+
its brand colour, not a copy of anyone's logo.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
|
|
11
|
+
from rich.text import Text
|
|
12
|
+
|
|
13
|
+
from k0ntrol.frontends.theme import DIM
|
|
14
|
+
|
|
15
|
+
_GAP = " " # between two badges
|
|
16
|
+
_ROLE_COLOR = "#6B7280"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True)
|
|
20
|
+
class Badge:
|
|
21
|
+
name: str
|
|
22
|
+
role: str
|
|
23
|
+
pixels: tuple[str, ...] # one string per pixel row; "." is empty
|
|
24
|
+
colors: dict[str, str] # pixel key -> colour
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _badge(name: str, role: str, colors: dict[str, str], *pixels: str) -> Badge:
|
|
28
|
+
return Badge(name, role, pixels, colors)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _issues(name: str, color: str) -> Badge:
|
|
32
|
+
"""An open-issue ring, in the tracker's colour."""
|
|
33
|
+
return _badge(name, "issues", {"i": color},
|
|
34
|
+
"..iiiii..",
|
|
35
|
+
".i.....i.",
|
|
36
|
+
"i.......i",
|
|
37
|
+
"i...i...i",
|
|
38
|
+
"i..iii..i",
|
|
39
|
+
"i...i...i",
|
|
40
|
+
".i.....i.",
|
|
41
|
+
"..iiiii..")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
TRACKER_BADGES: dict[str, Badge] = {
|
|
45
|
+
"github": _issues("GitHub", "#3FB950"),
|
|
46
|
+
"gitlab": _issues("GitLab", "#FC6D26"),
|
|
47
|
+
"linear": _badge("Linear", "issues", {"p": "#5E6AD2"},
|
|
48
|
+
"...ppp...",
|
|
49
|
+
".p.ppppp.",
|
|
50
|
+
"p.p.ppppp",
|
|
51
|
+
"pp.p.pppp",
|
|
52
|
+
"ppp.p.ppp",
|
|
53
|
+
"pppp.p.pp",
|
|
54
|
+
".pppp.p..",
|
|
55
|
+
"...ppp..."),
|
|
56
|
+
"jira": _badge("Jira", "issues", {"j": "#2684FF", "d": "#0052CC"},
|
|
57
|
+
"....j....",
|
|
58
|
+
"...jjj...",
|
|
59
|
+
"..jj.jj..",
|
|
60
|
+
".jj.d.jj.",
|
|
61
|
+
"jj.ddd.jj",
|
|
62
|
+
".jj.d.jj.",
|
|
63
|
+
"..jj.jj..",
|
|
64
|
+
"...jjj..."),
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
VCS_BADGES: dict[str, Badge] = {
|
|
68
|
+
"github": _badge("GitHub", "code", {"w": "#E5E7EB"},
|
|
69
|
+
".w.....w.",
|
|
70
|
+
".ww...ww.",
|
|
71
|
+
".wwwwwww.",
|
|
72
|
+
"ww.www.ww",
|
|
73
|
+
"wwwwwwwww",
|
|
74
|
+
".wwwwwww.",
|
|
75
|
+
"..w.w.w..",
|
|
76
|
+
".w..w..w."),
|
|
77
|
+
"gitlab": _badge("GitLab", "code", {"r": "#E24329", "o": "#FC6D26", "y": "#FCA326"},
|
|
78
|
+
"r.......r",
|
|
79
|
+
"rr.....rr",
|
|
80
|
+
"rrr...rrr",
|
|
81
|
+
"yrrooorry",
|
|
82
|
+
"yyoooooyy",
|
|
83
|
+
".yyoooyy.",
|
|
84
|
+
"...yoy...",
|
|
85
|
+
"....o...."),
|
|
86
|
+
"bitbucket": _badge("Bitbucket", "code", {"b": "#2684FF", "d": "#0052CC"},
|
|
87
|
+
"bbbbbbbbb",
|
|
88
|
+
"bbbbbbbbb",
|
|
89
|
+
".bbdddbb.",
|
|
90
|
+
".bbd.dbb.",
|
|
91
|
+
"..bdddb..",
|
|
92
|
+
"..bbbbb..",
|
|
93
|
+
"...bbb...",
|
|
94
|
+
"........."),
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
LLM_BADGES: dict[str, Badge] = {
|
|
98
|
+
"anthropic": _badge("Anthropic", "models", {"c": "#D97757"},
|
|
99
|
+
"....c....",
|
|
100
|
+
".c..c..c.",
|
|
101
|
+
"..c.c.c..",
|
|
102
|
+
"...ccc...",
|
|
103
|
+
"ccccccccc",
|
|
104
|
+
"...ccc...",
|
|
105
|
+
"..c.c.c..",
|
|
106
|
+
".c..c..c."),
|
|
107
|
+
"bedrock": _badge("Bedrock", "models", {"o": "#FF9900", "s": "#4B5563"},
|
|
108
|
+
"sssssssss",
|
|
109
|
+
".........",
|
|
110
|
+
"sssssssss",
|
|
111
|
+
".........",
|
|
112
|
+
"o.......o",
|
|
113
|
+
".oo...oo.",
|
|
114
|
+
"...ooo..o",
|
|
115
|
+
".......oo"),
|
|
116
|
+
"ollama": _badge("Ollama", "models", {"w": "#E5E7EB"},
|
|
117
|
+
"..w...w..",
|
|
118
|
+
"..w...w..",
|
|
119
|
+
"..wwwww..",
|
|
120
|
+
".ww.w.ww.",
|
|
121
|
+
".wwwwwww.",
|
|
122
|
+
"..wwwww..",
|
|
123
|
+
"...www...",
|
|
124
|
+
"...www..."),
|
|
125
|
+
"lmstudio": _badge("LM Studio", "models", {"v": "#8B5CF6", "l": "#C4B5FD"},
|
|
126
|
+
".vvvvv...",
|
|
127
|
+
".........",
|
|
128
|
+
"...lllll.",
|
|
129
|
+
".........",
|
|
130
|
+
".vvvvvv..",
|
|
131
|
+
".........",
|
|
132
|
+
"..lllll..",
|
|
133
|
+
"........."),
|
|
134
|
+
"openai": _badge("OpenAI", "models", {"w": "#E5E7EB"},
|
|
135
|
+
"...www...",
|
|
136
|
+
".ww...ww.",
|
|
137
|
+
"w..www..w",
|
|
138
|
+
"w.w...w.w",
|
|
139
|
+
"w.w...w.w",
|
|
140
|
+
"w..www..w",
|
|
141
|
+
".ww...ww.",
|
|
142
|
+
"...www..."),
|
|
143
|
+
"openrouter": _badge("OpenRouter", "models", {"s": "#94A3B8"},
|
|
144
|
+
".......s.",
|
|
145
|
+
".....ssss",
|
|
146
|
+
"....s..s.",
|
|
147
|
+
"ssss.....",
|
|
148
|
+
"....s..s.",
|
|
149
|
+
".....ssss",
|
|
150
|
+
".......s.",
|
|
151
|
+
"........."),
|
|
152
|
+
"vertex": _badge("Vertex", "models", {"b": "#4285F4", "r": "#EA4335", "y": "#FBBC04", "g": "#34A853"},
|
|
153
|
+
"....b....",
|
|
154
|
+
"...bbr...",
|
|
155
|
+
"..bbbrr..",
|
|
156
|
+
".bbbbrrr.",
|
|
157
|
+
".ggggyyy.",
|
|
158
|
+
"..gggyy..",
|
|
159
|
+
"...ggy...",
|
|
160
|
+
"....g...."),
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _chip(name: str) -> Badge:
|
|
165
|
+
"""Any other model provider: a plain chip with its name."""
|
|
166
|
+
return _badge(name, "models", {"g": "#9CA3AF", "d": "#4B5563"},
|
|
167
|
+
".g.g.g.g.",
|
|
168
|
+
"ggggggggg",
|
|
169
|
+
".gdddddg.",
|
|
170
|
+
"ggdddddgg",
|
|
171
|
+
".gdddddg.",
|
|
172
|
+
"ggdddddgg",
|
|
173
|
+
"ggggggggg",
|
|
174
|
+
".g.g.g.g.")
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def service_badges(*, tracker: str | None, vcs: str | None, provider: str | None) -> list[Badge]:
|
|
178
|
+
"""The project's badges, ticket then code then models; unknown or
|
|
179
|
+
missing trackers and hosts are left out, unknown models get a chip."""
|
|
180
|
+
badges = [TRACKER_BADGES.get(tracker or ""), VCS_BADGES.get(vcs or "")]
|
|
181
|
+
if provider:
|
|
182
|
+
badges.append(LLM_BADGES.get(provider) or _chip(provider))
|
|
183
|
+
return [badge for badge in badges if badge is not None]
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def half_block(top: str, bottom: str, colors: dict[str, str]) -> tuple[str, str]:
|
|
187
|
+
"""One text cell holding two pixels: the character and its style."""
|
|
188
|
+
if top == bottom:
|
|
189
|
+
return (" ", "") if top == "." else ("█", colors[top])
|
|
190
|
+
if bottom == ".":
|
|
191
|
+
return "▀", colors[top]
|
|
192
|
+
if top == ".":
|
|
193
|
+
return "▄", colors[bottom]
|
|
194
|
+
return "▀", f"{colors[top]} on {colors[bottom]}"
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def _width(badge: Badge) -> int:
|
|
198
|
+
return max(len(badge.pixels[0]), len(badge.name), len(badge.role))
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def block_width(badges: list[Badge]) -> int:
|
|
202
|
+
"""Columns the badge block takes, gaps included."""
|
|
203
|
+
return sum(_width(badge) for badge in badges) + len(_GAP) * max(len(badges) - 1, 0)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _icon_rows(badge: Badge) -> list[Text]:
|
|
207
|
+
rows = []
|
|
208
|
+
for top, bottom in zip(badge.pixels[::2], badge.pixels[1::2], strict=True):
|
|
209
|
+
row = Text()
|
|
210
|
+
for pair in zip(top, bottom, strict=True):
|
|
211
|
+
row.append(*half_block(*pair, badge.colors))
|
|
212
|
+
rows.append(row)
|
|
213
|
+
return rows
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def _column(badge: Badge) -> list[Text]:
|
|
217
|
+
"""A badge's rows, each centred to the badge width."""
|
|
218
|
+
width = _width(badge)
|
|
219
|
+
rows = [*_icon_rows(badge), Text(badge.name, style=DIM), Text(badge.role, style=_ROLE_COLOR)]
|
|
220
|
+
for row in rows:
|
|
221
|
+
row.align("center", width)
|
|
222
|
+
return rows
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def badge_block(badges: list[Badge]) -> list[Text]:
|
|
226
|
+
"""The badges side by side: four icon rows, then names, then roles."""
|
|
227
|
+
columns = [_column(badge) for badge in badges]
|
|
228
|
+
return [Text(_GAP).join(cells) for cells in zip(*columns, strict=True)]
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import os
|
|
3
|
+
from typing import TextIO
|
|
4
|
+
|
|
5
|
+
from rich.box import ROUNDED
|
|
6
|
+
from rich.cells import cell_len
|
|
7
|
+
from rich.console import Console
|
|
8
|
+
from rich.panel import Panel
|
|
9
|
+
from rich.table import Table
|
|
10
|
+
from rich.text import Text
|
|
11
|
+
|
|
12
|
+
from k0ntrol.frontends.badges import Badge, badge_block, block_width
|
|
13
|
+
from k0ntrol.frontends.logo import LOGO
|
|
14
|
+
|
|
15
|
+
_BORDER_COLOR = "#374151"
|
|
16
|
+
_LABEL_COLOR = "#9CA3AF"
|
|
17
|
+
_GRADIENT_START = (94, 234, 212)
|
|
18
|
+
_GRADIENT_END = (167, 139, 250)
|
|
19
|
+
# Panel border and padding (4), the logo (25) and the gap before the labels (2).
|
|
20
|
+
_LEFT_FIXED = 4 + 25 + 2
|
|
21
|
+
_BADGE_GAP = 3 # between the labels and the badges
|
|
22
|
+
_MIN_LABEL_ROOM = 20 # below this the badges give way to the path
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def banner_layout(width: int, badges: list[Badge]) -> tuple[list[Badge], int]:
|
|
26
|
+
"""The badges that fit a ``width``-column banner and the room left for
|
|
27
|
+
the path label; a narrow terminal drops the badges, never the path."""
|
|
28
|
+
room = width - _LEFT_FIXED - _BADGE_GAP - block_width(badges)
|
|
29
|
+
if not badges or room < _MIN_LABEL_ROOM:
|
|
30
|
+
return [], width - 40
|
|
31
|
+
return badges, room
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def render_banner(
|
|
35
|
+
*,
|
|
36
|
+
version: str,
|
|
37
|
+
provider: str | None,
|
|
38
|
+
cwd: str,
|
|
39
|
+
color: bool,
|
|
40
|
+
badges: list[Badge] | None = None,
|
|
41
|
+
width: int | None = None,
|
|
42
|
+
) -> str:
|
|
43
|
+
"""The welcome panel. With ``width`` it spans the terminal and the
|
|
44
|
+
``badges`` sit flush right; without, it takes its natural width."""
|
|
45
|
+
left = _build_body(version, provider, cwd)
|
|
46
|
+
badges = badges or []
|
|
47
|
+
natural = _body_width(left) + (_BADGE_GAP + block_width(badges) if badges else 0)
|
|
48
|
+
panel = Panel(
|
|
49
|
+
_with_badges(left, badges),
|
|
50
|
+
box=ROUNDED,
|
|
51
|
+
border_style=_BORDER_COLOR,
|
|
52
|
+
padding=(0, 1),
|
|
53
|
+
expand=width is not None,
|
|
54
|
+
)
|
|
55
|
+
output = io.StringIO()
|
|
56
|
+
# Both dimensions pinned: `Console.size` then returns them instead of
|
|
57
|
+
# probing the ambient terminal (a fixed 80x25 under TERM=dumb). The
|
|
58
|
+
# height value is inert — only the width constrains the render.
|
|
59
|
+
console = Console(
|
|
60
|
+
file=output,
|
|
61
|
+
no_color=not color,
|
|
62
|
+
force_terminal=color,
|
|
63
|
+
color_system="truecolor" if color else None,
|
|
64
|
+
width=width or max(80, natural + 4),
|
|
65
|
+
height=25,
|
|
66
|
+
)
|
|
67
|
+
console.print(panel)
|
|
68
|
+
return output.getvalue()
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _with_badges(left: Text, badges: list[Badge]) -> Text | Table:
|
|
72
|
+
"""The logo and labels, with the badge block on the right, one row down."""
|
|
73
|
+
if not badges:
|
|
74
|
+
return left
|
|
75
|
+
# A stretching middle column pushes the block right; the block itself
|
|
76
|
+
# stays left-justified so centred names and roles keep their columns.
|
|
77
|
+
grid = Table.grid(padding=(0, _BADGE_GAP), expand=True)
|
|
78
|
+
grid.add_column(no_wrap=True)
|
|
79
|
+
grid.add_column(ratio=1)
|
|
80
|
+
grid.add_column(no_wrap=True)
|
|
81
|
+
grid.add_row(left, "", Text("\n").join([Text(""), *badge_block(badges)]))
|
|
82
|
+
return grid
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def color_enabled(stream: TextIO) -> bool:
|
|
86
|
+
return "NO_COLOR" not in os.environ and stream.isatty()
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _body_width(body: Text) -> int:
|
|
90
|
+
return max(cell_len(line) for line in body.plain.splitlines())
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _build_body(version: str, provider: str | None, cwd: str) -> Text:
|
|
94
|
+
labels = {
|
|
95
|
+
2: f"k0ntrol v{version}",
|
|
96
|
+
4: cwd,
|
|
97
|
+
}
|
|
98
|
+
if provider:
|
|
99
|
+
labels[3] = provider
|
|
100
|
+
body = Text()
|
|
101
|
+
for index, logo_line in enumerate(LOGO.splitlines()):
|
|
102
|
+
if index:
|
|
103
|
+
body.append("\n")
|
|
104
|
+
body.append(_logo_line(logo_line))
|
|
105
|
+
label = labels.get(index)
|
|
106
|
+
if label is not None:
|
|
107
|
+
body.append(" ")
|
|
108
|
+
body.append(label, style=_LABEL_COLOR)
|
|
109
|
+
return body
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _logo_line(line: str) -> Text:
|
|
113
|
+
rendered = Text()
|
|
114
|
+
for column, character in enumerate(line):
|
|
115
|
+
if character == " ":
|
|
116
|
+
rendered.append(character)
|
|
117
|
+
else:
|
|
118
|
+
rendered.append(character, style=_logo_color(column))
|
|
119
|
+
return rendered
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _logo_color(column: int) -> str:
|
|
123
|
+
ratio = column / 24
|
|
124
|
+
channels = [
|
|
125
|
+
round(start + (end - start) * ratio)
|
|
126
|
+
for start, end in zip(_GRADIENT_START, _GRADIENT_END)
|
|
127
|
+
]
|
|
128
|
+
return f"rgb({channels[0]},{channels[1]},{channels[2]})"
|