@arela/uploader 1.1.3 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/.claude/worktrees/agent-multi-profile/.env.template +224 -0
  2. package/.claude/worktrees/agent-multi-profile/.prettierrc +13 -0
  3. package/.claude/worktrees/agent-multi-profile/README.md +405 -0
  4. package/.claude/worktrees/agent-multi-profile/package-lock.json +7096 -0
  5. package/.claude/worktrees/agent-multi-profile/package.json +78 -0
  6. package/.claude/worktrees/agent-multi-profile/scripts/cleanup-ds-store.js +109 -0
  7. package/.claude/worktrees/agent-multi-profile/scripts/cleanup-system-files.js +69 -0
  8. package/.claude/worktrees/agent-multi-profile/scripts/scoring-compare.js +243 -0
  9. package/.claude/worktrees/agent-multi-profile/scripts/scoring-phase4-check.js +96 -0
  10. package/.claude/worktrees/agent-multi-profile/scripts/tests/phase-7-features.test.js +415 -0
  11. package/.claude/worktrees/agent-multi-profile/scripts/tests/signal-handling.test.js +275 -0
  12. package/.claude/worktrees/agent-multi-profile/scripts/tests/smart-watch-integration.test.js +554 -0
  13. package/.claude/worktrees/agent-multi-profile/scripts/tests/watch-service-integration.test.js +584 -0
  14. package/.claude/worktrees/agent-multi-profile/src/commands/AgentCommand.js +229 -0
  15. package/.claude/worktrees/agent-multi-profile/src/commands/AgentInitCommand.js +316 -0
  16. package/.claude/worktrees/agent-multi-profile/src/commands/DatastageCommand.js +164 -0
  17. package/.claude/worktrees/agent-multi-profile/src/commands/GDriveSyncCommand.js +475 -0
  18. package/.claude/worktrees/agent-multi-profile/src/commands/IdentifyCommand.js +708 -0
  19. package/.claude/worktrees/agent-multi-profile/src/commands/PollWorkerCommand.js +169 -0
  20. package/.claude/worktrees/agent-multi-profile/src/commands/PropagateCommand.js +636 -0
  21. package/.claude/worktrees/agent-multi-profile/src/commands/PushCommand.js +743 -0
  22. package/.claude/worktrees/agent-multi-profile/src/commands/ScanCommand.js +722 -0
  23. package/.claude/worktrees/agent-multi-profile/src/commands/UploadCommand.js +587 -0
  24. package/.claude/worktrees/agent-multi-profile/src/commands/WatchCommand.js +1342 -0
  25. package/.claude/worktrees/agent-multi-profile/src/commands/WorkerCommand.js +337 -0
  26. package/.claude/worktrees/agent-multi-profile/src/config/config.js +862 -0
  27. package/.claude/worktrees/agent-multi-profile/src/document-type-shared.js +131 -0
  28. package/.claude/worktrees/agent-multi-profile/src/document-types/_pedimento-shared-extractors.js +348 -0
  29. package/.claude/worktrees/agent-multi-profile/src/document-types/doda-pdf.js +121 -0
  30. package/.claude/worktrees/agent-multi-profile/src/document-types/doda-xml.js +118 -0
  31. package/.claude/worktrees/agent-multi-profile/src/document-types/factura-inter-agencia.js +186 -0
  32. package/.claude/worktrees/agent-multi-profile/src/document-types/facturas-comerciales.js +233 -0
  33. package/.claude/worktrees/agent-multi-profile/src/document-types/pedimento-completo-xml.js +372 -0
  34. package/.claude/worktrees/agent-multi-profile/src/document-types/pedimento-completo.js +108 -0
  35. package/.claude/worktrees/agent-multi-profile/src/document-types/pedimento-simplificado.js +76 -0
  36. package/.claude/worktrees/agent-multi-profile/src/document-types/proforma.js +29 -0
  37. package/.claude/worktrees/agent-multi-profile/src/document-types/support-document.js +200 -0
  38. package/.claude/worktrees/agent-multi-profile/src/errors/ErrorHandler.js +278 -0
  39. package/.claude/worktrees/agent-multi-profile/src/errors/ErrorTypes.js +104 -0
  40. package/.claude/worktrees/agent-multi-profile/src/file-detection.js +338 -0
  41. package/.claude/worktrees/agent-multi-profile/src/index.js +890 -0
  42. package/.claude/worktrees/agent-multi-profile/src/scoring/db-matcher-adapter.js +98 -0
  43. package/.claude/worktrees/agent-multi-profile/src/scoring/matchers-seed.js +386 -0
  44. package/.claude/worktrees/agent-multi-profile/src/scoring/scoring-engine.js +251 -0
  45. package/.claude/worktrees/agent-multi-profile/src/services/AdvancedFilterService.js +505 -0
  46. package/.claude/worktrees/agent-multi-profile/src/services/AutoProcessingService.js +749 -0
  47. package/.claude/worktrees/agent-multi-profile/src/services/BenchmarkingService.js +381 -0
  48. package/.claude/worktrees/agent-multi-profile/src/services/DatabaseService.js +2173 -0
  49. package/.claude/worktrees/agent-multi-profile/src/services/DatastageApiService.js +240 -0
  50. package/.claude/worktrees/agent-multi-profile/src/services/ErrorMonitor.js +275 -0
  51. package/.claude/worktrees/agent-multi-profile/src/services/GoogleDriveService.js +217 -0
  52. package/.claude/worktrees/agent-multi-profile/src/services/LoggingService.js +649 -0
  53. package/.claude/worktrees/agent-multi-profile/src/services/MonitoringService.js +401 -0
  54. package/.claude/worktrees/agent-multi-profile/src/services/PerformanceOptimizer.js +511 -0
  55. package/.claude/worktrees/agent-multi-profile/src/services/PipelineApiService.js +274 -0
  56. package/.claude/worktrees/agent-multi-profile/src/services/PipelineJobRunner.js +389 -0
  57. package/.claude/worktrees/agent-multi-profile/src/services/ProfileManager.js +164 -0
  58. package/.claude/worktrees/agent-multi-profile/src/services/ReportingService.js +511 -0
  59. package/.claude/worktrees/agent-multi-profile/src/services/ScanApiService.js +775 -0
  60. package/.claude/worktrees/agent-multi-profile/src/services/SignalHandler.js +255 -0
  61. package/.claude/worktrees/agent-multi-profile/src/services/SmartWatchDatabaseService.js +527 -0
  62. package/.claude/worktrees/agent-multi-profile/src/services/WatchService.js +783 -0
  63. package/.claude/worktrees/agent-multi-profile/src/services/upload/ApiUploadService.js +676 -0
  64. package/.claude/worktrees/agent-multi-profile/src/services/upload/BaseUploadService.js +36 -0
  65. package/.claude/worktrees/agent-multi-profile/src/services/upload/MultiApiUploadService.js +233 -0
  66. package/.claude/worktrees/agent-multi-profile/src/services/upload/SupabaseUploadService.js +148 -0
  67. package/.claude/worktrees/agent-multi-profile/src/services/upload/UploadServiceFactory.js +100 -0
  68. package/.claude/worktrees/agent-multi-profile/src/utils/CleanupManager.js +262 -0
  69. package/.claude/worktrees/agent-multi-profile/src/utils/FileOperations.js +192 -0
  70. package/.claude/worktrees/agent-multi-profile/src/utils/FileSanitizer.js +99 -0
  71. package/.claude/worktrees/agent-multi-profile/src/utils/PathDetector.js +198 -0
  72. package/.claude/worktrees/agent-multi-profile/src/utils/PathNormalizer.js +274 -0
  73. package/.claude/worktrees/agent-multi-profile/src/utils/WatchEventHandler.js +522 -0
  74. package/.claude/worktrees/agent-multi-profile/supabase/migrations/001_create_initial_schema.sql +366 -0
  75. package/.claude/worktrees/agent-multi-profile/supabase/migrations/002_align_with_arela_api_schema.sql +145 -0
  76. package/.claude/worktrees/agent-multi-profile/tests/commands/IdentifyCommand.test.js +570 -0
  77. package/.claude/worktrees/agent-multi-profile/tests/commands/PropagateCommand.test.js +568 -0
  78. package/.claude/worktrees/agent-multi-profile/tests/commands/PushCommand.test.js +754 -0
  79. package/.claude/worktrees/agent-multi-profile/tests/commands/ScanCommand.test.js +382 -0
  80. package/.claude/worktrees/agent-multi-profile/tests/unit/PathAndTableNameGeneration.test.js +1211 -0
  81. package/.claude/worktrees/agent-multi-profile/tests/unit/factura-inter-agencia.test.js +218 -0
  82. package/.claude/worktrees/agent-multi-profile/tests/unit/pedimento-completo-xml-matcher.test.js +271 -0
  83. package/.claude/worktrees/agent-multi-profile/tests/unit/pedimento-simplificado-matcher.test.js +185 -0
  84. package/.claude/worktrees/agent-multi-profile/tests/unit/scoring-engine.test.js +221 -0
  85. package/README.md +65 -0
  86. package/package.json +1 -1
  87. package/src/commands/AgentCommand.js +210 -0
  88. package/src/commands/AgentInitCommand.js +316 -0
  89. package/src/commands/PollWorkerCommand.js +11 -322
  90. package/src/config/config.js +44 -6
  91. package/src/document-type-shared.js +1 -1
  92. package/src/document-types/pedimento-completo.js +8 -1
  93. package/src/file-detection.js +9 -0
  94. package/src/index.js +55 -0
  95. package/src/scoring/scoring-engine.js +1 -1
  96. package/src/services/LoggingService.js +35 -0
  97. package/src/services/PipelineApiService.js +7 -1
  98. package/src/services/PipelineJobRunner.js +389 -0
  99. package/src/services/ProfileManager.js +164 -0
