docorbit 0.1.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/LICENSE +21 -0
- package/README.md +660 -0
- package/apps/cli/bin/docorbit.js +8 -0
- package/apps/cli/src/commands/add.ts +44 -0
- package/apps/cli/src/commands/api.ts +38 -0
- package/apps/cli/src/commands/context.ts +47 -0
- package/apps/cli/src/commands/dashboard.ts +55 -0
- package/apps/cli/src/commands/diff.ts +30 -0
- package/apps/cli/src/commands/evaluate.ts +133 -0
- package/apps/cli/src/commands/examples.ts +39 -0
- package/apps/cli/src/commands/export.ts +89 -0
- package/apps/cli/src/commands/impact.ts +31 -0
- package/apps/cli/src/commands/init.ts +69 -0
- package/apps/cli/src/commands/inspect.ts +30 -0
- package/apps/cli/src/commands/mcp.ts +72 -0
- package/apps/cli/src/commands/pitfalls.ts +38 -0
- package/apps/cli/src/commands/recipes.ts +35 -0
- package/apps/cli/src/commands/search.ts +48 -0
- package/apps/cli/src/commands/update.ts +73 -0
- package/apps/cli/src/commands/verify.ts +48 -0
- package/apps/cli/src/formatters/colors.ts +23 -0
- package/apps/cli/src/formatters/inspection.ts +102 -0
- package/apps/cli/src/formatters/knowledge.ts +272 -0
- package/apps/cli/src/formatters/retrieval.ts +74 -0
- package/apps/cli/src/formatters/terminal.ts +6 -0
- package/apps/cli/src/formatters/verification.ts +126 -0
- package/apps/cli/src/index.ts +409 -0
- package/bin/docorbit.js +8 -0
- package/package.json +46 -0
- package/packages/core/src/dashboard/server.ts +314 -0
- package/packages/core/src/dashboard/ui.ts +586 -0
- package/packages/core/src/implementation-service.ts +451 -0
- package/packages/core/src/index.ts +7 -0
- package/packages/core/src/inspector.ts +71 -0
- package/packages/core/src/pipeline.ts +331 -0
- package/packages/crawler/src/config.ts +12 -0
- package/packages/crawler/src/fetcher.ts +185 -0
- package/packages/crawler/src/index.ts +2 -0
- package/packages/discovery/src/index.ts +31 -0
- package/packages/discovery/src/provider.ts +47 -0
- package/packages/discovery/src/providers/generic.ts +98 -0
- package/packages/discovery/src/providers/github.ts +61 -0
- package/packages/discovery/src/providers/llms-txt.ts +73 -0
- package/packages/discovery/src/providers/markdown.ts +48 -0
- package/packages/discovery/src/providers/openapi.ts +91 -0
- package/packages/discovery/src/providers/sitemap.ts +62 -0
- package/packages/discovery/src/providers/skill.ts +54 -0
- package/packages/discovery/src/ranker.ts +123 -0
- package/packages/evaluation/src/dataset.ts +963 -0
- package/packages/evaluation/src/index.ts +8 -0
- package/packages/evaluation/src/runner.ts +241 -0
- package/packages/evaluation/src/strategies/context7-runner.ts +269 -0
- package/packages/evaluation/src/strategies/docorbit-runner.ts +228 -0
- package/packages/evaluation/src/strategies/firecrawl-runner.ts +172 -0
- package/packages/evaluation/src/strategies/web-search-runner.ts +194 -0
- package/packages/evaluation/src/types.ts +34 -0
- package/packages/evaluation/src/version-matcher.ts +73 -0
- package/packages/export/src/agents-md.ts +200 -0
- package/packages/export/src/claude-md.ts +141 -0
- package/packages/export/src/docs-map.ts +150 -0
- package/packages/export/src/index.ts +6 -0
- package/packages/export/src/llms-txt.ts +96 -0
- package/packages/export/src/service.ts +250 -0
- package/packages/export/src/skill-md.ts +128 -0
- package/packages/mcp/src/index.ts +46 -0
- package/packages/mcp/src/resources/index.ts +189 -0
- package/packages/mcp/src/server.ts +278 -0
- package/packages/mcp/src/tools/analyze-impact.ts +74 -0
- package/packages/mcp/src/tools/check-api.ts +86 -0
- package/packages/mcp/src/tools/diff-docs.ts +68 -0
- package/packages/mcp/src/tools/export-context.ts +73 -0
- package/packages/mcp/src/tools/find-api.ts +99 -0
- package/packages/mcp/src/tools/find-example.ts +100 -0
- package/packages/mcp/src/tools/find-pitfall.ts +94 -0
- package/packages/mcp/src/tools/find-recipe.ts +98 -0
- package/packages/mcp/src/tools/get-doc.ts +130 -0
- package/packages/mcp/src/tools/get-docs-map.ts +64 -0
- package/packages/mcp/src/tools/get-version.ts +118 -0
- package/packages/mcp/src/tools/implementation-context.ts +88 -0
- package/packages/mcp/src/tools/index.ts +59 -0
- package/packages/mcp/src/tools/list-sources.ts +85 -0
- package/packages/mcp/src/tools/search-docs.ts +123 -0
- package/packages/mcp/src/tools/types.ts +28 -0
- package/packages/mcp/src/transports/http.ts +256 -0
- package/packages/mcp/src/transports/stdio.ts +105 -0
- package/packages/mcp/src/transports/types.ts +6 -0
- package/packages/mcp/src/types.ts +102 -0
- package/packages/normalizer/src/example-indexer.ts +240 -0
- package/packages/normalizer/src/html.ts +253 -0
- package/packages/normalizer/src/index.ts +8 -0
- package/packages/normalizer/src/llms.ts +83 -0
- package/packages/normalizer/src/openapi/endpoint-parser.ts +406 -0
- package/packages/normalizer/src/openapi/schema-resolver.ts +111 -0
- package/packages/normalizer/src/openapi.ts +2 -0
- package/packages/normalizer/src/page.ts +184 -0
- package/packages/normalizer/src/pitfall-extractor.ts +190 -0
- package/packages/normalizer/src/slicer.ts +455 -0
- package/packages/retrieval/src/engine.ts +120 -0
- package/packages/retrieval/src/index.ts +7 -0
- package/packages/retrieval/src/intent.ts +43 -0
- package/packages/retrieval/src/packer.ts +145 -0
- package/packages/retrieval/src/recipe-engine.ts +313 -0
- package/packages/retrieval/src/scorer.ts +139 -0
- package/packages/retrieval/src/weights.ts +31 -0
- package/packages/security/src/annotations.ts +112 -0
- package/packages/security/src/index.ts +2 -0
- package/packages/security/src/ssrf.ts +153 -0
- package/packages/shared/src/errors.ts +53 -0
- package/packages/shared/src/hashing.ts +23 -0
- package/packages/shared/src/index.ts +3 -0
- package/packages/shared/src/types.ts +881 -0
- package/packages/storage/src/db.ts +72 -0
- package/packages/storage/src/index.ts +11 -0
- package/packages/storage/src/interfaces.ts +115 -0
- package/packages/storage/src/repositories/api-repository.ts +219 -0
- package/packages/storage/src/repositories/chunk-repository.ts +316 -0
- package/packages/storage/src/repositories/example-repository.ts +206 -0
- package/packages/storage/src/repositories/page-repository.ts +205 -0
- package/packages/storage/src/repositories/pitfall-repository.ts +188 -0
- package/packages/storage/src/repositories/source-repository.ts +205 -0
- package/packages/storage/src/repository.ts +256 -0
- package/packages/storage/src/schema.ts +269 -0
- package/packages/storage/src/search-tokens.ts +28 -0
- package/packages/verification/src/diff-engine.ts +258 -0
- package/packages/verification/src/extractor.ts +339 -0
- package/packages/verification/src/impact-scanner.ts +203 -0
- package/packages/verification/src/index.ts +5 -0
- package/packages/verification/src/services.ts +238 -0
- package/packages/verification/src/verifier.ts +375 -0
- package/packages/workspace/src/detector.ts +143 -0
- package/packages/workspace/src/ecosystems/cargo.ts +84 -0
- package/packages/workspace/src/ecosystems/composer.ts +42 -0
- package/packages/workspace/src/ecosystems/go.ts +54 -0
- package/packages/workspace/src/ecosystems/index.ts +34 -0
- package/packages/workspace/src/ecosystems/maven.ts +34 -0
- package/packages/workspace/src/ecosystems/npm.ts +83 -0
- package/packages/workspace/src/ecosystems/pub.ts +40 -0
- package/packages/workspace/src/ecosystems/pypi.ts +100 -0
- package/packages/workspace/src/ecosystems/rubygems.ts +30 -0
- package/packages/workspace/src/ecosystems/types.ts +18 -0
- package/packages/workspace/src/index.ts +5 -0
- package/packages/workspace/src/lockfile.ts +194 -0
- package/packages/workspace/src/resolver.ts +234 -0
- package/packages/workspace/src/semver.ts +259 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SearchResult,
|
|
3
|
+
DocumentChunk,
|
|
4
|
+
ContextPackage,
|
|
5
|
+
ContextOptions,
|
|
6
|
+
QueryIntent,
|
|
7
|
+
} from '../../shared/src/index.ts';
|
|
8
|
+
|
|
9
|
+
export function packContext(
|
|
10
|
+
task: string,
|
|
11
|
+
candidates: SearchResult[],
|
|
12
|
+
detectedIntent: QueryIntent,
|
|
13
|
+
options: ContextOptions = {}
|
|
14
|
+
): ContextPackage {
|
|
15
|
+
const tokenBudget = options.tokenBudget || 3000;
|
|
16
|
+
const maxChunks = options.maxChunks || 15;
|
|
17
|
+
const redundancyPenalty = options.redundancyPenalty ?? 0.35;
|
|
18
|
+
|
|
19
|
+
const selectedChunks: DocumentChunk[] = [];
|
|
20
|
+
const selectedChunkIds = new Set<string>();
|
|
21
|
+
const selectedSectionCounts = new Map<string, number>();
|
|
22
|
+
const selectedPages = new Set<string>();
|
|
23
|
+
const sourcesSet = new Set<string>();
|
|
24
|
+
const warnings: string[] = [];
|
|
25
|
+
|
|
26
|
+
let totalEstimatedTokens = 0;
|
|
27
|
+
const remainingCandidates = [...candidates];
|
|
28
|
+
|
|
29
|
+
while (remainingCandidates.length > 0 && selectedChunks.length < maxChunks) {
|
|
30
|
+
let bestIdx = -1;
|
|
31
|
+
let bestUtility = -Infinity;
|
|
32
|
+
|
|
33
|
+
for (let i = 0; i < remainingCandidates.length; i++) {
|
|
34
|
+
const candidate = remainingCandidates[i];
|
|
35
|
+
const chunk = candidate.chunk;
|
|
36
|
+
|
|
37
|
+
// Check if it strictly fits within the remaining token budget
|
|
38
|
+
if (totalEstimatedTokens + chunk.tokenEstimate > tokenBudget) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
const sectionKey = `${chunk.pageId}:${chunk.sectionPath.join(' > ')}`;
|
|
44
|
+
const sectionOverlap = selectedSectionCounts.get(sectionKey) || 0;
|
|
45
|
+
|
|
46
|
+
// Penalize multiple chunks from the exact same section to encourage diversity/coverage
|
|
47
|
+
const redundancyFactor = Math.pow(1 - redundancyPenalty, sectionOverlap);
|
|
48
|
+
|
|
49
|
+
// Reward discovering new pages / sections (coverage bonus)
|
|
50
|
+
const coverageMultiplier = selectedPages.has(chunk.pageId) ? 1.0 : 1.25;
|
|
51
|
+
|
|
52
|
+
const utility = candidate.score * redundancyFactor * coverageMultiplier;
|
|
53
|
+
|
|
54
|
+
if (utility > bestUtility) {
|
|
55
|
+
bestUtility = utility;
|
|
56
|
+
bestIdx = i;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (bestIdx === -1) {
|
|
61
|
+
// No remaining candidate fits within the remaining token budget
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const chosen = remainingCandidates.splice(bestIdx, 1)[0];
|
|
66
|
+
const chunk = chosen.chunk;
|
|
67
|
+
|
|
68
|
+
selectedChunks.push(chunk);
|
|
69
|
+
selectedChunkIds.add(chunk.id);
|
|
70
|
+
|
|
71
|
+
const sectionKey = `${chunk.pageId}:${chunk.sectionPath.join(' > ')}`;
|
|
72
|
+
selectedSectionCounts.set(sectionKey, (selectedSectionCounts.get(sectionKey) || 0) + 1);
|
|
73
|
+
selectedPages.add(chunk.pageId);
|
|
74
|
+
|
|
75
|
+
if (chunk.provenance?.targetUrl) {
|
|
76
|
+
sourcesSet.add(chunk.provenance.targetUrl);
|
|
77
|
+
} else if (chunk.provenance?.sourceUrl) {
|
|
78
|
+
sourcesSet.add(chunk.provenance.sourceUrl);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
totalEstimatedTokens += chunk.tokenEstimate;
|
|
82
|
+
|
|
83
|
+
// If chunk is a warning, register it in top-level warnings
|
|
84
|
+
if (chunk.chunkType === 'warning') {
|
|
85
|
+
warnings.push(`[${chunk.sectionPath.join(' > ')}]: ${chunk.content.slice(0, 150)}...`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Group chunks by page in order of page first appearance in selectedChunks (most relevant page first)
|
|
90
|
+
const pageOrder = new Map<string, number>();
|
|
91
|
+
for (let i = 0; i < selectedChunks.length; i++) {
|
|
92
|
+
const pId = selectedChunks[i].pageId;
|
|
93
|
+
if (!pageOrder.has(pId)) {
|
|
94
|
+
pageOrder.set(pId, pageOrder.size);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const sortedChunks = [...selectedChunks].sort((a, b) => {
|
|
99
|
+
const pOrderA = pageOrder.get(a.pageId) ?? 0;
|
|
100
|
+
const pOrderB = pageOrder.get(b.pageId) ?? 0;
|
|
101
|
+
if (pOrderA !== pOrderB) return pOrderA - pOrderB;
|
|
102
|
+
return a.ordinal - b.ordinal;
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Build agent-facing Markdown package
|
|
106
|
+
const lines: string[] = [];
|
|
107
|
+
lines.push(`# Context Package: ${task}`);
|
|
108
|
+
lines.push(`- **Detected Intent**: \`${detectedIntent}\``);
|
|
109
|
+
lines.push(`- **Estimated Tokens**: ~${totalEstimatedTokens} / ${tokenBudget} tokens (heuristic estimate)`);
|
|
110
|
+
lines.push(`- **Sources Included**: ${sourcesSet.size > 0 ? Array.from(sourcesSet).map(s => `[${s}](${s})`).join(', ') : 'Local documentation'}`);
|
|
111
|
+
|
|
112
|
+
if (warnings.length > 0) {
|
|
113
|
+
lines.push('\n> [!WARNING]');
|
|
114
|
+
lines.push('> **Relevant Advisories & Caveats**:');
|
|
115
|
+
for (const w of warnings) {
|
|
116
|
+
lines.push(`> - ${w}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
lines.push('\n---\n');
|
|
121
|
+
|
|
122
|
+
for (const chunk of sortedChunks) {
|
|
123
|
+
const breadcrumb = chunk.sectionPath.length > 0 ? chunk.sectionPath.join(' > ') : (chunk.title || 'General');
|
|
124
|
+
lines.push(`### ${breadcrumb}`);
|
|
125
|
+
lines.push(`*Type: \`${chunk.chunkType}\` | Est. Tokens: ~${chunk.tokenEstimate}*`);
|
|
126
|
+
lines.push('\n' + chunk.content.trim() + '\n');
|
|
127
|
+
if (chunk.provenance?.sourceUrl) {
|
|
128
|
+
lines.push(`*Source: ${chunk.provenance.sourceUrl}*\n`);
|
|
129
|
+
}
|
|
130
|
+
lines.push('---\n');
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const markdown = lines.join('\n');
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
task,
|
|
137
|
+
detectedIntent,
|
|
138
|
+
totalEstimatedTokens,
|
|
139
|
+
tokenBudget,
|
|
140
|
+
chunks: sortedChunks,
|
|
141
|
+
markdown,
|
|
142
|
+
sources: Array.from(sourcesSet),
|
|
143
|
+
warnings,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import type {
|
|
3
|
+
Recipe,
|
|
4
|
+
RecipeStep,
|
|
5
|
+
RecipeValidationStep,
|
|
6
|
+
EvidenceLevel,
|
|
7
|
+
ApiEndpoint,
|
|
8
|
+
IndexedExample,
|
|
9
|
+
Pitfall,
|
|
10
|
+
} from '../../shared/src/index.ts';
|
|
11
|
+
import { DocOrbitRepository } from '../../storage/src/index.ts';
|
|
12
|
+
import { RetrievalEngine } from './engine.ts';
|
|
13
|
+
import { resolveProjectContext } from '../../workspace/src/index.ts';
|
|
14
|
+
|
|
15
|
+
export interface RecipeOptions {
|
|
16
|
+
docVersion?: string;
|
|
17
|
+
projectDir?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class RecipeEngine {
|
|
21
|
+
private repository: DocOrbitRepository;
|
|
22
|
+
private retrievalEngine: RetrievalEngine;
|
|
23
|
+
|
|
24
|
+
constructor(repository: DocOrbitRepository, retrievalEngine?: RetrievalEngine) {
|
|
25
|
+
this.repository = repository;
|
|
26
|
+
this.retrievalEngine = retrievalEngine || new RetrievalEngine(repository);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Deterministically compiles an implementation recipe grounded strictly in indexed evidence.
|
|
31
|
+
* Distinguishes documented facts, inferred relationships, and missing information.
|
|
32
|
+
* Never invents APIs, steps, prerequisites, or validation steps.
|
|
33
|
+
*/
|
|
34
|
+
async assembleRecipe(goal: string, options: RecipeOptions = {}): Promise<Recipe> {
|
|
35
|
+
const cleanGoal = goal.trim();
|
|
36
|
+
let effectiveDocVersion = options.docVersion;
|
|
37
|
+
|
|
38
|
+
// Resolve project and version context if projectDir is provided
|
|
39
|
+
if (options.projectDir) {
|
|
40
|
+
const projContext = resolveProjectContext(options.projectDir, cleanGoal, this.repository);
|
|
41
|
+
if (projContext.matchedDependency && !effectiveDocVersion) {
|
|
42
|
+
effectiveDocVersion = projContext.matchedDependency.targetDocVersion;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// 1. Query candidate API endpoints, examples, pitfalls, and search chunks
|
|
47
|
+
const endpoints = this.repository.searchApiEndpoints(cleanGoal, {
|
|
48
|
+
docVersion: effectiveDocVersion,
|
|
49
|
+
limit: 10,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const examples = this.repository.searchIndexedExamples(cleanGoal, {
|
|
53
|
+
docVersion: effectiveDocVersion,
|
|
54
|
+
limit: 10,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const pitfalls = this.repository.searchPitfalls(cleanGoal, {
|
|
58
|
+
docVersion: effectiveDocVersion,
|
|
59
|
+
limit: 10,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const searchResults = await this.retrievalEngine.search(cleanGoal, {
|
|
63
|
+
docVersion: effectiveDocVersion,
|
|
64
|
+
limit: 10,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// 2. Collect unique source URLs
|
|
68
|
+
const sourcesSet = new Set<string>();
|
|
69
|
+
for (const ep of endpoints) {
|
|
70
|
+
if (ep.provenance?.sourceUrl) sourcesSet.add(ep.provenance.sourceUrl);
|
|
71
|
+
}
|
|
72
|
+
for (const ex of examples) {
|
|
73
|
+
if (ex.sourceUrl) sourcesSet.add(ex.sourceUrl);
|
|
74
|
+
}
|
|
75
|
+
for (const pf of pitfalls) {
|
|
76
|
+
if (pf.provenance?.sourceUrl) sourcesSet.add(pf.provenance.sourceUrl);
|
|
77
|
+
}
|
|
78
|
+
for (const sr of searchResults) {
|
|
79
|
+
if (sr.chunk.provenance?.sourceUrl) sourcesSet.add(sr.chunk.provenance.sourceUrl);
|
|
80
|
+
}
|
|
81
|
+
const sources = Array.from(sourcesSet);
|
|
82
|
+
|
|
83
|
+
// 3. Assemble Prerequisites strictly from documented requirements
|
|
84
|
+
const prerequisites: Array<{ text: string; evidenceLevel: EvidenceLevel; sourceUrl?: string }> = [];
|
|
85
|
+
|
|
86
|
+
// Check required_config or permission pitfalls
|
|
87
|
+
for (const pf of pitfalls) {
|
|
88
|
+
if (pf.kind === 'required_config' || pf.kind === 'permission') {
|
|
89
|
+
prerequisites.push({
|
|
90
|
+
text: `${pf.title}: ${pf.content}`,
|
|
91
|
+
evidenceLevel: 'documented_fact',
|
|
92
|
+
sourceUrl: pf.provenance?.sourceUrl,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Check chunks with prerequisite/setup headings (only relevant chunks with score >= 5.0)
|
|
98
|
+
for (const sr of searchResults.filter(s => s.score >= 5.0)) {
|
|
99
|
+
const headingText = (sr.chunk.title || sr.chunk.sectionPath.join(' ')).toLowerCase();
|
|
100
|
+
if (headingText.includes('prerequisite') || headingText.includes('before you begin') || headingText.includes('requirements')) {
|
|
101
|
+
const textSnippet = sr.chunk.content.split('\n')[0].trim();
|
|
102
|
+
if (textSnippet && !prerequisites.some(p => p.text === textSnippet)) {
|
|
103
|
+
prerequisites.push({
|
|
104
|
+
text: textSnippet,
|
|
105
|
+
evidenceLevel: 'documented_fact',
|
|
106
|
+
sourceUrl: sr.chunk.provenance?.sourceUrl,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 4. Assemble Ordered Steps
|
|
113
|
+
const orderedSteps: RecipeStep[] = [];
|
|
114
|
+
const usedApis = new Set<string>();
|
|
115
|
+
const usedExamples = new Set<string>();
|
|
116
|
+
const usedPitfalls = new Set<string>();
|
|
117
|
+
let stepNumber = 1;
|
|
118
|
+
|
|
119
|
+
// A. Setup / Client Initialization Step (if documented in examples)
|
|
120
|
+
const initExample = examples.find(ex =>
|
|
121
|
+
/(?:new\s+\w+|initialize|config(?:ure)?|createClient|express\(\)|FastAPI\()/i.test(ex.code)
|
|
122
|
+
);
|
|
123
|
+
if (initExample) {
|
|
124
|
+
orderedSteps.push({
|
|
125
|
+
step: stepNumber++,
|
|
126
|
+
title: `Initialize Client / Service (${initExample.framework || initExample.language})`,
|
|
127
|
+
description: `Configure and initialize client instance as documented in ${initExample.task}`,
|
|
128
|
+
evidenceLevel: 'documented_fact',
|
|
129
|
+
exampleCode: initExample.code,
|
|
130
|
+
sourceUrl: initExample.sourceUrl,
|
|
131
|
+
sourceChunkIds: initExample.chunkId ? [initExample.chunkId] : undefined,
|
|
132
|
+
});
|
|
133
|
+
usedExamples.add(initExample.id);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// B. Primary API Actions (Documented Fact)
|
|
137
|
+
for (const ep of endpoints) {
|
|
138
|
+
const apiStr = `${ep.method.toUpperCase()} ${ep.path}`;
|
|
139
|
+
if (usedApis.has(apiStr)) continue;
|
|
140
|
+
usedApis.add(apiStr);
|
|
141
|
+
|
|
142
|
+
// Find matching code example for this API
|
|
143
|
+
const matchingEx = examples.find(ex =>
|
|
144
|
+
!usedExamples.has(ex.id) &&
|
|
145
|
+
(ex.relatedApi === apiStr || ex.code.includes(ep.path) || (ep.operationId && ex.code.includes(ep.operationId)))
|
|
146
|
+
);
|
|
147
|
+
if (matchingEx) usedExamples.add(matchingEx.id);
|
|
148
|
+
|
|
149
|
+
// Find matching pitfalls for this API
|
|
150
|
+
const matchingPfs = pitfalls.filter(pf =>
|
|
151
|
+
pf.relatedApi === apiStr ||
|
|
152
|
+
(ep.deprecated && pf.kind === 'deprecated') ||
|
|
153
|
+
(pf.kind === 'rate_limit') ||
|
|
154
|
+
(pf.kind === 'breaking_change')
|
|
155
|
+
);
|
|
156
|
+
const stepPitfalls: string[] = [];
|
|
157
|
+
for (const p of matchingPfs) {
|
|
158
|
+
const pfStr = `[${p.kind}] ${p.title}: ${p.content}`;
|
|
159
|
+
stepPitfalls.push(pfStr);
|
|
160
|
+
usedPitfalls.add(pfStr);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// If the endpoint is deprecated, explicitly highlight it in step title/description
|
|
164
|
+
const depWarning = ep.deprecated ? ' [DEPRECATED]' : '';
|
|
165
|
+
|
|
166
|
+
orderedSteps.push({
|
|
167
|
+
step: stepNumber++,
|
|
168
|
+
title: `${apiStr}${depWarning}${ep.summary ? ` - ${ep.summary}` : ''}`,
|
|
169
|
+
description: ep.description || ep.summary || `Execute documented ${apiStr} request with parameters: ${ep.parameters.map(p => `${p.name}${p.required ? ' (required)' : ''}`).join(', ')}`,
|
|
170
|
+
evidenceLevel: 'documented_fact',
|
|
171
|
+
apiEndpoint: apiStr,
|
|
172
|
+
exampleCode: matchingEx?.code,
|
|
173
|
+
pitfalls: stepPitfalls.length > 0 ? stepPitfalls : undefined,
|
|
174
|
+
sourceUrl: ep.provenance?.sourceUrl || matchingEx?.sourceUrl,
|
|
175
|
+
sourceChunkIds: matchingEx?.chunkId ? [matchingEx.chunkId] : undefined,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// C. Remaining Code Examples as implementation steps
|
|
180
|
+
for (const ex of examples) {
|
|
181
|
+
if (usedExamples.has(ex.id)) continue;
|
|
182
|
+
usedExamples.add(ex.id);
|
|
183
|
+
|
|
184
|
+
const matchingPfs = pitfalls.filter(pf =>
|
|
185
|
+
(ex.relatedApi && pf.relatedApi === ex.relatedApi) ||
|
|
186
|
+
(ex.relatedSymbol && pf.relatedSymbol === ex.relatedSymbol)
|
|
187
|
+
);
|
|
188
|
+
const stepPitfalls = matchingPfs.map(p => `[${p.kind}] ${p.title}: ${p.content}`);
|
|
189
|
+
stepPitfalls.forEach(p => usedPitfalls.add(p));
|
|
190
|
+
|
|
191
|
+
orderedSteps.push({
|
|
192
|
+
step: stepNumber++,
|
|
193
|
+
title: ex.task,
|
|
194
|
+
description: `Implement documented code example for ${ex.task} (${ex.language}${ex.framework ? ` / ${ex.framework}` : ''})`,
|
|
195
|
+
evidenceLevel: 'documented_fact',
|
|
196
|
+
apiEndpoint: ex.relatedApi,
|
|
197
|
+
exampleCode: ex.code,
|
|
198
|
+
pitfalls: stepPitfalls.length > 0 ? stepPitfalls : undefined,
|
|
199
|
+
sourceUrl: ex.sourceUrl,
|
|
200
|
+
sourceChunkIds: ex.chunkId ? [ex.chunkId] : undefined,
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
if (ex.relatedApi) usedApis.add(ex.relatedApi);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// D. If neither endpoints nor examples existed, check procedural chunks with strong relevance
|
|
207
|
+
if (orderedSteps.length === 0) {
|
|
208
|
+
for (const sr of searchResults.filter(s => s.score >= 5.0).slice(0, 3)) {
|
|
209
|
+
orderedSteps.push({
|
|
210
|
+
step: stepNumber++,
|
|
211
|
+
title: sr.chunk.title || sr.chunk.sectionPath.join(' > ') || 'Documented Procedure',
|
|
212
|
+
description: sr.chunk.content.substring(0, 300),
|
|
213
|
+
evidenceLevel: 'inferred_relationship',
|
|
214
|
+
sourceUrl: sr.chunk.provenance?.sourceUrl,
|
|
215
|
+
sourceChunkIds: [sr.chunk.id],
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// E. If documentation contains no relevant implementation steps, mark missing_information
|
|
221
|
+
if (orderedSteps.length === 0) {
|
|
222
|
+
orderedSteps.push({
|
|
223
|
+
step: stepNumber++,
|
|
224
|
+
title: 'Missing Implementation Knowledge',
|
|
225
|
+
description: `The indexed documentation does not contain documented API endpoints, code examples, or procedures for: "${cleanGoal}".`,
|
|
226
|
+
evidenceLevel: 'missing_information',
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// 5. Assemble Evidence-Based Validation Steps
|
|
231
|
+
const validationSteps: RecipeValidationStep[] = [];
|
|
232
|
+
let valStepNumber = 1;
|
|
233
|
+
|
|
234
|
+
// Validation from API response schemas
|
|
235
|
+
for (const ep of endpoints) {
|
|
236
|
+
const okResponse = ep.responseSchema?.['200'] || ep.responseSchema?.['201'];
|
|
237
|
+
if (okResponse && okResponse.schema) {
|
|
238
|
+
const props = okResponse.schema.properties ? Object.keys(okResponse.schema.properties) : [];
|
|
239
|
+
const propsDesc = props.length > 0 ? ` Expected properties: [${props.slice(0, 6).join(', ')}]` : '';
|
|
240
|
+
const exampleStr = okResponse.example ? JSON.stringify(okResponse.example, null, 2) : undefined;
|
|
241
|
+
|
|
242
|
+
validationSteps.push({
|
|
243
|
+
step: valStepNumber++,
|
|
244
|
+
description: `Verify ${ep.method.toUpperCase()} ${ep.path} returns HTTP ${okResponse.statusCode}.${propsDesc}`,
|
|
245
|
+
evidenceLevel: 'documented_fact',
|
|
246
|
+
expectedResponse: exampleStr || (props.length > 0 ? JSON.stringify(props) : undefined),
|
|
247
|
+
sourceUrl: ep.provenance?.sourceUrl,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Validation from code examples containing verification/assertion patterns
|
|
253
|
+
for (const ex of examples) {
|
|
254
|
+
if (/(?:constructEvent|verifySignature|assert|expect|validate|signature)/i.test(ex.code)) {
|
|
255
|
+
validationSteps.push({
|
|
256
|
+
step: valStepNumber++,
|
|
257
|
+
description: `Validate event/data integrity using documented pattern from ${ex.task}`,
|
|
258
|
+
evidenceLevel: 'documented_fact',
|
|
259
|
+
testPattern: ex.code,
|
|
260
|
+
sourceUrl: ex.sourceUrl,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// If no validation steps can be derived from evidence, do NOT invent any!
|
|
266
|
+
if (validationSteps.length === 0) {
|
|
267
|
+
validationSteps.push({
|
|
268
|
+
step: 1,
|
|
269
|
+
description: 'No response schemas, test instructions, or verification assertions are documented for this task.',
|
|
270
|
+
evidenceLevel: 'missing_information',
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Collect all pitfalls
|
|
275
|
+
for (const pf of pitfalls) {
|
|
276
|
+
const pfStr = `[${pf.kind}] ${pf.title}: ${pf.content}`;
|
|
277
|
+
usedPitfalls.add(pfStr);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Calculate confidence score strictly based on evidence
|
|
281
|
+
let confidence = 0.0;
|
|
282
|
+
const factSteps = orderedSteps.filter(s => s.evidenceLevel === 'documented_fact').length;
|
|
283
|
+
const totalSteps = orderedSteps.length;
|
|
284
|
+
|
|
285
|
+
if (totalSteps > 0 && factSteps > 0) {
|
|
286
|
+
confidence += 0.4 * (factSteps / totalSteps);
|
|
287
|
+
if (endpoints.length > 0) confidence += 0.3;
|
|
288
|
+
if (examples.length > 0) confidence += 0.2;
|
|
289
|
+
if (validationSteps.some(v => v.evidenceLevel === 'documented_fact')) confidence += 0.1;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
confidence = Math.min(1.0, Math.round(confidence * 100) / 100);
|
|
293
|
+
|
|
294
|
+
const recipeId = createHash('sha256')
|
|
295
|
+
.update(`${cleanGoal}:${effectiveDocVersion || 'default'}:${orderedSteps.length}`)
|
|
296
|
+
.digest('hex')
|
|
297
|
+
.substring(0, 16);
|
|
298
|
+
|
|
299
|
+
return {
|
|
300
|
+
id: recipeId,
|
|
301
|
+
goal: cleanGoal,
|
|
302
|
+
prerequisites,
|
|
303
|
+
orderedSteps,
|
|
304
|
+
requiredApis: Array.from(usedApis),
|
|
305
|
+
examples: Array.from(usedExamples),
|
|
306
|
+
pitfalls: Array.from(usedPitfalls),
|
|
307
|
+
validationSteps,
|
|
308
|
+
sources,
|
|
309
|
+
docVersion: effectiveDocVersion,
|
|
310
|
+
confidence,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DocumentChunk,
|
|
3
|
+
SymbolReference,
|
|
4
|
+
ChunkCode,
|
|
5
|
+
ScoringWeights,
|
|
6
|
+
QueryIntent,
|
|
7
|
+
SourceAuthority,
|
|
8
|
+
SearchResult,
|
|
9
|
+
} from '../../shared/src/index.ts';
|
|
10
|
+
|
|
11
|
+
export interface ScoreCandidateInput {
|
|
12
|
+
chunk: DocumentChunk;
|
|
13
|
+
ftsRank: number;
|
|
14
|
+
symbols: SymbolReference[];
|
|
15
|
+
codeSnippets: ChunkCode[];
|
|
16
|
+
query: string;
|
|
17
|
+
intent: QueryIntent;
|
|
18
|
+
authority?: SourceAuthority;
|
|
19
|
+
targetDocVersion?: string;
|
|
20
|
+
weights: ScoringWeights;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function scoreChunkCandidate(input: ScoreCandidateInput): SearchResult {
|
|
24
|
+
const { chunk, ftsRank, symbols, codeSnippets, query, intent, authority = 'official', targetDocVersion, weights } = input;
|
|
25
|
+
|
|
26
|
+
const matchReasons: string[] = [];
|
|
27
|
+
const queryLower = query.toLowerCase().trim();
|
|
28
|
+
const queryTokens = queryLower
|
|
29
|
+
.replace(/[^\w\s-]/g, ' ')
|
|
30
|
+
.split(/\s+/)
|
|
31
|
+
.filter(t => t.length > 2);
|
|
32
|
+
|
|
33
|
+
// 1. Base BM25 score from FTS5 (FTS5 rank is negative where smaller = more relevant)
|
|
34
|
+
const normalizedBm25 = Math.max(0.1, -ftsRank);
|
|
35
|
+
let totalScore = normalizedBm25 * weights.bm25Weight;
|
|
36
|
+
matchReasons.push(`BM25 base (${normalizedBm25.toFixed(2)})`);
|
|
37
|
+
|
|
38
|
+
// 2. Exact phrase match in content
|
|
39
|
+
if (chunk.content.toLowerCase().includes(queryLower)) {
|
|
40
|
+
totalScore += weights.exactPhraseBonus;
|
|
41
|
+
matchReasons.push(`Exact phrase match (+${weights.exactPhraseBonus})`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 3. Title match
|
|
45
|
+
const titleLower = (chunk.title || '').toLowerCase();
|
|
46
|
+
const titleMatches = queryTokens.filter(t => titleLower.includes(t));
|
|
47
|
+
if (titleMatches.length > 0) {
|
|
48
|
+
const boost = weights.titleBonus * (titleMatches.length / queryTokens.length);
|
|
49
|
+
totalScore += boost;
|
|
50
|
+
matchReasons.push(`Title match [${titleMatches.join(', ')}] (+${boost.toFixed(1)})`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 4. Section path match
|
|
54
|
+
const pathString = chunk.sectionPath.join(' ').toLowerCase();
|
|
55
|
+
const pathMatches = queryTokens.filter(t => pathString.includes(t));
|
|
56
|
+
if (pathMatches.length > 0) {
|
|
57
|
+
const boost = weights.sectionPathBonus * (pathMatches.length / queryTokens.length);
|
|
58
|
+
totalScore += boost;
|
|
59
|
+
matchReasons.push(`Section breadcrumbs match [${pathMatches.join(', ')}] (+${boost.toFixed(1)})`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 5. Symbol match (shallow deterministic regex symbols)
|
|
63
|
+
const matchedSymbols = symbols.filter(s => {
|
|
64
|
+
const symLower = s.name.toLowerCase();
|
|
65
|
+
return queryLower.includes(symLower) || symLower.includes(queryLower);
|
|
66
|
+
});
|
|
67
|
+
if (matchedSymbols.length > 0) {
|
|
68
|
+
totalScore += weights.symbolMatchBonus;
|
|
69
|
+
matchReasons.push(`Symbol match [${matchedSymbols.map(s => s.name).join(', ')}] (+${weights.symbolMatchBonus})`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 6. Query intent alignment
|
|
73
|
+
let intentBoost = 0;
|
|
74
|
+
if (intent === 'api' && chunk.chunkType === 'api') {
|
|
75
|
+
intentBoost = weights.intentTypeBonus;
|
|
76
|
+
} else if (intent === 'examples' && (chunk.chunkType === 'example' || chunk.chunkType === 'code')) {
|
|
77
|
+
intentBoost = weights.intentTypeBonus;
|
|
78
|
+
} else if (intent === 'troubleshooting' && (chunk.chunkType === 'warning' || /error|exception|fail|status|code/i.test(chunk.content))) {
|
|
79
|
+
intentBoost = weights.intentTypeBonus;
|
|
80
|
+
} else if (intent === 'implementation' && (chunk.chunkType === 'mixed' || chunk.chunkType === 'example')) {
|
|
81
|
+
intentBoost = weights.intentTypeBonus;
|
|
82
|
+
} else if (intent === 'conceptual' && chunk.chunkType === 'prose') {
|
|
83
|
+
intentBoost = weights.intentTypeBonus;
|
|
84
|
+
} else if (intent === 'configuration' && (symbols.some(s => s.kind === 'config') || chunk.chunkType === 'prose')) {
|
|
85
|
+
intentBoost = weights.intentTypeBonus;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (intentBoost > 0) {
|
|
89
|
+
totalScore += intentBoost;
|
|
90
|
+
matchReasons.push(`Intent alignment '${intent}' for chunkType '${chunk.chunkType}' (+${intentBoost})`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 7. Documentation version boosting & nuance
|
|
94
|
+
if (targetDocVersion && chunk.docVersion) {
|
|
95
|
+
const chunkVerClean = chunk.docVersion.replace(/^[v=]/, '');
|
|
96
|
+
const targetVerClean = targetDocVersion.replace(/^[v=]/, '');
|
|
97
|
+
|
|
98
|
+
const chunkMajor = chunkVerClean.split('.')[0];
|
|
99
|
+
const targetMajor = targetVerClean.split('.')[0];
|
|
100
|
+
|
|
101
|
+
if (chunkVerClean === targetVerClean || chunk.docVersion === targetDocVersion) {
|
|
102
|
+
// Exact version match
|
|
103
|
+
totalScore += weights.versionBonus;
|
|
104
|
+
matchReasons.push(`Target doc version exact match [${chunk.docVersion}] (+${weights.versionBonus})`);
|
|
105
|
+
} else if (chunkMajor === targetMajor) {
|
|
106
|
+
// Same major version (e.g. target 14.2.3, chunk v14 or 14.1)
|
|
107
|
+
const majorBoost = weights.versionBonus * 0.85;
|
|
108
|
+
totalScore += majorBoost;
|
|
109
|
+
matchReasons.push(`Target doc major match [${chunk.docVersion}] (+${majorBoost.toFixed(1)})`);
|
|
110
|
+
} else if (
|
|
111
|
+
chunk.docVersion === 'latest' ||
|
|
112
|
+
chunk.docVersion === 'stable' ||
|
|
113
|
+
chunk.docVersion === 'current'
|
|
114
|
+
) {
|
|
115
|
+
// Unversioned / latest documentation: neutral supplemental context
|
|
116
|
+
matchReasons.push(`Supplemental unversioned/latest documentation [${chunk.docVersion}]`);
|
|
117
|
+
} else {
|
|
118
|
+
// Evidence of major version incompatibility (e.g. target v14 vs chunk v16)
|
|
119
|
+
const penalty = weights.versionBonus * 0.5;
|
|
120
|
+
totalScore -= penalty;
|
|
121
|
+
matchReasons.push(`Version major discrepancy [chunk ${chunk.docVersion} vs target ${targetDocVersion}] (-${penalty.toFixed(1)})`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// 8. Authority weighting
|
|
126
|
+
const authMultiplier = weights.authorityWeights[authority] ?? 1.0;
|
|
127
|
+
totalScore *= authMultiplier;
|
|
128
|
+
if (authMultiplier !== 1.0) {
|
|
129
|
+
matchReasons.push(`Authority weighting: x${authMultiplier}`);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
chunk,
|
|
134
|
+
score: parseFloat(totalScore.toFixed(3)),
|
|
135
|
+
matchReasons,
|
|
136
|
+
symbols,
|
|
137
|
+
codeSnippets,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ScoringWeights } from '../../shared/src/index.ts';
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_SCORING_WEIGHTS_V1: ScoringWeights = {
|
|
4
|
+
version: '1.0.0',
|
|
5
|
+
bm25Weight: 1.0,
|
|
6
|
+
exactPhraseBonus: 5.0,
|
|
7
|
+
titleBonus: 8.0,
|
|
8
|
+
sectionPathBonus: 4.0,
|
|
9
|
+
symbolMatchBonus: 10.0,
|
|
10
|
+
intentTypeBonus: 6.0,
|
|
11
|
+
versionBonus: 12.0,
|
|
12
|
+
authorityWeights: {
|
|
13
|
+
official: 1.0,
|
|
14
|
+
community: 0.8,
|
|
15
|
+
third_party: 0.6,
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export function resolveScoringWeights(overrides?: Partial<ScoringWeights>): ScoringWeights {
|
|
20
|
+
if (!overrides) {
|
|
21
|
+
return { ...DEFAULT_SCORING_WEIGHTS_V1 };
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
...DEFAULT_SCORING_WEIGHTS_V1,
|
|
25
|
+
...overrides,
|
|
26
|
+
authorityWeights: {
|
|
27
|
+
...DEFAULT_SCORING_WEIGHTS_V1.authorityWeights,
|
|
28
|
+
...(overrides.authorityWeights || {}),
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|