Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
d90e56eb
Commit
d90e56eb
authored
Mar 06, 2026
by
神乐
Browse files
chore(openai): clean up ws usage window branch
parent
838ada88
Changes
2
Hide whitespace changes
Inline
Side-by-side
backend/internal/service/account_usage_service.go
View file @
d90e56eb
...
...
@@ -96,6 +96,7 @@ const (
apiCacheTTL
=
3
*
time
.
Minute
windowStatsCacheTTL
=
1
*
time
.
Minute
openAIProbeCacheTTL
=
10
*
time
.
Minute
openAICodexProbeVersion
=
"0.104.0"
)
// UsageCache 封装账户使用量相关的缓存
...
...
@@ -400,7 +401,7 @@ func (s *AccountUsageService) probeOpenAICodexSnapshot(ctx context.Context, acco
req
.
Header
.
Set
(
"Accept"
,
"text/event-stream"
)
req
.
Header
.
Set
(
"OpenAI-Beta"
,
"responses=experimental"
)
req
.
Header
.
Set
(
"Originator"
,
"codex_cli_rs"
)
req
.
Header
.
Set
(
"Version"
,
codexCLI
Version
)
req
.
Header
.
Set
(
"Version"
,
openAICodexProbe
Version
)
req
.
Header
.
Set
(
"User-Agent"
,
codexCLIUserAgent
)
if
s
.
identityCache
!=
nil
{
if
fp
,
fpErr
:=
s
.
identityCache
.
GetFingerprint
(
reqCtx
,
account
.
ID
);
fpErr
==
nil
&&
fp
!=
nil
&&
strings
.
TrimSpace
(
fp
.
UserAgent
)
!=
""
{
...
...
backend/internal/service/openai_gateway_service_test.go
View file @
d90e56eb
...
...
@@ -1288,90 +1288,6 @@ func TestOpenAIUpdateCodexUsageSnapshotFromHeaders(t *testing.T) {
}
}
func
TestOpenAIResponsesRequestPathSuffix
(
t
*
testing
.
T
)
{
gin
.
SetMode
(
gin
.
TestMode
)
rec
:=
httptest
.
NewRecorder
()
c
,
_
:=
gin
.
CreateTestContext
(
rec
)
tests
:=
[]
struct
{
name
string
path
string
want
string
}{
{
name
:
"exact v1 responses"
,
path
:
"/v1/responses"
,
want
:
""
},
{
name
:
"compact v1 responses"
,
path
:
"/v1/responses/compact"
,
want
:
"/compact"
},
{
name
:
"compact alias responses"
,
path
:
"/responses/compact/"
,
want
:
"/compact"
},
{
name
:
"nested suffix"
,
path
:
"/openai/v1/responses/compact/detail"
,
want
:
"/compact/detail"
},
{
name
:
"unrelated path"
,
path
:
"/v1/chat/completions"
,
want
:
""
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
c
.
Request
=
httptest
.
NewRequest
(
http
.
MethodPost
,
tt
.
path
,
nil
)
require
.
Equal
(
t
,
tt
.
want
,
openAIResponsesRequestPathSuffix
(
c
))
})
}
}
func
TestOpenAIBuildUpstreamRequestOpenAIPassthroughPreservesCompactPath
(
t
*
testing
.
T
)
{
gin
.
SetMode
(
gin
.
TestMode
)
rec
:=
httptest
.
NewRecorder
()
c
,
_
:=
gin
.
CreateTestContext
(
rec
)
c
.
Request
=
httptest
.
NewRequest
(
http
.
MethodPost
,
"/v1/responses/compact"
,
bytes
.
NewReader
([]
byte
(
`{"model":"gpt-5"}`
)))
svc
:=
&
OpenAIGatewayService
{}
account
:=
&
Account
{
Type
:
AccountTypeOAuth
}
req
,
err
:=
svc
.
buildUpstreamRequestOpenAIPassthrough
(
c
.
Request
.
Context
(),
c
,
account
,
[]
byte
(
`{"model":"gpt-5"}`
),
"token"
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
chatgptCodexURL
+
"/compact"
,
req
.
URL
.
String
())
require
.
Equal
(
t
,
"application/json"
,
req
.
Header
.
Get
(
"Accept"
))
require
.
Equal
(
t
,
codexCLIVersion
,
req
.
Header
.
Get
(
"Version"
))
require
.
NotEmpty
(
t
,
req
.
Header
.
Get
(
"Session_Id"
))
}
func
TestOpenAIBuildUpstreamRequestCompactForcesJSONAcceptForOAuth
(
t
*
testing
.
T
)
{
gin
.
SetMode
(
gin
.
TestMode
)
rec
:=
httptest
.
NewRecorder
()
c
,
_
:=
gin
.
CreateTestContext
(
rec
)
c
.
Request
=
httptest
.
NewRequest
(
http
.
MethodPost
,
"/v1/responses/compact"
,
bytes
.
NewReader
([]
byte
(
`{"model":"gpt-5"}`
)))
svc
:=
&
OpenAIGatewayService
{}
account
:=
&
Account
{
Type
:
AccountTypeOAuth
,
Credentials
:
map
[
string
]
any
{
"chatgpt_account_id"
:
"chatgpt-acc"
},
}
req
,
err
:=
svc
.
buildUpstreamRequest
(
c
.
Request
.
Context
(),
c
,
account
,
[]
byte
(
`{"model":"gpt-5"}`
),
"token"
,
false
,
""
,
true
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
chatgptCodexURL
+
"/compact"
,
req
.
URL
.
String
())
require
.
Equal
(
t
,
"application/json"
,
req
.
Header
.
Get
(
"Accept"
))
require
.
Equal
(
t
,
codexCLIVersion
,
req
.
Header
.
Get
(
"Version"
))
require
.
NotEmpty
(
t
,
req
.
Header
.
Get
(
"Session_Id"
))
}
func
TestOpenAIBuildUpstreamRequestPreservesCompactPathForAPIKeyBaseURL
(
t
*
testing
.
T
)
{
gin
.
SetMode
(
gin
.
TestMode
)
rec
:=
httptest
.
NewRecorder
()
c
,
_
:=
gin
.
CreateTestContext
(
rec
)
c
.
Request
=
httptest
.
NewRequest
(
http
.
MethodPost
,
"/responses/compact"
,
bytes
.
NewReader
([]
byte
(
`{"model":"gpt-5"}`
)))
svc
:=
&
OpenAIGatewayService
{
cfg
:
&
config
.
Config
{
Security
:
config
.
SecurityConfig
{
URLAllowlist
:
config
.
URLAllowlistConfig
{
Enabled
:
false
},
},
}}
account
:=
&
Account
{
Type
:
AccountTypeAPIKey
,
Platform
:
PlatformOpenAI
,
Credentials
:
map
[
string
]
any
{
"base_url"
:
"https://example.com/v1"
},
}
req
,
err
:=
svc
.
buildUpstreamRequest
(
c
.
Request
.
Context
(),
c
,
account
,
[]
byte
(
`{"model":"gpt-5"}`
),
"token"
,
false
,
""
,
false
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
"https://example.com/v1/responses/compact"
,
req
.
URL
.
String
())
}
// ==================== P1-08 修复:model 替换性能优化测试 =============
func
TestReplaceModelInSSELine
(
t
*
testing
.
T
)
{
svc
:=
&
OpenAIGatewayService
{}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment