@rizom/brain 0.2.0-alpha.63 → 0.2.0-alpha.65

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/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 kC=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 uC=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()}),aC=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};
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=CC65231FFF64C93864756E2164756E21
171
+ //# debugId=A6B6EB590F8D96C464756E2164756E21
172
172
  //# sourceMappingURL=services.js.map