@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
|
@@ -0,0 +1,730 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logical-request pipeline for non-streaming complete operations.
|
|
3
|
+
*
|
|
4
|
+
* The pipeline owns the request lifecycle, budget reservation, retry/fallback
|
|
5
|
+
* decision, and terminal events. Provider invocation remains one attempt at a
|
|
6
|
+
* time inside RequestExecutor.
|
|
7
|
+
*/
|
|
8
|
+
import { CircuitBreakerOpenError, ConfigurationError, ValidationError } from "../../errors/index.js";
|
|
9
|
+
import { calculateBackoff } from "../../reliability/backoff.js";
|
|
10
|
+
import { buildJsonSystemInstruction, buildRepairPrompt, extractJson } from "../../output/structured.js";
|
|
11
|
+
import { shouldRetry, shouldRetryNetworkError } from "../../reliability/retry.js";
|
|
12
|
+
export class RequestPipeline {
|
|
13
|
+
ports;
|
|
14
|
+
constructor(ports) {
|
|
15
|
+
this.ports = ports;
|
|
16
|
+
}
|
|
17
|
+
async runComplete(input) {
|
|
18
|
+
const prepared = this.prepare(input);
|
|
19
|
+
try {
|
|
20
|
+
await this.ports.plugins.fireRequest({
|
|
21
|
+
requestId: input.requestId,
|
|
22
|
+
provider: input.provider,
|
|
23
|
+
model: input.model,
|
|
24
|
+
messages: input.messages,
|
|
25
|
+
extra: input.params.extra,
|
|
26
|
+
signal: input.callerSignal,
|
|
27
|
+
timestamp: this.ports.now(),
|
|
28
|
+
});
|
|
29
|
+
const reservation = this.ports.budget.reserve(input.requestId, input.estimatedCost);
|
|
30
|
+
if (!reservation) {
|
|
31
|
+
const remaining = this.ports.budget.report().remainingBudget;
|
|
32
|
+
throw new ConfigurationError(`Budget reservation rejected — estimated cost $${input.estimatedCost.toFixed(4)} would exceed budget`, `Remaining budget: $${remaining?.toFixed(4) ?? "unknown"}. Options: (1) increase sessionBudget, (2) use a cheaper model, (3) reduce input token count`);
|
|
33
|
+
}
|
|
34
|
+
let attempt = 0;
|
|
35
|
+
let primaryError;
|
|
36
|
+
for (;;) {
|
|
37
|
+
const result = await this.ports.executor.executeComplete(prepared, input.params, { dispose: false, recordCircuitFailure: false });
|
|
38
|
+
if (result.ok) {
|
|
39
|
+
this.ports.budget.settle(input.requestId, input.estimatedCost, {
|
|
40
|
+
provider: input.provider,
|
|
41
|
+
model: input.model,
|
|
42
|
+
phase: "execute",
|
|
43
|
+
});
|
|
44
|
+
this.emitCompleted(input, attempt + 1, false, input.provider, input.model);
|
|
45
|
+
await this.ports.plugins.fireResponse({
|
|
46
|
+
requestId: input.requestId,
|
|
47
|
+
provider: input.provider,
|
|
48
|
+
model: input.model,
|
|
49
|
+
durationMs: this.ports.now() - input.startTime,
|
|
50
|
+
streaming: false,
|
|
51
|
+
});
|
|
52
|
+
return result.value;
|
|
53
|
+
}
|
|
54
|
+
primaryError = result.error;
|
|
55
|
+
if (input.callerSignal?.aborted) {
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
const status = primaryError.status;
|
|
59
|
+
const isNetworkError = primaryError instanceof TypeError
|
|
60
|
+
|| (primaryError instanceof Error && primaryError.name === "AbortError");
|
|
61
|
+
const canRetry = (isNetworkError && shouldRetryNetworkError(attempt, prepared.retryConfig))
|
|
62
|
+
|| (typeof status === "number" && shouldRetry(status, attempt, prepared.retryConfig));
|
|
63
|
+
if (canRetry) {
|
|
64
|
+
const delay = calculateBackoff(attempt, prepared.context.policy.backoff);
|
|
65
|
+
this.ports.emit({
|
|
66
|
+
type: "request.retrying",
|
|
67
|
+
requestId: input.requestId,
|
|
68
|
+
timestamp: this.ports.now(),
|
|
69
|
+
provider: input.provider,
|
|
70
|
+
attempt,
|
|
71
|
+
delayMs: delay,
|
|
72
|
+
reason: isNetworkError ? "network error" : `HTTP ${status}`,
|
|
73
|
+
});
|
|
74
|
+
try {
|
|
75
|
+
await this.ports.sleep(delay, input.callerSignal);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
primaryError = error;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
attempt++;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (prepared.context.policy.allowFallback) {
|
|
85
|
+
this.ports.budget.release(input.requestId);
|
|
86
|
+
const attemptedModels = [input.model];
|
|
87
|
+
const fallbacks = input.fallbackCandidates(attemptedModels);
|
|
88
|
+
for (const fallback of fallbacks) {
|
|
89
|
+
if (attemptedModels.includes(fallback.model))
|
|
90
|
+
continue;
|
|
91
|
+
const fallbackCost = input.estimateFallbackCost(fallback);
|
|
92
|
+
if (prepared.context.policy.maxFallbackCost !== null && fallbackCost > prepared.context.policy.maxFallbackCost)
|
|
93
|
+
continue;
|
|
94
|
+
attemptedModels.push(fallback.model);
|
|
95
|
+
const fallbackId = `${input.requestId}_fb_${fallback.model}`;
|
|
96
|
+
const fallbackReservation = this.ports.budget.reserve(fallbackId, fallbackCost);
|
|
97
|
+
if (!fallbackReservation)
|
|
98
|
+
continue;
|
|
99
|
+
let fallbackPrepared;
|
|
100
|
+
try {
|
|
101
|
+
fallbackPrepared = this.prepare({
|
|
102
|
+
...input,
|
|
103
|
+
provider: fallback.provider,
|
|
104
|
+
model: fallback.model,
|
|
105
|
+
params: { ...input.params, model: fallback.model },
|
|
106
|
+
callerSignal: prepared.signal,
|
|
107
|
+
providerTimeoutMs: 0,
|
|
108
|
+
});
|
|
109
|
+
const fallbackResult = await this.ports.executor.executeComplete(fallbackPrepared, { ...input.params, model: fallback.model }, { recordCircuitFailure: false });
|
|
110
|
+
if (!fallbackResult.ok && input.callerSignal?.aborted) {
|
|
111
|
+
primaryError = fallbackResult.error;
|
|
112
|
+
}
|
|
113
|
+
if (fallbackResult.ok) {
|
|
114
|
+
this.ports.budget.settle(fallbackId, fallbackCost, {
|
|
115
|
+
provider: fallback.provider,
|
|
116
|
+
model: fallback.model,
|
|
117
|
+
phase: "fallback",
|
|
118
|
+
});
|
|
119
|
+
this.emitCompleted(input, attempt + 2, false, fallback.provider, fallback.model);
|
|
120
|
+
await this.ports.plugins.fireResponse({
|
|
121
|
+
requestId: input.requestId,
|
|
122
|
+
provider: fallback.provider,
|
|
123
|
+
model: fallback.model,
|
|
124
|
+
durationMs: this.ports.now() - input.startTime,
|
|
125
|
+
streaming: false,
|
|
126
|
+
});
|
|
127
|
+
return fallbackResult.value;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
if (input.callerSignal?.aborted)
|
|
132
|
+
primaryError = error;
|
|
133
|
+
}
|
|
134
|
+
finally {
|
|
135
|
+
fallbackPrepared?.dispose();
|
|
136
|
+
this.ports.budget.release(fallbackId);
|
|
137
|
+
}
|
|
138
|
+
if (input.callerSignal?.aborted)
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
this.recordTerminalFailure(prepared, primaryError);
|
|
145
|
+
this.ports.budget.release(input.requestId);
|
|
146
|
+
const error = primaryError instanceof Error ? primaryError : new Error(String(primaryError));
|
|
147
|
+
this.ports.emit({
|
|
148
|
+
type: "request.failed",
|
|
149
|
+
requestId: input.requestId,
|
|
150
|
+
timestamp: this.ports.now(),
|
|
151
|
+
provider: input.provider,
|
|
152
|
+
model: input.model,
|
|
153
|
+
durationMs: this.ports.now() - input.startTime,
|
|
154
|
+
attempts: attempt + 1,
|
|
155
|
+
error: error.message,
|
|
156
|
+
});
|
|
157
|
+
await this.ports.plugins.fireError({
|
|
158
|
+
requestId: input.requestId,
|
|
159
|
+
provider: input.provider,
|
|
160
|
+
model: input.model,
|
|
161
|
+
error,
|
|
162
|
+
durationMs: this.ports.now() - input.startTime,
|
|
163
|
+
attempts: attempt + 1,
|
|
164
|
+
});
|
|
165
|
+
throw primaryError;
|
|
166
|
+
}
|
|
167
|
+
finally {
|
|
168
|
+
prepared.dispose();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
async *runStream(input) {
|
|
172
|
+
const prepared = this.prepare(input, "stream");
|
|
173
|
+
let reservationActive = false;
|
|
174
|
+
let inputTokens;
|
|
175
|
+
let outputTokens;
|
|
176
|
+
let firstChunkEmitted = false;
|
|
177
|
+
let attempt = 0;
|
|
178
|
+
let primaryError;
|
|
179
|
+
try {
|
|
180
|
+
await this.ports.plugins.fireRequest({
|
|
181
|
+
requestId: input.requestId,
|
|
182
|
+
provider: input.provider,
|
|
183
|
+
model: input.model,
|
|
184
|
+
messages: input.messages,
|
|
185
|
+
extra: input.params.extra,
|
|
186
|
+
signal: input.callerSignal,
|
|
187
|
+
timestamp: this.ports.now(),
|
|
188
|
+
});
|
|
189
|
+
const reservation = this.ports.budget.reserve(input.requestId, input.estimatedCost);
|
|
190
|
+
if (!reservation) {
|
|
191
|
+
const remaining = this.ports.budget.report().remainingBudget;
|
|
192
|
+
throw new ConfigurationError(`Budget reservation rejected — estimated cost $${input.estimatedCost.toFixed(4)} would exceed budget`, `Remaining budget: $${remaining?.toFixed(4) ?? "unknown"}. Options: (1) increase sessionBudget, (2) use a cheaper model, (3) reduce input token count`);
|
|
193
|
+
}
|
|
194
|
+
reservationActive = true;
|
|
195
|
+
for (;;) {
|
|
196
|
+
try {
|
|
197
|
+
for await (const chunk of this.ports.executor.executeStream(prepared, input.params, { dispose: false, recordCircuitFailure: false })) {
|
|
198
|
+
if (!firstChunkEmitted) {
|
|
199
|
+
firstChunkEmitted = true;
|
|
200
|
+
this.ports.emit({
|
|
201
|
+
type: "stream.first_chunk",
|
|
202
|
+
requestId: input.requestId,
|
|
203
|
+
timestamp: this.ports.now(),
|
|
204
|
+
latencyMs: 0,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
if (chunk.type === "usage") {
|
|
208
|
+
inputTokens = chunk.inputTokens;
|
|
209
|
+
outputTokens = chunk.outputTokens;
|
|
210
|
+
if (reservationActive) {
|
|
211
|
+
const actualCost = this.ports.budget.estimate(input.model, input.provider, chunk.inputTokens, chunk.outputTokens);
|
|
212
|
+
this.ports.budget.settle(input.requestId, actualCost, {
|
|
213
|
+
provider: input.provider,
|
|
214
|
+
model: input.model,
|
|
215
|
+
phase: "execute",
|
|
216
|
+
});
|
|
217
|
+
reservationActive = false;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
yield chunk;
|
|
221
|
+
}
|
|
222
|
+
if (reservationActive) {
|
|
223
|
+
this.ports.budget.settle(input.requestId, input.estimatedCost, {
|
|
224
|
+
provider: input.provider,
|
|
225
|
+
model: input.model,
|
|
226
|
+
phase: "execute",
|
|
227
|
+
});
|
|
228
|
+
reservationActive = false;
|
|
229
|
+
}
|
|
230
|
+
this.ports.emit({
|
|
231
|
+
type: "request.completed",
|
|
232
|
+
requestId: input.requestId,
|
|
233
|
+
timestamp: this.ports.now(),
|
|
234
|
+
provider: input.provider,
|
|
235
|
+
model: input.model,
|
|
236
|
+
durationMs: this.ports.now() - input.startTime,
|
|
237
|
+
attempts: attempt + 1,
|
|
238
|
+
inputTokens,
|
|
239
|
+
outputTokens,
|
|
240
|
+
structuredOutput: false,
|
|
241
|
+
});
|
|
242
|
+
await this.ports.plugins.fireResponse({
|
|
243
|
+
requestId: input.requestId,
|
|
244
|
+
provider: input.provider,
|
|
245
|
+
model: input.model,
|
|
246
|
+
durationMs: this.ports.now() - input.startTime,
|
|
247
|
+
inputTokens,
|
|
248
|
+
outputTokens,
|
|
249
|
+
streaming: true,
|
|
250
|
+
});
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
catch (error) {
|
|
254
|
+
primaryError = error;
|
|
255
|
+
const callerAborted = input.callerSignal?.aborted === true;
|
|
256
|
+
const internalTimeout = prepared.signal?.aborted === true;
|
|
257
|
+
if (callerAborted || firstChunkEmitted || internalTimeout) {
|
|
258
|
+
if (reservationActive) {
|
|
259
|
+
this.ports.budget.release(input.requestId);
|
|
260
|
+
reservationActive = false;
|
|
261
|
+
}
|
|
262
|
+
const terminalError = error instanceof Error ? error : new Error(String(error));
|
|
263
|
+
this.recordTerminalFailure(prepared, terminalError);
|
|
264
|
+
this.ports.emit({
|
|
265
|
+
type: "request.failed",
|
|
266
|
+
requestId: input.requestId,
|
|
267
|
+
timestamp: this.ports.now(),
|
|
268
|
+
provider: input.provider,
|
|
269
|
+
model: input.model,
|
|
270
|
+
durationMs: this.ports.now() - input.startTime,
|
|
271
|
+
attempts: attempt + 1,
|
|
272
|
+
error: terminalError.message,
|
|
273
|
+
});
|
|
274
|
+
await this.ports.plugins.fireError({
|
|
275
|
+
requestId: input.requestId,
|
|
276
|
+
provider: input.provider,
|
|
277
|
+
model: input.model,
|
|
278
|
+
error: terminalError,
|
|
279
|
+
durationMs: this.ports.now() - input.startTime,
|
|
280
|
+
attempts: attempt + 1,
|
|
281
|
+
});
|
|
282
|
+
throw error;
|
|
283
|
+
}
|
|
284
|
+
const status = error.status;
|
|
285
|
+
const isNetworkError = error instanceof TypeError
|
|
286
|
+
|| (error instanceof Error && error.name === "AbortError");
|
|
287
|
+
const canRetry = (isNetworkError && shouldRetryNetworkError(attempt, prepared.retryConfig))
|
|
288
|
+
|| (typeof status === "number" && shouldRetry(status, attempt, prepared.retryConfig));
|
|
289
|
+
if (!canRetry) {
|
|
290
|
+
if (prepared.context.policy.allowFallback) {
|
|
291
|
+
this.ports.budget.release(input.requestId);
|
|
292
|
+
reservationActive = false;
|
|
293
|
+
const attemptedModels = [input.model];
|
|
294
|
+
const fallbacks = input.fallbackCandidates(attemptedModels);
|
|
295
|
+
for (const fallback of fallbacks) {
|
|
296
|
+
if (attemptedModels.includes(fallback.model))
|
|
297
|
+
continue;
|
|
298
|
+
const fallbackCost = input.estimateFallbackCost(fallback);
|
|
299
|
+
if (prepared.context.policy.maxFallbackCost !== null && fallbackCost > prepared.context.policy.maxFallbackCost)
|
|
300
|
+
continue;
|
|
301
|
+
attemptedModels.push(fallback.model);
|
|
302
|
+
this.ports.emit({
|
|
303
|
+
type: "fallback.started",
|
|
304
|
+
requestId: input.requestId,
|
|
305
|
+
timestamp: this.ports.now(),
|
|
306
|
+
originalProvider: input.provider,
|
|
307
|
+
originalModel: input.model,
|
|
308
|
+
fallbackProvider: fallback.provider,
|
|
309
|
+
fallbackModel: fallback.model,
|
|
310
|
+
});
|
|
311
|
+
const fallbackId = `${input.requestId}_fb_${fallback.model}`;
|
|
312
|
+
const fallbackReservation = this.ports.budget.reserve(fallbackId, fallbackCost);
|
|
313
|
+
if (!fallbackReservation)
|
|
314
|
+
continue;
|
|
315
|
+
let fallbackPrepared;
|
|
316
|
+
let fallbackVisible = false;
|
|
317
|
+
let fallbackInputTokens;
|
|
318
|
+
let fallbackOutputTokens;
|
|
319
|
+
let fallbackReservationActive = true;
|
|
320
|
+
try {
|
|
321
|
+
fallbackPrepared = this.prepare({
|
|
322
|
+
...input,
|
|
323
|
+
provider: fallback.provider,
|
|
324
|
+
model: fallback.model,
|
|
325
|
+
params: { ...input.params, model: fallback.model },
|
|
326
|
+
callerSignal: prepared.signal,
|
|
327
|
+
providerTimeoutMs: 0,
|
|
328
|
+
}, "stream");
|
|
329
|
+
for await (const chunk of this.ports.executor.executeStream(fallbackPrepared, { ...input.params, model: fallback.model }, { recordCircuitFailure: false })) {
|
|
330
|
+
fallbackVisible = true;
|
|
331
|
+
if (chunk.type === "usage") {
|
|
332
|
+
fallbackInputTokens = chunk.inputTokens;
|
|
333
|
+
fallbackOutputTokens = chunk.outputTokens;
|
|
334
|
+
if (fallbackReservationActive) {
|
|
335
|
+
const actualCost = this.ports.budget.estimate(fallback.model, fallback.provider, chunk.inputTokens, chunk.outputTokens);
|
|
336
|
+
this.ports.budget.settle(fallbackId, actualCost, {
|
|
337
|
+
provider: fallback.provider,
|
|
338
|
+
model: fallback.model,
|
|
339
|
+
phase: "fallback",
|
|
340
|
+
});
|
|
341
|
+
fallbackReservationActive = false;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
yield chunk;
|
|
345
|
+
}
|
|
346
|
+
if (fallbackReservationActive) {
|
|
347
|
+
this.ports.budget.settle(fallbackId, fallbackCost, {
|
|
348
|
+
provider: fallback.provider,
|
|
349
|
+
model: fallback.model,
|
|
350
|
+
phase: "fallback",
|
|
351
|
+
});
|
|
352
|
+
fallbackReservationActive = false;
|
|
353
|
+
}
|
|
354
|
+
this.ports.emit({
|
|
355
|
+
type: "request.completed",
|
|
356
|
+
requestId: input.requestId,
|
|
357
|
+
timestamp: this.ports.now(),
|
|
358
|
+
provider: fallback.provider,
|
|
359
|
+
model: fallback.model,
|
|
360
|
+
durationMs: this.ports.now() - input.startTime,
|
|
361
|
+
attempts: attempt + 2,
|
|
362
|
+
inputTokens: fallbackInputTokens,
|
|
363
|
+
outputTokens: fallbackOutputTokens,
|
|
364
|
+
structuredOutput: false,
|
|
365
|
+
});
|
|
366
|
+
await this.ports.plugins.fireResponse({
|
|
367
|
+
requestId: input.requestId,
|
|
368
|
+
provider: fallback.provider,
|
|
369
|
+
model: fallback.model,
|
|
370
|
+
durationMs: this.ports.now() - input.startTime,
|
|
371
|
+
inputTokens: fallbackInputTokens,
|
|
372
|
+
outputTokens: fallbackOutputTokens,
|
|
373
|
+
streaming: true,
|
|
374
|
+
});
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
catch (fallbackError) {
|
|
378
|
+
if (input.callerSignal?.aborted || prepared.signal?.aborted || fallbackVisible) {
|
|
379
|
+
primaryError = fallbackError;
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
finally {
|
|
384
|
+
fallbackPrepared?.dispose();
|
|
385
|
+
if (fallbackReservationActive)
|
|
386
|
+
this.ports.budget.release(fallbackId);
|
|
387
|
+
}
|
|
388
|
+
if (input.callerSignal?.aborted || prepared.signal?.aborted)
|
|
389
|
+
break;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
if (!canRetry) {
|
|
394
|
+
if (reservationActive) {
|
|
395
|
+
this.ports.budget.release(input.requestId);
|
|
396
|
+
reservationActive = false;
|
|
397
|
+
}
|
|
398
|
+
const terminalError = primaryError instanceof Error ? primaryError : new Error(String(primaryError));
|
|
399
|
+
this.recordTerminalFailure(prepared, terminalError);
|
|
400
|
+
this.ports.emit({
|
|
401
|
+
type: "request.failed",
|
|
402
|
+
requestId: input.requestId,
|
|
403
|
+
timestamp: this.ports.now(),
|
|
404
|
+
provider: input.provider,
|
|
405
|
+
model: input.model,
|
|
406
|
+
durationMs: this.ports.now() - input.startTime,
|
|
407
|
+
attempts: attempt + 1,
|
|
408
|
+
error: terminalError.message,
|
|
409
|
+
});
|
|
410
|
+
await this.ports.plugins.fireError({
|
|
411
|
+
requestId: input.requestId,
|
|
412
|
+
provider: input.provider,
|
|
413
|
+
model: input.model,
|
|
414
|
+
error: terminalError,
|
|
415
|
+
durationMs: this.ports.now() - input.startTime,
|
|
416
|
+
attempts: attempt + 1,
|
|
417
|
+
});
|
|
418
|
+
throw primaryError;
|
|
419
|
+
}
|
|
420
|
+
const delay = calculateBackoff(attempt, prepared.context.policy.backoff);
|
|
421
|
+
this.ports.emit({
|
|
422
|
+
type: "request.retrying",
|
|
423
|
+
requestId: input.requestId,
|
|
424
|
+
timestamp: this.ports.now(),
|
|
425
|
+
provider: input.provider,
|
|
426
|
+
attempt,
|
|
427
|
+
delayMs: delay,
|
|
428
|
+
reason: isNetworkError ? "network error" : `HTTP ${status}`,
|
|
429
|
+
});
|
|
430
|
+
try {
|
|
431
|
+
await this.ports.sleep(delay, input.callerSignal);
|
|
432
|
+
}
|
|
433
|
+
catch (sleepError) {
|
|
434
|
+
if (reservationActive) {
|
|
435
|
+
this.ports.budget.release(input.requestId);
|
|
436
|
+
reservationActive = false;
|
|
437
|
+
}
|
|
438
|
+
const terminalError = sleepError instanceof Error ? sleepError : new Error(String(sleepError));
|
|
439
|
+
this.recordTerminalFailure(prepared, terminalError);
|
|
440
|
+
this.ports.emit({
|
|
441
|
+
type: "request.failed",
|
|
442
|
+
requestId: input.requestId,
|
|
443
|
+
timestamp: this.ports.now(),
|
|
444
|
+
provider: input.provider,
|
|
445
|
+
model: input.model,
|
|
446
|
+
durationMs: this.ports.now() - input.startTime,
|
|
447
|
+
attempts: attempt + 1,
|
|
448
|
+
error: terminalError.message,
|
|
449
|
+
});
|
|
450
|
+
await this.ports.plugins.fireError({
|
|
451
|
+
requestId: input.requestId,
|
|
452
|
+
provider: input.provider,
|
|
453
|
+
model: input.model,
|
|
454
|
+
error: terminalError,
|
|
455
|
+
durationMs: this.ports.now() - input.startTime,
|
|
456
|
+
attempts: attempt + 1,
|
|
457
|
+
});
|
|
458
|
+
throw sleepError;
|
|
459
|
+
}
|
|
460
|
+
attempt++;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
finally {
|
|
465
|
+
if (reservationActive)
|
|
466
|
+
this.ports.budget.release(input.requestId);
|
|
467
|
+
prepared.dispose();
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
async runStructuredComplete(input) {
|
|
471
|
+
const prepared = this.prepare(input);
|
|
472
|
+
try {
|
|
473
|
+
await this.ports.plugins.fireRequest({
|
|
474
|
+
requestId: input.requestId,
|
|
475
|
+
provider: input.provider,
|
|
476
|
+
model: input.model,
|
|
477
|
+
messages: input.messages,
|
|
478
|
+
extra: input.params.extra,
|
|
479
|
+
signal: input.callerSignal,
|
|
480
|
+
timestamp: this.ports.now(),
|
|
481
|
+
});
|
|
482
|
+
const reservation = this.ports.budget.reserve(input.requestId, input.estimatedCost);
|
|
483
|
+
if (!reservation) {
|
|
484
|
+
const remaining = this.ports.budget.report().remainingBudget;
|
|
485
|
+
throw new ConfigurationError(`Budget reservation rejected — estimated cost $${input.estimatedCost.toFixed(4)} would exceed budget`, `Remaining budget: $${remaining?.toFixed(4) ?? "unknown"}. Options: (1) increase sessionBudget, (2) use a cheaper model, (3) reduce input token count`);
|
|
486
|
+
}
|
|
487
|
+
const maxRepairAttempts = input.output.maxRepairAttempts ?? 2;
|
|
488
|
+
let structuredMessages = [
|
|
489
|
+
{ role: "system", content: buildJsonSystemInstruction(input.output.schema) },
|
|
490
|
+
...input.messages,
|
|
491
|
+
];
|
|
492
|
+
let structuredExtra = {
|
|
493
|
+
...input.params.extra,
|
|
494
|
+
...(input.jsonModeParams?.(input.provider) ?? {}),
|
|
495
|
+
};
|
|
496
|
+
let attempt = 0;
|
|
497
|
+
let primaryError;
|
|
498
|
+
for (;;) {
|
|
499
|
+
const result = await this.ports.executor.executeComplete(prepared, { ...input.params, messages: structuredMessages, extra: structuredExtra }, { dispose: false, recordCircuitFailure: false });
|
|
500
|
+
if (result.ok) {
|
|
501
|
+
let parsed;
|
|
502
|
+
let validation;
|
|
503
|
+
try {
|
|
504
|
+
parsed = JSON.parse(extractJson(result.value));
|
|
505
|
+
validation = input.output.schema.safeParse(parsed);
|
|
506
|
+
}
|
|
507
|
+
catch (error) {
|
|
508
|
+
validation = { success: false, error };
|
|
509
|
+
}
|
|
510
|
+
if (validation.success) {
|
|
511
|
+
this.ports.budget.settle(input.requestId, input.estimatedCost, {
|
|
512
|
+
provider: input.provider,
|
|
513
|
+
model: input.model,
|
|
514
|
+
phase: "execute",
|
|
515
|
+
});
|
|
516
|
+
this.ports.emit({
|
|
517
|
+
type: "structured.validate.pass",
|
|
518
|
+
requestId: input.requestId,
|
|
519
|
+
timestamp: this.ports.now(),
|
|
520
|
+
});
|
|
521
|
+
this.emitCompleted(input, attempt + 1, true, input.provider, input.model);
|
|
522
|
+
await this.ports.plugins.fireResponse({
|
|
523
|
+
requestId: input.requestId,
|
|
524
|
+
provider: input.provider,
|
|
525
|
+
model: input.model,
|
|
526
|
+
durationMs: this.ports.now() - input.startTime,
|
|
527
|
+
streaming: false,
|
|
528
|
+
});
|
|
529
|
+
return validation.data;
|
|
530
|
+
}
|
|
531
|
+
this.ports.emit({
|
|
532
|
+
type: "structured.validate.fail",
|
|
533
|
+
requestId: input.requestId,
|
|
534
|
+
timestamp: this.ports.now(),
|
|
535
|
+
attempt,
|
|
536
|
+
error: validation.error instanceof Error ? validation.error.message : String(validation.error),
|
|
537
|
+
});
|
|
538
|
+
if (attempt < maxRepairAttempts) {
|
|
539
|
+
const repairPrompt = buildRepairPrompt(validation.error, result.value, buildJsonSystemInstruction(input.output.schema), input.output.repairInstructions);
|
|
540
|
+
const lastUserIdx = structuredMessages.map((m) => m.role).lastIndexOf("user");
|
|
541
|
+
structuredMessages = lastUserIdx >= 0
|
|
542
|
+
? [...structuredMessages.slice(0, lastUserIdx), { role: "user", content: repairPrompt }]
|
|
543
|
+
: [...structuredMessages, { role: "user", content: repairPrompt }];
|
|
544
|
+
attempt++;
|
|
545
|
+
continue;
|
|
546
|
+
}
|
|
547
|
+
const validationError = new ValidationError(maxRepairAttempts + 1, validation.error, result.value, { requestId: input.requestId, model: input.model });
|
|
548
|
+
this.ports.budget.release(input.requestId);
|
|
549
|
+
this.ports.emit({
|
|
550
|
+
type: "request.failed",
|
|
551
|
+
requestId: input.requestId,
|
|
552
|
+
timestamp: this.ports.now(),
|
|
553
|
+
provider: input.provider,
|
|
554
|
+
model: input.model,
|
|
555
|
+
durationMs: this.ports.now() - input.startTime,
|
|
556
|
+
attempts: attempt + 1,
|
|
557
|
+
error: validationError.message,
|
|
558
|
+
});
|
|
559
|
+
await this.ports.plugins.fireError({
|
|
560
|
+
requestId: input.requestId,
|
|
561
|
+
provider: input.provider,
|
|
562
|
+
model: input.model,
|
|
563
|
+
error: validationError,
|
|
564
|
+
durationMs: this.ports.now() - input.startTime,
|
|
565
|
+
attempts: attempt + 1,
|
|
566
|
+
});
|
|
567
|
+
throw validationError;
|
|
568
|
+
}
|
|
569
|
+
primaryError = result.error;
|
|
570
|
+
if (input.callerSignal?.aborted)
|
|
571
|
+
break;
|
|
572
|
+
const status = primaryError.status;
|
|
573
|
+
const isNetworkError = primaryError instanceof TypeError
|
|
574
|
+
|| (primaryError instanceof Error && primaryError.name === "AbortError");
|
|
575
|
+
const canRetry = (isNetworkError && shouldRetryNetworkError(attempt, prepared.retryConfig))
|
|
576
|
+
|| (typeof status === "number" && shouldRetry(status, attempt, prepared.retryConfig));
|
|
577
|
+
if (canRetry) {
|
|
578
|
+
const delay = calculateBackoff(attempt, prepared.context.policy.backoff);
|
|
579
|
+
this.ports.emit({
|
|
580
|
+
type: "request.retrying",
|
|
581
|
+
requestId: input.requestId,
|
|
582
|
+
timestamp: this.ports.now(),
|
|
583
|
+
provider: input.provider,
|
|
584
|
+
attempt,
|
|
585
|
+
delayMs: delay,
|
|
586
|
+
reason: isNetworkError ? "network error" : `HTTP ${status}`,
|
|
587
|
+
});
|
|
588
|
+
try {
|
|
589
|
+
await this.ports.sleep(delay, input.callerSignal);
|
|
590
|
+
}
|
|
591
|
+
catch (error) {
|
|
592
|
+
primaryError = error;
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
attempt++;
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
if (prepared.context.policy.allowFallback) {
|
|
599
|
+
this.ports.budget.release(input.requestId);
|
|
600
|
+
const attemptedModels = [input.model];
|
|
601
|
+
const fallbacks = input.fallbackCandidates(attemptedModels);
|
|
602
|
+
for (const fallback of fallbacks) {
|
|
603
|
+
if (attemptedModels.includes(fallback.model))
|
|
604
|
+
continue;
|
|
605
|
+
const fallbackCost = input.estimateFallbackCost(fallback);
|
|
606
|
+
if (prepared.context.policy.maxFallbackCost !== null && fallbackCost > prepared.context.policy.maxFallbackCost)
|
|
607
|
+
continue;
|
|
608
|
+
attemptedModels.push(fallback.model);
|
|
609
|
+
const fallbackId = `${input.requestId}_fb_${fallback.model}`;
|
|
610
|
+
const fallbackReservation = this.ports.budget.reserve(fallbackId, fallbackCost);
|
|
611
|
+
if (!fallbackReservation)
|
|
612
|
+
continue;
|
|
613
|
+
let fallbackPrepared;
|
|
614
|
+
try {
|
|
615
|
+
fallbackPrepared = this.prepare({
|
|
616
|
+
...input,
|
|
617
|
+
provider: fallback.provider,
|
|
618
|
+
model: fallback.model,
|
|
619
|
+
params: { ...input.params, model: fallback.model },
|
|
620
|
+
callerSignal: prepared.signal,
|
|
621
|
+
providerTimeoutMs: 0,
|
|
622
|
+
});
|
|
623
|
+
const fallbackResult = await this.ports.executor.executeComplete(fallbackPrepared, {
|
|
624
|
+
...input.params,
|
|
625
|
+
model: fallback.model,
|
|
626
|
+
messages: structuredMessages,
|
|
627
|
+
extra: { ...structuredExtra, ...(input.jsonModeParams?.(fallback.provider) ?? {}) },
|
|
628
|
+
}, { recordCircuitFailure: false });
|
|
629
|
+
if (!fallbackResult.ok && input.callerSignal?.aborted) {
|
|
630
|
+
primaryError = fallbackResult.error;
|
|
631
|
+
}
|
|
632
|
+
if (fallbackResult.ok) {
|
|
633
|
+
const parsed = JSON.parse(extractJson(fallbackResult.value));
|
|
634
|
+
const validation = input.output.schema.safeParse(parsed);
|
|
635
|
+
if (validation.success) {
|
|
636
|
+
this.ports.budget.settle(fallbackId, fallbackCost, {
|
|
637
|
+
provider: fallback.provider,
|
|
638
|
+
model: fallback.model,
|
|
639
|
+
phase: "fallback",
|
|
640
|
+
});
|
|
641
|
+
this.emitCompleted(input, attempt + 2, true, fallback.provider, fallback.model);
|
|
642
|
+
await this.ports.plugins.fireResponse({
|
|
643
|
+
requestId: input.requestId,
|
|
644
|
+
provider: fallback.provider,
|
|
645
|
+
model: fallback.model,
|
|
646
|
+
durationMs: this.ports.now() - input.startTime,
|
|
647
|
+
streaming: false,
|
|
648
|
+
});
|
|
649
|
+
return validation.data;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
catch (error) {
|
|
654
|
+
if (input.callerSignal?.aborted)
|
|
655
|
+
primaryError = error;
|
|
656
|
+
}
|
|
657
|
+
finally {
|
|
658
|
+
fallbackPrepared?.dispose();
|
|
659
|
+
this.ports.budget.release(fallbackId);
|
|
660
|
+
}
|
|
661
|
+
if (input.callerSignal?.aborted)
|
|
662
|
+
break;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
break;
|
|
666
|
+
}
|
|
667
|
+
this.recordTerminalFailure(prepared, primaryError);
|
|
668
|
+
this.ports.budget.release(input.requestId);
|
|
669
|
+
const error = primaryError instanceof Error ? primaryError : new Error(String(primaryError));
|
|
670
|
+
this.ports.emit({
|
|
671
|
+
type: "request.failed",
|
|
672
|
+
requestId: input.requestId,
|
|
673
|
+
timestamp: this.ports.now(),
|
|
674
|
+
provider: input.provider,
|
|
675
|
+
model: input.model,
|
|
676
|
+
durationMs: this.ports.now() - input.startTime,
|
|
677
|
+
attempts: attempt + 1,
|
|
678
|
+
error: error.message,
|
|
679
|
+
});
|
|
680
|
+
await this.ports.plugins.fireError({
|
|
681
|
+
requestId: input.requestId,
|
|
682
|
+
provider: input.provider,
|
|
683
|
+
model: input.model,
|
|
684
|
+
error,
|
|
685
|
+
durationMs: this.ports.now() - input.startTime,
|
|
686
|
+
attempts: attempt + 1,
|
|
687
|
+
});
|
|
688
|
+
throw primaryError;
|
|
689
|
+
}
|
|
690
|
+
finally {
|
|
691
|
+
prepared.dispose();
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
recordTerminalFailure(prepared, error) {
|
|
695
|
+
if (!prepared.context.callerSignal?.aborted) {
|
|
696
|
+
prepared.circuitBreaker?.recordFailure(error instanceof Error ? error : undefined);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
prepare(input, operation = "complete") {
|
|
700
|
+
try {
|
|
701
|
+
return this.ports.executor.prepare({
|
|
702
|
+
requestId: input.requestId,
|
|
703
|
+
operation,
|
|
704
|
+
provider: input.provider,
|
|
705
|
+
model: input.model,
|
|
706
|
+
policy: input.policy,
|
|
707
|
+
providerTimeoutMs: input.providerTimeoutMs,
|
|
708
|
+
callerSignal: input.callerSignal,
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
catch (error) {
|
|
712
|
+
if (error instanceof CircuitBreakerOpenError) {
|
|
713
|
+
this.ports.onCircuitOpen?.(input.requestId, input.provider);
|
|
714
|
+
}
|
|
715
|
+
throw error;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
emitCompleted(input, attempts, structuredOutput, provider, model) {
|
|
719
|
+
this.ports.emit({
|
|
720
|
+
type: "request.completed",
|
|
721
|
+
requestId: input.requestId,
|
|
722
|
+
timestamp: this.ports.now(),
|
|
723
|
+
provider,
|
|
724
|
+
model,
|
|
725
|
+
durationMs: this.ports.now() - input.startTime,
|
|
726
|
+
attempts,
|
|
727
|
+
structuredOutput,
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
}
|