crca 1.4.0__py3-none-any.whl → 1.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 (306) hide show
  1. CRCA.py +172 -7
  2. MODEL_CARD.md +53 -0
  3. PKG-INFO +8 -2
  4. RELEASE_NOTES.md +17 -0
  5. STABILITY.md +19 -0
  6. architecture/hybrid/consistency_engine.py +362 -0
  7. architecture/hybrid/conversation_manager.py +421 -0
  8. architecture/hybrid/explanation_generator.py +452 -0
  9. architecture/hybrid/few_shot_learner.py +533 -0
  10. architecture/hybrid/graph_compressor.py +286 -0
  11. architecture/hybrid/hybrid_agent.py +4398 -0
  12. architecture/hybrid/language_compiler.py +623 -0
  13. architecture/hybrid/main,py +0 -0
  14. architecture/hybrid/reasoning_tracker.py +322 -0
  15. architecture/hybrid/self_verifier.py +524 -0
  16. architecture/hybrid/task_decomposer.py +567 -0
  17. architecture/hybrid/text_corrector.py +341 -0
  18. benchmark_results/crca_core_benchmarks.json +178 -0
  19. branches/crca_sd/crca_sd_realtime.py +6 -2
  20. branches/general_agent/__init__.py +102 -0
  21. branches/general_agent/general_agent.py +1400 -0
  22. branches/general_agent/personality.py +169 -0
  23. branches/general_agent/utils/__init__.py +19 -0
  24. branches/general_agent/utils/prompt_builder.py +170 -0
  25. {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/METADATA +8 -2
  26. {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/RECORD +303 -20
  27. crca_core/__init__.py +35 -0
  28. crca_core/benchmarks/__init__.py +14 -0
  29. crca_core/benchmarks/synthetic_scm.py +103 -0
  30. crca_core/core/__init__.py +23 -0
  31. crca_core/core/api.py +120 -0
  32. crca_core/core/estimate.py +208 -0
  33. crca_core/core/godclass.py +72 -0
  34. crca_core/core/intervention_design.py +174 -0
  35. crca_core/core/lifecycle.py +48 -0
  36. crca_core/discovery/__init__.py +9 -0
  37. crca_core/discovery/tabular.py +193 -0
  38. crca_core/identify/__init__.py +171 -0
  39. crca_core/identify/backdoor.py +39 -0
  40. crca_core/identify/frontdoor.py +48 -0
  41. crca_core/identify/graph.py +106 -0
  42. crca_core/identify/id_algorithm.py +43 -0
  43. crca_core/identify/iv.py +48 -0
  44. crca_core/models/__init__.py +67 -0
  45. crca_core/models/provenance.py +56 -0
  46. crca_core/models/refusal.py +39 -0
  47. crca_core/models/result.py +83 -0
  48. crca_core/models/spec.py +151 -0
  49. crca_core/models/validation.py +68 -0
  50. crca_core/scm/__init__.py +9 -0
  51. crca_core/scm/linear_gaussian.py +198 -0
  52. crca_core/timeseries/__init__.py +6 -0
  53. crca_core/timeseries/pcmci.py +181 -0
  54. crca_llm/__init__.py +12 -0
  55. crca_llm/client.py +85 -0
  56. crca_llm/coauthor.py +118 -0
  57. crca_llm/orchestrator.py +289 -0
  58. crca_llm/types.py +21 -0
  59. crca_reasoning/__init__.py +16 -0
  60. crca_reasoning/critique.py +54 -0
  61. crca_reasoning/godclass.py +206 -0
  62. crca_reasoning/memory.py +24 -0
  63. crca_reasoning/rationale.py +10 -0
  64. crca_reasoning/react_controller.py +81 -0
  65. crca_reasoning/tool_router.py +97 -0
  66. crca_reasoning/types.py +40 -0
  67. crca_sd/__init__.py +15 -0
  68. crca_sd/crca_sd_core.py +2 -0
  69. crca_sd/crca_sd_governance.py +2 -0
  70. crca_sd/crca_sd_mpc.py +2 -0
  71. crca_sd/crca_sd_realtime.py +2 -0
  72. crca_sd/crca_sd_tui.py +2 -0
  73. cuda-keyring_1.1-1_all.deb +0 -0
  74. cuda-keyring_1.1-1_all.deb.1 +0 -0
  75. docs/IMAGE_ANNOTATION_USAGE.md +539 -0
  76. docs/INSTALL_DEEPSPEED.md +125 -0
  77. docs/api/branches/crca-cg.md +19 -0
  78. docs/api/branches/crca-q.md +27 -0
  79. docs/api/branches/crca-sd.md +37 -0
  80. docs/api/branches/general-agent.md +24 -0
  81. docs/api/branches/overview.md +19 -0
  82. docs/api/crca/agent-methods.md +62 -0
  83. docs/api/crca/operations.md +79 -0
  84. docs/api/crca/overview.md +32 -0
  85. docs/api/image-annotation/engine.md +52 -0
  86. docs/api/image-annotation/overview.md +17 -0
  87. docs/api/schemas/annotation.md +34 -0
  88. docs/api/schemas/core-schemas.md +82 -0
  89. docs/api/schemas/overview.md +32 -0
  90. docs/api/schemas/policy.md +30 -0
  91. docs/api/utils/conversation.md +22 -0
  92. docs/api/utils/graph-reasoner.md +32 -0
  93. docs/api/utils/overview.md +21 -0
  94. docs/api/utils/router.md +19 -0
  95. docs/api/utils/utilities.md +97 -0
  96. docs/architecture/causal-graphs.md +41 -0
  97. docs/architecture/data-flow.md +29 -0
  98. docs/architecture/design-principles.md +33 -0
  99. docs/architecture/hybrid-agent/components.md +38 -0
  100. docs/architecture/hybrid-agent/consistency.md +26 -0
  101. docs/architecture/hybrid-agent/overview.md +44 -0
  102. docs/architecture/hybrid-agent/reasoning.md +22 -0
  103. docs/architecture/llm-integration.md +26 -0
  104. docs/architecture/modular-structure.md +37 -0
  105. docs/architecture/overview.md +69 -0
  106. docs/architecture/policy-engine-arch.md +29 -0
  107. docs/branches/crca-cg/corposwarm.md +39 -0
  108. docs/branches/crca-cg/esg-scoring.md +30 -0
  109. docs/branches/crca-cg/multi-agent.md +35 -0
  110. docs/branches/crca-cg/overview.md +40 -0
  111. docs/branches/crca-q/alternative-data.md +55 -0
  112. docs/branches/crca-q/architecture.md +71 -0
  113. docs/branches/crca-q/backtesting.md +45 -0
  114. docs/branches/crca-q/causal-engine.md +33 -0
  115. docs/branches/crca-q/execution.md +39 -0
  116. docs/branches/crca-q/market-data.md +60 -0
  117. docs/branches/crca-q/overview.md +58 -0
  118. docs/branches/crca-q/philosophy.md +60 -0
  119. docs/branches/crca-q/portfolio-optimization.md +66 -0
  120. docs/branches/crca-q/risk-management.md +102 -0
  121. docs/branches/crca-q/setup.md +65 -0
  122. docs/branches/crca-q/signal-generation.md +61 -0
  123. docs/branches/crca-q/signal-validation.md +43 -0
  124. docs/branches/crca-sd/core.md +84 -0
  125. docs/branches/crca-sd/governance.md +53 -0
  126. docs/branches/crca-sd/mpc-solver.md +65 -0
  127. docs/branches/crca-sd/overview.md +59 -0
  128. docs/branches/crca-sd/realtime.md +28 -0
  129. docs/branches/crca-sd/tui.md +20 -0
  130. docs/branches/general-agent/overview.md +37 -0
  131. docs/branches/general-agent/personality.md +36 -0
  132. docs/branches/general-agent/prompt-builder.md +30 -0
  133. docs/changelog/index.md +79 -0
  134. docs/contributing/code-style.md +69 -0
  135. docs/contributing/documentation.md +43 -0
  136. docs/contributing/overview.md +29 -0
  137. docs/contributing/testing.md +29 -0
  138. docs/core/crcagent/async-operations.md +65 -0
  139. docs/core/crcagent/automatic-extraction.md +107 -0
  140. docs/core/crcagent/batch-prediction.md +80 -0
  141. docs/core/crcagent/bayesian-inference.md +60 -0
  142. docs/core/crcagent/causal-graph.md +92 -0
  143. docs/core/crcagent/counterfactuals.md +96 -0
  144. docs/core/crcagent/deterministic-simulation.md +78 -0
  145. docs/core/crcagent/dual-mode-operation.md +82 -0
  146. docs/core/crcagent/initialization.md +88 -0
  147. docs/core/crcagent/optimization.md +65 -0
  148. docs/core/crcagent/overview.md +63 -0
  149. docs/core/crcagent/time-series.md +57 -0
  150. docs/core/schemas/annotation.md +30 -0
  151. docs/core/schemas/core-schemas.md +82 -0
  152. docs/core/schemas/overview.md +30 -0
  153. docs/core/schemas/policy.md +41 -0
  154. docs/core/templates/base-agent.md +31 -0
  155. docs/core/templates/feature-mixins.md +31 -0
  156. docs/core/templates/overview.md +29 -0
  157. docs/core/templates/templates-guide.md +75 -0
  158. docs/core/tools/mcp-client.md +34 -0
  159. docs/core/tools/overview.md +24 -0
  160. docs/core/utils/conversation.md +27 -0
  161. docs/core/utils/graph-reasoner.md +29 -0
  162. docs/core/utils/overview.md +27 -0
  163. docs/core/utils/router.md +27 -0
  164. docs/core/utils/utilities.md +97 -0
  165. docs/css/custom.css +84 -0
  166. docs/examples/basic-usage.md +57 -0
  167. docs/examples/general-agent/general-agent-examples.md +50 -0
  168. docs/examples/hybrid-agent/hybrid-agent-examples.md +56 -0
  169. docs/examples/image-annotation/image-annotation-examples.md +54 -0
  170. docs/examples/integration/integration-examples.md +58 -0
  171. docs/examples/overview.md +37 -0
  172. docs/examples/trading/trading-examples.md +46 -0
  173. docs/features/causal-reasoning/advanced-topics.md +101 -0
  174. docs/features/causal-reasoning/counterfactuals.md +43 -0
  175. docs/features/causal-reasoning/do-calculus.md +50 -0
  176. docs/features/causal-reasoning/overview.md +47 -0
  177. docs/features/causal-reasoning/structural-models.md +52 -0
  178. docs/features/hybrid-agent/advanced-components.md +55 -0
  179. docs/features/hybrid-agent/core-components.md +64 -0
  180. docs/features/hybrid-agent/overview.md +34 -0
  181. docs/features/image-annotation/engine.md +82 -0
  182. docs/features/image-annotation/features.md +113 -0
  183. docs/features/image-annotation/integration.md +75 -0
  184. docs/features/image-annotation/overview.md +53 -0
  185. docs/features/image-annotation/quickstart.md +73 -0
  186. docs/features/policy-engine/doctrine-ledger.md +105 -0
  187. docs/features/policy-engine/monitoring.md +44 -0
  188. docs/features/policy-engine/mpc-control.md +89 -0
  189. docs/features/policy-engine/overview.md +46 -0
  190. docs/getting-started/configuration.md +225 -0
  191. docs/getting-started/first-agent.md +164 -0
  192. docs/getting-started/installation.md +144 -0
  193. docs/getting-started/quickstart.md +137 -0
  194. docs/index.md +118 -0
  195. docs/js/mathjax.js +13 -0
  196. docs/lrm/discovery_proof_notes.md +25 -0
  197. docs/lrm/finetune_full.md +83 -0
  198. docs/lrm/math_appendix.md +120 -0
  199. docs/lrm/overview.md +32 -0
  200. docs/mkdocs.yml +238 -0
  201. docs/stylesheets/extra.css +21 -0
  202. docs_generated/crca_core/CounterfactualResult.md +12 -0
  203. docs_generated/crca_core/DiscoveryHypothesisResult.md +13 -0
  204. docs_generated/crca_core/DraftSpec.md +13 -0
  205. docs_generated/crca_core/EstimateResult.md +13 -0
  206. docs_generated/crca_core/IdentificationResult.md +17 -0
  207. docs_generated/crca_core/InterventionDesignResult.md +12 -0
  208. docs_generated/crca_core/LockedSpec.md +15 -0
  209. docs_generated/crca_core/RefusalResult.md +12 -0
  210. docs_generated/crca_core/ValidationReport.md +9 -0
  211. docs_generated/crca_core/index.md +13 -0
  212. examples/general_agent_example.py +277 -0
  213. examples/general_agent_quickstart.py +202 -0
  214. examples/general_agent_simple.py +92 -0
  215. examples/hybrid_agent_auto_extraction.py +84 -0
  216. examples/hybrid_agent_dictionary_demo.py +104 -0
  217. examples/hybrid_agent_enhanced.py +179 -0
  218. examples/hybrid_agent_general_knowledge.py +107 -0
  219. examples/image_annotation_quickstart.py +328 -0
  220. examples/test_hybrid_fixes.py +77 -0
  221. image_annotation/__init__.py +27 -0
  222. image_annotation/annotation_engine.py +2593 -0
  223. install_cuda_wsl2.sh +59 -0
  224. install_deepspeed.sh +56 -0
  225. install_deepspeed_simple.sh +87 -0
  226. mkdocs.yml +252 -0
  227. ollama/Modelfile +8 -0
  228. prompts/__init__.py +2 -1
  229. prompts/default_crca.py +9 -1
  230. prompts/general_agent.py +227 -0
  231. prompts/image_annotation.py +56 -0
  232. pyproject.toml +17 -2
  233. requirements-docs.txt +10 -0
  234. requirements.txt +21 -2
  235. schemas/__init__.py +26 -1
  236. schemas/annotation.py +222 -0
  237. schemas/conversation.py +193 -0
  238. schemas/hybrid.py +211 -0
  239. schemas/reasoning.py +276 -0
  240. schemas_export/crca_core/CounterfactualResult.schema.json +108 -0
  241. schemas_export/crca_core/DiscoveryHypothesisResult.schema.json +113 -0
  242. schemas_export/crca_core/DraftSpec.schema.json +635 -0
  243. schemas_export/crca_core/EstimateResult.schema.json +113 -0
  244. schemas_export/crca_core/IdentificationResult.schema.json +145 -0
  245. schemas_export/crca_core/InterventionDesignResult.schema.json +111 -0
  246. schemas_export/crca_core/LockedSpec.schema.json +646 -0
  247. schemas_export/crca_core/RefusalResult.schema.json +90 -0
  248. schemas_export/crca_core/ValidationReport.schema.json +62 -0
  249. scripts/build_lrm_dataset.py +80 -0
  250. scripts/export_crca_core_schemas.py +54 -0
  251. scripts/export_hf_lrm.py +37 -0
  252. scripts/export_ollama_gguf.py +45 -0
  253. scripts/generate_changelog.py +157 -0
  254. scripts/generate_crca_core_docs_from_schemas.py +86 -0
  255. scripts/run_crca_core_benchmarks.py +163 -0
  256. scripts/run_full_finetune.py +198 -0
  257. scripts/run_lrm_eval.py +31 -0
  258. templates/graph_management.py +29 -0
  259. tests/conftest.py +9 -0
  260. tests/test_core.py +2 -3
  261. tests/test_crca_core_discovery_tabular.py +15 -0
  262. tests/test_crca_core_estimate_dowhy.py +36 -0
  263. tests/test_crca_core_identify.py +18 -0
  264. tests/test_crca_core_intervention_design.py +36 -0
  265. tests/test_crca_core_linear_gaussian_scm.py +69 -0
  266. tests/test_crca_core_spec.py +25 -0
  267. tests/test_crca_core_timeseries_pcmci.py +15 -0
  268. tests/test_crca_llm_coauthor.py +12 -0
  269. tests/test_crca_llm_orchestrator.py +80 -0
  270. tests/test_hybrid_agent_llm_enhanced.py +556 -0
  271. tests/test_image_annotation_demo.py +376 -0
  272. tests/test_image_annotation_operational.py +408 -0
  273. tests/test_image_annotation_unit.py +551 -0
  274. tests/test_training_moe.py +13 -0
  275. training/__init__.py +42 -0
  276. training/datasets.py +140 -0
  277. training/deepspeed_zero2_0_5b.json +22 -0
  278. training/deepspeed_zero2_1_5b.json +22 -0
  279. training/deepspeed_zero3_0_5b.json +28 -0
  280. training/deepspeed_zero3_14b.json +28 -0
  281. training/deepspeed_zero3_h100_3gpu.json +20 -0
  282. training/deepspeed_zero3_offload.json +28 -0
  283. training/eval.py +92 -0
  284. training/finetune.py +516 -0
  285. training/public_datasets.py +89 -0
  286. training_data/react_train.jsonl +7473 -0
  287. utils/agent_discovery.py +311 -0
  288. utils/batch_processor.py +317 -0
  289. utils/conversation.py +78 -0
  290. utils/edit_distance.py +118 -0
  291. utils/formatter.py +33 -0
  292. utils/graph_reasoner.py +530 -0
  293. utils/rate_limiter.py +283 -0
  294. utils/router.py +2 -2
  295. utils/tool_discovery.py +307 -0
  296. webui/__init__.py +10 -0
  297. webui/app.py +229 -0
  298. webui/config.py +104 -0
  299. webui/static/css/style.css +332 -0
  300. webui/static/js/main.js +284 -0
  301. webui/templates/index.html +42 -0
  302. tests/test_crca_excel.py +0 -166
  303. tests/test_data_broker.py +0 -424
  304. tests/test_palantir.py +0 -349
  305. {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/WHEEL +0 -0
  306. {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,284 @@
1
+ // CRCA Web UI - Main JavaScript
2
+ // State management and UI interactions
3
+
4
+ // Application State
5
+ const AppState = {
6
+ currentAgentKey: null,
7
+ agents: [],
8
+ isLoading: false,
9
+ sessionId: null
10
+ };
11
+
12
+ // DOM Elements Cache
13
+ const DOM = {
14
+ agentSelect: null,
15
+ agentInfo: null,
16
+ chatMessages: null,
17
+ chatInput: null,
18
+ sendButton: null
19
+ };
20
+
21
+ // Initialize application
22
+ async function init() {
23
+ try {
24
+ // Cache DOM elements
25
+ DOM.agentSelect = document.getElementById('agentSelect');
26
+ DOM.agentInfo = document.getElementById('agentInfo');
27
+ DOM.chatMessages = document.getElementById('chatMessages');
28
+ DOM.chatInput = document.getElementById('chatInput');
29
+ DOM.sendButton = document.getElementById('sendButton');
30
+
31
+ // Load agents and setup
32
+ await loadAgents();
33
+ setupEventListeners();
34
+
35
+ // Focus input
36
+ DOM.chatInput.focus();
37
+ } catch (error) {
38
+ console.error('Initialization error:', error);
39
+ addMessage('error', 'System', 'Failed to initialize application. Please refresh the page.');
40
+ }
41
+ }
42
+
43
+ // Load available agents from API
44
+ async function loadAgents() {
45
+ try {
46
+ const response = await fetch('/api/agents');
47
+
48
+ if (!response.ok) {
49
+ throw new Error(`HTTP error! status: ${response.status}`);
50
+ }
51
+
52
+ const data = await response.json();
53
+
54
+ AppState.agents = data.agents || [];
55
+ AppState.currentAgentKey = data.default || (AppState.agents.length > 0 ? AppState.agents[0].key : null);
56
+
57
+ // Populate agent selector
58
+ populateAgentSelector();
59
+ updateAgentInfo();
60
+
61
+ } catch (error) {
62
+ console.error('Failed to load agents:', error);
63
+ addMessage('error', 'System', 'Failed to load agents. Please refresh the page.');
64
+ }
65
+ }
66
+
67
+ // Populate agent selector dropdown
68
+ function populateAgentSelector() {
69
+ if (!DOM.agentSelect) return;
70
+
71
+ DOM.agentSelect.innerHTML = '';
72
+
73
+ if (AppState.agents.length === 0) {
74
+ const option = document.createElement('option');
75
+ option.value = '';
76
+ option.textContent = 'No agents available';
77
+ DOM.agentSelect.appendChild(option);
78
+ return;
79
+ }
80
+
81
+ AppState.agents.forEach(agent => {
82
+ const option = document.createElement('option');
83
+ option.value = agent.key;
84
+ option.textContent = agent.name;
85
+ if (agent.key === AppState.currentAgentKey) {
86
+ option.selected = true;
87
+ }
88
+ DOM.agentSelect.appendChild(option);
89
+ });
90
+ }
91
+
92
+ // Update agent info display
93
+ function updateAgentInfo() {
94
+ if (!DOM.agentInfo) return;
95
+
96
+ const agent = AppState.agents.find(a => a.key === AppState.currentAgentKey);
97
+ if (agent) {
98
+ DOM.agentInfo.textContent = agent.description || '';
99
+ } else {
100
+ DOM.agentInfo.textContent = '';
101
+ }
102
+ }
103
+
104
+ // Setup all event listeners
105
+ function setupEventListeners() {
106
+ // Agent selection change
107
+ if (DOM.agentSelect) {
108
+ DOM.agentSelect.addEventListener('change', handleAgentChange);
109
+ }
110
+
111
+ // Send button click
112
+ if (DOM.sendButton) {
113
+ DOM.sendButton.addEventListener('click', sendMessage);
114
+ }
115
+
116
+ // Enter key to send (Shift+Enter for new line)
117
+ if (DOM.chatInput) {
118
+ DOM.chatInput.addEventListener('keydown', handleKeyDown);
119
+ DOM.chatInput.addEventListener('input', handleInputResize);
120
+ }
121
+ }
122
+
123
+ // Handle agent selection change
124
+ async function handleAgentChange(event) {
125
+ const newAgentKey = event.target.value;
126
+
127
+ if (!newAgentKey || newAgentKey === AppState.currentAgentKey) {
128
+ return;
129
+ }
130
+
131
+ try {
132
+ const response = await fetch('/api/switch', {
133
+ method: 'POST',
134
+ headers: {
135
+ 'Content-Type': 'application/json'
136
+ },
137
+ body: JSON.stringify({ agent_key: newAgentKey })
138
+ });
139
+
140
+ if (response.ok) {
141
+ const data = await response.json();
142
+ AppState.currentAgentKey = newAgentKey;
143
+ updateAgentInfo();
144
+
145
+ const agentName = AppState.agents.find(a => a.key === newAgentKey)?.name || newAgentKey;
146
+ addMessage('agent', 'System', `Switched to ${agentName}`);
147
+ } else {
148
+ const errorData = await response.json();
149
+ addMessage('error', 'System', `Failed to switch agent: ${errorData.error || 'Unknown error'}`);
150
+ // Revert selection
151
+ DOM.agentSelect.value = AppState.currentAgentKey;
152
+ }
153
+ } catch (error) {
154
+ console.error('Failed to switch agent:', error);
155
+ addMessage('error', 'System', 'Failed to switch agent. Please try again.');
156
+ DOM.agentSelect.value = AppState.currentAgentKey;
157
+ }
158
+ }
159
+
160
+ // Handle keyboard input
161
+ function handleKeyDown(event) {
162
+ if (event.key === 'Enter' && !event.shiftKey) {
163
+ event.preventDefault();
164
+ sendMessage();
165
+ }
166
+ }
167
+
168
+ // Auto-resize textarea
169
+ function handleInputResize() {
170
+ if (!DOM.chatInput) return;
171
+
172
+ DOM.chatInput.style.height = 'auto';
173
+ const newHeight = Math.min(DOM.chatInput.scrollHeight, 150);
174
+ DOM.chatInput.style.height = newHeight + 'px';
175
+ }
176
+
177
+ // Add message to chat display
178
+ function addMessage(type, sender, content) {
179
+ if (!DOM.chatMessages) return;
180
+
181
+ // Remove empty state if present
182
+ const emptyState = DOM.chatMessages.querySelector('.empty-state');
183
+ if (emptyState) {
184
+ emptyState.remove();
185
+ }
186
+
187
+ // Create message container
188
+ const messageDiv = document.createElement('div');
189
+ messageDiv.className = `message ${type}`;
190
+
191
+ // Create header
192
+ const headerDiv = document.createElement('div');
193
+ headerDiv.className = 'message-header';
194
+ headerDiv.textContent = sender;
195
+
196
+ // Create content
197
+ const contentDiv = document.createElement('div');
198
+ contentDiv.className = 'message-content';
199
+ contentDiv.textContent = content;
200
+
201
+ // Assemble message
202
+ messageDiv.appendChild(headerDiv);
203
+ messageDiv.appendChild(contentDiv);
204
+ DOM.chatMessages.appendChild(messageDiv);
205
+
206
+ // Scroll to bottom with smooth behavior
207
+ DOM.chatMessages.scrollTo({
208
+ top: DOM.chatMessages.scrollHeight,
209
+ behavior: 'smooth'
210
+ });
211
+ }
212
+
213
+ // Send message to agent
214
+ async function sendMessage() {
215
+ if (!DOM.chatInput || !DOM.sendButton) return;
216
+
217
+ const message = DOM.chatInput.value.trim();
218
+
219
+ // Validation
220
+ if (!message || AppState.isLoading || !AppState.currentAgentKey) {
221
+ return;
222
+ }
223
+
224
+ // Add user message to chat
225
+ addMessage('user', 'You', message);
226
+
227
+ // Clear and reset input
228
+ DOM.chatInput.value = '';
229
+ DOM.chatInput.style.height = 'auto';
230
+
231
+ // Update UI state
232
+ setLoadingState(true);
233
+
234
+ try {
235
+ const response = await fetch('/api/chat', {
236
+ method: 'POST',
237
+ headers: {
238
+ 'Content-Type': 'application/json'
239
+ },
240
+ body: JSON.stringify({
241
+ message: message,
242
+ agent_key: AppState.currentAgentKey
243
+ })
244
+ });
245
+
246
+ const data = await response.json();
247
+
248
+ if (response.ok && data.response) {
249
+ const agentName = data.agent_name || 'Agent';
250
+ addMessage('agent', agentName, data.response);
251
+ } else {
252
+ addMessage('error', 'Error', data.error || 'Failed to get response from agent');
253
+ }
254
+ } catch (error) {
255
+ console.error('Failed to send message:', error);
256
+ addMessage('error', 'Error', 'Failed to send message. Please try again.');
257
+ } finally {
258
+ setLoadingState(false);
259
+ DOM.chatInput.focus();
260
+ }
261
+ }
262
+
263
+ // Set loading state
264
+ function setLoadingState(loading) {
265
+ AppState.isLoading = loading;
266
+
267
+ if (!DOM.chatInput || !DOM.sendButton) return;
268
+
269
+ DOM.chatInput.disabled = loading;
270
+ DOM.sendButton.disabled = loading;
271
+
272
+ if (loading) {
273
+ DOM.sendButton.innerHTML = '<span class="loading"></span>Sending...';
274
+ } else {
275
+ DOM.sendButton.textContent = 'Send';
276
+ }
277
+ }
278
+
279
+ // Initialize on page load
280
+ if (document.readyState === 'loading') {
281
+ document.addEventListener('DOMContentLoaded', init);
282
+ } else {
283
+ init();
284
+ }
@@ -0,0 +1,42 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>CRCA Web UI</title>
7
+ <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
8
+ </head>
9
+ <body>
10
+ <div class="header">
11
+ <h1>CRCA Web UI</h1>
12
+ <p>Causal Reasoning with Counterfactual Analysis</p>
13
+ </div>
14
+
15
+ <div class="controls">
16
+ <div class="agent-selector">
17
+ <label for="agentSelect">Agent:</label>
18
+ <select id="agentSelect">
19
+ <option value="">Loading...</option>
20
+ </select>
21
+ </div>
22
+ <div class="agent-info" id="agentInfo"></div>
23
+ </div>
24
+
25
+ <div class="chat-container">
26
+ <div class="chat-messages" id="chatMessages">
27
+ <div class="empty-state">Start a conversation by typing a message below.</div>
28
+ </div>
29
+ <div class="chat-input-container">
30
+ <textarea
31
+ id="chatInput"
32
+ class="chat-input"
33
+ placeholder="Type your message here..."
34
+ rows="1"
35
+ ></textarea>
36
+ <button id="sendButton" class="send-button">Send</button>
37
+ </div>
38
+ </div>
39
+
40
+ <script src="{{ url_for('static', filename='js/main.js') }}"></script>
41
+ </body>
42
+ </html>
tests/test_crca_excel.py DELETED
@@ -1,166 +0,0 @@
1
- """
2
- Integration tests for CRCA Excel TUI System.
3
-
4
- Tests full planning workflow, SCM bridge, and TUI interactions.
5
- """
6
-
7
- import pytest
8
- from crca_excel.core.tables import TableManager
9
- from crca_excel.core.deps import DependencyGraph
10
- from crca_excel.core.eval import EvaluationEngine
11
- from crca_excel.core.scm import SCMBridge
12
- from crca_excel.core.objective import ObjectiveFunction, ConstraintChecker
13
- from crca_excel.core.planner import Planner
14
- from crca_excel.core.events import EventDispatcher
15
- from crca_excel.core.standard_tables import initialize_standard_tables
16
- from crca_excel.core.formulas import (
17
- formula_agent_load,
18
- formula_task_progress,
19
- formula_objective
20
- )
21
-
22
-
23
- def test_table_creation():
24
- """Test table creation and basic operations."""
25
- tables = TableManager()
26
- initialize_standard_tables(tables)
27
-
28
- assert tables.has_table("CONFIG")
29
- assert tables.has_table("AGENTS")
30
- assert tables.has_table("TASKS")
31
-
32
- # Add an agent
33
- agents_table = tables.get_table("AGENTS")
34
- agents_table.add_row("agent1", {"agent_id": "agent1", "max_tasks": 5})
35
-
36
- assert agents_table.has_row("agent1")
37
- assert agents_table.get_cell("agent1", "max_tasks") == 5
38
-
39
-
40
- def test_dependency_graph():
41
- """Test dependency graph operations."""
42
- graph = DependencyGraph()
43
-
44
- cell1 = ("TASKS", "task1", "difficulty")
45
- cell2 = ("AGENTS", "agent1", "load")
46
-
47
- graph.add_dependency(cell1, cell2)
48
-
49
- deps = graph.get_dependencies(cell2)
50
- assert cell1 in deps
51
-
52
- dependents = graph.get_dependents(cell1)
53
- assert cell2 in dependents
54
-
55
-
56
- def test_scm_bridge():
57
- """Test SCM bridge do-operator."""
58
- tables = TableManager()
59
- initialize_standard_tables(tables)
60
-
61
- graph = DependencyGraph()
62
- eval_engine = EvaluationEngine(tables, graph)
63
- scm_bridge = SCMBridge(tables, eval_engine)
64
-
65
- # Add agent and task
66
- agents_table = tables.get_table("AGENTS")
67
- agents_table.add_row("agent1", {"agent_id": "agent1", "max_tasks": 5})
68
-
69
- tasks_table = tables.get_table("TASKS")
70
- tasks_table.add_row("task1", {"task_id": "task1", "difficulty": 2.0})
71
-
72
- # Apply intervention
73
- intervention = {
74
- ("TASKS", "task1", "assigned_agent_id"): "agent1"
75
- }
76
-
77
- snapshot = scm_bridge.do_intervention(intervention)
78
- assert "TASKS" in snapshot
79
-
80
-
81
- def test_planner():
82
- """Test planner candidate generation and evaluation."""
83
- tables = TableManager()
84
- initialize_standard_tables(tables)
85
-
86
- graph = DependencyGraph()
87
- eval_engine = EvaluationEngine(tables, graph)
88
- scm_bridge = SCMBridge(tables, eval_engine)
89
-
90
- objective = ObjectiveFunction("value")
91
- constraint_checker = ConstraintChecker()
92
-
93
- planner = Planner(tables, scm_bridge, objective, constraint_checker)
94
-
95
- # Add test data
96
- agents_table = tables.get_table("AGENTS")
97
- agents_table.add_row("agent1", {"agent_id": "agent1", "max_tasks": 5})
98
-
99
- tasks_table = tables.get_table("TASKS")
100
- tasks_table.add_row("task1", {"task_id": "task1", "difficulty": 2.0, "value": 10.0})
101
-
102
- # Generate candidates
103
- intervention_vars = [("TASKS", "task1", "assigned_agent_id")]
104
- candidates = planner.generate_candidates(intervention_vars, n_candidates=5)
105
-
106
- assert len(candidates) > 0
107
- assert all(isinstance(c, dict) for c in candidates)
108
-
109
-
110
- def test_event_dispatcher():
111
- """Test event dispatcher."""
112
- tables = TableManager()
113
- initialize_standard_tables(tables)
114
-
115
- graph = DependencyGraph()
116
- eval_engine = EvaluationEngine(tables, graph)
117
- dispatcher = EventDispatcher(tables, eval_engine, graph)
118
-
119
- # Add agent
120
- agents_table = tables.get_table("AGENTS")
121
- agents_table.add_row("agent1", {"agent_id": "agent1"})
122
-
123
- # Dispatch edit event
124
- from crca_excel.core.events import EditCellEvent
125
- event = EditCellEvent(
126
- table_name="AGENTS",
127
- row_key="agent1",
128
- column_name="max_tasks",
129
- value=10
130
- )
131
-
132
- dispatcher.dispatch(event)
133
-
134
- assert agents_table.get_cell("agent1", "max_tasks") == 10
135
-
136
-
137
- def test_formulas():
138
- """Test formula evaluation."""
139
- tables = TableManager()
140
- initialize_standard_tables(tables)
141
-
142
- # Setup data
143
- agents_table = tables.get_table("AGENTS")
144
- agents_table.add_row("agent1", {"agent_id": "agent1", "max_tasks": 5})
145
-
146
- tasks_table = tables.get_table("TASKS")
147
- tasks_table.add_row("task1", {
148
- "task_id": "task1",
149
- "difficulty": 2.0,
150
- "assigned_agent_id": "agent1"
151
- })
152
-
153
- # Test agent load formula
154
- agent_row = agents_table.get_row_data("agent1")
155
- all_tables = {
156
- "AGENTS": {"agent1": agent_row},
157
- "TASKS": {"task1": tasks_table.get_row_data("task1")}
158
- }
159
-
160
- load = formula_agent_load(agent_row, all_tables, {})
161
- assert load == 2.0 # Sum of assigned task difficulties
162
-
163
-
164
- if __name__ == "__main__":
165
- pytest.main([__file__, "-v"])
166
-