/**
 * 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`. The loop halts at the
 * first snippet that would exceed the budget.
 *
 * @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) break;
		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;
