@mupt-ai/dari-gateway 0.1.0 → 0.2.1
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 +8 -1
- package/dist/bun.d.ts +2 -0
- package/dist/bun.js +4 -0
- package/dist/context/eval_catalog.d.ts +3 -0
- package/dist/context/eval_catalog.js +7 -0
- package/dist/context/interaction_fingerprint.d.ts +27 -0
- package/dist/context/interaction_fingerprint.js +519 -0
- package/dist/context/interaction_fingerprint_format.d.ts +42 -0
- package/dist/context/interaction_fingerprint_format.js +113 -0
- package/dist/context/interaction_lease_depth.d.ts +2 -0
- package/dist/context/interaction_lease_depth.js +110 -0
- package/dist/context/lease_history.d.ts +7 -0
- package/dist/context/lease_history.js +158 -0
- package/dist/context/pricing.d.ts +4 -0
- package/dist/context/pricing.js +23 -0
- package/dist/context/provider_prompt_accounting.d.ts +14 -0
- package/dist/context/provider_prompt_accounting.js +187 -0
- package/dist/context/routing_context.d.ts +40 -0
- package/dist/context/routing_context.js +211 -0
- package/dist/http/auth.d.ts +2 -0
- package/dist/http/auth.js +35 -0
- package/dist/http/errors.d.ts +19 -0
- package/dist/http/errors.js +89 -0
- package/dist/http/handler.d.ts +12 -0
- package/dist/http/handler.js +81 -0
- package/dist/http/http2_server.d.ts +6 -0
- package/dist/http/http2_server.js +103 -0
- package/dist/http/serve.d.ts +20 -0
- package/dist/http/serve.js +78 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +8 -0
- package/dist/ingress/anthropic_messages.d.ts +3 -0
- package/dist/ingress/anthropic_messages.js +122 -0
- package/dist/ingress/anthropic_models.d.ts +1 -0
- package/dist/ingress/anthropic_models.js +23 -0
- package/dist/ingress/chat_completions.d.ts +2 -0
- package/dist/ingress/chat_completions.js +37 -0
- package/dist/ingress/claude_code/request_context.d.ts +2 -0
- package/dist/ingress/claude_code/request_context.js +31 -0
- package/dist/ingress/codex.d.ts +3 -0
- package/dist/ingress/codex.js +65 -0
- package/dist/ingress/codex_models.d.ts +2 -0
- package/dist/ingress/codex_models.js +89 -0
- package/dist/ingress/json.d.ts +1 -0
- package/dist/ingress/json.js +95 -0
- package/dist/ingress/models.d.ts +1 -0
- package/dist/ingress/models.js +53 -0
- package/dist/interaction/capabilities.d.ts +3 -0
- package/dist/interaction/capabilities.js +24 -0
- package/dist/interaction/image.d.ts +4 -0
- package/dist/interaction/image.js +13 -0
- package/dist/interaction/portable_reasoning_state.d.ts +4 -0
- package/dist/interaction/portable_reasoning_state.js +18 -0
- package/dist/interaction/provider_continuation_state.d.ts +6 -0
- package/dist/interaction/provider_continuation_state.js +27 -0
- package/dist/interaction/tool_identity.d.ts +10 -0
- package/dist/interaction/tool_identity.js +41 -0
- package/dist/interaction/types.d.ts +203 -0
- package/dist/interaction/types.js +5 -0
- package/dist/models/context_limits.d.ts +2 -0
- package/dist/models/context_limits.js +7 -0
- package/dist/models/execution_provider.d.ts +8 -0
- package/dist/models/execution_provider.js +13 -0
- package/dist/models/gateways.d.ts +11 -0
- package/dist/models/gateways.js +45 -0
- package/dist/models/ids.d.ts +1 -0
- package/dist/models/ids.js +15 -0
- package/dist/models/provider_defaults.d.ts +4 -0
- package/dist/models/provider_defaults.js +41 -0
- package/dist/models/runtime_overrides.d.ts +4 -0
- package/dist/models/runtime_overrides.js +209 -0
- package/dist/observability/errors.d.ts +8 -0
- package/dist/observability/errors.js +11 -0
- package/dist/platform/platform.d.ts +152 -0
- package/dist/platform/platform.js +11 -0
- package/dist/platform/routing_contract.d.ts +2 -0
- package/dist/platform/routing_contract.js +5 -0
- package/dist/protocol/input/anthropic_messages.d.ts +2 -0
- package/dist/protocol/input/anthropic_messages.js +494 -0
- package/dist/protocol/input/chat_completions.d.ts +4 -0
- package/dist/protocol/input/chat_completions.js +212 -0
- package/dist/protocol/input/chat_completions_validation.d.ts +2 -0
- package/dist/protocol/input/chat_completions_validation.js +257 -0
- package/dist/protocol/input/openai_responses.d.ts +2 -0
- package/dist/protocol/input/openai_responses.js +666 -0
- package/dist/protocol/input/router_model.d.ts +7 -0
- package/dist/protocol/input/router_model.js +43 -0
- package/dist/protocol/output/anthropic_messages.d.ts +10 -0
- package/dist/protocol/output/anthropic_messages.js +15 -0
- package/dist/protocol/output/anthropic_messages_output.d.ts +10 -0
- package/dist/protocol/output/anthropic_messages_output.js +124 -0
- package/dist/protocol/output/anthropic_messages_stream.d.ts +16 -0
- package/dist/protocol/output/anthropic_messages_stream.js +267 -0
- package/dist/protocol/output/chat_completions.d.ts +29 -0
- package/dist/protocol/output/chat_completions.js +406 -0
- package/dist/protocol/output/chat_reasoning_detail.d.ts +2 -0
- package/dist/protocol/output/chat_reasoning_detail.js +13 -0
- package/dist/protocol/output/delivered_stream_snapshot.d.ts +10 -0
- package/dist/protocol/output/delivered_stream_snapshot.js +31 -0
- package/dist/protocol/output/openai_responses.d.ts +10 -0
- package/dist/protocol/output/openai_responses.js +14 -0
- package/dist/protocol/output/openai_responses_output.d.ts +14 -0
- package/dist/protocol/output/openai_responses_output.js +125 -0
- package/dist/protocol/output/openai_responses_stream.d.ts +18 -0
- package/dist/protocol/output/openai_responses_stream.js +546 -0
- package/dist/protocol/output/sse_stream.d.ts +31 -0
- package/dist/protocol/output/sse_stream.js +67 -0
- package/dist/providers/anthropic_safeguards.d.ts +11 -0
- package/dist/providers/anthropic_safeguards.js +81 -0
- package/dist/providers/aws_assume_role.d.ts +17 -0
- package/dist/providers/aws_assume_role.js +107 -0
- package/dist/providers/cancel_estimate.d.ts +43 -0
- package/dist/providers/cancel_estimate.js +89 -0
- package/dist/providers/execution.d.ts +72 -0
- package/dist/providers/execution.js +491 -0
- package/dist/providers/model_capabilities.d.ts +11 -0
- package/dist/providers/model_capabilities.js +29 -0
- package/dist/providers/pi.d.ts +65 -0
- package/dist/providers/pi.js +319 -0
- package/dist/providers/pi_context.d.ts +7 -0
- package/dist/providers/pi_context.js +405 -0
- package/dist/providers/pi_options.d.ts +11 -0
- package/dist/providers/pi_options.js +398 -0
- package/dist/providers/pi_output.d.ts +2 -0
- package/dist/providers/pi_output.js +453 -0
- package/dist/providers/provider_attempt_runner.d.ts +55 -0
- package/dist/providers/provider_attempt_runner.js +90 -0
- package/dist/providers/provider_call.d.ts +77 -0
- package/dist/providers/provider_call.js +292 -0
- package/dist/providers/provider_reported_cost.d.ts +10 -0
- package/dist/providers/provider_reported_cost.js +123 -0
- package/dist/providers/reasoning.d.ts +12 -0
- package/dist/providers/reasoning.js +30 -0
- package/dist/providers/response_capture.d.ts +23 -0
- package/dist/providers/response_capture.js +47 -0
- package/dist/providers/stream_finalization.d.ts +46 -0
- package/dist/providers/stream_finalization.js +165 -0
- package/dist/providers/subscription_limits.d.ts +31 -0
- package/dist/providers/subscription_limits.js +163 -0
- package/dist/providers/subscription_usage.d.ts +10 -0
- package/dist/providers/subscription_usage.js +79 -0
- package/dist/runtime/claude_code/auxiliary_routing.d.ts +3 -0
- package/dist/runtime/claude_code/auxiliary_routing.js +60 -0
- package/dist/runtime/customer_disconnect.d.ts +20 -0
- package/dist/runtime/customer_disconnect.js +49 -0
- package/dist/runtime/explicit_model.d.ts +4 -0
- package/dist/runtime/explicit_model.js +53 -0
- package/dist/runtime/harness_identity/conversation.d.ts +33 -0
- package/dist/runtime/harness_identity/conversation.js +39 -0
- package/dist/runtime/harness_identity/extract.d.ts +8 -0
- package/dist/runtime/harness_identity/extract.js +214 -0
- package/dist/runtime/harness_identity/index.d.ts +2 -0
- package/dist/runtime/harness_identity/index.js +2 -0
- package/dist/runtime/prepare_routed_completion.d.ts +33 -0
- package/dist/runtime/prepare_routed_completion.js +167 -0
- package/dist/runtime/provider_stream_lifecycle.d.ts +31 -0
- package/dist/runtime/provider_stream_lifecycle.js +88 -0
- package/dist/runtime/provider_stream_outcome.d.ts +23 -0
- package/dist/runtime/provider_stream_outcome.js +55 -0
- package/dist/runtime/request_tags.d.ts +7 -0
- package/dist/runtime/request_tags.js +33 -0
- package/dist/runtime/routed_completion.d.ts +17 -0
- package/dist/runtime/routed_completion.js +80 -0
- package/dist/runtime/routed_protocol.d.ts +30 -0
- package/dist/runtime/routed_protocol.js +1 -0
- package/dist/runtime/routed_runtime.d.ts +40 -0
- package/dist/runtime/routed_runtime.js +535 -0
- package/dist/runtime/routing_lease.d.ts +26 -0
- package/dist/runtime/routing_lease.js +210 -0
- package/dist/runtime/runtime_reporter.d.ts +71 -0
- package/dist/runtime/runtime_reporter.js +35 -0
- package/dist/self_host/config.d.ts +46 -0
- package/dist/self_host/config.js +105 -0
- package/dist/self_host/main.d.ts +16 -0
- package/dist/self_host/main.js +95 -0
- package/dist/self_host/platform.d.ts +13 -0
- package/dist/self_host/platform.js +50 -0
- package/dist/self_host/prefix_store.d.ts +46 -0
- package/dist/self_host/prefix_store.js +261 -0
- package/dist/self_host/resolve.d.ts +28 -0
- package/dist/self_host/resolve.js +171 -0
- package/dist/self_host/subscriptions/access.d.ts +10 -0
- package/dist/self_host/subscriptions/access.js +37 -0
- package/dist/self_host/subscriptions/http.d.ts +15 -0
- package/dist/self_host/subscriptions/http.js +187 -0
- package/dist/self_host/subscriptions/index.d.ts +24 -0
- package/dist/self_host/subscriptions/index.js +24 -0
- package/dist/self_host/subscriptions/oauth.d.ts +55 -0
- package/dist/self_host/subscriptions/oauth.js +281 -0
- package/dist/self_host/subscriptions/store.d.ts +78 -0
- package/dist/self_host/subscriptions/store.js +235 -0
- package/dist/strategies/auto_routing_selector.d.ts +12 -0
- package/dist/strategies/auto_routing_selector.js +61 -0
- package/dist/strategies/eval_relevance.d.ts +17 -0
- package/dist/strategies/eval_relevance.js +32 -0
- package/dist/strategies/fallback_decision.d.ts +8 -0
- package/dist/strategies/fallback_decision.js +17 -0
- package/dist/strategies/priority_selector.d.ts +7 -0
- package/dist/strategies/priority_selector.js +37 -0
- package/dist/strategies/route_decision.d.ts +22 -0
- package/dist/strategies/route_decision.js +65 -0
- package/dist/strategies/routing.d.ts +74 -0
- package/dist/strategies/routing.js +280 -0
- package/dist/strategies/selector_projection.d.ts +15 -0
- package/dist/strategies/selector_projection.js +161 -0
- package/dist/telemetry/background.d.ts +7 -0
- package/dist/telemetry/background.js +33 -0
- package/dist/telemetry/benchmark_replay.d.ts +37 -0
- package/dist/telemetry/benchmark_replay.js +108 -0
- package/dist/telemetry/billing_finalization.d.ts +64 -0
- package/dist/telemetry/billing_finalization.js +96 -0
- package/dist/telemetry/ingest_values.d.ts +4 -0
- package/dist/telemetry/ingest_values.js +12 -0
- package/dist/telemetry/model_cost.d.ts +3 -0
- package/dist/telemetry/model_cost.js +15 -0
- package/dist/telemetry/prefix_state.d.ts +47 -0
- package/dist/telemetry/prefix_state.js +116 -0
- package/dist/telemetry/report.d.ts +88 -0
- package/dist/telemetry/report.js +621 -0
- package/dist/telemetry/requests/interaction_observability.d.ts +45 -0
- package/dist/telemetry/requests/interaction_observability.js +180 -0
- package/dist/telemetry/requests/retained_interaction_request.d.ts +8 -0
- package/dist/telemetry/requests/retained_interaction_request.js +10 -0
- package/dist/telemetry/scrub.d.ts +2 -0
- package/dist/telemetry/scrub.js +31 -0
- package/dist/telemetry/skills/claude/catalog.d.ts +6 -0
- package/dist/telemetry/skills/claude/catalog.js +99 -0
- package/dist/telemetry/skills/claude/file_reads.d.ts +4 -0
- package/dist/telemetry/skills/claude/file_reads.js +110 -0
- package/dist/telemetry/skills/claude/observations.d.ts +3 -0
- package/dist/telemetry/skills/claude/observations.js +366 -0
- package/dist/telemetry/skills/codex/catalog.d.ts +11 -0
- package/dist/telemetry/skills/codex/catalog.js +138 -0
- package/dist/telemetry/skills/codex/exec_file_reads.d.ts +9 -0
- package/dist/telemetry/skills/codex/exec_file_reads.js +36 -0
- package/dist/telemetry/skills/codex/explicit.d.ts +3 -0
- package/dist/telemetry/skills/codex/explicit.js +90 -0
- package/dist/telemetry/skills/codex/file_reads.d.ts +4 -0
- package/dist/telemetry/skills/codex/file_reads.js +37 -0
- package/dist/telemetry/skills/codex/identity.d.ts +2 -0
- package/dist/telemetry/skills/codex/identity.js +20 -0
- package/dist/telemetry/skills/observations.d.ts +21 -0
- package/dist/telemetry/skills/observations.js +41 -0
- package/dist/telemetry/skills/paths.d.ts +11 -0
- package/dist/telemetry/skills/paths.js +61 -0
- package/dist/telemetry/skills/pi.d.ts +6 -0
- package/dist/telemetry/skills/pi.js +223 -0
- package/dist/telemetry/skills/shell_file_reads.d.ts +7 -0
- package/dist/telemetry/skills/shell_file_reads.js +226 -0
- package/dist/telemetry/skills/shell_words.d.ts +13 -0
- package/dist/telemetry/skills/shell_words.js +189 -0
- package/dist/telemetry/tools/input.d.ts +2 -0
- package/dist/telemetry/tools/input.js +14 -0
- package/dist/telemetry/tools/observations.d.ts +29 -0
- package/dist/telemetry/tools/observations.js +247 -0
- package/dist/telemetry/usage.d.ts +57 -0
- package/dist/telemetry/usage.js +136 -0
- package/dist/types.d.ts +94 -0
- package/dist/types.js +1 -0
- package/package.json +22 -5
- package/src/bun.ts +4 -0
- package/src/context/eval_catalog.ts +1 -1
- package/src/context/interaction_fingerprint.ts +9 -9
- package/src/context/interaction_fingerprint_format.ts +1 -1
- package/src/context/interaction_lease_depth.ts +1 -1
- package/src/context/pricing.ts +1 -1
- package/src/context/provider_prompt_accounting.ts +5 -5
- package/src/context/routing_context.ts +17 -17
- package/src/http/auth.ts +1 -1
- package/src/http/handler.ts +8 -8
- package/src/http/serve.ts +3 -3
- package/src/index.ts +10 -9
- package/src/ingress/anthropic_messages.ts +12 -12
- package/src/ingress/anthropic_models.ts +1 -1
- package/src/ingress/chat_completions.ts +9 -9
- package/src/ingress/claude_code/request_context.ts +2 -2
- package/src/ingress/codex.ts +10 -10
- package/src/ingress/codex_models.ts +4 -4
- package/src/ingress/json.ts +1 -1
- package/src/ingress/models.ts +10 -10
- package/src/interaction/capabilities.ts +1 -1
- package/src/interaction/portable_reasoning_state.ts +1 -1
- package/src/interaction/provider_continuation_state.ts +1 -1
- package/src/interaction/tool_identity.ts +2 -2
- package/src/interaction/types.ts +3 -0
- package/src/models/context_limits.ts +1 -1
- package/src/models/execution_provider.ts +1 -1
- package/src/models/ids.ts +1 -1
- package/src/models/provider_defaults.ts +1 -1
- package/src/models/runtime_overrides.ts +1 -1
- package/src/observability/errors.ts +2 -2
- package/src/platform/platform.ts +36 -4
- package/src/protocol/input/anthropic_messages.ts +7 -7
- package/src/protocol/input/chat_completions.ts +5 -5
- package/src/protocol/input/chat_completions_validation.ts +4 -4
- package/src/protocol/input/openai_responses.ts +6 -6
- package/src/protocol/output/anthropic_messages.ts +3 -3
- package/src/protocol/output/anthropic_messages_output.ts +6 -4
- package/src/protocol/output/anthropic_messages_stream.ts +7 -7
- package/src/protocol/output/chat_completions.ts +13 -13
- package/src/protocol/output/chat_reasoning_detail.ts +1 -1
- package/src/protocol/output/openai_responses.ts +3 -3
- package/src/protocol/output/openai_responses_output.ts +4 -2
- package/src/protocol/output/openai_responses_stream.ts +6 -6
- package/src/protocol/output/sse_stream.ts +3 -3
- package/src/providers/anthropic_safeguards.ts +2 -2
- package/src/providers/aws_assume_role.ts +2 -2
- package/src/providers/cancel_estimate.ts +1 -1
- package/src/providers/execution.ts +17 -17
- package/src/providers/pi.ts +115 -41
- package/src/providers/pi_context.ts +6 -6
- package/src/providers/pi_options.ts +283 -453
- package/src/providers/pi_output.ts +6 -5
- package/src/providers/provider_attempt_runner.ts +1 -1
- package/src/providers/provider_call.ts +37 -18
- package/src/providers/provider_reported_cost.ts +138 -0
- package/src/providers/reasoning.ts +2 -2
- package/src/providers/stream_finalization.ts +10 -10
- package/src/providers/subscription_limits.ts +3 -3
- package/src/runtime/claude_code/auxiliary_routing.ts +5 -5
- package/src/runtime/customer_disconnect.ts +1 -1
- package/src/runtime/explicit_model.ts +31 -4
- package/src/runtime/harness_identity/conversation.ts +1 -1
- package/src/runtime/harness_identity/extract.ts +3 -3
- package/src/runtime/harness_identity/index.ts +2 -2
- package/src/runtime/prepare_routed_completion.ts +46 -10
- package/src/runtime/provider_stream_lifecycle.ts +1 -1
- package/src/runtime/provider_stream_outcome.ts +2 -2
- package/src/runtime/request_tags.ts +34 -0
- package/src/runtime/routed_completion.ts +13 -11
- package/src/runtime/routed_protocol.ts +2 -2
- package/src/runtime/routed_runtime.ts +38 -20
- package/src/runtime/routing_lease.ts +7 -7
- package/src/runtime/runtime_reporter.ts +3 -3
- package/src/self_host/main.ts +19 -14
- package/src/self_host/platform.ts +5 -5
- package/src/self_host/prefix_store.ts +2 -2
- package/src/self_host/resolve.ts +11 -11
- package/src/self_host/subscriptions/access.ts +2 -2
- package/src/self_host/subscriptions/http.ts +2 -2
- package/src/self_host/subscriptions/index.ts +4 -4
- package/src/self_host/subscriptions/store.ts +1 -1
- package/src/strategies/auto_routing_selector.ts +2 -2
- package/src/strategies/eval_relevance.ts +3 -3
- package/src/strategies/priority_selector.ts +1 -1
- package/src/strategies/route_decision.ts +7 -7
- package/src/strategies/routing.ts +11 -11
- package/src/strategies/selector_projection.ts +2 -2
- package/src/telemetry/billing_finalization.ts +3 -3
- package/src/telemetry/model_cost.ts +1 -1
- package/src/telemetry/prefix_state.ts +4 -4
- package/src/telemetry/report.ts +30 -25
- package/src/telemetry/requests/interaction_observability.ts +1 -1
- package/src/telemetry/requests/retained_interaction_request.ts +1 -1
- package/src/telemetry/skills/claude/catalog.ts +1 -1
- package/src/telemetry/skills/claude/file_reads.ts +6 -6
- package/src/telemetry/skills/claude/observations.ts +3 -3
- package/src/telemetry/skills/codex/catalog.ts +3 -3
- package/src/telemetry/skills/codex/exec_file_reads.ts +4 -4
- package/src/telemetry/skills/codex/explicit.ts +3 -3
- package/src/telemetry/skills/codex/file_reads.ts +5 -5
- package/src/telemetry/skills/observations.ts +7 -7
- package/src/telemetry/skills/pi.ts +3 -3
- package/src/telemetry/skills/shell_file_reads.ts +2 -2
- package/src/telemetry/skills/shell_words.ts +1 -1
- package/src/telemetry/tools/input.ts +1 -1
- package/src/telemetry/tools/observations.ts +9 -9
- package/src/telemetry/usage.ts +9 -5
- package/src/types.ts +15 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { isRecord } from "@mupt-ai/dari-router/policy-engine";
|
|
2
|
+
import { scrubSecrets } from "../telemetry/scrub.js";
|
|
3
|
+
export function isProviderFailureMessage(message) {
|
|
4
|
+
return message.stopReason === "error" || message.stopReason === "aborted";
|
|
5
|
+
}
|
|
6
|
+
export function providerFailureFromEvent(event) {
|
|
7
|
+
if (isRecord(event.error)) {
|
|
8
|
+
return isProviderFailureMessage(event.error)
|
|
9
|
+
? event.error
|
|
10
|
+
: providerFailure(stringValue(event.error.errorMessage) ?? stringValue(event.error.message) ?? "Provider request failed.");
|
|
11
|
+
}
|
|
12
|
+
return providerFailure(stringValue(event.error) ?? stringValue(event.errorMessage) ?? stringValue(event.message) ?? "Provider request failed.");
|
|
13
|
+
}
|
|
14
|
+
export function providerFailureFromCaught(error) {
|
|
15
|
+
const failure = providerFailure(caughtErrorMessage(error));
|
|
16
|
+
if (!isRecord(error))
|
|
17
|
+
return failure;
|
|
18
|
+
for (const key of ["statusCode", "status_code", "status", "httpStatus", "code", "error"]) {
|
|
19
|
+
if (error[key] !== undefined)
|
|
20
|
+
failure[key] = error[key];
|
|
21
|
+
}
|
|
22
|
+
return failure;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A terminal provider message remains the source of truth for exact usage even
|
|
26
|
+
* when translating that message into a client protocol response fails.
|
|
27
|
+
*/
|
|
28
|
+
export function providerFailureFromTerminal(error, terminalMessage) {
|
|
29
|
+
return {
|
|
30
|
+
...providerFailureFromCaught(error),
|
|
31
|
+
...(typeof terminalMessage.responseId === "string"
|
|
32
|
+
? { responseId: terminalMessage.responseId }
|
|
33
|
+
: {}),
|
|
34
|
+
...(isRecord(terminalMessage.usage)
|
|
35
|
+
? { usage: terminalMessage.usage }
|
|
36
|
+
: {}),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function caughtErrorMessage(error) {
|
|
40
|
+
if (error instanceof Error)
|
|
41
|
+
return error.message;
|
|
42
|
+
if (typeof error === "string")
|
|
43
|
+
return error;
|
|
44
|
+
return "Provider request failed.";
|
|
45
|
+
}
|
|
46
|
+
export function providerFailure(message) {
|
|
47
|
+
return { stopReason: "error", errorMessage: message, content: [] };
|
|
48
|
+
}
|
|
49
|
+
export function providerFailureMessage(message) {
|
|
50
|
+
const detail = stringValue(message.errorMessage);
|
|
51
|
+
return detail ? scrubSecrets(detail) : "Provider request failed.";
|
|
52
|
+
}
|
|
53
|
+
function stringValue(value) {
|
|
54
|
+
return typeof value === "string" && value ? value : undefined;
|
|
55
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { RequestTags } from "../platform/platform.js";
|
|
2
|
+
/**
|
|
3
|
+
* Parse `X-Dari-Tags: run=r1,trial=3,setting=low` into attribution labels for
|
|
4
|
+
* the platform's request and usage records. Malformed or oversized headers
|
|
5
|
+
* are refused rather than silently dropped, so cost is never misattributed.
|
|
6
|
+
*/
|
|
7
|
+
export declare function requestTags(header: string | null): RequestTags;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { HttpError } from "../http/errors.js";
|
|
2
|
+
const TAGS_MAX_CHARS = 2048;
|
|
3
|
+
const TAGS_MAX_COUNT = 32;
|
|
4
|
+
const TAG_KEY = /^[A-Za-z0-9_.:-]{1,64}$/;
|
|
5
|
+
const TAG_VALUE_MAX_CHARS = 256;
|
|
6
|
+
/**
|
|
7
|
+
* Parse `X-Dari-Tags: run=r1,trial=3,setting=low` into attribution labels for
|
|
8
|
+
* the platform's request and usage records. Malformed or oversized headers
|
|
9
|
+
* are refused rather than silently dropped, so cost is never misattributed.
|
|
10
|
+
*/
|
|
11
|
+
export function requestTags(header) {
|
|
12
|
+
const tags = {};
|
|
13
|
+
if (header === null || header.trim() === "")
|
|
14
|
+
return tags;
|
|
15
|
+
if (header.length > TAGS_MAX_CHARS)
|
|
16
|
+
throw invalidTags(`X-Dari-Tags exceeds ${TAGS_MAX_CHARS} characters.`);
|
|
17
|
+
const pairs = header.split(",");
|
|
18
|
+
if (pairs.length > TAGS_MAX_COUNT)
|
|
19
|
+
throw invalidTags(`X-Dari-Tags has more than ${TAGS_MAX_COUNT} tags.`);
|
|
20
|
+
for (const pair of pairs) {
|
|
21
|
+
const separator = pair.indexOf("=");
|
|
22
|
+
const key = pair.slice(0, separator).trim();
|
|
23
|
+
const value = pair.slice(separator + 1).trim();
|
|
24
|
+
if (separator < 0 || !TAG_KEY.test(key) || value.length > TAG_VALUE_MAX_CHARS) {
|
|
25
|
+
throw invalidTags("X-Dari-Tags must be comma-separated key=value pairs.");
|
|
26
|
+
}
|
|
27
|
+
tags[key] = value;
|
|
28
|
+
}
|
|
29
|
+
return tags;
|
|
30
|
+
}
|
|
31
|
+
function invalidTags(message) {
|
|
32
|
+
return new HttpError(400, message, "invalid_tags");
|
|
33
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type InteractionRequest } from "../interaction/types.js";
|
|
2
|
+
import type { SelectorBackend } from "../strategies/routing.js";
|
|
3
|
+
import type { ClientHarness } from "../types.js";
|
|
4
|
+
import type { HarnessConversationIdentity } from "./harness_identity/index.js";
|
|
5
|
+
import type { RoutedProtocol } from "./routed_protocol.js";
|
|
6
|
+
export declare function handleRoutedCompletion(args: {
|
|
7
|
+
request: Request;
|
|
8
|
+
routerId: string;
|
|
9
|
+
apiKey: string;
|
|
10
|
+
payload: InteractionRequest;
|
|
11
|
+
responseBody: RoutedProtocol["responseBody"];
|
|
12
|
+
streamBody: RoutedProtocol["streamBody"];
|
|
13
|
+
errorResponse: (error: unknown) => Response;
|
|
14
|
+
selectorBackend: SelectorBackend;
|
|
15
|
+
clientHarness?: ClientHarness;
|
|
16
|
+
harnessConversationIdentity?: HarnessConversationIdentity;
|
|
17
|
+
}): Promise<Response>;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { isBackgroundRequest } from "../interaction/types.js";
|
|
3
|
+
import { explicitModelId } from "../protocol/input/router_model.js";
|
|
4
|
+
import { captureRequestError } from "../observability/errors.js";
|
|
5
|
+
import { assertAutoroutingAllowed, assertCreditAvailableBeforeRouting, routerSelectsModels } from "../telemetry/usage.js";
|
|
6
|
+
import { prepareRoutedCompletion } from "./prepare_routed_completion.js";
|
|
7
|
+
import { runPreparedRoutedCompletion } from "./routed_runtime.js";
|
|
8
|
+
export async function handleRoutedCompletion(args) {
|
|
9
|
+
const requestId = randomUUID();
|
|
10
|
+
const clientRequestId = normalizedClientRequestId(args.request.headers.get("x-request-id"));
|
|
11
|
+
const receivedAt = new Date();
|
|
12
|
+
const startedMs = Date.now();
|
|
13
|
+
const prepared = await prepareRoutedCompletion({
|
|
14
|
+
routerId: args.routerId,
|
|
15
|
+
apiKey: args.apiKey,
|
|
16
|
+
interaction: args.payload,
|
|
17
|
+
protocol: { responseBody: args.responseBody, streamBody: args.streamBody },
|
|
18
|
+
receivedAt,
|
|
19
|
+
startedMs,
|
|
20
|
+
requestId,
|
|
21
|
+
clientRequestId,
|
|
22
|
+
tagsHeader: args.request.headers.get("x-dari-tags"),
|
|
23
|
+
clientHarness: args.clientHarness,
|
|
24
|
+
harnessConversationIdentity: args.harnessConversationIdentity,
|
|
25
|
+
}).catch((error) => ({ error }));
|
|
26
|
+
if ("error" in prepared) {
|
|
27
|
+
const response = args.errorResponse(prepared.error);
|
|
28
|
+
captureRequestError(prepared.error, {
|
|
29
|
+
request: args.request,
|
|
30
|
+
routerId: args.routerId,
|
|
31
|
+
requestId,
|
|
32
|
+
sourceProtocol: args.payload.sourceProtocol,
|
|
33
|
+
status: response.status,
|
|
34
|
+
});
|
|
35
|
+
return responseWithRequestId(response, requestId);
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
// Only auto-selection needs the plan: an explicitly requested model and
|
|
39
|
+
// background work are served without the selector.
|
|
40
|
+
const selects = prepared.credentials.strict !== true &&
|
|
41
|
+
explicitModelId(prepared.interaction.model) === null &&
|
|
42
|
+
!isBackgroundRequest(prepared.interaction) &&
|
|
43
|
+
routerSelectsModels(prepared.credentials);
|
|
44
|
+
assertAutoroutingAllowed(prepared.credentials, selects);
|
|
45
|
+
assertCreditAvailableBeforeRouting(prepared.billable, prepared.credentials);
|
|
46
|
+
const response = await runPreparedRoutedCompletion({
|
|
47
|
+
prepared,
|
|
48
|
+
selectorBackend: args.selectorBackend,
|
|
49
|
+
clientSignal: args.request.signal,
|
|
50
|
+
});
|
|
51
|
+
return responseWithRequestId(response, requestId);
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
const terminal = prepared.reporter.freezeFailure(error);
|
|
55
|
+
terminal.report();
|
|
56
|
+
let response = args.errorResponse(error);
|
|
57
|
+
// Provider retries may already have consumed tokens before the HTTP error.
|
|
58
|
+
// Return the same model-only cost as successful OpenAI-compatible responses.
|
|
59
|
+
if (args.payload.sourceProtocol === "openai_chat") {
|
|
60
|
+
const body = await response.json();
|
|
61
|
+
response = Response.json({ ...body, usage: { cost: prepared.reporter.modelCost() } }, { status: response.status, headers: response.headers });
|
|
62
|
+
}
|
|
63
|
+
captureRequestError(error, {
|
|
64
|
+
request: args.request,
|
|
65
|
+
routerId: prepared.routerId,
|
|
66
|
+
requestId,
|
|
67
|
+
sourceProtocol: args.payload.sourceProtocol,
|
|
68
|
+
status: response.status,
|
|
69
|
+
});
|
|
70
|
+
return responseWithRequestId(response, requestId);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function normalizedClientRequestId(value) {
|
|
74
|
+
const normalized = value?.trim();
|
|
75
|
+
return normalized ? normalized.slice(0, 200) : null;
|
|
76
|
+
}
|
|
77
|
+
function responseWithRequestId(response, requestId) {
|
|
78
|
+
response.headers.set("X-Request-Id", requestId);
|
|
79
|
+
return response;
|
|
80
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { RoutingDecision } from "@mupt-ai/dari-router/policy-engine";
|
|
2
|
+
import type { InteractionOutput, InteractionRequest, ProviderIdentity } from "../interaction/types.js";
|
|
3
|
+
import type { ProviderStreamOutcome } from "./provider_stream_outcome.js";
|
|
4
|
+
export type RoutedCompletionResponseArgs = {
|
|
5
|
+
id: string;
|
|
6
|
+
model: string;
|
|
7
|
+
output: InteractionOutput;
|
|
8
|
+
modelCost?: number | null;
|
|
9
|
+
request: InteractionRequest;
|
|
10
|
+
routing: RoutingDecision;
|
|
11
|
+
conversationId?: string;
|
|
12
|
+
};
|
|
13
|
+
export type RoutedCompletionStreamArgs = {
|
|
14
|
+
id: string;
|
|
15
|
+
model: string;
|
|
16
|
+
routing: RoutingDecision;
|
|
17
|
+
events: AsyncIterable<Record<string, unknown>>;
|
|
18
|
+
source: ProviderIdentity;
|
|
19
|
+
request: InteractionRequest;
|
|
20
|
+
conversationId?: string;
|
|
21
|
+
onFinal?: (outcome: ProviderStreamOutcome) => void;
|
|
22
|
+
modelCost?: (message: Record<string, unknown>, failed: boolean) => number | null;
|
|
23
|
+
beforeClose?: (outcome: ProviderStreamOutcome) => Promise<void> | void;
|
|
24
|
+
clientSignal?: AbortSignal;
|
|
25
|
+
onAbort?: () => void;
|
|
26
|
+
};
|
|
27
|
+
export type RoutedProtocol = {
|
|
28
|
+
responseBody: (args: RoutedCompletionResponseArgs) => Record<string, unknown>;
|
|
29
|
+
streamBody: (args: RoutedCompletionStreamArgs) => ReadableStream<Uint8Array>;
|
|
30
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type PreviousDecision, type RouterPrefixHit, type RoutingDecision } from "@mupt-ai/dari-router/policy-engine";
|
|
2
|
+
import type { InteractionRequest } from "../interaction/types.js";
|
|
3
|
+
import { type CompletedNonStreamingExecution } from "../providers/execution.js";
|
|
4
|
+
import type { SelectorBackend } from "../strategies/routing.js";
|
|
5
|
+
import type { ResolvedRouter } from "../types.js";
|
|
6
|
+
import type { PreparedRoutedCompletion } from "./prepare_routed_completion.js";
|
|
7
|
+
import { type RoutedRuntimeReporter } from "./runtime_reporter.js";
|
|
8
|
+
export type RoutedRuntimeResult = {
|
|
9
|
+
routing: RoutingDecision;
|
|
10
|
+
output: CompletedNonStreamingExecution["output"];
|
|
11
|
+
prefixHit?: RouterPrefixHit;
|
|
12
|
+
};
|
|
13
|
+
export declare class RoutedRuntimeProviderSettledError extends Error {
|
|
14
|
+
readonly name = "RoutedRuntimeProviderSettledError";
|
|
15
|
+
readonly runtimeResult: RoutedRuntimeResult;
|
|
16
|
+
readonly cause: unknown;
|
|
17
|
+
constructor(message: string, runtimeResult: RoutedRuntimeResult, cause: unknown);
|
|
18
|
+
}
|
|
19
|
+
export type ExplicitRoutedInteractionArgs = {
|
|
20
|
+
routerId: string;
|
|
21
|
+
interaction: InteractionRequest;
|
|
22
|
+
credentials: ResolvedRouter;
|
|
23
|
+
selectorBackend: SelectorBackend;
|
|
24
|
+
previousDecision?: PreviousDecision;
|
|
25
|
+
billable?: boolean;
|
|
26
|
+
reporter?: RoutedRuntimeReporter;
|
|
27
|
+
};
|
|
28
|
+
export type { SelectorAdapter, SelectorAdapterArgs, SelectorBackend, SelectorRun, } from "../strategies/routing.js";
|
|
29
|
+
export type { ExperimentRoutedRuntimeReporter, RoutedRuntimeReporter, } from "./runtime_reporter.js";
|
|
30
|
+
export { createExperimentRoutedRuntimeReporter } from "./runtime_reporter.js";
|
|
31
|
+
export declare function runPreparedRoutedCompletion(args: {
|
|
32
|
+
prepared: PreparedRoutedCompletion;
|
|
33
|
+
selectorBackend: SelectorBackend;
|
|
34
|
+
clientSignal?: AbortSignal;
|
|
35
|
+
}): Promise<Response>;
|
|
36
|
+
export declare function runPreparedRoutedInteraction(args: {
|
|
37
|
+
prepared: PreparedRoutedCompletion;
|
|
38
|
+
selectorBackend: SelectorBackend;
|
|
39
|
+
}): Promise<RoutedRuntimeResult>;
|
|
40
|
+
export declare function runExplicitRoutedInteraction(args: ExplicitRoutedInteractionArgs): Promise<RoutedRuntimeResult>;
|