-
-
Notifications
You must be signed in to change notification settings - Fork 181
refactor(proxy): introduce EndpointPolicy to replace hardcoded count_tokens checks #801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| const V1_PREFIX = "/v1"; | ||
|
|
||
| export const V1_ENDPOINT_PATHS = { | ||
| MESSAGES: "/v1/messages", | ||
| MESSAGES_COUNT_TOKENS: "/v1/messages/count_tokens", | ||
| RESPONSES: "/v1/responses", | ||
| RESPONSES_COMPACT: "/v1/responses/compact", | ||
| CHAT_COMPLETIONS: "/v1/chat/completions", | ||
| MODELS: "/v1/models", | ||
| } as const; | ||
|
|
||
| export const STANDARD_ENDPOINT_PATHS = [ | ||
| V1_ENDPOINT_PATHS.MESSAGES, | ||
| V1_ENDPOINT_PATHS.MESSAGES_COUNT_TOKENS, | ||
| V1_ENDPOINT_PATHS.RESPONSES, | ||
| V1_ENDPOINT_PATHS.RESPONSES_COMPACT, | ||
| V1_ENDPOINT_PATHS.CHAT_COMPLETIONS, | ||
| V1_ENDPOINT_PATHS.MODELS, | ||
| ] as const; | ||
|
|
||
| export const STRICT_STANDARD_ENDPOINT_PATHS = [ | ||
| V1_ENDPOINT_PATHS.MESSAGES, | ||
| V1_ENDPOINT_PATHS.RESPONSES, | ||
| V1_ENDPOINT_PATHS.RESPONSES_COMPACT, | ||
| V1_ENDPOINT_PATHS.CHAT_COMPLETIONS, | ||
| ] as const; | ||
|
|
||
| const standardEndpointPathSet = new Set<string>(STANDARD_ENDPOINT_PATHS); | ||
| const strictStandardEndpointPathSet = new Set<string>(STRICT_STANDARD_ENDPOINT_PATHS); | ||
|
|
||
| export function normalizeEndpointPath(pathname: string): string { | ||
| const pathWithoutQuery = pathname.split("?")[0]; | ||
| const trimmedPath = | ||
| pathWithoutQuery.length > 1 && pathWithoutQuery.endsWith("/") | ||
| ? pathWithoutQuery.slice(0, -1) | ||
| : pathWithoutQuery; | ||
|
|
||
| return trimmedPath.toLowerCase(); | ||
| } | ||
|
|
||
| export function isStandardEndpointPath(pathname: string): boolean { | ||
| return standardEndpointPathSet.has(normalizeEndpointPath(pathname)); | ||
| } | ||
|
|
||
| export function isStrictStandardEndpointPath(pathname: string): boolean { | ||
| return strictStandardEndpointPathSet.has(normalizeEndpointPath(pathname)); | ||
| } | ||
|
|
||
| export function isCountTokensEndpointPath(pathname: string): boolean { | ||
| return normalizeEndpointPath(pathname) === V1_ENDPOINT_PATHS.MESSAGES_COUNT_TOKENS; | ||
| } | ||
|
|
||
| export function isResponseCompactEndpointPath(pathname: string): boolean { | ||
| return normalizeEndpointPath(pathname) === V1_ENDPOINT_PATHS.RESPONSES_COMPACT; | ||
| } | ||
|
|
||
| export function toV1RoutePath(pathname: string): string { | ||
| if (!pathname.startsWith(V1_PREFIX)) { | ||
| return pathname; | ||
| } | ||
|
|
||
| const routePath = pathname.slice(V1_PREFIX.length); | ||
| return routePath.length > 0 ? routePath : "/"; | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,68 @@ | ||||||
| import { normalizeEndpointPath, V1_ENDPOINT_PATHS } from "./endpoint-paths"; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. endpoint-paths 导入请改用 该文件位于 src 内,新增导入按规范应使用 建议修复-import { normalizeEndpointPath, V1_ENDPOINT_PATHS } from "./endpoint-paths";
+import { normalizeEndpointPath, V1_ENDPOINT_PATHS } from "@/app/v1/_lib/proxy/endpoint-paths";📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
|
|
||||||
| export type EndpointGuardPreset = "chat" | "raw_passthrough"; | ||||||
|
|
||||||
| export type EndpointPoolStrictness = "inherit" | "strict"; | ||||||
|
|
||||||
| export interface EndpointPolicy { | ||||||
| readonly kind: "default" | "raw_passthrough"; | ||||||
| readonly guardPreset: EndpointGuardPreset; | ||||||
| readonly allowRetry: boolean; | ||||||
| readonly allowProviderSwitch: boolean; | ||||||
| readonly allowCircuitBreakerAccounting: boolean; | ||||||
| readonly trackConcurrentRequests: boolean; | ||||||
| readonly bypassRequestFilters: boolean; | ||||||
| readonly bypassForwarderPreprocessing: boolean; | ||||||
| readonly bypassSpecialSettings: boolean; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused policy field
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! Prompt To Fix With AIThis is a comment left during a code review.
Path: src/app/v1/_lib/proxy/endpoint-policy.ts
Line: 16:16
Comment:
**Unused policy field `bypassSpecialSettings`**
`bypassSpecialSettings` is defined on the `EndpointPolicy` interface and set on both policy objects, but it is never read anywhere in the production source code. The same applies to `allowProviderSwitch` (line 11) and `endpointPoolStrictness` (line 18) — none of these fields are consumed by any component in this PR. If these are placeholders for future work, consider adding a brief comment indicating that; otherwise they add surface area to the interface without being tested in an integration context.
<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>
How can I resolve this? If you propose a fix, please make it concise. |
||||||
| readonly bypassResponseRectifier: boolean; | ||||||
| readonly endpointPoolStrictness: EndpointPoolStrictness; | ||||||
| } | ||||||
|
|
||||||
| const DEFAULT_ENDPOINT_POLICY: EndpointPolicy = Object.freeze({ | ||||||
| kind: "default", | ||||||
| guardPreset: "chat", | ||||||
| allowRetry: true, | ||||||
| allowProviderSwitch: true, | ||||||
| allowCircuitBreakerAccounting: true, | ||||||
| trackConcurrentRequests: true, | ||||||
| bypassRequestFilters: false, | ||||||
| bypassForwarderPreprocessing: false, | ||||||
| bypassSpecialSettings: false, | ||||||
| bypassResponseRectifier: false, | ||||||
| endpointPoolStrictness: "inherit", | ||||||
| }); | ||||||
|
|
||||||
| const RAW_PASSTHROUGH_ENDPOINT_POLICY: EndpointPolicy = Object.freeze({ | ||||||
| kind: "raw_passthrough", | ||||||
| guardPreset: "raw_passthrough", | ||||||
| allowRetry: false, | ||||||
| allowProviderSwitch: false, | ||||||
| allowCircuitBreakerAccounting: false, | ||||||
| trackConcurrentRequests: false, | ||||||
| bypassRequestFilters: true, | ||||||
| bypassForwarderPreprocessing: true, | ||||||
| bypassSpecialSettings: true, | ||||||
| bypassResponseRectifier: true, | ||||||
| endpointPoolStrictness: "strict", | ||||||
| }); | ||||||
|
|
||||||
| const rawPassthroughEndpointPathSet = new Set<string>([ | ||||||
| V1_ENDPOINT_PATHS.MESSAGES_COUNT_TOKENS, | ||||||
| V1_ENDPOINT_PATHS.RESPONSES_COMPACT, | ||||||
| ]); | ||||||
|
|
||||||
| export function isRawPassthroughEndpointPath(pathname: string): boolean { | ||||||
| return rawPassthroughEndpointPathSet.has(normalizeEndpointPath(pathname)); | ||||||
| } | ||||||
|
|
||||||
| export function isRawPassthroughEndpointPolicy(policy: EndpointPolicy): boolean { | ||||||
| return policy.kind === "raw_passthrough"; | ||||||
| } | ||||||
|
|
||||||
| export function resolveEndpointPolicy(pathname: string): EndpointPolicy { | ||||||
| if (isRawPassthroughEndpointPath(pathname)) { | ||||||
| return RAW_PASSTHROUGH_ENDPOINT_POLICY; | ||||||
| } | ||||||
|
|
||||||
| return DEFAULT_ENDPOINT_POLICY; | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Several exports are unused across the codebase
STANDARD_ENDPOINT_PATHS,STRICT_STANDARD_ENDPOINT_PATHS,isStandardEndpointPath,isStrictStandardEndpointPath, andtoV1RoutePathare all exported but never imported or used anywhere in the source or test files. If these are intended for future use, a brief comment would help; otherwise consider removing them to keep the module focused.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Prompt To Fix With AI