monkeybrain-runtime 1.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.
- monkeybrain_runtime-1.0.0.dist-info/METADATA +76 -0
- monkeybrain_runtime-1.0.0.dist-info/RECORD +838 -0
- monkeybrain_runtime-1.0.0.dist-info/WHEEL +5 -0
- monkeybrain_runtime-1.0.0.dist-info/entry_points.txt +3 -0
- monkeybrain_runtime-1.0.0.dist-info/top_level.txt +2 -0
- services/__init__.py +8 -0
- services/agentos/__init__.py +0 -0
- services/agentos/main.py +1 -0
- services/assets/helpers/__init__.py +12 -0
- services/assets/helpers/device.py +59 -0
- services/assets/helpers/equipment.py +179 -0
- services/assets/helpers/instruments.py +72 -0
- services/assets/helpers/machines.py +183 -0
- services/assets/helpers/materials.py +76 -0
- services/assets/helpers/parts.py +116 -0
- services/assets/helpers/plc.py +134 -0
- services/assets/helpers/tags.py +108 -0
- services/assets/helpers/tools.py +101 -0
- services/assets/main.py +75 -0
- services/assets/models/__init__.py +12 -0
- services/assets/models/device.py +79 -0
- services/assets/models/equipment.py +222 -0
- services/assets/models/instruments.py +85 -0
- services/assets/models/machines.py +230 -0
- services/assets/models/material.py +266 -0
- services/assets/models/parts.py +96 -0
- services/assets/models/plc.py +264 -0
- services/assets/models/tags.py +76 -0
- services/assets/models/tools.py +179 -0
- services/assets/routers/__init__.py +12 -0
- services/assets/routers/classes.py +65 -0
- services/assets/routers/device.py +86 -0
- services/assets/routers/equipment.py +145 -0
- services/assets/routers/families.py +61 -0
- services/assets/routers/instruments.py +70 -0
- services/assets/routers/machines.py +136 -0
- services/assets/routers/materials.py +105 -0
- services/assets/routers/parts.py +130 -0
- services/assets/routers/plc.py +94 -0
- services/assets/routers/subclasses.py +68 -0
- services/assets/routers/tags.py +138 -0
- services/assets/routers/tools.py +113 -0
- services/auth/helpers/__init__.py +13 -0
- services/auth/helpers/approval_decisions.py +261 -0
- services/auth/helpers/audit_elasticsearch_sync.py +350 -0
- services/auth/helpers/departments.py +53 -0
- services/auth/helpers/graph_store.py +848 -0
- services/auth/helpers/influx_store.py +280 -0
- services/auth/helpers/me.py +33 -0
- services/auth/helpers/nats_consumer.py +618 -0
- services/auth/helpers/nats_store.py +242 -0
- services/auth/helpers/permissions.py +62 -0
- services/auth/helpers/roles.py +87 -0
- services/auth/helpers/store.py +54 -0
- services/auth/helpers/team_members.py +155 -0
- services/auth/helpers/teams.py +87 -0
- services/auth/helpers/tokens.py +71 -0
- services/auth/helpers/users.py +119 -0
- services/auth/helpers/websocket_broadcast.py +41 -0
- services/auth/main.py +88 -0
- services/auth/models/__init__.py +12 -0
- services/auth/models/departments.py +55 -0
- services/auth/models/login.py +20 -0
- services/auth/models/permissions.py +61 -0
- services/auth/models/roles.py +53 -0
- services/auth/models/session.py +26 -0
- services/auth/models/teamMembers.py +59 -0
- services/auth/models/teams.py +56 -0
- services/auth/models/users.py +77 -0
- services/auth/routers/__init__.py +14 -0
- services/auth/routers/auth.py +3839 -0
- services/auth/routers/departments.py +84 -0
- services/auth/routers/integration_config.py +703 -0
- services/auth/routers/me.py +28 -0
- services/auth/routers/permissions.py +96 -0
- services/auth/routers/roles.py +139 -0
- services/auth/routers/session.py +224 -0
- services/auth/routers/team_members.py +152 -0
- services/auth/routers/teams.py +112 -0
- services/auth/routers/users.py +131 -0
- services/auth/routers/websocket_events.py +247 -0
- services/batch_execution/__init__.py +19 -0
- services/batch_execution/pipeline_triggers/__init__.py +53 -0
- services/batch_execution/pipeline_triggers/pipeline_parameter_adjuster.py +440 -0
- services/batch_execution/pipeline_triggers/pipeline_trigger_engine.py +445 -0
- services/batch_execution/pipeline_triggers/re_evaluation_queue.py +341 -0
- services/batch_execution/pipeline_triggers/test_phase3_implementation.py +553 -0
- services/batch_execution/pipeline_triggers/workflow_reevaluator.py +367 -0
- services/batch_execution/smoke_test_e2e_feedback_loop.py +704 -0
- services/batch_execution/workflow_executor.py +478 -0
- services/changeover/helpers/__init__.py +11 -0
- services/changeover/helpers/changeover.py +87 -0
- services/changeover/helpers/changeover_common.py +55 -0
- services/changeover/helpers/changeover_events.py +66 -0
- services/changeover/helpers/changeover_kpis.py +33 -0
- services/changeover/helpers/changeover_matrix.py +60 -0
- services/changeover/helpers/changeover_procedures.py +164 -0
- services/changeover/helpers/changeover_windows.py +96 -0
- services/changeover/main.py +52 -0
- services/changeover/models/__init__.py +11 -0
- services/changeover/models/changeover.py +73 -0
- services/changeover/models/changeover_events.py +142 -0
- services/changeover/models/changeover_kpis.py +75 -0
- services/changeover/models/changeover_matrix.py +63 -0
- services/changeover/models/changeover_procedures.py +108 -0
- services/changeover/models/changeover_tasks.py +87 -0
- services/changeover/models/changeover_windows.py +72 -0
- services/changeover/routers/__init__.py +9 -0
- services/changeover/routers/changeover_events.py +127 -0
- services/changeover/routers/changeover_kpis.py +80 -0
- services/changeover/routers/changeover_matrix.py +80 -0
- services/changeover/routers/changeover_procedures.py +118 -0
- services/changeover/routers/changeover_windows.py +98 -0
- services/common/__init__.py +2 -0
- services/common/approval_chains.py +648 -0
- services/common/auth.py +56 -0
- services/common/cdc.py +52 -0
- services/common/compat.py +217 -0
- services/common/compliance.py +562 -0
- services/common/config.py +134 -0
- services/common/cors.py +17 -0
- services/common/data_transformation.py +195 -0
- services/common/db.py +577 -0
- services/common/embeddings.py +97 -0
- services/common/event_reducers.py +194 -0
- services/common/event_types.py +51 -0
- services/common/integration_ingestion.py +169 -0
- services/common/logging.py +204 -0
- services/common/models/__init__.py +2 -0
- services/common/models/databricks.py +25 -0
- services/common/models/enums.py +64 -0
- services/common/module_control.py +422 -0
- services/common/mongo_cdc_watcher.py +106 -0
- services/common/n8n_auth.py +22 -0
- services/common/neo4j_mirror.py +1087 -0
- services/common/ontology_registry.py +110 -0
- services/common/reasoning_traces.py +52 -0
- services/common/supply_chain_cdc.py +555 -0
- services/common/tracing.py +159 -0
- services/common/utils.py +30 -0
- services/customers/helpers/__init__.py +8 -0
- services/customers/helpers/customer_details.py +64 -0
- services/customers/helpers/customer_metadata.py +64 -0
- services/customers/helpers/customer_order_metrics.py +67 -0
- services/customers/helpers/customer_payment_data.py +67 -0
- services/customers/main.py +50 -0
- services/customers/models/__init__.py +8 -0
- services/customers/models/customer_details.py +42 -0
- services/customers/models/customer_metadata.py +97 -0
- services/customers/models/customer_order_metrics.py +86 -0
- services/customers/models/customer_payment_data.py +60 -0
- services/customers/routers/__init__.py +8 -0
- services/customers/routers/customer_details.py +88 -0
- services/customers/routers/customer_metadata.py +88 -0
- services/customers/routers/customer_order_metrics.py +88 -0
- services/customers/routers/customer_payment_data.py +88 -0
- services/documents/__init__.py +1 -0
- services/documents/helpers/__init__.py +6 -0
- services/documents/helpers/document_metadata.py +569 -0
- services/documents/helpers/document_workflows.py +215 -0
- services/documents/helpers/report_templates.py +113 -0
- services/documents/main.py +49 -0
- services/documents/models/__init__.py +6 -0
- services/documents/models/document_metadata.py +215 -0
- services/documents/models/document_workflows.py +136 -0
- services/documents/models/report_templates.py +132 -0
- services/documents/routers/__init__.py +6 -0
- services/documents/routers/document_metadata.py +654 -0
- services/documents/routers/document_workflows.py +146 -0
- services/documents/routers/report_templates.py +86 -0
- services/events/helpers/__init__.py +5 -0
- services/events/helpers/events.py +394 -0
- services/events/main.py +40 -0
- services/events/models/__init__.py +5 -0
- services/events/models/events.py +50 -0
- services/events/routers/__init__.py +6 -0
- services/events/routers/count_events.py +109 -0
- services/events/routers/events.py +75 -0
- services/events/seed_events.py +196 -0
- services/facilities/helpers/__init__.py +8 -0
- services/facilities/helpers/lines.py +74 -0
- services/facilities/helpers/locations.py +231 -0
- services/facilities/helpers/plants.py +59 -0
- services/facilities/helpers/stages.py +110 -0
- services/facilities/helpers/workstation.py +213 -0
- services/facilities/main.py +60 -0
- services/facilities/models/__init__.py +10 -0
- services/facilities/models/industrialLine.py +72 -0
- services/facilities/models/industrialPlant.py +164 -0
- services/facilities/models/locations.py +74 -0
- services/facilities/models/stages.py +92 -0
- services/facilities/models/worker.py +73 -0
- services/facilities/models/workstation.py +117 -0
- services/facilities/models/workstation_live_state.py +59 -0
- services/facilities/routers/__init__.py +8 -0
- services/facilities/routers/bays.py +81 -0
- services/facilities/routers/buildings.py +92 -0
- services/facilities/routers/floors.py +81 -0
- services/facilities/routers/lines.py +154 -0
- services/facilities/routers/locations.py +208 -0
- services/facilities/routers/plant.py +203 -0
- services/facilities/routers/rooms.py +81 -0
- services/facilities/routers/stages.py +152 -0
- services/facilities/routers/workstation.py +173 -0
- services/file/backup.py +71 -0
- services/file/main.py +45 -0
- services/file/recieve.py +54 -0
- services/file/send.py +55 -0
- services/file/src/core/config.py +90 -0
- services/file/src/core/keycloak.py +152 -0
- services/file/src/core/logging_config.py +9 -0
- services/file/src/core/security.py +33 -0
- services/file/src/helpers/cad_conversion.py +331 -0
- services/file/src/helpers/helpers.py +825 -0
- services/file/src/routes/cad_conversion.py +26 -0
- services/file/src/routes/files.py +136 -0
- services/file/src/routes/presigned.py +154 -0
- services/file/src/services/websocket.py +293 -0
- services/floor_layout/helpers/__init__.py +8 -0
- services/floor_layout/helpers/bays.py +92 -0
- services/floor_layout/helpers/buildings.py +54 -0
- services/floor_layout/helpers/floors.py +65 -0
- services/floor_layout/helpers/rooms.py +76 -0
- services/floor_layout/main.py +52 -0
- services/floor_layout/models/__init__.py +8 -0
- services/floor_layout/models/bays.py +65 -0
- services/floor_layout/models/buildings.py +52 -0
- services/floor_layout/models/floors.py +45 -0
- services/floor_layout/models/rooms.py +61 -0
- services/floor_layout/routers/__init__.py +9 -0
- services/floor_layout/routers/bays.py +143 -0
- services/floor_layout/routers/buildings.py +116 -0
- services/floor_layout/routers/floors.py +89 -0
- services/floor_layout/routers/locations.py +80 -0
- services/floor_layout/routers/rooms.py +134 -0
- services/inventory/helpers/__init__.py +13 -0
- services/inventory/helpers/cycle_counts.py +124 -0
- services/inventory/helpers/inventory_allocations.py +134 -0
- services/inventory/helpers/inventory_item_counts.py +114 -0
- services/inventory/helpers/inventory_item_quantities.py +114 -0
- services/inventory/helpers/inventory_items.py +103 -0
- services/inventory/helpers/inventory_stage_outputs.py +134 -0
- services/inventory/helpers/inventory_transactions.py +112 -0
- services/inventory/helpers/stock_adjustment_requests.py +101 -0
- services/inventory/helpers/warehouse_cycle_counts.py +133 -0
- services/inventory/helpers/warehouse_locations.py +213 -0
- services/inventory/helpers/warehouse_regulated_records.py +123 -0
- services/inventory/main.py +62 -0
- services/inventory/models/__init__.py +17 -0
- services/inventory/models/cycle_counts.py +99 -0
- services/inventory/models/inventory_allocations.py +121 -0
- services/inventory/models/inventory_common.py +65 -0
- services/inventory/models/inventory_enums.py +21 -0
- services/inventory/models/inventory_item_count.py +65 -0
- services/inventory/models/inventory_item_quantity.py +82 -0
- services/inventory/models/inventory_items.py +168 -0
- services/inventory/models/inventory_responses.py +44 -0
- services/inventory/models/inventory_stage_outputs.py +96 -0
- services/inventory/models/inventory_state.py +15 -0
- services/inventory/models/inventory_transactions.py +80 -0
- services/inventory/models/stock_adjustment_requests.py +109 -0
- services/inventory/models/warehouse_cycle_counts.py +119 -0
- services/inventory/models/warehouse_location_models.py +708 -0
- services/inventory/models/warehouse_regulated_records.py +358 -0
- services/inventory/routers/__init__.py +13 -0
- services/inventory/routers/cycle_counts.py +106 -0
- services/inventory/routers/inventory_allocations.py +125 -0
- services/inventory/routers/inventory_item_counts.py +105 -0
- services/inventory/routers/inventory_item_quantities.py +105 -0
- services/inventory/routers/inventory_items.py +109 -0
- services/inventory/routers/inventory_stage_outputs.py +122 -0
- services/inventory/routers/inventory_transactions.py +96 -0
- services/inventory/routers/stock_adjustment_requests.py +124 -0
- services/inventory/routers/warehouse_cycle_counts.py +124 -0
- services/inventory/routers/warehouse_locations.py +426 -0
- services/inventory/routers/warehouse_regulated_records.py +273 -0
- services/iot/helpers/__init__.py +8 -0
- services/iot/helpers/ble_device.py +87 -0
- services/iot/helpers/mqtt_bridge.py +115 -0
- services/iot/helpers/sensor_readings.py +63 -0
- services/iot/helpers/sensors.py +77 -0
- services/iot/helpers/servers.py +72 -0
- services/iot/helpers/uwb_device.py +95 -0
- services/iot/main.py +53 -0
- services/iot/models/__init__.py +8 -0
- services/iot/models/ble_device.py +118 -0
- services/iot/models/sensors.py +256 -0
- services/iot/models/servers.py +206 -0
- services/iot/models/uwb_device.py +106 -0
- services/iot/routers/__init__.py +8 -0
- services/iot/routers/ble_device.py +110 -0
- services/iot/routers/sensors.py +144 -0
- services/iot/routers/servers.py +141 -0
- services/iot/routers/uwb_device.py +148 -0
- services/module_control/__init__.py +1 -0
- services/module_control/helpers/__init__.py +1 -0
- services/module_control/helpers/integration_config.py +243 -0
- services/module_control/helpers/security.py +104 -0
- services/module_control/main.py +44 -0
- services/module_control/models/__init__.py +1 -0
- services/module_control/models/module_control.py +65 -0
- services/module_control/routers/__init__.py +1 -0
- services/module_control/routers/module_control.py +219 -0
- services/orders/helpers/__init__.py +11 -0
- services/orders/helpers/invoices.py +123 -0
- services/orders/helpers/order_customer_metrics.py +61 -0
- services/orders/helpers/order_details.py +71 -0
- services/orders/helpers/order_metadata.py +61 -0
- services/orders/helpers/order_payment_metadata.py +74 -0
- services/orders/helpers/orders.py +119 -0
- services/orders/helpers/sales_orders.py +136 -0
- services/orders/main.py +56 -0
- services/orders/models/__init__.py +11 -0
- services/orders/models/invoices.py +415 -0
- services/orders/models/order_customer_metrics.py +78 -0
- services/orders/models/order_details.py +46 -0
- services/orders/models/order_metadata.py +60 -0
- services/orders/models/order_payment_metadata.py +63 -0
- services/orders/models/orders.py +64 -0
- services/orders/models/sales_orders.py +130 -0
- services/orders/routers/__init__.py +11 -0
- services/orders/routers/invoices.py +111 -0
- services/orders/routers/order_customer_metrics.py +87 -0
- services/orders/routers/order_details.py +87 -0
- services/orders/routers/order_metadata.py +87 -0
- services/orders/routers/order_payment_metadata.py +87 -0
- services/orders/routers/orders.py +74 -0
- services/orders/routers/sales_orders.py +111 -0
- services/pm/helpers/__init__.py +14 -0
- services/pm/helpers/calendar_bookings.py +114 -0
- services/pm/helpers/calibration_point.py +110 -0
- services/pm/helpers/calibrations.py +196 -0
- services/pm/helpers/checklists.py +318 -0
- services/pm/helpers/cleaning.py +333 -0
- services/pm/helpers/downtime.py +376 -0
- services/pm/helpers/kanban_boards.py +186 -0
- services/pm/helpers/maintainance.py +177 -0
- services/pm/helpers/sop.py +1155 -0
- services/pm/helpers/sop_cdc.py +324 -0
- services/pm/helpers/weekly_schedules.py +79 -0
- services/pm/main.py +62 -0
- services/pm/models/__init__.py +14 -0
- services/pm/models/calendar_booking.py +82 -0
- services/pm/models/calibration_point.py +44 -0
- services/pm/models/calibrations.py +167 -0
- services/pm/models/checklists.py +117 -0
- services/pm/models/cleaning.py +203 -0
- services/pm/models/downtime.py +109 -0
- services/pm/models/kanban_board.py +178 -0
- services/pm/models/maintainanceLog.py +148 -0
- services/pm/models/sop.py +152 -0
- services/pm/models/weekly_schedule.py +91 -0
- services/pm/routers/__init__.py +14 -0
- services/pm/routers/calendar_bookings.py +143 -0
- services/pm/routers/calibration_point.py +94 -0
- services/pm/routers/calibrations.py +232 -0
- services/pm/routers/checklists.py +188 -0
- services/pm/routers/cleaning.py +127 -0
- services/pm/routers/downtime.py +143 -0
- services/pm/routers/kanban_boards.py +283 -0
- services/pm/routers/maintainance.py +241 -0
- services/pm/routers/sop.py +437 -0
- services/pm/routers/weekly_schedules.py +108 -0
- services/process_definitions/helpers/__init__.py +11 -0
- services/process_definitions/helpers/cpp_cqa_registry.py +120 -0
- services/process_definitions/helpers/mbmr_templates.py +107 -0
- services/process_definitions/helpers/packing_instructions.py +113 -0
- services/process_definitions/helpers/process_constraints.py +495 -0
- services/process_definitions/helpers/process_corrections.py +279 -0
- services/process_definitions/helpers/process_definition.py +996 -0
- services/process_definitions/helpers/process_node_catalog.py +786 -0
- services/process_definitions/helpers/process_post_checks.py +441 -0
- services/process_definitions/helpers/process_pre_checks.py +351 -0
- services/process_definitions/helpers/process_steps.py +220 -0
- services/process_definitions/main.py +71 -0
- services/process_definitions/models/__init__.py +13 -0
- services/process_definitions/models/cpp_cqa_registry.py +145 -0
- services/process_definitions/models/gxp_change_controls.py +38 -0
- services/process_definitions/models/gxp_risk_assessments.py +30 -0
- services/process_definitions/models/gxp_validation_evidence.py +33 -0
- services/process_definitions/models/mbmr_templates.py +173 -0
- services/process_definitions/models/packing_instructions.py +176 -0
- services/process_definitions/models/process_constraints.py +159 -0
- services/process_definitions/models/process_corrections.py +118 -0
- services/process_definitions/models/process_definition.py +685 -0
- services/process_definitions/models/process_definition_common.py +48 -0
- services/process_definitions/models/process_node_catalog.py +25 -0
- services/process_definitions/models/process_post_checks.py +171 -0
- services/process_definitions/models/process_pre_checks.py +168 -0
- services/process_definitions/models/process_steps.py +170 -0
- services/process_definitions/node_services/__init__.py +8 -0
- services/process_definitions/node_services/common.py +95 -0
- services/process_definitions/node_services/executor.py +499 -0
- services/process_definitions/node_services/flow_simulator.py +733 -0
- services/process_definitions/node_services/functions.py +193 -0
- services/process_definitions/node_services/messaging.py +44 -0
- services/process_definitions/node_services/models.py +221 -0
- services/process_definitions/node_services/network.py +161 -0
- services/process_definitions/node_services/parsers.py +87 -0
- services/process_definitions/node_services/sequence.py +95 -0
- services/process_definitions/node_services/storage.py +50 -0
- services/process_definitions/node_services/webhooks.py +52 -0
- services/process_definitions/routers/__init__.py +10 -0
- services/process_definitions/routers/cpp_cqa_registry.py +86 -0
- services/process_definitions/routers/mbmr_templates.py +84 -0
- services/process_definitions/routers/packing_instructions.py +84 -0
- services/process_definitions/routers/process_constraints.py +564 -0
- services/process_definitions/routers/process_corrections.py +343 -0
- services/process_definitions/routers/process_definition.py +992 -0
- services/process_definitions/routers/process_post_checks.py +529 -0
- services/process_definitions/routers/process_pre_checks.py +435 -0
- services/process_definitions/routers/process_steps.py +274 -0
- services/procurement/helpers/__init__.py +9 -0
- services/procurement/helpers/goods_receipts.py +240 -0
- services/procurement/helpers/purchase_order_shipping_information.py +85 -0
- services/procurement/helpers/purchase_orders.py +68 -0
- services/procurement/helpers/quality_control.py +235 -0
- services/procurement/helpers/sampling.py +404 -0
- services/procurement/main.py +52 -0
- services/procurement/models/__init__.py +9 -0
- services/procurement/models/goods_receipts.py +165 -0
- services/procurement/models/purchase_orders.py +54 -0
- services/procurement/models/quality_control.py +464 -0
- services/procurement/models/reinspection_records.py +28 -0
- services/procurement/models/sampling.py +262 -0
- services/procurement/models/shipping_information.py +51 -0
- services/procurement/routers/__init__.py +9 -0
- services/procurement/routers/goods_receipts.py +201 -0
- services/procurement/routers/purchase_orders.py +106 -0
- services/procurement/routers/quality_control.py +386 -0
- services/procurement/routers/sampling.py +296 -0
- services/procurement/routers/shipping_information.py +97 -0
- services/production/__init__.py +1 -0
- services/production/agents/__init__.py +5 -0
- services/production/agents/batch_planning_agent.py +815 -0
- services/production/models/__init__.py +25 -0
- services/production/models/batch.py +253 -0
- services/products/helpers/__init__.py +10 -0
- services/products/helpers/boms.py +100 -0
- services/products/helpers/drug_research.py +644 -0
- services/products/helpers/product_component.py +168 -0
- services/products/helpers/product_inventory.py +221 -0
- services/products/helpers/product_pricing.py +123 -0
- services/products/helpers/product_utils.py +32 -0
- services/products/helpers/products.py +81 -0
- services/products/main.py +59 -0
- services/products/models/__init__.py +9 -0
- services/products/models/drug_research.py +138 -0
- services/products/models/product_common.py +60 -0
- services/products/models/product_component.py +1028 -0
- services/products/models/product_inventory.py +118 -0
- services/products/models/product_pricing.py +73 -0
- services/products/models/products.py +151 -0
- services/products/routers/__init__.py +9 -0
- services/products/routers/boms.py +116 -0
- services/products/routers/drug_research.py +115 -0
- services/products/routers/product_components.py +123 -0
- services/products/routers/product_inventory.py +185 -0
- services/products/routers/product_pricing.py +136 -0
- services/products/routers/products.py +165 -0
- services/replenishment/__init__.py +1 -0
- services/replenishment/main.py +46 -0
- services/replenishment/routers/__init__.py +1 -0
- services/replenishment/routers/replenishment.py +20 -0
- services/shifts/helpers/__init__.py +7 -0
- services/shifts/helpers/shift_templates.py +124 -0
- services/shifts/helpers/shifts.py +79 -0
- services/shifts/helpers/timesheets.py +137 -0
- services/shifts/main.py +48 -0
- services/shifts/models/__init__.py +8 -0
- services/shifts/models/shift.py +62 -0
- services/shifts/models/shift_template.py +82 -0
- services/shifts/models/time_range.py +31 -0
- services/shifts/models/timesheet.py +196 -0
- services/shifts/routers/__init__.py +7 -0
- services/shifts/routers/shift_templates.py +97 -0
- services/shifts/routers/shifts.py +117 -0
- services/shifts/routers/timesheets.py +117 -0
- services/shipping/helpers/__init__.py +15 -0
- services/shipping/helpers/carrier.py +78 -0
- services/shipping/helpers/customs_declaration.py +104 -0
- services/shipping/helpers/delivery_note.py +99 -0
- services/shipping/helpers/package.py +95 -0
- services/shipping/helpers/pallet.py +85 -0
- services/shipping/helpers/route.py +93 -0
- services/shipping/helpers/shipping_information.py +82 -0
- services/shipping/helpers/shipping_provider_details.py +59 -0
- services/shipping/helpers/shipping_provider_metadata.py +59 -0
- services/shipping/helpers/vehicle.py +85 -0
- services/shipping/helpers/waybill.py +86 -0
- services/shipping/main.py +64 -0
- services/shipping/models/__init__.py +15 -0
- services/shipping/models/carrier.py +97 -0
- services/shipping/models/customs_declaration.py +138 -0
- services/shipping/models/delivery_note.py +163 -0
- services/shipping/models/package.py +152 -0
- services/shipping/models/pallet.py +137 -0
- services/shipping/models/route.py +120 -0
- services/shipping/models/shipping_information.py +55 -0
- services/shipping/models/shipping_provider_details.py +42 -0
- services/shipping/models/shipping_provider_metadata.py +54 -0
- services/shipping/models/vehicle.py +129 -0
- services/shipping/models/waybill.py +189 -0
- services/shipping/routers/__init__.py +15 -0
- services/shipping/routers/carrier.py +99 -0
- services/shipping/routers/customs_declaration.py +132 -0
- services/shipping/routers/delivery_note.py +150 -0
- services/shipping/routers/package.py +141 -0
- services/shipping/routers/pallet.py +108 -0
- services/shipping/routers/route.py +128 -0
- services/shipping/routers/shipping_information.py +97 -0
- services/shipping/routers/shipping_provider_details.py +80 -0
- services/shipping/routers/shipping_provider_metadata.py +80 -0
- services/shipping/routers/vehicle.py +117 -0
- services/shipping/routers/waybill.py +119 -0
- services/suppliers/helpers/__init__.py +13 -0
- services/suppliers/helpers/supplier_capabilities.py +58 -0
- services/suppliers/helpers/supplier_certifications.py +67 -0
- services/suppliers/helpers/supplier_details.py +58 -0
- services/suppliers/helpers/supplier_financials.py +58 -0
- services/suppliers/helpers/supplier_inventory.py +74 -0
- services/suppliers/helpers/supplier_locations.py +60 -0
- services/suppliers/helpers/supplier_pricing.py +69 -0
- services/suppliers/helpers/supplier_quality.py +69 -0
- services/suppliers/helpers/supplier_shipping.py +69 -0
- services/suppliers/main.py +60 -0
- services/suppliers/models/__init__.py +13 -0
- services/suppliers/models/supplier_capabilities.py +70 -0
- services/suppliers/models/supplier_certifications.py +64 -0
- services/suppliers/models/supplier_details.py +75 -0
- services/suppliers/models/supplier_financials.py +69 -0
- services/suppliers/models/supplier_inventory.py +76 -0
- services/suppliers/models/supplier_locations.py +70 -0
- services/suppliers/models/supplier_pricing.py +74 -0
- services/suppliers/models/supplier_quality.py +74 -0
- services/suppliers/models/supplier_shipping.py +76 -0
- services/suppliers/routers/__init__.py +13 -0
- services/suppliers/routers/supplier_capabilities.py +88 -0
- services/suppliers/routers/supplier_certifications.py +87 -0
- services/suppliers/routers/supplier_details.py +83 -0
- services/suppliers/routers/supplier_financials.py +83 -0
- services/suppliers/routers/supplier_inventory.py +105 -0
- services/suppliers/routers/supplier_locations.py +89 -0
- services/suppliers/routers/supplier_pricing.py +96 -0
- services/suppliers/routers/supplier_quality.py +96 -0
- services/suppliers/routers/supplier_shipping.py +96 -0
- services/supply_allocation/main.py +46 -0
- services/supply_allocation/routers/__init__.py +1 -0
- services/supply_allocation/routers/allocation.py +20 -0
- services/taxonomy/helpers/__init__.py +7 -0
- services/taxonomy/helpers/classes.py +48 -0
- services/taxonomy/helpers/family.py +53 -0
- services/taxonomy/helpers/subclass.py +58 -0
- services/taxonomy/main.py +48 -0
- services/taxonomy/models/__init__.py +7 -0
- services/taxonomy/models/classes.py +52 -0
- services/taxonomy/models/family.py +60 -0
- services/taxonomy/models/subclass.py +50 -0
- services/taxonomy/routers/__init__.py +7 -0
- services/taxonomy/routers/classes.py +78 -0
- services/taxonomy/routers/family.py +77 -0
- services/taxonomy/routers/subclass.py +82 -0
- services/warehouse_execution/__init__.py +1 -0
- services/warehouse_execution/main.py +46 -0
- services/warehouse_execution/routers/__init__.py +1 -0
- services/warehouse_execution/routers/execution.py +21 -0
- services/work_order_agent/__init__.py +17 -0
- services/work_order_agent/agent/__init__.py +17 -0
- services/work_order_agent/agent/work_order_agent.py +658 -0
- services/work_order_agent/tracking/__init__.py +101 -0
- services/work_order_agent/tracking/event_system.py +182 -0
- services/work_order_agent/tracking/state_machine.py +163 -0
- services/work_order_agent/tracking/state_machine_integrator.py +295 -0
- services/work_order_agent/tracking/test_phase2_implementation.py +302 -0
- services/work_order_agent/tracking/time_analysis.py +301 -0
- services/work_order_agent/tracking/tracked_work_order.py +255 -0
- services/work_order_agent/tracking/work_order_adapter.py +367 -0
- services/work_order_agent/tracking/work_order_batch_manager.py +406 -0
- services/work_order_agent/tracking/work_order_repository.py +431 -0
- services/workorders/helpers/__init__.py +5 -0
- services/workorders/helpers/area_room_usage_ledger.py +139 -0
- services/workorders/helpers/batch_execution_records.py +265 -0
- services/workorders/helpers/batch_release_workflows.py +158 -0
- services/workorders/helpers/batch_step_executions.py +145 -0
- services/workorders/helpers/equipment_usage_ledger.py +209 -0
- services/workorders/helpers/executed_bmr_records.py +170 -0
- services/workorders/helpers/executed_bpr_records.py +170 -0
- services/workorders/helpers/executed_instruction_evidence.py +155 -0
- services/workorders/helpers/ipc_result_records.py +134 -0
- services/workorders/helpers/production_batches.py +117 -0
- services/workorders/helpers/work_orders.py +367 -0
- services/workorders/helpers/yield_reconciliation_records.py +158 -0
- services/workorders/main.py +110 -0
- services/workorders/models/__init__.py +5 -0
- services/workorders/models/area_room_usage_ledger.py +154 -0
- services/workorders/models/batch_execution_records.py +575 -0
- services/workorders/models/batch_release_workflows.py +190 -0
- services/workorders/models/batch_step_executions.py +142 -0
- services/workorders/models/equipment_usage_ledger.py +144 -0
- services/workorders/models/executed_bmr_records.py +220 -0
- services/workorders/models/executed_bpr_records.py +220 -0
- services/workorders/models/executed_instruction_evidence.py +128 -0
- services/workorders/models/ipc_result_records.py +164 -0
- services/workorders/models/production_batches.py +181 -0
- services/workorders/models/work_orders.py +255 -0
- services/workorders/models/yield_reconciliation_records.py +175 -0
- services/workorders/routers/__init__.py +5 -0
- services/workorders/routers/area_room_usage_ledger.py +117 -0
- services/workorders/routers/batch_execution_records.py +103 -0
- services/workorders/routers/batch_release_workflows.py +86 -0
- services/workorders/routers/batch_step_executions.py +88 -0
- services/workorders/routers/equipment_usage_ledger.py +115 -0
- services/workorders/routers/executed_bmr_records.py +86 -0
- services/workorders/routers/executed_bpr_records.py +86 -0
- services/workorders/routers/executed_instruction_evidence.py +86 -0
- services/workorders/routers/ipc_result_records.py +86 -0
- services/workorders/routers/production_batches.py +86 -0
- services/workorders/routers/work_orders.py +257 -0
- services/workorders/routers/yield_reconciliation_records.py +86 -0
- src/broca/__init__.py +5 -0
- src/broca/agent.py +201 -0
- src/cerebellum/__init__.py +0 -0
- src/cerebellum/adapter.py +84 -0
- src/cerebellum/capabilities/__init__.py +0 -0
- src/cerebellum/capabilities/agent/__init__.py +0 -0
- src/cerebellum/capabilities/agent/agents.py +65 -0
- src/cerebellum/capabilities/ai/__init__.py +0 -0
- src/cerebellum/capabilities/ai/providers.py +106 -0
- src/cerebellum/capabilities/api/__init__.py +0 -0
- src/cerebellum/capabilities/api/graphql.py +35 -0
- src/cerebellum/capabilities/api/rest_api.py +45 -0
- src/cerebellum/capabilities/api/webhook.py +30 -0
- src/cerebellum/capabilities/browser/__init__.py +0 -0
- src/cerebellum/capabilities/browser/browsers.py +27 -0
- src/cerebellum/capabilities/cloud/__init__.py +0 -0
- src/cerebellum/capabilities/cloud/cloud.py +46 -0
- src/cerebellum/capabilities/communication/__init__.py +0 -0
- src/cerebellum/capabilities/communication/communication.py +62 -0
- src/cerebellum/capabilities/database/__init__.py +0 -0
- src/cerebellum/capabilities/database/elasticsearch.py +40 -0
- src/cerebellum/capabilities/database/influxdb.py +37 -0
- src/cerebellum/capabilities/database/mongodb.py +50 -0
- src/cerebellum/capabilities/database/neo4j.py +32 -0
- src/cerebellum/capabilities/database/redis.py +44 -0
- src/cerebellum/capabilities/enterprise/__init__.py +0 -0
- src/cerebellum/capabilities/enterprise/opensource.py +180 -0
- src/cerebellum/capabilities/enterprise/proprietary.py +313 -0
- src/cerebellum/capabilities/event_streaming/__init__.py +0 -0
- src/cerebellum/capabilities/event_streaming/streaming.py +38 -0
- src/cerebellum/capabilities/infrastructure/__init__.py +0 -0
- src/cerebellum/capabilities/infrastructure/infrastructure.py +30 -0
- src/cerebellum/capabilities/productivity/__init__.py +0 -0
- src/cerebellum/capabilities/productivity/productivity.py +158 -0
- src/cerebellum/capabilities/robotics/__init__.py +0 -0
- src/cerebellum/capabilities/robotics/robotics.py +124 -0
- src/cerebellum/capabilities/runtime/__init__.py +0 -0
- src/cerebellum/capabilities/runtime/runtimes.py +92 -0
- src/cerebellum/capabilities/search/__init__.py +0 -0
- src/cerebellum/capabilities/search/search.py +63 -0
- src/cerebellum/capabilities/source_control/__init__.py +0 -0
- src/cerebellum/capabilities/source_control/source_control.py +113 -0
- src/cerebellum/capabilities/storage/__init__.py +0 -0
- src/cerebellum/capabilities/storage/storage.py +94 -0
- src/cerebellum/capabilities/workflow/__init__.py +0 -0
- src/cerebellum/capabilities/workflow/workflows.py +49 -0
- src/cerebellum/capability.py +108 -0
- src/cerebellum/config.py +157 -0
- src/cerebellum/fallback.py +147 -0
- src/cerebellum/fallback_engine.py +121 -0
- src/cerebellum/key_manager.py +129 -0
- src/cerebellum/keystore.py +179 -0
- src/cerebellum/lifecycle.py +54 -0
- src/cerebellum/metadata.py +61 -0
- src/cerebellum/operator/base.py +25 -0
- src/cerebellum/peripheral.py +92 -0
- src/cerebellum/registry.py +98 -0
- src/cerebellum/resolve_entity_capability.py +259 -0
- src/cingulate/benchmark/__init__.py +23 -0
- src/cingulate/benchmark/reporter.py +102 -0
- src/cingulate/benchmark/runner.py +159 -0
- src/cingulate/benchmark/scenario_runner.py +150 -0
- src/cingulate/benchmark/validator.py +102 -0
- src/cingulate/governance/__init__.py +21 -0
- src/cingulate/governance/architecture_validator.py +194 -0
- src/cingulate/governance/compliance.py +104 -0
- src/cingulate/governance/governance.py +77 -0
- src/cingulate/governance/policy_registry.py +91 -0
- src/cortex/__init__.py +33 -0
- src/cortex/cost.py +71 -0
- src/cortex/counterfactual.py +162 -0
- src/cortex/digital_twin.py +90 -0
- src/cortex/experience.py +83 -0
- src/cortex/feedback.py +144 -0
- src/cortex/loss.py +116 -0
- src/cortex/prediction.py +142 -0
- src/cortex/replay.py +130 -0
- src/cortex/reward.py +113 -0
- src/cortex/simulator.py +102 -0
- src/cortex/world_model.py +180 -0
- src/cortex/world_model_simulation.py +1591 -0
- src/cortex/world_state.py +121 -0
- src/cortex/xavier.py +250 -0
- src/deepdive/__init__.py +29 -0
- src/deepdive/aggregation.py +113 -0
- src/deepdive/digital_twin_aggregator.py +128 -0
- src/deepdive/elasticsearch_adapter.py +110 -0
- src/deepdive/fleet_analytics.py +131 -0
- src/deepdive/knowledge_aggregator.py +130 -0
- src/homeostasis/__init__.py +19 -0
- src/homeostasis/control_plane.py +159 -0
- src/introspection/__init__.py +38 -0
- src/introspection/alerting.py +142 -0
- src/introspection/health.py +101 -0
- src/introspection/lemon.py +243 -0
- src/introspection/logging.py +147 -0
- src/introspection/metrics.py +106 -0
- src/introspection/tracing.py +162 -0
- src/monkey_brain/__init__.py +1 -0
- src/monkey_brain/api/main.py +148 -0
- src/monkey_brain/api/models.py +81 -0
- src/monkey_brain/api/routes/routes/keys.py +106 -0
- src/monkey_brain/api/routes/routes/run.py +169 -0
- src/monkey_brain/api/routes/routes/simulate.py +485 -0
- src/monkey_brain/dlm/__init__.py +44 -0
- src/monkey_brain/dlm/dlm.py +139 -0
- src/monkey_brain/dlm/gc.py +115 -0
- src/monkey_brain/dlm/lifecycle.py +149 -0
- src/monkey_brain/dlm/orphans.py +99 -0
- src/monkey_brain/dlm/storage.py +149 -0
- src/monkey_brain/dlm/ttl.py +140 -0
- src/monkey_brain/documents/__init__.py +0 -0
- src/monkey_brain/documents/document_ocr.py +6 -0
- src/monkey_brain/kernel/__init__.py +53 -0
- src/monkey_brain/kernel/capability_interface.py +144 -0
- src/monkey_brain/kernel/classifier/__init__.py +1 -0
- src/monkey_brain/kernel/classifier/embed_classifier.py +125 -0
- src/monkey_brain/kernel/classifier/intent_examples.py +106 -0
- src/monkey_brain/kernel/dag.py +23 -0
- src/monkey_brain/kernel/execution_state.py +257 -0
- src/monkey_brain/kernel/goal_planner.py +85 -0
- src/monkey_brain/kernel/goal_router.py +20 -0
- src/monkey_brain/kernel/goals/__init__.py +1 -0
- src/monkey_brain/kernel/goals/goal.py +130 -0
- src/monkey_brain/kernel/goals/goal_bootstrap.py +38 -0
- src/monkey_brain/kernel/goals/goal_classifier.py +132 -0
- src/monkey_brain/kernel/goals/goal_registry.py +75 -0
- src/monkey_brain/kernel/intents/__init__.py +1 -0
- src/monkey_brain/kernel/intents/event_adapter.py +246 -0
- src/monkey_brain/kernel/intents/helpers.py +13 -0
- src/monkey_brain/kernel/intents/intent_registry.py +705 -0
- src/monkey_brain/kernel/intents/intent_router.py +102 -0
- src/monkey_brain/kernel/intents/predicates/approval_create.py +9 -0
- src/monkey_brain/kernel/intents/predicates/approval_decision.py +9 -0
- src/monkey_brain/kernel/intents/predicates/approval_hold.py +9 -0
- src/monkey_brain/kernel/intents/predicates/approval_query.py +9 -0
- src/monkey_brain/kernel/intents/predicates/batch_close.py +9 -0
- src/monkey_brain/kernel/intents/predicates/batch_creation.py +9 -0
- src/monkey_brain/kernel/intents/predicates/batch_delete.py +9 -0
- src/monkey_brain/kernel/intents/predicates/batch_hold.py +9 -0
- src/monkey_brain/kernel/intents/predicates/batch_record.py +9 -0
- src/monkey_brain/kernel/intents/predicates/batch_update.py +9 -0
- src/monkey_brain/kernel/intents/predicates/change_control.py +49 -0
- src/monkey_brain/kernel/intents/predicates/compliance_audit.py +14 -0
- src/monkey_brain/kernel/intents/predicates/decision_intelligence.py +9 -0
- src/monkey_brain/kernel/intents/predicates/drug_research.py +9 -0
- src/monkey_brain/kernel/intents/predicates/fuzzy_match.py +19 -0
- src/monkey_brain/kernel/intents/predicates/production_kpi.py +9 -0
- src/monkey_brain/kernel/intents/predicates/sop_create.py +9 -0
- src/monkey_brain/kernel/intents/predicates/sop_query.py +9 -0
- src/monkey_brain/kernel/intents/predicates/sop_update.py +9 -0
- src/monkey_brain/kernel/intents/predicates/warehouse_shipping.py +9 -0
- src/monkey_brain/kernel/intents/predicates/work_order_create.py +9 -0
- src/monkey_brain/kernel/intents/predicates/work_order_delete.py +9 -0
- src/monkey_brain/kernel/intents/predicates/work_order_hold.py +9 -0
- src/monkey_brain/kernel/intents/predicates/work_order_query.py +9 -0
- src/monkey_brain/kernel/intents/predicates/work_order_status.py +9 -0
- src/monkey_brain/kernel/intents/predicates/work_order_update.py +9 -0
- src/monkey_brain/kernel/intents/predicates/worker.py +9 -0
- src/monkey_brain/kernel/intents/telemetry_adapter.py +274 -0
- src/monkey_brain/kernel/intents/utils.py +68 -0
- src/monkey_brain/kernel/learning.py +98 -0
- src/monkey_brain/kernel/llm_explorer.py +188 -0
- src/monkey_brain/kernel/loss.py +81 -0
- src/monkey_brain/kernel/nlp/__init__.py +1 -0
- src/monkey_brain/kernel/nlp/compat.py +23 -0
- src/monkey_brain/kernel/nlp/models.py +10 -0
- src/monkey_brain/kernel/nlp/question_analyzer.py +203 -0
- src/monkey_brain/kernel/nlp/spacy_parser.py +53 -0
- src/monkey_brain/kernel/observer.py +97 -0
- src/monkey_brain/kernel/parser/__init__.py +3 -0
- src/monkey_brain/kernel/parser/ast.py +28 -0
- src/monkey_brain/kernel/parser/extractors/__init__.py +11 -0
- src/monkey_brain/kernel/parser/extractors/entities.py +21 -0
- src/monkey_brain/kernel/parser/extractors/filters.py +16 -0
- src/monkey_brain/kernel/parser/extractors/projections.py +36 -0
- src/monkey_brain/kernel/parser/extractors/verbs.py +31 -0
- src/monkey_brain/kernel/parser/parser.py +57 -0
- src/monkey_brain/kernel/parser/rules.py +75 -0
- src/monkey_brain/kernel/pipeline.py +44 -0
- src/monkey_brain/kernel/planner.py +57 -0
- src/monkey_brain/kernel/rl/__init__.py +33 -0
- src/monkey_brain/kernel/rl/learner.py +98 -0
- src/monkey_brain/kernel/rl/policy.py +254 -0
- src/monkey_brain/kernel/rl/reward.py +117 -0
- src/monkey_brain/kernel/rl/transition.py +112 -0
- src/monkey_brain/persistence/__init__.py +47 -0
- src/monkey_brain/persistence/adapters.py +49 -0
- src/monkey_brain/persistence/events.py +105 -0
- src/monkey_brain/persistence/manager.py +124 -0
- src/monkey_brain/persistence/mongodb_adapter.py +91 -0
- src/monkey_brain/persistence/redis_adapter.py +93 -0
- src/monkey_brain/persistence/reducer.py +111 -0
- src/monkey_brain/runtime/__init__.py +49 -0
- src/monkey_brain/runtime/depedencies.py +8 -0
- src/monkey_brain/runtime/engine.py +183 -0
- src/monkey_brain/runtime/message_bus.py +82 -0
- src/monkey_brain/runtime/process.py +144 -0
- src/monkey_brain/runtime/resource_manager.py +100 -0
- src/monkey_brain/runtime/routers.py +8 -0
- src/monkey_brain/runtime/runtime.py +199 -0
- src/monkey_brain/runtime/scheduler.py +165 -0
- src/monkey_brain/runtime/supervisor.py +133 -0
- src/monkey_brain/runtime/worker_pool.py +111 -0
- src/plasticity/seed/__init__.py +30 -0
- src/plasticity/seed/benchmark_generator.py +105 -0
- src/plasticity/seed/event_generator.py +122 -0
- src/plasticity/seed/scenario_builder.py +134 -0
- src/plasticity/seed/seed_data.py +206 -0
- src/plasticity/seed/seeder.py +122 -0
- src/plasticity/testing/__init__.py +28 -0
- src/plasticity/testing/performance.py +131 -0
- src/plasticity/testing/profiler.py +255 -0
- src/plasticity/testing/reporter.py +84 -0
- src/plasticity/testing/runner.py +209 -0
- src/sync/__init__.py +12 -0
- src/sync/cloud_aggregator.py +63 -0
- src/sync/edge_node.py +51 -0
- src/sync/sync_manager.py +74 -0
|
@@ -0,0 +1,848 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import re
|
|
3
|
+
from datetime import datetime, timezone
|
|
4
|
+
from importlib import import_module
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from fastapi import HTTPException, status
|
|
8
|
+
|
|
9
|
+
from services.common.config import settings
|
|
10
|
+
from services.common.neo4j_mirror import _neo4j_label, _singular, cleanup_neo4j_bloat
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
_driver = None
|
|
14
|
+
CANVAS_MONGO_ENTITY_MAP = {
|
|
15
|
+
"plants": ("industrial_plants", "plant_id"),
|
|
16
|
+
"lines": ("industrial_lines", "line_id"),
|
|
17
|
+
"stages": ("industrial_stages", "stage_id"),
|
|
18
|
+
"workstations": ("workstations", "workstation_id"),
|
|
19
|
+
"machines": ("pharmaceutical_machines", "machine_id"),
|
|
20
|
+
"equipment": ("pharmaceutical_equipment", "equipment_id"),
|
|
21
|
+
"parts": ("machine_parts", "part_id"),
|
|
22
|
+
"machine_parts": ("machine_parts", "part_id"),
|
|
23
|
+
"tools": ("tools", "tool_id"),
|
|
24
|
+
"equipment_groups": ("equipment_groups", "equipment_group_id"),
|
|
25
|
+
"machine_groups": ("machine_groups", "machine_group_id"),
|
|
26
|
+
"raw_material_groups": ("raw_material_groups", "raw_material_group_id"),
|
|
27
|
+
"tag_groups": ("tag_groups", "tag_group_id"),
|
|
28
|
+
"products": ("products", "product_id"),
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _neo4j_relationship_type(value: Any) -> str:
|
|
33
|
+
candidate = re.sub(r"[^A-Za-z0-9_]+", "_", str(value or "").strip()).upper()
|
|
34
|
+
candidate = re.sub(r"_+", "_", candidate).strip("_")
|
|
35
|
+
if not candidate or not candidate[0].isalpha():
|
|
36
|
+
return "CANVAS_LINK"
|
|
37
|
+
return candidate
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _get_driver():
|
|
41
|
+
global _driver
|
|
42
|
+
if _driver is None:
|
|
43
|
+
try:
|
|
44
|
+
neo4j = import_module("neo4j")
|
|
45
|
+
except ImportError as exc:
|
|
46
|
+
raise HTTPException(
|
|
47
|
+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
48
|
+
detail="Neo4j driver is not installed. Install project requirements.",
|
|
49
|
+
) from exc
|
|
50
|
+
_driver = neo4j.AsyncGraphDatabase.driver(
|
|
51
|
+
settings.NEO4J_URI,
|
|
52
|
+
auth=(settings.NEO4J_USER, settings.NEO4J_PASSWORD),
|
|
53
|
+
)
|
|
54
|
+
return _driver
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
async def close_graph_driver() -> None:
|
|
58
|
+
global _driver
|
|
59
|
+
if _driver is not None:
|
|
60
|
+
await _driver.close()
|
|
61
|
+
_driver = None
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
async def _raise_neo4j_http_error(exc: Exception) -> None:
|
|
65
|
+
await close_graph_driver()
|
|
66
|
+
error_name = exc.__class__.__name__
|
|
67
|
+
neo4j_code = getattr(exc, "code", "") or ""
|
|
68
|
+
if error_name == "AuthError" or "Security" in neo4j_code:
|
|
69
|
+
raise HTTPException(
|
|
70
|
+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
71
|
+
detail=(
|
|
72
|
+
"Neo4j authentication failed. Set NEO4J_USER/NEO4J_PASSWORD "
|
|
73
|
+
"to match the running Neo4j instance, or recreate the Neo4j "
|
|
74
|
+
"data volume if you want to use the default dev credentials."
|
|
75
|
+
),
|
|
76
|
+
) from exc
|
|
77
|
+
if error_name in {"ServiceUnavailable", "SessionExpired"}:
|
|
78
|
+
raise HTTPException(
|
|
79
|
+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
80
|
+
detail=f"Neo4j is unavailable: {exc}",
|
|
81
|
+
) from exc
|
|
82
|
+
raise exc
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def is_graph_key(key: str) -> bool:
|
|
86
|
+
return (
|
|
87
|
+
key in {
|
|
88
|
+
"factory-layout-symbol-canvas",
|
|
89
|
+
"factory-layout-symbol-canvas:seed-version",
|
|
90
|
+
"document-management-cad-canvas",
|
|
91
|
+
"pid-symbol-canvas-nodes",
|
|
92
|
+
"indus-dashboard-process-definition-steps",
|
|
93
|
+
"indus-dashboard-process-definition-prechecks",
|
|
94
|
+
"indus-dashboard-process-definition-postchecks",
|
|
95
|
+
"indus-dashboard-process-definition-constraints",
|
|
96
|
+
"indus-dashboard-corrective-actions",
|
|
97
|
+
"supply-chain-graph-tabs",
|
|
98
|
+
"supply-chain-graph-rows",
|
|
99
|
+
"supply-chain-graph-canvas",
|
|
100
|
+
"supply-chain-graph-canvas-positions",
|
|
101
|
+
"supply-chain-graph-canvas-links",
|
|
102
|
+
"supply-chain-graph-canvas-visual-nodes",
|
|
103
|
+
}
|
|
104
|
+
or key.endswith("-canvas-positions")
|
|
105
|
+
or key.endswith("-canvas-links")
|
|
106
|
+
or key.endswith("-canvas-visual-nodes")
|
|
107
|
+
or key.startswith("supply-chain-graph-rows-")
|
|
108
|
+
or key.startswith("indus-dashboard-process-definition-")
|
|
109
|
+
or key == "indus-dashboard-corrective-actions"
|
|
110
|
+
or "-canvas-" in key
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def graph_id_for_key(key: str) -> str:
|
|
115
|
+
if key.startswith("supply-chain-graph-") and "-canvas-" in key:
|
|
116
|
+
prefix = key.removesuffix("-canvas-positions")
|
|
117
|
+
prefix = prefix.removesuffix("-canvas-links")
|
|
118
|
+
prefix = prefix.removesuffix("-canvas-visual-nodes")
|
|
119
|
+
return prefix.replace("supply-chain-graph-", "supply-chain-graph:", 1)
|
|
120
|
+
if key.startswith("supply-chain-graph-rows-"):
|
|
121
|
+
tab_id = key.removeprefix("supply-chain-graph-rows-")
|
|
122
|
+
return f"supply-chain-graph:{tab_id}"
|
|
123
|
+
if key.startswith("supply-chain-graph"):
|
|
124
|
+
return "supply-chain-graph"
|
|
125
|
+
if key.endswith("-canvas-positions"):
|
|
126
|
+
return key.removesuffix("-canvas-positions")
|
|
127
|
+
if key.endswith("-canvas-links"):
|
|
128
|
+
return key.removesuffix("-canvas-links")
|
|
129
|
+
if key.endswith("-canvas-visual-nodes"):
|
|
130
|
+
return key.removesuffix("-canvas-visual-nodes")
|
|
131
|
+
if key.endswith("-canvas-nodes"):
|
|
132
|
+
return key.removesuffix("-nodes")
|
|
133
|
+
return key
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def state_type_for_key(key: str) -> str:
|
|
137
|
+
if key.endswith("-canvas-positions"):
|
|
138
|
+
return "positions"
|
|
139
|
+
if key.endswith("-canvas-links"):
|
|
140
|
+
return "links"
|
|
141
|
+
if key.endswith("-canvas-visual-nodes") or key.endswith("-canvas-nodes"):
|
|
142
|
+
return "visual_nodes"
|
|
143
|
+
if key.startswith("supply-chain-graph-rows"):
|
|
144
|
+
return "rows"
|
|
145
|
+
if key == "supply-chain-graph-tabs":
|
|
146
|
+
return "tabs"
|
|
147
|
+
if key.endswith(":seed-version"):
|
|
148
|
+
return "seed_version"
|
|
149
|
+
if key.endswith("-canvas"):
|
|
150
|
+
return "canvas"
|
|
151
|
+
if key.startswith("indus-dashboard-process-definition-") or key == "indus-dashboard-corrective-actions":
|
|
152
|
+
return "canvas"
|
|
153
|
+
return "state"
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def _item_id(item: Any) -> str | None:
|
|
157
|
+
if not isinstance(item, dict):
|
|
158
|
+
return None
|
|
159
|
+
value = item.get("id") or item.get("nodeId") or item.get("key")
|
|
160
|
+
if value is None and isinstance(item.get("data"), dict):
|
|
161
|
+
value = item["data"].get("id")
|
|
162
|
+
return str(value) if value is not None else None
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _edge_endpoints(item: dict) -> tuple[str | None, str | None]:
|
|
166
|
+
source = (
|
|
167
|
+
item.get("source")
|
|
168
|
+
or item.get("source_node_id")
|
|
169
|
+
or item.get("sourceNodeId")
|
|
170
|
+
or item.get("sourceId")
|
|
171
|
+
or item.get("from")
|
|
172
|
+
or item.get("fromId")
|
|
173
|
+
or item.get("start")
|
|
174
|
+
)
|
|
175
|
+
target = (
|
|
176
|
+
item.get("target")
|
|
177
|
+
or item.get("target_node_id")
|
|
178
|
+
or item.get("targetNodeId")
|
|
179
|
+
or item.get("targetId")
|
|
180
|
+
or item.get("to")
|
|
181
|
+
or item.get("toId")
|
|
182
|
+
or item.get("end")
|
|
183
|
+
)
|
|
184
|
+
return (str(source) if source is not None else None, str(target) if target is not None else None)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _canvas_node_id(item: Any) -> str | None:
|
|
188
|
+
if not isinstance(item, dict):
|
|
189
|
+
return None
|
|
190
|
+
value = item.get("node_id") or item.get("nodeId") or item.get("id")
|
|
191
|
+
return str(value) if value is not None else None
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _canvas_endpoint_name(node_id: str) -> str:
|
|
195
|
+
return node_id.split(":")[-1]
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def _canvas_endpoint_type(node_id: str) -> str:
|
|
199
|
+
parts = [part for part in node_id.split(":") if part]
|
|
200
|
+
if len(parts) < 2:
|
|
201
|
+
return "record"
|
|
202
|
+
return _singular(parts[-2])
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def _canvas_entity_ref(node_id: str) -> tuple[str, str, str] | None:
|
|
206
|
+
parts = [part for part in node_id.split(":") if part]
|
|
207
|
+
if len(parts) < 2:
|
|
208
|
+
return None
|
|
209
|
+
segment = parts[-2]
|
|
210
|
+
entity_id = parts[-1]
|
|
211
|
+
mapped = CANVAS_MONGO_ENTITY_MAP.get(segment)
|
|
212
|
+
if mapped:
|
|
213
|
+
collection, id_field = mapped
|
|
214
|
+
return collection, id_field, entity_id
|
|
215
|
+
node_type = _singular(segment)
|
|
216
|
+
return segment, f"{node_type}_id", entity_id
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def _canvas_edge_id(item: dict) -> str:
|
|
220
|
+
source, target = _edge_endpoints(item)
|
|
221
|
+
return str(item.get("edge_id") or item.get("id") or item.get("linkId") or f"{source}->{target}")
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _canvas_nodes(value: Any) -> dict[str, dict[str, Any]]:
|
|
225
|
+
if not isinstance(value, dict):
|
|
226
|
+
return {}
|
|
227
|
+
|
|
228
|
+
nodes: dict[str, dict[str, Any]] = {}
|
|
229
|
+
positions = value.get("positions")
|
|
230
|
+
if not isinstance(positions, dict) and isinstance(value.get("data"), dict):
|
|
231
|
+
positions = value["data"].get("positions")
|
|
232
|
+
if isinstance(positions, dict):
|
|
233
|
+
for node_id, position in positions.items():
|
|
234
|
+
nodes[str(node_id)] = {"position": position}
|
|
235
|
+
|
|
236
|
+
for item in value.get("nodes", []) if isinstance(value.get("nodes"), list) else []:
|
|
237
|
+
node_id = _canvas_node_id(item)
|
|
238
|
+
if not node_id:
|
|
239
|
+
continue
|
|
240
|
+
nodes.setdefault(node_id, {})
|
|
241
|
+
nodes[node_id]["node"] = item
|
|
242
|
+
nodes[node_id].setdefault("position", {"x": item.get("x"), "y": item.get("y")})
|
|
243
|
+
|
|
244
|
+
snapshots = value.get("node_snapshots")
|
|
245
|
+
data = value.get("data") if isinstance(value.get("data"), dict) else {}
|
|
246
|
+
if not isinstance(snapshots, list) and isinstance(data, dict):
|
|
247
|
+
snapshots = data.get("node_snapshots")
|
|
248
|
+
if isinstance(snapshots, list):
|
|
249
|
+
for snapshot in snapshots:
|
|
250
|
+
node_id = _canvas_node_id(snapshot)
|
|
251
|
+
if not node_id:
|
|
252
|
+
continue
|
|
253
|
+
nodes.setdefault(node_id, {})
|
|
254
|
+
nodes[node_id]["snapshot"] = snapshot
|
|
255
|
+
nodes[node_id].setdefault("position", snapshot.get("position"))
|
|
256
|
+
|
|
257
|
+
return nodes
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def _canvas_links(value: Any) -> list[dict]:
|
|
261
|
+
if isinstance(value, list):
|
|
262
|
+
return [item for item in value if isinstance(item, dict)]
|
|
263
|
+
if not isinstance(value, dict):
|
|
264
|
+
return []
|
|
265
|
+
|
|
266
|
+
links = []
|
|
267
|
+
for key in ("edges", "links", "connections"):
|
|
268
|
+
items = value.get(key)
|
|
269
|
+
if isinstance(items, list):
|
|
270
|
+
links.extend(item for item in items if isinstance(item, dict))
|
|
271
|
+
|
|
272
|
+
data = value.get("data") if isinstance(value.get("data"), dict) else {}
|
|
273
|
+
if isinstance(data, dict):
|
|
274
|
+
for key in ("links", "edges", "connections"):
|
|
275
|
+
items = data.get(key)
|
|
276
|
+
if isinstance(items, list):
|
|
277
|
+
links.extend(item for item in items if isinstance(item, dict))
|
|
278
|
+
|
|
279
|
+
deduped = {}
|
|
280
|
+
for item in links:
|
|
281
|
+
deduped[_canvas_edge_id(item)] = item
|
|
282
|
+
return list(deduped.values())
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
async def _hydrate_canvas_nodes(nodes: dict[str, dict[str, Any]]) -> None:
|
|
286
|
+
if not nodes:
|
|
287
|
+
return
|
|
288
|
+
try:
|
|
289
|
+
from services.common.db import get_database
|
|
290
|
+
|
|
291
|
+
db = get_database()
|
|
292
|
+
except Exception:
|
|
293
|
+
db = None
|
|
294
|
+
if db is None:
|
|
295
|
+
return
|
|
296
|
+
|
|
297
|
+
for node_id, node_data in nodes.items():
|
|
298
|
+
ref = _canvas_entity_ref(node_id)
|
|
299
|
+
if not ref:
|
|
300
|
+
continue
|
|
301
|
+
collection, id_field, entity_id = ref
|
|
302
|
+
try:
|
|
303
|
+
doc = await db[collection].find_one(
|
|
304
|
+
{"$or": [{id_field: entity_id}, {"id": entity_id}, {"group_id": entity_id}]}
|
|
305
|
+
)
|
|
306
|
+
except Exception:
|
|
307
|
+
continue
|
|
308
|
+
if doc:
|
|
309
|
+
doc = dict(doc)
|
|
310
|
+
doc.pop("_id", None)
|
|
311
|
+
node_data["payload"] = doc
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
def _iter_items(value: Any) -> list[Any]:
|
|
315
|
+
if isinstance(value, list):
|
|
316
|
+
return value
|
|
317
|
+
if isinstance(value, dict):
|
|
318
|
+
for nested_key in ("nodes", "links", "edges", "rows", "items"):
|
|
319
|
+
nested = value.get(nested_key)
|
|
320
|
+
if isinstance(nested, list):
|
|
321
|
+
return nested
|
|
322
|
+
return [{"id": key, **item} if isinstance(item, dict) else {"id": key, "value": item} for key, item in value.items()]
|
|
323
|
+
return []
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def _graph_state_json(value: Any, *, sort_keys: bool = False) -> str:
|
|
327
|
+
return json.dumps(value, default=str, sort_keys=sort_keys)
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
async def save_graph_state(user_id: str, key: str, value: Any) -> None:
|
|
331
|
+
if not settings.NEO4J_MIRROR_ENABLED:
|
|
332
|
+
return
|
|
333
|
+
graph_id = graph_id_for_key(key)
|
|
334
|
+
state_type = state_type_for_key(key)
|
|
335
|
+
value_json = _graph_state_json(value)
|
|
336
|
+
updated_at = datetime.now(timezone.utc).isoformat()
|
|
337
|
+
driver = _get_driver()
|
|
338
|
+
|
|
339
|
+
try:
|
|
340
|
+
async with driver.session() as session:
|
|
341
|
+
await session.execute_write(
|
|
342
|
+
_save_state_tx,
|
|
343
|
+
user_id,
|
|
344
|
+
graph_id,
|
|
345
|
+
key,
|
|
346
|
+
state_type,
|
|
347
|
+
value_json,
|
|
348
|
+
updated_at,
|
|
349
|
+
)
|
|
350
|
+
if state_type in {"visual_nodes", "rows", "positions"}:
|
|
351
|
+
await session.execute_write(_sync_nodes_tx, user_id, graph_id, state_type, value, updated_at)
|
|
352
|
+
elif state_type == "links":
|
|
353
|
+
links = _canvas_links(value)
|
|
354
|
+
existing_node_ids = await session.execute_read(_existing_canvas_node_ids_tx, user_id, graph_id)
|
|
355
|
+
endpoint_nodes: dict[str, dict[str, Any]] = {
|
|
356
|
+
node_id: {}
|
|
357
|
+
for node_id in existing_node_ids
|
|
358
|
+
}
|
|
359
|
+
await session.execute_write(
|
|
360
|
+
_sync_links_tx,
|
|
361
|
+
user_id,
|
|
362
|
+
graph_id,
|
|
363
|
+
value,
|
|
364
|
+
updated_at,
|
|
365
|
+
set(existing_node_ids),
|
|
366
|
+
)
|
|
367
|
+
await session.execute_write(_sync_canvas_links_tx, user_id, graph_id, links, endpoint_nodes, updated_at)
|
|
368
|
+
elif state_type == "canvas":
|
|
369
|
+
nodes = _canvas_nodes(value)
|
|
370
|
+
links = _canvas_links(value)
|
|
371
|
+
await _hydrate_canvas_nodes(nodes)
|
|
372
|
+
await session.execute_write(_sync_canvas_tx, user_id, graph_id, nodes, links, updated_at)
|
|
373
|
+
await cleanup_neo4j_bloat()
|
|
374
|
+
except HTTPException:
|
|
375
|
+
raise
|
|
376
|
+
except Exception as exc:
|
|
377
|
+
await _raise_neo4j_http_error(exc)
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
async def get_graph_state(user_id: str, key: str) -> str | None:
|
|
381
|
+
driver = _get_driver()
|
|
382
|
+
try:
|
|
383
|
+
async with driver.session() as session:
|
|
384
|
+
result = await session.execute_read(_get_state_tx, user_id, key)
|
|
385
|
+
return result
|
|
386
|
+
except HTTPException:
|
|
387
|
+
raise
|
|
388
|
+
except Exception as exc:
|
|
389
|
+
await _raise_neo4j_http_error(exc)
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
async def delete_graph_state(user_id: str, key: str) -> bool:
|
|
393
|
+
driver = _get_driver()
|
|
394
|
+
try:
|
|
395
|
+
async with driver.session() as session:
|
|
396
|
+
return await session.execute_write(_delete_state_tx, user_id, key)
|
|
397
|
+
except HTTPException:
|
|
398
|
+
raise
|
|
399
|
+
except Exception as exc:
|
|
400
|
+
await _raise_neo4j_http_error(exc)
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
async def delete_canvas_node(canvas_node_id: str) -> bool:
|
|
404
|
+
driver = _get_driver()
|
|
405
|
+
try:
|
|
406
|
+
async with driver.session() as session:
|
|
407
|
+
deleted = await session.execute_write(_delete_canvas_node_tx, canvas_node_id)
|
|
408
|
+
if deleted:
|
|
409
|
+
await session.execute_write(_remove_canvas_node_from_states_tx, canvas_node_id)
|
|
410
|
+
await cleanup_neo4j_bloat()
|
|
411
|
+
return deleted
|
|
412
|
+
except HTTPException:
|
|
413
|
+
raise
|
|
414
|
+
except Exception as exc:
|
|
415
|
+
await _raise_neo4j_http_error(exc)
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
async def _save_state_tx(tx, user_id: str, graph_id: str, key: str, state_type: str, value_json: str, updated_at: str) -> None:
|
|
419
|
+
await tx.run(
|
|
420
|
+
"""
|
|
421
|
+
MERGE (u:User {id: $user_id})
|
|
422
|
+
MERGE (g:Graph {user_id: $user_id, graph_id: $graph_id})
|
|
423
|
+
SET g.updated_at = $updated_at
|
|
424
|
+
MERGE (u)-[:OWNS_GRAPH]->(g)
|
|
425
|
+
MERGE (s:GraphState {user_id: $user_id, key: $key})
|
|
426
|
+
SET s.graph_id = $graph_id,
|
|
427
|
+
s.state_type = $state_type,
|
|
428
|
+
s.value_json = $value_json,
|
|
429
|
+
s.updated_at = $updated_at
|
|
430
|
+
MERGE (g)-[:HAS_STATE]->(s)
|
|
431
|
+
""",
|
|
432
|
+
user_id=user_id,
|
|
433
|
+
graph_id=graph_id,
|
|
434
|
+
key=key,
|
|
435
|
+
state_type=state_type,
|
|
436
|
+
value_json=value_json,
|
|
437
|
+
updated_at=updated_at,
|
|
438
|
+
)
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
async def _get_state_tx(tx, user_id: str, key: str) -> str | None:
|
|
442
|
+
result = await tx.run(
|
|
443
|
+
"MATCH (s:GraphState {user_id: $user_id, key: $key}) RETURN s.value_json AS value_json",
|
|
444
|
+
user_id=user_id,
|
|
445
|
+
key=key,
|
|
446
|
+
)
|
|
447
|
+
record = await result.single()
|
|
448
|
+
return record["value_json"] if record else None
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
async def _delete_state_tx(tx, user_id: str, key: str) -> bool:
|
|
452
|
+
result = await tx.run(
|
|
453
|
+
"""
|
|
454
|
+
MATCH (s:GraphState {user_id: $user_id, key: $key})
|
|
455
|
+
WITH collect(s) AS states, count(s) AS deleted
|
|
456
|
+
FOREACH (state IN states | DETACH DELETE state)
|
|
457
|
+
RETURN deleted
|
|
458
|
+
""",
|
|
459
|
+
user_id=user_id,
|
|
460
|
+
key=key,
|
|
461
|
+
)
|
|
462
|
+
record = await result.single()
|
|
463
|
+
return bool(record and record["deleted"])
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
async def _delete_canvas_node_tx(tx, canvas_node_id: str) -> bool:
|
|
467
|
+
result = await tx.run(
|
|
468
|
+
"""
|
|
469
|
+
MATCH (n)
|
|
470
|
+
WHERE (
|
|
471
|
+
coalesce(n.source, "frontend_canvas") = "frontend_canvas"
|
|
472
|
+
OR coalesce(n.collection, "") STARTS WITH "canvas_"
|
|
473
|
+
)
|
|
474
|
+
AND (
|
|
475
|
+
n.canvas_node_id = $canvas_node_id
|
|
476
|
+
OR n.node_id = $canvas_node_id
|
|
477
|
+
OR n.id = $canvas_node_id
|
|
478
|
+
OR n.canvas_node_id ENDS WITH ":" + $canvas_node_id
|
|
479
|
+
)
|
|
480
|
+
WITH collect(n) AS nodes, count(n) AS deleted
|
|
481
|
+
FOREACH (node IN nodes | DETACH DELETE node)
|
|
482
|
+
RETURN deleted
|
|
483
|
+
""",
|
|
484
|
+
canvas_node_id=canvas_node_id,
|
|
485
|
+
)
|
|
486
|
+
record = await result.single()
|
|
487
|
+
return bool(record and record["deleted"])
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
async def _remove_canvas_node_from_states_tx(tx, canvas_node_id: str) -> None:
|
|
491
|
+
result = await tx.run(
|
|
492
|
+
"""
|
|
493
|
+
MATCH (s:GraphState)
|
|
494
|
+
WHERE s.value_json CONTAINS $canvas_node_id
|
|
495
|
+
RETURN s.user_id AS user_id, s.key AS key, s.value_json AS value_json
|
|
496
|
+
""",
|
|
497
|
+
canvas_node_id=canvas_node_id,
|
|
498
|
+
)
|
|
499
|
+
records = await result.data()
|
|
500
|
+
for record in records:
|
|
501
|
+
cleaned = _remove_canvas_node_from_value(
|
|
502
|
+
json.loads(record["value_json"]),
|
|
503
|
+
canvas_node_id,
|
|
504
|
+
)
|
|
505
|
+
await tx.run(
|
|
506
|
+
"""
|
|
507
|
+
MATCH (s:GraphState {user_id: $user_id, key: $key})
|
|
508
|
+
SET s.value_json = $value_json,
|
|
509
|
+
s.updated_at = $updated_at
|
|
510
|
+
""",
|
|
511
|
+
user_id=record["user_id"],
|
|
512
|
+
key=record["key"],
|
|
513
|
+
value_json=_graph_state_json(cleaned),
|
|
514
|
+
updated_at=datetime.now(timezone.utc).isoformat(),
|
|
515
|
+
)
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
def _references_canvas_node(value: Any, canvas_node_id: str) -> bool:
|
|
519
|
+
if value is None:
|
|
520
|
+
return False
|
|
521
|
+
if isinstance(value, str):
|
|
522
|
+
return value == canvas_node_id or value.endswith(f":{canvas_node_id}")
|
|
523
|
+
if isinstance(value, dict):
|
|
524
|
+
return any(_references_canvas_node(item, canvas_node_id) for item in value.values())
|
|
525
|
+
if isinstance(value, list):
|
|
526
|
+
return any(_references_canvas_node(item, canvas_node_id) for item in value)
|
|
527
|
+
return str(value) == canvas_node_id
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
def _remove_canvas_node_from_value(value: Any, canvas_node_id: str) -> Any:
|
|
531
|
+
if isinstance(value, list):
|
|
532
|
+
cleaned = []
|
|
533
|
+
for item in value:
|
|
534
|
+
if isinstance(item, dict):
|
|
535
|
+
item_id = _canvas_node_id(item) or _item_id(item)
|
|
536
|
+
source, target = _edge_endpoints(item)
|
|
537
|
+
if (
|
|
538
|
+
_references_canvas_node(item_id, canvas_node_id)
|
|
539
|
+
or _references_canvas_node(source, canvas_node_id)
|
|
540
|
+
or _references_canvas_node(target, canvas_node_id)
|
|
541
|
+
):
|
|
542
|
+
continue
|
|
543
|
+
cleaned.append(_remove_canvas_node_from_value(item, canvas_node_id))
|
|
544
|
+
return cleaned
|
|
545
|
+
|
|
546
|
+
if not isinstance(value, dict):
|
|
547
|
+
return value
|
|
548
|
+
|
|
549
|
+
cleaned: dict[str, Any] = {}
|
|
550
|
+
for key, item in value.items():
|
|
551
|
+
if _references_canvas_node(key, canvas_node_id):
|
|
552
|
+
continue
|
|
553
|
+
if key in {"nodes", "links", "edges", "items", "rows"} and isinstance(item, list):
|
|
554
|
+
cleaned[key] = _remove_canvas_node_from_value(item, canvas_node_id)
|
|
555
|
+
continue
|
|
556
|
+
if key == "positions" and isinstance(item, dict):
|
|
557
|
+
cleaned[key] = {
|
|
558
|
+
position_key: position
|
|
559
|
+
for position_key, position in item.items()
|
|
560
|
+
if not _references_canvas_node(position_key, canvas_node_id)
|
|
561
|
+
}
|
|
562
|
+
continue
|
|
563
|
+
cleaned[key] = _remove_canvas_node_from_value(item, canvas_node_id)
|
|
564
|
+
return cleaned
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
async def _sync_nodes_tx(tx, user_id: str, graph_id: str, state_type: str, value: Any, updated_at: str) -> None:
|
|
568
|
+
if state_type == "positions" and isinstance(value, dict):
|
|
569
|
+
for node_id, position in value.items():
|
|
570
|
+
await tx.run(
|
|
571
|
+
"""
|
|
572
|
+
MERGE (n:GraphNode {user_id: $user_id, graph_id: $graph_id, node_id: $node_id})
|
|
573
|
+
SET n.position_json = $position_json,
|
|
574
|
+
n.updated_at = $updated_at
|
|
575
|
+
""",
|
|
576
|
+
user_id=user_id,
|
|
577
|
+
graph_id=graph_id,
|
|
578
|
+
node_id=str(node_id),
|
|
579
|
+
position_json=_graph_state_json(position),
|
|
580
|
+
updated_at=updated_at,
|
|
581
|
+
)
|
|
582
|
+
return
|
|
583
|
+
|
|
584
|
+
for item in _iter_items(value):
|
|
585
|
+
node_id = _item_id(item)
|
|
586
|
+
if not node_id:
|
|
587
|
+
continue
|
|
588
|
+
await tx.run(
|
|
589
|
+
"""
|
|
590
|
+
MERGE (g:Graph {user_id: $user_id, graph_id: $graph_id})
|
|
591
|
+
MERGE (n:GraphNode {user_id: $user_id, graph_id: $graph_id, node_id: $node_id})
|
|
592
|
+
SET n.payload_json = $payload_json,
|
|
593
|
+
n.updated_at = $updated_at
|
|
594
|
+
MERGE (g)-[:HAS_NODE]->(n)
|
|
595
|
+
""",
|
|
596
|
+
user_id=user_id,
|
|
597
|
+
graph_id=graph_id,
|
|
598
|
+
node_id=node_id,
|
|
599
|
+
payload_json=_graph_state_json(item),
|
|
600
|
+
updated_at=updated_at,
|
|
601
|
+
)
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
async def _sync_links_tx(
|
|
605
|
+
tx,
|
|
606
|
+
user_id: str,
|
|
607
|
+
graph_id: str,
|
|
608
|
+
value: Any,
|
|
609
|
+
updated_at: str,
|
|
610
|
+
valid_node_ids: set[str] | None = None,
|
|
611
|
+
) -> None:
|
|
612
|
+
await tx.run(
|
|
613
|
+
"MATCH ()-[r:GRAPH_LINK {user_id: $user_id, graph_id: $graph_id}]->() DELETE r",
|
|
614
|
+
user_id=user_id,
|
|
615
|
+
graph_id=graph_id,
|
|
616
|
+
)
|
|
617
|
+
for item in _iter_items(value):
|
|
618
|
+
if not isinstance(item, dict):
|
|
619
|
+
continue
|
|
620
|
+
source, target = _edge_endpoints(item)
|
|
621
|
+
if not source or not target:
|
|
622
|
+
continue
|
|
623
|
+
if valid_node_ids is not None and (source not in valid_node_ids or target not in valid_node_ids):
|
|
624
|
+
continue
|
|
625
|
+
link_id = str(item.get("id") or item.get("linkId") or f"{source}->{target}")
|
|
626
|
+
await tx.run(
|
|
627
|
+
"""
|
|
628
|
+
MERGE (g:Graph {user_id: $user_id, graph_id: $graph_id})
|
|
629
|
+
MERGE (source:GraphNode {user_id: $user_id, graph_id: $graph_id, node_id: $source})
|
|
630
|
+
MERGE (target:GraphNode {user_id: $user_id, graph_id: $graph_id, node_id: $target})
|
|
631
|
+
MERGE (g)-[:HAS_NODE]->(source)
|
|
632
|
+
MERGE (g)-[:HAS_NODE]->(target)
|
|
633
|
+
MERGE (source)-[r:GRAPH_LINK {user_id: $user_id, graph_id: $graph_id, link_id: $link_id}]->(target)
|
|
634
|
+
SET r.payload_json = $payload_json,
|
|
635
|
+
r.updated_at = $updated_at
|
|
636
|
+
""",
|
|
637
|
+
user_id=user_id,
|
|
638
|
+
graph_id=graph_id,
|
|
639
|
+
source=source,
|
|
640
|
+
target=target,
|
|
641
|
+
link_id=link_id,
|
|
642
|
+
payload_json=_graph_state_json(item),
|
|
643
|
+
updated_at=updated_at,
|
|
644
|
+
)
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
async def _sync_canvas_tx(
|
|
648
|
+
tx,
|
|
649
|
+
user_id: str,
|
|
650
|
+
graph_id: str,
|
|
651
|
+
nodes: dict[str, dict[str, Any]],
|
|
652
|
+
links: list[dict],
|
|
653
|
+
updated_at: str,
|
|
654
|
+
) -> None:
|
|
655
|
+
await _prune_canvas_nodes_tx(tx, user_id, graph_id, sorted(nodes))
|
|
656
|
+
|
|
657
|
+
for node_id, node_data in nodes.items():
|
|
658
|
+
await _upsert_canvas_node_tx(tx, user_id, graph_id, node_id, node_data, updated_at)
|
|
659
|
+
await _sync_canvas_links_tx(tx, user_id, graph_id, links, nodes, updated_at)
|
|
660
|
+
|
|
661
|
+
|
|
662
|
+
async def _existing_canvas_node_ids_tx(tx, user_id: str, graph_id: str) -> list[str]:
|
|
663
|
+
result = await tx.run(
|
|
664
|
+
"""
|
|
665
|
+
MATCH (n)
|
|
666
|
+
WHERE n.canvas_node_id IS NOT NULL
|
|
667
|
+
AND coalesce(n.source, 'frontend_canvas') = 'frontend_canvas'
|
|
668
|
+
AND n.user_id = $user_id
|
|
669
|
+
AND n.graph_id = $graph_id
|
|
670
|
+
RETURN collect(DISTINCT n.canvas_node_id) AS node_ids
|
|
671
|
+
""",
|
|
672
|
+
user_id=user_id,
|
|
673
|
+
graph_id=graph_id,
|
|
674
|
+
)
|
|
675
|
+
record = await result.single()
|
|
676
|
+
return list(record["node_ids"] or []) if record else []
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
async def _prune_canvas_nodes_tx(tx, user_id: str, graph_id: str, current_node_ids: list[str]) -> None:
|
|
680
|
+
await tx.run(
|
|
681
|
+
"""
|
|
682
|
+
MATCH (n)
|
|
683
|
+
WHERE n.canvas_node_id IS NOT NULL
|
|
684
|
+
AND coalesce(n.source, 'frontend_canvas') = 'frontend_canvas'
|
|
685
|
+
AND n.user_id = $user_id
|
|
686
|
+
AND (
|
|
687
|
+
n.graph_id = $graph_id
|
|
688
|
+
OR n.graph_id IS NULL
|
|
689
|
+
)
|
|
690
|
+
AND NOT n.canvas_node_id IN $current_node_ids
|
|
691
|
+
DETACH DELETE n
|
|
692
|
+
""",
|
|
693
|
+
user_id=user_id,
|
|
694
|
+
graph_id=graph_id,
|
|
695
|
+
current_node_ids=current_node_ids,
|
|
696
|
+
)
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
async def _sync_canvas_links_tx(
|
|
700
|
+
tx,
|
|
701
|
+
user_id: str,
|
|
702
|
+
graph_id: str,
|
|
703
|
+
links: list[dict],
|
|
704
|
+
endpoint_nodes: dict[str, dict[str, Any]],
|
|
705
|
+
updated_at: str,
|
|
706
|
+
) -> None:
|
|
707
|
+
await tx.run(
|
|
708
|
+
"""
|
|
709
|
+
MATCH ()-[r]->()
|
|
710
|
+
WHERE r.user_id = $user_id
|
|
711
|
+
AND r.graph_id = $graph_id
|
|
712
|
+
AND r.edge_id IS NOT NULL
|
|
713
|
+
DELETE r
|
|
714
|
+
""",
|
|
715
|
+
user_id=user_id,
|
|
716
|
+
graph_id=graph_id,
|
|
717
|
+
)
|
|
718
|
+
|
|
719
|
+
for item in links:
|
|
720
|
+
source, target = _edge_endpoints(item)
|
|
721
|
+
if not source or not target:
|
|
722
|
+
continue
|
|
723
|
+
if source not in endpoint_nodes or target not in endpoint_nodes:
|
|
724
|
+
continue
|
|
725
|
+
await _upsert_canvas_node_tx(tx, user_id, graph_id, source, endpoint_nodes.get(source, {}), updated_at)
|
|
726
|
+
await _upsert_canvas_node_tx(tx, user_id, graph_id, target, endpoint_nodes.get(target, {}), updated_at)
|
|
727
|
+
await _upsert_canvas_link_tx(tx, user_id, graph_id, item, source, target, updated_at)
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
async def _upsert_canvas_node_tx(
|
|
731
|
+
tx,
|
|
732
|
+
user_id: str,
|
|
733
|
+
graph_id: str,
|
|
734
|
+
node_id: str,
|
|
735
|
+
node_data: dict[str, Any],
|
|
736
|
+
updated_at: str,
|
|
737
|
+
) -> None:
|
|
738
|
+
name = _canvas_endpoint_name(node_id)
|
|
739
|
+
node_type = _canvas_endpoint_type(node_id)
|
|
740
|
+
label = _neo4j_label(node_type)
|
|
741
|
+
snapshot = node_data.get("snapshot") or node_data.get("node") or {}
|
|
742
|
+
payload = node_data.get("payload")
|
|
743
|
+
if payload is None and isinstance(node_data.get("snapshot"), dict):
|
|
744
|
+
payload = node_data["snapshot"].get("row") or node_data["snapshot"]
|
|
745
|
+
if payload is None and isinstance(node_data.get("node"), dict):
|
|
746
|
+
payload = node_data["node"].get("data") or node_data["node"]
|
|
747
|
+
if payload is None:
|
|
748
|
+
payload = {
|
|
749
|
+
"canvas_node_id": node_id,
|
|
750
|
+
"entity_id": name,
|
|
751
|
+
"type": node_type,
|
|
752
|
+
"name": name,
|
|
753
|
+
}
|
|
754
|
+
display_name = name
|
|
755
|
+
if isinstance(payload, dict) and payload.get("name"):
|
|
756
|
+
display_name = str(payload["name"])
|
|
757
|
+
await tx.run(
|
|
758
|
+
f"""
|
|
759
|
+
MERGE (n:{label} {{entity_id: $entity_id, type: $node_type}})
|
|
760
|
+
SET n.name = CASE
|
|
761
|
+
WHEN $name = $entity_id AND n.name IS NOT NULL AND n.name <> n.entity_id THEN n.name
|
|
762
|
+
ELSE $name
|
|
763
|
+
END,
|
|
764
|
+
n.user_id = $user_id,
|
|
765
|
+
n.graph_id = $graph_id,
|
|
766
|
+
n.collection = coalesce(n.collection, $collection),
|
|
767
|
+
n.canvas_node_id = $node_id,
|
|
768
|
+
n.source = coalesce(n.source, 'frontend_canvas'),
|
|
769
|
+
n.payload_json = $payload_json,
|
|
770
|
+
n.canvas_position_json = $position_json,
|
|
771
|
+
n.canvas_snapshot_json = $snapshot_json,
|
|
772
|
+
n.updated_at = $updated_at
|
|
773
|
+
""",
|
|
774
|
+
entity_id=name,
|
|
775
|
+
node_type=node_type,
|
|
776
|
+
name=display_name,
|
|
777
|
+
collection=f"canvas_{node_type}",
|
|
778
|
+
node_id=node_id,
|
|
779
|
+
user_id=user_id,
|
|
780
|
+
graph_id=graph_id,
|
|
781
|
+
payload_json=_graph_state_json(payload or {}, sort_keys=True),
|
|
782
|
+
position_json=_graph_state_json(node_data.get("position"), sort_keys=True),
|
|
783
|
+
snapshot_json=_graph_state_json(snapshot, sort_keys=True),
|
|
784
|
+
updated_at=updated_at,
|
|
785
|
+
)
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
async def _upsert_canvas_link_tx(
|
|
789
|
+
tx,
|
|
790
|
+
user_id: str,
|
|
791
|
+
graph_id: str,
|
|
792
|
+
item: dict,
|
|
793
|
+
source_node_id: str,
|
|
794
|
+
target_node_id: str,
|
|
795
|
+
updated_at: str,
|
|
796
|
+
) -> None:
|
|
797
|
+
source_name = _canvas_endpoint_name(source_node_id)
|
|
798
|
+
source_type = _canvas_endpoint_type(source_node_id)
|
|
799
|
+
target_name = _canvas_endpoint_name(target_node_id)
|
|
800
|
+
target_type = _canvas_endpoint_type(target_node_id)
|
|
801
|
+
link_id = _canvas_edge_id(item)
|
|
802
|
+
await tx.run(
|
|
803
|
+
"""
|
|
804
|
+
MATCH (source {canvas_node_id: $source_node_id, graph_id: $graph_id})
|
|
805
|
+
MATCH (target {canvas_node_id: $target_node_id, graph_id: $graph_id})
|
|
806
|
+
MERGE (source)-[r:CANVAS_LINK {edge_id: $link_id}]->(target)
|
|
807
|
+
SET r.source_node_id = $source_node_id,
|
|
808
|
+
r.target_node_id = $target_node_id,
|
|
809
|
+
r.user_id = $user_id,
|
|
810
|
+
r.graph_id = $graph_id,
|
|
811
|
+
r.name = coalesce($name, $label, $link_id),
|
|
812
|
+
r.label = coalesce($label, $name, $link_id),
|
|
813
|
+
r.source_handle = $source_handle,
|
|
814
|
+
r.target_handle = $target_handle,
|
|
815
|
+
r.flow_color = $flow_color,
|
|
816
|
+
r.flow_enabled = $flow_enabled,
|
|
817
|
+
r.route_mode = $route_mode,
|
|
818
|
+
r.sourceHandle = $source_handle,
|
|
819
|
+
r.targetHandle = $target_handle,
|
|
820
|
+
r.flowColor = $flow_color,
|
|
821
|
+
r.flowEnabled = $flow_enabled,
|
|
822
|
+
r.arrowHead = $arrow_head,
|
|
823
|
+
r.routeMode = $route_mode,
|
|
824
|
+
r.points = $points,
|
|
825
|
+
r.payload_json = $payload_json,
|
|
826
|
+
r.updated_at = $updated_at
|
|
827
|
+
""",
|
|
828
|
+
user_id=user_id,
|
|
829
|
+
graph_id=graph_id,
|
|
830
|
+
link_id=link_id,
|
|
831
|
+
source_name=source_name,
|
|
832
|
+
source_type=source_type,
|
|
833
|
+
target_name=target_name,
|
|
834
|
+
target_type=target_type,
|
|
835
|
+
source_node_id=source_node_id,
|
|
836
|
+
target_node_id=target_node_id,
|
|
837
|
+
name=item.get("name"),
|
|
838
|
+
label=item.get("label"),
|
|
839
|
+
source_handle=item.get("sourceHandle"),
|
|
840
|
+
target_handle=item.get("targetHandle"),
|
|
841
|
+
flow_color=item.get("flowColor"),
|
|
842
|
+
flow_enabled=item.get("flowEnabled"),
|
|
843
|
+
arrow_head=item.get("arrowHead"),
|
|
844
|
+
route_mode=item.get("routeMode"),
|
|
845
|
+
points=_graph_state_json(item.get("points", []), sort_keys=True),
|
|
846
|
+
payload_json=_graph_state_json(item, sort_keys=True),
|
|
847
|
+
updated_at=updated_at,
|
|
848
|
+
)
|