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.
Files changed (309) hide show
  1. opscloud/__init__.py +5 -0
  2. opscloud/__main__.py +6 -0
  3. opscloud/_constants.py +53 -0
  4. opscloud/_debug.py +145 -0
  5. opscloud/_debug_buffer.py +250 -0
  6. opscloud/_textual_patches.py +367 -0
  7. opscloud/_version.py +15 -0
  8. opscloud/agent/__init__.py +27 -0
  9. opscloud/agent/config.py +168 -0
  10. opscloud/agent/factory.py +1103 -0
  11. opscloud/approval_mode.py +44 -0
  12. opscloud/backend/__init__.py +49 -0
  13. opscloud/backend/composite.py +89 -0
  14. opscloud/backend/local.py +183 -0
  15. opscloud/backend/registry.py +79 -0
  16. opscloud/backend/sandbox_config.py +107 -0
  17. opscloud/backend/sandbox_factory.py +432 -0
  18. opscloud/built_in_skills/remember/SKILL.md +124 -0
  19. opscloud/built_in_skills/skill-creator/SKILL.md +348 -0
  20. opscloud/built_in_skills/skill-creator/scripts/init_skill.py +371 -0
  21. opscloud/built_in_skills/skill-creator/scripts/quick_validate.py +165 -0
  22. opscloud/cli/__init__.py +1 -0
  23. opscloud/cli/commands/__init__.py +15 -0
  24. opscloud/cli/commands/auth.py +45 -0
  25. opscloud/cli/commands/doctor.py +100 -0
  26. opscloud/cli/commands/mcp.py +137 -0
  27. opscloud/cli/commands/threads.py +51 -0
  28. opscloud/cli/interactive.py +154 -0
  29. opscloud/cli/main.py +903 -0
  30. opscloud/cli/non_interactive.py +425 -0
  31. opscloud/cli/server_manager.py +58 -0
  32. opscloud/commands/__init__.py +19 -0
  33. opscloud/commands/_base.py +110 -0
  34. opscloud/commands/_guards.py +52 -0
  35. opscloud/commands/_router.py +146 -0
  36. opscloud/commands/_types.py +36 -0
  37. opscloud/commands/core/__init__.py +47 -0
  38. opscloud/commands/core/auth.py +94 -0
  39. opscloud/commands/core/bug.py +46 -0
  40. opscloud/commands/core/clear.py +106 -0
  41. opscloud/commands/core/cloud.py +185 -0
  42. opscloud/commands/core/compact.py +128 -0
  43. opscloud/commands/core/config_cmd.py +120 -0
  44. opscloud/commands/core/context.py +220 -0
  45. opscloud/commands/core/cost.py +44 -0
  46. opscloud/commands/core/doctor.py +535 -0
  47. opscloud/commands/core/effort.py +108 -0
  48. opscloud/commands/core/exit_cmd.py +41 -0
  49. opscloud/commands/core/fast.py +148 -0
  50. opscloud/commands/core/help_cmd.py +94 -0
  51. opscloud/commands/core/mcp.py +134 -0
  52. opscloud/commands/core/model.py +109 -0
  53. opscloud/commands/core/permissions.py +196 -0
  54. opscloud/commands/core/plugins.py +92 -0
  55. opscloud/commands/core/pool.py +108 -0
  56. opscloud/commands/core/resume.py +97 -0
  57. opscloud/commands/core/skills.py +178 -0
  58. opscloud/commands/power/__init__.py +51 -0
  59. opscloud/commands/power/agents.py +98 -0
  60. opscloud/commands/power/btw.py +85 -0
  61. opscloud/commands/power/copy.py +97 -0
  62. opscloud/commands/power/goal.py +548 -0
  63. opscloud/commands/power/loop.py +295 -0
  64. opscloud/commands/power/memory.py +287 -0
  65. opscloud/commands/power/review.py +152 -0
  66. opscloud/commands/power/rubric.py +287 -0
  67. opscloud/commands/power/runtime.py +321 -0
  68. opscloud/commands/power/skill_creator.py +70 -0
  69. opscloud/commands/power/skill_invoke.py +165 -0
  70. opscloud/commands/power/tasks.py +209 -0
  71. opscloud/commands/power/trace.py +117 -0
  72. opscloud/commands/power/ui_toggles.py +135 -0
  73. opscloud/commands/power/version.py +52 -0
  74. opscloud/commands/registry.py +176 -0
  75. opscloud/config/__init__.py +114 -0
  76. opscloud/config/adapters/__init__.py +5 -0
  77. opscloud/config/adapters/postgres.py +776 -0
  78. opscloud/config/adapters/sqlite.py +769 -0
  79. opscloud/config/aws.py +248 -0
  80. opscloud/config/cloud_profiles.py +437 -0
  81. opscloud/config/env_vars.py +81 -0
  82. opscloud/config/langsmith.py +319 -0
  83. opscloud/config/manifest.py +817 -0
  84. opscloud/config/metadata.py +205 -0
  85. opscloud/config/paths.py +604 -0
  86. opscloud/config/plugins.py +130 -0
  87. opscloud/config/settings.py +834 -0
  88. opscloud/config/store.py +453 -0
  89. opscloud/config/store_factory.py +97 -0
  90. opscloud/config/toml_config.py +562 -0
  91. opscloud/editor.py +173 -0
  92. opscloud/exceptions.py +65 -0
  93. opscloud/file_ops.py +217 -0
  94. opscloud/hooks/__init__.py +139 -0
  95. opscloud/hooks/env.py +44 -0
  96. opscloud/hooks/interrupt.py +121 -0
  97. opscloud/hooks/models/__init__.py +109 -0
  98. opscloud/hooks/models/adapters.py +19 -0
  99. opscloud/hooks/models/config.py +79 -0
  100. opscloud/hooks/models/domain.py +434 -0
  101. opscloud/hooks/models/transport.py +37 -0
  102. opscloud/hooks/tools.py +231 -0
  103. opscloud/input.py +48 -0
  104. opscloud/integrations/__init__.py +80 -0
  105. opscloud/integrations/base.py +153 -0
  106. opscloud/integrations/content_filter.py +119 -0
  107. opscloud/integrations/event_bus.py +480 -0
  108. opscloud/integrations/hooks.py +238 -0
  109. opscloud/integrations/identity.py +181 -0
  110. opscloud/integrations/notifications.py +134 -0
  111. opscloud/integrations/stream_bridge.py +397 -0
  112. opscloud/integrations/thread_store.py +81 -0
  113. opscloud/json_types.py +20 -0
  114. opscloud/mcp/__init__.py +57 -0
  115. opscloud/mcp/config.py +203 -0
  116. opscloud/mcp/discovery.py +229 -0
  117. opscloud/mcp/mcp_info.py +109 -0
  118. opscloud/mcp/middleware.py +102 -0
  119. opscloud/mcp/preload.py +505 -0
  120. opscloud/mcp/raw_config.py +149 -0
  121. opscloud/mcp/semantic_profiler.py +305 -0
  122. opscloud/mcp/session_manager.py +727 -0
  123. opscloud/mcp/trust.py +105 -0
  124. opscloud/memory/__init__.py +27 -0
  125. opscloud/memory/branch.py +61 -0
  126. opscloud/memory/guard.py +19 -0
  127. opscloud/memory/onboarding.py +120 -0
  128. opscloud/memory/registry.py +134 -0
  129. opscloud/memory/store.py +223 -0
  130. opscloud/middleware/__init__.py +106 -0
  131. opscloud/middleware/_repository_bounds.py +459 -0
  132. opscloud/middleware/ask_user.py +152 -0
  133. opscloud/middleware/auto_mode.py +60 -0
  134. opscloud/middleware/auto_mode_hitl.py +2434 -0
  135. opscloud/middleware/compaction.py +566 -0
  136. opscloud/middleware/configurable_model.py +233 -0
  137. opscloud/middleware/cost_tracking.py +1021 -0
  138. opscloud/middleware/glm_stall_recovery.py +109 -0
  139. opscloud/middleware/goal_criteria.py +1234 -0
  140. opscloud/middleware/goal_state_notice.py +712 -0
  141. opscloud/middleware/goal_tools.py +407 -0
  142. opscloud/middleware/headless_mcp_guard.py +88 -0
  143. opscloud/middleware/jev_model_router.py +1018 -0
  144. opscloud/middleware/local_context.py +460 -0
  145. opscloud/middleware/mcp_context.py +151 -0
  146. opscloud/middleware/mcp_middleware.py +146 -0
  147. opscloud/middleware/memory_guard.py +393 -0
  148. opscloud/middleware/model_retry.py +205 -0
  149. opscloud/middleware/registry.py +137 -0
  150. opscloud/middleware/reliable_rubric.py +709 -0
  151. opscloud/middleware/resume_state.py +91 -0
  152. opscloud/middleware/server_hooks.py +1189 -0
  153. opscloud/middleware/shell_allow_list.py +90 -0
  154. opscloud/middleware/skills.py +382 -0
  155. opscloud/middleware/subagent_artifacts.py +84 -0
  156. opscloud/middleware/subagent_telemetry.py +261 -0
  157. opscloud/middleware/subagents.py +243 -0
  158. opscloud/middleware/tool_filter.py +384 -0
  159. opscloud/middleware/unified_system_message.py +62 -0
  160. opscloud/model/__init__.py +37 -0
  161. opscloud/model/config.py +2992 -0
  162. opscloud/model/factory.py +477 -0
  163. opscloud/model/pool.py +459 -0
  164. opscloud/model/reasoning.py +282 -0
  165. opscloud/offload.py +173 -0
  166. opscloud/output.py +64 -0
  167. opscloud/plugins/__init__.py +189 -0
  168. opscloud/plugins/_json.py +22 -0
  169. opscloud/plugins/adapters/__init__.py +13 -0
  170. opscloud/plugins/adapters/agents.py +112 -0
  171. opscloud/plugins/adapters/commands.py +190 -0
  172. opscloud/plugins/adapters/mcp.py +363 -0
  173. opscloud/plugins/adapters/skills.py +132 -0
  174. opscloud/plugins/commands_cli.py +541 -0
  175. opscloud/plugins/discovery.py +512 -0
  176. opscloud/plugins/manifest.py +335 -0
  177. opscloud/plugins/marketplace.py +893 -0
  178. opscloud/plugins/models.py +334 -0
  179. opscloud/plugins/project_plugins.py +317 -0
  180. opscloud/plugins/protocol.py +31 -0
  181. opscloud/plugins/store.py +905 -0
  182. opscloud/plugins/substitution.py +122 -0
  183. opscloud/project_utils.py +145 -0
  184. opscloud/prompts/__init__.py +37 -0
  185. opscloud/prompts/system.py +483 -0
  186. opscloud/prompts/templates/system_prompt.md +166 -0
  187. opscloud/prompts/types.py +54 -0
  188. opscloud/rubrics/__init__.py +42 -0
  189. opscloud/rubrics/evaluator.py +404 -0
  190. opscloud/rubrics/evidence_extractor.py +229 -0
  191. opscloud/rubrics/generator.py +239 -0
  192. opscloud/rubrics/jev_compiler.py +175 -0
  193. opscloud/rubrics/jev_grader.py +297 -0
  194. opscloud/rubrics/middleware.py +8 -0
  195. opscloud/schema/__init__.py +107 -0
  196. opscloud/schema/interrupts.py +981 -0
  197. opscloud/security/__init__.py +91 -0
  198. opscloud/security/approval_mode.py +528 -0
  199. opscloud/security/approval_mode_source.py +230 -0
  200. opscloud/security/cli_ast_evaluator.py +552 -0
  201. opscloud/security/jev_classifier.py +651 -0
  202. opscloud/security/shell_safety.py +340 -0
  203. opscloud/security/unicode_security.py +439 -0
  204. opscloud/security/url_validation.py +160 -0
  205. opscloud/server/__init__.py +13 -0
  206. opscloud/server/_server_config.py +158 -0
  207. opscloud/server/server.py +242 -0
  208. opscloud/server/server_graph.py +93 -0
  209. opscloud/skills/__init__.py +91 -0
  210. opscloud/skills/commands.py +1099 -0
  211. opscloud/skills/invocation.py +200 -0
  212. opscloud/skills/load.py +388 -0
  213. opscloud/skills/loader.py +23 -0
  214. opscloud/skills/merge.py +65 -0
  215. opscloud/skills/registry.py +263 -0
  216. opscloud/skills/sources.py +174 -0
  217. opscloud/skills/trust.py +417 -0
  218. opscloud/state/__init__.py +142 -0
  219. opscloud/state/base.py +154 -0
  220. opscloud/state/cloud_context.py +262 -0
  221. opscloud/state/goal_channels.py +124 -0
  222. opscloud/state/goal_state_limits.py +175 -0
  223. opscloud/state/resume_state.py +58 -0
  224. opscloud/state/service_context.py +281 -0
  225. opscloud/state/session.py +957 -0
  226. opscloud/state/state_migration.py +86 -0
  227. opscloud/subagents/__init__.py +30 -0
  228. opscloud/subagents/loader.py +331 -0
  229. opscloud/subagents/subagents_parser.py +117 -0
  230. opscloud/subagents/types.py +25 -0
  231. opscloud/tools/__init__.py +36 -0
  232. opscloud/tools/catalog.py +37 -0
  233. opscloud/tools/display.py +24 -0
  234. opscloud/tools/fetch_url.py +176 -0
  235. opscloud/tools/goal_tools.py +423 -0
  236. opscloud/tools/registry.py +105 -0
  237. opscloud/tools/thread.py +32 -0
  238. opscloud/tools/web_search.py +215 -0
  239. opscloud/ui/__init__.py +119 -0
  240. opscloud/ui/app.py +5400 -0
  241. opscloud/ui/app.tcss +479 -0
  242. opscloud/ui/clipboard.py +147 -0
  243. opscloud/ui/command_registry.py +482 -0
  244. opscloud/ui/message_store.py +152 -0
  245. opscloud/ui/permission_store.py +360 -0
  246. opscloud/ui/preferences.py +113 -0
  247. opscloud/ui/prompt_manager.py +96 -0
  248. opscloud/ui/remote_client.py +505 -0
  249. opscloud/ui/textual_adapter.py +1269 -0
  250. opscloud/ui/theme.py +728 -0
  251. opscloud/ui/ui_help.py +415 -0
  252. opscloud/ui/widgets/__init__.py +383 -0
  253. opscloud/ui/widgets/_ask_user_types.py +81 -0
  254. opscloud/ui/widgets/_copy_spans.py +52 -0
  255. opscloud/ui/widgets/_inline_prompt.py +290 -0
  256. opscloud/ui/widgets/_js_eval_display.py +139 -0
  257. opscloud/ui/widgets/_links.py +175 -0
  258. opscloud/ui/widgets/_tool_stream.py +308 -0
  259. opscloud/ui/widgets/agent_selector.py +297 -0
  260. opscloud/ui/widgets/approval.py +741 -0
  261. opscloud/ui/widgets/ask_user.py +539 -0
  262. opscloud/ui/widgets/auth.py +695 -0
  263. opscloud/ui/widgets/auth_manager.py +214 -0
  264. opscloud/ui/widgets/auto_mode_notice.py +146 -0
  265. opscloud/ui/widgets/autocomplete.py +709 -0
  266. opscloud/ui/widgets/chat_input.py +805 -0
  267. opscloud/ui/widgets/cloud_selector.py +528 -0
  268. opscloud/ui/widgets/config_manager.py +652 -0
  269. opscloud/ui/widgets/debug_console.py +1269 -0
  270. opscloud/ui/widgets/devops_renderers.py +184 -0
  271. opscloud/ui/widgets/diff.py +237 -0
  272. opscloud/ui/widgets/effort_selector.py +166 -0
  273. opscloud/ui/widgets/goal_review.py +612 -0
  274. opscloud/ui/widgets/goal_status.py +57 -0
  275. opscloud/ui/widgets/infra_panel.py +141 -0
  276. opscloud/ui/widgets/install_confirm.py +114 -0
  277. opscloud/ui/widgets/loading.py +170 -0
  278. opscloud/ui/widgets/mcp_viewer.py +1204 -0
  279. opscloud/ui/widgets/messages.py +4461 -0
  280. opscloud/ui/widgets/model_selector.py +863 -0
  281. opscloud/ui/widgets/notification_center.py +108 -0
  282. opscloud/ui/widgets/notification_settings.py +119 -0
  283. opscloud/ui/widgets/operation_card.py +106 -0
  284. opscloud/ui/widgets/permissions_manager.py +667 -0
  285. opscloud/ui/widgets/plugin_manager.py +1212 -0
  286. opscloud/ui/widgets/pool_selector.py +376 -0
  287. opscloud/ui/widgets/preamble.py +53 -0
  288. opscloud/ui/widgets/skills_viewer.py +432 -0
  289. opscloud/ui/widgets/status.py +1037 -0
  290. opscloud/ui/widgets/subagent_panel.py +927 -0
  291. opscloud/ui/widgets/theme_selector.py +232 -0
  292. opscloud/ui/widgets/thread_selector.py +358 -0
  293. opscloud/ui/widgets/toast.py +31 -0
  294. opscloud/ui/widgets/tool_display.py +325 -0
  295. opscloud/ui/widgets/tool_renderers.py +234 -0
  296. opscloud/ui/widgets/tool_widgets.py +278 -0
  297. opscloud/ui/widgets/welcome.py +421 -0
  298. opscloud/ui/widgets/welcome_popup.py +102 -0
  299. opscloud/utils/__init__.py +24 -0
  300. opscloud/utils/cost_estimation.py +169 -0
  301. opscloud/utils/git.py +390 -0
  302. opscloud/utils/logger.py +571 -0
  303. opscloud/utils/session_stats.py +115 -0
  304. opscloud/utils/startup_error.py +37 -0
  305. talkops_opscloud-0.3.0b1.dist-info/METADATA +449 -0
  306. talkops_opscloud-0.3.0b1.dist-info/RECORD +309 -0
  307. talkops_opscloud-0.3.0b1.dist-info/WHEEL +4 -0
  308. talkops_opscloud-0.3.0b1.dist-info/entry_points.txt +2 -0
  309. talkops_opscloud-0.3.0b1.dist-info/licenses/LICENSE +175 -0
