dsh-fundus 0.1.0 → 0.1.2

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/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/shared/types.ts
2
2
  var PLUGIN = "dsh-fundus";
3
- var VERSION = "0.1.0";
3
+ var VERSION = "0.1.2";
4
4
 
5
5
  // src/core/config.ts
6
6
  import { homedir } from "node:os";
@@ -582,6 +582,7 @@ function verifyQuote(input, lookup) {
582
582
  }
583
583
 
584
584
  // src/core/search.ts
585
+ var MIN_SUBSTRING_TOKEN_LEN = 4;
585
586
  function sanitizeFts5Query(raw) {
586
587
  const tokens = [];
587
588
  const re = /[\p{L}\p{N}]{2,}|[0-9]/gu;
@@ -603,6 +604,15 @@ function isBrowseQuery(query) {
603
604
  const q = query.trim();
604
605
  return q === "" || q === "*";
605
606
  }
607
+ function substringFallbackTokens(tokens) {
608
+ return tokens.filter((t) => t.length >= MIN_SUBSTRING_TOKEN_LEN);
609
+ }
610
+ function escapeLikePattern(raw) {
611
+ return raw.replace(/[\\%_]/g, (ch) => `\\${ch}`);
612
+ }
613
+ function likeContainsPattern(token) {
614
+ return `%${escapeLikePattern(token.toLowerCase())}%`;
615
+ }
606
616
 
607
617
  // src/core/vault.ts
608
618
  var ID_RE = /^(?:pas|src)-[0-9a-f]{12}$/;
@@ -864,7 +874,18 @@ CREATE VIRTUAL TABLE IF NOT EXISTS fundus_fts USING fts5(
864
874
  if (!sheet) continue;
865
875
  for (const block of sheet.blocks) {
866
876
  blockSheetMap.set(block.id, sheetId);
867
- if (!block.provenance) continue;
877
+ if (!block.provenance) {
878
+ if (block.type === "source_text") {
879
+ warnings.push({
880
+ code: "FUNDUS_UNSOURCED_QUOTE_BLOCK",
881
+ severity: "warn",
882
+ sheetId,
883
+ blockId: block.id,
884
+ message: `Block ${block.id} (source_text) has no provenance metadata attached.`
885
+ });
886
+ }
887
+ continue;
888
+ }
868
889
  const text = galleyInlinesToText(block.body);
869
890
  const res = this.verifyQuote({ text, provenance: block.provenance });
870
891
  if (!res.ok) {
@@ -875,6 +896,14 @@ CREATE VIRTUAL TABLE IF NOT EXISTS fundus_fts USING fts5(
875
896
  blockId: block.id,
876
897
  message: `Block ${block.id} provenance ${block.provenance.sourceId}: ${res.message ?? res.code}`
877
898
  });
899
+ } else if (res.code === "NON_FUNDUS_ID") {
900
+ warnings.push({
901
+ code: "FUNDUS_UNVERIFIED_EXTERNAL_ID",
902
+ severity: "warn",
903
+ sheetId,
904
+ blockId: block.id,
905
+ message: `Block ${block.id} provenance "${res.sourceId}" is an unverified external reference (not a Fundus pas-*/src-* ID).`
906
+ });
878
907
  }
879
908
  }
880
909
  }
@@ -891,6 +920,14 @@ CREATE VIRTUAL TABLE IF NOT EXISTS fundus_fts USING fts5(
891
920
  blockId: ann.blockId,
892
921
  message: `Annotation ${ann.id} (block ${ann.blockId}) provenance ${ann.provenance.sourceId}: ${res.message ?? res.code}`
893
922
  });
923
+ } else if (res.code === "NON_FUNDUS_ID") {
924
+ warnings.push({
925
+ code: "FUNDUS_UNVERIFIED_EXTERNAL_ID",
926
+ severity: "warn",
927
+ sheetId: blockSheetMap.get(ann.blockId),
928
+ blockId: ann.blockId,
929
+ message: `Annotation ${ann.id} (block ${ann.blockId}) provenance "${res.sourceId}" is an unverified external reference (not a Fundus pas-*/src-* ID).`
930
+ });
894
931
  }
895
932
  }
896
933
  }
