4528a55e6a
为什么:T6a 需要一张批次/差异表、全量只读扫描、R1-R8 判定和 finding 级处置;现有解析监控只转述台账状态,无法回答库内是否一致。 做什么:迁移 000084(upload_md5、runs/findings/actions、共享 removal_candidates 视图);WeKnora 字段与 1000/页扫描;runtime 只读客户端(0400 Key、503 降级);扫描/矩阵/动作/定时/僵死回收;9 条接口与 RBAC;overview 队列与删两张常量卡。 边界:只对测试库;对账零写请求;R1/R2 单条确认;R6 复用 /removal;R2 只管理员;生产链路未改。 验证:make verify 全绿(17 项);新增矩阵/扫描 R1-R8/零写/失败整批/动作/运行时/密钥权限测试。
168 lines
5.8 KiB
Go
168 lines
5.8 KiB
Go
package weknoraaccess
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/platform/weknora"
|
|
"knowledge-governance/internal/readiness"
|
|
)
|
|
|
|
var ErrNotConfigured = errors.New("WeKnora 有效端点/凭据未配置")
|
|
|
|
// defaultBusinessTimeout matches appconfig's TEST_WEKNORA_HTTP_TIMEOUT default.
|
|
// The readiness probe has its own 10s cap; it must not bound upload/poll calls.
|
|
const defaultBusinessTimeout = 60 * time.Second
|
|
|
|
// Client resolves the effective WeKnora target on every call. Direct calls are
|
|
// intentionally dynamic so console-managed endpoint/key/binding changes take
|
|
// effect. A job that performs several calls must call BeginTask first: it
|
|
// resolves once, verifies the owning tenant and pins that immutable connection
|
|
// in the returned context, so no later call can silently switch instances.
|
|
type Client struct {
|
|
Resolver readiness.Resolver
|
|
Timeout time.Duration
|
|
AllowDockerHTTP bool
|
|
AllowPrivateHTTP bool
|
|
}
|
|
|
|
type taskConnection struct {
|
|
remote *weknora.Client
|
|
baseURL string
|
|
}
|
|
|
|
type taskConnectionKey struct{}
|
|
|
|
func taskConnectionFrom(ctx context.Context) *taskConnection {
|
|
connection, _ := ctx.Value(taskConnectionKey{}).(*taskConnection)
|
|
return connection
|
|
}
|
|
|
|
func (client *Client) newRemote(target readiness.Target) (*weknora.Client, error) {
|
|
timeout := client.Timeout
|
|
if timeout <= 0 {
|
|
timeout = defaultBusinessTimeout
|
|
}
|
|
return weknora.NewClient(weknora.ClientConfig{
|
|
BaseURL: target.BaseURL,
|
|
APIKey: target.APIKey,
|
|
Timeout: timeout,
|
|
AllowDockerHTTP: client.AllowDockerHTTP,
|
|
AllowPrivateHTTP: client.AllowPrivateHTTP,
|
|
})
|
|
}
|
|
|
|
// BeginTask resolves the endpoint/key/binding exactly once, reads the owning
|
|
// tenant through that same connection and returns a context that pins it.
|
|
// Upload, poll, validate and remove must pass the returned context to every
|
|
// follow-up call and re-read, including recovery and delete read-back.
|
|
func (client *Client) BeginTask(ctx context.Context, knowledgeBaseID string) (context.Context, weknora.InstanceIdentity, error) {
|
|
target, err := client.Resolver.Resolve(ctx)
|
|
if err != nil {
|
|
return ctx, weknora.InstanceIdentity{}, err
|
|
}
|
|
if target.BaseURL == "" || target.APIKey == "" || knowledgeBaseID == "" {
|
|
return ctx, weknora.InstanceIdentity{}, ErrNotConfigured
|
|
}
|
|
remote, err := client.newRemote(target)
|
|
if err != nil {
|
|
return ctx, weknora.InstanceIdentity{}, err
|
|
}
|
|
base, err := remote.GetKnowledgeBase(ctx, knowledgeBaseID)
|
|
if err != nil {
|
|
return ctx, weknora.InstanceIdentity{}, err
|
|
}
|
|
connection := &taskConnection{remote: remote, baseURL: weknora.NormalizeInstanceURL(target.BaseURL)}
|
|
bound := context.WithValue(ctx, taskConnectionKey{}, connection)
|
|
return bound, weknora.InstanceIdentity{BaseURL: connection.baseURL, TenantID: base.TenantID}, nil
|
|
}
|
|
|
|
func (client *Client) with(ctx context.Context, call func(*weknora.Client) error) error {
|
|
if connection := taskConnectionFrom(ctx); connection != nil && connection.remote != nil {
|
|
return call(connection.remote)
|
|
}
|
|
target, err := client.Resolver.Resolve(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if target.BaseURL == "" || target.APIKey == "" {
|
|
return ErrNotConfigured
|
|
}
|
|
remote, err := client.newRemote(target)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return call(remote)
|
|
}
|
|
|
|
func (client *Client) TargetIdentity(ctx context.Context, knowledgeBaseID string) (weknora.InstanceIdentity, error) {
|
|
if connection := taskConnectionFrom(ctx); connection != nil && connection.remote != nil {
|
|
if knowledgeBaseID == "" {
|
|
return weknora.InstanceIdentity{}, ErrNotConfigured
|
|
}
|
|
base, err := connection.remote.GetKnowledgeBase(ctx, knowledgeBaseID)
|
|
if err != nil {
|
|
return weknora.InstanceIdentity{}, err
|
|
}
|
|
return weknora.InstanceIdentity{BaseURL: connection.baseURL, TenantID: base.TenantID}, nil
|
|
}
|
|
return client.identityOnce(ctx, knowledgeBaseID)
|
|
}
|
|
|
|
// identityOnce is the pre-task entry used by callers that only need identity.
|
|
// Multi-call jobs use BeginTask so verification and every later call share the
|
|
// same connection.
|
|
func (client *Client) identityOnce(ctx context.Context, knowledgeBaseID string) (weknora.InstanceIdentity, error) {
|
|
_, identity, err := client.BeginTask(ctx, knowledgeBaseID)
|
|
return identity, err
|
|
}
|
|
|
|
func (client *Client) UploadFile(ctx context.Context, request weknora.UploadRequest) (weknora.Knowledge, error) {
|
|
var knowledge weknora.Knowledge
|
|
err := client.with(ctx, func(remote *weknora.Client) error {
|
|
value, err := remote.UploadFile(ctx, request)
|
|
knowledge = value
|
|
return err
|
|
})
|
|
return knowledge, err
|
|
}
|
|
|
|
func (client *Client) ListKnowledge(ctx context.Context, knowledgeBaseID string, page, pageSize int) ([]weknora.Knowledge, int64, error) {
|
|
var items []weknora.Knowledge
|
|
var total int64
|
|
err := client.with(ctx, func(remote *weknora.Client) error {
|
|
value, count, err := remote.ListKnowledge(ctx, knowledgeBaseID, page, pageSize)
|
|
items, total = value, count
|
|
return err
|
|
})
|
|
return items, total, err
|
|
}
|
|
|
|
func (client *Client) GetKnowledge(ctx context.Context, knowledgeID string) (weknora.Knowledge, error) {
|
|
var knowledge weknora.Knowledge
|
|
err := client.with(ctx, func(remote *weknora.Client) error {
|
|
value, err := remote.GetKnowledge(ctx, knowledgeID)
|
|
knowledge = value
|
|
return err
|
|
})
|
|
return knowledge, err
|
|
}
|
|
|
|
func (client *Client) ListChunks(ctx context.Context, knowledgeID string, page, pageSize int, chunkTypes []string) ([]weknora.Chunk, int64, error) {
|
|
var chunks []weknora.Chunk
|
|
var total int64
|
|
err := client.with(ctx, func(remote *weknora.Client) error {
|
|
value, count, err := remote.ListChunks(ctx, knowledgeID, page, pageSize, chunkTypes)
|
|
chunks, total = value, count
|
|
return err
|
|
})
|
|
return chunks, total, err
|
|
}
|
|
|
|
func (client *Client) DeleteKnowledge(ctx context.Context, knowledgeID string) error {
|
|
return client.with(ctx, func(remote *weknora.Client) error {
|
|
return remote.DeleteKnowledge(ctx, knowledgeID)
|
|
})
|
|
}
|