dsh-fundus 0.1.0 → 0.1.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/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.1";
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}$/;
@@ -1533,6 +1543,37 @@ CREATE VIRTUAL TABLE IF NOT EXISTS fundus_fts USING fts5(
1533
1543
  if (rows2.length === 0 && tokens2.length > 1) {
1534
1544
  rows2 = runSourceMatch(orExpr2);
1535
1545
  }
1546
+ if (rows2.length < limit) {
1547
+ const subToks = substringFallbackTokens(tokens2);
1548
+ if (subToks.length) {
1549
+ const seen = new Set(rows2.map((r) => r.source_id));
1550
+ const likePatterns = subToks.map(likeContainsPattern);
1551
+ const likeClauses = likePatterns.map(
1552
+ () => `(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 '\\'))`
1553
+ ).join(" OR ");
1554
+ const likeParams = [];
1555
+ for (const pat of likePatterns) {
1556
+ likeParams.push(pat, pat);
1557
+ }
1558
+ const sql = `
1559
+ SELECT s.source_id AS entry_id, 'source' AS entry_kind, s.source_id, s.title, s.citation,
1560
+ s.license, s.tags_json, s.passage_count,
1561
+ (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
1562
+ FROM sources s
1563
+ WHERE 1=1 ${filterSql}
1564
+ AND (${likeClauses})
1565
+ ORDER BY s.source_id ASC
1566
+ LIMIT ?
1567
+ `;
1568
+ const extra = this.db.prepare(sql).all(...params, ...likeParams, limit * 4);
1569
+ for (const r of extra) {
1570
+ if (seen.has(r.source_id)) continue;
1571
+ seen.add(r.source_id);
1572
+ rows2.push(r);
1573
+ if (rows2.length >= limit) break;
1574
+ }
1575
+ }
1576
+ }
1536
1577
  return { ok: true, query: input.query, totalMatches: rows2.length, hits: mapHits(rows2) };
1537
1578
  }
