diff --git a/opencode_bridge/routes.py b/opencode_bridge/routes.py index 0aa8b78..067c7e8 100644 --- a/opencode_bridge/routes.py +++ b/opencode_bridge/routes.py @@ -14,20 +14,14 @@ from .session_manager import SessionManager log = logging.getLogger("opencode_bridge.routes") -MODE_SYSTEM_PROMPTS = { - "optimize": ( - "你是一个代码优化专家。请基于用户提供的代码上下文," - "返回只包含优化后代码的回复,不要任何解释或 markdown 围栏。" - ), - "fix": ( - "你是一个 Python 排错专家。用户给出了一段产生错误的代码和 traceback," - "请返回只包含修复后代码的回复,不要任何解释或 markdown 围栏。" - ), - "edit": ( - "你是一个代码编辑助手。基于用户的指令修改给定代码," - "返回只包含修改后完整代码的回复,不要任何解释或 markdown 围栏。" - ), -} +# Unified system prompt — the LLM (OpenCode) itself judges whether the user's +# natural-language instruction is an optimize / fix / edit request, based on +# the code context and the optional in the parts below. +UNIFIED_SYSTEM_PROMPT = ( + "你是一个代码编辑助手。基于提供的代码上下文(以及 traceback,如果有)," + "按照用户的指令修改代码。返回只包含修改后完整代码的回复," + "不要任何解释或 markdown 围栏。" +) def make_client(handler: APIHandler) -> OpenCodeClient: @@ -52,12 +46,13 @@ def get_session_manager(handler: APIHandler) -> SessionManager: return sm -def _build_request_body(mode: str, prompt: str, context: dict) -> dict: +def _build_request_body(prompt: str, context: dict) -> dict: """Build full request body for OpenCode POST /session/:id/message. - Returns dict with 'parts' (list) and 'system' (str) keys. + Returns dict with 'parts' (list) and 'system' (str) keys. No mode concept: + the LLM interprets the user's natural-language instruction, with the code + context and the optional traceback in front of it. """ - system = MODE_SYSTEM_PROMPTS[mode] parts: list[dict] = [] if context.get("previousCode"): @@ -85,10 +80,10 @@ def _build_request_body(mode: str, prompt: str, context: dict) -> dict: ), }) - if mode == "edit" and prompt: + if prompt: parts.append({"type": "text", "text": "\n%s\n" % prompt}) - return {"parts": parts, "system": system} + return {"parts": parts, "system": UNIFIED_SYSTEM_PROMPT} def _strip_code_fence(s: str) -> str: @@ -157,7 +152,6 @@ class EditHandler(APIHandler): async def post(self): try: body = json.loads(self.request.body) - mode = body["mode"] prompt = body.get("prompt", "") context = body["context"] provider_id = body.get("providerId") or None @@ -177,7 +171,7 @@ class EditHandler(APIHandler): sm = get_session_manager(self) try: sid = await sm.get_or_create(notebook_path) - request_body = _build_request_body(mode, prompt, context) + request_body = _build_request_body(prompt, context) result = await client.send_message_sync( sid, request_body["parts"], @@ -195,7 +189,6 @@ class EditHandler(APIHandler): self.finish(json.dumps({ "ok": True, - "mode": mode, "finalSource": final_source, "sessionId": sid, "notebookPath": notebook_path, diff --git a/opencode_bridge/tests/test_routes.py b/opencode_bridge/tests/test_routes.py index e93d6dc..32b9b39 100644 --- a/opencode_bridge/tests/test_routes.py +++ b/opencode_bridge/tests/test_routes.py @@ -115,7 +115,6 @@ async def test_edit_handler(monkeypatch, jp_fetch): ) body = json.dumps({ - "mode": "edit", "prompt": "Add type hints", "context": { "notebookPath": "test.ipynb",