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80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
"""Python facade over the Rust smolcode agent engine (smolcode_core)."""
from __future__ import annotations

import asyncio
import json
import os
import tempfile
from collections.abc import Awaitable, Callable
from dataclasses import dataclass, field
from typing import Any

from .trace_collector import TraceCollector, TraceEvent

try:
    import smolcode_core as _rust
except ImportError:
    _rust = None  # type: ignore


def rust_available() -> bool:
    return _rust is not None


ApprovalHandler = Callable[[str], Awaitable[bool]]


@dataclass
class RustRunResult:
    final: str
    hit_max_steps: bool = False
    errored: bool = False


class RustSession:
    """Thin wrapper around smolcode_core.Session."""

    def __init__(
        self,
        *,
        workspace: str | None = None,
        agent: str = "build",
        yolo: bool = False,
        model: str | None = None,
        base_url: str | None = None,
        api_key: str | None = None,
        profile: str = "full",
        approval_handler: ApprovalHandler | None = None,
    ) -> None:
        if _rust is None:
            raise RuntimeError(
                "smolcode_core is not installed; build with "
                "`maturin develop --release` in smolcode-cli/crates/smolcode-py"
            )
        if workspace is None:
            workspace = os.environ.get(
                "SMALLCODE_WORKSPACE",
                tempfile.mkdtemp(prefix="smolcode-"),
            )
        self._session = _rust.Session(
            workspace=workspace,
            agent=agent,
            yolo=yolo,
            model=model,
            base_url=base_url,
            api_key=api_key,
            profile=profile,
        )
        self.trace_collector = TraceCollector()
        self.approval_handler = approval_handler
        self.hit_max_steps = False
        self.errored = False
        self._steps: list[dict[str, Any]] = []
        self._final: str = ""
        self._cancelled = False

    def request_cancel(self) -> None:
        self._cancelled = True
        self.cancel_turn()

    @property
    def cancelled(self) -> bool:
        return self._cancelled

    def clear_cancel(self) -> None:
        self._cancelled = False

    @property
    def session_id(self) -> str:
        return self._session.session_id

    @property
    def workspace_path(self) -> str:
        return self._session.workspace()

    def set_model(self, model: str) -> None:
        self._session.set_model(model)

    def set_agent(self, agent: str) -> None:
        self._session.set_agent(agent)

    def set_think(self, level: str) -> None:
        self._session.set_think(level)

    def register_tool(self, name: str, fn: Callable[[dict], dict]) -> None:
        self._session.register_tool(name, fn)

    def files(self) -> dict[str, str]:
        out: dict[str, str] = {}
        for path in self._session.workspace_files():
            content = self._session.read_file(path)
            if content is not None:
                out[path] = content
        return out

    def run_shell(self, command: str) -> str:
        return self._session.run_shell(command)

    async def run(
        self,
        task: str,
        *,
        think: str | None = None,
        yolo: bool | None = None,
    ) -> RustRunResult:
        """Run one agent turn to completion."""
        self.hit_max_steps = False
        self.errored = False
        self._final = ""
        self.clear_cancel()
        self._session.start_turn(task, think=think, yolo=yolo)
        final_text = ""
        while True:
            if self._cancelled:
                break
            ev = await asyncio.to_thread(self._session.poll_event)
            if ev is None:
                await asyncio.sleep(0.05)
                continue
            kind = ev.get("kind")
            if kind == "approval":
                approved = True
                if self.approval_handler is not None:
                    approved = await self.approval_handler(ev.get("desc", ""))
                elif not (yolo if yolo is not None else False):
                    approved = False
                self._session.approve(approved)
                continue
            self._ingest_event(ev)
            if kind == "final":
                final_text = ev.get("text", "")
            if kind == "done":
                break
            if kind == "error":
                self.errored = True
        self._final = final_text
        if "step" in self._final.lower() and "without finishing" in self._final.lower():
            self.hit_max_steps = True
        self._session.record_turn(task, final_text)
        return RustRunResult(
            final=final_text,
            hit_max_steps=self.hit_max_steps,
            errored=self.errored,
        )

    async def poll_events_once(self) -> list[dict[str, Any]]:
        """Non-blocking poll for live UI updates during a turn."""
        events: list[dict[str, Any]] = []
        while True:
            ev = await asyncio.to_thread(self._session.poll_event)
            if ev is None:
                break
            kind = ev.get("kind")
            if kind == "approval":
                approved = True
                if self.approval_handler is not None:
                    approved = await self.approval_handler(ev.get("desc", ""))
                self._session.approve(approved)
                continue
            self._ingest_event(ev)
            events.append(ev)
            if kind in ("done",):
                break
        return events

    def _ingest_event(self, ev: dict[str, Any]) -> None:
        kind = ev.get("kind")
        if kind == "tool_call":
            args_raw = ev.get("args", "{}")
            try:
                args = json.loads(args_raw) if isinstance(args_raw, str) else args_raw
            except json.JSONDecodeError:
                args = {"raw": args_raw}
            self.trace_collector.record_tool_call(ev.get("name", ""), args)
        elif kind == "tool_result":
            text = ev.get("text", "")
            try:
                result = json.loads(text)
            except json.JSONDecodeError:
                result = {"output": text}
            self.trace_collector.record_tool_result(ev.get("name", ""), result)
        elif kind == "final":
            self.trace_collector.record_final(ev.get("text", ""))
        elif kind == "error":
            self.trace_collector.record_error(ev.get("text", ""))
            self.errored = True

    def save(self) -> None:
        self._session.save()

    @staticmethod
    def list_sessions() -> list[dict[str, Any]]:
        if _rust is None:
            return []
        return _rust.Session.list_sessions()

    def load_session(self, session_id: str) -> bool:
        return self._session.load_session(session_id)

    def fork(self) -> str | None:
        return self._session.fork()

    def rename(self, title: str) -> bool:
        return self._session.rename(title)

    def delete(self) -> bool:
        return self._session.delete()

    def cancel_turn(self) -> None:
        self._session.cancel_turn()

    def render_config(self) -> str:
        return self._session.render_config()


