aac8538372
build / build (push) Successful in 1m48s
- 迁移 000084 保存对内/对外绑定名称快照;清除密钥后仍显示已绑定库,不再只显示裸 ID - 测试/生产换绑只重指向同一侧的旧目标,避免测试保存误改另一环境的待入库 - 已保存连接但没有可用密钥时拒绝改绑定,禁止静默回落文件 Key - 生产换绑返回重指向/退休数量,页面提示与测试侧一致 验证:make verify(含隔离 PostgreSQL 集成用例)15/15 通过
231 lines
9.4 KiB
Go
231 lines
9.4 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
postgresstore "knowledge-governance/internal/platform/postgres"
|
|
"knowledge-governance/internal/platform/weknora"
|
|
)
|
|
|
|
// RoutingBindingStore persists the UI-managed knowledge base bindings
|
|
// (migration 000034). Empty stored values fall back to env defaults.
|
|
type RoutingBindingStore interface {
|
|
LoadRoutingKBBindings(context.Context) (string, string, string, error)
|
|
LoadRoutingKBBindingNames(context.Context) (string, string, error)
|
|
// SaveRoutingKBBindingsWithReplan also re-points 待入库 publications that
|
|
// were still waiting for the previous knowledge base (same transaction).
|
|
SaveRoutingKBBindingsWithReplan(ctx context.Context, save postgresstore.RoutingBindingSave, previous, effective postgresstore.EffectiveKBIDs, now time.Time) (postgresstore.RoutingBindingReplanResult, error)
|
|
}
|
|
|
|
// KnowledgeBaseLister enumerates candidate knowledge bases in the connected
|
|
// WeKnora so the settings page offers a picker instead of raw UUIDs.
|
|
type KnowledgeBaseLister interface {
|
|
ListKnowledgeBases(context.Context) ([]weknora.KnowledgeBase, error)
|
|
}
|
|
|
|
type routingBindingView struct {
|
|
InternalKBID string `json:"internal_kb_id"`
|
|
ExternalKBID string `json:"external_kb_id"`
|
|
InternalEnvFallback bool `json:"internal_env_fallback"`
|
|
ExternalEnvFallback bool `json:"external_env_fallback"`
|
|
// 取消绑定时必须说清「回退到了哪个库」,否则「留空」等于没说。
|
|
InternalFallbackKBID string `json:"internal_fallback_kb_id"`
|
|
ExternalFallbackKBID string `json:"external_fallback_kb_id"`
|
|
InternalBoundByUI bool `json:"internal_bound_by_ui"`
|
|
ExternalBoundByUI bool `json:"external_bound_by_ui"`
|
|
InternalKBName string `json:"internal_kb_name"`
|
|
ExternalKBName string `json:"external_kb_name"`
|
|
UpdatedBy string `json:"updated_by"`
|
|
WeKnoraConnected bool `json:"weknora_connected"`
|
|
// Replanned / RetiredDuplicates describe what THIS save did to 待入库 items
|
|
// that had not been uploaded yet. A plain read reports zeros.
|
|
Replanned int `json:"replanned"`
|
|
RetiredDuplicates int `json:"retired_duplicates"`
|
|
}
|
|
|
|
type routingKnowledgeBaseOption struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
KnowledgeCount int64 `json:"knowledge_count"`
|
|
}
|
|
|
|
// effectiveRoutingTargets resolves (internal, external) WeKnora knowledge
|
|
// base ids for this request: UI bindings win, environment defaults fill the
|
|
// unbound side. Load failures behave as "unbound" (fail toward env, never
|
|
// toward a half-configured new target).
|
|
func (config Config) effectiveRoutingTargets(ctx context.Context) (string, string) {
|
|
internal, external := config.InternalRoutingKnowledgeBaseID, config.ExternalRoutingKnowledgeBaseID
|
|
if config.RoutingBindings == nil {
|
|
return internal, external
|
|
}
|
|
storedInternal, storedExternal, _, err := config.RoutingBindings.LoadRoutingKBBindings(ctx)
|
|
if err != nil {
|
|
return internal, external
|
|
}
|
|
if storedInternal != "" {
|
|
internal = storedInternal
|
|
}
|
|
if storedExternal != "" {
|
|
external = storedExternal
|
|
}
|
|
return internal, external
|
|
}
|
|
|
|
func (config Config) routingBindingNames(ctx context.Context) (map[string]string, bool) {
|
|
if config.KnowledgeBases == nil {
|
|
return nil, false
|
|
}
|
|
bases, err := config.KnowledgeBases.ListKnowledgeBases(ctx)
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
names := make(map[string]string, len(bases))
|
|
for _, base := range bases {
|
|
names[base.ID] = base.Name
|
|
}
|
|
return names, true
|
|
}
|
|
|
|
// routingBindingPayload builds the settings-page projection. replan carries the
|
|
// counts of a binding change that just ran (zero on a plain read).
|
|
func (config Config) routingBindingPayload(ctx context.Context, replan postgresstore.RoutingBindingReplanResult) (routingBindingView, error) {
|
|
names, connected := config.routingBindingNames(ctx)
|
|
storedInternal, storedExternal, updatedBy, err := config.RoutingBindings.LoadRoutingKBBindings(ctx)
|
|
if err != nil {
|
|
return routingBindingView{}, err
|
|
}
|
|
storedInternalName, storedExternalName, err := config.RoutingBindings.LoadRoutingKBBindingNames(ctx)
|
|
if err != nil {
|
|
return routingBindingView{}, err
|
|
}
|
|
internal, external := storedInternal, storedExternal
|
|
if internal == "" {
|
|
internal = config.InternalRoutingKnowledgeBaseID
|
|
}
|
|
if external == "" {
|
|
external = config.ExternalRoutingKnowledgeBaseID
|
|
}
|
|
nameFor := func(id, storedName string) string {
|
|
if id == "" {
|
|
return ""
|
|
}
|
|
if connected {
|
|
return names[id]
|
|
}
|
|
return storedName
|
|
}
|
|
return routingBindingView{
|
|
InternalKBID: internal, ExternalKBID: external,
|
|
InternalBoundByUI: storedInternal != "", ExternalBoundByUI: storedExternal != "",
|
|
InternalEnvFallback: storedInternal == "" && config.InternalRoutingKnowledgeBaseID != "",
|
|
ExternalEnvFallback: storedExternal == "" && config.ExternalRoutingKnowledgeBaseID != "",
|
|
InternalFallbackKBID: config.InternalRoutingKnowledgeBaseID, ExternalFallbackKBID: config.ExternalRoutingKnowledgeBaseID,
|
|
InternalKBName: nameFor(internal, storedInternalName), ExternalKBName: nameFor(external, storedExternalName),
|
|
UpdatedBy: updatedBy, WeKnoraConnected: connected,
|
|
Replanned: replan.Replanned, RetiredDuplicates: replan.RetiredDuplicates,
|
|
}, nil
|
|
}
|
|
|
|
func (config Config) handleRoutingBindings(w http.ResponseWriter, r *http.Request) {
|
|
view, err := config.routingBindingPayload(r.Context(), postgresstore.RoutingBindingReplanResult{})
|
|
if err != nil {
|
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "could not load routing bindings"})
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"data": view})
|
|
}
|
|
|
|
func (config Config) handleRoutingKnowledgeBaseOptions(w http.ResponseWriter, r *http.Request) {
|
|
if config.KnowledgeBases == nil {
|
|
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "WeKnora connection is not configured"})
|
|
return
|
|
}
|
|
bases, err := config.KnowledgeBases.ListKnowledgeBases(r.Context())
|
|
if err != nil {
|
|
writeJSON(w, http.StatusBadGateway, map[string]string{"error": "could not list WeKnora knowledge bases"})
|
|
return
|
|
}
|
|
options := make([]routingKnowledgeBaseOption, 0, len(bases))
|
|
for _, base := range bases {
|
|
options = append(options, routingKnowledgeBaseOption{ID: base.ID, Name: base.Name, KnowledgeCount: base.KnowledgeCount})
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"data": options})
|
|
}
|
|
|
|
func (config Config) handleSaveRoutingBindings(w http.ResponseWriter, r *http.Request) {
|
|
var input struct {
|
|
InternalKBID string `json:"internal_kb_id"`
|
|
ExternalKBID string `json:"external_kb_id"`
|
|
}
|
|
if err := decodeStrictJSON(r, &input); err != nil {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid request body"})
|
|
return
|
|
}
|
|
internal := strings.TrimSpace(input.InternalKBID)
|
|
external := strings.TrimSpace(input.ExternalKBID)
|
|
for _, id := range []string{internal, external} {
|
|
if id != "" && !validCatalogID(id) {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "knowledge base ID is invalid"})
|
|
return
|
|
}
|
|
}
|
|
if internal != "" && internal == external {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "对内与对外必须绑定不同的知识库"})
|
|
return
|
|
}
|
|
// When WeKnora is reachable the binding must name a real knowledge base;
|
|
// offline saves fall through to the DB so the page never hard-fails, but
|
|
// the response reports connected=false honestly.
|
|
names, connected := config.routingBindingNames(r.Context())
|
|
if !connected && config.connectionKeyUnusable(r.Context(), "test") {
|
|
writeJSON(w, http.StatusConflict, map[string]string{"error": "测试 WeKnora 已保存连接但没有可用密钥:请重新录入 API Key 并测通后再改绑定"})
|
|
return
|
|
}
|
|
if connected {
|
|
if internal != "" {
|
|
if _, ok := names[internal]; !ok {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "对内绑定的知识库在该 WeKnora 实例中不存在"})
|
|
return
|
|
}
|
|
}
|
|
if external != "" {
|
|
if _, ok := names[external]; !ok {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "对外绑定的知识库在该 WeKnora 实例中不存在"})
|
|
return
|
|
}
|
|
}
|
|
}
|
|
// The saved binding re-routes FUTURE annotations; pending 待入库 items that
|
|
// have never been uploaded are re-pointed in the same transaction, otherwise
|
|
// they would keep aiming at the previous (possibly removed) knowledge base.
|
|
effectiveInternal := internal
|
|
if effectiveInternal == "" {
|
|
effectiveInternal = config.InternalRoutingKnowledgeBaseID
|
|
}
|
|
effectiveExternal := external
|
|
if effectiveExternal == "" {
|
|
effectiveExternal = config.ExternalRoutingKnowledgeBaseID
|
|
}
|
|
previousInternal, previousExternal := config.effectiveRoutingTargets(r.Context())
|
|
replan, err := config.RoutingBindings.SaveRoutingKBBindingsWithReplan(r.Context(), postgresstore.RoutingBindingSave{
|
|
InternalID: internal, ExternalID: external,
|
|
InternalName: names[internal], ExternalName: names[external],
|
|
UpdatedBy: actorFor(r),
|
|
}, postgresstore.EffectiveKBIDs{Internal: previousInternal, External: previousExternal},
|
|
postgresstore.EffectiveKBIDs{Internal: effectiveInternal, External: effectiveExternal},
|
|
config.Now().UTC())
|
|
if err != nil {
|
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "could not save routing bindings"})
|
|
return
|
|
}
|
|
view, err := config.routingBindingPayload(r.Context(), replan)
|
|
if err != nil {
|
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "could not load routing bindings"})
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"data": view})
|
|
}
|