modelglot 7.0.0__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 (571) hide show
  1. modelglot/__init__.py +1 -0
  2. modelglot/api/__init__.py +1 -0
  3. modelglot/api/admin_cache.py +50 -0
  4. modelglot/api/admin_routes.py +826 -0
  5. modelglot/api/admin_security.py +67 -0
  6. modelglot/api/admin_static/admin.css +1325 -0
  7. modelglot/api/admin_static/admin.js +2715 -0
  8. modelglot/api/admin_static/api_key_editor.js +216 -0
  9. modelglot/api/admin_static/code_sessions.css +109 -0
  10. modelglot/api/admin_static/code_sessions.js +1740 -0
  11. modelglot/api/admin_static/form_controls.js +15 -0
  12. modelglot/api/admin_static/index.html +447 -0
  13. modelglot/api/admin_static/model_combobox.js +274 -0
  14. modelglot/api/admin_static/model_picker_details.js +406 -0
  15. modelglot/api/admin_static/providers/LICENSE-llamacpp.txt +21 -0
  16. modelglot/api/admin_static/providers/LICENSE-lobe-icons.txt +21 -0
  17. modelglot/api/admin_static/providers/SOURCES.md +62 -0
  18. modelglot/api/admin_static/providers/agnesai.svg +1 -0
  19. modelglot/api/admin_static/providers/alibabacloud-color.svg +1 -0
  20. modelglot/api/admin_static/providers/anthropic.svg +1 -0
  21. modelglot/api/admin_static/providers/atlascloud.svg +1 -0
  22. modelglot/api/admin_static/providers/azure-color.svg +1 -0
  23. modelglot/api/admin_static/providers/bedrock-color.svg +1 -0
  24. modelglot/api/admin_static/providers/cerebras-color.svg +1 -0
  25. modelglot/api/admin_static/providers/cheaperinference.svg +5 -0
  26. modelglot/api/admin_static/providers/chutes.png +0 -0
  27. modelglot/api/admin_static/providers/cline.svg +1 -0
  28. modelglot/api/admin_static/providers/cloudflare-color.svg +1 -0
  29. modelglot/api/admin_static/providers/cohere-color.svg +1 -0
  30. modelglot/api/admin_static/providers/deepinfra-color.svg +1 -0
  31. modelglot/api/admin_static/providers/deepseek-color.svg +1 -0
  32. modelglot/api/admin_static/providers/experiential.svg +9 -0
  33. modelglot/api/admin_static/providers/featherless-color.svg +1 -0
  34. modelglot/api/admin_static/providers/fireworks-color.svg +1 -0
  35. modelglot/api/admin_static/providers/gemini-color.svg +1 -0
  36. modelglot/api/admin_static/providers/githubcopilot.svg +1 -0
  37. modelglot/api/admin_static/providers/groq.svg +1 -0
  38. modelglot/api/admin_static/providers/huggingface-color.svg +1 -0
  39. modelglot/api/admin_static/providers/kilocode.svg +1 -0
  40. modelglot/api/admin_static/providers/kimi-color.svg +1 -0
  41. modelglot/api/admin_static/providers/lightning.png +0 -0
  42. modelglot/api/admin_static/providers/llamacpp.svg +77 -0
  43. modelglot/api/admin_static/providers/llm7.png +0 -0
  44. modelglot/api/admin_static/providers/lmstudio.svg +1 -0
  45. modelglot/api/admin_static/providers/minimax-color.svg +1 -0
  46. modelglot/api/admin_static/providers/mistral-color.svg +1 -0
  47. modelglot/api/admin_static/providers/nararoute.png +0 -0
  48. modelglot/api/admin_static/providers/nebius.svg +1 -0
  49. modelglot/api/admin_static/providers/novita-color.svg +1 -0
  50. modelglot/api/admin_static/providers/nvidia-color.svg +1 -0
  51. modelglot/api/admin_static/providers/ollama.svg +1 -0
  52. modelglot/api/admin_static/providers/openai.svg +1 -0
  53. modelglot/api/admin_static/providers/opencode.svg +1 -0
  54. modelglot/api/admin_static/providers/openrouter.svg +1 -0
  55. modelglot/api/admin_static/providers/opper.svg +3 -0
  56. modelglot/api/admin_static/providers/orcarouter.png +0 -0
  57. modelglot/api/admin_static/providers/poolside-color.svg +1 -0
  58. modelglot/api/admin_static/providers/qwen-color.svg +1 -0
  59. modelglot/api/admin_static/providers/sambanova-color.svg +1 -0
  60. modelglot/api/admin_static/providers/scaleway.svg +11 -0
  61. modelglot/api/admin_static/providers/siliconcloud-color.svg +1 -0
  62. modelglot/api/admin_static/providers/together-color.svg +1 -0
  63. modelglot/api/admin_static/providers/tokenrouter.png +0 -0
  64. modelglot/api/admin_static/providers/vercel.svg +1 -0
  65. modelglot/api/admin_static/providers/vertexai-color.svg +1 -0
  66. modelglot/api/admin_static/providers/wafer.png +0 -0
  67. modelglot/api/admin_static/providers/wandb.png +0 -0
  68. modelglot/api/admin_static/providers/xai.svg +1 -0
  69. modelglot/api/admin_static/providers/xkiro.png +0 -0
  70. modelglot/api/admin_static/providers/zai.svg +1 -0
  71. modelglot/api/admin_static/providers/zenmux.svg +1 -0
  72. modelglot/api/admin_static/session_layout.css +552 -0
  73. modelglot/api/admin_static/session_ui.js +211 -0
  74. modelglot/api/admin_static/token_optimization.css +398 -0
  75. modelglot/api/admin_static/token_optimization.js +439 -0
  76. modelglot/api/app.py +219 -0
  77. modelglot/api/classifier_projection.py +47 -0
  78. modelglot/api/code_sessions_routes.py +387 -0
  79. modelglot/api/dependencies.py +168 -0
  80. modelglot/api/detection.py +34 -0
  81. modelglot/api/generation_paths.py +23 -0
  82. modelglot/api/handlers/__init__.py +7 -0
  83. modelglot/api/handlers/chat.py +116 -0
  84. modelglot/api/handlers/classifier_response.py +32 -0
  85. modelglot/api/handlers/gemini.py +112 -0
  86. modelglot/api/handlers/messages.py +355 -0
  87. modelglot/api/handlers/responses.py +170 -0
  88. modelglot/api/handlers/token_count.py +97 -0
  89. modelglot/api/inline_system_turns.py +41 -0
  90. modelglot/api/markdown.py +73 -0
  91. modelglot/api/model_catalog.py +308 -0
  92. modelglot/api/ports.py +150 -0
  93. modelglot/api/request_errors.py +113 -0
  94. modelglot/api/request_ids.py +103 -0
  95. modelglot/api/request_lifetime.py +135 -0
  96. modelglot/api/request_outcomes.py +231 -0
  97. modelglot/api/response_streams.py +495 -0
  98. modelglot/api/routes.py +463 -0
  99. modelglot/api/stream_delivery.py +151 -0
  100. modelglot/api/stream_frames.py +42 -0
  101. modelglot/api/stream_publication.py +358 -0
  102. modelglot/api/usage_csv.py +106 -0
  103. modelglot/api/validation_log.py +46 -0
  104. modelglot/application/__init__.py +1 -0
  105. modelglot/application/code_sessions/__init__.py +33 -0
  106. modelglot/application/code_sessions/models.py +250 -0
  107. modelglot/application/code_sessions/ports.py +230 -0
  108. modelglot/application/code_sessions/service.py +1196 -0
  109. modelglot/application/code_sessions/state.py +692 -0
  110. modelglot/application/connected_accounts.py +96 -0
  111. modelglot/application/errors.py +42 -0
  112. modelglot/application/execution.py +486 -0
  113. modelglot/application/model_catalog.py +204 -0
  114. modelglot/application/model_fallback/__init__.py +6 -0
  115. modelglot/application/model_fallback/decision.py +85 -0
  116. modelglot/application/model_fallback/runner.py +129 -0
  117. modelglot/application/model_fallback/traces.py +85 -0
  118. modelglot/application/model_fallback/types.py +15 -0
  119. modelglot/application/model_metadata.py +29 -0
  120. modelglot/application/model_titles.py +154 -0
  121. modelglot/application/ports.py +216 -0
  122. modelglot/application/readiness.py +43 -0
  123. modelglot/application/reasoning.py +203 -0
  124. modelglot/application/response_collection.py +155 -0
  125. modelglot/application/routing.py +413 -0
  126. modelglot/application/session_events.py +140 -0
  127. modelglot/application/token_optimization.py +328 -0
  128. modelglot/application/usage_events.py +820 -0
  129. modelglot/application/web_tools/__init__.py +1 -0
  130. modelglot/application/web_tools/ports.py +35 -0
  131. modelglot/application/web_tools/request.py +483 -0
  132. modelglot/application/web_tools/service.py +626 -0
  133. modelglot/assets/app-icon.icns +0 -0
  134. modelglot/assets/app-icon.ico +0 -0
  135. modelglot/assets/app-icon.png +0 -0
  136. modelglot/assets/app-icon.svg +22 -0
  137. modelglot/assets/macos/modelglot-desktop-arm64 +0 -0
  138. modelglot/assets/macos/modelglot-desktop-x86_64 +0 -0
  139. modelglot/cli/__init__.py +5 -0
  140. modelglot/cli/clipboard.py +14 -0
  141. modelglot/cli/commands.py +463 -0
  142. modelglot/cli/desktop.py +170 -0
  143. modelglot/cli/desktop_assets.py +31 -0
  144. modelglot/cli/desktop_entrypoint.py +40 -0
  145. modelglot/cli/desktop_notifications.py +108 -0
  146. modelglot/cli/desktop_tray.py +523 -0
  147. modelglot/cli/doctor.py +49 -0
  148. modelglot/cli/entrypoints.py +35 -0
  149. modelglot/cli/launchers/__init__.py +1 -0
  150. modelglot/cli/launchers/agy.py +70 -0
  151. modelglot/cli/launchers/aider.py +53 -0
  152. modelglot/cli/launchers/aider_config.py +83 -0
  153. modelglot/cli/launchers/catalog_http.py +139 -0
  154. modelglot/cli/launchers/claude.py +34 -0
  155. modelglot/cli/launchers/cline.py +78 -0
  156. modelglot/cli/launchers/cline_config.py +123 -0
  157. modelglot/cli/launchers/codex.py +47 -0
  158. modelglot/cli/launchers/common.py +453 -0
  159. modelglot/cli/launchers/dsh.py +145 -0
  160. modelglot/cli/launchers/grok.py +117 -0
  161. modelglot/cli/launchers/hermes.py +87 -0
  162. modelglot/cli/launchers/hermes_config.py +136 -0
  163. modelglot/cli/launchers/muse.py +68 -0
  164. modelglot/cli/launchers/opencode.py +85 -0
  165. modelglot/cli/launchers/opencode_config.py +112 -0
  166. modelglot/cli/launchers/pi.py +70 -0
  167. modelglot/cli/launchers/pi_extension.ts +193 -0
  168. modelglot/cli/launchers/runner.py +152 -0
  169. modelglot/cli/local_http.py +12 -0
  170. modelglot/cli/macos_app.py +143 -0
  171. modelglot/cli/managed/__init__.py +6 -0
  172. modelglot/cli/managed/claude.py +225 -0
  173. modelglot/cli/managed/diagnostics.py +47 -0
  174. modelglot/cli/managed/manager.py +207 -0
  175. modelglot/cli/managed/session.py +298 -0
  176. modelglot/cli/process_registry.py +77 -0
  177. modelglot/cli/server_socket.py +71 -0
  178. modelglot/cli/uvicorn_server.py +97 -0
  179. modelglot/cli/windows_app_identity.py +340 -0
  180. modelglot/cli/windows_desktop_notifications.py +226 -0
  181. modelglot/config/__init__.py +10 -0
  182. modelglot/config/admin/__init__.py +1 -0
  183. modelglot/config/admin/credentials.py +137 -0
  184. modelglot/config/admin/custom_providers.py +102 -0
  185. modelglot/config/admin/manifest.py +670 -0
  186. modelglot/config/admin/persistence.py +306 -0
  187. modelglot/config/admin/provider_manifest.py +500 -0
  188. modelglot/config/admin/specs.py +78 -0
  189. modelglot/config/admin/state.py +18 -0
  190. modelglot/config/admin/status.py +116 -0
  191. modelglot/config/admin/validation.py +40 -0
  192. modelglot/config/admin/values.py +172 -0
  193. modelglot/config/api_keys.py +66 -0
  194. modelglot/config/constants.py +15 -0
  195. modelglot/config/custom_providers.py +137 -0
  196. modelglot/config/env_files.py +54 -0
  197. modelglot/config/env_migrations.py +518 -0
  198. modelglot/config/errors.py +9 -0
  199. modelglot/config/loader.py +204 -0
  200. modelglot/config/logging_config.py +139 -0
  201. modelglot/config/model_catalog_mapping.py +44 -0
  202. modelglot/config/model_catalog_metadata.py +41 -0
  203. modelglot/config/model_refs.py +213 -0
  204. modelglot/config/model_title_overrides.py +22 -0
  205. modelglot/config/paths.py +132 -0
  206. modelglot/config/provider_catalog.py +843 -0
  207. modelglot/config/provider_proxies.py +32 -0
  208. modelglot/config/reasoning.py +25 -0
  209. modelglot/config/server_urls.py +55 -0
  210. modelglot/config/settings.py +889 -0
  211. modelglot/core/__init__.py +15 -0
  212. modelglot/core/anthropic/__init__.py +98 -0
  213. modelglot/core/anthropic/content.py +43 -0
  214. modelglot/core/anthropic/conversion.py +906 -0
  215. modelglot/core/anthropic/errors.py +125 -0
  216. modelglot/core/anthropic/image_sources.py +77 -0
  217. modelglot/core/anthropic/models.py +215 -0
  218. modelglot/core/anthropic/native.py +168 -0
  219. modelglot/core/anthropic/native_stream.py +290 -0
  220. modelglot/core/anthropic/passthrough.py +115 -0
  221. modelglot/core/anthropic/request_serialization.py +77 -0
  222. modelglot/core/anthropic/request_snapshot.py +33 -0
  223. modelglot/core/anthropic/server_tool_sse.py +240 -0
  224. modelglot/core/anthropic/server_tool_types.py +10 -0
  225. modelglot/core/anthropic/sse_aggregation.py +156 -0
  226. modelglot/core/anthropic/stream_contracts.py +217 -0
  227. modelglot/core/anthropic/streaming/__init__.py +25 -0
  228. modelglot/core/anthropic/streaming/decoder.py +68 -0
  229. modelglot/core/anthropic/streaming/emitter.py +62 -0
  230. modelglot/core/anthropic/streaming/ledger.py +443 -0
  231. modelglot/core/anthropic/thinking.py +140 -0
  232. modelglot/core/anthropic/tokens.py +141 -0
  233. modelglot/core/anthropic/tool_calls.py +139 -0
  234. modelglot/core/anthropic/tool_results.py +63 -0
  235. modelglot/core/anthropic/usage.py +38 -0
  236. modelglot/core/anthropic/utils.py +7 -0
  237. modelglot/core/async_iterators.py +26 -0
  238. modelglot/core/async_rwlock.py +52 -0
  239. modelglot/core/async_tasks.py +30 -0
  240. modelglot/core/chat_completions/__init__.py +1 -0
  241. modelglot/core/chat_completions/messages_stream.py +142 -0
  242. modelglot/core/chat_completions/request.py +610 -0
  243. modelglot/core/chat_completions/response.py +338 -0
  244. modelglot/core/chat_completions/responses_stream.py +302 -0
  245. modelglot/core/chat_completions/tool_calls.py +667 -0
  246. modelglot/core/continuation_overlap.py +490 -0
  247. modelglot/core/continuation_request.py +248 -0
  248. modelglot/core/continuation_stream.py +279 -0
  249. modelglot/core/data/README.md +12 -0
  250. modelglot/core/data/TIKTOKEN-LICENSE +21 -0
  251. modelglot/core/data/cl100k_base.tiktoken +100256 -0
  252. modelglot/core/delivered_response.py +694 -0
  253. modelglot/core/diagnostics.py +317 -0
  254. modelglot/core/failures.py +53 -0
  255. modelglot/core/gateway_model_ids.py +73 -0
  256. modelglot/core/gemini/__init__.py +6 -0
  257. modelglot/core/gemini/errors.py +27 -0
  258. modelglot/core/gemini/history.py +91 -0
  259. modelglot/core/gemini/messages_stream.py +168 -0
  260. modelglot/core/gemini/models.py +263 -0
  261. modelglot/core/gemini/native_request.py +664 -0
  262. modelglot/core/gemini/replay.py +199 -0
  263. modelglot/core/gemini/request.py +453 -0
  264. modelglot/core/gemini/response.py +285 -0
  265. modelglot/core/gemini/responses_stream.py +315 -0
  266. modelglot/core/gemini/schema.py +118 -0
  267. modelglot/core/gemini/tokens.py +32 -0
  268. modelglot/core/gemini/web_search_replay.py +153 -0
  269. modelglot/core/history_replay.py +1336 -0
  270. modelglot/core/interprocess_lock.py +90 -0
  271. modelglot/core/json_types.py +70 -0
  272. modelglot/core/model_capabilities.py +10 -0
  273. modelglot/core/openai_chat.py +54 -0
  274. modelglot/core/openai_responses/__init__.py +105 -0
  275. modelglot/core/openai_responses/chat_request.py +736 -0
  276. modelglot/core/openai_responses/errors.py +78 -0
  277. modelglot/core/openai_responses/events.py +110 -0
  278. modelglot/core/openai_responses/ids.py +32 -0
  279. modelglot/core/openai_responses/items.py +35 -0
  280. modelglot/core/openai_responses/messages_request.py +644 -0
  281. modelglot/core/openai_responses/messages_stream.py +391 -0
  282. modelglot/core/openai_responses/models.py +33 -0
  283. modelglot/core/openai_responses/native.py +165 -0
  284. modelglot/core/openai_responses/provider_input.py +412 -0
  285. modelglot/core/openai_responses/provider_stream.py +445 -0
  286. modelglot/core/openai_responses/reasoning.py +73 -0
  287. modelglot/core/openai_responses/streaming/__init__.py +1 -0
  288. modelglot/core/openai_responses/streaming/blocks.py +37 -0
  289. modelglot/core/openai_responses/streaming/completion.py +178 -0
  290. modelglot/core/openai_responses/streaming/error_mapping.py +16 -0
  291. modelglot/core/openai_responses/streaming/event_builders.py +250 -0
  292. modelglot/core/openai_responses/streaming/ledger.py +96 -0
  293. modelglot/core/openai_responses/tokens.py +133 -0
  294. modelglot/core/openai_responses/tool_adaptation.py +761 -0
  295. modelglot/core/openai_responses/tool_calls.py +347 -0
  296. modelglot/core/openai_responses/tool_search.py +204 -0
  297. modelglot/core/openai_responses/tools.py +145 -0
  298. modelglot/core/openai_tool_names.py +163 -0
  299. modelglot/core/rate_limit.py +79 -0
  300. modelglot/core/reasoning.py +136 -0
  301. modelglot/core/recovery_limits.py +3 -0
  302. modelglot/core/replay_finalization.py +110 -0
  303. modelglot/core/request_outcomes.py +81 -0
  304. modelglot/core/stream_delivery.py +69 -0
  305. modelglot/core/stream_output.py +92 -0
  306. modelglot/core/stream_recovery.py +162 -0
  307. modelglot/core/token_estimation.py +79 -0
  308. modelglot/core/tool_call_buffer.py +122 -0
  309. modelglot/core/tool_schema_patterns.py +169 -0
  310. modelglot/core/trace.py +232 -0
  311. modelglot/core/version.py +15 -0
  312. modelglot/core/web_tools.py +17 -0
  313. modelglot/core/windows_dpi.py +25 -0
  314. modelglot/harnesses/__init__.py +1 -0
  315. modelglot/harnesses/antigravity.py +203 -0
  316. modelglot/harnesses/claude.py +49 -0
  317. modelglot/harnesses/claude_desktop_integration.py +517 -0
  318. modelglot/harnesses/claude_integration.py +174 -0
  319. modelglot/harnesses/claude_onboarding.py +43 -0
  320. modelglot/harnesses/codex.py +96 -0
  321. modelglot/harnesses/codex_integration.py +229 -0
  322. modelglot/harnesses/codex_model_catalog.py +127 -0
  323. modelglot/harnesses/config_file.py +71 -0
  324. modelglot/harnesses/dsh_config.py +118 -0
  325. modelglot/harnesses/dsh_desktop_integration.py +216 -0
  326. modelglot/harnesses/dsh_files.py +170 -0
  327. modelglot/harnesses/environment.py +78 -0
  328. modelglot/harnesses/integration_cleanup.py +159 -0
  329. modelglot/harnesses/jetbrains_acp_integration.py +239 -0
  330. modelglot/harnesses/launch.py +21 -0
  331. modelglot/harnesses/model_policy.py +21 -0
  332. modelglot/harnesses/resources.py +42 -0
  333. modelglot/harnesses/vscode_chat_integration.py +148 -0
  334. modelglot/messaging/__init__.py +16 -0
  335. modelglot/messaging/cli_event_constants.py +66 -0
  336. modelglot/messaging/command_context.py +99 -0
  337. modelglot/messaging/command_dispatcher.py +31 -0
  338. modelglot/messaging/commands.py +182 -0
  339. modelglot/messaging/delivery_failures/__init__.py +39 -0
  340. modelglot/messaging/delivery_failures/decision.py +92 -0
  341. modelglot/messaging/delivery_failures/queue.py +16 -0
  342. modelglot/messaging/event_parser.py +184 -0
  343. modelglot/messaging/limiter.py +296 -0
  344. modelglot/messaging/managed_protocols.py +37 -0
  345. modelglot/messaging/models.py +56 -0
  346. modelglot/messaging/node_event_pipeline.py +124 -0
  347. modelglot/messaging/node_runner.py +404 -0
  348. modelglot/messaging/platforms/__init__.py +20 -0
  349. modelglot/messaging/platforms/discord.py +322 -0
  350. modelglot/messaging/platforms/discord_inbound.py +116 -0
  351. modelglot/messaging/platforms/discord_io.py +258 -0
  352. modelglot/messaging/platforms/factory.py +109 -0
  353. modelglot/messaging/platforms/outbox.py +146 -0
  354. modelglot/messaging/platforms/ports.py +99 -0
  355. modelglot/messaging/platforms/telegram.py +312 -0
  356. modelglot/messaging/platforms/telegram_inbound.py +135 -0
  357. modelglot/messaging/platforms/telegram_io.py +355 -0
  358. modelglot/messaging/platforms/voice_flow.py +387 -0
  359. modelglot/messaging/rendering/__init__.py +3 -0
  360. modelglot/messaging/rendering/discord_markdown.py +327 -0
  361. modelglot/messaging/rendering/markdown_tables.py +47 -0
  362. modelglot/messaging/rendering/profiles.py +53 -0
  363. modelglot/messaging/rendering/telegram_markdown.py +340 -0
  364. modelglot/messaging/safe_diagnostics.py +15 -0
  365. modelglot/messaging/transcript/__init__.py +6 -0
  366. modelglot/messaging/transcript/buffer.py +225 -0
  367. modelglot/messaging/transcript/context.py +18 -0
  368. modelglot/messaging/transcript/renderer.py +65 -0
  369. modelglot/messaging/transcript/segments.py +175 -0
  370. modelglot/messaging/transcript/subagents.py +108 -0
  371. modelglot/messaging/transcription.py +147 -0
  372. modelglot/messaging/trees/__init__.py +46 -0
  373. modelglot/messaging/trees/graph.py +264 -0
  374. modelglot/messaging/trees/identity.py +16 -0
  375. modelglot/messaging/trees/manager.py +712 -0
  376. modelglot/messaging/trees/node.py +56 -0
  377. modelglot/messaging/trees/ports.py +35 -0
  378. modelglot/messaging/trees/processor.py +287 -0
  379. modelglot/messaging/trees/queue.py +47 -0
  380. modelglot/messaging/trees/repository.py +146 -0
  381. modelglot/messaging/trees/runtime.py +525 -0
  382. modelglot/messaging/trees/snapshot.py +224 -0
  383. modelglot/messaging/trees/transaction.py +54 -0
  384. modelglot/messaging/trees/transitions.py +175 -0
  385. modelglot/messaging/turn_intake.py +236 -0
  386. modelglot/messaging/ui_updates.py +99 -0
  387. modelglot/messaging/voice.py +376 -0
  388. modelglot/messaging/workflow.py +707 -0
  389. modelglot/providers/__init__.py +12 -0
  390. modelglot/providers/admission.py +1167 -0
  391. modelglot/providers/admission_capacity.py +71 -0
  392. modelglot/providers/admission_policy.py +33 -0
  393. modelglot/providers/admission_registry.py +74 -0
  394. modelglot/providers/alibaba_cloud/__init__.py +5 -0
  395. modelglot/providers/alibaba_cloud/client.py +148 -0
  396. modelglot/providers/anthropic/__init__.py +5 -0
  397. modelglot/providers/anthropic/headers.py +68 -0
  398. modelglot/providers/anthropic/models.py +63 -0
  399. modelglot/providers/anthropic/provider.py +370 -0
  400. modelglot/providers/anthropic_messages/__init__.py +1 -0
  401. modelglot/providers/anthropic_messages/discovery.py +81 -0
  402. modelglot/providers/anthropic_messages/request_policy.py +170 -0
  403. modelglot/providers/anthropic_messages/transport.py +670 -0
  404. modelglot/providers/anthropic_messages/wire.py +110 -0
  405. modelglot/providers/attempt_loop/__init__.py +16 -0
  406. modelglot/providers/attempt_loop/attempt.py +78 -0
  407. modelglot/providers/attempt_loop/loop.py +495 -0
  408. modelglot/providers/base.py +126 -0
  409. modelglot/providers/canonical_failures.py +57 -0
  410. modelglot/providers/cloudflare/__init__.py +8 -0
  411. modelglot/providers/cloudflare/client.py +198 -0
  412. modelglot/providers/continuation.py +442 -0
  413. modelglot/providers/credential_validation.py +383 -0
  414. modelglot/providers/custom.py +424 -0
  415. modelglot/providers/deepseek/__init__.py +5 -0
  416. modelglot/providers/deepseek/client.py +123 -0
  417. modelglot/providers/deepseek/compat.py +246 -0
  418. modelglot/providers/endpoint.py +26 -0
  419. modelglot/providers/endpoint_types.py +25 -0
  420. modelglot/providers/failure_classification/__init__.py +6 -0
  421. modelglot/providers/failure_classification/classifier.py +211 -0
  422. modelglot/providers/failure_classification/evidence.py +220 -0
  423. modelglot/providers/failure_classification/verdict.py +26 -0
  424. modelglot/providers/failure_reporting.py +113 -0
  425. modelglot/providers/gemini/__init__.py +5 -0
  426. modelglot/providers/gemini/client.py +313 -0
  427. modelglot/providers/gemini/stream.py +353 -0
  428. modelglot/providers/gemini/transport.py +317 -0
  429. modelglot/providers/github_copilot/__init__.py +1 -0
  430. modelglot/providers/github_copilot/auth.py +502 -0
  431. modelglot/providers/github_copilot/broker.py +248 -0
  432. modelglot/providers/github_copilot/lifecycle.py +18 -0
  433. modelglot/providers/github_copilot/login.py +113 -0
  434. modelglot/providers/github_copilot/native_cli.py +83 -0
  435. modelglot/providers/github_copilot/provider.py +480 -0
  436. modelglot/providers/github_copilot/request_policy.py +125 -0
  437. modelglot/providers/github_copilot/responses_events.py +90 -0
  438. modelglot/providers/github_copilot/sdk.py +496 -0
  439. modelglot/providers/github_copilot/types.py +67 -0
  440. modelglot/providers/google_openai/__init__.py +15 -0
  441. modelglot/providers/google_openai/provider.py +125 -0
  442. modelglot/providers/google_openai/reasoning.py +114 -0
  443. modelglot/providers/google_openai/thought_signatures.py +150 -0
  444. modelglot/providers/groq/__init__.py +5 -0
  445. modelglot/providers/groq/client.py +366 -0
  446. modelglot/providers/groq/tpm.py +114 -0
  447. modelglot/providers/history_negotiation.py +286 -0
  448. modelglot/providers/history_replay.py +433 -0
  449. modelglot/providers/http.py +133 -0
  450. modelglot/providers/kilo/__init__.py +5 -0
  451. modelglot/providers/kilo/client.py +55 -0
  452. modelglot/providers/kilo/models.py +59 -0
  453. modelglot/providers/lmstudio/__init__.py +5 -0
  454. modelglot/providers/lmstudio/client.py +146 -0
  455. modelglot/providers/lmstudio/tokens.py +107 -0
  456. modelglot/providers/mistral/__init__.py +5 -0
  457. modelglot/providers/mistral/caching.py +59 -0
  458. modelglot/providers/mistral/client.py +237 -0
  459. modelglot/providers/mistral/reasoning.py +369 -0
  460. modelglot/providers/model_catalog_logos.py +191 -0
  461. modelglot/providers/model_catalog_source.py +357 -0
  462. modelglot/providers/model_listing.py +496 -0
  463. modelglot/providers/nvidia_nim/__init__.py +5 -0
  464. modelglot/providers/nvidia_nim/client.py +202 -0
  465. modelglot/providers/nvidia_nim/models.py +74 -0
  466. modelglot/providers/nvidia_nim/request_options.py +109 -0
  467. modelglot/providers/nvidia_nim/retry.py +32 -0
  468. modelglot/providers/nvidia_nim/tool_schema.py +256 -0
  469. modelglot/providers/nvidia_nim/voice.py +119 -0
  470. modelglot/providers/open_router/__init__.py +5 -0
  471. modelglot/providers/open_router/client.py +132 -0
  472. modelglot/providers/openai_api/__init__.py +5 -0
  473. modelglot/providers/openai_api/provider.py +215 -0
  474. modelglot/providers/openai_chat/__init__.py +91 -0
  475. modelglot/providers/openai_chat/base_url.py +7 -0
  476. modelglot/providers/openai_chat/behavior.py +155 -0
  477. modelglot/providers/openai_chat/client.py +40 -0
  478. modelglot/providers/openai_chat/extra_body.py +63 -0
  479. modelglot/providers/openai_chat/gemini_output.py +154 -0
  480. modelglot/providers/openai_chat/media_support.py +146 -0
  481. modelglot/providers/openai_chat/native_output.py +78 -0
  482. modelglot/providers/openai_chat/output_cap.py +212 -0
  483. modelglot/providers/openai_chat/profiles.py +875 -0
  484. modelglot/providers/openai_chat/provider.py +308 -0
  485. modelglot/providers/openai_chat/reasoning.py +178 -0
  486. modelglot/providers/openai_chat/reasoning_details.py +402 -0
  487. modelglot/providers/openai_chat/request_policy.py +146 -0
  488. modelglot/providers/openai_chat/stream_output.py +704 -0
  489. modelglot/providers/openai_chat/transport.py +1454 -0
  490. modelglot/providers/openai_chat/usage.py +117 -0
  491. modelglot/providers/openai_client.py +77 -0
  492. modelglot/providers/openai_codex/__init__.py +1 -0
  493. modelglot/providers/openai_codex/auth.py +650 -0
  494. modelglot/providers/openai_codex/endpoint.py +53 -0
  495. modelglot/providers/openai_codex/login.py +275 -0
  496. modelglot/providers/openai_codex/provider.py +354 -0
  497. modelglot/providers/openai_responses/__init__.py +5 -0
  498. modelglot/providers/openai_responses/presentation.py +130 -0
  499. modelglot/providers/openai_responses/transport.py +690 -0
  500. modelglot/providers/openai_stream.py +44 -0
  501. modelglot/providers/opencode/__init__.py +5 -0
  502. modelglot/providers/opencode/catalog.py +340 -0
  503. modelglot/providers/opencode/provider.py +455 -0
  504. modelglot/providers/reasoning_compatibility.py +210 -0
  505. modelglot/providers/recovery_errors.py +35 -0
  506. modelglot/providers/recovery_policy/__init__.py +38 -0
  507. modelglot/providers/recovery_policy/decision.py +138 -0
  508. modelglot/providers/recovery_policy/timing.py +62 -0
  509. modelglot/providers/request_correction/__init__.py +28 -0
  510. modelglot/providers/request_correction/corrector.py +88 -0
  511. modelglot/providers/request_correction/rejection.py +72 -0
  512. modelglot/providers/runtime/__init__.py +10 -0
  513. modelglot/providers/runtime/config.py +126 -0
  514. modelglot/providers/runtime/discovery.py +159 -0
  515. modelglot/providers/runtime/factory.py +389 -0
  516. modelglot/providers/runtime/model_cache.py +96 -0
  517. modelglot/providers/runtime/runtime.py +151 -0
  518. modelglot/providers/stream_failure_resolution/__init__.py +26 -0
  519. modelglot/providers/stream_failure_resolution/decision.py +123 -0
  520. modelglot/providers/stream_read.py +19 -0
  521. modelglot/providers/stream_recovery.py +7 -0
  522. modelglot/providers/vertex/__init__.py +5 -0
  523. modelglot/providers/vertex/auth.py +105 -0
  524. modelglot/providers/vertex/client.py +146 -0
  525. modelglot/providers/vertex/endpoint.py +50 -0
  526. modelglot/providers/vertex/models.py +52 -0
  527. modelglot/runtime/__init__.py +1 -0
  528. modelglot/runtime/application.py +774 -0
  529. modelglot/runtime/asgi.py +64 -0
  530. modelglot/runtime/bootstrap.py +185 -0
  531. modelglot/runtime/code_sessions_sqlite.py +873 -0
  532. modelglot/runtime/codex_app_server.py +781 -0
  533. modelglot/runtime/codex_catalog.py +62 -0
  534. modelglot/runtime/codex_protocol.py +690 -0
  535. modelglot/runtime/configuration.py +93 -0
  536. modelglot/runtime/diagnostics.py +426 -0
  537. modelglot/runtime/folder_picker.py +129 -0
  538. modelglot/runtime/integration_service.py +785 -0
  539. modelglot/runtime/lifecycle.py +38 -0
  540. modelglot/runtime/messaging_import.py +207 -0
  541. modelglot/runtime/messaging_service.py +344 -0
  542. modelglot/runtime/messaging_sqlite.py +196 -0
  543. modelglot/runtime/native_folder_dialog.py +137 -0
  544. modelglot/runtime/provider_manager.py +1004 -0
  545. modelglot/runtime/retired_chat.py +47 -0
  546. modelglot/runtime/sqlite_database.py +459 -0
  547. modelglot/runtime/sqlite_migrations/__init__.py +27 -0
  548. modelglot/runtime/sqlite_migrations/v0001_code_schema.py +101 -0
  549. modelglot/runtime/sqlite_migrations/v0002_code_permissions.py +16 -0
  550. modelglot/runtime/sqlite_migrations/v0003_code_context_usage.py +10 -0
  551. modelglot/runtime/sqlite_migrations/v0004_messaging_schema.py +108 -0
  552. modelglot/runtime/sqlite_migrations/v0005_active_code_prompts.py +10 -0
  553. modelglot/runtime/sqlite_migrations/v0006_usage_events.py +70 -0
  554. modelglot/runtime/token_optimization.py +367 -0
  555. modelglot/runtime/usage_events.py +326 -0
  556. modelglot/runtime/usage_events_sqlite.py +373 -0
  557. modelglot/runtime/web_tools/__init__.py +1 -0
  558. modelglot/runtime/web_tools/client.py +250 -0
  559. modelglot/runtime/web_tools/constants.py +17 -0
  560. modelglot/runtime/web_tools/egress.py +107 -0
  561. modelglot/runtime/web_tools/parsers.py +85 -0
  562. modelglot/updater/__init__.py +1 -0
  563. modelglot/updater/check.py +147 -0
  564. modelglot/updater/desktop.py +346 -0
  565. modelglot-7.0.0.data/scripts/modelglot-update +4 -0
  566. modelglot-7.0.0.data/scripts/modelglot-update.cmd +7 -0
  567. modelglot-7.0.0.dist-info/METADATA +866 -0
  568. modelglot-7.0.0.dist-info/RECORD +571 -0
  569. modelglot-7.0.0.dist-info/WHEEL +4 -0
  570. modelglot-7.0.0.dist-info/entry_points.txt +17 -0
  571. modelglot-7.0.0.dist-info/licenses/LICENSE +239 -0