1538
1579
  if (isBrowseQuery(input.query)) {
@@ -1575,6 +1616,73 @@ CREATE VIRTUAL TABLE IF NOT EXISTS fundus_fts USING fts5(
1575
1616
  if (rows.length === 0 && tokens.length > 1) {
1576
1617
  rows = runMatch(orExpr);
1577
1618
  }
1619
+ if (rows.length < limit) {
1620
+ const subToks = substringFallbackTokens(tokens);
1621
+ if (subToks.length) {
1622
+ const seen = new Set(rows.map((r) => r.entry_id));
1623
+ const likePatterns = subToks.map(likeContainsPattern);
1624
+ const need = limit - rows.length;
1625
+ const tagLangFilters = [];
1626
+ const tagLangParams = [];
1627
+ if (tag) {
1628
+ tagLangFilters.push(
1629
+ `EXISTS (SELECT 1 FROM json_each(s.tags_json) WHERE lower(json_each.value) = ?)`
1630
+ );
1631
+ tagLangParams.push(tag);
1632
+ }
1633
+ if (language) {
1634
+ tagLangFilters.push(`s.language = ?`);
1635
+ tagLangParams.push(language);
1636
+ }
1637
+ const tagLangSql = tagLangFilters.length ? `AND ${tagLangFilters.join(" AND ")}` : "";
1638
+ const appendExtra = (extra) => {
1639
+ for (const r of extra) {
1640
+ if (seen.has(r.entry_id)) continue;
1641
+ seen.add(r.entry_id);
1642
+ rows.push(r);
1643
+ if (rows.length >= limit) return true;
1644
+ }
1645
+ return false;
1646
+ };
1647
+ if (kind === "all" || kind === "passage") {
1648
+ const bodyClauses = likePatterns.map(() => `(lower(p.body) LIKE ? ESCAPE '\\' OR lower(s.title) LIKE ? ESCAPE '\\')`).join(" OR ");
1649
+ const bodyParams = [];
1650
+ for (const pat of likePatterns) bodyParams.push(pat, pat);
1651
+ const sql = `
1652
+ SELECT p.passage_id AS entry_id, 'passage' AS entry_kind, p.source_id, s.title, s.citation,
1653
+ s.license, s.tags_json, p.locator, p.quote_sha256, substr(p.body, 1, 240) AS snippet
1654
+ FROM passages p
1655
+ JOIN sources s ON s.source_id = p.source_id
1656
+ WHERE (${bodyClauses}) ${tagLangSql}
1657
+ ORDER BY p.passage_id ASC
1658
+ LIMIT ?
1659
+ `;
1660
+ const extra = this.db.prepare(sql).all(...bodyParams, ...tagLangParams, need * 4);
1661
+ if (appendExtra(extra)) {
1662
+ return { ok: true, query: input.query, totalMatches: rows.length, hits: mapHits(rows) };
1663
+ }
1664
+ }
1665
+ if (kind === "all" || kind === "media") {
1666
+ const mediaClauses = likePatterns.map(
1667
+ () => `(lower(COALESCE(m.caption, '')) LIKE ? ESCAPE '\\' OR lower(s.title) LIKE ? ESCAPE '\\')`
1668
+ ).join(" OR ");
1669
+ const mediaParams = [];
1670
+ for (const pat of likePatterns) mediaParams.push(pat, pat);
1671
+ const sql = `
1672
+ SELECT s.source_id AS entry_id, 'media' AS entry_kind, s.source_id, s.title, s.citation,
1673
+ s.license, s.tags_json, m.mime_type, m.width_px, m.height_px, m.caption,
1674
+ substr(COALESCE(m.caption, s.title), 1, 240) AS snippet
1675
+ FROM media_meta m
1676
+ JOIN sources s ON s.source_id = m.source_id
1677
+ WHERE (${mediaClauses}) ${tagLangSql}
1678
+ ORDER BY s.source_id ASC
1679
+ LIMIT ?
1680
+ `;
1681
+ const extra = this.db.prepare(sql).all(...mediaParams, ...tagLangParams, need * 4);
1682
+ appendExtra(extra);
1683
+ }
1684
+ }
1685
+ }
1578
1686
  return { ok: true, query: input.query, totalMatches: rows.length, hits: mapHits(rows) };
1579
1687
  }
1580
1688
  read(input) {
@@ -2071,19 +2179,25 @@ function registerPrimeTools(innerCtx, tools, vault) {
2071
2179
  name: "fundus_search",
2072
2180
  description: "Search indexed documents, verbatim source passages, and tagged media in the local Fundus archive (FTS5/BM25, zero vision tokens).",
2073
2181
  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)"
2182
+ type: "object",
2183
+ additionalProperties: false,
2184
+ properties: {
2185
+ query: { type: "string", description: 'Keywords or phrase; use "*" to list recent entries' },
2186
+ kind: {
2187
+ type: "string",
2188
+ enum: ["all", "passage", "source", "media"],
2189
+ description: "Filter by entry kind (default all)"
2190
+ },
2191
+ tag: { type: "string", description: "Exact tag filter (case-insensitive)" },
2192
+ language: { type: "string", description: "ISO language code filter" },
2193
+ limit: {
2194
+ type: "integer",
2195
+ minimum: 1,
2196
+ maximum: 25,
2197
+ description: "Max hits 1..25 (default 8)"
2198
+ }
2083
2199
  },
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)" }
2200
+ required: ["query"]
2087
2201
  },
2088
2202
  output: { schema: { type: "object" }, render: renderJson },
2089
2203
  async execute(args) {
@@ -2101,15 +2215,19 @@ function registerPrimeTools(innerCtx, tools, vault) {
2101
2215
  name: "fundus_read",
2102
2216
  description: "Read a verbatim SourcePassage (pas-*) or source/media metadata (src-*) with ready-to-paste provenance.",
2103
2217
  parameters: {
2104
- id: {
2105
- type: "string",
2106
- required: true,
2107
- description: "Passage id pas-<12hex> or source/media id src-<12hex>"
2218
+ type: "object",
2219
+ additionalProperties: false,
2220
+ properties: {
2221
+ id: {
2222
+ type: "string",
2223
+ description: "Passage id pas-<12hex> or source/media id src-<12hex>"
2224
+ },
2225
+ includePassages: {
2226
+ type: "boolean",
2227
+ description: "When reading src-*, include up to 50 passages"
2228
+ }
2108
2229
  },
2109
- includePassages: {
2110
- type: "boolean",
2111
- description: "When reading src-*, include up to 50 passages"
2112
- }
2230
+ required: ["id"]
2113
2231
  },
2114
2232
  output: { schema: { type: "object" }, render: renderJson },
2115
2233
  async execute(args) {