@rizom/brain 0.2.0-alpha.62 → 0.2.0-alpha.64
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/dist/brain.js +1090 -920
- package/dist/entities.d.ts +71 -69
- package/dist/entities.js +2 -2
- package/dist/entities.js.map +3 -3
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/interfaces.d.ts +28 -28
- package/dist/interfaces.js.map +2 -2
- package/dist/plugins.d.ts +274 -122
- package/dist/plugins.js +99 -99
- package/dist/plugins.js.map +23 -20
- package/dist/services.d.ts +73 -61
- package/dist/services.js +2 -2
- package/dist/services.js.map +3 -3
- package/dist/site.js +107 -107
- package/dist/site.js.map +25 -22
- package/package.json +1 -1
package/dist/services.d.ts
CHANGED
|
@@ -89,6 +89,64 @@ declare class Logger {
|
|
|
89
89
|
setUseStderr(useStderr: boolean): void;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Context passed to all DataSource operations
|
|
94
|
+
* Contains internal state that should not be mixed with user query parameters
|
|
95
|
+
*/
|
|
96
|
+
interface BaseDataSourceContext {
|
|
97
|
+
/**
|
|
98
|
+
* Whether to filter to only published/complete content
|
|
99
|
+
* Set by site-builder: true for production, false for preview
|
|
100
|
+
*/
|
|
101
|
+
publishedOnly?: boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Scoped entity service that auto-applies publishedOnly filter
|
|
104
|
+
* Datasources should use this instead of their injected entityService
|
|
105
|
+
* to ensure consistent filtering behavior across environments
|
|
106
|
+
*/
|
|
107
|
+
entityService: EntityService;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* DataSource Interface
|
|
111
|
+
*
|
|
112
|
+
* Provides data for templates through fetch, generate, or transform operations.
|
|
113
|
+
* DataSources are registered in the DataSourceRegistry and referenced by templates
|
|
114
|
+
* via their dataSourceId property.
|
|
115
|
+
*/
|
|
116
|
+
interface DataSource {
|
|
117
|
+
/**
|
|
118
|
+
* Unique identifier for this data source
|
|
119
|
+
*/
|
|
120
|
+
id: string;
|
|
121
|
+
/**
|
|
122
|
+
* Human-readable name for this data source
|
|
123
|
+
*/
|
|
124
|
+
name: string;
|
|
125
|
+
/**
|
|
126
|
+
* Optional description of what this data source provides
|
|
127
|
+
*/
|
|
128
|
+
description?: string;
|
|
129
|
+
/**
|
|
130
|
+
* Optional: Fetch existing data
|
|
131
|
+
* Used by data sources that aggregate or retrieve data (e.g., dashboard stats, API data)
|
|
132
|
+
* DataSources validate output using the provided schema
|
|
133
|
+
* @param query - Query parameters for fetching data
|
|
134
|
+
* @param outputSchema - Schema for validating output data
|
|
135
|
+
* @param context - Context with environment
|
|
136
|
+
*/
|
|
137
|
+
fetch?: <T>(query: unknown, outputSchema: z.ZodSchema<T>, context: BaseDataSourceContext) => Promise<T>;
|
|
138
|
+
/**
|
|
139
|
+
* Optional: Generate new content
|
|
140
|
+
* Used by data sources that create content (e.g., AI-generated content, reports)
|
|
141
|
+
*/
|
|
142
|
+
generate?: <T>(request: unknown, schema: z.ZodSchema<T>) => Promise<T>;
|
|
143
|
+
/**
|
|
144
|
+
* Optional: Transform content between formats
|
|
145
|
+
* Used by data sources that convert content (e.g., markdown to HTML, data formatting)
|
|
146
|
+
*/
|
|
147
|
+
transform?: <T>(content: unknown, format: string, schema: z.ZodSchema<T>) => Promise<T>;
|
|
148
|
+
}
|
|
149
|
+
|
|
92
150
|
/**
|
|
93
151
|
* Options for entity mutation operations (create, update, upsert)
|
|
94
152
|
*/
|
|
@@ -198,6 +256,19 @@ interface SearchOptions {
|
|
|
198
256
|
/** Score multipliers per entity type - applied after initial search */
|
|
199
257
|
weight?: Record<string, number>;
|
|
200
258
|
}
|
|
259
|
+
/**
|
|
260
|
+
* Configuration for entity type registration
|
|
261
|
+
*/
|
|
262
|
+
interface EntityTypeConfig {
|
|
263
|
+
/** Score multiplier for search results (default: 1.0) */
|
|
264
|
+
weight?: number;
|
|
265
|
+
/** Whether to generate embeddings for this entity type (default: true).
|
|
266
|
+
* Set to false for entity types with non-textual content (e.g., images). */
|
|
267
|
+
embeddable?: boolean;
|
|
268
|
+
/** Whether this entity type may be used as source material for derived projections (default: true).
|
|
269
|
+
* Set to false for projection outputs that would create feedback loops. */
|
|
270
|
+
projectionSource?: boolean;
|
|
271
|
+
}
|
|
201
272
|
/**
|
|
202
273
|
* Core entity service interface for read-only operations
|
|
203
274
|
* Used by core plugins that need entity access but shouldn't modify entities
|
|
@@ -258,12 +329,11 @@ interface ICoreEntityService {
|
|
|
258
329
|
entityType: string;
|
|
259
330
|
count: number;
|
|
260
331
|
}>>;
|
|
332
|
+
/** Get configuration for a specific entity type */
|
|
333
|
+
getEntityTypeConfig(type: string): EntityTypeConfig;
|
|
261
334
|
/** Get weight map for all registered entity types with non-default weights */
|
|
262
335
|
getWeightMap(): Record<string, number>;
|
|
263
336
|
}
|
|
264
|
-
/**
|
|
265
|
-
* Entity service interface for managing brain entities
|
|
266
|
-
*/
|
|
267
337
|
interface EntityService extends ICoreEntityService {
|
|
268
338
|
createEntity<T extends BaseEntity>(request: CreateEntityRequest<T>): Promise<EntityMutationResult>;
|
|
269
339
|
createEntityFromMarkdown(request: CreateEntityFromMarkdownRequest): Promise<EntityMutationResult>;
|
|
@@ -288,64 +358,6 @@ interface EntityService extends ICoreEntityService {
|
|
|
288
358
|
} | null>;
|
|
289
359
|
}
|
|
290
360
|
|
|
291
|
-
/**
|
|
292
|
-
* Context passed to all DataSource operations
|
|
293
|
-
* Contains internal state that should not be mixed with user query parameters
|
|
294
|
-
*/
|
|
295
|
-
interface BaseDataSourceContext {
|
|
296
|
-
/**
|
|
297
|
-
* Whether to filter to only published/complete content
|
|
298
|
-
* Set by site-builder: true for production, false for preview
|
|
299
|
-
*/
|
|
300
|
-
publishedOnly?: boolean;
|
|
301
|
-
/**
|
|
302
|
-
* Scoped entity service that auto-applies publishedOnly filter
|
|
303
|
-
* Datasources should use this instead of their injected entityService
|
|
304
|
-
* to ensure consistent filtering behavior across environments
|
|
305
|
-
*/
|
|
306
|
-
entityService: EntityService;
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* DataSource Interface
|
|
310
|
-
*
|
|
311
|
-
* Provides data for templates through fetch, generate, or transform operations.
|
|
312
|
-
* DataSources are registered in the DataSourceRegistry and referenced by templates
|
|
313
|
-
* via their dataSourceId property.
|
|
314
|
-
*/
|
|
315
|
-
interface DataSource {
|
|
316
|
-
/**
|
|
317
|
-
* Unique identifier for this data source
|
|
318
|
-
*/
|
|
319
|
-
id: string;
|
|
320
|
-
/**
|
|
321
|
-
* Human-readable name for this data source
|
|
322
|
-
*/
|
|
323
|
-
name: string;
|
|
324
|
-
/**
|
|
325
|
-
* Optional description of what this data source provides
|
|
326
|
-
*/
|
|
327
|
-
description?: string;
|
|
328
|
-
/**
|
|
329
|
-
* Optional: Fetch existing data
|
|
330
|
-
* Used by data sources that aggregate or retrieve data (e.g., dashboard stats, API data)
|
|
331
|
-
* DataSources validate output using the provided schema
|
|
332
|
-
* @param query - Query parameters for fetching data
|
|
333
|
-
* @param outputSchema - Schema for validating output data
|
|
334
|
-
* @param context - Context with environment
|
|
335
|
-
*/
|
|
336
|
-
fetch?: <T>(query: unknown, outputSchema: z.ZodSchema<T>, context: BaseDataSourceContext) => Promise<T>;
|
|
337
|
-
/**
|
|
338
|
-
* Optional: Generate new content
|
|
339
|
-
* Used by data sources that create content (e.g., AI-generated content, reports)
|
|
340
|
-
*/
|
|
341
|
-
generate?: <T>(request: unknown, schema: z.ZodSchema<T>) => Promise<T>;
|
|
342
|
-
/**
|
|
343
|
-
* Optional: Transform content between formats
|
|
344
|
-
* Used by data sources that convert content (e.g., markdown to HTML, data formatting)
|
|
345
|
-
*/
|
|
346
|
-
transform?: <T>(content: unknown, format: string, schema: z.ZodSchema<T>) => Promise<T>;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
361
|
/**
|
|
350
362
|
* Schema for pagination information
|
|
351
363
|
* Used by datasources that return paginated lists
|
package/dist/services.js
CHANGED
|
@@ -166,7 +166,7 @@ Please report this to https://github.com/markedjs/marked.`,$){let X="<p>An error
|
|
|
166
166
|
`}format($){try{let J=[`# ${this.config.title}`,""];for(let G of this.config.mappings)this.formatField($,G,J,2);return J.join(`
|
|
167
167
|
`)}catch{throw Error("Failed to format structured content")}}parse($){try{let J=this.processor.parse($),G=this.extractSections(J,2),X=this.buildDataFromSections(G,this.config.mappings);return this.schema.parse(X)}catch{throw Error("Failed to parse structured content")}}formatField($,J,G,X){let q="#".repeat(X)+" "+J.label,Y=this.getValueByPath($,J.key);if(J.type==="custom"&&J.formatter){if(Y!==void 0&&Y!==null){G.push(q,"");let V=J.formatter(Y);if(V)G.push(V,"")}return}switch(J.type){case"string":case"number":G.push(q,String(Y??""),"");break;case"object":if(G.push(q),J.children&&Y)for(let V of J.children)this.formatField(Y,V,G,X+1);break;case"array":if(G.push(q,""),Array.isArray(Y))if(J.itemType==="object"&&J.itemMappings)Y.forEach((V,W)=>{if(G.push(`${"#".repeat(X+1)} ${J.label.slice(0,-1)} ${W+1}`),G.push(""),J.itemMappings)for(let H of J.itemMappings)this.formatField(V,H,G,X+2)});else{for(let V of Y){let W=this.defaultArrayItemFormat(V);G.push(`- ${W}`)}G.push("")}break}}getValueByPath($,J){let G=J.split("."),X=$;for(let q of G)if(X&&typeof X==="object"&&q in X)X=X[q];else return;return X}defaultArrayItemFormat($){if(typeof $==="string"||typeof $==="number")return String($);return JSON.stringify($)}extractSections($,J){let G=new Map,X=null,q=[];for(let Y of $.children)if(Y.type==="heading"&&Y.depth===J){if(X)G.set(X.toLowerCase(),q);X=this.getHeadingText(Y),q=[]}else if(X)q.push(Y);if(X)G.set(X.toLowerCase(),q);return G}extractSubsections($,J){let G=new Map,X=null,q=[];for(let Y of $)if(Y.type==="heading"&&Y.depth===J){if(X)G.set(X.toLowerCase(),q);X=this.getHeadingText(Y),q=[]}else if(X)q.push(Y);if(X)G.set(X.toLowerCase(),q);return G}textNodeSchema=Z.object({type:Z.literal("text"),value:Z.string()});extractTextValue($){let J=this.textNodeSchema.safeParse($);return J.success?J.data.value:""}getHeadingText($){return $.children.filter((G)=>G.type==="text").map((G)=>this.extractTextValue(G)).join("")}buildDataFromSections($,J,G=2){let X={};for(let q of J){let Y=$.get(q.label.toLowerCase());if(q.type==="custom"&&q.parser&&Y){let V=this.getTextFromSection(Y),W=q.parser(V);this.setValueByPath(X,q.key,W)}else if(q.type==="object"&&q.children&&Y){let V=this.extractSubsections(Y,G+1),W=this.buildDataFromSections(V,q.children,G+1);this.setValueByPath(X,q.key,W)}else if(q.type==="array"&&Y)if(q.itemType==="object"&&q.itemMappings){let V=this.extractObjectArrayItems(Y,G+1,q.itemMappings);this.setValueByPath(X,q.key,V)}else{let V=this.extractSimpleArrayItems(Y);this.setValueByPath(X,q.key,V)}else if(Y){let V=this.getTextFromSection(Y),W=q.type==="number"?Number(V):V;this.setValueByPath(X,q.key,W)}}return X}setValueByPath($,J,G){let X=J.split("."),q=$;for(let V=0;V<X.length-1;V++){let W=X[V];if(!W)continue;if(!(W in q))q[W]={};q=q[W]}let Y=X[X.length-1];if(Y)q[Y]=G}getTextFromSection($){let J=[];for(let G of $)if(G.type==="paragraph"){let X=this.extractTextFromParagraph(G);if(X)J.push(X)}else if(G.type==="list"){let X=G;for(let q of X.children){let Y=this.extractTextFromListItem(q);if(Y)J.push(`- ${Y}`)}}return J.join(`
|
|
168
168
|
`)}extractTextFromParagraph($){return this.extractInlineText($.children).trim()}extractInlineText($){let J=[];for(let G of $)if(G.type==="text")J.push(this.extractTextValue(G));else if(G.type==="emphasis"){let X=this.extractInlineText(G.children);if(X)J.push(`*${X}*`)}else if(G.type==="strong"){let X=this.extractInlineText(G.children);if(X)J.push(`**${X}**`)}return J.join("")}extractSimpleArrayItems($){let J=[];for(let G of $)if(G.type==="list"){let X=G;for(let q of X.children){let Y=this.extractTextFromListItem(q);if(Y)J.push(Y)}}return J}extractObjectArrayItems($,J,G){let X=[],q=[],Y=!1;for(let V of $)if(V.type==="heading"&&V.depth===J){if(Y&&q.length>0){let W=this.extractSubsections(q,J+1),H=this.buildDataFromSections(W,G,J+1);X.push(H)}q=[],Y=!0}else if(Y)q.push(V);if(Y&&q.length>0){let V=this.extractSubsections(q,J+1),W=this.buildDataFromSections(V,G,J+1);X.push(W)}return X}extractTextFromListItem($){let J=[];for(let G of $.children)if(G.type==="paragraph"){let X=this.extractTextFromParagraph(G);if(X)J.push(X)}return J.join(`
|
|
169
|
-
`)}}var SS=Z.object({slug:Z.string(),title:Z.string(),type:Z.string(),entityId:Z.string(),contentHash:Z.string()});var BF=Z.object({success:Z.boolean(),entityId:Z.string().optional(),error:Z.string().optional()});var
|
|
169
|
+
`)}}var SS=Z.object({slug:Z.string(),title:Z.string(),type:Z.string(),entityId:Z.string(),contentHash:Z.string()});var BF=Z.object({success:Z.boolean(),entityId:Z.string().optional(),error:Z.string().optional()});var yC=Z.object({id:Z.string().min(1,"Entity ID is required"),entityType:Z.string().min(1,"Entity type is required"),contentHash:Z.string().min(1,"Content hash is required"),operation:Z.enum(["create","update"])});var pC=Z.object({limit:Z.number().int().positive().optional().default(20),offset:Z.number().int().min(0).optional().default(0),types:Z.array(Z.string()).optional().default([]),excludeTypes:Z.array(Z.string()).optional().default([]),weight:Z.record(Z.string(),Z.number()).optional()});var wF=Z.object({field:Z.string(),direction:Z.enum(["asc","desc"]),nullsFirst:Z.boolean().optional()}),tC=Z.object({limit:Z.number().int().positive().optional(),offset:Z.number().int().min(0).optional().default(0),sortFields:Z.array(wF).optional(),filter:Z.object({metadata:Z.record(Z.string(),Z.unknown()).optional()}).optional(),publishedOnly:Z.boolean().optional()});var IF=c2(r6(),1);var bX=Z.object({id:Z.string(),entityType:Z.string(),content:Z.string(),created:Z.string().datetime(),updated:Z.string().datetime(),metadata:Z.record(Z.string(),Z.unknown()),contentHash:Z.string()});var vF=Z.object({currentPage:Z.number(),totalPages:Z.number(),totalItems:Z.number(),pageSize:Z.number(),hasNextPage:Z.boolean(),hasPrevPage:Z.boolean()});function H8($,J,G){let X=Math.max(1,Math.ceil($/G));return{currentPage:J,totalPages:X,totalItems:$,pageSize:G,hasNextPage:J<X,hasPrevPage:J>1}}var xX=Z.object({id:Z.string().optional(),limit:Z.number().optional(),page:Z.number().optional(),pageSize:Z.number().optional(),baseUrl:Z.string().optional()}).passthrough(),gX=Z.object({entityType:Z.string().optional(),query:xX.optional()}).passthrough();class hX{logger;constructor($){this.logger=$}buildDetailResult($,J){throw Error(`${this.id}: buildDetailResult() not implemented. Override this method or override fetch() to handle detail views.`)}parseQuery($){let J=gX.parse($);return{entityType:J.entityType??this.config.entityType,query:J.query??{}}}async fetch($,J,G){let X=this.parseQuery($),q=G.entityService;if(X.query.id){let V=await this.fetchDetail(X.query.id,q);return J.parse(this.buildDetailResult(V.item,V.navigation))}let Y=await this.fetchList(X.query,q);return J.parse(this.buildListResult(Y.items,Y.pagination,X.query))}async fetchDetail($,J){let G=await this.lookupEntity($,J),X=this.transformEntity(G),q=null;if(this.config.enableNavigation)q=await this.resolveNavigation(G,J);return{item:X,navigation:q}}async fetchList($,J,G){let X=$.page??1,q=$.pageSize??$.limit??this.config.defaultLimit??100,Y=(X-1)*q,V=$.page!==void 0,[W,H]=await Promise.all([J.listEntities({entityType:this.config.entityType,options:{limit:q,offset:Y,sortFields:this.config.defaultSort,...G}}),V?J.countEntities({entityType:this.config.entityType,options:G?.filter?{filter:G.filter}:void 0}):Promise.resolve(0)]),U=W.map((M)=>this.transformEntity(M)),Q=V?H8(H,X,q):null;return{items:U,pagination:Q}}async resolveNavigation($,J,G){let X=this.config.navigationLimit??1000,q=await J.listEntities({entityType:this.config.entityType,options:{limit:X,sortFields:G??this.config.defaultSort}}),Y=q.findIndex((Q)=>Q.id===$.id),V=Y>0?q[Y-1]:void 0,W=Y<q.length-1?q[Y+1]:void 0,H=V?this.transformEntity(V):null,U=W?this.transformEntity(W):null;return{prev:H,next:U}}async lookupEntity($,J){if(this.config.lookupField==="id"){let q=await J.getEntity({entityType:this.config.entityType,id:$});if(!q)throw Error(`${this.config.entityType} not found: ${$}`);return q}let X=(await J.listEntities({entityType:this.config.entityType,options:{filter:{metadata:{slug:$}},limit:1}}))[0];if(!X)throw Error(`${this.config.entityType} not found with slug: ${$}`);return X}}export{xX as baseQuerySchema,gX as baseInputSchema,hX as BaseEntityDataSource};
|
|
170
170
|
|
|
171
|
-
//# debugId=
|
|
171
|
+
//# debugId=A6B6EB590F8D96C464756E2164756E21
|
|
172
172
|
//# sourceMappingURL=services.js.map
|