def render_config(session: RustSession) -> str:
    return session.render_config()


def apply_settings(session: RustSession, settings: Any) -> None:
    """Apply UI settings to a live Rust session before each agent turn.

    The "auto" / "auto:<size>" pseudo-selections are NOT real model tags — the Router
    picks the model and sets it on the session (see router.run_live), so we must not
    push them via set_model. Only concrete pins are applied here.
    """
    session.set_think(settings.think)
    model = settings.model or ""
    if model and model != "auto" and not model.startswith("auto:"):
        session.set_model(model)
    session.set_agent(settings.agent)


def list_commands(workspace: str) -> list[str]:
    if _rust is None:
        return []
    return _rust.list_commands(workspace)


def expand_command(workspace: str, name: str, args: str = "") -> str | None:
    if _rust is None:
        return None
    return _rust.expand_command(workspace, name, args)


def list_rules(workspace: str) -> list[dict[str, Any]]:
    if _rust is None:
        return []
    return _rust.list_rules(workspace)


def list_skills(workspace: str) -> list[dict[str, Any]]:
    if _rust is None:
        return []
    return _rust.list_skills(workspace)


def expand_skill(workspace: str, name: str, args: str = "") -> str | None:
    if _rust is None:
        return None
    return _rust.expand_skill(workspace, name, args)


def list_mcp(session: RustSession) -> list[dict[str, Any]]:
    return session._session.list_mcp()


def list_background_jobs() -> str:
    if _rust is None:
        return ""
    return _rust.list_background_jobs()


def write_agents_md(workspace: str) -> str:
    if _rust is None:
        raise RuntimeError("smolcode_core not installed")
    return _rust.write_agents_md(workspace)


def git_status(workspace: str) -> str:
    if _rust is None:
        return ""
    return _rust.git_status(workspace)


def workspace_tree(workspace: str, depth: int = 3) -> str:
    if _rust is None:
        return ""
    return _rust.workspace_tree(workspace, depth=depth)


UI_FILE_LIMIT = 1500
AUTOCOMPLETE_FILE_LIMIT = 200


ATTACH_FILE_MAX_BYTES = 8192


def read_workspace_file(
    workspace: str,
    path: str,
    *,
    max_bytes: int = ATTACH_FILE_MAX_BYTES,
    rust: RustSession | None = None,
) -> str | None:
    """Read a workspace file for @-attachment inlining. Returns None if missing."""
    if _rust is None:
        return None
    try:
        session = rust if rust is not None else RustSession(workspace=workspace, yolo=True)
        content = session._session.read_file(path)
        if content is None:
            return None
        if len(content) > max_bytes:
            return content[:max_bytes] + "\n… (truncated)"
        return content
    except Exception:
        return None


def workspace_paths(workspace: str, *, limit: int = UI_FILE_LIMIT) -> tuple[list[str], int]:
    """Workspace paths for UI sidebars (no file reads). Returns (paths, total_count)."""
    if _rust is None:
        return [], 0
    session = RustSession(workspace=workspace, yolo=True)
    paths = sorted(session._session.workspace_files())
    total = len(paths)
    if total > limit:
        paths = paths[:limit]
    return paths, total


def workspace_files(workspace: str) -> dict[str, str]:
    session = RustSession(workspace=workspace, yolo=True)
    return session.files()


def export_transcript(session_id: str, path: str | None = None) -> str:
    if _rust is None:
        raise RuntimeError("smolcode_core not installed")
    return _rust.export_transcript(session_id, path)


def session_timeline(session_id: str) -> list[str]:
    if _rust is None:
        return []
    return _rust.session_timeline(session_id)


def get_session_chat(session_id: str) -> list[dict[str, str]]:
    if _rust is None:
        return []
    return _rust.get_session_chat(session_id)


def chat_from_stored(lines: list[dict[str, str]]) -> list[dict[str, str]]:
    """Convert stored session lines to Gradio chat messages."""
    out: list[dict[str, str]] = []
    for m in lines:
        role = m.get("role", "assistant")
        text = m.get("text", "")
        if role == "user":
            out.append({"role": "user", "content": text})
        else:
            out.append({"role": "assistant", "content": text})
    return out


def session_choices() -> list[str]:
    """Dropdown labels: `title (id)`."""
    return [
        f"{r['title']} ({r['id']})"
        for r in RustSession.list_sessions()
    ]


def parse_session_label(label: str) -> str | None:
    if not label or "(" not in label:
        return None
    return label.rsplit("(", 1)[-1].rstrip(")")


def load_rust_config(
    *,
    model: str | None = None,
    base_url: str | None = None,
    api_key: str | None = None,
    agent: str | None = None,
    yolo: bool = False,
) -> dict[str, Any]:
    """Load layered config.toml via Rust Config."""
    if _rust is None:
        return {}
    cfg = _rust.Config.load(
        model=model,
        base_url=base_url,
        api_key=api_key,
        agent=agent,
        yolo=yolo,
    )
    return {
        "model": cfg.model,
        "base_url": cfg.base_url,
        "agent": cfg.agent,
        "yolo": cfg.yolo,
    }