From 3df6c031f97499997599d9ac771b0d40b8b94609 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Fri, 5 Jun 2026 23:39:31 -0400 Subject: [PATCH] refac --- .../components/chat/AssistantMessage.svelte | 2 +- cptr/routers/chat.py | 29 +++++++++++++++++++ pyproject.toml | 1 + 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte b/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte index 8852a57..f1e3710 100644 --- a/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte +++ b/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte @@ -207,7 +207,7 @@ nameCounts[name] = (nameCounts[name] || 0) + 1; } return Object.entries(nameCounts) - .map(([name, count]) => count > 1 ? `${count}× ${name}` : name) + .map(([name, count]) => count > 1 ? `${name} (${count})` : name) .join(', '); } diff --git a/cptr/routers/chat.py b/cptr/routers/chat.py index 012f50b..e5fe6bf 100644 --- a/cptr/routers/chat.py +++ b/cptr/routers/chat.py @@ -18,6 +18,32 @@ router = APIRouter(prefix="/api/chats", tags=["chats"]) COOKIE_NAME = "cptr_session" +def _ensure_gitignore(workspace: str) -> None: + """If workspace is a git repo, ensure .cptr is listed in .gitignore.""" + ws = Path(workspace) + if not (ws / ".git").exists(): + return + + gitignore = ws / ".gitignore" + entry = ".cptr" + + # Check if already ignored + if gitignore.exists(): + content = gitignore.read_text(encoding="utf-8", errors="replace") + # Match .cptr as a standalone line (not a substring of something else) + for line in content.splitlines(): + stripped = line.strip() + if stripped == entry or stripped == entry + "/": + return + # Append with a preceding newline if file doesn't end with one + if content and not content.endswith("\n"): + content += "\n" + content += f"{entry}\n" + gitignore.write_text(content, encoding="utf-8") + else: + gitignore.write_text(f"{entry}\n", encoding="utf-8") + + def _get_user(request: Request) -> str: """Extract user_id from cookie, raise 401 if not authenticated.""" token = request.cookies.get(COOKIE_NAME) @@ -295,6 +321,9 @@ async def send_message(body: SendMessageRequest, request: Request): chats_dir = Path(body.workspace) / ".cptr" / "chats" chats_dir.mkdir(parents=True, exist_ok=True) + # Auto-add .cptr to .gitignore if this is a git repo + _ensure_gitignore(body.workspace) + # Resolve connection for model connection, bare_model = await _resolve_connection(body.model_id, request.app.state) diff --git a/pyproject.toml b/pyproject.toml index d778364..22e1fdb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,6 +17,7 @@ dependencies = [ "PyJWT>=2.8", "cryptography>=42.0", "python-socketio[asgi]>=5.11", + "pywinpty>=2.0; sys_platform == 'win32'", ] [project.optional-dependencies]