/** * Token budget selection for context responses. * * Implements a greedy selection algorithm: snippets are added in ranked order * until adding the next snippet would exceed the token budget. */ import type { Snippet } from '$lib/types'; /** * Select snippets from a ranked list up to a maximum token budget. * * Snippets are evaluated in order. A snippet is included when its token count * does not push the running total past `maxTokens`. Oversized snippets are * skipped so lower-ranked results can still be considered. * * @param snippets - Ranked list of snippets (best first). * @param maxTokens - Inclusive upper bound on total token count. * @returns The largest prefix of `snippets` whose combined token count * does not exceed `maxTokens`. */ export function selectSnippetsWithinBudget(snippets: Snippet[], maxTokens: number): Snippet[] { const selected: Snippet[] = []; let usedTokens = 0; for (const snippet of snippets) { if (usedTokens + (snippet.tokenCount ?? 0) > maxTokens) continue; selected.push(snippet); usedTokens += snippet.tokenCount ?? 0; } return selected; } /** Default token budget when the caller does not specify `tokens`. */ export const DEFAULT_TOKEN_BUDGET = 10_000;