agentfootprint
Version:
The explainable agent framework — backtrack a wrong answer to the exact context that caused it (evidence, not guesses). Built on footprintjs.
54 lines (53 loc) • 2.33 kB
TypeScript
/**
* ToT — Tree of Thoughts: breadth-first iterative expansion + pruning.
*
* Paper: "Tree of Thoughts: Deliberate Problem Solving with Large
* Language Models" — Yao et al., 2023
* (https://arxiv.org/abs/2305.10601).
*
* Pattern: Factory → produces a `Runner` built from `Loop(depth times,
* Parallel(K thought branches) + prune-to-top-M)`.
* Role: patterns/ layer. Pure composition over existing primitives.
* Build-time-fixed depth + branching factor; runtime frontier
* is pruned to `beamWidth` per level via a consumer-supplied
* scorer.
*
* Tradeoff vs. full DFS: this shipped variant is BFS with constant
* width. True DFS with backtracking or adaptive branching factor would
* need runtime-variable Parallel (DynamicParallel) or recursion.
*/
import type { LLMProvider } from '../provider-contract/types.js';
import type { Runner } from '../core/runner.js';
export interface ToTOptions {
readonly provider: LLMProvider;
readonly model: string;
/** System prompt for the thought-generation LLMCall. */
readonly thoughtPrompt: string;
/** Depth of the tree (number of expansion iterations). */
readonly depth: number;
/** Branching factor — K thoughts generated per frontier node per iteration. */
readonly branchingFactor: number;
/**
* Scorer: given a thought, return a numeric score. Higher is better.
* The top `beamWidth` thoughts survive each level; the rest are pruned.
* Synchronous so pruning is deterministic.
*/
readonly score: (thought: string) => number;
/** Beam width — how many thoughts survive after each level. Default 1 (greedy). */
readonly beamWidth?: number;
readonly temperature?: number;
readonly maxTokens?: number;
readonly name?: string;
readonly id?: string;
}
/**
* Build a ToT Runner. At run time:
* 1. Seed — treat the input message as the initial frontier of 1 thought.
* 2. For each of `depth` iterations:
* a. Parallel fan-out: generate `branchingFactor` new thoughts.
* b. Score all new thoughts, keep top `beamWidth`, pass to next iteration.
* 3. Return the single best-scoring thought from the final frontier.
*/
export declare function tot(opts: ToTOptions): Runner<{
message: string;
}, string>;