polyrob 0.5.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 (745) hide show
  1. agents/README.md +453 -0
  2. agents/__init__.py +164 -0
  3. agents/base_agent.py +743 -0
  4. agents/personality/character.py +119 -0
  5. agents/personality/character_manager.py +309 -0
  6. agents/personality/characters/rob.character.json +56 -0
  7. agents/personality/characters/trump.character.json +63 -0
  8. agents/personality/persona_render.py +78 -0
  9. agents/personality/persona_resolver.py +115 -0
  10. agents/prompt/__init__.py +58 -0
  11. agents/prompt/base_prompt.py +92 -0
  12. agents/prompt/system.py +417 -0
  13. agents/task/README_PATH_MANAGEMENT.md +141 -0
  14. agents/task/__init__.py +162 -0
  15. agents/task/agent/__init__.py +70 -0
  16. agents/task/agent/agent_state.py +408 -0
  17. agents/task/agent/async_delegation.py +274 -0
  18. agents/task/agent/autonomy_state.py +275 -0
  19. agents/task/agent/conversation.py +75 -0
  20. agents/task/agent/core/__init__.py +6 -0
  21. agents/task/agent/core/aux_metering.py +61 -0
  22. agents/task/agent/core/background_review.py +157 -0
  23. agents/task/agent/core/construction.py +1179 -0
  24. agents/task/agent/core/conversational_exit.py +49 -0
  25. agents/task/agent/core/correspondent_gate.py +227 -0
  26. agents/task/agent/core/curator.py +246 -0
  27. agents/task/agent/core/episodic_digest.py +103 -0
  28. agents/task/agent/core/error_recovery.py +480 -0
  29. agents/task/agent/core/history_io.py +257 -0
  30. agents/task/agent/core/llm_provisioning.py +287 -0
  31. agents/task/agent/core/llm_runner.py +547 -0
  32. agents/task/agent/core/logging_io.py +276 -0
  33. agents/task/agent/core/loop_detection.py +262 -0
  34. agents/task/agent/core/memory_prefetch.py +325 -0
  35. agents/task/agent/core/memory_writer.py +667 -0
  36. agents/task/agent/core/model_introspection.py +82 -0
  37. agents/task/agent/core/model_swap.py +177 -0
  38. agents/task/agent/core/next_action_internal.py +1331 -0
  39. agents/task/agent/core/output_validation.py +303 -0
  40. agents/task/agent/core/project_context.py +272 -0
  41. agents/task/agent/core/resources.py +93 -0
  42. agents/task/agent/core/result_processing.py +389 -0
  43. agents/task/agent/core/run_loop.py +661 -0
  44. agents/task/agent/core/safety_lifecycle.py +228 -0
  45. agents/task/agent/core/secret_guard.py +293 -0
  46. agents/task/agent/core/self_wake.py +280 -0
  47. agents/task/agent/core/session_metadata.py +104 -0
  48. agents/task/agent/core/step.py +781 -0
  49. agents/task/agent/core/step_execution.py +398 -0
  50. agents/task/agent/core/step_telemetry.py +222 -0
  51. agents/task/agent/core/test_aux_metering.py +50 -0
  52. agents/task/agent/core/test_billing_failover.py +68 -0
  53. agents/task/agent/core/test_fatal_error_classifier.py +38 -0
  54. agents/task/agent/core/test_judge_validation.py +275 -0
  55. agents/task/agent/core/test_model_swap.py +190 -0
  56. agents/task/agent/core/test_reflection_offload.py +54 -0
  57. agents/task/agent/core/turn_input.py +8 -0
  58. agents/task/agent/core/untrusted_wrap.py +103 -0
  59. agents/task/agent/core/user_ingress.py +228 -0
  60. agents/task/agent/hitl_manager.py +396 -0
  61. agents/task/agent/log_sanitize.py +36 -0
  62. agents/task/agent/message_manager/config.py +97 -0
  63. agents/task/agent/message_manager/service.py +758 -0
  64. agents/task/agent/message_manager/tests.py +305 -0
  65. agents/task/agent/message_manager/tool_call_builder.py +539 -0
  66. agents/task/agent/message_manager/tool_message_repair.py +364 -0
  67. agents/task/agent/message_manager/views.py +143 -0
  68. agents/task/agent/messages/__init__.py +8 -0
  69. agents/task/agent/messages/builders.py +309 -0
  70. agents/task/agent/messages/compactor.py +611 -0
  71. agents/task/agent/messages/context_references.py +271 -0
  72. agents/task/agent/messages/filters.py +333 -0
  73. agents/task/agent/messages/guidance.py +328 -0
  74. agents/task/agent/messages/persistence.py +448 -0
  75. agents/task/agent/messages/retrieval.py +506 -0
  76. agents/task/agent/messages/sqlite_persistence.py +85 -0
  77. agents/task/agent/messages/test_compaction_upgrade.py +349 -0
  78. agents/task/agent/messages/test_persistence_subdir_fix.py +28 -0
  79. agents/task/agent/messages/test_sqlite_persistence.py +54 -0
  80. agents/task/agent/messages/token_counter.py +532 -0
  81. agents/task/agent/orchestrator.py +776 -0
  82. agents/task/agent/profile_manager.py +105 -0
  83. agents/task/agent/profile_registry.py +173 -0
  84. agents/task/agent/prompts.py +1090 -0
  85. agents/task/agent/scenario_registry.py +226 -0
  86. agents/task/agent/service.py +509 -0
  87. agents/task/agent/session.py +984 -0
  88. agents/task/agent/skill_discovery.py +110 -0
  89. agents/task/agent/skill_frontmatter.py +94 -0
  90. agents/task/agent/skill_manager.py +1201 -0
  91. agents/task/agent/skill_store.py +627 -0
  92. agents/task/agent/skill_validation.py +56 -0
  93. agents/task/agent/skill_writer.py +492 -0
  94. agents/task/agent/sub_agent_manager.py +1137 -0
  95. agents/task/agent/test_session_disk_recovery.py +34 -0
  96. agents/task/agent/tests.py +513 -0
  97. agents/task/agent/tool_call_tracker.py +511 -0
  98. agents/task/agent/views.py +469 -0
  99. agents/task/config.py +432 -0
  100. agents/task/constants.py +1384 -0
  101. agents/task/flag_defaults.py +63 -0
  102. agents/task/goals/__init__.py +1 -0
  103. agents/task/goals/autonomy_marker.py +28 -0
  104. agents/task/goals/board.py +690 -0
  105. agents/task/goals/completion_judge.py +247 -0
  106. agents/task/goals/context.py +62 -0
  107. agents/task/goals/dispatcher.py +848 -0
  108. agents/task/goals/escalation.py +102 -0
  109. agents/task/goals/planner.py +114 -0
  110. agents/task/logging_config.py +155 -0
  111. agents/task/path.py +991 -0
  112. agents/task/robust_parse_config.py +400 -0
  113. agents/task/runtime/__init__.py +0 -0
  114. agents/task/runtime/run_as_session.py +130 -0
  115. agents/task/runtime_safety.py +308 -0
  116. agents/task/session/__init__.py +5 -0
  117. agents/task/session/browser_pool.py +130 -0
  118. agents/task/session/cleanup.py +612 -0
  119. agents/task/session/execution.py +397 -0
  120. agents/task/session/feed.py +200 -0
  121. agents/task/session/hitl_ingress.py +201 -0
  122. agents/task/session/hooks.py +83 -0
  123. agents/task/session/multi_agent.py +243 -0
  124. agents/task/session/workspace.py +91 -0
  125. agents/task/session_registry.py +91 -0
  126. agents/task/session_route.py +35 -0
  127. agents/task/sqlite_session_registry.py +261 -0
  128. agents/task/surface_config.py +185 -0
  129. agents/task/telemetry/__init__.py +58 -0
  130. agents/task/telemetry/event_log.py +180 -0
  131. agents/task/telemetry/formatters.py +642 -0
  132. agents/task/telemetry/manager.py +571 -0
  133. agents/task/telemetry/memory_events.py +66 -0
  134. agents/task/telemetry/self_events.py +53 -0
  135. agents/task/telemetry/sequence.py +160 -0
  136. agents/task/telemetry/service.py +1265 -0
  137. agents/task/telemetry/views.py +882 -0
  138. agents/task/templates.py +98 -0
  139. agents/task/test_aux_chain.py +151 -0
  140. agents/task/test_aux_router.py +41 -0
  141. agents/task/tests/test_parse_robustness.py +170 -0
  142. agents/task/tool_defaults.py +110 -0
  143. agents/task/utils.py +762 -0
  144. agents/task/utils_json.py +768 -0
  145. agents/task/utils_webview.py +86 -0
  146. agents/task/workspace_context.py +229 -0
  147. agents/task_agent_lite.py +2398 -0
  148. api/README.md +683 -0
  149. api/TASK_API_DOCS.md +151 -0
  150. api/__init__.py +0 -0
  151. api/a2a/__init__.py +37 -0
  152. api/a2a/agent_card.py +337 -0
  153. api/a2a/client.py +657 -0
  154. api/a2a/endpoints.py +375 -0
  155. api/a2a/models.py +268 -0
  156. api/a2a/streaming.py +419 -0
  157. api/a2a/task_handler.py +929 -0
  158. api/admin_endpoints.py +1584 -0
  159. api/app.py +923 -0
  160. api/auth_constants.py +28 -0
  161. api/auth_endpoints.py +402 -0
  162. api/auth_state.py +36 -0
  163. api/chat_via_task.py +42 -0
  164. api/conversation_manager.py +231 -0
  165. api/dependencies.py +186 -0
  166. api/eip8004_endpoints.py +452 -0
  167. api/hyperliquid_models.py +174 -0
  168. api/hyperliquid_routes.py +450 -0
  169. api/interfaces.py +207 -0
  170. api/jwt_middleware.py +83 -0
  171. api/kb/__init__.py +1 -0
  172. api/kb/endpoints.py +314 -0
  173. api/mcp_models.py +279 -0
  174. api/mcp_routes.py +438 -0
  175. api/middleware.py +526 -0
  176. api/models.py +176 -0
  177. api/openai_compat/__init__.py +0 -0
  178. api/openai_compat/model_map.py +25 -0
  179. api/openai_compat/models.py +59 -0
  180. api/openai_compat/router.py +140 -0
  181. api/payment_endpoints.py +444 -0
  182. api/payment_verification.py +138 -0
  183. api/polymarket_models.py +155 -0
  184. api/polymarket_routes.py +501 -0
  185. api/pricing_endpoints.py +89 -0
  186. api/session_routing.py +54 -0
  187. api/skill_endpoints.py +453 -0
  188. api/task_http_api.py +1856 -0
  189. api/webhooks.py +52 -0
  190. api/x402_endpoints.py +280 -0
  191. avatar/README.md +44 -0
  192. avatar/__init__.py +1 -0
  193. avatar/config/rob.json +7 -0
  194. avatar/mindprint.js +395 -0
  195. avatar/renders/rob.meta.json +20 -0
  196. avatar/renders/rob.png +0 -0
  197. avatar/studio.html +373 -0
  198. avatar/webview/avatar-live.js +44 -0
  199. cli/README.md +89 -0
  200. cli/__init__.py +0 -0
  201. cli/cli_surface.py +67 -0
  202. cli/commands/__init__.py +0 -0
  203. cli/commands/_bootstrap.py +49 -0
  204. cli/commands/_errors.py +53 -0
  205. cli/commands/chat.py +973 -0
  206. cli/commands/config.py +100 -0
  207. cli/commands/dashboard.py +103 -0
  208. cli/commands/doctor.py +222 -0
  209. cli/commands/email.py +174 -0
  210. cli/commands/gateway.py +346 -0
  211. cli/commands/goals.py +509 -0
  212. cli/commands/init.py +276 -0
  213. cli/commands/journey.py +30 -0
  214. cli/commands/kb.py +280 -0
  215. cli/commands/model.py +107 -0
  216. cli/commands/owner.py +431 -0
  217. cli/commands/pfp.py +232 -0
  218. cli/commands/run.py +414 -0
  219. cli/commands/serve.py +41 -0
  220. cli/commands/session.py +684 -0
  221. cli/commands/skill_install.py +786 -0
  222. cli/commands/skills.py +128 -0
  223. cli/commands/subagents.py +100 -0
  224. cli/commands/surface.py +81 -0
  225. cli/commands/telegram.py +185 -0
  226. cli/commands/todos.py +209 -0
  227. cli/commands/tools.py +102 -0
  228. cli/commands/update.py +316 -0
  229. cli/commands/whatsapp.py +175 -0
  230. cli/config_store.py +276 -0
  231. cli/gitignore.py +43 -0
  232. cli/inventory.py +46 -0
  233. cli/keys.py +101 -0
  234. cli/persona.py +17 -0
  235. cli/polyrob.py +137 -0
  236. cli/ui/__init__.py +64 -0
  237. cli/ui/activity.py +141 -0
  238. cli/ui/app.py +538 -0
  239. cli/ui/banner.py +233 -0
  240. cli/ui/blocks.py +534 -0
  241. cli/ui/bootstrap_notice.py +47 -0
  242. cli/ui/commands/__init__.py +41 -0
  243. cli/ui/commands/h_cron.py +64 -0
  244. cli/ui/commands/h_journey.py +164 -0
  245. cli/ui/commands/h_kb.py +121 -0
  246. cli/ui/commands/h_learn.py +100 -0
  247. cli/ui/commands/h_mcp.py +215 -0
  248. cli/ui/commands/h_self.py +113 -0
  249. cli/ui/commands/h_skills.py +314 -0
  250. cli/ui/commands/handlers.py +1672 -0
  251. cli/ui/commands/registry.py +426 -0
  252. cli/ui/dialog.py +425 -0
  253. cli/ui/event_registry.py +110 -0
  254. cli/ui/event_specs.py +45 -0
  255. cli/ui/events.py +433 -0
  256. cli/ui/identity.py +15 -0
  257. cli/ui/lifecycle.py +159 -0
  258. cli/ui/live_hooks.py +43 -0
  259. cli/ui/model_selector.py +532 -0
  260. cli/ui/persistent_loop.py +181 -0
  261. cli/ui/pick.py +113 -0
  262. cli/ui/plain_renderer.py +351 -0
  263. cli/ui/renderer.py +384 -0
  264. cli/ui/rich_renderer.py +533 -0
  265. cli/ui/secrets.py +115 -0
  266. cli/ui/state.py +376 -0
  267. cli/ui/statusbar.py +231 -0
  268. cli/ui/streaming.py +254 -0
  269. cli/ui/terminal_render.py +105 -0
  270. cli/ui/theme.py +132 -0
  271. cli/update/__init__.py +6 -0
  272. cli/update/context.py +121 -0
  273. cli/update/detect.py +176 -0
  274. cli/update/engine.py +95 -0
  275. cli/update/migrate_guarded.py +55 -0
  276. cli/update/process_guard.py +215 -0
  277. cli/update/runners.py +78 -0
  278. cli/update/snapshot.py +270 -0
  279. cli/update/versions.py +205 -0
  280. core/README.md +377 -0
  281. core/__init__.py +174 -0
  282. core/assets.py +87 -0
  283. core/async_bridge.py +76 -0
  284. core/autonomy_runtime.py +259 -0
  285. core/base_component.py +302 -0
  286. core/bootstrap.py +681 -0
  287. core/bot.py +310 -0
  288. core/config.py +833 -0
  289. core/constants.py +126 -0
  290. core/container.py +462 -0
  291. core/db_manifest.py +101 -0
  292. core/embedding.py +57 -0
  293. core/env.py +39 -0
  294. core/exceptions.py +287 -0
  295. core/flags.py +144 -0
  296. core/flags_catalog.py +345 -0
  297. core/home_migration.py +62 -0
  298. core/identity.py +118 -0
  299. core/initialization.py +1004 -0
  300. core/instance.py +537 -0
  301. core/interactive_gate.py +96 -0
  302. core/logging.py +436 -0
  303. core/owner_doc_writer.py +176 -0
  304. core/pairing.py +193 -0
  305. core/path_safety.py +20 -0
  306. core/paths.py +27 -0
  307. core/payment_config.py +21 -0
  308. core/permissions.py +322 -0
  309. core/runtime_config.py +96 -0
  310. core/runtime_paths.py +135 -0
  311. core/seams.py +35 -0
  312. core/secret_scan.py +25 -0
  313. core/secret_scrub.py +71 -0
  314. core/secrets.py +35 -0
  315. core/security_logging_filter.py +148 -0
  316. core/self_context_writer.py +333 -0
  317. core/self_evolution.py +294 -0
  318. core/session_context.py +47 -0
  319. core/sqlite_util.py +59 -0
  320. core/surfaces/__init__.py +16 -0
  321. core/surfaces/access.py +118 -0
  322. core/surfaces/binding.py +88 -0
  323. core/surfaces/bootstrap.py +78 -0
  324. core/surfaces/circuit.py +168 -0
  325. core/surfaces/continuity.py +77 -0
  326. core/surfaces/correspondents.py +228 -0
  327. core/surfaces/dispatcher.py +216 -0
  328. core/surfaces/envelopes.py +75 -0
  329. core/surfaces/gc.py +30 -0
  330. core/surfaces/idempotency.py +57 -0
  331. core/surfaces/inbound_webhook.py +124 -0
  332. core/surfaces/media.py +32 -0
  333. core/surfaces/message_router.py +91 -0
  334. core/surfaces/outbound_allowlist.py +80 -0
  335. core/surfaces/outbound_dispatcher.py +110 -0
  336. core/surfaces/outbound_mirror.py +37 -0
  337. core/surfaces/outbound_queue.py +107 -0
  338. core/surfaces/outbound_target.py +12 -0
  339. core/surfaces/owner_admin.py +29 -0
  340. core/surfaces/proactive.py +78 -0
  341. core/surfaces/progress.py +108 -0
  342. core/surfaces/rate_bucket.py +21 -0
  343. core/surfaces/registry.py +51 -0
  344. core/surfaces/rendering.py +53 -0
  345. core/surfaces/send_policy.py +14 -0
  346. core/surfaces/serialize.py +24 -0
  347. core/surfaces/session_chat_registry.py +112 -0
  348. core/surfaces/session_policy.py +60 -0
  349. core/surfaces/surface.py +288 -0
  350. core/surfaces/transcription.py +88 -0
  351. core/surfaces/voice_echo.py +26 -0
  352. core/surfaces/voice_guard.py +23 -0
  353. core/tickers.py +174 -0
  354. core/tool_catalog.py +192 -0
  355. core/version.py +75 -0
  356. core/wallet/__init__.py +5 -0
  357. core/wallet/agent_wallet.py +59 -0
  358. core/wallet/audit_sink.py +61 -0
  359. core/wallet/config.py +70 -0
  360. core/wallet/factory.py +87 -0
  361. core/wallet/policy.py +104 -0
  362. core/wallet/signer.py +51 -0
  363. cron/README.md +65 -0
  364. cron/__init__.py +0 -0
  365. cron/delivery.py +277 -0
  366. cron/digest.py +124 -0
  367. cron/jobs.py +207 -0
  368. cron/runner.py +297 -0
  369. cron/schedule.py +194 -0
  370. cron/scheduler.py +201 -0
  371. cron/service.py +57 -0
  372. cron/wake_gate.py +223 -0
  373. data/__init__.py +1 -0
  374. data/prompts/autov2_prompts.json +87 -0
  375. data/prompts/skills/browser-automation/SKILL.md +43 -0
  376. data/prompts/skills/coding-workflow/SKILL.md +36 -0
  377. data/prompts/skills/crypto-trading-safety/SKILL.md +53 -0
  378. data/prompts/skills/document-writing/SKILL.md +33 -0
  379. data/prompts/skills/email-comms/SKILL.md +33 -0
  380. data/prompts/skills/file-data-ops/SKILL.md +35 -0
  381. data/prompts/skills/hyperliquid-account-review/SKILL.md +48 -0
  382. data/prompts/skills/hyperliquid-market-data/SKILL.md +52 -0
  383. data/prompts/skills/hyperliquid-trading/SKILL.md +59 -0
  384. data/prompts/skills/lead-research/SKILL.md +86 -0
  385. data/prompts/skills/market-research-brief/SKILL.md +104 -0
  386. data/prompts/skills/person-analyzer/SKILL.md +124 -0
  387. data/prompts/skills/polymarket-market-research/SKILL.md +54 -0
  388. data/prompts/skills/polymarket-portfolio-review/SKILL.md +50 -0
  389. data/prompts/skills/polymarket-trading/SKILL.md +55 -0
  390. data/prompts/skills/presentation-creator/SKILL.md +36 -0
  391. data/prompts/skills/project-analyzer/SKILL.md +169 -0
  392. data/prompts/skills/rules.json +268 -0
  393. data/prompts/skills/secret-handling/SKILL.md +20 -0
  394. data/prompts/skills/skill-authoring/SKILL.md +31 -0
  395. data/prompts/skills/skill-security-review/SKILL.md +27 -0
  396. data/prompts/skills/social-discovery/SKILL.md +57 -0
  397. data/prompts/skills/task-planning/SKILL.md +33 -0
  398. data/prompts/skills/web-research/SKILL.md +39 -0
  399. data/prompts/skills/web-scraping/SKILL.md +73 -0
  400. data/prompts/skills/x-engagement/SKILL.md +69 -0
  401. data/prompts/system_prompts.json +4 -0
  402. data/subscription_channels.json +5 -0
  403. modules/README.md +503 -0
  404. modules/__init__.py +124 -0
  405. modules/auth/__init__.py +13 -0
  406. modules/auth/api_key_manager.py +157 -0
  407. modules/auth/identity_mapper.py +380 -0
  408. modules/auth/siwe_auth.py +273 -0
  409. modules/auth/tier_manager.py +173 -0
  410. modules/base_module.py +190 -0
  411. modules/credits/__init__.py +29 -0
  412. modules/credits/balance_manager.py +237 -0
  413. modules/credits/cost_utils.py +129 -0
  414. modules/credits/pricing.py +139 -0
  415. modules/credits/unified_ledger.py +148 -0
  416. modules/credits/usage_meter.py +310 -0
  417. modules/credits/usage_tracker.py +674 -0
  418. modules/database/__init__.py +33 -0
  419. modules/database/audit_log.py +322 -0
  420. modules/database/auth_tables.py +276 -0
  421. modules/database/connection.py +644 -0
  422. modules/database/connection_pool.py +305 -0
  423. modules/database/conversation_contexts.py +446 -0
  424. modules/database/database_manager.py +237 -0
  425. modules/database/hyperliquid.py +375 -0
  426. modules/database/polymarket.py +574 -0
  427. modules/database/user_mcp_servers.py +728 -0
  428. modules/database/user_profiles.py +562 -0
  429. modules/database/utils.py +107 -0
  430. modules/database/x402_tables.py +92 -0
  431. modules/eip8004/README.md +1069 -0
  432. modules/eip8004/__init__.py +65 -0
  433. modules/eip8004/contracts.py +498 -0
  434. modules/eip8004/models.py +271 -0
  435. modules/eip8004/registration.py +148 -0
  436. modules/eip8004/reputation.py +367 -0
  437. modules/eip8004/validation.py +277 -0
  438. modules/llm/__init__.py +97 -0
  439. modules/llm/adapters.py +963 -0
  440. modules/llm/anthropic_client.py +831 -0
  441. modules/llm/available_models.py +106 -0
  442. modules/llm/brain_scrubber.py +137 -0
  443. modules/llm/cache_hints.py +162 -0
  444. modules/llm/deepseek_client.py +562 -0
  445. modules/llm/gemini_client.py +1367 -0
  446. modules/llm/llm_client.py +1139 -0
  447. modules/llm/llm_client_registry.py +188 -0
  448. modules/llm/llm_factory.py +237 -0
  449. modules/llm/llm_manager.py +1036 -0
  450. modules/llm/messages.py +374 -0
  451. modules/llm/model_registry.py +1935 -0
  452. modules/llm/nvidia_client.py +125 -0
  453. modules/llm/openai_client.py +800 -0
  454. modules/llm/openrouter_client.py +969 -0
  455. modules/llm/profiles.py +178 -0
  456. modules/llm/test_anthropic_cache_capture.py +35 -0
  457. modules/llm/test_cached_pricing_derivation.py +24 -0
  458. modules/llm/think_scrubber.py +444 -0
  459. modules/llm/token_counter.py +268 -0
  460. modules/memory/__init__.py +35 -0
  461. modules/memory/backend_factory.py +83 -0
  462. modules/memory/cache_manager.py +186 -0
  463. modules/memory/episodic.py +108 -0
  464. modules/memory/local_vector_memory_provider.py +552 -0
  465. modules/memory/memory_manager.py +225 -0
  466. modules/memory/models.py +312 -0
  467. modules/memory/provider.py +210 -0
  468. modules/memory/registry.py +205 -0
  469. modules/memory/sqlite_memory_provider.py +767 -0
  470. modules/memory/task/__init__.py +52 -0
  471. modules/memory/task/compaction_manager.py +157 -0
  472. modules/memory/task/context_retriever.py +809 -0
  473. modules/memory/task/hierarchical_memory.py +1002 -0
  474. modules/memory/task/lexical_retriever.py +71 -0
  475. modules/memory/task/null_context_manager.py +213 -0
  476. modules/memory/task/phase_manager.py +625 -0
  477. modules/memory/task/reflection_service.py +157 -0
  478. modules/memory/task/semantic_retriever.py +218 -0
  479. modules/memory/task/task_context_manager.py +1165 -0
  480. modules/memory/task/test_forgetting_engages.py +24 -0
  481. modules/memory/task/test_reflection_llm.py +39 -0
  482. modules/memory/task/test_threat_scan.py +53 -0
  483. modules/memory/task/threat_scan.py +90 -0
  484. modules/memory/test_sqlite_memory_provider.py +106 -0
  485. modules/memory/user_profile_manager.py +527 -0
  486. modules/payments/__init__.py +11 -0
  487. modules/payments/deposit_monitor.py +478 -0
  488. modules/payments/price_oracle.py +70 -0
  489. modules/payments/treasury_sweeper.py +401 -0
  490. modules/payments/wallet_generator.py +145 -0
  491. modules/pfp/__init__.py +1 -0
  492. modules/pfp/config.py +92 -0
  493. modules/pfp/mesh.py +565 -0
  494. modules/pfp/push.py +69 -0
  495. modules/pfp/renderer.py +85 -0
  496. modules/pfp/store.py +82 -0
  497. modules/skills/__init__.py +1 -0
  498. modules/skills/skill_usage.py +248 -0
  499. modules/transcription/__init__.py +24 -0
  500. modules/transcription/base.py +20 -0
  501. modules/transcription/faster_whisper_transcriber.py +55 -0
  502. modules/x402/README.md +249 -0
  503. modules/x402/__init__.py +22 -0
  504. modules/x402/invoicing.py +416 -0
  505. modules/x402/middleware.py +364 -0
  506. modules/x402/settlement_watcher.py +145 -0
  507. modules/x402/x402_integration.py +344 -0
  508. polyrob-0.5.0.dist-info/METADATA +482 -0
  509. polyrob-0.5.0.dist-info/RECORD +745 -0
  510. polyrob-0.5.0.dist-info/WHEEL +5 -0
  511. polyrob-0.5.0.dist-info/entry_points.txt +2 -0
  512. polyrob-0.5.0.dist-info/licenses/LICENSE +21 -0
  513. polyrob-0.5.0.dist-info/top_level.txt +12 -0
  514. surfaces/README.md +70 -0
  515. surfaces/__init__.py +0 -0
  516. surfaces/email/__init__.py +0 -0
  517. surfaces/email/dedup.py +44 -0
  518. surfaces/email/harness.py +187 -0
  519. surfaces/email/inbound.py +159 -0
  520. surfaces/email/seed.py +60 -0
  521. surfaces/email/surface.py +105 -0
  522. surfaces/telegram/__init__.py +0 -0
  523. surfaces/telegram/dedup.py +83 -0
  524. surfaces/telegram/harness.py +886 -0
  525. surfaces/telegram/inbound.py +145 -0
  526. surfaces/telegram/interactive_tools.py +49 -0
  527. surfaces/telegram/markdown.py +347 -0
  528. surfaces/telegram/rate_limit.py +167 -0
  529. surfaces/telegram/surface.py +187 -0
  530. surfaces/telegram/voice.py +52 -0
  531. surfaces/whatsapp/__init__.py +0 -0
  532. surfaces/whatsapp/client.py +51 -0
  533. surfaces/whatsapp/harness.py +62 -0
  534. surfaces/whatsapp/inbound.py +132 -0
  535. surfaces/whatsapp/surface.py +64 -0
  536. surfaces/whatsapp/window.py +34 -0
  537. tools/README.md +578 -0
  538. tools/__init__.py +424 -0
  539. tools/alchemy/__init__.py +5 -0
  540. tools/alchemy/alchemy_tool.py +389 -0
  541. tools/anysite/__init__.py +17 -0
  542. tools/anysite/client.py +88 -0
  543. tools/anysite/tool.py +89 -0
  544. tools/base_tool.py +468 -0
  545. tools/browser/__init__.py +22 -0
  546. tools/browser/actions.py +118 -0
  547. tools/browser/browser.py +1457 -0
  548. tools/browser/browser_manager.py +690 -0
  549. tools/browser/context.py +1748 -0
  550. tools/browser/playwright_utils.py +137 -0
  551. tools/browser/views.py +56 -0
  552. tools/code_exec/SANDBOX_SECURITY.md +144 -0
  553. tools/code_exec/__init__.py +178 -0
  554. tools/code_exec/backend.py +73 -0
  555. tools/code_exec/backends/__init__.py +1 -0
  556. tools/code_exec/backends/docker.py +779 -0
  557. tools/code_exec/backends/local_subprocess.py +154 -0
  558. tools/code_exec/env_policy.py +45 -0
  559. tools/code_exec/result.py +42 -0
  560. tools/code_exec/sandbox_guard.py +59 -0
  561. tools/code_exec/tool.py +227 -0
  562. tools/coding/__init__.py +50 -0
  563. tools/coding/edit.py +107 -0
  564. tools/coding/search.py +113 -0
  565. tools/coding/tool.py +353 -0
  566. tools/collabland/__init__.py +5 -0
  567. tools/collabland/collabland_tool.py +471 -0
  568. tools/controller/__init__.py +0 -0
  569. tools/controller/_helpers.py +165 -0
  570. tools/controller/action_registration.py +1765 -0
  571. tools/controller/approval.py +244 -0
  572. tools/controller/approval_interactive.py +90 -0
  573. tools/controller/delegation.py +195 -0
  574. tools/controller/execution.py +797 -0
  575. tools/controller/execution_context.py +141 -0
  576. tools/controller/hooks.py +135 -0
  577. tools/controller/introspection.py +344 -0
  578. tools/controller/mcp_registrar.py +190 -0
  579. tools/controller/message_send.py +31 -0
  580. tools/controller/registry/__init__.py +4 -0
  581. tools/controller/registry/schema_generators.py +395 -0
  582. tools/controller/registry/schema_sanitizer.py +406 -0
  583. tools/controller/registry/service.py +1266 -0
  584. tools/controller/registry/views.py +151 -0
  585. tools/controller/service.py +409 -0
  586. tools/controller/tool_management.py +334 -0
  587. tools/controller/types.py +28 -0
  588. tools/controller/views.py +450 -0
  589. tools/cronjob_tools.py +166 -0
  590. tools/crypto_trade_gate.py +59 -0
  591. tools/descriptors.py +452 -0
  592. tools/dom/__init__.py +0 -0
  593. tools/dom/history_tree_processor/service.py +244 -0
  594. tools/dom/history_tree_processor/view.py +99 -0
  595. tools/dom/service.py +471 -0
  596. tools/dom/views.py +346 -0
  597. tools/email_tool.py +403 -0
  598. tools/exceptions.py +169 -0
  599. tools/filesystem.py +1038 -0
  600. tools/filesystem_docproc.py +331 -0
  601. tools/filesystem_pdf.py +755 -0
  602. tools/git/__init__.py +42 -0
  603. tools/git/tool.py +258 -0
  604. tools/github/__init__.py +33 -0
  605. tools/github/client.py +94 -0
  606. tools/github/tool.py +237 -0
  607. tools/goal_tools.py +309 -0
  608. tools/hyperliquid/__init__.py +56 -0
  609. tools/hyperliquid/models.py +233 -0
  610. tools/hyperliquid/service.py +1680 -0
  611. tools/knowledge_ingest.py +950 -0
  612. tools/mcp/README.md +535 -0
  613. tools/mcp/__init__.py +52 -0
  614. tools/mcp/catalog.py +115 -0
  615. tools/mcp/config.py +337 -0
  616. tools/mcp/mcp_tool.py +1415 -0
  617. tools/mcp/param_coercion.py +289 -0
  618. tools/mcp/protocol.py +1520 -0
  619. tools/mcp/rate_limit.py +50 -0
  620. tools/mcp/security.py +424 -0
  621. tools/mcp/self_install.py +111 -0
  622. tools/mcp/server_manager.py +829 -0
  623. tools/mcp/subscriptions.py +72 -0
  624. tools/mcp/user_mcp_service.py +899 -0
  625. tools/mcp/validation_tracker.py +89 -0
  626. tools/mcp/views.py +346 -0
  627. tools/oauth/__init__.py +17 -0
  628. tools/oauth/manager.py +75 -0
  629. tools/oauth/provider.py +69 -0
  630. tools/oauth/providers/__init__.py +1 -0
  631. tools/oauth/providers/generic_oauth2.py +96 -0
  632. tools/perplexity_tool.py +280 -0
  633. tools/polymarket/__init__.py +41 -0
  634. tools/polymarket/clob_adapter.py +76 -0
  635. tools/polymarket/models.py +182 -0
  636. tools/polymarket/service.py +2082 -0
  637. tools/self_env/__init__.py +61 -0
  638. tools/self_env/tool.py +274 -0
  639. tools/shell/__init__.py +80 -0
  640. tools/shell/backend_pool.py +118 -0
  641. tools/shell/discipline.py +62 -0
  642. tools/shell/executor.py +157 -0
  643. tools/shell/loopback_allow.py +87 -0
  644. tools/shell/process_registry.py +113 -0
  645. tools/shell/process_tool.py +131 -0
  646. tools/shell/state.py +129 -0
  647. tools/shell/tool.py +151 -0
  648. tools/task_tool.py +637 -0
  649. tools/twitter_tool.py +2086 -0
  650. tools/user_directory.py +179 -0
  651. tools/web_fetch/__init__.py +3 -0
  652. tools/web_fetch/fetcher.py +144 -0
  653. tools/web_fetch/render.py +44 -0
  654. tools/web_fetch/tool.py +60 -0
  655. tools/x402/__init__.py +60 -0
  656. tools/x402/client.py +47 -0
  657. tools/x402/invoice_tool.py +143 -0
  658. tools/x402/real_client.py +250 -0
  659. tools/x402/service.py +131 -0
  660. utils/README.md +498 -0
  661. utils/__init__.py +46 -0
  662. utils/auth_utils.py +114 -0
  663. utils/bounded_collections.py +162 -0
  664. utils/circuit_breaker.py +389 -0
  665. utils/gif_utils.py +435 -0
  666. utils/markdown_utils.py +291 -0
  667. utils/message_utils.py +147 -0
  668. utils/metrics.py +36 -0
  669. utils/path_validator.py +227 -0
  670. utils/rate_limit_manager.py +399 -0
  671. utils/result_size.py +296 -0
  672. utils/time_utils.py +206 -0
  673. utils/user_utils.py +188 -0
  674. webview/README.md +111 -0
  675. webview/RENAME_AND_ALIGN_HANDOFF.md +200 -0
  676. webview/__init__.py +8 -0
  677. webview/activity.py +636 -0
  678. webview/owner_auth.py +108 -0
  679. webview/pages.py +448 -0
  680. webview/repair_sessions.py +399 -0
  681. webview/server.py +4323 -0
  682. webview/server_launcher.py +112 -0
  683. webview/static/css/activity.css +89 -0
  684. webview/static/css/chat.css +1772 -0
  685. webview/static/css/components.css +1190 -0
  686. webview/static/css/config-panel.css +651 -0
  687. webview/static/css/pages/admin.css +766 -0
  688. webview/static/css/pages/profile.css +231 -0
  689. webview/static/css/pages/settings.css +297 -0
  690. webview/static/css/pages/signin.css +316 -0
  691. webview/static/css/style.css +4250 -0
  692. webview/static/css/variables.css +147 -0
  693. webview/static/css/workspace-fullwidth-fix.css +109 -0
  694. webview/static/img/favicon.ico +0 -0
  695. webview/static/js/activity.js +394 -0
  696. webview/static/js/admin/activity.js +186 -0
  697. webview/static/js/admin/dashboard.js +163 -0
  698. webview/static/js/admin/user_detail.js +400 -0
  699. webview/static/js/admin/users.js +174 -0
  700. webview/static/js/admin/utils.js +214 -0
  701. webview/static/js/chat.js +3178 -0
  702. webview/static/js/config-panel.js +522 -0
  703. webview/static/js/constants.js +20 -0
  704. webview/static/js/error-handler.js +226 -0
  705. webview/static/js/ethers.min.js +1 -0
  706. webview/static/js/event-filter.js +137 -0
  707. webview/static/js/event-store.js +351 -0
  708. webview/static/js/file-attachments.js +374 -0
  709. webview/static/js/file-loader.js +163 -0
  710. webview/static/js/index.js +103 -0
  711. webview/static/js/performance-utils.js +196 -0
  712. webview/static/js/profile.js +396 -0
  713. webview/static/js/screenshot.js +386 -0
  714. webview/static/js/session.js +1527 -0
  715. webview/static/js/settings.js +1288 -0
  716. webview/static/js/sidebar-data.js +766 -0
  717. webview/static/js/sidebar-toggle.js +296 -0
  718. webview/static/js/socket.io.min.js +7 -0
  719. webview/static/js/stats.js +355 -0
  720. webview/static/js/ui-utils.js +420 -0
  721. webview/static/js/workspace.js +966 -0
  722. webview/stats_service.py +613 -0
  723. webview/templates/__init__.py +1 -0
  724. webview/templates/__pycache__/__init__.cpython-311.pyc +0 -0
  725. webview/templates/activity.html +36 -0
  726. webview/templates/admin/activity.html +122 -0
  727. webview/templates/admin/dashboard.html +112 -0
  728. webview/templates/admin/user_detail.html +228 -0
  729. webview/templates/admin/users.html +105 -0
  730. webview/templates/autonomy.html +60 -0
  731. webview/templates/error.html +133 -0
  732. webview/templates/finance.html +90 -0
  733. webview/templates/identity.html +73 -0
  734. webview/templates/index.html +42 -0
  735. webview/templates/layout.html +125 -0
  736. webview/templates/memory.html +59 -0
  737. webview/templates/owner_login.html +15 -0
  738. webview/templates/profile.html +121 -0
  739. webview/templates/session.html +312 -0
  740. webview/templates/settings.html +534 -0
  741. webview/templates/sidebar.html +112 -0
  742. webview/templates/signin.html +213 -0
  743. webview/templates/status.html +14 -0
  744. webview/templates/system.html +45 -0
  745. webview/webgate.py +196 -0