@@ -0,0 +1,442 @@
1
+ """Same-provider continuation using evidence from the public delivery boundary."""
2
+
3
+ from collections.abc import Mapping
4
+ from dataclasses import dataclass, replace
5
+ from typing import Any
6
+
7
+ from modelglot.core.continuation_request import (
8
+ ContinuationRequest,
9
+ UpstreamProtocol,
10
+ )
11
+ from modelglot.core.delivered_response import (
12
+ is_structured_text_format,
13
+ )
14
+ from modelglot.core.failures import ExecutionFailure, FailureKind
15
+ from modelglot.core.history_replay import protected_reasoning
16
+ from modelglot.core.json_types import (
17
+ JsonObject,
18
+ JsonValue,
19
+ json_array,
20
+ json_object,
21
+ json_objects,
22
+ optional_string,
23
+ )
24
+ from modelglot.core.openai_responses import is_client_call
25
+ from modelglot.core.stream_recovery import (
26
+ RecoveryAction,
27
+ SourceRecoverySnapshot,
28
+ StreamFailureContext,
29
+ assess_recovery,
30
+ )
31
+ from modelglot.core.trace import trace_event
32
+
33
+ from .admission import ProviderExecution
34
+
35
+ _USAGE_FIELDS = {
36
+ "messages": {
37
+ "input_tokens",
38
+ "output_tokens",
39
+ "cache_read_input_tokens",
40
+ "cache_creation_input_tokens",
41
+ "reasoning_tokens",
42
+ },
43
+ "chat": {
44
+ "prompt_tokens",
45
+ "completion_tokens",
46
+ "total_tokens",
47
+ "prompt_cache_hit_tokens",
48
+ "prompt_cache_miss_tokens",
49
+ "prompt_tokens_details",
50
+ "completion_tokens_details",
51
+ },
52
+ "responses": {
53
+ "input_tokens",
54
+ "output_tokens",
55
+ "total_tokens",
56
+ "input_tokens_details",
57
+ "output_tokens_details",
58
+ },
59
+ "gemini": {
60
+ "promptTokenCount",
61
+ "cachedContentTokenCount",
62
+ "candidatesTokenCount",
63
+ "thoughtsTokenCount",
64
+ "totalTokenCount",
65
+ },
66
+ }
67
+ _USAGE_DETAIL_FIELDS = {
68
+ "cached_tokens",
69
+ "cache_write_tokens",
70
+ "cache_creation_tokens",
71
+ "reasoning_tokens",
72
+ }
73
+
74
+
75
+ class SourceRecoveryState:
76
+ """Native source evidence cannot be inferred from buffered public events."""
77
+
78
+ def __init__(
79
+ self,
80
+ execution: ProviderExecution,
81
+ *,
82
+ previous: SourceRecoveryState | None = None,
83
+ ) -> None:
84
+ self._execution = execution
85
+ self._prior_unsafe_reason = (
86
+ previous.unsafe_reason or previous._prior_unsafe_reason
87
+ if previous is not None
88
+ else None
89
+ )
90
+ self.unsafe_reason: str | None = None
91
+ self._pending_reasoning: set[int | str] = set()
92
+ self._signed: set[int | str] = set()
93
+ self._handoff_blocked = False
94
+ self._usage_protocol: UpstreamProtocol | None = None
95
+ self._usage: JsonObject = {}
96
+
97
+ @property
98
+ def can_handoff(self) -> bool:
99
+ return not self._handoff_blocked and not self._pending_reasoning
100
+
101
+ def snapshot(
102
+ self,
103
+ *,
104
+ body: dict[str, Any],
105
+ protocol: UpstreamProtocol,
106
+ normal_stop_seen: bool,
107
+ for_model_fallback: bool = False,
108
+ ) -> SourceRecoverySnapshot:
109
+ return SourceRecoverySnapshot(
110
+ normal_stop_seen=normal_stop_seen,
111
+ structured_output=_structured_output(body, protocol),
112
+ unsafe_reason=self.unsafe_reason
113
+ or (self._prior_unsafe_reason if for_model_fallback else None),
114
+ can_handoff=self.can_handoff,
115
+ )
116
+
117
+ def usage_snapshot(self) -> tuple[UpstreamProtocol, JsonObject] | None:
118
+ """Return only allow-listed native usage fields collected for this attempt."""
119
+
120
+ if self._usage_protocol is None:
121
+ return None
122
+ return self._usage_protocol, dict(self._usage)
123
+
124
+ def _observe_usage(self, protocol: UpstreamProtocol, usage: JsonObject) -> None:
125
+ self._usage_protocol = protocol
126
+ for key in _USAGE_FIELDS[protocol]:
127
+ if key not in usage:
128
+ continue
129
+ value = usage[key]
130
+ if key.endswith("_details") and isinstance(value, dict):
131
+ self._usage[key] = {
132
+ detail: value[detail]
133
+ for detail in _USAGE_DETAIL_FIELDS
134
+ if detail in value
135
+ }
136
+ else:
137
+ self._usage[key] = value
138
+
139
+ def observe(
140
+ self,
141
+ protocol: UpstreamProtocol,
142
+ payload: Mapping[str, JsonValue],
143
+ *,
144
+ continuing: bool = False,
145
+ ) -> None:
146
+ kind = str(payload.get("type", ""))
147
+ usage = payload.get("usage")
148
+ if protocol == "messages" and kind == "message_start":
149
+ usage = json_object(payload.get("message", {})).get("usage")
150
+ elif protocol == "responses":
151
+ usage = json_object(payload.get("response", {})).get("usage")
152
+ elif protocol == "gemini":
153
+ usage = payload.get("usageMetadata")
154
+ if isinstance(usage, dict) and usage:
155
+ self._observe_usage(protocol, usage)
156
+ trace_event(
157
+ stage="provider",
158
+ event="provider.attempt.usage",
159
+ source="provider",
160
+ request_id=self._execution.request_id,
161
+ execution_id=self._execution.execution_id,
162
+ attempt=self._execution.attempts_started,
163
+ usage=usage,
164
+ )
165
+ if protocol == "gemini":
166
+ # The final native replay carrier is published only after the source
167
+ # completes. A call alone cannot prove its late signature is complete.
168
+ self._handoff_blocked = True
169
+ for candidate in json_objects(payload.get("candidates", [])):
170
+ for part in json_objects(
171
+ json_object(candidate.get("content", {})).get("parts", [])
172
+ ):
173
+ if part.get("thoughtSignature") and self.unsafe_reason is None:
174
+ self.unsafe_reason = "signed_thinking"
175
+ if any(
176
+ key
177
+ not in {"text", "thought", "thoughtSignature", "functionCall"}
178
+ for key in part
179
+ ):
180
+ self.unsafe_reason = "native_content"
181
+ elif protocol == "chat":
182
+ choices = list(json_objects(payload.get("choices", [])))
183
+ if len(choices) > 1 or any(
184
+ choice.get("index", 0) != 0 or choice.get("logprobs")
185
+ for choice in choices
186
+ ):
187
+ self.unsafe_reason = "native_content"
188
+ self._handoff_blocked = True
189
+ for choice in choices:
190
+ delta = json_object(choice.get("delta") or {})
191
+ if protected_reasoning({"extra_content": delta.get("extra_content")}):
192
+ self.unsafe_reason = "native_content"
193
+ details = delta.get("reasoning_details")
194
+ if delta.get("audio") or (
195
+ details
196
+ and (
197
+ not isinstance(details, list)
198
+ or any(
199
+ not isinstance(detail, dict)
200
+ or detail.get("type")
201
+ not in ("reasoning.text", "reasoning.summary")
202
+ or protected_reasoning(detail)
203
+ for detail in details
204
+ )
205
+ )
206
+ ):
207
+ self.unsafe_reason = "native_content"
208
+ self._handoff_blocked = True
209
+ elif protocol == "messages" and kind in (
210
+ "content_block_start",
211
+ "content_block_delta",
212
+ "content_block_stop",
213
+ ):
214
+ index = payload.get("index")
215
+ if not isinstance(index, int) or isinstance(index, bool) or index < 0:
216
+ # The Messages parser classifies malformed block events for retries.
217
+ return
218
+ block = payload.get("content_block")
219
+ if isinstance(block, dict) and block.get("type") in (
220
+ "thinking",
221
+ "redacted_thinking",
222
+ ):
223
+ self.unsafe_reason = "signed_thinking"
224
+ self._pending_reasoning.add(index)
225
+ if block.get("signature") or block.get("data"):
226
+ self._signed.add(index)
227
+ if isinstance(block, dict) and block.get("type") not in (
228
+ "text",
229
+ "tool_use",
230
+ "thinking",
231
+ "redacted_thinking",
232
+ ):
233
+ self.unsafe_reason = "native_content"
234
+ self._handoff_blocked = True
235
+ delta = json_object(payload.get("delta") or {})
236
+ if (isinstance(block, dict) and block.get("citations")) or delta.get(
237
+ "type"
238
+ ) == "citations_delta":
239
+ self.unsafe_reason = "native_content"
240
+ self._handoff_blocked = True
241
+ if delta.get("type") in ("thinking_delta", "signature_delta"):
242
+ self.unsafe_reason = "signed_thinking"
243
+ if delta.get("signature"):
244
+ self._signed.add(index)
245
+ if kind == "content_block_stop" and index in self._signed:
246
+ self._pending_reasoning.discard(index)
247
+ elif protocol == "responses":
248
+ item = payload.get("item")
249
+ if isinstance(item, dict):
250
+ self._response_item(item, completed=kind == "response.output_item.done")
251
+ response = payload.get("response")
252
+ if isinstance(response, dict):
253
+ for item in json_array(response.get("output", [])):
254
+ if isinstance(item, dict):
255
+ self._response_item(
256
+ item, completed=item.get("status") == "completed"
257
+ )
258
+ part = payload.get("part")
259
+ if (
260
+ kind in {"response.content_part.added", "response.content_part.done"}
261
+ and isinstance(part, dict)
262
+ and part.get("annotations")
263
+ ):
264
+ self.unsafe_reason = "native_content"
265
+ self._handoff_blocked = True
266
+ if kind.startswith(
267
+ (
268
+ "response.web_search",
269
+ "response.code_interpreter",
270
+ "response.mcp_",
271
+ "response.image_generation",
272
+ "response.audio",
273
+ )
274
+ ):
275
+ self.unsafe_reason = "hosted_or_native_content"
276
+ self._handoff_blocked = True
277
+ if kind == "response.output_text.annotation.added":
278
+ self.unsafe_reason = "native_content"
279
+ self._handoff_blocked = True
280
+ if (
281
+ continuing
282
+ and self.unsafe_reason
283
+ and not (protocol == "gemini" and self.unsafe_reason == "signed_thinking")
284
+ ):
285
+ raise ExecutionFailure(
286
+ kind=FailureKind.UPSTREAM,
287
+ status_code=502,
288
+ message="The continuation returned native state that cannot be combined with the delivered response.",
289
+ retryable=False,
290
+ )
291
+
292
+ def _response_item(self, item: JsonObject, *, completed: bool) -> None:
293
+ if item.get("type") not in ("message", "reasoning") and not is_client_call(
294
+ item
295
+ ):
296
+ self.unsafe_reason = "hosted_or_native_content"
297
+ self._handoff_blocked = True
298
+ identity = optional_string(item.get("id")) or ""
299
+ if item.get("encrypted_content"):
300
+ self.unsafe_reason = "opaque_reasoning"
301
+ if not completed:
302
+ self._pending_reasoning.add(identity)
303
+ if completed:
304
+ self._pending_reasoning.discard(identity)
305
+ if any(
306
+ part.get("annotations")
307
+ or part.get("type")
308
+ not in ("output_text", "refusal", "reasoning_text", "summary_text")
309
+ for field in ("content", "summary")
310
+ for part in json_objects(item.get(field) or [])
311
+ ):
312
+ self.unsafe_reason = "native_content"
313
+ self._handoff_blocked = True
314
+
315
+
316
+ @dataclass(frozen=True, slots=True)
317
+ class PublicRecovery:
318
+ body: dict[str, Any] | None = None
319
+
320
+
321
+ def public_stream_failure(
322
+ failure: ExecutionFailure,
323
+ *,
324
+ source: SourceRecoveryState,
325
+ body: dict[str, Any],
326
+ protocol: UpstreamProtocol,
327
+ normal_stop_seen: bool,
328
+ responses_failure_payload: JsonObject | None = None,
329
+ ) -> ExecutionFailure:
330
+ """Retain source evidence until routing has decided the public outcome."""
331
+ snapshot = source.snapshot(
332
+ body=body,
333
+ protocol=protocol,
334
+ normal_stop_seen=normal_stop_seen,
335
+ for_model_fallback=True,
336
+ )
337
+ if (
338
+ failure.stream_context is not None
339
+ and failure.stream_context.source.unsafe_reason
340
+ ):
341
+ snapshot = replace(
342
+ snapshot, unsafe_reason=failure.stream_context.source.unsafe_reason
343
+ )
344
+ return replace(
345
+ failure,
346
+ stream_context=StreamFailureContext(snapshot, responses_failure_payload),
347
+ )
348
+
349
+
350
+ def _structured_output(
351
+ body: Mapping[str, JsonValue], protocol: UpstreamProtocol
352
+ ) -> bool:
353
+ if protocol == "gemini":
354
+ config = json_object(body.get("generationConfig") or {})
355
+ format_ = json_object(config.get("responseFormat") or {})
356
+ text = json_object(format_.get("text") or {})
357
+ return (
358
+ config.get("responseMimeType") == "application/json"
359
+ or text.get("mimeType") == "APPLICATION_JSON"
360
+ or bool(
361
+ config.get("responseSchema")
362
+ or config.get("responseJsonSchema")
363
+ or text.get("schema")
364
+ )
365
+ )
366
+ if protocol == "chat":
367
+ extra = json_object(body.get("extra_body") or {})
368
+ value = extra.get("response_format", body.get("response_format"))
369
+ else:
370
+ options = json_object(
371
+ body.get("text" if protocol == "responses" else "output_config") or {}
372
+ )
373
+ value = options.get("format")
374
+ return is_structured_text_format(value)
375
+
376
+
377
+ def public_recovery(
378
+ execution: ProviderExecution,
379
+ *,
380
+ body: dict[str, Any],
381
+ request: ContinuationRequest,
382
+ retryable: bool,
383
+ normal_stop_seen: bool,
384
+ source: SourceRecoveryState,
385
+ ) -> PublicRecovery | None:
386
+ delivery = execution.delivery
387
+ if delivery is None or not delivery.content_released:
388
+ return None
389
+ if not retryable:
390
+ return None
391
+ assessment = assess_recovery(
392
+ content_released=True,
393
+ prefix=delivery.prefix,
394
+ source=source.snapshot(
395
+ body=body, protocol=request.protocol, normal_stop_seen=normal_stop_seen
396
+ ),
397
+ retryable=retryable,
398
+ )
399
+ if assessment.action is RecoveryAction.STOP:
400
+ trace_event(
401
+ stage="provider",
402
+ event="provider.recovery.ineligible",
403
+ source="provider",
404
+ request_id=execution.request_id,
405
+ reason=assessment.reason,
406
+ )
407
+ return None
408
+ if assessment.action is RecoveryAction.HANDOFF:
409
+ delivery.begin_continuation(handoff=True)
410
+ trace_event(
411
+ stage="provider",
412
+ event="provider.recovery.tool_handoff",
413
+ source="provider",
414
+ request_id=execution.request_id,
415
+ usage_estimated=True,
416
+ )
417
+ return PublicRecovery()
418
+ if not execution.can_attempt:
419
+ trace_event(
420
+ stage="provider",
421
+ event="provider.recovery.exhausted",
422
+ source="provider",
423
+ request_id=execution.request_id,
424
+ attempts_started=execution.attempts_started,
425
+ max_attempts=execution.max_attempts,
426
+ )
427
+ return None
428
+ seed = assessment.seed
429
+ if seed is None:
430
+ return None
431
+ replacement = request.build(body, seed)
432
+ delivery.begin_continuation()
433
+ trace_event(
434
+ stage="provider",
435
+ event="provider.recovery.continuation",
436
+ source="provider",
437
+ request_id=execution.request_id,
438
+ attempts_started=execution.attempts_started,
439
+ max_attempts=execution.max_attempts,
440
+ usage_estimated=True,
441
+ )
442
+ return PublicRecovery(body=replacement)