Browse Source

fix+feat: Tier-0 bugs + P1 PageIndex + 持久化 Agent Memory

Tier-0 Bug 修复:
- FIX-07: lambdagent/__init__.py 导出 EngineMode/EngineResult/RecursiveEngine/CEKEngine/Runtime/RuntimeResult
- FIX-09: knowledge.py 流式 asyncio.Queue 加 maxsize=1000,防慢客户端 OOM
- BUG-06 (trace 断裂): _execute_agent 将 ctx.trace 序列化为 JSON,
  UPDATE runs SET trace_json=?,traces.py 返回结构化步骤列表 + workspace 回退
- FIX-06 扩展: knowledge_bases/kb_files/kb_jobs/api_keys 补加 DB 索引

P1-1 PageIndex(无向量页面级 RAG):
- 新增 _build_page_index(): 按 \f/Page N/固定长度分页 + LLM 摘要 → rag_page_index.json
- 新增 _page_index_search(): token overlap 搜索页面索引
- chunk_strategy 新增 "page",SearchRequest.mode 新增 "pageindex"
- search_kb / search_stream 均支持 pageindex
- 前端 IndexBuilder + QA 面板对应更新

P1-2 Agent 持久化 Memory(Letta 3 层简化版):
- 新增 agentpaas/engine/memory_store.py: load/save Core Memory (JSON) + Recall log (JSONL)
- _execute_agent: 执行前 load_memory(agent_dir),执行后 save_memory(),跨 run 记忆积累

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
kenny67nju 5 months ago
parent
commit
aaea5450dd

+ 35 - 4
agentpaas/api/v1/agents.py

@@ -348,10 +348,11 @@ async def run_agent(
 
         db.execute(
             "UPDATE runs SET status='completed', output=?, duration_ms=?, "
-            "input_tokens=?, output_tokens=?, steps=?, workspace_path=?, completed_at=? WHERE id=?",
+            "input_tokens=?, output_tokens=?, steps=?, workspace_path=?, trace_json=?, completed_at=? WHERE id=?",
             (str(result), duration_ms,
              trace_info.get("input_tokens", 0), trace_info.get("output_tokens", 0),
-             trace_info.get("steps", 0), workspace_path, now_utc(), run_id)
+             trace_info.get("steps", 0), workspace_path,
+             trace_info.get("trace_json", "[]"), now_utc(), run_id)
         )
         db.commit()
 
