vantuz 3.4.1 → 3.5.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.
- package/LICENSE +45 -45
- package/admin-keygen.js +51 -0
- package/cli.js +685 -585
- package/config.js +733 -733
- package/core/agent-loop.js +190 -190
- package/core/ai-provider.js +298 -261
- package/core/automation.js +523 -523
- package/core/brand-analyst.js +101 -0
- package/core/channels.js +167 -167
- package/core/dashboard.js +230 -230
- package/core/database.js +135 -36
- package/core/eia-monitor.js +3 -1
- package/core/engine.js +648 -636
- package/core/gateway.js +447 -447
- package/core/learning.js +214 -214
- package/core/license.js +113 -0
- package/core/marketplace-adapter.js +168 -168
- package/core/memory.js +190 -190
- package/core/migrations/001-initial-schema.sql +1 -1
- package/core/queue.js +120 -120
- package/core/self-healer.js +314 -314
- package/core/unified-product.js +214 -214
- package/core/vision-service.js +113 -113
- package/index.js +217 -174
- package/modules/crm/sentiment-crm.js +231 -231
- package/modules/healer/listing-healer.js +201 -201
- package/modules/oracle/predictor.js +214 -214
- package/modules/researcher/agent.js +169 -169
- package/modules/team/agents/base.js +92 -92
- package/modules/team/agents/dev.js +33 -33
- package/modules/team/agents/josh.js +40 -40
- package/modules/team/agents/marketing.js +33 -33
- package/modules/team/agents/milo.js +36 -36
- package/modules/team/index.js +78 -78
- package/modules/team/shared-memory.js +87 -87
- package/modules/war-room/competitor-tracker.js +250 -250
- package/modules/war-room/pricing-engine.js +308 -308
- package/nodes/warehouse.js +238 -238
- package/onboard.js +1 -1
- package/package.json +7 -6
- package/platforms/pttavm.js +14 -14
- package/plugins/vantuz/index.js +528 -528
- package/plugins/vantuz/memory/hippocampus.js +465 -464
- package/plugins/vantuz/package.json +20 -20
- package/plugins/vantuz/platforms/_template.js +118 -118
- package/plugins/vantuz/platforms/amazon.js +236 -236
- package/plugins/vantuz/platforms/ciceksepeti.js +166 -166
- package/plugins/vantuz/platforms/hepsiburada.js +180 -180
- package/plugins/vantuz/platforms/index.js +165 -165
- package/plugins/vantuz/platforms/n11.js +229 -229
- package/plugins/vantuz/platforms/pazarama.js +154 -154
- package/plugins/vantuz/platforms/pttavm.js +127 -127
- package/plugins/vantuz/platforms/trendyol.js +326 -326
- package/plugins/vantuz/services/alerts.js +253 -253
- package/plugins/vantuz/services/license.js +34 -34
- package/plugins/vantuz/services/scheduler.js +232 -232
- package/plugins/vantuz/tools/analytics.js +152 -152
- package/plugins/vantuz/tools/crossborder.js +187 -187
- package/plugins/vantuz/tools/nl-parser.js +211 -211
- package/plugins/vantuz/tools/product.js +110 -110
- package/plugins/vantuz/tools/quick-report.js +175 -175
- package/plugins/vantuz/tools/repricer.js +314 -314
- package/plugins/vantuz/tools/sentiment.js +115 -115
- package/plugins/vantuz/tools/vision.js +257 -257
- package/private.pem +28 -0
- package/public.pem +9 -0
- package/server/app.js +260 -260
- package/server/public/index.html +514 -514
- package/start.bat +33 -33
- package/vantuz.sqlite +0 -0
package/core/memory.js
CHANGED
|
@@ -1,190 +1,190 @@
|
|
|
1
|
-
// core/memory.js
|
|
2
|
-
// Persistent Memory Module for Vantuz AI
|
|
3
|
-
// Provides long-term fact storage and recall via JSON files.
|
|
4
|
-
|
|
5
|
-
import fs from 'fs';
|
|
6
|
-
import path from 'path';
|
|
7
|
-
import os from 'os';
|
|
8
|
-
import { log } from './ai-provider.js';
|
|
9
|
-
|
|
10
|
-
const MEMORY_DIR = path.join(os.homedir(), '.vantuz', 'memory');
|
|
11
|
-
const FACTS_FILE = path.join(MEMORY_DIR, 'facts.json');
|
|
12
|
-
const STRATEGY_FILE = path.join(MEMORY_DIR, 'strategy.json');
|
|
13
|
-
const MAX_FACTS = 500;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Ensures the memory directory exists.
|
|
17
|
-
*/
|
|
18
|
-
function ensureDir() {
|
|
19
|
-
if (!fs.existsSync(MEMORY_DIR)) {
|
|
20
|
-
fs.mkdirSync(MEMORY_DIR, { recursive: true });
|
|
21
|
-
log('INFO', 'Memory directory created', { path: MEMORY_DIR });
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Loads a JSON file safely.
|
|
27
|
-
*/
|
|
28
|
-
function loadJson(filePath) {
|
|
29
|
-
try {
|
|
30
|
-
if (fs.existsSync(filePath)) {
|
|
31
|
-
return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
|
32
|
-
}
|
|
33
|
-
} catch (e) {
|
|
34
|
-
log('WARN', `Memory file corrupt or unreadable: ${filePath}`, { error: e.message });
|
|
35
|
-
}
|
|
36
|
-
return [];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Saves data to a JSON file atomically.
|
|
41
|
-
*/
|
|
42
|
-
function saveJson(filePath, data) {
|
|
43
|
-
ensureDir();
|
|
44
|
-
const tmp = filePath + '.tmp';
|
|
45
|
-
fs.writeFileSync(tmp, JSON.stringify(data, null, 2), 'utf-8');
|
|
46
|
-
fs.renameSync(tmp, filePath);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
50
|
-
// MEMORY CLASS
|
|
51
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
52
|
-
|
|
53
|
-
class Memory {
|
|
54
|
-
constructor() {
|
|
55
|
-
ensureDir();
|
|
56
|
-
this.facts = loadJson(FACTS_FILE);
|
|
57
|
-
this.strategies = loadJson(STRATEGY_FILE);
|
|
58
|
-
log('INFO', 'Memory loaded', {
|
|
59
|
-
facts: this.facts.length,
|
|
60
|
-
strategies: this.strategies.length
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Remember a fact. Facts are timestamped entries.
|
|
66
|
-
* @param {string} fact - The fact to remember.
|
|
67
|
-
* @param {string} category - Category: 'general', 'customer', 'product', 'strategy'
|
|
68
|
-
*/
|
|
69
|
-
remember(fact, category = 'general') {
|
|
70
|
-
const entry = {
|
|
71
|
-
id: Date.now().toString(36) + Math.random().toString(36).slice(2, 6),
|
|
72
|
-
fact,
|
|
73
|
-
category,
|
|
74
|
-
createdAt: new Date().toISOString()
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
this.facts.push(entry);
|
|
78
|
-
|
|
79
|
-
// Evict oldest if over limit
|
|
80
|
-
if (this.facts.length > MAX_FACTS) {
|
|
81
|
-
this.facts = this.facts.slice(-MAX_FACTS);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
saveJson(FACTS_FILE, this.facts);
|
|
85
|
-
log('INFO', 'Fact remembered', { id: entry.id, category });
|
|
86
|
-
return entry;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Recall facts matching a keyword query.
|
|
91
|
-
* Simple substring search (can be upgraded to vector later).
|
|
92
|
-
* @param {string} query - Search query.
|
|
93
|
-
* @param {number} limit - Max results.
|
|
94
|
-
* @returns {Array} Matching facts.
|
|
95
|
-
*/
|
|
96
|
-
recall(query, limit = 10) {
|
|
97
|
-
const q = query.toLowerCase();
|
|
98
|
-
const matches = this.facts.filter(f =>
|
|
99
|
-
f.fact.toLowerCase().includes(q) ||
|
|
100
|
-
f.category.toLowerCase().includes(q)
|
|
101
|
-
);
|
|
102
|
-
return matches.slice(-limit);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Save a strategic decision or rule.
|
|
107
|
-
* @param {string} rule - The strategy rule.
|
|
108
|
-
* @param {string} context - Why this decision was made.
|
|
109
|
-
*/
|
|
110
|
-
addStrategy(rule, context = '') {
|
|
111
|
-
const entry = {
|
|
112
|
-
id: Date.now().toString(36),
|
|
113
|
-
rule,
|
|
114
|
-
context,
|
|
115
|
-
createdAt: new Date().toISOString()
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
this.strategies.push(entry);
|
|
119
|
-
saveJson(STRATEGY_FILE, this.strategies);
|
|
120
|
-
log('INFO', 'Strategy recorded', { id: entry.id });
|
|
121
|
-
return entry;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Get all strategies.
|
|
126
|
-
*/
|
|
127
|
-
getStrategies() {
|
|
128
|
-
return this.strategies;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Get recent facts for context injection.
|
|
133
|
-
* @param {number} count - How many recent facts.
|
|
134
|
-
*/
|
|
135
|
-
getRecentFacts(count = 20) {
|
|
136
|
-
return this.facts.slice(-count);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Get a summary string for AI injection.
|
|
141
|
-
*/
|
|
142
|
-
getContextSummary() {
|
|
143
|
-
const recentFacts = this.getRecentFacts(10);
|
|
144
|
-
const strategies = this.getStrategies();
|
|
145
|
-
|
|
146
|
-
let summary = '';
|
|
147
|
-
|
|
148
|
-
if (strategies.length > 0) {
|
|
149
|
-
summary += '\n--- STRATEJİK KURALLAR ---\n';
|
|
150
|
-
strategies.forEach(s => {
|
|
151
|
-
summary += `- ${s.rule}\n`;
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
if (recentFacts.length > 0) {
|
|
156
|
-
summary += '\n--- SON HATIRALAR ---\n';
|
|
157
|
-
recentFacts.forEach(f => {
|
|
158
|
-
summary += `- [${f.category}] ${f.fact}\n`;
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return summary;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Full reset (dangerous).
|
|
167
|
-
*/
|
|
168
|
-
clear() {
|
|
169
|
-
this.facts = [];
|
|
170
|
-
this.strategies = [];
|
|
171
|
-
saveJson(FACTS_FILE, this.facts);
|
|
172
|
-
saveJson(STRATEGY_FILE, this.strategies);
|
|
173
|
-
log('WARN', 'Memory cleared');
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
178
|
-
// SINGLETON
|
|
179
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
180
|
-
|
|
181
|
-
let memoryInstance = null;
|
|
182
|
-
|
|
183
|
-
export function getMemory() {
|
|
184
|
-
if (!memoryInstance) {
|
|
185
|
-
memoryInstance = new Memory();
|
|
186
|
-
}
|
|
187
|
-
return memoryInstance;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
export default Memory;
|
|
1
|
+
// core/memory.js
|
|
2
|
+
// Persistent Memory Module for Vantuz AI
|
|
3
|
+
// Provides long-term fact storage and recall via JSON files.
|
|
4
|
+
|
|
5
|
+
import fs from 'fs';
|
|
6
|
+
import path from 'path';
|
|
7
|
+
import os from 'os';
|
|
8
|
+
import { log } from './ai-provider.js';
|
|
9
|
+
|
|
10
|
+
const MEMORY_DIR = path.join(os.homedir(), '.vantuz', 'memory');
|
|
11
|
+
const FACTS_FILE = path.join(MEMORY_DIR, 'facts.json');
|
|
12
|
+
const STRATEGY_FILE = path.join(MEMORY_DIR, 'strategy.json');
|
|
13
|
+
const MAX_FACTS = 500;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Ensures the memory directory exists.
|
|
17
|
+
*/
|
|
18
|
+
function ensureDir() {
|
|
19
|
+
if (!fs.existsSync(MEMORY_DIR)) {
|
|
20
|
+
fs.mkdirSync(MEMORY_DIR, { recursive: true });
|
|
21
|
+
log('INFO', 'Memory directory created', { path: MEMORY_DIR });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Loads a JSON file safely.
|
|
27
|
+
*/
|
|
28
|
+
function loadJson(filePath) {
|
|
29
|
+
try {
|
|
30
|
+
if (fs.existsSync(filePath)) {
|
|
31
|
+
return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
|
32
|
+
}
|
|
33
|
+
} catch (e) {
|
|
34
|
+
log('WARN', `Memory file corrupt or unreadable: ${filePath}`, { error: e.message });
|
|
35
|
+
}
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Saves data to a JSON file atomically.
|
|
41
|
+
*/
|
|
42
|
+
function saveJson(filePath, data) {
|
|
43
|
+
ensureDir();
|
|
44
|
+
const tmp = filePath + '.tmp';
|
|
45
|
+
fs.writeFileSync(tmp, JSON.stringify(data, null, 2), 'utf-8');
|
|
46
|
+
fs.renameSync(tmp, filePath);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
50
|
+
// MEMORY CLASS
|
|
51
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
52
|
+
|
|
53
|
+
class Memory {
|
|
54
|
+
constructor() {
|
|
55
|
+
ensureDir();
|
|
56
|
+
this.facts = loadJson(FACTS_FILE);
|
|
57
|
+
this.strategies = loadJson(STRATEGY_FILE);
|
|
58
|
+
log('INFO', 'Memory loaded', {
|
|
59
|
+
facts: this.facts.length,
|
|
60
|
+
strategies: this.strategies.length
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Remember a fact. Facts are timestamped entries.
|
|
66
|
+
* @param {string} fact - The fact to remember.
|
|
67
|
+
* @param {string} category - Category: 'general', 'customer', 'product', 'strategy'
|
|
68
|
+
*/
|
|
69
|
+
remember(fact, category = 'general') {
|
|
70
|
+
const entry = {
|
|
71
|
+
id: Date.now().toString(36) + Math.random().toString(36).slice(2, 6),
|
|
72
|
+
fact,
|
|
73
|
+
category,
|
|
74
|
+
createdAt: new Date().toISOString()
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
this.facts.push(entry);
|
|
78
|
+
|
|
79
|
+
// Evict oldest if over limit
|
|
80
|
+
if (this.facts.length > MAX_FACTS) {
|
|
81
|
+
this.facts = this.facts.slice(-MAX_FACTS);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
saveJson(FACTS_FILE, this.facts);
|
|
85
|
+
log('INFO', 'Fact remembered', { id: entry.id, category });
|
|
86
|
+
return entry;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Recall facts matching a keyword query.
|
|
91
|
+
* Simple substring search (can be upgraded to vector later).
|
|
92
|
+
* @param {string} query - Search query.
|
|
93
|
+
* @param {number} limit - Max results.
|
|
94
|
+
* @returns {Array} Matching facts.
|
|
95
|
+
*/
|
|
96
|
+
recall(query, limit = 10) {
|
|
97
|
+
const q = query.toLowerCase();
|
|
98
|
+
const matches = this.facts.filter(f =>
|
|
99
|
+
f.fact.toLowerCase().includes(q) ||
|
|
100
|
+
f.category.toLowerCase().includes(q)
|
|
101
|
+
);
|
|
102
|
+
return matches.slice(-limit);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Save a strategic decision or rule.
|
|
107
|
+
* @param {string} rule - The strategy rule.
|
|
108
|
+
* @param {string} context - Why this decision was made.
|
|
109
|
+
*/
|
|
110
|
+
addStrategy(rule, context = '') {
|
|
111
|
+
const entry = {
|
|
112
|
+
id: Date.now().toString(36),
|
|
113
|
+
rule,
|
|
114
|
+
context,
|
|
115
|
+
createdAt: new Date().toISOString()
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
this.strategies.push(entry);
|
|
119
|
+
saveJson(STRATEGY_FILE, this.strategies);
|
|
120
|
+
log('INFO', 'Strategy recorded', { id: entry.id });
|
|
121
|
+
return entry;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Get all strategies.
|
|
126
|
+
*/
|
|
127
|
+
getStrategies() {
|
|
128
|
+
return this.strategies;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Get recent facts for context injection.
|
|
133
|
+
* @param {number} count - How many recent facts.
|
|
134
|
+
*/
|
|
135
|
+
getRecentFacts(count = 20) {
|
|
136
|
+
return this.facts.slice(-count);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Get a summary string for AI injection.
|
|
141
|
+
*/
|
|
142
|
+
getContextSummary() {
|
|
143
|
+
const recentFacts = this.getRecentFacts(10);
|
|
144
|
+
const strategies = this.getStrategies();
|
|
145
|
+
|
|
146
|
+
let summary = '';
|
|
147
|
+
|
|
148
|
+
if (strategies.length > 0) {
|
|
149
|
+
summary += '\n--- STRATEJİK KURALLAR ---\n';
|
|
150
|
+
strategies.forEach(s => {
|
|
151
|
+
summary += `- ${s.rule}\n`;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (recentFacts.length > 0) {
|
|
156
|
+
summary += '\n--- SON HATIRALAR ---\n';
|
|
157
|
+
recentFacts.forEach(f => {
|
|
158
|
+
summary += `- [${f.category}] ${f.fact}\n`;
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return summary;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Full reset (dangerous).
|
|
167
|
+
*/
|
|
168
|
+
clear() {
|
|
169
|
+
this.facts = [];
|
|
170
|
+
this.strategies = [];
|
|
171
|
+
saveJson(FACTS_FILE, this.facts);
|
|
172
|
+
saveJson(STRATEGY_FILE, this.strategies);
|
|
173
|
+
log('WARN', 'Memory cleared');
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
178
|
+
// SINGLETON
|
|
179
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
180
|
+
|
|
181
|
+
let memoryInstance = null;
|
|
182
|
+
|
|
183
|
+
export function getMemory() {
|
|
184
|
+
if (!memoryInstance) {
|
|
185
|
+
memoryInstance = new Memory();
|
|
186
|
+
}
|
|
187
|
+
return memoryInstance;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export default Memory;
|
package/core/queue.js
CHANGED
|
@@ -1,120 +1,120 @@
|
|
|
1
|
-
// core/queue.js
|
|
2
|
-
// Critical Operation Queue (Lane Queue) for Vantuz AI
|
|
3
|
-
// Ensures write operations (price/stock updates) execute serially, never in parallel.
|
|
4
|
-
|
|
5
|
-
import { log } from './ai-provider.js';
|
|
6
|
-
|
|
7
|
-
class CriticalQueue {
|
|
8
|
-
constructor() {
|
|
9
|
-
this._queue = [];
|
|
10
|
-
this._running = false;
|
|
11
|
-
this._stats = { processed: 0, errors: 0, lastRun: null };
|
|
12
|
-
log('INFO', 'CriticalQueue (Lane Queue) initialized');
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Enqueue a critical operation.
|
|
17
|
-
* @param {string} label - Human readable label (e.g. "Fiyat güncelle: SKU-123")
|
|
18
|
-
* @param {function} fn - Async function to execute.
|
|
19
|
-
* @param {object} options - { priority: 'high'|'normal', dryRun: false }
|
|
20
|
-
* @returns {Promise} Resolves when the operation completes.
|
|
21
|
-
*/
|
|
22
|
-
enqueue(label, fn, options = {}) {
|
|
23
|
-
const { priority = 'normal', dryRun = false } = options;
|
|
24
|
-
|
|
25
|
-
return new Promise((resolve, reject) => {
|
|
26
|
-
const task = { label, fn, resolve, reject, priority, dryRun, enqueuedAt: Date.now() };
|
|
27
|
-
|
|
28
|
-
if (priority === 'high') {
|
|
29
|
-
// Insert at the front (after any currently running task)
|
|
30
|
-
this._queue.unshift(task);
|
|
31
|
-
} else {
|
|
32
|
-
this._queue.push(task);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
log('INFO', `Operation queued: "${label}"`, {
|
|
36
|
-
position: this._queue.length,
|
|
37
|
-
dryRun,
|
|
38
|
-
priority
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
this._processNext();
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Process the next item in the queue.
|
|
47
|
-
*/
|
|
48
|
-
async _processNext() {
|
|
49
|
-
if (this._running || this._queue.length === 0) return;
|
|
50
|
-
|
|
51
|
-
this._running = true;
|
|
52
|
-
const task = this._queue.shift();
|
|
53
|
-
|
|
54
|
-
log('INFO', `▶ Executing: "${task.label}"`, {
|
|
55
|
-
waitedMs: Date.now() - task.enqueuedAt,
|
|
56
|
-
remaining: this._queue.length
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
try {
|
|
60
|
-
if (task.dryRun) {
|
|
61
|
-
log('INFO', `[DRY RUN] Would execute: "${task.label}"`);
|
|
62
|
-
task.resolve({ dryRun: true, label: task.label, status: 'skipped' });
|
|
63
|
-
} else {
|
|
64
|
-
const result = await task.fn();
|
|
65
|
-
this._stats.processed++;
|
|
66
|
-
this._stats.lastRun = new Date().toISOString();
|
|
67
|
-
task.resolve(result);
|
|
68
|
-
}
|
|
69
|
-
} catch (error) {
|
|
70
|
-
this._stats.errors++;
|
|
71
|
-
log('ERROR', `✘ Failed: "${task.label}"`, { error: error.message });
|
|
72
|
-
task.reject(error);
|
|
73
|
-
} finally {
|
|
74
|
-
this._running = false;
|
|
75
|
-
// Process next in queue
|
|
76
|
-
if (this._queue.length > 0) {
|
|
77
|
-
// Small delay to prevent tight loops
|
|
78
|
-
setTimeout(() => this._processNext(), 50);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Get queue status.
|
|
85
|
-
*/
|
|
86
|
-
getStatus() {
|
|
87
|
-
return {
|
|
88
|
-
queueLength: this._queue.length,
|
|
89
|
-
isRunning: this._running,
|
|
90
|
-
pendingLabels: this._queue.map(t => t.label),
|
|
91
|
-
stats: { ...this._stats }
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Drain the queue (cancel all pending, don't cancel running).
|
|
97
|
-
*/
|
|
98
|
-
drain() {
|
|
99
|
-
const cancelled = this._queue.length;
|
|
100
|
-
this._queue.forEach(t => t.reject(new Error('Queue drained')));
|
|
101
|
-
this._queue = [];
|
|
102
|
-
log('WARN', `Queue drained, ${cancelled} operations cancelled`);
|
|
103
|
-
return cancelled;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
108
|
-
// SINGLETON
|
|
109
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
110
|
-
|
|
111
|
-
let queueInstance = null;
|
|
112
|
-
|
|
113
|
-
export function getCriticalQueue() {
|
|
114
|
-
if (!queueInstance) {
|
|
115
|
-
queueInstance = new CriticalQueue();
|
|
116
|
-
}
|
|
117
|
-
return queueInstance;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export default CriticalQueue;
|
|
1
|
+
// core/queue.js
|
|
2
|
+
// Critical Operation Queue (Lane Queue) for Vantuz AI
|
|
3
|
+
// Ensures write operations (price/stock updates) execute serially, never in parallel.
|
|
4
|
+
|
|
5
|
+
import { log } from './ai-provider.js';
|
|
6
|
+
|
|
7
|
+
class CriticalQueue {
|
|
8
|
+
constructor() {
|
|
9
|
+
this._queue = [];
|
|
10
|
+
this._running = false;
|
|
11
|
+
this._stats = { processed: 0, errors: 0, lastRun: null };
|
|
12
|
+
log('INFO', 'CriticalQueue (Lane Queue) initialized');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Enqueue a critical operation.
|
|
17
|
+
* @param {string} label - Human readable label (e.g. "Fiyat güncelle: SKU-123")
|
|
18
|
+
* @param {function} fn - Async function to execute.
|
|
19
|
+
* @param {object} options - { priority: 'high'|'normal', dryRun: false }
|
|
20
|
+
* @returns {Promise} Resolves when the operation completes.
|
|
21
|
+
*/
|
|
22
|
+
enqueue(label, fn, options = {}) {
|
|
23
|
+
const { priority = 'normal', dryRun = false } = options;
|
|
24
|
+
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
const task = { label, fn, resolve, reject, priority, dryRun, enqueuedAt: Date.now() };
|
|
27
|
+
|
|
28
|
+
if (priority === 'high') {
|
|
29
|
+
// Insert at the front (after any currently running task)
|
|
30
|
+
this._queue.unshift(task);
|
|
31
|
+
} else {
|
|
32
|
+
this._queue.push(task);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
log('INFO', `Operation queued: "${label}"`, {
|
|
36
|
+
position: this._queue.length,
|
|
37
|
+
dryRun,
|
|
38
|
+
priority
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
this._processNext();
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Process the next item in the queue.
|
|
47
|
+
*/
|
|
48
|
+
async _processNext() {
|
|
49
|
+
if (this._running || this._queue.length === 0) return;
|
|
50
|
+
|
|
51
|
+
this._running = true;
|
|
52
|
+
const task = this._queue.shift();
|
|
53
|
+
|
|
54
|
+
log('INFO', `▶ Executing: "${task.label}"`, {
|
|
55
|
+
waitedMs: Date.now() - task.enqueuedAt,
|
|
56
|
+
remaining: this._queue.length
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
if (task.dryRun) {
|
|
61
|
+
log('INFO', `[DRY RUN] Would execute: "${task.label}"`);
|
|
62
|
+
task.resolve({ dryRun: true, label: task.label, status: 'skipped' });
|
|
63
|
+
} else {
|
|
64
|
+
const result = await task.fn();
|
|
65
|
+
this._stats.processed++;
|
|
66
|
+
this._stats.lastRun = new Date().toISOString();
|
|
67
|
+
task.resolve(result);
|
|
68
|
+
}
|
|
69
|
+
} catch (error) {
|
|
70
|
+
this._stats.errors++;
|
|
71
|
+
log('ERROR', `✘ Failed: "${task.label}"`, { error: error.message });
|
|
72
|
+
task.reject(error);
|
|
73
|
+
} finally {
|
|
74
|
+
this._running = false;
|
|
75
|
+
// Process next in queue
|
|
76
|
+
if (this._queue.length > 0) {
|
|
77
|
+
// Small delay to prevent tight loops
|
|
78
|
+
setTimeout(() => this._processNext(), 50);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get queue status.
|
|
85
|
+
*/
|
|
86
|
+
getStatus() {
|
|
87
|
+
return {
|
|
88
|
+
queueLength: this._queue.length,
|
|
89
|
+
isRunning: this._running,
|
|
90
|
+
pendingLabels: this._queue.map(t => t.label),
|
|
91
|
+
stats: { ...this._stats }
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Drain the queue (cancel all pending, don't cancel running).
|
|
97
|
+
*/
|
|
98
|
+
drain() {
|
|
99
|
+
const cancelled = this._queue.length;
|
|
100
|
+
this._queue.forEach(t => t.reject(new Error('Queue drained')));
|
|
101
|
+
this._queue = [];
|
|
102
|
+
log('WARN', `Queue drained, ${cancelled} operations cancelled`);
|
|
103
|
+
return cancelled;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
108
|
+
// SINGLETON
|
|
109
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
110
|
+
|
|
111
|
+
let queueInstance = null;
|
|
112
|
+
|
|
113
|
+
export function getCriticalQueue() {
|
|
114
|
+
if (!queueInstance) {
|
|
115
|
+
queueInstance = new CriticalQueue();
|
|
116
|
+
}
|
|
117
|
+
return queueInstance;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export default CriticalQueue;
|