feat(ai): add packages/ai provider layer (Chunk #8)

Self-contained SSE-based provider layer for OpenAI-compatible endpoints
(Ollama/llama.cpp/LM Studio/vLLM), Anthropic, Gemini, and OpenRouter.

- budget.ts: re-export core token-budget engine (countTokens,
  allocateBudget, DEFAULT_BUDGET)
- client.ts: streamCompletion/collectStream/complete with budget
  enforcement via enforceBudget + measureBudget
- discover.ts: auto-detect local providers by probing common /v1/models
- adapters.ts: per-provider param mapping + streaming adapters + factory
- thinking.ts: thinking/reasoning-param normalization per provider
- http.ts: SSE parsing + 429 retry
- errors.ts: BudgetExceededError

35 unit/integration tests pass; typecheck and lint clean.
This commit is contained in:
Schwitzz
2026-10-28 18:59:54 -05:00
parent 5c28a94813
commit 4c4b83bb68
16 changed files with 1795 additions and 0 deletions
+15
View File
@@ -0,0 +1,15 @@
/**
* Token budget re-exports.
*
* The budget engine lives in `packages/core` (the single source of truth for
* token accounting). This module re-exports it so the AI layer has a single
* import surface, per CHUNK#8.md §3.
*/
export {
countTokens,
allocateBudget,
DEFAULT_BUDGET,
} from "@pubscript/core";
export type { Budget } from "@pubscript/core";