@fortemi/core 2026.7.5 → 2026.7.7
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/aiwg-index.d.ts +22 -1
- package/dist/aiwg-index.js +20 -12
- package/dist/aiwg-index.js.map +1 -1
- package/dist/index.d.ts +736 -50
- package/dist/index.js +1537 -196
- package/dist/index.js.map +1 -1
- package/package.json +4 -2
package/dist/aiwg-index.d.ts
CHANGED
|
@@ -195,6 +195,16 @@ interface AiwgIndexValidationResult {
|
|
|
195
195
|
errors: string[];
|
|
196
196
|
counts: Partial<Record<string, number>>;
|
|
197
197
|
}
|
|
198
|
+
interface AiwgKnowledgeShardOptions {
|
|
199
|
+
createdAt?: string;
|
|
200
|
+
matricVersion?: string;
|
|
201
|
+
/**
|
|
202
|
+
* Include React-native SKOS and provenance component files. The default
|
|
203
|
+
* portable profile keeps those projections in lossless note metadata so the
|
|
204
|
+
* shard remains importable by the current Fortemi server.
|
|
205
|
+
*/
|
|
206
|
+
includeNativeRichComponents?: boolean;
|
|
207
|
+
}
|
|
198
208
|
interface AiwgChunkedIndexValidationResult {
|
|
199
209
|
valid: boolean;
|
|
200
210
|
errors: string[];
|
|
@@ -489,5 +499,16 @@ declare function aiwgFortemiIndexToCommunityGraph(index: AiwgFortemiIndexExport,
|
|
|
489
499
|
nodes: string[];
|
|
490
500
|
}[];
|
|
491
501
|
};
|
|
502
|
+
/**
|
|
503
|
+
* Convert the static AIWG/Fortemi v2 index contract into a portable Knowledge
|
|
504
|
+
* Shard. Every note retains the complete source envelope and record in metadata,
|
|
505
|
+
* so the native note/link/SKOS/provenance projections are reversible.
|
|
506
|
+
*/
|
|
507
|
+
declare function aiwgFortemiIndexToKnowledgeShard(index: AiwgFortemiIndexExport, options?: AiwgKnowledgeShardOptions): Promise<Uint8Array>;
|
|
508
|
+
/**
|
|
509
|
+
* Recover the exact AIWG index envelope and records embedded by
|
|
510
|
+
* {@link aiwgFortemiIndexToKnowledgeShard}.
|
|
511
|
+
*/
|
|
512
|
+
declare function aiwgFortemiIndexFromKnowledgeShard(bytes: Uint8Array): AiwgFortemiIndexExport;
|
|
492
513
|
|
|
493
|
-
export { AIWG_SCAN_REQUIRED_FIELDS, type AiwgChunkedIndexBuildOptions, type AiwgChunkedIndexBuildResult, type AiwgChunkedIndexDetailLoader, type AiwgChunkedIndexLoadOptions, type AiwgChunkedIndexLoader, type AiwgChunkedIndexProgress, type AiwgChunkedIndexProgressPhase, type AiwgChunkedIndexQueryOptions, type AiwgChunkedIndexQueryResult, type AiwgChunkedIndexValidationResult, type AiwgDetailIdEncoding, type AiwgFortemiAttachmentReference, type AiwgFortemiBinarySource, type AiwgFortemiChunk, type AiwgFortemiChunkDetailRef, type AiwgFortemiChunkManifest, type AiwgFortemiChunkPart, type AiwgFortemiChunkPartRef, type AiwgFortemiIndexExport, type AiwgFortemiIndexExportSchemaVersion, type AiwgFortemiProjectedRecord, type AiwgFortemiProvenance, type AiwgFortemiProvenanceEvent, type AiwgFortemiRecord, type AiwgFortemiRecordEmbedding, type AiwgFortemiRecordSchemaVersion, type AiwgFortemiRecordSource, type AiwgFortemiRecordType, type AiwgFortemiRelationship, type AiwgFortemiRelationshipDirection, type AiwgFortemiSearchProjection, type AiwgFortemiSkosConcept, type AiwgFortemiSkosRelation, type AiwgFortemiSkosRelationType, type AiwgHeadlessEmbeddingBackend, type AiwgIndexController, type AiwgIndexControllerListener, type AiwgIndexControllerSnapshot, type AiwgIndexGraphOptions, type AiwgIndexQueryMatch, type AiwgIndexQueryOptions, type AiwgIndexQueryRankedItem, type AiwgIndexQueryResult, type AiwgIndexQueryWeights, type AiwgIndexValidationResult, type AiwgPrivacyClassification, type AiwgPrivacyFilterOptions, type AiwgProvenanceConfidence, type AiwgRelationshipDirection, type AiwgRelationshipEdgeSummary, type AiwgRelationshipNodeSummary, type AiwgRelationshipQueryOptions, type AiwgRelationshipSetOperation, type AiwgRelationshipSetOptions, type AiwgRelationshipSetResult, type AiwgRelationshipTraversalOptions, type AiwgRelationshipTraversalResult, type AiwgReviewAction, type AiwgReviewDecision, type AiwgReviewDecisionExport, type AiwgReviewInput, type AiwgStaticDuplicatePair, type AiwgStaticEmbeddingRecord, type AiwgStaticEmbeddingSet, type AiwgStaticHybridQueryOptions, type AiwgStaticSemanticQueryOptions, type AiwgStaticSemanticResult, type BuildAiwgStaticEmbeddingSetOptions, DEFAULT_AIWG_DUPLICATE_SCAN_MAX_EMBEDDINGS, aiwgDetailHrefForId, aiwgFortemiIndexToCommunityGraph, assertAiwgFortemiChunkManifest, assertAiwgFortemiChunkPart, assertAiwgFortemiIndexExport, assertAiwgStaticEmbeddingSet, buildAiwgChunkedIndex, buildAiwgStaticEmbeddingSet, createAiwgFetchChunkLoader, createAiwgFetchDetailLoader, createAiwgIndexController, createAiwgReviewDecisionExport, encodeAiwgDetailId, filterAiwgRecordsByPrivacy, findAiwgStaticDuplicatePairs, getAiwgFortemiFacets, queryAiwgFortemiIndex, queryAiwgHybridIndex, queryAiwgSemanticIndex, resolveAiwgFetchUrl, validateAiwgFortemiChunkManifest, validateAiwgFortemiChunkPart, validateAiwgFortemiIndexExport, validateAiwgStaticEmbeddingSet };
|
|
514
|
+
export { AIWG_SCAN_REQUIRED_FIELDS, type AiwgChunkedIndexBuildOptions, type AiwgChunkedIndexBuildResult, type AiwgChunkedIndexDetailLoader, type AiwgChunkedIndexLoadOptions, type AiwgChunkedIndexLoader, type AiwgChunkedIndexProgress, type AiwgChunkedIndexProgressPhase, type AiwgChunkedIndexQueryOptions, type AiwgChunkedIndexQueryResult, type AiwgChunkedIndexValidationResult, type AiwgDetailIdEncoding, type AiwgFortemiAttachmentReference, type AiwgFortemiBinarySource, type AiwgFortemiChunk, type AiwgFortemiChunkDetailRef, type AiwgFortemiChunkManifest, type AiwgFortemiChunkPart, type AiwgFortemiChunkPartRef, type AiwgFortemiIndexExport, type AiwgFortemiIndexExportSchemaVersion, type AiwgFortemiProjectedRecord, type AiwgFortemiProvenance, type AiwgFortemiProvenanceEvent, type AiwgFortemiRecord, type AiwgFortemiRecordEmbedding, type AiwgFortemiRecordSchemaVersion, type AiwgFortemiRecordSource, type AiwgFortemiRecordType, type AiwgFortemiRelationship, type AiwgFortemiRelationshipDirection, type AiwgFortemiSearchProjection, type AiwgFortemiSkosConcept, type AiwgFortemiSkosRelation, type AiwgFortemiSkosRelationType, type AiwgHeadlessEmbeddingBackend, type AiwgIndexController, type AiwgIndexControllerListener, type AiwgIndexControllerSnapshot, type AiwgIndexGraphOptions, type AiwgIndexQueryMatch, type AiwgIndexQueryOptions, type AiwgIndexQueryRankedItem, type AiwgIndexQueryResult, type AiwgIndexQueryWeights, type AiwgIndexValidationResult, type AiwgKnowledgeShardOptions, type AiwgPrivacyClassification, type AiwgPrivacyFilterOptions, type AiwgProvenanceConfidence, type AiwgRelationshipDirection, type AiwgRelationshipEdgeSummary, type AiwgRelationshipNodeSummary, type AiwgRelationshipQueryOptions, type AiwgRelationshipSetOperation, type AiwgRelationshipSetOptions, type AiwgRelationshipSetResult, type AiwgRelationshipTraversalOptions, type AiwgRelationshipTraversalResult, type AiwgReviewAction, type AiwgReviewDecision, type AiwgReviewDecisionExport, type AiwgReviewInput, type AiwgStaticDuplicatePair, type AiwgStaticEmbeddingRecord, type AiwgStaticEmbeddingSet, type AiwgStaticHybridQueryOptions, type AiwgStaticSemanticQueryOptions, type AiwgStaticSemanticResult, type BuildAiwgStaticEmbeddingSetOptions, DEFAULT_AIWG_DUPLICATE_SCAN_MAX_EMBEDDINGS, aiwgDetailHrefForId, aiwgFortemiIndexFromKnowledgeShard, aiwgFortemiIndexToCommunityGraph, aiwgFortemiIndexToKnowledgeShard, assertAiwgFortemiChunkManifest, assertAiwgFortemiChunkPart, assertAiwgFortemiIndexExport, assertAiwgStaticEmbeddingSet, buildAiwgChunkedIndex, buildAiwgStaticEmbeddingSet, createAiwgFetchChunkLoader, createAiwgFetchDetailLoader, createAiwgIndexController, createAiwgReviewDecisionExport, encodeAiwgDetailId, filterAiwgRecordsByPrivacy, findAiwgStaticDuplicatePairs, getAiwgFortemiFacets, queryAiwgFortemiIndex, queryAiwgHybridIndex, queryAiwgSemanticIndex, resolveAiwgFetchUrl, validateAiwgFortemiChunkManifest, validateAiwgFortemiChunkPart, validateAiwgFortemiIndexExport, validateAiwgStaticEmbeddingSet };
|
package/dist/aiwg-index.js
CHANGED
|
@@ -1,16 +1,24 @@
|
|
|
1
|
-
var we=["schema_version","id","type","title","text","facets","tags","concepts","privacy"];function ye(e,t){let i=e.privacy;return !!(!i||!P(i.classification)||typeof i.pii!="boolean"||i.classification==="private"&&!t?.includePrivate||i.pii&&!t?.includePii)}function ie(e,t){return e.filter(i=>!ye(i,t))}var Ae=["schema_version","id","type","source","facets","tags","concepts","relationships","provenance","privacy","updated_at"],K={title:4,tag:3,concept:2,text:1,facet:2,id:1,source:.25};function l(e){return typeof e=="string"&&e.length>0}function M(e,t,i){let r=e[t];r===void 0&&(r=Object.create(null),e[t]=r),r[i]=(r[i]??0)+1;}function C(e){return Number.isInteger(e)&&typeof e=="number"&&e>=0}function S(e){return Number.isInteger(e)&&typeof e=="number"&&e>0}function xe(e){return !e||typeof e!="object"||Array.isArray(e)?false:Object.values(e).every(t=>!!t&&typeof t=="object"&&!Array.isArray(t)&&Object.values(t).every(i=>C(i)))}function h(e){return !!e&&typeof e=="object"&&!Array.isArray(e)}function L(e){return e===void 0||Array.isArray(e)&&e.every(t=>typeof t=="string")}function re(e){return e==="aiwg.fortemi.index.export.v1"||e==="aiwg.fortemi.index.export.v2"}function ne(e){return e==="aiwg.fortemi.index.record.v1"||e==="aiwg.fortemi.index.record.v2"}function be(e,t,i){if(e.skos_concepts!==void 0)if(!Array.isArray(e.skos_concepts))i.push("items["+t+"].skos_concepts must be an array when present");else for(let[r,n]of e.skos_concepts.entries()){if(!h(n)){i.push("items["+t+"].skos_concepts["+r+"] must be an object");continue}l(n.id)||i.push("items["+t+"].skos_concepts["+r+"].id is required"),l(n.prefLabel)||i.push("items["+t+"].skos_concepts["+r+"].prefLabel is required"),L(n.altLabels)||i.push("items["+t+"].skos_concepts["+r+"].altLabels must be a string array"),n.metadata!==void 0&&!h(n.metadata)&&i.push("items["+t+"].skos_concepts["+r+"].metadata must be an object");}if(e.skos_relations!==void 0)if(!Array.isArray(e.skos_relations))i.push("items["+t+"].skos_relations must be an array when present");else for(let[r,n]of e.skos_relations.entries()){if(!h(n)){i.push("items["+t+"].skos_relations["+r+"] must be an object");continue}l(n.type)||i.push("items["+t+"].skos_relations["+r+"].type is required"),l(n.source_id)||i.push("items["+t+"].skos_relations["+r+"].source_id is required"),l(n.target_id)||i.push("items["+t+"].skos_relations["+r+"].target_id is required"),n.metadata!==void 0&&!h(n.metadata)&&i.push("items["+t+"].skos_relations["+r+"].metadata must be an object");}if(e.provenance_events!==void 0)if(!Array.isArray(e.provenance_events))i.push("items["+t+"].provenance_events must be an array when present");else for(let[r,n]of e.provenance_events.entries()){if(!h(n)){i.push("items["+t+"].provenance_events["+r+"] must be an object");continue}l(n.activity)||i.push("items["+t+"].provenance_events["+r+"].activity is required"),n.attributes!==void 0&&!h(n.attributes)&&i.push("items["+t+"].provenance_events["+r+"].attributes must be an object");}if(Array.isArray(e.relationships))for(let[r,n]of e.relationships.entries()){if(!h(n)){i.push("items["+t+"].relationships["+r+"] must be an object");continue}n.metadata!==void 0&&!h(n.metadata)&&i.push("items["+t+"].relationships["+r+"].metadata must be an object"),n.direction!==void 0&&n.direction!=="upstream"&&n.direction!=="downstream"&&n.direction!=="related"&&i.push("items["+t+"].relationships["+r+"].direction must be upstream, downstream, or related"),n.target_path!==void 0&&typeof n.target_path!="string"&&i.push("items["+t+"].relationships["+r+"].target_path must be a string");}if(e.search!==void 0&&(h(e.search)?(L(e.search.triggers)||i.push("items["+t+"].search.triggers must be a string array"),L(e.search.aliases)||i.push("items["+t+"].search.aliases must be a string array"),L(e.search.tags)||i.push("items["+t+"].search.tags must be a string array"),e.search.frontmatter!==void 0&&!h(e.search.frontmatter)&&i.push("items["+t+"].search.frontmatter must be an object")):i.push("items["+t+"].search must be an object")),e.chunks!==void 0)if(!Array.isArray(e.chunks))i.push("items["+t+"].chunks must be an array when present");else for(let[r,n]of e.chunks.entries()){if(!h(n)){i.push("items["+t+"].chunks["+r+"] must be an object");continue}n.metadata!==void 0&&!h(n.metadata)&&i.push("items["+t+"].chunks["+r+"].metadata must be an object");}if(e.embeddings!==void 0)if(!Array.isArray(e.embeddings))i.push("items["+t+"].embeddings must be an array when present");else for(let[r,n]of e.embeddings.entries()){if(!h(n)){i.push("items["+t+"].embeddings["+r+"] must be an object");continue}let s=n.embedding??n.vector;s!==void 0&&(!Array.isArray(s)||!s.every(o=>typeof o=="number"))&&i.push("items["+t+"].embeddings["+r+"].embedding/vector must be a number array"),n.metadata!==void 0&&!h(n.metadata)&&i.push("items["+t+"].embeddings["+r+"].metadata must be an object");}e.compatibility!==void 0&&!h(e.compatibility)&&i.push("items["+t+"].compatibility must be an object");}function P(e){return e==="private"||e==="sanitized"||e==="public"}function se(e){return e==="source"||e==="candidate"||e==="reviewed"||e==="rejected"}function ve(e,t,i){if(Array.isArray(e.provenance))for(let[r,n]of e.provenance.entries()){let s="items["+t+"].provenance["+r+"]";if(!h(n)){i.push(s+" must be an object");continue}l(n.field)||i.push(s+".field is required"),l(n.source)||i.push(s+".source is required"),l(n.path)||i.push(s+".path is required"),se(n.confidence)||i.push(s+".confidence must be one of source, candidate, reviewed, rejected"),P(n.privacy)||i.push(s+".privacy must be one of private, sanitized, public");}}var Re=["search","chunks","embeddings","skos_concepts","skos_relations","provenance_events","compatibility"],ke=["origin","generated","checksum","updated_at"],_e=["target_path","direction","metadata"];function Ie(e,t,i){if(e.schema_version!=="aiwg.fortemi.index.record.v1")return;let r="items["+t+"]",n=e,s=" is a v2-only field and must be absent on a record.v1 record";for(let o of Re)n[o]!==void 0&&i.push(r+"."+o+s);if(h(e.source)){let o=e.source;for(let a of ke)o[a]!==void 0&&i.push(r+".source."+a+s);}if(h(e.privacy)&&e.privacy.locality!==void 0&&i.push(r+".privacy.locality"+s),Array.isArray(e.relationships))for(let[o,a]of e.relationships.entries()){if(!h(a))continue;let c=a;for(let g of _e)c[g]!==void 0&&i.push(r+".relationships["+o+"]."+g+s);}}function oe(e){let t=[],i=Object.create(null),r=h(e)?e:{};h(e)||t.push("index export must be an object"),re(r?.schema_version)||t.push("schema_version must be aiwg.fortemi.index.export.v1 or aiwg.fortemi.index.export.v2"),l(r?.generated_at)||t.push("generated_at is required"),l(r?.source?.repo)||t.push("source.repo is required"),l(r?.source?.privacy)?P(r?.source?.privacy)||t.push("source.privacy must be one of private, sanitized, public"):t.push("source.privacy is required"),Array.isArray(r?.items)||t.push("items must be an array"),r.compatibility!==void 0&&!h(r.compatibility)&&t.push("compatibility must be an object"),r?.schema_version==="aiwg.fortemi.index.export.v1"&&(h(r.source)&&r.source.graph!==void 0&&t.push("source.graph is a v2-only field and must be absent on an export.v1 export"),r.compatibility!==void 0&&t.push("compatibility is a v2-only field and must be absent on an export.v1 export"));let n=new Set,s="",o=Array.isArray(r.items)?r.items:[];for(let[a,c]of o.entries()){if(!h(c)){t.push("items["+a+"] must be an object");continue}for(let g of Ae)g in c||t.push("items["+a+"]."+g+" is required");ne(c.schema_version)||t.push("items["+a+"].schema_version must be aiwg.fortemi.index.record.v1 or aiwg.fortemi.index.record.v2"),r.schema_version==="aiwg.fortemi.index.export.v1"&&c.schema_version==="aiwg.fortemi.index.record.v2"&&t.push("items["+a+"].schema_version must match aiwg.fortemi.index.export.v1"),l(c.id)||t.push("items["+a+"].id is required"),l(c.id)&&n.has(c.id)&&t.push("duplicate id: "+c.id),l(c.id)&&n.add(c.id),s&&l(c.id)&&s>c.id&&t.push("items must be sorted by id: "+s+" before "+c.id),l(c.id)&&(s=c.id),l(c.type)?i[c.type]=(i[c.type]??0)+1:t.push("items["+a+"].type must be a non-empty string"),l(c.source?.path)||t.push("items["+a+"].source.path is required"),l(c.source?.repo_relative_path)||t.push("items["+a+"].source.repo_relative_path is required"),l(c.source?.locator)||t.push("items["+a+"].source.locator is required"),typeof c.title!="string"&&typeof c.search?.title!="string"&&typeof c.search?.name!="string"&&t.push("items["+a+"].title or search.title/search.name is required"),typeof c.text!="string"&&typeof c.search?.body!="string"&&typeof c.search?.summary!="string"&&t.push("items["+a+"].text or search.body/search.summary is required"),Array.isArray(c.tags)||t.push("items["+a+"].tags must be an array"),Array.isArray(c.concepts)||t.push("items["+a+"].concepts must be an array"),Array.isArray(c.relationships)||t.push("items["+a+"].relationships must be an array"),(!Array.isArray(c.provenance)||c.provenance.length===0)&&t.push("items["+a+"].provenance must be a non-empty array"),be(c,a,t),ve(c,a,t),Ie(c,a,t),!c.privacy||typeof c.privacy.pii!="boolean"||!l(c.privacy.classification)?t.push("items["+a+"].privacy requires classification and pii"):P(c.privacy.classification)||t.push("items["+a+"].privacy.classification must be one of private, sanitized, public");}return {valid:t.length===0,errors:t,counts:i}}function q(e){let t=oe(e);if(!t.valid)throw new Error(`Invalid AIWG Fortemi index export:
|
|
1
|
+
import {gzipSync,gunzipSync}from'fflate';import {v5}from'uuid';var J="1.0.0",oe="matric-shard";async function ae(e){let t=new ArrayBuffer(e.byteLength);new Uint8Array(t).set(e);let r=await crypto.subtle.digest("SHA-256",t),i=new Uint8Array(r);return Array.from(i).map(n=>n.toString(16).padStart(2,"0")).join("")}var C=512,Oe="ustar\x0000";function N(e,t,r,i){for(let n=0;n<Math.min(r.length,i);n++)e[t+n]=r.charCodeAt(n);}function L(e,t,r,i){let n=r.toString(8).padStart(i-1,"0");N(e,t,n,i-1);}function je(e){let t=0;for(let r=0;r<C;r++)t+=r>=148&&r<156?32:e[r];return t}function De(e,t){let r=new Uint8Array(C);N(r,0,e,100),L(r,100,420,8),L(r,108,0,8),L(r,116,0,8),L(r,124,t,12),L(r,136,Math.floor(Date.now()/1e3),12),r[156]=48,N(r,257,Oe,8);let n=je(r).toString(8).padStart(6,"0");return N(r,148,n,6),r[154]=0,r[155]=32,r}function Le(e){let t=[];for(let[s,o]of e){t.push(De(s,o.byteLength)),t.push(o);let a=o.byteLength%C;a>0&&t.push(new Uint8Array(C-a));}t.push(new Uint8Array(C*2));let r=0;for(let s of t)r+=s.byteLength;let i=new Uint8Array(r),n=0;for(let s of t)i.set(s,n),n+=s.byteLength;return i}function ce(e,t,r){let i=t,n=t+r;for(;i<n&&e[i]!==0;)i++;return String.fromCharCode(...e.slice(t,i))}function Te(e,t,r){let i=ce(e,t,r).trim();return i.length>0?parseInt(i,8):0}function Qe(e,t){for(let r=0;r<C;r++)if(e[t+r]!==0)return false;return true}function Ue(e){let t=new Map,r=0;for(;r+C<=e.byteLength&&!Qe(e,r);){let i=ce(e,r,100),n=Te(e,r+124,12),s=e[r+156];r+=C,(s===48||s===0)&&t.set(i,e.slice(r,r+n));let o=Math.ceil(n/C);r+=o*C;}return t}function de(e,t){let r=Le(e),i=ue;if(r.byteLength>i)throw new Error("Refusing to create archive: uncompressed size "+r.byteLength+" exceeds cap "+i+" bytes");return gzipSync(r)}var ue=256*1024*1024;function ge(e,t){let r=ue;if(e.byteLength<18)throw new Error("Invalid gzip archive: too short");let n=new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(e.byteLength-4,true);if(n>r)throw new Error("Refusing to decompress archive: declared size "+n+" exceeds cap "+r+" bytes");let s=gunzipSync(e);if(s.byteLength>r)throw new Error("Refusing to decompress archive: decompressed size "+s.byteLength+" exceeds cap "+r+" bytes");return Ue(s)}var qe=["schema_version","id","type","title","text","facets","tags","concepts","privacy"];function Be(e,t){let r=e.privacy;return !!(!r||!j(r.classification)||typeof r.pii!="boolean"||r.classification==="private"&&!t?.includePrivate||r.pii&&!t?.includePii)}function we(e,t){return e.filter(r=>!Be(r,t))}var Ge=["schema_version","id","type","source","facets","tags","concepts","relationships","provenance","privacy","updated_at"],ie={title:4,tag:3,concept:2,text:1,facet:2,id:1,source:.25};function w(e){return typeof e=="string"&&e.length>0}function T(e,t,r){let i=e[t];i===void 0&&(i=Object.create(null),e[t]=i),i[r]=(i[r]??0)+1;}function E(e){return Number.isInteger(e)&&typeof e=="number"&&e>=0}function O(e){return Number.isInteger(e)&&typeof e=="number"&&e>0}function $e(e){return !e||typeof e!="object"||Array.isArray(e)?false:Object.values(e).every(t=>!!t&&typeof t=="object"&&!Array.isArray(t)&&Object.values(t).every(r=>E(r)))}function y(e){return !!e&&typeof e=="object"&&!Array.isArray(e)}function q(e){return e===void 0||Array.isArray(e)&&e.every(t=>typeof t=="string")}function ye(e){return e==="aiwg.fortemi.index.export.v1"||e==="aiwg.fortemi.index.export.v2"}function be(e){return e==="aiwg.fortemi.index.record.v1"||e==="aiwg.fortemi.index.record.v2"}function ze(e,t,r){if(e.skos_concepts!==void 0)if(!Array.isArray(e.skos_concepts))r.push("items["+t+"].skos_concepts must be an array when present");else for(let[i,n]of e.skos_concepts.entries()){if(!y(n)){r.push("items["+t+"].skos_concepts["+i+"] must be an object");continue}w(n.id)||r.push("items["+t+"].skos_concepts["+i+"].id is required"),w(n.prefLabel)||r.push("items["+t+"].skos_concepts["+i+"].prefLabel is required"),q(n.altLabels)||r.push("items["+t+"].skos_concepts["+i+"].altLabels must be a string array"),n.metadata!==void 0&&!y(n.metadata)&&r.push("items["+t+"].skos_concepts["+i+"].metadata must be an object");}if(e.skos_relations!==void 0)if(!Array.isArray(e.skos_relations))r.push("items["+t+"].skos_relations must be an array when present");else for(let[i,n]of e.skos_relations.entries()){if(!y(n)){r.push("items["+t+"].skos_relations["+i+"] must be an object");continue}w(n.type)||r.push("items["+t+"].skos_relations["+i+"].type is required"),w(n.source_id)||r.push("items["+t+"].skos_relations["+i+"].source_id is required"),w(n.target_id)||r.push("items["+t+"].skos_relations["+i+"].target_id is required"),n.metadata!==void 0&&!y(n.metadata)&&r.push("items["+t+"].skos_relations["+i+"].metadata must be an object");}if(e.provenance_events!==void 0)if(!Array.isArray(e.provenance_events))r.push("items["+t+"].provenance_events must be an array when present");else for(let[i,n]of e.provenance_events.entries()){if(!y(n)){r.push("items["+t+"].provenance_events["+i+"] must be an object");continue}w(n.activity)||r.push("items["+t+"].provenance_events["+i+"].activity is required"),n.attributes!==void 0&&!y(n.attributes)&&r.push("items["+t+"].provenance_events["+i+"].attributes must be an object");}if(Array.isArray(e.relationships))for(let[i,n]of e.relationships.entries()){if(!y(n)){r.push("items["+t+"].relationships["+i+"] must be an object");continue}n.metadata!==void 0&&!y(n.metadata)&&r.push("items["+t+"].relationships["+i+"].metadata must be an object"),n.direction!==void 0&&n.direction!=="upstream"&&n.direction!=="downstream"&&n.direction!=="related"&&r.push("items["+t+"].relationships["+i+"].direction must be upstream, downstream, or related"),n.target_path!==void 0&&typeof n.target_path!="string"&&r.push("items["+t+"].relationships["+i+"].target_path must be a string");}if(e.search!==void 0&&(y(e.search)?(q(e.search.triggers)||r.push("items["+t+"].search.triggers must be a string array"),q(e.search.aliases)||r.push("items["+t+"].search.aliases must be a string array"),q(e.search.tags)||r.push("items["+t+"].search.tags must be a string array"),e.search.frontmatter!==void 0&&!y(e.search.frontmatter)&&r.push("items["+t+"].search.frontmatter must be an object")):r.push("items["+t+"].search must be an object")),e.chunks!==void 0)if(!Array.isArray(e.chunks))r.push("items["+t+"].chunks must be an array when present");else for(let[i,n]of e.chunks.entries()){if(!y(n)){r.push("items["+t+"].chunks["+i+"] must be an object");continue}n.metadata!==void 0&&!y(n.metadata)&&r.push("items["+t+"].chunks["+i+"].metadata must be an object");}if(e.embeddings!==void 0)if(!Array.isArray(e.embeddings))r.push("items["+t+"].embeddings must be an array when present");else for(let[i,n]of e.embeddings.entries()){if(!y(n)){r.push("items["+t+"].embeddings["+i+"] must be an object");continue}let s=n.embedding??n.vector;s!==void 0&&(!Array.isArray(s)||!s.every(o=>typeof o=="number"))&&r.push("items["+t+"].embeddings["+i+"].embedding/vector must be a number array"),n.metadata!==void 0&&!y(n.metadata)&&r.push("items["+t+"].embeddings["+i+"].metadata must be an object");}e.compatibility!==void 0&&!y(e.compatibility)&&r.push("items["+t+"].compatibility must be an object");}function j(e){return e==="private"||e==="sanitized"||e==="public"}function Ae(e){return e==="source"||e==="candidate"||e==="reviewed"||e==="rejected"}function We(e,t,r){if(Array.isArray(e.provenance))for(let[i,n]of e.provenance.entries()){let s="items["+t+"].provenance["+i+"]";if(!y(n)){r.push(s+" must be an object");continue}w(n.field)||r.push(s+".field is required"),w(n.source)||r.push(s+".source is required"),w(n.path)||r.push(s+".path is required"),Ae(n.confidence)||r.push(s+".confidence must be one of source, candidate, reviewed, rejected"),j(n.privacy)||r.push(s+".privacy must be one of private, sanitized, public");}}var Ve=["search","chunks","embeddings","skos_concepts","skos_relations","provenance_events","compatibility"],He=["origin","generated","checksum","updated_at"],Ke=["target_path","direction","metadata"];function Je(e,t,r){if(e.schema_version!=="aiwg.fortemi.index.record.v1")return;let i="items["+t+"]",n=e,s=" is a v2-only field and must be absent on a record.v1 record";for(let o of Ve)n[o]!==void 0&&r.push(i+"."+o+s);if(y(e.source)){let o=e.source;for(let a of He)o[a]!==void 0&&r.push(i+".source."+a+s);}if(y(e.privacy)&&e.privacy.locality!==void 0&&r.push(i+".privacy.locality"+s),Array.isArray(e.relationships))for(let[o,a]of e.relationships.entries()){if(!y(a))continue;let c=a;for(let g of Ke)c[g]!==void 0&&r.push(i+".relationships["+o+"]."+g+s);}}function z(e){let t=[],r=Object.create(null),i=y(e)?e:{};y(e)||t.push("index export must be an object"),ye(i?.schema_version)||t.push("schema_version must be aiwg.fortemi.index.export.v1 or aiwg.fortemi.index.export.v2"),w(i?.generated_at)||t.push("generated_at is required"),w(i?.source?.repo)||t.push("source.repo is required"),w(i?.source?.privacy)?j(i?.source?.privacy)||t.push("source.privacy must be one of private, sanitized, public"):t.push("source.privacy is required"),Array.isArray(i?.items)||t.push("items must be an array"),i.compatibility!==void 0&&!y(i.compatibility)&&t.push("compatibility must be an object"),i?.schema_version==="aiwg.fortemi.index.export.v1"&&(y(i.source)&&i.source.graph!==void 0&&t.push("source.graph is a v2-only field and must be absent on an export.v1 export"),i.compatibility!==void 0&&t.push("compatibility is a v2-only field and must be absent on an export.v1 export"));let n=new Set,s="",o=Array.isArray(i.items)?i.items:[];for(let[a,c]of o.entries()){if(!y(c)){t.push("items["+a+"] must be an object");continue}for(let g of Ge)g in c||t.push("items["+a+"]."+g+" is required");be(c.schema_version)||t.push("items["+a+"].schema_version must be aiwg.fortemi.index.record.v1 or aiwg.fortemi.index.record.v2"),i.schema_version==="aiwg.fortemi.index.export.v1"&&c.schema_version==="aiwg.fortemi.index.record.v2"&&t.push("items["+a+"].schema_version must match aiwg.fortemi.index.export.v1"),w(c.id)||t.push("items["+a+"].id is required"),w(c.id)&&n.has(c.id)&&t.push("duplicate id: "+c.id),w(c.id)&&n.add(c.id),s&&w(c.id)&&s>c.id&&t.push("items must be sorted by id: "+s+" before "+c.id),w(c.id)&&(s=c.id),w(c.type)?r[c.type]=(r[c.type]??0)+1:t.push("items["+a+"].type must be a non-empty string"),w(c.source?.path)||t.push("items["+a+"].source.path is required"),w(c.source?.repo_relative_path)||t.push("items["+a+"].source.repo_relative_path is required"),w(c.source?.locator)||t.push("items["+a+"].source.locator is required"),typeof c.title!="string"&&typeof c.search?.title!="string"&&typeof c.search?.name!="string"&&t.push("items["+a+"].title or search.title/search.name is required"),typeof c.text!="string"&&typeof c.search?.body!="string"&&typeof c.search?.summary!="string"&&t.push("items["+a+"].text or search.body/search.summary is required"),Array.isArray(c.tags)||t.push("items["+a+"].tags must be an array"),Array.isArray(c.concepts)||t.push("items["+a+"].concepts must be an array"),Array.isArray(c.relationships)||t.push("items["+a+"].relationships must be an array"),(!Array.isArray(c.provenance)||c.provenance.length===0)&&t.push("items["+a+"].provenance must be a non-empty array"),ze(c,a,t),We(c,a,t),Je(c,a,t),!c.privacy||typeof c.privacy.pii!="boolean"||!w(c.privacy.classification)?t.push("items["+a+"].privacy requires classification and pii"):j(c.privacy.classification)||t.push("items["+a+"].privacy.classification must be one of private, sanitized, public");}return {valid:t.length===0,errors:t,counts:r}}function W(e){let t=z(e);if(!t.valid)throw new Error(`Invalid AIWG Fortemi index export:
|
|
2
2
|
`+t.errors.join(`
|
|
3
|
-
`));return e}function
|
|
3
|
+
`));return e}function Ye(e){let t=[],r=y(e)?e:{};if(y(e)||t.push("chunk manifest must be an object"),r?.schema_version!=="aiwg.fortemi.index.chunk-manifest.v1"&&t.push("schema_version must be aiwg.fortemi.index.chunk-manifest.v1"),w(r?.generated_at)||t.push("generated_at is required"),w(r?.source?.repo)||t.push("source.repo is required"),w(r?.source?.privacy)||t.push("source.privacy is required"),r?.source_export_schema_version!==void 0&&!ye(r.source_export_schema_version)&&t.push("source_export_schema_version must be aiwg.fortemi.index.export.v1 or aiwg.fortemi.index.export.v2 when present"),E(r?.total)||t.push("total must be a non-negative integer"),O(r?.part_size)||t.push("part_size must be a positive integer"),r.facets!==void 0&&!$e(r.facets)&&t.push("facets must be a nested string-to-number count object"),r.projection!==void 0)if(!Array.isArray(r.projection)||!r.projection.every(s=>typeof s=="string"))t.push("projection must be an array of field names");else {let s=new Set(r.projection);for(let o of qe)s.has(o)||t.push("projection must include scan-required field "+o);}r.detail!==void 0&&!y(r.detail)?t.push("detail must be an object"):r.detail!==void 0&&(w(r.detail.href)?r.detail.href.includes("{id}")||t.push("detail.href must contain the {id} placeholder"):t.push("detail.href is required"),r.detail.encoding!==void 0&&r.detail.encoding!=="uri"&&r.detail.encoding!=="base64url"&&t.push("detail.encoding must be 'uri' or 'base64url'")),Array.isArray(r?.parts)||t.push("parts must be an array");let i=0,n=Array.isArray(r?.parts)?r.parts:[];for(let[s,o]of n.entries()){if(!y(o)){t.push("parts["+s+"] must be an object");continue}w(o.href)||t.push("parts["+s+"].href is required"),E(o.offset)||t.push("parts["+s+"].offset must be a non-negative integer"),E(o.count)||t.push("parts["+s+"].count must be a non-negative integer"),E(o.offset)&&o.offset!==i&&t.push("parts["+s+"].offset must be "+i),E(o.count)&&(i+=o.count);}return E(r?.total)&&i!==r.total&&t.push("parts counts must add up to total"),{valid:t.length===0,errors:t}}function Ze(e){let t=Ye(e);if(!t.valid)throw new Error(`Invalid AIWG Fortemi chunk manifest:
|
|
4
4
|
`+t.errors.join(`
|
|
5
|
-
`));return e}function
|
|
6
|
-
`+
|
|
7
|
-
`));return e}var
|
|
8
|
-
`)??"",
|
|
9
|
-
`)}function
|
|
10
|
-
`)??"":""}function
|
|
11
|
-
`):[i
|
|
12
|
-
`)}function
|
|
13
|
-
`),
|
|
5
|
+
`));return e}function Xe(e,t,r){let i="items["+t+"]";if(!e.privacy||!y(e.privacy)?r.push(i+"/privacy requires classification and pii"):(j(e.privacy.classification)||r.push(i+"/privacy/classification must be one of private, sanitized, public"),typeof e.privacy.pii!="boolean"&&r.push(i+"/privacy/pii must be boolean")),Array.isArray(e.provenance))for(let[n,s]of e.provenance.entries()){if(!y(s)){r.push(i+"/provenance/"+n+" must be an object");continue}Ae(s.confidence)||r.push(i+"/provenance/"+n+"/confidence must be one of source, candidate, reviewed, rejected"),j(s.privacy)||r.push(i+"/provenance/"+n+"/privacy must be one of private, sanitized, public");}}function et(e,t){let r=[],i=new Set,n="";for(let[s,o]of e.entries()){if(!y(o)){r.push("items["+s+"] must be an object");continue}Xe(o,s,r);let a=t==="aiwg.fortemi.index.export.v2"?"aiwg.fortemi.index.record.v2":"aiwg.fortemi.index.record.v1";o.schema_version!==a&&r.push(`items[${s}].schema_version must match ${t}`),be(o.schema_version)||r.push("items["+s+"].schema_version must be aiwg.fortemi.index.record.v1 or aiwg.fortemi.index.record.v2"),w(o.id)||r.push("items["+s+"].id is required"),w(o.id)&&i.has(o.id)&&r.push("duplicate id: "+o.id),w(o.id)&&i.add(o.id),n&&w(o.id)&&n>o.id&&r.push("items must be sorted by id: "+n+" before "+o.id),w(o.id)&&(n=o.id),w(o.type)||r.push("items["+s+"].type must be a non-empty string"),typeof o.title!="string"&&typeof o.search?.title!="string"&&typeof o.search?.name!="string"&&r.push("items["+s+"].title or search.title/search.name is required"),typeof o.text!="string"&&typeof o.search?.body!="string"&&typeof o.search?.summary!="string"&&r.push("items["+s+"].text or search.body/search.summary is required"),(!o.facets||typeof o.facets!="object"||Array.isArray(o.facets))&&r.push("items["+s+"].facets must be an object"),Array.isArray(o.tags)||r.push("items["+s+"].tags must be an array"),Array.isArray(o.concepts)||r.push("items["+s+"].concepts must be an array");}return r}function tt(e,t,r){let i=[],n=y(e)?e:{};if(y(e)||i.push("chunk part must be an object"),n?.schema_version!=="aiwg.fortemi.index.chunk.v1"&&i.push("schema_version must be aiwg.fortemi.index.chunk.v1"),n?.manifest_schema_version!=="aiwg.fortemi.index.chunk-manifest.v1"&&i.push("manifest_schema_version must be aiwg.fortemi.index.chunk-manifest.v1"),E(n?.offset)||i.push("offset must be a non-negative integer"),Array.isArray(n?.items)||i.push("items must be an array"),t&&E(n?.offset)&&n.offset!==t.offset&&i.push("offset must match manifest part offset "+t.offset),t&&Array.isArray(n?.items)&&n.items.length!==t.count&&i.push("items length must match manifest part count "+t.count),Array.isArray(n?.items))if(r?.projection)i.push(...et(n.items,r.source_export_schema_version??"aiwg.fortemi.index.export.v1").map(s=>"items."+s));else {let s=z({schema_version:r?.source_export_schema_version??"aiwg.fortemi.index.export.v1",generated_at:r?.generated_at??"1970-01-01T00:00:00.000Z",source:r?.source??{repo:"chunk",privacy:"public"},...(r?.source_export_schema_version??"aiwg.fortemi.index.export.v1")==="aiwg.fortemi.index.export.v2"?{compatibility:{previous_schema_version:"aiwg.fortemi.index.export.v1",strategy:"supported"}}:{},items:n.items});i.push(...s.errors.map(o=>"items."+o));}return {valid:i.length===0,errors:i}}function rt(e,t,r){let i=tt(e,t,r);if(!i.valid)throw new Error(`Invalid AIWG Fortemi chunk part:
|
|
6
|
+
`+i.errors.join(`
|
|
7
|
+
`));return e}var le=new Set(["http:","https:","blob:","data:"]);function it(e){try{return new URL(e)}catch{return null}}function xe(e,t){if(t===void 0){let n=it(e);if(n&&!le.has(n.protocol))throw new Error("Refusing AIWG index fetch with disallowed scheme: "+n.protocol);return e}let r=new URL(t),i=new URL(e,r);if(!le.has(i.protocol))throw new Error("Refusing AIWG index fetch with disallowed scheme: "+i.protocol);if(i.origin!==r.origin)throw new Error("Refusing cross-origin AIWG index fetch: "+i.origin+" != "+r.origin);return i.toString()}function ir(e){return async t=>{let r=xe(t.href,e),i=await fetch(r);if(!i.ok)throw new Error("Failed to fetch AIWG index chunk "+r+": "+i.status);return i.json()}}function nt(e,t="base64url"){if(t==="uri")return encodeURIComponent(e);let r=new TextEncoder().encode(e),i="";for(let n of r)i+=String.fromCharCode(n);return btoa(i).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}function _e(e,t){return e.href.replace("{id}",nt(t,e.encoding??"uri"))}function nr(e){return async(t,r)=>{if(!r.detail?.href)throw new Error("Manifest has no detail.href for record resolution");let i=_e(r.detail,t),n=xe(i,e),s=await fetch(n);if(!s.ok)throw new Error("Failed to fetch AIWG index detail "+n+": "+s.status);return s.json()}}function ve(e){let t=Object.create(null);for(let r of e){T(t,"type",r.type),T(t,"privacy",r.privacy.classification);for(let i of r.tags)T(t,"tag",i);for(let i of r.concepts)T(t,"concept",i);for(let[i,n]of Object.entries(r.facets))for(let s of n)T(t,i,s);}return t}function D(e){return e.title??e.search?.title??e.search?.name??e.id}function Q(e){let t=e.text??e.search?.body??e.search?.summary??e.chunks?.map(i=>i.text??i.body??i.summary??"").filter(Boolean).join(`
|
|
8
|
+
`)??"",r=ke(e);return [t,r].filter(Boolean).join(`
|
|
9
|
+
`)}function ke(e){return "binary_sources"in e?e.binary_sources?.map(t=>t.extracted_text).filter(Boolean).join(`
|
|
10
|
+
`)??"":""}function st(e,t){let r=D(e),i=Q(e);return t==="title-summary"?[r,e.search?.summary??"",ke(e)].filter(Boolean).join(`
|
|
11
|
+
`):[r,i].filter(Boolean).join(`
|
|
12
|
+
`)}function ot(e){return e instanceof Date?e.toISOString():e??new Date().toISOString()}var at=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298];function F(e,t){return e>>>t|e<<32-t}function ct(e){let t=e.length*8,r=new Uint8Array(e.length+9+63>>6<<6);r.set(e),r[e.length]=128;let i=new DataView(r.buffer);i.setUint32(r.length-4,t>>>0),i.setUint32(r.length-8,Math.floor(t/4294967296));let n=1779033703,s=3144134277,o=1013904242,a=2773480762,c=1359893119,g=2600822924,d=528734635,p=1541459225,u=new Uint32Array(64);for(let h=0;h<r.length;h+=64){for(let f=0;f<16;f++)u[f]=i.getUint32(h+f*4);for(let f=16;f<64;f++){let m=F(u[f-15],7)^F(u[f-15],18)^u[f-15]>>>3,b=F(u[f-2],17)^F(u[f-2],19)^u[f-2]>>>10;u[f]=u[f-16]+m+u[f-7]+b>>>0;}let A=n,v=s,_=o,x=a,k=c,R=g,S=d,l=p;for(let f=0;f<64;f++){let m=F(k,6)^F(k,11)^F(k,25),b=k&R^~k&S,P=l+m+b+at[f]+u[f]>>>0,V=F(A,2)^F(A,13)^F(A,22),H=A&v^A&_^v&_,U=V+H>>>0;l=S,S=R,R=k,k=x+P>>>0,x=_,_=v,v=A,A=P+U>>>0;}n=n+A>>>0,s=s+v>>>0,o=o+_>>>0,a=a+x>>>0,c=c+k>>>0,g=g+R>>>0,d=d+S>>>0,p=p+l>>>0;}return [n,s,o,a,c,g,d,p].map(h=>h.toString(16).padStart(8,"0")).join("")}function dt(e){return `sha256:${ct(e)}`}async function sr(e,t){W(e);let r=t.granularity??"body",i=we(t.records??e.items,t.privacy),n=[];for(let o of i){let a=t.textForRecord?.(o)??st(o,r),c=await t.backend.embed(a,o);if(c.length!==t.backend.dimensions)throw new Error(`Embedding for ${o.id} has ${c.length} dimensions; expected ${t.backend.dimensions}`);n.push({record_id:o.id,embedding:c,granularity:r,input_hash:dt(new TextEncoder().encode(a)),source_path:o.source.path});}let s={schema_version:"aiwg.fortemi.embedding.set.v1",id:t.id,model:t.backend.model,dimensions:t.backend.dimensions,generated_at:ot(t.generatedAt),granularity:r,...t.metric?{metric:t.metric}:{},input_hash_algorithm:"sha256",embeddings:n};return ne(s),s}function ut(e){let t=e.search,r=[e.id,D(e),Q(e),t?.title,t?.name,t?.summary,t?.body,t?.capability,t?.phase,t?.type,...t?.triggers??[],...t?.aliases??[],...t?.tags??[],...(e.chunks??[]).flatMap(i=>[i.text,i.body,i.summary,i.source_path])];if(t?.frontmatter)for(let i of Object.values(t.frontmatter))typeof i=="string"?r.push(i):Array.isArray(i)&&r.push(...i.filter(n=>typeof n=="string"));return r.filter(i=>typeof i=="string"&&i.length>0)}function or(e,t={}){W(e);let r=O(t.partSize)?t.partSize:500,i=t.projection,n=t.idEncoding??"base64url",s=t.detailHref??"detail/{id}.json",o=we(e.items,t.privacy),a=u=>String(u).padStart(4,"0"),c=u=>{if(!i)return u;let h={};for(let A of i)h[A]=u[A];return h},g=[],d=[];for(let u=0,h=0;u<o.length;u+=r,h+=1){let A=o.slice(u,u+r),v="part-"+a(h)+".json";g.push({href:v,part:{schema_version:"aiwg.fortemi.index.chunk.v1",manifest_schema_version:"aiwg.fortemi.index.chunk-manifest.v1",offset:u,items:A.map(c)}}),d.push({href:v,offset:u,count:A.length});}return {manifest:{schema_version:"aiwg.fortemi.index.chunk-manifest.v1",generated_at:t.generatedAt??e.generated_at,source:e.source,source_export_schema_version:e.schema_version,total:o.length,part_size:r,facets:ve(o),parts:d,...i?{projection:i,detail:{href:s,encoding:n}}:{}},parts:g,details:i?o.map(u=>({id:u.id,href:_e({href:s,encoding:n},u.id),record:u})):[]}}function X(e,t){if(!t||t.length===0)return true;let r=new Set(e);return t.every(i=>r.has(i))}function gt(e,t){return t?Object.entries(t).every(([r,i])=>X(e.facets[r]??[],i)):true}function lt(e,t){if(!t)return [];let r=[],i=D(e),n=Q(e);i.toLowerCase().includes(t)&&r.push({field:"title",value:i}),n.toLowerCase().includes(t)&&r.push({field:"text",value:n});for(let s of e.tags)s.toLowerCase().includes(t)&&r.push({field:"tag",value:s});for(let s of e.concepts)s.toLowerCase().includes(t)&&r.push({field:"concept",value:s});for(let s of ut(e))s!==i&&s!==n&&s.toLowerCase().includes(t)&&r.push({field:"text",value:s,score:ie.text});return r}var mt=new Set(["the","a","an","and","or","of","for","to","in","on","with","into","from","is","are","be","i","we","my","it","you","me","us","your","our","this","that","these","those","there","here","some","any","all","also","please","about","how","what","which","where","when","who","why","find","give","show","need","want","looking","look","help","do","does","did","can","could","should","would","will","handle","handles","handling","aiwg","skill","skills","agent","agents","command","commands","rule","rules","flow","flows","workflow","workflows"]);function G(e){return e.toLowerCase().replace(/[-_\s]+/g," ").trim()}function ft(e){return e.toLowerCase().split(/[^a-z0-9-]+/).filter(t=>t.length>1&&!mt.has(t))}function Y(e,t){return t.flatMap(r=>e.facets[r]??[])}function me(e,t){e.some(r=>r.field===t.field&&r.value===t.value&&r.reason===t.reason)||e.push(t);}function pt(e,t){if(e===t)return true;if(Math.abs(e.length-t.length)>1)return false;if(e.length===t.length){let a=-1,c=0;for(let g=0;g<e.length;g+=1)e[g]!==t[g]&&(a<0&&(a=g),c+=1);return c===1?true:c===2&&a+1<e.length&&e[a]===t[a+1]&&e[a+1]===t[a]}let r=e.length<t.length?e:t,i=e.length<t.length?t:e,n=0,s=0,o=0;for(;n<r.length&&s<i.length;)if(r[n]===i[s])n+=1,s+=1;else {if(o+=1,o>1)return false;s+=1;}return true}function ht(e,t){let r=G(e).split(/\s+/).filter(Boolean),i=G(t).split(/\s+/).filter(Boolean);return r.length!==i.length?false:r.every((n,s)=>{let o=i[s];return n===o?true:n.length<5||o.length<5?false:pt(n,o)})}function wt(e){return e.map(t=>t.toLowerCase())}function yt(e,t,r={}){if(!t)return [];let i=[],n=ft(t),s=n.length>0?n.join(" "):t.toLowerCase().trim(),o=t.toLowerCase().trim(),a=e.id.split(/[:/]/),c=[e.id,D(e),e.search?.name,e.search?.title,...e.search?.aliases??[],...a,...Y(e,["name","canonical_name","command","skill","agent","rule"])].filter(m=>w(m)),g=[...Y(e,["trigger","triggers","trigger_phrase","trigger_phrases"]),...e.search?.triggers??[]],d=[...Y(e,["capability","capabilities","summary","description"]),e.search?.capability,e.search?.summary,e.search?.phase,e.search?.type,...e.search?.tags??[],...e.concepts,...e.tags].filter(m=>w(m)),p=[e.source?.path,e.source?.repo_relative_path,e.source?.locator].filter(m=>w(m)),u=D(e),h=[e.search?.summary,Q(e)].filter(m=>w(m)).join(`
|
|
13
|
+
`),A=e.search?.type??e.type,v=[...e.search?.tags??[],...e.tags],_=n.length>1,x=_?r.relaxOverlap?1:Math.ceil(n.length/2):1,k=m=>_&&m>=x,R=m=>me(i,{...m,score:0}),S=m=>{m>0&&me(i,{field:"id",value:e.id,score:m,reason:"aiwg discovery score"});};for(let m of c){if(G(t)===G(m))return R({field:"id",value:m,reason:"exact canonical name"}),S(1.001),i;if(ht(t,m))return R({field:"id",value:m,reason:"near canonical name"}),S(.951),i}let l=0,f=(m,b,P,V,H,U,Fe=0)=>{let K=b.toLowerCase();if(K.includes(s))l+=V*U,K===s&&(l+=Fe),R({field:m,value:b,reason:P});else if(_){let se=n.filter(Ee=>K.includes(Ee)).length;k(se)&&(l+=H*U*(se/n.length),R({field:m,value:b,reason:P}));}};for(let m of wt(g))if(m===s||m===o)return R({field:"facet",value:m,reason:"trigger phrase"}),S(1.0008),i;for(let m of g)f("facet",m,"trigger phrase",.25,.06,4);for(let m of d)f("concept",m,"capability overlap",.2,.1,2);f("title",u,"title token overlap",.3,.08,3,.2);for(let m of v)f("tag",m,"tag token overlap",.2,.05,2);h&&f("text",h,"body token overlap",.15,.04,1);for(let m of p)f("source",m,"path overlap",.1,.03,1);return A.toLowerCase().includes(s)&&(l+=.1,R({field:"facet",value:A,reason:"type overlap"})),S(Math.min(l,1)),i}function bt(e,t){return e.reduce((r,i)=>r+(i.score??t[i.field]),0)}function At(e,t,r){let i=Math.max(20,r);if(!e)return "";if(!t)return e.length>i?`${e.slice(0,i).trimEnd()}...`:e;let s=e.toLowerCase().indexOf(t);if(s<0)return e.length>i?`${e.slice(0,i).trimEnd()}...`:e;let o=Math.max(0,Math.floor((i-t.length)/2)),a=Math.max(0,s-o),c=Math.min(e.length,a+i),g=a>0?"...":"",d=c<e.length?"...":"";return `${g}${e.slice(a,c).trim()}${d}`}function xt(e,t,r,i){let n=t.find(a=>a.field==="text"),s=t.find(a=>a.field==="title"),o=n??s??t[0];return At(o?.value??Q(e),r,i)}function ee(e,t,r,i=0,n={}){let s={...ie,...r.weights},o=r.searchProfile??"default";return e.map((a,c)=>({item:a,ordinal:i+c,matches:o==="aiwg-discovery"?yt(a,t,n):lt(a,t)})).filter(({item:a,matches:c})=>!(t&&c.length===0||r.types&&!r.types.includes(a.type)||r.privacy&&!r.privacy.includes(a.privacy.classification)||!X(a.tags,r.tags)||!X(a.concepts,r.concepts)||!gt(a,r.facets)||r.relationshipTargetId&&!(a.relationships??[]).some(g=>g.target_id===r.relationshipTargetId))).map(({item:a,ordinal:c,matches:g})=>({item:a,ordinal:c,rank:bt(g,s),matches:g}))}function _t(e,t){return [...e].sort((r,i)=>t&&i.rank-r.rank||r.ordinal-i.ordinal)}function $(e,t,r){let i=_t(e,r.rank),n=r.offset??0,s=r.limit??i.length,o=i.slice(n,n+s),a={items:o.map(c=>c.item),total:i.length,facets:ve(i.map(c=>c.item))};if(r.rank||r.snippets||r.includeMatches){let c=r.snippetLength??160;a.rankedItems=o.map(g=>({item:g.item,rank:g.rank,...r.snippets?{snippet:xt(g.item,g.matches,t,c)}:{},...r.includeMatches?{matches:g.matches}:{}}));}return a}function Re(e,t="",r={}){let i=t.trim().toLowerCase(),n=ee(e.items,i,r);return n.length===0&&i&&r.searchProfile==="aiwg-discovery"?$(ee(e.items,i,r,0,{relaxOverlap:true}),i,r):$(n,i,r)}function Se(e,t){if(e.length!==t.length||e.length===0)return 0;let r=0,i=0,n=0;for(let s=0;s<e.length;s+=1){let o=e[s],a=t[s];r+=o*a,i+=o*o,n+=a*a;}return i===0||n===0?0:r/(Math.sqrt(i)*Math.sqrt(n))}function vt(e){let t=[],r=y(e)?e:{};y(e)||t.push("embedding set must be an object"),r?.schema_version!=="aiwg.fortemi.embedding.set.v1"&&t.push("schema_version must be aiwg.fortemi.embedding.set.v1"),w(r?.id)||t.push("id is required"),w(r?.model)||t.push("model is required"),O(r?.dimensions)||t.push("dimensions must be a positive integer"),w(r?.generated_at)||t.push("generated_at is required"),w(r?.granularity)||t.push("granularity is required"),Array.isArray(r?.embeddings)||t.push("embeddings must be an array");let i=Array.isArray(r.embeddings)?r.embeddings:[];for(let[n,s]of i.entries()){if(!y(s)){t.push("embeddings["+n+"] must be an object");continue}w(s.record_id)||t.push("embeddings["+n+"].record_id is required"),w(s.input_hash)||t.push("embeddings["+n+"].input_hash is required"),Array.isArray(s.embedding)?O(r?.dimensions)&&s.embedding.length!==r.dimensions?t.push("embeddings["+n+"].embedding length must match dimensions"):s.embedding.every(o=>typeof o=="number"&&Number.isFinite(o))||t.push("embeddings["+n+"].embedding must contain finite numbers"):t.push("embeddings["+n+"].embedding must be an array");}return {valid:t.length===0,errors:t}}function ne(e){let t=vt(e);if(!t.valid)throw new Error(`Invalid AIWG Fortemi embedding set:
|
|
14
14
|
`+t.errors.join(`
|
|
15
|
-
`));return e}function et(e,t,i,r={}){if(Y(t),i.length!==t.dimensions)throw new Error("query embedding length must match embedding set dimensions");let n=new Map(e.items.map(a=>[a.id,a])),s=r.offset??0,o=r.limit??20;return t.embeddings.map(a=>{let c=n.get(a.record_id);return c?{item:c,embedding:a,score:fe(i,a.embedding)}:null}).filter(a=>a!==null&&a.score>=(r.minScore??-1)).sort((a,c)=>c.score-a.score||a.item.id.localeCompare(c.item.id)).slice(s,s+o)}function kt(e,t,i,r,n={}){let s={...n};delete s.limit,delete s.offset;let o=ge(e,i,{...s,rank:true}),a=et(e,t,r,{limit:e.items.length}),c=n.lexicalWeight??.5,g=n.semanticWeight??.5,u=new Map(o.rankedItems?.map(A=>[A.item.id,A.rank])??[]),f=Math.max(1,...u.values()),d=new Map(a.flatMap(A=>A.embedding?[[A.item.id,A.embedding]]:[])),m=new Map(a.map(A=>[A.item.id,A.score])),y=new Set([...u.keys(),...m.keys()]),b=n.offset??0,x=n.limit??20;return [...y].map(A=>{let v=e.items.find(k=>k.id===A),R=d.get(A);return v?{item:v,...R?{embedding:R}:{},score:(u.get(A)??0)/f*c+(m.get(A)??0)*g}:null}).filter(A=>A!==null&&A.score>=(n.minScore??-1)).sort((A,v)=>v.score-A.score||A.item.id.localeCompare(v.item.id)).slice(b,b+x)}var tt=5e3;function _t(e,t,i=.9,r){Y(t);let n=r?.maxEmbeddings??tt;if(t.embeddings.length>n)throw new Error("Embedding set too large for duplicate scan: "+t.embeddings.length+" > "+n+" (raise options.maxEmbeddings to override for trusted input)");let s=new Map(e.items.map(a=>[a.id,a])),o=[];for(let a=0;a<t.embeddings.length;a+=1)for(let c=a+1;c<t.embeddings.length;c+=1){let g=t.embeddings[a],u=t.embeddings[c],f=s.get(g.record_id),d=s.get(u.record_id);if(!f||!d)continue;let m=fe(g.embedding,u.embedding);m>=i&&o.push({left:f,right:d,score:m});}return o.sort((a,c)=>c.score-a.score||a.left.id.localeCompare(c.left.id))}function it(e){return `${e.offset}:${e.href}`}function rt(e){return S(e)?e:3}function nt(e){return S(e)?e:32}function st(e){return S(e)?e:5e3}function ot(e,t){return JSON.stringify({q:e,types:t.types??null,facets:t.facets??null,tags:t.tags??null,concepts:t.concepts??null,privacy:t.privacy??null,rel:t.relationshipTargetId??null,searchProfile:t.searchProfile??"default",weights:{...K,...t.weights}})}function at(e,t,i){e.matchCache.delete(t),e.matchCache.set(t,i);let r=0;for(let n of e.matchCache.values())r+=n.length;for(;r>e.maxCachedMatches&&e.matchCache.size>1;){let n=e.matchCache.keys().next().value;if(n===void 0||n===t)break;r-=e.matchCache.get(n)?.length??0,e.matchCache.delete(n);}}function ct(e,t){return e.trim()===""&&!t.rank&&!t.snippets&&!t.includeMatches&&!t.types&&!t.facets&&!t.tags&&!t.concepts&&!t.privacy&&!t.relationshipTargetId}function dt(e,t,i){let r=t+i;return e.parts.filter(n=>n.count>0&&n.offset<r&&n.offset+n.count>t)}async function $(e,t){let i=it(t),r=e.partCache.get(i);if(r)return e.partCache.delete(i),e.partCache.set(i,r),{part:r,fetched:false};let n=Oe(await e.loader(t,e.manifest),t,e.manifest);for(e.partCache.set(i,n);e.partCache.size>e.maxCachedParts;){let s=e.partCache.keys().next().value;if(s===void 0)break;e.partCache.delete(s);}return {part:n,fetched:true}}async function le(e,t){let i=e.detailCache.get(t);if(i)return e.detailCache.delete(t),e.detailCache.set(t,i),i;if(!e.manifest.projection)for(let s of e.partCache.values()){let o=s.items.find(a=>a.id===t);if(o)return o}if(!e.detailLoader)throw new Error("No detailLoader configured to resolve record "+t);let r=await e.detailLoader(t,e.manifest),n=q({schema_version:"aiwg.fortemi.index.export.v1",generated_at:e.manifest.generated_at,source:e.manifest.source,items:[r]}).items[0];if(n.id!==t)throw new Error("Detail record id mismatch: expected "+t+", got "+n.id);for(e.detailCache.set(t,n);e.detailCache.size>e.maxCachedDetails;){let s=e.detailCache.keys().next().value;if(s===void 0)break;e.detailCache.delete(s);}return n}function ut(e){return e?.relationshipType??e?.type}function gt(e,t){return {source_id:e,target_id:t.target_id,type:t.type,...t.source_path?{source_path:t.source_path}:{},...t.target_path?{target_path:t.target_path}:{},...t.direction?{direction:t.direction}:{}}}function ft(e,t={}){let i=ut(t);return !(i&&e.type!==i||t.relationshipDirection&&e.direction!==t.relationshipDirection||t.sourceId&&e.source_id!==t.sourceId||t.targetId&&e.target_id!==t.targetId||t.endpointId&&e.source_id!==t.endpointId&&e.target_id!==t.endpointId)}function lt(e){if(e.direction==="out"&&!e.sourceId)throw new Error("relationship direction 'out' requires sourceId");if(e.direction==="in"&&!e.targetId)throw new Error("relationship direction 'in' requires targetId")}function mt(e){return {id:e.id,type:e.type,title:E(e)}}function ee(e,t){t&&e.set(t.id,mt(t));}function H(e,t={}){lt(t);let i=new Map(e.map(a=>[a.id,a])),r=[];for(let a of e)for(let c of a.relationships??[]){let g=gt(a.id,c);ft(g,t)&&r.push(g);}let n=r.sort((a,c)=>a.source_id.localeCompare(c.source_id)||a.target_id.localeCompare(c.target_id)||a.type.localeCompare(c.type)),s=t.limit?n.slice(0,t.limit):n,o=new Map;for(let a of s)ee(o,i.get(a.source_id)),ee(o,i.get(a.target_id));return {nodes:[...o.values()].sort((a,c)=>a.id.localeCompare(c.id)),edges:s,complete:true}}function pt(e,t={}){let i=t.direction??"both";return {...t,...i==="out"?{sourceId:e}:{},...i==="in"?{targetId:e}:{},...i==="both"?{endpointId:e}:{}}}function ht(e,t,i={}){let r=i.direction??"both",n=t.edges.filter(o=>r==="out"?o.source_id===e:r==="in"?o.target_id===e:o.source_id===e||o.target_id===e),s=new Set;for(let o of n)s.add(o.source_id),s.add(o.target_id);return {...t,edges:n,nodes:t.nodes.filter(o=>s.has(o.id))}}async function me(e,t){let i=0,r=0,n=[];for(let s of e.manifest.parts){let o=await $(e,s);o.fetched&&(r+=1),i+=1,t?.({phase:"part",done:i,total:e.manifest.parts.length,href:s.href});for(let a of o.part.items)n.push(a.relationships?a:await le(e,a.id));}return {records:n,scannedParts:i,fetchedParts:r}}async function te(e,t={}){let i=await me(e);return {...H(i.records,t),scannedParts:i.scannedParts,fetchedParts:i.fetchedParts}}async function wt(e,t="",i={}){let r=t.trim().toLowerCase(),n=0,s=0;if(ct(t,i)){let g=i.offset??0,u=i.limit??e.manifest.total,f=dt(e.manifest,g,u),d=[];for(let m of f){let y=await $(e,m);y.fetched&&(s+=1),n+=1,i.onProgress?.({phase:"part",done:n,total:f.length,href:m.href});let b=Math.max(0,g-m.offset),x=Math.min(y.part.items.length,g+u-m.offset);d.push(...y.part.items.slice(b,x));}return {items:d,total:e.manifest.total,facets:e.manifest.facets??{},manifestTotal:e.manifest.total,scannedParts:n,fetchedParts:s,complete:true}}let o=ot(r,i),a=e.matchCache.get(o);if(a)return e.matchCache.delete(o),e.matchCache.set(o,a),{...T(a,r,i),manifestTotal:e.manifest.total,scannedParts:0,fetchedParts:0,complete:true};let c=[];for(let g of e.manifest.parts){let u=await $(e,g);u.fetched&&(s+=1),n+=1,i.onProgress?.({phase:"part",done:n,total:e.manifest.parts.length,href:g.href}),c.push(...U(u.part.items,r,i,g.offset)),i.onProgress?.({phase:"query",done:n,total:e.manifest.parts.length,href:g.href});}return at(e,o,c),{...T(c,r,i),manifestTotal:e.manifest.total,scannedParts:n,fetchedParts:s,complete:true}}function yt(e,t,i=new Date().toISOString()){return {schema_version:"aiwg.fortemi.review-decisions.v1",generated_at:i,source_export_schema_version:e.schema_version,decisions:[...t].sort((r,n)=>r.item_id.localeCompare(n.item_id))}}function It(e){let t=e??null,i=null,r=null,n=null,s=[],o=new Set,a=()=>({index:t,chunked:i?{manifest:i.manifest,cachedParts:i.partCache.size,maxCachedParts:i.maxCachedParts}:null,data:r,error:n,reviewDecisions:[...s]}),c=()=>{let u=a();for(let f of o)f(u);},g=()=>{if(!t)throw new Error("No AIWG index export loaded");return t};return {loadIndex(u){try{let f=q(u);return t=f,i=null,r=null,s=[],n=null,c(),f}catch(f){throw n=f instanceof Error?f:new Error(String(f)),c(),n}},loadChunkedIndex(u,f,d={}){try{let m=Fe(u);return t=null,i={manifest:m,loader:f,maxCachedParts:rt(d.maxCachedParts),partCache:new Map,detailLoader:d.detailLoader,maxCachedDetails:nt(d.maxCachedDetails),detailCache:new Map,maxCachedMatches:st(d.maxCachedMatches),matchCache:new Map},r=null,s=[],n=null,c(),m}catch(m){throw n=m instanceof Error?m:new Error(String(m)),c(),n}},getIndex(){return t},getChunkedManifest(){return i?.manifest??null},getSnapshot(){return a()},query(u="",f){let d=ge(g(),u,f);return r=d,n=null,c(),d},async queryChunked(u="",f){if(!i)throw new Error("No AIWG chunked index manifest loaded");try{let d=await wt(i,u,f);return r=d,n=null,c(),d}catch(d){throw n=d instanceof Error?d:new Error(String(d)),c(),n}},async getRecord(u){if(i)try{return await le(i,u)}catch(d){throw n=d instanceof Error?d:new Error(String(d)),c(),n}let f=g().items.find(d=>d.id===u);if(!f)throw new Error("Record not found: "+u);return f},async neighbors(u,f){try{let d=pt(u,f),m=i?await te(i,d):H(g().items,d);return ht(u,m,f)}catch(d){throw n=d instanceof Error?d:new Error(String(d)),c(),n}},async relationshipQuery(u){try{return i?await te(i,u):H(g().items,u)}catch(f){throw n=f instanceof Error?f:new Error(String(f)),c(),n}},async relationshipSet(u){let[f,d]=await Promise.all([this.neighbors(u.a,u),this.neighbors(u.b,u)]),m=new Set(f.nodes.map(x=>x.id).filter(x=>x!==u.a)),y=new Set(d.nodes.map(x=>x.id).filter(x=>x!==u.b)),b;return u.op==="intersection"?b=[...m].filter(x=>y.has(x)):u.op==="difference"?b=[...m].filter(x=>!y.has(x)):b=[...new Set([...m,...y])],{op:u.op,ids:b.sort()}},clearChunkCache(){i?.partCache.clear(),i?.detailCache.clear(),i?.matchCache.clear(),n=null,c();},toCommunityGraph(u){return B(g(),u)},async toCommunityGraphChunked(u){if(!i)return B(g(),u);let f=await me(i,u?.onProgress);return B({generated_at:i.manifest.generated_at,source:i.manifest.source,items:f.records},u)},setReviewDecision(u){let f={...u,updated_at:new Date().toISOString()};return s=[...s.filter(d=>d.item_id!==f.item_id),f].sort((d,m)=>d.item_id.localeCompare(m.item_id)),n=null,c(),f},clearReviewDecision(u){s=s.filter(f=>f.item_id!==u),n=null,c();},createReviewDecisionExport(u){let f=t??(i?{schema_version:i.manifest.source_export_schema_version??"aiwg.fortemi.index.export.v1"}:null);if(!f)throw new Error("No AIWG index export or chunked manifest loaded");return yt(f,s,u)},subscribe(u){return o.add(u),()=>{o.delete(u);}}}}function B(e,t={}){let i=new Set(e.items.map(o=>o.id)),r=t.relationshipWeights??Object.create(null),n=new Map;for(let o of e.items)for(let a of o.relationships){if(!i.has(a.target_id)&&!t.includeDanglingRelationships)continue;let c=a.type,g=Object.prototype.hasOwnProperty.call(r,c)?r[c]:void 0,u=typeof g=="number"&&Number.isFinite(g)?g:1,f=`${o.id}\0${a.target_id}\0${c}`,d=n.get(f);d?d.weight+=u:n.set(f,{source:o.id,target:a.target_id,kind:c,weight:u});}let s=new Map;for(let o of e.items){let a=At(o,t);for(let c of a){let g=s.get(c)??[];g.push(o.id),s.set(c,g);}}return {nodes:e.items.map(o=>({id:o.id})),edges:Array.from(n.values()).sort((o,a)=>o.source.localeCompare(a.source)||o.target.localeCompare(a.target)||o.kind.localeCompare(a.kind)),communities:Array.from(s.entries()).map(([o,a])=>({id:o,nodes:[...new Set(a)].sort()})).sort((o,a)=>o.id.localeCompare(a.id))}}function At(e,t){if(t.communityFacet){let i=e.facets[t.communityFacet]??[];if(i.length>0)return i.map(r=>`${t.communityFacet}:${r}`)}if(t.communityTagPrefix){let i=t.communityTagPrefix,r=e.tags.filter(n=>n.startsWith(i));if(r.length>0)return r}return e.concepts.length>0?e.concepts.map(i=>`concept:${i}`):[`type:${e.type}`]}export{we as AIWG_SCAN_REQUIRED_FIELDS,tt as DEFAULT_AIWG_DUPLICATE_SCAN_MAX_EMBEDDINGS,ce as aiwgDetailHrefForId,B as aiwgFortemiIndexToCommunityGraph,Fe as assertAiwgFortemiChunkManifest,Oe as assertAiwgFortemiChunkPart,q as assertAiwgFortemiIndexExport,Y as assertAiwgStaticEmbeddingSet,Rt as buildAiwgChunkedIndex,vt as buildAiwgStaticEmbeddingSet,xt as createAiwgFetchChunkLoader,bt as createAiwgFetchDetailLoader,It as createAiwgIndexController,yt as createAiwgReviewDecisionExport,je as encodeAiwgDetailId,ie as filterAiwgRecordsByPrivacy,_t as findAiwgStaticDuplicatePairs,de as getAiwgFortemiFacets,ge as queryAiwgFortemiIndex,kt as queryAiwgHybridIndex,et as queryAiwgSemanticIndex,ae as resolveAiwgFetchUrl,Ce as validateAiwgFortemiChunkManifest,Ee as validateAiwgFortemiChunkPart,oe as validateAiwgFortemiIndexExport,Ze as validateAiwgStaticEmbeddingSet};//# sourceMappingURL=aiwg-index.js.map
|
|
15
|
+
`));return e}function kt(e,t,r,i={}){if(ne(t),r.length!==t.dimensions)throw new Error("query embedding length must match embedding set dimensions");let n=new Map(e.items.map(a=>[a.id,a])),s=i.offset??0,o=i.limit??20;return t.embeddings.map(a=>{let c=n.get(a.record_id);return c?{item:c,embedding:a,score:Se(r,a.embedding)}:null}).filter(a=>a!==null&&a.score>=(i.minScore??-1)).sort((a,c)=>c.score-a.score||a.item.id.localeCompare(c.item.id)).slice(s,s+o)}function ar(e,t,r,i,n={}){let s={...n};delete s.limit,delete s.offset;let o=Re(e,r,{...s,rank:true}),a=kt(e,t,i,{limit:e.items.length}),c=n.lexicalWeight??.5,g=n.semanticWeight??.5,d=new Map(o.rankedItems?.map(x=>[x.item.id,x.rank])??[]),p=Math.max(1,...d.values()),u=new Map(a.flatMap(x=>x.embedding?[[x.item.id,x.embedding]]:[])),h=new Map(a.map(x=>[x.item.id,x.score])),A=new Set([...d.keys(),...h.keys()]),v=n.offset??0,_=n.limit??20;return [...A].map(x=>{let k=e.items.find(S=>S.id===x),R=u.get(x);return k?{item:k,...R?{embedding:R}:{},score:(d.get(x)??0)/p*c+(h.get(x)??0)*g}:null}).filter(x=>x!==null&&x.score>=(n.minScore??-1)).sort((x,k)=>k.score-x.score||x.item.id.localeCompare(k.item.id)).slice(v,v+_)}var Rt=5e3;function cr(e,t,r=.9,i){ne(t);let n=i?.maxEmbeddings??Rt;if(t.embeddings.length>n)throw new Error("Embedding set too large for duplicate scan: "+t.embeddings.length+" > "+n+" (raise options.maxEmbeddings to override for trusted input)");let s=new Map(e.items.map(a=>[a.id,a])),o=[];for(let a=0;a<t.embeddings.length;a+=1)for(let c=a+1;c<t.embeddings.length;c+=1){let g=t.embeddings[a],d=t.embeddings[c],p=s.get(g.record_id),u=s.get(d.record_id);if(!p||!u)continue;let h=Se(g.embedding,d.embedding);h>=r&&o.push({left:p,right:u,score:h});}return o.sort((a,c)=>c.score-a.score||a.left.id.localeCompare(c.left.id))}function St(e){return `${e.offset}:${e.href}`}function It(e){return O(e)?e:3}function Ct(e){return O(e)?e:32}function Ft(e){return O(e)?e:5e3}function Et(e,t){return JSON.stringify({q:e,types:t.types??null,facets:t.facets??null,tags:t.tags??null,concepts:t.concepts??null,privacy:t.privacy??null,rel:t.relationshipTargetId??null,searchProfile:t.searchProfile??"default",weights:{...ie,...t.weights}})}function Pt(e,t,r){e.matchCache.delete(t),e.matchCache.set(t,r);let i=0;for(let n of e.matchCache.values())i+=n.length;for(;i>e.maxCachedMatches&&e.matchCache.size>1;){let n=e.matchCache.keys().next().value;if(n===void 0||n===t)break;i-=e.matchCache.get(n)?.length??0,e.matchCache.delete(n);}}function Mt(e,t){return e.trim()===""&&!t.rank&&!t.snippets&&!t.includeMatches&&!t.types&&!t.facets&&!t.tags&&!t.concepts&&!t.privacy&&!t.relationshipTargetId}function Ot(e,t,r){let i=t+r;return e.parts.filter(n=>n.count>0&&n.offset<i&&n.offset+n.count>t)}async function te(e,t){let r=St(t),i=e.partCache.get(r);if(i)return e.partCache.delete(r),e.partCache.set(r,i),{part:i,fetched:false};let n=rt(await e.loader(t,e.manifest),t,e.manifest);for(e.partCache.set(r,n);e.partCache.size>e.maxCachedParts;){let s=e.partCache.keys().next().value;if(s===void 0)break;e.partCache.delete(s);}return {part:n,fetched:true}}async function Ie(e,t){let r=e.detailCache.get(t);if(r)return e.detailCache.delete(t),e.detailCache.set(t,r),r;if(!e.manifest.projection)for(let s of e.partCache.values()){let o=s.items.find(a=>a.id===t);if(o)return o}if(!e.detailLoader)throw new Error("No detailLoader configured to resolve record "+t);let i=await e.detailLoader(t,e.manifest),n=W({schema_version:"aiwg.fortemi.index.export.v1",generated_at:e.manifest.generated_at,source:e.manifest.source,items:[i]}).items[0];if(n.id!==t)throw new Error("Detail record id mismatch: expected "+t+", got "+n.id);for(e.detailCache.set(t,n);e.detailCache.size>e.maxCachedDetails;){let s=e.detailCache.keys().next().value;if(s===void 0)break;e.detailCache.delete(s);}return n}function jt(e){return e?.relationshipType??e?.type}function Dt(e,t){return {source_id:e,target_id:t.target_id,type:t.type,...t.source_path?{source_path:t.source_path}:{},...t.target_path?{target_path:t.target_path}:{},...t.direction?{direction:t.direction}:{}}}function Lt(e,t={}){let r=jt(t);return !(r&&e.type!==r||t.relationshipDirection&&e.direction!==t.relationshipDirection||t.sourceId&&e.source_id!==t.sourceId||t.targetId&&e.target_id!==t.targetId||t.endpointId&&e.source_id!==t.endpointId&&e.target_id!==t.endpointId)}function Tt(e){if(e.direction==="out"&&!e.sourceId)throw new Error("relationship direction 'out' requires sourceId");if(e.direction==="in"&&!e.targetId)throw new Error("relationship direction 'in' requires targetId")}function Qt(e){return {id:e.id,type:e.type,title:D(e)}}function fe(e,t){t&&e.set(t.id,Qt(t));}function re(e,t={}){Tt(t);let r=new Map(e.map(a=>[a.id,a])),i=[];for(let a of e)for(let c of a.relationships??[]){let g=Dt(a.id,c);Lt(g,t)&&i.push(g);}let n=i.sort((a,c)=>a.source_id.localeCompare(c.source_id)||a.target_id.localeCompare(c.target_id)||a.type.localeCompare(c.type)),s=t.limit?n.slice(0,t.limit):n,o=new Map;for(let a of s)fe(o,r.get(a.source_id)),fe(o,r.get(a.target_id));return {nodes:[...o.values()].sort((a,c)=>a.id.localeCompare(c.id)),edges:s,complete:true}}function Ut(e,t={}){let r=t.direction??"both";return {...t,...r==="out"?{sourceId:e}:{},...r==="in"?{targetId:e}:{},...r==="both"?{endpointId:e}:{}}}function Nt(e,t,r={}){let i=r.direction??"both",n=t.edges.filter(o=>i==="out"?o.source_id===e:i==="in"?o.target_id===e:o.source_id===e||o.target_id===e),s=new Set;for(let o of n)s.add(o.source_id),s.add(o.target_id);return {...t,edges:n,nodes:t.nodes.filter(o=>s.has(o.id))}}async function Ce(e,t){let r=0,i=0,n=[];for(let s of e.manifest.parts){let o=await te(e,s);o.fetched&&(i+=1),r+=1,t?.({phase:"part",done:r,total:e.manifest.parts.length,href:s.href});for(let a of o.part.items)n.push(a.relationships?a:await Ie(e,a.id));}return {records:n,scannedParts:r,fetchedParts:i}}async function pe(e,t={}){let r=await Ce(e);return {...re(r.records,t),scannedParts:r.scannedParts,fetchedParts:r.fetchedParts}}async function qt(e,t="",r={}){let i=t.trim().toLowerCase(),n=0,s=0;if(Mt(t,r)){let g=r.offset??0,d=r.limit??e.manifest.total,p=Ot(e.manifest,g,d),u=[];for(let h of p){let A=await te(e,h);A.fetched&&(s+=1),n+=1,r.onProgress?.({phase:"part",done:n,total:p.length,href:h.href});let v=Math.max(0,g-h.offset),_=Math.min(A.part.items.length,g+d-h.offset);u.push(...A.part.items.slice(v,_));}return {items:u,total:e.manifest.total,facets:e.manifest.facets??{},manifestTotal:e.manifest.total,scannedParts:n,fetchedParts:s,complete:true}}let o=Et(i,r),a=e.matchCache.get(o);if(a)return e.matchCache.delete(o),e.matchCache.set(o,a),{...$(a,i,r),manifestTotal:e.manifest.total,scannedParts:0,fetchedParts:0,complete:true};let c=[];for(let g of e.manifest.parts){let d=await te(e,g);d.fetched&&(s+=1),n+=1,r.onProgress?.({phase:"part",done:n,total:e.manifest.parts.length,href:g.href}),c.push(...ee(d.part.items,i,r,g.offset)),r.onProgress?.({phase:"query",done:n,total:e.manifest.parts.length,href:g.href});}return Pt(e,o,c),{...$(c,i,r),manifestTotal:e.manifest.total,scannedParts:n,fetchedParts:s,complete:true}}function Bt(e,t,r=new Date().toISOString()){return {schema_version:"aiwg.fortemi.review-decisions.v1",generated_at:r,source_export_schema_version:e.schema_version,decisions:[...t].sort((i,n)=>i.item_id.localeCompare(n.item_id))}}function dr(e){let t=e??null,r=null,i=null,n=null,s=[],o=new Set,a=()=>({index:t,chunked:r?{manifest:r.manifest,cachedParts:r.partCache.size,maxCachedParts:r.maxCachedParts}:null,data:i,error:n,reviewDecisions:[...s]}),c=()=>{let d=a();for(let p of o)p(d);},g=()=>{if(!t)throw new Error("No AIWG index export loaded");return t};return {loadIndex(d){try{let p=W(d);return t=p,r=null,i=null,s=[],n=null,c(),p}catch(p){throw n=p instanceof Error?p:new Error(String(p)),c(),n}},loadChunkedIndex(d,p,u={}){try{let h=Ze(d);return t=null,r={manifest:h,loader:p,maxCachedParts:It(u.maxCachedParts),partCache:new Map,detailLoader:u.detailLoader,maxCachedDetails:Ct(u.maxCachedDetails),detailCache:new Map,maxCachedMatches:Ft(u.maxCachedMatches),matchCache:new Map},i=null,s=[],n=null,c(),h}catch(h){throw n=h instanceof Error?h:new Error(String(h)),c(),n}},getIndex(){return t},getChunkedManifest(){return r?.manifest??null},getSnapshot(){return a()},query(d="",p){let u=Re(g(),d,p);return i=u,n=null,c(),u},async queryChunked(d="",p){if(!r)throw new Error("No AIWG chunked index manifest loaded");try{let u=await qt(r,d,p);return i=u,n=null,c(),u}catch(u){throw n=u instanceof Error?u:new Error(String(u)),c(),n}},async getRecord(d){if(r)try{return await Ie(r,d)}catch(u){throw n=u instanceof Error?u:new Error(String(u)),c(),n}let p=g().items.find(u=>u.id===d);if(!p)throw new Error("Record not found: "+d);return p},async neighbors(d,p){try{let u=Ut(d,p),h=r?await pe(r,u):re(g().items,u);return Nt(d,h,p)}catch(u){throw n=u instanceof Error?u:new Error(String(u)),c(),n}},async relationshipQuery(d){try{return r?await pe(r,d):re(g().items,d)}catch(p){throw n=p instanceof Error?p:new Error(String(p)),c(),n}},async relationshipSet(d){let[p,u]=await Promise.all([this.neighbors(d.a,d),this.neighbors(d.b,d)]),h=new Set(p.nodes.map(_=>_.id).filter(_=>_!==d.a)),A=new Set(u.nodes.map(_=>_.id).filter(_=>_!==d.b)),v;return d.op==="intersection"?v=[...h].filter(_=>A.has(_)):d.op==="difference"?v=[...h].filter(_=>!A.has(_)):v=[...new Set([...h,...A])],{op:d.op,ids:v.sort()}},clearChunkCache(){r?.partCache.clear(),r?.detailCache.clear(),r?.matchCache.clear(),n=null,c();},toCommunityGraph(d){return Z(g(),d)},async toCommunityGraphChunked(d){if(!r)return Z(g(),d);let p=await Ce(r,d?.onProgress);return Z({generated_at:r.manifest.generated_at,source:r.manifest.source,items:p.records},d)},setReviewDecision(d){let p={...d,updated_at:new Date().toISOString()};return s=[...s.filter(u=>u.item_id!==p.item_id),p].sort((u,h)=>u.item_id.localeCompare(h.item_id)),n=null,c(),p},clearReviewDecision(d){s=s.filter(p=>p.item_id!==d),n=null,c();},createReviewDecisionExport(d){let p=t??(r?{schema_version:r.manifest.source_export_schema_version??"aiwg.fortemi.index.export.v1"}:null);if(!p)throw new Error("No AIWG index export or chunked manifest loaded");return Bt(p,s,d)},subscribe(d){return o.add(d),()=>{o.delete(d);}}}}function Z(e,t={}){let r=new Set(e.items.map(o=>o.id)),i=t.relationshipWeights??Object.create(null),n=new Map;for(let o of e.items)for(let a of o.relationships){if(!r.has(a.target_id)&&!t.includeDanglingRelationships)continue;let c=a.type,g=Object.prototype.hasOwnProperty.call(i,c)?i[c]:void 0,d=typeof g=="number"&&Number.isFinite(g)?g:1,p=`${o.id}\0${a.target_id}\0${c}`,u=n.get(p);u?u.weight+=d:n.set(p,{source:o.id,target:a.target_id,kind:c,weight:d});}let s=new Map;for(let o of e.items){let a=Gt(o,t);for(let c of a){let g=s.get(c)??[];g.push(o.id),s.set(c,g);}}return {nodes:e.items.map(o=>({id:o.id})),edges:Array.from(n.values()).sort((o,a)=>o.source.localeCompare(a.source)||o.target.localeCompare(a.target)||o.kind.localeCompare(a.kind)),communities:Array.from(s.entries()).map(([o,a])=>({id:o,nodes:[...new Set(a)].sort()})).sort((o,a)=>o.id.localeCompare(a.id))}}function Gt(e,t){if(t.communityFacet){let r=e.facets[t.communityFacet]??[];if(r.length>0)return r.map(i=>`${t.communityFacet}:${i}`)}if(t.communityTagPrefix){let r=t.communityTagPrefix,i=e.tags.filter(n=>n.startsWith(r));if(i.length>0)return i}return e.concepts.length>0?e.concepts.map(r=>`concept:${r}`):[`type:${e.type}`]}var $t="7ab5d1f8-29d2-5e35-9e2f-3a45de171a9e",B=new TextEncoder,zt=new TextDecoder;function I(e,t){return v5(`${e}:${t}`,$t)}function M(e,t){return !e||Number.isNaN(Date.parse(e))?t:new Date(e).toISOString()}function Wt(e){return e.title??e.search?.title??e.search?.name??e.id}function Vt(e){if(e.text)return e.text;if(e.search?.body)return e.search.body;let t=e.chunks?.map(r=>r.text??r.body??r.summary??"").filter(Boolean);return t?.length?t.join(`
|
|
16
|
+
|
|
17
|
+
`):e.search?.summary??""}function Ht(e,t){return {aiwg_fortemi_index:{envelope:{schema_version:e.schema_version,generated_at:e.generated_at,source:e.source,...e.compatibility?{compatibility:e.compatibility}:{}},record:t}}}function he(e){return B.encode(e.map(t=>JSON.stringify(t)).join(`
|
|
18
|
+
`))}async function ur(e,t={}){let r=z(e);if(!r.valid)throw new Error(`Invalid AIWG Fortemi index export:
|
|
19
|
+
${r.errors.join(`
|
|
20
|
+
`)}`);if(e.schema_version!=="aiwg.fortemi.index.export.v2")throw new Error("Knowledge Shard conversion requires aiwg.fortemi.index.export.v2");let i=M(t.createdAt??e.generated_at,new Date().toISOString()),n=new Map(e.items.map(l=>[l.id,I("record",l.id)])),s=e.items.map(l=>({id:n.get(l.id),title:Wt(l),original_content:Vt(l),revised_content:null,metadata:Ht(e,l),format:"markdown",source:"aiwg-index",starred:false,archived:false,tags:[...new Set(l.tags)].sort(),created_at:M(l.source.updated_at??l.updated_at,i),updated_at:M(l.updated_at,i),deleted_at:null})),o=[];for(let l of e.items)for(let[f,m]of l.relationships.entries()){let b=n.get(m.target_id)??null;o.push({id:I("relationship",`${l.id}\0${f}\0${m.type}\0${m.target_id}`),from_note_id:n.get(l.id),to_note_id:b,to_url:b?null:`aiwg://record/${encodeURIComponent(m.target_id)}`,kind:m.type,score:m.confidence??null,created_at:M(l.updated_at,i),metadata:{aiwg_fortemi_index:{source_record_id:l.id,target_record_id:m.target_id,relationship:m}}});}let a=e.source.graph??e.source.repo,c=new Map;for(let l of e.items){for(let f of l.concepts)c.set(f,{concept:{id:f,prefLabel:f},scheme:a});for(let f of l.skos_concepts??[])c.set(f.id,{concept:f,scheme:f.scheme??a});for(let f of l.skos_relations??[])c.has(f.source_id)||c.set(f.source_id,{concept:{id:f.source_id,prefLabel:f.source_id},scheme:a}),c.has(f.target_id)||c.set(f.target_id,{concept:{id:f.target_id,prefLabel:f.target_id},scheme:a});}let d=[...new Set([...c.values()].map(({scheme:l})=>l))].sort().map(l=>({id:I("skos-scheme",l),title:l,description:`SKOS scheme projected from ${e.source.repo}`,created_at:i,updated_at:i})),p=[...c.entries()].sort(([l],[f])=>l.localeCompare(f)).map(([l,{concept:f,scheme:m}])=>({id:I("skos-concept",l),scheme_id:I("skos-scheme",m),pref_label:f.prefLabel,alt_labels:f.altLabels??[],definition:f.definition??null,created_at:i,updated_at:i})),u=[],h=[],A=[];for(let l of e.items){let f=new Set([...l.concepts,...(l.skos_concepts??[]).map(m=>m.id)]);for(let m of [...f].sort())u.push({id:I("note-skos-tag",`${l.id}\0${m}`),note_id:n.get(l.id),concept_id:I("skos-concept",m),created_at:i});for(let[m,b]of (l.skos_relations??[]).entries()){let P=b.type==="broader"||b.type==="narrower"?b.type:"related";h.push({id:I("skos-relation",`${l.id}\0${m}\0${b.source_id}\0${b.type}\0${b.target_id}`),source_concept_id:I("skos-concept",b.source_id),target_concept_id:I("skos-concept",b.target_id),relation_type:P,created_at:i});}for(let[m,b]of l.provenance.entries())A.push({id:I("provenance",`${l.id}\0field\0${m}`),entity_type:"note",entity_id:n.get(l.id),activity:"source",agent:b.source,started_at:M(l.updated_at,i),ended_at:null,attributes:{aiwg_fortemi_index:{provenance:b}}});for(let[m,b]of (l.provenance_events??[]).entries())A.push({id:I("provenance",b.id??`${l.id}\0event\0${m}`),entity_type:"note",entity_id:n.get(l.id),activity:b.activity,agent:b.agent??"aiwg-index",started_at:M(b.started_at,i),ended_at:b.ended_at?M(b.ended_at,i):null,attributes:{aiwg_fortemi_index:{event:b}}});}let v=new Map,_=["notes","tags"],x={notes:s.length,tags:[...new Set(s.flatMap(l=>l.tags))].length};v.set("notes.jsonl",he(s)),v.set("tags.json",B.encode(JSON.stringify([...new Set(s.flatMap(l=>l.tags))].sort().map(l=>({name:l,created_at:i})))));let k=(l,f,m,b=true)=>{m.length!==0&&(_.push(l),x[l]=m.length,v.set(f,b?he(m):B.encode(JSON.stringify(m))));};k("links","links.jsonl",o),t.includeNativeRichComponents&&(k("skos_schemes","skos_schemes.json",d,false),k("skos_concepts","skos_concepts.json",p,false),k("skos_relations","skos_relations.jsonl",h),k("note_skos_tags","note_skos_tags.jsonl",u),k("provenance_edges","provenance_edges.jsonl",A));let R={};for(let[l,f]of v)R[l]=await ae(f);let S={version:J,matric_version:t.matricVersion??"fortemi-core-aiwg-index",format:oe,created_at:i,components:_,counts:x,checksums:R,min_reader_version:J,migrated_from:null,migration_history:[]};return v.set("manifest.json",B.encode(JSON.stringify(S,null,2))),de(v)}function gr(e){let r=ge(e).get("notes.jsonl"),i=(r?zt.decode(r):"").split(`
|
|
21
|
+
`).filter(Boolean).map(c=>JSON.parse(c));if(i.length===0)throw new Error("Knowledge Shard contains no AIWG index notes");let n,s=[];for(let c of i){let g=c.metadata?.aiwg_fortemi_index;if(!g||typeof g!="object"||Array.isArray(g))throw new Error(`Knowledge Shard note ${c.id} has no AIWG index metadata`);let d=g;n??=d.envelope,s.push(d.record);}let o={...n,items:s.sort((c,g)=>c.id.localeCompare(g.id))},a=z(o);if(!a.valid)throw new Error(`Knowledge Shard contains invalid AIWG index metadata:
|
|
22
|
+
${a.errors.join(`
|
|
23
|
+
`)}`);return o}export{qe as AIWG_SCAN_REQUIRED_FIELDS,Rt as DEFAULT_AIWG_DUPLICATE_SCAN_MAX_EMBEDDINGS,_e as aiwgDetailHrefForId,gr as aiwgFortemiIndexFromKnowledgeShard,Z as aiwgFortemiIndexToCommunityGraph,ur as aiwgFortemiIndexToKnowledgeShard,Ze as assertAiwgFortemiChunkManifest,rt as assertAiwgFortemiChunkPart,W as assertAiwgFortemiIndexExport,ne as assertAiwgStaticEmbeddingSet,or as buildAiwgChunkedIndex,sr as buildAiwgStaticEmbeddingSet,ir as createAiwgFetchChunkLoader,nr as createAiwgFetchDetailLoader,dr as createAiwgIndexController,Bt as createAiwgReviewDecisionExport,nt as encodeAiwgDetailId,we as filterAiwgRecordsByPrivacy,cr as findAiwgStaticDuplicatePairs,ve as getAiwgFortemiFacets,Re as queryAiwgFortemiIndex,ar as queryAiwgHybridIndex,kt as queryAiwgSemanticIndex,xe as resolveAiwgFetchUrl,Ye as validateAiwgFortemiChunkManifest,tt as validateAiwgFortemiChunkPart,z as validateAiwgFortemiIndexExport,vt as validateAiwgStaticEmbeddingSet};//# sourceMappingURL=aiwg-index.js.map
|
|
16
24
|
//# sourceMappingURL=aiwg-index.js.map
|