@hiveai/mcp 0.4.5 → 0.5.0
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 +558 -21
- package/dist/index.js.map +1 -1
- package/dist/server.d.ts +305 -2
- package/dist/server.js +566 -22
- package/dist/server.js.map +1 -1
- package/package.json +4 -4
package/dist/server.js
CHANGED
|
@@ -1195,6 +1195,9 @@ var GetBriefingInputSchema = {
|
|
|
1195
1195
|
),
|
|
1196
1196
|
symbols: z17.array(z17.string()).default([]).describe(
|
|
1197
1197
|
"Symbol names to look up in the code-map (e.g. ['PaymentService', 'TenantFilter']). Returns the file(s) exporting each symbol so agents don't need to grep. Requires `haive index code` to have been run."
|
|
1198
|
+
),
|
|
1199
|
+
min_semantic_score: z17.number().min(0).max(1).default(0).describe(
|
|
1200
|
+
"Drop semantic-only memory hits whose cosine score is below this threshold. Useful to avoid weakly-related noise when the task is short or the corpus is broad. Has no effect on memories matched via anchor/module/literal \u2014 those are always kept. Try 0.25\u20130.4 for stricter matching."
|
|
1198
1201
|
)
|
|
1199
1202
|
};
|
|
1200
1203
|
async function getBriefing(input, ctx) {
|
|
@@ -1227,6 +1230,7 @@ async function getBriefing(input, ctx) {
|
|
|
1227
1230
|
return true;
|
|
1228
1231
|
});
|
|
1229
1232
|
usage = await loadUsageIndex7(ctx.paths);
|
|
1233
|
+
byId = new Map(allMemories.map((m) => [m.memory.frontmatter.id, m]));
|
|
1230
1234
|
const semanticHits = input.task && input.semantic ? await trySemanticHits(ctx, input.task, allMemories.length * 2) : null;
|
|
1231
1235
|
if (input.task && input.semantic) {
|
|
1232
1236
|
searchMode = semanticHits ? "semantic" : "literal_fallback";
|
|
@@ -1291,6 +1295,10 @@ async function getBriefing(input, ctx) {
|
|
|
1291
1295
|
}
|
|
1292
1296
|
if (semanticHits) {
|
|
1293
1297
|
for (const hit of semanticHits) {
|
|
1298
|
+
if (hit.score < input.min_semantic_score) {
|
|
1299
|
+
const existing = seen.get(hit.id);
|
|
1300
|
+
if (!existing) continue;
|
|
1301
|
+
}
|
|
1294
1302
|
const loaded = byId.get(hit.id);
|
|
1295
1303
|
if (loaded) addOrUpdate(loaded, "semantic", hit.score, "semantic");
|
|
1296
1304
|
}
|
|
@@ -1304,7 +1312,6 @@ async function getBriefing(input, ctx) {
|
|
|
1304
1312
|
const sb = reasonScore(b) + confidenceScore(b) + (b.semantic_score ?? 0);
|
|
1305
1313
|
return sb - sa;
|
|
1306
1314
|
});
|
|
1307
|
-
byId = new Map(allMemories.map((m) => [m.memory.frontmatter.id, m]));
|
|
1308
1315
|
for (const mem of ranked.slice(0, input.max_memories)) {
|
|
1309
1316
|
if (seen.size >= input.max_memories * 2) break;
|
|
1310
1317
|
const loaded = byId.get(mem.id);
|
|
@@ -1496,6 +1503,36 @@ ${m.content}`).join("\n\n---\n\n"),
|
|
|
1496
1503
|
});
|
|
1497
1504
|
}
|
|
1498
1505
|
}
|
|
1506
|
+
const memoriesEmpty = outputMemories.length === 0;
|
|
1507
|
+
const hasMemoriesDir = existsSync17(ctx.paths.memoriesDir);
|
|
1508
|
+
const isColdStart = isTemplateContext && memoriesEmpty && !lastSession && !autoContextGenerated;
|
|
1509
|
+
const hints = [];
|
|
1510
|
+
if (isColdStart) {
|
|
1511
|
+
hints.push(
|
|
1512
|
+
"haive is uninitialized for this project (project-context.md is template, 0 memories, no past session). Skip future get_briefing calls until memories exist \u2014 use Read/Grep directly. Run `haive init` and the bootstrap_project prompt to fix."
|
|
1513
|
+
);
|
|
1514
|
+
} else {
|
|
1515
|
+
if (outputMemories.some((m) => m.type === "attempt")) {
|
|
1516
|
+
hints.push(
|
|
1517
|
+
"\u26A0\uFE0F One or more 'attempt' memories matched \u2014 these document failed approaches. Read them BEFORE writing code to avoid repeating the mistake."
|
|
1518
|
+
);
|
|
1519
|
+
}
|
|
1520
|
+
if (outputMemories.some((m) => m.type === "gotcha")) {
|
|
1521
|
+
hints.push(
|
|
1522
|
+
"Gotcha memories matched \u2014 non-obvious traps. Verify the 'how to apply' line still holds before assuming behavior."
|
|
1523
|
+
);
|
|
1524
|
+
}
|
|
1525
|
+
if (memoriesEmpty && hasMemoriesDir && input.task) {
|
|
1526
|
+
hints.push(
|
|
1527
|
+
"No memories matched this task. Try mem_search with broader/different terms, or call mem_for_files with the files you intend to edit."
|
|
1528
|
+
);
|
|
1529
|
+
}
|
|
1530
|
+
if (input.task && outputMemories.length > 0 && actionRequired.length === 0) {
|
|
1531
|
+
hints.push(
|
|
1532
|
+
"After completing the task: capture new gotchas with mem_observe, failed approaches with mem_tried, validated patterns with mem_save."
|
|
1533
|
+
);
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1499
1536
|
return {
|
|
1500
1537
|
...input.task ? { task: input.task } : {},
|
|
1501
1538
|
search_mode: searchMode,
|
|
@@ -1513,6 +1550,8 @@ ${m.content}`).join("\n\n---\n\n"),
|
|
|
1513
1550
|
action_required: actionRequired,
|
|
1514
1551
|
decay_warnings: decayWarnings,
|
|
1515
1552
|
setup_warnings: setupWarnings,
|
|
1553
|
+
...isColdStart ? { low_value: true } : {},
|
|
1554
|
+
...hints.length > 0 ? { hints } : {},
|
|
1516
1555
|
estimated_tokens: totalTokens,
|
|
1517
1556
|
budget: {
|
|
1518
1557
|
max_tokens: input.max_tokens,
|
|
@@ -1560,12 +1599,18 @@ async function loadModuleContexts2(ctx, modules) {
|
|
|
1560
1599
|
}
|
|
1561
1600
|
|
|
1562
1601
|
// src/tools/code-map.ts
|
|
1563
|
-
import { loadCodeMap as loadCodeMap2, queryCodeMap as queryCodeMap2 } from "@hiveai/core";
|
|
1602
|
+
import { estimateTokens as estimateTokens2, loadCodeMap as loadCodeMap2, queryCodeMap as queryCodeMap2 } from "@hiveai/core";
|
|
1564
1603
|
import { z as z18 } from "zod";
|
|
1565
1604
|
var CodeMapInputSchema = {
|
|
1566
1605
|
file: z18.string().optional().describe("Filter to files whose path contains this substring"),
|
|
1567
1606
|
symbol: z18.string().optional().describe("Filter to files exporting a symbol whose name contains this substring"),
|
|
1568
|
-
|
|
1607
|
+
paths: z18.array(z18.string()).default([]).describe(
|
|
1608
|
+
"Filter to files under any of these path prefixes (e.g. ['packages/mcp/src/tools/', 'src/auth/']). OR-joined with `file` substring; useful to get a focused view of one module."
|
|
1609
|
+
),
|
|
1610
|
+
max_files: z18.number().int().positive().default(40).describe("Cap on returned files (hard limit, applied after token budget)"),
|
|
1611
|
+
max_tokens: z18.number().int().positive().optional().describe(
|
|
1612
|
+
"Approximate token budget for the response. When the matching set exceeds it, files are ranked by export density (exports per LOC) and the highest-signal ones are kept first. Omit to disable budgeting (legacy behavior)."
|
|
1613
|
+
)
|
|
1569
1614
|
};
|
|
1570
1615
|
async function codeMapTool(input, ctx) {
|
|
1571
1616
|
const map = await loadCodeMap2(ctx.paths);
|
|
@@ -1576,19 +1621,63 @@ async function codeMapTool(input, ctx) {
|
|
|
1576
1621
|
notice: "No code map found. Run `haive index code` to generate `.ai/code-map.json`."
|
|
1577
1622
|
};
|
|
1578
1623
|
}
|
|
1579
|
-
const { files } = queryCodeMap2(map, { file: input.file, symbol: input.symbol });
|
|
1624
|
+
const { files: matched } = queryCodeMap2(map, { file: input.file, symbol: input.symbol });
|
|
1625
|
+
const pathsFiltered = input.paths.length === 0 ? matched : matched.filter((f) => input.paths.some((p) => f.path.startsWith(stripLeadingSlash(p))));
|
|
1626
|
+
const alphabetical = [...pathsFiltered].sort((a, b) => a.path.localeCompare(b.path));
|
|
1627
|
+
let kept = alphabetical;
|
|
1628
|
+
let budgetClipped = false;
|
|
1629
|
+
if (input.max_tokens !== void 0) {
|
|
1630
|
+
const byDensity = [...alphabetical].sort((a, b) => {
|
|
1631
|
+
const da = density(a.entry.exports.length, a.entry.loc);
|
|
1632
|
+
const db = density(b.entry.exports.length, b.entry.loc);
|
|
1633
|
+
if (da !== db) return db - da;
|
|
1634
|
+
return a.path.localeCompare(b.path);
|
|
1635
|
+
});
|
|
1636
|
+
const keepSet = /* @__PURE__ */ new Set();
|
|
1637
|
+
let spent = 0;
|
|
1638
|
+
for (const f of byDensity) {
|
|
1639
|
+
const cost = estimateFileEntryTokens(f);
|
|
1640
|
+
if (spent + cost > input.max_tokens && keepSet.size > 0) {
|
|
1641
|
+
budgetClipped = true;
|
|
1642
|
+
break;
|
|
1643
|
+
}
|
|
1644
|
+
keepSet.add(f.path);
|
|
1645
|
+
spent += cost;
|
|
1646
|
+
}
|
|
1647
|
+
if (budgetClipped) {
|
|
1648
|
+
kept = alphabetical.filter((f) => keepSet.has(f.path));
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
const finalFiles = kept.slice(0, input.max_files);
|
|
1652
|
+
const totalDropped = pathsFiltered.length - finalFiles.length;
|
|
1580
1653
|
return {
|
|
1581
1654
|
available: true,
|
|
1582
1655
|
generated_at: map.generated_at,
|
|
1583
1656
|
total_files: Object.keys(map.files).length,
|
|
1584
|
-
files:
|
|
1657
|
+
files: finalFiles.map((f) => ({
|
|
1585
1658
|
path: f.path,
|
|
1586
1659
|
...f.entry.summary ? { summary: f.entry.summary } : {},
|
|
1587
1660
|
loc: f.entry.loc,
|
|
1588
1661
|
exports: f.entry.exports
|
|
1589
|
-
}))
|
|
1662
|
+
})),
|
|
1663
|
+
...totalDropped > 0 ? { truncated: totalDropped } : {},
|
|
1664
|
+
...budgetClipped ? { budget_clipped: true } : {}
|
|
1590
1665
|
};
|
|
1591
1666
|
}
|
|
1667
|
+
function density(exports, loc) {
|
|
1668
|
+
if (loc <= 0) return 0;
|
|
1669
|
+
return exports / Math.max(loc, 1);
|
|
1670
|
+
}
|
|
1671
|
+
function stripLeadingSlash(p) {
|
|
1672
|
+
return p.startsWith("/") ? p.slice(1) : p;
|
|
1673
|
+
}
|
|
1674
|
+
function estimateFileEntryTokens(f) {
|
|
1675
|
+
const exportsCost = f.entry.exports.reduce(
|
|
1676
|
+
(acc, e) => acc + 6 + estimateTokens2(e.description ?? ""),
|
|
1677
|
+
0
|
|
1678
|
+
);
|
|
1679
|
+
return estimateTokens2(f.path) + estimateTokens2(f.entry.summary ?? "") + exportsCost + 4;
|
|
1680
|
+
}
|
|
1592
1681
|
|
|
1593
1682
|
// src/tools/mem-diff.ts
|
|
1594
1683
|
import { existsSync as existsSync18 } from "fs";
|
|
@@ -1635,13 +1724,349 @@ async function memDiff(input, ctx) {
|
|
|
1635
1724
|
};
|
|
1636
1725
|
}
|
|
1637
1726
|
|
|
1638
|
-
// src/
|
|
1727
|
+
// src/tools/get-recap.ts
|
|
1728
|
+
import { existsSync as existsSync19 } from "fs";
|
|
1729
|
+
import { loadMemoriesFromDir as loadMemoriesFromDir15 } from "@hiveai/core";
|
|
1639
1730
|
import { z as z20 } from "zod";
|
|
1731
|
+
var GetRecapInputSchema = {
|
|
1732
|
+
scope: z20.enum(["personal", "team", "any"]).default("any").describe(
|
|
1733
|
+
"Limit to a specific scope's recap. Default 'any' returns the most recent recap across both personal and team scopes."
|
|
1734
|
+
)
|
|
1735
|
+
};
|
|
1736
|
+
async function getRecap(input, ctx) {
|
|
1737
|
+
if (!existsSync19(ctx.paths.memoriesDir)) {
|
|
1738
|
+
return { recap: null, notice: "No .ai/memories directory \u2014 haive not initialized here." };
|
|
1739
|
+
}
|
|
1740
|
+
const all = await loadMemoriesFromDir15(ctx.paths.memoriesDir);
|
|
1741
|
+
const recaps = all.filter(({ memory }) => memory.frontmatter.type === "session_recap").filter(({ memory }) => input.scope === "any" || memory.frontmatter.scope === input.scope).sort(
|
|
1742
|
+
(a, b) => new Date(b.memory.frontmatter.created_at).getTime() - new Date(a.memory.frontmatter.created_at).getTime()
|
|
1743
|
+
);
|
|
1744
|
+
if (recaps.length === 0) {
|
|
1745
|
+
return {
|
|
1746
|
+
recap: null,
|
|
1747
|
+
notice: input.scope === "any" ? "No session recap saved yet. Run mem_session_end (or post_task prompt) to capture one." : `No session recap found in scope '${input.scope}'.`
|
|
1748
|
+
};
|
|
1749
|
+
}
|
|
1750
|
+
const r = recaps[0];
|
|
1751
|
+
const fm = r.memory.frontmatter;
|
|
1752
|
+
return {
|
|
1753
|
+
recap: {
|
|
1754
|
+
id: fm.id,
|
|
1755
|
+
scope: fm.scope,
|
|
1756
|
+
revision_count: fm.revision_count ?? 0,
|
|
1757
|
+
created_at: fm.created_at,
|
|
1758
|
+
body: r.memory.body
|
|
1759
|
+
}
|
|
1760
|
+
};
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
// src/tools/mem-relevant-to.ts
|
|
1764
|
+
import { z as z21 } from "zod";
|
|
1765
|
+
var MemRelevantToInputSchema = {
|
|
1766
|
+
task: z21.string().min(1).describe("What you are about to do, in 1\u20132 sentences. Used to rank relevant memories."),
|
|
1767
|
+
files: z21.array(z21.string()).default([]).describe("Optional: files you are about to edit \u2014 surfaces anchored memories."),
|
|
1768
|
+
limit: z21.number().int().positive().max(30).default(8).describe("Cap on returned memories."),
|
|
1769
|
+
min_semantic_score: z21.number().min(0).max(1).default(0.25).describe("Drop weakly-related semantic hits below this cosine threshold."),
|
|
1770
|
+
format: z21.enum(["full", "compact"]).default("full").describe("'compact' = id + 1-line summary; 'full' = complete bodies.")
|
|
1771
|
+
};
|
|
1772
|
+
async function memRelevantTo(input, ctx) {
|
|
1773
|
+
const briefingInput = {
|
|
1774
|
+
task: input.task,
|
|
1775
|
+
files: input.files,
|
|
1776
|
+
max_tokens: 16e3,
|
|
1777
|
+
max_memories: input.limit,
|
|
1778
|
+
include_project_context: false,
|
|
1779
|
+
include_module_contexts: false,
|
|
1780
|
+
semantic: true,
|
|
1781
|
+
include_stale: false,
|
|
1782
|
+
track: true,
|
|
1783
|
+
format: input.format,
|
|
1784
|
+
symbols: [],
|
|
1785
|
+
min_semantic_score: input.min_semantic_score
|
|
1786
|
+
};
|
|
1787
|
+
const briefing = await getBriefing(briefingInput, ctx);
|
|
1788
|
+
const out = {
|
|
1789
|
+
task: input.task,
|
|
1790
|
+
search_mode: briefing.search_mode,
|
|
1791
|
+
memories: briefing.memories
|
|
1792
|
+
};
|
|
1793
|
+
if (briefing.hints && briefing.hints.length > 0) out.hints = briefing.hints;
|
|
1794
|
+
if (briefing.memories.length === 0) out.empty = true;
|
|
1795
|
+
return out;
|
|
1796
|
+
}
|
|
1797
|
+
|
|
1798
|
+
// src/tools/code-search.ts
|
|
1799
|
+
import { z as z22 } from "zod";
|
|
1800
|
+
var CodeSearchInputSchema = {
|
|
1801
|
+
query: z22.string().min(1).describe(
|
|
1802
|
+
"Natural-language description of what you are looking for in the codebase (e.g. 'function that hashes passwords', 'JWT signing logic', 'route registration')."
|
|
1803
|
+
),
|
|
1804
|
+
k: z22.number().int().positive().max(50).default(5).describe("Number of top hits to return."),
|
|
1805
|
+
min_score: z22.number().min(0).max(1).default(0.2).describe(
|
|
1806
|
+
"Minimum cosine similarity. Hits below this threshold are dropped to avoid noise. Try 0.3+ for stricter matching."
|
|
1807
|
+
)
|
|
1808
|
+
};
|
|
1809
|
+
async function codeSearch(input, ctx) {
|
|
1810
|
+
let mod;
|
|
1811
|
+
try {
|
|
1812
|
+
mod = await import("@hiveai/embeddings");
|
|
1813
|
+
} catch {
|
|
1814
|
+
return {
|
|
1815
|
+
available: false,
|
|
1816
|
+
hits: [],
|
|
1817
|
+
notice: "@hiveai/embeddings is not installed. Install it (`pnpm add @hiveai/embeddings`) and run `haive index code-search` to enable semantic code search."
|
|
1818
|
+
};
|
|
1819
|
+
}
|
|
1820
|
+
const result = await mod.codeSemanticSearch(ctx.paths, input.query, {
|
|
1821
|
+
limit: input.k,
|
|
1822
|
+
minScore: input.min_score
|
|
1823
|
+
});
|
|
1824
|
+
if (!result) {
|
|
1825
|
+
return {
|
|
1826
|
+
available: false,
|
|
1827
|
+
hits: [],
|
|
1828
|
+
notice: "Code semantic-search index not built. Run `haive index code-search` to generate it (builds embeddings for every exported symbol in the code-map)."
|
|
1829
|
+
};
|
|
1830
|
+
}
|
|
1831
|
+
return { available: true, hits: result.hits };
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1834
|
+
// src/tools/why-this-file.ts
|
|
1835
|
+
import { existsSync as existsSync20 } from "fs";
|
|
1836
|
+
import { spawn } from "child_process";
|
|
1837
|
+
import path9 from "path";
|
|
1838
|
+
import {
|
|
1839
|
+
deriveConfidence as deriveConfidence5,
|
|
1840
|
+
getUsage as getUsage6,
|
|
1841
|
+
loadCodeMap as loadCodeMap3,
|
|
1842
|
+
loadMemoriesFromDir as loadMemoriesFromDir16,
|
|
1843
|
+
loadUsageIndex as loadUsageIndex8,
|
|
1844
|
+
memoryMatchesAnchorPaths as memoryMatchesAnchorPaths3
|
|
1845
|
+
} from "@hiveai/core";
|
|
1846
|
+
import { z as z23 } from "zod";
|
|
1847
|
+
var WhyThisFileInputSchema = {
|
|
1848
|
+
path: z23.string().min(1).describe(
|
|
1849
|
+
"Project-relative path to the file you want context on (e.g. 'packages/mcp/src/tools/mem-save.ts')."
|
|
1850
|
+
),
|
|
1851
|
+
git_log_limit: z23.number().int().positive().max(20).default(5).describe("How many recent commits touching this file to include."),
|
|
1852
|
+
memory_limit: z23.number().int().positive().max(20).default(5).describe("Cap on memories anchored to this path.")
|
|
1853
|
+
};
|
|
1854
|
+
async function whyThisFile(input, ctx) {
|
|
1855
|
+
const fileExists = existsSync20(path9.join(ctx.paths.root, input.path));
|
|
1856
|
+
const [commits, memories, codeMap] = await Promise.all([
|
|
1857
|
+
runGitLog(ctx.paths.root, input.path, input.git_log_limit).catch(() => []),
|
|
1858
|
+
collectAnchoredMemories(ctx, input.path, input.memory_limit),
|
|
1859
|
+
loadCodeMap3(ctx.paths)
|
|
1860
|
+
]);
|
|
1861
|
+
const codeMapEntry = codeMap?.files[input.path];
|
|
1862
|
+
const hints = [];
|
|
1863
|
+
if (!fileExists) {
|
|
1864
|
+
hints.push(`File '${input.path}' does not exist on disk \u2014 path may be wrong or file removed.`);
|
|
1865
|
+
}
|
|
1866
|
+
if (commits.length === 0 && fileExists) {
|
|
1867
|
+
hints.push("No git history found \u2014 file may be untracked or git not initialized.");
|
|
1868
|
+
}
|
|
1869
|
+
if (memories.length === 0 && fileExists) {
|
|
1870
|
+
hints.push(
|
|
1871
|
+
"No memories anchored here. If you discover something non-obvious while editing, use mem_observe (with where=" + input.path + ") to capture it."
|
|
1872
|
+
);
|
|
1873
|
+
}
|
|
1874
|
+
if (memories.some((m) => m.type === "attempt" || m.type === "gotcha")) {
|
|
1875
|
+
hints.push("\u26A0\uFE0F attempt/gotcha memories anchored to this file \u2014 read them BEFORE editing.");
|
|
1876
|
+
}
|
|
1877
|
+
return {
|
|
1878
|
+
file: input.path,
|
|
1879
|
+
exists: fileExists,
|
|
1880
|
+
recent_commits: commits,
|
|
1881
|
+
memories,
|
|
1882
|
+
code_map_entry: codeMapEntry ? {
|
|
1883
|
+
...codeMapEntry.summary ? { summary: codeMapEntry.summary } : {},
|
|
1884
|
+
loc: codeMapEntry.loc,
|
|
1885
|
+
exports: codeMapEntry.exports.map((e) => ({
|
|
1886
|
+
name: e.name,
|
|
1887
|
+
kind: e.kind,
|
|
1888
|
+
line: e.line,
|
|
1889
|
+
...e.description ? { description: e.description } : {}
|
|
1890
|
+
}))
|
|
1891
|
+
} : null,
|
|
1892
|
+
...hints.length > 0 ? { hints } : {}
|
|
1893
|
+
};
|
|
1894
|
+
}
|
|
1895
|
+
async function collectAnchoredMemories(ctx, filePath, limit) {
|
|
1896
|
+
if (!existsSync20(ctx.paths.memoriesDir)) return [];
|
|
1897
|
+
const all = await loadMemoriesFromDir16(ctx.paths.memoriesDir);
|
|
1898
|
+
const usage = await loadUsageIndex8(ctx.paths);
|
|
1899
|
+
const out = [];
|
|
1900
|
+
for (const { memory } of all) {
|
|
1901
|
+
const fm = memory.frontmatter;
|
|
1902
|
+
if (fm.status === "rejected" || fm.status === "deprecated") continue;
|
|
1903
|
+
if (fm.type === "session_recap") continue;
|
|
1904
|
+
if (!memoryMatchesAnchorPaths3(memory, [filePath])) continue;
|
|
1905
|
+
const u = getUsage6(usage, fm.id);
|
|
1906
|
+
out.push({
|
|
1907
|
+
id: fm.id,
|
|
1908
|
+
type: fm.type,
|
|
1909
|
+
scope: fm.scope,
|
|
1910
|
+
confidence: deriveConfidence5(fm, u),
|
|
1911
|
+
body_preview: memory.body.split("\n").slice(0, 6).join("\n")
|
|
1912
|
+
});
|
|
1913
|
+
if (out.length >= limit) break;
|
|
1914
|
+
}
|
|
1915
|
+
return out;
|
|
1916
|
+
}
|
|
1917
|
+
async function runGitLog(cwd, filePath, limit) {
|
|
1918
|
+
const sep = "<<HV>>";
|
|
1919
|
+
const fmt = `%h${sep}%an${sep}%ar${sep}%s`;
|
|
1920
|
+
const output = await runCommand(
|
|
1921
|
+
"git",
|
|
1922
|
+
["log", `-n`, String(limit), `--pretty=format:${fmt}`, "--", filePath],
|
|
1923
|
+
cwd
|
|
1924
|
+
);
|
|
1925
|
+
if (!output.trim()) return [];
|
|
1926
|
+
return output.split("\n").map((line) => {
|
|
1927
|
+
const [sha = "", author = "", relative_date = "", subject = ""] = line.split(sep);
|
|
1928
|
+
return { sha, author, relative_date, subject };
|
|
1929
|
+
}).filter((c) => c.sha);
|
|
1930
|
+
}
|
|
1931
|
+
function runCommand(cmd, args, cwd) {
|
|
1932
|
+
return new Promise((resolve, reject) => {
|
|
1933
|
+
const proc = spawn(cmd, args, { cwd, stdio: ["ignore", "pipe", "pipe"] });
|
|
1934
|
+
let stdout = "";
|
|
1935
|
+
let stderr = "";
|
|
1936
|
+
proc.stdout.on("data", (chunk) => {
|
|
1937
|
+
stdout += chunk.toString();
|
|
1938
|
+
});
|
|
1939
|
+
proc.stderr.on("data", (chunk) => {
|
|
1940
|
+
stderr += chunk.toString();
|
|
1941
|
+
});
|
|
1942
|
+
proc.on("error", reject);
|
|
1943
|
+
proc.on("close", (code) => {
|
|
1944
|
+
if (code === 0) resolve(stdout);
|
|
1945
|
+
else reject(new Error(stderr || `${cmd} exited with code ${code}`));
|
|
1946
|
+
});
|
|
1947
|
+
});
|
|
1948
|
+
}
|
|
1949
|
+
|
|
1950
|
+
// src/tools/anti-patterns-check.ts
|
|
1951
|
+
import { existsSync as existsSync21 } from "fs";
|
|
1952
|
+
import {
|
|
1953
|
+
deriveConfidence as deriveConfidence6,
|
|
1954
|
+
getUsage as getUsage7,
|
|
1955
|
+
loadMemoriesFromDir as loadMemoriesFromDir17,
|
|
1956
|
+
loadUsageIndex as loadUsageIndex9,
|
|
1957
|
+
literalMatchesAnyToken as literalMatchesAnyToken3,
|
|
1958
|
+
memoryMatchesAnchorPaths as memoryMatchesAnchorPaths4,
|
|
1959
|
+
tokenizeQuery as tokenizeQuery3
|
|
1960
|
+
} from "@hiveai/core";
|
|
1961
|
+
import { z as z24 } from "zod";
|
|
1962
|
+
var AntiPatternsCheckInputSchema = {
|
|
1963
|
+
diff: z24.string().optional().describe(
|
|
1964
|
+
"Raw unified diff text (or any code/text snippet) to scan for previously documented anti-patterns. Tokens from the diff are used to match memory bodies and the embeddings index."
|
|
1965
|
+
),
|
|
1966
|
+
paths: z24.array(z24.string()).default([]).describe(
|
|
1967
|
+
"File paths affected by the change. Memories anchored to any of these paths are surfaced regardless of the diff content."
|
|
1968
|
+
),
|
|
1969
|
+
limit: z24.number().int().positive().max(20).default(8).describe("Cap on returned warnings."),
|
|
1970
|
+
semantic: z24.boolean().default(true).describe(
|
|
1971
|
+
"When true, also use semantic search (requires @hiveai/embeddings + memory index) to find related anti-patterns."
|
|
1972
|
+
)
|
|
1973
|
+
};
|
|
1974
|
+
async function antiPatternsCheck(input, ctx) {
|
|
1975
|
+
if (!input.diff && input.paths.length === 0) {
|
|
1976
|
+
return {
|
|
1977
|
+
scanned: 0,
|
|
1978
|
+
warnings: [],
|
|
1979
|
+
notice: "Nothing to check \u2014 provide either `diff` text or `paths`."
|
|
1980
|
+
};
|
|
1981
|
+
}
|
|
1982
|
+
if (!existsSync21(ctx.paths.memoriesDir)) {
|
|
1983
|
+
return { scanned: 0, warnings: [], notice: "No .ai/memories directory \u2014 nothing to check against." };
|
|
1984
|
+
}
|
|
1985
|
+
const all = await loadMemoriesFromDir17(ctx.paths.memoriesDir);
|
|
1986
|
+
const negative = all.filter(({ memory }) => {
|
|
1987
|
+
const t = memory.frontmatter.type;
|
|
1988
|
+
if (t !== "attempt" && t !== "gotcha") return false;
|
|
1989
|
+
const s = memory.frontmatter.status;
|
|
1990
|
+
return s !== "rejected" && s !== "deprecated" && s !== "stale";
|
|
1991
|
+
});
|
|
1992
|
+
if (negative.length === 0) {
|
|
1993
|
+
return { scanned: 0, warnings: [], notice: "No attempt/gotcha memories found yet." };
|
|
1994
|
+
}
|
|
1995
|
+
const usage = await loadUsageIndex9(ctx.paths);
|
|
1996
|
+
const seen = /* @__PURE__ */ new Map();
|
|
1997
|
+
const upsert = (fm, body, reason, score) => {
|
|
1998
|
+
const existing = seen.get(fm.id);
|
|
1999
|
+
if (existing) {
|
|
2000
|
+
if (!existing.reasons.includes(reason)) existing.reasons.push(reason);
|
|
2001
|
+
if (score !== void 0 && (existing.semantic_score ?? 0) < score) {
|
|
2002
|
+
existing.semantic_score = score;
|
|
2003
|
+
}
|
|
2004
|
+
return;
|
|
2005
|
+
}
|
|
2006
|
+
const u = getUsage7(usage, fm.id);
|
|
2007
|
+
seen.set(fm.id, {
|
|
2008
|
+
id: fm.id,
|
|
2009
|
+
type: fm.type,
|
|
2010
|
+
scope: fm.scope,
|
|
2011
|
+
confidence: deriveConfidence6(fm, u),
|
|
2012
|
+
body_preview: body.split("\n").slice(0, 5).join("\n").slice(0, 400),
|
|
2013
|
+
reasons: [reason],
|
|
2014
|
+
...score !== void 0 ? { semantic_score: score } : {}
|
|
2015
|
+
});
|
|
2016
|
+
};
|
|
2017
|
+
if (input.paths.length > 0) {
|
|
2018
|
+
for (const { memory } of negative) {
|
|
2019
|
+
if (memoryMatchesAnchorPaths4(memory, input.paths)) {
|
|
2020
|
+
upsert(memory.frontmatter, memory.body, "anchor");
|
|
2021
|
+
}
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
if (input.diff) {
|
|
2025
|
+
const tokens = tokenizeQuery3(input.diff);
|
|
2026
|
+
if (tokens.length > 0) {
|
|
2027
|
+
for (const { memory } of negative) {
|
|
2028
|
+
if (literalMatchesAnyToken3(memory, tokens)) {
|
|
2029
|
+
upsert(memory.frontmatter, memory.body, "literal");
|
|
2030
|
+
}
|
|
2031
|
+
}
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
if (input.semantic && input.diff) {
|
|
2035
|
+
try {
|
|
2036
|
+
const mod = await import("@hiveai/embeddings");
|
|
2037
|
+
const result = await mod.semanticSearch(ctx.paths, input.diff, { limit: input.limit * 2 });
|
|
2038
|
+
if (result) {
|
|
2039
|
+
const negativeIds = new Set(negative.map(({ memory }) => memory.frontmatter.id));
|
|
2040
|
+
for (const hit of result.hits) {
|
|
2041
|
+
if (!negativeIds.has(hit.id)) continue;
|
|
2042
|
+
const found = negative.find(({ memory }) => memory.frontmatter.id === hit.id);
|
|
2043
|
+
if (found) upsert(found.memory.frontmatter, found.memory.body, "semantic", hit.score);
|
|
2044
|
+
}
|
|
2045
|
+
}
|
|
2046
|
+
} catch {
|
|
2047
|
+
}
|
|
2048
|
+
}
|
|
2049
|
+
const warnings = [...seen.values()].sort((a, b) => {
|
|
2050
|
+
const score = (w) => {
|
|
2051
|
+
const reasonW = (w.reasons.includes("anchor") ? 4 : 0) + (w.reasons.includes("literal") ? 2 : 0) + (w.reasons.includes("semantic") ? 1 : 0);
|
|
2052
|
+
const confW = w.confidence === "authoritative" ? 3 : w.confidence === "trusted" ? 2 : w.confidence === "low" ? 1 : 0;
|
|
2053
|
+
return reasonW + confW + (w.semantic_score ?? 0);
|
|
2054
|
+
};
|
|
2055
|
+
return score(b) - score(a);
|
|
2056
|
+
}).slice(0, input.limit);
|
|
2057
|
+
return {
|
|
2058
|
+
scanned: negative.length,
|
|
2059
|
+
warnings
|
|
2060
|
+
};
|
|
2061
|
+
}
|
|
2062
|
+
|
|
2063
|
+
// src/prompts/bootstrap-project.ts
|
|
2064
|
+
import { z as z25 } from "zod";
|
|
1640
2065
|
var BootstrapProjectArgsSchema = {
|
|
1641
|
-
module:
|
|
2066
|
+
module: z25.string().optional().describe(
|
|
1642
2067
|
"Optional module name to scope the analysis to (writes to .ai/modules/<module>/context.md)"
|
|
1643
2068
|
),
|
|
1644
|
-
focus:
|
|
2069
|
+
focus: z25.string().optional().describe("Optional area to emphasize (e.g. 'data layer', 'API surface')")
|
|
1645
2070
|
};
|
|
1646
2071
|
var ROOT_TEMPLATE = `# Project context
|
|
1647
2072
|
|
|
@@ -1723,10 +2148,10 @@ ${template}\`\`\`
|
|
|
1723
2148
|
}
|
|
1724
2149
|
|
|
1725
2150
|
// src/prompts/post-task.ts
|
|
1726
|
-
import { z as
|
|
2151
|
+
import { z as z26 } from "zod";
|
|
1727
2152
|
var PostTaskArgsSchema = {
|
|
1728
|
-
task_summary:
|
|
1729
|
-
files_touched:
|
|
2153
|
+
task_summary: z26.string().optional().describe("One sentence describing what you just did"),
|
|
2154
|
+
files_touched: z26.array(z26.string()).optional().describe("Files you created or modified during the task")
|
|
1730
2155
|
};
|
|
1731
2156
|
function postTaskPrompt(args, ctx) {
|
|
1732
2157
|
const taskLine = args.task_summary ? `
|
|
@@ -1808,12 +2233,12 @@ When done, respond with a brief summary: "Saved N memories: [list of IDs]. Sessi
|
|
|
1808
2233
|
}
|
|
1809
2234
|
|
|
1810
2235
|
// src/prompts/import-docs.ts
|
|
1811
|
-
import { z as
|
|
2236
|
+
import { z as z27 } from "zod";
|
|
1812
2237
|
var ImportDocsArgsSchema = {
|
|
1813
|
-
content:
|
|
1814
|
-
source:
|
|
1815
|
-
scope:
|
|
1816
|
-
dry_run:
|
|
2238
|
+
content: z27.string().describe("The documentation content to analyze and import as memories (Markdown, README, ADR, etc.)"),
|
|
2239
|
+
source: z27.string().optional().describe("Origin of the content (file path, URL, or document title) \u2014 used to anchor memories"),
|
|
2240
|
+
scope: z27.enum(["personal", "team"]).default("team").describe("Scope to assign to created memories"),
|
|
2241
|
+
dry_run: z27.boolean().default(false).describe("If true, describe what would be saved without actually calling mem_save")
|
|
1817
2242
|
};
|
|
1818
2243
|
function importDocsPrompt(args, ctx) {
|
|
1819
2244
|
const sourceLine = args.source ? `
|
|
@@ -1877,7 +2302,7 @@ When done, respond with: "Imported N memories: [list of IDs]" or "Nothing action
|
|
|
1877
2302
|
}
|
|
1878
2303
|
|
|
1879
2304
|
// src/session-tracker.ts
|
|
1880
|
-
import { loadConfig as loadConfig3 } from "@hiveai/core";
|
|
2305
|
+
import { appendUsageEvent, loadConfig as loadConfig3 } from "@hiveai/core";
|
|
1881
2306
|
var SessionTracker = class {
|
|
1882
2307
|
events = [];
|
|
1883
2308
|
startedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
@@ -1894,7 +2319,9 @@ var SessionTracker = class {
|
|
|
1894
2319
|
}
|
|
1895
2320
|
}
|
|
1896
2321
|
record(tool, summary) {
|
|
1897
|
-
|
|
2322
|
+
const event = { tool, at: (/* @__PURE__ */ new Date()).toISOString(), summary };
|
|
2323
|
+
this.events.push(event);
|
|
2324
|
+
void appendUsageEvent(this.ctx.paths, event);
|
|
1898
2325
|
}
|
|
1899
2326
|
registerShutdownHandler() {
|
|
1900
2327
|
if (this.shutdownRegistered) return;
|
|
@@ -1947,7 +2374,7 @@ function summarizeTools(events) {
|
|
|
1947
2374
|
|
|
1948
2375
|
// src/server.ts
|
|
1949
2376
|
var SERVER_NAME = "haive";
|
|
1950
|
-
var SERVER_VERSION = "0.
|
|
2377
|
+
var SERVER_VERSION = "0.5.0";
|
|
1951
2378
|
function jsonResult(data) {
|
|
1952
2379
|
return {
|
|
1953
2380
|
content: [
|
|
@@ -2156,7 +2583,10 @@ function createHaiveServer(options = {}) {
|
|
|
2156
2583
|
"RETURNS: array of { id, type, scope, status, confidence, body, match_quality }"
|
|
2157
2584
|
].join("\n"),
|
|
2158
2585
|
MemSearchInputSchema,
|
|
2159
|
-
async (input) =>
|
|
2586
|
+
async (input) => {
|
|
2587
|
+
tracker.record("mem_search", input.query.slice(0, 80));
|
|
2588
|
+
return jsonResult(await memSearch(input, context));
|
|
2589
|
+
}
|
|
2160
2590
|
);
|
|
2161
2591
|
server.tool(
|
|
2162
2592
|
"mem_for_files",
|
|
@@ -2384,6 +2814,113 @@ function createHaiveServer(options = {}) {
|
|
|
2384
2814
|
MemDeleteInputSchema,
|
|
2385
2815
|
async (input) => jsonResult(await memDelete(input, context))
|
|
2386
2816
|
);
|
|
2817
|
+
server.tool(
|
|
2818
|
+
"get_recap",
|
|
2819
|
+
[
|
|
2820
|
+
"Return ONLY the most recent session_recap. Cheaper than get_briefing when",
|
|
2821
|
+
"you just want to know 'what was I doing last time?' and don't need project",
|
|
2822
|
+
"context, modules, or memory ranking.",
|
|
2823
|
+
"",
|
|
2824
|
+
"PARAMETERS:",
|
|
2825
|
+
" scope \u2014 'personal' | 'team' | 'any' (default 'any', returns the most recent across both)",
|
|
2826
|
+
"",
|
|
2827
|
+
"RETURNS: { recap: { id, scope, revision_count, created_at, body } | null, notice? }"
|
|
2828
|
+
].join("\n"),
|
|
2829
|
+
GetRecapInputSchema,
|
|
2830
|
+
async (input) => {
|
|
2831
|
+
tracker.record("get_recap", input.scope);
|
|
2832
|
+
return jsonResult(await getRecap(input, context));
|
|
2833
|
+
}
|
|
2834
|
+
);
|
|
2835
|
+
server.tool(
|
|
2836
|
+
"mem_relevant_to",
|
|
2837
|
+
[
|
|
2838
|
+
"One-shot ranked memories for a task \u2014 use instead of get_briefing when",
|
|
2839
|
+
"project context is already loaded and you only want the relevant memory layer.",
|
|
2840
|
+
"",
|
|
2841
|
+
"Reuses the same ranking pipeline (anchor / module / literal / semantic) but",
|
|
2842
|
+
"skips project_context, modules, action_required, etc.",
|
|
2843
|
+
"",
|
|
2844
|
+
"PARAMETERS:",
|
|
2845
|
+
" task \u2014 1\u20132 sentences describing what you are about to do (required)",
|
|
2846
|
+
" files \u2014 files you'll edit (surfaces anchored memories)",
|
|
2847
|
+
" limit \u2014 cap on returned memories (default 8)",
|
|
2848
|
+
" min_semantic_score \u2014 drop weak semantic hits below this cosine (default 0.25)",
|
|
2849
|
+
"",
|
|
2850
|
+
"RETURNS: { task, search_mode, memories: [...], hints?: [...], empty?: true }"
|
|
2851
|
+
].join("\n"),
|
|
2852
|
+
MemRelevantToInputSchema,
|
|
2853
|
+
async (input) => {
|
|
2854
|
+
tracker.record("mem_relevant_to", input.task.slice(0, 80));
|
|
2855
|
+
return jsonResult(await memRelevantTo(input, context));
|
|
2856
|
+
}
|
|
2857
|
+
);
|
|
2858
|
+
server.tool(
|
|
2859
|
+
"code_search",
|
|
2860
|
+
[
|
|
2861
|
+
"Semantic search over the codebase \u2014 finds exported symbols (functions, classes,",
|
|
2862
|
+
"interfaces) related to a natural-language query. Replaces blind grep when you",
|
|
2863
|
+
"don't know the exact symbol name.",
|
|
2864
|
+
"",
|
|
2865
|
+
"Requires `haive index code-search` to have been run (builds embeddings for every",
|
|
2866
|
+
"exported symbol from the code-map). Falls back to a notice when index is missing.",
|
|
2867
|
+
"",
|
|
2868
|
+
"PARAMETERS:",
|
|
2869
|
+
" query \u2014 natural language (e.g. 'function that hashes passwords', 'JWT signing')",
|
|
2870
|
+
" k \u2014 number of top hits (default 5)",
|
|
2871
|
+
" min_score \u2014 minimum cosine similarity (default 0.2; try 0.3+ for stricter)",
|
|
2872
|
+
"",
|
|
2873
|
+
"RETURNS: { available: bool, hits: [{ file, name, kind, line, description?, score }] }"
|
|
2874
|
+
].join("\n"),
|
|
2875
|
+
CodeSearchInputSchema,
|
|
2876
|
+
async (input) => {
|
|
2877
|
+
tracker.record("code_search", input.query.slice(0, 80));
|
|
2878
|
+
return jsonResult(await codeSearch(input, context));
|
|
2879
|
+
}
|
|
2880
|
+
);
|
|
2881
|
+
server.tool(
|
|
2882
|
+
"why_this_file",
|
|
2883
|
+
[
|
|
2884
|
+
"One-shot file-context lookup: combines recent git history, memories anchored",
|
|
2885
|
+
"to the path, and the code-map entry. Answers 'why is this file the way it is?'",
|
|
2886
|
+
"in a single call instead of 3-4 manual ones.",
|
|
2887
|
+
"",
|
|
2888
|
+
"PARAMETERS:",
|
|
2889
|
+
" path \u2014 project-relative path (required)",
|
|
2890
|
+
" git_log_limit \u2014 recent commits to include (default 5)",
|
|
2891
|
+
" memory_limit \u2014 anchored memories cap (default 5)",
|
|
2892
|
+
"",
|
|
2893
|
+
"RETURNS: { file, exists, recent_commits: [...], memories: [...], code_map_entry, hints? }"
|
|
2894
|
+
].join("\n"),
|
|
2895
|
+
WhyThisFileInputSchema,
|
|
2896
|
+
async (input) => {
|
|
2897
|
+
tracker.record("why_this_file", input.path);
|
|
2898
|
+
return jsonResult(await whyThisFile(input, context));
|
|
2899
|
+
}
|
|
2900
|
+
);
|
|
2901
|
+
server.tool(
|
|
2902
|
+
"anti_patterns_check",
|
|
2903
|
+
[
|
|
2904
|
+
"Scan a diff (or set of paths) against documented attempt/gotcha memories.",
|
|
2905
|
+
"Surfaces 'you are about to repeat a known mistake' warnings BEFORE you commit.",
|
|
2906
|
+
"",
|
|
2907
|
+
"USE BEFORE finalizing a non-trivial change. Cheap and high-signal: the only",
|
|
2908
|
+
"memories scanned are 'attempt' and 'gotcha' types.",
|
|
2909
|
+
"",
|
|
2910
|
+
"PARAMETERS:",
|
|
2911
|
+
" diff \u2014 raw unified diff text (or any code snippet) \u2014 optional if `paths` provided",
|
|
2912
|
+
" paths \u2014 affected file paths (optional if `diff` provided)",
|
|
2913
|
+
" limit \u2014 cap on returned warnings (default 8)",
|
|
2914
|
+
" semantic \u2014 also use semantic search (default true; requires embeddings index)",
|
|
2915
|
+
"",
|
|
2916
|
+
"RETURNS: { scanned, warnings: [{ id, type, scope, confidence, body_preview, reasons, semantic_score? }] }"
|
|
2917
|
+
].join("\n"),
|
|
2918
|
+
AntiPatternsCheckInputSchema,
|
|
2919
|
+
async (input) => {
|
|
2920
|
+
tracker.record("anti_patterns_check", input.paths.join(",").slice(0, 80));
|
|
2921
|
+
return jsonResult(await antiPatternsCheck(input, context));
|
|
2922
|
+
}
|
|
2923
|
+
);
|
|
2387
2924
|
server.tool(
|
|
2388
2925
|
"mem_diff",
|
|
2389
2926
|
[
|
|
@@ -2440,6 +2977,13 @@ function createHaiveServer(options = {}) {
|
|
|
2440
2977
|
export {
|
|
2441
2978
|
SERVER_NAME,
|
|
2442
2979
|
SERVER_VERSION,
|
|
2443
|
-
|
|
2980
|
+
antiPatternsCheck,
|
|
2981
|
+
codeMapTool,
|
|
2982
|
+
codeSearch,
|
|
2983
|
+
createHaiveServer,
|
|
2984
|
+
getBriefing,
|
|
2985
|
+
getRecap,
|
|
2986
|
+
memRelevantTo,
|
|
2987
|
+
whyThisFile
|
|
2444
2988
|
};
|
|
2445
2989
|
//# sourceMappingURL=server.js.map
|