feat(TRUEREF-0009-0010): implement indexing pipeline job queue and public REST API
- SQLite-backed job queue with sequential processing and startup recovery - Atomic snippet replacement in single transaction - context7-compatible GET /api/v1/libs/search and GET /api/v1/context - Token budget limiting and JSON/txt response format support - CORS headers on all API routes via SvelteKit handle hook - Library ID parser supporting /owner/repo and /owner/repo/version Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
36
src/lib/server/api/token-budget.ts
Normal file
36
src/lib/server/api/token-budget.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* 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;
|
||||
Reference in New Issue
Block a user