zettcode-engine 0.4.3__tar.gz → 0.4.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/PKG-INFO +1 -1
- zettcode_engine-0.4.5/src/zettcode/_compat.py +81 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/config.py +18 -2
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/config_edit.py +18 -8
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/__init__.py +4 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/agent/settings.py +32 -12
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/core.py +76 -21
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/harness_paths.py +16 -0
- zettcode_engine-0.4.5/src/zettcode/engine/plugins/cli.py +315 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/plugins/context.py +73 -10
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/plugins/groups.py +12 -3
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/plugins/llm.py +11 -5
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/plugins/loader.py +20 -4
- zettcode_engine-0.4.5/src/zettcode/engine/provided.py +70 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/paths.py +4 -0
- zettcode_engine-0.4.5/src/zettcode/providers.py +208 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/version.py +1 -1
- zettcode_engine-0.4.3/src/zettcode/_compat.py +0 -35
- zettcode_engine-0.4.3/src/zettcode/engine/plugins/cli.py +0 -133
- zettcode_engine-0.4.3/src/zettcode/providers.py +0 -215
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/.gitignore +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/pyproject.toml +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/budget.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/agent/__init__.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/agent/base.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/agent/context.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/agent/exports.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/agent/interaction.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/agent/sessions.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/agent/turns.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/delivery.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/delivery_runtime.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/dialogue/__init__.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/dialogue/ask.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/dialogue/side.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/dialogue/title.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/export/__init__.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/export/trace/script.js +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/export/trace/style.css +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/export/trace/template.html +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/extensions/__init__.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/extensions/capabilities.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/extensions/compaction.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/extensions/context.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/extensions/files.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/extensions/guidance.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/extensions/harness_switch.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/extensions/mcp_notice.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/extensions/relay.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/extensions/subagents.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/extensions/usage.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/inbox.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/permission/__init__.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/permission/approval.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/permission/readonly.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/permission/sandbox.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/pipeline.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/plugins/__init__.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/plugins/commands.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/plugins/extension.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/plugins/graph.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/plugins/mixins.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/plugins/plugin.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/plugins/services.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/plugins/settings.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/plugins/state.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/plugins/switches.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/plugins/triggers.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/py.typed +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/server_tools.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/service_contracts.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/storage/__init__.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/storage/layout.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/storage/ledger.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/storage/locks.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/storage/metadata.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/storage/records.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/engine/storage/store.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/error_text.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/filelock.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/logging_setup.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/mention.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/modes.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/plugin_tui.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/processes.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/prompt.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/py.typed +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/registry.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/relay.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/statusline_parts.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/wire.py +0 -0
- {zettcode_engine-0.4.3 → zettcode_engine-0.4.5}/src/zettcode/wording.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: zettcode-engine
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.5
|
|
4
4
|
Summary: The agent core of ZettCode: the runtime over zett-agent, and the leaf types it is written on
|
|
5
5
|
Project-URL: Repository, https://github.com/Chang-LeHung/zettcode
|
|
6
6
|
Author: Chang-LeHung
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""Small shims for the language versions this project supports.
|
|
2
|
+
|
|
3
|
+
ZettCode runs on Python 3.10 through 3.15, so the few things the standard
|
|
4
|
+
library grew in between live here instead of behind version checks at each use.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import inspect
|
|
10
|
+
import sys
|
|
11
|
+
import typing
|
|
12
|
+
from enum import Enum
|
|
13
|
+
|
|
14
|
+
if sys.version_info >= (3, 11):
|
|
15
|
+
from builtins import BaseExceptionGroup, ExceptionGroup
|
|
16
|
+
|
|
17
|
+
import tomllib
|
|
18
|
+
else: # pragma: no cover - the branch only the 3.10 runtime takes
|
|
19
|
+
import tomli as tomllib
|
|
20
|
+
from exceptiongroup import BaseExceptionGroup, ExceptionGroup
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
__all__ = ["BaseExceptionGroup", "ExceptionGroup", "StrEnum", "tomllib"]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class StrEnum(str, Enum):
|
|
27
|
+
"""A string-valued enum, for the versions before ``enum.StrEnum``.
|
|
28
|
+
|
|
29
|
+
The standard library's version formats as its value, so ``f"{member}"`` is
|
|
30
|
+
``"ready"`` rather than ``"Activity.READY"``; the status line and the stored
|
|
31
|
+
metadata rely on that, so every interpreter uses this class instead of the
|
|
32
|
+
standard one and the wording cannot drift with the version.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __str__(self) -> str:
|
|
36
|
+
"""Return the member's value, as ``enum.StrEnum`` does."""
|
|
37
|
+
return str(self.value)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _patch_typing_eval_type() -> None:
|
|
41
|
+
"""Give the pre-release 3.14 ``_eval_type`` the keyword pydantic already passes.
|
|
42
|
+
|
|
43
|
+
Python 3.14.0b4 has no ``prefer_fwd_module`` argument; the final 3.14 release added it, and pydantic 2.13+
|
|
44
|
+
calls with it. The beta is what some readers still have, and the difference is an unused hint, so the wrapper
|
|
45
|
+
accepts it and calls the interpreter's own function without it. The final release is left alone.
|
|
46
|
+
"""
|
|
47
|
+
if sys.version_info < (3, 14) or sys.version_info.releaselevel == "final":
|
|
48
|
+
return
|
|
49
|
+
original = getattr(typing, "_eval_type", None)
|
|
50
|
+
if original is None or "prefer_fwd_module" in inspect.signature(original).parameters:
|
|
51
|
+
return
|
|
52
|
+
sentinel = getattr(typing, "_sentinel", None)
|
|
53
|
+
|
|
54
|
+
def _eval_type(
|
|
55
|
+
t: object,
|
|
56
|
+
globalns: object,
|
|
57
|
+
localns: object,
|
|
58
|
+
type_params: object = sentinel,
|
|
59
|
+
*,
|
|
60
|
+
recursive_guard: frozenset[str] = frozenset(),
|
|
61
|
+
format: object = None,
|
|
62
|
+
owner: object = None,
|
|
63
|
+
parent_fwdref: object = None,
|
|
64
|
+
prefer_fwd_module: bool = False,
|
|
65
|
+
) -> object:
|
|
66
|
+
del prefer_fwd_module
|
|
67
|
+
return original(
|
|
68
|
+
t,
|
|
69
|
+
globalns,
|
|
70
|
+
localns,
|
|
71
|
+
type_params,
|
|
72
|
+
recursive_guard=recursive_guard,
|
|
73
|
+
format=format,
|
|
74
|
+
owner=owner,
|
|
75
|
+
parent_fwdref=parent_fwdref,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
typing._eval_type = _eval_type
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
_patch_typing_eval_type()
|
|
@@ -217,6 +217,10 @@ MAX_MOUSE_SCROLL_SPEED = 20.0
|
|
|
217
217
|
#: has a fourth wording, ``add one with POST /models``, because that is the instruction over HTTP.
|
|
218
218
|
NO_MODEL_REFUSAL = "No model is configured; add one with /config"
|
|
219
219
|
|
|
220
|
+
#: What a path that needs a model is refused with when one is available but none has been chosen — a plugin offers
|
|
221
|
+
#: models and the file lists none, so the instruction is the picker, not the file editor.
|
|
222
|
+
NO_MODEL_CHOSEN = "No model is chosen; pick one with /model"
|
|
223
|
+
|
|
220
224
|
|
|
221
225
|
@dataclass(frozen=True, slots=True)
|
|
222
226
|
class ModelConfig:
|
|
@@ -261,6 +265,11 @@ class ModelConfig:
|
|
|
261
265
|
tuple, the default, means every level: a provider that cannot express one maps it
|
|
262
266
|
onto its closest, which is what makes the default right for an endpoint whose
|
|
263
267
|
support is unknown. An entry names the subset when the endpoint rejects the rest.
|
|
268
|
+
provider: The name of the plugin host that offered this model, when it came from one; ``None`` for an entry
|
|
269
|
+
the reader wrote. It is a record of where the entry came from, not something read to reach the host.
|
|
270
|
+
token_env: The environment variable a plugin named for this model's token, when it was not set when the
|
|
271
|
+
runtime was built; ``None`` when the entry carries its token, has no host to name one, or the variable
|
|
272
|
+
was present. It is shown so a model that cannot work says which key is missing.
|
|
264
273
|
"""
|
|
265
274
|
|
|
266
275
|
model: str
|
|
@@ -277,6 +286,8 @@ class ModelConfig:
|
|
|
277
286
|
temperature: float | None = None
|
|
278
287
|
web_search: bool = False
|
|
279
288
|
web_search_name: str | None = None
|
|
289
|
+
provider: str | None = None
|
|
290
|
+
token_env: str | None = None
|
|
280
291
|
|
|
281
292
|
def __post_init__(self) -> None:
|
|
282
293
|
"""Reject a model that cannot be called."""
|
|
@@ -684,9 +695,9 @@ def _setting(data: dict[str, object], key: str, expected: type, default: object
|
|
|
684
695
|
return _typed(value, f"Config key {key!r}", expected)
|
|
685
696
|
|
|
686
697
|
|
|
687
|
-
def _entry_model(entry: dict[str, object], index: int) -> ModelConfig:
|
|
698
|
+
def _entry_model(entry: dict[str, object], index: int, *, label: str | None = None) -> ModelConfig:
|
|
688
699
|
"""Build one model from a ``[[models]]`` table, rejecting unknown keys."""
|
|
689
|
-
label = f"Config models[{index}]"
|
|
700
|
+
label = label or f"Config models[{index}]"
|
|
690
701
|
unknown = sorted(set(entry) - MODEL_KEYS)
|
|
691
702
|
if unknown:
|
|
692
703
|
raise ValueError(f"Unknown config keys in {label}: {', '.join(unknown)}")
|
|
@@ -729,6 +740,11 @@ def _entry_model(entry: dict[str, object], index: int) -> ModelConfig:
|
|
|
729
740
|
raise ValueError(f"{label}: {error}") from error
|
|
730
741
|
|
|
731
742
|
|
|
743
|
+
def model_from_entry(entry: dict[str, object], *, label: str = "The entry you are adding") -> ModelConfig:
|
|
744
|
+
"""Build one model through the same reader ``load_config`` uses, for a form validating its draft."""
|
|
745
|
+
return _entry_model(entry, 1, label=label)
|
|
746
|
+
|
|
747
|
+
|
|
732
748
|
def _reject_shared_display_names(models: tuple[ModelConfig, ...], source: Path) -> None:
|
|
733
749
|
"""Refuse a configuration in which two models are shown by one name.
|
|
734
750
|
|
|
@@ -200,18 +200,20 @@ def _guarded(text: str, where: Path) -> tuple[list[str], list[tuple[int, int]],
|
|
|
200
200
|
return lines, blocks, data
|
|
201
201
|
|
|
202
202
|
|
|
203
|
-
def _checked(
|
|
203
|
+
def _checked(
|
|
204
|
+
new_text: str, before: dict[str, Any], expected: list[dict[str, Any]], where: Path, *, allow_empty: bool = False
|
|
205
|
+
) -> None:
|
|
204
206
|
"""Refuse a result that does not hold what the edit meant it to, before anything is written."""
|
|
205
207
|
after = _parse(new_text, where)
|
|
206
|
-
|
|
208
|
+
models = after.get("models") or []
|
|
209
|
+
if models != expected:
|
|
207
210
|
raise ConfigEditError("the edit did not produce the models it meant to; nothing was written")
|
|
208
211
|
others_before = {key: value for key, value in before.items() if key != "models"}
|
|
209
212
|
others_after = {key: value for key, value in after.items() if key != "models"}
|
|
210
213
|
if others_before != others_after:
|
|
211
214
|
raise ConfigEditError("the edit would have changed something other than the models; nothing was written")
|
|
212
|
-
models = after.get("models")
|
|
213
215
|
try:
|
|
214
|
-
_read_models(models, where)
|
|
216
|
+
_read_models(models, where, required=not allow_empty)
|
|
215
217
|
except ValueError as error:
|
|
216
218
|
raise ConfigEditError(str(error)) from error
|
|
217
219
|
|
|
@@ -257,12 +259,20 @@ def replace_model(text: str, index: int, entry: Mapping[str, object], *, where:
|
|
|
257
259
|
return result
|
|
258
260
|
|
|
259
261
|
|
|
260
|
-
def remove_model(text: str, index: int, *, where: Path) -> str:
|
|
261
|
-
"""Return ``text`` without model number ``index`` (from zero).
|
|
262
|
+
def remove_model(text: str, index: int, *, where: Path, allow_empty: bool = False) -> str:
|
|
263
|
+
"""Return ``text`` without model number ``index`` (from zero).
|
|
264
|
+
|
|
265
|
+
Args:
|
|
266
|
+
text: The file's text.
|
|
267
|
+
index: Which model to drop.
|
|
268
|
+
where: The file, for messages.
|
|
269
|
+
allow_empty: Allow the last entry to go. Only a caller that knows something else can serve a request (a
|
|
270
|
+
model a plugin offers) passes this; the file alone cannot be emptied.
|
|
271
|
+
"""
|
|
262
272
|
lines, blocks, data = _guarded(text, where)
|
|
263
273
|
if not 0 <= index < len(blocks):
|
|
264
274
|
raise ConfigEditError(f"there is no model number {index + 1}")
|
|
265
|
-
if len(blocks) == 1:
|
|
275
|
+
if len(blocks) == 1 and not allow_empty:
|
|
266
276
|
raise ConfigEditError("that is the only model; add another before removing it")
|
|
267
277
|
start, end = blocks[index]
|
|
268
278
|
# The blank line that set the block apart goes with it, so removing leaves no gap behind.
|
|
@@ -272,7 +282,7 @@ def remove_model(text: str, index: int, *, where: Path) -> str:
|
|
|
272
282
|
expected = list(data["models"])
|
|
273
283
|
del expected[index]
|
|
274
284
|
result = "\n".join(new)
|
|
275
|
-
_checked(result, data, expected, where)
|
|
285
|
+
_checked(result, data, expected, where, allow_empty=allow_empty)
|
|
276
286
|
return result
|
|
277
287
|
|
|
278
288
|
|
|
@@ -12,6 +12,10 @@ from __future__ import annotations
|
|
|
12
12
|
import importlib
|
|
13
13
|
from typing import TYPE_CHECKING
|
|
14
14
|
|
|
15
|
+
from zettcode._compat import _patch_typing_eval_type
|
|
16
|
+
|
|
17
|
+
_patch_typing_eval_type()
|
|
18
|
+
|
|
15
19
|
#: Where each public name lives; read by :func:`__getattr__` on first use.
|
|
16
20
|
_EXPORTS = {
|
|
17
21
|
"ZettCodeEngine": ".core",
|
|
@@ -6,7 +6,7 @@ from collections.abc import Mapping
|
|
|
6
6
|
from dataclasses import replace
|
|
7
7
|
from pathlib import Path
|
|
8
8
|
|
|
9
|
-
from zettcode.config import
|
|
9
|
+
from zettcode.config import ModelConfig, _read_models
|
|
10
10
|
from zettcode.config_edit import (
|
|
11
11
|
ConfigEditError,
|
|
12
12
|
add_model,
|
|
@@ -27,8 +27,8 @@ class SettingsMixin(AgentBase):
|
|
|
27
27
|
|
|
28
28
|
@property
|
|
29
29
|
def models(self) -> tuple[ModelConfig, ...]:
|
|
30
|
-
"""Return the
|
|
31
|
-
return self.runtime.
|
|
30
|
+
"""Return every model that can be chosen: the file's, then the ones plugins offered."""
|
|
31
|
+
return self.runtime.models
|
|
32
32
|
|
|
33
33
|
@property
|
|
34
34
|
def active_model(self) -> ModelConfig | None:
|
|
@@ -79,7 +79,7 @@ class SettingsMixin(AgentBase):
|
|
|
79
79
|
ConfigEditError: When the file cannot be read, is not valid, or lays its models out in
|
|
80
80
|
a way the editor cannot safely change.
|
|
81
81
|
"""
|
|
82
|
-
text = self._read_config_file(allow_missing=not self.models)
|
|
82
|
+
text = self._read_config_file(allow_missing=not (self.runtime.file_models or self.runtime.config.models))
|
|
83
83
|
path = self.config_file
|
|
84
84
|
entries = file_entries(text, where=path)
|
|
85
85
|
try:
|
|
@@ -101,7 +101,7 @@ class SettingsMixin(AgentBase):
|
|
|
101
101
|
write_text_atomically(path, text)
|
|
102
102
|
except OSError as error:
|
|
103
103
|
raise ConfigEditError(f"{path} could not be written: {error}") from error
|
|
104
|
-
models = _read_models(file_entries(text, where=path), path)
|
|
104
|
+
models = _read_models(file_entries(text, where=path), path, required=False)
|
|
105
105
|
return self.runtime.replace_models(models)
|
|
106
106
|
|
|
107
107
|
def add_configured_model(self, entry: Mapping[str, object]) -> ModelConfig:
|
|
@@ -119,7 +119,11 @@ class SettingsMixin(AgentBase):
|
|
|
119
119
|
ConfigEditError: When the entry is not a model that can be called, or the file cannot
|
|
120
120
|
be edited. Nothing is written then.
|
|
121
121
|
"""
|
|
122
|
-
text = add_model(
|
|
122
|
+
text = add_model(
|
|
123
|
+
self._read_config_file(allow_missing=not (self.runtime.file_models or self.runtime.config.models)),
|
|
124
|
+
entry,
|
|
125
|
+
where=self.config_file,
|
|
126
|
+
)
|
|
123
127
|
first = self.runtime.active_model is None
|
|
124
128
|
self._commit_models(text)
|
|
125
129
|
model = self.runtime.config.models[-1]
|
|
@@ -150,19 +154,29 @@ class SettingsMixin(AgentBase):
|
|
|
150
154
|
if not 0 < percent <= 100:
|
|
151
155
|
raise ValueError("The compression share must be more than 0 and at most 100.")
|
|
152
156
|
if self.runtime.active_model is None:
|
|
153
|
-
raise ValueError(
|
|
157
|
+
raise ValueError(self.runtime.no_model_refusal)
|
|
154
158
|
active = self.runtime.required_model
|
|
155
159
|
value = int(percent) if float(percent).is_integer() else percent
|
|
156
160
|
try:
|
|
157
161
|
for index, (model, entry) in enumerate(self.file_models()):
|
|
158
162
|
if model == active:
|
|
159
163
|
return self.update_configured_model(index, {**entry, "compact_percent": value}), None
|
|
160
|
-
reason = "the model in use is not in the config file"
|
|
161
164
|
except ConfigEditError as error:
|
|
162
165
|
reason = str(error)
|
|
166
|
+
else:
|
|
167
|
+
reason = "the model in use is not in the config file"
|
|
163
168
|
edited = replace(active, compact_percent=float(percent))
|
|
164
|
-
|
|
165
|
-
|
|
169
|
+
provided = self.runtime.provided_models
|
|
170
|
+
if any(model == active for model in provided):
|
|
171
|
+
return (
|
|
172
|
+
self.runtime.replace_provided(tuple(edited if model == active else model for model in provided)),
|
|
173
|
+
"a model a plugin offers has no line in the config file",
|
|
174
|
+
)
|
|
175
|
+
# The model in use came from neither list (a runtime a test built by hand): change it for this run.
|
|
176
|
+
self.runtime.active_model = edited
|
|
177
|
+
self.runtime.compaction.max_tokens = edited.compaction_max_tokens
|
|
178
|
+
self.runtime.compaction.keep_recent_tokens = edited.compaction_keep_tokens
|
|
179
|
+
return edited, reason
|
|
166
180
|
|
|
167
181
|
def remove_configured_model(self, index: int) -> ModelConfig:
|
|
168
182
|
"""Remove model number ``index`` (from zero) from the file and this session.
|
|
@@ -171,9 +185,15 @@ class SettingsMixin(AgentBase):
|
|
|
171
185
|
The model in use afterwards: the one before unless it was the one removed.
|
|
172
186
|
|
|
173
187
|
Raises:
|
|
174
|
-
ConfigEditError: When it is the only model
|
|
188
|
+
ConfigEditError: When it is the file's only model and nothing else (a model a plugin offers) could
|
|
189
|
+
serve a request, or the file cannot be edited.
|
|
175
190
|
"""
|
|
176
|
-
text = remove_model(
|
|
191
|
+
text = remove_model(
|
|
192
|
+
self._read_config_file(),
|
|
193
|
+
index,
|
|
194
|
+
where=self.config_file,
|
|
195
|
+
allow_empty=bool(self.runtime.provided_models),
|
|
196
|
+
)
|
|
177
197
|
return self._commit_models(text)
|
|
178
198
|
|
|
179
199
|
@property
|
|
@@ -32,6 +32,7 @@ from zett_agent.model import ReasoningEffort
|
|
|
32
32
|
from zettcode.config import (
|
|
33
33
|
DEFAULT_ANTHROPIC_BASE_URL,
|
|
34
34
|
KEYLESS_TOKEN,
|
|
35
|
+
NO_MODEL_CHOSEN,
|
|
35
36
|
NO_MODEL_REFUSAL,
|
|
36
37
|
ModelConfig,
|
|
37
38
|
ZettCodeConfig,
|
|
@@ -77,6 +78,7 @@ from .extensions.usage import UsageExtension
|
|
|
77
78
|
from .permission import Permission, ShellPolicy, parse_permission
|
|
78
79
|
from .permission.sandbox import ShellSandbox
|
|
79
80
|
from .pipeline import ImportPipeline
|
|
81
|
+
from .provided import entries, merged
|
|
80
82
|
from .storage import NoLocks, SessionGuard, SessionLocked, SessionLocks, SessionStore
|
|
81
83
|
|
|
82
84
|
if TYPE_CHECKING: # pragma: no cover - annotations only; the imports are the cost
|
|
@@ -251,7 +253,7 @@ class _ContextPageAdapter(ContextPageService):
|
|
|
251
253
|
async def snapshot(self, session_id: str) -> ContextPageSnapshot:
|
|
252
254
|
runtime = self._runtime
|
|
253
255
|
if runtime is None or runtime.active_model is None:
|
|
254
|
-
raise ValueError(NO_MODEL_REFUSAL)
|
|
256
|
+
raise ValueError(NO_MODEL_REFUSAL if runtime is None else runtime.no_model_refusal)
|
|
255
257
|
model = runtime.active_model
|
|
256
258
|
return ContextPageSnapshot(
|
|
257
259
|
model_name=model.shown_name,
|
|
@@ -313,6 +315,10 @@ class ZettCodeEngine:
|
|
|
313
315
|
#: The plugins' triggers, started with the runtime (see :meth:`start`) and stopped by :meth:`aclose`.
|
|
314
316
|
_triggers: TriggerHost | None = field(default=None, repr=False)
|
|
315
317
|
_models: dict[ModelConfig, Adapter] = field(default_factory=dict)
|
|
318
|
+
#: The models the configuration file holds, in file order: what `/config` lists and edits.
|
|
319
|
+
file_models: tuple[ModelConfig, ...] = ()
|
|
320
|
+
#: The models plugins offered, which can be chosen but are not in the file.
|
|
321
|
+
provided_models: tuple[ModelConfig, ...] = field(default_factory=tuple, repr=False)
|
|
316
322
|
#: Each session's own self-compaction choice as last read or written; see :meth:`self_compaction_choice`.
|
|
317
323
|
_self_choices: dict[str, bool | None] = field(default_factory=dict, repr=False)
|
|
318
324
|
#: Each session's own permission level as last read or written; see :meth:`permission_choice`.
|
|
@@ -368,9 +374,11 @@ class ZettCodeEngine:
|
|
|
368
374
|
"ZettCodeEngine.preview(shared=...) may not replace the runtime's own services: " + ", ".join(taken)
|
|
369
375
|
)
|
|
370
376
|
os.chdir(config.workspace)
|
|
377
|
+
file_models = config.models
|
|
371
378
|
# A machine that has never been set up has no model to start on; the shell opens anyway and says so (see
|
|
372
|
-
# the invariant S23), and `use_model` sizes this runtime the moment `/config` writes the first entry.
|
|
373
|
-
|
|
379
|
+
# the invariant S23), and `use_model` sizes this runtime the moment `/config` writes the first entry. A
|
|
380
|
+
# model a plugin offers is not made active on its own: `/model` is where a reader chooses it.
|
|
381
|
+
selected = file_models[0] if file_models else None
|
|
374
382
|
effort = config.reasoning_effort if selected is None else selected.accepted_effort(config.reasoning_effort)
|
|
375
383
|
# One policy, read by everything that decides whether a command is asked
|
|
376
384
|
# about: the memory lets `safe` and `auto` through, the deny extension
|
|
@@ -389,6 +397,7 @@ class ZettCodeEngine:
|
|
|
389
397
|
delivery = RuntimeDelivery()
|
|
390
398
|
context_reports = _ContextReportAdapter()
|
|
391
399
|
context_page = _ContextPageAdapter()
|
|
400
|
+
access = ModelAccess(config.models)
|
|
392
401
|
provided: dict[str, object] = {
|
|
393
402
|
POLICY: policy,
|
|
394
403
|
HARNESS: harness,
|
|
@@ -400,7 +409,7 @@ class ZettCodeEngine:
|
|
|
400
409
|
DELIVERY: delivery,
|
|
401
410
|
CONTEXT_REPORT: context_reports,
|
|
402
411
|
CONTEXT_PAGE: context_page,
|
|
403
|
-
LLM:
|
|
412
|
+
LLM: access,
|
|
404
413
|
PLUGIN_CLI: {},
|
|
405
414
|
}
|
|
406
415
|
if selected is not None:
|
|
@@ -409,6 +418,7 @@ class ZettCodeEngine:
|
|
|
409
418
|
provided[SELECTED_MODEL] = selected
|
|
410
419
|
if shared:
|
|
411
420
|
provided.update(shared)
|
|
421
|
+
plugins = (load or _load_engine_plugins)(config, provided)
|
|
412
422
|
runtime = cls(
|
|
413
423
|
config=config,
|
|
414
424
|
persistence=persistence,
|
|
@@ -420,11 +430,15 @@ class ZettCodeEngine:
|
|
|
420
430
|
session_id=new_uuid7(),
|
|
421
431
|
active_model=selected,
|
|
422
432
|
compaction_counter=counter,
|
|
423
|
-
plugins=
|
|
433
|
+
plugins=plugins,
|
|
424
434
|
_models=models,
|
|
435
|
+
file_models=file_models,
|
|
436
|
+
provided_models=entries(plugins.providers),
|
|
425
437
|
)
|
|
426
438
|
context_reports.bind(runtime)
|
|
427
439
|
context_page.bind(runtime)
|
|
440
|
+
# The plugins are loaded, so the models they offered are known: a plugin that lists the models sees its own.
|
|
441
|
+
access.replace(runtime.models)
|
|
428
442
|
# The session a launch starts in is a new one, so this always succeeds; a launch that resumes
|
|
429
443
|
# another moves the lock when it installs it.
|
|
430
444
|
runtime.session_locks.claim(runtime.session_id)
|
|
@@ -434,6 +448,20 @@ class ZettCodeEngine:
|
|
|
434
448
|
runtime.compaction.self_compact = runtime.self_compacting
|
|
435
449
|
return runtime
|
|
436
450
|
|
|
451
|
+
@property
|
|
452
|
+
def models(self) -> tuple[ModelConfig, ...]:
|
|
453
|
+
"""Return every model that can be chosen: the file's, then the ones plugins offered."""
|
|
454
|
+
return merged(self.file_models or self.config.models, self.provided_models)
|
|
455
|
+
|
|
456
|
+
@property
|
|
457
|
+
def no_model_refusal(self) -> str:
|
|
458
|
+
"""Return the sentence for a path that needs a model and has none in use.
|
|
459
|
+
|
|
460
|
+
A machine with nothing available is told to configure one; one that has models a plugin offers is told
|
|
461
|
+
to pick one, because ``/config`` edits only the file.
|
|
462
|
+
"""
|
|
463
|
+
return NO_MODEL_CHOSEN if self.models else NO_MODEL_REFUSAL
|
|
464
|
+
|
|
437
465
|
def start(self) -> asyncio.Task[ZettCodeEngine]:
|
|
438
466
|
"""Build the provider and the client once, whoever asks first.
|
|
439
467
|
|
|
@@ -461,7 +489,7 @@ class ZettCodeEngine:
|
|
|
461
489
|
self._triggers = TriggerHost(self.plugins.triggers, self.config.store.parent / "triggers")
|
|
462
490
|
self._triggers.start()
|
|
463
491
|
if self.active_model is None:
|
|
464
|
-
raise ValueError(
|
|
492
|
+
raise ValueError(self.no_model_refusal)
|
|
465
493
|
if self._starting is None:
|
|
466
494
|
self._begin_imports()
|
|
467
495
|
self.compaction_counter.start()
|
|
@@ -492,9 +520,9 @@ class ZettCodeEngine:
|
|
|
492
520
|
# only talks to OpenAI-compatible endpoints does not import `anthropic` on a thread that
|
|
493
521
|
# competes with the interface for the interpreter.
|
|
494
522
|
sdks: list[str] = []
|
|
495
|
-
if any(not model.anthropic for model in
|
|
523
|
+
if any(not model.anthropic for model in self.models):
|
|
496
524
|
sdks.extend(OPENAI_WARM_MODULES)
|
|
497
|
-
if any(model.anthropic for model in
|
|
525
|
+
if any(model.anthropic for model in self.models):
|
|
498
526
|
sdks.extend(ANTHROPIC_WARM_MODULES)
|
|
499
527
|
optional = tuple(module for setting, module in WARM_MODULES_WHEN if getattr(config, setting, False))
|
|
500
528
|
return (*sdks, *WARM_MODULES, *optional)
|
|
@@ -552,7 +580,7 @@ class ZettCodeEngine:
|
|
|
552
580
|
if selected is None:
|
|
553
581
|
# Nothing to build a provider for. The shell refuses a request before it gets this far; anything
|
|
554
582
|
# else that asked anyway is told the one thing that changes it.
|
|
555
|
-
raise ValueError(
|
|
583
|
+
raise ValueError(self.no_model_refusal)
|
|
556
584
|
model = self._provider(selected)
|
|
557
585
|
# Every extension the agent runs is a plugin's: the built-in ones (the coding tools, the session
|
|
558
586
|
# store, the shell approval, the compaction ... see `zettcode.sdk.builtins`) in the order
|
|
@@ -657,7 +685,7 @@ class ZettCodeEngine:
|
|
|
657
685
|
ValueError: No model is configured.
|
|
658
686
|
"""
|
|
659
687
|
if self.active_model is None:
|
|
660
|
-
raise ValueError(
|
|
688
|
+
raise ValueError(self.no_model_refusal)
|
|
661
689
|
return self.active_model
|
|
662
690
|
|
|
663
691
|
def set_event_dispatcher(self, dispatcher: AgentEventDispatcher | None) -> None:
|
|
@@ -841,7 +869,7 @@ class ZettCodeEngine:
|
|
|
841
869
|
raise ValueError(f"Unknown reasoning effort: {name}. Try one of: {levels}") from None
|
|
842
870
|
active = self.active_model
|
|
843
871
|
if active is None:
|
|
844
|
-
raise ValueError(
|
|
872
|
+
raise ValueError(self.no_model_refusal)
|
|
845
873
|
accepted = active.efforts
|
|
846
874
|
if level not in accepted:
|
|
847
875
|
levels = ", ".join(known.value for known in accepted)
|
|
@@ -859,27 +887,28 @@ class ZettCodeEngine:
|
|
|
859
887
|
with, and guessing would move the model under the reader without saying so.
|
|
860
888
|
"""
|
|
861
889
|
try:
|
|
862
|
-
index = model_index(self.
|
|
890
|
+
index = model_index(self.models, name)
|
|
863
891
|
except ValueError:
|
|
864
892
|
return None
|
|
865
|
-
return None if index is None else self.
|
|
893
|
+
return None if index is None else self.models[index]
|
|
866
894
|
|
|
867
895
|
def use_model(self, name: str | ModelConfig) -> ModelConfig:
|
|
868
|
-
"""Select a
|
|
896
|
+
"""Select a model for subsequent requests without changing sessions."""
|
|
897
|
+
models = self.models
|
|
869
898
|
if isinstance(name, ModelConfig):
|
|
870
899
|
# The very entry first, so two entries that hold the same settings stay two choices; an
|
|
871
900
|
# equal one is accepted too, because a model read back from the config file is a new
|
|
872
901
|
# object holding what the session's entry holds, and is the same model to a reader.
|
|
873
|
-
chosen = next((entry for entry in
|
|
874
|
-
(entry for entry in
|
|
902
|
+
chosen = next((entry for entry in models if entry is name), None) or next(
|
|
903
|
+
(entry for entry in models if entry == name), None
|
|
875
904
|
)
|
|
876
905
|
if chosen is None:
|
|
877
906
|
raise ValueError("Unknown model selection")
|
|
878
907
|
else:
|
|
879
|
-
index = model_index(
|
|
908
|
+
index = model_index(models, name)
|
|
880
909
|
if index is None:
|
|
881
910
|
raise ValueError(f"Unknown model: {name}")
|
|
882
|
-
chosen =
|
|
911
|
+
chosen = models[index]
|
|
883
912
|
self.active_model = chosen
|
|
884
913
|
# A preview runtime has no provider yet; the model it names is what
|
|
885
914
|
# :meth:`_build` starts from.
|
|
@@ -894,6 +923,12 @@ class ZettCodeEngine:
|
|
|
894
923
|
self.effort = chosen.accepted_effort(self.effort)
|
|
895
924
|
return chosen
|
|
896
925
|
|
|
926
|
+
def _refresh_models_service(self) -> None:
|
|
927
|
+
"""Keep the plugin model service in step with the list a front end would show."""
|
|
928
|
+
access = self.plugins.find(LLM, ModelAccess)
|
|
929
|
+
if access is not None:
|
|
930
|
+
access.replace(self.models)
|
|
931
|
+
|
|
897
932
|
def replace_models(self, models: tuple[ModelConfig, ...]) -> ModelConfig:
|
|
898
933
|
"""Adopt a new list of models in this running session, keeping the one in use if it is still there.
|
|
899
934
|
|
|
@@ -913,15 +948,35 @@ class ZettCodeEngine:
|
|
|
913
948
|
Returns:
|
|
914
949
|
The model in use afterwards, which is the previous one unless it was removed.
|
|
915
950
|
"""
|
|
916
|
-
if not models:
|
|
951
|
+
if not models and not self.provided_models:
|
|
917
952
|
raise ValueError("At least one model must stay configured")
|
|
918
953
|
previous = self.active_model
|
|
919
|
-
old = self.
|
|
954
|
+
old = self.models
|
|
920
955
|
place = old.index(previous) if previous is not None and previous in old else 0
|
|
956
|
+
self.file_models = models
|
|
921
957
|
self.config = replace(self.config, models=models)
|
|
958
|
+
current = self.models
|
|
922
959
|
# The adapters already built stay in the cache even for an entry that is gone: the cache is
|
|
923
960
|
# what shutdown walks to close each client, so dropping one here would leak its connection.
|
|
924
|
-
chosen = next((entry for entry in
|
|
961
|
+
chosen = next((entry for entry in current if entry == previous), current[min(place, len(current) - 1)])
|
|
962
|
+
self._refresh_models_service()
|
|
963
|
+
return self.use_model(chosen)
|
|
964
|
+
|
|
965
|
+
def replace_provided(self, models: tuple[ModelConfig, ...]) -> ModelConfig:
|
|
966
|
+
"""Adopt a new list of plugin-provided models, keeping the one in use if it is still there.
|
|
967
|
+
|
|
968
|
+
This is the same rule as :meth:`replace_models` for the models that are not in the file; a reader who
|
|
969
|
+
changes one of their settings (the compaction share, say) changes it for this run only.
|
|
970
|
+
"""
|
|
971
|
+
previous = self.active_model
|
|
972
|
+
old = self.models
|
|
973
|
+
place = old.index(previous) if previous is not None and previous in old else 0
|
|
974
|
+
current = merged(self.file_models or self.config.models, models)
|
|
975
|
+
if not current:
|
|
976
|
+
raise ValueError("At least one model must stay available")
|
|
977
|
+
self.provided_models = models
|
|
978
|
+
chosen = next((entry for entry in current if entry == previous), current[min(place, len(current) - 1)])
|
|
979
|
+
self._refresh_models_service()
|
|
925
980
|
return self.use_model(chosen)
|
|
926
981
|
|
|
927
982
|
def new_session(self) -> str:
|
|
@@ -34,6 +34,22 @@ def harness_root(config: ZettCodeConfig) -> Path:
|
|
|
34
34
|
return config.harness_dir / workspace_directory_name(config.workspace)
|
|
35
35
|
|
|
36
36
|
|
|
37
|
+
def session_path(config: ZettCodeConfig, session_id: str) -> Path:
|
|
38
|
+
"""Return a session's canonical harness folder without moving or creating anything."""
|
|
39
|
+
from zett_agent.jit_harness.repo import session_directory_name
|
|
40
|
+
|
|
41
|
+
return harness_root(config) / session_directory_name(session_id)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def existing_session_path(config: ZettCodeConfig, session_id: str) -> Path:
|
|
45
|
+
"""Return the canonical folder when it exists, else a pre-workspace one, without moving either."""
|
|
46
|
+
current = session_path(config, session_id)
|
|
47
|
+
if current.is_dir():
|
|
48
|
+
return current
|
|
49
|
+
legacy = config.harness_dir / current.name
|
|
50
|
+
return legacy if legacy.is_dir() else current
|
|
51
|
+
|
|
52
|
+
|
|
37
53
|
def move_session(legacy_root: Path, root: Path, session_id: str) -> Path:
|
|
38
54
|
"""Return this session's directory under ``root``, moving ``legacy_root``'s copy into it first.
|
|
39
55
|
|