@mupt-ai/dari-gateway 0.2.0 → 0.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 +8 -4
- 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 +27 -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 +88 -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 +7 -0
- package/dist/index.js +9 -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 +44 -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 +69 -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 +65 -0
- package/dist/ingress/muse_code.d.ts +2 -0
- package/dist/ingress/muse_code.js +8 -0
- package/dist/ingress/muse_models.d.ts +9 -0
- package/dist/ingress/muse_models.js +32 -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 +204 -0
- package/dist/interaction/types.js +5 -0
- package/dist/models/catalog.d.ts +35 -0
- package/dist/models/catalog.js +232 -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 +9 -0
- package/dist/models/execution_provider.js +17 -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 +44 -0
- package/dist/models/runtime_overrides.d.ts +7 -0
- package/dist/models/runtime_overrides.js +226 -0
- package/dist/models/subscriptions.d.ts +8 -0
- package/dist/models/subscriptions.js +37 -0
- package/dist/observability/errors.d.ts +8 -0
- package/dist/observability/errors.js +11 -0
- package/dist/platform/platform.d.ts +156 -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 +214 -0
- package/dist/protocol/input/chat_completions_validation.d.ts +2 -0
- package/dist/protocol/input/chat_completions_validation.js +258 -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 +325 -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 +410 -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 +296 -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 +33 -0
- package/dist/providers/subscription_limits.js +179 -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 +7 -0
- package/dist/runtime/explicit_model.js +57 -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 +186 -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/runtime/strict_provider_access.d.ts +6 -0
- package/dist/runtime/strict_provider_access.js +40 -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 +167 -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 +59 -0
- package/dist/self_host/subscriptions/oauth.js +376 -0
- package/dist/self_host/subscriptions/store.d.ts +78 -0
- package/dist/self_host/subscriptions/store.js +236 -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 +97 -0
- package/dist/types.js +1 -0
- package/package.json +22 -7
- package/src/bun.ts +2 -2
- 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 +6 -3
- package/src/context/provider_prompt_accounting.ts +5 -5
- package/src/context/routing_context.ts +16 -16
- package/src/http/auth.ts +1 -1
- package/src/http/handler.ts +15 -8
- package/src/http/serve.ts +3 -3
- package/src/index.ts +14 -6
- package/src/ingress/anthropic_messages.ts +12 -12
- package/src/ingress/anthropic_models.ts +1 -1
- package/src/ingress/chat_completions.ts +17 -10
- package/src/ingress/claude_code/request_context.ts +2 -2
- package/src/ingress/codex.ts +14 -10
- package/src/ingress/codex_models.ts +4 -4
- package/src/ingress/json.ts +1 -1
- package/src/ingress/models.ts +22 -10
- package/src/ingress/muse_code.ts +9 -0
- package/src/ingress/muse_models.ts +36 -0
- 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 +2 -0
- package/src/models/catalog.ts +310 -0
- package/src/models/context_limits.ts +1 -1
- package/src/models/execution_provider.ts +5 -2
- package/src/models/ids.ts +1 -1
- package/src/models/provider_defaults.ts +4 -1
- package/src/models/runtime_overrides.ts +23 -1
- package/src/models/subscriptions.ts +54 -0
- package/src/observability/errors.ts +2 -2
- package/src/platform/platform.ts +9 -5
- package/src/protocol/input/anthropic_messages.ts +7 -7
- package/src/protocol/input/chat_completions.ts +8 -6
- package/src/protocol/input/chat_completions_validation.ts +6 -5
- 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 +34 -19
- package/src/providers/pi_context.ts +6 -6
- package/src/providers/pi_options.ts +27 -11
- package/src/providers/pi_output.ts +5 -5
- package/src/providers/provider_attempt_runner.ts +1 -1
- package/src/providers/provider_call.ts +21 -17
- package/src/providers/provider_reported_cost.ts +3 -3
- package/src/providers/reasoning.ts +2 -2
- package/src/providers/stream_finalization.ts +9 -9
- package/src/providers/subscription_limits.ts +24 -7
- package/src/runtime/claude_code/auxiliary_routing.ts +5 -5
- package/src/runtime/customer_disconnect.ts +1 -1
- package/src/runtime/explicit_model.ts +19 -20
- 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 +35 -16
- package/src/runtime/provider_stream_lifecycle.ts +1 -1
- package/src/runtime/provider_stream_outcome.ts +2 -2
- package/src/runtime/request_tags.ts +2 -2
- package/src/runtime/routed_completion.ts +11 -11
- package/src/runtime/routed_protocol.ts +2 -2
- package/src/runtime/routed_runtime.ts +20 -19
- package/src/runtime/routing_lease.ts +7 -7
- package/src/runtime/runtime_reporter.ts +3 -3
- package/src/runtime/strict_provider_access.ts +42 -0
- package/src/self_host/main.ts +12 -12
- package/src/self_host/platform.ts +5 -5
- package/src/self_host/prefix_store.ts +2 -2
- package/src/self_host/resolve.ts +17 -31
- 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/oauth.ts +139 -8
- package/src/self_host/subscriptions/store.ts +2 -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 +10 -10
- 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 +21 -21
- 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 +5 -5
- package/src/types.ts +20 -3
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { HardCapability } from "@mupt-ai/dari-router/policy-engine";
|
|
2
|
+
import type { InteractionRequest } from "./types.js";
|
|
3
|
+
export declare function requiredCapabilitiesFor(request: Pick<InteractionRequest, "items" | "responseFormat" | "toolChoice" | "tools">): HardCapability[];
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export function requiredCapabilitiesFor(request) {
|
|
2
|
+
const capabilities = new Set();
|
|
3
|
+
if (request.items.some(itemHasImage))
|
|
4
|
+
capabilities.add("image_input");
|
|
5
|
+
if (request.items.some((item) => item.type === "reasoning_configuration")) {
|
|
6
|
+
capabilities.add("reasoning_configuration_update");
|
|
7
|
+
}
|
|
8
|
+
if (request.responseFormat !== undefined && request.responseFormat.type !== "text")
|
|
9
|
+
capabilities.add("structured_output");
|
|
10
|
+
if (request.tools.some((tool) => tool.kind === "hosted")
|
|
11
|
+
|| request.items.some(requiresWebSearch)
|
|
12
|
+
|| (typeof request.toolChoice === "object" && request.toolChoice.kind === "hosted")) {
|
|
13
|
+
capabilities.add("openai_hosted_web_search");
|
|
14
|
+
}
|
|
15
|
+
return [...capabilities];
|
|
16
|
+
}
|
|
17
|
+
function requiresWebSearch(item) {
|
|
18
|
+
return item.type === "hosted_tool_call" && item.tool === "web_search";
|
|
19
|
+
}
|
|
20
|
+
function itemHasImage(item) {
|
|
21
|
+
if (item.type === "message")
|
|
22
|
+
return item.content.some((part) => part.type === "image");
|
|
23
|
+
return item.type === "tool_result" && Array.isArray(item.output) && item.output.some((part) => part.type === "image");
|
|
24
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function parseDataImageUrl(url) {
|
|
2
|
+
const commaIndex = url.indexOf(",");
|
|
3
|
+
if (!url.startsWith("data:") || commaIndex === -1)
|
|
4
|
+
return undefined;
|
|
5
|
+
const header = url.slice(5, commaIndex);
|
|
6
|
+
const segments = header.split(";");
|
|
7
|
+
const mimeType = segments.shift();
|
|
8
|
+
const data = url.slice(commaIndex + 1);
|
|
9
|
+
if (!mimeType?.toLowerCase().startsWith("image/") || segments.at(-1)?.toLowerCase() !== "base64" || !data) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
return { mimeType, data };
|
|
13
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type PortableReasoningState } from "@mupt-ai/dari-router/protocols";
|
|
2
|
+
export type { PortableReasoningState } from "@mupt-ai/dari-router/protocols";
|
|
3
|
+
export declare function encodePortableReasoningState(state: PortableReasoningState): string;
|
|
4
|
+
export declare function decodeUntrustedPortableReasoningState(value: string): PortableReasoningState | null;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { decodeUntrustedPortableReasoningState as coreDecode, encodePortableReasoningState as coreEncode, } from "@mupt-ai/dari-router/protocols";
|
|
2
|
+
import { rethrowFrameworkErrorAsHttp } from "../http/errors.js";
|
|
3
|
+
export function encodePortableReasoningState(state) {
|
|
4
|
+
try {
|
|
5
|
+
return coreEncode(state);
|
|
6
|
+
}
|
|
7
|
+
catch (error) {
|
|
8
|
+
rethrowFrameworkErrorAsHttp(error, 500);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function decodeUntrustedPortableReasoningState(value) {
|
|
12
|
+
try {
|
|
13
|
+
return coreDecode(value);
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
rethrowFrameworkErrorAsHttp(error, 400);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type ProviderContinuationState, type ProviderIdentity } from "@mupt-ai/dari-router/protocols";
|
|
2
|
+
export type { ProviderContinuationState, ProviderIdentity } from "@mupt-ai/dari-router/protocols";
|
|
3
|
+
export { compatibleProviderContinuation, isAnthropicEffort, sameProviderIdentity } from "@mupt-ai/dari-router/protocols";
|
|
4
|
+
export declare function encodeProviderContinuationState(value: ProviderContinuationState): string;
|
|
5
|
+
export declare function decodeUntrustedProviderContinuationState(value: string): ProviderContinuationState | null;
|
|
6
|
+
export declare function providerIdentityFromModel(model: Record<string, unknown>): ProviderIdentity;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { decodeUntrustedProviderContinuationState as coreDecode, encodeProviderContinuationState as coreEncode, providerIdentityFromModel as coreProviderIdentityFromModel, } from "@mupt-ai/dari-router/protocols";
|
|
2
|
+
import { rethrowFrameworkErrorAsHttp } from "../http/errors.js";
|
|
3
|
+
export { compatibleProviderContinuation, isAnthropicEffort, sameProviderIdentity } from "@mupt-ai/dari-router/protocols";
|
|
4
|
+
export function encodeProviderContinuationState(value) {
|
|
5
|
+
try {
|
|
6
|
+
return coreEncode(value);
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
rethrowFrameworkErrorAsHttp(error, 500);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export function decodeUntrustedProviderContinuationState(value) {
|
|
13
|
+
try {
|
|
14
|
+
return coreDecode(value);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
rethrowFrameworkErrorAsHttp(error, 400);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export function providerIdentityFromModel(model) {
|
|
21
|
+
try {
|
|
22
|
+
return coreProviderIdentityFromModel(model);
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
rethrowFrameworkErrorAsHttp(error, 502);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { InteractionTool, ProviderIdentity } from "./types.js";
|
|
2
|
+
export declare const TOOL_CALL_ID_MAX_LENGTH = 512;
|
|
3
|
+
export declare function isProtocolToolCallId(value: unknown): value is string;
|
|
4
|
+
export declare function toolWireName(namespace: string | undefined, name: string): string;
|
|
5
|
+
export declare function toolsByWireName(tools: InteractionTool[]): Map<string, InteractionTool>;
|
|
6
|
+
export declare function providerToolCallId(callId: string, itemId: string | undefined, providerApi: string | null): string;
|
|
7
|
+
export declare function providerToolCallIdentity(id: string, source: ProviderIdentity): {
|
|
8
|
+
callId: string;
|
|
9
|
+
itemId?: string;
|
|
10
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { HttpError } from "../http/errors.js";
|
|
2
|
+
export const TOOL_CALL_ID_MAX_LENGTH = 512;
|
|
3
|
+
export function isProtocolToolCallId(value) {
|
|
4
|
+
return (typeof value === "string" &&
|
|
5
|
+
value.trim().length > 0 &&
|
|
6
|
+
value.length <= TOOL_CALL_ID_MAX_LENGTH);
|
|
7
|
+
}
|
|
8
|
+
export function toolWireName(namespace, name) {
|
|
9
|
+
return namespace ? `${namespace}__${name}` : name;
|
|
10
|
+
}
|
|
11
|
+
export function toolsByWireName(tools) {
|
|
12
|
+
const indexed = new Map();
|
|
13
|
+
for (const tool of tools) {
|
|
14
|
+
if (tool.kind === "hosted" || tool.kind === "server_tool")
|
|
15
|
+
continue;
|
|
16
|
+
const wireName = tool.kind === "client_tool" ? tool.name : toolWireName(tool.namespace, tool.name);
|
|
17
|
+
if (indexed.has(wireName)) {
|
|
18
|
+
throw new HttpError(400, `Tool names must remain unique after namespace encoding; '${wireName}' is ambiguous.`, "invalid_request_error", "tools");
|
|
19
|
+
}
|
|
20
|
+
indexed.set(wireName, tool);
|
|
21
|
+
}
|
|
22
|
+
return indexed;
|
|
23
|
+
}
|
|
24
|
+
export function providerToolCallId(callId, itemId, providerApi) {
|
|
25
|
+
if (!itemId || !usesResponsesToolCallIds(providerApi))
|
|
26
|
+
return callId;
|
|
27
|
+
return `${callId}|${itemId}`;
|
|
28
|
+
}
|
|
29
|
+
export function providerToolCallIdentity(id, source) {
|
|
30
|
+
const separator = usesResponsesToolCallIds(source.api) ? id.indexOf("|") : -1;
|
|
31
|
+
const identity = separator > 0 && separator < id.length - 1
|
|
32
|
+
? { callId: id.slice(0, separator), itemId: id.slice(separator + 1) }
|
|
33
|
+
: { callId: id };
|
|
34
|
+
if (!isProtocolToolCallId(identity.callId)) {
|
|
35
|
+
throw new HttpError(502, `Provider output tool call ID must be a non-empty string no longer than ${TOOL_CALL_ID_MAX_LENGTH} characters.`, "provider_output_invalid");
|
|
36
|
+
}
|
|
37
|
+
return identity;
|
|
38
|
+
}
|
|
39
|
+
function usesResponsesToolCallIds(api) {
|
|
40
|
+
return api === "openai-responses" || api === "azure-openai-responses";
|
|
41
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import type { ReasoningEffort } from "@mupt-ai/dari-router/policy-engine";
|
|
2
|
+
export type { ProviderIdentity, ProviderContinuationState, } from "@mupt-ai/dari-router/protocols";
|
|
3
|
+
import type { ProviderContinuationState, ProviderIdentity } from "@mupt-ai/dari-router/protocols";
|
|
4
|
+
export type SourceProtocol = "anthropic_messages" | "openai_chat" | "openai_responses";
|
|
5
|
+
export type AgentRequestClass = "main" | "subagent" | "workflow" | "auxiliary" | "compaction" | "side_reply" | "unknown";
|
|
6
|
+
export type AgentRequestContext = {
|
|
7
|
+
requestClass: AgentRequestClass;
|
|
8
|
+
agentId?: string;
|
|
9
|
+
parentAgentId?: string;
|
|
10
|
+
agentType?: string;
|
|
11
|
+
};
|
|
12
|
+
export declare function isBackgroundRequest(request: InteractionRequest): boolean;
|
|
13
|
+
export type InteractionRequestPurpose = "memory_extraction" | "tool_continuation";
|
|
14
|
+
export type InteractionResponseFormat = {
|
|
15
|
+
type: "text";
|
|
16
|
+
} | {
|
|
17
|
+
type: "json_object";
|
|
18
|
+
} | {
|
|
19
|
+
type: "json_schema";
|
|
20
|
+
name: string;
|
|
21
|
+
schema: Record<string, unknown>;
|
|
22
|
+
strict?: boolean;
|
|
23
|
+
description?: string;
|
|
24
|
+
};
|
|
25
|
+
export type InteractionText = {
|
|
26
|
+
type: "text";
|
|
27
|
+
text: string;
|
|
28
|
+
annotations?: Array<Record<string, unknown>>;
|
|
29
|
+
};
|
|
30
|
+
export type InteractionImage = {
|
|
31
|
+
type: "image";
|
|
32
|
+
url: string;
|
|
33
|
+
detail?: "auto" | "low" | "high" | "original";
|
|
34
|
+
};
|
|
35
|
+
export type InteractionToolReference = {
|
|
36
|
+
type: "tool_reference";
|
|
37
|
+
toolName: string;
|
|
38
|
+
};
|
|
39
|
+
export type InteractionContent = InteractionText | InteractionImage | InteractionToolReference;
|
|
40
|
+
export type InteractionMessage = {
|
|
41
|
+
type: "message";
|
|
42
|
+
id?: string;
|
|
43
|
+
clientTurnId?: string;
|
|
44
|
+
role: "system" | "developer" | "user" | "assistant";
|
|
45
|
+
content: InteractionContent[];
|
|
46
|
+
contentShape: "string" | "parts" | "null" | "absent";
|
|
47
|
+
phase?: "commentary" | "final_answer";
|
|
48
|
+
source?: ProviderIdentity;
|
|
49
|
+
};
|
|
50
|
+
export type InteractionReasoning = {
|
|
51
|
+
type: "reasoning";
|
|
52
|
+
id?: string;
|
|
53
|
+
summary: string[];
|
|
54
|
+
content: string[];
|
|
55
|
+
source?: ProviderIdentity;
|
|
56
|
+
continuation?: ProviderContinuationState;
|
|
57
|
+
};
|
|
58
|
+
export type ToolKind = "function" | "custom";
|
|
59
|
+
export type InteractionToolCall = {
|
|
60
|
+
type: "tool_call";
|
|
61
|
+
id?: string;
|
|
62
|
+
callId: string;
|
|
63
|
+
kind: ToolKind;
|
|
64
|
+
name: string;
|
|
65
|
+
namespace?: string;
|
|
66
|
+
input?: string | Record<string, unknown>;
|
|
67
|
+
status?: "in_progress" | "completed" | "incomplete";
|
|
68
|
+
};
|
|
69
|
+
export type InteractionToolResult = {
|
|
70
|
+
type: "tool_result";
|
|
71
|
+
id?: string;
|
|
72
|
+
callId: string;
|
|
73
|
+
kind: ToolKind;
|
|
74
|
+
name?: string;
|
|
75
|
+
output: string | InteractionContent[] | null;
|
|
76
|
+
outputShape: "string" | "parts" | "null" | "absent";
|
|
77
|
+
isError?: boolean;
|
|
78
|
+
loadedTools?: string[];
|
|
79
|
+
};
|
|
80
|
+
export type InteractionReasoningConfiguration = {
|
|
81
|
+
type: "reasoning_configuration";
|
|
82
|
+
effort: ReasoningEffort;
|
|
83
|
+
};
|
|
84
|
+
export type InteractionHostedToolCall = {
|
|
85
|
+
type: "hosted_tool_call";
|
|
86
|
+
id: string;
|
|
87
|
+
tool: "web_search" | "server_tool_use" | "tool_search";
|
|
88
|
+
providerType: "web_search_call" | "server_tool_use" | "tool_search_result";
|
|
89
|
+
status?: "in_progress" | "searching" | "completed" | "incomplete" | "failed";
|
|
90
|
+
payload: Record<string, unknown>;
|
|
91
|
+
source?: ProviderIdentity;
|
|
92
|
+
serverToolUseId?: string;
|
|
93
|
+
serverToolName?: string;
|
|
94
|
+
serverToolInput?: Record<string, unknown>;
|
|
95
|
+
serverToolResult?: InteractionToolReference[];
|
|
96
|
+
};
|
|
97
|
+
export type InteractionItem = InteractionMessage | InteractionReasoning | InteractionToolCall | InteractionToolResult | InteractionReasoningConfiguration | InteractionHostedToolCall;
|
|
98
|
+
export type InteractionOutputItem = InteractionMessage | InteractionReasoning | InteractionToolCall | InteractionHostedToolCall;
|
|
99
|
+
export type ServerToolKind = "bash_20250124" | "text_editor_20250124" | "tool_search_tool_20250919" | "tool_search_tool_regex_20250919" | "tool_search_tool_regex" | "tool_search_tool_regex_20251119" | "tool_search_tool_bm25" | "tool_search_tool_bm25_20251119";
|
|
100
|
+
export type ClientTool = {
|
|
101
|
+
kind: "client_tool";
|
|
102
|
+
name: string;
|
|
103
|
+
type: "bash_20250124" | "text_editor_20250124";
|
|
104
|
+
description?: string;
|
|
105
|
+
};
|
|
106
|
+
export type FunctionTool = {
|
|
107
|
+
kind: "function";
|
|
108
|
+
name: string;
|
|
109
|
+
namespace?: string;
|
|
110
|
+
description?: string;
|
|
111
|
+
parameters?: Record<string, unknown>;
|
|
112
|
+
strict?: boolean;
|
|
113
|
+
deferLoading?: boolean;
|
|
114
|
+
};
|
|
115
|
+
export type CustomTool = {
|
|
116
|
+
kind: "custom";
|
|
117
|
+
name: string;
|
|
118
|
+
namespace?: string;
|
|
119
|
+
description?: string;
|
|
120
|
+
format?: Record<string, unknown>;
|
|
121
|
+
};
|
|
122
|
+
export type HostedTool = {
|
|
123
|
+
kind: "hosted";
|
|
124
|
+
type: "web_search" | "web_search_preview";
|
|
125
|
+
config: Record<string, unknown>;
|
|
126
|
+
};
|
|
127
|
+
export type ServerTool = {
|
|
128
|
+
kind: "server_tool";
|
|
129
|
+
name: string;
|
|
130
|
+
type: ServerToolKind;
|
|
131
|
+
description?: string;
|
|
132
|
+
};
|
|
133
|
+
export type InteractionTool = FunctionTool | CustomTool | HostedTool | ServerTool | ClientTool;
|
|
134
|
+
export type InteractionToolChoice = "none" | "auto" | "required" | "any" | {
|
|
135
|
+
kind: ToolKind;
|
|
136
|
+
name: string;
|
|
137
|
+
namespace?: string;
|
|
138
|
+
} | {
|
|
139
|
+
kind: "hosted";
|
|
140
|
+
type: HostedTool["type"];
|
|
141
|
+
};
|
|
142
|
+
export type GenerationControls = {
|
|
143
|
+
temperature?: number;
|
|
144
|
+
topP?: number;
|
|
145
|
+
maxOutputTokens?: number;
|
|
146
|
+
stop?: string | string[];
|
|
147
|
+
};
|
|
148
|
+
export type ReasoningControls = {
|
|
149
|
+
effort?: ReasoningEffort;
|
|
150
|
+
baseEffort?: ReasoningEffort;
|
|
151
|
+
hasConfigurationUpdate?: boolean;
|
|
152
|
+
enabled?: boolean;
|
|
153
|
+
budgetTokens?: number;
|
|
154
|
+
};
|
|
155
|
+
export type AnthropicSafeguards = {
|
|
156
|
+
safeguards: unknown[];
|
|
157
|
+
betas: string[];
|
|
158
|
+
};
|
|
159
|
+
export type InteractionRequest = {
|
|
160
|
+
sourceProtocol: SourceProtocol;
|
|
161
|
+
requestPurpose?: InteractionRequestPurpose;
|
|
162
|
+
agentContext?: AgentRequestContext;
|
|
163
|
+
claudeCodeUserAgent?: string;
|
|
164
|
+
museCodeClientId?: string;
|
|
165
|
+
anthropicSafeguards?: AnthropicSafeguards;
|
|
166
|
+
model?: string;
|
|
167
|
+
instructions?: string;
|
|
168
|
+
items: InteractionItem[];
|
|
169
|
+
tools: InteractionTool[];
|
|
170
|
+
toolChoice?: InteractionToolChoice;
|
|
171
|
+
parallelToolCalls?: boolean;
|
|
172
|
+
generation: GenerationControls;
|
|
173
|
+
reasoning?: ReasoningControls;
|
|
174
|
+
responseFormat?: InteractionResponseFormat;
|
|
175
|
+
promptCacheKey?: string;
|
|
176
|
+
stream: boolean;
|
|
177
|
+
metadata?: Record<string, unknown>;
|
|
178
|
+
user?: string;
|
|
179
|
+
};
|
|
180
|
+
export type InteractionUsage = {
|
|
181
|
+
input: number;
|
|
182
|
+
output: number;
|
|
183
|
+
cacheRead: number;
|
|
184
|
+
cacheWrite: number;
|
|
185
|
+
totalTokens: number;
|
|
186
|
+
cost: {
|
|
187
|
+
input: number;
|
|
188
|
+
output: number;
|
|
189
|
+
cacheRead: number;
|
|
190
|
+
cacheWrite: number;
|
|
191
|
+
total: number;
|
|
192
|
+
};
|
|
193
|
+
costSource?: "provider";
|
|
194
|
+
};
|
|
195
|
+
export type InteractionOutput = {
|
|
196
|
+
responseId?: string;
|
|
197
|
+
createdAtMs: number;
|
|
198
|
+
source: ProviderIdentity;
|
|
199
|
+
items: InteractionOutputItem[];
|
|
200
|
+
usage: InteractionUsage;
|
|
201
|
+
stopReason: "stop" | "length" | "tool_use" | "error" | "aborted";
|
|
202
|
+
errorMessage?: string;
|
|
203
|
+
safeguardResults?: unknown[];
|
|
204
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type ReasoningEffort } from "@mupt-ai/dari-router/policy-engine";
|
|
2
|
+
export type CatalogModel = {
|
|
3
|
+
id: string;
|
|
4
|
+
provider: string;
|
|
5
|
+
native_model_id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
context_window: number | null;
|
|
8
|
+
max_output_tokens: number | null;
|
|
9
|
+
input_modalities: string[];
|
|
10
|
+
thinking_levels: ReasoningEffort[];
|
|
11
|
+
price: CatalogModelPrice | null;
|
|
12
|
+
price_as_of: string;
|
|
13
|
+
};
|
|
14
|
+
export type CatalogModelPrice = {
|
|
15
|
+
input: number;
|
|
16
|
+
output: number;
|
|
17
|
+
cached_input: number | null;
|
|
18
|
+
cache_write: number | null;
|
|
19
|
+
tiers: CatalogPriceTier[];
|
|
20
|
+
};
|
|
21
|
+
export type CatalogPriceTier = {
|
|
22
|
+
input_tokens_above: number;
|
|
23
|
+
input: number;
|
|
24
|
+
output: number;
|
|
25
|
+
cached_input: number | null;
|
|
26
|
+
cache_write: number | null;
|
|
27
|
+
};
|
|
28
|
+
export type ModelCatalogOptions = {
|
|
29
|
+
providers?: readonly string[] | undefined;
|
|
30
|
+
fetch?: ((url: string, init?: RequestInit) => Promise<Response>) | undefined;
|
|
31
|
+
signal?: AbortSignal | undefined;
|
|
32
|
+
};
|
|
33
|
+
export declare const DEFAULT_CATALOG_PROVIDERS: readonly string[];
|
|
34
|
+
/** List the agent-capable models of each requested provider. */
|
|
35
|
+
export declare function listModelCatalog(options?: ModelCatalogOptions): Promise<CatalogModel[]>;
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { getSupportedThinkingLevels } from "@mupt-ai/pi-ai";
|
|
2
|
+
import { getBuiltinModelDataGeneratedAt, getBuiltinModels, getBuiltinProviders, } from "@mupt-ai/pi-ai/providers/all";
|
|
3
|
+
import { REASONING_EFFORTS, isReasoningEffort, isRecord, } from "@mupt-ai/dari-router/policy-engine";
|
|
4
|
+
import { HttpError } from "../http/errors.js";
|
|
5
|
+
export const DEFAULT_CATALOG_PROVIDERS = [
|
|
6
|
+
"openai",
|
|
7
|
+
"anthropic",
|
|
8
|
+
"meta",
|
|
9
|
+
"xai",
|
|
10
|
+
"fireworks",
|
|
11
|
+
"openrouter",
|
|
12
|
+
"vercel-ai-gateway",
|
|
13
|
+
];
|
|
14
|
+
const LIVE_LISTINGS = {
|
|
15
|
+
openrouter: { url: "https://openrouter.ai/api/v1/models", models: openRouterModels },
|
|
16
|
+
"vercel-ai-gateway": { url: "https://ai-gateway.vercel.sh/v1/models", models: vercelModels },
|
|
17
|
+
};
|
|
18
|
+
/** List the agent-capable models of each requested provider. */
|
|
19
|
+
export async function listModelCatalog(options = {}) {
|
|
20
|
+
const providers = [...new Set(options.providers ?? DEFAULT_CATALOG_PROVIDERS)];
|
|
21
|
+
const builtin = new Set(getBuiltinProviders());
|
|
22
|
+
for (const provider of providers) {
|
|
23
|
+
if (LIVE_LISTINGS[provider] === undefined && !builtin.has(provider)) {
|
|
24
|
+
throw new HttpError(400, `Unknown catalog provider '${provider}'.`, "unknown_provider", "provider");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const lists = await Promise.all(providers.map(async (provider) => {
|
|
28
|
+
const listing = LIVE_LISTINGS[provider];
|
|
29
|
+
return listing === undefined
|
|
30
|
+
? piModels(provider)
|
|
31
|
+
: listing.models(await fetchListing(listing.url, options), new Date().toISOString());
|
|
32
|
+
}));
|
|
33
|
+
return lists.flat();
|
|
34
|
+
}
|
|
35
|
+
async function fetchListing(url, options) {
|
|
36
|
+
let status;
|
|
37
|
+
let body = null;
|
|
38
|
+
// Network errors, aborts, and unparseable bodies are upstream failures
|
|
39
|
+
// too: all of them surface as catalog_unavailable rather than a raw 500.
|
|
40
|
+
try {
|
|
41
|
+
const response = await (options.fetch ?? fetch)(url, {
|
|
42
|
+
headers: { accept: "application/json" },
|
|
43
|
+
...(options.signal ? { signal: options.signal } : {}),
|
|
44
|
+
});
|
|
45
|
+
status = response.status;
|
|
46
|
+
body = response.ok ? await response.json() : null;
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// Leaves body null.
|
|
50
|
+
}
|
|
51
|
+
if (!isRecord(body) || !Array.isArray(body.data)) {
|
|
52
|
+
const detail = status === undefined ? "could not be reached" : `returned no model list (HTTP ${status})`;
|
|
53
|
+
throw new HttpError(502, `${url} ${detail}.`, "catalog_unavailable");
|
|
54
|
+
}
|
|
55
|
+
return body.data;
|
|
56
|
+
}
|
|
57
|
+
function piModels(provider) {
|
|
58
|
+
const generatedAt = getBuiltinModelDataGeneratedAt();
|
|
59
|
+
const asOf = generatedAt === undefined ? "" : new Date(generatedAt).toISOString();
|
|
60
|
+
const models = getBuiltinModels(provider);
|
|
61
|
+
return models.map((model) => ({
|
|
62
|
+
id: `${provider}/${model.id}`,
|
|
63
|
+
provider,
|
|
64
|
+
native_model_id: model.id,
|
|
65
|
+
name: model.name,
|
|
66
|
+
context_window: model.contextWindow,
|
|
67
|
+
max_output_tokens: model.maxTokens,
|
|
68
|
+
input_modalities: [...model.input],
|
|
69
|
+
thinking_levels: orderedEfforts(getSupportedThinkingLevels(model)),
|
|
70
|
+
price: piPrice(model.cost),
|
|
71
|
+
price_as_of: asOf,
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
function piPrice(cost) {
|
|
75
|
+
return {
|
|
76
|
+
input: cost.input,
|
|
77
|
+
output: cost.output,
|
|
78
|
+
cached_input: cost.cacheRead,
|
|
79
|
+
cache_write: cost.cacheWrite,
|
|
80
|
+
tiers: (cost.tiers ?? []).map((tier) => ({
|
|
81
|
+
input_tokens_above: tier.inputTokensAbove,
|
|
82
|
+
input: tier.input,
|
|
83
|
+
output: tier.output,
|
|
84
|
+
cached_input: tier.cacheRead,
|
|
85
|
+
cache_write: tier.cacheWrite,
|
|
86
|
+
})),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
// https://openrouter.ai/docs/api/api-reference/models/get-models
|
|
90
|
+
function openRouterModels(data, asOf) {
|
|
91
|
+
return data.filter(isRecord).flatMap((model) => {
|
|
92
|
+
const architecture = isRecord(model.architecture) ? model.architecture : {};
|
|
93
|
+
const parameters = stringList(model.supported_parameters);
|
|
94
|
+
if (typeof model.id !== "string" || !parameters.includes("tools"))
|
|
95
|
+
return [];
|
|
96
|
+
if (!stringList(architecture.output_modalities).includes("text"))
|
|
97
|
+
return [];
|
|
98
|
+
const reasoning = isRecord(model.reasoning) ? model.reasoning : {};
|
|
99
|
+
const topProvider = isRecord(model.top_provider) ? model.top_provider : {};
|
|
100
|
+
return [{
|
|
101
|
+
id: `openrouter/${model.id}`,
|
|
102
|
+
provider: "openrouter",
|
|
103
|
+
native_model_id: model.id,
|
|
104
|
+
name: typeof model.name === "string" ? model.name : model.id,
|
|
105
|
+
context_window: positiveInteger(model.context_length),
|
|
106
|
+
max_output_tokens: positiveInteger(topProvider.max_completion_tokens),
|
|
107
|
+
input_modalities: stringList(architecture.input_modalities),
|
|
108
|
+
thinking_levels: gatewayThinkingLevels({
|
|
109
|
+
reasons: parameters.includes("reasoning"),
|
|
110
|
+
efforts: Array.isArray(reasoning.supported_efforts) ? stringList(reasoning.supported_efforts) : null,
|
|
111
|
+
canDisable: reasoning.mandatory !== true,
|
|
112
|
+
}),
|
|
113
|
+
price: isRecord(model.pricing) ? openRouterPrice(model.pricing) : null,
|
|
114
|
+
price_as_of: asOf,
|
|
115
|
+
}];
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
function openRouterPrice(pricing) {
|
|
119
|
+
const base = rates(pricing, "prompt", "completion");
|
|
120
|
+
if (base === null)
|
|
121
|
+
return null;
|
|
122
|
+
// Overrides restate only the rates they change; time-of-day overrides are
|
|
123
|
+
// not context tiers.
|
|
124
|
+
const tiers = (Array.isArray(pricing.overrides) ? pricing.overrides : [])
|
|
125
|
+
.filter(isRecord)
|
|
126
|
+
.flatMap((override) => {
|
|
127
|
+
const threshold = positiveInteger(override.min_prompt_tokens);
|
|
128
|
+
if (threshold === null || "utc_days" in override || "utc_start" in override || "utc_end" in override) {
|
|
129
|
+
return [];
|
|
130
|
+
}
|
|
131
|
+
const tier = rates({ ...pricing, ...override }, "prompt", "completion", override);
|
|
132
|
+
return tier === null ? [] : [{ input_tokens_above: threshold, ...tier }];
|
|
133
|
+
});
|
|
134
|
+
return { ...base, tiers };
|
|
135
|
+
}
|
|
136
|
+
// https://vercel.com/docs/ai-gateway/models-and-providers
|
|
137
|
+
function vercelModels(data, asOf) {
|
|
138
|
+
return data.filter(isRecord).flatMap((model) => {
|
|
139
|
+
const modalities = isRecord(model.modalities) ? model.modalities : {};
|
|
140
|
+
const parameters = stringList(model.supported_parameters);
|
|
141
|
+
if (typeof model.id !== "string" || model.type !== "language" || !parameters.includes("tools"))
|
|
142
|
+
return [];
|
|
143
|
+
if (!stringList(modalities.output).includes("text"))
|
|
144
|
+
return [];
|
|
145
|
+
const options = Array.isArray(model.reasoning_options) ? model.reasoning_options.filter(isRecord) : [];
|
|
146
|
+
const effort = options.find((option) => option.type === "effort");
|
|
147
|
+
return [{
|
|
148
|
+
id: `vercel-ai-gateway/${model.id}`,
|
|
149
|
+
provider: "vercel-ai-gateway",
|
|
150
|
+
native_model_id: model.id,
|
|
151
|
+
name: typeof model.name === "string" ? model.name : model.id,
|
|
152
|
+
context_window: positiveInteger(model.context_window),
|
|
153
|
+
max_output_tokens: positiveInteger(model.max_tokens),
|
|
154
|
+
input_modalities: stringList(modalities.input),
|
|
155
|
+
thinking_levels: gatewayThinkingLevels({
|
|
156
|
+
reasons: parameters.includes("reasoning") || options.length > 0,
|
|
157
|
+
efforts: effort === undefined ? null : stringList(effort.values),
|
|
158
|
+
canDisable: options.length === 0 || options.some((option) => option.type === "toggle"),
|
|
159
|
+
}),
|
|
160
|
+
price: isRecord(model.pricing) ? vercelPrice(model.pricing) : null,
|
|
161
|
+
price_as_of: asOf,
|
|
162
|
+
}];
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
function vercelPrice(pricing) {
|
|
166
|
+
const base = rates(pricing, "input", "output");
|
|
167
|
+
if (base === null)
|
|
168
|
+
return null;
|
|
169
|
+
// Each rate lists its own {cost, min, max} ranges; a range covers prompts of
|
|
170
|
+
// at least `min` tokens, so it is the tier above `min - 1`.
|
|
171
|
+
const ranged = new Map();
|
|
172
|
+
for (const key of ["input", "output", "input_cache_read", "input_cache_write"]) {
|
|
173
|
+
const ranges = Array.isArray(pricing[`${key}_tiers`]) ? pricing[`${key}_tiers`] : [];
|
|
174
|
+
for (const range of ranges.filter(isRecord)) {
|
|
175
|
+
const start = positiveInteger(range.min);
|
|
176
|
+
if (start === null || range.cost == null)
|
|
177
|
+
continue;
|
|
178
|
+
ranged.set(start - 1, { ...ranged.get(start - 1), [key]: range.cost });
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const tiers = [...ranged.entries()]
|
|
182
|
+
.sort(([left], [right]) => left - right)
|
|
183
|
+
.flatMap(([threshold, stated]) => {
|
|
184
|
+
const tier = rates({ ...pricing, ...stated }, "input", "output", stated);
|
|
185
|
+
return tier === null ? [] : [{ input_tokens_above: threshold, ...tier }];
|
|
186
|
+
});
|
|
187
|
+
return { ...base, tiers };
|
|
188
|
+
}
|
|
189
|
+
// Base rates from per-token strings. A tier inherits input and output from
|
|
190
|
+
// the base price but states its own cache rates, or has none.
|
|
191
|
+
function rates(pricing, inputKey, outputKey, cacheRates = pricing) {
|
|
192
|
+
const input = perMillion(pricing[inputKey]);
|
|
193
|
+
const output = perMillion(pricing[outputKey]);
|
|
194
|
+
// Meta-routers with variable prices list negative rates.
|
|
195
|
+
if (input === null || output === null)
|
|
196
|
+
return null;
|
|
197
|
+
return {
|
|
198
|
+
input,
|
|
199
|
+
output,
|
|
200
|
+
cached_input: perMillion(cacheRates.input_cache_read),
|
|
201
|
+
cache_write: perMillion(cacheRates.input_cache_write),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
// USD per token as a decimal string -> USD per million tokens. Shifting the
|
|
205
|
+
// exponent in the string keeps "0.000001" exactly 1.
|
|
206
|
+
function perMillion(value) {
|
|
207
|
+
const rate = typeof value === "string" ? Number(`${value.trim()}e6`)
|
|
208
|
+
: typeof value === "number" ? value * 1_000_000
|
|
209
|
+
: Number.NaN;
|
|
210
|
+
return Number.isFinite(rate) && rate >= 0 ? rate : null;
|
|
211
|
+
}
|
|
212
|
+
// Gateways forward `reasoning.effort`; models that reason without a stated
|
|
213
|
+
// effort list take the standard low/medium/high levels.
|
|
214
|
+
function gatewayThinkingLevels(args) {
|
|
215
|
+
if (!args.reasons)
|
|
216
|
+
return ["off"];
|
|
217
|
+
const efforts = args.efforts ?? ["low", "medium", "high"];
|
|
218
|
+
return orderedEfforts([
|
|
219
|
+
...efforts.map((effort) => effort === "none" ? "off" : effort),
|
|
220
|
+
...(args.canDisable ? ["off"] : []),
|
|
221
|
+
]);
|
|
222
|
+
}
|
|
223
|
+
function orderedEfforts(levels) {
|
|
224
|
+
const accepted = new Set(levels.filter(isReasoningEffort));
|
|
225
|
+
return REASONING_EFFORTS.filter((effort) => accepted.has(effort));
|
|
226
|
+
}
|
|
227
|
+
function stringList(value) {
|
|
228
|
+
return Array.isArray(value) ? value.filter((item) => typeof item === "string") : [];
|
|
229
|
+
}
|
|
230
|
+
function positiveInteger(value) {
|
|
231
|
+
return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : null;
|
|
232
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export function effectiveContextWindow(config, allowLongContext = false) {
|
|
2
|
+
const window = config?.context_window;
|
|
3
|
+
const limit = config?.standard_context_limit;
|
|
4
|
+
if (allowLongContext || limit == null)
|
|
5
|
+
return window ?? null;
|
|
6
|
+
return Math.min(window ?? limit, limit);
|
|
7
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ProviderKeySource, ResolvedRouter } from "../types.js";
|
|
2
|
+
/** Actual serving provider for a canonical model on a resolved managed router. */
|
|
3
|
+
export declare function executionProviderForModel(credentials: ResolvedRouter, model: string): string;
|
|
4
|
+
/**
|
|
5
|
+
* The key that serves a model on a resolved router: a platform-supplied
|
|
6
|
+
* credential first, then the resolved provider key. Missing declarations are
|
|
7
|
+
* user-owned so unknown platform data cannot trigger managed-key billing.
|
|
8
|
+
*/
|
|
9
|
+
export declare function keySourceForModel(credentials: ResolvedRouter, model: string): ProviderKeySource;
|