@oneciel-ai/ciel-runtime 0.1.1 → 0.2.0
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.
- package/README.md +132 -0
- package/ciel-runtime-menu.py +56 -6
- package/ciel_runtime.py +9379 -35909
- package/ciel_runtime_support/advisor_client.py +193 -0
- package/ciel_runtime_support/advisor_policy.py +320 -0
- package/ciel_runtime_support/advisor_refinement.py +160 -0
- package/ciel_runtime_support/advisor_request_builder.py +261 -0
- package/ciel_runtime_support/agy_installer.py +169 -0
- package/ciel_runtime_support/agy_mcp_restore.py +182 -0
- package/ciel_runtime_support/anthropic_model_policy.py +186 -0
- package/ciel_runtime_support/anthropic_response_writer.py +255 -0
- package/ciel_runtime_support/anthropic_tool_turns.py +130 -0
- package/ciel_runtime_support/api_key_cooldown.py +159 -0
- package/ciel_runtime_support/architecture.py +488 -1
- package/ciel_runtime_support/architecture_budget.py +42 -0
- package/ciel_runtime_support/channel_backlog.py +90 -0
- package/ciel_runtime_support/channel_cli.py +119 -0
- package/ciel_runtime_support/channel_compact_injection.py +82 -0
- package/ciel_runtime_support/channel_compact_poll.py +67 -0
- package/ciel_runtime_support/channel_compact_request_repository.py +113 -0
- package/ciel_runtime_support/channel_config_service.py +281 -0
- package/ciel_runtime_support/channel_connection_lifecycle.py +180 -0
- package/ciel_runtime_support/channel_connection_registry.py +128 -0
- package/ciel_runtime_support/channel_connection_worker.py +284 -0
- package/ciel_runtime_support/channel_cursor_recovery.py +92 -0
- package/ciel_runtime_support/channel_cursor_repository.py +89 -0
- package/ciel_runtime_support/channel_cursor_service.py +178 -0
- package/ciel_runtime_support/channel_event_identity.py +212 -0
- package/ciel_runtime_support/channel_event_projection.py +315 -0
- package/ciel_runtime_support/channel_inflight.py +127 -0
- package/ciel_runtime_support/channel_injection.py +115 -0
- package/ciel_runtime_support/channel_launch_guard_repository.py +58 -0
- package/ciel_runtime_support/channel_launch_policy.py +180 -0
- package/ciel_runtime_support/channel_llm_context.py +156 -0
- package/ciel_runtime_support/channel_mcp_discovery.py +186 -0
- package/ciel_runtime_support/channel_mcp_http_controller.py +239 -0
- package/ciel_runtime_support/channel_mcp_ownership.py +148 -0
- package/ciel_runtime_support/channel_mcp_tools.py +240 -0
- package/ciel_runtime_support/channel_mcp_transport.py +394 -0
- package/ciel_runtime_support/channel_message_dedupe.py +65 -0
- package/ciel_runtime_support/channel_message_policy.py +256 -0
- package/ciel_runtime_support/channel_message_prompt.py +305 -0
- package/ciel_runtime_support/channel_message_repository.py +234 -0
- package/ciel_runtime_support/channel_notification_projection.py +217 -0
- package/ciel_runtime_support/channel_panel.py +162 -0
- package/ciel_runtime_support/channel_pending_injection.py +209 -0
- package/ciel_runtime_support/channel_pending_poll.py +109 -0
- package/ciel_runtime_support/channel_probe_cache.py +433 -0
- package/ciel_runtime_support/channel_probe_report.py +101 -0
- package/ciel_runtime_support/channel_runtime_environment.py +181 -0
- package/ciel_runtime_support/channel_session_lifecycle.py +114 -0
- package/ciel_runtime_support/channel_session_repository.py +90 -0
- package/ciel_runtime_support/channel_terminal_dispatch.py +115 -0
- package/ciel_runtime_support/channel_terminal_input.py +277 -0
- package/ciel_runtime_support/channel_terminal_proxy.py +447 -0
- package/ciel_runtime_support/channel_tool_context.py +166 -0
- package/ciel_runtime_support/channel_transcript.py +414 -0
- package/ciel_runtime_support/channel_transcript_repository.py +96 -0
- package/ciel_runtime_support/channel_wake_claim_repository.py +126 -0
- package/ciel_runtime_support/channel_wake_delivery_repository.py +88 -0
- package/ciel_runtime_support/chat_files.py +138 -0
- package/ciel_runtime_support/chat_http_controller.py +235 -0
- package/ciel_runtime_support/claude_environment.py +375 -0
- package/ciel_runtime_support/claude_router.py +247 -193
- package/ciel_runtime_support/cli_dispatch.py +792 -0
- package/ciel_runtime_support/cli_parser.py +165 -0
- package/ciel_runtime_support/cli_usage.py +100 -0
- package/ciel_runtime_support/codex_app_server.py +20 -5
- package/ciel_runtime_support/codex_channel_sse_launch.py +87 -0
- package/ciel_runtime_support/codex_cli.py +42 -6
- package/ciel_runtime_support/codex_config.py +323 -0
- package/ciel_runtime_support/codex_launch_configuration.py +240 -0
- package/ciel_runtime_support/codex_launch_policy.py +66 -0
- package/ciel_runtime_support/codex_mcp_integration.py +195 -0
- package/ciel_runtime_support/codex_mcp_restore.py +304 -0
- package/ciel_runtime_support/codex_model_catalog.py +133 -0
- package/ciel_runtime_support/codex_process_lifecycle.py +271 -0
- package/ciel_runtime_support/codex_router.py +147 -1
- package/ciel_runtime_support/codex_session_repository.py +115 -0
- package/ciel_runtime_support/codex_session_selection.py +114 -0
- package/ciel_runtime_support/command_asset_installer.py +103 -0
- package/ciel_runtime_support/compatibility_probe.py +295 -0
- package/ciel_runtime_support/compatibility_protocol.py +251 -0
- package/ciel_runtime_support/compatibility_runtime.py +166 -0
- package/ciel_runtime_support/compatibility_test.py +370 -0
- package/ciel_runtime_support/config_migrations.py +307 -0
- package/ciel_runtime_support/config_repository.py +175 -0
- package/ciel_runtime_support/config_value_codec.py +64 -0
- package/ciel_runtime_support/configuration_cli.py +374 -0
- package/ciel_runtime_support/context_compaction.py +280 -0
- package/ciel_runtime_support/context_setup.py +208 -0
- package/ciel_runtime_support/context_summary_policy.py +392 -0
- package/ciel_runtime_support/credential_cli.py +104 -0
- package/ciel_runtime_support/credential_management.py +261 -0
- package/ciel_runtime_support/credentials.py +269 -0
- package/ciel_runtime_support/executable_discovery.py +141 -0
- package/ciel_runtime_support/github_copilot_oauth.py +335 -0
- package/ciel_runtime_support/github_copilot_oauth_runtime.py +213 -0
- package/ciel_runtime_support/header_forwarding.py +73 -0
- package/ciel_runtime_support/headless_config.py +221 -0
- package/ciel_runtime_support/http_response.py +129 -0
- package/ciel_runtime_support/install_diagnostics.py +149 -0
- package/ciel_runtime_support/kimi_identity.py +123 -0
- package/ciel_runtime_support/launch_diagnostics.py +204 -0
- package/ciel_runtime_support/launch_state.py +127 -0
- package/ciel_runtime_support/live_api_key_controller.py +58 -0
- package/ciel_runtime_support/llm_config_http.py +148 -0
- package/ciel_runtime_support/llm_option_config.py +259 -0
- package/ciel_runtime_support/llm_presentation_data.py +447 -0
- package/ciel_runtime_support/llm_presets.py +773 -0
- package/ciel_runtime_support/lm_studio_runtime.py +401 -0
- package/ciel_runtime_support/managed_mcp_config.py +144 -0
- package/ciel_runtime_support/managed_mcp_discovery.py +151 -0
- package/ciel_runtime_support/managed_service_cleanup.py +89 -0
- package/ciel_runtime_support/mcp_config_reader.py +230 -0
- package/ciel_runtime_support/mcp_http_proxy.py +607 -0
- package/ciel_runtime_support/mcp_inventory.py +59 -0
- package/ciel_runtime_support/mcp_notification_wait_policy.py +159 -0
- package/ciel_runtime_support/mcp_probe_codec.py +136 -0
- package/ciel_runtime_support/mcp_probe_transport.py +328 -0
- package/ciel_runtime_support/mcp_proxy_codec.py +345 -0
- package/ciel_runtime_support/mcp_proxy_config.py +107 -0
- package/ciel_runtime_support/mcp_proxy_notifications.py +261 -0
- package/ciel_runtime_support/mcp_proxy_process.py +560 -0
- package/ciel_runtime_support/mcp_split_proxy_http.py +165 -0
- package/ciel_runtime_support/mcp_stdio_probe.py +240 -0
- package/ciel_runtime_support/mcp_transport.py +146 -0
- package/ciel_runtime_support/model_cache_lifecycle.py +78 -0
- package/ciel_runtime_support/model_catalog_projection.py +61 -0
- package/ciel_runtime_support/model_context_hints.py +109 -0
- package/ciel_runtime_support/model_panel.py +147 -0
- package/ciel_runtime_support/model_registry_repository.py +231 -0
- package/ciel_runtime_support/npm_runtime.py +191 -0
- package/ciel_runtime_support/ollama_catalog.py +462 -0
- package/ciel_runtime_support/ollama_catalog_cli.py +41 -0
- package/ciel_runtime_support/ollama_catalog_repository.py +75 -0
- package/ciel_runtime_support/ollama_context_sync.py +87 -0
- package/ciel_runtime_support/ollama_forwarding.py +449 -0
- package/ciel_runtime_support/openai_chat_passthrough.py +67 -0
- package/ciel_runtime_support/openai_chat_router.py +64 -0
- package/ciel_runtime_support/openai_forwarding.py +194 -0
- package/ciel_runtime_support/openai_responses_router.py +291 -0
- package/ciel_runtime_support/openai_responses_stream.py +135 -0
- package/ciel_runtime_support/output_budget.py +89 -0
- package/ciel_runtime_support/package_lifecycle.py +217 -0
- package/ciel_runtime_support/plan_artifact_controller.py +104 -0
- package/ciel_runtime_support/prelaunch.py +959 -0
- package/ciel_runtime_support/prelaunch_launch_preference.py +75 -0
- package/ciel_runtime_support/prelaunch_panel_projection.py +396 -0
- package/ciel_runtime_support/prelaunch_terminal.py +764 -0
- package/ciel_runtime_support/process_control.py +708 -0
- package/ciel_runtime_support/prompt_compaction.py +322 -0
- package/ciel_runtime_support/prompt_injection.py +176 -0
- package/ciel_runtime_support/protocols/__init__.py +24 -0
- package/ciel_runtime_support/protocols/anthropic_content.py +29 -0
- package/ciel_runtime_support/protocols/anthropic_thinking_policy.py +336 -0
- package/ciel_runtime_support/protocols/chat_projection.py +315 -0
- package/ciel_runtime_support/protocols/conversation_policy.py +217 -0
- package/ciel_runtime_support/protocols/conversation_turn_policy.py +972 -0
- package/ciel_runtime_support/protocols/ollama_chat.py +111 -0
- package/ciel_runtime_support/protocols/ollama_response.py +231 -0
- package/ciel_runtime_support/protocols/openai_reasoning.py +75 -0
- package/ciel_runtime_support/protocols/openai_responses.py +271 -0
- package/ciel_runtime_support/protocols/pseudo_tool_history.py +248 -0
- package/ciel_runtime_support/protocols/tool_result_projection.py +112 -0
- package/ciel_runtime_support/provider_adapters.py +160 -0
- package/ciel_runtime_support/provider_catalog_sources.py +316 -0
- package/ciel_runtime_support/provider_choice.py +201 -0
- package/ciel_runtime_support/provider_compatibility.py +165 -0
- package/ciel_runtime_support/provider_config_mutations.py +361 -0
- package/ciel_runtime_support/provider_configuration_service.py +162 -0
- package/ciel_runtime_support/provider_context.py +308 -0
- package/ciel_runtime_support/provider_contract_projection.py +73 -0
- package/ciel_runtime_support/provider_descriptor.py +82 -0
- package/ciel_runtime_support/provider_endpoint_policy.py +130 -0
- package/ciel_runtime_support/provider_endpoint_probe.py +165 -0
- package/ciel_runtime_support/provider_launch_endpoint.py +109 -0
- package/ciel_runtime_support/provider_limits.py +457 -0
- package/ciel_runtime_support/provider_model_identity.py +139 -0
- package/ciel_runtime_support/provider_model_selection.py +431 -0
- package/ciel_runtime_support/provider_model_specs.py +142 -0
- package/ciel_runtime_support/provider_models.py +263 -0
- package/ciel_runtime_support/provider_network.py +176 -0
- package/ciel_runtime_support/provider_option_cli.py +238 -0
- package/ciel_runtime_support/provider_option_panel.py +275 -0
- package/ciel_runtime_support/provider_option_status.py +192 -0
- package/ciel_runtime_support/provider_policy.py +101 -0
- package/ciel_runtime_support/provider_query_policy.py +67 -0
- package/ciel_runtime_support/provider_readiness.py +112 -0
- package/ciel_runtime_support/provider_request_access.py +131 -0
- package/ciel_runtime_support/provider_request_builder.py +250 -0
- package/ciel_runtime_support/provider_responses_passthrough.py +81 -0
- package/ciel_runtime_support/provider_runtime_info.py +113 -0
- package/ciel_runtime_support/provider_runtime_modes.py +150 -0
- package/ciel_runtime_support/provider_sampling_policy.py +46 -0
- package/ciel_runtime_support/provider_status.py +145 -0
- package/ciel_runtime_support/provider_timeout_policy.py +184 -0
- package/ciel_runtime_support/provider_tool_policy.py +145 -0
- package/ciel_runtime_support/providers/__init__.py +65 -0
- package/ciel_runtime_support/providers/anthropic.py +160 -0
- package/ciel_runtime_support/providers/anthropic_catalog.py +119 -0
- package/ciel_runtime_support/providers/base.py +232 -0
- package/ciel_runtime_support/providers/catalog.py +326 -0
- package/ciel_runtime_support/providers/cloud.py +194 -0
- package/ciel_runtime_support/providers/constants.py +54 -0
- package/ciel_runtime_support/providers/deepseek.py +115 -0
- package/ciel_runtime_support/providers/fireworks.py +158 -0
- package/ciel_runtime_support/providers/github_copilot_oauth.py +199 -0
- package/ciel_runtime_support/providers/kimi.py +297 -0
- package/ciel_runtime_support/providers/lm_studio.py +76 -0
- package/ciel_runtime_support/providers/meta.py +257 -0
- package/ciel_runtime_support/providers/native.py +194 -0
- package/ciel_runtime_support/providers/nim.py +69 -0
- package/ciel_runtime_support/providers/nvidia.py +158 -0
- package/ciel_runtime_support/providers/nvidia_runtime.py +285 -0
- package/ciel_runtime_support/providers/ollama.py +181 -0
- package/ciel_runtime_support/providers/ollama_context.py +195 -0
- package/ciel_runtime_support/providers/ollama_runtime.py +213 -0
- package/ciel_runtime_support/providers/opencode.py +220 -0
- package/ciel_runtime_support/providers/opencode_go.py +37 -0
- package/ciel_runtime_support/providers/openrouter.py +57 -0
- package/ciel_runtime_support/providers/vllm.py +65 -0
- package/ciel_runtime_support/providers/zai.py +119 -0
- package/ciel_runtime_support/pseudo_tool_parser.py +115 -0
- package/ciel_runtime_support/rate_limit_policy.py +117 -0
- package/ciel_runtime_support/rate_limit_repository.py +154 -0
- package/ciel_runtime_support/registry.py +46 -0
- package/ciel_runtime_support/request_shortcuts.py +253 -0
- package/ciel_runtime_support/request_trace.py +323 -0
- package/ciel_runtime_support/response_collection.py +209 -0
- package/ciel_runtime_support/router_access.py +238 -0
- package/ciel_runtime_support/router_client_lifecycle.py +366 -0
- package/ciel_runtime_support/router_health_policy.py +101 -0
- package/ciel_runtime_support/router_http.py +513 -0
- package/ciel_runtime_support/router_process_lifecycle.py +401 -0
- package/ciel_runtime_support/router_rate_limit_service.py +285 -0
- package/ciel_runtime_support/router_server_runtime.py +103 -0
- package/ciel_runtime_support/router_shortcuts.py +201 -0
- package/ciel_runtime_support/routing_fallback.py +73 -0
- package/ciel_runtime_support/runtime_activity_repository.py +143 -0
- package/ciel_runtime_support/runtime_adapters.py +104 -0
- package/ciel_runtime_support/runtime_command_factory.py +73 -0
- package/ciel_runtime_support/runtime_compatibility.py +50 -0
- package/ciel_runtime_support/runtime_constants.py +178 -0
- package/ciel_runtime_support/runtime_launch.py +1602 -0
- package/ciel_runtime_support/runtime_llm_options.py +312 -0
- package/ciel_runtime_support/runtime_logging.py +161 -0
- package/ciel_runtime_support/runtime_paths.py +157 -0
- package/ciel_runtime_support/runtime_restart.py +84 -0
- package/ciel_runtime_support/runtime_upgrade.py +149 -0
- package/ciel_runtime_support/secure_json_repository.py +55 -0
- package/ciel_runtime_support/session_import.py +356 -0
- package/ciel_runtime_support/settings_repository.py +8 -0
- package/ciel_runtime_support/slash_command_assets.py +211 -0
- package/ciel_runtime_support/sse_stream.py +57 -0
- package/ciel_runtime_support/sse_trace.py +225 -0
- package/ciel_runtime_support/statusline_script.py +593 -0
- package/ciel_runtime_support/statusline_settings.py +53 -0
- package/ciel_runtime_support/stream_chunk_policy.py +18 -0
- package/ciel_runtime_support/streaming_anthropic.py +1955 -0
- package/ciel_runtime_support/synthetic_tool_policy.py +105 -0
- package/ciel_runtime_support/terminal_platform_io.py +127 -0
- package/ciel_runtime_support/timeout_profile.py +196 -0
- package/ciel_runtime_support/tool_dialects.py +85 -0
- package/ciel_runtime_support/tool_exposure_policy.py +63 -0
- package/ciel_runtime_support/tool_guard_hooks.py +218 -0
- package/ciel_runtime_support/tool_request_projection.py +96 -0
- package/ciel_runtime_support/tool_schema.py +483 -0
- package/ciel_runtime_support/tool_side_effect_dedupe.py +95 -0
- package/ciel_runtime_support/ui_text.py +266 -0
- package/ciel_runtime_support/upstream_error_policy.py +104 -0
- package/ciel_runtime_support/upstream_retry.py +419 -0
- package/ciel_runtime_support/upstream_stream_io.py +106 -0
- package/ciel_runtime_support/usage_events.py +96 -0
- package/ciel_runtime_support/visible_stream_filters.py +130 -0
- package/ciel_runtime_support/web_endpoints.py +447 -0
- package/ciel_runtime_support/web_ui.py +915 -0
- package/ciel_runtime_support/web_ui_controller.py +189 -0
- package/ciel_runtime_support/windows_console_input.py +137 -0
- package/ciel_runtime_support/windows_console_mode.py +112 -0
- package/docs/Architecture.md +54 -0
- package/docs/Configuration.md +17 -0
- package/docs/Module-Map.md +1093 -26
- package/docs/Providers.md +46 -1
- package/docs/adr/0001-runtime-bounded-contexts.md +295 -0
- package/npm-bin/run-ciel-runtime.js +22 -2
- package/package.json +9 -2
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
"""Channel MCP SSE and Streamable HTTP transport orchestration."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import time
|
|
6
|
+
import urllib.error
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from typing import Any, Callable
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(frozen=True, slots=True)
|
|
12
|
+
class ChannelMcpTransportConfig:
|
|
13
|
+
version: str
|
|
14
|
+
legacy_protocol: str
|
|
15
|
+
streamable_protocol: str
|
|
16
|
+
native_names: frozenset[str] | set[str] | tuple[str, ...]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True, slots=True)
|
|
20
|
+
class ChannelMcpTransportState:
|
|
21
|
+
connections: dict[str, dict[str, Any]]
|
|
22
|
+
lock: Any
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True, slots=True)
|
|
26
|
+
class ChannelMcpHttpPorts:
|
|
27
|
+
legacy_post: Callable[..., Any]
|
|
28
|
+
streamable_post: Callable[..., Any]
|
|
29
|
+
error_body: Callable[[BaseException], str]
|
|
30
|
+
session_not_found: Callable[[BaseException, str], bool]
|
|
31
|
+
parse_bool: Callable[..., bool]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass(frozen=True, slots=True)
|
|
35
|
+
class ChannelMcpEffects:
|
|
36
|
+
set_state: Callable[..., Any]
|
|
37
|
+
take_response: Callable[..., Any]
|
|
38
|
+
mark_session_lost: Callable[..., Any]
|
|
39
|
+
absolute_endpoint: Callable[..., str]
|
|
40
|
+
record_session: Callable[..., Any]
|
|
41
|
+
store_response: Callable[..., Any]
|
|
42
|
+
project_payload: Callable[..., Any]
|
|
43
|
+
append_message: Callable[..., Any]
|
|
44
|
+
log: Callable[[str, str], None]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class ChannelMcpTransport:
|
|
48
|
+
def __init__(
|
|
49
|
+
self,
|
|
50
|
+
config: ChannelMcpTransportConfig,
|
|
51
|
+
state: ChannelMcpTransportState,
|
|
52
|
+
http: ChannelMcpHttpPorts,
|
|
53
|
+
effects: ChannelMcpEffects,
|
|
54
|
+
) -> None:
|
|
55
|
+
self.config = config
|
|
56
|
+
self.state = state
|
|
57
|
+
self.http = http
|
|
58
|
+
self.effects = effects
|
|
59
|
+
|
|
60
|
+
def rpc_request(
|
|
61
|
+
self,
|
|
62
|
+
name: str,
|
|
63
|
+
method: str,
|
|
64
|
+
params: dict[str, Any] | None = None,
|
|
65
|
+
timeout: float | None = None,
|
|
66
|
+
) -> dict[str, Any]:
|
|
67
|
+
request_id = int(time.time_ns() % 9_000_000_000_000_000)
|
|
68
|
+
payload = {
|
|
69
|
+
"jsonrpc": "2.0",
|
|
70
|
+
"id": request_id,
|
|
71
|
+
"method": method,
|
|
72
|
+
"params": params or {},
|
|
73
|
+
}
|
|
74
|
+
for attempt in range(2):
|
|
75
|
+
with self.state.lock:
|
|
76
|
+
state = self.state.connections.get(name)
|
|
77
|
+
if not state:
|
|
78
|
+
raise RuntimeError(f"SSE channel {name} is not connected")
|
|
79
|
+
transport = str(state.get("transport") or "sse").strip().lower()
|
|
80
|
+
if transport in {"http", "streamable-http"} and not state.get(
|
|
81
|
+
"mcp_initialized"
|
|
82
|
+
):
|
|
83
|
+
needs_http_initialize = True
|
|
84
|
+
else:
|
|
85
|
+
needs_http_initialize = False
|
|
86
|
+
if needs_http_initialize:
|
|
87
|
+
self.initialize_streamable(name)
|
|
88
|
+
with self.state.lock:
|
|
89
|
+
state = self.state.connections.get(name)
|
|
90
|
+
if not state:
|
|
91
|
+
raise RuntimeError(f"SSE channel {name} is not connected")
|
|
92
|
+
if not state.get("mcp_initialized"):
|
|
93
|
+
raise RuntimeError(f"SSE channel {name} is not MCP initialized")
|
|
94
|
+
endpoint = str(state.get("mcp_endpoint") or "")
|
|
95
|
+
headers = dict(state.get("headers") or {})
|
|
96
|
+
transport = str(state.get("transport") or "sse").strip().lower()
|
|
97
|
+
protocol_version = str(
|
|
98
|
+
state.get("mcp_protocol_version") or self.config.legacy_protocol
|
|
99
|
+
)
|
|
100
|
+
session_id = str(state.get("mcp_session_id") or "").strip() or None
|
|
101
|
+
requires_session = self.http.parse_bool(
|
|
102
|
+
state.get("streamable_requires_session"), True
|
|
103
|
+
)
|
|
104
|
+
effective_timeout = float(
|
|
105
|
+
timeout
|
|
106
|
+
if timeout is not None
|
|
107
|
+
else state.get("mcp_timeout_seconds") or 20.0
|
|
108
|
+
)
|
|
109
|
+
if not endpoint:
|
|
110
|
+
raise RuntimeError(f"SSE channel {name} has no MCP endpoint")
|
|
111
|
+
if transport in {"http", "streamable-http"}:
|
|
112
|
+
if requires_session and not session_id:
|
|
113
|
+
raise RuntimeError(
|
|
114
|
+
f"Streamable HTTP MCP channel {name} has no Mcp-Session-Id"
|
|
115
|
+
)
|
|
116
|
+
try:
|
|
117
|
+
posted, returned_session = self.http.streamable_post(
|
|
118
|
+
endpoint,
|
|
119
|
+
headers,
|
|
120
|
+
payload,
|
|
121
|
+
max(1.0, min(120.0, effective_timeout)),
|
|
122
|
+
protocol_version,
|
|
123
|
+
session_id,
|
|
124
|
+
)
|
|
125
|
+
except urllib.error.HTTPError as exc:
|
|
126
|
+
body_text = self.http.error_body(exc)
|
|
127
|
+
if attempt == 0 and self.http.session_not_found(exc, body_text):
|
|
128
|
+
reason = (
|
|
129
|
+
f"streamable_http_session_not_found:HTTPError:{exc.code}"
|
|
130
|
+
)
|
|
131
|
+
self.effects.mark_session_lost(name, reason)
|
|
132
|
+
self.effects.log(
|
|
133
|
+
"WARN",
|
|
134
|
+
f"channel_http_mcp_session_lost name={name} method={method} error=HTTPError:{exc.code}:{exc.reason}",
|
|
135
|
+
)
|
|
136
|
+
continue
|
|
137
|
+
raise
|
|
138
|
+
if returned_session:
|
|
139
|
+
self.effects.set_state(name, mcp_session_id=returned_session)
|
|
140
|
+
else:
|
|
141
|
+
posted = self.http.legacy_post(
|
|
142
|
+
endpoint, headers, payload, max(1.0, min(120.0, effective_timeout))
|
|
143
|
+
)
|
|
144
|
+
break
|
|
145
|
+
else:
|
|
146
|
+
raise RuntimeError(f"SSE channel {name} could not send MCP request")
|
|
147
|
+
if (
|
|
148
|
+
isinstance(posted, dict)
|
|
149
|
+
and posted.get("id") == request_id
|
|
150
|
+
and ("result" in posted or "error" in posted)
|
|
151
|
+
):
|
|
152
|
+
return posted
|
|
153
|
+
response = self.effects.take_response(
|
|
154
|
+
name, request_id, max(1.0, min(120.0, effective_timeout))
|
|
155
|
+
)
|
|
156
|
+
if response is None:
|
|
157
|
+
raise TimeoutError(
|
|
158
|
+
f"timed out waiting for MCP SSE response id={request_id} method={method} channel={name}"
|
|
159
|
+
)
|
|
160
|
+
return response
|
|
161
|
+
|
|
162
|
+
def maybe_initialize(self, name: str, endpoint_text: str) -> None:
|
|
163
|
+
with self.state.lock:
|
|
164
|
+
state = self.state.connections.get(name)
|
|
165
|
+
if not state:
|
|
166
|
+
return
|
|
167
|
+
if not bool(state.get("mcp_enabled", True)):
|
|
168
|
+
return
|
|
169
|
+
stream_url = str(state.get("url") or "")
|
|
170
|
+
endpoint = self.effects.absolute_endpoint(stream_url, endpoint_text)
|
|
171
|
+
current_endpoint = str(state.get("mcp_endpoint") or "")
|
|
172
|
+
was_initialized = bool(state.get("mcp_initialized"))
|
|
173
|
+
if was_initialized and current_endpoint == endpoint:
|
|
174
|
+
return
|
|
175
|
+
headers = dict(state.get("headers") or {})
|
|
176
|
+
timeout = max(
|
|
177
|
+
5.0, min(120.0, float(state.get("mcp_timeout_seconds") or 20.0))
|
|
178
|
+
)
|
|
179
|
+
protocol_version = str(state.get("mcp_protocol_version") or "2024-11-05")
|
|
180
|
+
try:
|
|
181
|
+
if was_initialized and current_endpoint:
|
|
182
|
+
self.effects.log(
|
|
183
|
+
"INFO",
|
|
184
|
+
f"channel_sse_mcp_reinitializing name={name} old_endpoint={current_endpoint} new_endpoint={endpoint}",
|
|
185
|
+
)
|
|
186
|
+
initialize = {
|
|
187
|
+
"jsonrpc": "2.0",
|
|
188
|
+
"id": 1,
|
|
189
|
+
"method": "initialize",
|
|
190
|
+
"params": {
|
|
191
|
+
"protocolVersion": protocol_version,
|
|
192
|
+
"capabilities": {},
|
|
193
|
+
"clientInfo": {
|
|
194
|
+
"name": "ciel-runtime-channel-bridge",
|
|
195
|
+
"version": self.config.version,
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
}
|
|
199
|
+
self.http.legacy_post(endpoint, headers, initialize, timeout)
|
|
200
|
+
initialized = {
|
|
201
|
+
"jsonrpc": "2.0",
|
|
202
|
+
"method": "notifications/initialized",
|
|
203
|
+
"params": {},
|
|
204
|
+
}
|
|
205
|
+
self.http.legacy_post(endpoint, headers, initialized, timeout)
|
|
206
|
+
self.effects.set_state(
|
|
207
|
+
name,
|
|
208
|
+
mcp_endpoint=endpoint,
|
|
209
|
+
mcp_initialized=True,
|
|
210
|
+
mcp_last_error=None,
|
|
211
|
+
mcp_rpc_results={},
|
|
212
|
+
)
|
|
213
|
+
self.effects.log(
|
|
214
|
+
"INFO", f"channel_sse_mcp_initialized name={name} endpoint={endpoint}"
|
|
215
|
+
)
|
|
216
|
+
except Exception as exc:
|
|
217
|
+
self.effects.set_state(
|
|
218
|
+
name,
|
|
219
|
+
mcp_endpoint=endpoint,
|
|
220
|
+
mcp_initialized=False,
|
|
221
|
+
mcp_last_error=f"{type(exc).__name__}: {exc}",
|
|
222
|
+
)
|
|
223
|
+
self.effects.log(
|
|
224
|
+
"WARN",
|
|
225
|
+
f"channel_sse_mcp_initialize_failed name={name} endpoint={endpoint} error={type(exc).__name__}: {exc}",
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
def initialize_streamable(self, name: str) -> None:
|
|
229
|
+
with self.state.lock:
|
|
230
|
+
state = self.state.connections.get(name)
|
|
231
|
+
if not state:
|
|
232
|
+
return
|
|
233
|
+
if not bool(state.get("mcp_enabled", True)):
|
|
234
|
+
return
|
|
235
|
+
endpoint = str(state.get("url") or "")
|
|
236
|
+
if (
|
|
237
|
+
bool(state.get("mcp_initialized"))
|
|
238
|
+
and str(state.get("mcp_endpoint") or "") == endpoint
|
|
239
|
+
):
|
|
240
|
+
return
|
|
241
|
+
headers = dict(state.get("headers") or {})
|
|
242
|
+
timeout = max(
|
|
243
|
+
5.0, min(120.0, float(state.get("mcp_timeout_seconds") or 20.0))
|
|
244
|
+
)
|
|
245
|
+
protocol_version = str(
|
|
246
|
+
state.get("mcp_protocol_version") or self.config.streamable_protocol
|
|
247
|
+
)
|
|
248
|
+
requires_session = self.http.parse_bool(
|
|
249
|
+
state.get("streamable_requires_session"), True
|
|
250
|
+
)
|
|
251
|
+
try:
|
|
252
|
+
initialize = {
|
|
253
|
+
"jsonrpc": "2.0",
|
|
254
|
+
"id": 1,
|
|
255
|
+
"method": "initialize",
|
|
256
|
+
"params": {
|
|
257
|
+
"protocolVersion": protocol_version,
|
|
258
|
+
"capabilities": {},
|
|
259
|
+
"clientInfo": {
|
|
260
|
+
"name": "ciel-runtime-channel-bridge",
|
|
261
|
+
"version": self.config.version,
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
}
|
|
265
|
+
_result, session_id = self.http.streamable_post(
|
|
266
|
+
endpoint, headers, initialize, timeout, protocol_version
|
|
267
|
+
)
|
|
268
|
+
if requires_session and not session_id:
|
|
269
|
+
self.effects.set_state(
|
|
270
|
+
name,
|
|
271
|
+
mcp_endpoint=endpoint,
|
|
272
|
+
mcp_initialized=False,
|
|
273
|
+
mcp_session_id=None,
|
|
274
|
+
mcp_last_error="streamable_http_missing_session_id",
|
|
275
|
+
)
|
|
276
|
+
self.effects.log(
|
|
277
|
+
"WARN",
|
|
278
|
+
f"channel_http_mcp_initialize_failed name={name} endpoint={endpoint} error=missing_mcp_session_id",
|
|
279
|
+
)
|
|
280
|
+
return
|
|
281
|
+
initialized = {
|
|
282
|
+
"jsonrpc": "2.0",
|
|
283
|
+
"method": "notifications/initialized",
|
|
284
|
+
"params": {},
|
|
285
|
+
}
|
|
286
|
+
self.http.streamable_post(
|
|
287
|
+
endpoint, headers, initialized, timeout, protocol_version, session_id
|
|
288
|
+
)
|
|
289
|
+
self.effects.set_state(
|
|
290
|
+
name,
|
|
291
|
+
mcp_endpoint=endpoint,
|
|
292
|
+
mcp_initialized=True,
|
|
293
|
+
mcp_session_id=session_id,
|
|
294
|
+
mcp_last_error=None,
|
|
295
|
+
mcp_rpc_results={},
|
|
296
|
+
)
|
|
297
|
+
self.effects.record_session(name, endpoint, session_id, protocol_version)
|
|
298
|
+
visible_session = session_id or "-"
|
|
299
|
+
self.effects.log(
|
|
300
|
+
"INFO",
|
|
301
|
+
f"channel_http_mcp_initialized name={name} endpoint={endpoint} session={visible_session}",
|
|
302
|
+
)
|
|
303
|
+
except urllib.error.HTTPError as exc:
|
|
304
|
+
if exc.code == 405:
|
|
305
|
+
self.effects.set_state(
|
|
306
|
+
name,
|
|
307
|
+
transport="sse",
|
|
308
|
+
mcp_endpoint="",
|
|
309
|
+
mcp_initialized=False,
|
|
310
|
+
mcp_session_id=None,
|
|
311
|
+
mcp_protocol_version=self.config.legacy_protocol,
|
|
312
|
+
mcp_last_error="streamable_http_405_fallback_sse",
|
|
313
|
+
)
|
|
314
|
+
self.effects.log(
|
|
315
|
+
"WARN",
|
|
316
|
+
f"channel_http_fallback_sse name={name} endpoint={endpoint} reason=HTTPError:405",
|
|
317
|
+
)
|
|
318
|
+
return
|
|
319
|
+
self.effects.set_state(
|
|
320
|
+
name,
|
|
321
|
+
mcp_endpoint=endpoint,
|
|
322
|
+
mcp_initialized=False,
|
|
323
|
+
mcp_last_error=f"HTTPError: {exc.code} {exc.reason}",
|
|
324
|
+
)
|
|
325
|
+
self.effects.log(
|
|
326
|
+
"WARN",
|
|
327
|
+
f"channel_http_mcp_initialize_failed name={name} endpoint={endpoint} error=HTTPError:{exc.code}: {exc.reason}",
|
|
328
|
+
)
|
|
329
|
+
except Exception as exc:
|
|
330
|
+
self.effects.set_state(
|
|
331
|
+
name,
|
|
332
|
+
mcp_endpoint=endpoint,
|
|
333
|
+
mcp_initialized=False,
|
|
334
|
+
mcp_last_error=f"{type(exc).__name__}: {exc}",
|
|
335
|
+
)
|
|
336
|
+
self.effects.log(
|
|
337
|
+
"WARN",
|
|
338
|
+
f"channel_http_mcp_initialize_failed name={name} endpoint={endpoint} error={type(exc).__name__}: {exc}",
|
|
339
|
+
)
|
|
340
|
+
|
|
341
|
+
def dispatch(
|
|
342
|
+
self,
|
|
343
|
+
name: str,
|
|
344
|
+
event_name: str,
|
|
345
|
+
data_lines: list[str],
|
|
346
|
+
event_id: str | None = None,
|
|
347
|
+
) -> None:
|
|
348
|
+
data_text = "\n".join(data_lines)
|
|
349
|
+
if event_id is not None:
|
|
350
|
+
with self.state.lock:
|
|
351
|
+
state = self.state.connections.get(name)
|
|
352
|
+
if state:
|
|
353
|
+
state["last_sse_event_id"] = str(event_id)
|
|
354
|
+
if (event_name or "").strip().lower() == "endpoint":
|
|
355
|
+
self.maybe_initialize(name, data_text)
|
|
356
|
+
return
|
|
357
|
+
if self.effects.store_response(name, data_text):
|
|
358
|
+
return
|
|
359
|
+
if str(name or "").strip().lower() in self.config.native_names:
|
|
360
|
+
self.effects.log(
|
|
361
|
+
"INFO",
|
|
362
|
+
f"channel_sse_message_ignored name={name} event={event_name or 'message'} reason=native_router_self_echo",
|
|
363
|
+
)
|
|
364
|
+
return
|
|
365
|
+
with self.state.lock:
|
|
366
|
+
state = self.state.connections.get(name)
|
|
367
|
+
if not state:
|
|
368
|
+
return
|
|
369
|
+
defaults = dict(state)
|
|
370
|
+
payload = self.effects.project_payload(
|
|
371
|
+
data_text, event_name, defaults, event_id=event_id
|
|
372
|
+
)
|
|
373
|
+
if not payload:
|
|
374
|
+
return
|
|
375
|
+
saved = self.effects.append_message(payload)
|
|
376
|
+
if saved.get("_ciel_runtime_duplicate"):
|
|
377
|
+
self.effects.log(
|
|
378
|
+
"INFO",
|
|
379
|
+
f"channel_sse_message_skipped_duplicate name={name} event={event_name or 'message'} existing_id={saved.get('id')} channel={saved.get('channel')}",
|
|
380
|
+
)
|
|
381
|
+
return
|
|
382
|
+
now = time.strftime("%Y-%m-%dT%H:%M:%S")
|
|
383
|
+
with self.state.lock:
|
|
384
|
+
state = self.state.connections.get(name)
|
|
385
|
+
if state:
|
|
386
|
+
state["last_event_at"] = now
|
|
387
|
+
state["messages_received"] = (
|
|
388
|
+
int(state.get("messages_received") or 0) + 1
|
|
389
|
+
)
|
|
390
|
+
state["last_error"] = None
|
|
391
|
+
self.effects.log(
|
|
392
|
+
"INFO",
|
|
393
|
+
f"channel_sse_message_received name={name} event={event_name or 'message'} message_id={saved.get('id')} channel={saved.get('channel')}",
|
|
394
|
+
)
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""Channel message duplicate-detection application service."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True, slots=True)
|
|
11
|
+
class ChannelMessageDedupePorts:
|
|
12
|
+
stable_key: Callable[[dict[str, Any]], Any]
|
|
13
|
+
fallback_key: Callable[[dict[str, Any]], Any]
|
|
14
|
+
recent_rows: Callable[[], list[dict[str, Any]]]
|
|
15
|
+
launch_guard: Callable[[], dict[str, Any] | None]
|
|
16
|
+
timestamp_seconds: Callable[[Any], float]
|
|
17
|
+
now: Callable[[], float]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True, slots=True)
|
|
21
|
+
class ChannelMessageDedupeService:
|
|
22
|
+
ports: ChannelMessageDedupePorts
|
|
23
|
+
fallback_ttl_seconds: float
|
|
24
|
+
|
|
25
|
+
def duplicate(
|
|
26
|
+
self,
|
|
27
|
+
message: dict[str, Any],
|
|
28
|
+
) -> dict[str, Any] | None:
|
|
29
|
+
stable_key = self.ports.stable_key(message)
|
|
30
|
+
fallback_key = self.ports.fallback_key(message)
|
|
31
|
+
if not stable_key and not fallback_key:
|
|
32
|
+
return None
|
|
33
|
+
now = self.ports.now()
|
|
34
|
+
launch_guard = (
|
|
35
|
+
self.ports.launch_guard() if fallback_key else None
|
|
36
|
+
)
|
|
37
|
+
guard_max_existing_id = (
|
|
38
|
+
int(launch_guard.get("max_existing_id") or 0)
|
|
39
|
+
if launch_guard
|
|
40
|
+
else 0
|
|
41
|
+
)
|
|
42
|
+
for row in reversed(self.ports.recent_rows()):
|
|
43
|
+
if stable_key and self.ports.stable_key(row) == stable_key:
|
|
44
|
+
return row
|
|
45
|
+
if (
|
|
46
|
+
not fallback_key
|
|
47
|
+
or self.ports.fallback_key(row) != fallback_key
|
|
48
|
+
):
|
|
49
|
+
continue
|
|
50
|
+
row_time = self.ports.timestamp_seconds(row.get("time"))
|
|
51
|
+
if (
|
|
52
|
+
row_time > 0
|
|
53
|
+
and now - row_time <= self.fallback_ttl_seconds
|
|
54
|
+
):
|
|
55
|
+
return row
|
|
56
|
+
try:
|
|
57
|
+
row_id = int(row.get("id") or 0)
|
|
58
|
+
except Exception:
|
|
59
|
+
row_id = 0
|
|
60
|
+
if (
|
|
61
|
+
guard_max_existing_id > 0
|
|
62
|
+
and 0 < row_id <= guard_max_existing_id
|
|
63
|
+
):
|
|
64
|
+
return row
|
|
65
|
+
return None
|