Repository navigation
Expand file tree
/
Copy paththeoremKnowledgeSource.ts
More file actions
107 lines (103 loc) · 4.45 KB
/
Copy paththeoremKnowledgeSource.ts
File metadata and controls
107 lines (103 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { locateKnowledgeDeclaration, readKnowledgeSource, type TheoremKnowledgeBase } from './theoremKnowledge';
import { largestFittingPage, splitsSurrogate } from './textPage';
export type KnowledgeSourceOptions = {
file?: string;
name?: string;
startLine?: number;
lines?: number;
startOffset?: number;
maxBytes?: number;
};
const invalidOptions = () => {
throw new Error('math_knowledge_source_options_invalid');
};
const validInteger = (value: unknown, minimum: number, maximum: number) =>
typeof value === 'number' && Number.isSafeInteger(value) && value >= minimum && value <= maximum;
/** Return exact source bytes as a bounded excerpt, without asserting a complete Lean context. */
export function getKnowledgeSource(base: TheoremKnowledgeBase, id: string, options: KnowledgeSourceOptions = {}) {
if (!options || typeof options !== 'object' || Array.isArray(options)) invalidOptions();
const { file, name, startLine, startOffset, lines = 80, maxBytes = 16000 } = options;
if (
(file !== undefined && (typeof file !== 'string' || !file)) ||
(name !== undefined && (typeof name !== 'string' || !name)) ||
(startLine !== undefined && !validInteger(startLine, 1, Number.MAX_SAFE_INTEGER)) ||
(startOffset !== undefined && !validInteger(startOffset, 0, Number.MAX_SAFE_INTEGER)) ||
!validInteger(lines, 1, 200) ||
!validInteger(maxBytes, 1024, 65536) ||
(name !== undefined && (startLine !== undefined || startOffset !== undefined)) ||
(startLine !== undefined && startOffset !== undefined)
)
invalidOptions();
const { entry, file: selectedFile, sha256, source } = readKnowledgeSource(base, id, file);
const lineStarts = [0];
for (let offset = source.indexOf('\n'); offset >= 0; offset = source.indexOf('\n', offset + 1))
lineStarts.push(offset + 1);
const lineAt = (offset: number) => {
let lower = 0,
upper = lineStarts.length;
while (lower < upper) {
const middle = Math.floor((lower + upper) / 2);
if (lineStarts[middle]! <= offset) lower = middle + 1;
else upper = middle;
}
return lower;
};
const declaration = name === undefined ? undefined : locateKnowledgeDeclaration(source, name);
const mode = declaration ? 'name' : startOffset === undefined ? 'lines' : 'offset';
if (
(startLine !== undefined && startLine > lineStarts.length) ||
(startOffset !== undefined && (startOffset > source.length || splitsSurrogate(source, startOffset)))
)
throw new Error('math_knowledge_source_position_invalid');
const begin = declaration?.startOffset ?? startOffset ?? lineStarts[(startLine ?? 1) - 1]!;
const selectionEnd = declaration?.endOffset ?? source.length;
const firstLine = lineAt(begin);
const requestedEnd = Math.min(selectionEnd, lineStarts[firstLine - 1 + lines] ?? source.length);
const sourceBytes = Buffer.byteLength(source);
const packet = (end: number) => {
const text = source.slice(begin, end),
truncated = end < selectionEnd;
return {
id: entry.id,
contextDigest: entry.contextDigest,
file: selectedFile,
sha256,
evidence: base.evidence,
requiresFreshCheck: true as const,
contextCoverage: 'source-excerpt-not-complete-context' as const,
sourceBytes,
sourceLines: lineStarts.length,
offsetEncoding: 'utf16-code-unit' as const,
endOffsetExclusive: true as const,
mode,
display: {
startOffset: begin,
endOffset: end,
startLine: firstLine,
endLine: lineAt(end > begin ? end - 1 : begin),
bytes: Buffer.byteLength(text),
},
text,
truncated,
...(truncated ? { nextOffset: end, nextStartLine: lineAt(end) } : {}),
...(declaration
? {
declaration: {
name: declaration.name,
qualifiedName: declaration.qualifiedName,
namespace: declaration.namespace,
kind: declaration.kind,
startOffset: declaration.startOffset,
bodyOffset: declaration.bodyOffset,
endOffset: declaration.endOffset,
startLine: lineAt(declaration.startOffset),
endLine: lineAt(declaration.endOffset - 1),
complete: end === declaration.endOffset,
},
}
: {}),
};
};
// Include JSON escaping, metadata, and the CLI's trailing newline in the byte budget.
return largestFittingPage(source, begin, requestedEnd, maxBytes, packet, 'math_knowledge_source_budget_too_small');
}