refactor: drop mode from backend; unify system prompt

This commit is contained in:
tao.chen
2026-07-23 12:31:28 +08:00
parent b5adf331a6
commit 42ea857313
2 changed files with 15 additions and 23 deletions
+15 -22
View File
@@ -14,20 +14,14 @@ from .session_manager import SessionManager
log = logging.getLogger("opencode_bridge.routes") log = logging.getLogger("opencode_bridge.routes")
MODE_SYSTEM_PROMPTS = { # Unified system prompt — the LLM (OpenCode) itself judges whether the user's
"optimize": ( # natural-language instruction is an optimize / fix / edit request, based on
"你是一个代码优化专家。请基于用户提供的代码上下文," # the code context and the optional <traceback> in the parts below.
"返回只包含优化后代码的回复,不要任何解释或 markdown 围栏。" UNIFIED_SYSTEM_PROMPT = (
), "你是一个代码编辑助手。基于提供的代码上下文(以及 traceback,如果有),"
"fix": ( "按照用户的指令修改代码。返回只包含修改后完整代码的回复,"
"你是一个 Python 排错专家。用户给出了一段产生错误的代码和 traceback," "不要任何解释或 markdown 围栏。"
"请返回只包含修复后代码的回复,不要任何解释或 markdown 围栏。" )
),
"edit": (
"你是一个代码编辑助手。基于用户的指令修改给定代码,"
"返回只包含修改后完整代码的回复,不要任何解释或 markdown 围栏。"
),
}
def make_client(handler: APIHandler) -> OpenCodeClient: def make_client(handler: APIHandler) -> OpenCodeClient:
@@ -52,12 +46,13 @@ def get_session_manager(handler: APIHandler) -> SessionManager:
return sm 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. """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] = [] parts: list[dict] = []
if context.get("previousCode"): 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": "<instruction>\n%s\n</instruction>" % prompt}) parts.append({"type": "text", "text": "<instruction>\n%s\n</instruction>" % prompt})
return {"parts": parts, "system": system} return {"parts": parts, "system": UNIFIED_SYSTEM_PROMPT}
def _strip_code_fence(s: str) -> str: def _strip_code_fence(s: str) -> str:
@@ -157,7 +152,6 @@ class EditHandler(APIHandler):
async def post(self): async def post(self):
try: try:
body = json.loads(self.request.body) body = json.loads(self.request.body)
mode = body["mode"]
prompt = body.get("prompt", "") prompt = body.get("prompt", "")
context = body["context"] context = body["context"]
provider_id = body.get("providerId") or None provider_id = body.get("providerId") or None
@@ -177,7 +171,7 @@ class EditHandler(APIHandler):
sm = get_session_manager(self) sm = get_session_manager(self)
try: try:
sid = await sm.get_or_create(notebook_path) 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( result = await client.send_message_sync(
sid, sid,
request_body["parts"], request_body["parts"],
@@ -195,7 +189,6 @@ class EditHandler(APIHandler):
self.finish(json.dumps({ self.finish(json.dumps({
"ok": True, "ok": True,
"mode": mode,
"finalSource": final_source, "finalSource": final_source,
"sessionId": sid, "sessionId": sid,
"notebookPath": notebook_path, "notebookPath": notebook_path,
-1
View File
@@ -115,7 +115,6 @@ async def test_edit_handler(monkeypatch, jp_fetch):
) )
body = json.dumps({ body = json.dumps({
"mode": "edit",
"prompt": "Add type hints", "prompt": "Add type hints",
"context": { "context": {
"notebookPath": "test.ipynb", "notebookPath": "test.ipynb",