feat(backthink/admin): ③b — rag/delete 真实现 + D1 推送出口钩子(自建优先→个推兜底) + api-reference 补 D2/D3/D5 补丁
- B5: POST /admin/rag/delete 真做(DeleteDocument→DeleteProject 级联删章节+索引清理, owner+审计, 200/404/500, 500降级兜底); users/iot scenarios+rules 诚实501(无用户表/IoT场景规则模型, 消息写明缺什么, 不伪造), 锁住 501+not_implemented
- D1: backthink/push_outlet.go PushExporter{Export}; pushOutlet 自建HTTP(2xx=送达, token仅Authorization头+error脱敏)优先→Getui兜底(未接入, 诚实日志'getui not integrated'+(false,nil)); backthink.Deps.Exporter → pusher.withExporter, Push 在WS/IM后 exportAsync(detached+5s超时, 失败仅日志, 不破坏现有投递, 限流不上报); config PushOutletConfig(PUSH_EXPORTER_* + GETUI_*占位)
- 前端 Rag.tsx/api.ts 删除后 reload + 类型; Docs/api-client v0.31 + api-reference(#23 owner/200-404, §8.4, 端点表PATCH/DELETE messages + pinned + messages?q=)
- 测试(push_outlet/admin_rag_delete/admin_honest501/knowledge store), 后端+前端build全绿+3平台交叉编译
- 刻意: Getui SDK 未接入(无SDK不硬造), 主人自建服务填 PUSH_EXPORTER_URL/TOKEN 即可
This commit is contained in:
1 parent
844b2a74c1
commit
2dee372fb2
23 files changed
+927
-95
No files matched your search
@@ -15,9 +15,9 @@
|
||||
// stores it as a project plus its chapters, making it text/keyword-searchable
|
||||
// immediately. Semantic/vector embedding of an imported document is a graceful
|
||||
// no-op here (this data source holds no vector index); the existing semantic path
|
||||
// still ranks project-linked memories via the retriever. Deletion remains
|
||||
// unimplemented (M10) and returns ErrWriteUnsupported so the admin handler
|
||||
// answers honestly instead of fabricating a success.
|
||||
// still ranks project-linked memories via the retriever. DeleteDocument removes a
|
||||
// document (its project entry and, in one transaction, every chapter that
|
||||
// references it) so the import/delete pair is a real round-trip.
|
||||
package knowledge
|
||||
|
||||
import (
|
||||
@@ -36,10 +36,15 @@ import (
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/memory/projectindex"
|
||||
)
|
||||
|
||||
// ErrWriteUnsupported is returned by DeleteDocument. Deletion is milestone M10;
|
||||
// the admin handler maps it to a clear 501 so the frontend can explain rather
|
||||
// than show a placeholder alert.
|
||||
var ErrWriteUnsupported = errors.New("knowledge: delete is not implemented in this milestone")
|
||||
// ErrWriteUnsupported is returned by a knowledge store implementation that does
|
||||
// not support document deletion. *Store implements DeleteDocument, so it never
|
||||
// returns this; it remains for alternate/degraded implementations the admin
|
||||
// handler maps to a clear 501 rather than a fabricated success.
|
||||
var ErrWriteUnsupported = errors.New("knowledge: delete is not supported by this store")
|
||||
|
||||
// ErrDocumentNotFound is returned by DeleteDocument when the id is blank or no
|
||||
// knowledge document (project-index entry) carries it.
|
||||
var ErrDocumentNotFound = errors.New("knowledge: document not found")
|
||||
|
||||
// defaultTopK bounds the semantic retrieval path.
|
||||
const defaultTopK = 8
|
||||
@@ -322,10 +327,27 @@ func (s *Store) IndexDocument(ctx context.Context, doc Doc) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteDocument is not implemented in this milestone. It returns
|
||||
// ErrWriteUnsupported so the handler answers honestly (delete = M10).
|
||||
func (s *Store) DeleteDocument(_ context.Context, _ string) error {
|
||||
return ErrWriteUnsupported
|
||||
// DeleteDocument removes a knowledge document: the project-index entry and, in
|
||||
// the same transaction, every chapter that references it (projectindex.DeleteProject
|
||||
// cascades the chunk cleanup). It returns ErrDocumentNotFound for a blank id or
|
||||
// one with no project entry, and an error when no project index is wired (there
|
||||
// is nowhere to delete from, so a fabricated success is never returned).
|
||||
func (s *Store) DeleteDocument(ctx context.Context, id string) error {
|
||||
docID := strings.TrimSpace(id)
|
||||
if docID == "" {
|
||||
return fmt.Errorf("knowledge: delete document: %w", ErrDocumentNotFound)
|
||||
}
|
||||
if s.projects == nil {
|
||||
return errors.New("knowledge: delete document: project index store is not configured")
|
||||
}
|
||||
if err := s.projects.DeleteProject(ctx, docID); err != nil {
|
||||
if errors.Is(err, projectindex.ErrProjectNotFound) {
|
||||
return fmt.Errorf("knowledge: delete document %q: %w", docID, ErrDocumentNotFound)
|
||||
}
|
||||
return fmt.Errorf("knowledge: delete document %q: %w", docID, err)
|
||||
}
|
||||
s.log.Info("deleted document", "project", docID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// maxChapterRunes bounds a single chapter's content length. Paragraphs longer
|
||||
|
||||
@@ -99,10 +99,56 @@ func TestIndexDocumentRequiresProjectStore(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteDocumentStillUnsupported(t *testing.T) {
|
||||
// TestDeleteDocumentRemovesProjectAndChapters proves delete is a real round-trip:
|
||||
// the document disappears from the list and the search, and deleting it again is
|
||||
// a not-found rather than a silent success.
|
||||
func TestDeleteDocumentRemovesProjectAndChapters(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := New(Deps{Log: testLogger()})
|
||||
if err := s.DeleteDocument(context.Background(), "any"); !errors.Is(err, ErrWriteUnsupported) {
|
||||
t.Fatalf("DeleteDocument err = %v, want ErrWriteUnsupported", err)
|
||||
ctx := context.Background()
|
||||
s := newTestStore(t)
|
||||
if err := s.IndexDocument(ctx, Doc{
|
||||
ID: "doc-1",
|
||||
Name: "Cyrene 设计",
|
||||
Content: "第一段:项目描述。\n\n第二段:内部实现要点。",
|
||||
}); err != nil {
|
||||
t.Fatalf("IndexDocument: %v", err)
|
||||
}
|
||||
|
||||
if err := s.DeleteDocument(ctx, "doc-1"); err != nil {
|
||||
t.Fatalf("DeleteDocument: %v", err)
|
||||
}
|
||||
docs, err := s.ListDocuments(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListDocuments: %v", err)
|
||||
}
|
||||
if len(docs) != 0 {
|
||||
t.Fatalf("documents = %+v, want empty after delete", docs)
|
||||
}
|
||||
hits, err := s.Search(ctx, "内部实现")
|
||||
if err != nil {
|
||||
t.Fatalf("Search: %v", err)
|
||||
}
|
||||
if len(hits) != 0 {
|
||||
t.Fatalf("hits = %+v, want none after delete", hits)
|
||||
}
|
||||
|
||||
if err := s.DeleteDocument(ctx, "doc-1"); !errors.Is(err, ErrDocumentNotFound) {
|
||||
t.Fatalf("second DeleteDocument err = %v, want ErrDocumentNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteDocumentBlankIDNotFound(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := newTestStore(t)
|
||||
if err := s.DeleteDocument(context.Background(), " "); !errors.Is(err, ErrDocumentNotFound) {
|
||||
t.Fatalf("DeleteDocument blank id err = %v, want ErrDocumentNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteDocumentRequiresProjectStore(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := New(Deps{Log: testLogger()}) // projects == nil
|
||||
if err := s.DeleteDocument(context.Background(), "doc-1"); err == nil {
|
||||
t.Fatal("DeleteDocument without a project index should error")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user