1、修复 chat_history.txt不存在的问题

This commit is contained in:
dark 2026-06-02 16:57:55 +08:00
parent 0cd43c37a7
commit 6dfb79dcd2
2 changed files with 43 additions and 2 deletions

View File

@ -6,6 +6,8 @@ import time
import json
import shlex
import builtins
import os
import sys
from dataclasses import asdict
from pathlib import Path
from typing import Any, Callable
@ -557,8 +559,16 @@ def _build_prompt_input(input_func: InputFunc) -> InputFunc:
"checkpoint",
"exit",
]
history = None
try:
history_path = Path("runtime") / "chat_history.txt"
history_path.parent.mkdir(parents=True, exist_ok=True)
history = FileHistory(str(history_path))
except OSError:
history = None
session = PromptSession(
history=FileHistory(str(Path("runtime") / "chat_history.txt")),
history=history,
completer=WordCompleter(commands, ignore_case=True, sentence=True),
)
return session.prompt

View File

@ -1,8 +1,11 @@
import builtins
from pathlib import Path
import pytest
from pam_deploy_graph.agent import PamDeployAgent
from pam_deploy_graph.fake_runner import FakeActionRunner
from pam_deploy_graph.interactive import InteractiveCliSession
from pam_deploy_graph.interactive import InteractiveCliSession, _build_prompt_input
PARAMS = {
@ -130,3 +133,31 @@ def test_chat_can_hot_load_mcp_config(tmp_path: Path):
assert session.agent.mcp_runner is not None
assert session.agent.router.mcp_runner is not None
assert any("MCP 配置已加载" in item for item in output)
def test_prompt_history_creates_runtime_dir(tmp_path: Path, monkeypatch):
pytest.importorskip("prompt_toolkit")
monkeypatch.chdir(tmp_path)
prompt = _build_prompt_input(builtins.input)
assert callable(prompt)
assert (tmp_path / "runtime").is_dir()
def test_packaged_chat_uses_plain_input_by_default(monkeypatch):
monkeypatch.setattr("sys.frozen", True, raising=False)
monkeypatch.delenv("PAM_CHAT_PROMPT_TOOLKIT", raising=False)
monkeypatch.delenv("PAM_CHAT_SIMPLE_INPUT", raising=False)
prompt = _build_prompt_input(builtins.input)
assert prompt is builtins.input
def test_simple_input_env_disables_prompt_toolkit(monkeypatch):
monkeypatch.setenv("PAM_CHAT_SIMPLE_INPUT", "1")
prompt = _build_prompt_input(builtins.input)
assert prompt is builtins.input