@@ -0,0 +1,169 @@
1
+ import logger from '../services/LoggingService.js';
2
+ import { PipelineApiService } from '../services/PipelineApiService.js';
3
+ import { PipelineJobRunner } from '../services/PipelineJobRunner.js';
4
+
5
+ import appConfig from '../config/config.js';
6
+ import ErrorHandler from '../errors/ErrorHandler.js';
7
+
8
+ /**
9
+ * Poll Worker Command Handler
10
+ * Runs arela-uploader as an HTTP-polling worker, fetching jobs from the API.
11
+ * Alternative to BullMQ/Redis-based worker for environments without Redis access.
12
+ * Job execution lives in PipelineJobRunner (shared with `arela agent`).
13
+ *
14
+ * Usage:
15
+ * arela worker --poll
16
+ * arela worker --poll --interval 10000
17
+ *
18
+ * Environment variables:
19
+ * WORKER_POLL_INTERVAL - Polling interval in ms (default: 5000)
20
+ * WORKER_POLL_API_TARGET - API target for polling (default: 'agencia')
21
+ * ARELA_SERVER_ID - Server identifier for job assignment
22
+ */
23
+ export class PollWorkerCommand {
24
+ constructor() {
25
+ this.errorHandler = new ErrorHandler(logger);
26
+ this.isShuttingDown = false;
27
+ this.currentJob = null;
28
+ this.pipelineApi = null;
29
+ this.jobRunner = null;
30
+ this.pollTimer = null;
31
+ }
32
+
33
+ /**
34
+ * Execute the poll worker command
35
+ * @param {Object} options - Command options
36
+ * @param {number} options.interval - Polling interval in milliseconds
37
+ * @param {string} options.api - API target for polling
38
+ */
39
+ async execute(options = {}) {
40
+ try {
41
+ const workerConfig = appConfig.getWorkerConfig();
42
+ const pollConfig = workerConfig.poll;
43
+
44
+ // Get configuration
45
+ const interval = parseInt(options.interval) || pollConfig.interval;
46
+ const apiTarget = options.api || pollConfig.apiTarget;
47
+ const serverId = appConfig.getServerId();
48
+
49
+ if (!serverId) {
50
+ throw new Error(
51
+ 'ARELA_SERVER_ID is required for poll mode. Set it in your .env file.',
52
+ );
53
+ }
54
+
55
+ this.serverId = serverId;
56
+
57
+ // Initialize Pipeline API service and the shared job runner
58
+ this.pipelineApi = new PipelineApiService(apiTarget);
59
+ this.jobRunner = new PipelineJobRunner(this.pipelineApi, serverId);
60
+
61
+ console.log('\nšŸ”§ Starting Arela Poll Worker');
62
+ console.log(`šŸ“” API Target: ${apiTarget}`);
63
+ console.log(`šŸ–„ļø Server ID: ${serverId}`);
64
+ console.log(`ā±ļø Poll Interval: ${interval}ms`);
65
+
66
+ logger.info('šŸ”§ Starting Arela Poll Worker');
67
+ logger.info(`šŸ“” API Target: ${apiTarget}`);
68
+ logger.info(`šŸ–„ļø Server ID: ${serverId}`);
69
+ logger.info(`ā±ļø Poll Interval: ${interval}ms`);
70
+
71
+ // Setup graceful shutdown handlers
72
+ this.#setupShutdownHandlers();
73
+
74
+ // Start polling loop
75
+ await this.#startPolling(serverId, interval);
76
+ } catch (error) {
77
+ logger.error('āŒ Poll Worker failed to start:', error.message);
78
+ throw error;
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Start the polling loop
84
+ * @private
85
+ */
86
+ async #startPolling(serverId, interval) {
87
+ console.log('\nāœ… Poll Worker is running. Press Ctrl+C to stop.\n');
88
+ logger.success('Poll Worker is running');
89
+
90
+ while (!this.isShuttingDown) {
91
+ try {
92
+ // Check for next job
93
+ const job = await this.pipelineApi.getNextJob(serverId);
94
+
95
+ if (job && job.id) {
96
+ console.log(`šŸ“„ Got job: ${job.type} for RFC ${job.rfc}`);
97
+ logger.info(`šŸ“„ Got job: ${job.id} - ${job.type} for RFC ${job.rfc}`);
98
+ this.currentJob = job;
99
+ try {
100
+ await this.jobRunner.processJob(job);
101
+ } finally {
102
+ this.currentJob = null;
103
+ }
104
+ } else {
105
+ // Only log to file, not console (too noisy)
106
+ logger.debug('šŸ“­ No jobs available, waiting...');
107
+ }
108
+ } catch (error) {
109
+ console.error(`āŒ Polling error: ${error.message}`);
110
+ logger.error(`āŒ Polling error: ${error.message}`);
111
+ }
112
+
113
+ // Wait for next poll (unless shutting down)
114
+ if (!this.isShuttingDown) {
115
+ await this.#sleep(interval);
116
+ }
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Sleep for specified milliseconds
122
+ * @private
123
+ */
124
+ #sleep(ms) {
125
+ return new Promise((resolve) => {
126
+ this.pollTimer = setTimeout(resolve, ms);
127
+ });
128
+ }
129
+
130
+ /**
131
+ * Setup graceful shutdown handlers
132
+ * @private
133
+ */
134
+ #setupShutdownHandlers() {
135
+ const shutdown = async (signal) => {
136
+ if (this.isShuttingDown) return;
137
+ this.isShuttingDown = true;
138
+
139
+ logger.info(`\nšŸ‘‹ Received ${signal}. Shutting down poll worker...`);
140
+
141
+ // Clear poll timer
142
+ if (this.pollTimer) {
143
+ clearTimeout(this.pollTimer);
144
+ }
145
+
146
+ // Wait for current job to finish if any
147
+ if (this.currentJob) {
148
+ logger.info(
149
+ ` ā³ Waiting for current job to finish: ${this.currentJob.id}`,
150
+ );
151
+ // Give it a moment to complete
152
+ await this.#sleep(2000);
153
+ }
154
+
155
+ // Cleanup
156
+ if (this.pipelineApi) {
157
+ this.pipelineApi.destroy();
158
+ }
159
+
160
+ logger.info('āœ… Poll worker stopped gracefully');
161
+ process.exit(0);
162
+ };
163
+
164
+ process.on('SIGINT', () => shutdown('SIGINT'));
165
+ process.on('SIGTERM', () => shutdown('SIGTERM'));
166
+ }
167
+ }
168
+
169
+ export default new PollWorkerCommand();