Align OAuth transform with OpenCode instructions
This commit is contained in:
@@ -13,11 +13,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
codexReleaseAPIURL = "https://api.github.com/repos/openai/codex/releases/latest"
|
codexReleaseAPIURL = "https://api.github.com/repos/openai/codex/releases/latest"
|
||||||
codexReleaseHTMLURL = "https://github.com/openai/codex/releases/latest"
|
codexReleaseHTMLURL = "https://github.com/openai/codex/releases/latest"
|
||||||
codexPromptURLFmt = "https://raw.githubusercontent.com/openai/codex/%s/codex-rs/core/%s"
|
codexPromptURLFmt = "https://raw.githubusercontent.com/openai/codex/%s/codex-rs/core/%s"
|
||||||
opencodeCodexURL = "https://raw.githubusercontent.com/anomalyco/opencode/dev/packages/opencode/src/session/prompt/codex.txt"
|
opencodeCodexURL = "https://raw.githubusercontent.com/anomalyco/opencode/dev/packages/opencode/src/session/prompt/codex.txt"
|
||||||
codexCacheTTL = 15 * time.Minute
|
opencodeCodexHeaderURL = "https://raw.githubusercontent.com/anomalyco/opencode/dev/packages/opencode/src/session/prompt/codex_header.txt"
|
||||||
|
codexCacheTTL = 15 * time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
type codexModelFamily string
|
type codexModelFamily string
|
||||||
@@ -177,7 +178,7 @@ func applyCodexOAuthTransform(reqBody map[string]any) codexTransformResult {
|
|||||||
result.PromptCacheKey = strings.TrimSpace(v)
|
result.PromptCacheKey = strings.TrimSpace(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
instructions := strings.TrimSpace(getCodexInstructions(normalizedModel))
|
instructions := strings.TrimSpace(getOpenCodeCodexHeader())
|
||||||
existingInstructions, _ := reqBody["instructions"].(string)
|
existingInstructions, _ := reqBody["instructions"].(string)
|
||||||
existingInstructions = strings.TrimSpace(existingInstructions)
|
existingInstructions = strings.TrimSpace(existingInstructions)
|
||||||
|
|
||||||
@@ -408,13 +409,13 @@ func parseReleaseTagFromHTML(html string) (string, error) {
|
|||||||
return "", fmt.Errorf("release tag not found")
|
return "", fmt.Errorf("release tag not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
func getOpenCodeCodexPrompt() string {
|
func getOpenCodeCachedPrompt(url, cacheFileName, metaFileName string) string {
|
||||||
cacheDir := codexCachePath("")
|
cacheDir := codexCachePath("")
|
||||||
if cacheDir == "" {
|
if cacheDir == "" {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
cacheFile := filepath.Join(cacheDir, "opencode-codex.txt")
|
cacheFile := filepath.Join(cacheDir, cacheFileName)
|
||||||
metaFile := filepath.Join(cacheDir, "opencode-codex-meta.json")
|
metaFile := filepath.Join(cacheDir, metaFileName)
|
||||||
|
|
||||||
var cachedContent string
|
var cachedContent string
|
||||||
if content, ok := readFile(cacheFile); ok {
|
if content, ok := readFile(cacheFile); ok {
|
||||||
@@ -428,7 +429,7 @@ func getOpenCodeCodexPrompt() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
content, etag, status, err := fetchWithETag(opencodeCodexURL, meta.ETag)
|
content, etag, status, err := fetchWithETag(url, meta.ETag)
|
||||||
if err == nil && status == http.StatusNotModified && cachedContent != "" {
|
if err == nil && status == http.StatusNotModified && cachedContent != "" {
|
||||||
return cachedContent
|
return cachedContent
|
||||||
}
|
}
|
||||||
@@ -446,6 +447,14 @@ func getOpenCodeCodexPrompt() string {
|
|||||||
return cachedContent
|
return cachedContent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getOpenCodeCodexPrompt() string {
|
||||||
|
return getOpenCodeCachedPrompt(opencodeCodexURL, "opencode-codex.txt", "opencode-codex-meta.json")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getOpenCodeCodexHeader() string {
|
||||||
|
return getOpenCodeCachedPrompt(opencodeCodexHeaderURL, "opencode-codex-header.txt", "opencode-codex-header-meta.json")
|
||||||
|
}
|
||||||
|
|
||||||
func filterCodexInput(input []any) []any {
|
func filterCodexInput(input []any) []any {
|
||||||
filtered := make([]any, 0, len(input))
|
filtered := make([]any, 0, len(input))
|
||||||
for _, item := range input {
|
for _, item := range input {
|
||||||
|
|||||||
@@ -580,7 +580,7 @@ func (s *OpenAIGatewayService) Forward(ctx context.Context, c *gin.Context, acco
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Build upstream request
|
// Build upstream request
|
||||||
upstreamReq, err := s.buildUpstreamRequest(ctx, c, account, body, token, reqStream, promptCacheKey)
|
upstreamReq, err := s.buildUpstreamRequest(ctx, c, account, body, token, reqStream, promptCacheKey, isCodexCLI)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -641,7 +641,7 @@ func (s *OpenAIGatewayService) Forward(ctx context.Context, c *gin.Context, acco
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *OpenAIGatewayService) buildUpstreamRequest(ctx context.Context, c *gin.Context, account *Account, body []byte, token string, isStream bool, promptCacheKey string) (*http.Request, error) {
|
func (s *OpenAIGatewayService) buildUpstreamRequest(ctx context.Context, c *gin.Context, account *Account, body []byte, token string, isStream bool, promptCacheKey string, isCodexCLI bool) (*http.Request, error) {
|
||||||
// Determine target URL based on account type
|
// Determine target URL based on account type
|
||||||
var targetURL string
|
var targetURL string
|
||||||
switch account.Type {
|
switch account.Type {
|
||||||
@@ -694,7 +694,11 @@ func (s *OpenAIGatewayService) buildUpstreamRequest(ctx context.Context, c *gin.
|
|||||||
}
|
}
|
||||||
if account.Type == AccountTypeOAuth {
|
if account.Type == AccountTypeOAuth {
|
||||||
req.Header.Set("OpenAI-Beta", "responses=experimental")
|
req.Header.Set("OpenAI-Beta", "responses=experimental")
|
||||||
req.Header.Set("originator", "codex_cli_rs")
|
if isCodexCLI {
|
||||||
|
req.Header.Set("originator", "codex_cli_rs")
|
||||||
|
} else {
|
||||||
|
req.Header.Set("originator", "opencode")
|
||||||
|
}
|
||||||
req.Header.Set("accept", "text/event-stream")
|
req.Header.Set("accept", "text/event-stream")
|
||||||
if promptCacheKey != "" {
|
if promptCacheKey != "" {
|
||||||
req.Header.Set("conversation_id", promptCacheKey)
|
req.Header.Set("conversation_id", promptCacheKey)
|
||||||
|
|||||||
@@ -220,7 +220,7 @@ func TestOpenAIInvalidBaseURLWhenAllowlistDisabled(t *testing.T) {
|
|||||||
Credentials: map[string]any{"base_url": "://invalid-url"},
|
Credentials: map[string]any{"base_url": "://invalid-url"},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := svc.buildUpstreamRequest(c.Request.Context(), c, account, []byte("{}"), "token", false, "")
|
_, err := svc.buildUpstreamRequest(c.Request.Context(), c, account, []byte("{}"), "token", false, "", false)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expected error for invalid base_url when allowlist disabled")
|
t.Fatalf("expected error for invalid base_url when allowlist disabled")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user