equilibria-mcp-server 1.0.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/CHANGELOG.md +142 -0
- package/LICENSE +21 -0
- package/README.md +240 -0
- package/dist/builders/YAMLBuilder.d.ts +77 -0
- package/dist/builders/YAMLBuilder.d.ts.map +1 -0
- package/dist/builders/YAMLBuilder.js +251 -0
- package/dist/builders/YAMLBuilder.js.map +1 -0
- package/dist/economics/constants.d.ts +39 -0
- package/dist/economics/constants.d.ts.map +1 -0
- package/dist/economics/constants.js +46 -0
- package/dist/economics/constants.js.map +1 -0
- package/dist/economics/formulas.d.ts +19 -0
- package/dist/economics/formulas.d.ts.map +1 -0
- package/dist/economics/formulas.js +260 -0
- package/dist/economics/formulas.js.map +1 -0
- package/dist/economics/index.d.ts +9 -0
- package/dist/economics/index.d.ts.map +1 -0
- package/dist/economics/index.js +9 -0
- package/dist/economics/index.js.map +1 -0
- package/dist/economics/validators.d.ts +18 -0
- package/dist/economics/validators.d.ts.map +1 -0
- package/dist/economics/validators.js +111 -0
- package/dist/economics/validators.js.map +1 -0
- package/dist/errors/index.d.ts +172 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +313 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/formatters/index.d.ts +8 -0
- package/dist/formatters/index.d.ts.map +1 -0
- package/dist/formatters/index.js +8 -0
- package/dist/formatters/index.js.map +1 -0
- package/dist/formatters/redux.d.ts +15 -0
- package/dist/formatters/redux.d.ts.map +1 -0
- package/dist/formatters/redux.js +35 -0
- package/dist/formatters/redux.js.map +1 -0
- package/dist/formatters/yaml.d.ts +18 -0
- package/dist/formatters/yaml.d.ts.map +1 -0
- package/dist/formatters/yaml.js +40 -0
- package/dist/formatters/yaml.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/server.d.ts +14 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +86 -0
- package/dist/server.js.map +1 -0
- package/dist/templates/advancedMicro.d.ts +8 -0
- package/dist/templates/advancedMicro.d.ts.map +1 -0
- package/dist/templates/advancedMicro.js +834 -0
- package/dist/templates/advancedMicro.js.map +1 -0
- package/dist/templates/consumer.d.ts +12 -0
- package/dist/templates/consumer.d.ts.map +1 -0
- package/dist/templates/consumer.js +1978 -0
- package/dist/templates/consumer.js.map +1 -0
- package/dist/templates/elasticity.d.ts +8 -0
- package/dist/templates/elasticity.d.ts.map +1 -0
- package/dist/templates/elasticity.js +500 -0
- package/dist/templates/elasticity.js.map +1 -0
- package/dist/templates/externalities.d.ts +11 -0
- package/dist/templates/externalities.d.ts.map +1 -0
- package/dist/templates/externalities.js +997 -0
- package/dist/templates/externalities.js.map +1 -0
- package/dist/templates/financeBehavioral.d.ts +8 -0
- package/dist/templates/financeBehavioral.d.ts.map +1 -0
- package/dist/templates/financeBehavioral.js +860 -0
- package/dist/templates/financeBehavioral.js.map +1 -0
- package/dist/templates/growth.d.ts +8 -0
- package/dist/templates/growth.d.ts.map +1 -0
- package/dist/templates/growth.js +740 -0
- package/dist/templates/growth.js.map +1 -0
- package/dist/templates/index.d.ts +31 -0
- package/dist/templates/index.d.ts.map +1 -0
- package/dist/templates/index.js +91 -0
- package/dist/templates/index.js.map +1 -0
- package/dist/templates/inequality.d.ts +8 -0
- package/dist/templates/inequality.d.ts.map +1 -0
- package/dist/templates/inequality.js +562 -0
- package/dist/templates/inequality.js.map +1 -0
- package/dist/templates/intertemporalMacro.d.ts +8 -0
- package/dist/templates/intertemporalMacro.d.ts.map +1 -0
- package/dist/templates/intertemporalMacro.js +550 -0
- package/dist/templates/intertemporalMacro.js.map +1 -0
- package/dist/templates/isLM.d.ts +8 -0
- package/dist/templates/isLM.d.ts.map +1 -0
- package/dist/templates/isLM.js +747 -0
- package/dist/templates/isLM.js.map +1 -0
- package/dist/templates/macro.d.ts +8 -0
- package/dist/templates/macro.d.ts.map +1 -0
- package/dist/templates/macro.js +600 -0
- package/dist/templates/macro.js.map +1 -0
- package/dist/templates/marketStructures.d.ts +11 -0
- package/dist/templates/marketStructures.d.ts.map +1 -0
- package/dist/templates/marketStructures.js +1135 -0
- package/dist/templates/marketStructures.js.map +1 -0
- package/dist/templates/newKeynesian.d.ts +8 -0
- package/dist/templates/newKeynesian.d.ts.map +1 -0
- package/dist/templates/newKeynesian.js +633 -0
- package/dist/templates/newKeynesian.js.map +1 -0
- package/dist/templates/oligopoly.d.ts +11 -0
- package/dist/templates/oligopoly.d.ts.map +1 -0
- package/dist/templates/oligopoly.js +1113 -0
- package/dist/templates/oligopoly.js.map +1 -0
- package/dist/templates/ppf.d.ts +8 -0
- package/dist/templates/ppf.d.ts.map +1 -0
- package/dist/templates/ppf.js +439 -0
- package/dist/templates/ppf.js.map +1 -0
- package/dist/templates/producer.d.ts +11 -0
- package/dist/templates/producer.d.ts.map +1 -0
- package/dist/templates/producer.js +979 -0
- package/dist/templates/producer.js.map +1 -0
- package/dist/templates/production.d.ts +8 -0
- package/dist/templates/production.d.ts.map +1 -0
- package/dist/templates/production.js +574 -0
- package/dist/templates/production.js.map +1 -0
- package/dist/templates/supplyDemand.d.ts +8 -0
- package/dist/templates/supplyDemand.d.ts.map +1 -0
- package/dist/templates/supplyDemand.js +1282 -0
- package/dist/templates/supplyDemand.js.map +1 -0
- package/dist/templates/tradeGrowth.d.ts +8 -0
- package/dist/templates/tradeGrowth.d.ts.map +1 -0
- package/dist/templates/tradeGrowth.js +637 -0
- package/dist/templates/tradeGrowth.js.map +1 -0
- package/dist/tools/index.d.ts +25 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +54 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/models.d.ts +8 -0
- package/dist/tools/models.d.ts.map +1 -0
- package/dist/tools/models.js +828 -0
- package/dist/tools/models.js.map +1 -0
- package/dist/tools/output.d.ts +8 -0
- package/dist/tools/output.d.ts.map +1 -0
- package/dist/tools/output.js +236 -0
- package/dist/tools/output.js.map +1 -0
- package/dist/tools/templates.d.ts +8 -0
- package/dist/tools/templates.d.ts.map +1 -0
- package/dist/tools/templates.js +247 -0
- package/dist/tools/templates.js.map +1 -0
- package/dist/tools/validation.d.ts +8 -0
- package/dist/tools/validation.d.ts.map +1 -0
- package/dist/tools/validation.js +181 -0
- package/dist/tools/validation.js.map +1 -0
- package/dist/types/index.d.ts +187 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +7 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/cache.d.ts +99 -0
- package/dist/utils/cache.d.ts.map +1 -0
- package/dist/utils/cache.js +192 -0
- package/dist/utils/cache.js.map +1 -0
- package/dist/utils/index.d.ts +8 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +8 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/logger.d.ts +128 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +251 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/validation/index.d.ts +42 -0
- package/dist/validation/index.d.ts.map +1 -0
- package/dist/validation/index.js +282 -0
- package/dist/validation/index.js.map +1 -0
- package/package.json +73 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation Tools
|
|
3
|
+
*
|
|
4
|
+
* Tools for validating expressions, documents, and economic constraints.
|
|
5
|
+
*/
|
|
6
|
+
import { validateExpression, validateDocument, checkCircularDependencies, } from '../validation/index.js';
|
|
7
|
+
/**
|
|
8
|
+
* Tool: validate_expression
|
|
9
|
+
*
|
|
10
|
+
* Validates a mathematical expression for syntax and parameter references.
|
|
11
|
+
*/
|
|
12
|
+
const validateExpressionTool = {
|
|
13
|
+
tool: {
|
|
14
|
+
name: 'validate_expression',
|
|
15
|
+
description: 'Validate a mathematical expression for syntax errors and check if referenced parameters exist.',
|
|
16
|
+
inputSchema: {
|
|
17
|
+
type: 'object',
|
|
18
|
+
properties: {
|
|
19
|
+
expression: {
|
|
20
|
+
type: 'string',
|
|
21
|
+
description: 'The mathematical expression to validate',
|
|
22
|
+
},
|
|
23
|
+
availableParameters: {
|
|
24
|
+
type: 'array',
|
|
25
|
+
items: { type: 'string' },
|
|
26
|
+
description: 'List of available parameter names',
|
|
27
|
+
},
|
|
28
|
+
allowX: {
|
|
29
|
+
type: 'boolean',
|
|
30
|
+
description: 'Allow "x" as a variable (for equations)',
|
|
31
|
+
default: true,
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
required: ['expression'],
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
handler: async (args, _context) => {
|
|
38
|
+
const expression = args.expression;
|
|
39
|
+
const availableParams = args.availableParameters || [];
|
|
40
|
+
const allowX = args.allowX !== false;
|
|
41
|
+
const result = validateExpression(expression, availableParams, allowX);
|
|
42
|
+
if (result.valid) {
|
|
43
|
+
return {
|
|
44
|
+
content: [
|
|
45
|
+
{
|
|
46
|
+
type: 'text',
|
|
47
|
+
text: `Expression is valid.\n\nParsed symbols: ${result.symbols.join(', ') || 'none'}`,
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
let output = 'Expression validation failed:\n\n';
|
|
53
|
+
for (const error of result.errors) {
|
|
54
|
+
output += `- ${error.message}`;
|
|
55
|
+
if (error.suggestion) {
|
|
56
|
+
output += ` (Suggestion: ${error.suggestion})`;
|
|
57
|
+
}
|
|
58
|
+
output += '\n';
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
content: [
|
|
62
|
+
{
|
|
63
|
+
type: 'text',
|
|
64
|
+
text: output,
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
isError: true,
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Tool: validate_yaml
|
|
73
|
+
*
|
|
74
|
+
* Validates YAML content against the Equilibria schema.
|
|
75
|
+
*/
|
|
76
|
+
const validateYAML = {
|
|
77
|
+
tool: {
|
|
78
|
+
name: 'validate_yaml',
|
|
79
|
+
description: 'Validate YAML content against the Equilibria document schema. Returns detailed error messages with suggestions.',
|
|
80
|
+
inputSchema: {
|
|
81
|
+
type: 'object',
|
|
82
|
+
properties: {
|
|
83
|
+
yaml: {
|
|
84
|
+
type: 'string',
|
|
85
|
+
description: 'The YAML content to validate',
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
required: ['yaml'],
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
handler: async (args, _context) => {
|
|
92
|
+
const yaml = args.yaml;
|
|
93
|
+
const result = validateDocument(yaml);
|
|
94
|
+
if (result.valid) {
|
|
95
|
+
let output = 'Document is valid!\n';
|
|
96
|
+
if (result.warnings.length > 0) {
|
|
97
|
+
output += '\nWarnings:\n';
|
|
98
|
+
for (const warning of result.warnings) {
|
|
99
|
+
output += `- ${warning.path}: ${warning.message}\n`;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
content: [
|
|
104
|
+
{
|
|
105
|
+
type: 'text',
|
|
106
|
+
text: output,
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
let output = 'Validation failed:\n\n';
|
|
112
|
+
for (const error of result.errors) {
|
|
113
|
+
output += `**${error.path}**\n`;
|
|
114
|
+
output += ` ${error.message}\n`;
|
|
115
|
+
if (error.suggestion) {
|
|
116
|
+
output += ` Suggestion: ${error.suggestion}\n`;
|
|
117
|
+
}
|
|
118
|
+
output += '\n';
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
content: [
|
|
122
|
+
{
|
|
123
|
+
type: 'text',
|
|
124
|
+
text: output,
|
|
125
|
+
},
|
|
126
|
+
],
|
|
127
|
+
isError: true,
|
|
128
|
+
};
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Tool: check_dependencies
|
|
133
|
+
*
|
|
134
|
+
* Checks for circular dependencies in parameter definitions.
|
|
135
|
+
*/
|
|
136
|
+
const checkDependencies = {
|
|
137
|
+
tool: {
|
|
138
|
+
name: 'check_dependencies',
|
|
139
|
+
description: 'Check for circular dependencies in parameter expressions. Circular dependencies cause infinite loops and must be avoided.',
|
|
140
|
+
inputSchema: {
|
|
141
|
+
type: 'object',
|
|
142
|
+
properties: {
|
|
143
|
+
parameters: {
|
|
144
|
+
type: 'object',
|
|
145
|
+
description: 'Map of parameter names to their expressions. E.g., {"a": "b + 1", "b": "c * 2", "c": "10"}',
|
|
146
|
+
additionalProperties: { type: 'string' },
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
required: ['parameters'],
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
handler: async (args, _context) => {
|
|
153
|
+
const parameters = args.parameters;
|
|
154
|
+
const result = checkCircularDependencies(parameters);
|
|
155
|
+
if (result.valid) {
|
|
156
|
+
return {
|
|
157
|
+
content: [
|
|
158
|
+
{
|
|
159
|
+
type: 'text',
|
|
160
|
+
text: `No circular dependencies found.\n\nEvaluation order: ${result.order.join(' -> ')}`,
|
|
161
|
+
},
|
|
162
|
+
],
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
content: [
|
|
167
|
+
{
|
|
168
|
+
type: 'text',
|
|
169
|
+
text: `Circular dependency detected!\n\nCycle: ${result.cycle.join(' -> ')} -> ${result.cycle[0]}`,
|
|
170
|
+
},
|
|
171
|
+
],
|
|
172
|
+
isError: true,
|
|
173
|
+
};
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
export const validationTools = [
|
|
177
|
+
validateExpressionTool,
|
|
178
|
+
validateYAML,
|
|
179
|
+
checkDependencies,
|
|
180
|
+
];
|
|
181
|
+
//# sourceMappingURL=validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/tools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P;wBACE,IAAI,EAAE,MAAM;wBACZ,IAAI,EAAE,wDAAwD,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE;qBAC1F;iBACF;aACF,CAAC;QACJ,CAAC;QAED,OAAO;YACL,OAAO,EAAE;gBACP;oBACE,IAAI,EAAE,MAAM;oBACZ,IAAI,EAAE,2CAA2C,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE;iBACnG;aACF;YACD,OAAO,EAAE,IAAI;SACd,CAAC;IACJ,CAAC;CACF,CAAC;AAEF,MAAM,CAAC,MAAM,eAAe,GAAuB;IACjD,sBAAsB;IACtB,YAAY;IACZ,iBAAiB;CAClB,CAAC"}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type Definitions for Equilibria MCP Server
|
|
3
|
+
*
|
|
4
|
+
* Core types used across the MCP server implementation.
|
|
5
|
+
*/
|
|
6
|
+
import type { Tool, CallToolResult } from '@modelcontextprotocol/sdk/types.js';
|
|
7
|
+
import type { Document as EquilibriaDocument } from 'equilibria-engine';
|
|
8
|
+
/**
|
|
9
|
+
* Output format types supported by the MCP server
|
|
10
|
+
*/
|
|
11
|
+
export type OutputFormat = 'yaml' | 'redux' | 'api';
|
|
12
|
+
/**
|
|
13
|
+
* Economic model categories
|
|
14
|
+
*/
|
|
15
|
+
export type ModelCategory = 'supply_demand' | 'production' | 'consumer_theory' | 'market_structures' | 'macro' | 'international' | 'finance' | 'behavioral' | 'inequality';
|
|
16
|
+
/**
|
|
17
|
+
* Economic education level
|
|
18
|
+
*/
|
|
19
|
+
export type EducationLevel = 'high_school' | 'undergraduate' | 'postgraduate';
|
|
20
|
+
/**
|
|
21
|
+
* Economic model template metadata
|
|
22
|
+
*/
|
|
23
|
+
export interface ModelTemplate {
|
|
24
|
+
/** Unique identifier for the model */
|
|
25
|
+
id: string;
|
|
26
|
+
/** Human-readable name */
|
|
27
|
+
name: string;
|
|
28
|
+
/** Brief description */
|
|
29
|
+
description: string;
|
|
30
|
+
/** Model category */
|
|
31
|
+
category: ModelCategory;
|
|
32
|
+
/** Target education level */
|
|
33
|
+
level: EducationLevel;
|
|
34
|
+
/** Available parameters and their defaults */
|
|
35
|
+
parameters: Record<string, ParameterDefinition>;
|
|
36
|
+
/** Tags for searchability */
|
|
37
|
+
tags: string[];
|
|
38
|
+
/** Generator function */
|
|
39
|
+
generate: (params: Record<string, unknown>) => EquilibriaDocument;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Parameter definition for model templates
|
|
43
|
+
*/
|
|
44
|
+
export interface ParameterDefinition {
|
|
45
|
+
/** Parameter type */
|
|
46
|
+
type: 'number' | 'string' | 'boolean';
|
|
47
|
+
/** Default value */
|
|
48
|
+
default: number | string | boolean;
|
|
49
|
+
/** Minimum value (for numbers) */
|
|
50
|
+
min?: number;
|
|
51
|
+
/** Maximum value (for numbers) */
|
|
52
|
+
max?: number;
|
|
53
|
+
/** Description */
|
|
54
|
+
description: string;
|
|
55
|
+
/** Whether parameter is required */
|
|
56
|
+
required?: boolean;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Shared context passed to tool handlers
|
|
60
|
+
*/
|
|
61
|
+
export interface MCPToolContext {
|
|
62
|
+
/** Current document being built (for incremental construction) */
|
|
63
|
+
currentDocument: Partial<EquilibriaDocument> | null;
|
|
64
|
+
/** Session state for multi-step operations */
|
|
65
|
+
sessionState: Map<string, unknown>;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Tool handler function signature
|
|
69
|
+
*/
|
|
70
|
+
export type ToolHandler = (args: Record<string, unknown>, context: MCPToolContext) => Promise<CallToolResult>;
|
|
71
|
+
/**
|
|
72
|
+
* Tool registration entry
|
|
73
|
+
*/
|
|
74
|
+
export interface ToolRegistration {
|
|
75
|
+
/** Tool definition for MCP */
|
|
76
|
+
tool: Tool;
|
|
77
|
+
/** Handler function */
|
|
78
|
+
handler: ToolHandler;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Validation error with details
|
|
82
|
+
*/
|
|
83
|
+
export interface ValidationError {
|
|
84
|
+
/** Error path (e.g., "charts[0].elements[1].equation") */
|
|
85
|
+
path: string;
|
|
86
|
+
/** Error message */
|
|
87
|
+
message: string;
|
|
88
|
+
/** Suggested fix (if available) */
|
|
89
|
+
suggestion?: string;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Result of document validation
|
|
93
|
+
*/
|
|
94
|
+
export interface ValidationResult {
|
|
95
|
+
/** Whether document is valid */
|
|
96
|
+
valid: boolean;
|
|
97
|
+
/** List of errors (if invalid) */
|
|
98
|
+
errors: ValidationError[];
|
|
99
|
+
/** Warnings (non-fatal issues) */
|
|
100
|
+
warnings: ValidationError[];
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Economic formula definition
|
|
104
|
+
*/
|
|
105
|
+
export interface EconomicFormula {
|
|
106
|
+
/** Formula identifier */
|
|
107
|
+
id: string;
|
|
108
|
+
/** Human-readable name */
|
|
109
|
+
name: string;
|
|
110
|
+
/** Math.js expression */
|
|
111
|
+
expression: string;
|
|
112
|
+
/** Required input parameters */
|
|
113
|
+
inputs: string[];
|
|
114
|
+
/** Output description */
|
|
115
|
+
output: string;
|
|
116
|
+
/** Category of formula */
|
|
117
|
+
category: string;
|
|
118
|
+
/** Additional notes */
|
|
119
|
+
notes?: string;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Builder state for incremental document construction
|
|
123
|
+
*/
|
|
124
|
+
export interface BuilderState {
|
|
125
|
+
/** Document metadata */
|
|
126
|
+
metadata: {
|
|
127
|
+
title?: string;
|
|
128
|
+
description?: string;
|
|
129
|
+
specVersion: string;
|
|
130
|
+
};
|
|
131
|
+
/** Global parameters */
|
|
132
|
+
parameters: Record<string, ParameterConfig>;
|
|
133
|
+
/** Charts in the document */
|
|
134
|
+
charts: ChartConfig[];
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Parameter configuration for builder
|
|
138
|
+
*/
|
|
139
|
+
export interface ParameterConfig {
|
|
140
|
+
value?: number | string | boolean;
|
|
141
|
+
expression?: string;
|
|
142
|
+
min?: number;
|
|
143
|
+
max?: number;
|
|
144
|
+
step?: number;
|
|
145
|
+
label?: string;
|
|
146
|
+
hidden?: boolean;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Chart configuration for builder
|
|
150
|
+
*/
|
|
151
|
+
export interface ChartConfig {
|
|
152
|
+
id: string;
|
|
153
|
+
title?: string;
|
|
154
|
+
parameters?: Record<string, ParameterConfig>;
|
|
155
|
+
xAxis: AxisConfig;
|
|
156
|
+
yAxis: AxisConfig;
|
|
157
|
+
elements: ElementConfig[];
|
|
158
|
+
annotations?: AnnotationConfig[];
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Axis configuration
|
|
162
|
+
*/
|
|
163
|
+
export interface AxisConfig {
|
|
164
|
+
label: string;
|
|
165
|
+
min: number | string;
|
|
166
|
+
max: number | string;
|
|
167
|
+
ticks?: number;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Element configuration (generic)
|
|
171
|
+
*/
|
|
172
|
+
export interface ElementConfig {
|
|
173
|
+
id: string;
|
|
174
|
+
type: string;
|
|
175
|
+
[key: string]: unknown;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Annotation configuration
|
|
179
|
+
*/
|
|
180
|
+
export interface AnnotationConfig {
|
|
181
|
+
id: string;
|
|
182
|
+
text: string;
|
|
183
|
+
x: number | string;
|
|
184
|
+
y: number | string;
|
|
185
|
+
[key: string]: unknown;
|
|
186
|
+
}
|
|
187
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA;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AC;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACnB,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACnB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation Cache
|
|
3
|
+
*
|
|
4
|
+
* Caches validation results to avoid redundant computations.
|
|
5
|
+
* Uses LRU eviction to bound memory usage.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Cache statistics
|
|
9
|
+
*/
|
|
10
|
+
export interface CacheStats {
|
|
11
|
+
hits: number;
|
|
12
|
+
misses: number;
|
|
13
|
+
size: number;
|
|
14
|
+
maxSize: number;
|
|
15
|
+
hitRate: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Validation cache with LRU eviction
|
|
19
|
+
*/
|
|
20
|
+
export declare class ValidationCache<T> {
|
|
21
|
+
private cache;
|
|
22
|
+
private hits;
|
|
23
|
+
private misses;
|
|
24
|
+
private maxSize;
|
|
25
|
+
private ttl;
|
|
26
|
+
/**
|
|
27
|
+
* Create a validation cache
|
|
28
|
+
* @param maxSize Maximum number of entries
|
|
29
|
+
* @param ttl Time-to-live in milliseconds (0 = no expiration)
|
|
30
|
+
*/
|
|
31
|
+
constructor(maxSize?: number, ttl?: number);
|
|
32
|
+
/**
|
|
33
|
+
* Generate cache key from input
|
|
34
|
+
*/
|
|
35
|
+
private generateKey;
|
|
36
|
+
/**
|
|
37
|
+
* Check if entry is expired
|
|
38
|
+
*/
|
|
39
|
+
private isExpired;
|
|
40
|
+
/**
|
|
41
|
+
* Evict least recently used entries
|
|
42
|
+
*/
|
|
43
|
+
private evictLRU;
|
|
44
|
+
/**
|
|
45
|
+
* Get a cached value
|
|
46
|
+
*/
|
|
47
|
+
get(input: unknown): T | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* Set a cached value
|
|
50
|
+
*/
|
|
51
|
+
set(input: unknown, value: T): void;
|
|
52
|
+
/**
|
|
53
|
+
* Get or compute a value
|
|
54
|
+
*/
|
|
55
|
+
getOrCompute(input: unknown, compute: () => T): T;
|
|
56
|
+
/**
|
|
57
|
+
* Get or compute a value (async)
|
|
58
|
+
*/
|
|
59
|
+
getOrComputeAsync(input: unknown, compute: () => Promise<T>): Promise<T>;
|
|
60
|
+
/**
|
|
61
|
+
* Check if key exists
|
|
62
|
+
*/
|
|
63
|
+
has(input: unknown): boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Delete a cached value
|
|
66
|
+
*/
|
|
67
|
+
delete(input: unknown): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Clear all cached values
|
|
70
|
+
*/
|
|
71
|
+
clear(): void;
|
|
72
|
+
/**
|
|
73
|
+
* Get cache statistics
|
|
74
|
+
*/
|
|
75
|
+
getStats(): CacheStats;
|
|
76
|
+
/**
|
|
77
|
+
* Remove expired entries
|
|
78
|
+
*/
|
|
79
|
+
cleanup(): number;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Pre-configured caches for common use cases
|
|
83
|
+
*/
|
|
84
|
+
export declare const expressionCache: ValidationCache<{
|
|
85
|
+
valid: boolean;
|
|
86
|
+
errors: unknown[];
|
|
87
|
+
symbols: string[];
|
|
88
|
+
}>;
|
|
89
|
+
export declare const documentCache: ValidationCache<{
|
|
90
|
+
valid: boolean;
|
|
91
|
+
errors: unknown[];
|
|
92
|
+
warnings: unknown[];
|
|
93
|
+
}>;
|
|
94
|
+
export declare const templateCache: ValidationCache<unknown>;
|
|
95
|
+
/**
|
|
96
|
+
* Create a validation cache with custom configuration
|
|
97
|
+
*/
|
|
98
|
+
export declare function createValidationCache<T>(maxSize?: number, ttl?: number): ValidationCache<T>;
|
|
99
|
+
//# sourceMappingURL=cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../src/utils/cache.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAWH;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,qBAAa,eAAe,CAAC,CAAC;IAC5B,OAAO,CAAC,KAAK,CAAyC;IACtD,OAAO,CAAC,IAAI,CAAK;IACjB,OAAO,CAAC,MAAM,CAAK;IACnB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,GAAG,CAAS;IAEpB;;;;OAIG;gBACS,OAAO,SAAO,EAAE,GAAG,SAAI;IAKnC;;OAEG;IACH,OAAO,CAAC,WAAW;IAOnB;;OAEG;IACH,OAAO,CAAC,SAAS;IAKjB;;OAEG;IACH,OAAO,CAAC,QAAQ;IAmBhB;;OAEG;IACH,GAAG,CAAC,KAAK,EAAE,OAAO,GAAG,CAAC,GAAG,SAAS;IAoBlC;;OAEG;IACH,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI;IAenC;;OAEG;IACH,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC;IAWjD;;OAEG;IACG,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAW9E;;OAEG;IACH,GAAG,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO;IAa5B;;OAEG;IACH,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO;IAK/B;;OAEG;IACH,KAAK,IAAI,IAAI;IAMb;;OAEG;IACH,QAAQ,IAAI,UAAU;IAWtB;;OAEG;IACH,OAAO,IAAI,MAAM;CAYlB;AAED;;GAEG;AAGH,eAAO,MAAM,eAAe;WACnB,OAAO;YACN,OAAO,EAAE;aACR,MAAM,EAAE;EACT,CAAC;AAGX,eAAO,MAAM,aAAa;WACjB,OAAO;YACN,OAAO,EAAE;cACP,OAAO,EAAE;EACP,CAAC;AAGf,eAAO,MAAM,aAAa,0BAAuC,CAAC;AAElE;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,EACrC,OAAO,SAAO,EACd,GAAG,SAAI,GACN,eAAe,CAAC,CAAC,CAAC,CAEpB"}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation Cache
|
|
3
|
+
*
|
|
4
|
+
* Caches validation results to avoid redundant computations.
|
|
5
|
+
* Uses LRU eviction to bound memory usage.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Validation cache with LRU eviction
|
|
9
|
+
*/
|
|
10
|
+
export class ValidationCache {
|
|
11
|
+
cache = new Map();
|
|
12
|
+
hits = 0;
|
|
13
|
+
misses = 0;
|
|
14
|
+
maxSize;
|
|
15
|
+
ttl;
|
|
16
|
+
/**
|
|
17
|
+
* Create a validation cache
|
|
18
|
+
* @param maxSize Maximum number of entries
|
|
19
|
+
* @param ttl Time-to-live in milliseconds (0 = no expiration)
|
|
20
|
+
*/
|
|
21
|
+
constructor(maxSize = 1000, ttl = 0) {
|
|
22
|
+
this.maxSize = maxSize;
|
|
23
|
+
this.ttl = ttl;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Generate cache key from input
|
|
27
|
+
*/
|
|
28
|
+
generateKey(input) {
|
|
29
|
+
if (typeof input === 'string') {
|
|
30
|
+
return input;
|
|
31
|
+
}
|
|
32
|
+
return JSON.stringify(input);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Check if entry is expired
|
|
36
|
+
*/
|
|
37
|
+
isExpired(entry) {
|
|
38
|
+
if (this.ttl === 0)
|
|
39
|
+
return false;
|
|
40
|
+
return Date.now() - entry.timestamp > this.ttl;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Evict least recently used entries
|
|
44
|
+
*/
|
|
45
|
+
evictLRU() {
|
|
46
|
+
if (this.cache.size < this.maxSize)
|
|
47
|
+
return;
|
|
48
|
+
// Find entry with lowest hit count (simplified LRU)
|
|
49
|
+
let minHits = Infinity;
|
|
50
|
+
let minKey = null;
|
|
51
|
+
for (const [key, entry] of this.cache) {
|
|
52
|
+
if (entry.hits < minHits) {
|
|
53
|
+
minHits = entry.hits;
|
|
54
|
+
minKey = key;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (minKey) {
|
|
58
|
+
this.cache.delete(minKey);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Get a cached value
|
|
63
|
+
*/
|
|
64
|
+
get(input) {
|
|
65
|
+
const key = this.generateKey(input);
|
|
66
|
+
const entry = this.cache.get(key);
|
|
67
|
+
if (!entry) {
|
|
68
|
+
this.misses++;
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
if (this.isExpired(entry)) {
|
|
72
|
+
this.cache.delete(key);
|
|
73
|
+
this.misses++;
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
this.hits++;
|
|
77
|
+
entry.hits++;
|
|
78
|
+
return entry.value;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Set a cached value
|
|
82
|
+
*/
|
|
83
|
+
set(input, value) {
|
|
84
|
+
const key = this.generateKey(input);
|
|
85
|
+
// Evict if at capacity
|
|
86
|
+
if (!this.cache.has(key)) {
|
|
87
|
+
this.evictLRU();
|
|
88
|
+
}
|
|
89
|
+
this.cache.set(key, {
|
|
90
|
+
value,
|
|
91
|
+
timestamp: Date.now(),
|
|
92
|
+
hits: 1,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Get or compute a value
|
|
97
|
+
*/
|
|
98
|
+
getOrCompute(input, compute) {
|
|
99
|
+
const cached = this.get(input);
|
|
100
|
+
if (cached !== undefined) {
|
|
101
|
+
return cached;
|
|
102
|
+
}
|
|
103
|
+
const value = compute();
|
|
104
|
+
this.set(input, value);
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Get or compute a value (async)
|
|
109
|
+
*/
|
|
110
|
+
async getOrComputeAsync(input, compute) {
|
|
111
|
+
const cached = this.get(input);
|
|
112
|
+
if (cached !== undefined) {
|
|
113
|
+
return cached;
|
|
114
|
+
}
|
|
115
|
+
const value = await compute();
|
|
116
|
+
this.set(input, value);
|
|
117
|
+
return value;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Check if key exists
|
|
121
|
+
*/
|
|
122
|
+
has(input) {
|
|
123
|
+
const key = this.generateKey(input);
|
|
124
|
+
const entry = this.cache.get(key);
|
|
125
|
+
if (!entry)
|
|
126
|
+
return false;
|
|
127
|
+
if (this.isExpired(entry)) {
|
|
128
|
+
this.cache.delete(key);
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Delete a cached value
|
|
135
|
+
*/
|
|
136
|
+
delete(input) {
|
|
137
|
+
const key = this.generateKey(input);
|
|
138
|
+
return this.cache.delete(key);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Clear all cached values
|
|
142
|
+
*/
|
|
143
|
+
clear() {
|
|
144
|
+
this.cache.clear();
|
|
145
|
+
this.hits = 0;
|
|
146
|
+
this.misses = 0;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Get cache statistics
|
|
150
|
+
*/
|
|
151
|
+
getStats() {
|
|
152
|
+
const total = this.hits + this.misses;
|
|
153
|
+
return {
|
|
154
|
+
hits: this.hits,
|
|
155
|
+
misses: this.misses,
|
|
156
|
+
size: this.cache.size,
|
|
157
|
+
maxSize: this.maxSize,
|
|
158
|
+
hitRate: total > 0 ? this.hits / total : 0,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Remove expired entries
|
|
163
|
+
*/
|
|
164
|
+
cleanup() {
|
|
165
|
+
if (this.ttl === 0)
|
|
166
|
+
return 0;
|
|
167
|
+
let removed = 0;
|
|
168
|
+
for (const [key, entry] of this.cache) {
|
|
169
|
+
if (this.isExpired(entry)) {
|
|
170
|
+
this.cache.delete(key);
|
|
171
|
+
removed++;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return removed;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Pre-configured caches for common use cases
|
|
179
|
+
*/
|
|
180
|
+
// Expression validation cache (expressions don't change often)
|
|
181
|
+
export const expressionCache = new ValidationCache(500, 0);
|
|
182
|
+
// Document validation cache (shorter TTL as documents may be modified)
|
|
183
|
+
export const documentCache = new ValidationCache(100, 60000); // 1 minute TTL
|
|
184
|
+
// Template cache (templates are static)
|
|
185
|
+
export const templateCache = new ValidationCache(200, 0);
|
|
186
|
+
/**
|
|
187
|
+
* Create a validation cache with custom configuration
|
|
188
|
+
*/
|
|
189
|
+
export function createValidationCache(maxSize = 1000, ttl = 0) {
|
|
190
|
+
return new ValidationCache(maxSize, ttl);
|
|
191
|
+
}
|
|
192
|
+
//# sourceMappingURL=cache.js.map
|