@mupt-ai/dari-gateway 0.0.0-stage → 0.2.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/LICENSE +202 -0
- package/README.md +135 -2
- package/dari-gateway.example.yaml +28 -0
- package/package.json +42 -4
- package/src/bun.ts +4 -0
- package/src/context/eval_catalog.ts +11 -0
- package/src/context/interaction_fingerprint.ts +691 -0
- package/src/context/interaction_fingerprint_format.ts +189 -0
- package/src/context/interaction_lease_depth.ts +150 -0
- package/src/context/lease_history.ts +200 -0
- package/src/context/pricing.ts +30 -0
- package/src/context/provider_prompt_accounting.ts +241 -0
- package/src/context/routing_context.ts +337 -0
- package/src/http/auth.ts +43 -0
- package/src/http/errors.ts +114 -0
- package/src/http/handler.ts +110 -0
- package/src/http/http2_server.ts +122 -0
- package/src/http/serve.ts +119 -0
- package/src/index.ts +22 -0
- package/src/ingress/anthropic_messages.ts +162 -0
- package/src/ingress/anthropic_models.ts +28 -0
- package/src/ingress/chat_completions.ts +49 -0
- package/src/ingress/claude_code/request_context.ts +36 -0
- package/src/ingress/codex.ts +81 -0
- package/src/ingress/codex_models.ts +113 -0
- package/src/ingress/json.ts +113 -0
- package/src/ingress/models.ts +55 -0
- package/src/interaction/capabilities.ts +30 -0
- package/src/interaction/image.ts +12 -0
- package/src/interaction/portable_reasoning_state.ts +25 -0
- package/src/interaction/provider_continuation_state.ts +36 -0
- package/src/interaction/tool_identity.ts +65 -0
- package/src/interaction/types.ts +282 -0
- package/src/models/context_limits.ts +8 -0
- package/src/models/execution_provider.ts +22 -0
- package/src/models/gateways.ts +73 -0
- package/src/models/ids.ts +26 -0
- package/src/models/provider_defaults.ts +47 -0
- package/src/models/runtime_overrides.ts +221 -0
- package/src/observability/errors.ts +25 -0
- package/src/platform/platform.ts +204 -0
- package/src/platform/routing_contract.ts +6 -0
- package/src/protocol/input/anthropic_messages.ts +531 -0
- package/src/protocol/input/chat_completions.ts +255 -0
- package/src/protocol/input/chat_completions_validation.ts +293 -0
- package/src/protocol/input/openai_responses.ts +749 -0
- package/src/protocol/input/router_model.ts +53 -0
- package/src/protocol/output/anthropic_messages.ts +32 -0
- package/src/protocol/output/anthropic_messages_output.ts +129 -0
- package/src/protocol/output/anthropic_messages_stream.ts +281 -0
- package/src/protocol/output/chat_completions.ts +451 -0
- package/src/protocol/output/chat_reasoning_detail.ts +19 -0
- package/src/protocol/output/delivered_stream_snapshot.ts +31 -0
- package/src/protocol/output/openai_responses.ts +30 -0
- package/src/protocol/output/openai_responses_output.ts +159 -0
- package/src/protocol/output/openai_responses_stream.ts +631 -0
- package/src/protocol/output/sse_stream.ts +102 -0
- package/src/providers/anthropic_safeguards.ts +115 -0
- package/src/providers/aws_assume_role.ts +147 -0
- package/src/providers/cancel_estimate.ts +140 -0
- package/src/providers/execution.ts +800 -0
- package/src/providers/model_capabilities.ts +50 -0
- package/src/providers/pi.ts +499 -0
- package/src/providers/pi_context.ts +466 -0
- package/src/providers/pi_options.ts +416 -0
- package/src/providers/pi_output.ts +500 -0
- package/src/providers/provider_attempt_runner.ts +153 -0
- package/src/providers/provider_call.ts +433 -0
- package/src/providers/provider_reported_cost.ts +138 -0
- package/src/providers/reasoning.ts +59 -0
- package/src/providers/response_capture.ts +65 -0
- package/src/providers/stream_finalization.ts +264 -0
- package/src/providers/subscription_limits.ts +208 -0
- package/src/providers/subscription_usage.ts +99 -0
- package/src/runtime/claude_code/auxiliary_routing.ts +80 -0
- package/src/runtime/customer_disconnect.ts +66 -0
- package/src/runtime/explicit_model.ts +86 -0
- package/src/runtime/harness_identity/conversation.ts +87 -0
- package/src/runtime/harness_identity/extract.ts +323 -0
- package/src/runtime/harness_identity/index.ts +14 -0
- package/src/runtime/prepare_routed_completion.ts +224 -0
- package/src/runtime/provider_stream_lifecycle.ts +105 -0
- package/src/runtime/provider_stream_outcome.ts +83 -0
- package/src/runtime/request_tags.ts +34 -0
- package/src/runtime/routed_completion.ts +102 -0
- package/src/runtime/routed_protocol.ts +33 -0
- package/src/runtime/routed_runtime.ts +722 -0
- package/src/runtime/routing_lease.ts +273 -0
- package/src/runtime/runtime_reporter.ts +115 -0
- package/src/self_host/config.ts +173 -0
- package/src/self_host/main.ts +104 -0
- package/src/self_host/platform.ts +63 -0
- package/src/self_host/prefix_store.ts +315 -0
- package/src/self_host/resolve.ts +240 -0
- package/src/self_host/subscriptions/access.ts +43 -0
- package/src/self_host/subscriptions/http.ts +204 -0
- package/src/self_host/subscriptions/index.ts +43 -0
- package/src/self_host/subscriptions/oauth.ts +395 -0
- package/src/self_host/subscriptions/store.ts +339 -0
- package/src/strategies/auto_routing_selector.ts +72 -0
- package/src/strategies/eval_relevance.ts +57 -0
- package/src/strategies/fallback_decision.ts +30 -0
- package/src/strategies/priority_selector.ts +40 -0
- package/src/strategies/route_decision.ts +106 -0
- package/src/strategies/routing.ts +455 -0
- package/src/strategies/selector_projection.ts +207 -0
- package/src/telemetry/background.ts +40 -0
- package/src/telemetry/benchmark_replay.ts +160 -0
- package/src/telemetry/billing_finalization.ts +167 -0
- package/src/telemetry/ingest_values.ts +15 -0
- package/src/telemetry/model_cost.ts +14 -0
- package/src/telemetry/prefix_state.ts +183 -0
- package/src/telemetry/report.ts +832 -0
- package/src/telemetry/requests/interaction_observability.ts +258 -0
- package/src/telemetry/requests/retained_interaction_request.ts +20 -0
- package/src/telemetry/scrub.ts +38 -0
- package/src/telemetry/skills/claude/catalog.ts +119 -0
- package/src/telemetry/skills/claude/file_reads.ts +153 -0
- package/src/telemetry/skills/claude/observations.ts +450 -0
- package/src/telemetry/skills/codex/catalog.ts +171 -0
- package/src/telemetry/skills/codex/exec_file_reads.ts +55 -0
- package/src/telemetry/skills/codex/explicit.ts +116 -0
- package/src/telemetry/skills/codex/file_reads.ts +51 -0
- package/src/telemetry/skills/codex/identity.ts +21 -0
- package/src/telemetry/skills/observations.ts +94 -0
- package/src/telemetry/skills/paths.ts +82 -0
- package/src/telemetry/skills/pi.ts +276 -0
- package/src/telemetry/skills/shell_file_reads.ts +267 -0
- package/src/telemetry/skills/shell_words.ts +202 -0
- package/src/telemetry/tools/input.ts +16 -0
- package/src/telemetry/tools/observations.ts +349 -0
- package/src/telemetry/usage.ts +173 -0
- package/src/types.ts +150 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ChatCompletionRequest } from "@mupt-ai/dari-router/policy-engine";
|
|
2
|
+
import { bearerToken } from "../http/auth";
|
|
3
|
+
import { errorResponse } from "../http/errors";
|
|
4
|
+
import { captureRequestError } from "../observability/errors";
|
|
5
|
+
import { chatCompletionResponse, chatCompletionsSseStream } from "../protocol/output/chat_completions";
|
|
6
|
+
import { chatCompletionsToInteraction } from "../protocol/input/chat_completions";
|
|
7
|
+
import { handleRoutedCompletion } from "../runtime/routed_completion";
|
|
8
|
+
import type { SelectorBackend } from "../strategies/routing";
|
|
9
|
+
import {
|
|
10
|
+
chatCompletionsConversationIdentity,
|
|
11
|
+
openCodeRequestContext,
|
|
12
|
+
} from "../runtime/harness_identity";
|
|
13
|
+
import { parseJsonRequest } from "./json";
|
|
14
|
+
|
|
15
|
+
export async function handleChatCompletions(
|
|
16
|
+
request: Request,
|
|
17
|
+
routerId: string,
|
|
18
|
+
selectorBackend: SelectorBackend,
|
|
19
|
+
): Promise<Response> {
|
|
20
|
+
try {
|
|
21
|
+
const apiKey = bearerToken(request.headers);
|
|
22
|
+
const payload = await parseJsonRequest<ChatCompletionRequest>(request);
|
|
23
|
+
const interaction = chatCompletionsToInteraction({ ...payload, source_protocol: "openai_chat" });
|
|
24
|
+
const agentContext = openCodeRequestContext(request.headers);
|
|
25
|
+
return await handleRoutedCompletion({
|
|
26
|
+
request,
|
|
27
|
+
routerId,
|
|
28
|
+
apiKey,
|
|
29
|
+
payload: agentContext === undefined ? interaction : { ...interaction, agentContext },
|
|
30
|
+
responseBody: chatCompletionResponse,
|
|
31
|
+
streamBody: chatCompletionsSseStream,
|
|
32
|
+
errorResponse,
|
|
33
|
+
selectorBackend,
|
|
34
|
+
harnessConversationIdentity: chatCompletionsConversationIdentity(
|
|
35
|
+
request.headers,
|
|
36
|
+
payload.prompt_cache_key,
|
|
37
|
+
),
|
|
38
|
+
});
|
|
39
|
+
} catch (error) {
|
|
40
|
+
const response = errorResponse(error);
|
|
41
|
+
captureRequestError(error, {
|
|
42
|
+
request,
|
|
43
|
+
routerId,
|
|
44
|
+
sourceProtocol: "openai_chat",
|
|
45
|
+
status: response.status,
|
|
46
|
+
});
|
|
47
|
+
return response;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { HttpError } from "../../http/errors";
|
|
2
|
+
import type { AgentRequestContext, AgentRequestClass } from "../../interaction/types";
|
|
3
|
+
|
|
4
|
+
const REQUEST_CLASSES = new Set<AgentRequestClass>([
|
|
5
|
+
"main", "subagent", "workflow", "auxiliary", "compaction", "side_reply",
|
|
6
|
+
]);
|
|
7
|
+
|
|
8
|
+
// These are client-supplied routing hints, not authentication claims. Never
|
|
9
|
+
// infer them from prompt text: a user can legitimately paste any such prompt.
|
|
10
|
+
export function claudeCodeRequestContext(headers: Headers): AgentRequestContext {
|
|
11
|
+
const value = headers.get("x-claude-code-request-class")?.trim();
|
|
12
|
+
const requestClass = REQUEST_CLASSES.has(value as AgentRequestClass)
|
|
13
|
+
? value as AgentRequestClass
|
|
14
|
+
: "unknown";
|
|
15
|
+
const agentId = identifier(headers, "x-claude-code-agent-id");
|
|
16
|
+
if ((requestClass === "subagent" || requestClass === "workflow") && !agentId) {
|
|
17
|
+
throw new HttpError(400, "Agent requests require x-claude-code-agent-id.",
|
|
18
|
+
"invalid_request_error", "x-claude-code-agent-id");
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
requestClass,
|
|
22
|
+
...(agentId === undefined ? {} : { agentId }),
|
|
23
|
+
parentAgentId: identifier(headers, "x-claude-code-parent-agent-id"),
|
|
24
|
+
agentType: identifier(headers, "x-claude-code-agent-type"),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function identifier(headers: Headers, name: string): string | undefined {
|
|
29
|
+
const value = headers.get(name)?.trim();
|
|
30
|
+
if (!value) return undefined;
|
|
31
|
+
if (value.length > 200) {
|
|
32
|
+
throw new HttpError(400, `${name} must not exceed 200 characters.`,
|
|
33
|
+
"invalid_request_error", name);
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { bearerToken } from "../http/auth";
|
|
2
|
+
import { errorResponse } from "../http/errors";
|
|
3
|
+
import { captureRequestError } from "../observability/errors";
|
|
4
|
+
import { openAIResponsesResponse } from "../protocol/output/openai_responses";
|
|
5
|
+
import { openAIResponsesSseStream } from "../protocol/output/openai_responses_stream";
|
|
6
|
+
import { openAIResponsesToInteraction } from "../protocol/input/openai_responses";
|
|
7
|
+
import { parseJsonRequest } from "./json";
|
|
8
|
+
import { handleRoutedCompletion } from "../runtime/routed_completion";
|
|
9
|
+
import type { SelectorBackend } from "../strategies/routing";
|
|
10
|
+
import {
|
|
11
|
+
codexConversationIdentity,
|
|
12
|
+
codexRequestPurpose,
|
|
13
|
+
codexRequestContext,
|
|
14
|
+
} from "../runtime/harness_identity";
|
|
15
|
+
|
|
16
|
+
export async function handleResponses(
|
|
17
|
+
request: Request,
|
|
18
|
+
routerId: string,
|
|
19
|
+
selectorBackend: SelectorBackend,
|
|
20
|
+
): Promise<Response> {
|
|
21
|
+
try {
|
|
22
|
+
const apiKey = bearerToken(request.headers);
|
|
23
|
+
const rawPayload = await parseJsonRequest<Record<string, unknown>>(request);
|
|
24
|
+
const interaction = openAIResponsesToInteraction(rawPayload);
|
|
25
|
+
const requestPurpose = codexRequestPurpose(request.headers, rawPayload);
|
|
26
|
+
const agentContext = codexRequestContext(request.headers, rawPayload);
|
|
27
|
+
const payload = {
|
|
28
|
+
...interaction,
|
|
29
|
+
...(requestPurpose === undefined ? {} : { requestPurpose }),
|
|
30
|
+
...(agentContext === undefined ? {} : { agentContext }),
|
|
31
|
+
};
|
|
32
|
+
const harnessConversationIdentity = codexConversationIdentity(
|
|
33
|
+
request.headers,
|
|
34
|
+
rawPayload,
|
|
35
|
+
);
|
|
36
|
+
// Codex applies its global model_reasoning_effort setting to custom model
|
|
37
|
+
// providers. That preference describes a direct model, not a heterogeneous
|
|
38
|
+
// router, so let Dari select the reasoning effort for verified Codex turns.
|
|
39
|
+
// Parsing above still validates malformed Responses reasoning controls.
|
|
40
|
+
if (harnessConversationIdentity !== undefined) payload.reasoning = undefined;
|
|
41
|
+
return await handleRoutedCompletion({
|
|
42
|
+
request,
|
|
43
|
+
routerId,
|
|
44
|
+
apiKey,
|
|
45
|
+
payload,
|
|
46
|
+
responseBody: openAIResponsesResponse,
|
|
47
|
+
streamBody: openAIResponsesSseStream,
|
|
48
|
+
errorResponse,
|
|
49
|
+
selectorBackend,
|
|
50
|
+
harnessConversationIdentity,
|
|
51
|
+
});
|
|
52
|
+
} catch (error) {
|
|
53
|
+
const response = errorResponse(error);
|
|
54
|
+
captureRequestError(error, {
|
|
55
|
+
request,
|
|
56
|
+
routerId,
|
|
57
|
+
sourceProtocol: "openai_responses",
|
|
58
|
+
status: response.status,
|
|
59
|
+
});
|
|
60
|
+
return response;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function handleResponsesWebSocketFallback(): Response {
|
|
65
|
+
return Response.json(
|
|
66
|
+
{
|
|
67
|
+
error: {
|
|
68
|
+
message: "Responses WebSocket is not supported by this router endpoint; use HTTP Responses.",
|
|
69
|
+
type: "unsupported_transport",
|
|
70
|
+
code: "unsupported_transport",
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
status: 426,
|
|
75
|
+
headers: {
|
|
76
|
+
"Cache-Control": "no-store",
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { ResolvedRouter } from "../types";
|
|
2
|
+
import { CANONICAL_ROUTER_MODEL, explicitModelSlug } from "../protocol/input/router_model";
|
|
3
|
+
import { effectiveContextWindow } from "../models/context_limits";
|
|
4
|
+
import { modelSupportsHostedWebSearch } from "../providers/model_capabilities";
|
|
5
|
+
|
|
6
|
+
const DEFAULT_CODEX_CONTEXT_WINDOW = 128_000;
|
|
7
|
+
|
|
8
|
+
function codexRouterContextWindow(router: Pick<ResolvedRouter, "enabled_models" | "model_configs" | "allow_long_context">): number {
|
|
9
|
+
const windows = router.enabled_models.map((model) => {
|
|
10
|
+
const configured = effectiveContextWindow(router.model_configs?.[model], router.allow_long_context === true);
|
|
11
|
+
return typeof configured === "number" && Number.isFinite(configured) && configured > 0
|
|
12
|
+
? Math.floor(configured)
|
|
13
|
+
: DEFAULT_CODEX_CONTEXT_WINDOW;
|
|
14
|
+
});
|
|
15
|
+
return windows.length > 0 ? Math.max(...windows) : DEFAULT_CODEX_CONTEXT_WINDOW;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function codexRouterSupportsHostedWebSearch(
|
|
19
|
+
router: Pick<ResolvedRouter, "enabled_models" | "model_configs">
|
|
20
|
+
): boolean {
|
|
21
|
+
return router.enabled_models.some((model) => {
|
|
22
|
+
const config = router.model_configs?.[model];
|
|
23
|
+
return config ? modelSupportsHostedWebSearch({ model: config }) : false;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
type CodexCatalogEntry = {
|
|
28
|
+
slug: string;
|
|
29
|
+
displayName: string;
|
|
30
|
+
description: string;
|
|
31
|
+
priority: number;
|
|
32
|
+
contextWindow: number;
|
|
33
|
+
hostedWebSearch: boolean;
|
|
34
|
+
imageInput: boolean;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
function codexCatalogEntry(args: CodexCatalogEntry): Record<string, unknown> {
|
|
38
|
+
return {
|
|
39
|
+
slug: args.slug,
|
|
40
|
+
display_name: args.displayName,
|
|
41
|
+
description: args.description,
|
|
42
|
+
default_reasoning_level: null,
|
|
43
|
+
supported_reasoning_levels: [],
|
|
44
|
+
shell_type: "shell_command",
|
|
45
|
+
visibility: "list",
|
|
46
|
+
supported_in_api: true,
|
|
47
|
+
priority: args.priority,
|
|
48
|
+
additional_speed_tiers: [],
|
|
49
|
+
service_tiers: [],
|
|
50
|
+
default_service_tier: null,
|
|
51
|
+
availability_nux: null,
|
|
52
|
+
upgrade: null,
|
|
53
|
+
base_instructions:
|
|
54
|
+
"You are Codex, a coding agent. You and the user share one workspace, and your job is to collaborate with them until their goal is genuinely handled.",
|
|
55
|
+
model_messages: null,
|
|
56
|
+
include_skills_usage_instructions: false,
|
|
57
|
+
// Codex 0.144 requires the legacy field; current main reads the renamed field below.
|
|
58
|
+
supports_reasoning_summaries: false,
|
|
59
|
+
supports_reasoning_summary_parameter: false,
|
|
60
|
+
default_reasoning_summary: "none",
|
|
61
|
+
support_verbosity: false,
|
|
62
|
+
default_verbosity: null,
|
|
63
|
+
apply_patch_tool_type: "freeform",
|
|
64
|
+
web_search_tool_type: args.hostedWebSearch ? "text" : null,
|
|
65
|
+
truncation_policy: { mode: "tokens", limit: 10_000 },
|
|
66
|
+
supports_parallel_tool_calls: true,
|
|
67
|
+
supports_image_detail_original: false,
|
|
68
|
+
context_window: args.contextWindow,
|
|
69
|
+
max_context_window: args.contextWindow,
|
|
70
|
+
auto_compact_token_limit: null,
|
|
71
|
+
comp_hash: null,
|
|
72
|
+
effective_context_window_percent: 95,
|
|
73
|
+
experimental_supported_tools: [],
|
|
74
|
+
input_modalities: args.imageInput ? ["text", "image"] : ["text"],
|
|
75
|
+
supports_search_tool: false,
|
|
76
|
+
use_responses_lite: false,
|
|
77
|
+
auto_review_model_override: args.slug,
|
|
78
|
+
tool_mode: null,
|
|
79
|
+
multi_agent_version: null,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/* `dari/routing` stays first (the Codex launcher patches it); each enabled
|
|
84
|
+
model follows as `dari/model/<id>` so Codex's /model menu can switch to it. */
|
|
85
|
+
export function codexModelsResponse(
|
|
86
|
+
router: Pick<ResolvedRouter, "enabled_models" | "model_configs" | "allow_long_context">,
|
|
87
|
+
imageInputFor: (model: string) => boolean
|
|
88
|
+
): Record<string, unknown> {
|
|
89
|
+
return {
|
|
90
|
+
models: [
|
|
91
|
+
codexCatalogEntry({
|
|
92
|
+
slug: CANONICAL_ROUTER_MODEL,
|
|
93
|
+
displayName: "Dari Routing",
|
|
94
|
+
description: "Routes Codex HTTP Responses requests through the configured Dari model router.",
|
|
95
|
+
priority: 0,
|
|
96
|
+
contextWindow: codexRouterContextWindow(router),
|
|
97
|
+
hostedWebSearch: codexRouterSupportsHostedWebSearch(router),
|
|
98
|
+
imageInput: router.enabled_models.some(imageInputFor),
|
|
99
|
+
}),
|
|
100
|
+
...router.enabled_models.map((model, index) =>
|
|
101
|
+
codexCatalogEntry({
|
|
102
|
+
slug: explicitModelSlug(model),
|
|
103
|
+
displayName: model,
|
|
104
|
+
description: `Serves ${model} directly, without routing.`,
|
|
105
|
+
priority: index + 1,
|
|
106
|
+
contextWindow: codexRouterContextWindow({ ...router, enabled_models: [model] }),
|
|
107
|
+
hostedWebSearch: codexRouterSupportsHostedWebSearch({ ...router, enabled_models: [model] }),
|
|
108
|
+
imageInput: imageInputFor(model),
|
|
109
|
+
})
|
|
110
|
+
),
|
|
111
|
+
],
|
|
112
|
+
};
|
|
113
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { Readable, type Transform } from "node:stream";
|
|
2
|
+
import {
|
|
3
|
+
createBrotliDecompress,
|
|
4
|
+
createGunzip,
|
|
5
|
+
createInflate,
|
|
6
|
+
createZstdDecompress,
|
|
7
|
+
} from "node:zlib";
|
|
8
|
+
|
|
9
|
+
import { HttpError } from "../http/errors";
|
|
10
|
+
|
|
11
|
+
const MAX_ENCODED_JSON_REQUEST_BYTES = 64 * 1024 * 1024;
|
|
12
|
+
const MAX_DECODED_JSON_REQUEST_BYTES = 64 * 1024 * 1024;
|
|
13
|
+
const MAX_CONTENT_ENCODING_LAYERS = 3;
|
|
14
|
+
|
|
15
|
+
export async function parseJsonRequest<T extends Record<string, unknown>>(request: Request): Promise<T> {
|
|
16
|
+
let payload: unknown;
|
|
17
|
+
try {
|
|
18
|
+
payload = JSON.parse(await requestText(request));
|
|
19
|
+
} catch (error) {
|
|
20
|
+
if (error instanceof HttpError) throw error;
|
|
21
|
+
throw new HttpError(400, "Request body must be valid JSON.", "invalid_request_error");
|
|
22
|
+
}
|
|
23
|
+
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) {
|
|
24
|
+
throw new HttpError(400, "Request body must be a JSON object.", "invalid_request_error");
|
|
25
|
+
}
|
|
26
|
+
return payload as T;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function requestText(request: Request): Promise<string> {
|
|
30
|
+
const contentLength = request.headers.get("content-length");
|
|
31
|
+
if (contentLength !== null) {
|
|
32
|
+
const parsed = Number(contentLength);
|
|
33
|
+
if (Number.isFinite(parsed) && parsed > MAX_ENCODED_JSON_REQUEST_BYTES) {
|
|
34
|
+
throw requestTooLarge();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const encodings = contentEncodings(request.headers.get("content-encoding"));
|
|
38
|
+
if (encodings.length > MAX_CONTENT_ENCODING_LAYERS) {
|
|
39
|
+
throw new HttpError(415, "Too many Content-Encoding layers.", "unsupported_content_encoding", "content-encoding");
|
|
40
|
+
}
|
|
41
|
+
let stream: AsyncIterable<Uint8Array> = boundedChunks(
|
|
42
|
+
request.body ?? emptyByteStream(),
|
|
43
|
+
MAX_ENCODED_JSON_REQUEST_BYTES
|
|
44
|
+
);
|
|
45
|
+
for (const encoding of encodings.reverse()) {
|
|
46
|
+
stream = boundedChunks(
|
|
47
|
+
decodedChunks(stream, encoding),
|
|
48
|
+
MAX_DECODED_JSON_REQUEST_BYTES
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
return await readText(stream);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function contentEncodings(header: string | null): string[] {
|
|
55
|
+
if (!header) return [];
|
|
56
|
+
return header
|
|
57
|
+
.split(",")
|
|
58
|
+
.map((value) => value.trim().toLowerCase())
|
|
59
|
+
.filter((value) => value && value !== "identity");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function* boundedChunks(
|
|
63
|
+
stream: AsyncIterable<Uint8Array>,
|
|
64
|
+
maxBytes: number
|
|
65
|
+
): AsyncGenerator<Uint8Array> {
|
|
66
|
+
let bytesRead = 0;
|
|
67
|
+
for await (const chunk of stream) {
|
|
68
|
+
bytesRead += chunk.byteLength;
|
|
69
|
+
if (bytesRead > maxBytes) throw requestTooLarge();
|
|
70
|
+
yield chunk;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function* decodedChunks(
|
|
75
|
+
stream: AsyncIterable<Uint8Array>,
|
|
76
|
+
encoding: string
|
|
77
|
+
): AsyncGenerator<Uint8Array> {
|
|
78
|
+
const decoded = Readable.from(stream).pipe(decompressor(encoding));
|
|
79
|
+
try {
|
|
80
|
+
for await (const chunk of decoded) {
|
|
81
|
+
if (!(chunk instanceof Uint8Array)) throw new Error("Decompressor emitted a non-byte chunk.");
|
|
82
|
+
yield chunk;
|
|
83
|
+
}
|
|
84
|
+
} finally {
|
|
85
|
+
decoded.destroy();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function decompressor(encoding: string): Transform {
|
|
90
|
+
if (encoding === "zstd") return createZstdDecompress();
|
|
91
|
+
if (encoding === "gzip" || encoding === "x-gzip") return createGunzip();
|
|
92
|
+
if (encoding === "deflate") return createInflate();
|
|
93
|
+
if (encoding === "br") return createBrotliDecompress();
|
|
94
|
+
throw new HttpError(415, `Unsupported Content-Encoding '${encoding}'.`, "unsupported_content_encoding", "content-encoding");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function readText(stream: AsyncIterable<Uint8Array>): Promise<string> {
|
|
98
|
+
const decoder = new TextDecoder();
|
|
99
|
+
const parts: string[] = [];
|
|
100
|
+
for await (const chunk of stream) {
|
|
101
|
+
parts.push(decoder.decode(chunk, { stream: true }));
|
|
102
|
+
}
|
|
103
|
+
parts.push(decoder.decode());
|
|
104
|
+
return parts.join("");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async function* emptyByteStream(): AsyncGenerator<Uint8Array> {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function requestTooLarge(): HttpError {
|
|
112
|
+
return new HttpError(413, "Request body is too large.", "request_entity_too_large");
|
|
113
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/* Shared GET /models discovery route. One URL serves two client protocols:
|
|
2
|
+
Codex reads the Codex catalog; Anthropic-SDK clients (Claude Desktop /
|
|
3
|
+
Cowork gateway probes, Claude Code) read the Anthropic list shape. The
|
|
4
|
+
handler detects the caller, authenticates in that protocol's auth shape,
|
|
5
|
+
and selects the matching catalog adapter. */
|
|
6
|
+
|
|
7
|
+
import { anthropicToken, bearerToken } from "../http/auth";
|
|
8
|
+
import { errorResponse } from "../http/errors";
|
|
9
|
+
import { captureRequestError } from "../observability/errors";
|
|
10
|
+
import { anthropicErrorResponse } from "./anthropic_messages";
|
|
11
|
+
import { modelSupportsImageInput } from "../providers/model_capabilities";
|
|
12
|
+
import { modelMetadataLookup } from "../providers/pi";
|
|
13
|
+
import { resolveRouter } from "../strategies/routing";
|
|
14
|
+
import { anthropicModelsResponse } from "./anthropic_models";
|
|
15
|
+
import { codexModelsResponse } from "./codex_models";
|
|
16
|
+
|
|
17
|
+
export async function handleModels(request: Request, routerId: string): Promise<Response> {
|
|
18
|
+
// Anthropic SDK clients always send anthropic-version, and API-key probes
|
|
19
|
+
// authenticate via x-api-key instead of a bearer Authorization header.
|
|
20
|
+
// Codex sends neither.
|
|
21
|
+
const anthropicClient =
|
|
22
|
+
request.headers.has("anthropic-version") ||
|
|
23
|
+
(!request.headers.get("authorization") && request.headers.has("x-api-key"));
|
|
24
|
+
try {
|
|
25
|
+
const apiKey = anthropicClient ? anthropicToken(request.headers) : bearerToken(request.headers);
|
|
26
|
+
// Resolution authenticates the key and confirms the router exists even
|
|
27
|
+
// when the response body (Anthropic shape) does not depend on it.
|
|
28
|
+
const router = await resolveRouter({ routerId, apiKey });
|
|
29
|
+
if (anthropicClient) {
|
|
30
|
+
return Response.json(anthropicModelsResponse(), {
|
|
31
|
+
headers: {
|
|
32
|
+
"Cache-Control": "no-store",
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
const metadataLookup = modelMetadataLookup(router.model_configs);
|
|
37
|
+
const imageInputFor = (model: string) => modelSupportsImageInput(metadataLookup(model));
|
|
38
|
+
return Response.json(codexModelsResponse(router, imageInputFor), {
|
|
39
|
+
headers: {
|
|
40
|
+
"Cache-Control": "no-store",
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
} catch (error) {
|
|
44
|
+
// Each protocol's clients parse their own error envelope: Anthropic's
|
|
45
|
+
// {type:"error",error:{type,message}} vs the OpenAI {error:{...}} shape.
|
|
46
|
+
const response = anthropicClient ? anthropicErrorResponse(error) : errorResponse(error);
|
|
47
|
+
captureRequestError(error, {
|
|
48
|
+
request,
|
|
49
|
+
routerId,
|
|
50
|
+
sourceProtocol: anthropicClient ? "anthropic_models" : "openai_models",
|
|
51
|
+
status: response.status,
|
|
52
|
+
});
|
|
53
|
+
return response;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { HardCapability } from "@mupt-ai/dari-router/policy-engine";
|
|
2
|
+
import type { InteractionItem, InteractionRequest } from "./types";
|
|
3
|
+
|
|
4
|
+
export function requiredCapabilitiesFor(
|
|
5
|
+
request: Pick<InteractionRequest, "items" | "responseFormat" | "toolChoice" | "tools">
|
|
6
|
+
): HardCapability[] {
|
|
7
|
+
const capabilities = new Set<HardCapability>();
|
|
8
|
+
if (request.items.some(itemHasImage)) capabilities.add("image_input");
|
|
9
|
+
if (request.items.some((item) => item.type === "reasoning_configuration")) {
|
|
10
|
+
capabilities.add("reasoning_configuration_update");
|
|
11
|
+
}
|
|
12
|
+
if (request.responseFormat !== undefined && request.responseFormat.type !== "text") capabilities.add("structured_output");
|
|
13
|
+
if (
|
|
14
|
+
request.tools.some((tool) => tool.kind === "hosted")
|
|
15
|
+
|| request.items.some(requiresWebSearch)
|
|
16
|
+
|| (typeof request.toolChoice === "object" && request.toolChoice.kind === "hosted")
|
|
17
|
+
) {
|
|
18
|
+
capabilities.add("openai_hosted_web_search");
|
|
19
|
+
}
|
|
20
|
+
return [...capabilities];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function requiresWebSearch(item: InteractionItem): boolean {
|
|
24
|
+
return item.type === "hosted_tool_call" && item.tool === "web_search";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function itemHasImage(item: InteractionItem): boolean {
|
|
28
|
+
if (item.type === "message") return item.content.some((part) => part.type === "image");
|
|
29
|
+
return item.type === "tool_result" && Array.isArray(item.output) && item.output.some((part) => part.type === "image");
|
|
30
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function parseDataImageUrl(url: string): { mimeType: string; data: string } | undefined {
|
|
2
|
+
const commaIndex = url.indexOf(",");
|
|
3
|
+
if (!url.startsWith("data:") || commaIndex === -1) return undefined;
|
|
4
|
+
const header = url.slice(5, commaIndex);
|
|
5
|
+
const segments = header.split(";");
|
|
6
|
+
const mimeType = segments.shift();
|
|
7
|
+
const data = url.slice(commaIndex + 1);
|
|
8
|
+
if (!mimeType?.toLowerCase().startsWith("image/") || segments.at(-1)?.toLowerCase() !== "base64" || !data) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
return { mimeType, data };
|
|
12
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {
|
|
2
|
+
decodeUntrustedPortableReasoningState as coreDecode,
|
|
3
|
+
encodePortableReasoningState as coreEncode,
|
|
4
|
+
type PortableReasoningState,
|
|
5
|
+
} from "@mupt-ai/dari-router/protocols";
|
|
6
|
+
|
|
7
|
+
import { rethrowFrameworkErrorAsHttp } from "../http/errors";
|
|
8
|
+
|
|
9
|
+
export type { PortableReasoningState } from "@mupt-ai/dari-router/protocols";
|
|
10
|
+
|
|
11
|
+
export function encodePortableReasoningState(state: PortableReasoningState): string {
|
|
12
|
+
try {
|
|
13
|
+
return coreEncode(state);
|
|
14
|
+
} catch (error) {
|
|
15
|
+
rethrowFrameworkErrorAsHttp(error, 500);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function decodeUntrustedPortableReasoningState(value: string): PortableReasoningState | null {
|
|
20
|
+
try {
|
|
21
|
+
return coreDecode(value);
|
|
22
|
+
} catch (error) {
|
|
23
|
+
rethrowFrameworkErrorAsHttp(error, 400);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
decodeUntrustedProviderContinuationState as coreDecode,
|
|
3
|
+
encodeProviderContinuationState as coreEncode,
|
|
4
|
+
providerIdentityFromModel as coreProviderIdentityFromModel,
|
|
5
|
+
type ProviderContinuationState,
|
|
6
|
+
type ProviderIdentity,
|
|
7
|
+
} from "@mupt-ai/dari-router/protocols";
|
|
8
|
+
|
|
9
|
+
import { rethrowFrameworkErrorAsHttp } from "../http/errors";
|
|
10
|
+
|
|
11
|
+
export type { ProviderContinuationState, ProviderIdentity } from "@mupt-ai/dari-router/protocols";
|
|
12
|
+
export { compatibleProviderContinuation, isAnthropicEffort, sameProviderIdentity } from "@mupt-ai/dari-router/protocols";
|
|
13
|
+
|
|
14
|
+
export function encodeProviderContinuationState(value: ProviderContinuationState): string {
|
|
15
|
+
try {
|
|
16
|
+
return coreEncode(value);
|
|
17
|
+
} catch (error) {
|
|
18
|
+
rethrowFrameworkErrorAsHttp(error, 500);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function decodeUntrustedProviderContinuationState(value: string): ProviderContinuationState | null {
|
|
23
|
+
try {
|
|
24
|
+
return coreDecode(value);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
rethrowFrameworkErrorAsHttp(error, 400);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function providerIdentityFromModel(model: Record<string, unknown>): ProviderIdentity {
|
|
31
|
+
try {
|
|
32
|
+
return coreProviderIdentityFromModel(model);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
rethrowFrameworkErrorAsHttp(error, 502);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { HttpError } from "../http/errors";
|
|
2
|
+
import type { InteractionTool, ProviderIdentity } from "./types";
|
|
3
|
+
|
|
4
|
+
export const TOOL_CALL_ID_MAX_LENGTH = 512;
|
|
5
|
+
|
|
6
|
+
export function isProtocolToolCallId(value: unknown): value is string {
|
|
7
|
+
return (
|
|
8
|
+
typeof value === "string" &&
|
|
9
|
+
value.trim().length > 0 &&
|
|
10
|
+
value.length <= TOOL_CALL_ID_MAX_LENGTH
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function toolWireName(namespace: string | undefined, name: string): string {
|
|
15
|
+
return namespace ? `${namespace}__${name}` : name;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function toolsByWireName(tools: InteractionTool[]): Map<string, InteractionTool> {
|
|
19
|
+
const indexed = new Map<string, InteractionTool>();
|
|
20
|
+
for (const tool of tools) {
|
|
21
|
+
if (tool.kind === "hosted" || tool.kind === "server_tool") continue;
|
|
22
|
+
const wireName = tool.kind === "client_tool" ? tool.name : toolWireName(tool.namespace, tool.name);
|
|
23
|
+
if (indexed.has(wireName)) {
|
|
24
|
+
throw new HttpError(
|
|
25
|
+
400,
|
|
26
|
+
`Tool names must remain unique after namespace encoding; '${wireName}' is ambiguous.`,
|
|
27
|
+
"invalid_request_error",
|
|
28
|
+
"tools"
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
indexed.set(wireName, tool);
|
|
32
|
+
}
|
|
33
|
+
return indexed;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function providerToolCallId(
|
|
37
|
+
callId: string,
|
|
38
|
+
itemId: string | undefined,
|
|
39
|
+
providerApi: string | null
|
|
40
|
+
): string {
|
|
41
|
+
if (!itemId || !usesResponsesToolCallIds(providerApi)) return callId;
|
|
42
|
+
return `${callId}|${itemId}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function providerToolCallIdentity(
|
|
46
|
+
id: string,
|
|
47
|
+
source: ProviderIdentity
|
|
48
|
+
): { callId: string; itemId?: string } {
|
|
49
|
+
const separator = usesResponsesToolCallIds(source.api) ? id.indexOf("|") : -1;
|
|
50
|
+
const identity = separator > 0 && separator < id.length - 1
|
|
51
|
+
? { callId: id.slice(0, separator), itemId: id.slice(separator + 1) }
|
|
52
|
+
: { callId: id };
|
|
53
|
+
if (!isProtocolToolCallId(identity.callId)) {
|
|
54
|
+
throw new HttpError(
|
|
55
|
+
502,
|
|
56
|
+
`Provider output tool call ID must be a non-empty string no longer than ${TOOL_CALL_ID_MAX_LENGTH} characters.`,
|
|
57
|
+
"provider_output_invalid"
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return identity;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function usesResponsesToolCallIds(api: string | null): boolean {
|
|
64
|
+
return api === "openai-responses" || api === "azure-openai-responses";
|
|
65
|
+
}
|