@@ -1533,6 +1570,37 @@ CREATE VIRTUAL TABLE IF NOT EXISTS fundus_fts USING fts5(
1533
1570
  if (rows2.length === 0 && tokens2.length > 1) {
1534
1571
  rows2 = runSourceMatch(orExpr2);
1535
1572
  }
1573
+ if (rows2.length < limit) {
1574
+ const subToks = substringFallbackTokens(tokens2);
1575
+ if (subToks.length) {
1576
+ const seen = new Set(rows2.map((r) => r.source_id));
1577
+ const likePatterns = subToks.map(likeContainsPattern);
1578
+ const likeClauses = likePatterns.map(
1579
+ () => `(lower(s.title) LIKE ? ESCAPE '\\' OR EXISTS (SELECT 1 FROM passages p2 WHERE p2.source_id = s.source_id AND lower(p2.body) LIKE ? ESCAPE '\\'))`
1580
+ ).join(" OR ");
1581
+ const likeParams = [];
1582
+ for (const pat of likePatterns) {
1583
+ likeParams.push(pat, pat);
1584
+ }
1585
+ const sql = `
1586
+ SELECT s.source_id AS entry_id, 'source' AS entry_kind, s.source_id, s.title, s.citation,
1587
+ s.license, s.tags_json, s.passage_count,
1588
+ (SELECT substr(p.body, 1, 240) FROM passages p WHERE p.source_id = s.source_id ORDER BY p.ordinal ASC LIMIT 1) AS snippet
1589
+ FROM sources s
1590
+ WHERE 1=1 ${filterSql}
1591
+ AND (${likeClauses})
1592
+ ORDER BY s.source_id ASC
1593
+ LIMIT ?
1594
+ `;
1595
+ const extra = this.db.prepare(sql).all(...params, ...likeParams, limit * 4);
1596
+ for (const r of extra) {
1597
+ if (seen.has(r.source_id)) continue;
1598
+ seen.add(r.source_id);
1599
+ rows2.push(r);
1600
+ if (rows2.length >= limit) break;
1601
+ }
1602
+ }
1603
+ }
1536
1604
  return { ok: true, query: input.query, totalMatches: rows2.length, hits: mapHits(rows2) };
1537
1605
  }
