mirror of
https://github.com/tiennm99/goclaw.git
synced 2026-10-11 12:18:59 +00:00
* refactor(codex-pool): remove redundant primary_first strategy * test(tools): update tool schema fixtures to pointer form * feat(create_image): route Codex pools through chain with member failover Codex pool chain entries now iterate pool members per the pool's own strategy (round_robin or priority_order) and fail over internally before the outer chain advances to the next entry. - ChatGPTOAuthRouter.GenerateImage implements NativeImageProvider: iterates orderedProviders, tries each member, advances round-robin state only on success, aggregates errors on pool exhaustion. - media_provider_chain.wrapPoolProvider wraps a *CodexProvider in a router when RoutingDefaults has extras or a non-primary-first strategy. Solo Codex (no extras) stays unwrapped. - Router exposes ProviderType() so chain telemetry records "chatgpt_oauth". Closes #1008 * feat(ui/builtin-tools): pool badge on create_image chain entries Chain entry card shows a read-only "Pool · <strategy>" badge when the entry's provider is a Codex pool base. Strategy label translates in en/vi/zh. No new toggle or selector — pool config lives on the provider itself. #1008 * refactor(providers): move Codex test helpers to providertest subpackage Addresses code-review High finding: NewTestCodexProviderFast and its staticTestTokenSource were exported from a non-_test.go file, compiling into the production binary. Relocating to internal/providers/providertest/ keeps the helper importable from other packages' tests without leaking test-only symbols into production. Also tightens wrapPoolProvider — pools with zero extra members no longer get wrapped in a router (KISS: nothing to rotate between). - Added CodexProvider.WithRetryConfig as a legitimate fluent option (the test helper now uses the public API). - Dropped the internal-package test helper file. #1008 * docs(pr-1006): add pool badge UI evidence Force-UI style capture of the read-only "Pool · Round-robin" badge on the create_image chain entry card when the selected provider carries settings.codex_pool with extras. Captured against staging gateway. * refactor(ui/builtin-tools): unify pool UX with Create Agent pattern The create_image chain Provider dropdown was listing pool members alongside pool owners, letting users accidentally bypass pool semantics by picking a member directly. Matches the pattern already used by Create Agent: hide pool members, show an inline "Pool" chip on owners. - Filter pool members from the chain Provider dropdown via getChatGPTOAuthPoolOwnership.ownerByMember. - Inline "Pool" chip on owner options, reusing the existing providers:list.poolBadge i18n key. - Drop the separate card-level "Pool · <strategy>" badge — the dropdown chip alone conveys the information without duplication. - Remove the now-unused isPoolProvider / poolStrategyOf helpers and the builtin.mediaChain.poolBadge* i18n keys we briefly introduced. #1008 * docs(pr-1006): add pool-filtered dropdown screenshot * fix(ui/builtin-tools): migrate stale pool-member chain entries on load Red-team blocker: a chain saved before the pool-aware UI landed may reference a pool member by name (e.g. openai-codex-2). After the dropdown started hiding members, such an entry produced: - empty Select trigger (no SelectItem matches the stored value) - conflicting Row 1 label still showing the member name - silent runtime misroute (bare solo call, no pool wrap) parseInitialEntries now consults getChatGPTOAuthPoolOwnership and rewrites any chain entry whose provider is a pool member to the owner's name + id. The next save persists the migrated value. Safe no-op for non-pool entries and for entries already pointing at an owner. Also memoize enabledProviders in the parent form so the card's useMemo boundaries actually hold (minor perf ding flagged by same review). * docs(pr-1006): refresh HTML evidence to match filter-based UX * fix(ui/agent-codex-pool): traffic policy reflects effective strategy under inherit On the agent's OpenAI Account Pool page, when Agent routing mode is Use Provider Defaults, the Traffic Policy buttons painted the draft's placeholder strategy ("priority_order") as selected — contradicting the top-of-page chip which already correctly shows the provider's effective strategy. The removal of primary_first in this PR unmasked the latent bug: the placeholder used to be a deprecated value that didn't match any live button, so nothing appeared selected. Derive selectedStrategy from defaultRouting when mode === "inherit": the button highlight now mirrors what actually runs. Buttons remain disabled in inherit mode (unchanged), but the displayed selection no longer misleads the user. * docs(pr-1006): add screenshot of inherit-mode Traffic Policy fix * fix(permissions): remove duplicate MethodSessionsCompact entry The writeExact slice listed MethodSessionsCompact twice. slices.Contains still returned correct results so runtime behavior is unchanged, but the duplicate entry was dead code. Spotted during review of PR #1006. --------- Co-authored-by: viettranx <edu@200lab.io>
239 lines
6.5 KiB
Go
239 lines
6.5 KiB
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/nextlevelbuilder/goclaw/internal/store"
|
|
)
|
|
|
|
func marshalJSONRaw(value any) (json.RawMessage, error) {
|
|
if value == nil {
|
|
return nil, nil
|
|
}
|
|
data, err := json.Marshal(value)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return json.RawMessage(data), nil
|
|
}
|
|
|
|
func normalizedProviderNamesForValidation(names []string) []string {
|
|
if len(names) == 0 {
|
|
return nil
|
|
}
|
|
seen := make(map[string]bool, len(names))
|
|
result := make([]string, 0, len(names))
|
|
for _, name := range names {
|
|
name = strings.TrimSpace(name)
|
|
if name == "" || seen[name] {
|
|
continue
|
|
}
|
|
seen[name] = true
|
|
result = append(result, name)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func validateChatGPTOAuthPoolGraph(providers []store.LLMProviderData) error {
|
|
providersByName := make(map[string]store.LLMProviderData, len(providers))
|
|
for _, provider := range providers {
|
|
providersByName[provider.Name] = provider
|
|
}
|
|
|
|
ownerByMember := map[string]string{}
|
|
hasPoolConfig := map[string]bool{}
|
|
|
|
for _, provider := range providers {
|
|
settings := store.ParseChatGPTOAuthProviderSettings(provider.Settings)
|
|
if settings == nil {
|
|
continue
|
|
}
|
|
if provider.ProviderType != store.ProviderChatGPTOAuth {
|
|
return fmt.Errorf("provider %q must be chatgpt_oauth to manage an OpenAI Codex pool", provider.Name)
|
|
}
|
|
|
|
hasPoolConfig[provider.Name] = true
|
|
for _, memberName := range normalizedProviderNamesForValidation(settings.CodexPool.ExtraProviderNames) {
|
|
if memberName == provider.Name {
|
|
return fmt.Errorf("provider %q cannot include itself in its OpenAI Codex pool", provider.Name)
|
|
}
|
|
|
|
member, ok := providersByName[memberName]
|
|
if !ok {
|
|
// Stale reference — member was deleted or disabled. Skip
|
|
// instead of blocking the pool owner from saving.
|
|
continue
|
|
}
|
|
if member.ProviderType != store.ProviderChatGPTOAuth {
|
|
return fmt.Errorf("provider %q can only add chatgpt_oauth members; %q is %s", provider.Name, memberName, member.ProviderType)
|
|
}
|
|
|
|
if existingOwner, ok := ownerByMember[memberName]; ok && existingOwner != provider.Name {
|
|
return fmt.Errorf("provider %q already belongs to pool %q", memberName, existingOwner)
|
|
}
|
|
ownerByMember[memberName] = provider.Name
|
|
}
|
|
}
|
|
|
|
for ownerName := range hasPoolConfig {
|
|
if existingOwner, ok := ownerByMember[ownerName]; ok {
|
|
return fmt.Errorf("provider %q already belongs to pool %q and cannot manage its own pool", ownerName, existingOwner)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func validateChatGPTOAuthProviderCandidate(
|
|
ctx context.Context,
|
|
providerStore store.ProviderStore,
|
|
currentID uuid.UUID,
|
|
candidate *store.LLMProviderData,
|
|
) error {
|
|
if providerStore == nil || candidate == nil {
|
|
return nil
|
|
}
|
|
|
|
existingProviders, err := providerStore.ListProviders(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Only consider chatgpt_oauth providers for pool graph validation.
|
|
finalProviders := make([]store.LLMProviderData, 0, len(existingProviders)+1)
|
|
replaced := false
|
|
for _, provider := range existingProviders {
|
|
if provider.ProviderType != store.ProviderChatGPTOAuth {
|
|
continue
|
|
}
|
|
// Skip disabled providers — their stale pool configs should not
|
|
// block validation for active providers.
|
|
if !provider.Enabled {
|
|
continue
|
|
}
|
|
if currentID != uuid.Nil && provider.ID == currentID {
|
|
finalProviders = append(finalProviders, *candidate)
|
|
replaced = true
|
|
continue
|
|
}
|
|
finalProviders = append(finalProviders, provider)
|
|
}
|
|
if currentID == uuid.Nil || !replaced {
|
|
finalProviders = append(finalProviders, *candidate)
|
|
}
|
|
|
|
if err := validateChatGPTOAuthPoolGraph(finalProviders); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Strip stale member references from the candidate's settings so they
|
|
// don't accumulate as garbage in the DB after deleted/disabled providers.
|
|
stripStalePoolMembers(candidate, finalProviders)
|
|
return nil
|
|
}
|
|
|
|
// stripStalePoolMembers removes pool member names from the candidate's
|
|
// settings that no longer exist in the active provider set. This prevents
|
|
// deleted/disabled provider names from accumulating as garbage in the DB.
|
|
func stripStalePoolMembers(candidate *store.LLMProviderData, activeProviders []store.LLMProviderData) {
|
|
if candidate == nil || len(candidate.Settings) == 0 {
|
|
return
|
|
}
|
|
|
|
// Parse full settings as generic map to preserve non-pool fields.
|
|
var raw map[string]any
|
|
if json.Unmarshal(candidate.Settings, &raw) != nil {
|
|
return
|
|
}
|
|
cpRaw, ok := raw["codex_pool"]
|
|
if !ok {
|
|
return
|
|
}
|
|
cpMap, ok := cpRaw.(map[string]any)
|
|
if !ok {
|
|
return
|
|
}
|
|
namesRaw, ok := cpMap["extra_provider_names"]
|
|
if !ok {
|
|
return
|
|
}
|
|
namesSlice, ok := namesRaw.([]any)
|
|
if !ok || len(namesSlice) == 0 {
|
|
return
|
|
}
|
|
|
|
known := make(map[string]bool, len(activeProviders))
|
|
for _, p := range activeProviders {
|
|
known[p.Name] = true
|
|
}
|
|
|
|
filtered := make([]any, 0, len(namesSlice))
|
|
for _, n := range namesSlice {
|
|
if name, ok := n.(string); ok && known[name] {
|
|
filtered = append(filtered, name)
|
|
}
|
|
}
|
|
|
|
if len(filtered) == len(namesSlice) {
|
|
return // nothing changed
|
|
}
|
|
|
|
cpMap["extra_provider_names"] = filtered
|
|
if updated, err := json.Marshal(raw); err == nil {
|
|
candidate.Settings = updated
|
|
}
|
|
}
|
|
|
|
func validateChatGPTOAuthAgentRouting(
|
|
ctx context.Context,
|
|
providerStore store.ProviderStore,
|
|
providerName string,
|
|
routing *store.ChatGPTOAuthRoutingConfig,
|
|
) error {
|
|
if providerStore == nil || providerName == "" || routing == nil {
|
|
return nil
|
|
}
|
|
|
|
tenantID := store.TenantIDFromContext(ctx)
|
|
baseProvider, err := lookupProviderByNameWithMasterFallback(ctx, providerStore, tenantID, providerName)
|
|
if err != nil || baseProvider == nil || baseProvider.ProviderType != store.ProviderChatGPTOAuth {
|
|
return nil
|
|
}
|
|
|
|
defaultSettings := store.ParseChatGPTOAuthProviderSettings(baseProvider.Settings)
|
|
defaultMembers := []string{}
|
|
if defaultSettings != nil {
|
|
defaultMembers = normalizedProviderNamesForValidation(defaultSettings.CodexPool.ExtraProviderNames)
|
|
}
|
|
|
|
if len(defaultMembers) == 0 {
|
|
if len(routing.ExtraProviderNames) > 0 {
|
|
return fmt.Errorf("configure OpenAI Codex pool members on provider %q before enabling agent-level routing", providerName)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
if routing.OverrideMode == store.ChatGPTOAuthOverrideInherit {
|
|
return nil
|
|
}
|
|
|
|
if len(routing.ExtraProviderNames) == 0 {
|
|
return nil
|
|
}
|
|
|
|
if !slices.Equal(
|
|
normalizedProviderNamesForValidation(routing.ExtraProviderNames),
|
|
defaultMembers,
|
|
) {
|
|
return fmt.Errorf("agent routing cannot change pool membership for provider %q; manage members on the provider instead", providerName)
|
|
}
|
|
|
|
return nil
|
|
}
|