Spaces:
Running
Running
File size: 7,345 Bytes
f9a28cd 96bdf6c f9a28cd 96bdf6c f9a28cd 96bdf6c f9a28cd 96bdf6c f9a28cd 96bdf6c f9a28cd 96bdf6c f9a28cd 96bdf6c f9a28cd 96bdf6c f9a28cd 96bdf6c f9a28cd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | import { AgentApiError } from './api-error-response';
import {
isStreamingChannelRequestMode,
resolveChannelRequestMode,
type ChannelRequestMode,
type ChannelRequestModeBackend,
type ChannelRequestModeDecision
} from './channel-request-mode';
import type { ChannelCredential } from './channel-router';
import { RequestValidationError } from './image-request-utils';
import {
resolveImageStreamEnabled,
type ImageStreamMode,
type ImageStreamingStrategy
} from './image-upstream-strategy';
import type { getServerChannelState } from './server-channel-router';
import { readAffinityKey } from './server-runtime';
export type AgentChannelRequestModePlan = {
imageBackend: ChannelRequestModeBackend;
preferred: ChannelRequestMode;
fallback?: ChannelRequestMode;
candidates: readonly ChannelRequestMode[];
};
export type AgentChannelSelection = {
selectedCredential?: ChannelCredential;
requestMode: ChannelRequestMode;
preferredRequestMode?: ChannelRequestMode;
requestModePriority?: readonly ChannelRequestMode[];
fallbackApplied: boolean;
noChannelReason?: string;
};
export function createAgentChannelRequestModePlan(input: {
imageBackend: ChannelRequestModeBackend;
streamMode: ImageStreamMode;
streamingStrategy: ImageStreamingStrategy;
}): AgentChannelRequestModePlan {
const preferred = resolveChannelRequestMode({
imageBackend: input.imageBackend,
streamEnabled: resolveStaticAgentStreamEnabled(input)
});
if (input.streamMode !== 'auto' || !isStreamingChannelRequestMode(preferred)) {
return { imageBackend: input.imageBackend, preferred, candidates: [preferred] };
}
if (input.streamingStrategy !== 'auto') {
return { imageBackend: input.imageBackend, preferred, candidates: [preferred] };
}
const fallback = resolveChannelRequestMode({ imageBackend: input.imageBackend, streamEnabled: false });
return {
imageBackend: input.imageBackend,
preferred: fallback,
fallback: preferred,
candidates: [fallback, preferred]
};
}
export function selectAgentChannelCredential(input: {
router: ReturnType<typeof getServerChannelState>['router'];
headers: Headers;
requestModePlan: AgentChannelRequestModePlan;
}): AgentChannelSelection {
if (input.requestModePlan.candidates.length > 1 && input.router) {
try {
const selection = input.router.selectWithRequestModes({
affinityKey: readAffinityKey(input.headers),
requestModes: input.requestModePlan.candidates
});
return {
selectedCredential: selection.credential,
requestMode: selection.requestMode,
preferredRequestMode: selection.preferredRequestMode,
requestModePriority: selection.requestModePriority,
fallbackApplied: selection.requestMode !== selection.preferredRequestMode
};
} catch (error) {
throw normalizeChannelSelectionError(error, input.requestModePlan, false);
}
}
try {
return selectAgentChannelForMode(input, input.requestModePlan.preferred, false);
} catch (error) {
if (!input.requestModePlan.fallback || !(error instanceof RequestValidationError)) {
throw normalizeChannelSelectionError(error, input.requestModePlan, false);
}
try {
return selectAgentChannelForMode(input, input.requestModePlan.fallback, true);
} catch (fallbackError) {
throw normalizeChannelSelectionError(fallbackError, input.requestModePlan, true);
}
}
}
export function buildAgentChannelRequestModeDecision(input: {
requestModePlan: AgentChannelRequestModePlan;
selection: AgentChannelSelection;
selectedCredential?: ChannelCredential;
upstreamHost?: string;
}): ChannelRequestModeDecision {
const selectedChannelId = input.selectedCredential?.channelId ?? input.selection.selectedCredential?.channelId;
const preferredRequestMode = input.selection.preferredRequestMode ?? input.requestModePlan.preferred;
const requestModePriority = input.selection.requestModePriority ?? [
preferredRequestMode,
...input.requestModePlan.candidates.filter((mode) => mode !== preferredRequestMode)
];
const fallbackRequestMode = input.requestModePlan.candidates.find((mode) => mode !== preferredRequestMode);
return {
requested_backend: input.requestModePlan.imageBackend,
candidate_channel_request_modes: input.requestModePlan.candidates,
request_mode_priority: requestModePriority,
preferred_channel_request_mode: preferredRequestMode,
...(fallbackRequestMode ? { fallback_channel_request_mode: fallbackRequestMode } : {}),
selected_channel_request_mode: input.selection.requestMode,
fallback_applied: input.selection.fallbackApplied,
...(selectedChannelId ? { selected_channel_id: selectedChannelId } : {}),
...(input.upstreamHost ? { upstream_host: input.upstreamHost } : {}),
...(input.selection.noChannelReason ? { no_channel_reason: input.selection.noChannelReason } : {})
};
}
function resolveStaticAgentStreamEnabled(input: {
imageBackend: ChannelRequestModeBackend;
streamMode: ImageStreamMode;
streamingStrategy: ImageStreamingStrategy;
}): boolean {
if (input.streamMode === 'non_stream') return false;
if (input.streamMode === 'auto' && input.streamingStrategy === 'off') return false;
return resolveImageStreamEnabled({
imageBackend: input.imageBackend,
requestedStream: true,
streamingStrategy: input.streamingStrategy
});
}
function selectAgentChannelForMode(
input: {
router: ReturnType<typeof getServerChannelState>['router'];
headers: Headers;
},
requestMode: ChannelRequestMode,
fallbackApplied: boolean
): AgentChannelSelection {
return {
selectedCredential: input.router?.select({
affinityKey: readAffinityKey(input.headers),
requestMode
}),
requestMode,
preferredRequestMode: requestMode,
requestModePriority: [requestMode],
fallbackApplied
};
}
function normalizeChannelSelectionError(
error: unknown,
requestModePlan: AgentChannelRequestModePlan,
fallbackApplied: boolean
): unknown {
if (!(error instanceof RequestValidationError)) {
return error;
}
const requestMode = fallbackApplied
? (requestModePlan.fallback ?? requestModePlan.preferred)
: requestModePlan.preferred;
return new AgentApiError({
code: error.status >= 500 ? 'configuration_error' : 'validation_error',
message: error.message,
status: error.status,
retryable: false,
details: error.details,
diagnostics: {
channel_request_mode: requestMode,
channel_request_mode_fallback_applied: fallbackApplied,
route_decision: buildAgentChannelRequestModeDecision({
requestModePlan,
selection: {
requestMode,
fallbackApplied,
noChannelReason: error.message
}
})
}
});
}
|