@hilbras/sdk 3.0.0 → 3.2.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 +69 -34
- package/dist/adapters/anthropic.d.ts +0 -2
- package/dist/adapters/anthropic.js +4 -4
- package/dist/adapters/azure.d.ts +0 -2
- package/dist/adapters/azure.js +4 -3
- package/dist/adapters/bedrock.d.ts +0 -2
- package/dist/adapters/bedrock.js +4 -5
- 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 +2 -2
- package/dist/adapters/deepgram.d.ts +0 -1
- package/dist/adapters/deepgram.js +4 -5
- 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 +2 -0
- package/dist/adapters/extra.js +34 -0
- package/dist/adapters/fireworks.d.ts +0 -1
- package/dist/adapters/fireworks.js +0 -1
- package/dist/adapters/google-genai.d.ts +0 -2
- package/dist/adapters/google-genai.js +2 -3
- package/dist/adapters/google-vertex.d.ts +0 -2
- package/dist/adapters/google-vertex.js +2 -3
- package/dist/adapters/groq.d.ts +0 -2
- package/dist/adapters/groq.js +4 -4
- package/dist/adapters/huggingface.d.ts +0 -2
- package/dist/adapters/huggingface.js +2 -3
- package/dist/adapters/mistral.d.ts +0 -1
- package/dist/adapters/mistral.js +0 -1
- package/dist/adapters/ollama.d.ts +0 -2
- package/dist/adapters/ollama.js +4 -4
- package/dist/adapters/openai-compatible.d.ts +0 -3
- package/dist/adapters/openai-compatible.js +4 -3
- package/dist/adapters/openai.d.ts +0 -2
- package/dist/adapters/openai.js +4 -3
- 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 +1 -2
- package/dist/catalog/models.d.ts +0 -1
- package/dist/catalog/models.js +11 -9
- package/dist/catalog/provider-catalog.d.ts +228 -0
- package/dist/catalog/provider-catalog.js +1493 -0
- package/dist/client/client.d.ts +9 -3
- package/dist/client/client.js +194 -356
- package/dist/client/hooks.d.ts +0 -1
- package/dist/client/hooks.js +0 -1
- package/dist/config/config.d.ts +0 -1
- package/dist/config/config.js +0 -1
- package/dist/config/prompts.d.ts +0 -1
- package/dist/config/prompts.js +0 -1
- package/dist/config/provider-config.d.ts +2 -1
- package/dist/config/provider-config.js +12 -2
- package/dist/config/schema.d.ts +0 -1
- package/dist/config/schema.js +0 -1
- 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 +1 -1
- package/dist/cost/alerts.js +18 -6
- package/dist/cost/tracker.d.ts +0 -1
- package/dist/cost/tracker.js +8 -9
- 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 +6 -1
- package/dist/features/rag/in-memory-store.d.ts +0 -1
- package/dist/features/rag/in-memory-store.js +2 -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 +3 -7
- package/dist/features/rag/retriever.d.ts +5 -1
- package/dist/features/rag/retriever.js +22 -26
- 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 +2 -1
- package/dist/index.js +3 -1
- package/dist/logging/logger.d.ts +4 -47
- package/dist/logging/logger.js +9 -64
- package/dist/mcp/index.d.ts +0 -1
- package/dist/mcp/index.js +0 -1
- package/dist/middleware/middleware.d.ts +1 -2
- package/dist/middleware/middleware.js +28 -11
- 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 +3 -1
- package/dist/plugin/registry.js +8 -7
- 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 +9 -8
- 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 +5 -2
- 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 +2 -1
- package/dist/security/audit-logger.js +39 -24
- package/dist/security/pii-guard.d.ts +0 -1
- package/dist/security/pii-guard.js +21 -6
- package/dist/security/prompt-injection-guard.d.ts +0 -1
- package/dist/security/prompt-injection-guard.js +22 -5
- 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 +110 -28
- package/dist/telemetry/body-logger.d.ts +0 -1
- package/dist/telemetry/body-logger.js +18 -10
- 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 +20 -8
- 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 +48 -17
- 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 +3 -2
- package/dist/types/tool-builder.js +3 -2
- package/dist/types/tools.d.ts +0 -3
- package/dist/types/tools.js +1 -12
- 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 +124 -108
- 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/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.json +0 -246
- 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/client/pipeline.d.ts +0 -134
- package/dist/client/pipeline.d.ts.map +0 -1
- package/dist/client/pipeline.js +0 -222
- package/dist/client/pipeline.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/memory/index.d.ts +0 -3
- package/dist/features/memory/index.d.ts.map +0 -1
- package/dist/features/memory/index.js +0 -2
- package/dist/features/memory/index.js.map +0 -1
- package/dist/features/memory/memory.d.ts +0 -46
- package/dist/features/memory/memory.d.ts.map +0 -1
- package/dist/features/memory/memory.js +0 -152
- package/dist/features/memory/memory.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/index.d.ts +0 -9
- package/dist/plugin/index.d.ts.map +0 -1
- package/dist/plugin/index.js +0 -8
- package/dist/plugin/index.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/index.d.ts +0 -18
- package/dist/security/index.d.ts.map +0 -1
- package/dist/security/index.js +0 -12
- package/dist/security/index.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/index.d.ts +0 -15
- package/dist/telemetry/index.d.ts.map +0 -1
- package/dist/telemetry/index.js +0 -11
- package/dist/telemetry/index.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,24 +9,26 @@
|
|
|
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";
|
|
12
14
|
import { ProviderRegistry } from "../providers/registry.js";
|
|
15
|
+
import { cloneProviderConfig } from "../config/provider-config.js";
|
|
13
16
|
import { FetchTransport } from "../transport/fetch.js";
|
|
14
17
|
import { MiddlewareTransport } from "../transport/middleware-transport.js";
|
|
15
18
|
import { getCircuitBreakerRegistry } from "../reliability/circuit-breaker.js";
|
|
16
|
-
import {
|
|
19
|
+
import { shouldRetry, shouldRetryNetworkError } from "../reliability/retry.js";
|
|
17
20
|
import { calculateBackoff, sleep } from "../reliability/backoff.js";
|
|
18
|
-
import { ProviderNotFoundError, ModelNotFoundError, CircuitBreakerOpenError,
|
|
19
|
-
import { createTimeoutSignal } from "../reliability/timeout.js";
|
|
21
|
+
import { ProviderNotFoundError, ModelNotFoundError, CircuitBreakerOpenError, ConfigurationError } from "../errors/index.js";
|
|
20
22
|
import { getDefaultAdapterRegistry } from "../providers/adapter-registry.js";
|
|
21
23
|
import { dictToMessage } from "../types/messages.js";
|
|
22
24
|
import { validateBaseUrl } from "../security/url-guard.js";
|
|
23
25
|
import { resolvePolicy } from "../reliability/presets.js";
|
|
24
26
|
import { ModelRouter } from "../router/model-router.js";
|
|
25
|
-
import {
|
|
27
|
+
import { buildJsonModeParams } from "../output/structured.js";
|
|
26
28
|
import { ClientHooks } from "./hooks.js";
|
|
27
29
|
import { BudgetTracker } from "../cost/tracker.js";
|
|
28
30
|
import { estimateTokens } from "../tokens/counter.js";
|
|
29
|
-
import { getProviderCatalog, getModelsForProvider } from "../catalog/index.js";
|
|
31
|
+
import { getProviderCatalog, getModelsForProvider, listProviders } from "../catalog/index.js";
|
|
30
32
|
import { PluginRegistry } from "../plugin/registry.js";
|
|
31
33
|
export class HilbrasClient {
|
|
32
34
|
_registry = new ProviderRegistry();
|
|
@@ -42,6 +44,8 @@ export class HilbrasClient {
|
|
|
42
44
|
_allowPrivateNetwork;
|
|
43
45
|
_tokenizer;
|
|
44
46
|
_plugins = new PluginRegistry();
|
|
47
|
+
_requestExecutor;
|
|
48
|
+
_requestPipeline;
|
|
45
49
|
constructor(config) {
|
|
46
50
|
this._transport = config?.transport ?? new FetchTransport();
|
|
47
51
|
// v2.5.0: Wrap transport with middleware pipeline when provided
|
|
@@ -59,6 +63,28 @@ export class HilbrasClient {
|
|
|
59
63
|
this._defaultPolicy = config?.policy ?? (sdk ? this._policyFromSDK(sdk) : undefined);
|
|
60
64
|
const sdkBudget = sdk ? this._budgetFromSDK(sdk) : undefined;
|
|
61
65
|
this._budgetTracker = new BudgetTracker(config?.budget ?? sdkBudget);
|
|
66
|
+
this._requestExecutor = new RequestExecutor({
|
|
67
|
+
policy: {
|
|
68
|
+
resolve: (policy) => resolvePolicy(policy ?? this._defaultPolicy),
|
|
69
|
+
},
|
|
70
|
+
circuitBreakers: {
|
|
71
|
+
getOrCreate: (provider, circuitPolicy) => getCircuitBreakerRegistry().getOrCreate(provider, circuitPolicy),
|
|
72
|
+
},
|
|
73
|
+
adapters: {
|
|
74
|
+
get: (provider) => this._adapters.get(provider),
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
this._requestPipeline = new RequestPipeline({
|
|
78
|
+
executor: this._requestExecutor,
|
|
79
|
+
budget: this._budgetTracker,
|
|
80
|
+
plugins: this._plugins,
|
|
81
|
+
emit: (event) => this._emit(event),
|
|
82
|
+
now: () => performance.now(),
|
|
83
|
+
sleep: (ms, signal) => sleep(ms, signal),
|
|
84
|
+
onCircuitOpen: (requestId, provider) => {
|
|
85
|
+
this._emit({ type: "circuit_breaker.open", requestId, timestamp: performance.now(), provider });
|
|
86
|
+
},
|
|
87
|
+
});
|
|
62
88
|
// v1.1.0: Register providers from SDKConfig if provided
|
|
63
89
|
if (sdk?.providers?.length) {
|
|
64
90
|
for (const provider of sdk.providers) {
|
|
@@ -136,7 +162,8 @@ export class HilbrasClient {
|
|
|
136
162
|
*/
|
|
137
163
|
async use(...plugins) {
|
|
138
164
|
await this._plugins.add(...plugins);
|
|
139
|
-
|
|
165
|
+
for (const plugin of plugins)
|
|
166
|
+
await this._plugins.setup(plugin, this);
|
|
140
167
|
}
|
|
141
168
|
_nextRequestId() {
|
|
142
169
|
return `req_${++this._requestCounter}`;
|
|
@@ -158,20 +185,22 @@ export class HilbrasClient {
|
|
|
158
185
|
}
|
|
159
186
|
// ─── Provider Management ────────────────────────────────────────────────
|
|
160
187
|
addProvider(config) {
|
|
188
|
+
const ownedConfig = cloneProviderConfig(config);
|
|
161
189
|
// v0.9.3: SSRF guard. Per-provider `allowInsecure` overrides client-wide
|
|
162
|
-
// `allowInsecureUrls
|
|
163
|
-
const guard = validateBaseUrl(
|
|
164
|
-
allowInsecure:
|
|
190
|
+
// `allowInsecureUrls`; local targets require explicit opt-in.
|
|
191
|
+
const guard = validateBaseUrl(ownedConfig.baseUrl, {
|
|
192
|
+
allowInsecure: ownedConfig.allowInsecure ?? this._allowInsecureUrls,
|
|
165
193
|
allowPrivateNetwork: this._allowPrivateNetwork,
|
|
166
194
|
});
|
|
167
195
|
if (!guard.ok) {
|
|
168
|
-
throw new ConfigurationError(`Refused to register provider '${
|
|
196
|
+
throw new ConfigurationError(`Refused to register provider '${ownedConfig.name}': ${guard.reason}`);
|
|
169
197
|
}
|
|
170
|
-
this.
|
|
171
|
-
|
|
172
|
-
provider: config,
|
|
198
|
+
const adapter = this._adapterRegistry.create(ownedConfig.adapter, {
|
|
199
|
+
provider: ownedConfig,
|
|
173
200
|
transport: this._transport,
|
|
174
|
-
})
|
|
201
|
+
});
|
|
202
|
+
this._registry.add(ownedConfig);
|
|
203
|
+
this._adapters.set(ownedConfig.name, adapter);
|
|
175
204
|
this._router.updateProviders(this._registry.list().map((p) => p.name));
|
|
176
205
|
}
|
|
177
206
|
removeProvider(name) {
|
|
@@ -194,10 +223,11 @@ export class HilbrasClient {
|
|
|
194
223
|
* ```
|
|
195
224
|
*/
|
|
196
225
|
addProviderFromCatalog(providerId, modelId, apiKey) {
|
|
197
|
-
const
|
|
226
|
+
const catalogProviderId = listProviders().find((id) => id.toLowerCase() === providerId.toLowerCase()) ?? providerId;
|
|
227
|
+
const provider = getProviderCatalog(catalogProviderId);
|
|
198
228
|
if (!provider)
|
|
199
229
|
throw new ConfigurationError(`Unknown provider: ${providerId}`, `Available catalog providers: openai, anthropic, google-vertex, google-genai, groq, mistral, deepseek, cohere, huggingface, ollama, bedrock, azure, elevenlabs, voyageai`);
|
|
200
|
-
const models = getModelsForProvider(
|
|
230
|
+
const models = getModelsForProvider(catalogProviderId);
|
|
201
231
|
const model = models.find((m) => m.id === modelId);
|
|
202
232
|
if (!model)
|
|
203
233
|
throw new ConfigurationError(`Model ${modelId} not found for provider ${providerId}`, `Available models for ${providerId}: ${models.map((m) => m.id).join(", ")}`);
|
|
@@ -212,10 +242,11 @@ export class HilbrasClient {
|
|
|
212
242
|
id: model.id,
|
|
213
243
|
contextWindow: model.contextWindow,
|
|
214
244
|
maxOutputTokens: model.maxOutput,
|
|
215
|
-
capabilities: this._parseCapabilities(model.capabilities,
|
|
245
|
+
capabilities: this._parseCapabilities(model.capabilities, catalogProviderId),
|
|
216
246
|
},
|
|
217
247
|
],
|
|
218
248
|
});
|
|
249
|
+
return provider.name;
|
|
219
250
|
}
|
|
220
251
|
/** Access the adapter registry for plugins */
|
|
221
252
|
get adapterRegistry() {
|
|
@@ -309,34 +340,31 @@ export class HilbrasClient {
|
|
|
309
340
|
* helper. Pure refactor: the returned values are byte-for-byte equivalent
|
|
310
341
|
* to what the inline code produced before PR-4.
|
|
311
342
|
*/
|
|
312
|
-
_prepareRequest(requestId, providerName, providerConfig, policy, userSignal) {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
343
|
+
_prepareRequest(requestId, providerName, providerConfig, policy, userSignal, operation = "complete") {
|
|
344
|
+
try {
|
|
345
|
+
const prepared = this._requestExecutor.prepare({
|
|
346
|
+
requestId,
|
|
347
|
+
operation,
|
|
348
|
+
provider: providerName,
|
|
349
|
+
model: providerConfig.models[0]?.id ?? "",
|
|
350
|
+
policy,
|
|
351
|
+
providerTimeoutMs: providerConfig.timeout,
|
|
352
|
+
callerSignal: userSignal,
|
|
321
353
|
});
|
|
322
|
-
|
|
354
|
+
return {
|
|
355
|
+
resolved: prepared.context.policy,
|
|
356
|
+
circuitBreaker: prepared.circuitBreaker,
|
|
357
|
+
retryConfig: prepared.retryConfig,
|
|
358
|
+
signal: prepared.signal,
|
|
359
|
+
dispose: prepared.dispose,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
catch (error) {
|
|
363
|
+
if (error instanceof CircuitBreakerOpenError) {
|
|
323
364
|
this._emit({ type: "circuit_breaker.open", requestId, timestamp: performance.now(), provider: providerName });
|
|
324
|
-
throw new CircuitBreakerOpenError(providerName, {
|
|
325
|
-
failureCount: circuitBreaker.stats.failureCount,
|
|
326
|
-
retryAfterMs: resolved.circuitBreaker.timeoutMs,
|
|
327
|
-
});
|
|
328
365
|
}
|
|
366
|
+
throw error;
|
|
329
367
|
}
|
|
330
|
-
const retryConfig = createRetryConfig({
|
|
331
|
-
maxRetries: resolved.retry.maxRetries,
|
|
332
|
-
retryableStatuses: resolved.retry.retryableStatuses,
|
|
333
|
-
retryableNetworkErrors: resolved.retry.retryableNetworkErrors,
|
|
334
|
-
});
|
|
335
|
-
const timeoutMs = resolved.timeout.requestTimeoutMs || providerConfig.timeout;
|
|
336
|
-
const signal = timeoutMs
|
|
337
|
-
? createTimeoutSignal({ requestTimeoutMs: timeoutMs }, userSignal)
|
|
338
|
-
: userSignal;
|
|
339
|
-
return { resolved, circuitBreaker, retryConfig, signal };
|
|
340
368
|
}
|
|
341
369
|
// ─── Provider/Model Resolution ──────────────────────────────────────────
|
|
342
370
|
_resolveProviderModel(params) {
|
|
@@ -377,14 +405,6 @@ export class HilbrasClient {
|
|
|
377
405
|
async *stream(params) {
|
|
378
406
|
const requestId = this._nextRequestId();
|
|
379
407
|
const startTime = performance.now();
|
|
380
|
-
let inputTokens;
|
|
381
|
-
let outputTokens;
|
|
382
|
-
let firstChunkEmitted = false;
|
|
383
|
-
let adapterStartTime = 0;
|
|
384
|
-
// Budget tracking for streaming. v0.9.3: stream() now reserves like complete().
|
|
385
|
-
let estimatedCost = 0;
|
|
386
|
-
let reservationActive = false;
|
|
387
|
-
let usageSettled = false;
|
|
388
408
|
this._emit({ type: "request.start", requestId, timestamp: startTime, provider: params.provider, model: params.model, task: params.task });
|
|
389
409
|
// Resolve provider + model — either explicit or via router
|
|
390
410
|
const { providerName, modelId } = this._resolveProviderModel(params);
|
|
@@ -393,160 +413,30 @@ export class HilbrasClient {
|
|
|
393
413
|
if (!model)
|
|
394
414
|
throw new ModelNotFoundError(modelId, providerName, providerConfig.models.map((m) => m.id));
|
|
395
415
|
this._emit({ type: "routing.resolved", requestId, timestamp: performance.now(), provider: providerName, model: modelId, score: 0, reasons: params.provider ? ["Explicit provider/model"] : ["Router selected"] });
|
|
396
|
-
const adapter = this._getAdapter(providerName);
|
|
397
|
-
// v0.10.0 PR-4: extract the duplicated policy/circuit-breaker/retry/signal
|
|
398
|
-
// setup into _prepareRequest so stream() and complete() share it.
|
|
399
|
-
const { resolved, circuitBreaker, retryConfig, signal } = this._prepareRequest(requestId, providerName, providerConfig, params.policy, params.signal);
|
|
400
416
|
const messages = this._normalizeMessages(params.messages);
|
|
401
|
-
|
|
402
|
-
|
|
417
|
+
const estimatedCost = this._budgetTracker.estimate(modelId, providerName, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
418
|
+
yield* this._requestPipeline.runStream({
|
|
403
419
|
requestId,
|
|
420
|
+
startTime,
|
|
404
421
|
provider: providerName,
|
|
405
422
|
model: modelId,
|
|
406
423
|
messages,
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
424
|
+
params: {
|
|
425
|
+
model: modelId,
|
|
426
|
+
messages,
|
|
427
|
+
temperature: params.temperature,
|
|
428
|
+
maxTokens: params.maxTokens,
|
|
429
|
+
tools: params.tools,
|
|
430
|
+
extra: params.extra,
|
|
431
|
+
},
|
|
432
|
+
estimatedCost,
|
|
433
|
+
policy: params.policy,
|
|
434
|
+
providerTimeoutMs: providerConfig.timeout,
|
|
435
|
+
callerSignal: params.signal,
|
|
436
|
+
fallbackCandidates: (excludeModels) => this._getFallbacks(excludeModels, params),
|
|
437
|
+
estimateFallbackCost: (candidate) => this._budgetTracker.estimate(candidate.model, candidate.provider, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0),
|
|
438
|
+
getProviderTimeout: (provider) => this._registry.get(provider)?.timeout,
|
|
410
439
|
});
|
|
411
|
-
// Atomic budget reservation before any provider call. Reserves the
|
|
412
|
-
// estimated cost for the entire retry+fallback chain; the reservation is
|
|
413
|
-
// converted to actual on the first usage chunk, or released on failure.
|
|
414
|
-
estimatedCost = this._budgetTracker.estimate(modelId, providerName, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
415
|
-
const initialReservation = this._budgetTracker.reserve(requestId, estimatedCost);
|
|
416
|
-
if (!initialReservation) {
|
|
417
|
-
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`);
|
|
418
|
-
}
|
|
419
|
-
reservationActive = true;
|
|
420
|
-
try {
|
|
421
|
-
for (let attempt = 0;; attempt++) {
|
|
422
|
-
try {
|
|
423
|
-
adapterStartTime = performance.now();
|
|
424
|
-
const gen = adapter.stream({
|
|
425
|
-
model: modelId,
|
|
426
|
-
messages,
|
|
427
|
-
temperature: params.temperature,
|
|
428
|
-
maxTokens: params.maxTokens,
|
|
429
|
-
tools: params.tools,
|
|
430
|
-
extra: params.extra,
|
|
431
|
-
signal,
|
|
432
|
-
});
|
|
433
|
-
for await (const chunk of gen) {
|
|
434
|
-
// Track first chunk latency
|
|
435
|
-
if (!firstChunkEmitted) {
|
|
436
|
-
firstChunkEmitted = true;
|
|
437
|
-
this._emit({ type: "stream.first_chunk", requestId, timestamp: performance.now(), latencyMs: performance.now() - adapterStartTime });
|
|
438
|
-
}
|
|
439
|
-
// Track usage tokens and convert reservation on first usage chunk
|
|
440
|
-
if (chunk.type === "usage") {
|
|
441
|
-
const inT = chunk.inputTokens;
|
|
442
|
-
const outT = chunk.outputTokens;
|
|
443
|
-
inputTokens = inT;
|
|
444
|
-
outputTokens = outT;
|
|
445
|
-
if (reservationActive && !usageSettled) {
|
|
446
|
-
const actualCost = this._budgetTracker.estimate(modelId, providerName, inT ?? 0, outT ?? 0);
|
|
447
|
-
this._budgetTracker.settle(requestId, actualCost, { provider: providerName, model: modelId, phase: "execute" });
|
|
448
|
-
reservationActive = false;
|
|
449
|
-
usageSettled = true;
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
yield chunk;
|
|
453
|
-
}
|
|
454
|
-
circuitBreaker?.recordSuccess();
|
|
455
|
-
// End-of-stream with no usage chunk: settle at the estimate
|
|
456
|
-
if (reservationActive) {
|
|
457
|
-
this._budgetTracker.settle(requestId, estimatedCost, { provider: providerName, model: modelId, phase: "execute" });
|
|
458
|
-
reservationActive = false;
|
|
459
|
-
}
|
|
460
|
-
this._emit({ type: "request.completed", requestId, timestamp: performance.now(), provider: providerName, model: modelId, durationMs: performance.now() - startTime, attempts: attempt + 1, inputTokens, outputTokens, structuredOutput: false });
|
|
461
|
-
// v3.0.0: Plugin onResponse hook
|
|
462
|
-
await this._plugins.fireResponse({ requestId, provider: providerName, model: modelId, durationMs: performance.now() - startTime, inputTokens, outputTokens, streaming: true });
|
|
463
|
-
return;
|
|
464
|
-
}
|
|
465
|
-
catch (err) {
|
|
466
|
-
// Check if we should retry
|
|
467
|
-
const isNetworkError = err instanceof TypeError || (err instanceof Error && err.name === "AbortError");
|
|
468
|
-
const status = err.status;
|
|
469
|
-
if (isNetworkError && shouldRetryNetworkError(attempt, retryConfig)) {
|
|
470
|
-
const delay = calculateBackoff(attempt, resolved.backoff);
|
|
471
|
-
this._emit({ type: "request.retrying", requestId, timestamp: performance.now(), provider: providerName, attempt, delayMs: delay, reason: "network error" });
|
|
472
|
-
await sleep(delay);
|
|
473
|
-
continue;
|
|
474
|
-
}
|
|
475
|
-
if (typeof status === "number" && shouldRetry(status, attempt, retryConfig)) {
|
|
476
|
-
const delay = calculateBackoff(attempt, resolved.backoff);
|
|
477
|
-
this._emit({ type: "request.retrying", requestId, timestamp: performance.now(), provider: providerName, attempt, delayMs: delay, reason: `HTTP ${status}` });
|
|
478
|
-
await sleep(delay);
|
|
479
|
-
continue;
|
|
480
|
-
}
|
|
481
|
-
// Try fallback if allowed and we have candidates
|
|
482
|
-
if (resolved.allowFallback && attempt >= retryConfig.maxRetries) {
|
|
483
|
-
const fallbacks = this._getFallbacks([modelId], params);
|
|
484
|
-
let fallbackSucceeded = false;
|
|
485
|
-
for (const fb of fallbacks) {
|
|
486
|
-
this._emit({ type: "fallback.started", requestId, timestamp: performance.now(), originalProvider: providerName, originalModel: modelId, fallbackProvider: fb.provider, fallbackModel: fb.model });
|
|
487
|
-
// Release original reservation, re-reserve under fallback id
|
|
488
|
-
if (reservationActive) {
|
|
489
|
-
this._budgetTracker.release(requestId);
|
|
490
|
-
reservationActive = false;
|
|
491
|
-
}
|
|
492
|
-
const fbReservationId = `${requestId}_fb_${fb.model}`;
|
|
493
|
-
const fbEstimatedCost = this._budgetTracker.estimate(fb.model, fb.provider, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
494
|
-
const fbReservation = this._budgetTracker.reserve(fbReservationId, fbEstimatedCost);
|
|
495
|
-
if (!fbReservation)
|
|
496
|
-
continue; // budget exceeded for this fallback
|
|
497
|
-
let fbReservationActive = true;
|
|
498
|
-
try {
|
|
499
|
-
const fbAdapter = this._getAdapter(fb.provider);
|
|
500
|
-
const gen = fbAdapter.stream({ model: fb.model, messages, temperature: params.temperature, maxTokens: params.maxTokens, tools: params.tools, extra: params.extra, signal });
|
|
501
|
-
for await (const chunk of gen) {
|
|
502
|
-
if (chunk.type === "usage") {
|
|
503
|
-
const inT = chunk.inputTokens;
|
|
504
|
-
const outT = chunk.outputTokens;
|
|
505
|
-
if (fbReservationActive) {
|
|
506
|
-
const actualCost = this._budgetTracker.estimate(fb.model, fb.provider, inT ?? 0, outT ?? 0);
|
|
507
|
-
this._budgetTracker.settle(fbReservationId, actualCost, { provider: fb.provider, model: fb.model, phase: "fallback" });
|
|
508
|
-
fbReservationActive = false;
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
yield chunk;
|
|
512
|
-
}
|
|
513
|
-
// End-of-stream without usage chunk: settle at estimate
|
|
514
|
-
if (fbReservationActive) {
|
|
515
|
-
this._budgetTracker.settle(fbReservationId, fbEstimatedCost, { provider: fb.provider, model: fb.model, phase: "fallback" });
|
|
516
|
-
fbReservationActive = false;
|
|
517
|
-
}
|
|
518
|
-
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 });
|
|
519
|
-
fallbackSucceeded = true;
|
|
520
|
-
return;
|
|
521
|
-
}
|
|
522
|
-
catch {
|
|
523
|
-
if (fbReservationActive) {
|
|
524
|
-
this._budgetTracker.release(fbReservationId);
|
|
525
|
-
fbReservationActive = false;
|
|
526
|
-
}
|
|
527
|
-
/* fallback also failed — continue to next */
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
if (fallbackSucceeded)
|
|
531
|
-
return; // unreachable but for type safety
|
|
532
|
-
}
|
|
533
|
-
circuitBreaker?.recordFailure(err instanceof Error ? err : undefined);
|
|
534
|
-
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) });
|
|
535
|
-
// v3.0.0: Plugin onError hook
|
|
536
|
-
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 });
|
|
537
|
-
throw err;
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
finally {
|
|
542
|
-
// Guarantee release on any path that did not settle (consumer break,
|
|
543
|
-
// signal abort, unexpected throw). Idempotent: settle/release are no-ops
|
|
544
|
-
// on missing reservations.
|
|
545
|
-
if (reservationActive) {
|
|
546
|
-
this._budgetTracker.release(requestId);
|
|
547
|
-
reservationActive = false;
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
440
|
}
|
|
551
441
|
// ─── Non-Streaming Completion ───────────────────────────────────────────
|
|
552
442
|
async complete(params) {
|
|
@@ -557,161 +447,80 @@ export class HilbrasClient {
|
|
|
557
447
|
const resolved_ = this._resolveProviderModel({ ...params, hasOutput: !!params.output });
|
|
558
448
|
const providerConfig = this._registry.getOrThrow(resolved_.providerName);
|
|
559
449
|
const model = providerConfig.models.find((m) => m.id === resolved_.modelId);
|
|
560
|
-
if (!model)
|
|
450
|
+
if (!model && providerConfig.models.length > 0) {
|
|
561
451
|
throw new ModelNotFoundError(resolved_.modelId, resolved_.providerName, providerConfig.models.map((m) => m.id));
|
|
452
|
+
}
|
|
562
453
|
const providerName = resolved_.providerName;
|
|
563
454
|
const modelId = resolved_.modelId;
|
|
564
455
|
this._emit({ type: "routing.resolved", requestId, timestamp: performance.now(), provider: providerName, model: modelId, score: 0, reasons: params.provider ? ["Explicit provider/model"] : ["Router selected"] });
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
{ role: "system", content: buildJsonSystemInstruction(outputConfig.schema) },
|
|
591
|
-
...messages,
|
|
592
|
-
];
|
|
593
|
-
}
|
|
594
|
-
const maxRepairAttempts = outputConfig?.maxRepairAttempts ?? 2;
|
|
595
|
-
// Budget: atomic reserve before execution
|
|
596
|
-
const estimatedCost = this._budgetTracker.estimate(modelId, providerName, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
597
|
-
const reservation = this._budgetTracker.reserve(requestId, estimatedCost);
|
|
598
|
-
if (!reservation) {
|
|
599
|
-
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`);
|
|
456
|
+
if (!params.output) {
|
|
457
|
+
const messages = this._normalizeMessages(params.messages);
|
|
458
|
+
const estimatedCost = this._budgetTracker.estimate(modelId, providerName, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
459
|
+
return this._requestPipeline.runComplete({
|
|
460
|
+
requestId,
|
|
461
|
+
startTime,
|
|
462
|
+
provider: providerName,
|
|
463
|
+
model: modelId,
|
|
464
|
+
messages,
|
|
465
|
+
params: {
|
|
466
|
+
model: modelId,
|
|
467
|
+
messages,
|
|
468
|
+
temperature: params.temperature,
|
|
469
|
+
maxTokens: params.maxTokens,
|
|
470
|
+
tools: params.tools,
|
|
471
|
+
extra: params.extra,
|
|
472
|
+
},
|
|
473
|
+
estimatedCost,
|
|
474
|
+
policy: params.policy,
|
|
475
|
+
providerTimeoutMs: providerConfig.timeout,
|
|
476
|
+
callerSignal: params.signal,
|
|
477
|
+
fallbackCandidates: (excludeModels) => this._getFallbacks(excludeModels, params),
|
|
478
|
+
estimateFallbackCost: (candidate) => this._budgetTracker.estimate(candidate.model, candidate.provider, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0),
|
|
479
|
+
getProviderTimeout: (provider) => this._registry.get(provider)?.timeout,
|
|
480
|
+
});
|
|
600
481
|
}
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
482
|
+
if (params.output) {
|
|
483
|
+
const messages = this._normalizeMessages(params.messages);
|
|
484
|
+
const estimatedCost = this._budgetTracker.estimate(modelId, providerName, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0);
|
|
485
|
+
return this._requestPipeline.runStructuredComplete({
|
|
486
|
+
requestId,
|
|
487
|
+
startTime,
|
|
488
|
+
provider: providerName,
|
|
489
|
+
model: modelId,
|
|
490
|
+
messages,
|
|
491
|
+
params: {
|
|
604
492
|
model: modelId,
|
|
605
|
-
messages
|
|
493
|
+
messages,
|
|
606
494
|
temperature: params.temperature,
|
|
607
495
|
maxTokens: params.maxTokens,
|
|
608
496
|
tools: params.tools,
|
|
609
|
-
extra:
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
const validation = outputConfig.schema.safeParse(parsed);
|
|
625
|
-
if (validation.success) {
|
|
626
|
-
this._budgetTracker.settle(requestId, estimatedCost, { provider: providerName, model: modelId, phase: "execute" });
|
|
627
|
-
this._emit({ type: "structured.validate.pass", requestId, timestamp: performance.now() });
|
|
628
|
-
this._emit({ type: "request.completed", requestId, timestamp: performance.now(), provider: providerName, model: modelId, durationMs: performance.now() - startTime, attempts: attempt + 1, structuredOutput: true });
|
|
629
|
-
// v3.0.0: Plugin onResponse hook
|
|
630
|
-
await this._plugins.fireResponse({ requestId, provider: providerName, model: modelId, durationMs: performance.now() - startTime, streaming: false });
|
|
631
|
-
return validation.data;
|
|
632
|
-
}
|
|
633
|
-
// Validation failed — attempt repair
|
|
634
|
-
this._emit({ type: "structured.validate.fail", requestId, timestamp: performance.now(), attempt, error: validation.error instanceof Error ? validation.error.message : String(validation.error) });
|
|
635
|
-
if (attempt < maxRepairAttempts) {
|
|
636
|
-
const repairPrompt = buildRepairPrompt(validation.error, result, buildJsonSystemInstruction(outputConfig.schema), outputConfig.repairInstructions);
|
|
637
|
-
// Replace last user message with repair prompt
|
|
638
|
-
const lastUserIdx = structuredMessages.map((m) => m.role).lastIndexOf("user");
|
|
639
|
-
if (lastUserIdx >= 0) {
|
|
640
|
-
structuredMessages = [
|
|
641
|
-
...structuredMessages.slice(0, lastUserIdx),
|
|
642
|
-
{ role: "user", content: repairPrompt },
|
|
643
|
-
];
|
|
644
|
-
}
|
|
645
|
-
else {
|
|
646
|
-
structuredMessages = [...structuredMessages, { role: "user", content: repairPrompt }];
|
|
647
|
-
}
|
|
648
|
-
continue;
|
|
649
|
-
}
|
|
650
|
-
// Exhausted repair attempts
|
|
651
|
-
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" });
|
|
652
|
-
throw new ValidationError(maxRepairAttempts + 1, validation.error, result, { requestId, model: modelId });
|
|
653
|
-
}
|
|
654
|
-
catch (err) {
|
|
655
|
-
// Don't retry validation errors through the retry loop
|
|
656
|
-
if (err instanceof ValidationError) {
|
|
657
|
-
throw err;
|
|
658
|
-
}
|
|
659
|
-
const isNetworkError = err instanceof TypeError || (err instanceof Error && err.name === "AbortError");
|
|
660
|
-
const status = err.status;
|
|
661
|
-
if (isNetworkError && shouldRetryNetworkError(attempt, retryConfig)) {
|
|
662
|
-
const delay = calculateBackoff(attempt, resolved.backoff);
|
|
663
|
-
this._emit({ type: "request.retrying", requestId, timestamp: performance.now(), provider: providerName, attempt, delayMs: delay, reason: "network error" });
|
|
664
|
-
await sleep(delay);
|
|
665
|
-
continue;
|
|
666
|
-
}
|
|
667
|
-
if (typeof status === "number" && shouldRetry(status, attempt, retryConfig)) {
|
|
668
|
-
const delay = calculateBackoff(attempt, resolved.backoff);
|
|
669
|
-
this._emit({ type: "request.retrying", requestId, timestamp: performance.now(), provider: providerName, attempt, delayMs: delay, reason: `HTTP ${status}` });
|
|
670
|
-
await sleep(delay);
|
|
671
|
-
continue;
|
|
672
|
-
}
|
|
673
|
-
// Try fallback if allowed and retries exhausted
|
|
674
|
-
if (resolved.allowFallback && attempt >= retryConfig.maxRetries && !(err instanceof ValidationError)) {
|
|
675
|
-
this._budgetTracker.release(requestId); // Release original reservation
|
|
676
|
-
const fallbacks = this._getFallbacks([modelId], params);
|
|
677
|
-
for (const fb of fallbacks) {
|
|
678
|
-
const fbEstimatedCost = this._budgetTracker.estimate(fb.model, fb.provider, 0, 0);
|
|
679
|
-
const fbReservation = this._budgetTracker.reserve(`${requestId}_fb_${fb.model}`, fbEstimatedCost);
|
|
680
|
-
if (!fbReservation)
|
|
681
|
-
continue; // Budget exceeded — skip this fallback
|
|
682
|
-
try {
|
|
683
|
-
const fbAdapter = this._getAdapter(fb.provider);
|
|
684
|
-
const result = await fbAdapter.complete({ model: fb.model, messages: structuredMessages, temperature: params.temperature, maxTokens: params.maxTokens, tools: params.tools, extra: structuredExtra, signal });
|
|
685
|
-
this._budgetTracker.settle(`${requestId}_fb_${fb.model}`, fbEstimatedCost, { provider: fb.provider, model: fb.model, phase: "fallback" });
|
|
686
|
-
this._emit({ type: "request.completed", requestId, timestamp: performance.now(), provider: fb.provider, model: fb.model, durationMs: performance.now() - startTime, attempts: attempt + 2, structuredOutput: !!outputConfig });
|
|
687
|
-
if (!outputConfig)
|
|
688
|
-
return result;
|
|
689
|
-
const json = extractJson(result);
|
|
690
|
-
const parsed = JSON.parse(json);
|
|
691
|
-
const validation = outputConfig.schema.safeParse(parsed);
|
|
692
|
-
if (validation.success)
|
|
693
|
-
return validation.data;
|
|
694
|
-
throw new ValidationError(maxRepairAttempts + 1, validation.error, result, { requestId, model: fb.model });
|
|
695
|
-
}
|
|
696
|
-
catch {
|
|
697
|
-
this._budgetTracker.release(`${requestId}_fb_${fb.model}`);
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
// Release reservation on final failure
|
|
702
|
-
this._budgetTracker.release(requestId);
|
|
703
|
-
circuitBreaker?.recordFailure(err instanceof Error ? err : undefined);
|
|
704
|
-
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) });
|
|
705
|
-
// v3.0.0: Plugin onError hook
|
|
706
|
-
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 });
|
|
707
|
-
throw err;
|
|
708
|
-
}
|
|
497
|
+
extra: params.extra,
|
|
498
|
+
},
|
|
499
|
+
estimatedCost,
|
|
500
|
+
policy: params.policy,
|
|
501
|
+
providerTimeoutMs: providerConfig.timeout,
|
|
502
|
+
callerSignal: params.signal,
|
|
503
|
+
output: params.output,
|
|
504
|
+
jsonModeParams: (provider) => {
|
|
505
|
+
const adapter = this._adapters.get(provider);
|
|
506
|
+
return adapter ? buildJsonModeParams(adapter.id) : {};
|
|
507
|
+
},
|
|
508
|
+
fallbackCandidates: (excludeModels) => this._getFallbacks(excludeModels, { ...params, needsStructuredOutput: true }),
|
|
509
|
+
estimateFallbackCost: (candidate) => this._budgetTracker.estimate(candidate.model, candidate.provider, this._estimateTokens(messages.map((m) => m.content ?? "").join("")), 0),
|
|
510
|
+
getProviderTimeout: (provider) => this._registry.get(provider)?.timeout,
|
|
511
|
+
});
|
|
709
512
|
}
|
|
513
|
+
throw new Error("Unreachable completion state");
|
|
710
514
|
}
|
|
711
515
|
// ─── Multi-Modal Reliability Pipeline ──────────────────────────────────
|
|
712
516
|
/**
|
|
713
517
|
* Run a multi-modal adapter method with retry, circuit breaker, timeout,
|
|
714
|
-
* and hook events
|
|
518
|
+
* and hook events.
|
|
519
|
+
*
|
|
520
|
+
* v3.2.0 intentionally keeps multimodal calls outside the logical budget
|
|
521
|
+
* pipeline until provider-specific pricing and reservation semantics are
|
|
522
|
+
* defined. They share the one-attempt reliability preparation, but do not
|
|
523
|
+
* silently reserve or settle new costs.
|
|
715
524
|
*/
|
|
716
525
|
async _runMultiModal(requestId, providerName, modelId, _operation, fn, userSignal) {
|
|
717
526
|
const startTime = performance.now();
|
|
@@ -820,6 +629,7 @@ export class HilbrasClient {
|
|
|
820
629
|
async *streamText(params) {
|
|
821
630
|
const maxSteps = params.maxSteps ?? 1;
|
|
822
631
|
const messages = this._normalizeMessages(params.messages);
|
|
632
|
+
const allowedToolNames = new Set((params.tools ?? []).map((tool) => tool.function.name));
|
|
823
633
|
let currentMessages = [...messages];
|
|
824
634
|
for (let step = 0; step < maxSteps; step++) {
|
|
825
635
|
let stepText = "";
|
|
@@ -853,21 +663,35 @@ export class HilbrasClient {
|
|
|
853
663
|
// Only finalize when done=true, or if done field is absent (backwards compat: treat as complete)
|
|
854
664
|
if (chunk.done === true || chunk.done === undefined) {
|
|
855
665
|
let parsedArgs;
|
|
666
|
+
let argumentError;
|
|
856
667
|
try {
|
|
857
|
-
|
|
668
|
+
const parsed = JSON.parse(pending.arguments || "{}");
|
|
669
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
670
|
+
throw new Error("Tool arguments must be an object");
|
|
671
|
+
}
|
|
672
|
+
parsedArgs = parsed;
|
|
673
|
+
}
|
|
674
|
+
catch (err) {
|
|
675
|
+
argumentError = err instanceof Error ? err.message : String(err);
|
|
676
|
+
parsedArgs = {};
|
|
677
|
+
}
|
|
678
|
+
const toolCall = { name: pending.name, args: parsedArgs, result: undefined };
|
|
679
|
+
stepToolCalls.push(toolCall);
|
|
680
|
+
if (argumentError) {
|
|
681
|
+
toolCall.result = { error: argumentError };
|
|
682
|
+
yield { type: "error", message: `Invalid arguments for tool ${pending.name}: ${argumentError}`, retryable: false };
|
|
858
683
|
}
|
|
859
|
-
|
|
860
|
-
|
|
684
|
+
else if (!allowedToolNames.has(pending.name)) {
|
|
685
|
+
const message = `Tool ${pending.name} is not in the configured allowlist`;
|
|
686
|
+
toolCall.result = { error: message };
|
|
687
|
+
yield { type: "error", message, retryable: false };
|
|
861
688
|
}
|
|
862
|
-
|
|
863
|
-
// Execute tool if handler provided
|
|
864
|
-
if (params.toolExecution) {
|
|
689
|
+
else if (params.toolExecution) {
|
|
865
690
|
try {
|
|
866
|
-
|
|
867
|
-
stepToolCalls[stepToolCalls.length - 1].result = result;
|
|
691
|
+
toolCall.result = await params.toolExecution(pending.name, parsedArgs);
|
|
868
692
|
}
|
|
869
693
|
catch (err) {
|
|
870
|
-
|
|
694
|
+
toolCall.result = { error: err.message };
|
|
871
695
|
}
|
|
872
696
|
}
|
|
873
697
|
pendingToolCalls.delete(idx);
|
|
@@ -883,20 +707,35 @@ export class HilbrasClient {
|
|
|
883
707
|
// Flush any remaining pending tool calls (stream ended without done=true)
|
|
884
708
|
for (const [, tc] of pendingToolCalls) {
|
|
885
709
|
let parsedArgs;
|
|
710
|
+
let argumentError;
|
|
886
711
|
try {
|
|
887
|
-
|
|
712
|
+
const parsed = JSON.parse(tc.arguments || "{}");
|
|
713
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
714
|
+
throw new Error("Tool arguments must be an object");
|
|
715
|
+
}
|
|
716
|
+
parsedArgs = parsed;
|
|
888
717
|
}
|
|
889
|
-
catch {
|
|
890
|
-
|
|
718
|
+
catch (err) {
|
|
719
|
+
argumentError = err instanceof Error ? err.message : String(err);
|
|
720
|
+
parsedArgs = {};
|
|
721
|
+
}
|
|
722
|
+
const toolCall = { name: tc.name, args: parsedArgs, result: undefined };
|
|
723
|
+
stepToolCalls.push(toolCall);
|
|
724
|
+
if (argumentError) {
|
|
725
|
+
toolCall.result = { error: argumentError };
|
|
726
|
+
yield { type: "error", message: `Invalid arguments for tool ${tc.name}: ${argumentError}`, retryable: false };
|
|
727
|
+
}
|
|
728
|
+
else if (!allowedToolNames.has(tc.name)) {
|
|
729
|
+
const message = `Tool ${tc.name} is not in the configured allowlist`;
|
|
730
|
+
toolCall.result = { error: message };
|
|
731
|
+
yield { type: "error", message, retryable: false };
|
|
891
732
|
}
|
|
892
|
-
|
|
893
|
-
if (params.toolExecution) {
|
|
733
|
+
else if (params.toolExecution) {
|
|
894
734
|
try {
|
|
895
|
-
|
|
896
|
-
stepToolCalls[stepToolCalls.length - 1].result = result;
|
|
735
|
+
toolCall.result = await params.toolExecution(tc.name, parsedArgs);
|
|
897
736
|
}
|
|
898
737
|
catch (err) {
|
|
899
|
-
|
|
738
|
+
toolCall.result = { error: err.message };
|
|
900
739
|
}
|
|
901
740
|
}
|
|
902
741
|
}
|
|
@@ -1059,4 +898,3 @@ export class HilbrasClient {
|
|
|
1059
898
|
await this.dispose();
|
|
1060
899
|
}
|
|
1061
900
|
}
|
|
1062
|
-
//# sourceMappingURL=client.js.map
|