Repository navigation
feat(provider): 接入 prompt 缓存(Anthropic 断点 + OpenAI 路由键) #732
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
Open
Cai-Tang-www
wants to merge
8
commits into
1024XEngineer:main
Choose a base branch
from
Cai-Tang-www:feat/prompt-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f9e8424
feat(provider): 接入 prompt 缓存并统一缓存用量口径
Cai-Tang-www 9ac0cb1
fix(context): 将 memo 索引移出稳定提示段
Cai-Tang-www 1280c56
docs(config): 补充 prompt_cache 配置与缓存事件字段
Cai-Tang-www 5ac1f12
fix(config): 打通 prompt_cache 的 provider 文件读写路径
Cai-Tang-www 63c0455
fix(provider): 按 driver 区分缓存默认值并收敛超额命中数
Cai-Tang-www 4afdd76
feat(provider): responses 模式同样发送缓存路由键
Cai-Tang-www 8c99cb9
test(runtime): 覆盖缓存 usage 穿过生成与对账链路
Cai-Tang-www 5cd5b3d
docs(config): 修正 prompt_cache 默认值与 responses 模式说明
Cai-Tang-www File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "neo-code/internal/provider" | ||
| ) | ||
|
|
||
| func TestValidateOptionalPromptCache(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| valid := []string{"", "auto", "off", " AUTO ", "Off"} | ||
| for _, value := range valid { | ||
| if err := validateOptionalPromptCache(value); err != nil { | ||
| t.Fatalf("validateOptionalPromptCache(%q) unexpected error: %v", value, err) | ||
| } | ||
| } | ||
|
|
||
| if err := validateOptionalPromptCache("always"); err == nil { | ||
| t.Fatal("expected error for unsupported prompt_cache value") | ||
| } else if !strings.Contains(err.Error(), provider.PromptCacheModeAuto) { | ||
| t.Fatalf("error should mention accepted values, got %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestProviderConfigPromptCacheReachesRuntimeConfig(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| resolved := ResolvedProviderConfig{ | ||
| ProviderConfig: ProviderConfig{ | ||
| Name: "cached-provider", | ||
| Driver: "openaicompat", | ||
| BaseURL: "https://llm.example.com/v1", | ||
| Model: "server-default", | ||
| APIKeyEnv: "CACHED_PROVIDER_KEY", | ||
| PromptCache: "off", | ||
| }, | ||
| } | ||
|
|
||
| runtimeCfg, err := resolved.ToRuntimeConfig() | ||
| if err != nil { | ||
| t.Fatalf("ToRuntimeConfig() error = %v", err) | ||
| } | ||
| if runtimeCfg.PromptCacheMode != provider.PromptCacheModeOff { | ||
| t.Fatalf("PromptCacheMode = %q, want %q", runtimeCfg.PromptCacheMode, provider.PromptCacheModeOff) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "neo-code/internal/provider" | ||
| ) | ||
|
|
||
| func TestCustomProviderPromptCacheRoundTrip(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| baseDir := t.TempDir() | ||
| const providerName = "cached-gateway" | ||
| if err := SaveCustomProviderWithModels(baseDir, SaveCustomProviderInput{ | ||
| Name: providerName, | ||
| Driver: provider.DriverOpenAICompat, | ||
| BaseURL: "https://llm.example.com/v1", | ||
| APIKeyEnv: "CACHED_GATEWAY_KEY", | ||
| ModelSource: ModelSourceDiscover, | ||
| DiscoveryEndpointPath: provider.DiscoveryEndpointPathModels, | ||
| PromptCache: "OFF", | ||
| }); err != nil { | ||
| t.Fatalf("SaveCustomProviderWithModels() error = %v", err) | ||
| } | ||
|
|
||
| cfg, err := loadCustomProvider(filepath.Join(baseDir, providersDirName, providerName)) | ||
| if err != nil { | ||
| t.Fatalf("loadCustomProvider() error = %v", err) | ||
| } | ||
| if cfg.PromptCache != provider.PromptCacheModeOff { | ||
| t.Fatalf("PromptCache = %q, want %q after round trip", cfg.PromptCache, provider.PromptCacheModeOff) | ||
| } | ||
|
|
||
| runtimeCfg, err := cfg.Resolve() | ||
| if err != nil { | ||
| t.Fatalf("Resolve() error = %v", err) | ||
| } | ||
| providerCfg, err := runtimeCfg.ToRuntimeConfig() | ||
| if err != nil { | ||
| t.Fatalf("ToRuntimeConfig() error = %v", err) | ||
| } | ||
| if providerCfg.PromptCacheMode != provider.PromptCacheModeOff { | ||
| t.Fatalf("PromptCacheMode = %q, want %q", providerCfg.PromptCacheMode, provider.PromptCacheModeOff) | ||
| } | ||
| } | ||
|
|
||
| func TestCustomProviderFileAcceptsPromptCacheKey(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| loader := NewLoader(t.TempDir(), testDefaultConfig()) | ||
| customDir := filepath.Join(loader.BaseDir(), providersDirName, "company-gateway") | ||
| if err := os.MkdirAll(customDir, 0o755); err != nil { | ||
| t.Fatalf("mkdir custom provider dir: %v", err) | ||
| } | ||
|
|
||
| // 该键曾被 KnownFields(true) 拒绝:结构体缺少 prompt_cache 字段时,按文档写配置会直接加载失败。 | ||
| providerYAML := ` | ||
| name: company-gateway | ||
| driver: openaicompat | ||
| base_url: https://llm.example.com/v1 | ||
| api_key_env: COMPANY_GATEWAY_API_KEY | ||
| discovery_endpoint_path: /models | ||
| prompt_cache: off | ||
| ` | ||
| if err := os.WriteFile(filepath.Join(customDir, customProviderConfigName), []byte(strings.TrimSpace(providerYAML)+"\n"), 0o644); err != nil { | ||
| t.Fatalf("write provider.yaml: %v", err) | ||
| } | ||
|
|
||
| cfg, err := loader.Load(context.Background()) | ||
| if err != nil { | ||
| t.Fatalf("Load() error = %v", err) | ||
| } | ||
| found := false | ||
| for _, p := range cfg.Providers { | ||
| if p.Name == "company-gateway" { | ||
| found = true | ||
| if p.PromptCache != provider.PromptCacheModeOff { | ||
| t.Fatalf("PromptCache = %q, want off", p.PromptCache) | ||
| } | ||
| } | ||
| } | ||
| if !found { | ||
| t.Fatal("custom provider company-gateway not loaded") | ||
| } | ||
| } | ||
|
|
||
| func TestCustomProviderFileRejectsInvalidPromptCache(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| loader := NewLoader(t.TempDir(), testDefaultConfig()) | ||
| customDir := filepath.Join(loader.BaseDir(), providersDirName, "bad-cache-gateway") | ||
| if err := os.MkdirAll(customDir, 0o755); err != nil { | ||
| t.Fatalf("mkdir custom provider dir: %v", err) | ||
| } | ||
|
|
||
| providerYAML := ` | ||
| name: bad-cache-gateway | ||
| driver: openaicompat | ||
| base_url: https://llm.example.com/v1 | ||
| api_key_env: BAD_CACHE_GATEWAY_KEY | ||
| discovery_endpoint_path: /models | ||
| prompt_cache: always | ||
| ` | ||
| if err := os.WriteFile(filepath.Join(customDir, customProviderConfigName), []byte(strings.TrimSpace(providerYAML)+"\n"), 0o644); err != nil { | ||
| t.Fatalf("write provider.yaml: %v", err) | ||
| } | ||
|
|
||
| _, err := loader.Load(context.Background()) | ||
| if err == nil { | ||
| t.Fatal("expected load to reject unsupported prompt_cache value") | ||
| } | ||
| if !strings.Contains(err.Error(), "prompt_cache") { | ||
| t.Fatalf("error should mention prompt_cache, got %v", err) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.