This repository has been archived on 2026-09-03. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
open-brainy/src/neural/embeddedTypeEmbeddings.ts

118 lines
343 KiB
TypeScript
Raw Normal View History

/**
* 🧠 BRAINY EMBEDDED TYPE EMBEDDINGS
*
* AUTO-GENERATED - DO NOT EDIT
feat(contract): declare contract 1, serve three operators, refuse four by name Open Brainy's side of the API contract the accelerated engine published. DECLARED: package.json carries "brainyContract": 1 and the engine states its own via contractVersion() / BRAINY_CONTRACT_VERSION — two engines compare an integer instead of probing prototypes, and a tool reads the package field without importing the engine. Pinned so the two can never drift apart. SERVED: hasAll, noneOf and excludes now work on the index path. The defect underneath was worse than the reported divergence — the metadata index's operator switch had NO DEFAULT CASE, so any operator without a case left the field's match set at its initial [] and find() returned an empty page. Documented, validator-accepted, matcher-implemented operators answering silently wrong. hasAll intersects each element's posting set (an empty operand is vacuously true of every row that has the field), noneOf complements their union, excludes complements contains. REFUSED BY NAME: startsWith, endsWith, matches and length raise INVALID_QUERY naming the operator, the field and the reason. An equality/range posting index cannot evaluate a substring, a pattern or an array length without reading every row — which is the cost this path exists to avoid — so it refuses rather than answering an empty page. Both engines now agree on all 25 tokens and contract 1 has no remaining operator divergence. This is a visible change for a consumer calling those four through find({ where }): an empty page becomes a typed refusal. EMITTED: scripts/emit-contract-manifest.mjs generates docs/api-contract.json from the BUILT surface — prototype doors, exported error classes, the operator sets read out of their single definitions, the field-addressing vocabulary, the health verdicts. Nothing hand-maintained, so a diff between two manifests is a diff between two engines. `--check` fails on a stale manifest, which makes the announce-every-addition duty mechanical rather than remembered. RATIFIED in docs/contract-1-ratification.md: the 41-of-57 required split with the promise spelled out (a refusal is part of a door; deprecation is not removal), the serving-withholding list confirmed exhaustive and identical, the minor/major rule adopted with the announcement duty, the 30 storage seam methods committed as supported surface until Stage 2, and a finding filed against the spec — is / isNot / greaterEqual / lessEqual are listed there as served aliases and have never existed in this engine, which throws INVALID_QUERY on all four.
2026-08-28 10:57:43 -07:00
* Generated: 2026-08-27T09:18:45-07:00
* Noun Types: 42
* Verb Types: 127
*
* This file contains pre-computed embeddings for all NounTypes and VerbTypes.
* No runtime computation needed, instant availability!
*/
import { NounType, VerbType } from '../types/graphTypes.js'
import { Vector } from '../coreTypes.js'
// Type metadata
export const TYPE_METADATA = {
nounTypes: 42,
verbTypes: 127,
totalTypes: 169,
embeddingDimensions: 384,
feat(contract): declare contract 1, serve three operators, refuse four by name Open Brainy's side of the API contract the accelerated engine published. DECLARED: package.json carries "brainyContract": 1 and the engine states its own via contractVersion() / BRAINY_CONTRACT_VERSION — two engines compare an integer instead of probing prototypes, and a tool reads the package field without importing the engine. Pinned so the two can never drift apart. SERVED: hasAll, noneOf and excludes now work on the index path. The defect underneath was worse than the reported divergence — the metadata index's operator switch had NO DEFAULT CASE, so any operator without a case left the field's match set at its initial [] and find() returned an empty page. Documented, validator-accepted, matcher-implemented operators answering silently wrong. hasAll intersects each element's posting set (an empty operand is vacuously true of every row that has the field), noneOf complements their union, excludes complements contains. REFUSED BY NAME: startsWith, endsWith, matches and length raise INVALID_QUERY naming the operator, the field and the reason. An equality/range posting index cannot evaluate a substring, a pattern or an array length without reading every row — which is the cost this path exists to avoid — so it refuses rather than answering an empty page. Both engines now agree on all 25 tokens and contract 1 has no remaining operator divergence. This is a visible change for a consumer calling those four through find({ where }): an empty page becomes a typed refusal. EMITTED: scripts/emit-contract-manifest.mjs generates docs/api-contract.json from the BUILT surface — prototype doors, exported error classes, the operator sets read out of their single definitions, the field-addressing vocabulary, the health verdicts. Nothing hand-maintained, so a diff between two manifests is a diff between two engines. `--check` fails on a stale manifest, which makes the announce-every-addition duty mechanical rather than remembered. RATIFIED in docs/contract-1-ratification.md: the 41-of-57 required split with the promise spelled out (a refusal is part of a door; deprecation is not removal), the serving-withholding list confirmed exhaustive and identical, the minor/major rule adopted with the announcement duty, the 30 storage seam methods committed as supported surface until Stage 2, and a finding filed against the spec — is / isNot / greaterEqual / lessEqual are listed there as served aliases and have never existed in this engine, which throws INVALID_QUERY on all four.
2026-08-28 10:57:43 -07:00
generatedAt: "2026-08-27T09:18:45-07:00",
sizeBytes: {
embeddings: 259584,
base64: 346112
}
}
// All noun types in order
const NOUN_TYPE_ORDER: NounType[] = ["person","organization","location","thing","concept","event","agent","organism","substance","quality","timeInterval","function","proposition","document","media","file","message","collection","dataset","product","service","task","project","process","state","role","language","currency","measurement","hypothesis","experiment","contract","regulation","interface","resource","custom","socialGroup","institution","norm","informationContent","informationBearer","relationship"]
// All verb types in order
const VERB_TYPE_ORDER: VerbType[] = ["instanceOf","subclassOf","participatesIn","relatedTo","contains","partOf","references","locatedAt","adjacentTo","precedes","during","occursAt","causes","enables","prevents","dependsOn","requires","creates","transforms","becomes","modifies","consumes","destroys","owns","attributedTo","hasQuality","realizes","affects","composedOf","inherits","memberOf","worksWith","friendOf","follows","likes","reportsTo","mentors","communicates","describes","defines","categorizes","measures","evaluates","uses","implements","extends","equivalentTo","believes","conflicts","synchronizes","competes","canCause","mustCause","wouldCauseIf","couldBe","mustBe","counterfactual","knows","doubts","desires","intends","fears","loves","hates","hopes","perceives","learns","probablyCauses","uncertainRelation","correlatesWith","approximatelyEquals","greaterThan","similarityDegree","moreXThan","hasDegree","partiallyHas","carries","encodes","obligatedTo","permittedTo","prohibitedFrom","shouldDo","mustNotDo","trueInContext","perceivedAs","interpretedAs","validInFrame","trueFrom","overlaps","immediatelyAfter","eventuallyLeadsTo","simultaneousWith","hasDuration","recurringWith","containsSpatially","overlapsSpatially","surrounds","connectedTo","above","below","inside","outside","facing","represents","embodies","opposes","alliesWith","conformsTo","measuredIn","convertsTo","hasMagnitude","dimensionallyEquals","persistsThrough","gainsProperty","losesProperty","remainsSame","functionalPartOf","topologicalPartOf","temporalPartOf","conceptualPartOf","rigidlyDependsOn","functionallyDependsOn","historicallyDependsOn","endorses","contradicts","supports","supersedes"]
// Pre-computed embeddings (338.0KB base64)
const EMBEDDINGS_BASE64 = "WepJvWwRE72lxOO8M+9Y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aU94zjEPJRr77y5Iq29kPJdPMFyhTw3oNW8WRI2PYQoRDxEnzC87KdgvaRsPL024I09NMlnPaL7Bz0gKw29wJ1FvNTEzD1FeHe93bFHPSQyIDyzCOm7xStVvNbbg71LO5Q9XUSTvC8m8ryYrB69eGZnvXbgYzwczzu9HlsMPVnsWD2lHJy9nDRfPaHy5zzrYLu8Mj+JPRVDhzx3YUq9GD9Ru/7E3b3FUaK9TyR+utfQyDtXj6U92hf/PW2Mgj03nkC9UpAcPQsykLu3GS49Zn0OPM0j4DzCaWQ9UqlpPfiIZD1ZjIS86I48PXU10Ttjmd87y2cLPU0ZZ71x7Xm9M/4XPfGu1LzhXAc9Ovz9vIk9GL3SMYG9B+VQvPV6y7xvUTe8hZwcvTnFsz1kwAK9BWEXPYmmkT2dxo270VZMvE3rdj3ToAK9hVHZvRoUtj3PaPk80bvLvCNsyrKwl728AiqEPHH55zzqM7G8OWJdPeJ3LL7+Fnm8fKZFvbnzibyHWNu8+NaGPVOxwTq2sZ+9I1PTOyPghDzBQe48mKudvRhT0ztRQNi8NnSmujozDryTO6m9MnbDvHWGBDzbWoi8DOgrva6ZD70PLT68P9bXO+TCPDvCWeQ8Y0kwPVM/t7u5Goe8OwYtPbJAfr0WPBq9v/kCPJOQyjuOH/q8OAaHves4Sr30V5a79snhPMTp8Tzsk9s7iUgBvceZxztN7zi9qZiwvUZ86juL1048fNmgvW42Yz2VqEm9yEQyvYY5mjzIh387reVw
// Decode embeddings at startup (happens once, <10ms)
function decodeEmbeddings(): Uint8Array {
if (typeof Buffer !== 'undefined') {
// Node.js environment
return Buffer.from(EMBEDDINGS_BASE64, 'base64')
} else if (typeof atob !== 'undefined') {
// Browser environment
const binaryString = atob(EMBEDDINGS_BASE64)
const bytes = new Uint8Array(binaryString.length)
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i)
}
return bytes
}
return new Uint8Array(0)
}
// Cached decoded embeddings
let decodedEmbeddings: Uint8Array | null = null
/**
* Get noun type embeddings as a Map for fast lookup
* This is called once and cached
*/
export function getNounTypeEmbeddings(): Map<NounType, Vector> {
if (!decodedEmbeddings) {
decodedEmbeddings = decodeEmbeddings()
}
const embeddings = new Map<NounType, Vector>()
const view = new DataView(decodedEmbeddings.buffer)
const embeddingSize = 384
NOUN_TYPE_ORDER.forEach((type, index) => {
const offset = index * embeddingSize * 4
const embedding = new Float32Array(embeddingSize)
for (let i = 0; i < embeddingSize; i++) {
embedding[i] = view.getFloat32(offset + i * 4, true)
}
embeddings.set(type, Array.from(embedding))
})
return embeddings
}
/**
* Get verb type embeddings as a Map for fast lookup
* This is called once and cached
*/
export function getVerbTypeEmbeddings(): Map<VerbType, Vector> {
if (!decodedEmbeddings) {
decodedEmbeddings = decodeEmbeddings()
}
const embeddings = new Map<VerbType, Vector>()
const view = new DataView(decodedEmbeddings.buffer)
const embeddingSize = 384
// Verb embeddings start after noun embeddings
const verbStartOffset = 42 * embeddingSize * 4
VERB_TYPE_ORDER.forEach((type, index) => {
const offset = verbStartOffset + index * embeddingSize * 4
const embedding = new Float32Array(embeddingSize)
for (let i = 0; i < embeddingSize; i++) {
embedding[i] = view.getFloat32(offset + i * 4, true)
}
embeddings.set(type, Array.from(embedding))
})
return embeddings
}
// Import logging
import { prodLog } from '../utils/logger.js'
prodLog.info(`🧠 Brainy Type Embeddings loaded: ${TYPE_METADATA.nounTypes} nouns, ${TYPE_METADATA.verbTypes} verbs, ${(TYPE_METADATA.sizeBytes.embeddings / 1024).toFixed(1)}KB`)