@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,43 @@
|
|
|
1
|
+
/* The router exposes one logical model, `dari/routing`. Some client protocols
|
|
2
|
+
need protocol-specific synonyms for it; each synonym set lives here beside
|
|
3
|
+
the canonical id, scoped by protocol, so a future set for another protocol
|
|
4
|
+
(e.g. Codex) slots in alongside rather than widening a shared one.
|
|
5
|
+
|
|
6
|
+
Anthropic: Claude Desktop / Cowork only mark a discovered model "usable"
|
|
7
|
+
when its id maps to a Claude family tier, so the Anthropic-facing surfaces
|
|
8
|
+
accept bare tier names. The alias carries no routing meaning — the selector
|
|
9
|
+
decides the executed model — it only keeps every tier a harness can emit
|
|
10
|
+
(fable main threads, opus subagents, haiku background work) funneling into
|
|
11
|
+
the router instead of leaking to a direct provider call. */
|
|
12
|
+
export const CANONICAL_ROUTER_MODEL = "dari/routing";
|
|
13
|
+
/* Full anthropicFamilyTier enum per Anthropic's third-party gateway docs,
|
|
14
|
+
ordered most to least capable; discovery advertises them in this order. */
|
|
15
|
+
export const ANTHROPIC_ROUTER_MODEL_TIERS = ["mythos", "opus", "fable", "sonnet", "haiku"];
|
|
16
|
+
const ANTHROPIC_ROUTER_MODELS = new Set([CANONICAL_ROUTER_MODEL, ...ANTHROPIC_ROUTER_MODEL_TIERS]);
|
|
17
|
+
/* Claude Code's subagent bootstrap resolves tiers to concrete ids before the
|
|
18
|
+
request leaves the client (wire-captured live: Task spawns emit
|
|
19
|
+
`claude-opus-5` even when the session model is dari/routing), so concrete
|
|
20
|
+
Claude-family ids are synonyms too — both bare (`claude-opus-5`) and
|
|
21
|
+
provider-prefixed (`anthropic/claude-opus-5`) forms. */
|
|
22
|
+
const ANTHROPIC_MODEL_ID_PATTERN = /^(anthropic\/)?claude-[a-z0-9][a-z0-9.-]*$/i;
|
|
23
|
+
export function isAnthropicRouterModel(model) {
|
|
24
|
+
return ANTHROPIC_ROUTER_MODELS.has(model) || ANTHROPIC_MODEL_ID_PATTERN.test(model);
|
|
25
|
+
}
|
|
26
|
+
/* An explicit model, `dari/model/<catalog id>`, serves exactly that enabled
|
|
27
|
+
model without selection, so a client's model picker can switch models. It
|
|
28
|
+
is a separate namespace so the synonyms above, which Claude Code subagents
|
|
29
|
+
emit, keep routing. */
|
|
30
|
+
export const EXPLICIT_MODEL_PREFIX = "dari/model/";
|
|
31
|
+
export function explicitModelId(model) {
|
|
32
|
+
if (!model?.startsWith(EXPLICIT_MODEL_PREFIX))
|
|
33
|
+
return null;
|
|
34
|
+
const id = model.slice(EXPLICIT_MODEL_PREFIX.length);
|
|
35
|
+
return id.length > 0 ? id : null;
|
|
36
|
+
}
|
|
37
|
+
export function explicitModelSlug(modelId) {
|
|
38
|
+
return EXPLICIT_MODEL_PREFIX + modelId;
|
|
39
|
+
}
|
|
40
|
+
const UNSUPPORTED_MODEL_MESSAGE = `Only model '${CANONICAL_ROUTER_MODEL}' or '${EXPLICIT_MODEL_PREFIX}<model>' is supported.`;
|
|
41
|
+
export function unsupportedModelMessage() {
|
|
42
|
+
return UNSUPPORTED_MODEL_MESSAGE;
|
|
43
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { RoutingDecision } from "@mupt-ai/dari-router/policy-engine";
|
|
2
|
+
import type { InteractionOutput, InteractionRequest } from "../../interaction/types.js";
|
|
3
|
+
export declare function anthropicMessagesResponse(args: {
|
|
4
|
+
id: string;
|
|
5
|
+
model: string;
|
|
6
|
+
output: InteractionOutput;
|
|
7
|
+
request: InteractionRequest;
|
|
8
|
+
routing: RoutingDecision;
|
|
9
|
+
conversationId?: string;
|
|
10
|
+
}): Record<string, unknown>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { anthropicContent, anthropicMessageId, anthropicRouting, anthropicStopReason, anthropicUsage, } from "./anthropic_messages_output.js";
|
|
2
|
+
export function anthropicMessagesResponse(args) {
|
|
3
|
+
return {
|
|
4
|
+
id: anthropicMessageId(args.id),
|
|
5
|
+
type: "message",
|
|
6
|
+
role: "assistant",
|
|
7
|
+
model: args.model,
|
|
8
|
+
content: anthropicContent(args.output),
|
|
9
|
+
stop_reason: anthropicStopReason(args.output),
|
|
10
|
+
stop_sequence: null,
|
|
11
|
+
usage: anthropicUsage(args.output),
|
|
12
|
+
...(args.output.safeguardResults ? { safeguard_results: args.output.safeguardResults } : {}),
|
|
13
|
+
dari_routing: anthropicRouting(args.routing, args.id, args.conversationId),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { InteractionOutput } from "../../interaction/types.js";
|
|
2
|
+
import type { RoutingDecision } from "@mupt-ai/dari-router/policy-engine";
|
|
3
|
+
export declare function anthropicMessageId(requestId: string): string;
|
|
4
|
+
export declare function anthropicContent(output: InteractionOutput): Array<Record<string, unknown>>;
|
|
5
|
+
export declare function anthropicReasoningBlock(item: Extract<InteractionOutput["items"][number], {
|
|
6
|
+
type: "reasoning";
|
|
7
|
+
}>): Record<string, unknown>;
|
|
8
|
+
export declare function anthropicStopReason(output: Pick<InteractionOutput, "stopReason">): string;
|
|
9
|
+
export declare function anthropicUsage(output: Pick<InteractionOutput, "usage">): Record<string, unknown>;
|
|
10
|
+
export declare function anthropicRouting(decision: RoutingDecision, requestId: string, conversationId?: string): Record<string, unknown>;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { parseDataImageUrl } from "../../interaction/image.js";
|
|
2
|
+
import { encodeProviderContinuationState } from "../../interaction/provider_continuation_state.js";
|
|
3
|
+
import { encodePortableReasoningState } from "../../interaction/portable_reasoning_state.js";
|
|
4
|
+
export function anthropicMessageId(requestId) {
|
|
5
|
+
return `msg_${requestId.replace(/[^A-Za-z0-9_-]/g, "_")}`.slice(0, 64);
|
|
6
|
+
}
|
|
7
|
+
export function anthropicContent(output) {
|
|
8
|
+
const content = [];
|
|
9
|
+
for (const item of output.items) {
|
|
10
|
+
if (item.type === "reasoning") {
|
|
11
|
+
content.push(anthropicReasoningBlock(item));
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
if (item.type === "tool_call") {
|
|
15
|
+
content.push({
|
|
16
|
+
type: "tool_use",
|
|
17
|
+
id: item.callId,
|
|
18
|
+
name: item.namespace ? `${item.namespace}__${item.name}` : item.name,
|
|
19
|
+
input: toolInput(item.kind, item.input),
|
|
20
|
+
});
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (item.type === "hosted_tool_call") {
|
|
24
|
+
// Render server tool use and tool search results for Anthropic protocol.
|
|
25
|
+
if (item.tool === "server_tool_use") {
|
|
26
|
+
content.push({
|
|
27
|
+
type: "server_tool_use",
|
|
28
|
+
id: item.serverToolUseId ?? item.id,
|
|
29
|
+
name: item.serverToolName ?? "tool",
|
|
30
|
+
input: item.serverToolInput ?? {},
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
else if (item.tool === "tool_search") {
|
|
34
|
+
content.push({
|
|
35
|
+
type: "tool_search_tool_result",
|
|
36
|
+
tool_use_id: item.serverToolUseId ?? item.id,
|
|
37
|
+
content: (item.serverToolResult ?? []).map((ref) => ({
|
|
38
|
+
type: "tool_reference",
|
|
39
|
+
tool_name: ref.toolName,
|
|
40
|
+
})),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
for (const part of item.content) {
|
|
46
|
+
if (part.type === "text") {
|
|
47
|
+
content.push({ type: "text", text: part.text });
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (part.type === "tool_reference") {
|
|
51
|
+
content.push({ type: "text", text: `Referenced tool: ${part.toolName}` });
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const image = parseDataImageUrl(part.url);
|
|
55
|
+
if (!image)
|
|
56
|
+
throw new Error("Anthropic output images must use base64 data URLs.");
|
|
57
|
+
content.push({
|
|
58
|
+
type: "image",
|
|
59
|
+
source: { type: "base64", media_type: image.mimeType, data: image.data },
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return content.length > 0 ? content : [{ type: "text", text: "" }];
|
|
64
|
+
}
|
|
65
|
+
export function anthropicReasoningBlock(item) {
|
|
66
|
+
const encoded = item.continuation
|
|
67
|
+
? encodeProviderContinuationState(item.continuation)
|
|
68
|
+
: encodePortableReasoningState({ source: item.source, itemId: item.id });
|
|
69
|
+
if (item.continuation?.kind === "anthropic_redacted_thinking") {
|
|
70
|
+
return { type: "redacted_thinking", data: encoded };
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
type: "thinking",
|
|
74
|
+
thinking: item.continuation?.kind === "anthropic_thinking"
|
|
75
|
+
? item.continuation.thinking
|
|
76
|
+
: [...item.summary, ...item.content].join("\n"),
|
|
77
|
+
signature: encoded,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
export function anthropicStopReason(output) {
|
|
81
|
+
if (output.stopReason === "length")
|
|
82
|
+
return "max_tokens";
|
|
83
|
+
if (output.stopReason === "tool_use")
|
|
84
|
+
return "tool_use";
|
|
85
|
+
return "end_turn";
|
|
86
|
+
}
|
|
87
|
+
export function anthropicUsage(output) {
|
|
88
|
+
return {
|
|
89
|
+
input_tokens: output.usage.input,
|
|
90
|
+
cache_creation_input_tokens: output.usage.cacheWrite,
|
|
91
|
+
cache_read_input_tokens: output.usage.cacheRead,
|
|
92
|
+
output_tokens: output.usage.output,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
export function anthropicRouting(decision, requestId, conversationId) {
|
|
96
|
+
return {
|
|
97
|
+
requested_model: "dari/routing",
|
|
98
|
+
selected_model: decision.selectedModel,
|
|
99
|
+
reasoning_effort: decision.reasoningEffort,
|
|
100
|
+
reason: decision.reason,
|
|
101
|
+
request_id: requestId,
|
|
102
|
+
...(conversationId ? { conversation_id: conversationId } : {}),
|
|
103
|
+
...(decision.leaseTurnsRemaining === undefined
|
|
104
|
+
? {}
|
|
105
|
+
: { lease_turns_remaining: decision.leaseTurnsRemaining }),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function toolInput(kind, value) {
|
|
109
|
+
if (kind === "custom")
|
|
110
|
+
return { input: typeof value === "string" ? value : "" };
|
|
111
|
+
if (value && typeof value === "object")
|
|
112
|
+
return value;
|
|
113
|
+
if (typeof value !== "string" || !value)
|
|
114
|
+
return {};
|
|
115
|
+
try {
|
|
116
|
+
const parsed = JSON.parse(value);
|
|
117
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed)
|
|
118
|
+
? parsed
|
|
119
|
+
: {};
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
return {};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { InteractionRequest, ProviderIdentity } from "../../interaction/types.js";
|
|
2
|
+
import { type ProviderStreamOutcome } from "../../runtime/provider_stream_outcome.js";
|
|
3
|
+
export declare function anthropicMessagesSseStream(args: {
|
|
4
|
+
id: string;
|
|
5
|
+
model: string;
|
|
6
|
+
routing: RoutingDecision;
|
|
7
|
+
events: AsyncIterable<Record<string, unknown>>;
|
|
8
|
+
source: ProviderIdentity;
|
|
9
|
+
request: InteractionRequest;
|
|
10
|
+
conversationId?: string;
|
|
11
|
+
onFinal?: (outcome: ProviderStreamOutcome) => void;
|
|
12
|
+
beforeClose?: (outcome: ProviderStreamOutcome) => Promise<void> | void;
|
|
13
|
+
clientSignal?: AbortSignal;
|
|
14
|
+
onAbort?: () => void;
|
|
15
|
+
}): ReadableStream<Uint8Array>;
|
|
16
|
+
import { type RoutingDecision } from "@mupt-ai/dari-router/policy-engine";
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { piMessageToInteractionOutput } from "../../providers/pi_output.js";
|
|
2
|
+
import { providerToolCallIdentity } from "../../interaction/tool_identity.js";
|
|
3
|
+
import { isProviderFailureMessage, providerFailureFromEvent, providerFailureMessage, } from "../../runtime/provider_stream_outcome.js";
|
|
4
|
+
import { sseStream } from "./sse_stream.js";
|
|
5
|
+
import { anthropicMessageId, anthropicReasoningBlock, anthropicRouting, anthropicStopReason, anthropicUsage, } from "./anthropic_messages_output.js";
|
|
6
|
+
export function anthropicMessagesSseStream(args) {
|
|
7
|
+
const encoder = new TextEncoder();
|
|
8
|
+
const id = anthropicMessageId(args.id);
|
|
9
|
+
return sseStream({
|
|
10
|
+
protocol: "anthropic_messages",
|
|
11
|
+
lifecycle: args,
|
|
12
|
+
start: ({ controller, snapshot, lifecycle, settle, observeTerminal }) => {
|
|
13
|
+
const send = (event, payload) => {
|
|
14
|
+
if (!lifecycle.canWrite)
|
|
15
|
+
return;
|
|
16
|
+
snapshot.record({ event, data: payload });
|
|
17
|
+
controller.enqueue(encoder.encode(`event: ${event}\ndata: ${JSON.stringify(payload)}\n\n`));
|
|
18
|
+
};
|
|
19
|
+
const sendProviderError = (message) => {
|
|
20
|
+
send("error", {
|
|
21
|
+
type: "error",
|
|
22
|
+
error: { type: "api_error", message: providerFailureMessage(message) },
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
send("message_start", {
|
|
26
|
+
type: "message_start",
|
|
27
|
+
message: {
|
|
28
|
+
id,
|
|
29
|
+
type: "message",
|
|
30
|
+
role: "assistant",
|
|
31
|
+
model: args.model,
|
|
32
|
+
content: [],
|
|
33
|
+
stop_reason: null,
|
|
34
|
+
stop_sequence: null,
|
|
35
|
+
usage: { input_tokens: 0, output_tokens: 0 },
|
|
36
|
+
dari_routing: anthropicRouting(args.routing, args.id, args.conversationId),
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
const argumentDeltaIndexes = new Set();
|
|
40
|
+
const redactedThinkingIndexes = new Set();
|
|
41
|
+
const thinkingTextByIndex = new Map();
|
|
42
|
+
return {
|
|
43
|
+
consume: async () => {
|
|
44
|
+
for await (const event of args.events) {
|
|
45
|
+
if (lifecycle.shouldStopConsuming)
|
|
46
|
+
break;
|
|
47
|
+
const type = event.type;
|
|
48
|
+
const index = contentIndex(event);
|
|
49
|
+
if (type === "text_start" && index !== null) {
|
|
50
|
+
send("content_block_start", {
|
|
51
|
+
type: "content_block_start",
|
|
52
|
+
index,
|
|
53
|
+
content_block: { type: "text", text: "" },
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
else if (type === "text_delta" && index !== null && typeof event.delta === "string") {
|
|
57
|
+
send("content_block_delta", {
|
|
58
|
+
type: "content_block_delta",
|
|
59
|
+
index,
|
|
60
|
+
delta: { type: "text_delta", text: event.delta },
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
else if (type === "text_end" && index !== null) {
|
|
64
|
+
send("content_block_stop", { type: "content_block_stop", index });
|
|
65
|
+
}
|
|
66
|
+
else if (type === "thinking_start" && index !== null) {
|
|
67
|
+
thinkingTextByIndex.set(index, "");
|
|
68
|
+
const reasoning = reasoningFromPartial(event.partial, index, args.source, args.request);
|
|
69
|
+
if (reasoning?.continuation?.kind === "anthropic_redacted_thinking") {
|
|
70
|
+
redactedThinkingIndexes.add(index);
|
|
71
|
+
send("content_block_start", {
|
|
72
|
+
type: "content_block_start",
|
|
73
|
+
index,
|
|
74
|
+
content_block: anthropicReasoningBlock(reasoning),
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
send("content_block_start", {
|
|
79
|
+
type: "content_block_start",
|
|
80
|
+
index,
|
|
81
|
+
content_block: { type: "thinking", thinking: "", signature: "" },
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else if (type === "thinking_delta" && index !== null && typeof event.delta === "string") {
|
|
86
|
+
if (!redactedThinkingIndexes.has(index)) {
|
|
87
|
+
thinkingTextByIndex.set(index, `${thinkingTextByIndex.get(index) ?? ""}${event.delta}`);
|
|
88
|
+
send("content_block_delta", {
|
|
89
|
+
type: "content_block_delta",
|
|
90
|
+
index,
|
|
91
|
+
delta: { type: "thinking_delta", thinking: event.delta },
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else if (type === "thinking_end" && index !== null) {
|
|
96
|
+
if (!redactedThinkingIndexes.has(index)) {
|
|
97
|
+
const reasoning = reasoningFromPartial(event.partial, index, args.source, args.request);
|
|
98
|
+
const thinkingText = thinkingTextByIndex.get(index) ?? "";
|
|
99
|
+
const fallbackReasoning = {
|
|
100
|
+
type: "reasoning",
|
|
101
|
+
summary: thinkingText ? [thinkingText] : [],
|
|
102
|
+
content: [],
|
|
103
|
+
source: args.source,
|
|
104
|
+
};
|
|
105
|
+
const block = anthropicReasoningBlock(reasoning ?? fallbackReasoning);
|
|
106
|
+
const signature = String(block.signature);
|
|
107
|
+
send("content_block_delta", {
|
|
108
|
+
type: "content_block_delta",
|
|
109
|
+
index,
|
|
110
|
+
delta: { type: "signature_delta", signature },
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
thinkingTextByIndex.delete(index);
|
|
114
|
+
send("content_block_stop", { type: "content_block_stop", index });
|
|
115
|
+
}
|
|
116
|
+
else if (type === "toolcall_start" && index !== null) {
|
|
117
|
+
const toolCall = toolCallFromEvent(event);
|
|
118
|
+
const providerId = stringValue(event.toolCallId) ?? stringValue(toolCall.id) ?? `toolu_${index}`;
|
|
119
|
+
const identity = providerToolCallIdentity(providerId, args.source);
|
|
120
|
+
send("content_block_start", {
|
|
121
|
+
type: "content_block_start",
|
|
122
|
+
index,
|
|
123
|
+
content_block: {
|
|
124
|
+
type: "tool_use",
|
|
125
|
+
id: identity.callId,
|
|
126
|
+
name: stringValue(event.toolName) ?? stringValue(toolCall.name) ?? "tool",
|
|
127
|
+
input: {},
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
else if (type === "toolcall_delta" && index !== null && typeof event.delta === "string") {
|
|
132
|
+
argumentDeltaIndexes.add(index);
|
|
133
|
+
send("content_block_delta", {
|
|
134
|
+
type: "content_block_delta",
|
|
135
|
+
index,
|
|
136
|
+
delta: { type: "input_json_delta", partial_json: event.delta },
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
else if (type === "toolcall_end" && index !== null) {
|
|
140
|
+
if (!argumentDeltaIndexes.has(index)) {
|
|
141
|
+
const toolCall = toolCallFromEvent(event);
|
|
142
|
+
send("content_block_delta", {
|
|
143
|
+
type: "content_block_delta",
|
|
144
|
+
index,
|
|
145
|
+
delta: {
|
|
146
|
+
type: "input_json_delta",
|
|
147
|
+
partial_json: JSON.stringify(isRecord(toolCall.arguments) ? toolCall.arguments : {}),
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
send("content_block_stop", { type: "content_block_stop", index });
|
|
152
|
+
}
|
|
153
|
+
else if (type === "server_tool_call_start" && index !== null) {
|
|
154
|
+
const serverToolCall = isRecord(event.serverToolCall) ? event.serverToolCall : {};
|
|
155
|
+
const id = stringValue(serverToolCall.id) ?? `st_${index}`;
|
|
156
|
+
send("content_block_start", {
|
|
157
|
+
type: "content_block_start",
|
|
158
|
+
index,
|
|
159
|
+
content_block: {
|
|
160
|
+
type: "server_tool_use",
|
|
161
|
+
id,
|
|
162
|
+
name: stringValue(serverToolCall.name) ?? "tool_search_tool",
|
|
163
|
+
input: {},
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
send("content_block_delta", {
|
|
167
|
+
type: "content_block_delta",
|
|
168
|
+
index,
|
|
169
|
+
delta: {
|
|
170
|
+
type: "input_json_delta",
|
|
171
|
+
partial_json: JSON.stringify(isRecord(serverToolCall.arguments) ? serverToolCall.arguments : {}),
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
else if (type === "server_tool_call_end" && index !== null) {
|
|
176
|
+
send("content_block_stop", { type: "content_block_stop", index });
|
|
177
|
+
}
|
|
178
|
+
else if (type === "tool_search_result_start" && index !== null) {
|
|
179
|
+
const result = isRecord(event.toolSearchResult) ? event.toolSearchResult : {};
|
|
180
|
+
const content = Array.isArray(result.content) ? result.content.map((ref) => {
|
|
181
|
+
if (isRecord(ref))
|
|
182
|
+
return { type: "tool_reference", tool_name: String(ref.tool_name ?? "") };
|
|
183
|
+
return ref;
|
|
184
|
+
}) : [];
|
|
185
|
+
send("content_block_start", {
|
|
186
|
+
type: "content_block_start",
|
|
187
|
+
index,
|
|
188
|
+
content_block: {
|
|
189
|
+
type: "tool_search_tool_result",
|
|
190
|
+
tool_use_id: String(result.toolUseId ?? ""),
|
|
191
|
+
content,
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
else if (type === "tool_search_result_end" && index !== null) {
|
|
196
|
+
send("content_block_stop", { type: "content_block_stop", index });
|
|
197
|
+
}
|
|
198
|
+
else if (type === "done") {
|
|
199
|
+
if (!isRecord(event.message))
|
|
200
|
+
throw new Error("Provider stream ended without a terminal message.");
|
|
201
|
+
observeTerminal(event.message);
|
|
202
|
+
if (isProviderFailureMessage(event.message)) {
|
|
203
|
+
settle({ status: "provider_error", message: event.message });
|
|
204
|
+
sendProviderError(event.message);
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
const output = piMessageToInteractionOutput(event.message, args.source, args.request);
|
|
208
|
+
settle({ status: "completed", message: event.message, output });
|
|
209
|
+
send("message_delta", {
|
|
210
|
+
type: "message_delta",
|
|
211
|
+
delta: {
|
|
212
|
+
stop_reason: anthropicStopReason(output),
|
|
213
|
+
stop_sequence: null,
|
|
214
|
+
...(output.safeguardResults ? { safeguard_results: output.safeguardResults } : {}),
|
|
215
|
+
},
|
|
216
|
+
// message_start goes out before the provider answers, so the
|
|
217
|
+
// cumulative input and cache counts ride here, as on Anthropic.
|
|
218
|
+
// Claude Code sizes its context window from them.
|
|
219
|
+
usage: anthropicUsage(output),
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
else if (type === "error") {
|
|
225
|
+
const message = providerFailureFromEvent(event);
|
|
226
|
+
settle({ status: "provider_error", message });
|
|
227
|
+
sendProviderError(message);
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
emitProviderError: sendProviderError,
|
|
233
|
+
close: (outcome) => {
|
|
234
|
+
if (outcome.status === "completed") {
|
|
235
|
+
send("message_stop", { type: "message_stop" });
|
|
236
|
+
}
|
|
237
|
+
controller.close();
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
function reasoningFromPartial(partial, index, source, request) {
|
|
244
|
+
if (!isRecord(partial))
|
|
245
|
+
return null;
|
|
246
|
+
const output = piMessageToInteractionOutput(partial, source, request);
|
|
247
|
+
const item = output.items[index];
|
|
248
|
+
return item?.type === "reasoning" ? item : null;
|
|
249
|
+
}
|
|
250
|
+
function contentIndex(event) {
|
|
251
|
+
if (typeof event.contentIndex === "number")
|
|
252
|
+
return event.contentIndex;
|
|
253
|
+
return typeof event.index === "number" ? event.index : null;
|
|
254
|
+
}
|
|
255
|
+
function toolCallFromEvent(event) {
|
|
256
|
+
if (isRecord(event.toolCall))
|
|
257
|
+
return event.toolCall;
|
|
258
|
+
if (!isRecord(event.partial) || !Array.isArray(event.partial.content))
|
|
259
|
+
return {};
|
|
260
|
+
const index = contentIndex(event);
|
|
261
|
+
const block = index === null ? undefined : event.partial.content[index];
|
|
262
|
+
return isRecord(block) && block.type === "toolCall" ? block : {};
|
|
263
|
+
}
|
|
264
|
+
function stringValue(value) {
|
|
265
|
+
return typeof value === "string" && value ? value : undefined;
|
|
266
|
+
}
|
|
267
|
+
import { isRecord } from "@mupt-ai/dari-router/policy-engine";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type ChatMessage, type RoutingDecision } from "@mupt-ai/dari-router/policy-engine";
|
|
2
|
+
import type { InteractionOutput, InteractionRequest, ProviderIdentity } from "../../interaction/types.js";
|
|
3
|
+
import { type ProviderStreamOutcome } from "../../runtime/provider_stream_outcome.js";
|
|
4
|
+
export declare function chatCompletionResponse(args: {
|
|
5
|
+
id: string;
|
|
6
|
+
model: string;
|
|
7
|
+
output: InteractionOutput;
|
|
8
|
+
modelCost?: number | null;
|
|
9
|
+
routing: RoutingDecision;
|
|
10
|
+
conversationId?: string;
|
|
11
|
+
}): Record<string, unknown>;
|
|
12
|
+
export declare function chatCompletionsSseStream(args: {
|
|
13
|
+
id: string;
|
|
14
|
+
model: string;
|
|
15
|
+
routing: RoutingDecision;
|
|
16
|
+
events: AsyncIterable<Record<string, unknown>>;
|
|
17
|
+
source: ProviderIdentity;
|
|
18
|
+
request: InteractionRequest;
|
|
19
|
+
conversationId?: string;
|
|
20
|
+
onFinal?: (outcome: ProviderStreamOutcome) => void;
|
|
21
|
+
modelCost?: (message: Record<string, unknown>, failed: boolean) => number | null;
|
|
22
|
+
beforeClose?: (outcome: ProviderStreamOutcome) => Promise<void> | void;
|
|
23
|
+
clientSignal?: AbortSignal;
|
|
24
|
+
onAbort?: () => void;
|
|
25
|
+
}): ReadableStream<Uint8Array>;
|
|
26
|
+
export declare function assistantMessagePayload(output: InteractionOutput): ChatMessage;
|
|
27
|
+
export declare function chatHostedToolReplayPayload(item: Extract<InteractionOutput["items"][number], {
|
|
28
|
+
type: "hosted_tool_call";
|
|
29
|
+
}>): Record<string, unknown>;
|