@hiveforge/hivemind-mcp 2.2.0 → 2.4.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/README.md +114 -25
- package/dist/cli.js +193 -2
- package/dist/cli.js.map +1 -1
- package/dist/graph/builder.d.ts +7 -3
- package/dist/graph/builder.d.ts.map +1 -1
- package/dist/graph/builder.js +51 -21
- package/dist/graph/builder.js.map +1 -1
- package/dist/mcp/index.d.ts +8 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +8 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/tool-generator.d.ts +110 -0
- package/dist/mcp/tool-generator.d.ts.map +1 -0
- package/dist/mcp/tool-generator.js +281 -0
- package/dist/mcp/tool-generator.js.map +1 -0
- package/dist/parser/markdown.d.ts +9 -0
- package/dist/parser/markdown.d.ts.map +1 -1
- package/dist/parser/markdown.js +13 -2
- package/dist/parser/markdown.js.map +1 -1
- package/dist/search/engine.d.ts +9 -1
- package/dist/search/engine.d.ts.map +1 -1
- package/dist/search/engine.js +16 -4
- package/dist/search/engine.js.map +1 -1
- package/dist/server.d.ts +8 -4
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +66 -199
- package/dist/server.js.map +1 -1
- package/dist/templates/builtin/people-management.d.ts +18 -0
- package/dist/templates/builtin/people-management.d.ts.map +1 -0
- package/dist/templates/builtin/people-management.js +523 -0
- package/dist/templates/builtin/people-management.js.map +1 -0
- package/dist/templates/builtin/research.d.ts +18 -0
- package/dist/templates/builtin/research.d.ts.map +1 -0
- package/dist/templates/builtin/research.js +349 -0
- package/dist/templates/builtin/research.js.map +1 -0
- package/dist/templates/builtin/worldbuilding.d.ts +20 -0
- package/dist/templates/builtin/worldbuilding.d.ts.map +1 -0
- package/dist/templates/builtin/worldbuilding.js +602 -0
- package/dist/templates/builtin/worldbuilding.js.map +1 -0
- package/dist/templates/detector.d.ts +51 -0
- package/dist/templates/detector.d.ts.map +1 -0
- package/dist/templates/detector.js +71 -0
- package/dist/templates/detector.js.map +1 -0
- package/dist/templates/folder-mapper.d.ts +66 -0
- package/dist/templates/folder-mapper.d.ts.map +1 -0
- package/dist/templates/folder-mapper.js +148 -0
- package/dist/templates/folder-mapper.js.map +1 -0
- package/dist/templates/index.d.ts +15 -0
- package/dist/templates/index.d.ts.map +1 -0
- package/dist/templates/index.js +15 -0
- package/dist/templates/index.js.map +1 -0
- package/dist/templates/loader.d.ts +117 -0
- package/dist/templates/loader.d.ts.map +1 -0
- package/dist/templates/loader.js +208 -0
- package/dist/templates/loader.js.map +1 -0
- package/dist/templates/registry.d.ts +127 -0
- package/dist/templates/registry.d.ts.map +1 -0
- package/dist/templates/registry.js +205 -0
- package/dist/templates/registry.js.map +1 -0
- package/dist/templates/schema-factory.d.ts +76 -0
- package/dist/templates/schema-factory.d.ts.map +1 -0
- package/dist/templates/schema-factory.js +171 -0
- package/dist/templates/schema-factory.js.map +1 -0
- package/dist/templates/types.d.ts +40 -0
- package/dist/templates/types.d.ts.map +1 -1
- package/dist/templates/validator.d.ts +310 -0
- package/dist/templates/validator.d.ts.map +1 -0
- package/dist/templates/validator.js +169 -0
- package/dist/templates/validator.js.map +1 -0
- package/dist/types/index.d.ts +57 -21
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +39 -15
- package/dist/types/index.js.map +1 -1
- package/dist/vault/reader.d.ts.map +1 -1
- package/dist/vault/reader.js +15 -1
- package/dist/vault/reader.js.map +1 -1
- package/package.json +7 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../../src/templates/loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AA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AoD,cAAc,EAAE,CACnG,CAAC;IACJ,CAAC;IAED,OAAO,aAAa,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;AAC7C,CAAC"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Template registry for managing template lifecycle.
|
|
3
|
+
*
|
|
4
|
+
* Provides centralized template management with O(1) lookups
|
|
5
|
+
* and validation on registration.
|
|
6
|
+
*/
|
|
7
|
+
import type { TemplateDefinition, TemplateRegistryEntry, EntityTypeConfig, RelationshipTypeConfig } from './types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Source of a template registration.
|
|
10
|
+
*/
|
|
11
|
+
type TemplateSource = 'builtin' | 'config';
|
|
12
|
+
/**
|
|
13
|
+
* Manages registered templates with fast lookups.
|
|
14
|
+
*
|
|
15
|
+
* Singleton pattern ensures consistent state across the application.
|
|
16
|
+
* Validates templates on registration and provides O(1) entity type lookups.
|
|
17
|
+
*/
|
|
18
|
+
export declare class TemplateRegistry {
|
|
19
|
+
/** Map from template ID to registry entry */
|
|
20
|
+
private templates;
|
|
21
|
+
/** ID of the currently active template */
|
|
22
|
+
private activeTemplateId;
|
|
23
|
+
/**
|
|
24
|
+
* Registers a template in the registry.
|
|
25
|
+
*
|
|
26
|
+
* Validates the template definition and creates optimized lookup maps.
|
|
27
|
+
*
|
|
28
|
+
* @param template - Template definition to register
|
|
29
|
+
* @param source - Source of the template (builtin or config)
|
|
30
|
+
* @throws {Error} If template with this ID is already registered
|
|
31
|
+
*/
|
|
32
|
+
register(template: TemplateDefinition, source: TemplateSource): void;
|
|
33
|
+
/**
|
|
34
|
+
* Sets the active template by ID.
|
|
35
|
+
*
|
|
36
|
+
* @param templateId - ID of template to activate
|
|
37
|
+
* @throws {Error} If template ID is not registered
|
|
38
|
+
*/
|
|
39
|
+
activate(templateId: string): void;
|
|
40
|
+
/**
|
|
41
|
+
* Gets the currently active template.
|
|
42
|
+
*
|
|
43
|
+
* @returns Active template entry, or null if no template is active
|
|
44
|
+
*/
|
|
45
|
+
getActive(): TemplateRegistryEntry | null;
|
|
46
|
+
/**
|
|
47
|
+
* Gets a template by ID.
|
|
48
|
+
*
|
|
49
|
+
* @param templateId - Template ID to retrieve
|
|
50
|
+
* @returns Template entry if found, undefined otherwise
|
|
51
|
+
*/
|
|
52
|
+
get(templateId: string): TemplateRegistryEntry | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Checks if a template is registered.
|
|
55
|
+
*
|
|
56
|
+
* @param templateId - Template ID to check
|
|
57
|
+
* @returns True if template exists in registry
|
|
58
|
+
*/
|
|
59
|
+
has(templateId: string): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Lists all registered template IDs.
|
|
62
|
+
*
|
|
63
|
+
* @returns Array of template IDs
|
|
64
|
+
*/
|
|
65
|
+
listTemplates(): string[];
|
|
66
|
+
/**
|
|
67
|
+
* Gets an entity type config from the active template.
|
|
68
|
+
*
|
|
69
|
+
* O(1) lookup using the entity type map.
|
|
70
|
+
*
|
|
71
|
+
* @param name - Entity type name to retrieve
|
|
72
|
+
* @returns Entity type config if found, undefined otherwise
|
|
73
|
+
* @throws {Error} If no template is active
|
|
74
|
+
*/
|
|
75
|
+
getEntityType(name: string): EntityTypeConfig | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* Gets all entity type configs from the active template.
|
|
78
|
+
*
|
|
79
|
+
* @returns Array of entity type configs
|
|
80
|
+
* @throws {Error} If no template is active
|
|
81
|
+
*/
|
|
82
|
+
getEntityTypes(): EntityTypeConfig[];
|
|
83
|
+
/**
|
|
84
|
+
* Gets a relationship type config from the active template.
|
|
85
|
+
*
|
|
86
|
+
* O(1) lookup using the relationship type map.
|
|
87
|
+
*
|
|
88
|
+
* @param id - Relationship type ID to retrieve
|
|
89
|
+
* @returns Relationship type config if found, undefined otherwise
|
|
90
|
+
* @throws {Error} If no template is active
|
|
91
|
+
*/
|
|
92
|
+
getRelationshipType(id: string): RelationshipTypeConfig | undefined;
|
|
93
|
+
/**
|
|
94
|
+
* Gets all relationship type configs from the active template.
|
|
95
|
+
*
|
|
96
|
+
* @returns Array of relationship type configs
|
|
97
|
+
* @throws {Error} If no template is active
|
|
98
|
+
*/
|
|
99
|
+
getRelationshipTypes(): RelationshipTypeConfig[];
|
|
100
|
+
/**
|
|
101
|
+
* Gets valid relationship types for a source-target entity type pair.
|
|
102
|
+
*
|
|
103
|
+
* Returns all relationship types where:
|
|
104
|
+
* - sourceTypes includes the source entity type (or is 'any')
|
|
105
|
+
* - targetTypes includes the target entity type (or is 'any')
|
|
106
|
+
*
|
|
107
|
+
* @param sourceType - Source entity type name
|
|
108
|
+
* @param targetType - Target entity type name
|
|
109
|
+
* @returns Array of valid relationship type configs
|
|
110
|
+
* @throws {Error} If no template is active
|
|
111
|
+
*/
|
|
112
|
+
getValidRelationships(sourceType: string, targetType: string): RelationshipTypeConfig[];
|
|
113
|
+
/**
|
|
114
|
+
* Clears all registered templates and resets active template.
|
|
115
|
+
*
|
|
116
|
+
* Primarily for testing purposes.
|
|
117
|
+
*/
|
|
118
|
+
clear(): void;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Singleton instance of the template registry.
|
|
122
|
+
*
|
|
123
|
+
* Use this throughout the application for consistent template state.
|
|
124
|
+
*/
|
|
125
|
+
export declare const templateRegistry: TemplateRegistry;
|
|
126
|
+
export {};
|
|
127
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/templates/registry.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,kBAAkB,EAClB,qBAAqB,EACrB,gBAAgB,EAChB,sBAAsB,EACvB,MAAM,YAAY,CAAC;AAEpB;;GAEG;AACH,KAAK,cAAc,GAAG,SAAS,GAAG,QAAQ,CAAC;AAE3C;;;;;GAKG;AACH,qBAAa,gBAAgB;IAC3B,6CAA6C;IAC7C,OAAO,CAAC,SAAS,CAA4C;IAE7D,0CAA0C;IAC1C,OAAO,CAAC,gBAAgB,CAAuB;IAE/C;;;;;;;;OAQG;IACH,QAAQ,CAAC,QAAQ,EAAE,kBAAkB,EAAE,MAAM,EAAE,cAAc,GAAG,IAAI;IAuCpE;;;;;OAKG;IACH,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;IAUlC;;;;OAIG;IACH,SAAS,IAAI,qBAAqB,GAAG,IAAI;IAOzC;;;;;OAKG;IACH,GAAG,CAAC,UAAU,EAAE,MAAM,GAAG,qBAAqB,GAAG,SAAS;IAI1D;;;;;OAKG;IACH,GAAG,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO;IAIhC;;;;OAIG;IACH,aAAa,IAAI,MAAM,EAAE;IAIzB;;;;;;;;OAQG;IACH,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS;IAQzD;;;;;OAKG;IACH,cAAc,IAAI,gBAAgB,EAAE;IAQpC;;;;;;;;OAQG;IACH,mBAAmB,CAAC,EAAE,EAAE,MAAM,GAAG,sBAAsB,GAAG,SAAS;IAQnE;;;;;OAKG;IACH,oBAAoB,IAAI,sBAAsB,EAAE;IAQhD;;;;;;;;;;;OAWG;IACH,qBAAqB,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,sBAAsB,EAAE;IAgBvF;;;;OAIG;IACH,KAAK,IAAI,IAAI;CAId;AAED;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,kBAAyB,CAAC"}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Template registry for managing template lifecycle.
|
|
3
|
+
*
|
|
4
|
+
* Provides centralized template management with O(1) lookups
|
|
5
|
+
* and validation on registration.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Manages registered templates with fast lookups.
|
|
9
|
+
*
|
|
10
|
+
* Singleton pattern ensures consistent state across the application.
|
|
11
|
+
* Validates templates on registration and provides O(1) entity type lookups.
|
|
12
|
+
*/
|
|
13
|
+
export class TemplateRegistry {
|
|
14
|
+
/** Map from template ID to registry entry */
|
|
15
|
+
templates = new Map();
|
|
16
|
+
/** ID of the currently active template */
|
|
17
|
+
activeTemplateId = null;
|
|
18
|
+
/**
|
|
19
|
+
* Registers a template in the registry.
|
|
20
|
+
*
|
|
21
|
+
* Validates the template definition and creates optimized lookup maps.
|
|
22
|
+
*
|
|
23
|
+
* @param template - Template definition to register
|
|
24
|
+
* @param source - Source of the template (builtin or config)
|
|
25
|
+
* @throws {Error} If template with this ID is already registered
|
|
26
|
+
*/
|
|
27
|
+
register(template, source) {
|
|
28
|
+
if (this.templates.has(template.id)) {
|
|
29
|
+
throw new Error(`Template "${template.id}" is already registered`);
|
|
30
|
+
}
|
|
31
|
+
// Create entity type lookup map for O(1) access
|
|
32
|
+
const entityTypeMap = new Map();
|
|
33
|
+
for (const entityType of template.entityTypes) {
|
|
34
|
+
if (entityTypeMap.has(entityType.name)) {
|
|
35
|
+
throw new Error(`Duplicate entity type "${entityType.name}" in template "${template.id}"`);
|
|
36
|
+
}
|
|
37
|
+
entityTypeMap.set(entityType.name, entityType);
|
|
38
|
+
}
|
|
39
|
+
// Create relationship type lookup map for O(1) access
|
|
40
|
+
const relationshipTypeMap = new Map();
|
|
41
|
+
if (template.relationshipTypes) {
|
|
42
|
+
for (const relType of template.relationshipTypes) {
|
|
43
|
+
if (relationshipTypeMap.has(relType.id)) {
|
|
44
|
+
throw new Error(`Duplicate relationship type "${relType.id}" in template "${template.id}"`);
|
|
45
|
+
}
|
|
46
|
+
relationshipTypeMap.set(relType.id, relType);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const entry = {
|
|
50
|
+
...template,
|
|
51
|
+
source,
|
|
52
|
+
entityTypeMap,
|
|
53
|
+
relationshipTypeMap,
|
|
54
|
+
};
|
|
55
|
+
this.templates.set(template.id, entry);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Sets the active template by ID.
|
|
59
|
+
*
|
|
60
|
+
* @param templateId - ID of template to activate
|
|
61
|
+
* @throws {Error} If template ID is not registered
|
|
62
|
+
*/
|
|
63
|
+
activate(templateId) {
|
|
64
|
+
if (!this.templates.has(templateId)) {
|
|
65
|
+
throw new Error(`Cannot activate template "${templateId}": not registered. ` +
|
|
66
|
+
`Available templates: ${Array.from(this.templates.keys()).join(', ')}`);
|
|
67
|
+
}
|
|
68
|
+
this.activeTemplateId = templateId;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Gets the currently active template.
|
|
72
|
+
*
|
|
73
|
+
* @returns Active template entry, or null if no template is active
|
|
74
|
+
*/
|
|
75
|
+
getActive() {
|
|
76
|
+
if (this.activeTemplateId === null) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
return this.templates.get(this.activeTemplateId) ?? null;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Gets a template by ID.
|
|
83
|
+
*
|
|
84
|
+
* @param templateId - Template ID to retrieve
|
|
85
|
+
* @returns Template entry if found, undefined otherwise
|
|
86
|
+
*/
|
|
87
|
+
get(templateId) {
|
|
88
|
+
return this.templates.get(templateId);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Checks if a template is registered.
|
|
92
|
+
*
|
|
93
|
+
* @param templateId - Template ID to check
|
|
94
|
+
* @returns True if template exists in registry
|
|
95
|
+
*/
|
|
96
|
+
has(templateId) {
|
|
97
|
+
return this.templates.has(templateId);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Lists all registered template IDs.
|
|
101
|
+
*
|
|
102
|
+
* @returns Array of template IDs
|
|
103
|
+
*/
|
|
104
|
+
listTemplates() {
|
|
105
|
+
return Array.from(this.templates.keys());
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Gets an entity type config from the active template.
|
|
109
|
+
*
|
|
110
|
+
* O(1) lookup using the entity type map.
|
|
111
|
+
*
|
|
112
|
+
* @param name - Entity type name to retrieve
|
|
113
|
+
* @returns Entity type config if found, undefined otherwise
|
|
114
|
+
* @throws {Error} If no template is active
|
|
115
|
+
*/
|
|
116
|
+
getEntityType(name) {
|
|
117
|
+
const active = this.getActive();
|
|
118
|
+
if (!active) {
|
|
119
|
+
throw new Error('Cannot get entity type: no active template');
|
|
120
|
+
}
|
|
121
|
+
return active.entityTypeMap.get(name);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Gets all entity type configs from the active template.
|
|
125
|
+
*
|
|
126
|
+
* @returns Array of entity type configs
|
|
127
|
+
* @throws {Error} If no template is active
|
|
128
|
+
*/
|
|
129
|
+
getEntityTypes() {
|
|
130
|
+
const active = this.getActive();
|
|
131
|
+
if (!active) {
|
|
132
|
+
throw new Error('Cannot get entity types: no active template');
|
|
133
|
+
}
|
|
134
|
+
return active.entityTypes;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Gets a relationship type config from the active template.
|
|
138
|
+
*
|
|
139
|
+
* O(1) lookup using the relationship type map.
|
|
140
|
+
*
|
|
141
|
+
* @param id - Relationship type ID to retrieve
|
|
142
|
+
* @returns Relationship type config if found, undefined otherwise
|
|
143
|
+
* @throws {Error} If no template is active
|
|
144
|
+
*/
|
|
145
|
+
getRelationshipType(id) {
|
|
146
|
+
const active = this.getActive();
|
|
147
|
+
if (!active) {
|
|
148
|
+
throw new Error('Cannot get relationship type: no active template');
|
|
149
|
+
}
|
|
150
|
+
return active.relationshipTypeMap.get(id);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Gets all relationship type configs from the active template.
|
|
154
|
+
*
|
|
155
|
+
* @returns Array of relationship type configs
|
|
156
|
+
* @throws {Error} If no template is active
|
|
157
|
+
*/
|
|
158
|
+
getRelationshipTypes() {
|
|
159
|
+
const active = this.getActive();
|
|
160
|
+
if (!active) {
|
|
161
|
+
throw new Error('Cannot get relationship types: no active template');
|
|
162
|
+
}
|
|
163
|
+
return active.relationshipTypes || [];
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Gets valid relationship types for a source-target entity type pair.
|
|
167
|
+
*
|
|
168
|
+
* Returns all relationship types where:
|
|
169
|
+
* - sourceTypes includes the source entity type (or is 'any')
|
|
170
|
+
* - targetTypes includes the target entity type (or is 'any')
|
|
171
|
+
*
|
|
172
|
+
* @param sourceType - Source entity type name
|
|
173
|
+
* @param targetType - Target entity type name
|
|
174
|
+
* @returns Array of valid relationship type configs
|
|
175
|
+
* @throws {Error} If no template is active
|
|
176
|
+
*/
|
|
177
|
+
getValidRelationships(sourceType, targetType) {
|
|
178
|
+
const active = this.getActive();
|
|
179
|
+
if (!active) {
|
|
180
|
+
throw new Error('Cannot get valid relationships: no active template');
|
|
181
|
+
}
|
|
182
|
+
const relationshipTypes = active.relationshipTypes || [];
|
|
183
|
+
return relationshipTypes.filter((rel) => {
|
|
184
|
+
const sourceValid = rel.sourceTypes === 'any' || rel.sourceTypes.includes(sourceType);
|
|
185
|
+
const targetValid = rel.targetTypes === 'any' || rel.targetTypes.includes(targetType);
|
|
186
|
+
return sourceValid && targetValid;
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Clears all registered templates and resets active template.
|
|
191
|
+
*
|
|
192
|
+
* Primarily for testing purposes.
|
|
193
|
+
*/
|
|
194
|
+
clear() {
|
|
195
|
+
this.templates.clear();
|
|
196
|
+
this.activeTemplateId = null;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Singleton instance of the template registry.
|
|
201
|
+
*
|
|
202
|
+
* Use this throughout the application for consistent template state.
|
|
203
|
+
*/
|
|
204
|
+
export const templateRegistry = new TemplateRegistry();
|
|
205
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/templates/registry.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAcH;;;;;GA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C,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE;YACtC,MAAM,WAAW,GACf,GAAG,CAAC,WAAW,KAAK,KAAK,IAAI,GAAG,CAAC,WAAW,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;YACpE,MAAM,WAAW,GACf,GAAG,CAAC,WAAW,KAAK,KAAK,IAAI,GAAG,CAAC,WAAW,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;YACpE,OAAO,WAAW,IAAI,WAAW,CAAC;QACpC,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,KAAK;QACH,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;QACvB,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC;IAC/B,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,gBAAgB,EAAE,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema factory for dynamically generating Zod schemas from entity type configurations.
|
|
3
|
+
*
|
|
4
|
+
* This module is the core of the template system's runtime validation - it transforms
|
|
5
|
+
* user-defined EntityTypeConfig objects into Zod schemas that extend the base entity
|
|
6
|
+
* schema with custom fields.
|
|
7
|
+
*/
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
import type { EntityTypeConfig } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Create a complete Zod schema for an entity type from its configuration.
|
|
12
|
+
*
|
|
13
|
+
* The generated schema extends BaseFrontmatterSchema with:
|
|
14
|
+
* - Type literal enforcement (type must match entity type name)
|
|
15
|
+
* - All custom fields from the entity config
|
|
16
|
+
*
|
|
17
|
+
* @param config - Entity type configuration
|
|
18
|
+
* @returns Zod schema that validates entities of this type
|
|
19
|
+
*/
|
|
20
|
+
export declare function createEntitySchema(config: EntityTypeConfig): z.ZodObject<any>;
|
|
21
|
+
/**
|
|
22
|
+
* Factory class for managing schema generation with caching.
|
|
23
|
+
*
|
|
24
|
+
* Generates Zod schemas once per entity type and caches them for performance.
|
|
25
|
+
* Useful when validating multiple entities of the same type.
|
|
26
|
+
*/
|
|
27
|
+
export declare class SchemaFactory {
|
|
28
|
+
private schemaCache;
|
|
29
|
+
/**
|
|
30
|
+
* Get or create a schema for an entity type.
|
|
31
|
+
*
|
|
32
|
+
* If a schema for this entity type has been generated before, returns the
|
|
33
|
+
* cached version. Otherwise, generates and caches a new schema.
|
|
34
|
+
*
|
|
35
|
+
* @param config - Entity type configuration
|
|
36
|
+
* @returns Cached or newly generated Zod schema
|
|
37
|
+
*/
|
|
38
|
+
getSchema(config: EntityTypeConfig): z.ZodObject<any>;
|
|
39
|
+
/**
|
|
40
|
+
* Generate schemas for multiple entity types at once.
|
|
41
|
+
*
|
|
42
|
+
* Useful for bulk schema generation (e.g., when loading a template).
|
|
43
|
+
* Returns a map of entity type name to schema.
|
|
44
|
+
*
|
|
45
|
+
* @param configs - Array of entity type configurations
|
|
46
|
+
* @returns Map from entity type name to Zod schema
|
|
47
|
+
*/
|
|
48
|
+
generateSchemas(configs: EntityTypeConfig[]): Map<string, z.ZodObject<any>>;
|
|
49
|
+
/**
|
|
50
|
+
* Clear the schema cache.
|
|
51
|
+
*
|
|
52
|
+
* Useful for testing or when entity type configurations change at runtime.
|
|
53
|
+
*/
|
|
54
|
+
clearCache(): void;
|
|
55
|
+
/**
|
|
56
|
+
* Get the number of cached schemas.
|
|
57
|
+
*
|
|
58
|
+
* @returns Number of schemas currently cached
|
|
59
|
+
*/
|
|
60
|
+
getCacheSize(): number;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Singleton instance of SchemaFactory for shared caching across the application.
|
|
64
|
+
*/
|
|
65
|
+
export declare const schemaFactory: SchemaFactory;
|
|
66
|
+
/**
|
|
67
|
+
* Type helper to infer TypeScript type from generated entity schema.
|
|
68
|
+
*
|
|
69
|
+
* Usage:
|
|
70
|
+
* ```ts
|
|
71
|
+
* const characterSchema = createEntitySchema(characterConfig);
|
|
72
|
+
* type Character = InferEntityType<typeof characterSchema>;
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export type InferEntityType<T extends z.ZodObject<any>> = z.infer<T>;
|
|
76
|
+
//# sourceMappingURL=schema-factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-factory.d.ts","sourceRoot":"","sources":["../../src/templates/schema-factory.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,gBAAgB,EAA0B,MAAM,YAAY,CAAC;AA2F3E;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,gBAAgB,GAAG,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAe7E;AAED;;;;;GAKG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,WAAW,CAA4C;IAE/D;;;;;;;;OAQG;IACH,SAAS,CAAC,MAAM,EAAE,gBAAgB,GAAG,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC;IAWrD;;;;;;;;OAQG;IACH,eAAe,CAAC,OAAO,EAAE,gBAAgB,EAAE,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IAU3E;;;;OAIG;IACH,UAAU,IAAI,IAAI;IAIlB;;;;OAIG;IACH,YAAY,IAAI,MAAM;CAGvB;AAED;;GAEG;AACH,eAAO,MAAM,aAAa,eAAsB,CAAC;AAEjD;;;;;;;;GAQG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema factory for dynamically generating Zod schemas from entity type configurations.
|
|
3
|
+
*
|
|
4
|
+
* This module is the core of the template system's runtime validation - it transforms
|
|
5
|
+
* user-defined EntityTypeConfig objects into Zod schemas that extend the base entity
|
|
6
|
+
* schema with custom fields.
|
|
7
|
+
*/
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
import { BaseFrontmatterSchema } from '../types/index.js';
|
|
10
|
+
/**
|
|
11
|
+
* Build a Zod schema for a primitive field type.
|
|
12
|
+
* Used for array item types and standalone fields.
|
|
13
|
+
*/
|
|
14
|
+
function buildPrimitiveSchema(type) {
|
|
15
|
+
switch (type) {
|
|
16
|
+
case 'string':
|
|
17
|
+
return z.string();
|
|
18
|
+
case 'number':
|
|
19
|
+
return z.number();
|
|
20
|
+
case 'boolean':
|
|
21
|
+
return z.boolean();
|
|
22
|
+
case 'date':
|
|
23
|
+
return z.string(); // ISO 8601 date string
|
|
24
|
+
case 'record':
|
|
25
|
+
return z.record(z.string(), z.any());
|
|
26
|
+
default:
|
|
27
|
+
// For enum/array types used as primitives, fall back to string
|
|
28
|
+
return z.string();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Build a Zod schema for a single field based on its configuration.
|
|
33
|
+
*
|
|
34
|
+
* Handles all field types: string, number, boolean, enum, array, date, record.
|
|
35
|
+
* Applies required/optional modifiers and default values.
|
|
36
|
+
*/
|
|
37
|
+
function buildFieldSchema(field) {
|
|
38
|
+
let schema;
|
|
39
|
+
switch (field.type) {
|
|
40
|
+
case 'string':
|
|
41
|
+
schema = z.string();
|
|
42
|
+
break;
|
|
43
|
+
case 'number':
|
|
44
|
+
schema = z.number();
|
|
45
|
+
break;
|
|
46
|
+
case 'boolean':
|
|
47
|
+
schema = z.boolean();
|
|
48
|
+
break;
|
|
49
|
+
case 'enum':
|
|
50
|
+
if (!field.enumValues || field.enumValues.length === 0) {
|
|
51
|
+
throw new Error(`Field "${field.name}" of type enum must specify enumValues`);
|
|
52
|
+
}
|
|
53
|
+
// Build enum schema from first value + rest
|
|
54
|
+
schema = z.enum([field.enumValues[0], ...field.enumValues.slice(1)]);
|
|
55
|
+
break;
|
|
56
|
+
case 'array':
|
|
57
|
+
// Determine the item type (default to string if not specified)
|
|
58
|
+
const itemType = field.arrayItemType || 'string';
|
|
59
|
+
const itemSchema = buildPrimitiveSchema(itemType);
|
|
60
|
+
schema = z.array(itemSchema);
|
|
61
|
+
break;
|
|
62
|
+
case 'date':
|
|
63
|
+
// ISO 8601 date string
|
|
64
|
+
schema = z.string();
|
|
65
|
+
break;
|
|
66
|
+
case 'record':
|
|
67
|
+
// Key-value pairs
|
|
68
|
+
schema = z.record(z.string(), z.any());
|
|
69
|
+
break;
|
|
70
|
+
default:
|
|
71
|
+
// Fallback for unknown types
|
|
72
|
+
schema = z.any();
|
|
73
|
+
}
|
|
74
|
+
// Apply optional/required modifiers
|
|
75
|
+
// Fields are optional by default unless explicitly marked required
|
|
76
|
+
if (!field.required) {
|
|
77
|
+
schema = schema.optional();
|
|
78
|
+
}
|
|
79
|
+
// Apply default value if provided
|
|
80
|
+
if (field.default !== undefined) {
|
|
81
|
+
schema = schema.default(field.default);
|
|
82
|
+
}
|
|
83
|
+
return schema;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Create a complete Zod schema for an entity type from its configuration.
|
|
87
|
+
*
|
|
88
|
+
* The generated schema extends BaseFrontmatterSchema with:
|
|
89
|
+
* - Type literal enforcement (type must match entity type name)
|
|
90
|
+
* - All custom fields from the entity config
|
|
91
|
+
*
|
|
92
|
+
* @param config - Entity type configuration
|
|
93
|
+
* @returns Zod schema that validates entities of this type
|
|
94
|
+
*/
|
|
95
|
+
export function createEntitySchema(config) {
|
|
96
|
+
// Build the shape for custom fields
|
|
97
|
+
const customFields = {};
|
|
98
|
+
for (const field of config.fields) {
|
|
99
|
+
customFields[field.name] = buildFieldSchema(field);
|
|
100
|
+
}
|
|
101
|
+
// Extend BaseFrontmatterSchema with custom fields and enforce type literal
|
|
102
|
+
const schema = BaseFrontmatterSchema.extend({
|
|
103
|
+
type: z.literal(config.name), // Enforce this specific entity type
|
|
104
|
+
...customFields,
|
|
105
|
+
});
|
|
106
|
+
return schema;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Factory class for managing schema generation with caching.
|
|
110
|
+
*
|
|
111
|
+
* Generates Zod schemas once per entity type and caches them for performance.
|
|
112
|
+
* Useful when validating multiple entities of the same type.
|
|
113
|
+
*/
|
|
114
|
+
export class SchemaFactory {
|
|
115
|
+
schemaCache = new Map();
|
|
116
|
+
/**
|
|
117
|
+
* Get or create a schema for an entity type.
|
|
118
|
+
*
|
|
119
|
+
* If a schema for this entity type has been generated before, returns the
|
|
120
|
+
* cached version. Otherwise, generates and caches a new schema.
|
|
121
|
+
*
|
|
122
|
+
* @param config - Entity type configuration
|
|
123
|
+
* @returns Cached or newly generated Zod schema
|
|
124
|
+
*/
|
|
125
|
+
getSchema(config) {
|
|
126
|
+
const cached = this.schemaCache.get(config.name);
|
|
127
|
+
if (cached) {
|
|
128
|
+
return cached;
|
|
129
|
+
}
|
|
130
|
+
const schema = createEntitySchema(config);
|
|
131
|
+
this.schemaCache.set(config.name, schema);
|
|
132
|
+
return schema;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Generate schemas for multiple entity types at once.
|
|
136
|
+
*
|
|
137
|
+
* Useful for bulk schema generation (e.g., when loading a template).
|
|
138
|
+
* Returns a map of entity type name to schema.
|
|
139
|
+
*
|
|
140
|
+
* @param configs - Array of entity type configurations
|
|
141
|
+
* @returns Map from entity type name to Zod schema
|
|
142
|
+
*/
|
|
143
|
+
generateSchemas(configs) {
|
|
144
|
+
const schemas = new Map();
|
|
145
|
+
for (const config of configs) {
|
|
146
|
+
schemas.set(config.name, this.getSchema(config));
|
|
147
|
+
}
|
|
148
|
+
return schemas;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Clear the schema cache.
|
|
152
|
+
*
|
|
153
|
+
* Useful for testing or when entity type configurations change at runtime.
|
|
154
|
+
*/
|
|
155
|
+
clearCache() {
|
|
156
|
+
this.schemaCache.clear();
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Get the number of cached schemas.
|
|
160
|
+
*
|
|
161
|
+
* @returns Number of schemas currently cached
|
|
162
|
+
*/
|
|
163
|
+
getCacheSize() {
|
|
164
|
+
return this.schemaCache.size;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Singleton instance of SchemaFactory for shared caching across the application.
|
|
169
|
+
*/
|
|
170
|
+
export const schemaFactory = new SchemaFactory();
|
|
171
|
+
//# sourceMappingURL=schema-factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-factory.js","sourceRoot":"","sources":["../../src/templates/schema-factory.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAE1D;;;GAGG;AACH,SAAS,oBAAoB,CAAC,IAAe;IAC3C,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,QAAQ;YACX,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC;QACpB,KAAK,QAAQ;YACX,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC;QACpB,KAAK,SAAS;YACZ,OAAO,CAAC,CAAC,OAAO,EAAE,CAAC;QACrB,KAAK,MAAM;YACT,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,uBAAuB;QAC5C,KAAK,QAAQ;YACX,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;QACvC;YACE,+DAA+D;YAC/D,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC;IACtB,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,KAAkB;IAC1C,IAAI,MAAoB,CAAC;IAEzB,QAAQ,KAA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;IAED;;;;;;;;OAQG;IACH,eAAe,CAAC,OAA2B;QACzC,MAAM,OAAO,GAAG,IAAI,GAAG,EAA4B,CAAC;QAEpD,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC7B,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;QACnD,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;IAED;;;;OAIG;IACH,UAAU;QACR,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC;IAC3B,CAAC;IAED;;;;OAIG;IACH,YAAY;QACV,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;IAC/B,CAAC;CACF;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,aAAa,EAAE,CAAC"}
|
|
@@ -39,6 +39,30 @@ export interface FieldConfig {
|
|
|
39
39
|
/** Human-readable description for documentation and MCP tool descriptions */
|
|
40
40
|
description?: string;
|
|
41
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Configuration for a relationship type between entity types.
|
|
44
|
+
*
|
|
45
|
+
* Defines valid relationships (e.g., "knows", "located_in") with
|
|
46
|
+
* source/target constraints, bidirectionality rules, and optional properties.
|
|
47
|
+
*/
|
|
48
|
+
export interface RelationshipTypeConfig {
|
|
49
|
+
/** Relationship identifier (snake_case, e.g., "knows", "allied_with") */
|
|
50
|
+
id: string;
|
|
51
|
+
/** Human-readable name (e.g., "Knows", "Allied With") */
|
|
52
|
+
displayName: string;
|
|
53
|
+
/** Description of this relationship type */
|
|
54
|
+
description?: string;
|
|
55
|
+
/** Valid source entity types, or 'any' for all */
|
|
56
|
+
sourceTypes: string[] | 'any';
|
|
57
|
+
/** Valid target entity types, or 'any' for all */
|
|
58
|
+
targetTypes: string[] | 'any';
|
|
59
|
+
/** Whether to auto-create reverse relationship */
|
|
60
|
+
bidirectional: boolean;
|
|
61
|
+
/** ID of reverse relationship type (required if bidirectional) */
|
|
62
|
+
reverseId?: string;
|
|
63
|
+
/** Optional properties stored on relationship edges */
|
|
64
|
+
properties?: FieldConfig[];
|
|
65
|
+
}
|
|
42
66
|
/**
|
|
43
67
|
* Configuration for a custom entity type.
|
|
44
68
|
*
|
|
@@ -76,6 +100,8 @@ export interface TemplateDefinition {
|
|
|
76
100
|
description?: string;
|
|
77
101
|
/** Entity types included in this template */
|
|
78
102
|
entityTypes: EntityTypeConfig[];
|
|
103
|
+
/** Custom relationship type definitions */
|
|
104
|
+
relationshipTypes?: RelationshipTypeConfig[];
|
|
79
105
|
}
|
|
80
106
|
/**
|
|
81
107
|
* Structure of the template configuration in config.json.
|
|
@@ -98,5 +124,19 @@ export interface TemplateRegistryEntry extends TemplateDefinition {
|
|
|
98
124
|
source: 'builtin' | 'config';
|
|
99
125
|
/** Fast lookup map from entity type name to config */
|
|
100
126
|
entityTypeMap: Map<string, EntityTypeConfig>;
|
|
127
|
+
/** Fast lookup map from relationship type ID to config */
|
|
128
|
+
relationshipTypeMap: Map<string, RelationshipTypeConfig>;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Mapping from folder name/pattern to entity type.
|
|
132
|
+
*
|
|
133
|
+
* Used by FolderMapper to infer entity types from file paths.
|
|
134
|
+
* Supports both exact folder name matches and glob-like patterns.
|
|
135
|
+
*/
|
|
136
|
+
export interface FolderMapping {
|
|
137
|
+
/** Folder name or pattern to match (case-insensitive) */
|
|
138
|
+
pattern: string;
|
|
139
|
+
/** Entity type to assign when pattern matches */
|
|
140
|
+
entityType: string;
|
|
101
141
|
}
|
|
102
142
|
//# sourceMappingURL=types.d.ts.map
|