mirror of
https://github.com/NCBM/plyngent.git
synced 2026-11-07 07:17:54 +08:00
154509b929
Typing `@shot.png` in the line attaches that picture to the turn — no separate command — and `@notes.txt` appends the file as text under a `--- @path ---` header, capped like a tool result with a resumable `truncate_token`. `@"my shot.png"` is the quoted form, and Tab completes the path after `@` (directories end in `/`, a name with a space completes quoted). An image is skipped, with a note naming the key to set, on a model without `image_in`; a missing or denylisted path is a note too, and the turn still goes. The default catalogs mark what can take a picture (`deepseek-flash` yes, `deepseek-v4-pro` no, the OpenAI defaults yes). `Agent.run` takes the parts, so the turn is stored with its images and shows up as `[image]` wherever only prose is wanted.
244 lines
7.6 KiB
Python
244 lines
7.6 KiB
Python
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
|
|
import pytest
|
|
import tomlkit
|
|
|
|
from plyngent.cli.readline_setup import (
|
|
build_completer,
|
|
filter_prefix,
|
|
history_path,
|
|
slash_commands,
|
|
)
|
|
from plyngent.cli.state import ReplState
|
|
from plyngent.config.models import ModelConfig, OpenAICompatibleProvider, OpenAIProvider
|
|
from plyngent.config.store import ConfigStore
|
|
|
|
pytestmark = pytest.mark.skipif(
|
|
importlib.util.find_spec("readline") is None,
|
|
reason="readline not available (e.g. Windows/Wine)",
|
|
)
|
|
|
|
|
|
def _minimal_state(tmp_path: object) -> ReplState:
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock
|
|
|
|
assert isinstance(tmp_path, Path)
|
|
provider = OpenAICompatibleProvider(
|
|
access_key_or_token="sk",
|
|
url="https://example.com/v1",
|
|
models={"alpha": ModelConfig(), "beta": ModelConfig()},
|
|
)
|
|
config = ConfigStore(path=tmp_path / "plyngent.toml", document=tomlkit.document())
|
|
config.providers = {
|
|
"local": OpenAIProvider(access_key_or_token="sk"),
|
|
"remote": provider,
|
|
}
|
|
# Avoid real client/network: build ReplState pieces manually via object.__new__
|
|
state = object.__new__(ReplState)
|
|
state.config = config
|
|
state.provider = provider
|
|
state.provider_name = "remote"
|
|
state.model = "alpha"
|
|
state.tools_enabled = True
|
|
state.memory = MagicMock()
|
|
state.workspace = tmp_path
|
|
state.session_id = None
|
|
state.client = MagicMock()
|
|
state.agent = MagicMock()
|
|
return state
|
|
|
|
|
|
def test_filter_prefix() -> None:
|
|
assert filter_prefix("/he", ["/help", "/quit"]) == ["/help"]
|
|
assert filter_prefix("", ["a", "b"]) == ["a", "b"]
|
|
|
|
|
|
def test_history_path_under_user_data() -> None:
|
|
path = history_path()
|
|
assert path.name == "repl_history"
|
|
assert "plyngent" in str(path)
|
|
|
|
|
|
def test_completer_commands(tmp_path: object, monkeypatch: object) -> None:
|
|
import readline
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
assert isinstance(tmp_path, Path)
|
|
assert isinstance(monkeypatch, pytest.MonkeyPatch)
|
|
|
|
state = _minimal_state(tmp_path)
|
|
completer = build_completer(state)
|
|
monkeypatch.setattr(readline, "get_line_buffer", lambda: "/")
|
|
monkeypatch.setattr(readline, "get_begidx", lambda: 0)
|
|
|
|
found: list[str] = []
|
|
index = 0
|
|
while True:
|
|
item = completer("/", index)
|
|
if item is None:
|
|
break
|
|
found.append(item)
|
|
index += 1
|
|
names = slash_commands()
|
|
assert "/help" in found
|
|
assert "/stream" in names
|
|
assert "/verbose" in names
|
|
assert set(found) <= set(names)
|
|
|
|
|
|
def test_bind_tab_complete_runs() -> None:
|
|
from plyngent.cli.readline_setup import bind_tab_complete
|
|
|
|
class FakeReadline:
|
|
binds: list[str]
|
|
|
|
def __init__(self) -> None:
|
|
self.binds = []
|
|
|
|
def parse_and_bind(self, s: str) -> None:
|
|
self.binds.append(s)
|
|
|
|
fake = FakeReadline()
|
|
bind_tab_complete(fake)
|
|
assert any("complete" in b or "rl_complete" in b for b in fake.binds)
|
|
|
|
|
|
def test_completer_provider_args(tmp_path: object, monkeypatch: object) -> None:
|
|
import readline
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
assert isinstance(tmp_path, Path)
|
|
assert isinstance(monkeypatch, pytest.MonkeyPatch)
|
|
|
|
state = _minimal_state(tmp_path)
|
|
completer = build_completer(state)
|
|
monkeypatch.setattr(readline, "get_line_buffer", lambda: "/provider ")
|
|
monkeypatch.setattr(readline, "get_begidx", lambda: len("/provider "))
|
|
|
|
first = completer("r", 0)
|
|
assert first == "remote"
|
|
|
|
|
|
def test_completer_help_commands(tmp_path: object, monkeypatch: object) -> None:
|
|
import readline
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
assert isinstance(tmp_path, Path)
|
|
assert isinstance(monkeypatch, pytest.MonkeyPatch)
|
|
|
|
state = _minimal_state(tmp_path)
|
|
completer = build_completer(state)
|
|
monkeypatch.setattr(readline, "get_line_buffer", lambda: "/help ")
|
|
monkeypatch.setattr(readline, "get_begidx", lambda: len("/help "))
|
|
|
|
found: list[str] = []
|
|
index = 0
|
|
while True:
|
|
item = completer("c", index)
|
|
if item is None:
|
|
break
|
|
found.append(item)
|
|
index += 1
|
|
assert "compact" in found
|
|
assert "config" in found
|
|
|
|
|
|
def test_completer_stream_on_off(tmp_path: object, monkeypatch: object) -> None:
|
|
import readline
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
assert isinstance(tmp_path, Path)
|
|
assert isinstance(monkeypatch, pytest.MonkeyPatch)
|
|
|
|
state = _minimal_state(tmp_path)
|
|
completer = build_completer(state)
|
|
monkeypatch.setattr(readline, "get_line_buffer", lambda: "/stream ")
|
|
monkeypatch.setattr(readline, "get_begidx", lambda: len("/stream "))
|
|
|
|
assert completer("o", 0) == "on"
|
|
assert completer("o", 1) == "off"
|
|
|
|
|
|
def test_completer_path_mentions(tmp_path: object, monkeypatch: object) -> None:
|
|
"""An ``@path`` token completes files, wherever it sits in the line."""
|
|
import readline
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
assert isinstance(tmp_path, Path)
|
|
assert isinstance(monkeypatch, pytest.MonkeyPatch)
|
|
(tmp_path / "shot.png").write_bytes(b"")
|
|
(tmp_path / "screens").mkdir()
|
|
|
|
state = _minimal_state(tmp_path)
|
|
completer = build_completer(state)
|
|
monkeypatch.setattr(readline, "get_line_buffer", lambda: "look at @sh")
|
|
monkeypatch.setattr(readline, "get_begidx", lambda: len("look at "))
|
|
monkeypatch.chdir(tmp_path)
|
|
|
|
found = [item for index in range(10) if (item := completer("@sh", index)) is not None]
|
|
assert found == ["@shot.png"]
|
|
|
|
|
|
def test_a_plain_word_is_still_a_slash_argument(tmp_path: object, monkeypatch: object) -> None:
|
|
"""The mention branch must not swallow ordinary argument completion."""
|
|
import readline
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
assert isinstance(tmp_path, Path)
|
|
assert isinstance(monkeypatch, pytest.MonkeyPatch)
|
|
|
|
state = _minimal_state(tmp_path)
|
|
completer = build_completer(state)
|
|
monkeypatch.setattr(readline, "get_line_buffer", lambda: "/provider r")
|
|
monkeypatch.setattr(readline, "get_begidx", lambda: len("/provider "))
|
|
|
|
assert completer("r", 0) == "remote"
|
|
|
|
|
|
def test_complete_slash_args_from_registry(tmp_path: object) -> None:
|
|
from pathlib import Path
|
|
|
|
from plyngent.cli.slash import complete_slash_args
|
|
|
|
assert isinstance(tmp_path, Path)
|
|
state = _minimal_state(tmp_path)
|
|
assert complete_slash_args(state, "/provider", "r") == ["remote"]
|
|
assert complete_slash_args(state, "/model", "a") == ["alpha"]
|
|
assert complete_slash_args(state, "/export", "j") == ["json"]
|
|
assert complete_slash_args(state, "/help", "st") == ["status", "stream"]
|
|
assert complete_slash_args(state, "/yolo", "") == ["on", "off", "once"]
|
|
assert complete_slash_args(state, "/yolo", "o") == ["on", "off", "once"]
|
|
assert "push" in complete_slash_args(state, "/todos", "p")
|
|
assert "pop" in complete_slash_args(state, "/todos", "p")
|
|
assert "--persist" in complete_slash_args(state, "/model", "--p")
|
|
assert "--verbose" in complete_slash_args(state, "/history", "--v")
|
|
assert complete_slash_args(state, "/history", "la") == ["last"]
|
|
assert "32" in complete_slash_args(state, "/rounds", "3")
|
|
|
|
from plyngent.agent.todo_stack import TodoStack
|
|
|
|
state.todo_stack = TodoStack()
|
|
_ = state.todo_stack.push_group(["alpha", "beta"])
|
|
numbers = complete_slash_args(state, "/todos", "", prior_args=["done"])
|
|
assert numbers == ["1", "2"]
|
|
assert complete_slash_args(state, "/todos", "2", prior_args=["done"]) == ["2"]
|
|
|
|
state.remember_session_ids([3, 12, 40])
|
|
assert complete_slash_args(state, "/resume", "1") == ["12"]
|
|
assert complete_slash_args(state, "/delete", "4") == ["40"]
|