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:
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Error types for the AI provider layer.
|
||||
*
|
||||
* Errors are classified so callers can react appropriately: auth failures are
|
||||
* terminal (the key is wrong), rate-limit/overloaded errors are retryable, and
|
||||
* budget violations are a client-side guard.
|
||||
*
|
||||
* See CHUNK#8.md §3.
|
||||
*/
|
||||
|
||||
/** Base class for all provider-layer errors. */
|
||||
export class ProviderError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = "ProviderError";
|
||||
}
|
||||
}
|
||||
|
||||
/** The provider rejected the request due to missing/invalid credentials. */
|
||||
export class ProviderAuthError extends ProviderError {
|
||||
constructor(message = "Provider authentication failed (invalid API key).") {
|
||||
super(message);
|
||||
this.name = "ProviderAuthError";
|
||||
}
|
||||
}
|
||||
|
||||
/** The provider rate-limited or overloaded the request; safe to retry. */
|
||||
export class ProviderRateLimitError extends ProviderError {
|
||||
constructor(message = "Provider rate limit exceeded; retrying.") {
|
||||
super(message);
|
||||
this.name = "ProviderRateLimitError";
|
||||
}
|
||||
}
|
||||
|
||||
/** The request exceeded the configured token budget. */
|
||||
export class BudgetExceededError extends ProviderError {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = "BudgetExceededError";
|
||||
}
|
||||
}
|
||||
|
||||
/** No provider adapter is registered for the given kind. */
|
||||
export class UnknownProviderError extends ProviderError {
|
||||
constructor(kind: string) {
|
||||
super(`Unknown provider kind: ${kind}`);
|
||||
this.name = "UnknownProviderError";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user