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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env npx tsx
|
| 2 |
+
/**
|
| 3 |
+
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 4 |
+
* ARDR - Adaptive Recurrent Dendritic Reasoning (High Mode)
|
| 5 |
+
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 6 |
+
*
|
| 7 |
+
* A multi-stage reasoning pipeline that orchestrates multiple AI models in parallel
|
| 8 |
+
* for superior synthesis and analysis capabilities.
|
| 9 |
+
*
|
| 10 |
+
* SETUP:
|
| 11 |
+
* 1. Get your OpenRouter API key from https://openrouter.ai/keys
|
| 12 |
+
* 2. Replace "YOUR_OPENROUTER_API_KEY" below with your actual key
|
| 13 |
+
* 3. Run with: npx tsx scripts/ardr/ARDR.ts
|
| 14 |
+
*
|
| 15 |
+
* OPTIONS:
|
| 16 |
+
* --tier low|high|max Set reasoning tier (default: high)
|
| 17 |
+
* --debug Show detailed debug output
|
| 18 |
+
*
|
| 19 |
+
* EXAMPLES:
|
| 20 |
+
* npx tsx scripts/ardr/ARDR.ts # Interactive mode, high tier
|
| 21 |
+
* npx tsx scripts/ardr/ARDR.ts --tier max # Use max tier (Opus 4.5)
|
| 22 |
+
* npx tsx scripts/ardr/ARDR.ts --tier low --debug # Low tier with debug output
|
| 23 |
+
*
|
| 24 |
+
* IN-SESSION COMMANDS:
|
| 25 |
+
* tier low|high|max Switch reasoning tier
|
| 26 |
+
* debug Toggle debug mode
|
| 27 |
+
* exit / quit End session
|
| 28 |
+
*
|
| 29 |
+
* PIPELINE STAGES:
|
| 30 |
+
* Stage 0: Task Profiler - Analyzes complexity and allocates reasoning budget
|
| 31 |
+
* Stage A: Structured Decomposition - Creates symbolic/invariant/formal views
|
| 32 |
+
* Stage B: Dendritic Branches - Parallel specialized reasoning (logic, pattern, world, code, adversarial)
|
| 33 |
+
* Stage C: Verification - Attacks hypotheses and measures uncertainty
|
| 34 |
+
* Stage D: Adaptive Recurrence - Re-runs weak branches with targeted instructions
|
| 35 |
+
* Final: Grand Synthesis - Chief model synthesizes verified evidence into response
|
| 36 |
+
*
|
| 37 |
+
* MODELS BY TIER:
|
| 38 |
+
* Low: Llama 3.3 70B (free)
|
| 39 |
+
* High: Deepseek V3.2
|
| 40 |
+
* Max: Claude Opus 4.5
|
| 41 |
+
*
|
| 42 |
+
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 43 |
+
*/
|
| 44 |
+
|
| 45 |
+
import * as readline from "readline";
|
| 46 |
+
import { NexusTier, ARDRState } from "./ARDR_types";
|
| 47 |
+
import { colors } from "./ARDR_models";
|
| 48 |
+
import { initializeOpenAI, log } from "./ARDR_utils";
|
| 49 |
+
import {
|
| 50 |
+
stage0_TaskProfiler,
|
| 51 |
+
stageA_StructuredDecomposition,
|
| 52 |
+
stageB_DendriticBranches,
|
| 53 |
+
stageC_Verification,
|
| 54 |
+
stageD_AdaptiveRecurrence,
|
| 55 |
+
stageFinal_GrandSynthesis,
|
| 56 |
+
handleConversation
|
| 57 |
+
} from "./ARDR_stages";
|
| 58 |
+
|
| 59 |
+
const OPENROUTER_API_KEY = "YOUR_OPENROUTER_API_KEY";
|
| 60 |
+
|
| 61 |
+
if (OPENROUTER_API_KEY === "YOUR_OPENROUTER_API_KEY") {
|
| 62 |
+
console.error(`
|
| 63 |
+
${colors.red}${colors.bright}Error: API key not configured${colors.reset}
|
| 64 |
+
|
| 65 |
+
Please edit ${colors.cyan}scripts/ardr/ARDR.ts${colors.reset} and replace:
|
| 66 |
+
${colors.dim}const OPENROUTER_API_KEY = "YOUR_OPENROUTER_API_KEY";${colors.reset}
|
| 67 |
+
|
| 68 |
+
With your actual OpenRouter API key from:
|
| 69 |
+
${colors.cyan}https://openrouter.ai/keys${colors.reset}
|
| 70 |
+
`);
|
| 71 |
+
process.exit(1);
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
initializeOpenAI(OPENROUTER_API_KEY);
|
| 75 |
+
|
| 76 |
+
async function runARDR(prompt: string, tier: NexusTier, debug: boolean): Promise<string> {
|
| 77 |
+
console.log(`\n${colors.bgMagenta}${colors.white}${colors.bright} ARDR - ${tier.toUpperCase()} MODE ${colors.reset}\n`);
|
| 78 |
+
|
| 79 |
+
const state: ARDRState = {
|
| 80 |
+
originalPrompt: prompt,
|
| 81 |
+
tier,
|
| 82 |
+
budget: null as any,
|
| 83 |
+
triPack: null as any,
|
| 84 |
+
scratchpad: { entries: [], sharedArtifacts: new Map() },
|
| 85 |
+
branchOutputs: [],
|
| 86 |
+
verification: null as any,
|
| 87 |
+
recurrenceCount: 0,
|
| 88 |
+
finalResponse: ""
|
| 89 |
+
};
|
| 90 |
+
|
| 91 |
+
state.budget = await stage0_TaskProfiler(prompt, tier, debug);
|
| 92 |
+
|
| 93 |
+
if (state.budget.taskType === "conversation" || state.budget.branches.length === 0) {
|
| 94 |
+
log("Profiler", "Detected conversational input - using fast path", colors.green);
|
| 95 |
+
return await handleConversation(prompt, tier);
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
state.triPack = await stageA_StructuredDecomposition(prompt, state.budget, debug);
|
| 99 |
+
|
| 100 |
+
state.branchOutputs = await stageB_DendriticBranches(
|
| 101 |
+
prompt, state.triPack, state.budget, state.scratchpad, debug
|
| 102 |
+
);
|
| 103 |
+
|
| 104 |
+
state.verification = await stageC_Verification(
|
| 105 |
+
prompt, state.triPack, state.branchOutputs, state.scratchpad, debug
|
| 106 |
+
);
|
| 107 |
+
|
| 108 |
+
let shouldContinue = true;
|
| 109 |
+
while (shouldContinue && state.recurrenceCount < state.budget.allowedDepth) {
|
| 110 |
+
const recurrenceResult = await stageD_AdaptiveRecurrence(
|
| 111 |
+
prompt, state.triPack, state.branchOutputs, state.scratchpad,
|
| 112 |
+
state.verification, state.budget, state.recurrenceCount, debug
|
| 113 |
+
);
|
| 114 |
+
|
| 115 |
+
state.branchOutputs = recurrenceResult.updatedBranches;
|
| 116 |
+
|
| 117 |
+
if (recurrenceResult.shouldRecur) {
|
| 118 |
+
state.recurrenceCount++;
|
| 119 |
+
state.verification = await stageC_Verification(
|
| 120 |
+
prompt, state.triPack, state.branchOutputs, state.scratchpad, debug
|
| 121 |
+
);
|
| 122 |
+
} else {
|
| 123 |
+
shouldContinue = false;
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
state.finalResponse = await stageFinal_GrandSynthesis(prompt, state, debug);
|
| 128 |
+
|
| 129 |
+
return state.finalResponse;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
async function main() {
|
| 133 |
+
const args = process.argv.slice(2);
|
| 134 |
+
const tier: NexusTier = args.includes("--tier")
|
| 135 |
+
? (args[args.indexOf("--tier") + 1] as NexusTier) || "high"
|
| 136 |
+
: "high";
|
| 137 |
+
const debug = args.includes("--debug");
|
| 138 |
+
|
| 139 |
+
console.log(`
|
| 140 |
+
${colors.bright}${colors.cyan}βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 141 |
+
β β
|
| 142 |
+
β ${colors.white} ββββββ βββββββ βββββββ βββββββ ${colors.cyan} β
|
| 143 |
+
β ${colors.white}ββββββββββββββββββββββββββββββββ${colors.cyan} β
|
| 144 |
+
β ${colors.white}βββββββββββββββββββ βββββββββββ${colors.cyan} β
|
| 145 |
+
β ${colors.white}βββββββββββββββββββ βββββββββββ${colors.cyan} β
|
| 146 |
+
β ${colors.white}βββ ββββββ ββββββββββββββ βββ${colors.cyan} β
|
| 147 |
+
β ${colors.white}βββ ββββββ ββββββββββ βββ βββ${colors.cyan} β
|
| 148 |
+
β β
|
| 149 |
+
β ${colors.yellow}Adaptive Recurrent Dendritic Reasoning${colors.cyan} β
|
| 150 |
+
β β
|
| 151 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${colors.reset}
|
| 152 |
+
|
| 153 |
+
${colors.dim}Tier: ${tier.toUpperCase()} | Debug: ${debug ? "ON" : "OFF"}${colors.reset}
|
| 154 |
+
${colors.dim}Type your query or 'exit' to quit. Use 'tier low|high|max' to switch.${colors.reset}
|
| 155 |
+
`);
|
| 156 |
+
|
| 157 |
+
const rl = readline.createInterface({
|
| 158 |
+
input: process.stdin,
|
| 159 |
+
output: process.stdout
|
| 160 |
+
});
|
| 161 |
+
|
| 162 |
+
let currentTier = tier;
|
| 163 |
+
let currentDebug = debug;
|
| 164 |
+
let isClosed = false;
|
| 165 |
+
|
| 166 |
+
rl.on('close', () => {
|
| 167 |
+
isClosed = true;
|
| 168 |
+
console.log(`\n${colors.cyan}Session ended.${colors.reset}\n`);
|
| 169 |
+
process.exit(0);
|
| 170 |
+
});
|
| 171 |
+
|
| 172 |
+
process.on('SIGINT', () => {
|
| 173 |
+
console.log(`\n${colors.cyan}Interrupted. Goodbye!${colors.reset}\n`);
|
| 174 |
+
rl.close();
|
| 175 |
+
process.exit(0);
|
| 176 |
+
});
|
| 177 |
+
|
| 178 |
+
const promptUser = () => {
|
| 179 |
+
if (isClosed) return;
|
| 180 |
+
|
| 181 |
+
rl.question(`\n${colors.bright}${colors.green}You [${currentTier.toUpperCase()}]>${colors.reset} `, async (input) => {
|
| 182 |
+
if (isClosed) return;
|
| 183 |
+
|
| 184 |
+
const trimmed = input?.trim() || "";
|
| 185 |
+
|
| 186 |
+
if (trimmed.toLowerCase() === "exit" || trimmed.toLowerCase() === "quit") {
|
| 187 |
+
console.log(`\n${colors.cyan}Goodbye!${colors.reset}\n`);
|
| 188 |
+
rl.close();
|
| 189 |
+
process.exit(0);
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
if (trimmed.toLowerCase().startsWith("tier ")) {
|
| 193 |
+
const newTier = trimmed.slice(5).toLowerCase() as NexusTier;
|
| 194 |
+
if (["low", "high", "max"].includes(newTier)) {
|
| 195 |
+
currentTier = newTier;
|
| 196 |
+
console.log(`${colors.green}Switched to ${newTier.toUpperCase()} mode${colors.reset}`);
|
| 197 |
+
} else {
|
| 198 |
+
console.log(`${colors.red}Invalid tier. Use: low, high, or max${colors.reset}`);
|
| 199 |
+
}
|
| 200 |
+
if (!isClosed) promptUser();
|
| 201 |
+
return;
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
if (trimmed.toLowerCase() === "debug") {
|
| 205 |
+
currentDebug = !currentDebug;
|
| 206 |
+
console.log(`${colors.yellow}Debug mode: ${currentDebug ? "ON" : "OFF"}${colors.reset}`);
|
| 207 |
+
if (!isClosed) promptUser();
|
| 208 |
+
return;
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
if (!trimmed) {
|
| 212 |
+
if (!isClosed) promptUser();
|
| 213 |
+
return;
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
try {
|
| 217 |
+
await runARDR(trimmed, currentTier, currentDebug);
|
| 218 |
+
} catch (error: any) {
|
| 219 |
+
console.log(`\n${colors.red}Error: ${error.message}${colors.reset}`);
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
if (!isClosed) promptUser();
|
| 223 |
+
});
|
| 224 |
+
};
|
| 225 |
+
|
| 226 |
+
promptUser();
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
main().catch(console.error);
|