-
Notifications
You must be signed in to change notification settings - Fork 11.2k
feat: add combo routing #5443
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
androw
wants to merge
3
commits into
QuantumNous:main
Choose a base branch
from
androw:combo
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
feat: add combo routing #5443
Changes from all commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/QuantumNous/new-api/common" | ||
| "github.com/QuantumNous/new-api/model" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // CreateCombo handles POST /api/combo/ | ||
| func CreateCombo(c *gin.Context) { | ||
| var combo model.Combo | ||
| if err := c.ShouldBindJSON(&combo); err != nil { | ||
| common.ApiErrorMsg(c, fmt.Sprintf("Invalid request: %s", err.Error())) | ||
| return | ||
| } | ||
|
|
||
| // Validate required fields | ||
| if combo.Name == "" { | ||
| common.ApiErrorMsg(c, "Combo name is required") | ||
| return | ||
| } | ||
| if combo.Models == "" { | ||
| common.ApiErrorMsg(c, "At least one model is required") | ||
| return | ||
| } | ||
| if combo.Strategy == "" { | ||
| combo.Strategy = "fallback" | ||
| } | ||
| if combo.Strategy != "fallback" && combo.Strategy != "random" && combo.Strategy != "weighted" && combo.Strategy != "round_robin" { | ||
| common.ApiErrorMsg(c, "Strategy must be one of: fallback, random, weighted, round_robin") | ||
| return | ||
| } | ||
| userId := c.GetInt("id") | ||
| combo.UserId = userId | ||
| combo.Status = 1 | ||
| combo.CreatedTime = time.Now().Unix() | ||
|
|
||
| // Check name uniqueness scoped to this user. | ||
| if existing, _ := model.GetComboByNameUserId(combo.Name, userId); existing != nil { | ||
| common.ApiErrorMsg(c, "Combo name already exists") | ||
| return | ||
| } | ||
|
|
||
| if err := combo.Insert(); err != nil { | ||
| common.ApiErrorMsg(c, fmt.Sprintf("Failed to create combo: %s", err.Error())) | ||
| return | ||
| } | ||
|
|
||
| common.ApiSuccess(c, combo) | ||
| } | ||
|
|
||
| // GetComboList handles GET /api/combo/ | ||
| func GetComboList(c *gin.Context) { | ||
| userId := c.GetInt("id") | ||
| role := c.GetInt("role") | ||
| pageInfo := common.GetPageQuery(c) | ||
|
|
||
| var combos []*model.Combo | ||
| var total int64 | ||
| var err error | ||
|
|
||
| if role >= common.RoleAdminUser { | ||
| // Admins see all combos | ||
| combos, total, err = model.GetAllCombos(pageInfo) | ||
| } else { | ||
| combos, err = model.GetCombosByUserId(userId) | ||
| if err == nil { | ||
| total = int64(len(combos)) | ||
| } | ||
| } | ||
|
|
||
| if err != nil { | ||
| common.ApiErrorMsg(c, fmt.Sprintf("Failed to get combos: %s", err.Error())) | ||
| return | ||
| } | ||
|
|
||
| if combos == nil { | ||
| combos = []*model.Combo{} | ||
| } | ||
|
|
||
| common.ApiSuccess(c, gin.H{ | ||
| "items": combos, | ||
| "total": total, | ||
| "page": pageInfo.GetPage(), | ||
| "page_size": pageInfo.GetPageSize(), | ||
| }) | ||
| } | ||
|
|
||
| // GetCombo handles GET /api/combo/:id | ||
| func GetCombo(c *gin.Context) { | ||
| idStr := c.Param("id") | ||
| id, err := strconv.Atoi(idStr) | ||
| if err != nil { | ||
| common.ApiErrorMsg(c, "Invalid combo id") | ||
| return | ||
| } | ||
|
|
||
| combo, err := model.GetComboById(id) | ||
| if err != nil { | ||
| common.ApiErrorMsg(c, "Combo not found") | ||
| return | ||
| } | ||
|
|
||
| // Ownership check: admin or owner | ||
| userId := c.GetInt("id") | ||
| role := c.GetInt("role") | ||
| if role < common.RoleAdminUser && combo.UserId != userId { | ||
| common.ApiErrorMsg(c, "Combo not found") | ||
| return | ||
| } | ||
|
|
||
| common.ApiSuccess(c, combo) | ||
| } | ||
|
|
||
| // UpdateCombo handles PUT /api/combo/:id | ||
| func UpdateCombo(c *gin.Context) { | ||
| idStr := c.Param("id") | ||
| id, err := strconv.Atoi(idStr) | ||
| if err != nil { | ||
| common.ApiErrorMsg(c, "Invalid combo id") | ||
| return | ||
| } | ||
|
|
||
| combo, err := model.GetComboById(id) | ||
| if err != nil { | ||
| common.ApiErrorMsg(c, "Combo not found") | ||
| return | ||
| } | ||
|
|
||
| // Ownership check | ||
| userId := c.GetInt("id") | ||
| role := c.GetInt("role") | ||
| if role < common.RoleAdminUser && combo.UserId != userId { | ||
| common.ApiErrorMsg(c, "Combo not found") | ||
| return | ||
| } | ||
|
|
||
| var updateData model.Combo | ||
| if err := c.ShouldBindJSON(&updateData); err != nil { | ||
| common.ApiErrorMsg(c, fmt.Sprintf("Invalid request: %s", err.Error())) | ||
| return | ||
| } | ||
|
|
||
| if updateData.Name != "" { | ||
| // Check name uniqueness scoped to this user. | ||
| if existing, _ := model.GetComboByNameUserId(updateData.Name, combo.UserId); existing != nil && existing.Id != combo.Id { | ||
| common.ApiErrorMsg(c, "Combo name already exists") | ||
| return | ||
| } | ||
| combo.Name = updateData.Name | ||
| } | ||
|
|
||
| if updateData.Status >= 0 && updateData.Status <= 1 { | ||
| combo.Status = updateData.Status | ||
| } | ||
|
|
||
| if err := combo.Update(); err != nil { | ||
| common.ApiErrorMsg(c, fmt.Sprintf("Failed to update combo: %s", err.Error())) | ||
| return | ||
| } | ||
|
|
||
| common.ApiSuccess(c, combo) | ||
| } | ||
|
|
||
| // DeleteCombo handles DELETE /api/combo/:id | ||
| func DeleteCombo(c *gin.Context) { | ||
| idStr := c.Param("id") | ||
| id, err := strconv.Atoi(idStr) | ||
| if err != nil { | ||
| common.ApiErrorMsg(c, "Invalid combo id") | ||
| return | ||
| } | ||
|
|
||
| userId := c.GetInt("id") | ||
| role := c.GetInt("role") | ||
|
|
||
| var delErr error | ||
| if role >= common.RoleAdminUser { | ||
| delErr = model.DeleteComboById(id) | ||
| } else { | ||
| delErr = model.DeleteComboById(id, userId) | ||
| } | ||
|
|
||
| if delErr != nil { | ||
| common.ApiErrorMsg(c, fmt.Sprintf("Failed to delete combo: %s", delErr.Error())) | ||
| return | ||
| } | ||
|
|
||
| common.ApiSuccess(c, gin.H{"id": id}) | ||
| } | ||
|
|
||
| // SearchCombos handles GET /api/combo/search | ||
| func SearchCombos(c *gin.Context) { | ||
| keyword := c.Query("keyword") | ||
| if keyword == "" { | ||
| common.ApiErrorMsg(c, "Search keyword is required") | ||
| return | ||
| } | ||
|
|
||
| pageInfo := common.GetPageQuery(c) | ||
| combos, total, err := model.SearchCombos(keyword, pageInfo) | ||
| if err != nil { | ||
| common.ApiErrorMsg(c, fmt.Sprintf("Failed to search combos: %s", err.Error())) | ||
| return | ||
| } | ||
|
|
||
| if combos == nil { | ||
| combos = []*model.Combo{} | ||
| } | ||
|
|
||
| common.ApiSuccess(c, gin.H{ | ||
| "items": combos, | ||
| "total": total, | ||
| "page": pageInfo.GetPage(), | ||
| "page_size": pageInfo.GetPageSize(), | ||
| }) | ||
| } |
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,61 @@ | ||
| # Combo Feature — Multi-Model Routing | ||
|
|
||
| > **Date**: 2026-06-11 | ||
| > **Status**: Phase 1 complete (backend CRUD) | ||
|
|
||
| ## Concept | ||
|
|
||
| A **Combo** bundles multiple models with a routing strategy into a named configuration. | ||
| Users send requests to a combo (via `model: "combo:my-combo"`), and the system resolves | ||
| which model(s) to use based on the strategy, then routes through the existing channel | ||
| selection layer. | ||
|
|
||
| --- | ||
|
|
||
| ## Data Model | ||
|
|
||
| | Field | Type | Description | | ||
| |---|---|---| | ||
| | `id` | `int` (PK, auto-increment) | | | ||
| | `name` | `varchar(128)` unique | Combo identifier, used as `combo:<name>` in requests | | ||
| | `user_id` | `int` | Creator / owner | | ||
| | `models` | `text` | CSV — `"gpt-4,claude-3,gemini-pro"` | | ||
| | `strategy` | `varchar(32)` | `fallback` / `random` / `weighted` / `round_robin` | | ||
| | `weights` | `text` | JSON map for weighted: `{"gpt-4":3,"claude-3":2}` | | ||
| | `status` | `int` (0/1) | 1 = enabled | | ||
| | `created_time` | `bigint` | Unix timestamp | | ||
|
|
||
| --- | ||
|
|
||
| ## Routing Strategies | ||
|
|
||
| | Strategy | Behaviour | | ||
| |---|---| | ||
| | `fallback` | Iterate models in order → pick first that has an available channel | | ||
| | `random` | Uniform random selection from the model list | | ||
| | `weighted` | Weighted random using `weights` JSON | | ||
| | `round_robin` | Atomic counter → cycling through models evenly | | ||
|
|
||
| --- | ||
|
|
||
| ## Phases | ||
|
|
||
| - **Phase 1** ✅ — Backend CRUD + DB migration | ||
| - **Phase 2** ✅ — Routing integration in `middleware/distributor.go` | ||
| - **Phase 3** ✅ — Frontend management UI | ||
| - **Phase 4** — Advanced (parallel, billing, sharing) | ||
|
|
||
| --- | ||
|
|
||
| ## Key Files | ||
|
|
||
| | Layer | File | | ||
| |---|---| | ||
| | Model | `model/combo.go` | | ||
| | Migration | `model/main.go` | | ||
| | Controller | `controller/combo.go` | | ||
| | Routes | `router/api-router.go` | | ||
| | Service | `service/combo_routing.go` | | ||
| | Context keys | `constant/context_key.go` | | ||
| | Frontend feature | `web/default/src/features/combos/` | | ||
| | i18n | `web/default/src/i18n/locales/*.json` | |
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
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.