T-136: M2.1e — MCP copilot read_knowledge tool: read-only methodology cards + empirical findings / provider caveats lookup via injected ports in libs/research/src/mcp/tools/

This commit is contained in:
Studio 2026-12-01 06:35:32 -05:00
parent 515b685ffc
commit a98bfa8934
2 changed files with 198 additions and 35 deletions

View file

@ -313,15 +313,15 @@ function matchesAllTags(
function rankLibraryCard(
card: LibraryCardRecord,
input: ReadKnowledgeInput,
exactId: string | undefined,
normalizedQuery: string | null,
requiredLowerTags: readonly string[] | null
): number | null {
if (requiredLowerTags !== null && !matchesAllTags(card.tags, requiredLowerTags)) {
return null;
}
if (input.id !== undefined) {
return card.id === input.id ? 0 : null;
if (exactId !== undefined) {
return card.id === exactId ? 0 : null;
}
if (normalizedQuery === null) {
return 2;
@ -332,19 +332,16 @@ function rankLibraryCard(
if (!titleMatches && !tagMatches && !summaryMatches) {
return null;
}
if (card.id === input.query) {
return 0;
}
return titleMatches ? 1 : 2;
}
function rankFinding(
finding: FindingRecord,
input: ReadKnowledgeInput,
exactId: string | undefined,
normalizedQuery: string | null
): number | null {
if (input.id !== undefined) {
return finding.id === input.id ? 0 : null;
if (exactId !== undefined) {
return finding.id === exactId ? 0 : null;
}
if (normalizedQuery === null) {
return 2;
@ -354,23 +351,20 @@ function rankFinding(
if (!titleMatches && !summaryMatches) {
return null;
}
if (finding.id === input.query) {
return 0;
}
return titleMatches ? 1 : 2;
}
function rankProviderCaveat(
caveat: ProviderCaveatRecord,
input: ReadKnowledgeInput,
exactId: string | undefined,
normalizedQuery: string | null,
normalizedProvider: string | null
): number | null {
if (normalizedProvider !== null && caveat.provider.toLowerCase() !== normalizedProvider) {
return null;
}
if (input.id !== undefined) {
return caveat.id === input.id ? 0 : null;
if (exactId !== undefined) {
return caveat.id === exactId ? 0 : null;
}
if (normalizedQuery === null) {
return 2;
@ -381,9 +375,6 @@ function rankProviderCaveat(
if (!datasetMatches && !providerMatches && !caveatMatches) {
return null;
}
if (caveat.id === input.query) {
return 0;
}
return datasetMatches ? 1 : 2;
}
@ -479,18 +470,20 @@ export function createReadKnowledgeTool(options: ReadKnowledgeToolOptions): McpT
const wantFindings = input.source === 'all' || input.source === 'findings';
const wantCaveats = input.source === 'all' || input.source === 'provider_caveats';
const baseQuery: FindingQuery =
input.id !== undefined
? { id: input.id }
: input.query !== undefined
? { query: input.query }
: {};
const libraryQuery: LibraryCardQuery = {
...(input.id !== undefined ? { id: input.id } : {}),
...(input.query !== undefined ? { query: input.query } : {}),
...baseQuery,
...(input.tags !== undefined ? { tags: input.tags } : {}),
};
const findingQuery: FindingQuery = {
...(input.id !== undefined ? { id: input.id } : {}),
...(input.query !== undefined ? { query: input.query } : {}),
};
const findingQuery: FindingQuery = baseQuery;
const caveatQuery: ProviderCaveatQuery = {
...(input.id !== undefined ? { id: input.id } : {}),
...(input.query !== undefined ? { query: input.query } : {}),
...baseQuery,
...(input.provider !== undefined ? { provider: input.provider } : {}),
};
@ -515,7 +508,7 @@ export function createReadKnowledgeTool(options: ReadKnowledgeToolOptions): McpT
}
for (let i = 0; i < rawLibrary.length; i += 1) {
const card = validateLibraryCard(rawLibrary[i] as LibraryCardRecord);
const rank = rankLibraryCard(card, input, normalizedQuery, requiredLowerTags);
const rank = rankLibraryCard(card, input.id, normalizedQuery, requiredLowerTags);
if (rank !== null) {
eligible.push({ rank, row: projectLibraryCard(card) });
}
@ -528,7 +521,7 @@ export function createReadKnowledgeTool(options: ReadKnowledgeToolOptions): McpT
}
for (let i = 0; i < rawFindings.length; i += 1) {
const finding = validateFinding(rawFindings[i] as FindingRecord);
const rank = rankFinding(finding, input, normalizedQuery);
const rank = rankFinding(finding, input.id, normalizedQuery);
if (rank !== null) {
eligible.push({ rank, row: projectFinding(finding) });
}
@ -541,7 +534,7 @@ export function createReadKnowledgeTool(options: ReadKnowledgeToolOptions): McpT
}
for (let i = 0; i < rawCaveats.length; i += 1) {
const caveat = validateProviderCaveat(rawCaveats[i] as ProviderCaveatRecord);
const rank = rankProviderCaveat(caveat, input, normalizedQuery, normalizedProvider);
const rank = rankProviderCaveat(caveat, input.id, normalizedQuery, normalizedProvider);
if (rank !== null) {
eligible.push({ rank, row: projectProviderCaveat(caveat) });
}