@unrdf/knowledge-engine 5.0.1 → 26.4.3
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/package.json +13 -7
- package/src/ai-enhanced-search.mjs +371 -0
- package/src/anomaly-detector.mjs +226 -0
- package/src/artifact-generator.mjs +251 -0
- package/src/browser.mjs +1 -1
- package/src/chatman/disruption-arithmetic.mjs +140 -0
- package/src/chatman/market-dynamics.mjs +140 -0
- package/src/chatman/organizational-dynamics.mjs +140 -0
- package/src/chatman/strategic-dynamics.mjs +140 -0
- package/src/chatman-config-loader.mjs +282 -0
- package/src/chatman-engine.mjs +431 -0
- package/src/chatman-operator.mjs +342 -0
- package/src/dark-field-detector.mjs +312 -0
- package/src/formation-theorems.mjs +345 -0
- package/src/index.mjs +20 -2
- package/src/knowledge-hook-manager.mjs +1 -1
- package/src/lockchain-writer-browser.mjs +2 -2
- package/src/observability.mjs +40 -4
- package/src/query-optimizer.mjs +1 -1
- package/src/resolution-layer.mjs +1 -1
- package/src/transaction.mjs +11 -9
- package/README.md +0 -84
- package/src/browser-shims.mjs +0 -343
- package/src/canonicalize.mjs +0 -414
- package/src/condition-cache.mjs +0 -109
- package/src/condition-evaluator.mjs +0 -722
- package/src/dark-matter-core.mjs +0 -742
- package/src/define-hook.mjs +0 -213
- package/src/effect-sandbox-browser.mjs +0 -283
- package/src/effect-sandbox-worker.mjs +0 -170
- package/src/effect-sandbox.mjs +0 -517
- package/src/engines/index.mjs +0 -11
- package/src/engines/rdf-engine.mjs +0 -299
- package/src/file-resolver.mjs +0 -387
- package/src/hook-executor-batching.mjs +0 -277
- package/src/hook-executor.mjs +0 -870
- package/src/hook-management.mjs +0 -150
- package/src/ken-parliment.mjs +0 -119
- package/src/ken.mjs +0 -149
- package/src/knowledge-engine/builtin-rules.mjs +0 -190
- package/src/knowledge-engine/inference-engine.mjs +0 -418
- package/src/knowledge-engine/knowledge-engine.mjs +0 -317
- package/src/knowledge-engine/pattern-dsl.mjs +0 -142
- package/src/knowledge-engine/pattern-matcher.mjs +0 -215
- package/src/knowledge-engine/rules.mjs +0 -184
- package/src/knowledge-engine.mjs +0 -319
- package/src/knowledge-hook-engine.mjs +0 -360
- package/src/knowledge-substrate-core.mjs +0 -927
- package/src/lite.mjs +0 -222
- package/src/lockchain-writer.mjs +0 -602
- package/src/monitoring/andon-signals.mjs +0 -775
- package/src/parse.mjs +0 -290
- package/src/performance-optimizer.mjs +0 -678
- package/src/policy-pack.mjs +0 -572
- package/src/query-cache.mjs +0 -116
- package/src/query.mjs +0 -306
- package/src/reason.mjs +0 -350
- package/src/schemas.mjs +0 -1063
- package/src/security/error-sanitizer.mjs +0 -257
- package/src/security/path-validator.mjs +0 -194
- package/src/security/sandbox-restrictions.mjs +0 -331
- package/src/security-validator.mjs +0 -389
- package/src/store-cache.mjs +0 -137
- package/src/telemetry.mjs +0 -167
- package/src/utils/adaptive-monitor.mjs +0 -746
- package/src/utils/circuit-breaker.mjs +0 -513
- package/src/utils/edge-case-handler.mjs +0 -503
- package/src/utils/memory-manager.mjs +0 -498
- package/src/utils/ring-buffer.mjs +0 -282
- package/src/validate.mjs +0 -319
- package/src/validators/index.mjs +0 -338
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Strategic Dynamics Rules
|
|
3
|
+
* @module knowledge-engine/chatman/strategic-dynamics
|
|
4
|
+
*
|
|
5
|
+
* @description
|
|
6
|
+
* Strategic dynamics rules for Chatman Equation analysis.
|
|
7
|
+
* Defines patterns for strategic options, competitive advantages,
|
|
8
|
+
* strategic assumptions, industry shifts, and success factors.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { z } from 'zod';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Strategic dynamics rule schema
|
|
15
|
+
*/
|
|
16
|
+
export const StrategicDynamicsRuleSchema = z.object({
|
|
17
|
+
id: z.string(),
|
|
18
|
+
name: z.string(),
|
|
19
|
+
category: z.enum([
|
|
20
|
+
'strategic_options',
|
|
21
|
+
'competitive_advantages',
|
|
22
|
+
'strategic_assumptions',
|
|
23
|
+
'industry_shifts',
|
|
24
|
+
'success_factors',
|
|
25
|
+
]),
|
|
26
|
+
pattern: z.string(),
|
|
27
|
+
darkFieldMultiplier: z.number().positive().default(19),
|
|
28
|
+
confidence: z.number().min(0).max(1).default(0.8),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Strategic dynamics rules
|
|
33
|
+
*/
|
|
34
|
+
export const STRATEGIC_DYNAMICS_RULES = [
|
|
35
|
+
{
|
|
36
|
+
id: 'sd-001',
|
|
37
|
+
name: 'Latent Strategic Options',
|
|
38
|
+
category: 'strategic_options',
|
|
39
|
+
pattern: 'latent_strategic_options',
|
|
40
|
+
darkFieldMultiplier: 19,
|
|
41
|
+
confidence: 0.84,
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
id: 'sd-002',
|
|
45
|
+
name: 'Hidden Competitive Advantages',
|
|
46
|
+
category: 'competitive_advantages',
|
|
47
|
+
pattern: 'hidden_competitive_advantages',
|
|
48
|
+
darkFieldMultiplier: 19,
|
|
49
|
+
confidence: 0.86,
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
id: 'sd-003',
|
|
53
|
+
name: 'Implicit Strategic Assumptions',
|
|
54
|
+
category: 'strategic_assumptions',
|
|
55
|
+
pattern: 'implicit_strategic_assumptions',
|
|
56
|
+
darkFieldMultiplier: 19,
|
|
57
|
+
confidence: 0.82,
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
id: 'sd-004',
|
|
61
|
+
name: 'Emergent Industry Shifts',
|
|
62
|
+
category: 'industry_shifts',
|
|
63
|
+
pattern: 'emergent_industry_shifts',
|
|
64
|
+
darkFieldMultiplier: 19,
|
|
65
|
+
confidence: 0.85,
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
id: 'sd-005',
|
|
69
|
+
name: 'Tacit Success Factors',
|
|
70
|
+
category: 'success_factors',
|
|
71
|
+
pattern: 'tacit_success_factors',
|
|
72
|
+
darkFieldMultiplier: 19,
|
|
73
|
+
confidence: 0.83,
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
id: 'sd-006',
|
|
77
|
+
name: 'Invisible Threat Vectors',
|
|
78
|
+
category: 'competitive_advantages',
|
|
79
|
+
pattern: 'invisible_threat_vectors',
|
|
80
|
+
darkFieldMultiplier: 19,
|
|
81
|
+
confidence: 0.8,
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
id: 'sd-007',
|
|
85
|
+
name: 'Unrecognized Opportunities',
|
|
86
|
+
category: 'strategic_options',
|
|
87
|
+
pattern: 'unrecognized_opportunities',
|
|
88
|
+
darkFieldMultiplier: 19,
|
|
89
|
+
confidence: 0.81,
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
id: 'sd-008',
|
|
93
|
+
name: 'Hidden Ecosystem Dependencies',
|
|
94
|
+
category: 'industry_shifts',
|
|
95
|
+
pattern: 'hidden_ecosystem_dependencies',
|
|
96
|
+
darkFieldMultiplier: 19,
|
|
97
|
+
confidence: 0.79,
|
|
98
|
+
},
|
|
99
|
+
];
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Get strategic dynamics rules by category
|
|
103
|
+
* @param {string} category - Rule category
|
|
104
|
+
* @returns {Array<Object>} Filtered rules
|
|
105
|
+
*/
|
|
106
|
+
export function getRulesByCategory(category) {
|
|
107
|
+
return STRATEGIC_DYNAMICS_RULES.filter(rule => rule.category === category);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Get strategic dynamics rule by ID
|
|
112
|
+
* @param {string} id - Rule ID
|
|
113
|
+
* @returns {Object|undefined} Rule or undefined
|
|
114
|
+
*/
|
|
115
|
+
export function getRuleById(id) {
|
|
116
|
+
return STRATEGIC_DYNAMICS_RULES.find(rule => rule.id === id);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Validate strategic dynamics rules
|
|
121
|
+
* @param {Array<Object>} rules - Rules to validate
|
|
122
|
+
* @returns {Object} Validation result
|
|
123
|
+
*/
|
|
124
|
+
export function validateRules(rules) {
|
|
125
|
+
const errors = [];
|
|
126
|
+
for (const rule of rules) {
|
|
127
|
+
const result = StrategicDynamicsRuleSchema.safeParse(rule);
|
|
128
|
+
if (!result.success) {
|
|
129
|
+
errors.push({
|
|
130
|
+
rule: rule.id || 'unknown',
|
|
131
|
+
errors: result.error.errors,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
valid: errors.length === 0,
|
|
138
|
+
errors,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Chatman Configuration Loader - TOML Config Parser
|
|
3
|
+
* @module knowledge-engine/chatman-config-loader
|
|
4
|
+
*
|
|
5
|
+
* @description
|
|
6
|
+
* Loads Chatman Equation configurations from TOML files.
|
|
7
|
+
* Supports market dynamics, organizational dynamics, strategic dynamics,
|
|
8
|
+
* and disruption arithmetic rule configurations.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { readFile } from 'fs/promises';
|
|
12
|
+
import { existsSync } from 'fs';
|
|
13
|
+
import { resolve } from 'path';
|
|
14
|
+
import { parse as parseToml } from '@iarna/toml';
|
|
15
|
+
import { z } from 'zod';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Chatman config schema
|
|
19
|
+
*/
|
|
20
|
+
export const ChatmanConfigSchema = z.object({
|
|
21
|
+
chatman: z
|
|
22
|
+
.object({
|
|
23
|
+
version: z.string().default('1.0.0'),
|
|
24
|
+
observable_ratio: z.number().min(0).max(1).default(0.05),
|
|
25
|
+
closure_threshold: z.number().min(0).max(1).default(0.95),
|
|
26
|
+
})
|
|
27
|
+
.optional(),
|
|
28
|
+
market_dynamics: z
|
|
29
|
+
.array(
|
|
30
|
+
z.object({
|
|
31
|
+
id: z.string(),
|
|
32
|
+
name: z.string(),
|
|
33
|
+
category: z.string(),
|
|
34
|
+
pattern: z.string(),
|
|
35
|
+
dark_field_multiplier: z.number().positive().default(19),
|
|
36
|
+
confidence: z.number().min(0).max(1).default(0.8),
|
|
37
|
+
})
|
|
38
|
+
)
|
|
39
|
+
.optional(),
|
|
40
|
+
organizational_dynamics: z
|
|
41
|
+
.array(
|
|
42
|
+
z.object({
|
|
43
|
+
id: z.string(),
|
|
44
|
+
name: z.string(),
|
|
45
|
+
category: z.string(),
|
|
46
|
+
pattern: z.string(),
|
|
47
|
+
dark_field_multiplier: z.number().positive().default(19),
|
|
48
|
+
confidence: z.number().min(0).max(1).default(0.8),
|
|
49
|
+
})
|
|
50
|
+
)
|
|
51
|
+
.optional(),
|
|
52
|
+
strategic_dynamics: z
|
|
53
|
+
.array(
|
|
54
|
+
z.object({
|
|
55
|
+
id: z.string(),
|
|
56
|
+
name: z.string(),
|
|
57
|
+
category: z.string(),
|
|
58
|
+
pattern: z.string(),
|
|
59
|
+
dark_field_multiplier: z.number().positive().default(19),
|
|
60
|
+
confidence: z.number().min(0).max(1).default(0.8),
|
|
61
|
+
})
|
|
62
|
+
)
|
|
63
|
+
.optional(),
|
|
64
|
+
disruption_arithmetic: z
|
|
65
|
+
.array(
|
|
66
|
+
z.object({
|
|
67
|
+
id: z.string(),
|
|
68
|
+
name: z.string(),
|
|
69
|
+
category: z.string(),
|
|
70
|
+
pattern: z.string(),
|
|
71
|
+
dark_field_multiplier: z.number().positive().default(19),
|
|
72
|
+
confidence: z.number().min(0).max(1).default(0.8),
|
|
73
|
+
})
|
|
74
|
+
)
|
|
75
|
+
.optional(),
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Configuration load error
|
|
80
|
+
*/
|
|
81
|
+
export class ConfigLoadError extends Error {
|
|
82
|
+
/**
|
|
83
|
+
* @param {string} message - Error message
|
|
84
|
+
* @param {Object} [options] - Error options
|
|
85
|
+
*/
|
|
86
|
+
constructor(message, options = {}) {
|
|
87
|
+
super(message);
|
|
88
|
+
this.name = 'ConfigLoadError';
|
|
89
|
+
this.path = options.path;
|
|
90
|
+
this.cause = options.cause;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Chatman configuration loader
|
|
96
|
+
*/
|
|
97
|
+
export class ChatmanConfigLoader {
|
|
98
|
+
/**
|
|
99
|
+
* Create a new config loader
|
|
100
|
+
* @param {Object} [options] - Loader options
|
|
101
|
+
* @param {Function} [options.tracer] - OTEL tracer function
|
|
102
|
+
*/
|
|
103
|
+
constructor(options = {}) {
|
|
104
|
+
this.tracer = options.tracer;
|
|
105
|
+
this.cache = new Map();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Load configuration from TOML file
|
|
110
|
+
* @param {string} configPath - Path to config file
|
|
111
|
+
* @param {Object} [options] - Load options
|
|
112
|
+
* @param {boolean} [options.useCache=true] - Use cached config
|
|
113
|
+
* @returns {Promise<Object>} Parsed configuration
|
|
114
|
+
*/
|
|
115
|
+
async load(configPath, options = {}) {
|
|
116
|
+
const { useCache = true } = options;
|
|
117
|
+
const span = this.tracer?.startSpan?.('chatman.config.load');
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
const absolutePath = resolve(configPath);
|
|
121
|
+
|
|
122
|
+
// Check cache
|
|
123
|
+
if (useCache && this.cache.has(absolutePath)) {
|
|
124
|
+
span?.addEvent?.('config_cache_hit', { path: absolutePath });
|
|
125
|
+
return this.cache.get(absolutePath);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
span?.setAttribute?.('config.path', absolutePath);
|
|
129
|
+
|
|
130
|
+
// Verify file exists
|
|
131
|
+
if (!existsSync(absolutePath)) {
|
|
132
|
+
throw new ConfigLoadError(`Configuration file not found: ${absolutePath}`, {
|
|
133
|
+
path: absolutePath,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Read and parse TOML
|
|
138
|
+
const content = await readFile(absolutePath, 'utf-8');
|
|
139
|
+
const parsed = parseToml(content);
|
|
140
|
+
|
|
141
|
+
// Validate with Zod
|
|
142
|
+
const validated = ChatmanConfigSchema.parse(parsed);
|
|
143
|
+
|
|
144
|
+
// Normalize field names (convert snake_case to camelCase for internal use)
|
|
145
|
+
const normalized = this._normalizeConfig(validated);
|
|
146
|
+
|
|
147
|
+
// Cache the result
|
|
148
|
+
if (useCache) {
|
|
149
|
+
this.cache.set(absolutePath, normalized);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
span?.addEvent?.('config_loaded', {
|
|
153
|
+
'config.version': normalized.chatman?.version || '1.0.0',
|
|
154
|
+
'config.market_rules': normalized.marketDynamics?.length || 0,
|
|
155
|
+
'config.org_rules': normalized.organizationalDynamics?.length || 0,
|
|
156
|
+
'config.strategic_rules': normalized.strategicDynamics?.length || 0,
|
|
157
|
+
'config.disruption_rules': normalized.disruptionArithmetic?.length || 0,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
return normalized;
|
|
161
|
+
} catch (error) {
|
|
162
|
+
span?.recordException?.(error);
|
|
163
|
+
span?.setStatus?.({ code: 2, message: error.message });
|
|
164
|
+
|
|
165
|
+
if (error instanceof ConfigLoadError) {
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (error.name === 'ZodError') {
|
|
170
|
+
throw new ConfigLoadError('Configuration validation failed', {
|
|
171
|
+
cause: error.message,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
throw new ConfigLoadError('Failed to load configuration', {
|
|
176
|
+
cause: error.message,
|
|
177
|
+
});
|
|
178
|
+
} finally {
|
|
179
|
+
span?.end?.();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Normalize configuration field names
|
|
185
|
+
* @param {Object} config - Parsed config
|
|
186
|
+
* @returns {Object} Normalized config
|
|
187
|
+
* @private
|
|
188
|
+
*/
|
|
189
|
+
_normalizeConfig(config) {
|
|
190
|
+
const normalized = {};
|
|
191
|
+
|
|
192
|
+
if (config.chatman) {
|
|
193
|
+
normalized.chatman = {
|
|
194
|
+
version: config.chatman.version,
|
|
195
|
+
observableRatio: config.chatman.observable_ratio,
|
|
196
|
+
closureThreshold: config.chatman.closure_threshold,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (config.market_dynamics) {
|
|
201
|
+
normalized.marketDynamics = config.market_dynamics.map(rule => ({
|
|
202
|
+
id: rule.id,
|
|
203
|
+
name: rule.name,
|
|
204
|
+
category: rule.category,
|
|
205
|
+
pattern: rule.pattern,
|
|
206
|
+
darkFieldMultiplier: rule.dark_field_multiplier,
|
|
207
|
+
confidence: rule.confidence,
|
|
208
|
+
}));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (config.organizational_dynamics) {
|
|
212
|
+
normalized.organizationalDynamics = config.organizational_dynamics.map(rule => ({
|
|
213
|
+
id: rule.id,
|
|
214
|
+
name: rule.name,
|
|
215
|
+
category: rule.category,
|
|
216
|
+
pattern: rule.pattern,
|
|
217
|
+
darkFieldMultiplier: rule.dark_field_multiplier,
|
|
218
|
+
confidence: rule.confidence,
|
|
219
|
+
}));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (config.strategic_dynamics) {
|
|
223
|
+
normalized.strategicDynamics = config.strategic_dynamics.map(rule => ({
|
|
224
|
+
id: rule.id,
|
|
225
|
+
name: rule.name,
|
|
226
|
+
category: rule.category,
|
|
227
|
+
pattern: rule.pattern,
|
|
228
|
+
darkFieldMultiplier: rule.dark_field_multiplier,
|
|
229
|
+
confidence: rule.confidence,
|
|
230
|
+
}));
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (config.disruption_arithmetic) {
|
|
234
|
+
normalized.disruptionArithmetic = config.disruption_arithmetic.map(rule => ({
|
|
235
|
+
id: rule.id,
|
|
236
|
+
name: rule.name,
|
|
237
|
+
category: rule.category,
|
|
238
|
+
pattern: rule.pattern,
|
|
239
|
+
darkFieldMultiplier: rule.dark_field_multiplier,
|
|
240
|
+
confidence: rule.confidence,
|
|
241
|
+
}));
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return normalized;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Clear configuration cache
|
|
249
|
+
*/
|
|
250
|
+
clearCache() {
|
|
251
|
+
this.cache.clear();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Get cached configuration
|
|
256
|
+
* @param {string} configPath - Config path
|
|
257
|
+
* @returns {Object|undefined} Cached config or undefined
|
|
258
|
+
*/
|
|
259
|
+
getCached(configPath) {
|
|
260
|
+
return this.cache.get(resolve(configPath));
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Create a Chatman config loader instance
|
|
266
|
+
* @param {Object} [options] - Loader options
|
|
267
|
+
* @returns {ChatmanConfigLoader} Loader instance
|
|
268
|
+
*/
|
|
269
|
+
export function createChatmanConfigLoader(options = {}) {
|
|
270
|
+
return new ChatmanConfigLoader(options);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Load Chatman configuration from TOML file
|
|
275
|
+
* @param {string} configPath - Path to config file
|
|
276
|
+
* @param {Object} [options] - Load options
|
|
277
|
+
* @returns {Promise<Object>} Parsed configuration
|
|
278
|
+
*/
|
|
279
|
+
export async function loadChatmanConfig(configPath, options = {}) {
|
|
280
|
+
const loader = new ChatmanConfigLoader(options);
|
|
281
|
+
return loader.load(configPath, options);
|
|
282
|
+
}
|