Repository navigation
feat(channel): 支持创建后切换密钥存储模式 #7196
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
Omnls
wants to merge
4
commits into
QuantumNous:main
Choose a base branch
from
Omnls:codex/issue-7115-key-storage-mode
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 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,106 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/QuantumNous/new-api/constant" | ||
| "github.com/QuantumNous/new-api/model" | ||
| "github.com/QuantumNous/new-api/relaykit/dto" | ||
| ) | ||
|
|
||
| const ( | ||
| keyStorageModeSingle = "single" | ||
| keyStorageModeMulti = "multi" | ||
| ) | ||
|
|
||
| func channelSupportsMultiKey(channel *model.Channel) bool { | ||
| if channel.Type == constant.ChannelTypeCodex { | ||
| return false | ||
| } | ||
| if channel.Type == constant.ChannelTypeVertexAi && channel.GetOtherSettings().VertexKeyType == dto.VertexKeyTypeAPIKey { | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func parseUpdateKeyList(channel *model.Channel, key string) ([]string, error) { | ||
| if channel.Type == constant.ChannelTypeVertexAi && channel.GetOtherSettings().VertexKeyType != dto.VertexKeyTypeAPIKey { | ||
| trimmed := strings.TrimSpace(key) | ||
| if strings.HasPrefix(trimmed, "[") { | ||
| return getVertexArrayKeys(key) | ||
| } | ||
| if trimmed == "" { | ||
| return nil, fmt.Errorf("密钥不能为空") | ||
| } | ||
| return []string{trimmed}, nil | ||
| } | ||
| keys := make([]string, 0) | ||
| for _, line := range strings.Split(key, "\n") { | ||
| line = strings.TrimSpace(line) | ||
| if line == "" { | ||
| continue | ||
| } | ||
| keys = append(keys, line) | ||
| } | ||
| return keys, nil | ||
| } | ||
|
|
||
| func applyKeyStorageMode(channel *PatchChannel, origin *model.Channel, keyConfig *model.Channel) error { | ||
| if channel.KeyStorageMode == nil { | ||
| return nil | ||
| } | ||
| if keyConfig == nil { | ||
| keyConfig = origin | ||
| } | ||
| mode := strings.TrimSpace(*channel.KeyStorageMode) | ||
| if mode == "" { | ||
| return nil | ||
| } | ||
| if mode != keyStorageModeSingle && mode != keyStorageModeMulti { | ||
| return fmt.Errorf("不支持的密钥存储模式") | ||
| } | ||
| if strings.TrimSpace(channel.Key) == "" { | ||
| return fmt.Errorf("转换密钥模式必须提供新的密钥") | ||
| } | ||
| keys, err := parseUpdateKeyList(keyConfig, channel.Key) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| switch mode { | ||
| case keyStorageModeSingle: | ||
| if !origin.ChannelInfo.IsMultiKey { | ||
| return fmt.Errorf("渠道已经是单密钥模式") | ||
| } | ||
| if len(keys) != 1 { | ||
| return fmt.Errorf("转换为单密钥时必须只提供一把新密钥") | ||
| } | ||
| channel.Key = keys[0] | ||
| channel.ChannelInfo = model.ChannelInfo{} | ||
| return nil | ||
| default: | ||
| if origin.ChannelInfo.IsMultiKey { | ||
| return fmt.Errorf("渠道已经是多密钥模式") | ||
| } | ||
| if !channelSupportsMultiKey(keyConfig) { | ||
| return fmt.Errorf("当前渠道类型不支持多密钥模式") | ||
| } | ||
| if len(keys) < 2 { | ||
| return fmt.Errorf("转换为多密钥时至少需要两把密钥") | ||
| } | ||
| rotation := constant.MultiKeyModeRandom | ||
| if channel.MultiKeyMode != nil { | ||
| requested := constant.MultiKeyMode(strings.TrimSpace(*channel.MultiKeyMode)) | ||
| if requested == constant.MultiKeyModeRandom || requested == constant.MultiKeyModePolling { | ||
| rotation = requested | ||
| } | ||
| } | ||
| channel.Key = strings.Join(keys, "\n") | ||
| channel.ChannelInfo = model.ChannelInfo{ | ||
| IsMultiKey: true, | ||
| MultiKeySize: len(keys), | ||
| MultiKeyMode: rotation, | ||
| } | ||
| return nil | ||
| } | ||
| } |
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.