delos-cli 5.9.18__tar.gz → 5.9.22__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.
- {delos_cli-5.9.18 → delos_cli-5.9.22}/PKG-INFO +1 -1
- delos_cli-5.9.22/delos_cli/_build.json +5 -0
- delos_cli-5.9.22/delos_cli/agent/session.py +296 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/agent/transport.py +21 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/apps/base.py +15 -5
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/apps/chat/app.py +56 -8
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/apps/chat/commands.py +1 -1
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/apps/chat/render.py +115 -19
- delos_cli-5.9.22/delos_cli/apps/chat/rewind.py +136 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/auth/oauth.py +26 -21
- delos_cli-5.9.22/delos_cli/checkpoints.py +142 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/commands/builtin.py +6 -19
- delos_cli-5.9.22/delos_cli/commands/mcp.py +173 -0
- delos_cli-5.9.22/delos_cli/commands/permissions.py +53 -0
- delos_cli-5.9.22/delos_cli/commands/sandbox.py +105 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/context_report.py +5 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/ctx.py +42 -1
- delos_cli-5.9.22/delos_cli/file_state.py +63 -0
- delos_cli-5.9.22/delos_cli/hooks.py +157 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/main.py +1 -145
- delos_cli-5.9.22/delos_cli/mcp/landing.py +106 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/mcp/manager.py +2 -4
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/mcp/remote.py +19 -8
- delos_cli-5.9.22/delos_cli/permissions.py +94 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/project_context.py +14 -6
- delos_cli-5.9.22/delos_cli/sandbox/__init__.py +20 -0
- delos_cli-5.9.22/delos_cli/sandbox/approval.py +76 -0
- delos_cli-5.9.22/delos_cli/sandbox/bwrap.py +82 -0
- delos_cli-5.9.22/delos_cli/sandbox/config.py +79 -0
- delos_cli-5.9.22/delos_cli/sandbox/core.py +80 -0
- delos_cli-5.9.22/delos_cli/sandbox/denial.py +42 -0
- delos_cli-5.9.22/delos_cli/sandbox/platform.py +133 -0
- delos_cli-5.9.22/delos_cli/sandbox/policy.py +173 -0
- delos_cli-5.9.22/delos_cli/sandbox/seatbelt.py +81 -0
- delos_cli-5.9.22/delos_cli/sandbox/seatbelt.sbpl +129 -0
- delos_cli-5.9.22/delos_cli/sandbox/seatbelt_network.sbpl +33 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/serve/confirm.py +31 -16
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/serve/rpc.py +29 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/serve/server.py +95 -15
- delos_cli-5.9.22/delos_cli/settings.py +115 -0
- delos_cli-5.9.22/delos_cli/skills.py +184 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/state.py +26 -9
- delos_cli-5.9.22/delos_cli/tool_output.py +77 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/tools/__init__.py +3 -1
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/tools/edit_content.py +31 -12
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/tools/explore.py +26 -8
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/tools/glob_tool.py +12 -12
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/tools/grep.py +12 -9
- delos_cli-5.9.22/delos_cli/tools/read.py +135 -0
- delos_cli-5.9.22/delos_cli/tools/run_shell.py +699 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/tools/verify.py +38 -23
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/tools/write_content.py +30 -10
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/transport/chats.py +42 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/transport/client.py +6 -1
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/ui/__init__.py +1 -2
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/ui/collapse.py +32 -12
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/ui/output.py +13 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/ui/repl.py +339 -110
- {delos_cli-5.9.18 → delos_cli-5.9.22}/pyproject.toml +1 -1
- delos_cli-5.9.22/tests/agent/test_hooks.py +163 -0
- delos_cli-5.9.22/tests/agent/test_steering.py +76 -0
- delos_cli-5.9.22/tests/conftest.py +20 -0
- delos_cli-5.9.22/tests/context/__init__.py +0 -0
- delos_cli-5.9.22/tests/context/test_checkpoints.py +165 -0
- delos_cli-5.9.22/tests/context/test_permissions.py +93 -0
- delos_cli-5.9.22/tests/context/test_project_context.py +36 -0
- delos_cli-5.9.22/tests/context/test_skills.py +93 -0
- delos_cli-5.9.22/tests/mcp/__init__.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/tests/mcp/test_manager.py +5 -2
- {delos_cli-5.9.18 → delos_cli-5.9.22}/tests/mcp/test_remote.py +125 -6
- delos_cli-5.9.22/tests/sandbox/__init__.py +0 -0
- delos_cli-5.9.22/tests/sandbox/test_approval.py +57 -0
- delos_cli-5.9.22/tests/sandbox/test_bwrap.py +95 -0
- delos_cli-5.9.22/tests/sandbox/test_config.py +69 -0
- delos_cli-5.9.22/tests/sandbox/test_denial.py +37 -0
- delos_cli-5.9.22/tests/sandbox/test_integration.py +155 -0
- delos_cli-5.9.22/tests/sandbox/test_platform.py +44 -0
- delos_cli-5.9.22/tests/sandbox/test_policy.py +105 -0
- delos_cli-5.9.22/tests/sandbox/test_seatbelt.py +60 -0
- delos_cli-5.9.22/tests/serve/__init__.py +0 -0
- delos_cli-5.9.22/tests/serve/test_confirm.py +109 -0
- delos_cli-5.9.22/tests/serve/test_steer.py +68 -0
- delos_cli-5.9.22/tests/tools/__init__.py +0 -0
- delos_cli-5.9.22/tests/tools/test_file_state.py +110 -0
- delos_cli-5.9.22/tests/tools/test_run_shell.py +282 -0
- delos_cli-5.9.22/tests/tools/test_tool_output.py +98 -0
- delos_cli-5.9.22/tests/ui/__init__.py +0 -0
- delos_cli-5.9.22/tests/ui/test_steering_keys.py +116 -0
- delos_cli-5.9.22/tests/ui/test_transcript_view.py +65 -0
- delos_cli-5.9.18/delos_cli/_build.json +0 -5
- delos_cli-5.9.18/delos_cli/agent/session.py +0 -165
- delos_cli-5.9.18/delos_cli/tools/read.py +0 -116
- delos_cli-5.9.18/delos_cli/tools/run_shell.py +0 -279
- delos_cli-5.9.18/delos_cli/ui/integrations_picker.py +0 -163
- {delos_cli-5.9.18 → delos_cli-5.9.22}/.gitignore +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/README.md +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/__init__.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/__main__.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/agent/__init__.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/agent/tools.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/agent/turn.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/apps/__init__.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/apps/chat/__init__.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/apps/replay.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/auth/__init__.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/auth/config.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/auth/mfa.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/auth/token_manager.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/commands/__init__.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/commands/base.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/commands/prompts.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/git_context.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/loop.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/mcp/__init__.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/mcp/client.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/mcp/config.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/mcp/handlers.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/mcp/state.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/public_key.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/sentry_identity.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/serve/__init__.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/serve/app.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/serve/protocol.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/telemetry.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/tools/ask_user_input.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/tools/task.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/tools/todo_write.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/transport/__init__.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/transport/documents.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/transport/integrations.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/transport/models.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/ui/banner.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/ui/chat_picker.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/ui/completer.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/ui/lexer.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/ui/model_picker.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/ui/style.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/delos_cli/usage_report.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/tests/__init__.py +0 -0
- {delos_cli-5.9.18/tests/mcp → delos_cli-5.9.22/tests/agent}/__init__.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/tests/mcp/fake_server.py +0 -0
- {delos_cli-5.9.18 → delos_cli-5.9.22}/tests/mcp/test_client.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: delos-cli
|
|
3
|
-
Version: 5.9.
|
|
3
|
+
Version: 5.9.22
|
|
4
4
|
Summary: Terminal REPL for the Delos agent — the Claude-Code-style CLI for Delos.
|
|
5
5
|
Project-URL: Homepage, https://delos.so
|
|
6
6
|
Project-URL: Repository, https://github.com/Delos-Intelligence/cosmos-saas
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
"""AgentSession — drives one user turn end-to-end.
|
|
2
|
+
|
|
3
|
+
A turn may span multiple SSE streams: each client-side tool call ends
|
|
4
|
+
one stream and starts another with ``tool_continuation=true``. From the
|
|
5
|
+
caller's POV, :meth:`AgentSession.send_turn` is a single async iterator
|
|
6
|
+
of UI events — all the multi-stream stitching is internal.
|
|
7
|
+
|
|
8
|
+
A :class:`ToolRegistry` resolves both how server-tool events render and
|
|
9
|
+
how client-tool calls collect input from the user.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import asyncio
|
|
15
|
+
import uuid
|
|
16
|
+
from itertools import starmap
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
from typing import TYPE_CHECKING, Any
|
|
19
|
+
|
|
20
|
+
from delos_cli.hooks import run_hooks
|
|
21
|
+
|
|
22
|
+
if TYPE_CHECKING:
|
|
23
|
+
from collections.abc import AsyncIterator
|
|
24
|
+
|
|
25
|
+
from delos_cli.ctx import Ctx
|
|
26
|
+
|
|
27
|
+
from .tools import ToolRegistry
|
|
28
|
+
from .transport import AgentTransport
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
#: Synthetic event: the steers a finished turn never reached (``data.contents``, oldest first).
|
|
32
|
+
STEERS_UNSENT = "data-steers-unsent"
|
|
33
|
+
#: Synthetic event: what a hook did that the user should know (``data.message``).
|
|
34
|
+
HOOK_NOTICE = "data-hook"
|
|
35
|
+
#: Body keys of the user's own message, not carried to a Stop hook's follow-up.
|
|
36
|
+
_USER_TURN_KEYS = frozenset({"messages", "git_context", "message_files"})
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class AgentSession:
|
|
40
|
+
"""One session per ``(transport, chat_id)`` pair."""
|
|
41
|
+
|
|
42
|
+
def __init__(
|
|
43
|
+
self,
|
|
44
|
+
transport: AgentTransport,
|
|
45
|
+
chat_id: str,
|
|
46
|
+
registry: ToolRegistry,
|
|
47
|
+
) -> None:
|
|
48
|
+
"""Bind to a chat row. Multiple turns share the same session."""
|
|
49
|
+
self._transport = transport
|
|
50
|
+
self._chat_id = chat_id
|
|
51
|
+
self._registry = registry
|
|
52
|
+
#: Steers sent but not yet taken by the agent, id → content, in send order.
|
|
53
|
+
self.pending_steers: dict[str, str] = {}
|
|
54
|
+
# Serialises a steer's inject with the withdrawal that ends the turn:
|
|
55
|
+
# a steer posted after it would wait in the backend for the next turn.
|
|
56
|
+
self._steer_lock = asyncio.Lock()
|
|
57
|
+
self._steers_closed = False
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def chat_id(self) -> str:
|
|
61
|
+
"""The chat row id this session is bound to."""
|
|
62
|
+
return self._chat_id
|
|
63
|
+
|
|
64
|
+
async def send_turn(
|
|
65
|
+
self,
|
|
66
|
+
body: dict[str, Any],
|
|
67
|
+
ctx: Ctx,
|
|
68
|
+
*,
|
|
69
|
+
emit_tool_output: bool = False,
|
|
70
|
+
hooks: bool = False,
|
|
71
|
+
) -> AsyncIterator[dict[str, Any]]:
|
|
72
|
+
"""Send one user turn and yield every UI event the agent emits.
|
|
73
|
+
|
|
74
|
+
Streams the initial ``/completions`` request. When the run
|
|
75
|
+
terminates with unresolved tool calls, treats them as client-side,
|
|
76
|
+
invokes the registered handler, posts the result via
|
|
77
|
+
``/tool-result``, and re-streams a continuation request with
|
|
78
|
+
``tool_continuation=true``, ``messages=[]`` and no ``git_context``
|
|
79
|
+
(the backend replays the snapshot it froze on the user message).
|
|
80
|
+
Loops until no pending calls remain or the user explicitly stopped.
|
|
81
|
+
|
|
82
|
+
The backend never emits ``tool-output-available`` for client-side
|
|
83
|
+
tools — with ``emit_tool_output=True``, a synthetic one is yielded
|
|
84
|
+
after each local handler resolves, so headless frontends can close
|
|
85
|
+
the matching tool card. The REPL renders results itself and keeps
|
|
86
|
+
the default (False).
|
|
87
|
+
|
|
88
|
+
Steers (:meth:`steer`) the agent did not reach before the turn ended
|
|
89
|
+
are withdrawn and yielded back as one synthetic ``STEERS_UNSENT``
|
|
90
|
+
event, for the frontend to send as the next turn.
|
|
91
|
+
|
|
92
|
+
With ``hooks=True`` the user's hooks (:mod:`delos_cli.hooks`) run:
|
|
93
|
+
UserPromptSubmit before the first request, PreToolUse and
|
|
94
|
+
PostToolUse around each client tool, Stop when the agent ends —
|
|
95
|
+
whose exit 2 sends its stderr as a new user message, and the turn
|
|
96
|
+
goes on. What they report comes as ``HOOK_NOTICE`` events.
|
|
97
|
+
|
|
98
|
+
Interruption is not handled here: the turn runs inside an
|
|
99
|
+
:class:`~delos_cli.agent.turn.AgentTurn`, which cancels the task
|
|
100
|
+
driving this generator. The resulting ``CancelledError`` propagates
|
|
101
|
+
through the ``async for`` below and closes the underlying HTTP stream.
|
|
102
|
+
"""
|
|
103
|
+
current_body = dict(body)
|
|
104
|
+
cwd = Path.cwd()
|
|
105
|
+
messages = list(body.get("messages") or [])
|
|
106
|
+
if hooks and messages and messages[-1].get("role") == "user":
|
|
107
|
+
message = messages[-1]
|
|
108
|
+
payload = {"session_id": self._chat_id, "prompt": message.get("content", "")}
|
|
109
|
+
result = await run_hooks("UserPromptSubmit", payload, cwd)
|
|
110
|
+
for failure in result.failures:
|
|
111
|
+
yield _notice(failure)
|
|
112
|
+
if result.blocked is not None:
|
|
113
|
+
yield {"type": "error", "errorText": f"A UserPromptSubmit hook blocked the message:\n{result.blocked}"}
|
|
114
|
+
return
|
|
115
|
+
if result.context:
|
|
116
|
+
context = f"<user-prompt-submit-hook>\n{result.context}\n</user-prompt-submit-hook>"
|
|
117
|
+
messages[-1] = {**message, "content": f"{message.get('content', '')}\n\n{context}"}
|
|
118
|
+
current_body["messages"] = messages
|
|
119
|
+
stop_hook_active = False
|
|
120
|
+
notices: list[str] = []
|
|
121
|
+
while True:
|
|
122
|
+
pending: dict[str, dict[str, Any]] = {}
|
|
123
|
+
stopped = False
|
|
124
|
+
async for event in self._transport.stream_completions(current_body):
|
|
125
|
+
etype = event.get("type", "")
|
|
126
|
+
if etype == "tool-input-available":
|
|
127
|
+
tcid = event.get("toolCallId", "")
|
|
128
|
+
if tcid:
|
|
129
|
+
pending[tcid] = {
|
|
130
|
+
"name": event.get("toolName", ""),
|
|
131
|
+
"input": event.get("input", {}) or {},
|
|
132
|
+
}
|
|
133
|
+
elif etype == "tool-output-available":
|
|
134
|
+
pending.pop(event.get("toolCallId", ""), None)
|
|
135
|
+
elif etype == "data-stopped":
|
|
136
|
+
stopped = True
|
|
137
|
+
elif etype == "data-inject-consumed":
|
|
138
|
+
self.pending_steers.pop(str((event.get("data") or {}).get("messageId")), None)
|
|
139
|
+
yield event
|
|
140
|
+
|
|
141
|
+
if stopped or not pending:
|
|
142
|
+
# Not while a steer waits: the agent was about to read it anyway.
|
|
143
|
+
if hooks and not stopped and not self.pending_steers:
|
|
144
|
+
result = await run_hooks(
|
|
145
|
+
"Stop", {"session_id": self._chat_id, "stop_hook_active": stop_hook_active}, cwd
|
|
146
|
+
)
|
|
147
|
+
for failure in result.failures:
|
|
148
|
+
yield _notice(failure)
|
|
149
|
+
if result.blocked is not None:
|
|
150
|
+
stop_hook_active = True
|
|
151
|
+
yield _notice(f"Stop hook: the agent goes on — {result.blocked}")
|
|
152
|
+
current_body = {
|
|
153
|
+
**{k: v for k, v in body.items() if k not in _USER_TURN_KEYS},
|
|
154
|
+
"messages": [
|
|
155
|
+
{
|
|
156
|
+
"role": "user",
|
|
157
|
+
"content": f"A Stop hook kept the turn going:\n{result.blocked}",
|
|
158
|
+
"messageId": uuid.uuid4().hex,
|
|
159
|
+
}
|
|
160
|
+
],
|
|
161
|
+
}
|
|
162
|
+
continue
|
|
163
|
+
# Typed after the agent's last tool boundary: the turn is over
|
|
164
|
+
# without it. ``STEERS_UNSENT`` hands it back to the frontend.
|
|
165
|
+
if unsent := await self.take_back_steers():
|
|
166
|
+
yield {"type": STEERS_UNSENT, "data": {"contents": unsent}}
|
|
167
|
+
return
|
|
168
|
+
|
|
169
|
+
# The backend only batches non-interactive client tools
|
|
170
|
+
# (read/grep/glob…) in the same turn, so the pending calls are
|
|
171
|
+
# safe to EXECUTE concurrently. Results are then POSTed
|
|
172
|
+
# sequentially: the ``app_cli_messages`` seq trigger computes
|
|
173
|
+
# ``MAX(seq)+1``, so concurrent inserts on the same chat race
|
|
174
|
+
# into a unique-constraint 500. Execution is the slow part;
|
|
175
|
+
# the posts are cheap.
|
|
176
|
+
async def _execute(
|
|
177
|
+
tcid: str,
|
|
178
|
+
meta: dict[str, Any],
|
|
179
|
+
) -> tuple[str, dict[str, Any], str]:
|
|
180
|
+
handler = self._registry.handler_for(meta["name"])
|
|
181
|
+
if not hooks:
|
|
182
|
+
return tcid, meta, await handler(meta["input"], ctx)
|
|
183
|
+
payload = {"session_id": self._chat_id, "tool_name": meta["name"], "tool_input": meta["input"]}
|
|
184
|
+
before = await run_hooks("PreToolUse", payload, cwd, tool_name=meta["name"])
|
|
185
|
+
notices.extend(before.failures)
|
|
186
|
+
if before.blocked is not None:
|
|
187
|
+
return tcid, meta, f"A PreToolUse hook blocked this call:\n{before.blocked}"
|
|
188
|
+
result = await handler(meta["input"], ctx)
|
|
189
|
+
after = await run_hooks(
|
|
190
|
+
"PostToolUse", {**payload, "tool_response": result}, cwd, tool_name=meta["name"]
|
|
191
|
+
)
|
|
192
|
+
notices.extend(after.failures)
|
|
193
|
+
if after.blocked is not None:
|
|
194
|
+
result = f"{result}\n\nA PostToolUse hook says:\n{after.blocked}"
|
|
195
|
+
return tcid, meta, result
|
|
196
|
+
|
|
197
|
+
outcomes = await asyncio.gather(
|
|
198
|
+
*starmap(_execute, pending.items()),
|
|
199
|
+
return_exceptions=True,
|
|
200
|
+
)
|
|
201
|
+
for failure in notices:
|
|
202
|
+
yield _notice(failure)
|
|
203
|
+
notices.clear()
|
|
204
|
+
for outcome in outcomes:
|
|
205
|
+
if isinstance(outcome, BaseException):
|
|
206
|
+
raise outcome
|
|
207
|
+
tcid, meta, result = outcome
|
|
208
|
+
await self._transport.submit_tool_result(
|
|
209
|
+
self._chat_id,
|
|
210
|
+
tcid,
|
|
211
|
+
meta["name"],
|
|
212
|
+
result,
|
|
213
|
+
)
|
|
214
|
+
if emit_tool_output:
|
|
215
|
+
yield {
|
|
216
|
+
"type": "tool-output-available",
|
|
217
|
+
"toolCallId": tcid,
|
|
218
|
+
"output": result,
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
# ``git_context`` is dropped: the backend froze the turn's snapshot
|
|
222
|
+
# onto the user message on the first request and replays it from
|
|
223
|
+
# there, so re-sending a fresh one would be ignored at best.
|
|
224
|
+
# ``custom_instructions`` stays — the system prompt is re-rendered
|
|
225
|
+
# on every request and reads it.
|
|
226
|
+
current_body = {
|
|
227
|
+
**{k: v for k, v in body.items() if k != "git_context"},
|
|
228
|
+
"messages": [],
|
|
229
|
+
"tool_continuation": True,
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async def stop(self) -> None:
|
|
233
|
+
"""Set the backend stop signal. Idempotent."""
|
|
234
|
+
await self._transport.stop(self._chat_id)
|
|
235
|
+
|
|
236
|
+
async def clear_chat(self) -> None:
|
|
237
|
+
"""Delete the chat row on the backend."""
|
|
238
|
+
await self._transport.clear_chat(self._chat_id)
|
|
239
|
+
|
|
240
|
+
async def queue_message(self, content: str) -> str:
|
|
241
|
+
"""Add a message to the FIFO queue. Returns the new message id."""
|
|
242
|
+
return await self._transport.queue_message(self._chat_id, content)
|
|
243
|
+
|
|
244
|
+
async def list_queue(self) -> list[dict[str, Any]]:
|
|
245
|
+
"""Return the current queued-messages list."""
|
|
246
|
+
return await self._transport.list_queue(self._chat_id)
|
|
247
|
+
|
|
248
|
+
async def edit_queued(self, message_id: str, content: str) -> None:
|
|
249
|
+
"""Edit a queued message in place."""
|
|
250
|
+
await self._transport.edit_queued(self._chat_id, message_id, content)
|
|
251
|
+
|
|
252
|
+
async def remove_queued(self, message_id: str) -> None:
|
|
253
|
+
"""Remove a queued message."""
|
|
254
|
+
await self._transport.remove_queued(self._chat_id, message_id)
|
|
255
|
+
|
|
256
|
+
async def steer(self, content: str, *, message_id: str | None = None) -> str | None:
|
|
257
|
+
"""Send a message the agent reads at its next tool boundary. Returns its id.
|
|
258
|
+
|
|
259
|
+
``None`` once the turn is over (:meth:`take_back_steers` ran): the
|
|
260
|
+
caller keeps the message for the next turn.
|
|
261
|
+
"""
|
|
262
|
+
async with self._steer_lock:
|
|
263
|
+
if self._steers_closed:
|
|
264
|
+
return None
|
|
265
|
+
message_id = message_id or str(uuid.uuid4())
|
|
266
|
+
await self._transport.inject_message(self._chat_id, message_id, content)
|
|
267
|
+
self.pending_steers[message_id] = content
|
|
268
|
+
return message_id
|
|
269
|
+
|
|
270
|
+
async def take_back_steers(self) -> list[str]:
|
|
271
|
+
"""End the turn's steering: withdraw what the agent has not taken, oldest first.
|
|
272
|
+
|
|
273
|
+
Called when the turn ends — the agent finished or was stopped before
|
|
274
|
+
its next tool boundary. A steer it took meanwhile (a 404 here) is gone
|
|
275
|
+
from the result: it is in the conversation already. Later steers are
|
|
276
|
+
refused (:meth:`steer` returns ``None``).
|
|
277
|
+
"""
|
|
278
|
+
async with self._steer_lock:
|
|
279
|
+
self._steers_closed = True
|
|
280
|
+
pending, self.pending_steers = self.pending_steers, {}
|
|
281
|
+
return [
|
|
282
|
+
content
|
|
283
|
+
for message_id, content in pending.items()
|
|
284
|
+
if await self._transport.remove_injected(self._chat_id, message_id)
|
|
285
|
+
]
|
|
286
|
+
|
|
287
|
+
async def remove_steer(self, message_id: str) -> bool:
|
|
288
|
+
"""Withdraw one steer. False when the agent already took it."""
|
|
289
|
+
async with self._steer_lock:
|
|
290
|
+
if self.pending_steers.pop(message_id, None) is None:
|
|
291
|
+
return False
|
|
292
|
+
return await self._transport.remove_injected(self._chat_id, message_id)
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def _notice(message: str) -> dict[str, Any]:
|
|
296
|
+
return {"type": HOOK_NOTICE, "data": {"message": message}}
|
|
@@ -8,8 +8,11 @@ exposes via ``create_agent_router``.
|
|
|
8
8
|
from __future__ import annotations
|
|
9
9
|
|
|
10
10
|
import uuid
|
|
11
|
+
from http import HTTPStatus
|
|
11
12
|
from typing import TYPE_CHECKING, Any
|
|
12
13
|
|
|
14
|
+
from delos_cli.transport.client import TransportError
|
|
15
|
+
|
|
13
16
|
if TYPE_CHECKING:
|
|
14
17
|
from collections.abc import AsyncIterator
|
|
15
18
|
|
|
@@ -104,3 +107,21 @@ class AgentTransport:
|
|
|
104
107
|
"""``DELETE /queue/{org}/{chat_id}/{msg_id}``."""
|
|
105
108
|
path = f"{self._path('queue', chat_id)}/{message_id}"
|
|
106
109
|
await self._http.json_request("DELETE", path, None)
|
|
110
|
+
|
|
111
|
+
async def inject_message(self, chat_id: str, message_id: str, content: str) -> None:
|
|
112
|
+
"""``POST /inject/{org}/{chat_id}`` — reaches the agent at its next tool boundary."""
|
|
113
|
+
await self._http.json_post(
|
|
114
|
+
self._path("inject", chat_id),
|
|
115
|
+
{"message_id": message_id, "content": content},
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
async def remove_injected(self, chat_id: str, message_id: str) -> bool:
|
|
119
|
+
"""``DELETE /inject/{org}/{chat_id}/{msg_id}`` — False when the agent already took it."""
|
|
120
|
+
path = f"{self._path('inject', chat_id)}/{message_id}"
|
|
121
|
+
try:
|
|
122
|
+
await self._http.json_request("DELETE", path, None)
|
|
123
|
+
except TransportError as e:
|
|
124
|
+
if e.status == HTTPStatus.NOT_FOUND:
|
|
125
|
+
return False
|
|
126
|
+
raise
|
|
127
|
+
return True
|
|
@@ -99,12 +99,22 @@ class App(ABC):
|
|
|
99
99
|
"""
|
|
100
100
|
_ = self, ctx
|
|
101
101
|
|
|
102
|
-
async def
|
|
103
|
-
"""
|
|
102
|
+
async def steer(self, ctx: Ctx, content: str) -> bool:
|
|
103
|
+
"""Send a message to the in-flight turn, for the agent to read at its next tool call.
|
|
104
104
|
|
|
105
|
-
Default returns ``False``
|
|
106
|
-
|
|
107
|
-
|
|
105
|
+
Default returns ``False`` — the caller then holds the message until
|
|
106
|
+
the turn ends. Apps that wrap a real agent inject it and return
|
|
107
|
+
``True`` on success.
|
|
108
108
|
"""
|
|
109
109
|
_ = self, ctx, content
|
|
110
110
|
return False
|
|
111
|
+
|
|
112
|
+
def pending_steers(self) -> list[str]:
|
|
113
|
+
"""Steers the in-flight turn has not reached yet, oldest first."""
|
|
114
|
+
_ = self
|
|
115
|
+
return []
|
|
116
|
+
|
|
117
|
+
async def take_back_steers(self) -> list[str]:
|
|
118
|
+
"""Withdraw the steers the in-flight turn has not reached; returns them, oldest first."""
|
|
119
|
+
_ = self
|
|
120
|
+
return []
|
|
@@ -13,17 +13,22 @@ renderers / handlers per tool). The :class:`V6Renderer` returned from
|
|
|
13
13
|
from __future__ import annotations
|
|
14
14
|
|
|
15
15
|
import contextlib
|
|
16
|
+
import uuid
|
|
16
17
|
from dataclasses import dataclass, field
|
|
17
18
|
from pathlib import Path
|
|
18
19
|
from typing import TYPE_CHECKING, Any, override
|
|
19
20
|
|
|
20
21
|
from rich.text import Text
|
|
21
22
|
|
|
23
|
+
from delos_cli import sandbox
|
|
22
24
|
from delos_cli.agent import AgentSession, AgentTransport, ToolRegistry
|
|
23
25
|
from delos_cli.apps.base import App
|
|
24
26
|
from delos_cli.apps.replay import replay_messages
|
|
27
|
+
from delos_cli.checkpoints import history_for
|
|
28
|
+
from delos_cli.commands import GLOBAL_COMMANDS
|
|
25
29
|
from delos_cli.git_context import collect_git_context
|
|
26
30
|
from delos_cli.mcp import McpManager, register_tools
|
|
31
|
+
from delos_cli.skills import skill_commands, skill_specs
|
|
27
32
|
from delos_cli.state import TodoStore
|
|
28
33
|
from delos_cli.tools import (
|
|
29
34
|
handle_ask_user_input,
|
|
@@ -35,6 +40,7 @@ from delos_cli.tools import (
|
|
|
35
40
|
handle_read,
|
|
36
41
|
handle_run_shell,
|
|
37
42
|
handle_run_tests,
|
|
43
|
+
handle_shell_output,
|
|
38
44
|
handle_todo_write,
|
|
39
45
|
handle_write_content,
|
|
40
46
|
render_ask_user_input,
|
|
@@ -46,6 +52,7 @@ from delos_cli.tools import (
|
|
|
46
52
|
render_read,
|
|
47
53
|
render_run_shell,
|
|
48
54
|
render_run_tests,
|
|
55
|
+
render_shell_output,
|
|
49
56
|
render_task,
|
|
50
57
|
render_todo_write,
|
|
51
58
|
render_write_content,
|
|
@@ -55,6 +62,7 @@ from delos_cli.transport.models import fetch_picker_models
|
|
|
55
62
|
|
|
56
63
|
from .commands import CHAT_COMMANDS
|
|
57
64
|
from .render import V6Renderer
|
|
65
|
+
from .rewind import REWIND
|
|
58
66
|
|
|
59
67
|
if TYPE_CHECKING:
|
|
60
68
|
from collections.abc import AsyncIterator
|
|
@@ -70,6 +78,8 @@ def default_tool_registry() -> ToolRegistry:
|
|
|
70
78
|
reg = ToolRegistry()
|
|
71
79
|
reg.handlers["run_shell"] = handle_run_shell
|
|
72
80
|
reg.renderers["run_shell"] = render_run_shell
|
|
81
|
+
reg.handlers["shell_output"] = handle_shell_output
|
|
82
|
+
reg.renderers["shell_output"] = render_shell_output
|
|
73
83
|
reg.handlers["write_content"] = handle_write_content
|
|
74
84
|
reg.renderers["write_content"] = render_write_content
|
|
75
85
|
reg.handlers["edit_content"] = handle_edit_content
|
|
@@ -110,7 +120,14 @@ class ChatApp(App):
|
|
|
110
120
|
model_titles: dict[str, str] = field(default_factory=dict)
|
|
111
121
|
#: Model new chats start on: the first model of the picker's top section, else its first model.
|
|
112
122
|
default_model: str | None = None
|
|
113
|
-
|
|
123
|
+
#: Built-ins, ``/rewind``, then one ``/<name>`` per skill of the working directory (:mod:`delos_cli.skills`).
|
|
124
|
+
commands: dict[str, CommandSpec] = field(
|
|
125
|
+
default_factory=lambda: {
|
|
126
|
+
**skill_commands(Path.cwd(), Path.home(), taken=set(CHAT_COMMANDS) | set(GLOBAL_COMMANDS) | {REWIND.name}),
|
|
127
|
+
**CHAT_COMMANDS,
|
|
128
|
+
REWIND.name: REWIND,
|
|
129
|
+
},
|
|
130
|
+
)
|
|
114
131
|
#: Per-tool renderers + handlers. Built-in entries cover the
|
|
115
132
|
#: client-side tools the backend exposes (``run_shell`` for now).
|
|
116
133
|
#: Override / extend by mutating ``tools.handlers`` and
|
|
@@ -138,6 +155,11 @@ class ChatApp(App):
|
|
|
138
155
|
first = top or (models[0] if models else None)
|
|
139
156
|
self.default_model = first["id"] if first else None
|
|
140
157
|
|
|
158
|
+
@property
|
|
159
|
+
def busy(self) -> bool:
|
|
160
|
+
"""Whether a turn is running."""
|
|
161
|
+
return self._current_session is not None
|
|
162
|
+
|
|
141
163
|
def model_label(self, model: str) -> str:
|
|
142
164
|
"""Gate's display name for ``model``, or the slug itself when unknown."""
|
|
143
165
|
return self.model_titles.get(model, model)
|
|
@@ -171,6 +193,12 @@ class ChatApp(App):
|
|
|
171
193
|
transport = AgentTransport(ctx.http, "cli", ctx.cfg.org_uuid)
|
|
172
194
|
session = AgentSession(transport, ctx.state.conv_id, self.tools)
|
|
173
195
|
|
|
196
|
+
# The user message's id rides along into its stored row: a rewind names
|
|
197
|
+
# the turn by it, and the agent's file writes from now on are its.
|
|
198
|
+
message_id = uuid.uuid4().hex
|
|
199
|
+
messages = [{**m, "messageId": message_id} if m.get("role") == "user" else m for m in messages]
|
|
200
|
+
history_for(ctx.state.conv_id).begin_turn(message_id)
|
|
201
|
+
|
|
174
202
|
# Backend reads model + everything else from ``app_cli``; we only
|
|
175
203
|
# send what's strictly per-turn.
|
|
176
204
|
body: dict[str, Any] = {
|
|
@@ -180,13 +208,19 @@ class ChatApp(App):
|
|
|
180
208
|
}
|
|
181
209
|
if ctx.custom_instructions:
|
|
182
210
|
body["custom_instructions"] = ctx.custom_instructions
|
|
211
|
+
# Names, descriptions and paths only: the agent reads a SKILL.md when a task needs it.
|
|
212
|
+
if skills := skill_specs(Path.cwd(), Path.home()):
|
|
213
|
+
body["skills"] = skills
|
|
183
214
|
# Fresh git snapshot per turn — rendered into the system prompt so
|
|
184
215
|
# the agent doesn't burn its first tool call on `git status`.
|
|
185
216
|
if (git_ctx := await collect_git_context(Path.cwd())) is not None:
|
|
186
217
|
body["git_context"] = git_ctx
|
|
218
|
+
# Rendered into ``<turn_context>``; rides every request of the turn, like ``mode``.
|
|
219
|
+
body["sandbox"] = sandbox.active(Path.cwd()).turn_context()
|
|
187
220
|
# Local MCP tools ride in every request of the turn (continuations
|
|
188
|
-
# re-send the body)
|
|
189
|
-
|
|
221
|
+
# re-send the body) and in every mode — the tool list must not change
|
|
222
|
+
# with it; Plan mode refuses their calls (``refused_in_plan``).
|
|
223
|
+
if self.mcp is not None:
|
|
190
224
|
specs = await self.mcp.client_tool_specs()
|
|
191
225
|
if specs:
|
|
192
226
|
register_tools(self.tools, self.mcp, specs)
|
|
@@ -202,7 +236,7 @@ class ChatApp(App):
|
|
|
202
236
|
# 🔎 explore line hangs with no closing ✓. Matches the serve.py
|
|
203
237
|
# (VSCode) path. Input-only renderers (read/grep/glob/run_shell)
|
|
204
238
|
# return None on the output event, so nothing double-renders.
|
|
205
|
-
async for event in session.send_turn(body, ctx, emit_tool_output=True):
|
|
239
|
+
async for event in session.send_turn(body, ctx, emit_tool_output=True, hooks=True):
|
|
206
240
|
yield event
|
|
207
241
|
finally:
|
|
208
242
|
self._current_session = None
|
|
@@ -227,16 +261,30 @@ class ChatApp(App):
|
|
|
227
261
|
await self._current_session.stop()
|
|
228
262
|
|
|
229
263
|
@override
|
|
230
|
-
async def
|
|
231
|
-
"""
|
|
264
|
+
async def steer(self, ctx: Ctx, content: str) -> bool:
|
|
265
|
+
"""Inject a message into the running turn; it reaches the agent at its next tool call."""
|
|
232
266
|
_ = ctx
|
|
233
267
|
if self._current_session is None:
|
|
234
268
|
return False
|
|
235
269
|
try:
|
|
236
|
-
await self._current_session.
|
|
270
|
+
return await self._current_session.steer(content) is not None
|
|
237
271
|
except TransportError:
|
|
238
272
|
return False
|
|
239
|
-
|
|
273
|
+
|
|
274
|
+
@override
|
|
275
|
+
def pending_steers(self) -> list[str]:
|
|
276
|
+
"""Steers sent to the running turn that the agent has not taken yet."""
|
|
277
|
+
return list(self._current_session.pending_steers.values()) if self._current_session else []
|
|
278
|
+
|
|
279
|
+
@override
|
|
280
|
+
async def take_back_steers(self) -> list[str]:
|
|
281
|
+
"""Withdraw the running turn's untaken steers (best-effort: a failed withdrawal keeps nothing)."""
|
|
282
|
+
if self._current_session is None:
|
|
283
|
+
return []
|
|
284
|
+
try:
|
|
285
|
+
return await self._current_session.take_back_steers()
|
|
286
|
+
except TransportError:
|
|
287
|
+
return []
|
|
240
288
|
|
|
241
289
|
@override
|
|
242
290
|
def toolbar_fragment(self, ctx: Ctx) -> str:
|
|
@@ -67,7 +67,7 @@ async def _pick_model(ctx: Ctx) -> None:
|
|
|
67
67
|
return
|
|
68
68
|
|
|
69
69
|
titles: dict[str, str] = getattr(ctx.app, "model_titles", {})
|
|
70
|
-
options = [PickOption(m, titles.get(m, m)
|
|
70
|
+
options = [PickOption(m, titles.get(m, m)) for m in models]
|
|
71
71
|
chosen = await ctx.pick("Select a model", options, ctx.state.model)
|
|
72
72
|
if chosen is None or chosen == ctx.state.model:
|
|
73
73
|
return
|