saicode 0.3.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.
- saicode/__init__.py +3 -0
- saicode/__main__.py +4 -0
- saicode/ai/__init__.py +46 -0
- saicode/ai/base.py +82 -0
- saicode/ai/gemini.py +144 -0
- saicode/cli/__init__.py +1 -0
- saicode/cli/main.py +181 -0
- saicode/core/__init__.py +4 -0
- saicode/core/agent.py +166 -0
- saicode/core/config.py +253 -0
- saicode/core/credentials.py +60 -0
- saicode/core/events.py +117 -0
- saicode/core/policy.py +168 -0
- saicode/sandbox/__init__.py +57 -0
- saicode/tools/__init__.py +305 -0
- saicode/tui/__init__.py +1 -0
- saicode/tui/app.py +457 -0
- saicode-0.3.0.dist-info/METADATA +211 -0
- saicode-0.3.0.dist-info/RECORD +23 -0
- saicode-0.3.0.dist-info/WHEEL +4 -0
- saicode-0.3.0.dist-info/entry_points.txt +2 -0
- saicode-0.3.0.dist-info/licenses/LICENSE +202 -0
- saicode-0.3.0.dist-info/licenses/NOTICE +5 -0
saicode/tui/app.py
ADDED
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
"""Terminal front end. Draws events from the core and asks the user for approvals."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from prompt_toolkit import PromptSession
|
|
8
|
+
from prompt_toolkit.history import FileHistory
|
|
9
|
+
from rich.console import Console, Group
|
|
10
|
+
from rich.markdown import Markdown
|
|
11
|
+
from rich.markup import escape
|
|
12
|
+
from rich.panel import Panel
|
|
13
|
+
from rich.rule import Rule
|
|
14
|
+
from rich.status import Status
|
|
15
|
+
from rich.syntax import Syntax
|
|
16
|
+
from rich.table import Table
|
|
17
|
+
from rich.text import Text
|
|
18
|
+
|
|
19
|
+
from saicode import __version__
|
|
20
|
+
from saicode.ai.base import Provider, ProviderError
|
|
21
|
+
from saicode.core.agent import Agent
|
|
22
|
+
from saicode.core.config import Config
|
|
23
|
+
from saicode.core.events import Event, EventBus, SessionLog
|
|
24
|
+
from saicode.core.policy import Policy
|
|
25
|
+
from saicode.tools import ApprovalRequest
|
|
26
|
+
|
|
27
|
+
ACCENT = "#7aa2f7"
|
|
28
|
+
MUTED = "grey50"
|
|
29
|
+
OK = "#9ece6a"
|
|
30
|
+
WARN = "#e0af68"
|
|
31
|
+
ERR = "#f7768e"
|
|
32
|
+
|
|
33
|
+
LOGO = r"""
|
|
34
|
+
███████╗ █████╗ ██╗ ██████╗ ██████╗ ██████╗ ███████╗
|
|
35
|
+
██╔════╝ ██╔══██╗ ██║ ██╔════╝ ██╔═══██╗ ██╔══██╗ ██╔════╝
|
|
36
|
+
███████╗ ███████║ ██║ ██║ ██║ ██║ ██║ ██║ █████╗
|
|
37
|
+
╚════██║ ██╔══██║ ██║ ██║ ██║ ██║ ██║ ██║ ██╔══╝
|
|
38
|
+
███████║ ██║ ██║ ██║ ╚██████╗ ╚██████╔╝ ██████╔╝ ███████╗
|
|
39
|
+
╚══════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝
|
|
40
|
+
"""
|
|
41
|
+
GRADIENT = ["#7dcfff", "#7aa2f7", "#7aa2f7", "#9a8cf7", "#bb9af7", "#bb9af7"]
|
|
42
|
+
|
|
43
|
+
console = Console(highlight=False)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def short_home(path: Path | str) -> str:
|
|
47
|
+
home, s = str(Path.home()), str(path)
|
|
48
|
+
return "~" + s[len(home) :] if s.startswith(home) else s
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def notice(title: str, body: str, style: str = WARN) -> None:
|
|
52
|
+
console.print(
|
|
53
|
+
Panel(
|
|
54
|
+
body,
|
|
55
|
+
title=f"[bold {style}]{title}[/]",
|
|
56
|
+
title_align="left",
|
|
57
|
+
border_style=style,
|
|
58
|
+
padding=(1, 2),
|
|
59
|
+
)
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
# ---- event renderer ------------------------------------------------------------
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class Renderer:
|
|
67
|
+
"""Turns core events into screen output."""
|
|
68
|
+
|
|
69
|
+
def __init__(self) -> None:
|
|
70
|
+
self.status: Status | None = None
|
|
71
|
+
|
|
72
|
+
def _stop(self) -> None:
|
|
73
|
+
if self.status is not None:
|
|
74
|
+
self.status.stop()
|
|
75
|
+
self.status = None
|
|
76
|
+
|
|
77
|
+
def _spin(self, message: str) -> None:
|
|
78
|
+
self._stop()
|
|
79
|
+
self.status = console.status(message, spinner="dots")
|
|
80
|
+
self.status.start()
|
|
81
|
+
|
|
82
|
+
def __call__(self, e: Event) -> None:
|
|
83
|
+
d = e.data
|
|
84
|
+
t = e.type
|
|
85
|
+
if t == "thinking":
|
|
86
|
+
self._spin(f"[{MUTED}]thinking...[/]")
|
|
87
|
+
return
|
|
88
|
+
if t == "waiting":
|
|
89
|
+
self._spin(f"[{WARN}]{d['reason']}, retrying in {d['seconds']}s[/]")
|
|
90
|
+
return
|
|
91
|
+
if t == "running":
|
|
92
|
+
self._spin(f"[{MUTED}]running...[/]")
|
|
93
|
+
return
|
|
94
|
+
self._stop()
|
|
95
|
+
if t == "assistant_message":
|
|
96
|
+
console.print()
|
|
97
|
+
console.print(Markdown(d["text"]))
|
|
98
|
+
console.print()
|
|
99
|
+
elif t == "tool_call":
|
|
100
|
+
console.print(
|
|
101
|
+
Text.assemble(("● ", ACCENT), (d["action"], "bold"), (" " + str(d["target"]), ""))
|
|
102
|
+
)
|
|
103
|
+
elif t == "tool_result":
|
|
104
|
+
style = {"ok": MUTED, "denied": ERR, "error": ERR}.get(d["status"], MUTED)
|
|
105
|
+
if d["tool"] in ("write_file", "edit_file") and d["status"] == "ok":
|
|
106
|
+
style = OK
|
|
107
|
+
if d["tool"] == "run_command" and d["status"] == "ok":
|
|
108
|
+
style = OK
|
|
109
|
+
console.print(Text(" └ " + d["summary"], style=style))
|
|
110
|
+
if d["tool"] == "run_command" and d.get("content"):
|
|
111
|
+
for line in str(d["content"]).splitlines()[-8:]:
|
|
112
|
+
console.print(Text(" " + line, style=MUTED))
|
|
113
|
+
elif t == "turn_end":
|
|
114
|
+
console.print(
|
|
115
|
+
Text(f" {d['tokens_in']:,} in · {d['tokens_out']:,} out (session)", style=MUTED)
|
|
116
|
+
)
|
|
117
|
+
elif t == "cancelled":
|
|
118
|
+
console.print(Text(" cancelled", style=WARN))
|
|
119
|
+
elif t == "step_limit":
|
|
120
|
+
notice("Stopped", f"Reached the limit of {d['limit']} tool steps for one request.")
|
|
121
|
+
elif t == "model_switched":
|
|
122
|
+
console.print(
|
|
123
|
+
Text.assemble((" model switched to ", MUTED), (d["new"], f"bold {ACCENT}"))
|
|
124
|
+
)
|
|
125
|
+
elif t == "conversation_reset":
|
|
126
|
+
console.print(Text(" new conversation started", style=MUTED))
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
# ---- approvals -----------------------------------------------------------------
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def approver(req: ApprovalRequest) -> str:
|
|
133
|
+
if req.detail_format == "diff":
|
|
134
|
+
body = Syntax(req.detail, "diff", theme="ansi_dark")
|
|
135
|
+
elif req.detail_format.startswith("file:"):
|
|
136
|
+
path = req.detail_format[5:]
|
|
137
|
+
lines = req.detail.splitlines()
|
|
138
|
+
shown = "\n".join(lines[:60]) + ("\n..." if len(lines) > 60 else "")
|
|
139
|
+
body = Syntax(
|
|
140
|
+
shown, Syntax.guess_lexer(path, code=req.detail), theme="ansi_dark", line_numbers=True
|
|
141
|
+
)
|
|
142
|
+
else:
|
|
143
|
+
body = Syntax(req.detail, "bash", theme="ansi_dark")
|
|
144
|
+
console.print(Panel(body, border_style=MUTED, padding=(0, 1)))
|
|
145
|
+
kind_name = "file changes" if req.kind == "write" else "commands"
|
|
146
|
+
options = "(y) yes (n) no" + (f" (a) always for {kind_name}" if req.can_always else "")
|
|
147
|
+
while True:
|
|
148
|
+
answer = console.input(f" [bold]Allow?[/] [{MUTED}]{options}[/] ").strip().lower()
|
|
149
|
+
if answer in ("y", "yes"):
|
|
150
|
+
return "yes"
|
|
151
|
+
if answer in ("n", "no", ""):
|
|
152
|
+
return "no"
|
|
153
|
+
if answer in ("a", "always") and req.can_always:
|
|
154
|
+
console.print(Text(f" {kind_name} auto-approved for this session", style=MUTED))
|
|
155
|
+
return "always"
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
# ---- the app -------------------------------------------------------------------
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
class App:
|
|
162
|
+
def __init__(
|
|
163
|
+
self,
|
|
164
|
+
*,
|
|
165
|
+
config: Config,
|
|
166
|
+
provider: Provider,
|
|
167
|
+
agent: Agent,
|
|
168
|
+
bus: EventBus,
|
|
169
|
+
policy: Policy,
|
|
170
|
+
log: SessionLog | None,
|
|
171
|
+
key_source: str,
|
|
172
|
+
workdir: Path,
|
|
173
|
+
runner_name: str,
|
|
174
|
+
):
|
|
175
|
+
self.config = config
|
|
176
|
+
self.provider = provider
|
|
177
|
+
self.agent = agent
|
|
178
|
+
self.bus = bus
|
|
179
|
+
self.policy = policy
|
|
180
|
+
self.log = log
|
|
181
|
+
self.key_source = key_source
|
|
182
|
+
self.workdir = workdir
|
|
183
|
+
self.runner_name = runner_name
|
|
184
|
+
agent.on_error = self.handle_error
|
|
185
|
+
|
|
186
|
+
# ---- errors ----------------------------------------------------------------
|
|
187
|
+
|
|
188
|
+
def pick(self, candidates: list[str], prompt: str) -> str | None:
|
|
189
|
+
table = Table(show_header=False, box=None, padding=(0, 2))
|
|
190
|
+
for i, name in enumerate(candidates, 1):
|
|
191
|
+
table.add_row(f"[{ACCENT}]{i}[/]", name)
|
|
192
|
+
console.print(table)
|
|
193
|
+
choice = console.input(
|
|
194
|
+
f" [bold]{prompt}[/] [{MUTED}](number, Enter to cancel)[/] "
|
|
195
|
+
).strip()
|
|
196
|
+
if choice.isdigit() and 1 <= int(choice) <= len(candidates):
|
|
197
|
+
return candidates[int(choice) - 1]
|
|
198
|
+
return None
|
|
199
|
+
|
|
200
|
+
def handle_error(self, e: ProviderError) -> str | None:
|
|
201
|
+
# Model names and API messages are outside text: escape them before using rich markup.
|
|
202
|
+
model = escape(self.agent.model)
|
|
203
|
+
detail = escape(e.message or "")
|
|
204
|
+
if e.kind == "quota":
|
|
205
|
+
if e.no_quota and e.free_tier:
|
|
206
|
+
reason = (
|
|
207
|
+
f"[bold]{model}[/] is not available on the Gemini API free tier.\n"
|
|
208
|
+
"Your API key's project has no billing attached, so this model has zero quota."
|
|
209
|
+
)
|
|
210
|
+
elif e.daily:
|
|
211
|
+
reason = f"The daily quota for [bold]{model}[/] has been used up."
|
|
212
|
+
if e.retry_after:
|
|
213
|
+
h, rem = divmod(e.retry_after, 3600)
|
|
214
|
+
reason += f" It resets in about {h}h {rem // 60}m."
|
|
215
|
+
else:
|
|
216
|
+
reason = f"The quota for [bold]{model}[/] has been reached."
|
|
217
|
+
notice(
|
|
218
|
+
"Quota exceeded",
|
|
219
|
+
reason + "\n\n"
|
|
220
|
+
f"[{MUTED}]To use paid models: aistudio.google.com → API keys → Set up billing "
|
|
221
|
+
"for this key's project.[/]",
|
|
222
|
+
)
|
|
223
|
+
options = [m for m in self.config.get("model.fallbacks", []) if m != model]
|
|
224
|
+
chosen = self.pick(options, "Switch to another model and retry?") if options else None
|
|
225
|
+
if chosen:
|
|
226
|
+
self.agent.switch_model(chosen)
|
|
227
|
+
return "retry"
|
|
228
|
+
return None
|
|
229
|
+
messages = {
|
|
230
|
+
"auth": (
|
|
231
|
+
"Invalid API key",
|
|
232
|
+
"The provider rejected the API key.\n"
|
|
233
|
+
f"[{MUTED}]Run [bold]saicode login[/bold] to store a new key.[/]",
|
|
234
|
+
),
|
|
235
|
+
"permission": (
|
|
236
|
+
"Permission denied",
|
|
237
|
+
f"This key cannot use this model.\n[{MUTED}]{detail[:300]}[/]",
|
|
238
|
+
),
|
|
239
|
+
"not_found": (
|
|
240
|
+
"Model not found",
|
|
241
|
+
f"[bold]{model}[/] is not available for this key.\n"
|
|
242
|
+
f"[{MUTED}]Type /model to choose one from the list.[/]",
|
|
243
|
+
),
|
|
244
|
+
"network": (
|
|
245
|
+
"Connection problem",
|
|
246
|
+
"Could not reach the model service. Check your connection.\n"
|
|
247
|
+
f"[{MUTED}]{detail[:300]}[/]",
|
|
248
|
+
),
|
|
249
|
+
}
|
|
250
|
+
title, body = messages.get(
|
|
251
|
+
e.kind, (f"API error {e.code or ''}".strip(), f"[{MUTED}]{detail[:500]}[/]")
|
|
252
|
+
)
|
|
253
|
+
notice(title, body, ERR)
|
|
254
|
+
return None
|
|
255
|
+
|
|
256
|
+
# ---- screens -------------------------------------------------------------------
|
|
257
|
+
|
|
258
|
+
def check_connection(self) -> tuple[str, str]:
|
|
259
|
+
try:
|
|
260
|
+
with console.status(f"[{MUTED}]connecting...[/]", spinner="dots"):
|
|
261
|
+
self.provider.check_model(self.agent.model)
|
|
262
|
+
return "connected", OK
|
|
263
|
+
except ProviderError as e:
|
|
264
|
+
return {
|
|
265
|
+
"not_found": "model not found - type /model",
|
|
266
|
+
"auth": "invalid API key",
|
|
267
|
+
"permission": "permission denied for this key",
|
|
268
|
+
"network": "offline",
|
|
269
|
+
}.get(e.kind, f"error {e.code or ''}".strip()), ERR
|
|
270
|
+
|
|
271
|
+
def welcome(self) -> None:
|
|
272
|
+
console.clear()
|
|
273
|
+
logo = Text()
|
|
274
|
+
for i, line in enumerate(LOGO.strip("\n").splitlines()):
|
|
275
|
+
logo.append(line + "\n", style=f"bold {GRADIENT[i % len(GRADIENT)]}")
|
|
276
|
+
status, style = self.check_connection()
|
|
277
|
+
info = Table.grid(padding=(0, 2))
|
|
278
|
+
info.add_column(style=MUTED, justify="right")
|
|
279
|
+
info.add_column()
|
|
280
|
+
info.add_row("version", __version__)
|
|
281
|
+
info.add_row("model", Text(self.agent.model, style=f"bold {ACCENT}"))
|
|
282
|
+
info.add_row("workspace", short_home(self.workdir))
|
|
283
|
+
info.add_row(
|
|
284
|
+
"permissions",
|
|
285
|
+
self.policy.mode
|
|
286
|
+
+ (" (set by your organization)" if self.config.is_locked("permissions.mode") else ""),
|
|
287
|
+
)
|
|
288
|
+
info.add_row("status", Text(status, style=style))
|
|
289
|
+
tips = Text.assemble(
|
|
290
|
+
("/help", ACCENT),
|
|
291
|
+
(" commands ", MUTED),
|
|
292
|
+
("/model", ACCENT),
|
|
293
|
+
(" switch model ", MUTED),
|
|
294
|
+
("/status", ACCENT),
|
|
295
|
+
(" session info ", MUTED),
|
|
296
|
+
("/exit", ACCENT),
|
|
297
|
+
(" quit", MUTED),
|
|
298
|
+
)
|
|
299
|
+
console.print(logo)
|
|
300
|
+
console.print(
|
|
301
|
+
Panel(
|
|
302
|
+
Group(info, Text(""), tips),
|
|
303
|
+
title="[bold]saicode[/]",
|
|
304
|
+
title_align="left",
|
|
305
|
+
subtitle=f"[{MUTED}]terminal coding agent[/]",
|
|
306
|
+
subtitle_align="right",
|
|
307
|
+
border_style=ACCENT,
|
|
308
|
+
padding=(1, 2),
|
|
309
|
+
)
|
|
310
|
+
)
|
|
311
|
+
if any(layer.name == "system" for layer in self.config.layers):
|
|
312
|
+
console.print(
|
|
313
|
+
Text(" Your organization's policy is active. Type /config to see it.", style=MUTED)
|
|
314
|
+
)
|
|
315
|
+
console.print()
|
|
316
|
+
|
|
317
|
+
def show_help(self) -> None:
|
|
318
|
+
table = Table(show_header=False, box=None, padding=(0, 2))
|
|
319
|
+
table.add_column(style=ACCENT)
|
|
320
|
+
table.add_column()
|
|
321
|
+
for cmd, desc in [
|
|
322
|
+
("/help", "show this help"),
|
|
323
|
+
("/model", "choose a model from the list"),
|
|
324
|
+
("/model <name>", "switch to a model by name"),
|
|
325
|
+
("/models", "list available models"),
|
|
326
|
+
("/status", "session, log file, key and policy"),
|
|
327
|
+
("/config", "effective settings and where each comes from"),
|
|
328
|
+
("/clear", "start a new conversation"),
|
|
329
|
+
("/exit", "quit saicode"),
|
|
330
|
+
]:
|
|
331
|
+
table.add_row(cmd, desc)
|
|
332
|
+
console.print(
|
|
333
|
+
Panel(
|
|
334
|
+
table,
|
|
335
|
+
title="[bold]commands[/]",
|
|
336
|
+
title_align="left",
|
|
337
|
+
border_style=MUTED,
|
|
338
|
+
padding=(1, 2),
|
|
339
|
+
)
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
def show_status(self) -> None:
|
|
343
|
+
table = Table.grid(padding=(0, 2))
|
|
344
|
+
table.add_column(style=MUTED, justify="right")
|
|
345
|
+
table.add_column()
|
|
346
|
+
table.add_row("session", self.bus.session_id)
|
|
347
|
+
table.add_row("user", self.bus.user)
|
|
348
|
+
table.add_row("provider", self.provider.name)
|
|
349
|
+
table.add_row("model", self.agent.model)
|
|
350
|
+
table.add_row("api key", self.key_source)
|
|
351
|
+
table.add_row("permissions", self.policy.mode)
|
|
352
|
+
table.add_row("commands run", f"{self.runner_name} (sandbox arrives in phase A2)")
|
|
353
|
+
table.add_row("session log", short_home(self.log.path) if self.log else "off")
|
|
354
|
+
table.add_row(
|
|
355
|
+
"config layers",
|
|
356
|
+
", ".join(
|
|
357
|
+
layer.name + (f" ({short_home(layer.path)})" if layer.path else "")
|
|
358
|
+
for layer in self.config.layers
|
|
359
|
+
),
|
|
360
|
+
)
|
|
361
|
+
console.print(
|
|
362
|
+
Panel(
|
|
363
|
+
table,
|
|
364
|
+
title="[bold]status[/]",
|
|
365
|
+
title_align="left",
|
|
366
|
+
border_style=MUTED,
|
|
367
|
+
padding=(1, 2),
|
|
368
|
+
)
|
|
369
|
+
)
|
|
370
|
+
|
|
371
|
+
def show_config(self) -> None:
|
|
372
|
+
table = Table(box=None, padding=(0, 2), header_style=MUTED)
|
|
373
|
+
table.add_column("setting", style=ACCENT)
|
|
374
|
+
table.add_column("value")
|
|
375
|
+
table.add_column("from", style=MUTED)
|
|
376
|
+
for key, value, source in self.config.flat():
|
|
377
|
+
shown = ", ".join(map(str, value)) if isinstance(value, list) else str(value)
|
|
378
|
+
lock = " (locked)" if self.config.is_locked(key) else ""
|
|
379
|
+
table.add_row(key, shown or "-", source + lock)
|
|
380
|
+
console.print(
|
|
381
|
+
Panel(
|
|
382
|
+
table,
|
|
383
|
+
title="[bold]config[/]",
|
|
384
|
+
title_align="left",
|
|
385
|
+
border_style=MUTED,
|
|
386
|
+
padding=(1, 1),
|
|
387
|
+
)
|
|
388
|
+
)
|
|
389
|
+
|
|
390
|
+
# ---- commands ------------------------------------------------------------------
|
|
391
|
+
|
|
392
|
+
def models(self) -> list[str]:
|
|
393
|
+
with console.status(f"[{MUTED}]loading models...[/]", spinner="dots"):
|
|
394
|
+
return self.provider.list_models()
|
|
395
|
+
|
|
396
|
+
def command(self, line: str) -> bool:
|
|
397
|
+
cmd, _, arg = line.partition(" ")
|
|
398
|
+
cmd, arg = cmd.lower(), arg.strip()
|
|
399
|
+
if cmd in ("/exit", "/quit"):
|
|
400
|
+
return False
|
|
401
|
+
try:
|
|
402
|
+
if cmd == "/help":
|
|
403
|
+
self.show_help()
|
|
404
|
+
elif cmd == "/status":
|
|
405
|
+
self.show_status()
|
|
406
|
+
elif cmd == "/config":
|
|
407
|
+
self.show_config()
|
|
408
|
+
elif cmd == "/clear":
|
|
409
|
+
self.agent.reset()
|
|
410
|
+
elif cmd == "/models":
|
|
411
|
+
for name in self.models():
|
|
412
|
+
marker = "●" if name == self.agent.model else " "
|
|
413
|
+
console.print(Text.assemble((f" {marker} ", ACCENT), name))
|
|
414
|
+
elif cmd == "/model":
|
|
415
|
+
chosen = arg or self.pick(self.models(), "Choose a model")
|
|
416
|
+
if chosen:
|
|
417
|
+
self.agent.switch_model(chosen)
|
|
418
|
+
else:
|
|
419
|
+
console.print(Text(f" unknown command {cmd} - type /help", style=WARN))
|
|
420
|
+
except ProviderError as e:
|
|
421
|
+
self.handle_error(e)
|
|
422
|
+
return True
|
|
423
|
+
|
|
424
|
+
def run(self) -> None:
|
|
425
|
+
self.welcome()
|
|
426
|
+
history = Path.home() / ".saicode" / "history"
|
|
427
|
+
history.parent.mkdir(parents=True, exist_ok=True)
|
|
428
|
+
session: PromptSession[str] = PromptSession(history=FileHistory(str(history)))
|
|
429
|
+
|
|
430
|
+
def toolbar():
|
|
431
|
+
return [
|
|
432
|
+
(
|
|
433
|
+
"class:bottom-toolbar",
|
|
434
|
+
f" {self.agent.model} · {short_home(self.workdir)}"
|
|
435
|
+
f" · {self.policy.mode} · /help ",
|
|
436
|
+
)
|
|
437
|
+
]
|
|
438
|
+
|
|
439
|
+
while True:
|
|
440
|
+
try:
|
|
441
|
+
line = session.prompt([("bold", "› ")], bottom_toolbar=toolbar).strip()
|
|
442
|
+
except KeyboardInterrupt:
|
|
443
|
+
continue
|
|
444
|
+
except EOFError:
|
|
445
|
+
break
|
|
446
|
+
if not line:
|
|
447
|
+
continue
|
|
448
|
+
if line.lower() in ("exit", "quit"):
|
|
449
|
+
break
|
|
450
|
+
if line.startswith("/"):
|
|
451
|
+
if not self.command(line):
|
|
452
|
+
break
|
|
453
|
+
continue
|
|
454
|
+
console.print(Rule(style="grey23"))
|
|
455
|
+
self.agent.run_turn(line)
|
|
456
|
+
console.print()
|
|
457
|
+
console.print(Text(" goodbye", style=MUTED))
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: saicode
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: A terminal coding agent: small core, safe by default, enterprise-ready foundations.
|
|
5
|
+
Project-URL: Homepage, https://github.com/the-schoolofai/saicode
|
|
6
|
+
Project-URL: Repository, https://github.com/the-schoolofai/saicode
|
|
7
|
+
Project-URL: Issues, https://github.com/the-schoolofai/saicode/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/the-schoolofai/saicode/blob/main/CHANGELOG.md
|
|
9
|
+
Project-URL: Security, https://github.com/the-schoolofai/saicode/security/policy
|
|
10
|
+
Author: SAI Code Team
|
|
11
|
+
Maintainer: Muhammad Rizwan
|
|
12
|
+
License-Expression: Apache-2.0
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
License-File: NOTICE
|
|
15
|
+
Keywords: agent,ai,cli,coding-agent,developer-tools,gemini,llm,terminal
|
|
16
|
+
Classifier: Development Status :: 3 - Alpha
|
|
17
|
+
Classifier: Environment :: Console
|
|
18
|
+
Classifier: Intended Audience :: Developers
|
|
19
|
+
Classifier: Operating System :: MacOS
|
|
20
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
21
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
22
|
+
Classifier: Programming Language :: Python :: 3
|
|
23
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
27
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
28
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
29
|
+
Classifier: Topic :: Software Development
|
|
30
|
+
Requires-Python: >=3.11
|
|
31
|
+
Requires-Dist: google-genai<4,>=1.50
|
|
32
|
+
Requires-Dist: keyring<27,>=25
|
|
33
|
+
Requires-Dist: prompt-toolkit<4,>=3.0.40
|
|
34
|
+
Requires-Dist: rich<15,>=13.7
|
|
35
|
+
Provides-Extra: dev
|
|
36
|
+
Requires-Dist: mypy>=2.4; extra == 'dev'
|
|
37
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
38
|
+
Requires-Dist: ruff>=0.17; extra == 'dev'
|
|
39
|
+
Description-Content-Type: text/markdown
|
|
40
|
+
|
|
41
|
+
# SAI Code
|
|
42
|
+
|
|
43
|
+
A terminal coding agent: small core, safe by default, built on enterprise-ready foundations.
|
|
44
|
+
|
|
45
|
+
Version 0.3.0 completes roadmap phase **A1 (Foundation)**.
|
|
46
|
+
|
|
47
|
+
> **Safety note.** saicode reads files, edits files and runs shell commands in your project.
|
|
48
|
+
> It asks before every change and command, but in 0.3.x approved commands run **without an
|
|
49
|
+
> OS sandbox**, with your own permissions. Review each command before you approve it.
|
|
50
|
+
|
|
51
|
+
## Install
|
|
52
|
+
|
|
53
|
+
Requires Python 3.11 or newer on macOS, Linux or Windows.
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
uv tool install saicode # recommended
|
|
57
|
+
# or
|
|
58
|
+
pipx install saicode
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Upgrade with `uv tool upgrade saicode` (or `pipx upgrade saicode`).
|
|
62
|
+
|
|
63
|
+
### From source
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
git clone https://github.com/the-schoolofai/saicode.git
|
|
67
|
+
cd saicode
|
|
68
|
+
uv tool install .
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Store your API key (once)
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
saicode login
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The key goes into the system keychain (macOS Keychain). You can also use the
|
|
78
|
+
`GEMINI_API_KEY` environment variable; it takes priority over the keychain.
|
|
79
|
+
|
|
80
|
+
## Use
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
cd ~/your-project
|
|
84
|
+
saicode
|
|
85
|
+
saicode --model gemini-3-flash-preview
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Inside saicode: `/help`, `/model`, `/models`, `/status`, `/config`, `/clear`, `/exit`.
|
|
89
|
+
|
|
90
|
+
Other commands:
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
saicode config # effective settings and where each one comes from
|
|
94
|
+
saicode logout # remove the stored key
|
|
95
|
+
saicode --version
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Configuration layers
|
|
99
|
+
|
|
100
|
+
Later layers win. The system policy always wins and cannot be overridden.
|
|
101
|
+
|
|
102
|
+
| Layer | File |
|
|
103
|
+
| ------- | ---- |
|
|
104
|
+
| default | built in |
|
|
105
|
+
| user | `~/.saicode/config.toml` |
|
|
106
|
+
| project | `<project>/.saicode/config.toml` |
|
|
107
|
+
| system | macOS `/Library/Application Support/saicode/policy.toml`, Linux `/etc/saicode/policy.toml`, Windows `%ProgramData%\saicode\policy.toml` |
|
|
108
|
+
|
|
109
|
+
Rules that make the layers safe:
|
|
110
|
+
|
|
111
|
+
- `permissions.deny` and `permissions.deny_read` are combined across all layers. No layer can remove a rule.
|
|
112
|
+
- A project config comes from the repository, so its `permissions.allow`, `permissions.mode` and
|
|
113
|
+
`permissions.allow_always` are ignored unless your user config sets `trust_project_permissions = true`
|
|
114
|
+
(and the system policy does not forbid it).
|
|
115
|
+
- A project config can never change `logging.*`, `identity.email` or `providers.allowed`, so a
|
|
116
|
+
cloned repository cannot switch off or redirect your session log.
|
|
117
|
+
|
|
118
|
+
Example user config:
|
|
119
|
+
|
|
120
|
+
```toml
|
|
121
|
+
[model]
|
|
122
|
+
name = "gemini-3-flash-preview"
|
|
123
|
+
|
|
124
|
+
[permissions]
|
|
125
|
+
allow = ["run:git status*", "run:pytest*"]
|
|
126
|
+
|
|
127
|
+
[identity]
|
|
128
|
+
email = "you@example.com"
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Example system policy (set by an administrator):
|
|
132
|
+
|
|
133
|
+
```toml
|
|
134
|
+
[providers]
|
|
135
|
+
allowed = ["gemini"]
|
|
136
|
+
|
|
137
|
+
[permissions]
|
|
138
|
+
mode = "ask" # or "read-only"
|
|
139
|
+
allow_always = false # users must approve every change
|
|
140
|
+
deny = ["run:git push*"]
|
|
141
|
+
|
|
142
|
+
[logging]
|
|
143
|
+
sessions = true
|
|
144
|
+
include_content = false
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Permissions
|
|
148
|
+
|
|
149
|
+
| Action | Default |
|
|
150
|
+
| ------ | ------- |
|
|
151
|
+
| list and read files | allowed |
|
|
152
|
+
| protected files (`.env`, keys, `.ssh`, `secrets/`, ...) | always blocked |
|
|
153
|
+
| anything outside the project folder | always blocked |
|
|
154
|
+
| write or edit files | asks, with a diff |
|
|
155
|
+
| run commands | asks; dangerous patterns blocked |
|
|
156
|
+
|
|
157
|
+
Rules use the form `run:<command glob>`, `write:<path glob>`, `read:<path glob>`.
|
|
158
|
+
|
|
159
|
+
## Session logs
|
|
160
|
+
|
|
161
|
+
Every session is written to `~/.saicode/sessions/<date>/<session id>.jsonl`, one event per line,
|
|
162
|
+
each with a session ID, turn ID, event ID, user and timestamp. File contents and command output are
|
|
163
|
+
left out unless `logging.include_content = true`.
|
|
164
|
+
|
|
165
|
+
## Project layout
|
|
166
|
+
|
|
167
|
+
```
|
|
168
|
+
src/saicode/
|
|
169
|
+
ai/ providers behind one interface (Gemini today), provider allowlist
|
|
170
|
+
core/ config layers, policy engine, events and session log, credentials, agent loop
|
|
171
|
+
tools/ list, read, write, edit, run - each checks the policy first
|
|
172
|
+
sandbox/ command runner interface (OS sandbox arrives in phase A2)
|
|
173
|
+
tui/ the terminal screen; the only package that draws
|
|
174
|
+
cli/ the `saicode` command
|
|
175
|
+
tests/ pytest suite
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
The core never imports the UI. Editor integrations (ACP, VS Code) and headless mode will reuse the same core.
|
|
179
|
+
|
|
180
|
+
## Development
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
uv sync --extra dev # install saicode and the dev tools
|
|
184
|
+
uv run pytest # tests
|
|
185
|
+
uv run ruff check # lint
|
|
186
|
+
uv run ruff format # format
|
|
187
|
+
uv run mypy # type check
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
CI runs all of these on every push and pull request: Python 3.11 to 3.14 on Linux,
|
|
191
|
+
and Python 3.11 and 3.14 on macOS and Windows. A pull request should keep every check green.
|
|
192
|
+
|
|
193
|
+
Maintainers: the release process is described in [RELEASING.md](https://github.com/the-schoolofai/saicode/blob/main/RELEASING.md).
|
|
194
|
+
|
|
195
|
+
## Known limits in 0.3.0
|
|
196
|
+
|
|
197
|
+
- Commands run without an OS sandbox (phase A2). Set `sandbox.required = true` to refuse commands until then.
|
|
198
|
+
- Blocking protected files inside shell commands is best-effort text matching until the sandbox ships.
|
|
199
|
+
- Gemini is the only provider (phase A3 adds more).
|
|
200
|
+
|
|
201
|
+
## Security
|
|
202
|
+
|
|
203
|
+
Please report vulnerabilities privately. See [SECURITY.md](https://github.com/the-schoolofai/saicode/blob/main/SECURITY.md).
|
|
204
|
+
|
|
205
|
+
## Changelog
|
|
206
|
+
|
|
207
|
+
See [CHANGELOG.md](https://github.com/the-schoolofai/saicode/blob/main/CHANGELOG.md).
|
|
208
|
+
|
|
209
|
+
## License
|
|
210
|
+
|
|
211
|
+
Licensed under the [Apache License, Version 2.0](https://github.com/the-schoolofai/saicode/blob/main/LICENSE). Copyright 2026 SAI Code Team.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
saicode/__init__.py,sha256=m_PUj00y9V-PHfUbqjsFv-uK9Iwj6vO4JVorso3iVNI,64
|
|
2
|
+
saicode/__main__.py,sha256=s6rFK3dq4EovS2vdMVKxwAC3Rd5asCbQUqwfEuuf2EQ,73
|
|
3
|
+
saicode/ai/__init__.py,sha256=WnsDBQVxlkcBEOJO2P5D1N8zKcOhCyWUM8ItcgeFeII,1215
|
|
4
|
+
saicode/ai/base.py,sha256=oeW0bhB86G_5Rfr5qNZ9-mZ6X3MfAMtaK685W0A9I6Q,1868
|
|
5
|
+
saicode/ai/gemini.py,sha256=fDSec6c4XljgT42YUjT8BH-PJ9fveSW4fumcJJb4qIA,4759
|
|
6
|
+
saicode/cli/__init__.py,sha256=neoW6CnvZje8ww_QVVnbj7R21KmCyRtirFATauz9zmA,33
|
|
7
|
+
saicode/cli/main.py,sha256=rB_z6gbptyvMPpKikW3V3swoU2BAQcoKIPPkEtftiFk,6024
|
|
8
|
+
saicode/core/__init__.py,sha256=JvDrVXhnAr-IJ0LqfG1WmNB9kbWdbvtlzf412DnMW9E,133
|
|
9
|
+
saicode/core/agent.py,sha256=SOZcOY9SDjGUqtRefUHbIEipYABnkxHslH_0xNLflMc,6388
|
|
10
|
+
saicode/core/config.py,sha256=FdHw1vuxmhnr_533jiqFODRSttYAaSjq0FQvQD678GM,8262
|
|
11
|
+
saicode/core/credentials.py,sha256=Y3AurVNrYxmyPxC4AkGHgzmYsHhdPCc7uPELnEuzeGQ,1468
|
|
12
|
+
saicode/core/events.py,sha256=pK_du3ZitM4VexfQUYaM6-sIOo3nK2yYk9rcEoCIMVk,3414
|
|
13
|
+
saicode/core/policy.py,sha256=mU7lJF19YRNKhtAnNo9a4gdCz8AflDx0GrZuvFMJTxM,6293
|
|
14
|
+
saicode/sandbox/__init__.py,sha256=jFUgzcRyxm3SWoJLNtTid9XhbUEA8Fhdwf0K7Ib4uNg,1596
|
|
15
|
+
saicode/tools/__init__.py,sha256=Bct7y1yuHIjhCyq_cQQM1zIMYUGQJ0wYBUnfthFVhcg,10818
|
|
16
|
+
saicode/tui/__init__.py,sha256=9uCsv-CgV7uSBHvFAVFqLJxn-Rjv397GkBX3z4110J0,74
|
|
17
|
+
saicode/tui/app.py,sha256=hfUHqJx-q014ziINxYxB_71F72f1xvYjqZ2C0ycXcrg,17635
|
|
18
|
+
saicode-0.3.0.dist-info/METADATA,sha256=A2-O6FGM4uWqQ67nQdVyCYCs8ShgEOgqlRNm-YS5Sd0,6982
|
|
19
|
+
saicode-0.3.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
20
|
+
saicode-0.3.0.dist-info/entry_points.txt,sha256=C0WR-11sVHDYFjYaeF_GiuB8QMPiqVmCk9CNTkDNh44,50
|
|
21
|
+
saicode-0.3.0.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
22
|
+
saicode-0.3.0.dist-info/licenses/NOTICE,sha256=iKFKMg_zlvgcLjfwMjO7wAZSmO4esFMOgTIszHogCnk,151
|
|
23
|
+
saicode-0.3.0.dist-info/RECORD,,
|