feat(settings): bind internal/external knowledge bases from the UI
Routing targets move from env-only to a UI-managed binding (migration 000034 routing_kb_bindings, single active row): - GET/PUT /api/v1/routing-bindings + /options: env defaults act as the bootstrap fallback; a UI binding wins and takes effect immediately (per-request resolution, no restart), and clearing falls back to env - PUT validates against the connected WeKnora instance (real knowledge base list via the scoped API key) and rejects same-KB bindings; saves require the admin role - route/catalog capabilities now resolve through the binding so both annotation paths share one source of truth - system settings > 服务配置 gains a 知识库绑定 panel: dropdown picker (or raw ID when WeKnora is unreachable) showing bound name, origin (UI vs env default) and last author - contract tests: OpenAPI, handler matrix (fallback, validation, same-KB rejection, capability resolution), frontend fetch/save layer
This commit is contained in:
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS routing_kb_bindings;
|
||||
@@ -0,0 +1,14 @@
|
||||
-- Policy-driven ingestion, step 3: routable knowledge-base bindings move from
|
||||
-- environment-only configuration into the UI. A single row stores which
|
||||
-- WeKnora knowledge base receives 对内 and which receives 对外 annotations.
|
||||
-- The env-supplied defaults stay as a bootstrap fallback only.
|
||||
CREATE TABLE routing_kb_bindings (
|
||||
id TEXT PRIMARY KEY DEFAULT 'active' CHECK (id = 'active'),
|
||||
internal_kb_id VARCHAR(64) NOT NULL DEFAULT '',
|
||||
external_kb_id VARCHAR(64) NOT NULL DEFAULT '',
|
||||
updated_by VARCHAR(64) NOT NULL DEFAULT '',
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
INSERT INTO routing_kb_bindings(id,internal_kb_id,external_kb_id,updated_by)
|
||||
VALUES('active','','','system:migration');
|
||||
@@ -11,8 +11,8 @@ func TestEmbeddedMigrationsAreVersionSorted(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("loadMigrations() error = %v", err)
|
||||
}
|
||||
if len(migrations) != 33 {
|
||||
t.Fatalf("migration count = %d, want 33", len(migrations))
|
||||
if len(migrations) != 34 {
|
||||
t.Fatalf("migration count = %d, want 34", len(migrations))
|
||||
}
|
||||
if migrations[0].Version != 1 || !strings.Contains(migrations[0].UpSQL, "CREATE TABLE IF NOT EXISTS jobs") {
|
||||
t.Fatalf("migration 1 = %#v", migrations[0])
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// RoutingBinding is the UI-managed knowledge base binding row. Empty ids mean
|
||||
// "not bound in the UI"; callers fall back to their environment defaults.
|
||||
type RoutingBinding struct {
|
||||
InternalKBID string
|
||||
ExternalKBID string
|
||||
UpdatedBy string
|
||||
}
|
||||
|
||||
// LoadRoutingKBBindings reads the single active binding row. A missing row
|
||||
// reads back as unbound rather than an error.
|
||||
func (s *Store) LoadRoutingKBBindings(ctx context.Context) (string, string, string, error) {
|
||||
var binding RoutingBinding
|
||||
err := s.db.QueryRow(ctx, `SELECT internal_kb_id,external_kb_id,updated_by FROM routing_kb_bindings WHERE id='active'`).
|
||||
Scan(&binding.InternalKBID, &binding.ExternalKBID, &binding.UpdatedBy)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", "", "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", "", "", errors.New("load routing kb bindings")
|
||||
}
|
||||
return binding.InternalKBID, binding.ExternalKBID, binding.UpdatedBy, nil
|
||||
}
|
||||
|
||||
// SaveRoutingKBBindings upserts the binding row. Each side is either empty
|
||||
// (unbind, fall back to env) or a knowledge base id validated by the caller.
|
||||
func (s *Store) SaveRoutingKBBindings(ctx context.Context, internalID, externalID, updatedBy string) error {
|
||||
_, err := s.db.Exec(ctx, `
|
||||
INSERT INTO routing_kb_bindings(id,internal_kb_id,external_kb_id,updated_by,updated_at)
|
||||
VALUES('active',$1,$2,$3,NOW())
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
internal_kb_id=EXCLUDED.internal_kb_id,
|
||||
external_kb_id=EXCLUDED.external_kb_id,
|
||||
updated_by=EXCLUDED.updated_by,
|
||||
updated_at=NOW()`, internalID, externalID, updatedBy)
|
||||
if err != nil {
|
||||
return errors.New("save routing kb bindings")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -221,6 +221,19 @@ func (c *Client) ListKnowledge(ctx context.Context, knowledgeBaseID string, page
|
||||
return response.Data, response.Total, nil
|
||||
}
|
||||
|
||||
// ListKnowledgeBases lists the knowledge bases visible to this API key so the
|
||||
// UI can bind routing targets without humans copying UUIDs.
|
||||
func (c *Client) ListKnowledgeBases(ctx context.Context) ([]KnowledgeBase, error) {
|
||||
var response knowledgeBaseListResponse
|
||||
if err := c.doJSON(ctx, http.MethodGet, "/api/v1/knowledge-bases", nil, &response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !response.Success {
|
||||
return nil, errors.New("WeKnora returned an unsuccessful knowledge base list")
|
||||
}
|
||||
return response.Data, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListChunks(ctx context.Context, knowledgeID string, page, pageSize int, chunkTypes []string) ([]Chunk, int64, error) {
|
||||
if !validPathSegment(knowledgeID) || !validPage(page, pageSize) {
|
||||
return nil, 0, errors.New("invalid chunk list request")
|
||||
|
||||
@@ -62,6 +62,20 @@ type knowledgeListResponse struct {
|
||||
PageSize int `json:"page_size"`
|
||||
}
|
||||
|
||||
// KnowledgeBase is the routing-relevant projection of a WeKnora knowledge
|
||||
// base as listed by GET /api/v1/knowledge-bases.
|
||||
type KnowledgeBase struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
KnowledgeCount int64 `json:"knowledge_count"`
|
||||
ChunkCount int64 `json:"chunk_count"`
|
||||
}
|
||||
|
||||
type knowledgeBaseListResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Data []KnowledgeBase `json:"data"`
|
||||
}
|
||||
|
||||
type chunkListResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Data []Chunk `json:"data"`
|
||||
|
||||
Reference in New Issue
Block a user