feat(bizdev): 成功解析的材料一律写入 material_chunks
大小阈值只决定全文注入还是目录加检索片段,不再决定是否切块。上传后的异步解析同样跳过 60KB 门槛。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -17,7 +17,8 @@ import (
|
||||
|
||||
// ResolveMaterials 将请求材料 + 项目 CAS 资产解析为可注入工作区的 squad 材料。
|
||||
// 项目内全部有效资产自动纳入(含私密文档);显式附件优先并去重。
|
||||
// 已配置 MinerU 时优先使用解析缓存;大文档改为目录 + RAG 片段注入。
|
||||
// 已配置 MinerU 时优先使用解析缓存。解析成功的材料一律切块写入 material_chunks;
|
||||
// 正文不超过全文阈值时仍全文注入,超过阈值时改为目录 + RAG 片段注入。
|
||||
func (s *Service) ResolveMaterials(ctx context.Context, req TaskRunRequest) ([]squad.MaterialItem, map[string]any, error) {
|
||||
manifest := map[string]any{
|
||||
"explicit_materials": len(req.Materials),
|
||||
@@ -173,6 +174,15 @@ func (s *Service) materialToItems(
|
||||
base = "material.bin"
|
||||
}
|
||||
|
||||
// 切块入库与注入方式分开:任何成功解析的正文都写入 material_chunks,失败只记日志。
|
||||
if s.materialIndex != nil {
|
||||
if err := s.materialIndex.IndexChunks(ctx, projectID, blobSHA, base, text); err != nil {
|
||||
slog.Warn("material chunk index failed", "file", base, "project_id", projectID, "err", err)
|
||||
} else {
|
||||
meta["chunks_indexed"] = true
|
||||
}
|
||||
}
|
||||
|
||||
// 小文档:全文注入,文件名改为 .md 避免 workspace 二次按扩展名刮串冲掉页锚点
|
||||
if len(text) <= fullMax {
|
||||
outName := textMaterialName(base)
|
||||
@@ -191,7 +201,6 @@ func (s *Service) materialToItems(
|
||||
toc := materialindex.BuildTOC(base, text)
|
||||
ragBody := ""
|
||||
if s.materialIndex != nil {
|
||||
_ = s.materialIndex.IndexChunks(ctx, projectID, blobSHA, base, text)
|
||||
hits, searchErr := s.materialIndex.Search(ctx, projectID, blobSHA, ragQuery, 0)
|
||||
if searchErr == nil {
|
||||
ragBody = materialindex.FormatRAGMarkdown(base, hits)
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package bizdev
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"chat-agent-system/internal/config"
|
||||
"chat-agent-system/internal/materialindex"
|
||||
)
|
||||
|
||||
func TestMaterialToItems_SmallDocStillFulltextAfterIndex(t *testing.T) {
|
||||
svc := &Service{materialIndex: materialindex.NewPipeline(&config.Config{}, nil)}
|
||||
items, meta, err := svc.materialToItems(context.Background(), "note.txt", "abc123", []byte("能量参数是 2.5 bar"), 0, 0, 7, "能量")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if meta["inject_mode"] != "fulltext" {
|
||||
t.Fatalf("small doc inject_mode=%v", meta["inject_mode"])
|
||||
}
|
||||
if meta["chunks_indexed"] != true {
|
||||
t.Fatalf("small doc must attempt chunk index, meta=%v", meta)
|
||||
}
|
||||
if len(items) != 1 || !strings.Contains(string(items[0].Content), "能量参数是 2.5 bar") {
|
||||
t.Fatalf("full text missing: %+v", items)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaterialToItems_LargeDocIndexesBeforeRAGInject(t *testing.T) {
|
||||
svc := &Service{materialIndex: materialindex.NewPipeline(&config.Config{MaterialFulltextMaxBytes: 8}, nil)}
|
||||
body := []byte("能量参数是 2.5 bar,并且还有更多正文用来越过全文阈值。")
|
||||
items, meta, err := svc.materialToItems(context.Background(), "note.txt", "def456", body, 0, 0, 7, "能量")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if meta["chunks_indexed"] != true {
|
||||
t.Fatalf("large doc must attempt chunk index, meta=%v", meta)
|
||||
}
|
||||
if meta["inject_mode"] != "rag" && meta["inject_mode"] != "rag_degraded" {
|
||||
t.Fatalf("large doc must not use fulltext, inject_mode=%v", meta["inject_mode"])
|
||||
}
|
||||
if len(items) != 2 {
|
||||
t.Fatalf("want toc+rag items, got %d", len(items))
|
||||
}
|
||||
}
|
||||
@@ -284,10 +284,8 @@ func (p *Pipeline) EnqueueAsync(fileName string, blobSHA string, data []byte, pr
|
||||
if err != nil || !ok || strings.TrimSpace(md) == "" {
|
||||
return
|
||||
}
|
||||
if utf8.RuneCountInString(md) > 0 && len(md) > p.FulltextMaxBytes() {
|
||||
if idxErr := p.IndexChunks(ctx, projectID, blobSHA, fileName, md); idxErr != nil {
|
||||
slog.Warn("materialindex index chunks failed", "sha", blobSHA[:min(12, len(blobSHA))], "error", idxErr)
|
||||
}
|
||||
if idxErr := p.IndexChunks(ctx, projectID, blobSHA, fileName, md); idxErr != nil {
|
||||
slog.Warn("materialindex index chunks failed", "sha", blobSHA[:min(12, len(blobSHA))], "error", idxErr)
|
||||
}
|
||||
}()
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user