1538
1606
  if (isBrowseQuery(input.query)) {
@@ -1575,6 +1643,73 @@ CREATE VIRTUAL TABLE IF NOT EXISTS fundus_fts USING fts5(
1575
1643
  if (rows.length === 0 && tokens.length > 1) {
1576
1644
  rows = runMatch(orExpr);
1577
1645
  }
1646
+ if (rows.length < limit) {
1647
+ const subToks = substringFallbackTokens(tokens);
1648
+ if (subToks.length) {
1649
+ const seen = new Set(rows.map((r) => r.entry_id));
1650
+ const likePatterns = subToks.map(likeContainsPattern);
1651
+ const need = limit - rows.length;
1652
+ const tagLangFilters = [];
1653
+ const tagLangParams = [];
1654
+ if (tag) {
1655
+ tagLangFilters.push(
1656
+ `EXISTS (SELECT 1 FROM json_each(s.tags_json) WHERE lower(json_each.value) = ?)`
1657
+ );
1658
+ tagLangParams.push(tag);
1659
+ }
1660
+ if (language) {
1661
+ tagLangFilters.push(`s.language = ?`);
1662
+ tagLangParams.push(language);
1663
+ }
1664
+ const tagLangSql = tagLangFilters.length ? `AND ${tagLangFilters.join(" AND ")}` : "";
1665
+ const appendExtra = (extra) => {
1666
+ for (const r of extra) {
1667
+ if (seen.has(r.entry_id)) continue;
1668
+ seen.add(r.entry_id);
1669
+ rows.push(r);
1670
+ if (rows.length >= limit) return true;
1671
+ }
1672
+ return false;
1673
+ };
1674
+ if (kind === "all" || kind === "passage") {
1675
+ const bodyClauses = likePatterns.map(() => `(lower(p.body) LIKE ? ESCAPE '\\' OR lower(s.title) LIKE ? ESCAPE '\\')`).join(" OR ");
1676
+ const bodyParams = [];
1677
+ for (const pat of likePatterns) bodyParams.push(pat, pat);
1678
+ const sql = `
1679
+ SELECT p.passage_id AS entry_id, 'passage' AS entry_kind, p.source_id, s.title, s.citation,
1680
+ s.license, s.tags_json, p.locator, p.quote_sha256, substr(p.body, 1, 240) AS snippet
1681
+ FROM passages p
1682
+ JOIN sources s ON s.source_id = p.source_id
1683
+ WHERE (${bodyClauses}) ${tagLangSql}
1684
+ ORDER BY p.passage_id ASC
1685
+ LIMIT ?
1686
+ `;
1687
+ const extra = this.db.prepare(sql).all(...bodyParams, ...tagLangParams, need * 4);
1688
+ if (appendExtra(extra)) {
1689
+ return { ok: true, query: input.query, totalMatches: rows.length, hits: mapHits(rows) };
1690
+ }
1691
+ }
1692
+ if (kind === "all" || kind === "media") {
1693
+ const mediaClauses = likePatterns.map(
1694
+ () => `(lower(COALESCE(m.caption, '')) LIKE ? ESCAPE '\\' OR lower(s.title) LIKE ? ESCAPE '\\')`
1695
+ ).join(" OR ");
1696
+ const mediaParams = [];
1697
+ for (const pat of likePatterns) mediaParams.push(pat, pat);
1698
+ const sql = `
1699
+ SELECT s.source_id AS entry_id, 'media' AS entry_kind, s.source_id, s.title, s.citation,
1700
+ s.license, s.tags_json, m.mime_type, m.width_px, m.height_px, m.caption,
1701
+ substr(COALESCE(m.caption, s.title), 1, 240) AS snippet
1702
+ FROM media_meta m
1703
+ JOIN sources s ON s.source_id = m.source_id
1704
+ WHERE (${mediaClauses}) ${tagLangSql}
1705
+ ORDER BY s.source_id ASC
1706
+ LIMIT ?
1707
+ `;
1708
+ const extra = this.db.prepare(sql).all(...mediaParams, ...tagLangParams, need * 4);
1709
+ appendExtra(extra);
1710
+ }
1711
+ }
1712
+ }
1578
1713
  return { ok: true, query: input.query, totalMatches: rows.length, hits: mapHits(rows) };
1579
1714
  }
1580
1715
  read(input) {
@@ -2071,19 +2206,25 @@ function registerPrimeTools(innerCtx, tools, vault) {
2071
2206
  name: "fundus_search",
2072
2207
  description: "Search indexed documents, verbatim source passages, and tagged media in the local Fundus archive (FTS5/BM25, zero vision tokens).",
2073
2208
  parameters: {
2074
- query: {
2075
- type: "string",
2076
- required: true,
2077
- description: 'Keywords or phrase; use "*" to list recent entries'
2078
- },
2079
- kind: {
2080
- type: "string",
2081
- enum: ["all", "passage", "source", "media"],
2082
- description: "Filter by entry kind (default all)"
2209
+ type: "object",
2210
+ additionalProperties: false,
2211
+ properties: {
2212
+ query: { type: "string", description: 'Keywords or phrase; use "*" to list recent entries' },
2213
+ kind: {
2214
+ type: "string",
2215
+ enum: ["all", "passage", "source", "media"],
2216
+ description: "Filter by entry kind (default all)"
2217
+ },
2218
+ tag: { type: "string", description: "Exact tag filter (case-insensitive)" },
2219
+ language: { type: "string", description: "ISO language code filter" },
2220
+ limit: {
2221
+ type: "integer",
2222
+ minimum: 1,
2223
+ maximum: 25,
2224
+ description: "Max hits 1..25 (default 8)"
2225
+ }
2083
2226
  },
2084
- tag: { type: "string", description: "Exact tag filter (case-insensitive)" },
2085
- language: { type: "string", description: "ISO language code filter" },
2086
- limit: { type: "integer", description: "Max hits 1..25 (default 8)" }
2227
+ required: ["query"]
2087
2228
  },
2088
2229
  output: { schema: { type: "object" }, render: renderJson },
2089
2230
  async execute(args) {
@@ -2101,15 +2242,19 @@ function registerPrimeTools(innerCtx, tools, vault) {
2101
2242
  name: "fundus_read",
2102
2243
  description: "Read a verbatim SourcePassage (pas-*) or source/media metadata (src-*) with ready-to-paste provenance.",
2103
2244
  parameters: {
2104
- id: {
2105
- type: "string",
2106
- required: true,
2107
- description: "Passage id pas-<12hex> or source/media id src-<12hex>"
2245
+ type: "object",
2246
+ additionalProperties: false,
2247
+ properties: {
2248
+ id: {
2249
+ type: "string",
2250
+ description: "Passage id pas-<12hex> or source/media id src-<12hex>"
2251
+ },
2252
+ includePassages: {
2253
+ type: "boolean",
2254
+ description: "When reading src-*, include up to 50 passages"
2255
+ }
2108
2256
  },
2109
- includePassages: {
2110
- type: "boolean",
2111
- description: "When reading src-*, include up to 50 passages"
2112
- }
2257
+ required: ["id"]
2113
2258
  },
2114
2259
  output: { schema: { type: "object" }, render: renderJson },
2115
2260
  async execute(args) {