@hilbras/sdk 3.1.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -4
- package/dist/adapters/anthropic.d.ts +0 -1
- package/dist/adapters/anthropic.js +0 -1
- package/dist/adapters/azure.d.ts +0 -1
- package/dist/adapters/azure.js +0 -1
- package/dist/adapters/bedrock.d.ts +0 -1
- package/dist/adapters/bedrock.js +0 -1
- package/dist/adapters/cerebras.d.ts +0 -1
- package/dist/adapters/cerebras.js +0 -1
- package/dist/adapters/cohere-rerank.d.ts +0 -1
- package/dist/adapters/cohere-rerank.js +0 -1
- package/dist/adapters/cohere.d.ts +0 -1
- package/dist/adapters/cohere.js +0 -1
- package/dist/adapters/deepgram.d.ts +0 -1
- package/dist/adapters/deepgram.js +0 -1
- package/dist/adapters/deepinfra.d.ts +0 -1
- package/dist/adapters/deepinfra.js +0 -1
- package/dist/adapters/deepseek.d.ts +0 -1
- package/dist/adapters/deepseek.js +0 -1
- package/dist/adapters/elevenlabs.d.ts +0 -1
- package/dist/adapters/elevenlabs.js +0 -1
- package/dist/adapters/extra.d.ts +0 -1
- package/dist/adapters/extra.js +0 -1
- package/dist/adapters/fireworks.d.ts +0 -1
- package/dist/adapters/fireworks.js +0 -1
- package/dist/adapters/google-genai.d.ts +0 -1
- package/dist/adapters/google-genai.js +0 -1
- package/dist/adapters/google-vertex.d.ts +0 -1
- package/dist/adapters/google-vertex.js +0 -1
- package/dist/adapters/groq.d.ts +0 -1
- package/dist/adapters/groq.js +0 -1
- package/dist/adapters/huggingface.d.ts +0 -1
- package/dist/adapters/huggingface.js +0 -1
- package/dist/adapters/mistral.d.ts +0 -1
- package/dist/adapters/mistral.js +0 -1
- package/dist/adapters/ollama.d.ts +0 -1
- package/dist/adapters/ollama.js +0 -1
- package/dist/adapters/openai-compatible.d.ts +0 -1
- package/dist/adapters/openai-compatible.js +0 -1
- package/dist/adapters/openai.d.ts +0 -1
- package/dist/adapters/openai.js +0 -1
- package/dist/adapters/perplexity.d.ts +0 -1
- package/dist/adapters/perplexity.js +0 -1
- package/dist/adapters/text-tool-call-parser.d.ts +0 -1
- package/dist/adapters/text-tool-call-parser.js +0 -1
- package/dist/adapters/together.d.ts +0 -1
- package/dist/adapters/together.js +0 -1
- package/dist/adapters/voyageai.d.ts +0 -1
- package/dist/adapters/voyageai.js +0 -1
- package/dist/adapters/xai.d.ts +0 -1
- package/dist/adapters/xai.js +0 -1
- package/dist/catalog/index.d.ts +0 -1
- package/dist/catalog/index.js +0 -1
- package/dist/catalog/models.d.ts +0 -1
- package/dist/catalog/models.js +0 -1
- package/dist/catalog/provider-catalog.d.ts +0 -1
- package/dist/catalog/provider-catalog.js +0 -1
- package/dist/client/client.d.ts +26 -11
- package/dist/client/client.js +153 -391
- package/dist/client/hooks.d.ts +0 -1
- package/dist/client/hooks.js +0 -1
- package/dist/config/config-resolver.d.ts +3 -0
- package/dist/config/config-resolver.js +173 -0
- package/dist/config/config-schema.d.ts +69 -0
- package/dist/config/config-schema.js +56 -0
- package/dist/config/config.d.ts +10 -18
- package/dist/config/config.js +37 -136
- package/dist/config/index.d.ts +11 -0
- package/dist/config/index.js +8 -0
- package/dist/config/prompts.d.ts +0 -1
- package/dist/config/prompts.js +0 -1
- package/dist/config/provider-config.d.ts +0 -1
- package/dist/config/provider-config.js +0 -1
- package/dist/config/resolved-config.d.ts +15 -0
- package/dist/config/resolved-config.js +60 -0
- package/dist/config/schema.d.ts +5 -60
- package/dist/config/schema.js +4 -29
- package/dist/config/sources/defaults.d.ts +2 -0
- package/dist/config/sources/defaults.js +10 -0
- package/dist/config/sources/environment.d.ts +4 -0
- package/dist/config/sources/environment.js +114 -0
- package/dist/config/sources/file.d.ts +6 -0
- package/dist/config/sources/file.js +33 -0
- package/dist/config/sources/runtime.d.ts +3 -0
- package/dist/config/sources/runtime.js +7 -0
- package/dist/core/execution/execution-result.d.ts +18 -0
- package/dist/core/execution/execution-result.js +12 -0
- package/dist/core/execution/ports.d.ts +54 -0
- package/dist/core/execution/ports.js +7 -0
- package/dist/core/execution/request-context.d.ts +63 -0
- package/dist/core/execution/request-context.js +48 -0
- package/dist/core/execution/request-executor.d.ts +49 -0
- package/dist/core/execution/request-executor.js +107 -0
- package/dist/core/execution/request-pipeline.d.ts +66 -0
- package/dist/core/execution/request-pipeline.js +730 -0
- package/dist/cost/alerts.d.ts +0 -1
- package/dist/cost/alerts.js +0 -1
- package/dist/cost/tracker.d.ts +0 -1
- package/dist/cost/tracker.js +0 -1
- package/dist/cost/types.d.ts +0 -1
- package/dist/cost/types.js +0 -1
- package/dist/credentials/oidc.d.ts +0 -1
- package/dist/credentials/oidc.js +0 -1
- package/dist/credentials/provider.d.ts +0 -1
- package/dist/credentials/provider.js +0 -1
- package/dist/devtools/dashboard.d.ts +0 -1
- package/dist/devtools/dashboard.js +0 -1
- package/dist/devtools/index.d.ts +0 -1
- package/dist/devtools/index.js +0 -1
- package/dist/errors/index.d.ts +0 -1
- package/dist/errors/index.js +0 -1
- package/dist/features/agent/index.d.ts +0 -1
- package/dist/features/agent/index.js +0 -1
- package/dist/features/agent/plan-and-execute.d.ts +0 -1
- package/dist/features/agent/plan-and-execute.js +0 -1
- package/dist/features/agent/react.d.ts +0 -1
- package/dist/features/agent/react.js +0 -1
- package/dist/features/agent/tool-loop.d.ts +0 -1
- package/dist/features/agent/tool-loop.js +0 -1
- package/dist/features/agent/types.d.ts +0 -1
- package/dist/features/agent/types.js +0 -1
- package/dist/features/eval/ab-test.d.ts +0 -1
- package/dist/features/eval/ab-test.js +0 -1
- package/dist/features/eval/evaluate.d.ts +0 -1
- package/dist/features/eval/evaluate.js +0 -1
- package/dist/features/eval/index.d.ts +0 -1
- package/dist/features/eval/index.js +0 -1
- package/dist/features/eval/metrics.d.ts +0 -1
- package/dist/features/eval/metrics.js +0 -1
- package/dist/features/eval/types.d.ts +0 -1
- package/dist/features/eval/types.js +0 -1
- package/dist/features/fine-tune/formatters.d.ts +0 -1
- package/dist/features/fine-tune/formatters.js +0 -1
- package/dist/features/fine-tune/index.d.ts +0 -1
- package/dist/features/fine-tune/index.js +0 -1
- package/dist/features/fine-tune/quality.d.ts +0 -1
- package/dist/features/fine-tune/quality.js +0 -1
- package/dist/features/fine-tune/split.d.ts +0 -1
- package/dist/features/fine-tune/split.js +0 -1
- package/dist/features/fine-tune/types.d.ts +0 -1
- package/dist/features/fine-tune/types.js +0 -1
- package/dist/features/rag/chunker.d.ts +0 -1
- package/dist/features/rag/chunker.js +0 -1
- package/dist/features/rag/in-memory-store.d.ts +0 -1
- package/dist/features/rag/in-memory-store.js +0 -1
- package/dist/features/rag/index.d.ts +0 -1
- package/dist/features/rag/index.js +0 -1
- package/dist/features/rag/pipeline.d.ts +0 -1
- package/dist/features/rag/pipeline.js +0 -1
- package/dist/features/rag/retriever.d.ts +0 -1
- package/dist/features/rag/retriever.js +0 -1
- package/dist/features/rag/vector-store.d.ts +0 -1
- package/dist/features/rag/vector-store.js +0 -1
- package/dist/frameworks/angular/angular-shim.d.ts +0 -1
- package/dist/frameworks/angular/angular-shim.js +0 -1
- package/dist/frameworks/angular/chat.service.d.ts +0 -1
- package/dist/frameworks/angular/chat.service.js +0 -1
- package/dist/frameworks/angular/completion.service.d.ts +0 -1
- package/dist/frameworks/angular/completion.service.js +0 -1
- package/dist/frameworks/angular/index.d.ts +0 -1
- package/dist/frameworks/angular/index.js +0 -1
- package/dist/frameworks/angular/object.service.d.ts +0 -1
- package/dist/frameworks/angular/object.service.js +0 -1
- package/dist/frameworks/astro/endpoints.d.ts +0 -1
- package/dist/frameworks/astro/endpoints.js +0 -1
- package/dist/frameworks/astro/hooks.d.ts +0 -1
- package/dist/frameworks/astro/hooks.js +0 -1
- package/dist/frameworks/astro/index.d.ts +0 -1
- package/dist/frameworks/astro/index.js +0 -1
- package/dist/frameworks/nextjs/client.d.ts +0 -1
- package/dist/frameworks/nextjs/client.js +0 -1
- package/dist/frameworks/nextjs/index.d.ts +0 -1
- package/dist/frameworks/nextjs/index.js +0 -1
- package/dist/frameworks/nextjs/route-handlers.d.ts +0 -1
- package/dist/frameworks/nextjs/route-handlers.js +0 -1
- package/dist/frameworks/nextjs/server.d.ts +0 -1
- package/dist/frameworks/nextjs/server.js +0 -1
- package/dist/frameworks/nextjs/signal-shim.d.ts +0 -1
- package/dist/frameworks/nextjs/signal-shim.js +0 -1
- package/dist/frameworks/nextjs/use-chat.d.ts +0 -1
- package/dist/frameworks/nextjs/use-chat.js +0 -1
- package/dist/frameworks/nextjs/use-completion.d.ts +0 -1
- package/dist/frameworks/nextjs/use-completion.js +0 -1
- package/dist/frameworks/qwik/index.d.ts +0 -1
- package/dist/frameworks/qwik/index.js +0 -1
- package/dist/frameworks/qwik/signal-shim.d.ts +0 -1
- package/dist/frameworks/qwik/signal-shim.js +0 -1
- package/dist/frameworks/qwik/stream-parser.d.ts +0 -1
- package/dist/frameworks/qwik/stream-parser.js +0 -1
- package/dist/frameworks/qwik/use-chat.d.ts +0 -1
- package/dist/frameworks/qwik/use-chat.js +0 -1
- package/dist/frameworks/qwik/use-completion.d.ts +0 -1
- package/dist/frameworks/qwik/use-completion.js +0 -1
- package/dist/frameworks/react/generative-ui.d.ts +0 -1
- package/dist/frameworks/react/generative-ui.js +0 -1
- package/dist/frameworks/react/index.d.ts +0 -1
- package/dist/frameworks/react/index.js +0 -1
- package/dist/frameworks/react/stream-parser.d.ts +0 -1
- package/dist/frameworks/react/stream-parser.js +0 -1
- package/dist/frameworks/react/use-chat.d.ts +0 -1
- package/dist/frameworks/react/use-chat.js +0 -1
- package/dist/frameworks/react/use-completion.d.ts +0 -1
- package/dist/frameworks/react/use-completion.js +0 -1
- package/dist/frameworks/react/use-object.d.ts +0 -1
- package/dist/frameworks/react/use-object.js +0 -1
- package/dist/frameworks/remix/actions.d.ts +0 -1
- package/dist/frameworks/remix/actions.js +0 -1
- package/dist/frameworks/remix/hooks.d.ts +0 -1
- package/dist/frameworks/remix/hooks.js +0 -1
- package/dist/frameworks/remix/index.d.ts +0 -1
- package/dist/frameworks/remix/index.js +0 -1
- package/dist/frameworks/solid/index.d.ts +0 -1
- package/dist/frameworks/solid/index.js +0 -1
- package/dist/frameworks/solid/solid-shim.d.ts +0 -1
- package/dist/frameworks/solid/solid-shim.js +0 -1
- package/dist/frameworks/solid/stream-parser.d.ts +0 -1
- package/dist/frameworks/solid/stream-parser.js +0 -1
- package/dist/frameworks/solid/use-chat.d.ts +0 -1
- package/dist/frameworks/solid/use-chat.js +0 -1
- package/dist/frameworks/solid/use-completion.d.ts +0 -1
- package/dist/frameworks/solid/use-completion.js +0 -1
- package/dist/frameworks/solid/use-object.d.ts +0 -1
- package/dist/frameworks/solid/use-object.js +0 -1
- package/dist/frameworks/svelte/index.d.ts +0 -1
- package/dist/frameworks/svelte/index.js +0 -1
- package/dist/frameworks/svelte/stream-parser.d.ts +0 -1
- package/dist/frameworks/svelte/stream-parser.js +0 -1
- package/dist/frameworks/svelte/use-chat.d.ts +0 -1
- package/dist/frameworks/svelte/use-chat.js +0 -1
- package/dist/frameworks/svelte/use-completion.d.ts +0 -1
- package/dist/frameworks/svelte/use-completion.js +0 -1
- package/dist/frameworks/svelte/use-object.d.ts +0 -1
- package/dist/frameworks/svelte/use-object.js +0 -1
- package/dist/frameworks/vue/index.d.ts +0 -1
- package/dist/frameworks/vue/index.js +0 -1
- package/dist/frameworks/vue/stream-parser.d.ts +0 -1
- package/dist/frameworks/vue/stream-parser.js +0 -1
- package/dist/frameworks/vue/use-chat.d.ts +0 -1
- package/dist/frameworks/vue/use-chat.js +0 -1
- package/dist/frameworks/vue/use-completion.d.ts +0 -1
- package/dist/frameworks/vue/use-completion.js +0 -1
- package/dist/frameworks/vue/use-object.d.ts +0 -1
- package/dist/frameworks/vue/use-object.js +0 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +6 -1
- package/dist/logging/logger.d.ts +0 -1
- package/dist/logging/logger.js +0 -1
- package/dist/mcp/index.d.ts +0 -1
- package/dist/mcp/index.js +0 -1
- package/dist/middleware/middleware.d.ts +0 -1
- package/dist/middleware/middleware.js +0 -1
- package/dist/output/schema-helpers.d.ts +0 -1
- package/dist/output/schema-helpers.js +0 -1
- package/dist/output/structured.d.ts +0 -1
- package/dist/output/structured.js +0 -1
- package/dist/plugin/registry.d.ts +0 -1
- package/dist/plugin/registry.js +0 -1
- package/dist/plugin/types.d.ts +0 -1
- package/dist/plugin/types.js +0 -1
- package/dist/providers/adapter-registry.d.ts +0 -1
- package/dist/providers/adapter-registry.js +0 -1
- package/dist/providers/registry.d.ts +0 -1
- package/dist/providers/registry.js +0 -1
- package/dist/realtime/index.d.ts +0 -1
- package/dist/realtime/index.js +0 -1
- package/dist/reasoning/normalizer.d.ts +0 -1
- package/dist/reasoning/normalizer.js +0 -1
- package/dist/reliability/backoff.d.ts +1 -2
- package/dist/reliability/backoff.js +21 -3
- package/dist/reliability/circuit-breaker.d.ts +0 -1
- package/dist/reliability/circuit-breaker.js +0 -1
- package/dist/reliability/degradation.d.ts +0 -1
- package/dist/reliability/degradation.js +0 -1
- package/dist/reliability/presets.d.ts +0 -1
- package/dist/reliability/presets.js +0 -1
- package/dist/reliability/retry.d.ts +0 -1
- package/dist/reliability/retry.js +0 -1
- package/dist/reliability/timeout.d.ts +12 -1
- package/dist/reliability/timeout.js +51 -12
- package/dist/router/model-router.d.ts +0 -1
- package/dist/router/model-router.js +0 -1
- package/dist/security/audit-logger.d.ts +0 -1
- package/dist/security/audit-logger.js +0 -1
- package/dist/security/pii-guard.d.ts +0 -1
- package/dist/security/pii-guard.js +0 -1
- package/dist/security/prompt-injection-guard.d.ts +0 -1
- package/dist/security/prompt-injection-guard.js +0 -1
- package/dist/security/rate-limiter.d.ts +0 -1
- package/dist/security/rate-limiter.js +0 -1
- package/dist/security/rbac.d.ts +0 -1
- package/dist/security/rbac.js +0 -1
- package/dist/security/request-signer.d.ts +0 -1
- package/dist/security/request-signer.js +0 -1
- package/dist/security/url-guard.d.ts +0 -1
- package/dist/security/url-guard.js +0 -1
- package/dist/telemetry/body-logger.d.ts +0 -1
- package/dist/telemetry/body-logger.js +0 -1
- package/dist/telemetry/dashboard.d.ts +0 -1
- package/dist/telemetry/dashboard.js +0 -1
- package/dist/telemetry/opentelemetry.d.ts +0 -1
- package/dist/telemetry/opentelemetry.js +0 -1
- package/dist/telemetry/sla.d.ts +0 -1
- package/dist/telemetry/sla.js +0 -1
- package/dist/telemetry/structured-logger.d.ts +0 -1
- package/dist/telemetry/structured-logger.js +0 -1
- package/dist/tokens/counter.d.ts +0 -1
- package/dist/tokens/counter.js +0 -1
- package/dist/tokens/prompt-cache.d.ts +0 -1
- package/dist/tokens/prompt-cache.js +0 -1
- package/dist/transport/fetch.d.ts +0 -1
- package/dist/transport/fetch.js +0 -1
- package/dist/transport/middleware-transport.d.ts +0 -1
- package/dist/transport/middleware-transport.js +0 -1
- package/dist/transport/transport.d.ts +0 -1
- package/dist/transport/transport.js +0 -1
- package/dist/transport/websocket.d.ts +0 -1
- package/dist/transport/websocket.js +0 -1
- package/dist/types/adapter.d.ts +0 -1
- package/dist/types/adapter.js +0 -1
- package/dist/types/execution.d.ts +0 -1
- package/dist/types/execution.js +0 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/index.js +0 -1
- package/dist/types/messages.d.ts +0 -1
- package/dist/types/messages.js +0 -1
- package/dist/types/models.d.ts +0 -1
- package/dist/types/models.js +0 -1
- package/dist/types/multi-modal.d.ts +0 -1
- package/dist/types/multi-modal.js +0 -1
- package/dist/types/observability.d.ts +0 -1
- package/dist/types/observability.js +0 -1
- package/dist/types/policy.d.ts +0 -1
- package/dist/types/policy.js +0 -1
- package/dist/types/providers.d.ts +0 -1
- package/dist/types/providers.js +0 -1
- package/dist/types/router.d.ts +0 -1
- package/dist/types/router.js +0 -1
- package/dist/types/schema.d.ts +0 -1
- package/dist/types/schema.js +0 -1
- package/dist/types/streams.d.ts +0 -1
- package/dist/types/streams.js +0 -1
- package/dist/types/tool-builder.d.ts +0 -1
- package/dist/types/tool-builder.js +0 -1
- package/dist/types/tools.d.ts +0 -1
- package/dist/types/tools.js +0 -1
- package/dist/types/ui-protocol.d.ts +0 -1
- package/dist/types/ui-protocol.js +0 -1
- package/dist/utils/id.d.ts +0 -1
- package/dist/utils/id.js +0 -1
- package/dist/utils/sse-writer.d.ts +0 -1
- package/dist/utils/sse-writer.js +0 -1
- package/dist/utils/sse.d.ts +0 -1
- package/dist/utils/sse.js +0 -1
- package/package.json +4 -4
- package/dist/adapters/anthropic.d.ts.map +0 -1
- package/dist/adapters/anthropic.js.map +0 -1
- package/dist/adapters/azure.d.ts.map +0 -1
- package/dist/adapters/azure.js.map +0 -1
- package/dist/adapters/bedrock.d.ts.map +0 -1
- package/dist/adapters/bedrock.js.map +0 -1
- package/dist/adapters/cerebras.d.ts.map +0 -1
- package/dist/adapters/cerebras.js.map +0 -1
- package/dist/adapters/cohere-rerank.d.ts.map +0 -1
- package/dist/adapters/cohere-rerank.js.map +0 -1
- package/dist/adapters/cohere.d.ts.map +0 -1
- package/dist/adapters/cohere.js.map +0 -1
- package/dist/adapters/deepgram.d.ts.map +0 -1
- package/dist/adapters/deepgram.js.map +0 -1
- package/dist/adapters/deepinfra.d.ts.map +0 -1
- package/dist/adapters/deepinfra.js.map +0 -1
- package/dist/adapters/deepseek.d.ts.map +0 -1
- package/dist/adapters/deepseek.js.map +0 -1
- package/dist/adapters/elevenlabs.d.ts.map +0 -1
- package/dist/adapters/elevenlabs.js.map +0 -1
- package/dist/adapters/extra.d.ts.map +0 -1
- package/dist/adapters/extra.js.map +0 -1
- package/dist/adapters/fireworks.d.ts.map +0 -1
- package/dist/adapters/fireworks.js.map +0 -1
- package/dist/adapters/google-genai.d.ts.map +0 -1
- package/dist/adapters/google-genai.js.map +0 -1
- package/dist/adapters/google-vertex.d.ts.map +0 -1
- package/dist/adapters/google-vertex.js.map +0 -1
- package/dist/adapters/groq.d.ts.map +0 -1
- package/dist/adapters/groq.js.map +0 -1
- package/dist/adapters/huggingface.d.ts.map +0 -1
- package/dist/adapters/huggingface.js.map +0 -1
- package/dist/adapters/mistral.d.ts.map +0 -1
- package/dist/adapters/mistral.js.map +0 -1
- package/dist/adapters/ollama.d.ts.map +0 -1
- package/dist/adapters/ollama.js.map +0 -1
- package/dist/adapters/openai-compatible.d.ts.map +0 -1
- package/dist/adapters/openai-compatible.js.map +0 -1
- package/dist/adapters/openai.d.ts.map +0 -1
- package/dist/adapters/openai.js.map +0 -1
- package/dist/adapters/perplexity.d.ts.map +0 -1
- package/dist/adapters/perplexity.js.map +0 -1
- package/dist/adapters/text-tool-call-parser.d.ts.map +0 -1
- package/dist/adapters/text-tool-call-parser.js.map +0 -1
- package/dist/adapters/together.d.ts.map +0 -1
- package/dist/adapters/together.js.map +0 -1
- package/dist/adapters/voyageai.d.ts.map +0 -1
- package/dist/adapters/voyageai.js.map +0 -1
- package/dist/adapters/xai.d.ts.map +0 -1
- package/dist/adapters/xai.js.map +0 -1
- package/dist/catalog/index.d.ts.map +0 -1
- package/dist/catalog/index.js.map +0 -1
- package/dist/catalog/models.d.ts.map +0 -1
- package/dist/catalog/models.js.map +0 -1
- package/dist/catalog/provider-catalog.d.ts.map +0 -1
- package/dist/catalog/provider-catalog.js.map +0 -1
- package/dist/client/client.d.ts.map +0 -1
- package/dist/client/client.js.map +0 -1
- package/dist/client/hooks.d.ts.map +0 -1
- package/dist/client/hooks.js.map +0 -1
- package/dist/config/config.d.ts.map +0 -1
- package/dist/config/config.js.map +0 -1
- package/dist/config/prompts.d.ts.map +0 -1
- package/dist/config/prompts.js.map +0 -1
- package/dist/config/provider-config.d.ts.map +0 -1
- package/dist/config/provider-config.js.map +0 -1
- package/dist/config/schema.d.ts.map +0 -1
- package/dist/config/schema.js.map +0 -1
- package/dist/cost/alerts.d.ts.map +0 -1
- package/dist/cost/alerts.js.map +0 -1
- package/dist/cost/tracker.d.ts.map +0 -1
- package/dist/cost/tracker.js.map +0 -1
- package/dist/cost/types.d.ts.map +0 -1
- package/dist/cost/types.js.map +0 -1
- package/dist/credentials/oidc.d.ts.map +0 -1
- package/dist/credentials/oidc.js.map +0 -1
- package/dist/credentials/provider.d.ts.map +0 -1
- package/dist/credentials/provider.js.map +0 -1
- package/dist/devtools/dashboard.d.ts.map +0 -1
- package/dist/devtools/dashboard.js.map +0 -1
- package/dist/devtools/index.d.ts.map +0 -1
- package/dist/devtools/index.js.map +0 -1
- package/dist/errors/index.d.ts.map +0 -1
- package/dist/errors/index.js.map +0 -1
- package/dist/features/agent/index.d.ts.map +0 -1
- package/dist/features/agent/index.js.map +0 -1
- package/dist/features/agent/plan-and-execute.d.ts.map +0 -1
- package/dist/features/agent/plan-and-execute.js.map +0 -1
- package/dist/features/agent/react.d.ts.map +0 -1
- package/dist/features/agent/react.js.map +0 -1
- package/dist/features/agent/tool-loop.d.ts.map +0 -1
- package/dist/features/agent/tool-loop.js.map +0 -1
- package/dist/features/agent/types.d.ts.map +0 -1
- package/dist/features/agent/types.js.map +0 -1
- package/dist/features/eval/ab-test.d.ts.map +0 -1
- package/dist/features/eval/ab-test.js.map +0 -1
- package/dist/features/eval/evaluate.d.ts.map +0 -1
- package/dist/features/eval/evaluate.js.map +0 -1
- package/dist/features/eval/index.d.ts.map +0 -1
- package/dist/features/eval/index.js.map +0 -1
- package/dist/features/eval/metrics.d.ts.map +0 -1
- package/dist/features/eval/metrics.js.map +0 -1
- package/dist/features/eval/types.d.ts.map +0 -1
- package/dist/features/eval/types.js.map +0 -1
- package/dist/features/fine-tune/formatters.d.ts.map +0 -1
- package/dist/features/fine-tune/formatters.js.map +0 -1
- package/dist/features/fine-tune/index.d.ts.map +0 -1
- package/dist/features/fine-tune/index.js.map +0 -1
- package/dist/features/fine-tune/quality.d.ts.map +0 -1
- package/dist/features/fine-tune/quality.js.map +0 -1
- package/dist/features/fine-tune/split.d.ts.map +0 -1
- package/dist/features/fine-tune/split.js.map +0 -1
- package/dist/features/fine-tune/types.d.ts.map +0 -1
- package/dist/features/fine-tune/types.js.map +0 -1
- package/dist/features/rag/chunker.d.ts.map +0 -1
- package/dist/features/rag/chunker.js.map +0 -1
- package/dist/features/rag/in-memory-store.d.ts.map +0 -1
- package/dist/features/rag/in-memory-store.js.map +0 -1
- package/dist/features/rag/index.d.ts.map +0 -1
- package/dist/features/rag/index.js.map +0 -1
- package/dist/features/rag/pipeline.d.ts.map +0 -1
- package/dist/features/rag/pipeline.js.map +0 -1
- package/dist/features/rag/retriever.d.ts.map +0 -1
- package/dist/features/rag/retriever.js.map +0 -1
- package/dist/features/rag/vector-store.d.ts.map +0 -1
- package/dist/features/rag/vector-store.js.map +0 -1
- package/dist/frameworks/angular/angular-shim.d.ts.map +0 -1
- package/dist/frameworks/angular/angular-shim.js.map +0 -1
- package/dist/frameworks/angular/chat.service.d.ts.map +0 -1
- package/dist/frameworks/angular/chat.service.js.map +0 -1
- package/dist/frameworks/angular/completion.service.d.ts.map +0 -1
- package/dist/frameworks/angular/completion.service.js.map +0 -1
- package/dist/frameworks/angular/index.d.ts.map +0 -1
- package/dist/frameworks/angular/index.js.map +0 -1
- package/dist/frameworks/angular/object.service.d.ts.map +0 -1
- package/dist/frameworks/angular/object.service.js.map +0 -1
- package/dist/frameworks/astro/endpoints.d.ts.map +0 -1
- package/dist/frameworks/astro/endpoints.js.map +0 -1
- package/dist/frameworks/astro/hooks.d.ts.map +0 -1
- package/dist/frameworks/astro/hooks.js.map +0 -1
- package/dist/frameworks/astro/index.d.ts.map +0 -1
- package/dist/frameworks/astro/index.js.map +0 -1
- package/dist/frameworks/nextjs/client.d.ts.map +0 -1
- package/dist/frameworks/nextjs/client.js.map +0 -1
- package/dist/frameworks/nextjs/index.d.ts.map +0 -1
- package/dist/frameworks/nextjs/index.js.map +0 -1
- package/dist/frameworks/nextjs/route-handlers.d.ts.map +0 -1
- package/dist/frameworks/nextjs/route-handlers.js.map +0 -1
- package/dist/frameworks/nextjs/server.d.ts.map +0 -1
- package/dist/frameworks/nextjs/server.js.map +0 -1
- package/dist/frameworks/nextjs/signal-shim.d.ts.map +0 -1
- package/dist/frameworks/nextjs/signal-shim.js.map +0 -1
- package/dist/frameworks/nextjs/use-chat.d.ts.map +0 -1
- package/dist/frameworks/nextjs/use-chat.js.map +0 -1
- package/dist/frameworks/nextjs/use-completion.d.ts.map +0 -1
- package/dist/frameworks/nextjs/use-completion.js.map +0 -1
- package/dist/frameworks/qwik/index.d.ts.map +0 -1
- package/dist/frameworks/qwik/index.js.map +0 -1
- package/dist/frameworks/qwik/signal-shim.d.ts.map +0 -1
- package/dist/frameworks/qwik/signal-shim.js.map +0 -1
- package/dist/frameworks/qwik/stream-parser.d.ts.map +0 -1
- package/dist/frameworks/qwik/stream-parser.js.map +0 -1
- package/dist/frameworks/qwik/use-chat.d.ts.map +0 -1
- package/dist/frameworks/qwik/use-chat.js.map +0 -1
- package/dist/frameworks/qwik/use-completion.d.ts.map +0 -1
- package/dist/frameworks/qwik/use-completion.js.map +0 -1
- package/dist/frameworks/react/generative-ui.d.ts.map +0 -1
- package/dist/frameworks/react/generative-ui.js.map +0 -1
- package/dist/frameworks/react/index.d.ts.map +0 -1
- package/dist/frameworks/react/index.js.map +0 -1
- package/dist/frameworks/react/stream-parser.d.ts.map +0 -1
- package/dist/frameworks/react/stream-parser.js.map +0 -1
- package/dist/frameworks/react/use-chat.d.ts.map +0 -1
- package/dist/frameworks/react/use-chat.js.map +0 -1
- package/dist/frameworks/react/use-completion.d.ts.map +0 -1
- package/dist/frameworks/react/use-completion.js.map +0 -1
- package/dist/frameworks/react/use-object.d.ts.map +0 -1
- package/dist/frameworks/react/use-object.js.map +0 -1
- package/dist/frameworks/remix/actions.d.ts.map +0 -1
- package/dist/frameworks/remix/actions.js.map +0 -1
- package/dist/frameworks/remix/hooks.d.ts.map +0 -1
- package/dist/frameworks/remix/hooks.js.map +0 -1
- package/dist/frameworks/remix/index.d.ts.map +0 -1
- package/dist/frameworks/remix/index.js.map +0 -1
- package/dist/frameworks/solid/index.d.ts.map +0 -1
- package/dist/frameworks/solid/index.js.map +0 -1
- package/dist/frameworks/solid/solid-shim.d.ts.map +0 -1
- package/dist/frameworks/solid/solid-shim.js.map +0 -1
- package/dist/frameworks/solid/stream-parser.d.ts.map +0 -1
- package/dist/frameworks/solid/stream-parser.js.map +0 -1
- package/dist/frameworks/solid/use-chat.d.ts.map +0 -1
- package/dist/frameworks/solid/use-chat.js.map +0 -1
- package/dist/frameworks/solid/use-completion.d.ts.map +0 -1
- package/dist/frameworks/solid/use-completion.js.map +0 -1
- package/dist/frameworks/solid/use-object.d.ts.map +0 -1
- package/dist/frameworks/solid/use-object.js.map +0 -1
- package/dist/frameworks/svelte/index.d.ts.map +0 -1
- package/dist/frameworks/svelte/index.js.map +0 -1
- package/dist/frameworks/svelte/stream-parser.d.ts.map +0 -1
- package/dist/frameworks/svelte/stream-parser.js.map +0 -1
- package/dist/frameworks/svelte/use-chat.d.ts.map +0 -1
- package/dist/frameworks/svelte/use-chat.js.map +0 -1
- package/dist/frameworks/svelte/use-completion.d.ts.map +0 -1
- package/dist/frameworks/svelte/use-completion.js.map +0 -1
- package/dist/frameworks/svelte/use-object.d.ts.map +0 -1
- package/dist/frameworks/svelte/use-object.js.map +0 -1
- package/dist/frameworks/vue/index.d.ts.map +0 -1
- package/dist/frameworks/vue/index.js.map +0 -1
- package/dist/frameworks/vue/stream-parser.d.ts.map +0 -1
- package/dist/frameworks/vue/stream-parser.js.map +0 -1
- package/dist/frameworks/vue/use-chat.d.ts.map +0 -1
- package/dist/frameworks/vue/use-chat.js.map +0 -1
- package/dist/frameworks/vue/use-completion.d.ts.map +0 -1
- package/dist/frameworks/vue/use-completion.js.map +0 -1
- package/dist/frameworks/vue/use-object.d.ts.map +0 -1
- package/dist/frameworks/vue/use-object.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/logging/logger.d.ts.map +0 -1
- package/dist/logging/logger.js.map +0 -1
- package/dist/mcp/index.d.ts.map +0 -1
- package/dist/mcp/index.js.map +0 -1
- package/dist/middleware/middleware.d.ts.map +0 -1
- package/dist/middleware/middleware.js.map +0 -1
- package/dist/output/schema-helpers.d.ts.map +0 -1
- package/dist/output/schema-helpers.js.map +0 -1
- package/dist/output/structured.d.ts.map +0 -1
- package/dist/output/structured.js.map +0 -1
- package/dist/plugin/registry.d.ts.map +0 -1
- package/dist/plugin/registry.js.map +0 -1
- package/dist/plugin/types.d.ts.map +0 -1
- package/dist/plugin/types.js.map +0 -1
- package/dist/providers/adapter-registry.d.ts.map +0 -1
- package/dist/providers/adapter-registry.js.map +0 -1
- package/dist/providers/registry.d.ts.map +0 -1
- package/dist/providers/registry.js.map +0 -1
- package/dist/realtime/index.d.ts.map +0 -1
- package/dist/realtime/index.js.map +0 -1
- package/dist/reasoning/normalizer.d.ts.map +0 -1
- package/dist/reasoning/normalizer.js.map +0 -1
- package/dist/reliability/backoff.d.ts.map +0 -1
- package/dist/reliability/backoff.js.map +0 -1
- package/dist/reliability/circuit-breaker.d.ts.map +0 -1
- package/dist/reliability/circuit-breaker.js.map +0 -1
- package/dist/reliability/degradation.d.ts.map +0 -1
- package/dist/reliability/degradation.js.map +0 -1
- package/dist/reliability/presets.d.ts.map +0 -1
- package/dist/reliability/presets.js.map +0 -1
- package/dist/reliability/retry.d.ts.map +0 -1
- package/dist/reliability/retry.js.map +0 -1
- package/dist/reliability/timeout.d.ts.map +0 -1
- package/dist/reliability/timeout.js.map +0 -1
- package/dist/router/model-router.d.ts.map +0 -1
- package/dist/router/model-router.js.map +0 -1
- package/dist/security/audit-logger.d.ts.map +0 -1
- package/dist/security/audit-logger.js.map +0 -1
- package/dist/security/pii-guard.d.ts.map +0 -1
- package/dist/security/pii-guard.js.map +0 -1
- package/dist/security/prompt-injection-guard.d.ts.map +0 -1
- package/dist/security/prompt-injection-guard.js.map +0 -1
- package/dist/security/rate-limiter.d.ts.map +0 -1
- package/dist/security/rate-limiter.js.map +0 -1
- package/dist/security/rbac.d.ts.map +0 -1
- package/dist/security/rbac.js.map +0 -1
- package/dist/security/request-signer.d.ts.map +0 -1
- package/dist/security/request-signer.js.map +0 -1
- package/dist/security/url-guard.d.ts.map +0 -1
- package/dist/security/url-guard.js.map +0 -1
- package/dist/telemetry/body-logger.d.ts.map +0 -1
- package/dist/telemetry/body-logger.js.map +0 -1
- package/dist/telemetry/dashboard.d.ts.map +0 -1
- package/dist/telemetry/dashboard.js.map +0 -1
- package/dist/telemetry/opentelemetry.d.ts.map +0 -1
- package/dist/telemetry/opentelemetry.js.map +0 -1
- package/dist/telemetry/sla.d.ts.map +0 -1
- package/dist/telemetry/sla.js.map +0 -1
- package/dist/telemetry/structured-logger.d.ts.map +0 -1
- package/dist/telemetry/structured-logger.js.map +0 -1
- package/dist/tokens/counter.d.ts.map +0 -1
- package/dist/tokens/counter.js.map +0 -1
- package/dist/tokens/prompt-cache.d.ts.map +0 -1
- package/dist/tokens/prompt-cache.js.map +0 -1
- package/dist/transport/fetch.d.ts.map +0 -1
- package/dist/transport/fetch.js.map +0 -1
- package/dist/transport/middleware-transport.d.ts.map +0 -1
- package/dist/transport/middleware-transport.js.map +0 -1
- package/dist/transport/transport.d.ts.map +0 -1
- package/dist/transport/transport.js.map +0 -1
- package/dist/transport/websocket.d.ts.map +0 -1
- package/dist/transport/websocket.js.map +0 -1
- package/dist/types/adapter.d.ts.map +0 -1
- package/dist/types/adapter.js.map +0 -1
- package/dist/types/execution.d.ts.map +0 -1
- package/dist/types/execution.js.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js.map +0 -1
- package/dist/types/messages.d.ts.map +0 -1
- package/dist/types/messages.js.map +0 -1
- package/dist/types/models.d.ts.map +0 -1
- package/dist/types/models.js.map +0 -1
- package/dist/types/multi-modal.d.ts.map +0 -1
- package/dist/types/multi-modal.js.map +0 -1
- package/dist/types/observability.d.ts.map +0 -1
- package/dist/types/observability.js.map +0 -1
- package/dist/types/policy.d.ts.map +0 -1
- package/dist/types/policy.js.map +0 -1
- package/dist/types/providers.d.ts.map +0 -1
- package/dist/types/providers.js.map +0 -1
- package/dist/types/router.d.ts.map +0 -1
- package/dist/types/router.js.map +0 -1
- package/dist/types/schema.d.ts.map +0 -1
- package/dist/types/schema.js.map +0 -1
- package/dist/types/streams.d.ts.map +0 -1
- package/dist/types/streams.js.map +0 -1
- package/dist/types/tool-builder.d.ts.map +0 -1
- package/dist/types/tool-builder.js.map +0 -1
- package/dist/types/tools.d.ts.map +0 -1
- package/dist/types/tools.js.map +0 -1
- package/dist/types/ui-protocol.d.ts.map +0 -1
- package/dist/types/ui-protocol.js.map +0 -1
- package/dist/utils/id.d.ts.map +0 -1
- package/dist/utils/id.js.map +0 -1
- package/dist/utils/sse-writer.d.ts.map +0 -1
- package/dist/utils/sse-writer.js.map +0 -1
- package/dist/utils/sse.d.ts.map +0 -1
- package/dist/utils/sse.js.map +0 -1
package/dist/client/client.js
CHANGED
|
@@ -9,21 +9,24 @@
|
|
|
9
9
|
* client.addProvider({ name: "OpenAI", baseUrl: "https://api.openai.com/v1", ... });
|
|
10
10
|
* for await (const chunk of client.stream({ provider: "OpenAI", model: "gpt-4", messages })) { ... }
|
|
11
11
|
*/
|
|
12
|
+
import { RequestExecutor } from "../core/execution/request-executor.js";
|
|
13
|
+
import { RequestPipeline } from "../core/execution/request-pipeline.js";
|
|
14
|
+
import { resolveConfig } from "../config/config-resolver.js";
|
|
15
|
+
import { budgetFromConfig, policyFromConfig } from "../config/resolved-config.js";
|
|
12
16
|
import { ProviderRegistry } from "../providers/registry.js";
|
|
13
17
|
import { cloneProviderConfig } from "../config/provider-config.js";
|
|
14
18
|
import { FetchTransport } from "../transport/fetch.js";
|
|
15
19
|
import { MiddlewareTransport } from "../transport/middleware-transport.js";
|
|
16
20
|
import { getCircuitBreakerRegistry } from "../reliability/circuit-breaker.js";
|
|
17
|
-
import {
|
|
21
|
+
import { shouldRetry, shouldRetryNetworkError } from "../reliability/retry.js";
|
|
18
22
|
import { calculateBackoff, sleep } from "../reliability/backoff.js";
|
|
19
|
-
import { ProviderNotFoundError, ModelNotFoundError, CircuitBreakerOpenError,
|
|
20
|
-
import { createTimeoutSignal } from "../reliability/timeout.js";
|
|
23
|
+
import { ProviderNotFoundError, ModelNotFoundError, CircuitBreakerOpenError, ConfigurationError } from "../errors/index.js";
|
|
21
24
|
import { getDefaultAdapterRegistry } from "../providers/adapter-registry.js";
|
|
22
25
|
import { dictToMessage } from "../types/messages.js";
|
|
23
26
|
import { validateBaseUrl } from "../security/url-guard.js";
|
|
24
27
|
import { resolvePolicy } from "../reliability/presets.js";
|
|
25
28
|
import { ModelRouter } from "../router/model-router.js";
|
|
26
|
-
import {
|
|
29
|
+
import { buildJsonModeParams } from "../output/structured.js";
|
|
27
30
|
import { ClientHooks } from "./hooks.js";
|
|
28
31
|
import { BudgetTracker } from "../cost/tracker.js";
|
|
29
32
|
import { estimateTokens } from "../tokens/counter.js";
|
|
@@ -35,6 +38,7 @@ export class HilbrasClient {
|
|
|
35
38
|
_adapterRegistry;
|
|
36
39
|
_adapters = new Map();
|
|
37
40
|
_defaultPolicy;
|
|
41
|
+
_resolvedConfig;
|
|
38
42
|
_router;
|
|
39
43
|
_hooks = new ClientHooks();
|
|
40
44
|
_requestCounter = 0;
|
|
@@ -43,6 +47,8 @@ export class HilbrasClient {
|
|
|
43
47
|
_allowPrivateNetwork;
|
|
44
48
|
_tokenizer;
|
|
45
49
|
_plugins = new PluginRegistry();
|
|
50
|
+
_requestExecutor;
|
|
51
|
+
_requestPipeline;
|
|
46
52
|
constructor(config) {
|
|
47
53
|
this._transport = config?.transport ?? new FetchTransport();
|
|
48
54
|
// v2.5.0: Wrap transport with middleware pipeline when provided
|
|
@@ -54,15 +60,42 @@ export class HilbrasClient {
|
|
|
54
60
|
this._allowInsecureUrls = config?.allowInsecureUrls ?? false;
|
|
55
61
|
this._allowPrivateNetwork = config?.allowPrivateNetwork ?? false;
|
|
56
62
|
this._tokenizer = config?.tokenizer ?? null;
|
|
57
|
-
//
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
63
|
+
// v3.3.0: resolve layered configuration exactly once at construction.
|
|
64
|
+
const inputConfig = config?.config ?? config?.sdkConfig;
|
|
65
|
+
this._resolvedConfig = config?.resolvedConfig ?? resolveConfig({
|
|
66
|
+
sources: config?.configSources,
|
|
67
|
+
overrides: inputConfig,
|
|
68
|
+
strict: true,
|
|
69
|
+
});
|
|
70
|
+
const resolvedSdk = this._resolvedConfig.values;
|
|
71
|
+
this._defaultPolicy = config?.policy ?? policyFromConfig(resolvedSdk);
|
|
72
|
+
const sdkBudget = budgetFromConfig(resolvedSdk);
|
|
62
73
|
this._budgetTracker = new BudgetTracker(config?.budget ?? sdkBudget);
|
|
74
|
+
this._requestExecutor = new RequestExecutor({
|
|
75
|
+
policy: {
|
|
76
|
+
resolve: (policy) => resolvePolicy(policy ?? this._defaultPolicy),
|
|
77
|
+
},
|
|
78
|
+
circuitBreakers: {
|
|
79
|
+
getOrCreate: (provider, circuitPolicy) => getCircuitBreakerRegistry().getOrCreate(provider, circuitPolicy),
|
|
80
|
+
},
|
|
81
|
+
adapters: {
|
|
82
|
+
get: (provider) => this._adapters.get(provider),
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
this._requestPipeline = new RequestPipeline({
|
|
86
|
+
executor: this._requestExecutor,
|
|
87
|
+
budget: this._budgetTracker,
|
|
88
|
+
plugins: this._plugins,
|
|
89
|
+
emit: (event) => this._emit(event),
|
|
90
|
+
now: () => performance.now(),
|
|
91
|
+
sleep: (ms, signal) => sleep(ms, signal),
|
|
92
|
+
onCircuitOpen: (requestId, provider) => {
|
|
93
|
+
this._emit({ type: "circuit_breaker.open", requestId, timestamp: performance.now(), provider });
|
|
94
|
+
},
|
|
95
|
+
});
|
|
63
96
|
// v1.1.0: Register providers from SDKConfig if provided
|
|
64
|
-
if (
|
|
65
|
-
for (const provider of
|
|
97
|
+
if (resolvedSdk.providers.length > 0) {
|
|
98
|
+
for (const provider of resolvedSdk.providers) {
|
|
66
99
|
if (!this._registry.get(provider.name)) {
|
|
67
100
|
this.addProvider(provider);
|
|
68
101
|
}
|
|
@@ -76,38 +109,6 @@ export class HilbrasClient {
|
|
|
76
109
|
config.telemetry.openTelemetry.instrumentClient(this);
|
|
77
110
|
}
|
|
78
111
|
}
|
|
79
|
-
/**
|
|
80
|
-
* Map the legacy SDKConfig fields (maxRetries, requestTimeoutMs,
|
|
81
|
-
* circuitBreakerEnabled/Threshold/ResetMs) to an ExecutionPolicy.
|
|
82
|
-
* Unrecognized fields are left undefined; the per-request policy
|
|
83
|
-
* resolver fills in defaults.
|
|
84
|
-
*/
|
|
85
|
-
_policyFromSDK(sdk) {
|
|
86
|
-
const policy = {};
|
|
87
|
-
if (sdk.maxRetries !== undefined) {
|
|
88
|
-
policy.retry = { maxRetries: sdk.maxRetries };
|
|
89
|
-
}
|
|
90
|
-
if (sdk.requestTimeoutMs !== undefined) {
|
|
91
|
-
policy.timeout = { requestTimeoutMs: sdk.requestTimeoutMs };
|
|
92
|
-
}
|
|
93
|
-
if (sdk.circuitBreakerEnabled !== undefined || sdk.circuitBreakerThreshold !== undefined || sdk.circuitBreakerResetMs !== undefined) {
|
|
94
|
-
policy.circuitBreaker = {
|
|
95
|
-
enabled: sdk.circuitBreakerEnabled,
|
|
96
|
-
failureThreshold: sdk.circuitBreakerThreshold,
|
|
97
|
-
timeoutMs: sdk.circuitBreakerResetMs,
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
return policy;
|
|
101
|
-
}
|
|
102
|
-
/** Map the legacy SDKConfig budget fields to a BudgetConfig. */
|
|
103
|
-
_budgetFromSDK(sdk) {
|
|
104
|
-
const budget = {};
|
|
105
|
-
if (sdk.sessionBudget !== undefined)
|
|
106
|
-
budget.sessionBudget = sdk.sessionBudget;
|
|
107
|
-
if (sdk.perRequestBudget !== undefined)
|
|
108
|
-
budget.perRequestBudget = sdk.perRequestBudget;
|
|
109
|
-
return budget;
|
|
110
|
-
}
|
|
111
112
|
/** Subscribe to lifecycle events. Returns an unsubscribe function. */
|
|
112
113
|
on(event, listener) {
|
|
113
114
|
return this._hooks.on(event, listener);
|
|
@@ -189,6 +190,14 @@ export class HilbrasClient {
|
|
|
189
190
|
listProviders() {
|
|
190
191
|
return this._registry.list();
|
|
191
192
|
}
|
|
193
|
+
/** Return a redacted snapshot of the resolved client configuration. */
|
|
194
|
+
getConfigSnapshot() {
|
|
195
|
+
return this._resolvedConfig.safeSnapshot();
|
|
196
|
+
}
|
|
197
|
+
/** Return configuration diagnostics without secret values. */
|
|
198
|
+
getConfigDiagnostics() {
|
|
199
|
+
return this._resolvedConfig.diagnostics.map((diagnostic) => ({ ...diagnostic }));
|
|
200
|
+
}
|
|
192
201
|
/**
|
|
193
202
|
* Add a provider from the built-in catalog with one line.
|
|
194
203
|
*
|
|
@@ -315,34 +324,31 @@ export class HilbrasClient {
|
|
|
315
324
|
* helper. Pure refactor: the returned values are byte-for-byte equivalent
|
|
316
325
|
* to what the inline code produced before PR-4.
|
|
317
326
|
*/
|
|
318
|
-
_prepareRequest(requestId, providerName, providerConfig, policy, userSignal) {
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
+
_prepareRequest(requestId, providerName, providerConfig, policy, userSignal, operation = "complete") {
|
|
328
|
+
try {
|
|
329
|
+
const prepared = this._requestExecutor.prepare({
|
|
330
|
+
requestId,
|
|
331
|
+
operation,
|
|
332
|
+
provider: providerName,
|
|
333
|
+
model: providerConfig.models[0]?.id ?? "",
|
|
334
|
+
policy,
|
|
335
|
+
providerTimeoutMs: providerConfig.timeout,
|
|
336
|
+
callerSignal: userSignal,
|
|
327
337
|
});
|
|
328
|
-
|
|
338
|
+
return {
|
|
339
|
+
resolved: prepared.context.policy,
|
|
340
|
+
circuitBreaker: prepared.circuitBreaker,
|
|
341
|
+
retryConfig: prepared.retryConfig,
|
|
342
|
+
signal: prepared.signal,
|
|
343
|
+
dispose: prepared.dispose,
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
catch (error) {
|
|
347
|
+
if (error instanceof CircuitBreakerOpenError) {
|
|
329
348
|
this._emit({ type: "circuit_breaker.open", requestId, timestamp: performance.now(), provider: providerName });
|
|
330
|
-
throw new CircuitBreakerOpenError(providerName, {
|
|
331
|
-
failureCount: circuitBreaker.stats.failureCount,
|
|
332
|
-
retryAfterMs: resolved.circuitBreaker.timeoutMs,
|
|
333
|
-
});
|
|
334
349
|
}
|
|
350
|
+
throw error;
|
|
335
351
|
}
|
|
336
|
-
const retryConfig = createRetryConfig({
|
|
337
|
-
maxRetries: resolved.retry.maxRetries,
|
|
338
|
-
retryableStatuses: resolved.retry.retryableStatuses,
|
|
339
|
-
retryableNetworkErrors: resolved.retry.retryableNetworkErrors,
|
|
340
|
-
});
|
|
341
|
-
const timeoutMs = resolved.timeout.requestTimeoutMs || providerConfig.timeout;
|
|
342
|
-
const signal = timeoutMs
|
|
343
|
-
? createTimeoutSignal({ requestTimeoutMs: timeoutMs }, userSignal)
|
|
344
|
-
: userSignal;
|
|
345
|
-
return { resolved, circuitBreaker, retryConfig, signal };
|
|
346
352
|
}
|
|
347
353
|
// ─── Provider/Model Resolution ──────────────────────────────────────────
|
|
348
354
|
_resolveProviderModel(params) {
|
|
@@ -383,14 +389,6 @@ export class HilbrasClient {
|
|
|
383
389
|
async *stream(params) {
|
|
384
390
|
const requestId = this._nextRequestId();
|
|
385
391
|
const startTime = performance.now();
|
|
386
|
-
let inputTokens;
|
|
387
|
-
let outputTokens;
|
|
388
|
-
let firstChunkEmitted = false;
|
|
389
|
-
let adapterStartTime = 0;
|
|
390
|
-
// Budget tracking for streaming. v0.9.3: stream() now reserves like complete().
|
|
391
|
-
let estimatedCost = 0;
|
|
392
|
-
let reservationActive = false;
|
|
393
|
-
let usageSettled = false;
|
|
394
392
|
this._emit({ type: "request.start", requestId, timestamp: startTime, provider: params.provider, model: params.model, task: params.task });
|
|
395
393
|
// Resolve provider + model — either explicit or via router
|
|
396
394
|
const { providerName, modelId } = this._resolveProviderModel(params);
|
|
@@ -399,170 +397,30 @@ export class HilbrasClient {
|
|
|
399
397
|
if (!model)
|
|
400
398
|
throw new ModelNotFoundError(modelId, providerName, providerConfig.models.map((m) => m.id));
|
|
401
399
|
this._emit({ type: "routing.resolved", requestId, timestamp: performance.now(), provider: providerName, model: modelId, score: 0, reasons: params.provider ? ["Explicit provider/model"] : ["Router selected"] });
|
|
402
|
-
const adapter = this._getAdapter(providerName);
|
|
403
|
-
// v0.10.0 PR-4: extract the duplicated policy/circuit-breaker/retry/signal
|
|
404
|
-
// setup into _prepareRequest so stream() and complete() share it.
|
|
405
|
-
const { resolved, circuitBreaker, retryConfig, signal } = this._prepareRequest(requestId, providerName, providerConfig, params.policy, params.signal);
|
|
406
400
|
const messages = this._normalizeMessages(params.messages);
|
|
407
|
-
|
|
408
|
-
|
|
401
|
+
const estimatedCost = this._budgetTracker.estimate(modelId, providerName, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
402
|
+
yield* this._requestPipeline.runStream({
|
|
409
403
|
requestId,
|
|
404
|
+
startTime,
|
|
410
405
|
provider: providerName,
|
|
411
406
|
model: modelId,
|
|
412
407
|
messages,
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
408
|
+
params: {
|
|
409
|
+
model: modelId,
|
|
410
|
+
messages,
|
|
411
|
+
temperature: params.temperature,
|
|
412
|
+
maxTokens: params.maxTokens,
|
|
413
|
+
tools: params.tools,
|
|
414
|
+
extra: params.extra,
|
|
415
|
+
},
|
|
416
|
+
estimatedCost,
|
|
417
|
+
policy: params.policy,
|
|
418
|
+
providerTimeoutMs: providerConfig.timeout,
|
|
419
|
+
callerSignal: params.signal,
|
|
420
|
+
fallbackCandidates: (excludeModels) => this._getFallbacks(excludeModels, params),
|
|
421
|
+
estimateFallbackCost: (candidate) => this._budgetTracker.estimate(candidate.model, candidate.provider, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0),
|
|
422
|
+
getProviderTimeout: (provider) => this._registry.get(provider)?.timeout,
|
|
416
423
|
});
|
|
417
|
-
// Atomic budget reservation before any provider call. Reserves the
|
|
418
|
-
// estimated cost for the entire retry+fallback chain; the reservation is
|
|
419
|
-
// converted to actual on the first usage chunk, or released on failure.
|
|
420
|
-
estimatedCost = this._budgetTracker.estimate(modelId, providerName, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
421
|
-
const initialReservation = this._budgetTracker.reserve(requestId, estimatedCost);
|
|
422
|
-
if (!initialReservation) {
|
|
423
|
-
throw new ConfigurationError(`Budget reservation rejected — estimated cost $${estimatedCost.toFixed(4)} would exceed budget`, `Remaining budget: $${this._budgetTracker.report().remainingBudget?.toFixed(4) ?? "unknown"}. Options: (1) increase sessionBudget, (2) use a cheaper model, (3) reduce input token count`);
|
|
424
|
-
}
|
|
425
|
-
reservationActive = true;
|
|
426
|
-
try {
|
|
427
|
-
for (let attempt = 0;; attempt++) {
|
|
428
|
-
try {
|
|
429
|
-
adapterStartTime = performance.now();
|
|
430
|
-
const gen = adapter.stream({
|
|
431
|
-
model: modelId,
|
|
432
|
-
messages,
|
|
433
|
-
temperature: params.temperature,
|
|
434
|
-
maxTokens: params.maxTokens,
|
|
435
|
-
tools: params.tools,
|
|
436
|
-
extra: params.extra,
|
|
437
|
-
signal,
|
|
438
|
-
});
|
|
439
|
-
for await (const chunk of gen) {
|
|
440
|
-
// Track first chunk latency
|
|
441
|
-
if (!firstChunkEmitted) {
|
|
442
|
-
firstChunkEmitted = true;
|
|
443
|
-
this._emit({ type: "stream.first_chunk", requestId, timestamp: performance.now(), latencyMs: performance.now() - adapterStartTime });
|
|
444
|
-
}
|
|
445
|
-
// Track usage tokens and convert reservation on first usage chunk
|
|
446
|
-
if (chunk.type === "usage") {
|
|
447
|
-
const inT = chunk.inputTokens;
|
|
448
|
-
const outT = chunk.outputTokens;
|
|
449
|
-
inputTokens = inT;
|
|
450
|
-
outputTokens = outT;
|
|
451
|
-
if (reservationActive && !usageSettled) {
|
|
452
|
-
const actualCost = this._budgetTracker.estimate(modelId, providerName, inT ?? 0, outT ?? 0);
|
|
453
|
-
this._budgetTracker.settle(requestId, actualCost, { provider: providerName, model: modelId, phase: "execute" });
|
|
454
|
-
reservationActive = false;
|
|
455
|
-
usageSettled = true;
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
yield chunk;
|
|
459
|
-
}
|
|
460
|
-
circuitBreaker?.recordSuccess();
|
|
461
|
-
// End-of-stream with no usage chunk: settle at the estimate
|
|
462
|
-
if (reservationActive) {
|
|
463
|
-
this._budgetTracker.settle(requestId, estimatedCost, { provider: providerName, model: modelId, phase: "execute" });
|
|
464
|
-
reservationActive = false;
|
|
465
|
-
}
|
|
466
|
-
this._emit({ type: "request.completed", requestId, timestamp: performance.now(), provider: providerName, model: modelId, durationMs: performance.now() - startTime, attempts: attempt + 1, inputTokens, outputTokens, structuredOutput: false });
|
|
467
|
-
// v3.0.0: Plugin onResponse hook
|
|
468
|
-
await this._plugins.fireResponse({ requestId, provider: providerName, model: modelId, durationMs: performance.now() - startTime, inputTokens, outputTokens, streaming: true });
|
|
469
|
-
return;
|
|
470
|
-
}
|
|
471
|
-
catch (err) {
|
|
472
|
-
// Once a chunk has escaped to the caller, retrying would duplicate
|
|
473
|
-
// output and potentially repeat tool side effects. Caller cancellation
|
|
474
|
-
// is terminal as well.
|
|
475
|
-
const callerAborted = params.signal?.aborted === true;
|
|
476
|
-
if (callerAborted || firstChunkEmitted) {
|
|
477
|
-
circuitBreaker?.recordFailure(err instanceof Error ? err : undefined);
|
|
478
|
-
this._emit({ type: "request.failed", requestId, timestamp: performance.now(), provider: providerName, model: modelId, durationMs: performance.now() - startTime, attempts: attempt + 1, error: err instanceof Error ? err.message : String(err) });
|
|
479
|
-
await this._plugins.fireError({ requestId, provider: providerName, model: modelId, error: err instanceof Error ? err : new Error(String(err)), durationMs: performance.now() - startTime, attempts: attempt + 1 });
|
|
480
|
-
throw err;
|
|
481
|
-
}
|
|
482
|
-
// Check if we should retry
|
|
483
|
-
const isNetworkError = err instanceof TypeError || (err instanceof Error && err.name === "AbortError");
|
|
484
|
-
const status = err.status;
|
|
485
|
-
if (isNetworkError && shouldRetryNetworkError(attempt, retryConfig)) {
|
|
486
|
-
const delay = calculateBackoff(attempt, resolved.backoff);
|
|
487
|
-
this._emit({ type: "request.retrying", requestId, timestamp: performance.now(), provider: providerName, attempt, delayMs: delay, reason: "network error" });
|
|
488
|
-
await sleep(delay);
|
|
489
|
-
continue;
|
|
490
|
-
}
|
|
491
|
-
if (typeof status === "number" && shouldRetry(status, attempt, retryConfig)) {
|
|
492
|
-
const delay = calculateBackoff(attempt, resolved.backoff);
|
|
493
|
-
this._emit({ type: "request.retrying", requestId, timestamp: performance.now(), provider: providerName, attempt, delayMs: delay, reason: `HTTP ${status}` });
|
|
494
|
-
await sleep(delay);
|
|
495
|
-
continue;
|
|
496
|
-
}
|
|
497
|
-
// Try fallback if allowed and we have candidates
|
|
498
|
-
if (resolved.allowFallback && attempt >= retryConfig.maxRetries) {
|
|
499
|
-
const fallbacks = this._getFallbacks([modelId], params);
|
|
500
|
-
let fallbackSucceeded = false;
|
|
501
|
-
for (const fb of fallbacks) {
|
|
502
|
-
this._emit({ type: "fallback.started", requestId, timestamp: performance.now(), originalProvider: providerName, originalModel: modelId, fallbackProvider: fb.provider, fallbackModel: fb.model });
|
|
503
|
-
// Release original reservation, re-reserve under fallback id
|
|
504
|
-
if (reservationActive) {
|
|
505
|
-
this._budgetTracker.release(requestId);
|
|
506
|
-
reservationActive = false;
|
|
507
|
-
}
|
|
508
|
-
const fbReservationId = `${requestId}_fb_${fb.model}`;
|
|
509
|
-
const fbEstimatedCost = this._budgetTracker.estimate(fb.model, fb.provider, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
510
|
-
const fbReservation = this._budgetTracker.reserve(fbReservationId, fbEstimatedCost);
|
|
511
|
-
if (!fbReservation)
|
|
512
|
-
continue; // budget exceeded for this fallback
|
|
513
|
-
let fbReservationActive = true;
|
|
514
|
-
try {
|
|
515
|
-
const fbAdapter = this._getAdapter(fb.provider);
|
|
516
|
-
const gen = fbAdapter.stream({ model: fb.model, messages, temperature: params.temperature, maxTokens: params.maxTokens, tools: params.tools, extra: params.extra, signal });
|
|
517
|
-
for await (const chunk of gen) {
|
|
518
|
-
if (chunk.type === "usage") {
|
|
519
|
-
const inT = chunk.inputTokens;
|
|
520
|
-
const outT = chunk.outputTokens;
|
|
521
|
-
if (fbReservationActive) {
|
|
522
|
-
const actualCost = this._budgetTracker.estimate(fb.model, fb.provider, inT ?? 0, outT ?? 0);
|
|
523
|
-
this._budgetTracker.settle(fbReservationId, actualCost, { provider: fb.provider, model: fb.model, phase: "fallback" });
|
|
524
|
-
fbReservationActive = false;
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
yield chunk;
|
|
528
|
-
}
|
|
529
|
-
// End-of-stream without usage chunk: settle at estimate
|
|
530
|
-
if (fbReservationActive) {
|
|
531
|
-
this._budgetTracker.settle(fbReservationId, fbEstimatedCost, { provider: fb.provider, model: fb.model, phase: "fallback" });
|
|
532
|
-
fbReservationActive = false;
|
|
533
|
-
}
|
|
534
|
-
this._emit({ type: "request.completed", requestId, timestamp: performance.now(), provider: fb.provider, model: fb.model, durationMs: performance.now() - startTime, attempts: attempt + 2, inputTokens, outputTokens, structuredOutput: false });
|
|
535
|
-
fallbackSucceeded = true;
|
|
536
|
-
return;
|
|
537
|
-
}
|
|
538
|
-
catch {
|
|
539
|
-
if (fbReservationActive) {
|
|
540
|
-
this._budgetTracker.release(fbReservationId);
|
|
541
|
-
fbReservationActive = false;
|
|
542
|
-
}
|
|
543
|
-
/* fallback also failed — continue to next */
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
if (fallbackSucceeded)
|
|
547
|
-
return; // unreachable but for type safety
|
|
548
|
-
}
|
|
549
|
-
circuitBreaker?.recordFailure(err instanceof Error ? err : undefined);
|
|
550
|
-
this._emit({ type: "request.failed", requestId, timestamp: performance.now(), provider: providerName, model: modelId, durationMs: performance.now() - startTime, attempts: attempt + 1, error: err instanceof Error ? err.message : String(err) });
|
|
551
|
-
// v3.0.0: Plugin onError hook
|
|
552
|
-
await this._plugins.fireError({ requestId, provider: providerName, model: modelId, error: err instanceof Error ? err : new Error(String(err)), durationMs: performance.now() - startTime, attempts: attempt + 1 });
|
|
553
|
-
throw err;
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
finally {
|
|
558
|
-
// Guarantee release on any path that did not settle (consumer break,
|
|
559
|
-
// signal abort, unexpected throw). Idempotent: settle/release are no-ops
|
|
560
|
-
// on missing reservations.
|
|
561
|
-
if (reservationActive) {
|
|
562
|
-
this._budgetTracker.release(requestId);
|
|
563
|
-
reservationActive = false;
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
424
|
}
|
|
567
425
|
// ─── Non-Streaming Completion ───────────────────────────────────────────
|
|
568
426
|
async complete(params) {
|
|
@@ -579,169 +437,74 @@ export class HilbrasClient {
|
|
|
579
437
|
const providerName = resolved_.providerName;
|
|
580
438
|
const modelId = resolved_.modelId;
|
|
581
439
|
this._emit({ type: "routing.resolved", requestId, timestamp: performance.now(), provider: providerName, model: modelId, score: 0, reasons: params.provider ? ["Explicit provider/model"] : ["Router selected"] });
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
{ role: "system", content: buildJsonSystemInstruction(outputConfig.schema) },
|
|
608
|
-
...messages,
|
|
609
|
-
];
|
|
610
|
-
}
|
|
611
|
-
const maxRepairAttempts = outputConfig?.maxRepairAttempts ?? 2;
|
|
612
|
-
// Budget: atomic reserve before execution
|
|
613
|
-
const estimatedCost = this._budgetTracker.estimate(modelId, providerName, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
614
|
-
const reservation = this._budgetTracker.reserve(requestId, estimatedCost);
|
|
615
|
-
if (!reservation) {
|
|
616
|
-
throw new ConfigurationError(`Budget reservation rejected — estimated cost $${estimatedCost.toFixed(4)} would exceed budget`, `Remaining budget: $${this._budgetTracker.report().remainingBudget?.toFixed(4) ?? "unknown"}. Options: (1) increase sessionBudget, (2) use a cheaper model, (3) reduce input token count`);
|
|
617
|
-
}
|
|
618
|
-
try {
|
|
619
|
-
for (let attempt = 0;; attempt++) {
|
|
620
|
-
try {
|
|
621
|
-
const result = await adapter.complete({
|
|
622
|
-
model: modelId,
|
|
623
|
-
messages: structuredMessages,
|
|
624
|
-
temperature: params.temperature,
|
|
625
|
-
maxTokens: params.maxTokens,
|
|
626
|
-
tools: params.tools,
|
|
627
|
-
extra: structuredExtra,
|
|
628
|
-
signal,
|
|
629
|
-
});
|
|
630
|
-
circuitBreaker?.recordSuccess();
|
|
631
|
-
// If no structured output, settle and return
|
|
632
|
-
if (!outputConfig) {
|
|
633
|
-
this._budgetTracker.settle(requestId, estimatedCost, { provider: providerName, model: modelId, phase: "execute" });
|
|
634
|
-
this._emit({ type: "request.completed", requestId, timestamp: performance.now(), provider: providerName, model: modelId, durationMs: performance.now() - startTime, attempts: attempt + 1, structuredOutput: false });
|
|
635
|
-
// v3.0.0: Plugin onResponse hook
|
|
636
|
-
await this._plugins.fireResponse({ requestId, provider: providerName, model: modelId, durationMs: performance.now() - startTime, streaming: false });
|
|
637
|
-
return result;
|
|
638
|
-
}
|
|
639
|
-
// Validate structured output
|
|
640
|
-
const json = extractJson(result);
|
|
641
|
-
const parsed = JSON.parse(json);
|
|
642
|
-
const validation = outputConfig.schema.safeParse(parsed);
|
|
643
|
-
if (validation.success) {
|
|
644
|
-
this._budgetTracker.settle(requestId, estimatedCost, { provider: providerName, model: modelId, phase: "execute" });
|
|
645
|
-
this._emit({ type: "structured.validate.pass", requestId, timestamp: performance.now() });
|
|
646
|
-
this._emit({ type: "request.completed", requestId, timestamp: performance.now(), provider: providerName, model: modelId, durationMs: performance.now() - startTime, attempts: attempt + 1, structuredOutput: true });
|
|
647
|
-
// v3.0.0: Plugin onResponse hook
|
|
648
|
-
await this._plugins.fireResponse({ requestId, provider: providerName, model: modelId, durationMs: performance.now() - startTime, streaming: false });
|
|
649
|
-
return validation.data;
|
|
650
|
-
}
|
|
651
|
-
// Validation failed — attempt repair
|
|
652
|
-
this._emit({ type: "structured.validate.fail", requestId, timestamp: performance.now(), attempt, error: validation.error instanceof Error ? validation.error.message : String(validation.error) });
|
|
653
|
-
if (attempt < maxRepairAttempts) {
|
|
654
|
-
const repairPrompt = buildRepairPrompt(validation.error, result, buildJsonSystemInstruction(outputConfig.schema), outputConfig.repairInstructions);
|
|
655
|
-
// Replace last user message with repair prompt
|
|
656
|
-
const lastUserIdx = structuredMessages.map((m) => m.role).lastIndexOf("user");
|
|
657
|
-
if (lastUserIdx >= 0) {
|
|
658
|
-
structuredMessages = [
|
|
659
|
-
...structuredMessages.slice(0, lastUserIdx),
|
|
660
|
-
{ role: "user", content: repairPrompt },
|
|
661
|
-
];
|
|
662
|
-
}
|
|
663
|
-
else {
|
|
664
|
-
structuredMessages = [...structuredMessages, { role: "user", content: repairPrompt }];
|
|
665
|
-
}
|
|
666
|
-
continue;
|
|
667
|
-
}
|
|
668
|
-
// Exhausted repair attempts
|
|
669
|
-
this._emit({ type: "request.failed", requestId, timestamp: performance.now(), provider: providerName, model: modelId, durationMs: performance.now() - startTime, attempts: attempt + 1, error: "Validation failed after repair attempts" });
|
|
670
|
-
throw new ValidationError(maxRepairAttempts + 1, validation.error, result, { requestId, model: modelId });
|
|
671
|
-
}
|
|
672
|
-
catch (err) {
|
|
673
|
-
// Don't retry validation errors through the retry loop
|
|
674
|
-
if (err instanceof ValidationError) {
|
|
675
|
-
throw err;
|
|
676
|
-
}
|
|
677
|
-
if (params.signal?.aborted) {
|
|
678
|
-
circuitBreaker?.recordFailure(err instanceof Error ? err : undefined);
|
|
679
|
-
this._emit({ type: "request.failed", requestId, timestamp: performance.now(), provider: providerName, model: modelId, durationMs: performance.now() - startTime, attempts: attempt + 1, error: err instanceof Error ? err.message : String(err) });
|
|
680
|
-
await this._plugins.fireError({ requestId, provider: providerName, model: modelId, error: err instanceof Error ? err : new Error(String(err)), durationMs: performance.now() - startTime, attempts: attempt + 1 });
|
|
681
|
-
throw err;
|
|
682
|
-
}
|
|
683
|
-
const isNetworkError = err instanceof TypeError || (err instanceof Error && err.name === "AbortError");
|
|
684
|
-
const status = err.status;
|
|
685
|
-
if (isNetworkError && shouldRetryNetworkError(attempt, retryConfig)) {
|
|
686
|
-
const delay = calculateBackoff(attempt, resolved.backoff);
|
|
687
|
-
this._emit({ type: "request.retrying", requestId, timestamp: performance.now(), provider: providerName, attempt, delayMs: delay, reason: "network error" });
|
|
688
|
-
await sleep(delay);
|
|
689
|
-
continue;
|
|
690
|
-
}
|
|
691
|
-
if (typeof status === "number" && shouldRetry(status, attempt, retryConfig)) {
|
|
692
|
-
const delay = calculateBackoff(attempt, resolved.backoff);
|
|
693
|
-
this._emit({ type: "request.retrying", requestId, timestamp: performance.now(), provider: providerName, attempt, delayMs: delay, reason: `HTTP ${status}` });
|
|
694
|
-
await sleep(delay);
|
|
695
|
-
continue;
|
|
696
|
-
}
|
|
697
|
-
// Try fallback if allowed and retries exhausted
|
|
698
|
-
if (resolved.allowFallback && attempt >= retryConfig.maxRetries && !(err instanceof ValidationError)) {
|
|
699
|
-
this._budgetTracker.release(requestId); // Release original reservation
|
|
700
|
-
const fallbacks = this._getFallbacks([modelId], params);
|
|
701
|
-
for (const fb of fallbacks) {
|
|
702
|
-
const fbEstimatedCost = this._budgetTracker.estimate(fb.model, fb.provider, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
703
|
-
const fbReservation = this._budgetTracker.reserve(`${requestId}_fb_${fb.model}`, fbEstimatedCost);
|
|
704
|
-
if (!fbReservation)
|
|
705
|
-
continue; // Budget exceeded — skip this fallback
|
|
706
|
-
try {
|
|
707
|
-
const fbAdapter = this._getAdapter(fb.provider);
|
|
708
|
-
const result = await fbAdapter.complete({ model: fb.model, messages: structuredMessages, temperature: params.temperature, maxTokens: params.maxTokens, tools: params.tools, extra: structuredExtra, signal });
|
|
709
|
-
this._budgetTracker.settle(`${requestId}_fb_${fb.model}`, fbEstimatedCost, { provider: fb.provider, model: fb.model, phase: "fallback" });
|
|
710
|
-
this._emit({ type: "request.completed", requestId, timestamp: performance.now(), provider: fb.provider, model: fb.model, durationMs: performance.now() - startTime, attempts: attempt + 2, structuredOutput: !!outputConfig });
|
|
711
|
-
if (!outputConfig)
|
|
712
|
-
return result;
|
|
713
|
-
const json = extractJson(result);
|
|
714
|
-
const parsed = JSON.parse(json);
|
|
715
|
-
const validation = outputConfig.schema.safeParse(parsed);
|
|
716
|
-
if (validation.success)
|
|
717
|
-
return validation.data;
|
|
718
|
-
throw new ValidationError(maxRepairAttempts + 1, validation.error, result, { requestId, model: fb.model });
|
|
719
|
-
}
|
|
720
|
-
catch {
|
|
721
|
-
this._budgetTracker.release(`${requestId}_fb_${fb.model}`);
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
// Release reservation on final failure
|
|
726
|
-
this._budgetTracker.release(requestId);
|
|
727
|
-
circuitBreaker?.recordFailure(err instanceof Error ? err : undefined);
|
|
728
|
-
this._emit({ type: "request.failed", requestId, timestamp: performance.now(), provider: providerName, model: modelId, durationMs: performance.now() - startTime, attempts: attempt + 1, error: err instanceof Error ? err.message : String(err) });
|
|
729
|
-
// v3.0.0: Plugin onError hook
|
|
730
|
-
await this._plugins.fireError({ requestId, provider: providerName, model: modelId, error: err instanceof Error ? err : new Error(String(err)), durationMs: performance.now() - startTime, attempts: attempt + 1 });
|
|
731
|
-
throw err;
|
|
732
|
-
}
|
|
733
|
-
}
|
|
440
|
+
if (!params.output) {
|
|
441
|
+
const messages = this._normalizeMessages(params.messages);
|
|
442
|
+
const estimatedCost = this._budgetTracker.estimate(modelId, providerName, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
443
|
+
return this._requestPipeline.runComplete({
|
|
444
|
+
requestId,
|
|
445
|
+
startTime,
|
|
446
|
+
provider: providerName,
|
|
447
|
+
model: modelId,
|
|
448
|
+
messages,
|
|
449
|
+
params: {
|
|
450
|
+
model: modelId,
|
|
451
|
+
messages,
|
|
452
|
+
temperature: params.temperature,
|
|
453
|
+
maxTokens: params.maxTokens,
|
|
454
|
+
tools: params.tools,
|
|
455
|
+
extra: params.extra,
|
|
456
|
+
},
|
|
457
|
+
estimatedCost,
|
|
458
|
+
policy: params.policy,
|
|
459
|
+
providerTimeoutMs: providerConfig.timeout,
|
|
460
|
+
callerSignal: params.signal,
|
|
461
|
+
fallbackCandidates: (excludeModels) => this._getFallbacks(excludeModels, params),
|
|
462
|
+
estimateFallbackCost: (candidate) => this._budgetTracker.estimate(candidate.model, candidate.provider, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0),
|
|
463
|
+
getProviderTimeout: (provider) => this._registry.get(provider)?.timeout,
|
|
464
|
+
});
|
|
734
465
|
}
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
this.
|
|
466
|
+
if (params.output) {
|
|
467
|
+
const messages = this._normalizeMessages(params.messages);
|
|
468
|
+
const estimatedCost = this._budgetTracker.estimate(modelId, providerName, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
469
|
+
return this._requestPipeline.runStructuredComplete({
|
|
470
|
+
requestId,
|
|
471
|
+
startTime,
|
|
472
|
+
provider: providerName,
|
|
473
|
+
model: modelId,
|
|
474
|
+
messages,
|
|
475
|
+
params: {
|
|
476
|
+
model: modelId,
|
|
477
|
+
messages,
|
|
478
|
+
temperature: params.temperature,
|
|
479
|
+
maxTokens: params.maxTokens,
|
|
480
|
+
tools: params.tools,
|
|
481
|
+
extra: params.extra,
|
|
482
|
+
},
|
|
483
|
+
estimatedCost,
|
|
484
|
+
policy: params.policy,
|
|
485
|
+
providerTimeoutMs: providerConfig.timeout,
|
|
486
|
+
callerSignal: params.signal,
|
|
487
|
+
output: params.output,
|
|
488
|
+
jsonModeParams: (provider) => {
|
|
489
|
+
const adapter = this._adapters.get(provider);
|
|
490
|
+
return adapter ? buildJsonModeParams(adapter.id) : {};
|
|
491
|
+
},
|
|
492
|
+
fallbackCandidates: (excludeModels) => this._getFallbacks(excludeModels, { ...params, needsStructuredOutput: true }),
|
|
493
|
+
estimateFallbackCost: (candidate) => this._budgetTracker.estimate(candidate.model, candidate.provider, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0),
|
|
494
|
+
getProviderTimeout: (provider) => this._registry.get(provider)?.timeout,
|
|
495
|
+
});
|
|
739
496
|
}
|
|
497
|
+
throw new Error("Unreachable completion state");
|
|
740
498
|
}
|
|
741
499
|
// ─── Multi-Modal Reliability Pipeline ──────────────────────────────────
|
|
742
500
|
/**
|
|
743
501
|
* Run a multi-modal adapter method with retry, circuit breaker, timeout,
|
|
744
|
-
* and hook events
|
|
502
|
+
* and hook events.
|
|
503
|
+
*
|
|
504
|
+
* v3.2.0 intentionally keeps multimodal calls outside the logical budget
|
|
505
|
+
* pipeline until provider-specific pricing and reservation semantics are
|
|
506
|
+
* defined. They share the one-attempt reliability preparation, but do not
|
|
507
|
+
* silently reserve or settle new costs.
|
|
745
508
|
*/
|
|
746
509
|
async _runMultiModal(requestId, providerName, modelId, _operation, fn, userSignal) {
|
|
747
510
|
const startTime = performance.now();
|
|
@@ -1119,4 +882,3 @@ export class HilbrasClient {
|
|
|
1119
882
|
await this.dispose();
|
|
1120
883
|
}
|
|
1121
884
|
}
|
|
1122
|
-
//# sourceMappingURL=client.js.map
|
package/dist/client/hooks.d.ts
CHANGED