opscloud/model/pool.py ADDED
@@ -0,0 +1,459 @@
1
+ """Dynamic Model Pool Manager for Jev-powered autonomous model routing.
2
+
3
+ Discovers candidate models and provider-native reasoning parameters directly
4
+ from OpsCloud's model capability registry (AVAILABLE_MODELS and MODEL_PROFILES)
5
+ without hardcoding provider model strings.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from dataclasses import dataclass
11
+ import re
12
+ import threading
13
+ from typing import Any
14
+
15
+ from langchain_core.language_models import BaseChatModel
16
+ from langchain_typesafe.experimental.middleware import ModelChoice
17
+
18
+ from opscloud.model.config import (
19
+ AVAILABLE_MODELS,
20
+ DEFAULT_PROVIDER_PRIORITY,
21
+ detect_provider,
22
+ get_model_profile,
23
+ has_provider_credentials,
24
+ normalize_model_spec,
25
+ )
26
+ from opscloud.model.factory import ModelResult, create_model
27
+ from opscloud.model.reasoning import (
28
+ default_effort_for_model,
29
+ supported_efforts_for_model,
30
+ with_effort_model_params,
31
+ )
32
+ from opscloud.utils.logger import get_logger
33
+
34
+ logger = get_logger(__name__)
35
+
36
+
37
+ ROUTER_INSTRUCTIONS = (
38
+ "Select the optimal operational tier based on task complexity and reasoning requirements. "
39
+ "Match conversational, read-only, and trivial queries to 'fast'. "
40
+ "Match focused, routine, and single-scope coding or operational tasks to 'standard'. "
41
+ "Match multi-agent delegation, comprehensive audits and optimization (cost, performance, security), "
42
+ "complex debugging, and cross-system architectural refactoring to 'powerful'."
43
+ )
44
+
45
+ # ── Orchestrator Route Criteria (Main Supervisor Agent) ─────────────────────────
46
+
47
+ ORCHESTRATOR_ROUTE_CRITERIA: dict[str, str] = {
48
+ "fast": (
49
+ "Conversational greetings, capability inquiries ('who are you?', 'what can you do?'), "
50
+ "read-only workspace exploration (viewing files, checking git status/diff, directory listing), "
51
+ "trivial syntax checks, or general explanations requiring no code modifications, "
52
+ "no multi-step operations, and no subagent delegation."
53
+ ),
54
+ "standard": (
55
+ "Focused single-domain coding or operational tasks, editing a single file, script, "
56
+ "manifest, or configuration (e.g. localized bug fix, Dockerfile update), routine resource "
57
+ "inspection and state queries, executing pre-defined workflows, or delegating a narrow, "
58
+ "well-defined single-step task."
59
+ ),
60
+ "powerful": (
61
+ "Multi-agent delegation and coordination (e.g. planning and launching specialized agents), "
62
+ "comprehensive system assessments (cost/FinOps optimization, waste analysis, performance "
63
+ "tuning, security compliance), complex debugging and root-cause analysis (cascading failures, "
64
+ "subtle code bugs, error spikes), architectural code refactoring across multiple modules, "
65
+ "and any workflow requiring cross-service synthesis, blast-radius safeguards, and structured "
66
+ "deliverable authoring."
67
+ ),
68
+ }
69
+
70
+ # ── Subagent Route Criteria (Specialized Worker Subagents) ──────────────────────
71
+
72
+ SUBAGENT_ROUTE_CRITERIA: dict[str, str] = {
73
+ "fast": (
74
+ "Direct single-item lookups, checking individual resource/component status, "
75
+ "verifying credentials or environment variables, or reading local configuration files."
76
+ ),
77
+ "standard": (
78
+ "Domain-specific workflows, running multiple tools or analysis steps in sequence, "
79
+ "auditing configurations or resources across a domain, generating inventory data, "
80
+ "and formatting structured markdown reports."
81
+ ),
82
+ "powerful": (
83
+ "Deep cross-service root-cause correlation, multi-variable mathematical, financial, "
84
+ "or capacity modeling, complex code/state reconciliation, parsing large log or trace "
85
+ "outputs to isolate transient issues, or executing high-stakes destructive operations "
86
+ "requiring blast-radius calculation."
87
+ ),
88
+ }
89
+
90
+ # Backward compatibility alias
91
+ DEFAULT_ROUTE_CRITERIA = ORCHESTRATOR_ROUTE_CRITERIA
92
+
93
+ ROUTE_TO_TIER_MAP: dict[str, int] = {
94
+ "fast": 0,
95
+ "standard": 1,
96
+ "powerful": 2,
97
+ "tier_0_fast": 0,
98
+ "tier_1_standard": 1,
99
+ "tier_2_deep": 2,
100
+ }
101
+
102
+ TIER_TO_ROUTE_MAP: dict[int, str] = {
103
+ 0: "fast",
104
+ 1: "standard",
105
+ 2: "powerful",
106
+ }
107
+
108
+
109
+ @dataclass(frozen=True)
110
+ class TierResolution:
111
+ """Resolved model spec and reasoning effort for a specific operational tier."""
112
+
113
+ tier: int
114
+ spec: str
115
+ effort: str
116
+ is_reasoning_enabled: bool
117
+
118
+
119
+ class DynamicModelPoolManager:
120
+ """Manages dynamic model tiering and hot instances for the Jev model router."""
121
+
122
+ def __init__(self) -> None:
123
+ self._instances: dict[str, ModelResult] = {}
124
+ self._lock = threading.Lock()
125
+
126
+ def _resolve_provider(self, provider: str | None = None, base_spec: str | None = None) -> str:
127
+ """Determine the active provider from explicit parameter, base spec, or credentials across all supported providers."""
128
+ if provider:
129
+ return provider
130
+ if base_spec and base_spec not in ("dynamic", "auto", "default"):
131
+ inferred = detect_provider(base_spec)
132
+ if inferred and inferred not in ("dynamic", "auto"):
133
+ return inferred
134
+ if ":" in base_spec:
135
+ cand = base_spec.split(":", 1)[0]
136
+ if cand in AVAILABLE_MODELS:
137
+ return cand
138
+
139
+ # Check configured model in ModelConfig or Settings if credentials exist
140
+ try:
141
+ from opscloud.config.settings import get_settings
142
+ from opscloud.model.config import ModelConfig
143
+
144
+ config = ModelConfig.load()
145
+ settings = get_settings()
146
+ for cand in (
147
+ getattr(settings, "model_name", None),
148
+ getattr(settings, "model", None),
149
+ config.default_model,
150
+ config.recent_model,
151
+ ):
152
+ if cand and cand not in ("dynamic", "auto", "default"):
153
+ cand_prov = detect_provider(cand) or (cand.split(":", 1)[0] if ":" in cand else None)
154
+ if cand_prov and cand_prov in AVAILABLE_MODELS and has_provider_credentials(cand_prov) is True:
155
+ return cand_prov
156
+ except Exception as exc:
157
+ logger.debug("Failed inspecting configured default/recent model for provider resolution: %s", exc)
158
+
159
+ all_supported = [p for p in DEFAULT_PROVIDER_PRIORITY if p in AVAILABLE_MODELS] + [
160
+ p for p in AVAILABLE_MODELS if p not in DEFAULT_PROVIDER_PRIORITY
161
+ ]
162
+ for prov in all_supported:
163
+ if has_provider_credentials(prov) is True:
164
+ return prov
165
+
166
+ return "google_genai"
167
+
168
+ def discover_tiers(
169
+ self,
170
+ provider: str | None = None,
171
+ base_spec: str | None = None,
172
+ ) -> dict[int, tuple[str, str]]:
173
+ """Dynamically build Tier 0, 1, and 2 definitions from registry metadata.
174
+
175
+ Args:
176
+ provider: Target provider name, or None to infer.
177
+ base_spec: Baseline model spec selected by the session/thread.
178
+
179
+ Returns:
180
+ Dictionary mapping tier level (0, 1, 2) to (model_spec, reasoning_effort).
181
+ """
182
+ # 1. Prioritize user-configured pool from config.toml [agent_pool]
183
+ from opscloud.config.toml_config import load_agent_pool
184
+
185
+ user_pool = load_agent_pool()
186
+ if user_pool:
187
+ resolved_provider = self._resolve_provider(provider, base_spec)
188
+ pool_prov = user_pool.get("provider")
189
+ if not pool_prov:
190
+ sample = user_pool.get("standard") or user_pool.get("fast") or user_pool.get("powerful") or ""
191
+ pool_prov = detect_provider(sample) if sample else None
192
+
193
+ if not provider or not pool_prov or pool_prov == resolved_provider:
194
+ t0_raw = user_pool.get("fast")
195
+ t1_raw = user_pool.get("standard")
196
+ t2_raw = user_pool.get("powerful")
197
+
198
+ def _norm(raw_spec: str) -> str:
199
+ s = raw_spec.strip()
200
+ if ":" not in s:
201
+ return f"{resolved_provider}:{s}"
202
+ return normalize_model_spec(s)
203
+
204
+ if t0_raw and t1_raw and t2_raw:
205
+ t0 = _norm(t0_raw)
206
+ t1 = _norm(t1_raw)
207
+ t2 = _norm(t2_raw)
208
+
209
+ t0_efforts = supported_efforts_for_model(t0)
210
+ e0 = "off" if ("off" in t0_efforts or not t0_efforts) else ("minimal" if "minimal" in t0_efforts else "off")
211
+
212
+ t1_efforts = supported_efforts_for_model(t1)
213
+ e1 = "medium" if "medium" in t1_efforts else (default_effort_for_model(t1) or "off")
214
+
215
+ t2_efforts = supported_efforts_for_model(t2)
216
+ e2 = "max" if "max" in t2_efforts else ("high" if "high" in t2_efforts else ("medium" if "medium" in t2_efforts else (default_effort_for_model(t2) or "off")))
217
+
218
+ return {
219
+ 0: (t0, e0),
220
+ 1: (t1, e1),
221
+ 2: (t2, e2),
222
+ }
223
+
224
+ all_supported = [p for p in DEFAULT_PROVIDER_PRIORITY if p in AVAILABLE_MODELS] + [
225
+ p for p in AVAILABLE_MODELS if p not in DEFAULT_PROVIDER_PRIORITY
226
+ ]
227
+
228
+ if provider in ("dynamic", "auto") or base_spec in ("dynamic", "auto"):
229
+ specs = []
230
+ for prov in all_supported:
231
+ if has_provider_credentials(prov) is True:
232
+ specs.extend([f"{prov}:{m[0]}" for m in AVAILABLE_MODELS.get(prov, [])])
233
+ else:
234
+ resolved_provider = self._resolve_provider(provider, base_spec)
235
+ models = AVAILABLE_MODELS.get(resolved_provider, [])
236
+ specs = [f"{resolved_provider}:{m[0]}" for m in models]
237
+
238
+ if not specs and base_spec:
239
+ specs = [normalize_model_spec(base_spec)]
240
+ elif not specs:
241
+ specs = ["google_genai:gemini-2.5-flash"]
242
+
243
+ pro_tokens = {"pro", "opus", "o3", "astra", "sol", "reasoner", "r1"}
244
+ fast_tokens = {"flash", "mini", "haiku", "lite", "micro", "scout", "small"}
245
+
246
+ def _spec_tokens(s: str) -> set[str]:
247
+ name = s.split(":", 1)[1] if ":" in s else s
248
+ tokens = set(re.split(r"[-_:/]", name.lower()))
249
+ sub_tokens: set[str] = set()
250
+ for tok in tokens:
251
+ if "." in tok:
252
+ sub_tokens.update(tok.split("."))
253
+ return tokens | sub_tokens
254
+
255
+ tier2_pro_candidates: list[str] = []
256
+ tier2_fallback_candidates: list[str] = []
257
+ tier1_candidates: list[str] = []
258
+ tier0_candidates: list[str] = []
259
+
260
+ for spec in specs:
261
+ tokens = _spec_tokens(spec)
262
+ is_pro = bool(tokens & pro_tokens)
263
+ is_fast = bool(tokens & fast_tokens)
264
+ prof_entry = get_model_profile(spec)
265
+ prof = prof_entry["profile"] if prof_entry else {}
266
+ has_reasoning = prof.get("reasoning_output", False) or bool(supported_efforts_for_model(spec))
267
+
268
+ if is_pro:
269
+ tier2_pro_candidates.append(spec)
270
+ elif has_reasoning:
271
+ tier2_fallback_candidates.append(spec)
272
+
273
+ if (is_fast and has_reasoning) or (not is_pro and not is_fast):
274
+ tier1_candidates.append(spec)
275
+
276
+ if is_fast:
277
+ tier0_candidates.append(spec)
278
+
279
+ normalized_base = normalize_model_spec(base_spec) if base_spec and base_spec not in ("dynamic", "auto", "default") else None
280
+
281
+ # Tier 2 (Frontier Deep Reasoning): prioritize true Pro/Flagship models with reasoning effort
282
+ tier2_reasoning_pro = [s for s in tier2_pro_candidates if supported_efforts_for_model(s)]
283
+ if normalized_base and normalized_base in tier2_reasoning_pro:
284
+ t2 = normalized_base
285
+ elif tier2_reasoning_pro:
286
+ t2 = tier2_reasoning_pro[0]
287
+ elif normalized_base and normalized_base in tier2_pro_candidates:
288
+ t2 = normalized_base
289
+ elif tier2_pro_candidates:
290
+ t2 = tier2_pro_candidates[0]
291
+ elif normalized_base and normalized_base in tier2_fallback_candidates:
292
+ t2 = normalized_base
293
+ elif tier2_fallback_candidates:
294
+ t2 = tier2_fallback_candidates[0]
295
+ else:
296
+ t2 = normalized_base or specs[0]
297
+
298
+ # Tier 1 (Standard Execution): prefer fast reasoning model distinct from T2
299
+ t1_options = [s for s in tier1_candidates if s != t2]
300
+ if normalized_base and normalized_base in t1_options:
301
+ t1 = normalized_base
302
+ elif t1_options:
303
+ t1 = t1_options[0]
304
+ else:
305
+ t1 = tier1_candidates[0] if tier1_candidates else specs[0]
306
+
307
+ # Tier 0 (Fast Reflex): prefer fast model distinct from T1 and T2
308
+ t0_options = [s for s in tier0_candidates if s != t1 and s != t2]
309
+ if normalized_base and normalized_base in t0_options:
310
+ t0 = normalized_base
311
+ elif t0_options:
312
+ fast_priority_tokens = {"3.6", "lite", "flash-lite", "micro", "nano", "mini", "haiku"}
313
+ t0 = next((s for s in t0_options if bool(_spec_tokens(s) & fast_priority_tokens)), t0_options[0])
314
+ else:
315
+ t0 = tier0_candidates[0] if tier0_candidates else specs[-1]
316
+
317
+ # Resolve reasoning efforts dynamically from model capabilities
318
+ # Tier 0: Disable reasoning to eliminate latency and cost on reflex queries
319
+ e0 = "off"
320
+
321
+ # Tier 1: Standard reasoning (profile default, medium, or low)
322
+ t1_efforts = supported_efforts_for_model(t1)
323
+ if "medium" in t1_efforts:
324
+ e1 = "medium"
325
+ elif "low" in t1_efforts:
326
+ e1 = "low"
327
+ else:
328
+ e1 = default_effort_for_model(t1) or "off"
329
+
330
+ # Tier 2: Deep frontier reasoning (prefer max > xhigh > high > medium)
331
+ t2_efforts = supported_efforts_for_model(t2)
332
+ if "max" in t2_efforts:
333
+ e2 = "max"
334
+ elif "xhigh" in t2_efforts:
335
+ e2 = "xhigh"
336
+ elif "high" in t2_efforts:
337
+ e2 = "high"
338
+ elif "medium" in t2_efforts:
339
+ e2 = "medium"
340
+ else:
341
+ e2 = default_effort_for_model(t2) or "off"
342
+
343
+ return {
344
+ 0: (t0, e0),
345
+ 1: (t1, e1),
346
+ 2: (t2, e2),
347
+ }
348
+
349
+ def get_model_for_tier(
350
+ self,
351
+ tier: int,
352
+ provider: str | None = None,
353
+ base_spec: str | None = None,
354
+ requested_effort: str | None = None,
355
+ ) -> tuple[BaseChatModel, str, str, dict[str, Any]]:
356
+ """Instantiate or retrieve a cached model instance for the target tier.
357
+
358
+ Returns:
359
+ tuple[BaseChatModel, selected_spec, effort_level, model_settings]
360
+ """
361
+ tiers = self.discover_tiers(provider=provider, base_spec=base_spec)
362
+ selected_spec, default_effort = tiers.get(tier, tiers.get(1, (base_spec or "google_genai:gemini-2.5-flash", "off")))
363
+ effort = requested_effort or default_effort
364
+ if tier == 0 or effort in ("off", "none", "clear", "0", "reset") or not supported_efforts_for_model(selected_spec):
365
+ model_settings = {}
366
+ else:
367
+ model_settings = with_effort_model_params(selected_spec, {}, effort)
368
+
369
+ cache_key = f"{selected_spec}::effort={effort}"
370
+ with self._lock:
371
+ cached = self._instances.get(cache_key)
372
+ if cached is None:
373
+ try:
374
+ res = create_model(selected_spec, extra_kwargs=model_settings or None)
375
+ self._instances[cache_key] = res
376
+ cached = res
377
+ except Exception as exc:
378
+ logger.warning(
379
+ "Failed to instantiate model %s with effort %s: %s; falling back to base model",
380
+ selected_spec,
381
+ effort,
382
+ exc,
383
+ )
384
+ cached = create_model(base_spec)
385
+ selected_spec = cached.model_name
386
+ effort = "off"
387
+ model_settings = {}
388
+
389
+ return cached.model, selected_spec, effort, model_settings
390
+
391
+ def get_model_choices(
392
+ self,
393
+ provider: str | None = None,
394
+ base_spec: str | None = None,
395
+ criteria: dict[str, str] | None = None,
396
+ is_subagent: bool = False,
397
+ ) -> dict[str, ModelChoice]:
398
+ """Build dictionary of ModelChoice instances for ModelRouterMiddleware.
399
+
400
+ Resolves distinct provider models and reasoning settings from the pool
401
+ and maps them to standard routing keys ('fast', 'standard', 'powerful').
402
+ The candidate model spec and reasoning effort are explicitly embedded in
403
+ the criteria so the candidate model names appear on the input payload sent to Jev.
404
+ """
405
+ if criteria is not None:
406
+ criteria_map = criteria
407
+ else:
408
+ criteria_map = SUBAGENT_ROUTE_CRITERIA if is_subagent else ORCHESTRATOR_ROUTE_CRITERIA
409
+
410
+ fallback_criteria = SUBAGENT_ROUTE_CRITERIA if is_subagent else ORCHESTRATOR_ROUTE_CRITERIA
411
+
412
+ m0, spec0, eff0, _ = self.get_model_for_tier(0, provider=provider, base_spec=base_spec)
413
+ m1, spec1, eff1, _ = self.get_model_for_tier(1, provider=provider, base_spec=base_spec)
414
+ m2, spec2, eff2, _ = self.get_model_for_tier(2, provider=provider, base_spec=base_spec)
415
+
416
+ def _format_criteria(spec: str, eff: str, base_desc: str) -> str:
417
+ if base_desc.startswith("Model:"):
418
+ return base_desc
419
+ return f"Model: {spec} (effort: {eff}). {base_desc}"
420
+
421
+ return {
422
+ "fast": ModelChoice(
423
+ model=m0,
424
+ criteria=_format_criteria(spec0, eff0, criteria_map.get("fast", fallback_criteria["fast"])),
425
+ ),
426
+ "standard": ModelChoice(
427
+ model=m1,
428
+ criteria=_format_criteria(spec1, eff1, criteria_map.get("standard", fallback_criteria["standard"])),
429
+ ),
430
+ "powerful": ModelChoice(
431
+ model=m2,
432
+ criteria=_format_criteria(spec2, eff2, criteria_map.get("powerful", fallback_criteria["powerful"])),
433
+ ),
434
+ }
435
+
436
+
437
+ _GLOBAL_POOL_MANAGER: DynamicModelPoolManager | None = None
438
+
439
+
440
+ def get_model_pool_manager() -> DynamicModelPoolManager:
441
+ """Return singleton instance of DynamicModelPoolManager."""
442
+ global _GLOBAL_POOL_MANAGER
443
+ if _GLOBAL_POOL_MANAGER is None:
444
+ _GLOBAL_POOL_MANAGER = DynamicModelPoolManager()
445
+ return _GLOBAL_POOL_MANAGER
446
+
447
+
448
+ __all__ = [
449
+ "DEFAULT_ROUTE_CRITERIA",
450
+ "DynamicModelPoolManager",
451
+ "ModelChoice",
452
+ "ORCHESTRATOR_ROUTE_CRITERIA",
453
+ "ROUTER_INSTRUCTIONS",
454
+ "ROUTE_TO_TIER_MAP",
455
+ "SUBAGENT_ROUTE_CRITERIA",
456
+ "TIER_TO_ROUTE_MAP",
457
+ "TierResolution",
458
+ "get_model_pool_manager",
459
+ ]