refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2026-07-22 07:05:31 +00:00
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"path/filepath"
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
"slices"
|
2026-07-22 04:05:02 +00:00
|
|
|
"strings"
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
"time"
|
|
|
|
|
|
2026-09-08 07:36:33 +00:00
|
|
|
"github.com/Tencent/WeKnora/internal/application/access"
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
"github.com/Tencent/WeKnora/internal/application/service/retriever"
|
|
|
|
|
werrors "github.com/Tencent/WeKnora/internal/errors"
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/logger"
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/tracing/langfuse"
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
2026-06-02 11:25:38 +00:00
|
|
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/hibiken/asynq"
|
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-22 07:05:31 +00:00
|
|
|
// copyOwnedObject copies srcPath into a NEW object owned by the destination
|
|
|
|
|
// tenant, returning the new provider:// (resource) path.
|
|
|
|
|
//
|
|
|
|
|
// Extracted/embedded chunk images MUST land in the tenant's exports/ namespace,
|
|
|
|
|
// because GET /knowledge-bases/:id/files only serves objects that pass
|
|
|
|
|
// ValidateKBScopedStoragePath (i.e. {tenant}/exports/...). CopyFile writes to
|
|
|
|
|
// the knowledge-scoped upload layout ({tenant}/{knowledgeID}/...) used for raw
|
|
|
|
|
// source files, which the KB proxy rejects — so a clone that used CopyFile
|
|
|
|
|
// produced images that could no longer be rendered. Instead, read the source
|
|
|
|
|
// bytes and re-save them via SaveBytes, exactly mirroring how the original
|
|
|
|
|
// images were persisted during ingestion (see image_resolver.saveReferencedImage),
|
|
|
|
|
// so the copy is a genuine independent object in the servable namespace.
|
2026-06-02 11:25:38 +00:00
|
|
|
func copyOwnedObject(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
srcSvc, dstSvc interfaces.FileService,
|
|
|
|
|
srcPath string,
|
|
|
|
|
tenantID uint64,
|
|
|
|
|
knowledgeID string,
|
|
|
|
|
) (string, error) {
|
2026-07-22 07:05:31 +00:00
|
|
|
_ = knowledgeID // exports objects are tenant-scoped, not knowledge-scoped
|
|
|
|
|
rc, err := srcSvc.GetFile(ctx, srcPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("read source image %q: %w", srcPath, err)
|
|
|
|
|
}
|
|
|
|
|
defer rc.Close()
|
|
|
|
|
data, err := io.ReadAll(rc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("buffer source image %q: %w", srcPath, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileName := uuid.New().String() + imageExtForCopy(srcPath, data)
|
|
|
|
|
newPath, err := dstSvc.SaveBytes(ctx, data, tenantID, fileName, false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("save copied image for %q: %w", srcPath, err)
|
|
|
|
|
}
|
|
|
|
|
return newPath, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// imageExtForCopy resolves the file extension to use for a copied image. It
|
|
|
|
|
// prefers an image extension already present on the source path, then falls
|
|
|
|
|
// back to sniffing the content bytes, and finally defaults to ".png" (matching
|
|
|
|
|
// image_resolver's default) so the object is always served with a sane type.
|
|
|
|
|
func imageExtForCopy(srcPath string, data []byte) string {
|
|
|
|
|
if ext := strings.ToLower(filepath.Ext(srcPath)); isImageExt(ext) {
|
|
|
|
|
return ext
|
|
|
|
|
}
|
|
|
|
|
switch http.DetectContentType(data) {
|
|
|
|
|
case "image/png":
|
|
|
|
|
return ".png"
|
|
|
|
|
case "image/jpeg":
|
|
|
|
|
return ".jpg"
|
|
|
|
|
case "image/gif":
|
|
|
|
|
return ".gif"
|
|
|
|
|
case "image/webp":
|
|
|
|
|
return ".webp"
|
|
|
|
|
case "image/bmp":
|
|
|
|
|
return ".bmp"
|
|
|
|
|
}
|
|
|
|
|
return ".png"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isImageExt(ext string) bool {
|
|
|
|
|
switch ext {
|
|
|
|
|
case ".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".svg":
|
|
|
|
|
return true
|
|
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
2026-06-02 11:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// cloneChunkImageInfo parses a chunk's image_info JSON, copies every referenced
|
|
|
|
|
// object into a NEW object owned by (tenantID, knowledgeID), and returns the
|
|
|
|
|
// re-serialized image_info plus the list of newly-created object URLs (for
|
|
|
|
|
// rollback on failure). urlCache dedups identical source objects across chunks
|
2026-07-22 05:30:21 +00:00
|
|
|
// so the same source image is copied at most once per clone AND accumulates the
|
|
|
|
|
// full old->new URL mapping so callers can rewrite in-content Markdown image
|
|
|
|
|
// references (see rewriteContentImageURLs).
|
2026-06-02 11:25:38 +00:00
|
|
|
//
|
2026-07-22 05:30:21 +00:00
|
|
|
// An empty srcImageInfo yields ("", nil, nil). A JSON parse failure returns an
|
|
|
|
|
// error (the clone fails) rather than silently inheriting the shared-reference
|
|
|
|
|
// bug. When an image's OriginalURL points at the same object as its URL (the
|
|
|
|
|
// common case for extracted images), OriginalURL is rewritten to the new path
|
|
|
|
|
// too; an OriginalURL from a different/external source is preserved.
|
2026-06-02 11:25:38 +00:00
|
|
|
func cloneChunkImageInfo(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
dstSvc interfaces.FileService,
|
|
|
|
|
srcImageInfo string,
|
|
|
|
|
tenantID uint64,
|
|
|
|
|
knowledgeID string,
|
|
|
|
|
urlCache map[string]string,
|
2026-07-22 05:30:21 +00:00
|
|
|
) (newImageInfo string, copiedURLs []string, err error) {
|
2026-06-02 11:25:38 +00:00
|
|
|
if srcImageInfo == "" {
|
2026-07-22 05:30:21 +00:00
|
|
|
return "", nil, nil
|
2026-06-02 11:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var images []*types.ImageInfo
|
|
|
|
|
if err := json.Unmarshal([]byte(srcImageInfo), &images); err != nil {
|
2026-07-22 05:30:21 +00:00
|
|
|
return "", nil, fmt.Errorf("failed to parse chunk image_info JSON: %w", err)
|
2026-06-02 11:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, img := range images {
|
|
|
|
|
if img == nil || img.URL == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
originalMatchedURL := img.OriginalURL == img.URL
|
|
|
|
|
|
|
|
|
|
newURL, cached := urlCache[img.URL]
|
|
|
|
|
if !cached {
|
|
|
|
|
newURL, err = copyOwnedObject(ctx, dstSvc, dstSvc, img.URL, tenantID, knowledgeID)
|
|
|
|
|
if err != nil {
|
2026-07-22 05:30:21 +00:00
|
|
|
return "", copiedURLs, fmt.Errorf("failed to copy chunk image %q: %w", img.URL, err)
|
2026-06-02 11:25:38 +00:00
|
|
|
}
|
|
|
|
|
urlCache[img.URL] = newURL
|
|
|
|
|
copiedURLs = append(copiedURLs, newURL)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if originalMatchedURL {
|
|
|
|
|
img.OriginalURL = newURL
|
|
|
|
|
}
|
|
|
|
|
img.URL = newURL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out, err := json.Marshal(images)
|
|
|
|
|
if err != nil {
|
2026-07-22 05:30:21 +00:00
|
|
|
return "", copiedURLs, fmt.Errorf("failed to re-serialize chunk image_info: %w", err)
|
2026-07-22 04:05:02 +00:00
|
|
|
}
|
2026-07-22 05:30:21 +00:00
|
|
|
return string(out), copiedURLs, nil
|
2026-07-22 04:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rewriteContentImageURLs replaces every occurrence of an old image URL with its
|
2026-07-22 05:30:21 +00:00
|
|
|
// new (copied) URL in content, using the old->new mapping accumulated in
|
|
|
|
|
// urlCache. It is the second half of the image deep-copy: chunk Content embeds
|
|
|
|
|
// image URLs as Markdown  references, but for document knowledge the
|
|
|
|
|
// image objects live in independent image_ocr/image_caption child chunks — the
|
|
|
|
|
// parent text chunk carries the  reference with an empty image_info. So
|
|
|
|
|
// the old->new mapping is only known after every chunk's image_info has been
|
|
|
|
|
// processed; this rewrite must therefore run as a final pass once urlCache is
|
|
|
|
|
// complete, over ALL cloned chunks, not per-chunk.
|
|
|
|
|
//
|
|
|
|
|
// Replacements are applied longest-old-URL first so a URL that is a prefix of
|
|
|
|
|
// another is not partially rewritten. Entries whose old==new are skipped.
|
|
|
|
|
func rewriteContentImageURLs(content string, urlCache map[string]string) string {
|
|
|
|
|
if content == "" || len(urlCache) == 0 {
|
2026-07-22 04:05:02 +00:00
|
|
|
return content
|
|
|
|
|
}
|
2026-07-22 05:30:21 +00:00
|
|
|
oldURLs := make([]string, 0, len(urlCache))
|
|
|
|
|
for oldURL, newURL := range urlCache {
|
2026-07-22 04:05:02 +00:00
|
|
|
if oldURL == "" || oldURL == newURL {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
oldURLs = append(oldURLs, oldURL)
|
|
|
|
|
}
|
|
|
|
|
slices.SortFunc(oldURLs, func(a, b string) int { return len(b) - len(a) })
|
|
|
|
|
for _, oldURL := range oldURLs {
|
2026-07-22 05:30:21 +00:00
|
|
|
content = strings.ReplaceAll(content, oldURL, urlCache[oldURL])
|
2026-06-02 11:25:38 +00:00
|
|
|
}
|
2026-07-22 04:05:02 +00:00
|
|
|
return content
|
2026-06-02 11:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// cleanupCopiedObjects deletes objects that were newly created during a clone
|
|
|
|
|
// that subsequently failed, to avoid orphaning storage. It is best-effort:
|
|
|
|
|
// delete errors are logged but never returned (the original clone error wins).
|
|
|
|
|
func cleanupCopiedObjects(ctx context.Context, svc interfaces.FileService, paths []string) {
|
|
|
|
|
if len(paths) == 0 || svc == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
logger.Infof(ctx, "Cleaning up %d copied objects after clone failure", len(paths))
|
|
|
|
|
for _, p := range paths {
|
|
|
|
|
if p == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if err := svc.DeleteFile(ctx, p); err != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to clean up copied object %s: %v", p, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
func (s *knowledgeService) CloneKnowledgeBase(ctx context.Context, srcID, dstID string) error {
|
2026-09-08 07:36:33 +00:00
|
|
|
source, target, err := s.kbService.CopyKnowledgeBase(ctx, srcID, dstID)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
if source.Type == types.KnowledgeBaseTypeFAQ {
|
|
|
|
|
p := &types.KBCloneProgress{TaskID: access.TransferTaskID(ctx), SourceID: source.ID, TargetID: target.ID}
|
|
|
|
|
return s.cloneFAQKnowledgeBase(ctx, source, target, p, func(*types.KBCloneProgress, error, string) {})
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
feat(knowledge): derive knowledge base descriptions from document profiles (#3370)
* feat(knowledge): derive knowledge base descriptions from document profiles
Knowledge bases only had a hand-written description, which most users
leave empty, so agents bound to several knowledge bases had nothing to
route on and searched all of them. This adds a generated description
that is derived from per-document data instead of maintained by a model.
Document profile: the summary call now returns JSON with a 2-3 sentence
summary plus a gist, 3-5 topic keywords, a document type and one
typical question (knowledges.profile). Input is capped at 8k chars and
output at 1024 tokens; plain-text replies from custom templates still
work and simply carry no profile. The summary chunk embeds the gist and
short summary.
Knowledge base aggregate: a database-only fold over those rows (counts
per file type, tag, normalized topic and doc type; stratified samples of
titles and questions; a hash of the inputs). Deleting, moving or
re-parsing a document changes nothing but the rows, so the next rebuild
is exact without any model judgement.
Description text: one small model call over the aggregate yields a
gist, merged topics and typical questions (knowledge_bases.
generated_profile). The call is skipped when the aggregate hash is
unchanged; failures keep the previous text and record the error; an
empty knowledge base clears it. The manual description is never
touched, and the agent runtime context renders both so the model can
pass knowledge_base_ids instead of fanning out.
Refreshes are requested from summary task exit, post-process completion,
deletion, cross-KB moves and enabling the option, debounced per
knowledge base in 30 s windows via kb:profile on the summary queue.
POST /knowledge-bases/:id/profile/generate regenerates synchronously.
profile_config (enabled, model_id, custom_instructions) opts a document
knowledge base in; the settings dialog shows the generated card with
regenerate and adopt-as-description actions in five locales. Search
results now log hits per chunk type so the value of indexing summary
chunks can be measured.
Migrations 000101 (PostgreSQL) and 000021 (SQLite) add the columns; the
SQLite schema test expects the new version and columns.
* fix(knowledge): keep generated descriptions out of duplicates and refresh clones
DuplicateKnowledgeBase copies the source row through JSON, which carried
generated_profile onto a knowledge base that has no documents yet, so
an empty duplicate advertised the source's topics. The duplicate now
starts without a generated description and keeps profile_config so it
derives its own once documents arrive.
CopyKnowledgeBase builds the target field by field and skipped
profile_config, and the clone task never asked for a description after
copying the documents, so a clone stayed undescribed until its next
upload. The target now carries a copy of profile_config and both clone
paths request a refresh on completion.
* fix(knowledge): refresh the loaded clone target and carry document profiles
ProcessKBClone requested the description refresh with the reservation
stub it builds before CopyKnowledgeBase runs. For the main create path
that stub has neither a type nor profile_config, so the refresh helper
rejected it and nothing was enqueued. The refresh now uses the loaded
target returned by CopyKnowledgeBase.
cloneKnowledge copied the summary text but not the structured profile,
so even a scheduled refresh would have aggregated titles without topics
until every clone was re-summarized. Cloned documents now carry an
independent copy of the profile.
2026-09-17 14:21:55 +00:00
|
|
|
if err := s.executeKnowledgeClone(ctx, source, target, nil); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
// Derive the target's description from the cloned documents now rather
|
|
|
|
|
// than on the next upload.
|
|
|
|
|
_ = requestKnowledgeBaseProfileRefresh(ctx, s.task, target, false)
|
|
|
|
|
return nil
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CloneChunk clone chunks from one knowledge to another
|
|
|
|
|
// This method transfers a chunk from a source knowledge document to a target knowledge document
|
|
|
|
|
// It handles the creation of new chunks in the target knowledge and updates the vector database accordingly
|
|
|
|
|
// Parameters:
|
|
|
|
|
// - ctx: Context with authentication and request information
|
|
|
|
|
// - src: Source knowledge document containing the chunk to move
|
|
|
|
|
// - dst: Target knowledge document where the chunk will be moved
|
|
|
|
|
//
|
|
|
|
|
// Returns:
|
|
|
|
|
// - error: Any error encountered during the move operation
|
|
|
|
|
//
|
|
|
|
|
// This method handles the chunk transfer logic, including creating new chunks in the target knowledge
|
|
|
|
|
// and updating the vector database representation of the moved chunks.
|
|
|
|
|
// It also ensures that the chunk's relationships (like pre and next chunk IDs) are maintained
|
|
|
|
|
// by mapping the source chunk IDs to the new target chunk IDs.
|
2026-06-02 11:25:38 +00:00
|
|
|
func (s *knowledgeService) CloneChunk(ctx context.Context, src, dst *types.Knowledge) (err error) {
|
2026-09-08 07:36:33 +00:00
|
|
|
sourceKB, err := knowledgeWriteKB(ctx, s.kbService, src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
targetKB, err := knowledgeWriteKB(ctx, s.kbService, dst)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := access.RequireKBTransfer(ctx, sourceKB, targetKB, access.KBTransferClone); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
stored, err := s.repo.GetKnowledgeByID(ctx, dst.TenantID, dst.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if stored == nil || stored.ID != dst.ID || stored.TenantID != dst.TenantID ||
|
|
|
|
|
stored.KnowledgeBaseID != targetKB.ID {
|
|
|
|
|
return access.ErrForbidden
|
|
|
|
|
}
|
|
|
|
|
sourceChunks, err := s.transferChunks(ctx, src, sourceKB.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
chunkPageSize := 100
|
|
|
|
|
srcTodst := map[string]string{}
|
|
|
|
|
tagIDMapping := map[string]string{} // srcTagID -> dstTagID
|
|
|
|
|
targetChunks := make([]*types.Chunk, 0, 10)
|
2026-06-02 11:25:38 +00:00
|
|
|
|
|
|
|
|
// Resolve the destination FileService so extracted images can be copied
|
|
|
|
|
// into objects owned by the destination knowledge. urlCache dedups identical
|
|
|
|
|
// source images across chunks; copiedURLs accumulates new objects so they can
|
|
|
|
|
// be cleaned up if the clone fails partway through.
|
|
|
|
|
dstKB, dstKBErr := s.kbService.GetKnowledgeBaseByID(ctx, dst.KnowledgeBaseID)
|
|
|
|
|
if dstKBErr != nil {
|
|
|
|
|
return fmt.Errorf("failed to load destination knowledge base for image copy: %w", dstKBErr)
|
|
|
|
|
}
|
|
|
|
|
dstSvc := s.resolveFileService(ctx, dstKB)
|
|
|
|
|
urlCache := map[string]string{}
|
|
|
|
|
var copiedURLs []string
|
2026-09-08 07:36:33 +00:00
|
|
|
chunksAttempted := false
|
|
|
|
|
var rollbackIndices func() error
|
2026-06-02 11:25:38 +00:00
|
|
|
defer func() {
|
2026-09-08 07:36:33 +00:00
|
|
|
if err == nil {
|
|
|
|
|
return
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
if chunksAttempted {
|
|
|
|
|
// A failed create may have committed before losing its acknowledgement.
|
|
|
|
|
// Claim the original destination before removing any persisted data.
|
|
|
|
|
before, after := *stored, *stored
|
|
|
|
|
after.ParseStatus = types.ParseStatusFailed
|
|
|
|
|
after.ErrorMessage = err.Error()
|
|
|
|
|
if checkpointErr := s.repo.UpdateKnowledgeForTransfer(ctx, &before, &after); checkpointErr != nil {
|
|
|
|
|
err = errors.Join(err, fmt.Errorf("claim failed clone cleanup: %w", checkpointErr))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if rollbackIndices != nil {
|
|
|
|
|
if cleanupErr := rollbackIndices(); cleanupErr != nil {
|
|
|
|
|
err = errors.Join(err, fmt.Errorf("clean failed clone indices: %w", cleanupErr))
|
|
|
|
|
return
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
if cleanupErr := s.chunkRepo.DeleteChunksByKnowledgeID(ctx, dst.TenantID, dst.ID); cleanupErr != nil {
|
|
|
|
|
err = errors.Join(err, fmt.Errorf("clean failed clone chunks: %w", cleanupErr))
|
|
|
|
|
return
|
2026-06-02 11:25:38 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
}
|
|
|
|
|
cleanupCopiedObjects(ctx, dstSvc, copiedURLs)
|
|
|
|
|
}()
|
2026-06-02 11:25:38 +00:00
|
|
|
|
2026-09-08 07:36:33 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
for _, sourceChunk := range sourceChunks {
|
|
|
|
|
// Map TagID to target knowledge base
|
|
|
|
|
targetTagID := ""
|
|
|
|
|
if sourceChunk.TagID != "" {
|
|
|
|
|
if mappedTagID, ok := tagIDMapping[sourceChunk.TagID]; ok {
|
|
|
|
|
targetTagID = mappedTagID
|
|
|
|
|
} else {
|
|
|
|
|
// Try to find or create the tag in target knowledge base
|
|
|
|
|
targetTagID = s.getOrCreateTagInTarget(ctx,
|
|
|
|
|
src.TenantID,
|
|
|
|
|
dst.TenantID,
|
|
|
|
|
dst.KnowledgeBaseID,
|
|
|
|
|
sourceChunk.TagID,
|
|
|
|
|
tagIDMapping)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
|
|
|
|
|
// Deep-copy extracted images into objects owned by the destination
|
|
|
|
|
// knowledge so deleting the source never breaks this clone. Content
|
|
|
|
|
// URL rewriting happens in a final pass below, once urlCache holds
|
|
|
|
|
// the complete old->new mapping (image objects live in independent
|
|
|
|
|
// child chunks, so a parent text chunk's  reference cannot be
|
|
|
|
|
// rewritten until its child image chunk has been processed).
|
|
|
|
|
newImageInfo, copied, copyErr := cloneChunkImageInfo(
|
|
|
|
|
ctx, dstSvc, sourceChunk.ImageInfo, dst.TenantID, dst.ID, urlCache)
|
|
|
|
|
copiedURLs = append(copiedURLs, copied...)
|
|
|
|
|
if copyErr != nil {
|
|
|
|
|
err = fmt.Errorf("clone chunk image copy failed: %w", copyErr)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
targetChunk := &types.Chunk{
|
|
|
|
|
ID: uuid.New().String(),
|
|
|
|
|
TenantID: dst.TenantID,
|
|
|
|
|
KnowledgeID: dst.ID,
|
|
|
|
|
KnowledgeBaseID: dst.KnowledgeBaseID,
|
|
|
|
|
TagID: targetTagID,
|
|
|
|
|
Content: sourceChunk.Content,
|
|
|
|
|
ChunkIndex: sourceChunk.ChunkIndex,
|
|
|
|
|
IsEnabled: sourceChunk.IsEnabled,
|
|
|
|
|
Flags: sourceChunk.Flags,
|
|
|
|
|
Status: sourceChunk.Status,
|
|
|
|
|
IndexStatus: sourceChunk.IndexStatus,
|
|
|
|
|
StartAt: sourceChunk.StartAt,
|
|
|
|
|
EndAt: sourceChunk.EndAt,
|
|
|
|
|
PreChunkID: sourceChunk.PreChunkID,
|
|
|
|
|
NextChunkID: sourceChunk.NextChunkID,
|
|
|
|
|
ChunkType: sourceChunk.ChunkType,
|
|
|
|
|
ParentChunkID: sourceChunk.ParentChunkID,
|
|
|
|
|
Metadata: sourceChunk.Metadata,
|
|
|
|
|
ContentHash: sourceChunk.ContentHash,
|
|
|
|
|
ImageInfo: newImageInfo,
|
|
|
|
|
CreatedAt: now,
|
|
|
|
|
UpdatedAt: now,
|
|
|
|
|
}
|
|
|
|
|
targetChunks = append(targetChunks, targetChunk)
|
|
|
|
|
srcTodst[sourceChunk.ID] = targetChunk.ID
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
for _, targetChunk := range targetChunks {
|
2026-07-22 05:30:21 +00:00
|
|
|
// Rewrite in-content Markdown image URLs now that urlCache holds the
|
|
|
|
|
// complete old->new mapping across all chunks. This fixes parent text
|
|
|
|
|
// chunks whose  reference points at a source object copied while
|
|
|
|
|
// processing an independent image_ocr/image_caption child chunk.
|
|
|
|
|
targetChunk.Content = rewriteContentImageURLs(targetChunk.Content, urlCache)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
if val, ok := srcTodst[targetChunk.PreChunkID]; ok {
|
|
|
|
|
targetChunk.PreChunkID = val
|
|
|
|
|
} else {
|
|
|
|
|
targetChunk.PreChunkID = ""
|
|
|
|
|
}
|
|
|
|
|
if val, ok := srcTodst[targetChunk.NextChunkID]; ok {
|
|
|
|
|
targetChunk.NextChunkID = val
|
|
|
|
|
} else {
|
|
|
|
|
targetChunk.NextChunkID = ""
|
|
|
|
|
}
|
|
|
|
|
if val, ok := srcTodst[targetChunk.ParentChunkID]; ok {
|
|
|
|
|
targetChunk.ParentChunkID = val
|
|
|
|
|
} else {
|
|
|
|
|
targetChunk.ParentChunkID = ""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for chunks := range slices.Chunk(targetChunks, chunkPageSize) {
|
2026-09-08 07:36:33 +00:00
|
|
|
chunksAttempted = true
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
err := s.chunkRepo.CreateChunks(ctx, chunks)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
refactor(retriever): introduce factory for KB-scoped retrieve engine resolution
Extracts the 25 repeated NewCompositeRetrieveEngine call sites across
seven services into two factory functions with tenant-ownership
verification: CreateRetrieveEngineForKB (synchronous) and
CreateRetrieveEngineFromPayload (async task handlers). Promotes
GetByStoreID from concrete-only to interfaces.RetrieveEngineRegistry.
Extends KBDeletePayload / IndexDeletePayload with an omitempty
VectorStoreID snapshot.
Design highlights
- Factory verifies tenant ownership of the resolved store
(defense-in-depth) so a gap in PR3 validation or a tampered Asynq
payload cannot cross tenants. Cross-tenant attempts return
ErrVectorStoreForbidden with a structured log entry.
- Sentinel errors (ErrTenantInfoMissing, ErrVectorStoreNotFound,
ErrVectorStoreForbidden) let async handlers classify non-retryable
failures as asynq.SkipRetry.
- nil and empty-string pointers for vectorStoreID normalize to
"unbound" so callers never send an empty UUID to GetByStoreID.
- resolveBoundEngine constructs engineInfos directly (with
slices.Clone of Support()) instead of going through
NewCompositeRetrieveEngine. The latter only reads from the
byEngineType map and cannot reach DB stores in byStoreID -- multiple
stores can share the same engine type. Unbound fallbacks still reuse
NewCompositeRetrieveEngine so the tenant effective-engines path is
byte-for-byte unchanged.
- DeleteKnowledgeBase now reads the KB before soft-delete so the
enqueued KBDeletePayload carries a VectorStoreID snapshot; once the
row is soft-deleted GORM's default scope hides it. Cost: +1 SELECT
per KB-delete request (a rare admin operation).
- enqueueIndexDeleteTask gains a vectorStoreID *string parameter that
the single caller (tag.go) populates from the owning KB. The worker
(ProcessIndexDelete) validates ownership via the factory.
- cleanupKnowledgeResources loads the KB so bound-store cleanup routes
correctly. If the load fails it warns and falls back to tenant
effective engines; orphan vectors become observable in logs instead
of disappearing silently.
- DeleteKnowledges (batch) stays on the tenant effective-engines path
-- batches can span stores and multi-store fan-out is PR4 scope.
Noted inline.
Merge safety
All knowledge bases have VectorStoreID = NULL at merge time (introduced
nullable in PR1, Tencent/WeKnora#994). Every factory call falls back to
NewCompositeRetrieveEngine(registry, tenantInfo.GetEffectiveEngines()),
which is the existing code path for these KBs. Async payloads from
before this change decode to VectorStoreID = nil (omitempty) and take
the same fallback.
Testing
- go build ./...
- go vet ./...
- go test -race -count=1 ./internal/application/service/retriever/...
covers unbound (nil/empty), bound, cross-tenant, unregistered store,
ownership infra error, legacy payload shapes (missing field/null/
empty string/nil), tampered payload, and parallel invocation.
- grep -r NewCompositeRetrieveEngine internal/application/service
returns zero results outside factory.go/composite.go.
Refs: Part of Tencent/WeKnora#993. Depends on Tencent/WeKnora#994.
2026-05-13 08:24:17 +00:00
|
|
|
tenantID := types.MustTenantIDFromContext(ctx)
|
|
|
|
|
// Route CopyIndices via the source KB's bound store. This function does
|
|
|
|
|
// not handle cross-store copies — embeddings written by different
|
|
|
|
|
// VectorStore backends are not bit-compatible, so callers that allow
|
|
|
|
|
// source/target KBs to bind to different stores must perform their own
|
|
|
|
|
// cross-store migration before invoking this.
|
2026-09-08 07:36:33 +00:00
|
|
|
if len(srcTodst) == 0 || dst.EmbeddingModelID == "" {
|
|
|
|
|
return nil
|
refactor(retriever): introduce factory for KB-scoped retrieve engine resolution
Extracts the 25 repeated NewCompositeRetrieveEngine call sites across
seven services into two factory functions with tenant-ownership
verification: CreateRetrieveEngineForKB (synchronous) and
CreateRetrieveEngineFromPayload (async task handlers). Promotes
GetByStoreID from concrete-only to interfaces.RetrieveEngineRegistry.
Extends KBDeletePayload / IndexDeletePayload with an omitempty
VectorStoreID snapshot.
Design highlights
- Factory verifies tenant ownership of the resolved store
(defense-in-depth) so a gap in PR3 validation or a tampered Asynq
payload cannot cross tenants. Cross-tenant attempts return
ErrVectorStoreForbidden with a structured log entry.
- Sentinel errors (ErrTenantInfoMissing, ErrVectorStoreNotFound,
ErrVectorStoreForbidden) let async handlers classify non-retryable
failures as asynq.SkipRetry.
- nil and empty-string pointers for vectorStoreID normalize to
"unbound" so callers never send an empty UUID to GetByStoreID.
- resolveBoundEngine constructs engineInfos directly (with
slices.Clone of Support()) instead of going through
NewCompositeRetrieveEngine. The latter only reads from the
byEngineType map and cannot reach DB stores in byStoreID -- multiple
stores can share the same engine type. Unbound fallbacks still reuse
NewCompositeRetrieveEngine so the tenant effective-engines path is
byte-for-byte unchanged.
- DeleteKnowledgeBase now reads the KB before soft-delete so the
enqueued KBDeletePayload carries a VectorStoreID snapshot; once the
row is soft-deleted GORM's default scope hides it. Cost: +1 SELECT
per KB-delete request (a rare admin operation).
- enqueueIndexDeleteTask gains a vectorStoreID *string parameter that
the single caller (tag.go) populates from the owning KB. The worker
(ProcessIndexDelete) validates ownership via the factory.
- cleanupKnowledgeResources loads the KB so bound-store cleanup routes
correctly. If the load fails it warns and falls back to tenant
effective engines; orphan vectors become observable in logs instead
of disappearing silently.
- DeleteKnowledges (batch) stays on the tenant effective-engines path
-- batches can span stores and multi-store fan-out is PR4 scope.
Noted inline.
Merge safety
All knowledge bases have VectorStoreID = NULL at merge time (introduced
nullable in PR1, Tencent/WeKnora#994). Every factory call falls back to
NewCompositeRetrieveEngine(registry, tenantInfo.GetEffectiveEngines()),
which is the existing code path for these KBs. Async payloads from
before this change decode to VectorStoreID = nil (omitempty) and take
the same fallback.
Testing
- go build ./...
- go vet ./...
- go test -race -count=1 ./internal/application/service/retriever/...
covers unbound (nil/empty), bound, cross-tenant, unregistered store,
ownership infra error, legacy payload shapes (missing field/null/
empty string/nil), tampered payload, and parallel invocation.
- grep -r NewCompositeRetrieveEngine internal/application/service
returns zero results outside factory.go/composite.go.
Refs: Part of Tencent/WeKnora#993. Depends on Tencent/WeKnora#994.
2026-05-13 08:24:17 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
sourceStoreID := sourceKB.VectorStoreID
|
refactor(retriever): introduce factory for KB-scoped retrieve engine resolution
Extracts the 25 repeated NewCompositeRetrieveEngine call sites across
seven services into two factory functions with tenant-ownership
verification: CreateRetrieveEngineForKB (synchronous) and
CreateRetrieveEngineFromPayload (async task handlers). Promotes
GetByStoreID from concrete-only to interfaces.RetrieveEngineRegistry.
Extends KBDeletePayload / IndexDeletePayload with an omitempty
VectorStoreID snapshot.
Design highlights
- Factory verifies tenant ownership of the resolved store
(defense-in-depth) so a gap in PR3 validation or a tampered Asynq
payload cannot cross tenants. Cross-tenant attempts return
ErrVectorStoreForbidden with a structured log entry.
- Sentinel errors (ErrTenantInfoMissing, ErrVectorStoreNotFound,
ErrVectorStoreForbidden) let async handlers classify non-retryable
failures as asynq.SkipRetry.
- nil and empty-string pointers for vectorStoreID normalize to
"unbound" so callers never send an empty UUID to GetByStoreID.
- resolveBoundEngine constructs engineInfos directly (with
slices.Clone of Support()) instead of going through
NewCompositeRetrieveEngine. The latter only reads from the
byEngineType map and cannot reach DB stores in byStoreID -- multiple
stores can share the same engine type. Unbound fallbacks still reuse
NewCompositeRetrieveEngine so the tenant effective-engines path is
byte-for-byte unchanged.
- DeleteKnowledgeBase now reads the KB before soft-delete so the
enqueued KBDeletePayload carries a VectorStoreID snapshot; once the
row is soft-deleted GORM's default scope hides it. Cost: +1 SELECT
per KB-delete request (a rare admin operation).
- enqueueIndexDeleteTask gains a vectorStoreID *string parameter that
the single caller (tag.go) populates from the owning KB. The worker
(ProcessIndexDelete) validates ownership via the factory.
- cleanupKnowledgeResources loads the KB so bound-store cleanup routes
correctly. If the load fails it warns and falls back to tenant
effective engines; orphan vectors become observable in logs instead
of disappearing silently.
- DeleteKnowledges (batch) stays on the tenant effective-engines path
-- batches can span stores and multi-store fan-out is PR4 scope.
Noted inline.
Merge safety
All knowledge bases have VectorStoreID = NULL at merge time (introduced
nullable in PR1, Tencent/WeKnora#994). Every factory call falls back to
NewCompositeRetrieveEngine(registry, tenantInfo.GetEffectiveEngines()),
which is the existing code path for these KBs. Async payloads from
before this change decode to VectorStoreID = nil (omitempty) and take
the same fallback.
Testing
- go build ./...
- go vet ./...
- go test -race -count=1 ./internal/application/service/retriever/...
covers unbound (nil/empty), bound, cross-tenant, unregistered store,
ownership infra error, legacy payload shapes (missing field/null/
empty string/nil), tampered payload, and parallel invocation.
- grep -r NewCompositeRetrieveEngine internal/application/service
returns zero results outside factory.go/composite.go.
Refs: Part of Tencent/WeKnora#993. Depends on Tencent/WeKnora#994.
2026-05-13 08:24:17 +00:00
|
|
|
retrieveEngine, err := retriever.CreateRetrieveEngineForKB(
|
|
|
|
|
ctx, s.retrieveEngine, s.ownership, tenantID, sourceStoreID)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
embeddingModel, err := s.modelService.GetEmbeddingModel(ctx, dst.EmbeddingModelID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
rollbackIndices = func() error {
|
|
|
|
|
return retrieveEngine.DeleteByKnowledgeIDList(ctx, []string{dst.ID}, embeddingModel.GetDimensions(), dst.Type)
|
|
|
|
|
}
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
if err := retrieveEngine.CopyIndices(ctx, src.KnowledgeBaseID, dst.KnowledgeBaseID,
|
|
|
|
|
map[string]string{src.ID: dst.ID},
|
|
|
|
|
srcTodst,
|
|
|
|
|
embeddingModel.GetDimensions(),
|
|
|
|
|
dst.Type,
|
|
|
|
|
); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
kbCloneProgressKeyPrefix = "kb_clone_progress:"
|
|
|
|
|
kbCloneProgressTTL = 24 * time.Hour
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// getKBCloneProgressKey returns the Redis key for storing KB clone progress
|
|
|
|
|
func getKBCloneProgressKey(taskID string) string {
|
|
|
|
|
return kbCloneProgressKeyPrefix + taskID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ProcessKBClone handles Asynq knowledge base clone tasks
|
|
|
|
|
func (s *knowledgeService) ProcessKBClone(ctx context.Context, t *asynq.Task) error {
|
|
|
|
|
var payload types.KBClonePayload
|
|
|
|
|
if err := json.Unmarshal(t.Payload(), &payload); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to unmarshal KB clone payload: %w", err)
|
|
|
|
|
}
|
2026-07-22 11:15:27 +00:00
|
|
|
ctx = payload.Initiator.Apply(ctx)
|
2026-07-22 12:08:31 +00:00
|
|
|
ctx = withKBActivityTask(ctx, payload.TaskID, kbActivityTrigger(ctx))
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
|
2026-09-08 07:36:33 +00:00
|
|
|
if payload.TenantID == 0 || payload.SourceID == "" || payload.TaskID == "" {
|
|
|
|
|
return fmt.Errorf("invalid clone task: %w", asynq.SkipRetry)
|
|
|
|
|
}
|
|
|
|
|
ctx = types.WithExecutionTenant(ctx, payload.TenantID)
|
|
|
|
|
source, err := s.kbService.GetKnowledgeBaseByID(ctx, payload.SourceID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
create := payload.CreateTarget || payload.TargetID == ""
|
|
|
|
|
if payload.TargetID == "" {
|
|
|
|
|
// Compatibility for tasks admitted before destination IDs were reserved at
|
|
|
|
|
// enqueue time. The same legacy task always resolves the same target.
|
|
|
|
|
payload.TargetID = uuid.NewSHA1(uuid.NameSpaceOID,
|
|
|
|
|
[]byte(fmt.Sprintf("kb-clone:%d:%s:%s",
|
|
|
|
|
payload.TenantID,
|
|
|
|
|
payload.SourceID,
|
|
|
|
|
payload.TaskID))).
|
|
|
|
|
String()
|
|
|
|
|
}
|
|
|
|
|
target := &types.KnowledgeBase{ID: payload.TargetID, TenantID: payload.TenantID, CreatorID: payload.CreatorID}
|
|
|
|
|
if !create {
|
|
|
|
|
target, err = s.kbService.GetKnowledgeBaseByID(ctx, payload.TargetID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if source == nil || source.ID != payload.SourceID || target == nil || target.ID != payload.TargetID {
|
|
|
|
|
return fmt.Errorf("invalid clone binding: %w", asynq.SkipRetry)
|
|
|
|
|
}
|
|
|
|
|
ctx, err = access.WithKBTransferTask(
|
|
|
|
|
ctx,
|
|
|
|
|
source,
|
|
|
|
|
target,
|
|
|
|
|
payload.TenantID,
|
|
|
|
|
access.KBTransferClone,
|
|
|
|
|
payload.TaskID,
|
|
|
|
|
create,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("invalid clone scope: %v: %w", err, asynq.SkipRetry)
|
|
|
|
|
}
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
|
|
|
|
|
// Get tenant info and add to context
|
|
|
|
|
tenantInfo, err := s.tenantRepo.GetTenantByID(ctx, payload.TenantID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to get tenant info: %v", err)
|
|
|
|
|
return fmt.Errorf("failed to get tenant info: %w", err)
|
|
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
if tenantInfo == nil || tenantInfo.ID != payload.TenantID {
|
|
|
|
|
return fmt.Errorf("invalid clone tenant: %w", asynq.SkipRetry)
|
|
|
|
|
}
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
ctx = context.WithValue(ctx, types.TenantInfoContextKey, tenantInfo)
|
|
|
|
|
|
2026-09-08 07:36:33 +00:00
|
|
|
// Get source and target knowledge bases
|
|
|
|
|
srcKB, dstKB, err := s.kbService.CopyKnowledgeBase(ctx, payload.SourceID, payload.TargetID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
retry, maxRetry := 0, 0
|
|
|
|
|
retry, _ = asynq.GetRetryCount(ctx)
|
|
|
|
|
maxRetry, _ = asynq.GetMaxRetry(ctx)
|
|
|
|
|
status := types.KBCloneStatusProcessing
|
|
|
|
|
if retry >= maxRetry {
|
|
|
|
|
status = types.KBCloneStatusFailed
|
|
|
|
|
}
|
|
|
|
|
_ = s.saveKBCloneProgress(
|
|
|
|
|
ctx,
|
|
|
|
|
&types.KBCloneProgress{
|
|
|
|
|
TaskID: payload.TaskID,
|
|
|
|
|
SourceID: payload.SourceID,
|
|
|
|
|
TargetID: payload.TargetID,
|
|
|
|
|
Status: status,
|
|
|
|
|
Error: err.Error(),
|
|
|
|
|
Message: "Clone preflight failed",
|
|
|
|
|
UpdatedAt: time.Now().Unix(),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
// Check if this is the last retry
|
|
|
|
|
retryCount, _ := asynq.GetRetryCount(ctx)
|
|
|
|
|
maxRetry, _ := asynq.GetMaxRetry(ctx)
|
|
|
|
|
isLastRetry := retryCount >= maxRetry
|
|
|
|
|
|
|
|
|
|
logger.Infof(ctx, "Processing KB clone task: %s, source: %s, target: %s, retry: %d/%d",
|
|
|
|
|
payload.TaskID, payload.SourceID, payload.TargetID, retryCount, maxRetry)
|
|
|
|
|
|
|
|
|
|
// Helper function to handle errors - only mark as failed on last retry
|
|
|
|
|
handleError := func(progress *types.KBCloneProgress, err error, message string) {
|
|
|
|
|
if isLastRetry {
|
|
|
|
|
progress.Status = types.KBCloneStatusFailed
|
|
|
|
|
progress.Error = err.Error()
|
|
|
|
|
progress.Message = message
|
|
|
|
|
progress.UpdatedAt = time.Now().Unix()
|
|
|
|
|
_ = s.saveKBCloneProgress(ctx, progress)
|
2026-07-22 11:15:27 +00:00
|
|
|
recordKBActivity(ctx, s.audit, payload.TenantID, payload.TargetID, types.AuditActionKBCloneFailed,
|
|
|
|
|
"knowledge_base", payload.TargetID, types.AuditOutcomeFailed,
|
|
|
|
|
map[string]any{"source_kb_id": payload.SourceID, "task_id": payload.TaskID})
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update progress to processing
|
|
|
|
|
progress := &types.KBCloneProgress{
|
|
|
|
|
TaskID: payload.TaskID,
|
|
|
|
|
SourceID: payload.SourceID,
|
|
|
|
|
TargetID: payload.TargetID,
|
|
|
|
|
Status: types.KBCloneStatusProcessing,
|
|
|
|
|
Progress: 0,
|
|
|
|
|
Message: "Starting knowledge base clone...",
|
|
|
|
|
UpdatedAt: time.Now().Unix(),
|
|
|
|
|
}
|
|
|
|
|
if err := s.saveKBCloneProgress(ctx, progress); err != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to update KB clone progress: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-22 11:15:27 +00:00
|
|
|
if retryCount == 0 {
|
|
|
|
|
recordKBActivity(ctx, s.audit, payload.TenantID, payload.TargetID, types.AuditActionKBCloneStarted,
|
|
|
|
|
"knowledge_base", payload.TargetID, types.AuditOutcomeAccepted,
|
|
|
|
|
map[string]any{"source_kb_id": payload.SourceID, "task_id": payload.TaskID})
|
|
|
|
|
}
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
|
|
|
|
|
// Use different sync strategies based on knowledge base type
|
|
|
|
|
if srcKB.Type == types.KnowledgeBaseTypeFAQ {
|
2026-07-22 11:15:27 +00:00
|
|
|
if err := s.cloneFAQKnowledgeBase(ctx, srcKB, dstKB, progress, handleError); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
recordKBActivity(ctx, s.audit, payload.TenantID, payload.TargetID, types.AuditActionKBCloneCompleted,
|
|
|
|
|
"knowledge_base", payload.TargetID, types.AuditOutcomeSuccess,
|
|
|
|
|
map[string]any{"source_kb_id": payload.SourceID, "task_id": payload.TaskID, "total": progress.Total})
|
|
|
|
|
return nil
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
2026-09-08 07:36:33 +00:00
|
|
|
err = s.executeKnowledgeClone(ctx, srcKB, dstKB, func(done, total int) {
|
|
|
|
|
progress.Total = total
|
|
|
|
|
progress.Processed = done
|
|
|
|
|
if total > 0 {
|
|
|
|
|
progress.Progress = done * 100 / total
|
|
|
|
|
}
|
|
|
|
|
progress.Message = fmt.Sprintf("Processed %d/%d clone operations", done, total)
|
|
|
|
|
progress.UpdatedAt = time.Now().Unix()
|
|
|
|
|
_ = s.saveKBCloneProgress(ctx, progress)
|
|
|
|
|
})
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
|
handleError(progress, err, "Failed to clone knowledge")
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
totalOperations := progress.Total
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
|
|
|
|
|
// Mark as completed
|
|
|
|
|
progress.Status = types.KBCloneStatusCompleted
|
|
|
|
|
progress.Progress = 100
|
|
|
|
|
progress.Processed = totalOperations
|
|
|
|
|
progress.Message = "Knowledge base clone completed successfully"
|
|
|
|
|
progress.UpdatedAt = time.Now().Unix()
|
|
|
|
|
if err := s.saveKBCloneProgress(ctx, progress); err != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to update KB clone progress to completed: %v", err)
|
|
|
|
|
}
|
feat(knowledge): derive knowledge base descriptions from document profiles (#3370)
* feat(knowledge): derive knowledge base descriptions from document profiles
Knowledge bases only had a hand-written description, which most users
leave empty, so agents bound to several knowledge bases had nothing to
route on and searched all of them. This adds a generated description
that is derived from per-document data instead of maintained by a model.
Document profile: the summary call now returns JSON with a 2-3 sentence
summary plus a gist, 3-5 topic keywords, a document type and one
typical question (knowledges.profile). Input is capped at 8k chars and
output at 1024 tokens; plain-text replies from custom templates still
work and simply carry no profile. The summary chunk embeds the gist and
short summary.
Knowledge base aggregate: a database-only fold over those rows (counts
per file type, tag, normalized topic and doc type; stratified samples of
titles and questions; a hash of the inputs). Deleting, moving or
re-parsing a document changes nothing but the rows, so the next rebuild
is exact without any model judgement.
Description text: one small model call over the aggregate yields a
gist, merged topics and typical questions (knowledge_bases.
generated_profile). The call is skipped when the aggregate hash is
unchanged; failures keep the previous text and record the error; an
empty knowledge base clears it. The manual description is never
touched, and the agent runtime context renders both so the model can
pass knowledge_base_ids instead of fanning out.
Refreshes are requested from summary task exit, post-process completion,
deletion, cross-KB moves and enabling the option, debounced per
knowledge base in 30 s windows via kb:profile on the summary queue.
POST /knowledge-bases/:id/profile/generate regenerates synchronously.
profile_config (enabled, model_id, custom_instructions) opts a document
knowledge base in; the settings dialog shows the generated card with
regenerate and adopt-as-description actions in five locales. Search
results now log hits per chunk type so the value of indexing summary
chunks can be measured.
Migrations 000101 (PostgreSQL) and 000021 (SQLite) add the columns; the
SQLite schema test expects the new version and columns.
* fix(knowledge): keep generated descriptions out of duplicates and refresh clones
DuplicateKnowledgeBase copies the source row through JSON, which carried
generated_profile onto a knowledge base that has no documents yet, so
an empty duplicate advertised the source's topics. The duplicate now
starts without a generated description and keeps profile_config so it
derives its own once documents arrive.
CopyKnowledgeBase builds the target field by field and skipped
profile_config, and the clone task never asked for a description after
copying the documents, so a clone stayed undescribed until its next
upload. The target now carries a copy of profile_config and both clone
paths request a refresh on completion.
* fix(knowledge): refresh the loaded clone target and carry document profiles
ProcessKBClone requested the description refresh with the reservation
stub it builds before CopyKnowledgeBase runs. For the main create path
that stub has neither a type nor profile_config, so the refresh helper
rejected it and nothing was enqueued. The refresh now uses the loaded
target returned by CopyKnowledgeBase.
cloneKnowledge copied the summary text but not the structured profile,
so even a scheduled refresh would have aggregated titles without topics
until every clone was re-summarized. Cloned documents now carry an
independent copy of the profile.
2026-09-17 14:21:55 +00:00
|
|
|
// Derive the target's description from the cloned documents now instead
|
|
|
|
|
// of waiting for the next upload. dstKB is the loaded target (type and
|
|
|
|
|
// profile_config included); `target` above is only the reservation stub
|
|
|
|
|
// when the clone created the knowledge base, and the refresh helper
|
|
|
|
|
// would reject it.
|
|
|
|
|
_ = requestKnowledgeBaseProfileRefresh(ctx, s.task, dstKB, false)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
|
|
|
|
|
logger.Infof(ctx, "KB clone task completed: %s", payload.TaskID)
|
2026-07-22 11:15:27 +00:00
|
|
|
recordKBActivity(ctx, s.audit, payload.TenantID, payload.TargetID, types.AuditActionKBCloneCompleted,
|
|
|
|
|
"knowledge_base", payload.TargetID, types.AuditOutcomeSuccess,
|
|
|
|
|
map[string]any{"source_kb_id": payload.SourceID, "task_id": payload.TaskID, "total": totalOperations})
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// cloneFAQKnowledgeBase handles FAQ knowledge base cloning with chunk-level incremental sync
|
|
|
|
|
func (s *knowledgeService) cloneFAQKnowledgeBase(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
srcKB, dstKB *types.KnowledgeBase,
|
|
|
|
|
progress *types.KBCloneProgress,
|
|
|
|
|
handleError func(*types.KBCloneProgress, error, string),
|
2026-06-02 11:25:38 +00:00
|
|
|
) (retErr error) {
|
2026-09-08 07:36:33 +00:00
|
|
|
srcByID, dstByID, err := s.preflightFAQClone(ctx, srcKB, dstKB)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 11:25:38 +00:00
|
|
|
// Deep-copy extracted FAQ images into objects owned by the destination KB.
|
|
|
|
|
// urlCache dedups identical source images across chunks; copiedURLs tracks
|
|
|
|
|
// new objects for best-effort cleanup if the clone fails partway through.
|
|
|
|
|
dstSvc := s.resolveFileService(ctx, dstKB)
|
|
|
|
|
imageURLCache := map[string]string{}
|
|
|
|
|
var copiedImageURLs []string
|
|
|
|
|
defer func() {
|
|
|
|
|
if retErr != nil {
|
|
|
|
|
cleanupCopiedObjects(ctx, dstSvc, copiedImageURLs)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
// Get source FAQ knowledge first (FAQ KB has exactly one Knowledge entry)
|
|
|
|
|
srcKnowledgeList, err := s.repo.ListKnowledgeByKnowledgeBaseID(ctx, srcKB.TenantID, srcKB.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to get source FAQ knowledge: %v", err)
|
|
|
|
|
handleError(progress, err, "Failed to get source FAQ knowledge")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if len(srcKnowledgeList) == 0 {
|
|
|
|
|
// Source has no FAQ knowledge, nothing to clone
|
|
|
|
|
progress.Status = types.KBCloneStatusCompleted
|
|
|
|
|
progress.Progress = 100
|
|
|
|
|
progress.Message = "Source FAQ knowledge base is empty"
|
|
|
|
|
progress.UpdatedAt = time.Now().Unix()
|
|
|
|
|
_ = s.saveKBCloneProgress(ctx, progress)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
srcKnowledge := srcKnowledgeList[0]
|
|
|
|
|
|
2026-07-22 12:57:31 +00:00
|
|
|
// Get chunk-level differences based on content_hash.
|
|
|
|
|
diff, err := s.chunkRepo.FAQChunkDiff(ctx, srcKB.TenantID, srcKB.ID, dstKB.TenantID, dstKB.ID)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to calculate FAQ chunk difference: %v", err)
|
|
|
|
|
handleError(progress, err, "Failed to calculate FAQ chunk difference")
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
// Validate the complete diff, including matched status/tag updates, before
|
|
|
|
|
// any tag creation, index deletion or chunk write. A stored-but-unindexed
|
|
|
|
|
// destination from a failed delivery must be replaced, not counted as done.
|
|
|
|
|
for _, id := range diff.ChunksToAdd {
|
|
|
|
|
if srcByID[id] == nil {
|
|
|
|
|
return fmt.Errorf("FAQ source diff escaped transfer scope")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for _, id := range diff.ChunksToDelete {
|
|
|
|
|
if dstByID[id] == nil {
|
|
|
|
|
return fmt.Errorf("FAQ target diff escaped transfer scope")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
matched := make([]types.FAQChunkSyncPair, 0, len(diff.MatchedPairs))
|
|
|
|
|
for _, pair := range diff.MatchedPairs {
|
|
|
|
|
src, dst := srcByID[pair.SrcChunkID], dstByID[pair.DstChunkID]
|
|
|
|
|
if src == nil || dst == nil {
|
|
|
|
|
return fmt.Errorf("FAQ matched diff escaped transfer scope")
|
|
|
|
|
}
|
|
|
|
|
if dst.Status != int(types.ChunkStatusIndexed) {
|
|
|
|
|
diff.ChunksToAdd = append(diff.ChunksToAdd, src.ID)
|
|
|
|
|
diff.ChunksToDelete = append(diff.ChunksToDelete, dst.ID)
|
|
|
|
|
} else {
|
|
|
|
|
matched = append(matched, pair)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
diff.MatchedPairs = matched
|
2026-07-22 12:57:31 +00:00
|
|
|
chunksToAdd := diff.ChunksToAdd
|
|
|
|
|
chunksToDelete := diff.ChunksToDelete
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
|
2026-07-22 12:57:31 +00:00
|
|
|
tagIDMapping := map[string]string{}
|
|
|
|
|
resolveFAQTag := func(srcTagID string) string {
|
|
|
|
|
if srcTagID == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
if id, ok := tagIDMapping[srcTagID]; ok {
|
|
|
|
|
return id
|
|
|
|
|
}
|
|
|
|
|
return s.getOrCreateTagInTarget(ctx, srcKB.TenantID, dstKB.TenantID, dstKB.ID, srcTagID, tagIDMapping)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
syncPlan, err := s.buildFAQStatusSyncPlan(ctx, srcKB.TenantID, dstKB.TenantID, diff.MatchedPairs, resolveFAQTag)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to build FAQ status sync plan: %v", err)
|
|
|
|
|
handleError(progress, err, "Failed to build FAQ status sync plan")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
chunksToUpdate := syncPlan.Pairs
|
|
|
|
|
|
|
|
|
|
totalOperations := len(chunksToAdd) + len(chunksToDelete) + len(chunksToUpdate)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
progress.Total = totalOperations
|
2026-07-22 12:57:31 +00:00
|
|
|
progress.Message = fmt.Sprintf(
|
|
|
|
|
"Found %d FAQ entries to add, %d to delete, %d to update status",
|
|
|
|
|
len(chunksToAdd), len(chunksToDelete), len(chunksToUpdate),
|
|
|
|
|
)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
progress.UpdatedAt = time.Now().Unix()
|
|
|
|
|
_ = s.saveKBCloneProgress(ctx, progress)
|
|
|
|
|
|
2026-07-22 12:57:31 +00:00
|
|
|
logger.Infof(ctx, "FAQ chunks to add: %d, delete: %d, update status: %d",
|
|
|
|
|
len(chunksToAdd), len(chunksToDelete), len(chunksToUpdate))
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
|
|
|
|
|
if totalOperations == 0 {
|
|
|
|
|
progress.Status = types.KBCloneStatusCompleted
|
|
|
|
|
progress.Progress = 100
|
|
|
|
|
progress.Message = "FAQ knowledge base is already in sync"
|
|
|
|
|
progress.UpdatedAt = time.Now().Unix()
|
|
|
|
|
_ = s.saveKBCloneProgress(ctx, progress)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
refactor(retriever): introduce factory for KB-scoped retrieve engine resolution
Extracts the 25 repeated NewCompositeRetrieveEngine call sites across
seven services into two factory functions with tenant-ownership
verification: CreateRetrieveEngineForKB (synchronous) and
CreateRetrieveEngineFromPayload (async task handlers). Promotes
GetByStoreID from concrete-only to interfaces.RetrieveEngineRegistry.
Extends KBDeletePayload / IndexDeletePayload with an omitempty
VectorStoreID snapshot.
Design highlights
- Factory verifies tenant ownership of the resolved store
(defense-in-depth) so a gap in PR3 validation or a tampered Asynq
payload cannot cross tenants. Cross-tenant attempts return
ErrVectorStoreForbidden with a structured log entry.
- Sentinel errors (ErrTenantInfoMissing, ErrVectorStoreNotFound,
ErrVectorStoreForbidden) let async handlers classify non-retryable
failures as asynq.SkipRetry.
- nil and empty-string pointers for vectorStoreID normalize to
"unbound" so callers never send an empty UUID to GetByStoreID.
- resolveBoundEngine constructs engineInfos directly (with
slices.Clone of Support()) instead of going through
NewCompositeRetrieveEngine. The latter only reads from the
byEngineType map and cannot reach DB stores in byStoreID -- multiple
stores can share the same engine type. Unbound fallbacks still reuse
NewCompositeRetrieveEngine so the tenant effective-engines path is
byte-for-byte unchanged.
- DeleteKnowledgeBase now reads the KB before soft-delete so the
enqueued KBDeletePayload carries a VectorStoreID snapshot; once the
row is soft-deleted GORM's default scope hides it. Cost: +1 SELECT
per KB-delete request (a rare admin operation).
- enqueueIndexDeleteTask gains a vectorStoreID *string parameter that
the single caller (tag.go) populates from the owning KB. The worker
(ProcessIndexDelete) validates ownership via the factory.
- cleanupKnowledgeResources loads the KB so bound-store cleanup routes
correctly. If the load fails it warns and falls back to tenant
effective engines; orphan vectors become observable in logs instead
of disappearing silently.
- DeleteKnowledges (batch) stays on the tenant effective-engines path
-- batches can span stores and multi-store fan-out is PR4 scope.
Noted inline.
Merge safety
All knowledge bases have VectorStoreID = NULL at merge time (introduced
nullable in PR1, Tencent/WeKnora#994). Every factory call falls back to
NewCompositeRetrieveEngine(registry, tenantInfo.GetEffectiveEngines()),
which is the existing code path for these KBs. Async payloads from
before this change decode to VectorStoreID = nil (omitempty) and take
the same fallback.
Testing
- go build ./...
- go vet ./...
- go test -race -count=1 ./internal/application/service/retriever/...
covers unbound (nil/empty), bound, cross-tenant, unregistered store,
ownership infra error, legacy payload shapes (missing field/null/
empty string/nil), tampered payload, and parallel invocation.
- grep -r NewCompositeRetrieveEngine internal/application/service
returns zero results outside factory.go/composite.go.
Refs: Part of Tencent/WeKnora#993. Depends on Tencent/WeKnora#994.
2026-05-13 08:24:17 +00:00
|
|
|
// Route the FAQ clone through the source KB's bound store. Same
|
|
|
|
|
// constraint as CloneChunk: callers must ensure source and target share
|
|
|
|
|
// the same VectorStore (cross-store FAQ clone is not handled here).
|
|
|
|
|
var sourceStoreID *string
|
|
|
|
|
if srcKB != nil {
|
|
|
|
|
sourceStoreID = srcKB.VectorStoreID
|
|
|
|
|
}
|
|
|
|
|
retrieveEngine, err := retriever.CreateRetrieveEngineForKB(
|
|
|
|
|
ctx, s.retrieveEngine, s.ownership, types.MustTenantIDFromContext(ctx), sourceStoreID)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to init retrieve engine: %v", err)
|
|
|
|
|
handleError(progress, err, "Failed to initialize retrieve engine")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get embedding model
|
|
|
|
|
embeddingModel, err := s.modelService.GetEmbeddingModel(ctx, dstKB.EmbeddingModelID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to get embedding model: %v", err)
|
|
|
|
|
handleError(progress, err, "Failed to get embedding model")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
processedCount := 0
|
|
|
|
|
|
|
|
|
|
// Delete FAQ chunks that don't exist in source
|
|
|
|
|
if len(chunksToDelete) > 0 {
|
|
|
|
|
// Delete from vector store
|
|
|
|
|
if err := retrieveEngine.DeleteByChunkIDList(ctx, chunksToDelete, embeddingModel.GetDimensions(), types.KnowledgeTypeFAQ); err != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to delete FAQ chunks from vector store: %v", err)
|
|
|
|
|
handleError(progress, err, "Failed to delete FAQ entries from vector store")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
// Delete from database
|
|
|
|
|
if err := s.chunkRepo.DeleteChunks(ctx, dstKB.TenantID, chunksToDelete); err != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to delete FAQ chunks from database: %v", err)
|
|
|
|
|
handleError(progress, err, "Failed to delete FAQ entries from database")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
processedCount += len(chunksToDelete)
|
|
|
|
|
if totalOperations > 0 {
|
|
|
|
|
progress.Progress = processedCount * 100 / totalOperations
|
|
|
|
|
}
|
|
|
|
|
progress.Processed = processedCount
|
|
|
|
|
progress.Message = fmt.Sprintf("Deleted %d FAQ entries, adding %d...", len(chunksToDelete), len(chunksToAdd))
|
|
|
|
|
progress.UpdatedAt = time.Now().Unix()
|
|
|
|
|
_ = s.saveKBCloneProgress(ctx, progress)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get or create the FAQ knowledge entry in destination
|
|
|
|
|
dstKnowledge, err := s.getOrCreateFAQKnowledge(ctx, dstKB, srcKnowledge)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to get or create FAQ knowledge: %v", err)
|
|
|
|
|
handleError(progress, err, "Failed to prepare FAQ knowledge entry")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clone FAQ chunks from source to destination
|
|
|
|
|
batch := 50
|
|
|
|
|
for i := 0; i < len(chunksToAdd); i += batch {
|
|
|
|
|
end := i + batch
|
|
|
|
|
if end > len(chunksToAdd) {
|
|
|
|
|
end = len(chunksToAdd)
|
|
|
|
|
}
|
|
|
|
|
batchIDs := chunksToAdd[i:end]
|
|
|
|
|
|
2026-09-08 07:36:33 +00:00
|
|
|
srcChunks := make([]*types.Chunk, 0, len(batchIDs))
|
|
|
|
|
for _, id := range batchIDs {
|
|
|
|
|
srcChunks = append(srcChunks, srcByID[id])
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create new chunks for destination
|
|
|
|
|
newChunks := make([]*types.Chunk, 0, len(srcChunks))
|
|
|
|
|
for _, srcChunk := range srcChunks {
|
|
|
|
|
// Map TagID to target knowledge base
|
|
|
|
|
targetTagID := ""
|
|
|
|
|
if srcChunk.TagID != "" {
|
2026-07-22 12:57:31 +00:00
|
|
|
targetTagID = resolveFAQTag(srcChunk.TagID)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
2026-06-02 11:25:38 +00:00
|
|
|
// Deep-copy extracted images into objects owned by the destination
|
2026-07-22 12:57:31 +00:00
|
|
|
// FAQ knowledge so deleting the source never breaks this clone.
|
2026-07-22 05:30:21 +00:00
|
|
|
newImageInfo, copied, copyErr := cloneChunkImageInfo(
|
|
|
|
|
ctx, dstSvc, srcChunk.ImageInfo, dstKB.TenantID, dstKnowledge.ID, imageURLCache)
|
2026-06-02 11:25:38 +00:00
|
|
|
if copyErr != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to copy FAQ chunk images: %v", copyErr)
|
|
|
|
|
handleError(progress, copyErr, "Failed to copy FAQ entry images")
|
|
|
|
|
retErr = copyErr
|
|
|
|
|
return retErr
|
|
|
|
|
}
|
|
|
|
|
copiedImageURLs = append(copiedImageURLs, copied...)
|
|
|
|
|
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
newChunk := &types.Chunk{
|
|
|
|
|
ID: uuid.New().String(),
|
|
|
|
|
TenantID: dstKB.TenantID,
|
|
|
|
|
KnowledgeID: dstKnowledge.ID,
|
|
|
|
|
KnowledgeBaseID: dstKB.ID,
|
|
|
|
|
TagID: targetTagID,
|
2026-07-22 05:30:21 +00:00
|
|
|
Content: rewriteContentImageURLs(srcChunk.Content, imageURLCache),
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
ChunkIndex: srcChunk.ChunkIndex,
|
|
|
|
|
IsEnabled: srcChunk.IsEnabled,
|
|
|
|
|
Flags: srcChunk.Flags,
|
|
|
|
|
ChunkType: types.ChunkTypeFAQ,
|
|
|
|
|
Metadata: srcChunk.Metadata,
|
|
|
|
|
ContentHash: srcChunk.ContentHash,
|
2026-06-02 11:25:38 +00:00
|
|
|
ImageInfo: newImageInfo,
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
Status: int(types.ChunkStatusStored), // Initially stored, will be indexed
|
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
|
UpdatedAt: time.Now(),
|
|
|
|
|
}
|
|
|
|
|
newChunks = append(newChunks, newChunk)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save to database
|
|
|
|
|
if err := s.chunkRepo.CreateChunks(ctx, newChunks); err != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to create FAQ chunks: %v", err)
|
|
|
|
|
handleError(progress, err, "Failed to create FAQ entries")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-08 07:36:33 +00:00
|
|
|
// Saved rows now own these images, including when indexing later fails.
|
|
|
|
|
// A later batch failure must not delete files from an earlier saved batch.
|
|
|
|
|
copiedImageURLs = nil
|
|
|
|
|
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
// Index in vector store using existing method
|
|
|
|
|
// This will index standard question + similar questions based on FAQConfig
|
|
|
|
|
if err := s.indexFAQChunks(ctx, dstKB, dstKnowledge, newChunks, embeddingModel, false, false); err != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to index FAQ chunks: %v", err)
|
|
|
|
|
handleError(progress, err, "Failed to index FAQ entries")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update chunk status to indexed
|
|
|
|
|
for _, chunk := range newChunks {
|
|
|
|
|
chunk.Status = int(types.ChunkStatusIndexed)
|
|
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
if err := s.chunkRepo.UpdateChunks(ctx, newChunks); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to update FAQ chunks status: %w", err)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
// Don't fail the whole operation for status update failure
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
processedCount += len(batchIDs)
|
|
|
|
|
if totalOperations > 0 {
|
|
|
|
|
progress.Progress = processedCount * 100 / totalOperations
|
|
|
|
|
}
|
|
|
|
|
progress.Processed = processedCount
|
|
|
|
|
progress.Message = fmt.Sprintf("Added %d/%d FAQ entries", processedCount-len(chunksToDelete), len(chunksToAdd))
|
|
|
|
|
progress.UpdatedAt = time.Now().Unix()
|
|
|
|
|
_ = s.saveKBCloneProgress(ctx, progress)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-22 12:57:31 +00:00
|
|
|
for i := 0; i < len(chunksToUpdate); i += batch {
|
|
|
|
|
end := i + batch
|
|
|
|
|
if end > len(chunksToUpdate) {
|
|
|
|
|
end = len(chunksToUpdate)
|
|
|
|
|
}
|
|
|
|
|
if err := s.syncFAQChunkStatusBatch(
|
|
|
|
|
ctx, dstKB, chunksToUpdate[i:end], syncPlan.SrcByID, syncPlan.DstByID, resolveFAQTag); err != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to sync FAQ status fields: %v", err)
|
|
|
|
|
handleError(progress, err, "Failed to sync FAQ status fields")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
processedCount += end - i
|
|
|
|
|
if totalOperations > 0 {
|
|
|
|
|
progress.Progress = processedCount * 100 / totalOperations
|
|
|
|
|
}
|
|
|
|
|
progress.Processed = processedCount
|
|
|
|
|
progress.Message = fmt.Sprintf("Updated %d/%d FAQ status fields", processedCount-len(chunksToDelete)-len(chunksToAdd), len(chunksToUpdate))
|
|
|
|
|
progress.UpdatedAt = time.Now().Unix()
|
|
|
|
|
_ = s.saveKBCloneProgress(ctx, progress)
|
|
|
|
|
}
|
|
|
|
|
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
// Mark as completed
|
|
|
|
|
progress.Status = types.KBCloneStatusCompleted
|
|
|
|
|
progress.Progress = 100
|
|
|
|
|
progress.Processed = totalOperations
|
|
|
|
|
progress.Message = "FAQ knowledge base clone completed successfully"
|
|
|
|
|
progress.UpdatedAt = time.Now().Unix()
|
|
|
|
|
if err := s.saveKBCloneProgress(ctx, progress); err != nil {
|
|
|
|
|
logger.Errorf(ctx, "Failed to update KB clone progress to completed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getOrCreateFAQKnowledge gets or creates the FAQ knowledge entry for a knowledge base
|
|
|
|
|
// If srcKnowledge is provided, it will copy relevant fields from source when creating new knowledge
|
|
|
|
|
func (s *knowledgeService) getOrCreateFAQKnowledge(ctx context.Context, kb *types.KnowledgeBase, srcKnowledge *types.Knowledge) (*types.Knowledge, error) {
|
|
|
|
|
// FAQ knowledge base should have exactly one Knowledge entry
|
|
|
|
|
knowledgeList, err := s.repo.ListKnowledgeByKnowledgeBaseID(ctx, kb.TenantID, kb.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(knowledgeList) > 0 {
|
|
|
|
|
return knowledgeList[0], nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a new FAQ knowledge entry, copying from source if available
|
|
|
|
|
knowledge := &types.Knowledge{
|
|
|
|
|
ID: uuid.New().String(),
|
|
|
|
|
TenantID: kb.TenantID,
|
|
|
|
|
KnowledgeBaseID: kb.ID,
|
|
|
|
|
Type: types.KnowledgeTypeFAQ,
|
|
|
|
|
Channel: types.ChannelWeb,
|
|
|
|
|
Title: "FAQ",
|
|
|
|
|
ParseStatus: "completed",
|
|
|
|
|
EnableStatus: "enabled",
|
|
|
|
|
EmbeddingModelID: kb.EmbeddingModelID,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copy additional fields from source knowledge if available
|
|
|
|
|
if srcKnowledge != nil {
|
|
|
|
|
knowledge.Title = srcKnowledge.Title
|
|
|
|
|
knowledge.Description = srcKnowledge.Description
|
|
|
|
|
knowledge.Source = srcKnowledge.Source
|
|
|
|
|
knowledge.Channel = srcKnowledge.Channel
|
2026-09-08 07:36:33 +00:00
|
|
|
fields := map[string]json.RawMessage{}
|
|
|
|
|
if len(srcKnowledge.Metadata) > 0 {
|
|
|
|
|
if err := json.Unmarshal(srcKnowledge.Metadata, &fields); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("decode source FAQ metadata: %w", err)
|
|
|
|
|
}
|
|
|
|
|
delete(fields, types.KnowledgeTransferMetadataKey)
|
|
|
|
|
knowledge.Metadata, err = json.Marshal(fields)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := s.repo.CreateKnowledge(ctx, knowledge); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return knowledge, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// saveKBCloneProgress saves the KB clone progress to Redis
|
|
|
|
|
func (s *knowledgeService) saveKBCloneProgress(ctx context.Context, progress *types.KBCloneProgress) error {
|
|
|
|
|
key := getKBCloneProgressKey(progress.TaskID)
|
|
|
|
|
data, err := json.Marshal(progress)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to marshal progress: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return s.redisClient.Set(ctx, key, data, kbCloneProgressTTL).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SaveKBCloneProgress saves the KB clone progress to Redis (public method for handler use)
|
|
|
|
|
func (s *knowledgeService) SaveKBCloneProgress(ctx context.Context, progress *types.KBCloneProgress) error {
|
2026-09-08 07:36:33 +00:00
|
|
|
data, err := json.Marshal(progress)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return s.redisClient.SetNX(ctx, getKBCloneProgressKey(progress.TaskID), data, kbCloneProgressTTL).Err()
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetKBCloneProgress retrieves the progress of a knowledge base clone task
|
|
|
|
|
func (s *knowledgeService) GetKBCloneProgress(ctx context.Context, taskID string) (*types.KBCloneProgress, error) {
|
|
|
|
|
key := getKBCloneProgressKey(taskID)
|
|
|
|
|
data, err := s.redisClient.Get(ctx, key).Bytes()
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, redis.Nil) {
|
|
|
|
|
return nil, werrors.NewNotFoundError("KB clone task not found")
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("failed to get progress from Redis: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var progress types.KBCloneProgress
|
|
|
|
|
if err := json.Unmarshal(data, &progress); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal progress: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return &progress, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
knowledgeMoveProgressKeyPrefix = "knowledge_move_progress:"
|
|
|
|
|
knowledgeMoveProgressTTL = 24 * time.Hour
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func getKnowledgeMoveProgressKey(taskID string) string {
|
|
|
|
|
return knowledgeMoveProgressKeyPrefix + taskID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *knowledgeService) saveKnowledgeMoveProgress(ctx context.Context, progress *types.KnowledgeMoveProgress) error {
|
|
|
|
|
key := getKnowledgeMoveProgressKey(progress.TaskID)
|
|
|
|
|
data, err := json.Marshal(progress)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to marshal move progress: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return s.redisClient.Set(ctx, key, data, knowledgeMoveProgressTTL).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SaveKnowledgeMoveProgress saves the knowledge move progress to Redis (public method for handler use)
|
|
|
|
|
func (s *knowledgeService) SaveKnowledgeMoveProgress(ctx context.Context, progress *types.KnowledgeMoveProgress) error {
|
2026-09-08 07:36:33 +00:00
|
|
|
data, err := json.Marshal(progress)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return s.redisClient.SetNX(ctx, getKnowledgeMoveProgressKey(progress.TaskID), data, knowledgeMoveProgressTTL).Err()
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetKnowledgeMoveProgress retrieves the progress of a knowledge move task
|
|
|
|
|
func (s *knowledgeService) GetKnowledgeMoveProgress(ctx context.Context, taskID string) (*types.KnowledgeMoveProgress, error) {
|
|
|
|
|
key := getKnowledgeMoveProgressKey(taskID)
|
|
|
|
|
data, err := s.redisClient.Get(ctx, key).Bytes()
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, redis.Nil) {
|
|
|
|
|
return nil, werrors.NewNotFoundError("Knowledge move task not found")
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("failed to get move progress from Redis: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var progress types.KnowledgeMoveProgress
|
|
|
|
|
if err := json.Unmarshal(data, &progress); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal move progress: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return &progress, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ProcessKnowledgeMove handles Asynq knowledge move tasks
|
|
|
|
|
func (s *knowledgeService) ProcessKnowledgeMove(ctx context.Context, t *asynq.Task) error {
|
|
|
|
|
var payload types.KnowledgeMovePayload
|
|
|
|
|
if err := json.Unmarshal(t.Payload(), &payload); err != nil {
|
2026-09-08 07:36:33 +00:00
|
|
|
return fmt.Errorf("invalid move payload: %v: %w", err, asynq.SkipRetry)
|
|
|
|
|
}
|
|
|
|
|
if payload.TenantID == 0 || payload.TaskID == "" || payload.SourceKBID == "" || payload.TargetKBID == "" {
|
|
|
|
|
return fmt.Errorf("invalid move scope: %w", asynq.SkipRetry)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
2026-07-22 11:15:27 +00:00
|
|
|
ctx = payload.Initiator.Apply(ctx)
|
2026-07-22 12:08:31 +00:00
|
|
|
ctx = withKBActivityTask(ctx, payload.TaskID, kbActivityTrigger(ctx))
|
2026-09-08 07:36:33 +00:00
|
|
|
ctx = types.WithExecutionTenant(ctx, payload.TenantID)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
sourceKB, err := s.kbService.GetKnowledgeBaseByID(ctx, payload.SourceKBID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
targetKB, err := s.kbService.GetKnowledgeBaseByID(ctx, payload.TargetKBID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
if sourceKB == nil || sourceKB.ID != payload.SourceKBID || targetKB == nil || targetKB.ID != payload.TargetKBID {
|
|
|
|
|
return fmt.Errorf("invalid move binding: %w", asynq.SkipRetry)
|
|
|
|
|
}
|
|
|
|
|
ctx, err = access.WithKBTransferTask(
|
|
|
|
|
ctx,
|
|
|
|
|
sourceKB,
|
|
|
|
|
targetKB,
|
|
|
|
|
payload.TenantID,
|
|
|
|
|
access.KBTransferMove,
|
|
|
|
|
payload.TaskID,
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("invalid move scope: %v: %w", err, asynq.SkipRetry)
|
|
|
|
|
}
|
|
|
|
|
tenant, err := s.tenantRepo.GetTenantByID(ctx, payload.TenantID)
|
|
|
|
|
if err != nil {
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
return err
|
|
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
if tenant == nil || tenant.ID != payload.TenantID {
|
|
|
|
|
return fmt.Errorf("invalid move tenant: %w", asynq.SkipRetry)
|
|
|
|
|
}
|
|
|
|
|
ctx = context.WithValue(ctx, types.TenantInfoContextKey, tenant)
|
|
|
|
|
// No item is claimed and no failure callback can alter document state until
|
|
|
|
|
// every requested document and its children passed the same pair preflight.
|
|
|
|
|
retry, _ := asynq.GetRetryCount(ctx)
|
|
|
|
|
maxRetry, _ := asynq.GetMaxRetry(ctx)
|
|
|
|
|
items, err := s.planKnowledgeMove(ctx, sourceKB, targetKB, payload.KnowledgeIDs, payload.Mode)
|
|
|
|
|
if err != nil {
|
|
|
|
|
status := types.KBCloneStatusProcessing
|
|
|
|
|
if retry >= maxRetry {
|
|
|
|
|
status = types.KBCloneStatusFailed
|
|
|
|
|
}
|
|
|
|
|
_ = s.saveKnowledgeMoveProgress(
|
|
|
|
|
ctx,
|
|
|
|
|
&types.KnowledgeMoveProgress{
|
|
|
|
|
TaskID: payload.TaskID,
|
|
|
|
|
SourceKBID: sourceKB.ID,
|
|
|
|
|
TargetKBID: targetKB.ID,
|
|
|
|
|
Status: status,
|
|
|
|
|
Error: err.Error(),
|
|
|
|
|
Message: "Move preflight failed",
|
|
|
|
|
UpdatedAt: time.Now().Unix(),
|
|
|
|
|
},
|
|
|
|
|
)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
return err
|
|
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
progress := &types.KnowledgeMoveProgress{
|
|
|
|
|
TaskID: payload.TaskID,
|
|
|
|
|
SourceKBID: sourceKB.ID,
|
|
|
|
|
TargetKBID: targetKB.ID,
|
|
|
|
|
Status: types.KBCloneStatusProcessing,
|
|
|
|
|
Total: len(items),
|
|
|
|
|
UpdatedAt: time.Now().Unix(),
|
|
|
|
|
}
|
|
|
|
|
record := func(action types.AuditAction, outcome types.AuditOutcome) {
|
|
|
|
|
for _, kbID := range []string{sourceKB.ID, targetKB.ID} {
|
|
|
|
|
recordKBActivity(
|
|
|
|
|
ctx,
|
|
|
|
|
s.audit,
|
|
|
|
|
payload.TenantID,
|
|
|
|
|
kbID,
|
|
|
|
|
action,
|
|
|
|
|
"knowledge_move",
|
|
|
|
|
payload.TaskID,
|
|
|
|
|
outcome,
|
|
|
|
|
map[string]any{
|
|
|
|
|
"source_kb_id": sourceKB.ID,
|
|
|
|
|
"target_kb_id": targetKB.ID,
|
|
|
|
|
"task_id": payload.TaskID,
|
|
|
|
|
"count": len(items),
|
|
|
|
|
"mode": payload.Mode,
|
|
|
|
|
},
|
|
|
|
|
)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
}
|
|
|
|
|
if retry == 0 {
|
|
|
|
|
record(types.AuditActionKnowledgeMoveStarted, types.AuditOutcomeAccepted)
|
|
|
|
|
}
|
|
|
|
|
var failures error
|
|
|
|
|
for _, item := range items {
|
|
|
|
|
if err := s.moveOneKnowledge(ctx, item.ID, sourceKB, targetKB, payload.Mode); err != nil {
|
|
|
|
|
failures = errors.Join(failures, fmt.Errorf("knowledge %s: %w", item.ID, err))
|
|
|
|
|
if retry >= maxRetry {
|
|
|
|
|
s.markMoveItemFailed(ctx, item.ID, sourceKB, targetKB, payload.Mode, err)
|
|
|
|
|
}
|
|
|
|
|
progress.Failed++
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
progress.Processed++
|
|
|
|
|
progress.Progress = progress.Processed * 100 / progress.Total
|
|
|
|
|
progress.Message = fmt.Sprintf(
|
|
|
|
|
"Moved %d/%d knowledge items",
|
|
|
|
|
progress.Processed-progress.Failed,
|
|
|
|
|
progress.Total,
|
|
|
|
|
)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
progress.UpdatedAt = time.Now().Unix()
|
|
|
|
|
_ = s.saveKnowledgeMoveProgress(ctx, progress)
|
|
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
if failures != nil {
|
|
|
|
|
progress.Error = failures.Error()
|
|
|
|
|
if retry >= maxRetry {
|
|
|
|
|
progress.Status = types.KBCloneStatusFailed
|
|
|
|
|
outcome := types.AuditOutcomeFailed
|
|
|
|
|
if progress.Failed < progress.Total {
|
|
|
|
|
outcome = types.AuditOutcomePartial
|
|
|
|
|
}
|
|
|
|
|
record(types.AuditActionKnowledgeMoveFailed, outcome)
|
|
|
|
|
}
|
|
|
|
|
_ = s.saveKnowledgeMoveProgress(ctx, progress)
|
|
|
|
|
// Return an error for partial failures too. Completed items are recognized
|
|
|
|
|
// from their persisted task marker and skipped on the next delivery.
|
|
|
|
|
return failures
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
progress.Status = types.KBCloneStatusCompleted
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
progress.Progress = 100
|
2026-09-08 07:36:33 +00:00
|
|
|
progress.Error = ""
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
_ = s.saveKnowledgeMoveProgress(ctx, progress)
|
2026-09-08 07:36:33 +00:00
|
|
|
record(types.AuditActionKnowledgeMoveCompleted, types.AuditOutcomeSuccess)
|
feat(knowledge): derive knowledge base descriptions from document profiles (#3370)
* feat(knowledge): derive knowledge base descriptions from document profiles
Knowledge bases only had a hand-written description, which most users
leave empty, so agents bound to several knowledge bases had nothing to
route on and searched all of them. This adds a generated description
that is derived from per-document data instead of maintained by a model.
Document profile: the summary call now returns JSON with a 2-3 sentence
summary plus a gist, 3-5 topic keywords, a document type and one
typical question (knowledges.profile). Input is capped at 8k chars and
output at 1024 tokens; plain-text replies from custom templates still
work and simply carry no profile. The summary chunk embeds the gist and
short summary.
Knowledge base aggregate: a database-only fold over those rows (counts
per file type, tag, normalized topic and doc type; stratified samples of
titles and questions; a hash of the inputs). Deleting, moving or
re-parsing a document changes nothing but the rows, so the next rebuild
is exact without any model judgement.
Description text: one small model call over the aggregate yields a
gist, merged topics and typical questions (knowledge_bases.
generated_profile). The call is skipped when the aggregate hash is
unchanged; failures keep the previous text and record the error; an
empty knowledge base clears it. The manual description is never
touched, and the agent runtime context renders both so the model can
pass knowledge_base_ids instead of fanning out.
Refreshes are requested from summary task exit, post-process completion,
deletion, cross-KB moves and enabling the option, debounced per
knowledge base in 30 s windows via kb:profile on the summary queue.
POST /knowledge-bases/:id/profile/generate regenerates synchronously.
profile_config (enabled, model_id, custom_instructions) opts a document
knowledge base in; the settings dialog shows the generated card with
regenerate and adopt-as-description actions in five locales. Search
results now log hits per chunk type so the value of indexing summary
chunks can be measured.
Migrations 000101 (PostgreSQL) and 000021 (SQLite) add the columns; the
SQLite schema test expects the new version and columns.
* fix(knowledge): keep generated descriptions out of duplicates and refresh clones
DuplicateKnowledgeBase copies the source row through JSON, which carried
generated_profile onto a knowledge base that has no documents yet, so
an empty duplicate advertised the source's topics. The duplicate now
starts without a generated description and keeps profile_config so it
derives its own once documents arrive.
CopyKnowledgeBase builds the target field by field and skipped
profile_config, and the clone task never asked for a description after
copying the documents, so a clone stayed undescribed until its next
upload. The target now carries a copy of profile_config and both clone
paths request a refresh on completion.
* fix(knowledge): refresh the loaded clone target and carry document profiles
ProcessKBClone requested the description refresh with the reservation
stub it builds before CopyKnowledgeBase runs. For the main create path
that stub has neither a type nor profile_config, so the refresh helper
rejected it and nothing was enqueued. The refresh now uses the loaded
target returned by CopyKnowledgeBase.
cloneKnowledge copied the summary text but not the structured profile,
so even a scheduled refresh would have aggregated titles without topics
until every clone was re-summarized. Cloned documents now carry an
independent copy of the profile.
2026-09-17 14:21:55 +00:00
|
|
|
// A move is a delete on the source and an add on the target as far as
|
|
|
|
|
// the description aggregation is concerned.
|
|
|
|
|
_ = requestKnowledgeBaseProfileRefresh(ctx, s.task, sourceKB, false)
|
|
|
|
|
_ = requestKnowledgeBaseProfileRefresh(ctx, s.task, targetKB, false)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// moveOneKnowledge moves a single knowledge item from source KB to target KB.
|
|
|
|
|
func (s *knowledgeService) moveOneKnowledge(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
knowledgeID string,
|
|
|
|
|
sourceKB, targetKB *types.KnowledgeBase,
|
|
|
|
|
mode string,
|
|
|
|
|
) error {
|
2026-09-08 07:36:33 +00:00
|
|
|
if err := access.RequireKBTransfer(ctx, sourceKB, targetKB, access.KBTransferMove); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
tenant, _ := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
|
|
|
|
|
if err := access.ValidateKBTransferCompatibility(sourceKB,
|
|
|
|
|
targetKB,
|
|
|
|
|
access.KBTransferMove,
|
|
|
|
|
mode,
|
|
|
|
|
tenant); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
tenantID := sourceKB.TenantID
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
knowledge, err := s.repo.GetKnowledgeByID(ctx, tenantID, knowledgeID)
|
|
|
|
|
if err != nil {
|
2026-09-08 07:36:33 +00:00
|
|
|
return err
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
if knowledge == nil || knowledge.ID != knowledgeID {
|
|
|
|
|
return access.ErrNotFound
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
if err := validateMoveItem(ctx, knowledge, sourceKB, targetKB, mode); err != nil {
|
|
|
|
|
return err
|
2026-06-05 01:13:45 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
copyOfKnowledge := *knowledge
|
|
|
|
|
knowledge = ©OfKnowledge
|
|
|
|
|
state, err := transferState(knowledge)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
if matchesTransfer(ctx, state, sourceKB, targetKB, access.KBTransferMove, knowledgeID, mode) {
|
|
|
|
|
if state.Phase == "done" || state.Phase == "reparse_pending" {
|
|
|
|
|
if err := s.cleanupMovedSourceWiki(ctx, knowledge, sourceKB, targetKB); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if state.Phase == "reparse_pending" {
|
|
|
|
|
return s.enqueueMovedKnowledge(ctx, knowledge, sourceKB, targetKB)
|
|
|
|
|
}
|
|
|
|
|
if mode == "reuse_vectors" && targetKB.IsWikiEnabled() {
|
|
|
|
|
_, err := EnqueueWikiIngest(ctx, s.task, s.taskPendingRepo, tenantID, targetKB.ID, knowledge.ID)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
state = &knowledgeTransferState{
|
|
|
|
|
TaskID: access.TransferTaskID(ctx),
|
|
|
|
|
Operation: access.KBTransferMove,
|
|
|
|
|
SourceKB: sourceKB.ID,
|
|
|
|
|
TargetKB: targetKB.ID,
|
|
|
|
|
SourceID: knowledge.ID,
|
|
|
|
|
Mode: mode,
|
|
|
|
|
Phase: "moving",
|
|
|
|
|
}
|
|
|
|
|
if sourceKB.IsWikiEnabled() {
|
|
|
|
|
chunks, err := s.transferChunks(ctx, knowledge, sourceKB.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for _, chunk := range chunks {
|
|
|
|
|
state.WikiChunkIDs = append(state.WikiChunkIDs, chunk.ID)
|
|
|
|
|
}
|
|
|
|
|
state.WikiSummary = knowledge.Description
|
|
|
|
|
}
|
|
|
|
|
before := *knowledge
|
|
|
|
|
knowledge.ParseStatus = types.ParseStatusProcessing
|
|
|
|
|
if err := setTransferState(knowledge, *state); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := s.repo.UpdateKnowledgeForTransfer(ctx, &before, knowledge); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-07-27 06:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
switch mode {
|
|
|
|
|
case "reuse_vectors":
|
2026-07-27 06:52:09 +00:00
|
|
|
if err := s.moveKnowledgeReuseVectors(ctx, knowledge, sourceKB, targetKB); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
if err := s.cleanupMovedSourceWiki(ctx, knowledge, sourceKB, targetKB); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-07-27 06:52:09 +00:00
|
|
|
// reparse re-ingests through KnowledgePostProcess once the new chunks
|
|
|
|
|
// land; reuse_vectors keeps the existing chunks and never re-enters that
|
|
|
|
|
// pipeline, so the target KB has to be told about the document here.
|
|
|
|
|
if targetKB.IsWikiEnabled() {
|
2026-09-08 07:36:33 +00:00
|
|
|
_, err := EnqueueWikiIngest(ctx, s.task, s.taskPendingRepo, tenantID, targetKB.ID, knowledge.ID)
|
|
|
|
|
return err
|
2026-07-27 06:52:09 +00:00
|
|
|
}
|
|
|
|
|
return nil
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
case "reparse":
|
|
|
|
|
return s.moveKnowledgeReparse(ctx, knowledge, sourceKB, targetKB)
|
|
|
|
|
default:
|
|
|
|
|
return fmt.Errorf("unknown move mode: %s", mode)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-08 07:36:33 +00:00
|
|
|
// moveKnowledgeReuseVectors relocates vector metadata while retaining physical IDs.
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
func (s *knowledgeService) moveKnowledgeReuseVectors(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
knowledge *types.Knowledge,
|
|
|
|
|
sourceKB, targetKB *types.KnowledgeBase,
|
|
|
|
|
) error {
|
|
|
|
|
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
|
|
|
|
|
|
2026-09-08 07:36:33 +00:00
|
|
|
// Metadata relocation is supported only within the same vector store.
|
|
|
|
|
// Cross-store moves must reparse into the target store.
|
2026-06-05 01:13:45 +00:00
|
|
|
if !sourceKB.SharesStoreWith(targetKB) {
|
|
|
|
|
return fmt.Errorf(
|
|
|
|
|
"reuse_vectors move across different vector stores is not supported "+
|
|
|
|
|
"(source KB %s, target KB %s); use reparse mode", sourceKB.ID, targetKB.ID)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-08 07:36:33 +00:00
|
|
|
if err := access.RequireKBTransfer(ctx, sourceKB, targetKB, access.KBTransferMove); err != nil {
|
|
|
|
|
return err
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
oldChunks, err := s.transferChunks(ctx, knowledge, sourceKB.ID, targetKB.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
chunkIDs := make([]string, 0, len(oldChunks))
|
|
|
|
|
for _, chunk := range oldChunks {
|
|
|
|
|
chunkIDs = append(chunkIDs, chunk.ID)
|
|
|
|
|
}
|
|
|
|
|
if knowledge.EmbeddingModelID != "" {
|
|
|
|
|
engine, err := retriever.CreateRetrieveEngineForKB(
|
|
|
|
|
ctx,
|
|
|
|
|
s.retrieveEngine,
|
|
|
|
|
s.ownership,
|
|
|
|
|
tenantID,
|
|
|
|
|
sourceKB.VectorStoreID,
|
|
|
|
|
)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
if err != nil {
|
2026-09-08 07:36:33 +00:00
|
|
|
return err
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
model, err := s.modelService.GetEmbeddingModel(ctx, knowledge.EmbeddingModelID)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
if err != nil {
|
2026-09-08 07:36:33 +00:00
|
|
|
return err
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
// Move index metadata in place. Copy + delete by the unchanged document ID
|
|
|
|
|
// would also delete the just-created target indices.
|
|
|
|
|
if err := engine.MoveKnowledgeIndices(ctx,
|
|
|
|
|
sourceKB.ID,
|
|
|
|
|
targetKB.ID,
|
|
|
|
|
knowledge.ID,
|
|
|
|
|
chunkIDs,
|
|
|
|
|
model.GetDimensions(),
|
|
|
|
|
sourceKB.Type); err != nil {
|
|
|
|
|
return err
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
}
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
|
2026-09-08 07:36:33 +00:00
|
|
|
// Source graph namespaces must not keep exposing a document after it
|
|
|
|
|
// leaves the KB. The exact namespace deletion is repeatable on retry.
|
|
|
|
|
if s.graphEngine != nil {
|
|
|
|
|
if err := s.graphEngine.DelGraph(ctx,
|
|
|
|
|
[]types.NameSpace{{
|
|
|
|
|
KnowledgeBase: sourceKB.ID,
|
|
|
|
|
Knowledge: knowledge.ID,
|
|
|
|
|
}}); err != nil {
|
|
|
|
|
return err
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Update chunks' knowledge_base_id in DB
|
|
|
|
|
if err := s.chunkRepo.MoveChunksByKnowledgeID(ctx, tenantID, knowledge.ID, targetKB.ID); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to move chunks: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-25 18:18:10 +00:00
|
|
|
// 4. Update knowledge record (tags are KB-scoped; clear relations before moving)
|
|
|
|
|
if err := s.repo.DeleteKnowledgeTagRelations(ctx, knowledge.ID); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to clear knowledge tag relations: %w", err)
|
|
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
before := *knowledge
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
knowledge.KnowledgeBaseID = targetKB.ID
|
|
|
|
|
knowledge.ParseStatus = types.ParseStatusCompleted
|
2026-09-08 07:36:33 +00:00
|
|
|
knowledge.ErrorMessage = ""
|
|
|
|
|
state, err := transferState(knowledge)
|
|
|
|
|
if err != nil || state == nil {
|
|
|
|
|
return fmt.Errorf("move state unavailable")
|
|
|
|
|
}
|
|
|
|
|
state.Phase = "done"
|
|
|
|
|
if err := setTransferState(knowledge, *state); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := s.repo.UpdateKnowledgeForTransfer(ctx, &before, knowledge); err != nil {
|
|
|
|
|
return err
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// moveKnowledgeReparse moves knowledge to target KB and re-parses it with target KB's configuration.
|
|
|
|
|
func (s *knowledgeService) moveKnowledgeReparse(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
knowledge *types.Knowledge,
|
2026-09-08 07:36:33 +00:00
|
|
|
sourceKB, targetKB *types.KnowledgeBase,
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
) error {
|
2026-09-08 07:36:33 +00:00
|
|
|
if err := access.RequireKBTransfer(ctx, sourceKB, targetKB, access.KBTransferMove); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
before := *knowledge
|
|
|
|
|
cleanup := *knowledge
|
|
|
|
|
cleanup.StorageSize = 0
|
|
|
|
|
if err := s.cleanupKnowledgeResources(ctx, &cleanup); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to clean up source: %w", err)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
2026-06-25 18:18:10 +00:00
|
|
|
if err := s.repo.DeleteKnowledgeTagRelations(ctx, knowledge.ID); err != nil {
|
2026-09-08 07:36:33 +00:00
|
|
|
return err
|
2026-06-25 18:18:10 +00:00
|
|
|
}
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
knowledge.KnowledgeBaseID = targetKB.ID
|
|
|
|
|
knowledge.EmbeddingModelID = targetKB.EmbeddingModelID
|
|
|
|
|
knowledge.ParseStatus = types.ParseStatusPending
|
2026-09-08 07:36:33 +00:00
|
|
|
knowledge.ErrorMessage = ""
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
knowledge.EnableStatus = "disabled"
|
|
|
|
|
knowledge.Description = ""
|
|
|
|
|
knowledge.ProcessedAt = nil
|
2026-09-08 07:36:33 +00:00
|
|
|
knowledge.StorageSize = 0
|
|
|
|
|
state, err := transferState(knowledge)
|
|
|
|
|
if err != nil || state == nil {
|
|
|
|
|
return fmt.Errorf("move state unavailable")
|
|
|
|
|
}
|
|
|
|
|
state.Phase = "reparse_pending"
|
|
|
|
|
if err := setTransferState(knowledge, *state); err != nil {
|
|
|
|
|
return err
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
if err := s.repo.UpdateKnowledgeForTransfer(ctx, &before, knowledge); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := s.cleanupMovedSourceWiki(ctx, knowledge, sourceKB, targetKB); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return s.enqueueMovedKnowledge(ctx, knowledge, sourceKB, targetKB)
|
|
|
|
|
}
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
|
2026-09-08 07:36:33 +00:00
|
|
|
func (s *knowledgeService) enqueueMovedKnowledge(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
knowledge *types.Knowledge,
|
|
|
|
|
sourceKB, targetKB *types.KnowledgeBase,
|
|
|
|
|
) error {
|
|
|
|
|
if err := access.RequireKBTransfer(ctx, sourceKB, targetKB, access.KBTransferMove); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if knowledge.ParseStatus == types.ParseStatusFailed {
|
|
|
|
|
before := *knowledge
|
|
|
|
|
knowledge.ParseStatus = types.ParseStatusPending
|
|
|
|
|
knowledge.ErrorMessage = ""
|
|
|
|
|
if err := s.repo.UpdateKnowledgeForTransfer(ctx, &before, knowledge); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tenantID := knowledge.TenantID
|
|
|
|
|
taskID := "move-reparse:" + access.TransferTaskID(ctx) + ":" + knowledge.ID
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
// 3. Enqueue document processing task with target KB's configuration
|
|
|
|
|
if knowledge.IsManual() {
|
|
|
|
|
meta, err := knowledge.ManualMetadata()
|
|
|
|
|
if err != nil || meta == nil {
|
|
|
|
|
return fmt.Errorf("failed to get manual metadata for reparse: %w", err)
|
|
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
_, err = s.enqueueManualProcessing(
|
|
|
|
|
ctx,
|
|
|
|
|
knowledge,
|
|
|
|
|
meta.Content,
|
|
|
|
|
false,
|
|
|
|
|
asynq.TaskID(taskID),
|
|
|
|
|
asynq.Retention(7*24*time.Hour),
|
|
|
|
|
)
|
|
|
|
|
if err != nil && !errors.Is(err, asynq.ErrTaskIDConflict) && !errors.Is(err, asynq.ErrDuplicateTask) {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return s.acknowledgeMovedReparse(ctx, knowledge, sourceKB, targetKB)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if knowledge.FilePath != "" {
|
|
|
|
|
enableMultimodel := targetKB.IsMultimodalEnabled()
|
|
|
|
|
enableQuestionGeneration := false
|
|
|
|
|
questionCount := 3
|
|
|
|
|
if targetKB.QuestionGenerationConfig != nil && targetKB.QuestionGenerationConfig.Enabled {
|
|
|
|
|
enableQuestionGeneration = true
|
|
|
|
|
if targetKB.QuestionGenerationConfig.QuestionCount > 0 {
|
|
|
|
|
questionCount = targetKB.QuestionGenerationConfig.QuestionCount
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-05 07:24:10 +00:00
|
|
|
lang := types.LanguageFromContextOrDefault(ctx)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
taskPayload := types.DocumentProcessPayload{
|
|
|
|
|
TenantID: tenantID,
|
|
|
|
|
KnowledgeID: knowledge.ID,
|
|
|
|
|
KnowledgeBaseID: targetKB.ID,
|
|
|
|
|
FilePath: knowledge.FilePath,
|
|
|
|
|
FileName: knowledge.FileName,
|
|
|
|
|
FileType: getFileType(knowledge.FileName),
|
|
|
|
|
EnableMultimodel: enableMultimodel,
|
|
|
|
|
EnableQuestionGeneration: enableQuestionGeneration,
|
|
|
|
|
QuestionCount: questionCount,
|
|
|
|
|
Language: lang,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
langfuse.InjectTracing(ctx, &taskPayload)
|
|
|
|
|
payloadBytes, err := json.Marshal(taskPayload)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to marshal document process payload: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-08 07:36:33 +00:00
|
|
|
task := asynq.NewTask(
|
|
|
|
|
types.TypeDocumentProcess,
|
|
|
|
|
payloadBytes,
|
|
|
|
|
documentProcessTaskOptions(
|
|
|
|
|
s.config,
|
|
|
|
|
asynq.MaxRetry(3),
|
|
|
|
|
asynq.TaskID(taskID),
|
|
|
|
|
asynq.Retention(7*24*time.Hour),
|
|
|
|
|
)...)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
info, err := s.task.Enqueue(task)
|
2026-09-08 07:36:33 +00:00
|
|
|
if err != nil && !errors.Is(err, asynq.ErrTaskIDConflict) && !errors.Is(err, asynq.ErrDuplicateTask) {
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
return fmt.Errorf("failed to enqueue document process task: %w", err)
|
|
|
|
|
}
|
2026-09-08 07:36:33 +00:00
|
|
|
_ = info
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
2026-09-08 07:36:33 +00:00
|
|
|
return s.acknowledgeMovedReparse(ctx, knowledge, sourceKB, targetKB)
|
refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.
Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.
knowledge.go 497 struct + constructor + errs + CRUD reads + Search
knowledge_create.go 1144 CreateFrom{File,URL,Passage,Manual} + helpers
knowledge_delete.go 686 Delete + wiki cleanup + ProcessKnowledgeListDelete
knowledge_process.go 2407 ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
knowledge_clone_move.go 1049 Clone* + Move* + progress persistence
knowledge_faq.go 1932 FAQ CRUD + Search + Export
knowledge_faq_import.go 1939 FAQ import/validate/index + runningFAQImportInfo
knowledge_util.go 352 file-type/url/hash + VLM+Storage config + resolveFileService
Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getOrCreateTagInTarget finds or creates a tag in the target knowledge base based on the source tag.
|
|
|
|
|
// It looks up the source tag by ID, then tries to find a tag with the same name in the target KB.
|
|
|
|
|
// If not found, it creates a new tag with the same properties.
|
|
|
|
|
// The mapping is cached in tagIDMapping for subsequent lookups.
|
|
|
|
|
func (s *knowledgeService) getOrCreateTagInTarget(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
srcTenantID, dstTenantID uint64,
|
|
|
|
|
dstKnowledgeBaseID string,
|
|
|
|
|
srcTagID string,
|
|
|
|
|
tagIDMapping map[string]string,
|
|
|
|
|
) string {
|
|
|
|
|
// Get source tag
|
|
|
|
|
srcTag, err := s.tagRepo.GetByID(ctx, srcTenantID, srcTagID)
|
|
|
|
|
if err != nil || srcTag == nil {
|
|
|
|
|
logger.Warnf(ctx, "Failed to get source tag %s: %v", srcTagID, err)
|
|
|
|
|
tagIDMapping[srcTagID] = "" // Cache empty result to avoid repeated lookups
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to find existing tag with same name in target KB
|
|
|
|
|
dstTag, err := s.tagRepo.GetByName(ctx, dstTenantID, dstKnowledgeBaseID, srcTag.Name)
|
|
|
|
|
if err == nil && dstTag != nil {
|
|
|
|
|
tagIDMapping[srcTagID] = dstTag.ID
|
|
|
|
|
return dstTag.ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create new tag in target KB
|
|
|
|
|
// "未分类" tag should have the lowest sort order to appear first
|
|
|
|
|
sortOrder := srcTag.SortOrder
|
|
|
|
|
if srcTag.Name == types.UntaggedTagName {
|
|
|
|
|
sortOrder = -1
|
|
|
|
|
}
|
|
|
|
|
newTag := &types.KnowledgeTag{
|
|
|
|
|
ID: uuid.New().String(),
|
|
|
|
|
TenantID: dstTenantID,
|
|
|
|
|
KnowledgeBaseID: dstKnowledgeBaseID,
|
|
|
|
|
Name: srcTag.Name,
|
|
|
|
|
Color: srcTag.Color,
|
|
|
|
|
SortOrder: sortOrder,
|
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
|
UpdatedAt: time.Now(),
|
|
|
|
|
}
|
|
|
|
|
if err := s.tagRepo.Create(ctx, newTag); err != nil {
|
|
|
|
|
logger.Warnf(ctx, "Failed to create tag %s in target KB: %v", srcTag.Name, err)
|
|
|
|
|
tagIDMapping[srcTagID] = "" // Cache empty result
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tagIDMapping[srcTagID] = newTag.ID
|
|
|
|
|
logger.Infof(ctx, "Created tag %s (ID: %s) in target KB %s", newTag.Name, newTag.ID, dstKnowledgeBaseID)
|
|
|
|
|
return newTag.ID
|
|
|
|
|
}
|