Skip to content

Commit 0085ec7

Browse files
committed
refac
1 parent 2982fe9 commit 0085ec7

1 file changed

Lines changed: 25 additions & 3 deletions

File tree

open_terminal/main.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,33 @@ def get_system_info() -> str:
6363
)
6464

6565

66+
def _system_prompt_variables() -> dict[str, str]:
67+
return {
68+
"os": platform.system(),
69+
"kernel": platform.release(),
70+
"arch": platform.machine(),
71+
"hostname": socket.gethostname(),
72+
"user": "" if MULTI_USER else os.getenv("USER", "unknown"),
73+
"shell": os.environ.get("SHELL", "/bin/sh"),
74+
"python_version": sys.version.split()[0],
75+
"home": os.path.expanduser("~"),
76+
}
77+
78+
79+
def _expand_system_prompt_template(template: str) -> str:
80+
values = _system_prompt_variables()
81+
82+
def replace(match: re.Match) -> str:
83+
key = match.group(1).strip()
84+
return values.get(key, match.group(0))
85+
86+
return re.sub(r"\{\{\s*([a-zA-Z0-9_]+)\s*\}\}", replace, template)
87+
88+
6689
def get_system_prompt() -> str:
6790
"""Build a default system prompt for LLM integration."""
6891
if SYSTEM_PROMPT:
69-
return SYSTEM_PROMPT
92+
return _expand_system_prompt_template(SYSTEM_PROMPT)
7093

7194
shell = os.environ.get("SHELL", "/bin/sh")
7295
user_part = f" as user '{os.getenv('USER', 'unknown')}'" if not MULTI_USER else ""
@@ -418,7 +441,7 @@ async def get_cwd(
418441
fs: UserFS = Depends(get_filesystem),
419442
):
420443
session_id = http_request.headers.get("x-session-id")
421-
return {"cwd": _get_session_cwd(session_id, fs)}
444+
return {"cwd": _get_session_cwd(session_id, fs), "home": fs.home}
422445

423446

424447
@app.post(
@@ -1783,4 +1806,3 @@ async def _pty_reader():
17831806
from open_terminal.utils.notebooks import create_notebooks_router
17841807

17851808
app.include_router(create_notebooks_router(verify_api_key))
1786-

0 commit comments

Comments
 (0)