Skip to content

Commit e402772

Browse files
authored
fix: create prompt history file when using Agent.to_cli() (#1791)
1 parent 4e65ec4 commit e402772

File tree

3 files changed

+12
-25
lines changed

3 files changed

+12
-25
lines changed

pydantic_ai_slim/pydantic_ai/_cli.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
This folder is used to store the prompt history and configuration.
5454
"""
5555

56-
PROMPT_HISTORY_PATH = PYDANTIC_AI_HOME / 'prompt-history.txt'
56+
PROMPT_HISTORY_FILENAME = 'prompt-history.txt'
5757

5858

5959
class SimpleCodeBlock(CodeBlock):
@@ -211,27 +211,26 @@ def cli(args_list: Sequence[str] | None = None, *, prog_name: str = 'pai') -> in
211211
pass
212212
return 0
213213

214-
# Ensure the history directory and file exist
215-
PROMPT_HISTORY_PATH.parent.mkdir(parents=True, exist_ok=True)
216-
PROMPT_HISTORY_PATH.touch(exist_ok=True)
217-
218-
# doing this instead of `PromptSession[Any](history=` allows mocking of PromptSession in tests
219-
session: PromptSession[Any] = PromptSession(history=FileHistory(str(PROMPT_HISTORY_PATH)))
220214
try:
221-
return asyncio.run(run_chat(session, stream, agent, console, code_theme, prog_name))
215+
return asyncio.run(run_chat(stream, agent, console, code_theme, prog_name))
222216
except KeyboardInterrupt: # pragma: no cover
223217
return 0
224218

225219

226220
async def run_chat(
227-
session: PromptSession[Any],
228221
stream: bool,
229222
agent: Agent[AgentDepsT, OutputDataT],
230223
console: Console,
231224
code_theme: str,
232225
prog_name: str,
226+
config_dir: Path | None = None,
233227
deps: AgentDepsT = None,
234228
) -> int:
229+
prompt_history_path = (config_dir or PYDANTIC_AI_HOME) / PROMPT_HISTORY_FILENAME
230+
prompt_history_path.parent.mkdir(parents=True, exist_ok=True)
231+
prompt_history_path.touch(exist_ok=True)
232+
session: PromptSession[Any] = PromptSession(history=FileHistory(str(prompt_history_path)))
233+
235234
multiline = False
236235
messages: list[ModelMessage] = []
237236

pydantic_ai_slim/pydantic_ai/agent.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,18 +1773,14 @@ async def main():
17731773
await agent.to_cli()
17741774
```
17751775
"""
1776-
from prompt_toolkit import PromptSession
1777-
from prompt_toolkit.history import FileHistory
17781776
from rich.console import Console
17791777

1780-
from pydantic_ai._cli import PROMPT_HISTORY_PATH, run_chat
1778+
from pydantic_ai._cli import run_chat
17811779

17821780
# TODO(Marcelo): We need to refactor the CLI code to be able to be able to just pass `agent`, `deps` and
17831781
# `prog_name` from here.
17841782

1785-
session: PromptSession[Any] = PromptSession(history=FileHistory(str(PROMPT_HISTORY_PATH)))
17861783
await run_chat(
1787-
session=session,
17881784
stream=True,
17891785
agent=self,
17901786
deps=deps,

tests/test_cli.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,35 +224,28 @@ def test_code_theme_unset(mocker: MockerFixture, env: TestEnv):
224224
env.set('OPENAI_API_KEY', 'test')
225225
mock_run_chat = mocker.patch('pydantic_ai._cli.run_chat')
226226
cli([])
227-
mock_run_chat.assert_awaited_once_with(
228-
IsInstance(PromptSession), True, IsInstance(Agent), IsInstance(Console), 'monokai', 'pai'
229-
)
227+
mock_run_chat.assert_awaited_once_with(True, IsInstance(Agent), IsInstance(Console), 'monokai', 'pai')
230228

231229

232230
def test_code_theme_light(mocker: MockerFixture, env: TestEnv):
233231
env.set('OPENAI_API_KEY', 'test')
234232
mock_run_chat = mocker.patch('pydantic_ai._cli.run_chat')
235233
cli(['--code-theme=light'])
236-
mock_run_chat.assert_awaited_once_with(
237-
IsInstance(PromptSession), True, IsInstance(Agent), IsInstance(Console), 'default', 'pai'
238-
)
234+
mock_run_chat.assert_awaited_once_with(True, IsInstance(Agent), IsInstance(Console), 'default', 'pai')
239235

240236

241237
def test_code_theme_dark(mocker: MockerFixture, env: TestEnv):
242238
env.set('OPENAI_API_KEY', 'test')
243239
mock_run_chat = mocker.patch('pydantic_ai._cli.run_chat')
244240
cli(['--code-theme=dark'])
245-
mock_run_chat.assert_awaited_once_with(
246-
IsInstance(PromptSession), True, IsInstance(Agent), IsInstance(Console), 'monokai', 'pai'
247-
)
241+
mock_run_chat.assert_awaited_once_with(True, IsInstance(Agent), IsInstance(Console), 'monokai', 'pai')
248242

249243

250244
def test_agent_to_cli_sync(mocker: MockerFixture, env: TestEnv):
251245
env.set('OPENAI_API_KEY', 'test')
252246
mock_run_chat = mocker.patch('pydantic_ai._cli.run_chat')
253247
cli_agent.to_cli_sync()
254248
mock_run_chat.assert_awaited_once_with(
255-
session=IsInstance(PromptSession),
256249
stream=True,
257250
agent=IsInstance(Agent),
258251
console=IsInstance(Console),
@@ -268,7 +261,6 @@ async def test_agent_to_cli_async(mocker: MockerFixture, env: TestEnv):
268261
mock_run_chat = mocker.patch('pydantic_ai._cli.run_chat')
269262
await cli_agent.to_cli()
270263
mock_run_chat.assert_awaited_once_with(
271-
session=IsInstance(PromptSession),
272264
stream=True,
273265
agent=IsInstance(Agent),
274266
console=IsInstance(Console),

0 commit comments

Comments
 (0)