verifiable-thinking-mcp 0.4.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 +339 -0
- package/package.json +75 -0
- package/src/index.ts +38 -0
- package/src/lib/cache.ts +246 -0
- package/src/lib/compression.ts +804 -0
- package/src/lib/compute/cache.ts +86 -0
- package/src/lib/compute/classifier.ts +555 -0
- package/src/lib/compute/confidence.ts +79 -0
- package/src/lib/compute/context.ts +154 -0
- package/src/lib/compute/extract.ts +200 -0
- package/src/lib/compute/filter.ts +224 -0
- package/src/lib/compute/index.ts +171 -0
- package/src/lib/compute/math.ts +247 -0
- package/src/lib/compute/patterns.ts +564 -0
- package/src/lib/compute/registry.ts +145 -0
- package/src/lib/compute/solvers/arithmetic.ts +65 -0
- package/src/lib/compute/solvers/calculus.ts +249 -0
- package/src/lib/compute/solvers/derivation-core.ts +371 -0
- package/src/lib/compute/solvers/derivation-latex.ts +160 -0
- package/src/lib/compute/solvers/derivation-mistakes.ts +1046 -0
- package/src/lib/compute/solvers/derivation-simplify.ts +451 -0
- package/src/lib/compute/solvers/derivation-transform.ts +620 -0
- package/src/lib/compute/solvers/derivation.ts +67 -0
- package/src/lib/compute/solvers/facts.ts +120 -0
- package/src/lib/compute/solvers/formula.ts +728 -0
- package/src/lib/compute/solvers/index.ts +36 -0
- package/src/lib/compute/solvers/logic.ts +422 -0
- package/src/lib/compute/solvers/probability.ts +307 -0
- package/src/lib/compute/solvers/statistics.ts +262 -0
- package/src/lib/compute/solvers/word-problems.ts +408 -0
- package/src/lib/compute/types.ts +107 -0
- package/src/lib/concepts.ts +111 -0
- package/src/lib/domain.ts +731 -0
- package/src/lib/extraction.ts +912 -0
- package/src/lib/index.ts +122 -0
- package/src/lib/judge.ts +260 -0
- package/src/lib/math/ast.ts +842 -0
- package/src/lib/math/index.ts +8 -0
- package/src/lib/math/operators.ts +171 -0
- package/src/lib/math/tokenizer.ts +477 -0
- package/src/lib/patterns.ts +200 -0
- package/src/lib/session.ts +825 -0
- package/src/lib/think/challenge.ts +323 -0
- package/src/lib/think/complexity.ts +504 -0
- package/src/lib/think/confidence-drift.ts +507 -0
- package/src/lib/think/consistency.ts +347 -0
- package/src/lib/think/guidance.ts +188 -0
- package/src/lib/think/helpers.ts +568 -0
- package/src/lib/think/hypothesis.ts +216 -0
- package/src/lib/think/index.ts +127 -0
- package/src/lib/think/prompts.ts +262 -0
- package/src/lib/think/route.ts +358 -0
- package/src/lib/think/schema.ts +98 -0
- package/src/lib/think/scratchpad-schema.ts +662 -0
- package/src/lib/think/spot-check.ts +961 -0
- package/src/lib/think/types.ts +93 -0
- package/src/lib/think/verification.ts +260 -0
- package/src/lib/tokens.ts +177 -0
- package/src/lib/verification.ts +620 -0
- package/src/prompts/index.ts +10 -0
- package/src/prompts/templates.ts +336 -0
- package/src/resources/index.ts +8 -0
- package/src/resources/sessions.ts +196 -0
- package/src/tools/compress.ts +138 -0
- package/src/tools/index.ts +5 -0
- package/src/tools/scratchpad.ts +2659 -0
- package/src/tools/sessions.ts +144 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hypothesis Resolution - Detects when a branch's hypothesis is confirmed or refuted
|
|
3
|
+
*
|
|
4
|
+
* Uses O(n) pattern matching to detect resolution signals in step content:
|
|
5
|
+
* - Confirmation: "therefore", "confirmed", "proves", "QED", "thus we have shown"
|
|
6
|
+
* - Refutation: "contradiction", "impossible", "disproved", "fails", "cannot be"
|
|
7
|
+
*
|
|
8
|
+
* Returns resolution status for branches with hypotheses.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Resolution status for a hypothesis */
|
|
12
|
+
export interface HypothesisResolution {
|
|
13
|
+
/** Whether the hypothesis has been resolved */
|
|
14
|
+
resolved: boolean;
|
|
15
|
+
/** Resolution outcome if resolved */
|
|
16
|
+
outcome: "confirmed" | "refuted" | "inconclusive" | null;
|
|
17
|
+
/** Confidence in the resolution (0-1) */
|
|
18
|
+
confidence: number;
|
|
19
|
+
/** Step number where resolution was detected */
|
|
20
|
+
resolved_at_step: number | null;
|
|
21
|
+
/** Evidence text that triggered resolution */
|
|
22
|
+
evidence: string | null;
|
|
23
|
+
/** The original hypothesis being tested */
|
|
24
|
+
hypothesis: string;
|
|
25
|
+
/** The success criteria (if provided) */
|
|
26
|
+
success_criteria: string | null;
|
|
27
|
+
/** Suggested action based on resolution */
|
|
28
|
+
suggestion: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Confirmation patterns - signals that hypothesis is proven true
|
|
32
|
+
const CONFIRMATION_PATTERNS = [
|
|
33
|
+
/\b(?:therefore|thus|hence|consequently)\b.*\b(?:true|correct|valid|proven|confirmed)\b/i,
|
|
34
|
+
/\b(?:this\s+)?(?:confirms?|proves?|shows?|demonstrates?)\s+(?:that\s+)?(?:the\s+)?hypothesis\b/i,
|
|
35
|
+
/\b(?:QED|Q\.E\.D\.|quod\s+erat\s+demonstrandum)\b/i,
|
|
36
|
+
/\b(?:we\s+have\s+shown|we\s+conclude|this\s+establishes)\b/i,
|
|
37
|
+
/\bhypothesis\s+(?:is\s+)?(?:true|correct|valid|confirmed)\b/i,
|
|
38
|
+
// "as expected/hypothesized" but NOT "assume is true"
|
|
39
|
+
/\bas\s+(?:we\s+)?(?:hypothesized|expected|predicted)\b/i,
|
|
40
|
+
/\bsuccess(?:fully)?\s+(?:verified|confirmed|proven)\b/i,
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
// Refutation patterns - signals that hypothesis is proven false
|
|
44
|
+
const REFUTATION_PATTERNS = [
|
|
45
|
+
/\b(?:contradiction|contradicts?|inconsistent)\b/i,
|
|
46
|
+
/\b(?:impossible|cannot\s+be|can't\s+be)\b/i,
|
|
47
|
+
/\b(?:disprove[ds]?|refute[ds]?|falsif(?:y|ied))\b/i,
|
|
48
|
+
/\bhypothesis\s+(?:is\s+)?(?:false|incorrect|invalid|wrong|fails?)\b/i,
|
|
49
|
+
/\b(?:this\s+)?(?:fails?|violates?|breaks?)\s+(?:the\s+)?(?:assumption|hypothesis)\b/i,
|
|
50
|
+
/\b(?:counterexample|counter-example)\b/i,
|
|
51
|
+
/\bnot\s+(?:true|valid|correct|possible)\b/i,
|
|
52
|
+
/\b(?:rejected?|abandon|discard)\s+(?:the\s+)?hypothesis\b/i,
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
// Inconclusive patterns - explicitly states uncertainty remains
|
|
56
|
+
const INCONCLUSIVE_PATTERNS = [
|
|
57
|
+
/\b(?:inconclusive|undetermined|unclear|uncertain)\b/i,
|
|
58
|
+
/\b(?:need|require)s?\s+(?:more|further|additional)\s+(?:evidence|proof|analysis)\b/i,
|
|
59
|
+
/\b(?:cannot\s+(?:yet\s+)?(?:determine|conclude|decide))\b/i,
|
|
60
|
+
/\b(?:insufficient\s+(?:evidence|data|information))\b/i,
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Check if text matches any pattern in a list
|
|
65
|
+
* Returns the matching text if found
|
|
66
|
+
*/
|
|
67
|
+
function findMatch(text: string, patterns: RegExp[]): string | null {
|
|
68
|
+
for (const pattern of patterns) {
|
|
69
|
+
const match = text.match(pattern);
|
|
70
|
+
if (match) {
|
|
71
|
+
return match[0];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Check if success criteria is mentioned/satisfied in text
|
|
79
|
+
*/
|
|
80
|
+
function checkSuccessCriteria(text: string, criteria: string): boolean {
|
|
81
|
+
// Normalize both for comparison
|
|
82
|
+
const normalizedText = text.toLowerCase();
|
|
83
|
+
const normalizedCriteria = criteria.toLowerCase();
|
|
84
|
+
|
|
85
|
+
// Check if criteria keywords appear in text
|
|
86
|
+
const criteriaWords = normalizedCriteria.split(/\s+/).filter((w) => w.length > 3); // Skip short words
|
|
87
|
+
|
|
88
|
+
const matchCount = criteriaWords.filter((word) => normalizedText.includes(word)).length;
|
|
89
|
+
|
|
90
|
+
// If >50% of meaningful words match, consider criteria referenced
|
|
91
|
+
return matchCount >= criteriaWords.length * 0.5;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Analyze a step for hypothesis resolution signals
|
|
96
|
+
*
|
|
97
|
+
* @param stepText - The thought content of the step
|
|
98
|
+
* @param hypothesis - The hypothesis being tested
|
|
99
|
+
* @param successCriteria - Optional success criteria
|
|
100
|
+
* @param stepNumber - The step number
|
|
101
|
+
* @returns Resolution analysis or null if no resolution detected
|
|
102
|
+
*/
|
|
103
|
+
export function analyzeStepForResolution(
|
|
104
|
+
stepText: string,
|
|
105
|
+
hypothesis: string,
|
|
106
|
+
successCriteria: string | null,
|
|
107
|
+
stepNumber: number,
|
|
108
|
+
): HypothesisResolution {
|
|
109
|
+
const baseResult: HypothesisResolution = {
|
|
110
|
+
resolved: false,
|
|
111
|
+
outcome: null,
|
|
112
|
+
confidence: 0,
|
|
113
|
+
resolved_at_step: null,
|
|
114
|
+
evidence: null,
|
|
115
|
+
hypothesis,
|
|
116
|
+
success_criteria: successCriteria,
|
|
117
|
+
suggestion: "Continue testing the hypothesis.",
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// Check for refutation first (stronger signal - contradictions are definitive)
|
|
121
|
+
const refutationMatch = findMatch(stepText, REFUTATION_PATTERNS);
|
|
122
|
+
if (refutationMatch) {
|
|
123
|
+
return {
|
|
124
|
+
...baseResult,
|
|
125
|
+
resolved: true,
|
|
126
|
+
outcome: "refuted",
|
|
127
|
+
confidence: 0.9,
|
|
128
|
+
resolved_at_step: stepNumber,
|
|
129
|
+
evidence: refutationMatch,
|
|
130
|
+
suggestion: "Hypothesis refuted. Consider abandoning this branch or revising the hypothesis.",
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Check for explicit inconclusive
|
|
135
|
+
const inconclusiveMatch = findMatch(stepText, INCONCLUSIVE_PATTERNS);
|
|
136
|
+
if (inconclusiveMatch) {
|
|
137
|
+
return {
|
|
138
|
+
...baseResult,
|
|
139
|
+
resolved: true,
|
|
140
|
+
outcome: "inconclusive",
|
|
141
|
+
confidence: 0.7,
|
|
142
|
+
resolved_at_step: stepNumber,
|
|
143
|
+
evidence: inconclusiveMatch,
|
|
144
|
+
suggestion: "Hypothesis inconclusive. Gather more evidence or reformulate.",
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Check for confirmation
|
|
149
|
+
const confirmationMatch = findMatch(stepText, CONFIRMATION_PATTERNS);
|
|
150
|
+
if (confirmationMatch) {
|
|
151
|
+
let confidence = 0.85;
|
|
152
|
+
|
|
153
|
+
// Boost confidence if success criteria is explicitly satisfied
|
|
154
|
+
if (successCriteria && checkSuccessCriteria(stepText, successCriteria)) {
|
|
155
|
+
confidence = 0.95;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
...baseResult,
|
|
160
|
+
resolved: true,
|
|
161
|
+
outcome: "confirmed",
|
|
162
|
+
confidence,
|
|
163
|
+
resolved_at_step: stepNumber,
|
|
164
|
+
evidence: confirmationMatch,
|
|
165
|
+
suggestion: "Hypothesis confirmed. Consider merging findings back to main branch.",
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Check if success criteria is mentioned even without explicit confirmation
|
|
170
|
+
if (successCriteria && checkSuccessCriteria(stepText, successCriteria)) {
|
|
171
|
+
return {
|
|
172
|
+
...baseResult,
|
|
173
|
+
resolved: false,
|
|
174
|
+
outcome: null,
|
|
175
|
+
confidence: 0.6,
|
|
176
|
+
evidence: "Success criteria keywords detected",
|
|
177
|
+
suggestion: "Success criteria may be satisfied. Verify and explicitly confirm or refute.",
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return baseResult;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Analyze all steps in a branch for hypothesis resolution
|
|
186
|
+
*
|
|
187
|
+
* @param steps - Array of steps with their content
|
|
188
|
+
* @param hypothesis - The hypothesis being tested
|
|
189
|
+
* @param successCriteria - Optional success criteria
|
|
190
|
+
* @returns Resolution status based on all steps
|
|
191
|
+
*/
|
|
192
|
+
export function analyzeHypothesisResolution(
|
|
193
|
+
steps: Array<{ step: number; thought: string }>,
|
|
194
|
+
hypothesis: string,
|
|
195
|
+
successCriteria: string | null,
|
|
196
|
+
): HypothesisResolution {
|
|
197
|
+
// Analyze each step, return first resolution found (chronological)
|
|
198
|
+
for (const { step, thought } of steps) {
|
|
199
|
+
const result = analyzeStepForResolution(thought, hypothesis, successCriteria, step);
|
|
200
|
+
if (result.resolved) {
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// No resolution found
|
|
206
|
+
return {
|
|
207
|
+
resolved: false,
|
|
208
|
+
outcome: null,
|
|
209
|
+
confidence: 0,
|
|
210
|
+
resolved_at_step: null,
|
|
211
|
+
evidence: null,
|
|
212
|
+
hypothesis,
|
|
213
|
+
success_criteria: successCriteria,
|
|
214
|
+
suggestion: `Continue testing hypothesis: "${hypothesis.slice(0, 50)}${hypothesis.length > 50 ? "..." : ""}"`,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Think Library - Barrel export for think-related modules
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// Local complexity assessment
|
|
6
|
+
export {
|
|
7
|
+
assessPromptComplexity,
|
|
8
|
+
type ComplexityResult,
|
|
9
|
+
getTrivialPrompt,
|
|
10
|
+
isTrivialQuestion,
|
|
11
|
+
} from "./complexity.ts";
|
|
12
|
+
// Guidance engine (failure pattern detection for free-form reasoning)
|
|
13
|
+
export {
|
|
14
|
+
analyzeThought,
|
|
15
|
+
detectDomain,
|
|
16
|
+
FAILURE_PATTERNS,
|
|
17
|
+
type FailurePattern,
|
|
18
|
+
type ThoughtAnalysis,
|
|
19
|
+
VALID_PURPOSES,
|
|
20
|
+
} from "./guidance.ts";
|
|
21
|
+
|
|
22
|
+
// Helpers (extracted for cognitive complexity reduction)
|
|
23
|
+
export {
|
|
24
|
+
type AugmentResult,
|
|
25
|
+
assessComplexity,
|
|
26
|
+
buildBaselineResponse,
|
|
27
|
+
buildRecord,
|
|
28
|
+
buildResponse,
|
|
29
|
+
type ComplexityInfo,
|
|
30
|
+
type CompressionLevel,
|
|
31
|
+
type CompressionStats,
|
|
32
|
+
compressChainContext,
|
|
33
|
+
compressInput,
|
|
34
|
+
compressOutput,
|
|
35
|
+
type ExecuteContext,
|
|
36
|
+
errorResponse,
|
|
37
|
+
findMissingDeps,
|
|
38
|
+
initContext,
|
|
39
|
+
jsonResponse,
|
|
40
|
+
runGuidance,
|
|
41
|
+
runVerify,
|
|
42
|
+
type StreamFn,
|
|
43
|
+
storeThought,
|
|
44
|
+
tryAugment,
|
|
45
|
+
tryCompute,
|
|
46
|
+
validateBranch,
|
|
47
|
+
validateRevision,
|
|
48
|
+
} from "./helpers.ts";
|
|
49
|
+
|
|
50
|
+
// Prompts (verbosity-aware templates)
|
|
51
|
+
export {
|
|
52
|
+
// Domain-aware prompts
|
|
53
|
+
DOMAIN_PROMPTS,
|
|
54
|
+
// User prompts
|
|
55
|
+
formatBaselinePrompt,
|
|
56
|
+
formatBaselinePromptTerse,
|
|
57
|
+
formatCriticalCheckPrompt,
|
|
58
|
+
formatCriticalCheckPromptTerse,
|
|
59
|
+
formatDomainExplanatoryPrompt,
|
|
60
|
+
formatExplanatoryPrompt,
|
|
61
|
+
formatReasoningPrompt,
|
|
62
|
+
formatReasoningPromptTerse,
|
|
63
|
+
formatVerificationPrompt,
|
|
64
|
+
formatVerificationPromptTerse,
|
|
65
|
+
getDomainSystemPrompt,
|
|
66
|
+
// Unified getters
|
|
67
|
+
getSystemPrompt,
|
|
68
|
+
getUserPrompt,
|
|
69
|
+
getVerbosity,
|
|
70
|
+
SYSTEM_ANSWER_ONLY,
|
|
71
|
+
SYSTEM_ANSWER_ONLY_TERSE,
|
|
72
|
+
// System prompts
|
|
73
|
+
SYSTEM_BASELINE,
|
|
74
|
+
SYSTEM_BASELINE_TERSE,
|
|
75
|
+
SYSTEM_EXPLANATORY,
|
|
76
|
+
SYSTEM_REASONING,
|
|
77
|
+
SYSTEM_REASONING_TERSE,
|
|
78
|
+
SYSTEM_VERIFICATION,
|
|
79
|
+
SYSTEM_VERIFICATION_TERSE,
|
|
80
|
+
type Verbosity,
|
|
81
|
+
} from "./prompts.ts";
|
|
82
|
+
|
|
83
|
+
// Routing (complexity-based path selection)
|
|
84
|
+
export {
|
|
85
|
+
getComplexityInfo,
|
|
86
|
+
isExplanatoryQuestion,
|
|
87
|
+
type RoutePrompts,
|
|
88
|
+
type RouteResult,
|
|
89
|
+
type RoutingPath,
|
|
90
|
+
routeQuestion,
|
|
91
|
+
} from "./route.ts";
|
|
92
|
+
// Schema
|
|
93
|
+
export { NextActionSchema, type ThinkArgs, ThinkSchema } from "./schema.ts";
|
|
94
|
+
// Scratchpad schema
|
|
95
|
+
export {
|
|
96
|
+
type ScratchpadArgs,
|
|
97
|
+
type ScratchpadResponse,
|
|
98
|
+
ScratchpadSchema,
|
|
99
|
+
} from "./scratchpad-schema.ts";
|
|
100
|
+
// Spot-check (lightweight trap detection for High+ complexity)
|
|
101
|
+
export {
|
|
102
|
+
hasTrapPatterns,
|
|
103
|
+
type NeedsSpotCheckResult,
|
|
104
|
+
needsSpotCheck,
|
|
105
|
+
PRIME_AGGRESSIVE,
|
|
106
|
+
PRIME_DEFAULTS,
|
|
107
|
+
type PrimeOptions,
|
|
108
|
+
type PrimeResult,
|
|
109
|
+
primeQuestion,
|
|
110
|
+
type SpotCheckResult,
|
|
111
|
+
spotCheck,
|
|
112
|
+
type TrapDetector,
|
|
113
|
+
} from "./spot-check.ts";
|
|
114
|
+
|
|
115
|
+
// Types
|
|
116
|
+
export type {
|
|
117
|
+
BaselineResult,
|
|
118
|
+
BenchmarkResults,
|
|
119
|
+
BenchmarkSummary,
|
|
120
|
+
Question,
|
|
121
|
+
QuestionSet,
|
|
122
|
+
RunResult,
|
|
123
|
+
ToolResult,
|
|
124
|
+
} from "./types.ts";
|
|
125
|
+
|
|
126
|
+
// Verification
|
|
127
|
+
export { estimateTokens, verifyAnswer } from "./verification.ts";
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prompt Templates for LLM Interactions
|
|
3
|
+
* Centralized prompts used by the think tool and benchmarks
|
|
4
|
+
*
|
|
5
|
+
* TERSE MODE: ~50% fewer tokens using Chain-of-Draft style
|
|
6
|
+
* - Short fragments instead of full sentences
|
|
7
|
+
* - Minimalist system prompts
|
|
8
|
+
* - Direct answer extraction
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
// =============================================================================
|
|
12
|
+
// VERBOSITY CONFIGURATION
|
|
13
|
+
// =============================================================================
|
|
14
|
+
|
|
15
|
+
export type Verbosity = "terse" | "normal" | "verbose";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Determine verbosity based on question characteristics
|
|
19
|
+
* Short questions without "explain/why/how" get terse mode
|
|
20
|
+
*/
|
|
21
|
+
export function getVerbosity(question: string): Verbosity {
|
|
22
|
+
const wordCount = question.split(/\s+/).length;
|
|
23
|
+
const needsExplanation = /explain|why|how\s+does|describe|compare/i.test(question);
|
|
24
|
+
const isSimple = /^\s*(?:what|calculate|compute|is)\s+/i.test(question);
|
|
25
|
+
|
|
26
|
+
if (needsExplanation) return "verbose";
|
|
27
|
+
if (wordCount < 15 && isSimple) return "terse";
|
|
28
|
+
return "normal";
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// =============================================================================
|
|
32
|
+
// SYSTEM PROMPTS - NORMAL (default)
|
|
33
|
+
// =============================================================================
|
|
34
|
+
|
|
35
|
+
export const SYSTEM_BASELINE = "Answer directly and concisely. Plain text only.";
|
|
36
|
+
export const SYSTEM_REASONING =
|
|
37
|
+
"Show reasoning step-by-step, then give final answer. Plain text math only.";
|
|
38
|
+
export const SYSTEM_VERIFICATION = "Double-check reasoning. Fix errors. Plain text only.";
|
|
39
|
+
export const SYSTEM_ANSWER_ONLY = "Answer only.";
|
|
40
|
+
|
|
41
|
+
// System prompt for explanatory questions - emphasizes conciseness
|
|
42
|
+
export const SYSTEM_EXPLANATORY = "Explain clearly and concisely. Plain text only.";
|
|
43
|
+
|
|
44
|
+
// =============================================================================
|
|
45
|
+
// DOMAIN-SPECIFIC PROMPTS (token-light steering)
|
|
46
|
+
// =============================================================================
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Domain-specific system prompts - concise but effective steering.
|
|
50
|
+
* ~15-25 tokens each, optimized for explanation quality.
|
|
51
|
+
*/
|
|
52
|
+
export const DOMAIN_PROMPTS: Record<string, { system: string; style: string }> = {
|
|
53
|
+
// Technical domains
|
|
54
|
+
coding: {
|
|
55
|
+
system: "Explain clearly. Use code examples when they clarify.",
|
|
56
|
+
style: "technical",
|
|
57
|
+
},
|
|
58
|
+
scientific: {
|
|
59
|
+
system: "Explain precisely. Use correct terminology and show derivations.",
|
|
60
|
+
style: "precise",
|
|
61
|
+
},
|
|
62
|
+
// Educational - clarity focus
|
|
63
|
+
educational: {
|
|
64
|
+
system: "Explain clearly. Start with intuition, then details.",
|
|
65
|
+
style: "pedagogical",
|
|
66
|
+
},
|
|
67
|
+
// Financial - accuracy focus
|
|
68
|
+
financial: {
|
|
69
|
+
system: "Explain clearly. State assumptions and show calculations.",
|
|
70
|
+
style: "careful",
|
|
71
|
+
},
|
|
72
|
+
// General - balanced
|
|
73
|
+
general: {
|
|
74
|
+
system: "Explain clearly and directly.",
|
|
75
|
+
style: "balanced",
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// =============================================================================
|
|
80
|
+
// SYSTEM PROMPTS - TERSE (~50% fewer tokens)
|
|
81
|
+
// =============================================================================
|
|
82
|
+
|
|
83
|
+
export const SYSTEM_BASELINE_TERSE = "Answer directly.";
|
|
84
|
+
export const SYSTEM_REASONING_TERSE = "Solve step-by-step. End: Answer: [X]";
|
|
85
|
+
export const SYSTEM_VERIFICATION_TERSE = "Verify. Fix errors. Answer: [X]";
|
|
86
|
+
export const SYSTEM_ANSWER_ONLY_TERSE = "Answer only.";
|
|
87
|
+
|
|
88
|
+
// =============================================================================
|
|
89
|
+
// USER PROMPT TEMPLATES - NORMAL
|
|
90
|
+
// =============================================================================
|
|
91
|
+
|
|
92
|
+
export function formatBaselinePrompt(question: string): string {
|
|
93
|
+
return `${question}
|
|
94
|
+
|
|
95
|
+
Answer clearly. Number for numeric, choice letter for multiple choice.`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function formatReasoningPrompt(question: string): string {
|
|
99
|
+
return `${question}
|
|
100
|
+
|
|
101
|
+
End with "Answer: " followed by just the answer.`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function formatVerificationPrompt(
|
|
105
|
+
question: string,
|
|
106
|
+
initialReasoning: string,
|
|
107
|
+
patterns: string[],
|
|
108
|
+
): string {
|
|
109
|
+
return `Verify: ${question}
|
|
110
|
+
|
|
111
|
+
Prior reasoning:
|
|
112
|
+
${initialReasoning}
|
|
113
|
+
|
|
114
|
+
Risk flags: ${patterns.join(", ")}
|
|
115
|
+
|
|
116
|
+
Check for errors, correct if needed.
|
|
117
|
+
|
|
118
|
+
Answer:`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function formatCriticalCheckPrompt(question: string): string {
|
|
122
|
+
return `${question}
|
|
123
|
+
|
|
124
|
+
Double-check. Answer only:`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Format an explanatory prompt - concise explanations without padding
|
|
129
|
+
*/
|
|
130
|
+
export function formatExplanatoryPrompt(question: string): string {
|
|
131
|
+
return `${question}
|
|
132
|
+
|
|
133
|
+
Be direct. Focus on key concepts.`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Format a domain-aware explanatory prompt (token-light)
|
|
138
|
+
* Just the question - system prompt provides domain steering
|
|
139
|
+
*/
|
|
140
|
+
export function formatDomainExplanatoryPrompt(question: string, _metaDomain: string): string {
|
|
141
|
+
return question;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Get domain-aware system prompt for explanatory questions
|
|
146
|
+
*/
|
|
147
|
+
export function getDomainSystemPrompt(metaDomain: string): string {
|
|
148
|
+
return DOMAIN_PROMPTS[metaDomain]?.system ?? "Direct answer.";
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// =============================================================================
|
|
152
|
+
// USER PROMPT TEMPLATES - TERSE (Chain-of-Draft style)
|
|
153
|
+
// ~50% fewer tokens, uses fragments and minimal structure
|
|
154
|
+
// =============================================================================
|
|
155
|
+
|
|
156
|
+
export function formatBaselinePromptTerse(question: string): string {
|
|
157
|
+
return `Q: ${question}
|
|
158
|
+
A:`;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function formatReasoningPromptTerse(question: string): string {
|
|
162
|
+
return `Q: ${question}
|
|
163
|
+
Steps (max 5 words each):
|
|
164
|
+
Answer:`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function formatVerificationPromptTerse(
|
|
168
|
+
question: string,
|
|
169
|
+
initialReasoning: string,
|
|
170
|
+
patterns: string[],
|
|
171
|
+
): string {
|
|
172
|
+
// Extract just the answer from initial reasoning if possible
|
|
173
|
+
const answerMatch = initialReasoning.match(/(?:answer|result)[:\s]+([^\n.]+)/i);
|
|
174
|
+
const prevAnswer = answerMatch?.[1]?.trim() || "?";
|
|
175
|
+
|
|
176
|
+
return `Q: ${question}
|
|
177
|
+
Prev: ${prevAnswer}
|
|
178
|
+
Flags: ${patterns.slice(0, 2).join(", ")}
|
|
179
|
+
Check. Correct if needed.
|
|
180
|
+
Answer:`;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function formatCriticalCheckPromptTerse(question: string): string {
|
|
184
|
+
return `${question}
|
|
185
|
+
Answer:`;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// =============================================================================
|
|
189
|
+
// UNIFIED PROMPT GETTERS (respects verbosity setting)
|
|
190
|
+
// =============================================================================
|
|
191
|
+
|
|
192
|
+
export function getSystemPrompt(
|
|
193
|
+
type: "baseline" | "reasoning" | "verification" | "answer_only" | "explanatory",
|
|
194
|
+
verbosity: Verbosity = "normal",
|
|
195
|
+
): string {
|
|
196
|
+
if (verbosity === "terse") {
|
|
197
|
+
switch (type) {
|
|
198
|
+
case "baseline":
|
|
199
|
+
return SYSTEM_BASELINE_TERSE;
|
|
200
|
+
case "reasoning":
|
|
201
|
+
return SYSTEM_REASONING_TERSE;
|
|
202
|
+
case "verification":
|
|
203
|
+
return SYSTEM_VERIFICATION_TERSE;
|
|
204
|
+
case "answer_only":
|
|
205
|
+
return SYSTEM_ANSWER_ONLY_TERSE;
|
|
206
|
+
case "explanatory":
|
|
207
|
+
return SYSTEM_EXPLANATORY; // No terse version, use standard
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
// Normal or verbose use standard prompts
|
|
211
|
+
switch (type) {
|
|
212
|
+
case "baseline":
|
|
213
|
+
return SYSTEM_BASELINE;
|
|
214
|
+
case "reasoning":
|
|
215
|
+
return SYSTEM_REASONING;
|
|
216
|
+
case "verification":
|
|
217
|
+
return SYSTEM_VERIFICATION;
|
|
218
|
+
case "answer_only":
|
|
219
|
+
return SYSTEM_ANSWER_ONLY;
|
|
220
|
+
case "explanatory":
|
|
221
|
+
return SYSTEM_EXPLANATORY;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function getUserPrompt(
|
|
226
|
+
type: "baseline" | "reasoning" | "verification" | "critical" | "explanatory",
|
|
227
|
+
question: string,
|
|
228
|
+
verbosity: Verbosity = "normal",
|
|
229
|
+
opts?: { initialReasoning?: string; patterns?: string[] },
|
|
230
|
+
): string {
|
|
231
|
+
if (verbosity === "terse") {
|
|
232
|
+
switch (type) {
|
|
233
|
+
case "baseline":
|
|
234
|
+
return formatBaselinePromptTerse(question);
|
|
235
|
+
case "reasoning":
|
|
236
|
+
return formatReasoningPromptTerse(question);
|
|
237
|
+
case "verification":
|
|
238
|
+
return formatVerificationPromptTerse(
|
|
239
|
+
question,
|
|
240
|
+
opts?.initialReasoning || "",
|
|
241
|
+
opts?.patterns || [],
|
|
242
|
+
);
|
|
243
|
+
case "critical":
|
|
244
|
+
return formatCriticalCheckPromptTerse(question);
|
|
245
|
+
case "explanatory":
|
|
246
|
+
return formatExplanatoryPrompt(question); // No terse version
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
// Normal or verbose use standard prompts
|
|
250
|
+
switch (type) {
|
|
251
|
+
case "baseline":
|
|
252
|
+
return formatBaselinePrompt(question);
|
|
253
|
+
case "reasoning":
|
|
254
|
+
return formatReasoningPrompt(question);
|
|
255
|
+
case "verification":
|
|
256
|
+
return formatVerificationPrompt(question, opts?.initialReasoning || "", opts?.patterns || []);
|
|
257
|
+
case "critical":
|
|
258
|
+
return formatCriticalCheckPrompt(question);
|
|
259
|
+
case "explanatory":
|
|
260
|
+
return formatExplanatoryPrompt(question);
|
|
261
|
+
}
|
|
262
|
+
}
|