talkops-opscloud 0.3.0b1__py3-none-any.whl
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.
- opscloud/__init__.py +5 -0
- opscloud/__main__.py +6 -0
- opscloud/_constants.py +53 -0
- opscloud/_debug.py +145 -0
- opscloud/_debug_buffer.py +250 -0
- opscloud/_textual_patches.py +367 -0
- opscloud/_version.py +15 -0
- opscloud/agent/__init__.py +27 -0
- opscloud/agent/config.py +168 -0
- opscloud/agent/factory.py +1103 -0
- opscloud/approval_mode.py +44 -0
- opscloud/backend/__init__.py +49 -0
- opscloud/backend/composite.py +89 -0
- opscloud/backend/local.py +183 -0
- opscloud/backend/registry.py +79 -0
- opscloud/backend/sandbox_config.py +107 -0
- opscloud/backend/sandbox_factory.py +432 -0
- opscloud/built_in_skills/remember/SKILL.md +124 -0
- opscloud/built_in_skills/skill-creator/SKILL.md +348 -0
- opscloud/built_in_skills/skill-creator/scripts/init_skill.py +371 -0
- opscloud/built_in_skills/skill-creator/scripts/quick_validate.py +165 -0
- opscloud/cli/__init__.py +1 -0
- opscloud/cli/commands/__init__.py +15 -0
- opscloud/cli/commands/auth.py +45 -0
- opscloud/cli/commands/doctor.py +100 -0
- opscloud/cli/commands/mcp.py +137 -0
- opscloud/cli/commands/threads.py +51 -0
- opscloud/cli/interactive.py +154 -0
- opscloud/cli/main.py +903 -0
- opscloud/cli/non_interactive.py +425 -0
- opscloud/cli/server_manager.py +58 -0
- opscloud/commands/__init__.py +19 -0
- opscloud/commands/_base.py +110 -0
- opscloud/commands/_guards.py +52 -0
- opscloud/commands/_router.py +146 -0
- opscloud/commands/_types.py +36 -0
- opscloud/commands/core/__init__.py +47 -0
- opscloud/commands/core/auth.py +94 -0
- opscloud/commands/core/bug.py +46 -0
- opscloud/commands/core/clear.py +106 -0
- opscloud/commands/core/cloud.py +185 -0
- opscloud/commands/core/compact.py +128 -0
- opscloud/commands/core/config_cmd.py +120 -0
- opscloud/commands/core/context.py +220 -0
- opscloud/commands/core/cost.py +44 -0
- opscloud/commands/core/doctor.py +535 -0
- opscloud/commands/core/effort.py +108 -0
- opscloud/commands/core/exit_cmd.py +41 -0
- opscloud/commands/core/fast.py +148 -0
- opscloud/commands/core/help_cmd.py +94 -0
- opscloud/commands/core/mcp.py +134 -0
- opscloud/commands/core/model.py +109 -0
- opscloud/commands/core/permissions.py +196 -0
- opscloud/commands/core/plugins.py +92 -0
- opscloud/commands/core/pool.py +108 -0
- opscloud/commands/core/resume.py +97 -0
- opscloud/commands/core/skills.py +178 -0
- opscloud/commands/power/__init__.py +51 -0
- opscloud/commands/power/agents.py +98 -0
- opscloud/commands/power/btw.py +85 -0
- opscloud/commands/power/copy.py +97 -0
- opscloud/commands/power/goal.py +548 -0
- opscloud/commands/power/loop.py +295 -0
- opscloud/commands/power/memory.py +287 -0
- opscloud/commands/power/review.py +152 -0
- opscloud/commands/power/rubric.py +287 -0
- opscloud/commands/power/runtime.py +321 -0
- opscloud/commands/power/skill_creator.py +70 -0
- opscloud/commands/power/skill_invoke.py +165 -0
- opscloud/commands/power/tasks.py +209 -0
- opscloud/commands/power/trace.py +117 -0
- opscloud/commands/power/ui_toggles.py +135 -0
- opscloud/commands/power/version.py +52 -0
- opscloud/commands/registry.py +176 -0
- opscloud/config/__init__.py +114 -0
- opscloud/config/adapters/__init__.py +5 -0
- opscloud/config/adapters/postgres.py +776 -0
- opscloud/config/adapters/sqlite.py +769 -0
- opscloud/config/aws.py +248 -0
- opscloud/config/cloud_profiles.py +437 -0
- opscloud/config/env_vars.py +81 -0
- opscloud/config/langsmith.py +319 -0
- opscloud/config/manifest.py +817 -0
- opscloud/config/metadata.py +205 -0
- opscloud/config/paths.py +604 -0
- opscloud/config/plugins.py +130 -0
- opscloud/config/settings.py +834 -0
- opscloud/config/store.py +453 -0
- opscloud/config/store_factory.py +97 -0
- opscloud/config/toml_config.py +562 -0
- opscloud/editor.py +173 -0
- opscloud/exceptions.py +65 -0
- opscloud/file_ops.py +217 -0
- opscloud/hooks/__init__.py +139 -0
- opscloud/hooks/env.py +44 -0
- opscloud/hooks/interrupt.py +121 -0
- opscloud/hooks/models/__init__.py +109 -0
- opscloud/hooks/models/adapters.py +19 -0
- opscloud/hooks/models/config.py +79 -0
- opscloud/hooks/models/domain.py +434 -0
- opscloud/hooks/models/transport.py +37 -0
- opscloud/hooks/tools.py +231 -0
- opscloud/input.py +48 -0
- opscloud/integrations/__init__.py +80 -0
- opscloud/integrations/base.py +153 -0
- opscloud/integrations/content_filter.py +119 -0
- opscloud/integrations/event_bus.py +480 -0
- opscloud/integrations/hooks.py +238 -0
- opscloud/integrations/identity.py +181 -0
- opscloud/integrations/notifications.py +134 -0
- opscloud/integrations/stream_bridge.py +397 -0
- opscloud/integrations/thread_store.py +81 -0
- opscloud/json_types.py +20 -0
- opscloud/mcp/__init__.py +57 -0
- opscloud/mcp/config.py +203 -0
- opscloud/mcp/discovery.py +229 -0
- opscloud/mcp/mcp_info.py +109 -0
- opscloud/mcp/middleware.py +102 -0
- opscloud/mcp/preload.py +505 -0
- opscloud/mcp/raw_config.py +149 -0
- opscloud/mcp/semantic_profiler.py +305 -0
- opscloud/mcp/session_manager.py +727 -0
- opscloud/mcp/trust.py +105 -0
- opscloud/memory/__init__.py +27 -0
- opscloud/memory/branch.py +61 -0
- opscloud/memory/guard.py +19 -0
- opscloud/memory/onboarding.py +120 -0
- opscloud/memory/registry.py +134 -0
- opscloud/memory/store.py +223 -0
- opscloud/middleware/__init__.py +106 -0
- opscloud/middleware/_repository_bounds.py +459 -0
- opscloud/middleware/ask_user.py +152 -0
- opscloud/middleware/auto_mode.py +60 -0
- opscloud/middleware/auto_mode_hitl.py +2434 -0
- opscloud/middleware/compaction.py +566 -0
- opscloud/middleware/configurable_model.py +233 -0
- opscloud/middleware/cost_tracking.py +1021 -0
- opscloud/middleware/glm_stall_recovery.py +109 -0
- opscloud/middleware/goal_criteria.py +1234 -0
- opscloud/middleware/goal_state_notice.py +712 -0
- opscloud/middleware/goal_tools.py +407 -0
- opscloud/middleware/headless_mcp_guard.py +88 -0
- opscloud/middleware/jev_model_router.py +1018 -0
- opscloud/middleware/local_context.py +460 -0
- opscloud/middleware/mcp_context.py +151 -0
- opscloud/middleware/mcp_middleware.py +146 -0
- opscloud/middleware/memory_guard.py +393 -0
- opscloud/middleware/model_retry.py +205 -0
- opscloud/middleware/registry.py +137 -0
- opscloud/middleware/reliable_rubric.py +709 -0
- opscloud/middleware/resume_state.py +91 -0
- opscloud/middleware/server_hooks.py +1189 -0
- opscloud/middleware/shell_allow_list.py +90 -0
- opscloud/middleware/skills.py +382 -0
- opscloud/middleware/subagent_artifacts.py +84 -0
- opscloud/middleware/subagent_telemetry.py +261 -0
- opscloud/middleware/subagents.py +243 -0
- opscloud/middleware/tool_filter.py +384 -0
- opscloud/middleware/unified_system_message.py +62 -0
- opscloud/model/__init__.py +37 -0
- opscloud/model/config.py +2992 -0
- opscloud/model/factory.py +477 -0
- opscloud/model/pool.py +459 -0
- opscloud/model/reasoning.py +282 -0
- opscloud/offload.py +173 -0
- opscloud/output.py +64 -0
- opscloud/plugins/__init__.py +189 -0
- opscloud/plugins/_json.py +22 -0
- opscloud/plugins/adapters/__init__.py +13 -0
- opscloud/plugins/adapters/agents.py +112 -0
- opscloud/plugins/adapters/commands.py +190 -0
- opscloud/plugins/adapters/mcp.py +363 -0
- opscloud/plugins/adapters/skills.py +132 -0
- opscloud/plugins/commands_cli.py +541 -0
- opscloud/plugins/discovery.py +512 -0
- opscloud/plugins/manifest.py +335 -0
- opscloud/plugins/marketplace.py +893 -0
- opscloud/plugins/models.py +334 -0
- opscloud/plugins/project_plugins.py +317 -0
- opscloud/plugins/protocol.py +31 -0
- opscloud/plugins/store.py +905 -0
- opscloud/plugins/substitution.py +122 -0
- opscloud/project_utils.py +145 -0
- opscloud/prompts/__init__.py +37 -0
- opscloud/prompts/system.py +483 -0
- opscloud/prompts/templates/system_prompt.md +166 -0
- opscloud/prompts/types.py +54 -0
- opscloud/rubrics/__init__.py +42 -0
- opscloud/rubrics/evaluator.py +404 -0
- opscloud/rubrics/evidence_extractor.py +229 -0
- opscloud/rubrics/generator.py +239 -0
- opscloud/rubrics/jev_compiler.py +175 -0
- opscloud/rubrics/jev_grader.py +297 -0
- opscloud/rubrics/middleware.py +8 -0
- opscloud/schema/__init__.py +107 -0
- opscloud/schema/interrupts.py +981 -0
- opscloud/security/__init__.py +91 -0
- opscloud/security/approval_mode.py +528 -0
- opscloud/security/approval_mode_source.py +230 -0
- opscloud/security/cli_ast_evaluator.py +552 -0
- opscloud/security/jev_classifier.py +651 -0
- opscloud/security/shell_safety.py +340 -0
- opscloud/security/unicode_security.py +439 -0
- opscloud/security/url_validation.py +160 -0
- opscloud/server/__init__.py +13 -0
- opscloud/server/_server_config.py +158 -0
- opscloud/server/server.py +242 -0
- opscloud/server/server_graph.py +93 -0
- opscloud/skills/__init__.py +91 -0
- opscloud/skills/commands.py +1099 -0
- opscloud/skills/invocation.py +200 -0
- opscloud/skills/load.py +388 -0
- opscloud/skills/loader.py +23 -0
- opscloud/skills/merge.py +65 -0
- opscloud/skills/registry.py +263 -0
- opscloud/skills/sources.py +174 -0
- opscloud/skills/trust.py +417 -0
- opscloud/state/__init__.py +142 -0
- opscloud/state/base.py +154 -0
- opscloud/state/cloud_context.py +262 -0
- opscloud/state/goal_channels.py +124 -0
- opscloud/state/goal_state_limits.py +175 -0
- opscloud/state/resume_state.py +58 -0
- opscloud/state/service_context.py +281 -0
- opscloud/state/session.py +957 -0
- opscloud/state/state_migration.py +86 -0
- opscloud/subagents/__init__.py +30 -0
- opscloud/subagents/loader.py +331 -0
- opscloud/subagents/subagents_parser.py +117 -0
- opscloud/subagents/types.py +25 -0
- opscloud/tools/__init__.py +36 -0
- opscloud/tools/catalog.py +37 -0
- opscloud/tools/display.py +24 -0
- opscloud/tools/fetch_url.py +176 -0
- opscloud/tools/goal_tools.py +423 -0
- opscloud/tools/registry.py +105 -0
- opscloud/tools/thread.py +32 -0
- opscloud/tools/web_search.py +215 -0
- opscloud/ui/__init__.py +119 -0
- opscloud/ui/app.py +5400 -0
- opscloud/ui/app.tcss +479 -0
- opscloud/ui/clipboard.py +147 -0
- opscloud/ui/command_registry.py +482 -0
- opscloud/ui/message_store.py +152 -0
- opscloud/ui/permission_store.py +360 -0
- opscloud/ui/preferences.py +113 -0
- opscloud/ui/prompt_manager.py +96 -0
- opscloud/ui/remote_client.py +505 -0
- opscloud/ui/textual_adapter.py +1269 -0
- opscloud/ui/theme.py +728 -0
- opscloud/ui/ui_help.py +415 -0
- opscloud/ui/widgets/__init__.py +383 -0
- opscloud/ui/widgets/_ask_user_types.py +81 -0
- opscloud/ui/widgets/_copy_spans.py +52 -0
- opscloud/ui/widgets/_inline_prompt.py +290 -0
- opscloud/ui/widgets/_js_eval_display.py +139 -0
- opscloud/ui/widgets/_links.py +175 -0
- opscloud/ui/widgets/_tool_stream.py +308 -0
- opscloud/ui/widgets/agent_selector.py +297 -0
- opscloud/ui/widgets/approval.py +741 -0
- opscloud/ui/widgets/ask_user.py +539 -0
- opscloud/ui/widgets/auth.py +695 -0
- opscloud/ui/widgets/auth_manager.py +214 -0
- opscloud/ui/widgets/auto_mode_notice.py +146 -0
- opscloud/ui/widgets/autocomplete.py +709 -0
- opscloud/ui/widgets/chat_input.py +805 -0
- opscloud/ui/widgets/cloud_selector.py +528 -0
- opscloud/ui/widgets/config_manager.py +652 -0
- opscloud/ui/widgets/debug_console.py +1269 -0
- opscloud/ui/widgets/devops_renderers.py +184 -0
- opscloud/ui/widgets/diff.py +237 -0
- opscloud/ui/widgets/effort_selector.py +166 -0
- opscloud/ui/widgets/goal_review.py +612 -0
- opscloud/ui/widgets/goal_status.py +57 -0
- opscloud/ui/widgets/infra_panel.py +141 -0
- opscloud/ui/widgets/install_confirm.py +114 -0
- opscloud/ui/widgets/loading.py +170 -0
- opscloud/ui/widgets/mcp_viewer.py +1204 -0
- opscloud/ui/widgets/messages.py +4461 -0
- opscloud/ui/widgets/model_selector.py +863 -0
- opscloud/ui/widgets/notification_center.py +108 -0
- opscloud/ui/widgets/notification_settings.py +119 -0
- opscloud/ui/widgets/operation_card.py +106 -0
- opscloud/ui/widgets/permissions_manager.py +667 -0
- opscloud/ui/widgets/plugin_manager.py +1212 -0
- opscloud/ui/widgets/pool_selector.py +376 -0
- opscloud/ui/widgets/preamble.py +53 -0
- opscloud/ui/widgets/skills_viewer.py +432 -0
- opscloud/ui/widgets/status.py +1037 -0
- opscloud/ui/widgets/subagent_panel.py +927 -0
- opscloud/ui/widgets/theme_selector.py +232 -0
- opscloud/ui/widgets/thread_selector.py +358 -0
- opscloud/ui/widgets/toast.py +31 -0
- opscloud/ui/widgets/tool_display.py +325 -0
- opscloud/ui/widgets/tool_renderers.py +234 -0
- opscloud/ui/widgets/tool_widgets.py +278 -0
- opscloud/ui/widgets/welcome.py +421 -0
- opscloud/ui/widgets/welcome_popup.py +102 -0
- opscloud/utils/__init__.py +24 -0
- opscloud/utils/cost_estimation.py +169 -0
- opscloud/utils/git.py +390 -0
- opscloud/utils/logger.py +571 -0
- opscloud/utils/session_stats.py +115 -0
- opscloud/utils/startup_error.py +37 -0
- talkops_opscloud-0.3.0b1.dist-info/METADATA +449 -0
- talkops_opscloud-0.3.0b1.dist-info/RECORD +309 -0
- talkops_opscloud-0.3.0b1.dist-info/WHEEL +4 -0
- talkops_opscloud-0.3.0b1.dist-info/entry_points.txt +2 -0
- talkops_opscloud-0.3.0b1.dist-info/licenses/LICENSE +175 -0
|
@@ -0,0 +1,566 @@
|
|
|
1
|
+
"""CLI conversation compaction middleware for context window optimization."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
from functools import partial
|
|
7
|
+
from typing import TYPE_CHECKING, Annotated, Any, NamedTuple, cast
|
|
8
|
+
from uuid import uuid4
|
|
9
|
+
|
|
10
|
+
from deepagents.backends.protocol import (
|
|
11
|
+
FILE_NOT_FOUND,
|
|
12
|
+
BackendProtocol,
|
|
13
|
+
EditResult,
|
|
14
|
+
FileDownloadResponse,
|
|
15
|
+
WriteResult,
|
|
16
|
+
)
|
|
17
|
+
from deepagents.middleware.summarization import (
|
|
18
|
+
SummarizationMiddleware,
|
|
19
|
+
SummarizationToolMiddleware,
|
|
20
|
+
create_summarization_middleware,
|
|
21
|
+
create_summarization_tool_middleware,
|
|
22
|
+
)
|
|
23
|
+
from langchain.agents.middleware.types import (
|
|
24
|
+
ModelRequest,
|
|
25
|
+
ModelResponse,
|
|
26
|
+
)
|
|
27
|
+
from langchain.tools import ToolRuntime
|
|
28
|
+
from langchain_core.messages import ToolMessage
|
|
29
|
+
from langchain_core.tools import InjectedToolArg, StructuredTool
|
|
30
|
+
from langgraph.types import Command
|
|
31
|
+
|
|
32
|
+
if TYPE_CHECKING:
|
|
33
|
+
from collections.abc import Awaitable, Callable
|
|
34
|
+
|
|
35
|
+
from langchain_core.language_models import BaseChatModel
|
|
36
|
+
from langgraph.prebuilt.tool_node import ToolCallRequest
|
|
37
|
+
|
|
38
|
+
import contextlib
|
|
39
|
+
|
|
40
|
+
from opscloud.utils.logger import get_logger
|
|
41
|
+
|
|
42
|
+
logger = get_logger(__name__)
|
|
43
|
+
|
|
44
|
+
COMPACTION_FAILURE_PREFIX = "Compaction failed"
|
|
45
|
+
_OFFLOAD_SEED_ID_PREFIX = "offload-seed-"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _offload_seed_message_id(tool_call_id: str) -> str:
|
|
49
|
+
return f"{_OFFLOAD_SEED_ID_PREFIX}{tool_call_id}"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _without_offload_seed(messages: list[Any], tool_call_id: str) -> list[Any]:
|
|
53
|
+
if not tool_call_id:
|
|
54
|
+
return messages
|
|
55
|
+
seed_id = _offload_seed_message_id(tool_call_id)
|
|
56
|
+
return [
|
|
57
|
+
message
|
|
58
|
+
for message in messages
|
|
59
|
+
if (message.get("id") if isinstance(message, dict) else getattr(message, "id", None)) != seed_id
|
|
60
|
+
]
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class RuntimeModelConfig(NamedTuple):
|
|
64
|
+
"""Active model configuration read from a tool runtime."""
|
|
65
|
+
|
|
66
|
+
model_spec: str | None
|
|
67
|
+
model_params: dict[str, Any]
|
|
68
|
+
profile_overrides: dict[str, Any]
|
|
69
|
+
context_limit: int | None
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _runtime_model_config(runtime: ToolRuntime[Any, Any]) -> RuntimeModelConfig:
|
|
73
|
+
"""Read the active model configuration from a tool runtime."""
|
|
74
|
+
context = runtime.context
|
|
75
|
+
if hasattr(context, "model_params"):
|
|
76
|
+
return RuntimeModelConfig(
|
|
77
|
+
model_spec=getattr(context, "model", None),
|
|
78
|
+
model_params=getattr(context, "model_params", {}),
|
|
79
|
+
profile_overrides=getattr(context, "profile_overrides", {}),
|
|
80
|
+
context_limit=getattr(context, "model_context_limit", None),
|
|
81
|
+
)
|
|
82
|
+
if isinstance(context, dict):
|
|
83
|
+
model = context.get("model")
|
|
84
|
+
params = context.get("model_params")
|
|
85
|
+
profile_overrides = context.get("profile_overrides")
|
|
86
|
+
context_limit = context.get("model_context_limit")
|
|
87
|
+
return RuntimeModelConfig(
|
|
88
|
+
model_spec=model if isinstance(model, str) else None,
|
|
89
|
+
model_params=dict(params) if isinstance(params, dict) else {},
|
|
90
|
+
profile_overrides=(dict(profile_overrides) if isinstance(profile_overrides, dict) else {}),
|
|
91
|
+
context_limit=context_limit if isinstance(context_limit, int) else None,
|
|
92
|
+
)
|
|
93
|
+
return RuntimeModelConfig(model_spec=None, model_params={}, profile_overrides={}, context_limit=None)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _offload_tool_call_id(context: object) -> str | None:
|
|
97
|
+
"""Extract offload tool call ID from context if present."""
|
|
98
|
+
if hasattr(context, "offload_tool_call_id"):
|
|
99
|
+
value = getattr(context, "offload_tool_call_id", None)
|
|
100
|
+
elif isinstance(context, dict):
|
|
101
|
+
value = context.get("offload_tool_call_id")
|
|
102
|
+
else:
|
|
103
|
+
value = None
|
|
104
|
+
return value if isinstance(value, str) and value else None
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
class _ArchiveReadGuard:
|
|
108
|
+
"""Context manager preventing concurrent reads during archive compaction."""
|
|
109
|
+
|
|
110
|
+
def __init__(self, backend: BackendProtocol) -> None:
|
|
111
|
+
"""Initialize _ArchiveReadGuard wrapping the given storage backend.
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
backend: Storage backend providing file operations.
|
|
115
|
+
"""
|
|
116
|
+
self._backend = backend
|
|
117
|
+
self._read_failed = False
|
|
118
|
+
|
|
119
|
+
def _record_response_errors(self, responses: list[FileDownloadResponse]) -> list[FileDownloadResponse]:
|
|
120
|
+
if any(response.error is not None and response.error != FILE_NOT_FOUND for response in responses):
|
|
121
|
+
self._read_failed = True
|
|
122
|
+
return responses
|
|
123
|
+
|
|
124
|
+
def _ensure_read_succeeded(self) -> None:
|
|
125
|
+
if self._read_failed:
|
|
126
|
+
msg = "archive read failed; refusing to overwrite existing history"
|
|
127
|
+
raise RuntimeError(msg)
|
|
128
|
+
|
|
129
|
+
def download_files(self, paths: list[str]) -> list[FileDownloadResponse]:
|
|
130
|
+
"""Download multiple files synchronously while tracking read failure states.
|
|
131
|
+
|
|
132
|
+
Args:
|
|
133
|
+
paths: List of file paths to download.
|
|
134
|
+
|
|
135
|
+
Returns:
|
|
136
|
+
List of FileDownloadResponse objects.
|
|
137
|
+
"""
|
|
138
|
+
try:
|
|
139
|
+
responses = self._backend.download_files(paths)
|
|
140
|
+
except Exception:
|
|
141
|
+
self._read_failed = True
|
|
142
|
+
raise
|
|
143
|
+
return self._record_response_errors(responses)
|
|
144
|
+
|
|
145
|
+
async def adownload_files(self, paths: list[str]) -> list[FileDownloadResponse]:
|
|
146
|
+
"""Download multiple files asynchronously while tracking read failure states.
|
|
147
|
+
|
|
148
|
+
Args:
|
|
149
|
+
paths: List of file paths to download.
|
|
150
|
+
|
|
151
|
+
Returns:
|
|
152
|
+
List of FileDownloadResponse objects.
|
|
153
|
+
"""
|
|
154
|
+
try:
|
|
155
|
+
responses = await self._backend.adownload_files(paths)
|
|
156
|
+
except Exception:
|
|
157
|
+
self._read_failed = True
|
|
158
|
+
raise
|
|
159
|
+
return self._record_response_errors(responses)
|
|
160
|
+
|
|
161
|
+
def write(self, file_path: str, content: str) -> WriteResult:
|
|
162
|
+
"""Write content synchronously only if preceding archive reads succeeded.
|
|
163
|
+
|
|
164
|
+
Args:
|
|
165
|
+
file_path: Destination file path.
|
|
166
|
+
content: File content string.
|
|
167
|
+
|
|
168
|
+
Returns:
|
|
169
|
+
WriteResult object.
|
|
170
|
+
"""
|
|
171
|
+
self._ensure_read_succeeded()
|
|
172
|
+
return self._backend.write(file_path, content)
|
|
173
|
+
|
|
174
|
+
async def awrite(self, file_path: str, content: str) -> WriteResult:
|
|
175
|
+
"""Write content asynchronously only if preceding archive reads succeeded.
|
|
176
|
+
|
|
177
|
+
Args:
|
|
178
|
+
file_path: Destination file path.
|
|
179
|
+
content: File content string.
|
|
180
|
+
|
|
181
|
+
Returns:
|
|
182
|
+
WriteResult object.
|
|
183
|
+
"""
|
|
184
|
+
self._ensure_read_succeeded()
|
|
185
|
+
return await self._backend.awrite(file_path, content)
|
|
186
|
+
|
|
187
|
+
def edit(
|
|
188
|
+
self,
|
|
189
|
+
file_path: str,
|
|
190
|
+
old_string: str,
|
|
191
|
+
new_string: str,
|
|
192
|
+
replace_all: bool = False,
|
|
193
|
+
) -> EditResult:
|
|
194
|
+
"""Edit file content synchronously only if preceding archive reads succeeded.
|
|
195
|
+
|
|
196
|
+
Args:
|
|
197
|
+
file_path: Target file path.
|
|
198
|
+
old_string: Target substring to replace.
|
|
199
|
+
new_string: Replacement substring.
|
|
200
|
+
replace_all: Whether to replace all occurrences.
|
|
201
|
+
|
|
202
|
+
Returns:
|
|
203
|
+
EditResult object.
|
|
204
|
+
"""
|
|
205
|
+
self._ensure_read_succeeded()
|
|
206
|
+
return self._backend.edit(file_path, old_string, new_string, replace_all=replace_all)
|
|
207
|
+
|
|
208
|
+
async def aedit(
|
|
209
|
+
self,
|
|
210
|
+
file_path: str,
|
|
211
|
+
old_string: str,
|
|
212
|
+
new_string: str,
|
|
213
|
+
replace_all: bool = False,
|
|
214
|
+
) -> EditResult:
|
|
215
|
+
"""Edit file content asynchronously only if preceding archive reads succeeded.
|
|
216
|
+
|
|
217
|
+
Args:
|
|
218
|
+
file_path: Target file path.
|
|
219
|
+
old_string: Target substring to replace.
|
|
220
|
+
new_string: Replacement substring.
|
|
221
|
+
replace_all: Whether to replace all occurrences.
|
|
222
|
+
|
|
223
|
+
Returns:
|
|
224
|
+
EditResult object.
|
|
225
|
+
"""
|
|
226
|
+
self._ensure_read_succeeded()
|
|
227
|
+
return await self._backend.aedit(file_path, old_string, new_string, replace_all=replace_all)
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def _resolve_session_id(summarization: Any, state: Any) -> str:
|
|
231
|
+
getter = getattr(summarization, "_get_session_id", None)
|
|
232
|
+
if callable(getter) and state is not None:
|
|
233
|
+
try:
|
|
234
|
+
return str(getter(state))
|
|
235
|
+
except Exception:
|
|
236
|
+
pass
|
|
237
|
+
if isinstance(state, dict) and "_summarization_session_id" in state:
|
|
238
|
+
val = state.get("_summarization_session_id")
|
|
239
|
+
if val:
|
|
240
|
+
return str(val)
|
|
241
|
+
return f"session_{uuid4().hex}"
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _offload_to_backend_compat(
|
|
245
|
+
summarization: Any,
|
|
246
|
+
backend: BackendProtocol,
|
|
247
|
+
to_summarize: list[Any],
|
|
248
|
+
session_id: str,
|
|
249
|
+
) -> str | None:
|
|
250
|
+
try:
|
|
251
|
+
return summarization._offload_to_backend(backend, to_summarize, session_id)
|
|
252
|
+
except TypeError:
|
|
253
|
+
return summarization._offload_to_backend(backend, to_summarize)
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
async def _aoffload_to_backend_compat(
|
|
257
|
+
summarization: Any,
|
|
258
|
+
backend: BackendProtocol,
|
|
259
|
+
to_summarize: list[Any],
|
|
260
|
+
session_id: str,
|
|
261
|
+
) -> str | None:
|
|
262
|
+
try:
|
|
263
|
+
return await summarization._aoffload_to_backend(backend, to_summarize, session_id)
|
|
264
|
+
except TypeError:
|
|
265
|
+
return await summarization._aoffload_to_backend(backend, to_summarize)
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _build_compact_result_compat(
|
|
269
|
+
middleware: Any,
|
|
270
|
+
runtime: ToolRuntime[Any, Any],
|
|
271
|
+
to_summarize: list[Any],
|
|
272
|
+
summary: str,
|
|
273
|
+
file_path: str | None,
|
|
274
|
+
event: Any,
|
|
275
|
+
cutoff: int,
|
|
276
|
+
session_id: str,
|
|
277
|
+
) -> Command[Any]:
|
|
278
|
+
try:
|
|
279
|
+
return middleware._build_compact_result(runtime, to_summarize, summary, file_path, event, cutoff, session_id)
|
|
280
|
+
except TypeError:
|
|
281
|
+
return middleware._build_compact_result(runtime, to_summarize, summary, file_path, event, cutoff)
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
class CLICompactionMiddleware(SummarizationToolMiddleware):
|
|
285
|
+
"""Add explicit forced compaction and runtime model selection for OpsCloud."""
|
|
286
|
+
|
|
287
|
+
@property
|
|
288
|
+
def name(self) -> str:
|
|
289
|
+
"""Return the canonical middleware identifier."""
|
|
290
|
+
return "SummarizationMiddleware"
|
|
291
|
+
|
|
292
|
+
@staticmethod
|
|
293
|
+
def _offload_rejection(request: ToolCallRequest) -> ToolMessage | None:
|
|
294
|
+
expected_id = _offload_tool_call_id(request.runtime.context)
|
|
295
|
+
if expected_id is None:
|
|
296
|
+
return None
|
|
297
|
+
|
|
298
|
+
tool_call = request.tool_call
|
|
299
|
+
args = tool_call.get("args")
|
|
300
|
+
messages = request.state.get("messages", [])
|
|
301
|
+
last_message = messages[-1] if messages else None
|
|
302
|
+
last_message_id = (
|
|
303
|
+
last_message.get("id") if isinstance(last_message, dict) else getattr(last_message, "id", None)
|
|
304
|
+
)
|
|
305
|
+
is_seeded_compaction = (
|
|
306
|
+
tool_call.get("id") == expected_id
|
|
307
|
+
and tool_call.get("name") == "compact_conversation"
|
|
308
|
+
and isinstance(args, dict)
|
|
309
|
+
and args.get("force") is True
|
|
310
|
+
and last_message_id == _offload_seed_message_id(expected_id)
|
|
311
|
+
)
|
|
312
|
+
if is_seeded_compaction:
|
|
313
|
+
return None
|
|
314
|
+
|
|
315
|
+
return ToolMessage(
|
|
316
|
+
content=("Not executed: /offload only authorizes its seeded conversation compaction call."),
|
|
317
|
+
name=tool_call.get("name", "unknown"),
|
|
318
|
+
tool_call_id=tool_call.get("id", ""),
|
|
319
|
+
status="error",
|
|
320
|
+
)
|
|
321
|
+
|
|
322
|
+
def wrap_tool_call(
|
|
323
|
+
self,
|
|
324
|
+
request: ToolCallRequest,
|
|
325
|
+
handler: Callable[[ToolCallRequest], ToolMessage | Command[Any]],
|
|
326
|
+
) -> ToolMessage | Command[Any]:
|
|
327
|
+
"""Wrap synchronous tool call and guard against unauthorized execution during /offload.
|
|
328
|
+
|
|
329
|
+
Args:
|
|
330
|
+
request: Tool execution request.
|
|
331
|
+
handler: Synchronous tool handler.
|
|
332
|
+
|
|
333
|
+
Returns:
|
|
334
|
+
ToolMessage error or handler result.
|
|
335
|
+
"""
|
|
336
|
+
if (rejection := self._offload_rejection(request)) is not None:
|
|
337
|
+
return rejection
|
|
338
|
+
return handler(request)
|
|
339
|
+
|
|
340
|
+
async def awrap_tool_call(
|
|
341
|
+
self,
|
|
342
|
+
request: ToolCallRequest,
|
|
343
|
+
handler: Callable[[ToolCallRequest], Awaitable[ToolMessage | Command[Any]]],
|
|
344
|
+
) -> ToolMessage | Command[Any]:
|
|
345
|
+
"""Wrap asynchronous tool call and guard against unauthorized execution during /offload.
|
|
346
|
+
|
|
347
|
+
Args:
|
|
348
|
+
request: Tool execution request.
|
|
349
|
+
handler: Asynchronous tool handler.
|
|
350
|
+
|
|
351
|
+
Returns:
|
|
352
|
+
ToolMessage error or handler result.
|
|
353
|
+
"""
|
|
354
|
+
if (rejection := self._offload_rejection(request)) is not None:
|
|
355
|
+
return rejection
|
|
356
|
+
return await handler(request)
|
|
357
|
+
|
|
358
|
+
def wrap_model_call( # pyright: ignore[reportIncompatibleMethodOverride]
|
|
359
|
+
self,
|
|
360
|
+
request: ModelRequest,
|
|
361
|
+
handler: Callable[[ModelRequest], ModelResponse],
|
|
362
|
+
) -> ModelResponse:
|
|
363
|
+
"""Run compact-tool prompt injection and delegate to automatic summarization."""
|
|
364
|
+
call_model = partial(super().wrap_model_call, handler=handler)
|
|
365
|
+
runtime = getattr(request, "runtime", None)
|
|
366
|
+
summarization = self._summarization_for_runtime(runtime) if runtime is not None else self._summarization
|
|
367
|
+
return cast(ModelResponse, summarization.wrap_model_call(request, call_model))
|
|
368
|
+
|
|
369
|
+
async def awrap_model_call( # pyright: ignore[reportIncompatibleMethodOverride]
|
|
370
|
+
self,
|
|
371
|
+
request: ModelRequest,
|
|
372
|
+
handler: Callable[[ModelRequest], Awaitable[ModelResponse]],
|
|
373
|
+
) -> ModelResponse:
|
|
374
|
+
"""Run compact-tool prompt injection and delegate to automatic summarization (async)."""
|
|
375
|
+
call_model = partial(super().awrap_model_call, handler=handler)
|
|
376
|
+
runtime = getattr(request, "runtime", None)
|
|
377
|
+
if runtime is not None:
|
|
378
|
+
summarization = await asyncio.to_thread(self._summarization_for_runtime, runtime)
|
|
379
|
+
else:
|
|
380
|
+
summarization = self._summarization
|
|
381
|
+
return cast(ModelResponse, await summarization.awrap_model_call(request, call_model))
|
|
382
|
+
|
|
383
|
+
def _create_compact_tool(self) -> StructuredTool:
|
|
384
|
+
middleware = self
|
|
385
|
+
|
|
386
|
+
def sync_compact(
|
|
387
|
+
runtime: ToolRuntime[Any, Any],
|
|
388
|
+
force: Annotated[bool, InjectedToolArg] = False,
|
|
389
|
+
) -> Command[Any]:
|
|
390
|
+
"""Synchronously compact older messages into a summary."""
|
|
391
|
+
del force
|
|
392
|
+
if _offload_tool_call_id(runtime.context) != runtime.tool_call_id:
|
|
393
|
+
return middleware._run_compact(runtime)
|
|
394
|
+
return middleware._run_forced_compact(runtime)
|
|
395
|
+
|
|
396
|
+
async def async_compact(
|
|
397
|
+
runtime: ToolRuntime[Any, Any],
|
|
398
|
+
force: Annotated[bool, InjectedToolArg] = False,
|
|
399
|
+
) -> Command[Any]:
|
|
400
|
+
"""Asynchronously compact older messages into a summary."""
|
|
401
|
+
del force
|
|
402
|
+
if _offload_tool_call_id(runtime.context) != runtime.tool_call_id:
|
|
403
|
+
return await middleware._arun_compact(runtime)
|
|
404
|
+
return await middleware._arun_forced_compact(runtime)
|
|
405
|
+
|
|
406
|
+
return StructuredTool.from_function(
|
|
407
|
+
name="compact_conversation",
|
|
408
|
+
description=(
|
|
409
|
+
"Compact the conversation by summarizing older messages into "
|
|
410
|
+
"a concise summary. Use this proactively when the conversation "
|
|
411
|
+
"is getting long to free up context window space."
|
|
412
|
+
),
|
|
413
|
+
func=sync_compact,
|
|
414
|
+
coroutine=async_compact,
|
|
415
|
+
)
|
|
416
|
+
|
|
417
|
+
def _guarded_backend(self, runtime: ToolRuntime[Any, Any] | None = None) -> BackendProtocol:
|
|
418
|
+
backend_obj = getattr(self._summarization, "_backend", None)
|
|
419
|
+
if callable(backend_obj) and runtime is not None:
|
|
420
|
+
backend = cast(BackendProtocol, backend_obj(runtime))
|
|
421
|
+
else:
|
|
422
|
+
backend = cast(BackendProtocol, backend_obj)
|
|
423
|
+
return cast(BackendProtocol, _ArchiveReadGuard(backend))
|
|
424
|
+
|
|
425
|
+
def _summarization_for_runtime(self, runtime: ToolRuntime[Any, Any] | None) -> SummarizationMiddleware:
|
|
426
|
+
"""Build a summarizer for the active runtime model when overridden."""
|
|
427
|
+
if runtime is None:
|
|
428
|
+
return self._summarization
|
|
429
|
+
config = _runtime_model_config(runtime)
|
|
430
|
+
if not config.model_spec:
|
|
431
|
+
return self._summarization
|
|
432
|
+
|
|
433
|
+
from opscloud.model.factory import create_model
|
|
434
|
+
|
|
435
|
+
model = create_model(
|
|
436
|
+
config.model_spec,
|
|
437
|
+
extra_kwargs=config.model_params or None,
|
|
438
|
+
profile_overrides=config.profile_overrides or None,
|
|
439
|
+
).model
|
|
440
|
+
context_limit = config.context_limit
|
|
441
|
+
if context_limit is not None:
|
|
442
|
+
profile = getattr(model, "profile", None)
|
|
443
|
+
native = profile.get("max_input_tokens") if isinstance(profile, dict) else None
|
|
444
|
+
if native != context_limit:
|
|
445
|
+
merged = (
|
|
446
|
+
{**profile, "max_input_tokens": context_limit}
|
|
447
|
+
if isinstance(profile, dict)
|
|
448
|
+
else {"max_input_tokens": context_limit}
|
|
449
|
+
)
|
|
450
|
+
try:
|
|
451
|
+
cast(Any, model).profile = merged
|
|
452
|
+
except (AttributeError, TypeError, ValueError):
|
|
453
|
+
logger.warning(
|
|
454
|
+
"Could not apply runtime context limit %d to the offload "
|
|
455
|
+
"model profile; using its resolved profile",
|
|
456
|
+
context_limit,
|
|
457
|
+
exc_info=True,
|
|
458
|
+
)
|
|
459
|
+
backend = self._guarded_backend(runtime)
|
|
460
|
+
return create_summarization_middleware(model, backend)
|
|
461
|
+
|
|
462
|
+
def _run_forced_compact(self, runtime: ToolRuntime[Any, Any]) -> Command[Any]:
|
|
463
|
+
tool_call_id = runtime.tool_call_id or ""
|
|
464
|
+
try:
|
|
465
|
+
summarization = self._summarization_for_runtime(runtime)
|
|
466
|
+
messages = runtime.state.get("messages", [])
|
|
467
|
+
event = runtime.state.get("_summarization_event")
|
|
468
|
+
effective = summarization._apply_event_to_messages(messages, event)
|
|
469
|
+
effective = _without_offload_seed(effective, tool_call_id)
|
|
470
|
+
cutoff = summarization._determine_cutoff_index(effective)
|
|
471
|
+
if cutoff == 0:
|
|
472
|
+
return self._nothing_to_compact(tool_call_id)
|
|
473
|
+
|
|
474
|
+
session_id = _resolve_session_id(summarization, runtime.state)
|
|
475
|
+
to_summarize, _ = summarization._partition_messages(effective, cutoff)
|
|
476
|
+
summary = summarization._create_summary(to_summarize)
|
|
477
|
+
backend = self._guarded_backend(runtime)
|
|
478
|
+
file_path = _offload_to_backend_compat(summarization, backend, to_summarize, session_id)
|
|
479
|
+
return _build_compact_result_compat(
|
|
480
|
+
self, runtime, to_summarize, summary, file_path, event, cutoff, session_id
|
|
481
|
+
)
|
|
482
|
+
except Exception as exc:
|
|
483
|
+
logger.exception("forced compact_conversation failed")
|
|
484
|
+
return self._forced_compact_error(tool_call_id, exc)
|
|
485
|
+
|
|
486
|
+
async def _arun_forced_compact(self, runtime: ToolRuntime[Any, Any]) -> Command[Any]:
|
|
487
|
+
tool_call_id = runtime.tool_call_id or ""
|
|
488
|
+
try:
|
|
489
|
+
summarization = await asyncio.to_thread(self._summarization_for_runtime, runtime)
|
|
490
|
+
messages = runtime.state.get("messages", [])
|
|
491
|
+
event = runtime.state.get("_summarization_event")
|
|
492
|
+
effective = summarization._apply_event_to_messages(messages, event)
|
|
493
|
+
effective = _without_offload_seed(effective, tool_call_id)
|
|
494
|
+
cutoff = summarization._determine_cutoff_index(effective)
|
|
495
|
+
if cutoff == 0:
|
|
496
|
+
return self._nothing_to_compact(tool_call_id)
|
|
497
|
+
|
|
498
|
+
session_id = _resolve_session_id(summarization, runtime.state)
|
|
499
|
+
to_summarize, _ = summarization._partition_messages(effective, cutoff)
|
|
500
|
+
summary = await summarization._acreate_summary(to_summarize)
|
|
501
|
+
backend = self._guarded_backend(runtime)
|
|
502
|
+
file_path = await _aoffload_to_backend_compat(summarization, backend, to_summarize, session_id)
|
|
503
|
+
return _build_compact_result_compat(
|
|
504
|
+
self, runtime, to_summarize, summary, file_path, event, cutoff, session_id
|
|
505
|
+
)
|
|
506
|
+
except Exception as exc:
|
|
507
|
+
logger.exception("forced compact_conversation failed")
|
|
508
|
+
return self._forced_compact_error(tool_call_id, exc)
|
|
509
|
+
|
|
510
|
+
@staticmethod
|
|
511
|
+
def _forced_compact_error(tool_call_id: str, exc: Exception) -> Command[Any]:
|
|
512
|
+
return Command(
|
|
513
|
+
update={
|
|
514
|
+
"messages": [
|
|
515
|
+
ToolMessage(
|
|
516
|
+
content=(
|
|
517
|
+
f"{COMPACTION_FAILURE_PREFIX}: an error occurred "
|
|
518
|
+
f"during compaction ({type(exc).__name__}: {exc}). "
|
|
519
|
+
"Your conversation is unchanged."
|
|
520
|
+
),
|
|
521
|
+
tool_call_id=tool_call_id,
|
|
522
|
+
)
|
|
523
|
+
],
|
|
524
|
+
}
|
|
525
|
+
)
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
def _create_cli_compaction_middleware(
|
|
529
|
+
model: str | BaseChatModel,
|
|
530
|
+
backend: BackendProtocol,
|
|
531
|
+
) -> CLICompactionMiddleware:
|
|
532
|
+
"""Create the OpsCloud compaction middleware from the SDK configuration."""
|
|
533
|
+
profile = getattr(model, "profile", None)
|
|
534
|
+
if not isinstance(model, str) and (not isinstance(profile, dict) or not profile.get("max_input_tokens")):
|
|
535
|
+
default_profile = {"max_input_tokens": 128_000}
|
|
536
|
+
if isinstance(profile, dict):
|
|
537
|
+
default_profile.update(profile)
|
|
538
|
+
try:
|
|
539
|
+
cast(Any, model).profile = default_profile
|
|
540
|
+
except Exception:
|
|
541
|
+
with contextlib.suppress(Exception):
|
|
542
|
+
cast(Any, type(model)).profile = property(lambda self: dict(default_profile))
|
|
543
|
+
sdk_middleware = create_summarization_tool_middleware(model, backend)
|
|
544
|
+
return CLICompactionMiddleware(
|
|
545
|
+
sdk_middleware._summarization,
|
|
546
|
+
system_prompt=sdk_middleware.system_prompt,
|
|
547
|
+
)
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
CompactionMiddleware = CLICompactionMiddleware
|
|
551
|
+
|
|
552
|
+
__all__ = [
|
|
553
|
+
"COMPACTION_FAILURE_PREFIX",
|
|
554
|
+
"CLICompactionMiddleware",
|
|
555
|
+
"CompactionMiddleware",
|
|
556
|
+
"RuntimeModelConfig",
|
|
557
|
+
"_create_cli_compaction_middleware",
|
|
558
|
+
]
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
create_cli_compaction_middleware = _create_cli_compaction_middleware
|
|
562
|
+
|
|
563
|
+
__all__ = [
|
|
564
|
+
'_create_cli_compaction_middleware',
|
|
565
|
+
'create_cli_compaction_middleware',
|
|
566
|
+
]
|