@mupt-ai/dari-gateway 0.2.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/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 +21 -6
- 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 +1 -1
- 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 +8 -8
- package/src/http/serve.ts +3 -3
- package/src/index.ts +6 -6
- 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 +9 -9
- 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/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 +4 -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 +14 -14
- package/src/providers/pi_context.ts +6 -6
- package/src/providers/pi_options.ts +10 -10
- package/src/providers/pi_output.ts +5 -5
- package/src/providers/provider_attempt_runner.ts +1 -1
- package/src/providers/provider_call.ts +14 -14
- 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 +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 +4 -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 +13 -13
- 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/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 +6 -6
- 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 +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
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import { getSupportedThinkingLevels, normalizeContext, } from "@mupt-ai/pi-ai";
|
|
2
|
+
import { anthropicMessagesApi } from "@mupt-ai/pi-ai/api/anthropic-messages.lazy";
|
|
3
|
+
import { azureOpenAIResponsesApi } from "@mupt-ai/pi-ai/api/azure-openai-responses.lazy";
|
|
4
|
+
import { bedrockConverseStreamApi } from "@mupt-ai/pi-ai/api/bedrock-converse-stream.lazy";
|
|
5
|
+
import { googleGenerativeAIApi } from "@mupt-ai/pi-ai/api/google-generative-ai.lazy";
|
|
6
|
+
import { googleVertexApi } from "@mupt-ai/pi-ai/api/google-vertex.lazy";
|
|
7
|
+
import { mistralConversationsApi } from "@mupt-ai/pi-ai/api/mistral-conversations.lazy";
|
|
8
|
+
import { openAICodexResponsesApi } from "@mupt-ai/pi-ai/api/openai-codex-responses.lazy";
|
|
9
|
+
import { openAICompletionsApi } from "@mupt-ai/pi-ai/api/openai-completions.lazy";
|
|
10
|
+
import { openAIResponsesApi } from "@mupt-ai/pi-ai/api/openai-responses.lazy";
|
|
11
|
+
import { getBuiltinModel } from "@mupt-ai/pi-ai/providers/all";
|
|
12
|
+
import { REASONING_EFFORTS, isRecord, nativeModelId, providerForModel, } from "@mupt-ai/dari-router/policy-engine";
|
|
13
|
+
import { HttpError } from "../http/errors.js";
|
|
14
|
+
import { effectiveContextWindow } from "../models/context_limits.js";
|
|
15
|
+
import { defaultBaseUrl } from "../models/provider_defaults.js";
|
|
16
|
+
import { contextWindowOverride, inputModalitiesOverride, runtimeCompat } from "../models/runtime_overrides.js";
|
|
17
|
+
import { BEDROCK_API } from "./model_capabilities.js";
|
|
18
|
+
import { buildContext } from "./pi_context.js";
|
|
19
|
+
import { providerIdentityFromModel } from "../interaction/provider_continuation_state.js";
|
|
20
|
+
import { buildOptions, supportedThinkingLevels } from "./pi_options.js";
|
|
21
|
+
import { eventsWithSafeguardResults, forwardsAnthropicSafeguards, messageWithSafeguardResults, safeguardResultsTap, } from "./anthropic_safeguards.js";
|
|
22
|
+
import { eventsWithProviderReportedCost, messageWithProviderReportedCost, providerReportedCostTap, reportsProviderCost, } from "./provider_reported_cost.js";
|
|
23
|
+
import { eventsWithSubscriptionCacheWrites, messageWithSubscriptionCacheWrites, modelPricingFromPiModel, } from "./subscription_usage.js";
|
|
24
|
+
// Pi's built-in providers stream through these API implementations unchanged.
|
|
25
|
+
// Dari always passes resolved credentials, so it dispatches on the API rather
|
|
26
|
+
// than through a Models collection with its own auth resolution.
|
|
27
|
+
const PI_APIS = {
|
|
28
|
+
"anthropic-messages": anthropicMessagesApi(),
|
|
29
|
+
"azure-openai-responses": azureOpenAIResponsesApi(),
|
|
30
|
+
"bedrock-converse-stream": bedrockConverseStreamApi(),
|
|
31
|
+
"google-generative-ai": googleGenerativeAIApi(),
|
|
32
|
+
"google-vertex": googleVertexApi(),
|
|
33
|
+
"mistral-conversations": mistralConversationsApi(),
|
|
34
|
+
"openai-codex-responses": openAICodexResponsesApi(),
|
|
35
|
+
"openai-completions": openAICompletionsApi(),
|
|
36
|
+
"openai-responses": openAIResponsesApi(),
|
|
37
|
+
};
|
|
38
|
+
function piStreamSimple(model, context, options) {
|
|
39
|
+
const api = PI_APIS[String(model.api)];
|
|
40
|
+
if (!api)
|
|
41
|
+
throw new Error(`No Pi API implementation for ${String(model.api)}.`);
|
|
42
|
+
return api.streamSimple(model, normalizeContext(context), options);
|
|
43
|
+
}
|
|
44
|
+
export const pi = {
|
|
45
|
+
completeSimple: async (model, context, options) => await piStreamSimple(model, context, options).result(),
|
|
46
|
+
streamSimple: (model, context, options) => piStreamSimple(model, context, options),
|
|
47
|
+
getModel: (provider, id) => getBuiltinModel(provider, id),
|
|
48
|
+
getSupportedThinkingLevels: (model) => getSupportedThinkingLevels(model),
|
|
49
|
+
};
|
|
50
|
+
export async function completeInteractionModel(args) {
|
|
51
|
+
const model = modelAtEndpoint(buildModel(pi, args.model, args.pricing, args.config), args.endpoint);
|
|
52
|
+
const source = providerIdentityFromModel(model);
|
|
53
|
+
const context = buildContext(args.request, source);
|
|
54
|
+
const costTap = reportsProviderCost(model) ? providerReportedCostTap(args.fetch) : undefined;
|
|
55
|
+
const safeguards = forwardsAnthropicSafeguards(args.request, model)
|
|
56
|
+
? safeguardResultsTap(costTap?.fetch ?? args.fetch)
|
|
57
|
+
: undefined;
|
|
58
|
+
const options = buildOptions(args.request, args.auth ?? args.apiKey, args.model, model, pi, args.sessionId, args.selectedReasoningEffort, args.timeoutMs, args.signal, safeguards?.fetch ?? costTap?.fetch ?? args.fetch);
|
|
59
|
+
const usageContext = subscriptionUsageContext(args.model, model, source);
|
|
60
|
+
const message = options.emitProviderEvents === true
|
|
61
|
+
? await completeProviderEventStream(pi.streamSimple(model, context, options))
|
|
62
|
+
: await pi.completeSimple(model, context, options);
|
|
63
|
+
return {
|
|
64
|
+
message: messageWithProviderReportedCost(messageWithSubscriptionCacheWrites(messageWithSafeguardResults(message, safeguards?.results()), usageContext), costTap?.cost()),
|
|
65
|
+
source,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function subscriptionUsageContext(modelId, model, source) {
|
|
69
|
+
// pi-ai priced this call from model.cost, so the repricing must read it back
|
|
70
|
+
// rather than the caller's catalog pricing, which fast mode can override.
|
|
71
|
+
return { api: source.api, model: modelId, pricing: modelPricingFromPiModel(model) };
|
|
72
|
+
}
|
|
73
|
+
export async function completeProviderEventStream(events) {
|
|
74
|
+
let providerResponse;
|
|
75
|
+
const providerOutput = new Map();
|
|
76
|
+
for await (const event of events) {
|
|
77
|
+
if (event.type === "provider_event" && isRecord(event.event)) {
|
|
78
|
+
const raw = event.event;
|
|
79
|
+
if (raw.type === "response.output_item.done" && isRecord(raw.item)) {
|
|
80
|
+
const outputIndex = raw.output_index;
|
|
81
|
+
if (typeof outputIndex !== "number" || !Number.isInteger(outputIndex) || outputIndex < 0) {
|
|
82
|
+
throw new Error("Provider output item is missing a valid output index.");
|
|
83
|
+
}
|
|
84
|
+
providerOutput.set(outputIndex, raw.item);
|
|
85
|
+
}
|
|
86
|
+
if ((raw.type === "response.completed" || raw.type === "response.incomplete") &&
|
|
87
|
+
isRecord(raw.response)) {
|
|
88
|
+
providerResponse = raw.response;
|
|
89
|
+
}
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (event.type === "done" && isRecord(event.message)) {
|
|
93
|
+
if (providerResponse) {
|
|
94
|
+
const terminalOutput = providerResponse.output;
|
|
95
|
+
if ((!Array.isArray(terminalOutput) || terminalOutput.length === 0) && providerOutput.size > 0) {
|
|
96
|
+
const entries = [...providerOutput.entries()].sort(([left], [right]) => left - right);
|
|
97
|
+
if (entries.some(([index], position) => index !== position)) {
|
|
98
|
+
throw new Error("Provider stream ended with non-contiguous output item indexes.");
|
|
99
|
+
}
|
|
100
|
+
providerResponse = { ...providerResponse, output: entries.map(([, item]) => item) };
|
|
101
|
+
}
|
|
102
|
+
return { ...event.message, providerResponse };
|
|
103
|
+
}
|
|
104
|
+
return event.message;
|
|
105
|
+
}
|
|
106
|
+
if (event.type === "error" && isRecord(event.error))
|
|
107
|
+
return event.error;
|
|
108
|
+
}
|
|
109
|
+
throw new Error("Provider stream ended without a terminal event.");
|
|
110
|
+
}
|
|
111
|
+
export async function streamInteractionModel(args) {
|
|
112
|
+
const model = modelAtEndpoint(buildModel(pi, args.model, args.pricing, args.config), args.endpoint);
|
|
113
|
+
const source = providerIdentityFromModel(model);
|
|
114
|
+
const costTap = reportsProviderCost(model) ? providerReportedCostTap(args.fetch) : undefined;
|
|
115
|
+
const safeguards = forwardsAnthropicSafeguards(args.request, model)
|
|
116
|
+
? safeguardResultsTap(costTap?.fetch ?? args.fetch)
|
|
117
|
+
: undefined;
|
|
118
|
+
const events = pi.streamSimple(model, buildContext(args.request, source), buildOptions(args.request, args.auth ?? args.apiKey, args.model, model, pi, args.sessionId, args.selectedReasoningEffort, undefined, args.signal, safeguards?.fetch ?? costTap?.fetch ?? args.fetch));
|
|
119
|
+
const withCacheWrites = eventsWithSubscriptionCacheWrites(safeguards ? eventsWithSafeguardResults(events, safeguards) : events, subscriptionUsageContext(args.model, model, source));
|
|
120
|
+
return {
|
|
121
|
+
events: costTap ? eventsWithProviderReportedCost(withCacheWrites, costTap) : withCacheWrites,
|
|
122
|
+
source,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function modelAtEndpoint(model, endpoint) {
|
|
126
|
+
if (endpoint === undefined)
|
|
127
|
+
return model;
|
|
128
|
+
return {
|
|
129
|
+
...model,
|
|
130
|
+
...(endpoint.baseUrl ? { baseUrl: endpoint.baseUrl } : {}),
|
|
131
|
+
...(endpoint.headers
|
|
132
|
+
? { headers: { ...(isRecord(model.headers) ? model.headers : {}), ...endpoint.headers } }
|
|
133
|
+
: {}),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
export function modelContextWindow(modelId) {
|
|
137
|
+
const provider = providerForModel(modelId);
|
|
138
|
+
const nativeId = nativeModelId(modelId);
|
|
139
|
+
const registered = resolveModelMetadata(pi, provider, modelId, nativeId);
|
|
140
|
+
return contextWindowForModel(modelId, registered);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Pi's catalog price for a canonical model served by `provider`, in USD per
|
|
144
|
+
* million tokens. Null when Pi does not list the model.
|
|
145
|
+
*/
|
|
146
|
+
export function registeredModelPrice(modelId, provider = providerForModel(modelId)) {
|
|
147
|
+
const registered = resolveModelMetadata(pi, provider, modelId, nativeModelId(modelId));
|
|
148
|
+
const cost = registered?.cost;
|
|
149
|
+
if (!isRecord(cost) || typeof cost.input !== "number" || typeof cost.output !== "number") {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
input: cost.input,
|
|
154
|
+
output: cost.output,
|
|
155
|
+
cached_input: typeof cost.cacheRead === "number" ? cost.cacheRead : null,
|
|
156
|
+
cache_write: typeof cost.cacheWrite === "number" ? cost.cacheWrite : null,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
export function modelMetadataLookup(configs) {
|
|
160
|
+
return (modelId, pricing) => {
|
|
161
|
+
const model = buildModel(pi, modelId, pricing, configs?.[modelId]);
|
|
162
|
+
return {
|
|
163
|
+
model,
|
|
164
|
+
supportedThinkingLevels: supportedThinkingLevels(model, pi),
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
export function buildModel(pi, modelId, pricing, config) {
|
|
169
|
+
const provider = config?.provider ?? providerForModel(modelId);
|
|
170
|
+
const nativeId = config?.native_model_id ?? nativeModelId(modelId);
|
|
171
|
+
const registered = resolveModelMetadata(pi, provider, modelId, nativeId);
|
|
172
|
+
const id = registered?.id ?? (provider === "anthropic" ? nativeId.replace(/(?<=\d)\.(?=\d)/g, "-") : nativeId);
|
|
173
|
+
const price = executionPricing(pricing, config);
|
|
174
|
+
const configuredThinkingLevels = config?.supported_thinking_levels;
|
|
175
|
+
// A null catalog value delegates to Pi's registry when it knows the model.
|
|
176
|
+
// Custom OpenAI-compatible models are not necessarily registered, so keep
|
|
177
|
+
// the public effort set permissive and let the provider validate the wire
|
|
178
|
+
// value instead of silently treating an unknown model as off-only.
|
|
179
|
+
const fallbackThinkingLevels = config !== undefined && configuredThinkingLevels == null && registered === undefined
|
|
180
|
+
? REASONING_EFFORTS
|
|
181
|
+
: undefined;
|
|
182
|
+
const effectiveThinkingLevels = Array.isArray(configuredThinkingLevels)
|
|
183
|
+
? configuredThinkingLevels
|
|
184
|
+
: fallbackThinkingLevels;
|
|
185
|
+
const configuredThinking = effectiveThinkingLevels
|
|
186
|
+
? thinkingMetadata(effectiveThinkingLevels, registered)
|
|
187
|
+
: null;
|
|
188
|
+
const defaultThinkingLevel = config?.default_thinking_level ?? undefined;
|
|
189
|
+
const api = config?.api ?? registered?.api ?? defaultApi(provider);
|
|
190
|
+
const baseUrl = config?.base_url ?? registered?.baseUrl ?? defaultBaseUrl(provider);
|
|
191
|
+
if (!baseUrl) {
|
|
192
|
+
throw new HttpError(502, `No base URL is configured for provider ${provider}.`, "provider_configuration_error");
|
|
193
|
+
}
|
|
194
|
+
const contextWindow = contextWindowForModel(modelId, registered, config) ?? 128000;
|
|
195
|
+
const compat = responsesToolContract(api, runtimeCompat(provider, registered, modelId));
|
|
196
|
+
return {
|
|
197
|
+
...(registered ?? {}),
|
|
198
|
+
...(compat !== undefined ? { compat } : {}),
|
|
199
|
+
id,
|
|
200
|
+
name: registered?.name ?? id,
|
|
201
|
+
provider,
|
|
202
|
+
api,
|
|
203
|
+
baseUrl,
|
|
204
|
+
reasoning: configuredThinking?.reasoning ?? registered?.reasoning ?? false,
|
|
205
|
+
input: registered?.input ?? inputModalitiesOverride(modelId) ?? ["text"],
|
|
206
|
+
cost: price ?? registered?.cost ?? { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
207
|
+
contextWindow,
|
|
208
|
+
maxTokens: config?.max_output_tokens ?? registered?.maxTokens ?? contextWindow,
|
|
209
|
+
...(defaultThinkingLevel !== undefined ? { defaultThinkingLevel } : {}),
|
|
210
|
+
...(configuredThinking ? { thinkingLevelMap: configuredThinking.thinkingLevelMap } : {}),
|
|
211
|
+
...(effectiveThinkingLevels
|
|
212
|
+
? { supportedThinkingLevels: [...effectiveThinkingLevels] }
|
|
213
|
+
: {}),
|
|
214
|
+
fastMode: config?.fast_mode ?? false,
|
|
215
|
+
zeroDataRetention: config?.zdr ?? false,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
function thinkingMetadata(supportedLevels, registered) {
|
|
219
|
+
const supported = new Set(supportedLevels);
|
|
220
|
+
const registeredMap = isRecord(registered?.thinkingLevelMap)
|
|
221
|
+
? registered.thinkingLevelMap
|
|
222
|
+
: {};
|
|
223
|
+
const thinkingLevelMap = {};
|
|
224
|
+
for (const level of REASONING_EFFORTS) {
|
|
225
|
+
if (!supported.has(level)) {
|
|
226
|
+
thinkingLevelMap[level] = null;
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
const registeredValue = registeredMap[level];
|
|
230
|
+
if (typeof registeredValue === "string") {
|
|
231
|
+
thinkingLevelMap[level] = registeredValue;
|
|
232
|
+
}
|
|
233
|
+
else if (level === "xhigh" || level === "max") {
|
|
234
|
+
// Pi requires extended levels to be explicitly mapped. The catalog
|
|
235
|
+
// levels use the provider-native OpenAI spelling.
|
|
236
|
+
thinkingLevelMap[level] = level;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return {
|
|
240
|
+
reasoning: supportedLevels.some((level) => level !== "off"),
|
|
241
|
+
thinkingLevelMap,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
function resolveModelMetadata(pi, provider, modelId, nativeId) {
|
|
245
|
+
const registry = registryProvider(provider, modelId);
|
|
246
|
+
return pi.getModel?.(registry, nativeId) ?? pi.getModel?.(registry, modelId);
|
|
247
|
+
}
|
|
248
|
+
// Dari's "azure" provider has no Pi registry of its own. One Foundry resource
|
|
249
|
+
// serves OpenAI deployments through Pi's Azure OpenAI Responses registry and
|
|
250
|
+
// Claude deployments through the Anthropic registry, whose model metadata
|
|
251
|
+
// (input modalities, adaptive-thinking compat, thinking level maps) is the
|
|
252
|
+
// same on Foundry; the resolved config still owns api, base URL, and cost.
|
|
253
|
+
function registryProvider(provider, modelId) {
|
|
254
|
+
if (provider !== "azure")
|
|
255
|
+
return provider;
|
|
256
|
+
return providerForModel(modelId) === "anthropic" ? "anthropic" : "azure-openai-responses";
|
|
257
|
+
}
|
|
258
|
+
function contextWindowForModel(modelId, registered, config) {
|
|
259
|
+
const registeredWindow = typeof registered?.contextWindow === "number" ? registered.contextWindow : null;
|
|
260
|
+
const configWindow = effectiveContextWindow(config, config?.allow_long_context === true);
|
|
261
|
+
const override = contextWindowOverride(modelId);
|
|
262
|
+
const configured = configWindow ?? registeredWindow;
|
|
263
|
+
if (configWindow !== null || override === undefined)
|
|
264
|
+
return configured;
|
|
265
|
+
return Math.max(configured ?? 0, override);
|
|
266
|
+
}
|
|
267
|
+
// Pi 1.4 sends Responses tool definitions as conversation items, not the request's
|
|
268
|
+
// `tools` field, when a model supports mid-conversation system messages. The gateway
|
|
269
|
+
// keeps tools at the request level until that native tool-change path is validated.
|
|
270
|
+
const RESPONSES_APIS = new Set(["openai-responses", "azure-openai-responses", "openai-codex-responses"]);
|
|
271
|
+
function responsesToolContract(api, compat) {
|
|
272
|
+
if (typeof api !== "string" || !RESPONSES_APIS.has(api))
|
|
273
|
+
return compat;
|
|
274
|
+
if (!isRecord(compat) || compat.supportsMidConvoSystemMessages !== true)
|
|
275
|
+
return compat;
|
|
276
|
+
return { ...compat, supportsMidConvoSystemMessages: false };
|
|
277
|
+
}
|
|
278
|
+
function modelPricingFromConfig(config) {
|
|
279
|
+
const configuredCost = config?.fast_mode && config.provider === "anthropic" && config.fast_mode_cost
|
|
280
|
+
? config.fast_mode_cost
|
|
281
|
+
: config?.cost;
|
|
282
|
+
if (!configuredCost || typeof configuredCost.input !== "number" || typeof configuredCost.output !== "number")
|
|
283
|
+
return null;
|
|
284
|
+
return {
|
|
285
|
+
input: configuredCost.input,
|
|
286
|
+
output: configuredCost.output,
|
|
287
|
+
cacheRead: configuredCost.cached_input ?? configuredCost.input,
|
|
288
|
+
cacheWrite: configuredCost.cache_write ?? configuredCost.input,
|
|
289
|
+
...(configuredCost.tiers?.length ? { tiers: configuredCost.tiers.map((tier) => ({
|
|
290
|
+
inputTokensAbove: tier.input_tokens_above,
|
|
291
|
+
input: tier.input,
|
|
292
|
+
output: tier.output,
|
|
293
|
+
cacheRead: tier.cached_input ?? tier.input,
|
|
294
|
+
cacheWrite: tier.cache_write ?? tier.input,
|
|
295
|
+
})) } : {}),
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
function executionPricing(pricing, config) {
|
|
299
|
+
if (config?.fast_mode &&
|
|
300
|
+
config.provider === "openai" &&
|
|
301
|
+
config.api === "openai-responses") {
|
|
302
|
+
// The platform publishes fast prices for routing decisions and the UI,
|
|
303
|
+
// while Pi multiplies the standard rates using the service tier actually
|
|
304
|
+
// returned by OpenAI. Feeding Pi premium rates here would charge twice.
|
|
305
|
+
return modelPricingFromConfig({ ...config, fast_mode: false });
|
|
306
|
+
}
|
|
307
|
+
return pricing ?? modelPricingFromConfig(config);
|
|
308
|
+
}
|
|
309
|
+
function defaultApi(provider) {
|
|
310
|
+
if (provider === "openai" || provider === "xai")
|
|
311
|
+
return "openai-responses";
|
|
312
|
+
if (provider === "anthropic")
|
|
313
|
+
return "anthropic-messages";
|
|
314
|
+
if (provider === "amazon-bedrock")
|
|
315
|
+
return BEDROCK_API;
|
|
316
|
+
if (provider === "azure")
|
|
317
|
+
return "azure-openai-responses";
|
|
318
|
+
return "openai-completions";
|
|
319
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { InteractionRequest, ProviderIdentity } from "../interaction/types.js";
|
|
2
|
+
export type PiContext = Record<string, unknown> & {
|
|
3
|
+
messages: Array<Record<string, unknown>>;
|
|
4
|
+
systemPrompt?: string;
|
|
5
|
+
tools?: Array<Record<string, unknown>>;
|
|
6
|
+
};
|
|
7
|
+
export declare function buildContext(request: InteractionRequest, target: ProviderIdentity): PiContext;
|