@@ -458,10 +459,11 @@ async def run_agent_stream(
                 workspace_path = trace_info.get("workspace_path", "")
                 db.execute(
                     "UPDATE runs SET status='completed', output=?, duration_ms=?, "
-                    "input_tokens=?, output_tokens=?, steps=?, workspace_path=?, completed_at=? WHERE id=?",
+                    "input_tokens=?, output_tokens=?, steps=?, workspace_path=?, trace_json=?, completed_at=? WHERE id=?",
                     (str(result), duration_ms,
                      trace_info.get("input_tokens", 0), trace_info.get("output_tokens", 0),
-                     trace_info.get("steps", 0), workspace_path, now_utc(), run_id)
+                     trace_info.get("steps", 0), workspace_path,
+                     trace_info.get("trace_json", "[]"), now_utc(), run_id)
                 )
                 db.commit()
                 event_queue.put({"event": "done", "data": {
@@ -769,17 +771,46 @@ def _execute_agent(config: dict, input_text: str, on_step=None,
 
         term = from_config(config_path, **overrides)
         ctx = Context(workspace_path=workspace_path, run_id=run_id)
+
+        # P1-2: Load persistent Core Memory into ctx.memory before execution
+        from agentpaas.engine.memory_store import load_memory, save_memory
+        if agent_dir:
+            ctx.memory = load_memory(agent_dir)
+
         result = term.apply(input_text, ctx)
 
         # Extract trace info
+        # BUG-06: serialize ctx.trace to JSON for DB persistence
+        import json as _json
+        def _trace_entry_to_dict(e):
+            return {
+                "term_name": getattr(e, 'term_name', ''),
+                "term_id": getattr(e, 'term_id', ''),
+                "input": str(getattr(e, 'input', ''))[:500],
+                "output": str(getattr(e, 'output', ''))[:500],
+                "duration_ms": getattr(e, 'duration_ms', 0),
+                "model": getattr(e, 'model', ''),
+                "tokens_used": getattr(e, 'tokens_used', 0),
+            }
+        try:
+            trace_json = _json.dumps([_trace_entry_to_dict(e) for e in ctx.trace], ensure_ascii=False)
+        except Exception:
+            trace_json = "[]"
+
         trace_info = {
             "steps": len(ctx.trace),
             "input_tokens": sum(getattr(e, 'tokens_used', 0) for e in ctx.trace),
             "output_tokens": 0,
             "total_tokens": sum(getattr(e, 'tokens_used', 0) for e in ctx.trace),
             "workspace_path": workspace_path,
+            "trace_json": trace_json,
         }
 
+        # P1-2: Persist updated Core Memory + append Recall log entry
+        if agent_dir:
+            save_memory(agent_dir, ctx.memory, run_id=run_id,
+                        input_text=input_text, output_text=str(result))
+
         # 保存 run 产物
         if workspace_path:
             save_run_artifacts(

+ 177 - 6
agentpaas/api/v1/knowledge.py

@@ -59,7 +59,7 @@ class AddFilesRequest(BaseModel):
 class IndexBuildRequest(BaseModel):
     index_type: str = Field(default="bm25", description="bm25 | vector | graph | all")
     mode: str = Field(default="full", description="full | incremental")
-    chunk_strategy: str = Field(default="token", description="token | contextual")
+    chunk_strategy: str = Field(default="token", description="token | contextual | page")
 
 
 class WikiBuildRequest(BaseModel):
@@ -69,7 +69,7 @@ class WikiBuildRequest(BaseModel):
 
 class SearchRequest(BaseModel):
     query: str = Field(..., max_length=4096)
-    mode: str = Field(default="lambda", description="lambda | bm25 | vector | wiki | fusion")
+    mode: str = Field(default="lambda", description="lambda | bm25 | vector | wiki | fusion | pageindex")
     top_k: int = Field(default=5, ge=1, le=20)
     weights: Optional[Dict[str, float]] = None  # bm25/vector/graph/wiki overrides
 
@@ -344,6 +344,11 @@ def _run_index_job(db: Database, kb_id: str, job_id: str, kb_root: str, index_ty
                         log_buf.append("[Contextual] 启动上下文增强分块...")
                         _flush("running", log_buf)
                         _contextual_enrich_index(kb_path, scripts_dir, log_buf, db, job_id, _flush)
+                    elif chunk_strategy == "page":
+                        # ── Step 2c: PageIndex build (page-level, no vector) ──
+                        log_buf.append("[PageIndex] 构建页面级索引...")
+                        _flush("running", log_buf)
+                        _build_page_index(kb_path, log_buf, db, job_id, _flush)
                 else:
                     log_buf.append("[BM25] rebuild_index.py / build_index.py 均未找到 — 已跳过")
 
@@ -1274,6 +1279,155 @@ def _start_schedule_checker():
         t.start()
 
 
+# ─── P1: PageIndex — no-vector page-level RAG ───
+
+def _build_page_index(
+    kb_path: Path,
+    log_buf: List[str],
+    db: "Database",
+    job_id: str,
+    flush_fn,
+    provider_id: str = "",
+    model_override: str = "",
+):
+    """Build rag_page_index.json from processed/ text files.
+
+    Each page becomes one index entry:
+      {id, source, page, summary, text, length}
+
+    Pages are detected by '\\f' (form feed) or '--- Page N ---' markers that
+    text extractors like extract_docs.py typically emit. Falls back to
+    splitting every ~3000 chars when no page markers are found.
+
+    The summary is generated by LLM (one call per page); falls back to the
+    first 150 chars of the page text if LLM is unavailable.
+    """
+    processed_dir = kb_path / "processed"
+    if not processed_dir.is_dir():
+        log_buf.append("[PageIndex] processed/ 目录不存在,跳过")
+        return
+
+    txt_files = list(processed_dir.glob("*.txt"))
+    if not txt_files:
+        log_buf.append("[PageIndex] processed/ 中没有 .txt 文件,跳过")
+        return
+
+    # Try to get LLM for summaries (graceful fallback if unavailable)
+    chat_url = headers = model_name = None
+    is_anthropic = False
+    try:
+        from agentpaas.api.v1.providers_api import DEFAULT_PROVIDERS
+        chat_url, headers, model_name, _ = _resolve_llm(provider_id, model_override)
+        pdef = DEFAULT_PROVIDERS.get(provider_id, {})
+        is_anthropic = pdef.get("type") == "anthropic"
+    except Exception:
+        pass  # No LLM → use truncated text as summary
+
+    log_buf.append(f"[PageIndex] 处理 {len(txt_files)} 个文档,生成页面索引...")
+    flush_fn("running", log_buf)
+
+    entries: List[Dict] = []
+    for txt_file in sorted(txt_files):
+        source = txt_file.stem
+        try:
+            text = txt_file.read_text(encoding="utf-8", errors="replace")
+        except Exception:
+            continue
+
+        # Split into pages
+        pages: List[str] = []
+        if "\f" in text:
+            # Form-feed page breaks (PDF extractors)
+            pages = [p.strip() for p in text.split("\f") if p.strip()]
+        else:
+            import re
+            # "--- Page N ---" markers
+            parts = re.split(r"---\s*[Pp]age\s*\d+\s*---", text)
+            if len(parts) > 1:
+                pages = [p.strip() for p in parts if p.strip()]
+            else:
+                # No markers: split every ~3000 chars at paragraph boundary
+                chunk_size = 3000
+                remaining = text
+                while remaining:
+                    if len(remaining) <= chunk_size:
+                        pages.append(remaining.strip())
+                        break
+                    split_at = remaining.rfind("\n\n", 0, chunk_size)
+                    if split_at == -1:
+                        split_at = chunk_size
+                    pages.append(remaining[:split_at].strip())
+                    remaining = remaining[split_at:].strip()
+
+        for page_num, page_text in enumerate(pages, start=1):
+            if not page_text:
+                continue
+            # Generate summary
+            summary = page_text[:150]  # default fallback
+            if chat_url:
+                try:
+                    prompt = [
+                        {"role": "system", "content": "请用1-2句话概括以下页面内容,突出关键信息。"},
+                        {"role": "user", "content": page_text[:1500]},
+                    ]
+                    summary = _call_llm_once(
+                        chat_url, headers, model_name, prompt,
+                        is_anthropic=is_anthropic, max_tokens=100, timeout=15,
+                    ).strip() or page_text[:150]
+                except Exception:
+                    pass
+
+            entries.append({
+                "id": f"{source}::p{page_num}",
+                "source": source,
+                "page": page_num,
+                "summary": summary,
+                "text": page_text,
+                "length": len(page_text),
+            })
+
+    if entries:
+        index_file = kb_path / "rag_page_index.json"
+        index_file.write_text(json.dumps(entries, ensure_ascii=False, indent=2), encoding="utf-8")
+        log_buf.append(f"[PageIndex] ✅ 已生成 {len(entries)} 个页面条目({len(txt_files)} 篇文档)→ rag_page_index.json")
+    else:
+        log_buf.append("[PageIndex] ⚠️ 未生成任何页面条目")
+    flush_fn("running", log_buf)
+
+
+def _page_index_search(kb_root: str, query: str, top_k: int) -> List[Dict]:
+    """BM25-style search over rag_page_index.json (summaries + text).
+
+    Uses a simple TF token overlap score since we avoid importing rank_bm25
+    at the top level. Returns results in {id, source, page, summary, text, score} format.
+    """
+    index_file = Path(kb_root) / "rag_page_index.json"
+    if not index_file.exists():
+        return []
+    try:
+        entries = json.loads(index_file.read_text(encoding="utf-8"))
+    except Exception:
+        return []
+
+    # Tokenise query
+    import re
+    query_tokens = set(re.sub(r"\W+", " ", query.lower()).split())
+    if not query_tokens:
+        return entries[:top_k]
+
+    scored: List[tuple] = []
+    for entry in entries:
+        haystack = f"{entry.get('summary','')} {entry.get('text','')}".lower()
+        score = sum(1 for tok in query_tokens if tok in haystack) / max(len(query_tokens), 1)
+        scored.append((score, entry))
+
+    scored.sort(key=lambda x: -x[0])
+    results = []
+    for score, entry in scored[:top_k]:
+        results.append({**entry, "score": round(score, 4)})
+    return results
+
+
 # ─── Search helpers ───
 
 def _search_subprocess(kb_root: str, scripts_dir: Path, mode: str, query: str, top_k: int) -> List[Dict]:
@@ -1411,11 +1565,15 @@ async def search_kb(
 
     try:
         if req.mode == "fusion":
-            # RAG Fusion: expand query → multi-search → RRF
             results = await asyncio.get_event_loop().run_in_executor(
                 None,
                 lambda: _fusion_search(kb_root, scripts_dir, req.query, req.top_k),
             )
+        elif req.mode == "pageindex":
+            results = await asyncio.get_event_loop().run_in_executor(
+                None,
+                lambda: _page_index_search(kb_root, req.query, req.top_k),
+            )
         else:
             results = await asyncio.get_event_loop().run_in_executor(
                 None,
@@ -1613,7 +1771,18 @@ async def search_stream(
         await asyncio.sleep(0)
 
         chunks: List[Dict] = []
-        if scripts_dir and (Path(kb_root) / "rag_index.json").exists():
+        index_exists = (Path(kb_root) / "rag_index.json").exists()
+        page_index_exists = (Path(kb_root) / "rag_page_index.json").exists()
+
+        if mode == "pageindex" and page_index_exists:
+            try:
+                chunks = await asyncio.get_event_loop().run_in_executor(
+                    None,
+                    lambda: _page_index_search(kb_root, q, top_k)
+                )
+            except Exception as e:
+                yield f"data: {json.dumps({'type': 'status', 'text': f'PageIndex 检索出错: {e}'})}\n\n"
+        elif scripts_dir and index_exists:
             try:
                 if mode == "fusion":
                     chunks = await asyncio.get_event_loop().run_in_executor(
@@ -1628,7 +1797,8 @@ async def search_stream(
             except Exception as e:
                 yield f"data: {json.dumps({'type': 'status', 'text': f'检索出错: {e}'})}\n\n"
         else:
-            yield f"data: {json.dumps({'type': 'status', 'text': 'BM25 索引不存在,请先构建索引'})}\n\n"
+            msg = "PageIndex 不存在,请先以 page 分块策略构建索引" if mode == "pageindex" else "BM25 索引不存在,请先构建索引"
+            yield f"data: {json.dumps({'type': 'status', 'text': msg})}\n\n"
 
         # Emit retrieved chunks so the UI can show sources panel
         yield f"data: {json.dumps({'type': 'chunks', 'chunks': chunks[:top_k]})}\n\n"
@@ -1688,7 +1858,8 @@ async def search_stream(
         try:
             loop = asyncio.get_event_loop()
             # Use asyncio.Queue so the thread can push tokens without blocking the event loop
-            aio_q: asyncio.Queue = asyncio.Queue()
+            # FIX-09: bounded queue prevents OOM from slow clients
+            aio_q: asyncio.Queue = asyncio.Queue(maxsize=1000)
 
             def _fill_q():
                 """Runs in a thread — pushes (kind, val) into the asyncio queue."""

+ 26 - 1
agentpaas/api/v1/traces.py

@@ -10,10 +10,35 @@ router = APIRouter(prefix="/traces", tags=["observability"])
 
 @router.get("/{run_id}")
 async def get_trace(run_id: str, tenant: TenantContext = Depends(get_tenant), db: Database = Depends(get_database)):
+    """Return run row plus structured step-level trace (BUG-06 fix)."""
     run = db.fetchone("SELECT * FROM runs WHERE id = ? AND tenant_id = ?", (run_id, tenant.tenant_id))
     if not run:
         raise HTTPException(404, {"error": {"code": "RUN_NOT_FOUND"}})
-    return run
+
+    # Parse persisted trace JSON (BUG-06)
+    trace_steps = []
+    raw_trace = run.get("trace_json") or "[]"
+    try:
+        trace_steps = json.loads(raw_trace)
+    except (json.JSONDecodeError, TypeError):
+        trace_steps = []
+
+    # If no DB trace, try workspace trace file
+    if not trace_steps and run.get("workspace_path"):
+        import os
+        trace_file = os.path.join(run["workspace_path"], "trace.json")
+        if os.path.isfile(trace_file):
+            try:
+                with open(trace_file, encoding="utf-8") as f:
+                    trace_steps = json.load(f)
+            except Exception:
+                pass
+
+    return {
+        **dict(run),
+        "trace": trace_steps,
+        "trace_count": len(trace_steps),
+    }
 
 
 @router.get("/{run_id}/tools")

+ 16 - 0
agentpaas/db/models.py

@@ -175,6 +175,7 @@ class Database:
             CREATE INDEX IF NOT EXISTS idx_usage_tenant ON usage_records(tenant_id);
             CREATE INDEX IF NOT EXISTS idx_usage_agent ON usage_records(agent_id);
             CREATE INDEX IF NOT EXISTS idx_usage_run ON usage_records(run_id);
+            CREATE INDEX IF NOT EXISTS idx_api_keys_prefix ON api_keys(key_prefix);
         """)
         self.conn.commit()
 
@@ -193,6 +194,8 @@ class Database:
             ("agents", "instance_dir", "ALTER TABLE agents ADD COLUMN instance_dir TEXT"),
             ("runs", "workspace_path", "ALTER TABLE runs ADD COLUMN workspace_path TEXT"),
             ("runs", "idempotency_key", "ALTER TABLE runs ADD COLUMN idempotency_key TEXT"),
+            # BUG-06: persist ctx.trace JSON so /traces/{run_id} returns step-level data
+            ("runs", "trace_json", "ALTER TABLE runs ADD COLUMN trace_json TEXT DEFAULT ''"),
             ("knowledge_bases", "ignore_dirs", "ALTER TABLE knowledge_bases ADD COLUMN ignore_dirs TEXT DEFAULT '[]'"),
         ]
         for table, column, sql in migrations:
@@ -244,6 +247,19 @@ class Database:
             )
         """)
 
+        # Indexes for KB tables (FIX-06 extension)
+        try:
+            self.conn.executescript("""
+                CREATE INDEX IF NOT EXISTS idx_kb_tenant ON knowledge_bases(tenant_id);
+                CREATE INDEX IF NOT EXISTS idx_kbfiles_kb ON kb_files(kb_id);
+                CREATE INDEX IF NOT EXISTS idx_kbjobs_kb ON kb_jobs(kb_id);
+                CREATE INDEX IF NOT EXISTS idx_kbjobs_status ON kb_jobs(status);
+                CREATE INDEX IF NOT EXISTS idx_kbjobs_tenant ON kb_jobs(tenant_id);
+            """)
+            self.conn.commit()
+        except Exception:
+            pass
+
     def execute(self, sql: str, params: tuple = ()) -> sqlite3.Cursor:
         return self.conn.execute(sql, params)
 

+ 143 - 0
agentpaas/engine/memory_store.py

@@ -0,0 +1,143 @@
+"""
+agentpaas.engine.memory_store — Persistent cross-run agent memory.
+
+Implements a simplified version of the Letta 3-layer memory model:
+
+  Layer 1 — Core Memory (always in context):
+      Small structured dict: persona, user facts, task context.
+      Stored in {data_dir}/core_memory.json.
+
+  Layer 2 — Recall Memory (recent interactions):
+      Append-only JSONL log of (run_id, input, output, timestamp).
+      Stored in {data_dir}/recall_log.jsonl.
+      Bounded to last MAX_RECALL entries to control size.
+
+  Layer 3 — Archival Memory (searchable long-term store):
+      Delegated to the Knowledge Base system (BM25 indexes).
+      This module only handles layers 1-2.
+
+Usage in _execute_agent:
+    ctx.memory = load_memory(agent_dir)    # before term.apply()
+    save_memory(agent_dir, ctx.memory, run_id, input_text, str(result))
+"""
+from __future__ import annotations
+import json
+import os
+from pathlib import Path
+from typing import Any, Dict
+
+MAX_RECALL = 50  # keep last N run summaries in recall log
+
+
+def _memory_dir(agent_dir: str) -> Path:
+    """Return the memory sub-directory for an agent, creating it if needed."""
+    p = Path(agent_dir) / "memory"
+    p.mkdir(parents=True, exist_ok=True)
+    return p
+
+
+def load_memory(agent_dir: str) -> Dict[str, Any]:
+    """Load Core Memory dict from {agent_dir}/memory/core_memory.json.
+
+    Returns an empty dict if the file does not exist yet.
+    The dict is injected into ctx.memory before term execution so that
+    Memory(agent, store) terms can read/write it within the run.
+    """
+    if not agent_dir:
+        return {}
+    core_file = _memory_dir(agent_dir) / "core_memory.json"
+    if not core_file.exists():
+        return {}
+    try:
+        return json.loads(core_file.read_text(encoding="utf-8"))
+    except Exception:
+        return {}
+
+
+def save_memory(
+    agent_dir: str,
+    memory: Dict[str, Any],
+    run_id: str = "",
+    input_text: str = "",
+    output_text: str = "",
+) -> None:
+    """Persist Core Memory and append a Recall entry.
+
+    Args:
+        agent_dir:   Agent / instance directory.
+        memory:      ctx.memory dict after run completion.
+        run_id:      Run identifier for recall log.
+        input_text:  User input (truncated to 500 chars in recall).
+        output_text: Agent output (truncated to 500 chars in recall).
+    """
+    if not agent_dir:
+        return
+    mem_dir = _memory_dir(agent_dir)
+
+    # ── Layer 1: save Core Memory ────────────────────────────────
+    try:
+        core_file = mem_dir / "core_memory.json"
+        tmp = core_file.with_suffix(".json.tmp")
+        tmp.write_text(
+            json.dumps(memory, ensure_ascii=False, indent=2),
+            encoding="utf-8",
+        )
+        tmp.replace(core_file)
+    except Exception:
+        pass
+
+    # ── Layer 2: append Recall log ───────────────────────────────
+    if not run_id:
+        return
+    try:
+        from agentpaas.db.models import now_utc
+        entry = json.dumps({
+            "run_id": run_id,
+            "input": input_text[:500],
+            "output": output_text[:500],
+            "ts": now_utc(),
+        }, ensure_ascii=False)
+
+        recall_file = mem_dir / "recall_log.jsonl"
+        existing_lines: list[str] = []
+        if recall_file.exists():
+            existing_lines = recall_file.read_text(encoding="utf-8").splitlines()
+
+        # Trim to MAX_RECALL - 1, then append new entry
+        trimmed = existing_lines[-(MAX_RECALL - 1):] + [entry]
+        recall_file.write_text("\n".join(trimmed) + "\n", encoding="utf-8")
+    except Exception:
+        pass
+
+
+def load_recall_summary(agent_dir: str, n: int = 5) -> str:
+    """Return the last N recall entries as a readable string for prompt injection.
+
+    Example output:
+        [上次对话]
+        Run run_abc: Q: ... A: ...
+        Run run_xyz: Q: ... A: ...
+    """
+    if not agent_dir:
+        return ""
+    recall_file = _memory_dir(agent_dir) / "recall_log.jsonl"
+    if not recall_file.exists():
+        return ""
+    try:
+        lines = recall_file.read_text(encoding="utf-8").splitlines()
+        recent = lines[-n:] if len(lines) >= n else lines
+        entries = []
+        for line in recent:
+            try:
+                d = json.loads(line)
+                entries.append(
+                    f"Run {d.get('run_id','?')} ({d.get('ts','?')[:10]}): "
+                    f"Q: {d.get('input','')[:100]}  A: {d.get('output','')[:100]}"
+                )
+            except Exception:
+                pass
+        if not entries:
+            return ""
+        return "[近期对话记录]\n" + "\n".join(entries)
+    except Exception:
+        return ""

+ 9 - 0
lambdagent/__init__.py

@@ -121,6 +121,12 @@ from .fromconfig import from_config, build_agent, describe_config
 from .fromconfig import from_config as from_config_v2  # backwards compat alias
 from .fromconfig import lint_config, to_lambda_expr
 
+# Phase 6.5: Dual engine — FIX-07: export engine classes
+from .agentruntime.engine import EngineMode, EngineResult, UnifiedTraceRecord
+from .agentruntime.recursive_engine import RecursiveEngine
+from .agentruntime.cek_engine import CEKEngine
+from .agentruntime.runtime import Runtime, RuntimeResult
+
 __all__ = [
     # 元层级
     "Term", "Context", "TraceEntry",
@@ -223,4 +229,7 @@ __all__ = [
     "RetryPolicy", "CircuitBreaker", "CircuitOpenError",
     "with_retry", "with_retry_sync",
     "IsolationLevel", "IsolatedWorkspace", "WorkspaceManager",
+    # Phase 6.5: Dual engine (FIX-07)
+    "EngineMode", "EngineResult", "UnifiedTraceRecord",
+    "RecursiveEngine", "CEKEngine", "Runtime", "RuntimeResult",
 ]

+ 1 - 1
webui/src/api/knowledge.ts

@@ -117,7 +117,7 @@ export const knowledgeApi = {
   removeFile: (kbId: string, fileId: string) =>
     api.delete(`/knowledge/${kbId}/files/${fileId}`),
 
-  buildIndex: (kbId: string, index_type = 'bm25', mode = 'full', chunk_strategy = 'token') =>
+  buildIndex: (kbId: string, index_type = 'bm25', mode = 'full', chunk_strategy: 'token' | 'contextual' | 'page' = 'token') =>
     api.post<{ job_id: string; chunk_strategy: string }>(`/knowledge/${kbId}/index`, { index_type, mode, chunk_strategy }),
   buildWiki: (kbId: string, mode = 'incremental', concurrency = 2) =>
     api.post<{ job_id: string }>(`/knowledge/${kbId}/wiki`, { mode, concurrency }),

+ 10 - 3
webui/src/pages/KnowledgeDetail.tsx

@@ -691,7 +691,7 @@ function IndexStats({ kbId }: { kbId: string }) {
 
 function IndexBuilder({ kbId }: { kbId: string }) {
   const [activeLog, setActiveLog] = useState<string | null>(null)
-  const [chunkStrategy, setChunkStrategy] = useState<'token' | 'contextual'>('token')
+  const [chunkStrategy, setChunkStrategy] = useState<'token' | 'contextual' | 'page'>('token')
   const qc = useQueryClient()
 
   const { data: jobsData, refetch: refetchJobs } = useQuery({
@@ -748,17 +748,23 @@ function IndexBuilder({ kbId }: { kbId: string }) {
           <label className="text-xs font-medium text-gray-600">分块策略:</label>
           <select
             value={chunkStrategy}
-            onChange={e => setChunkStrategy(e.target.value as 'token' | 'contextual')}
+            onChange={e => setChunkStrategy(e.target.value as 'token' | 'contextual' | 'page')}
             className="text-xs border border-gray-300 rounded px-2 py-1"
           >
             <option value="token">Token 分块(默认)</option>
             <option value="contextual">Contextual 分块(LLM 增强)</option>
+            <option value="page">PageIndex(页面级,无向量)</option>
           </select>
           {chunkStrategy === 'contextual' && (
             <span className="text-xs text-amber-600 bg-amber-50 px-2 py-0.5 rounded">
               每篇文档额外 1 次 LLM 调用生成摘要
             </span>
           )}
+          {chunkStrategy === 'page' && (
+            <span className="text-xs text-blue-600 bg-blue-50 px-2 py-0.5 rounded">
+              构建 rag_page_index.json,使用 pageindex 模式搜索
+            </span>
+          )}
         </div>
         <Button variant="secondary" size="sm" icon={<RefreshCw size={13}/>}
           onClick={() => { refetchJobs(); qc.invalidateQueries({ queryKey: ['kb-detail', kbId] }) }}>
@@ -1127,7 +1133,7 @@ function QAPanel({
   }, [state.answer, state.streaming])
 
   const modeLabel: Record<string, string> = {
-    bm25: 'BM25', lambda: 'LambdaRAG', vector: '向量', wiki: 'Wiki', fusion: 'RAG Fusion',
+    bm25: 'BM25', lambda: 'LambdaRAG', vector: '向量', wiki: 'Wiki', fusion: 'RAG Fusion', pageindex: 'PageIndex',
   }
 
   return (
@@ -1283,6 +1289,7 @@ function SearchTest({ kbId }: { kbId: string }) {
     { value: 'vector', label: '向量语义' },
     { value: 'wiki', label: 'Wiki 全文' },
     { value: 'fusion', label: 'RAG Fusion(多查询+RRF)' },
+    { value: 'pageindex', label: 'PageIndex(页面级无向量)' },
   ]
 
   const startQA = (side: 'left' | 'right', mode: string) => {