@fortemi/core 2026.6.9 → 2026.7.1

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/index.js CHANGED
@@ -2083,7 +2083,9 @@ var ManageNoteInputSchema = z.object({
2083
2083
  });
2084
2084
  var SearchInputSchema = z.object({
2085
2085
  query: z.string(),
2086
- mode: z.enum(["text", "semantic", "hybrid"]).default("text"),
2086
+ mode: z.enum(["text", "semantic", "hybrid", "auto"]).default("text"),
2087
+ query_embedding: z.array(z.number()).optional(),
2088
+ embeddingSetId: z.string().optional(),
2087
2089
  limit: z.number().int().min(1).max(100).default(20),
2088
2090
  offset: z.number().int().min(0).default(0),
2089
2091
  tags: z.array(z.string()).optional(),
@@ -4512,15 +4514,17 @@ async function captureKnowledge(db, rawInput, events) {
4512
4514
  // src/tools/search.ts
4513
4515
  async function searchTool(db, rawInput) {
4514
4516
  const input = SearchInputSchema.parse(rawInput);
4515
- if (input.mode !== "text") {
4517
+ const semanticAvailable = !!input.query_embedding?.length;
4518
+ if ((input.mode === "semantic" || input.mode === "hybrid") && !semanticAvailable) {
4516
4519
  throw new Error(
4517
- `Search mode '${input.mode}' is not available. Only 'text' mode is currently supported. semantic_available: false`
4520
+ `Search mode '${input.mode}' requires query_embedding. semantic_available: false`
4518
4521
  );
4519
4522
  }
4520
- const repo = new SearchRepository(db);
4523
+ const repo = new SearchRepository(db, semanticAvailable);
4521
4524
  return repo.search(input.query, {
4522
4525
  limit: input.limit,
4523
4526
  offset: input.offset,
4527
+ mode: input.mode,
4524
4528
  tags: input.tags,
4525
4529
  collection_id: input.collection_id,
4526
4530
  date_from: input.date_from,
@@ -4530,8 +4534,9 @@ async function searchTool(db, rawInput) {
4530
4534
  format: input.format,
4531
4535
  source: input.source,
4532
4536
  visibility: input.visibility,
4533
- include_facets: input.include_facets
4534
- });
4537
+ include_facets: input.include_facets,
4538
+ embeddingSetId: input.embeddingSetId
4539
+ }, input.query_embedding);
4535
4540
  }
4536
4541
  function zodToJsonSchema(schema) {
4537
4542
  if (schema instanceof z.ZodObject) {
@@ -8088,8 +8093,6 @@ var REQUIRED_RECORD_FIELDS = [
8088
8093
  "id",
8089
8094
  "type",
8090
8095
  "source",
8091
- "title",
8092
- "text",
8093
8096
  "facets",
8094
8097
  "tags",
8095
8098
  "concepts",
@@ -8098,19 +8101,14 @@ var REQUIRED_RECORD_FIELDS = [
8098
8101
  "privacy",
8099
8102
  "updated_at"
8100
8103
  ];
8101
- var VALID_TYPES = /* @__PURE__ */ new Set([
8102
- "crm.contact",
8103
- "crm.organization",
8104
- "crm.event",
8105
- "crm.interaction",
8106
- "aiwg.artifact",
8107
- "docs.page"
8108
- ]);
8109
8104
  var DEFAULT_QUERY_WEIGHTS = {
8110
8105
  title: 4,
8111
8106
  tag: 3,
8112
8107
  concept: 2,
8113
- text: 1
8108
+ text: 1,
8109
+ facet: 2,
8110
+ id: 1,
8111
+ source: 0.25
8114
8112
  };
8115
8113
  function hasString(value) {
8116
8114
  return typeof value === "string" && value.length > 0;
@@ -8135,6 +8133,12 @@ function isPlainRecord(value) {
8135
8133
  function isOptionalStringArray(value) {
8136
8134
  return value === void 0 || Array.isArray(value) && value.every((item) => typeof item === "string");
8137
8135
  }
8136
+ function isSupportedIndexSchemaVersion(value) {
8137
+ return value === "aiwg.fortemi.index.export.v1" || value === "aiwg.fortemi.index.export.v2";
8138
+ }
8139
+ function isSupportedRecordSchemaVersion(value) {
8140
+ return value === "aiwg.fortemi.index.record.v1" || value === "aiwg.fortemi.index.record.v2";
8141
+ }
8138
8142
  function validateOptionalRichMetadata(item, index, errors) {
8139
8143
  if (item.skos_concepts !== void 0) {
8140
8144
  if (!Array.isArray(item.skos_concepts)) {
@@ -8181,28 +8185,80 @@ function validateOptionalRichMetadata(item, index, errors) {
8181
8185
  if (relationship.metadata !== void 0 && !isPlainRecord(relationship.metadata)) {
8182
8186
  errors.push("items[" + index + "].relationships[" + relationshipIndex + "].metadata must be an object");
8183
8187
  }
8188
+ if (relationship.direction !== void 0 && relationship.direction !== "upstream" && relationship.direction !== "downstream" && relationship.direction !== "related") {
8189
+ errors.push("items[" + index + "].relationships[" + relationshipIndex + "].direction must be upstream, downstream, or related");
8190
+ }
8191
+ if (relationship.target_path !== void 0 && typeof relationship.target_path !== "string") {
8192
+ errors.push("items[" + index + "].relationships[" + relationshipIndex + "].target_path must be a string");
8193
+ }
8184
8194
  }
8185
8195
  }
8196
+ if (item.search !== void 0) {
8197
+ if (!isPlainRecord(item.search)) {
8198
+ errors.push("items[" + index + "].search must be an object");
8199
+ } else {
8200
+ if (!isOptionalStringArray(item.search.triggers)) errors.push("items[" + index + "].search.triggers must be a string array");
8201
+ if (!isOptionalStringArray(item.search.aliases)) errors.push("items[" + index + "].search.aliases must be a string array");
8202
+ if (!isOptionalStringArray(item.search.tags)) errors.push("items[" + index + "].search.tags must be a string array");
8203
+ if (item.search.frontmatter !== void 0 && !isPlainRecord(item.search.frontmatter)) {
8204
+ errors.push("items[" + index + "].search.frontmatter must be an object");
8205
+ }
8206
+ }
8207
+ }
8208
+ if (item.chunks !== void 0) {
8209
+ if (!Array.isArray(item.chunks)) {
8210
+ errors.push("items[" + index + "].chunks must be an array when present");
8211
+ } else {
8212
+ for (const [chunkIndex, chunk] of item.chunks.entries()) {
8213
+ if (!isPlainRecord(chunk)) errors.push("items[" + index + "].chunks[" + chunkIndex + "] must be an object");
8214
+ if (chunk.metadata !== void 0 && !isPlainRecord(chunk.metadata)) {
8215
+ errors.push("items[" + index + "].chunks[" + chunkIndex + "].metadata must be an object");
8216
+ }
8217
+ }
8218
+ }
8219
+ }
8220
+ if (item.embeddings !== void 0) {
8221
+ if (!Array.isArray(item.embeddings)) {
8222
+ errors.push("items[" + index + "].embeddings must be an array when present");
8223
+ } else {
8224
+ for (const [embeddingIndex, embedding] of item.embeddings.entries()) {
8225
+ if (!isPlainRecord(embedding)) errors.push("items[" + index + "].embeddings[" + embeddingIndex + "] must be an object");
8226
+ const vector2 = embedding.embedding ?? embedding.vector;
8227
+ if (vector2 !== void 0 && (!Array.isArray(vector2) || !vector2.every((entry) => typeof entry === "number"))) {
8228
+ errors.push("items[" + index + "].embeddings[" + embeddingIndex + "].embedding/vector must be a number array");
8229
+ }
8230
+ if (embedding.metadata !== void 0 && !isPlainRecord(embedding.metadata)) {
8231
+ errors.push("items[" + index + "].embeddings[" + embeddingIndex + "].metadata must be an object");
8232
+ }
8233
+ }
8234
+ }
8235
+ }
8236
+ if (item.compatibility !== void 0 && !isPlainRecord(item.compatibility)) {
8237
+ errors.push("items[" + index + "].compatibility must be an object");
8238
+ }
8186
8239
  }
8187
8240
  function validateAiwgFortemiIndexExport(value) {
8188
8241
  const errors = [];
8189
8242
  const counts = {};
8190
8243
  const data = value;
8191
- if (data?.schema_version !== "aiwg.fortemi.index.export.v1") {
8192
- errors.push("schema_version must be aiwg.fortemi.index.export.v1");
8244
+ if (!isSupportedIndexSchemaVersion(data?.schema_version)) {
8245
+ errors.push("schema_version must be aiwg.fortemi.index.export.v1 or aiwg.fortemi.index.export.v2");
8193
8246
  }
8194
8247
  if (!hasString(data?.generated_at)) errors.push("generated_at is required");
8195
8248
  if (!hasString(data?.source?.repo)) errors.push("source.repo is required");
8196
8249
  if (!hasString(data?.source?.privacy)) errors.push("source.privacy is required");
8197
8250
  if (!Array.isArray(data?.items)) errors.push("items must be an array");
8251
+ if (data.compatibility !== void 0 && !isPlainRecord(data.compatibility)) {
8252
+ errors.push("compatibility must be an object");
8253
+ }
8198
8254
  const ids = /* @__PURE__ */ new Set();
8199
8255
  let previousId = "";
8200
8256
  for (const [index, item] of (data.items ?? []).entries()) {
8201
8257
  for (const field of REQUIRED_RECORD_FIELDS) {
8202
8258
  if (!(field in item)) errors.push("items[" + index + "]." + field + " is required");
8203
8259
  }
8204
- if (item.schema_version !== "aiwg.fortemi.index.record.v1") {
8205
- errors.push("items[" + index + "].schema_version must be aiwg.fortemi.index.record.v1");
8260
+ if (!isSupportedRecordSchemaVersion(item.schema_version)) {
8261
+ errors.push("items[" + index + "].schema_version must be aiwg.fortemi.index.record.v1 or aiwg.fortemi.index.record.v2");
8206
8262
  }
8207
8263
  if (!hasString(item.id)) errors.push("items[" + index + "].id is required");
8208
8264
  if (hasString(item.id) && ids.has(item.id)) errors.push("duplicate id: " + item.id);
@@ -8211,11 +8267,17 @@ function validateAiwgFortemiIndexExport(value) {
8211
8267
  errors.push("items must be sorted by id: " + previousId + " before " + item.id);
8212
8268
  }
8213
8269
  if (hasString(item.id)) previousId = item.id;
8214
- if (!VALID_TYPES.has(item.type)) errors.push("items[" + index + "].type is invalid");
8270
+ if (!hasString(item.type)) errors.push("items[" + index + "].type must be a non-empty string");
8215
8271
  else counts[item.type] = (counts[item.type] ?? 0) + 1;
8216
8272
  if (!hasString(item.source?.path)) errors.push("items[" + index + "].source.path is required");
8217
8273
  if (!hasString(item.source?.repo_relative_path)) errors.push("items[" + index + "].source.repo_relative_path is required");
8218
8274
  if (!hasString(item.source?.locator)) errors.push("items[" + index + "].source.locator is required");
8275
+ if (typeof item.title !== "string" && typeof item.search?.title !== "string" && typeof item.search?.name !== "string") {
8276
+ errors.push("items[" + index + "].title or search.title/search.name is required");
8277
+ }
8278
+ if (typeof item.text !== "string" && typeof item.search?.body !== "string" && typeof item.search?.summary !== "string") {
8279
+ errors.push("items[" + index + "].text or search.body/search.summary is required");
8280
+ }
8219
8281
  if (!Array.isArray(item.tags)) errors.push("items[" + index + "].tags must be an array");
8220
8282
  if (!Array.isArray(item.concepts)) errors.push("items[" + index + "].concepts must be an array");
8221
8283
  if (!Array.isArray(item.relationships)) errors.push("items[" + index + "].relationships must be an array");
@@ -8296,8 +8358,8 @@ function validateProjectedRecords(items) {
8296
8358
  const ids = /* @__PURE__ */ new Set();
8297
8359
  let previousId = "";
8298
8360
  for (const [index, item] of items.entries()) {
8299
- if (item.schema_version !== "aiwg.fortemi.index.record.v1") {
8300
- errors.push("items[" + index + "].schema_version must be aiwg.fortemi.index.record.v1");
8361
+ if (!isSupportedRecordSchemaVersion(item.schema_version)) {
8362
+ errors.push("items[" + index + "].schema_version must be aiwg.fortemi.index.record.v1 or aiwg.fortemi.index.record.v2");
8301
8363
  }
8302
8364
  if (!hasString(item.id)) errors.push("items[" + index + "].id is required");
8303
8365
  if (hasString(item.id) && ids.has(item.id)) errors.push("duplicate id: " + item.id);
@@ -8306,9 +8368,13 @@ function validateProjectedRecords(items) {
8306
8368
  errors.push("items must be sorted by id: " + previousId + " before " + item.id);
8307
8369
  }
8308
8370
  if (hasString(item.id)) previousId = item.id;
8309
- if (!item.type || !VALID_TYPES.has(item.type)) errors.push("items[" + index + "].type is invalid");
8310
- if (!hasString(item.title)) errors.push("items[" + index + "].title is required");
8311
- if (typeof item.text !== "string") errors.push("items[" + index + "].text is required");
8371
+ if (!hasString(item.type)) errors.push("items[" + index + "].type must be a non-empty string");
8372
+ if (typeof item.title !== "string" && typeof item.search?.title !== "string" && typeof item.search?.name !== "string") {
8373
+ errors.push("items[" + index + "].title or search.title/search.name is required");
8374
+ }
8375
+ if (typeof item.text !== "string" && typeof item.search?.body !== "string" && typeof item.search?.summary !== "string") {
8376
+ errors.push("items[" + index + "].text or search.body/search.summary is required");
8377
+ }
8312
8378
  if (!item.facets || typeof item.facets !== "object" || Array.isArray(item.facets)) {
8313
8379
  errors.push("items[" + index + "].facets must be an object");
8314
8380
  }
@@ -8400,6 +8466,38 @@ function getAiwgFortemiFacets(items) {
8400
8466
  }
8401
8467
  return result;
8402
8468
  }
8469
+ function recordTitle(item) {
8470
+ return item.title ?? item.search?.title ?? item.search?.name ?? item.id;
8471
+ }
8472
+ function recordText(item) {
8473
+ return item.text ?? item.search?.body ?? item.search?.summary ?? item.chunks?.map((chunk) => chunk.text ?? chunk.body ?? chunk.summary ?? "").filter(Boolean).join("\n") ?? "";
8474
+ }
8475
+ function recordSearchValues(item) {
8476
+ const search = item.search;
8477
+ const values = [
8478
+ item.id,
8479
+ recordTitle(item),
8480
+ recordText(item),
8481
+ search?.title,
8482
+ search?.name,
8483
+ search?.summary,
8484
+ search?.body,
8485
+ search?.capability,
8486
+ search?.phase,
8487
+ search?.type,
8488
+ ...search?.triggers ?? [],
8489
+ ...search?.aliases ?? [],
8490
+ ...search?.tags ?? [],
8491
+ ...(item.chunks ?? []).flatMap((chunk) => [chunk.text, chunk.body, chunk.summary, chunk.source_path])
8492
+ ];
8493
+ if (search?.frontmatter) {
8494
+ for (const value of Object.values(search.frontmatter)) {
8495
+ if (typeof value === "string") values.push(value);
8496
+ else if (Array.isArray(value)) values.push(...value.filter((entry) => typeof entry === "string"));
8497
+ }
8498
+ }
8499
+ return values.filter((value) => typeof value === "string" && value.length > 0);
8500
+ }
8403
8501
  function buildAiwgChunkedIndex(index, options = {}) {
8404
8502
  const partSize = hasPositiveInteger(options.partSize) ? options.partSize : 500;
8405
8503
  const projection = options.projection;
@@ -8461,18 +8559,129 @@ function matchesFacetFilters(item, filters) {
8461
8559
  function queryMatches(item, q) {
8462
8560
  if (!q) return [];
8463
8561
  const matches = [];
8464
- if (item.title.toLowerCase().includes(q)) matches.push({ field: "title", value: item.title });
8465
- if (item.text.toLowerCase().includes(q)) matches.push({ field: "text", value: item.text });
8562
+ const title = recordTitle(item);
8563
+ const text = recordText(item);
8564
+ if (title.toLowerCase().includes(q)) matches.push({ field: "title", value: title });
8565
+ if (text.toLowerCase().includes(q)) matches.push({ field: "text", value: text });
8466
8566
  for (const tag of item.tags) {
8467
8567
  if (tag.toLowerCase().includes(q)) matches.push({ field: "tag", value: tag });
8468
8568
  }
8469
8569
  for (const concept of item.concepts) {
8470
8570
  if (concept.toLowerCase().includes(q)) matches.push({ field: "concept", value: concept });
8471
8571
  }
8572
+ for (const value of recordSearchValues(item)) {
8573
+ if (value !== title && value !== text && value.toLowerCase().includes(q)) {
8574
+ matches.push({ field: "text", value, score: DEFAULT_QUERY_WEIGHTS.text });
8575
+ }
8576
+ }
8577
+ return matches;
8578
+ }
8579
+ var DISCOVERY_STOPWORDS = /* @__PURE__ */ new Set([
8580
+ "a",
8581
+ "an",
8582
+ "and",
8583
+ "are",
8584
+ "as",
8585
+ "for",
8586
+ "from",
8587
+ "how",
8588
+ "i",
8589
+ "in",
8590
+ "is",
8591
+ "me",
8592
+ "of",
8593
+ "on",
8594
+ "or",
8595
+ "please",
8596
+ "the",
8597
+ "to",
8598
+ "use",
8599
+ "with"
8600
+ ]);
8601
+ function normalizeDiscoveryText(value) {
8602
+ return value.toLowerCase().replace(/[_/]+/g, " ").replace(/[^a-z0-9.-]+/g, " ").trim();
8603
+ }
8604
+ function canonicalDiscoveryName(value) {
8605
+ return normalizeDiscoveryText(value).replace(/[\s.-]+/g, "");
8606
+ }
8607
+ function discoveryTokens(value) {
8608
+ return normalizeDiscoveryText(value).split(/\s+/).filter((token) => token.length > 1 && !DISCOVERY_STOPWORDS.has(token));
8609
+ }
8610
+ function facetValues(item, names) {
8611
+ return names.flatMap((name) => item.facets[name] ?? []);
8612
+ }
8613
+ function addDiscoveryMatch(matches, match) {
8614
+ if (!matches.some((existing) => existing.field === match.field && existing.value === match.value && existing.reason === match.reason)) {
8615
+ matches.push(match);
8616
+ }
8617
+ }
8618
+ function tokenOverlapScore(tokens, value) {
8619
+ if (tokens.length === 0 || !value) return 0;
8620
+ const normalized = normalizeDiscoveryText(value);
8621
+ const hits = tokens.filter((token) => normalized.includes(token)).length;
8622
+ return hits / tokens.length;
8623
+ }
8624
+ function discoveryMatches(item, query) {
8625
+ if (!query) return [];
8626
+ const matches = [];
8627
+ const tokens = discoveryTokens(query);
8628
+ const canonicalQuery = canonicalDiscoveryName(query);
8629
+ const idParts = item.id.split(/[:/]/);
8630
+ const names = [
8631
+ item.id,
8632
+ recordTitle(item),
8633
+ item.search?.name,
8634
+ item.search?.title,
8635
+ ...item.search?.aliases ?? [],
8636
+ ...idParts,
8637
+ ...facetValues(item, ["name", "canonical_name", "command", "skill", "agent", "rule"])
8638
+ ].filter((value) => hasString(value));
8639
+ const triggers = [
8640
+ ...facetValues(item, ["trigger", "triggers", "trigger_phrase", "trigger_phrases"]),
8641
+ ...item.search?.triggers ?? []
8642
+ ];
8643
+ const capabilities = [
8644
+ ...facetValues(item, ["capability", "capabilities", "summary", "description"]),
8645
+ item.search?.capability,
8646
+ item.search?.summary,
8647
+ item.search?.phase,
8648
+ item.search?.type,
8649
+ ...item.search?.tags ?? [],
8650
+ ...item.concepts,
8651
+ ...item.tags
8652
+ ].filter((value) => hasString(value));
8653
+ const sourceValues = [item.source?.path, item.source?.repo_relative_path, item.source?.locator].filter((value) => hasString(value));
8654
+ for (const name of names) {
8655
+ const canonicalName = canonicalDiscoveryName(name);
8656
+ if (!canonicalName) continue;
8657
+ if (canonicalName === canonicalQuery) {
8658
+ addDiscoveryMatch(matches, { field: "id", value: name, score: 80, reason: "exact canonical name" });
8659
+ } else if (canonicalName.includes(canonicalQuery) || canonicalQuery.includes(canonicalName)) {
8660
+ addDiscoveryMatch(matches, { field: "id", value: name, score: 48, reason: "near canonical name" });
8661
+ }
8662
+ }
8663
+ const title = recordTitle(item);
8664
+ const titleOverlap = tokenOverlapScore(tokens, title);
8665
+ if (titleOverlap > 0) addDiscoveryMatch(matches, { field: "title", value: title, score: 18 * titleOverlap, reason: "title token overlap" });
8666
+ for (const trigger of triggers) {
8667
+ const overlap = tokenOverlapScore(tokens, trigger);
8668
+ if (overlap > 0) addDiscoveryMatch(matches, { field: "facet", value: trigger, score: 34 * overlap, reason: "trigger phrase" });
8669
+ }
8670
+ for (const capability of capabilities) {
8671
+ const overlap = tokenOverlapScore(tokens, capability);
8672
+ if (overlap > 0) addDiscoveryMatch(matches, { field: "concept", value: capability, score: 22 * overlap, reason: "capability overlap" });
8673
+ }
8674
+ const text = recordText(item);
8675
+ const textOverlap = tokenOverlapScore(tokens, text);
8676
+ if (textOverlap > 0) addDiscoveryMatch(matches, { field: "text", value: text, score: 8 * textOverlap, reason: "body token overlap" });
8677
+ for (const source of sourceValues) {
8678
+ const overlap = tokenOverlapScore(tokens, source);
8679
+ if (overlap > 0) addDiscoveryMatch(matches, { field: "source", value: source, score: 2 * overlap, reason: "path overlap" });
8680
+ }
8472
8681
  return matches;
8473
8682
  }
8474
8683
  function rankMatches(matches, weights) {
8475
- return matches.reduce((total, match) => total + weights[match.field], 0);
8684
+ return matches.reduce((total, match) => total + (match.score ?? weights[match.field]), 0);
8476
8685
  }
8477
8686
  function clipSnippet(value, q, maxLength) {
8478
8687
  const normalizedLength = Math.max(20, maxLength);
@@ -8492,11 +8701,16 @@ function createSnippet(item, matches, q, maxLength) {
8492
8701
  const textMatch = matches.find((match) => match.field === "text");
8493
8702
  const titleMatch = matches.find((match) => match.field === "title");
8494
8703
  const firstMatch = textMatch ?? titleMatch ?? matches[0];
8495
- return clipSnippet(firstMatch?.value ?? item.text, q, maxLength);
8704
+ return clipSnippet(firstMatch?.value ?? recordText(item), q, maxLength);
8496
8705
  }
8497
8706
  function createRankedEntries(items, q, options, ordinalBase = 0) {
8498
8707
  const weights = { ...DEFAULT_QUERY_WEIGHTS, ...options.weights };
8499
- return items.map((item, ordinal) => ({ item, ordinal: ordinalBase + ordinal, matches: queryMatches(item, q) })).filter(({ item, matches }) => {
8708
+ const profile = options.searchProfile ?? "default";
8709
+ return items.map((item, ordinal) => ({
8710
+ item,
8711
+ ordinal: ordinalBase + ordinal,
8712
+ matches: profile === "aiwg-discovery" ? discoveryMatches(item, q) : queryMatches(item, q)
8713
+ })).filter(({ item, matches }) => {
8500
8714
  if (q && matches.length === 0) return false;
8501
8715
  if (options.types && !options.types.includes(item.type)) return false;
8502
8716
  if (options.privacy && !options.privacy.includes(item.privacy.classification)) return false;
@@ -8543,7 +8757,106 @@ function createQueryResultFromRankedEntries(entries, query, options) {
8543
8757
  }
8544
8758
  function queryAiwgFortemiIndex(index, query = "", options = {}) {
8545
8759
  const q = query.trim().toLowerCase();
8546
- return createQueryResultFromRankedEntries(createRankedEntries(index.items, q, options), q, options);
8760
+ const entries = createRankedEntries(index.items, q, options);
8761
+ if (entries.length === 0 && q && options.searchProfile === "aiwg-discovery") {
8762
+ const relaxed = discoveryTokens(q).join(" ");
8763
+ return createQueryResultFromRankedEntries(createRankedEntries(index.items, relaxed, options), relaxed, options);
8764
+ }
8765
+ return createQueryResultFromRankedEntries(entries, q, options);
8766
+ }
8767
+ function cosineSimilarity2(left, right) {
8768
+ if (left.length !== right.length || left.length === 0) return 0;
8769
+ let dot = 0;
8770
+ let leftMag = 0;
8771
+ let rightMag = 0;
8772
+ for (let i = 0; i < left.length; i += 1) {
8773
+ const l = left[i];
8774
+ const r = right[i];
8775
+ dot += l * r;
8776
+ leftMag += l * l;
8777
+ rightMag += r * r;
8778
+ }
8779
+ if (leftMag === 0 || rightMag === 0) return 0;
8780
+ return dot / (Math.sqrt(leftMag) * Math.sqrt(rightMag));
8781
+ }
8782
+ function validateAiwgStaticEmbeddingSet(value) {
8783
+ const errors = [];
8784
+ const data = value;
8785
+ if (data?.schema_version !== "aiwg.fortemi.embedding.set.v1") errors.push("schema_version must be aiwg.fortemi.embedding.set.v1");
8786
+ if (!hasString(data?.id)) errors.push("id is required");
8787
+ if (!hasString(data?.model)) errors.push("model is required");
8788
+ if (!hasPositiveInteger(data?.dimensions)) errors.push("dimensions must be a positive integer");
8789
+ if (!hasString(data?.generated_at)) errors.push("generated_at is required");
8790
+ if (!hasString(data?.granularity)) errors.push("granularity is required");
8791
+ if (!Array.isArray(data?.embeddings)) errors.push("embeddings must be an array");
8792
+ for (const [index, embedding] of (data.embeddings ?? []).entries()) {
8793
+ if (!hasString(embedding.record_id)) errors.push("embeddings[" + index + "].record_id is required");
8794
+ if (!hasString(embedding.input_hash)) errors.push("embeddings[" + index + "].input_hash is required");
8795
+ if (!Array.isArray(embedding.embedding)) errors.push("embeddings[" + index + "].embedding must be an array");
8796
+ else if (hasPositiveInteger(data?.dimensions) && embedding.embedding.length !== data.dimensions) {
8797
+ errors.push("embeddings[" + index + "].embedding length must match dimensions");
8798
+ } else if (!embedding.embedding.every((number) => typeof number === "number" && Number.isFinite(number))) {
8799
+ errors.push("embeddings[" + index + "].embedding must contain finite numbers");
8800
+ }
8801
+ }
8802
+ return { valid: errors.length === 0, errors };
8803
+ }
8804
+ function assertAiwgStaticEmbeddingSet(value) {
8805
+ const result = validateAiwgStaticEmbeddingSet(value);
8806
+ if (!result.valid) throw new Error("Invalid AIWG Fortemi embedding set:\n" + result.errors.join("\n"));
8807
+ return value;
8808
+ }
8809
+ function queryAiwgSemanticIndex(index, embeddingSet, queryEmbedding, options = {}) {
8810
+ assertAiwgStaticEmbeddingSet(embeddingSet);
8811
+ if (queryEmbedding.length !== embeddingSet.dimensions) throw new Error("query embedding length must match embedding set dimensions");
8812
+ const byId = new Map(index.items.map((item) => [item.id, item]));
8813
+ const offset = options.offset ?? 0;
8814
+ const limit = options.limit ?? 20;
8815
+ return embeddingSet.embeddings.map((embedding) => {
8816
+ const item = byId.get(embedding.record_id);
8817
+ if (!item) return null;
8818
+ return { item, embedding, score: cosineSimilarity2(queryEmbedding, embedding.embedding) };
8819
+ }).filter((result) => result !== null && result.score >= (options.minScore ?? -1)).sort((left, right) => right.score - left.score || left.item.id.localeCompare(right.item.id)).slice(offset, offset + limit);
8820
+ }
8821
+ function queryAiwgHybridIndex(index, embeddingSet, query, queryEmbedding, options = {}) {
8822
+ const lexical = queryAiwgFortemiIndex(index, query, { ...options, rank: true });
8823
+ const semantic = queryAiwgSemanticIndex(index, embeddingSet, queryEmbedding, { limit: index.items.length });
8824
+ const lexicalWeight = options.lexicalWeight ?? 0.5;
8825
+ const semanticWeight = options.semanticWeight ?? 0.5;
8826
+ const lexicalScores = new Map(lexical.rankedItems?.map((entry) => [entry.item.id, entry.rank]) ?? []);
8827
+ const maxLexical = Math.max(1, ...lexicalScores.values());
8828
+ const embeddingById = new Map(semantic.flatMap((entry) => entry.embedding ? [[entry.item.id, entry.embedding]] : []));
8829
+ const semanticScores = new Map(semantic.map((entry) => [entry.item.id, entry.score]));
8830
+ const ids = /* @__PURE__ */ new Set([...lexicalScores.keys(), ...semanticScores.keys()]);
8831
+ const offset = options.offset ?? 0;
8832
+ const limit = options.limit ?? 20;
8833
+ return [...ids].map((id) => {
8834
+ const item = index.items.find((candidate) => candidate.id === id);
8835
+ const embedding = embeddingById.get(id);
8836
+ if (!item) return null;
8837
+ return {
8838
+ item,
8839
+ ...embedding ? { embedding } : {},
8840
+ score: (lexicalScores.get(id) ?? 0) / maxLexical * lexicalWeight + (semanticScores.get(id) ?? 0) * semanticWeight
8841
+ };
8842
+ }).filter((result) => result !== null && result.score >= (options.minScore ?? -1)).sort((left, right) => right.score - left.score || left.item.id.localeCompare(right.item.id)).slice(offset, offset + limit);
8843
+ }
8844
+ function findAiwgStaticDuplicatePairs(index, embeddingSet, threshold = 0.9) {
8845
+ assertAiwgStaticEmbeddingSet(embeddingSet);
8846
+ const byId = new Map(index.items.map((item) => [item.id, item]));
8847
+ const pairs = [];
8848
+ for (let leftIndex = 0; leftIndex < embeddingSet.embeddings.length; leftIndex += 1) {
8849
+ for (let rightIndex = leftIndex + 1; rightIndex < embeddingSet.embeddings.length; rightIndex += 1) {
8850
+ const leftEmbedding = embeddingSet.embeddings[leftIndex];
8851
+ const rightEmbedding = embeddingSet.embeddings[rightIndex];
8852
+ const left = byId.get(leftEmbedding.record_id);
8853
+ const right = byId.get(rightEmbedding.record_id);
8854
+ if (!left || !right) continue;
8855
+ const score = cosineSimilarity2(leftEmbedding.embedding, rightEmbedding.embedding);
8856
+ if (score >= threshold) pairs.push({ left, right, score });
8857
+ }
8858
+ }
8859
+ return pairs.sort((left, right) => right.score - left.score || left.left.id.localeCompare(right.left.id));
8547
8860
  }
8548
8861
  function chunkPartCacheKey(part) {
8549
8862
  return `${part.offset}:${part.href}`;
@@ -8642,6 +8955,107 @@ async function getChunkRecord(runtime, id) {
8642
8955
  }
8643
8956
  return record;
8644
8957
  }
8958
+ function relationshipTypeFilter(options) {
8959
+ return options?.relationshipType ?? options?.type;
8960
+ }
8961
+ function edgeFromRelationship(sourceId, relationship) {
8962
+ return {
8963
+ source_id: sourceId,
8964
+ target_id: relationship.target_id,
8965
+ type: relationship.type,
8966
+ ...relationship.source_path ? { source_path: relationship.source_path } : {},
8967
+ ...relationship.target_path ? { target_path: relationship.target_path } : {},
8968
+ ...relationship.direction ? { direction: relationship.direction } : {}
8969
+ };
8970
+ }
8971
+ function relationshipMatches(edge, options = {}) {
8972
+ const type = relationshipTypeFilter(options);
8973
+ const direction = options.direction ?? "both";
8974
+ if (type && edge.type !== type) return false;
8975
+ if (options.relationshipDirection && edge.direction !== options.relationshipDirection) return false;
8976
+ if (options.sourceId && edge.source_id !== options.sourceId) return false;
8977
+ if (options.targetId && edge.target_id !== options.targetId) return false;
8978
+ if (direction === "out" && options.targetId && edge.target_id !== options.targetId) return false;
8979
+ if (direction === "in" && options.sourceId && edge.source_id !== options.sourceId) return false;
8980
+ return true;
8981
+ }
8982
+ function nodeSummary(item) {
8983
+ return { id: item.id, type: item.type, title: recordTitle(item) };
8984
+ }
8985
+ function addNode(nodes, item) {
8986
+ if (item) nodes.set(item.id, nodeSummary(item));
8987
+ }
8988
+ function relationshipResultFromRecords(records, options = {}) {
8989
+ const byId = new Map(records.map((record) => [record.id, record]));
8990
+ const edges = [];
8991
+ for (const record of records) {
8992
+ for (const relationship of record.relationships ?? []) {
8993
+ const edge = edgeFromRelationship(record.id, relationship);
8994
+ if (!relationshipMatches(edge, options)) continue;
8995
+ edges.push(edge);
8996
+ }
8997
+ }
8998
+ const limitedEdges = (options.limit ? edges.slice(0, options.limit) : edges).sort((left, right) => left.source_id.localeCompare(right.source_id) || left.target_id.localeCompare(right.target_id) || left.type.localeCompare(right.type));
8999
+ const nodes = /* @__PURE__ */ new Map();
9000
+ for (const edge of limitedEdges) {
9001
+ addNode(nodes, byId.get(edge.source_id));
9002
+ addNode(nodes, byId.get(edge.target_id));
9003
+ }
9004
+ return {
9005
+ nodes: [...nodes.values()].sort((left, right) => left.id.localeCompare(right.id)),
9006
+ edges: limitedEdges,
9007
+ complete: true
9008
+ };
9009
+ }
9010
+ function neighborQueryOptions(id, options = {}) {
9011
+ const direction = options.direction ?? "both";
9012
+ return {
9013
+ ...options,
9014
+ ...direction === "out" ? { sourceId: id } : {},
9015
+ ...direction === "in" ? { targetId: id } : {}
9016
+ };
9017
+ }
9018
+ function filterNeighborResult(id, result, options = {}) {
9019
+ const direction = options.direction ?? "both";
9020
+ const edges = result.edges.filter((edge) => {
9021
+ if (direction === "out") return edge.source_id === id;
9022
+ if (direction === "in") return edge.target_id === id;
9023
+ return edge.source_id === id || edge.target_id === id;
9024
+ });
9025
+ const ids = /* @__PURE__ */ new Set();
9026
+ for (const edge of edges) {
9027
+ ids.add(edge.source_id);
9028
+ ids.add(edge.target_id);
9029
+ }
9030
+ return {
9031
+ ...result,
9032
+ edges,
9033
+ nodes: result.nodes.filter((node) => ids.has(node.id))
9034
+ };
9035
+ }
9036
+ async function recordsFromChunkedRuntime(runtime, onProgress) {
9037
+ let scannedParts = 0;
9038
+ let fetchedParts = 0;
9039
+ const records = [];
9040
+ for (const partRef of runtime.manifest.parts) {
9041
+ const loaded = await loadChunkPart(runtime, partRef);
9042
+ if (loaded.fetched) fetchedParts += 1;
9043
+ scannedParts += 1;
9044
+ onProgress?.({ phase: "part", done: scannedParts, total: runtime.manifest.parts.length, href: partRef.href });
9045
+ for (const item of loaded.part.items) {
9046
+ records.push(item.relationships ? item : await getChunkRecord(runtime, item.id));
9047
+ }
9048
+ }
9049
+ return { records, scannedParts, fetchedParts };
9050
+ }
9051
+ async function relationshipResultFromChunkedRuntime(runtime, options = {}) {
9052
+ const loaded = await recordsFromChunkedRuntime(runtime);
9053
+ return {
9054
+ ...relationshipResultFromRecords(loaded.records, options),
9055
+ scannedParts: loaded.scannedParts,
9056
+ fetchedParts: loaded.fetchedParts
9057
+ };
9058
+ }
8645
9059
  async function queryChunkedAiwgFortemiIndex(runtime, query = "", options = {}) {
8646
9060
  const q = query.trim().toLowerCase();
8647
9061
  let scannedParts = 0;
@@ -8822,6 +9236,39 @@ function createAiwgIndexController(initialIndex) {
8822
9236
  if (!found) throw new Error("Record not found: " + id);
8823
9237
  return found;
8824
9238
  },
9239
+ async neighbors(id, options) {
9240
+ try {
9241
+ const queryOptions = neighborQueryOptions(id, options);
9242
+ const result = chunked ? await relationshipResultFromChunkedRuntime(chunked, queryOptions) : relationshipResultFromRecords(requireIndex().items, queryOptions);
9243
+ return filterNeighborResult(id, result, options);
9244
+ } catch (err) {
9245
+ error = err instanceof Error ? err : new Error(String(err));
9246
+ notify();
9247
+ throw error;
9248
+ }
9249
+ },
9250
+ async relationshipQuery(options) {
9251
+ try {
9252
+ return chunked ? await relationshipResultFromChunkedRuntime(chunked, options) : relationshipResultFromRecords(requireIndex().items, options);
9253
+ } catch (err) {
9254
+ error = err instanceof Error ? err : new Error(String(err));
9255
+ notify();
9256
+ throw error;
9257
+ }
9258
+ },
9259
+ async relationshipSet(options) {
9260
+ const [left, right] = await Promise.all([
9261
+ this.neighbors(options.a, options),
9262
+ this.neighbors(options.b, options)
9263
+ ]);
9264
+ const leftIds = new Set(left.nodes.map((node) => node.id).filter((id) => id !== options.a));
9265
+ const rightIds = new Set(right.nodes.map((node) => node.id).filter((id) => id !== options.b));
9266
+ let ids;
9267
+ if (options.op === "intersection") ids = [...leftIds].filter((id) => rightIds.has(id));
9268
+ else if (options.op === "difference") ids = [...leftIds].filter((id) => !rightIds.has(id));
9269
+ else ids = [.../* @__PURE__ */ new Set([...leftIds, ...rightIds])];
9270
+ return { op: options.op, ids: ids.sort() };
9271
+ },
8825
9272
  clearChunkCache() {
8826
9273
  chunked?.partCache.clear();
8827
9274
  chunked?.detailCache.clear();
@@ -8832,6 +9279,15 @@ function createAiwgIndexController(initialIndex) {
8832
9279
  toCommunityGraph(options) {
8833
9280
  return aiwgFortemiIndexToCommunityGraph(requireIndex(), options);
8834
9281
  },
9282
+ async toCommunityGraphChunked(options) {
9283
+ if (!chunked) return aiwgFortemiIndexToCommunityGraph(requireIndex(), options);
9284
+ const loaded = await recordsFromChunkedRuntime(chunked, options?.onProgress);
9285
+ return aiwgFortemiIndexToCommunityGraph({
9286
+ generated_at: chunked.manifest.generated_at,
9287
+ source: chunked.manifest.source,
9288
+ items: loaded.records
9289
+ }, options);
9290
+ },
8835
9291
  setReviewDecision(input) {
8836
9292
  const decision = {
8837
9293
  ...input,
@@ -8908,8 +9364,8 @@ function communityIdsFor(item, options) {
8908
9364
  }
8909
9365
 
8910
9366
  // src/index.ts
8911
- var VERSION = "2026.6.8";
9367
+ var VERSION = "2026.7.1";
8912
9368
 
8913
- export { AIWG_SCAN_REQUIRED_FIELDS, ArchiveManager, AttachmentsRepository, CURRENT_MIGRATION_HEAD, CURRENT_SHARD_VERSION, CapabilityManager, CaptureKnowledgeInputSchema, CollectionsRepository, CommunitiesRepository, DB_SNAPSHOT_SCHEMA_VERSION, DbSnapshotVersionError, EMBED_REQUEST_KIND, EMBED_RESPONSE_KIND, EmbeddingSetsRepository, FallbackRouter, FortemiToolManifest, GetNoteInputSchema, GraphRepository, JOB_CAPABILITIES, JOB_PRIORITIES, JobQueueWorker, LOCAL_ENDPOINTS, LinksRepository, ListNotesInputSchema, ManageArchiveInputSchema, ManageAttachmentsInputSchema, ManageCapabilitiesInputSchema, ManageCollectionsInputSchema, ManageLinksInputSchema, ManageNoteInputSchema, ManageTagsInputSchema, MemoryBlobStore, MigrationRunner, NotesRepository, OpenAICompatibleProvider, PGliteStorageBackend, PGliteStorageBackendFactory, PGliteWorkerClient, PGliteWorkerStorageBackend, PGliteWorkerStorageBackendFactory, ProvenanceRepository, ProviderRegistry, SHARD_FORMAT, SUPPORTED_PGLITE_VERSION, SearchInputSchema, SearchRepository, SkosRepository, TagsRepository, TransactionProxy, TypedEventBus, VERSION, aiRevisionHandler, aiwgFortemiIndexToCommunityGraph, allMigrations, appendPluginScript, assertAiwgFortemiChunkManifest, assertAiwgFortemiChunkPart, assertAiwgFortemiIndexExport, buildAiwgChunkedIndex, buildNoteConditions, buildPluginCsp, captureKnowledge, chunkText, classifyError, classifyModel, clearPrefetchedShard, collectionFromShard, collectionToShard, computeHash, computeSri, conceptTaggingHandler, cosineSimilarity, createAiwgFetchChunkLoader, createAiwgFetchDetailLoader, createAiwgIndexController, createAiwgReviewDecisionExport, createBlobStore, createCosineSemanticProvider, createCspReportHandler, createFortemi, createLegacyProvider, createPGliteBackend, createPGliteInstance, createRemoteBackend, createRoutes, createShardBackend, createWorkerEmbedFunction, defaultStorageBackendFactory, detectCommunities, detectGpuCapabilities, detectInferenceCapabilities, discoverLocalProviders, dumpDbSnapshot, embeddingFromShard, embeddingGenerationHandler, embeddingSetFromShard, embeddingSetMemberToShard, embeddingSetToShard, embeddingToShard, enqueueFullWorkflow, enqueueJob, enqueueNoteCreationJobs, estimateModelFit, estimateVramMB, estimateVramTier, exportShard, fetchPluginScript, fortemiManifest, fromPrefetched, generateId, getAiwgFortemiFacets, getEmbedFunction, getFortemiBridge, getFortemiSecretStore, getJobQueueStatus, getLlmFunction, getNote, getPrefetchedSha256, handleEmbedRequests, hasFortemiSecureSecrets, importShard, isPluginScriptAllowed, isShardPrefetched, linkFromShard, linkToShard, linkingHandler, listNotes, manageArchive, manageAttachments, manageCapabilities, manageCollections, manageLinks, manageNote, manageTags, matchRoute, noteFromShard, noteSkosTagToShard, noteToShard, openShard, packTarGz, parseCspReport, prefetchShard, provenanceEdgeToShard, queryAiwgFortemiIndex, registerLlmCapability, registerSemanticCapability, registerSemanticCapabilityWorker, registerServiceWorker, restoreDbSnapshot, searchTool, selectBackend, selectLlmModel, setEmbedFunction, setLlmFunction, sha256Hex, skosConceptToShard, skosRelationToShard, skosSchemeToShard, suggestTags, tagsFromShard, tagsToShard, titleGenerationHandler, unpackTarGz, unregisterLlmCapability, unregisterSemanticCapability, validateAiwgFortemiChunkManifest, validateAiwgFortemiChunkPart, validateAiwgFortemiIndexExport, validateChecksums, verifyDbSnapshotMeta, verifySri };
9369
+ export { AIWG_SCAN_REQUIRED_FIELDS, ArchiveManager, AttachmentsRepository, CURRENT_MIGRATION_HEAD, CURRENT_SHARD_VERSION, CapabilityManager, CaptureKnowledgeInputSchema, CollectionsRepository, CommunitiesRepository, DB_SNAPSHOT_SCHEMA_VERSION, DbSnapshotVersionError, EMBED_REQUEST_KIND, EMBED_RESPONSE_KIND, EmbeddingSetsRepository, FallbackRouter, FortemiToolManifest, GetNoteInputSchema, GraphRepository, JOB_CAPABILITIES, JOB_PRIORITIES, JobQueueWorker, LOCAL_ENDPOINTS, LinksRepository, ListNotesInputSchema, ManageArchiveInputSchema, ManageAttachmentsInputSchema, ManageCapabilitiesInputSchema, ManageCollectionsInputSchema, ManageLinksInputSchema, ManageNoteInputSchema, ManageTagsInputSchema, MemoryBlobStore, MigrationRunner, NotesRepository, OpenAICompatibleProvider, PGliteStorageBackend, PGliteStorageBackendFactory, PGliteWorkerClient, PGliteWorkerStorageBackend, PGliteWorkerStorageBackendFactory, ProvenanceRepository, ProviderRegistry, SHARD_FORMAT, SUPPORTED_PGLITE_VERSION, SearchInputSchema, SearchRepository, SkosRepository, TagsRepository, TransactionProxy, TypedEventBus, VERSION, aiRevisionHandler, aiwgFortemiIndexToCommunityGraph, allMigrations, appendPluginScript, assertAiwgFortemiChunkManifest, assertAiwgFortemiChunkPart, assertAiwgFortemiIndexExport, assertAiwgStaticEmbeddingSet, buildAiwgChunkedIndex, buildNoteConditions, buildPluginCsp, captureKnowledge, chunkText, classifyError, classifyModel, clearPrefetchedShard, collectionFromShard, collectionToShard, computeHash, computeSri, conceptTaggingHandler, cosineSimilarity, createAiwgFetchChunkLoader, createAiwgFetchDetailLoader, createAiwgIndexController, createAiwgReviewDecisionExport, createBlobStore, createCosineSemanticProvider, createCspReportHandler, createFortemi, createLegacyProvider, createPGliteBackend, createPGliteInstance, createRemoteBackend, createRoutes, createShardBackend, createWorkerEmbedFunction, defaultStorageBackendFactory, detectCommunities, detectGpuCapabilities, detectInferenceCapabilities, discoverLocalProviders, dumpDbSnapshot, embeddingFromShard, embeddingGenerationHandler, embeddingSetFromShard, embeddingSetMemberToShard, embeddingSetToShard, embeddingToShard, enqueueFullWorkflow, enqueueJob, enqueueNoteCreationJobs, estimateModelFit, estimateVramMB, estimateVramTier, exportShard, fetchPluginScript, findAiwgStaticDuplicatePairs, fortemiManifest, fromPrefetched, generateId, getAiwgFortemiFacets, getEmbedFunction, getFortemiBridge, getFortemiSecretStore, getJobQueueStatus, getLlmFunction, getNote, getPrefetchedSha256, handleEmbedRequests, hasFortemiSecureSecrets, importShard, isPluginScriptAllowed, isShardPrefetched, linkFromShard, linkToShard, linkingHandler, listNotes, manageArchive, manageAttachments, manageCapabilities, manageCollections, manageLinks, manageNote, manageTags, matchRoute, noteFromShard, noteSkosTagToShard, noteToShard, openShard, packTarGz, parseCspReport, prefetchShard, provenanceEdgeToShard, queryAiwgFortemiIndex, queryAiwgHybridIndex, queryAiwgSemanticIndex, registerLlmCapability, registerSemanticCapability, registerSemanticCapabilityWorker, registerServiceWorker, restoreDbSnapshot, searchTool, selectBackend, selectLlmModel, setEmbedFunction, setLlmFunction, sha256Hex, skosConceptToShard, skosRelationToShard, skosSchemeToShard, suggestTags, tagsFromShard, tagsToShard, titleGenerationHandler, unpackTarGz, unregisterLlmCapability, unregisterSemanticCapability, validateAiwgFortemiChunkManifest, validateAiwgFortemiChunkPart, validateAiwgFortemiIndexExport, validateAiwgStaticEmbeddingSet, validateChecksums, verifyDbSnapshotMeta, verifySri };
8914
9370
  //# sourceMappingURL=index.js.map
8915
9371
  //# sourceMappingURL=index.js.map