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,569 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import hashlib
|
|
3
|
+
import io
|
|
4
|
+
from datetime import date, datetime, timezone
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Optional
|
|
7
|
+
from uuid import uuid4
|
|
8
|
+
|
|
9
|
+
from fastapi import UploadFile
|
|
10
|
+
from motor.motor_asyncio import AsyncIOMotorDatabase
|
|
11
|
+
|
|
12
|
+
from services.common.embeddings import build_embedding
|
|
13
|
+
from src.monkey_brain.documents.document_ocr import DocumentOcrError, extract_document_text_with_qwen_ocr
|
|
14
|
+
from services.documents.models.document_metadata import (
|
|
15
|
+
DocumentMetadataCreate,
|
|
16
|
+
DocumentMetadataResponse,
|
|
17
|
+
DocumentMetadataUpdate,
|
|
18
|
+
serialize_document,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
COLLECTION = "document_metadata"
|
|
22
|
+
DOCUMENT_EMBEDDINGS_COLLECTION = "document_embeddings"
|
|
23
|
+
GRAPH_RAG_DOCUMENT_CHUNKS_COLLECTION = "agentos_document_chunks"
|
|
24
|
+
LOCAL_UPLOAD_DIR = Path(os.getenv("DOCUMENT_UPLOAD_DIR", "uploads/documents"))
|
|
25
|
+
DOCUMENT_EMBEDDING_MAX_BYTES = int(os.getenv("DOCUMENT_EMBEDDING_MAX_BYTES", str(5 * 1024 * 1024)))
|
|
26
|
+
DOCUMENT_EMBEDDING_TEXT_MAX_CHARS = int(os.getenv("DOCUMENT_EMBEDDING_TEXT_MAX_CHARS", "20000"))
|
|
27
|
+
DOCUMENT_CHUNK_SIZE = int(os.getenv("DOCUMENT_CHUNK_SIZE", "1400"))
|
|
28
|
+
DOCUMENT_CHUNK_OVERLAP = int(os.getenv("DOCUMENT_CHUNK_OVERLAP", "180"))
|
|
29
|
+
S3_BUCKET = os.getenv("S3_DOCUMENT_BUCKET") or os.getenv("AWS_S3_BUCKET")
|
|
30
|
+
S3_PREFIX = os.getenv("S3_DOCUMENT_PREFIX", "documents").strip("/")
|
|
31
|
+
S3_REGION = os.getenv("AWS_REGION") or os.getenv("AWS_DEFAULT_REGION")
|
|
32
|
+
S3_ENDPOINT_URL = os.getenv("S3_ENDPOINT_URL")
|
|
33
|
+
S3_PUBLIC_BASE_URL = os.getenv("S3_PUBLIC_BASE_URL")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _utc_now() -> datetime:
|
|
37
|
+
return datetime.now(timezone.utc)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _serialize(doc: Optional[dict]) -> Optional[dict]:
|
|
41
|
+
if not doc:
|
|
42
|
+
return None
|
|
43
|
+
return serialize_document(_coerce_legacy_document(doc))
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _coerce_legacy_document(doc: dict) -> dict:
|
|
47
|
+
"""Keep older metadata rows readable after the schema replacement."""
|
|
48
|
+
coerced = dict(doc)
|
|
49
|
+
now = _utc_now()
|
|
50
|
+
|
|
51
|
+
coerced.setdefault("document_name", coerced.get("document_title") or "Untitled Document")
|
|
52
|
+
coerced.setdefault("document_version", "1.0")
|
|
53
|
+
coerced.setdefault("document_tags", coerced.get("tags") or [])
|
|
54
|
+
coerced.setdefault(
|
|
55
|
+
"document_url",
|
|
56
|
+
coerced.get("s3_url")
|
|
57
|
+
or coerced.get("local_path")
|
|
58
|
+
or f"/api/v1/document-metadata/{coerced.get('document_id', 'unknown')}/view",
|
|
59
|
+
)
|
|
60
|
+
coerced.setdefault("document_status", coerced.get("status") or "Draft")
|
|
61
|
+
coerced.setdefault("document_owner", coerced.get("document_author") or "Unknown Owner")
|
|
62
|
+
coerced.setdefault("document_preparer", coerced.get("created_by") or "Unknown Preparer")
|
|
63
|
+
coerced.setdefault("document_reviewer", "Unknown Reviewer")
|
|
64
|
+
coerced.setdefault("document_approver", "Unknown Approver")
|
|
65
|
+
coerced.setdefault("document_due_date", now)
|
|
66
|
+
coerced.setdefault("created_by", coerced.get("document_author") or "system")
|
|
67
|
+
coerced.setdefault("last_modified_by", coerced.get("document_author") or "system")
|
|
68
|
+
coerced.setdefault("created_at", now)
|
|
69
|
+
coerced.setdefault("last_modified_at", coerced.get("updated_at") or coerced.get("created_at") or now)
|
|
70
|
+
|
|
71
|
+
return coerced
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _prepare(value):
|
|
75
|
+
if isinstance(value, datetime):
|
|
76
|
+
if value.tzinfo is None:
|
|
77
|
+
return value.replace(tzinfo=timezone.utc)
|
|
78
|
+
return value.astimezone(timezone.utc)
|
|
79
|
+
if isinstance(value, date):
|
|
80
|
+
return datetime(value.year, value.month, value.day, tzinfo=timezone.utc)
|
|
81
|
+
if isinstance(value, dict):
|
|
82
|
+
return {key: _prepare(item) for key, item in value.items()}
|
|
83
|
+
if isinstance(value, list):
|
|
84
|
+
return [_prepare(item) for item in value]
|
|
85
|
+
return value
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _as_response_doc(data: dict) -> dict:
|
|
89
|
+
return _prepare(DocumentMetadataResponse(**data).model_dump(mode="python"))
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _document_url_from_storage(document_id: str, storage_fields: dict) -> str:
|
|
93
|
+
if storage_fields.get("s3_url"):
|
|
94
|
+
return storage_fields["s3_url"]
|
|
95
|
+
if storage_fields.get("local_path"):
|
|
96
|
+
return storage_fields["local_path"]
|
|
97
|
+
return f"/api/v1/document-metadata/{document_id}/view"
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _safe_filename(filename: str) -> str:
|
|
101
|
+
cleaned = Path(filename or "document").name
|
|
102
|
+
return cleaned.replace("/", "_").replace("\\", "_")
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _looks_like_text(content: bytes) -> bool:
|
|
106
|
+
if not content:
|
|
107
|
+
return False
|
|
108
|
+
sample = content[:2048]
|
|
109
|
+
printable = sum(1 for byte in sample if byte in (9, 10, 13) or 32 <= byte <= 126)
|
|
110
|
+
return printable / max(len(sample), 1) >= 0.85
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _decode_text(content: bytes) -> str:
|
|
114
|
+
for encoding in ("utf-8", "utf-16", "latin-1"):
|
|
115
|
+
try:
|
|
116
|
+
return content.decode(encoding)
|
|
117
|
+
except UnicodeDecodeError:
|
|
118
|
+
continue
|
|
119
|
+
return content.decode("utf-8", errors="ignore")
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _extract_pdf_text(content: bytes) -> str:
|
|
123
|
+
try:
|
|
124
|
+
from pypdf import PdfReader
|
|
125
|
+
except Exception:
|
|
126
|
+
return ""
|
|
127
|
+
|
|
128
|
+
try:
|
|
129
|
+
reader = PdfReader(io.BytesIO(content))
|
|
130
|
+
pages = []
|
|
131
|
+
for page in reader.pages:
|
|
132
|
+
text = page.extract_text() or ""
|
|
133
|
+
if text.strip():
|
|
134
|
+
pages.append(text)
|
|
135
|
+
return "\n\n".join(pages)
|
|
136
|
+
except Exception:
|
|
137
|
+
return ""
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _extract_upload_text(content: bytes, content_type: str | None, filename: str | None) -> tuple[str, str]:
|
|
141
|
+
content_type_normalized = (content_type or "").split(";")[0].strip().lower()
|
|
142
|
+
suffix = Path(filename or "").suffix.lower()
|
|
143
|
+
|
|
144
|
+
if content_type_normalized == "application/pdf" or suffix in {".pdf", ".pfd"}:
|
|
145
|
+
text = _extract_pdf_text(content)
|
|
146
|
+
return text[:DOCUMENT_EMBEDDING_TEXT_MAX_CHARS], "pdf"
|
|
147
|
+
|
|
148
|
+
text_extensions = {
|
|
149
|
+
".csv",
|
|
150
|
+
".htm",
|
|
151
|
+
".html",
|
|
152
|
+
".json",
|
|
153
|
+
".log",
|
|
154
|
+
".md",
|
|
155
|
+
".txt",
|
|
156
|
+
".xml",
|
|
157
|
+
".yaml",
|
|
158
|
+
".yml",
|
|
159
|
+
}
|
|
160
|
+
if content_type_normalized.startswith("text/") or suffix in text_extensions or _looks_like_text(content):
|
|
161
|
+
text = _decode_text(content)
|
|
162
|
+
return text[:DOCUMENT_EMBEDDING_TEXT_MAX_CHARS], "text"
|
|
163
|
+
|
|
164
|
+
return "", "unsupported"
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _chunk_document_text(text: str) -> list[str]:
|
|
168
|
+
normalized = "\n\n".join(part.strip() for part in text.splitlines() if part.strip())
|
|
169
|
+
if not normalized:
|
|
170
|
+
return []
|
|
171
|
+
chunks = []
|
|
172
|
+
start = 0
|
|
173
|
+
while start < len(normalized):
|
|
174
|
+
end = min(start + DOCUMENT_CHUNK_SIZE, len(normalized))
|
|
175
|
+
chunk = normalized[start:end].strip()
|
|
176
|
+
if chunk:
|
|
177
|
+
chunks.append(chunk)
|
|
178
|
+
if end >= len(normalized):
|
|
179
|
+
break
|
|
180
|
+
start = max(end - DOCUMENT_CHUNK_OVERLAP, start + 1)
|
|
181
|
+
return chunks
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
async def _replace_document_chunks(
|
|
185
|
+
db: AsyncIOMotorDatabase,
|
|
186
|
+
document: dict,
|
|
187
|
+
storage_fields: dict,
|
|
188
|
+
extracted_text: str,
|
|
189
|
+
extraction_method: str,
|
|
190
|
+
) -> None:
|
|
191
|
+
document_id = document["document_id"]
|
|
192
|
+
await db[GRAPH_RAG_DOCUMENT_CHUNKS_COLLECTION].delete_many({"document_id": document_id})
|
|
193
|
+
chunks = _chunk_document_text(extracted_text)
|
|
194
|
+
if not chunks:
|
|
195
|
+
return
|
|
196
|
+
now = _utc_now()
|
|
197
|
+
records = []
|
|
198
|
+
for index, chunk in enumerate(chunks):
|
|
199
|
+
record = {
|
|
200
|
+
"document_id": document_id,
|
|
201
|
+
"chunk_id": f"{document_id}-chunk-{index + 1}",
|
|
202
|
+
"filename": storage_fields.get("file_name"),
|
|
203
|
+
"document_name": document.get("document_name"),
|
|
204
|
+
"content_type": storage_fields.get("content_type"),
|
|
205
|
+
"extraction_method": extraction_method,
|
|
206
|
+
"chunk_index": index,
|
|
207
|
+
"text": chunk,
|
|
208
|
+
"created_at": now,
|
|
209
|
+
"updated_at": now,
|
|
210
|
+
}
|
|
211
|
+
record["embedding"] = build_embedding(GRAPH_RAG_DOCUMENT_CHUNKS_COLLECTION, record)
|
|
212
|
+
records.append(record)
|
|
213
|
+
await db[GRAPH_RAG_DOCUMENT_CHUNKS_COLLECTION].insert_many(records)
|
|
214
|
+
await db[GRAPH_RAG_DOCUMENT_CHUNKS_COLLECTION].create_index("document_id")
|
|
215
|
+
await db[GRAPH_RAG_DOCUMENT_CHUNKS_COLLECTION].create_index("chunk_id", unique=True)
|
|
216
|
+
await db[GRAPH_RAG_DOCUMENT_CHUNKS_COLLECTION].create_index("filename")
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
async def _read_upload_bytes_for_embedding(file: UploadFile) -> tuple[bytes, bool]:
|
|
220
|
+
await file.seek(0)
|
|
221
|
+
content = await file.read(DOCUMENT_EMBEDDING_MAX_BYTES + 1)
|
|
222
|
+
await file.seek(0)
|
|
223
|
+
return content[:DOCUMENT_EMBEDDING_MAX_BYTES], len(content) > DOCUMENT_EMBEDDING_MAX_BYTES
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
async def _upsert_document_embedding(
|
|
227
|
+
db: AsyncIOMotorDatabase,
|
|
228
|
+
document: dict,
|
|
229
|
+
file: UploadFile,
|
|
230
|
+
storage_fields: dict,
|
|
231
|
+
) -> None:
|
|
232
|
+
content, bytes_truncated = await _read_upload_bytes_for_embedding(file)
|
|
233
|
+
ocr_model = None
|
|
234
|
+
ocr_pages: list[int] = []
|
|
235
|
+
try:
|
|
236
|
+
extraction = await extract_document_text_with_qwen_ocr(
|
|
237
|
+
storage_fields.get("file_name") or file.filename or "document",
|
|
238
|
+
storage_fields.get("content_type") or file.content_type,
|
|
239
|
+
content,
|
|
240
|
+
)
|
|
241
|
+
extracted_text = extraction.text[:DOCUMENT_EMBEDDING_TEXT_MAX_CHARS]
|
|
242
|
+
extraction_method = extraction.extraction_method
|
|
243
|
+
ocr_model = extraction.ocr_model
|
|
244
|
+
ocr_pages = extraction.ocr_pages
|
|
245
|
+
except DocumentOcrError:
|
|
246
|
+
extracted_text, extraction_method = _extract_upload_text(
|
|
247
|
+
content,
|
|
248
|
+
storage_fields.get("content_type") or file.content_type,
|
|
249
|
+
storage_fields.get("file_name") or file.filename,
|
|
250
|
+
)
|
|
251
|
+
now = _utc_now()
|
|
252
|
+
document_id = document["document_id"]
|
|
253
|
+
source = {
|
|
254
|
+
"document_id": document_id,
|
|
255
|
+
"document_name": document.get("document_name"),
|
|
256
|
+
"document_type": document.get("document_type"),
|
|
257
|
+
"document_version": document.get("document_version"),
|
|
258
|
+
"document_tags": document.get("document_tags"),
|
|
259
|
+
"document_description": document.get("document_description"),
|
|
260
|
+
"file_name": storage_fields.get("file_name"),
|
|
261
|
+
"content_type": storage_fields.get("content_type"),
|
|
262
|
+
"file_size": storage_fields.get("file_size"),
|
|
263
|
+
"extracted_text": extracted_text,
|
|
264
|
+
}
|
|
265
|
+
embedding_record = {
|
|
266
|
+
"embedding_id": f"document-embedding-{document_id}",
|
|
267
|
+
"document_id": document_id,
|
|
268
|
+
"document_name": document.get("document_name"),
|
|
269
|
+
"document_type": document.get("document_type"),
|
|
270
|
+
"document_version": document.get("document_version"),
|
|
271
|
+
"document_url": document.get("document_url"),
|
|
272
|
+
"document_tags": document.get("document_tags") or [],
|
|
273
|
+
"file_name": storage_fields.get("file_name"),
|
|
274
|
+
"content_type": storage_fields.get("content_type"),
|
|
275
|
+
"file_size": storage_fields.get("file_size"),
|
|
276
|
+
"storage_backend": storage_fields.get("storage_backend"),
|
|
277
|
+
"local_path": storage_fields.get("local_path"),
|
|
278
|
+
"s3_bucket": storage_fields.get("s3_bucket"),
|
|
279
|
+
"s3_key": storage_fields.get("s3_key"),
|
|
280
|
+
"source_bytes_sha256": hashlib.sha256(content).hexdigest(),
|
|
281
|
+
"bytes_indexed": len(content),
|
|
282
|
+
"bytes_truncated": bytes_truncated,
|
|
283
|
+
"extraction_method": extraction_method,
|
|
284
|
+
"ocr_model": ocr_model,
|
|
285
|
+
"ocr_pages": ocr_pages,
|
|
286
|
+
"extracted_text": extracted_text,
|
|
287
|
+
"text_truncated": len(extracted_text) >= DOCUMENT_EMBEDDING_TEXT_MAX_CHARS,
|
|
288
|
+
"created_at": now,
|
|
289
|
+
"updated_at": now,
|
|
290
|
+
"embedding": build_embedding(DOCUMENT_EMBEDDINGS_COLLECTION, source),
|
|
291
|
+
}
|
|
292
|
+
await db[DOCUMENT_EMBEDDINGS_COLLECTION].replace_one(
|
|
293
|
+
{"document_id": document_id},
|
|
294
|
+
embedding_record,
|
|
295
|
+
upsert=True,
|
|
296
|
+
)
|
|
297
|
+
await _replace_document_chunks(db, document, storage_fields, extracted_text, extraction_method)
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def _s3_client():
|
|
301
|
+
try:
|
|
302
|
+
import boto3
|
|
303
|
+
except ImportError as exc:
|
|
304
|
+
raise RuntimeError("boto3 is not installed") from exc
|
|
305
|
+
return boto3.client("s3", region_name=S3_REGION, endpoint_url=S3_ENDPOINT_URL)
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def generate_s3_view_url(doc: dict, expires_in: int = 3600, *, as_attachment: bool = False) -> Optional[str]:
|
|
309
|
+
bucket = doc.get("s3_bucket") or S3_BUCKET
|
|
310
|
+
key = doc.get("s3_key")
|
|
311
|
+
document_url = doc.get("document_url")
|
|
312
|
+
if not key and isinstance(document_url, str):
|
|
313
|
+
if document_url.startswith("s3://"):
|
|
314
|
+
_, _, bucket_and_key = document_url.partition("s3://")
|
|
315
|
+
parsed_bucket, _, parsed_key = bucket_and_key.partition("/")
|
|
316
|
+
bucket = bucket or parsed_bucket
|
|
317
|
+
key = parsed_key
|
|
318
|
+
elif not document_url.startswith(("http://", "https://", "file://", "/")):
|
|
319
|
+
key = document_url
|
|
320
|
+
if not bucket or not key:
|
|
321
|
+
return None
|
|
322
|
+
try:
|
|
323
|
+
params = {"Bucket": bucket, "Key": key}
|
|
324
|
+
if as_attachment:
|
|
325
|
+
filename = doc.get("file_name") or Path(str(key)).name or doc.get("document_id") or "document"
|
|
326
|
+
safe_name = Path(str(filename)).name.replace('"', "")
|
|
327
|
+
params["ResponseContentDisposition"] = f'attachment; filename="{safe_name}"'
|
|
328
|
+
else:
|
|
329
|
+
params["ResponseContentDisposition"] = "inline"
|
|
330
|
+
return _s3_client().generate_presigned_url("get_object", Params=params, ExpiresIn=expires_in)
|
|
331
|
+
except Exception:
|
|
332
|
+
return doc.get("s3_url")
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
async def _save_locally(file: UploadFile, document_id: str) -> dict:
|
|
336
|
+
LOCAL_UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
|
337
|
+
safe_name = _safe_filename(file.filename or f"{document_id}.bin")
|
|
338
|
+
local_path = LOCAL_UPLOAD_DIR / f"{document_id}-{safe_name}"
|
|
339
|
+
size = 0
|
|
340
|
+
with local_path.open("wb") as output:
|
|
341
|
+
while True:
|
|
342
|
+
chunk = await file.read(1024 * 1024)
|
|
343
|
+
if not chunk:
|
|
344
|
+
break
|
|
345
|
+
size += len(chunk)
|
|
346
|
+
output.write(chunk)
|
|
347
|
+
return {
|
|
348
|
+
"storage_backend": "local",
|
|
349
|
+
"file_name": safe_name,
|
|
350
|
+
"content_type": file.content_type,
|
|
351
|
+
"file_size": size,
|
|
352
|
+
"local_path": str(local_path),
|
|
353
|
+
"s3_bucket": None,
|
|
354
|
+
"s3_key": None,
|
|
355
|
+
"s3_url": None,
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
async def store_upload(file: UploadFile, document_id: str) -> dict:
|
|
360
|
+
safe_name = _safe_filename(file.filename or f"{document_id}.bin")
|
|
361
|
+
s3_error: Optional[str] = None
|
|
362
|
+
|
|
363
|
+
if S3_BUCKET:
|
|
364
|
+
key = f"{S3_PREFIX}/{document_id}/{safe_name}" if S3_PREFIX else f"{document_id}/{safe_name}"
|
|
365
|
+
try:
|
|
366
|
+
await file.seek(0)
|
|
367
|
+
client = _s3_client()
|
|
368
|
+
extra_args = {}
|
|
369
|
+
if file.content_type:
|
|
370
|
+
extra_args["ContentType"] = file.content_type
|
|
371
|
+
if extra_args:
|
|
372
|
+
client.upload_fileobj(file.file, S3_BUCKET, key, ExtraArgs=extra_args)
|
|
373
|
+
else:
|
|
374
|
+
client.upload_fileobj(file.file, S3_BUCKET, key)
|
|
375
|
+
await file.seek(0)
|
|
376
|
+
contents = await file.read()
|
|
377
|
+
s3_url = (
|
|
378
|
+
f"{S3_PUBLIC_BASE_URL.rstrip('/')}/{key}"
|
|
379
|
+
if S3_PUBLIC_BASE_URL
|
|
380
|
+
else f"s3://{S3_BUCKET}/{key}"
|
|
381
|
+
)
|
|
382
|
+
return {
|
|
383
|
+
"storage_backend": "s3",
|
|
384
|
+
"file_name": safe_name,
|
|
385
|
+
"content_type": file.content_type,
|
|
386
|
+
"file_size": len(contents),
|
|
387
|
+
"s3_bucket": S3_BUCKET,
|
|
388
|
+
"s3_key": key,
|
|
389
|
+
"s3_url": s3_url,
|
|
390
|
+
"local_path": None,
|
|
391
|
+
"storage_error": None,
|
|
392
|
+
}
|
|
393
|
+
except Exception as exc:
|
|
394
|
+
s3_error = str(exc)
|
|
395
|
+
|
|
396
|
+
await file.seek(0)
|
|
397
|
+
local = await _save_locally(file, document_id)
|
|
398
|
+
local["storage_error"] = s3_error
|
|
399
|
+
return local
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
async def get_all(
|
|
403
|
+
db: AsyncIOMotorDatabase,
|
|
404
|
+
page: int = 1,
|
|
405
|
+
page_size: int = 20,
|
|
406
|
+
) -> tuple[list[dict], int]:
|
|
407
|
+
query: dict = {}
|
|
408
|
+
total = await db[COLLECTION].count_documents(query)
|
|
409
|
+
cursor = db[COLLECTION].find(query).skip((page - 1) * page_size).limit(page_size)
|
|
410
|
+
return [_serialize(doc) async for doc in cursor], total
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
async def count(
|
|
414
|
+
db: AsyncIOMotorDatabase,
|
|
415
|
+
*,
|
|
416
|
+
document_type: Optional[str] = None,
|
|
417
|
+
document_owner: Optional[str] = None,
|
|
418
|
+
document_department: Optional[str] = None,
|
|
419
|
+
document_project: Optional[str] = None,
|
|
420
|
+
document_status: Optional[str] = None,
|
|
421
|
+
tag: Optional[str] = None,
|
|
422
|
+
) -> int:
|
|
423
|
+
query: dict = {}
|
|
424
|
+
if document_type:
|
|
425
|
+
query["document_type"] = document_type
|
|
426
|
+
if document_owner:
|
|
427
|
+
query["document_owner"] = document_owner
|
|
428
|
+
if document_department:
|
|
429
|
+
query["document_department"] = document_department
|
|
430
|
+
if document_project:
|
|
431
|
+
query["document_project"] = document_project
|
|
432
|
+
if document_status:
|
|
433
|
+
query["document_status"] = document_status
|
|
434
|
+
if tag:
|
|
435
|
+
query["document_tags"] = tag
|
|
436
|
+
return await db[COLLECTION].count_documents(query)
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
async def get_by_id(db: AsyncIOMotorDatabase, document_id: str) -> Optional[dict]:
|
|
440
|
+
return _serialize(await db[COLLECTION].find_one({"document_id": document_id}))
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
async def get_by_type(db: AsyncIOMotorDatabase, document_type: str) -> list[dict]:
|
|
444
|
+
cursor = db[COLLECTION].find({"document_type": document_type})
|
|
445
|
+
return [_serialize(doc) async for doc in cursor]
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
async def get_by_owner(db: AsyncIOMotorDatabase, document_owner: str) -> list[dict]:
|
|
449
|
+
cursor = db[COLLECTION].find({"document_owner": document_owner})
|
|
450
|
+
return [_serialize(doc) async for doc in cursor]
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
async def get_by_department(db: AsyncIOMotorDatabase, document_department: str) -> list[dict]:
|
|
454
|
+
cursor = db[COLLECTION].find({"document_department": document_department})
|
|
455
|
+
return [_serialize(doc) async for doc in cursor]
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
async def get_by_project(db: AsyncIOMotorDatabase, document_project: str) -> list[dict]:
|
|
459
|
+
cursor = db[COLLECTION].find({"document_project": document_project})
|
|
460
|
+
return [_serialize(doc) async for doc in cursor]
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
async def get_by_tag(db: AsyncIOMotorDatabase, tag: str) -> list[dict]:
|
|
464
|
+
cursor = db[COLLECTION].find({"document_tags": tag})
|
|
465
|
+
return [_serialize(doc) async for doc in cursor]
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
async def get_by_status(db: AsyncIOMotorDatabase, document_status: str) -> list[dict]:
|
|
469
|
+
cursor = db[COLLECTION].find({"document_status": document_status})
|
|
470
|
+
return [_serialize(doc) async for doc in cursor]
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
async def create(db: AsyncIOMotorDatabase, data: DocumentMetadataCreate) -> dict:
|
|
474
|
+
now = _utc_now()
|
|
475
|
+
payload = data.model_dump(mode="python")
|
|
476
|
+
document_id = payload.get("document_id") or f"document-{uuid4().hex[:12]}"
|
|
477
|
+
doc = _as_response_doc(
|
|
478
|
+
{
|
|
479
|
+
**payload,
|
|
480
|
+
"document_id": document_id,
|
|
481
|
+
"created_at": payload.get("created_at") or now,
|
|
482
|
+
"last_modified_at": payload.get("last_modified_at") or now,
|
|
483
|
+
}
|
|
484
|
+
)
|
|
485
|
+
await db[COLLECTION].insert_one(doc)
|
|
486
|
+
return _serialize(doc)
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
async def create_with_file(
|
|
490
|
+
db: AsyncIOMotorDatabase,
|
|
491
|
+
data: DocumentMetadataCreate,
|
|
492
|
+
file: UploadFile,
|
|
493
|
+
) -> dict:
|
|
494
|
+
now = _utc_now()
|
|
495
|
+
payload = data.model_dump(mode="python")
|
|
496
|
+
document_id = payload.get("document_id") or f"document-{uuid4().hex[:12]}"
|
|
497
|
+
storage_fields = await store_upload(file, document_id)
|
|
498
|
+
doc = _as_response_doc(
|
|
499
|
+
{
|
|
500
|
+
**payload,
|
|
501
|
+
**storage_fields,
|
|
502
|
+
"document_id": document_id,
|
|
503
|
+
"document_url": _document_url_from_storage(document_id, storage_fields),
|
|
504
|
+
"created_at": payload.get("created_at") or now,
|
|
505
|
+
"last_modified_at": payload.get("last_modified_at") or now,
|
|
506
|
+
}
|
|
507
|
+
)
|
|
508
|
+
await db[COLLECTION].insert_one(doc)
|
|
509
|
+
await _upsert_document_embedding(db, doc, file, storage_fields)
|
|
510
|
+
return _serialize(doc)
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
async def attach_file(
|
|
514
|
+
db: AsyncIOMotorDatabase,
|
|
515
|
+
document_id: str,
|
|
516
|
+
file: UploadFile,
|
|
517
|
+
) -> Optional[dict]:
|
|
518
|
+
existing = await get_by_id(db, document_id)
|
|
519
|
+
if not existing:
|
|
520
|
+
return None
|
|
521
|
+
storage_fields = await store_upload(file, document_id)
|
|
522
|
+
storage_document_url = _document_url_from_storage(document_id, storage_fields)
|
|
523
|
+
doc = _as_response_doc(
|
|
524
|
+
{
|
|
525
|
+
**existing,
|
|
526
|
+
**storage_fields,
|
|
527
|
+
"document_id": document_id,
|
|
528
|
+
"document_url": storage_document_url,
|
|
529
|
+
"last_modified_at": _utc_now(),
|
|
530
|
+
}
|
|
531
|
+
)
|
|
532
|
+
result = await db[COLLECTION].find_one_and_update(
|
|
533
|
+
{"document_id": document_id},
|
|
534
|
+
{"$set": doc},
|
|
535
|
+
return_document=True,
|
|
536
|
+
)
|
|
537
|
+
await _upsert_document_embedding(db, doc, file, storage_fields)
|
|
538
|
+
return _serialize(result)
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
async def update(
|
|
542
|
+
db: AsyncIOMotorDatabase,
|
|
543
|
+
document_id: str,
|
|
544
|
+
data: DocumentMetadataUpdate,
|
|
545
|
+
) -> Optional[dict]:
|
|
546
|
+
existing = await get_by_id(db, document_id)
|
|
547
|
+
if not existing:
|
|
548
|
+
return None
|
|
549
|
+
fields = data.model_dump(mode="python", exclude_unset=True)
|
|
550
|
+
if not fields:
|
|
551
|
+
return existing
|
|
552
|
+
merged = {
|
|
553
|
+
**existing,
|
|
554
|
+
**fields,
|
|
555
|
+
"document_id": document_id,
|
|
556
|
+
"last_modified_at": fields.get("last_modified_at") or _utc_now(),
|
|
557
|
+
}
|
|
558
|
+
doc = _as_response_doc(merged)
|
|
559
|
+
result = await db[COLLECTION].find_one_and_update(
|
|
560
|
+
{"document_id": document_id},
|
|
561
|
+
{"$set": doc},
|
|
562
|
+
return_document=True,
|
|
563
|
+
)
|
|
564
|
+
return _serialize(result)
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
async def delete(db: AsyncIOMotorDatabase, document_id: str) -> bool:
|
|
568
|
+
result = await db[COLLECTION].delete_one({"document_id": document_id})
|
|
569
|
+
return result.deleted_count == 1
|