@@ -0,0 +1,728 @@
1
+ """
2
+ Database handler for user MCP server configurations.
3
+
4
+ Manages CRUD operations for user_mcp_servers, user_mcp_settings, and user_mcp_audit_log tables.
5
+ """
6
+
7
+ import json
8
+ import logging
9
+ from typing import List, Optional, Dict, Any
10
+ from dataclasses import dataclass, field
11
+ from datetime import datetime
12
+
13
+ from modules.database.connection import DatabaseConnection
14
+ from tools.mcp.security import MCPEncryption, get_encryption
15
+
16
+ logger = logging.getLogger(__name__)
17
+
18
+
19
+ @dataclass
20
+ class UserMCPServer:
21
+ """User MCP server record."""
22
+
23
+ id: int
24
+ user_id: str
25
+ server_name: str
26
+ server_url: str
27
+ server_type: str
28
+ auth_method: str
29
+ enabled: bool
30
+ timeout: int
31
+ retry_attempts: int
32
+ retry_delay: int
33
+ auto_reconnect: bool
34
+ max_concurrent_requests: int
35
+ auth_status: str
36
+ connection_count: int
37
+ tools_discovered: int
38
+ created_at: datetime
39
+ updated_at: datetime
40
+ display_name: Optional[str] = None
41
+ last_connected_at: Optional[datetime] = None
42
+ last_error: Optional[str] = None
43
+ message_endpoint: Optional[str] = None # For SSE servers with separate POST endpoint
44
+
45
+ # Not stored directly - decrypted on demand
46
+ api_key: Optional[str] = field(default=None, repr=False)
47
+ headers: Optional[Dict[str, str]] = field(default=None, repr=False)
48
+
49
+
50
+ @dataclass
51
+ class UserMCPSettings:
52
+ """User MCP settings record."""
53
+
54
+ user_id: str
55
+ mcp_enabled: bool = True
56
+ include_global_servers: bool = True
57
+ max_servers: int = 10
58
+ default_timeout: int = 30
59
+ created_at: Optional[datetime] = None
60
+ updated_at: Optional[datetime] = None
61
+
62
+
63
+ class UserMCPServersHandler:
64
+ """Handler for user MCP server database operations."""
65
+
66
+ # FIX #16: Schema version for migrations
67
+ SCHEMA_VERSION = 2 # Increment when adding migrations
68
+
69
+ # SECURITY FIX: Whitelist of allowed column names to prevent SQL injection
70
+ # Only these columns can be updated via update_server() and update_connection_status()
71
+ ALLOWED_UPDATE_COLUMNS = frozenset({
72
+ 'display_name', 'server_url', 'server_type', 'auth_method',
73
+ 'api_key_encrypted', 'headers_encrypted', 'enabled', 'timeout',
74
+ 'retry_attempts', 'retry_delay', 'auto_reconnect', 'max_concurrent_requests',
75
+ 'auth_status', 'last_connected_at', 'last_error', 'connection_count',
76
+ 'tools_discovered', 'updated_at', 'message_endpoint'
77
+ })
78
+
79
+ def __init__(self, db: DatabaseConnection, encryption: Optional[MCPEncryption] = None):
80
+ """
81
+ Initialize handler.
82
+
83
+ Args:
84
+ db: Database connection
85
+ encryption: Encryption instance (uses default if not provided)
86
+ """
87
+ self.db = db
88
+ self.encryption = encryption or get_encryption()
89
+ self.logger = logging.getLogger('database.user_mcp_servers')
90
+ self._tables_initialized = False
91
+
92
+ async def ensure_tables(self) -> None:
93
+ """
94
+ Ensure all required tables exist and run migrations.
95
+
96
+ Creates tables if they don't exist. Safe to call multiple times.
97
+ Runs any pending schema migrations (FIX #16).
98
+ """
99
+ if self._tables_initialized:
100
+ return
101
+
102
+ self.logger.info("Ensuring user MCP tables exist...")
103
+
104
+ # FIX #16: Create schema version table first
105
+ await self.db.execute('''
106
+ CREATE TABLE IF NOT EXISTS user_mcp_schema_version (
107
+ id INTEGER PRIMARY KEY CHECK (id = 1),
108
+ version INTEGER NOT NULL DEFAULT 1,
109
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
110
+ )
111
+ ''')
112
+
113
+ # Get current schema version
114
+ current_version = await self._get_schema_version()
115
+
116
+ # Create user_mcp_servers table
117
+ await self.db.execute('''
118
+ CREATE TABLE IF NOT EXISTS user_mcp_servers (
119
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
120
+ user_id TEXT NOT NULL,
121
+ server_name TEXT NOT NULL,
122
+ display_name TEXT,
123
+ server_url TEXT NOT NULL,
124
+ server_type TEXT NOT NULL,
125
+ auth_method TEXT NOT NULL DEFAULT 'api_key',
126
+ api_key_encrypted BLOB,
127
+ headers_encrypted BLOB,
128
+ enabled INTEGER DEFAULT 1,
129
+ timeout INTEGER DEFAULT 30,
130
+ retry_attempts INTEGER DEFAULT 3,
131
+ retry_delay INTEGER DEFAULT 5,
132
+ auto_reconnect INTEGER DEFAULT 1,
133
+ max_concurrent_requests INTEGER DEFAULT 5,
134
+ auth_status TEXT DEFAULT 'configured',
135
+ last_connected_at TIMESTAMP,
136
+ last_error TEXT,
137
+ connection_count INTEGER DEFAULT 0,
138
+ tools_discovered INTEGER DEFAULT 0,
139
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
140
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
141
+ UNIQUE(user_id, server_name),
142
+ CHECK (server_type IN ('sse', 'http', 'streamable_http')),
143
+ CHECK (auth_method IN ('api_key', 'bearer', 'none'))
144
+ )
145
+ ''')
146
+
147
+ # Create indices
148
+ await self.db.execute('''
149
+ CREATE INDEX IF NOT EXISTS idx_user_mcp_servers_user
150
+ ON user_mcp_servers(user_id)
151
+ ''')
152
+ await self.db.execute('''
153
+ CREATE INDEX IF NOT EXISTS idx_user_mcp_servers_enabled
154
+ ON user_mcp_servers(user_id, enabled)
155
+ ''')
156
+
157
+ # Create user_mcp_settings table
158
+ await self.db.execute('''
159
+ CREATE TABLE IF NOT EXISTS user_mcp_settings (
160
+ user_id TEXT PRIMARY KEY,
161
+ mcp_enabled INTEGER DEFAULT 1,
162
+ include_global_servers INTEGER DEFAULT 1,
163
+ max_servers INTEGER DEFAULT 10,
164
+ default_timeout INTEGER DEFAULT 30,
165
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
166
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
167
+ )
168
+ ''')
169
+
170
+ # Create user_mcp_audit_log table
171
+ await self.db.execute('''
172
+ CREATE TABLE IF NOT EXISTS user_mcp_audit_log (
173
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
174
+ user_id TEXT NOT NULL,
175
+ action TEXT NOT NULL,
176
+ server_name TEXT,
177
+ details TEXT,
178
+ ip_address TEXT,
179
+ timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
180
+ )
181
+ ''')
182
+
183
+ # Create audit indices
184
+ await self.db.execute('''
185
+ CREATE INDEX IF NOT EXISTS idx_user_mcp_audit_user
186
+ ON user_mcp_audit_log(user_id)
187
+ ''')
188
+ await self.db.execute('''
189
+ CREATE INDEX IF NOT EXISTS idx_user_mcp_audit_time
190
+ ON user_mcp_audit_log(timestamp)
191
+ ''')
192
+
193
+ # FIX #16: Run migrations if needed
194
+ await self._run_migrations(current_version)
195
+
196
+ self._tables_initialized = True
197
+ self.logger.info("User MCP tables ready")
198
+
199
+ async def _get_schema_version(self) -> int:
200
+ """Get current schema version (FIX #16)."""
201
+ try:
202
+ result = await self.db.fetch_one(
203
+ "SELECT version FROM user_mcp_schema_version WHERE id = 1"
204
+ )
205
+ return result[0] if result else 0
206
+ except Exception:
207
+ return 0
208
+
209
+ async def _set_schema_version(self, version: int) -> None:
210
+ """Set schema version (FIX #16)."""
211
+ await self.db.execute('''
212
+ INSERT INTO user_mcp_schema_version (id, version, updated_at)
213
+ VALUES (1, ?, CURRENT_TIMESTAMP)
214
+ ON CONFLICT(id) DO UPDATE SET version = ?, updated_at = CURRENT_TIMESTAMP
215
+ ''', (version, version))
216
+
217
+ async def _run_migrations(self, current_version: int) -> None:
218
+ """Run pending schema migrations (FIX #16)."""
219
+ if current_version >= self.SCHEMA_VERSION:
220
+ return
221
+
222
+ self.logger.info(f"Running migrations from v{current_version} to v{self.SCHEMA_VERSION}")
223
+
224
+ # Migration v1 -> v2: Add message_endpoint column
225
+ if current_version < 2:
226
+ # Check if column already exists before trying to add it
227
+ try:
228
+ result = await self.db.fetch_one(
229
+ "SELECT 1 FROM pragma_table_info('user_mcp_servers') WHERE name='message_endpoint'"
230
+ )
231
+ if not result:
232
+ await self.db.execute('''
233
+ ALTER TABLE user_mcp_servers
234
+ ADD COLUMN message_endpoint TEXT
235
+ ''')
236
+ self.logger.info("Migration v2: Added message_endpoint column")
237
+ else:
238
+ self.logger.debug("Migration v2: message_endpoint column already exists")
239
+ except Exception as e:
240
+ # Silently ignore duplicate column errors
241
+ if "duplicate column" not in str(e).lower():
242
+ self.logger.warning(f"Migration v2 warning: {e}")
243
+
244
+ # Update schema version
245
+ await self._set_schema_version(self.SCHEMA_VERSION)
246
+ self.logger.info(f"Schema migrated to v{self.SCHEMA_VERSION}")
247
+
248
+ async def add_server(
249
+ self,
250
+ user_id: str,
251
+ server_name: str,
252
+ server_url: str,
253
+ server_type: str = "sse",
254
+ auth_method: str = "api_key",
255
+ api_key: Optional[str] = None,
256
+ headers: Optional[Dict[str, str]] = None,
257
+ display_name: Optional[str] = None,
258
+ **options
259
+ ) -> UserMCPServer:
260
+ """
261
+ Add a new MCP server for a user.
262
+
263
+ Args:
264
+ user_id: Owner's user ID
265
+ server_name: Unique name for the server
266
+ server_url: Server endpoint URL
267
+ server_type: 'sse' or 'http' (NOT 'stdio')
268
+ auth_method: 'api_key', 'bearer', or 'none'
269
+ api_key: API key (will be encrypted)
270
+ headers: Custom headers (will be encrypted)
271
+ display_name: Friendly display name
272
+ **options: Additional options (enabled, timeout, etc.)
273
+
274
+ Returns:
275
+ Created UserMCPServer record
276
+
277
+ Raises:
278
+ ValueError: If server_type is 'stdio'
279
+ """
280
+ # Validate server_type
281
+ if server_type not in ("sse", "http", "streamable_http"):
282
+ raise ValueError("Server type must be 'sse', 'http', or 'streamable_http' (STDIO not allowed)")
283
+
284
+ # Encrypt credentials
285
+ api_key_encrypted = self.encryption.encrypt(api_key) if api_key else None
286
+ headers_encrypted = self.encryption.encrypt_dict(headers) if headers else None
287
+
288
+ query = """
289
+ INSERT INTO user_mcp_servers (
290
+ user_id, server_name, display_name, server_url, server_type,
291
+ auth_method, api_key_encrypted, headers_encrypted,
292
+ enabled, timeout, retry_attempts, retry_delay,
293
+ auto_reconnect, max_concurrent_requests, auth_status, message_endpoint
294
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
295
+ """
296
+
297
+ await self.db.execute(query, (
298
+ user_id,
299
+ server_name,
300
+ display_name,
301
+ server_url,
302
+ server_type,
303
+ auth_method,
304
+ api_key_encrypted,
305
+ headers_encrypted,
306
+ 1 if options.get('enabled', True) else 0,
307
+ options.get('timeout', 30),
308
+ options.get('retry_attempts', 3),
309
+ options.get('retry_delay', 5),
310
+ 1 if options.get('auto_reconnect', True) else 0,
311
+ options.get('max_concurrent_requests', 5),
312
+ 'configured' if api_key or auth_method == 'none' else 'unconfigured',
313
+ options.get('message_endpoint')
314
+ ))
315
+
316
+ self.logger.info(f"Added MCP server '{server_name}' for user {user_id}")
317
+
318
+ # Log audit event
319
+ await self._audit_log(user_id, 'add', server_name, {
320
+ 'server_type': server_type,
321
+ 'auth_method': auth_method,
322
+ 'server_url': server_url
323
+ })
324
+
325
+ return await self.get_server(user_id, server_name)
326
+
327
+ async def get_server(self, user_id: str, server_name: str) -> Optional[UserMCPServer]:
328
+ """
329
+ Get a specific server for a user.
330
+
331
+ Args:
332
+ user_id: Owner's user ID
333
+ server_name: Server name
334
+
335
+ Returns:
336
+ UserMCPServer or None if not found
337
+ """
338
+ query = "SELECT * FROM user_mcp_servers WHERE user_id = ? AND server_name = ?"
339
+
340
+ row = await self.db.fetch_one(query, (user_id, server_name))
341
+ if row:
342
+ return self._row_to_server(row)
343
+ return None
344
+
345
+ async def get_server_by_id(self, server_id: int) -> Optional[UserMCPServer]:
346
+ """
347
+ Get a server by its ID.
348
+
349
+ Args:
350
+ server_id: Server record ID
351
+
352
+ Returns:
353
+ UserMCPServer or None if not found
354
+ """
355
+ query = "SELECT * FROM user_mcp_servers WHERE id = ?"
356
+
357
+ row = await self.db.fetch_one(query, (server_id,))
358
+ if row:
359
+ return self._row_to_server(row)
360
+ return None
361
+
362
+ async def get_user_servers(
363
+ self,
364
+ user_id: str,
365
+ enabled_only: bool = False
366
+ ) -> List[UserMCPServer]:
367
+ """
368
+ Get all MCP servers for a user.
369
+
370
+ Args:
371
+ user_id: Owner's user ID
372
+ enabled_only: Only return enabled servers
373
+
374
+ Returns:
375
+ List of UserMCPServer records
376
+ """
377
+ query = "SELECT * FROM user_mcp_servers WHERE user_id = ?"
378
+ params = [user_id]
379
+
380
+ if enabled_only:
381
+ query += " AND enabled = 1"
382
+
383
+ query += " ORDER BY created_at DESC"
384
+
385
+ rows = await self.db.fetch_all(query, tuple(params))
386
+ return [self._row_to_server(row) for row in rows]
387
+
388
+ async def update_server(
389
+ self,
390
+ user_id: str,
391
+ server_name: str,
392
+ **updates
393
+ ) -> bool:
394
+ """
395
+ Update server configuration.
396
+
397
+ Args:
398
+ user_id: Owner's user ID
399
+ server_name: Server name
400
+ **updates: Fields to update
401
+
402
+ Returns:
403
+ True if updated successfully
404
+ """
405
+ if not updates:
406
+ return False
407
+
408
+ # Handle credential updates specially
409
+ if 'api_key' in updates:
410
+ api_key = updates.pop('api_key')
411
+ updates['api_key_encrypted'] = self.encryption.encrypt(api_key) if api_key else None
412
+
413
+ if 'headers' in updates:
414
+ headers = updates.pop('headers')
415
+ updates['headers_encrypted'] = self.encryption.encrypt_dict(headers) if headers else None
416
+
417
+ # Convert boolean values to integers for SQLite
418
+ for key in ['enabled', 'auto_reconnect']:
419
+ if key in updates:
420
+ updates[key] = 1 if updates[key] else 0
421
+
422
+ updates['updated_at'] = datetime.utcnow().isoformat()
423
+
424
+ # SECURITY FIX: Validate all column names against whitelist to prevent SQL injection
425
+ invalid_columns = set(updates.keys()) - self.ALLOWED_UPDATE_COLUMNS
426
+ if invalid_columns:
427
+ self.logger.error(f"SQL injection attempt blocked: invalid columns {invalid_columns}")
428
+ raise ValueError(f"Invalid column names: {invalid_columns}")
429
+
430
+ set_clause = ", ".join(f"{k} = ?" for k in updates.keys())
431
+ query = f"""
432
+ UPDATE user_mcp_servers
433
+ SET {set_clause}
434
+ WHERE user_id = ? AND server_name = ?
435
+ """
436
+
437
+ result = await self.db.execute(
438
+ query,
439
+ tuple(list(updates.values()) + [user_id, server_name])
440
+ )
441
+
442
+ if result:
443
+ self.logger.info(f"Updated MCP server '{server_name}' for user {user_id}")
444
+ await self._audit_log(user_id, 'update', server_name, {
445
+ 'fields': list(updates.keys())
446
+ })
447
+
448
+ return result > 0 if isinstance(result, int) else True
449
+
450
+ async def delete_server(self, user_id: str, server_name: str) -> bool:
451
+ """
452
+ Delete a server.
453
+
454
+ Args:
455
+ user_id: Owner's user ID
456
+ server_name: Server name
457
+
458
+ Returns:
459
+ True if deleted successfully
460
+ """
461
+ query = "DELETE FROM user_mcp_servers WHERE user_id = ? AND server_name = ?"
462
+
463
+ result = await self.db.execute(query, (user_id, server_name))
464
+
465
+ if result:
466
+ self.logger.info(f"Deleted MCP server '{server_name}' for user {user_id}")
467
+ await self._audit_log(user_id, 'delete', server_name)
468
+
469
+ return result > 0 if isinstance(result, int) else True
470
+
471
+ async def get_decrypted_credentials(
472
+ self,
473
+ user_id: str,
474
+ server_name: str
475
+ ) -> tuple[Optional[str], Optional[Dict[str, str]]]:
476
+ """
477
+ Get decrypted API key and headers for a server.
478
+
479
+ Args:
480
+ user_id: Owner's user ID
481
+ server_name: Server name
482
+
483
+ Returns:
484
+ Tuple of (api_key, headers) - both may be None
485
+ """
486
+ query = """
487
+ SELECT api_key_encrypted, headers_encrypted
488
+ FROM user_mcp_servers
489
+ WHERE user_id = ? AND server_name = ?
490
+ """
491
+
492
+ row = await self.db.fetch_one(query, (user_id, server_name))
493
+
494
+ if not row:
495
+ return None, None
496
+
497
+ api_key = self.encryption.decrypt(row['api_key_encrypted']) if row['api_key_encrypted'] else None
498
+ headers = self.encryption.decrypt_dict(row['headers_encrypted']) if row['headers_encrypted'] else None
499
+
500
+ return api_key, headers
501
+
502
+ async def update_connection_status(
503
+ self,
504
+ user_id: str,
505
+ server_name: str,
506
+ connected: bool,
507
+ error: Optional[str] = None,
508
+ tools_count: Optional[int] = None
509
+ ) -> bool:
510
+ """
511
+ Update server connection status.
512
+
513
+ Args:
514
+ user_id: Owner's user ID
515
+ server_name: Server name
516
+ connected: Whether connection was successful
517
+ error: Error message if failed
518
+ tools_count: Number of tools discovered
519
+
520
+ Returns:
521
+ True if updated
522
+ """
523
+ updates = {
524
+ 'last_error': error,
525
+ 'auth_status': 'connected' if connected else 'error',
526
+ 'updated_at': datetime.utcnow().isoformat()
527
+ }
528
+
529
+ if connected:
530
+ updates['last_connected_at'] = datetime.utcnow().isoformat()
531
+ updates['connection_count'] = 'connection_count + 1' # Will need special handling
532
+
533
+ if tools_count is not None:
534
+ updates['tools_discovered'] = tools_count
535
+
536
+ # Build query with special handling for increment
537
+ set_parts = []
538
+ values = []
539
+ for k, v in updates.items():
540
+ if v == 'connection_count + 1':
541
+ set_parts.append("connection_count = connection_count + 1")
542
+ else:
543
+ set_parts.append(f"{k} = ?")
544
+ values.append(v)
545
+
546
+ query = f"""
547
+ UPDATE user_mcp_servers
548
+ SET {', '.join(set_parts)}
549
+ WHERE user_id = ? AND server_name = ?
550
+ """
551
+
552
+ result = await self.db.execute(query, tuple(values + [user_id, server_name]))
553
+ return result > 0 if isinstance(result, int) else True
554
+
555
+ async def get_user_settings(self, user_id: str) -> UserMCPSettings:
556
+ """
557
+ Get user's MCP settings (creates defaults if not exist).
558
+
559
+ Args:
560
+ user_id: User ID
561
+
562
+ Returns:
563
+ UserMCPSettings record
564
+ """
565
+ query = "SELECT * FROM user_mcp_settings WHERE user_id = ?"
566
+ row = await self.db.fetch_one(query, (user_id,))
567
+
568
+ if row:
569
+ return UserMCPSettings(
570
+ user_id=row['user_id'],
571
+ mcp_enabled=bool(row['mcp_enabled']),
572
+ include_global_servers=bool(row['include_global_servers']),
573
+ max_servers=row['max_servers'],
574
+ default_timeout=row['default_timeout'],
575
+ created_at=row.get('created_at'),
576
+ updated_at=row.get('updated_at')
577
+ )
578
+
579
+ # Return defaults (not persisted until explicitly saved)
580
+ return UserMCPSettings(user_id=user_id)
581
+
582
+ async def update_user_settings(self, user_id: str, **updates) -> bool:
583
+ """
584
+ Update user's MCP settings (upsert).
585
+
586
+ Args:
587
+ user_id: User ID
588
+ **updates: Settings to update
589
+
590
+ Returns:
591
+ True if updated
592
+ """
593
+ # Convert boolean values
594
+ for key in ['mcp_enabled', 'include_global_servers']:
595
+ if key in updates:
596
+ updates[key] = 1 if updates[key] else 0
597
+
598
+ # Upsert query
599
+ columns = ['user_id'] + list(updates.keys()) + ['updated_at']
600
+ values = [user_id] + list(updates.values()) + [datetime.utcnow().isoformat()]
601
+ placeholders = ', '.join(['?'] * len(columns))
602
+
603
+ update_clause = ', '.join(f"{k} = excluded.{k}" for k in updates.keys())
604
+ update_clause += ", updated_at = excluded.updated_at"
605
+
606
+ query = f"""
607
+ INSERT INTO user_mcp_settings ({', '.join(columns)})
608
+ VALUES ({placeholders})
609
+ ON CONFLICT(user_id) DO UPDATE SET {update_clause}
610
+ """
611
+
612
+ await self.db.execute(query, tuple(values))
613
+ return True
614
+
615
+ async def _audit_log(
616
+ self,
617
+ user_id: str,
618
+ action: str,
619
+ server_name: Optional[str] = None,
620
+ details: Optional[Dict[str, Any]] = None,
621
+ ip_address: Optional[str] = None
622
+ ) -> None:
623
+ """
624
+ Record an audit log entry.
625
+
626
+ Args:
627
+ user_id: User who performed the action
628
+ action: Action type ('add', 'update', 'delete', 'connect', 'error')
629
+ server_name: Server name if applicable
630
+ details: Additional details (JSON serializable, no secrets!)
631
+ ip_address: Client IP address
632
+ """
633
+ query = """
634
+ INSERT INTO user_mcp_audit_log (user_id, action, server_name, details, ip_address)
635
+ VALUES (?, ?, ?, ?, ?)
636
+ """
637
+
638
+ details_json = json.dumps(details) if details else None
639
+
640
+ await self.db.execute(query, (
641
+ user_id,
642
+ action,
643
+ server_name,
644
+ details_json,
645
+ ip_address
646
+ ))
647
+
648
+ async def get_audit_log(
649
+ self,
650
+ user_id: str,
651
+ limit: int = 100,
652
+ server_name: Optional[str] = None
653
+ ) -> List[Dict[str, Any]]:
654
+ """
655
+ Get audit log entries for a user.
656
+
657
+ Args:
658
+ user_id: User ID
659
+ limit: Maximum entries to return
660
+ server_name: Filter by server name
661
+
662
+ Returns:
663
+ List of audit log entries
664
+ """
665
+ query = "SELECT * FROM user_mcp_audit_log WHERE user_id = ?"
666
+ params = [user_id]
667
+
668
+ if server_name:
669
+ query += " AND server_name = ?"
670
+ params.append(server_name)
671
+
672
+ query += " ORDER BY timestamp DESC LIMIT ?"
673
+ params.append(limit)
674
+
675
+ rows = await self.db.fetch_all(query, tuple(params))
676
+
677
+ return [
678
+ {
679
+ 'id': row['id'],
680
+ 'user_id': row['user_id'],
681
+ 'action': row['action'],
682
+ 'server_name': row['server_name'],
683
+ 'details': json.loads(row['details']) if row['details'] else None,
684
+ 'ip_address': row['ip_address'],
685
+ 'timestamp': row['timestamp']
686
+ }
687
+ for row in rows
688
+ ]
689
+
690
+ def _row_to_server(self, row) -> UserMCPServer:
691
+ """Convert database row to UserMCPServer object."""
692
+ return UserMCPServer(
693
+ id=row['id'],
694
+ user_id=row['user_id'],
695
+ server_name=row['server_name'],
696
+ display_name=row.get('display_name'),
697
+ server_url=row['server_url'],
698
+ server_type=row['server_type'],
699
+ auth_method=row['auth_method'],
700
+ enabled=bool(row['enabled']),
701
+ timeout=row['timeout'],
702
+ retry_attempts=row['retry_attempts'],
703
+ retry_delay=row['retry_delay'],
704
+ auto_reconnect=bool(row['auto_reconnect']),
705
+ max_concurrent_requests=row['max_concurrent_requests'],
706
+ auth_status=row['auth_status'],
707
+ last_connected_at=row.get('last_connected_at'),
708
+ last_error=row.get('last_error'),
709
+ connection_count=row['connection_count'],
710
+ tools_discovered=row['tools_discovered'],
711
+ created_at=row['created_at'],
712
+ updated_at=row['updated_at'],
713
+ message_endpoint=row.get('message_endpoint')
714
+ )
715
+
716
+ async def server_count(self, user_id: str) -> int:
717
+ """
718
+ Get count of servers for a user.
719
+
720
+ Args:
721
+ user_id: User ID
722
+
723
+ Returns:
724
+ Number of servers
725
+ """
726
+ query = "SELECT COUNT(*) as count FROM user_mcp_servers WHERE user_id = ?"
727
+ row = await self.db.fetch_one(query, (user_id,))
728
+ return row['count'] if row else 0