Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/app/v1/_lib/proxy-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { attachSessionIdToErrorResponse } from "./proxy/error-session-id";
import { ProxyError } from "./proxy/errors";
import { detectClientFormat, detectFormatByEndpoint } from "./proxy/format-mapper";
import { ProxyForwarder } from "./proxy/forwarder";
import { GuardPipelineBuilder, RequestType } from "./proxy/guard-pipeline";
import { GuardPipelineBuilder } from "./proxy/guard-pipeline";
import { ProxyResponseHandler } from "./proxy/response-handler";
import { ProxyResponses } from "./proxy/responses";
import { ProxySession } from "./proxy/session";
Expand Down Expand Up @@ -49,9 +49,8 @@ export async function handleProxyRequest(c: Context): Promise<Response> {
}
}

// Decide request type and build configured guard pipeline
const type = session.isCountTokensRequest() ? RequestType.COUNT_TOKENS : RequestType.CHAT;
const pipeline = GuardPipelineBuilder.fromRequestType(type);
// Build guard pipeline from session endpoint policy
const pipeline = GuardPipelineBuilder.fromSession(session);

// Run guard chain; may return early Response
const early = await pipeline.run(session);
Expand All @@ -60,7 +59,7 @@ export async function handleProxyRequest(c: Context): Promise<Response> {
}

// 9. 增加并发计数(在所有检查通过后,请求开始前)- 跳过 count_tokens
if (session.sessionId && !session.isCountTokensRequest()) {
if (session.sessionId && session.getEndpointPolicy().trackConcurrentRequests) {
await SessionTracker.incrementConcurrentCount(session.sessionId);
}

Expand Down Expand Up @@ -97,7 +96,7 @@ export async function handleProxyRequest(c: Context): Promise<Response> {
return ProxyResponses.buildError(500, "代理请求发生未知错误");
} finally {
// 11. 减少并发计数(确保无论成功失败都执行)- 跳过 count_tokens
if (session?.sessionId && !session.isCountTokensRequest()) {
if (session?.sessionId && session.getEndpointPolicy().trackConcurrentRequests) {
await SessionTracker.decrementConcurrentCount(session.sessionId);
}
}
Expand Down
64 changes: 64 additions & 0 deletions src/app/v1/_lib/proxy/endpoint-paths.ts
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;
Comment on lines +12 to +26
Copy link

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, and toV1RoutePath are 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
This is a comment left during a code review.
Path: src/app/v1/_lib/proxy/endpoint-paths.ts
Line: 12:26

Comment:
**Several exports are unused across the codebase**

`STANDARD_ENDPOINT_PATHS`, `STRICT_STANDARD_ENDPOINT_PATHS`, `isStandardEndpointPath`, `isStrictStandardEndpointPath`, and `toV1RoutePath` are 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.

<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.


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 : "/";
}
68 changes: 68 additions & 0 deletions src/app/v1/_lib/proxy/endpoint-policy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { normalizeEndpointPath, V1_ENDPOINT_PATHS } from "./endpoint-paths";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

endpoint-paths 导入请改用 @/ 路径别名。

该文件位于 src 内,新增导入按规范应使用 @/ 前缀。As per coding guidelines: Use path alias @/ to reference files in ./src/ directory.

建议修复
-import { normalizeEndpointPath, V1_ENDPOINT_PATHS } from "./endpoint-paths";
+import { normalizeEndpointPath, V1_ENDPOINT_PATHS } from "@/app/v1/_lib/proxy/endpoint-paths";
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import { normalizeEndpointPath, V1_ENDPOINT_PATHS } from "./endpoint-paths";
import { normalizeEndpointPath, V1_ENDPOINT_PATHS } from "@/app/v1/_lib/proxy/endpoint-paths";
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/app/v1/_lib/proxy/endpoint-policy.ts` at line 1, Replace the relative
import of normalizeEndpointPath and V1_ENDPOINT_PATHS in endpoint-policy.ts with
the project path-alias import using the `@/` prefix; locate the import statement
that references "./endpoint-paths" and change it to import normalizeEndpointPath
and V1_ENDPOINT_PATHS from "@/app/v1/_lib/proxy/endpoint-paths" so the module
uses the src path alias.


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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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
This 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;
}
Loading
Loading