Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ci-paths.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ignore = [

[suites]

knowledge_base = ["evaluation/knowledge_base/**", "frontend/knowledge-base/**", "plugins/knowledge-base/**", "tests/knowledge_base/**", "tests/e2e/test_knowledge_base*.py"]
knowledge_base = ["evaluation/knowledge_base/**", "plugins/knowledge-base/**", "tests/knowledge_base/**", "tests/e2e/test_knowledge_base*.py"]

gateway = ["gateway/**", ".github/workflows/gateway.yml", "tests/e2e/gateway_tck.py", "tests/e2e/test_gateway_authentication.py", "tests/e2e/test_gateway_http1_lifecycle.py"]

Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1936,9 +1936,9 @@ jobs:
- uses: ./.github/actions/cache-npm
id: dashboard-npm-cache
with:
cache-dependency-glob: frontend/knowledge-base/package-lock.json
cache-dependency-glob: plugins/knowledge-base/frontend/package-lock.json
- name: Install Dashboard dependencies
run: node "$PC_CI_NODE_INSTALLER" npm ci --prefix frontend/knowledge-base --no-audit --no-fund
run: node "$PC_CI_NODE_INSTALLER" npm ci --prefix plugins/knowledge-base/frontend --no-audit --no-fund
- name: Save Dashboard npm downloads
if: steps.dashboard-npm-cache.outputs.cache-hit != 'true'
uses: ./.github/actions/cache-save
Expand All @@ -1947,10 +1947,10 @@ jobs:
key: ${{ steps.dashboard-npm-cache.outputs.cache-key }}
- name: Build and verify plugin Dashboard assets
run: |
npm run build --prefix frontend/knowledge-base
git diff --exit-code -- plugins/knowledge-base/ui
npm run build --prefix plugins/knowledge-base/frontend
git diff --exit-code -- plugins/knowledge-base/runtime/ui
- name: Check plugin types with its optional dependencies
run: uv run --no-sync ty check --no-force-exclude plugins/knowledge-base
run: uv run --no-sync ty check --no-force-exclude plugins/knowledge-base/runtime
- name: Identify the Playwright browser cache
id: browser-key
run: |
Expand Down
2 changes: 1 addition & 1 deletion .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ header:
- 'examples/jupyter/*.ipynb'
- '**/*.svg'
- '.github/**'
- 'frontend/knowledge-base/.npmrc'
- 'plugins/knowledge-base/frontend/.npmrc'
- '**/.gitignore'
- '**/.dockerignore'
- '**/.env.example'
Expand Down
14 changes: 14 additions & 0 deletions docs/en/docs/develop/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ powercontext server run
`uv pip install pypdf`. A `requirements.txt` in the plugin directory only informs deployers; Server does not install it.
- Adding, changing, or removing a plugin directory needs no Server restart; see "Update plugins".

### First-party plugin source layout

Keep each first-party plugin's implementation in `plugins/<plugin-name>/` relative to the repository root. The
configured installation directory is a separate path chosen by the deployer. When its Dashboard is built separately, use
`frontend/` for React/TypeScript sources, the lockfile and build tooling, and `runtime/` for the contents installed under
the plugin name (`plugin.py`, the Python package and the built `ui/` assets). The frontend build writes checked-in assets
to `runtime/ui/`.

Only the **contents** of `runtime/` are copied into `<plugins-dir>/<plugin-name>/`; copying the directory itself would
give the plugin the wrong identity. Build tooling and `node_modules` therefore never participate in plugin discovery,
fingerprinting or the runtime snapshot. A Python-only first-party plugin may keep `plugin.py` directly in its named
directory. Host integrations and the core Dashboard keep their own packaging conventions; this layout applies only to
first-party Server plugins. See the [knowledge-base plugin](../workflows/knowledge-base.md) for a staged install example.

Check what loaded with administrator credentials:

```bash
Expand Down
5 changes: 4 additions & 1 deletion docs/en/docs/workflows/knowledge-base.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ Dashboard document operations use the framework `client.artifacts` bridge.
Run from a repository checkout. These commands create a separate local instance, install optional model/OCR dependencies,
and bind the Server to loopback. The first embedding call downloads the pinned public model; provision its cache before
using an offline deployment. Server and Worker must use the same Python environment, plugin code and local data directory.
The plugin is staged from `plugins/knowledge-base/runtime/`: do not point `POWERCONTEXT_SERVER_PLUGINS_DIR` directly at the
checkout's `plugins/`, because the frontend sources and `node_modules` must not become part of the plugin fingerprint.

```bash
uv sync --locked --extra knowledge-base --extra server --extra cli
mkdir -p "$PWD/.powercontext/kb-demo/plugins"
cp -R plugins/knowledge-base "$PWD/.powercontext/kb-demo/plugins/knowledge-base"
mkdir -p "$PWD/.powercontext/kb-demo/plugins/knowledge-base"
cp -R plugins/knowledge-base/runtime/. "$PWD/.powercontext/kb-demo/plugins/knowledge-base/"
export POWERCONTEXT_KB_DATA_DIR="$PWD/.powercontext/kb-demo/data"
export POWERCONTEXT_KB_IMPORT_ROOTS="$PWD/docs"
export POWERCONTEXT_SERVER_PLUGINS_DIR="$PWD/.powercontext/kb-demo/plugins"
Expand Down
9 changes: 9 additions & 0 deletions docs/zh/docs/develop/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ powercontext server run
部署者看的说明,Server 不会自动安装。
- 新增、修改或删除插件目录不需要重启 Server,见"更新插件"。

### 一方插件源码布局

一方插件的实现集中在仓库根目录下的 `plugins/<插件名>/`。配置的安装目录由部署者另行选择。如果 Dashboard 需要单独构建,使用 `frontend/` 存放 React/TypeScript 源码、锁文件和构建工具,
使用 `runtime/` 存放按插件名安装的内容(`plugin.py`、Python 包和构建后的 `ui/` 资源)。前端构建把资源写到 `runtime/ui/`。

安装时只把 `runtime/` 的**内容**复制到 `<plugins-dir>/<插件名>/`;复制 `runtime` 目录本身会让插件身份错误。构建工具和
`node_modules` 因此不会进入插件发现、指纹或运行时快照。纯 Python 的一方插件可以把 `plugin.py` 直接放在自己的命名目录中。宿主集成和核心 Dashboard 使用各自的打包约定,
本布局只适用于一方 Server 插件。分步安装示例见[知识库插件](../workflows/knowledge-base.md)。

用管理员凭据查看加载结果:

```bash
Expand Down
5 changes: 4 additions & 1 deletion docs/zh/docs/workflows/knowledge-base.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ Dashboard 文档操作使用框架 `client.artifacts` 桥接。

从仓库检出目录执行。以下命令创建独立本地实例、安装可选模型/OCR 依赖,并仅监听本机。
首次向量调用下载固定版本的公开模型;离线部署前应准备模型缓存。Server 与 Worker 必须使用相同的 Python 环境、插件代码和本地数据目录。
插件从 `plugins/knowledge-base/runtime/` 分步安装:不要把 `POWERCONTEXT_SERVER_PLUGINS_DIR` 直接指向 checkout 的
`plugins/`,否则前端源码和 `node_modules` 会进入插件指纹。

```bash
uv sync --locked --extra knowledge-base --extra server --extra cli
mkdir -p "$PWD/.powercontext/kb-demo/plugins"
cp -R plugins/knowledge-base "$PWD/.powercontext/kb-demo/plugins/knowledge-base"
mkdir -p "$PWD/.powercontext/kb-demo/plugins/knowledge-base"
cp -R plugins/knowledge-base/runtime/. "$PWD/.powercontext/kb-demo/plugins/knowledge-base/"
export POWERCONTEXT_KB_DATA_DIR="$PWD/.powercontext/kb-demo/data"
export POWERCONTEXT_KB_IMPORT_ROOTS="$PWD/docs"
export POWERCONTEXT_SERVER_PLUGINS_DIR="$PWD/.powercontext/kb-demo/plugins"
Expand Down
2 changes: 1 addition & 1 deletion evaluation/knowledge_base/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from evaluation.knowledge_base.legacy import RECORD

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE.parents[1] / "plugins" / "knowledge-base"))
sys.path.insert(0, str(HERE.parents[1] / "plugins" / "knowledge-base" / "runtime"))
Comment thread
Zxf-xufeng marked this conversation as resolved.
Resolved
from pc_knowledge_base.content import CAPABILITIES, OCR_ASSETS, parse_document, parser_profile # noqa: E402
from pc_knowledge_base.embedding import Embedding # noqa: E402
from pc_knowledge_base.parser_contract import ParsedDocument, context_locator # noqa: E402
Expand Down
13 changes: 12 additions & 1 deletion evaluation/knowledge_base/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ def main() -> int:
parser.add_argument("checkout", type=Path, help="Checkout whose legacy parse entry point will be exercised")
parser.add_argument("--export", type=Path, help="Save the exported hashes instead of comparing with the baseline")
args = parser.parse_args()
sys.path.insert(0, str(args.checkout.resolve() / "plugins" / "knowledge-base"))
plugin_root = args.checkout.resolve() / "plugins" / "knowledge-base"
import_root = next(
(
candidate
for candidate in (plugin_root / "runtime", plugin_root)
if (candidate / "pc_knowledge_base").is_dir()
),
None,
)
if import_root is None:
raise SystemExit(f"No pc_knowledge_base package under {plugin_root}") # noqa: TRY003
sys.path.insert(0, str(import_root))
parse = import_module("pc_knowledge_base.content").parse
files, _ = generate()
cases = {"service.md": files["service.md"], "service.markdown": files["service.md"]}
Expand Down
2 changes: 1 addition & 1 deletion evaluation/knowledge_base/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@
"unicode": "15.0.0"
},
"schema_version": 1,
"scorer_sha256": "09a7dfe6631006830ff97443cd9f6bdfc3d9d4d0c3190d0298247d0b92185ad9",
"scorer_sha256": "112a170237fef0cdd85e0062d814c11dc431f1ddf76e81858b3a7327ab797a9c",
"thresholds": {
"coverage": 1.0,
"min_text_similarity": 1.0,
Expand Down
9 changes: 9 additions & 0 deletions plugins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# First-party Server plugins

Each subdirectory is the source root of one first-party PowerContext Server plugin. A plugin with a separately built
Dashboard keeps its frontend sources under `frontend/` and the installable files under `runtime/`; only the contents of
`runtime/` are copied into `<configured-plugins-dir>/<plugin-name>/`, so `plugin.py` is directly inside the named
directory. For example, the knowledge-base runtime is installed at `<configured-plugins-dir>/knowledge-base/`.

- [Knowledge-base plugin](knowledge-base/README.md)
- [Write a plugin](../docs/en/docs/develop/plugins.md) · [编写插件](../docs/zh/docs/develop/plugins.md)
Comment on lines +3 to +9

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep plugins/README.md scoped as a directory index instead of describing frontend//runtime/ layout details or plugin-specific installation paths here. Different first-party plugins can have different subdirectory layouts (for example, Python-only plugins without frontend/ or runtime/) and different installation steps; those details belong in each plugin's own README and the plugin development guides.

Suggested change
Each subdirectory is the source root of one first-party PowerContext Server plugin. A plugin with a separately built
Dashboard keeps its frontend sources under `frontend/` and the installable files under `runtime/`; only the contents of
`runtime/` are copied into `<configured-plugins-dir>/<plugin-name>/`, so `plugin.py` is directly inside the named
directory. For example, the knowledge-base runtime is installed at `<configured-plugins-dir>/knowledge-base/`.
- [Knowledge-base plugin](knowledge-base/README.md)
- [Write a plugin](../docs/en/docs/develop/plugins.md) · [编写插件](../docs/zh/docs/develop/plugins.md)
Each subdirectory holds the source of one first-party PowerContext Server plugin. Follow each plugin's README for its
layout, build, and installation steps, and see the plugin development guide for general conventions.
## Plugins
- [Knowledge-base](knowledge-base/README.md)
## Guides
- [Write a plugin](../docs/en/docs/develop/plugins.md) · [编写插件](../docs/zh/docs/develop/plugins.md)

17 changes: 15 additions & 2 deletions plugins/knowledge-base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ A knowledge base is a core Scope; spaces and their membership/defaults live only
- [中文指南](../../docs/zh/docs/workflows/knowledge-base.md)
- [Design RFC #2570](https://github.com/oceanbase/powercontext/pull/2570)

Copy this **whole directory**, keeping its name `knowledge-base`, into the configured Server plugin directory.
Copy the **contents** of `runtime/` into a directory named `knowledge-base` in the configured Server plugin directory.
Copying the `runtime` directory itself would give the plugin the wrong identity:

```bash
mkdir -p "$POWERCONTEXT_SERVER_PLUGINS_DIR/knowledge-base"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Make the installation destination explicit before creating it. A Server configured through its service environment can have a plugin directory even when the shell running these commands has no POWERCONTEXT_SERVER_PLUGINS_DIR. In that shell the example expands to mkdir -p /knowledge-base and copies the runtime there. It fails for a typical unprivileged user or installs outside the configured directory if the account can write to /. Please show how to set the variable to the actual Server plugin directory, or guard the snippet before either filesystem operation with : "${POWERCONTEXT_SERVER_PLUGINS_DIR:?Set this to the Server plugin directory}". The named knowledge-base/ destination itself is correct.

cp -R plugins/knowledge-base/runtime/. "$POWERCONTEXT_SERVER_PLUGINS_DIR/knowledge-base/"
```

The Dashboard is built from `frontend/` and the result is checked in under `runtime/ui/`:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put the optional Dashboard build before the install step and state that both examples run from the repository root. Someone installing a checkout can use the assets already committed under runtime/ui/. Someone who edited the frontend needs to rebuild before copying runtime/. Following the examples in their current order installs the old assets and leaves the rebuilt files in the checkout.


```bash
npm ci --prefix plugins/knowledge-base/frontend
npm run build --prefix plugins/knowledge-base/frontend
```
Install `powercontext[server,knowledge-base]` into the same environment used by Server and Workers. The plugin is
copy-installed rather than automatically enabled by installing the Python extra.

Expand All @@ -16,4 +29,4 @@ and [#2660](https://github.com/oceanbase/powercontext/pull/2660). The plugin doe
It uses `ctx.storage`, the framework writer, and the Dashboard `client.artifacts` bridge.

The bundled Dashboard provides a document table, separate chunk reader, retrieval testing and settings.
Frontend development and rebuild instructions are in [frontend/knowledge-base](../../frontend/knowledge-base/README.md).
Frontend development and rebuild instructions are in [frontend](frontend/README.md).
Comment thread
Zxf-xufeng marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Knowledge-base Dashboard

React and TypeScript sources for the knowledge-base plugin's sandboxed Dashboard page. The Server serves the
checked-in assets in `plugins/knowledge-base/ui`; production does not require Node or an extra frontend server.
checked-in assets in `plugins/knowledge-base/runtime/ui`; production does not require Node or an extra frontend server.

From the repository root, with Node 22:

```sh
npm ci --prefix frontend/knowledge-base
npm run build --prefix frontend/knowledge-base
npm ci --prefix plugins/knowledge-base/frontend
npm run build --prefix plugins/knowledge-base/frontend
```

Commit the source, lockfile and rebuilt assets together. The knowledge-base CI job rebuilds and checks for drift.
Keep `node_modules` outside the plugin directory: the framework hashes the complete plugin directory when loading it.
Keep `node_modules` under `frontend/` and install only the contents of `runtime/`: the framework hashes the installed plugin directory when loading it.
The build emits a classic script for the opaque-origin iframe, keeps third-party license notices, and checks the
framework's 5 MiB per-asset limit.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { readFile, writeFile, stat } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import path from "node:path";
const root = path.dirname(fileURLToPath(import.meta.url));
const output = path.resolve(root, "../../plugins/knowledge-base/ui");
const output = path.resolve(root, "../runtime/ui");
const license = (await readFile(path.join(root, "src/main.tsx"), "utf8")).split("*/", 1)[0] + "*/";
await build({
absWorkingDir: root,
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ only-include = ["src", "pyproject.toml", "README.md", "LICENSE", "LICENSES"]
python = "./.venv"
python-version = "3.11"
extra-paths = [
"./plugins/knowledge-base",
"./plugins/knowledge-base/runtime",
"./scripts",
"./deploy/observability",
"./desktop/tests",
Expand All @@ -263,7 +263,7 @@ extra-paths = [
[tool.ty.src]
exclude = [
# The plugin CI installs optional OCR/model dependencies and checks this directory explicitly.
"plugins/knowledge-base",
"plugins/knowledge-base/runtime",
# README asset renderers need Playwright and numpy, installed separately when regenerating images.
"docs/assets",
# Optional Jupyter dependencies and notebook state are validated by notebooks-test.
Expand Down
7 changes: 6 additions & 1 deletion tests/e2e/test_knowledge_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
pytest.importorskip("opendal")

P = "/v1/plugins/knowledge-base/api"
PLUGIN = Path(__file__).resolve().parents[2] / "plugins" / "knowledge-base"
PLUGIN = Path(__file__).resolve().parents[2] / "plugins" / "knowledge-base" / "runtime"


@pytest.mark.parametrize("real_model", [False, True], ids=["fixture-embedding", "real-embedding"])
Expand All @@ -41,6 +41,11 @@ def test_knowledge_base_import_edit_forget_and_space_selection(tmp_path, real_mo
pytest.importorskip("fastembed")
directory = tmp_path / "plugins"
shutil.copytree(PLUGIN, directory / "knowledge-base")
installed = directory / "knowledge-base"
assert (installed / "plugin.py").is_file()
assert not (installed / "runtime").exists()
assert not (installed / "frontend").exists()
assert not (installed / "node_modules").exists()
if not real_model:
entry = directory / "knowledge-base" / "plugin.py"
entry.write_text(entry.read_text() + FIXTURE_MODEL)
Expand Down
2 changes: 1 addition & 1 deletion tests/knowledge_base/test_content_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import pytest

sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "plugins" / "knowledge-base"))
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "plugins" / "knowledge-base" / "runtime"))
from pc_knowledge_base.content import ContentError, Snapshots, parse, relative_path
from pc_knowledge_base.store import Store

Expand Down
2 changes: 1 addition & 1 deletion tests/knowledge_base/test_context_locators.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from pathlib import Path
from types import SimpleNamespace

sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "plugins" / "knowledge-base"))
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "plugins" / "knowledge-base" / "runtime"))
from pc_knowledge_base.parser_contract import CONTEXT_LOCATOR_MAX_BYTES
from pc_knowledge_base.service import KnowledgeBase
from pc_knowledge_base.store import Store
Expand Down
2 changes: 1 addition & 1 deletion tests/knowledge_base/test_identity_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import pytest
from sqlalchemy import text

sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "plugins" / "knowledge-base"))
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "plugins" / "knowledge-base" / "runtime"))
from pc_knowledge_base.models import identity
from pc_knowledge_base.service import KnowledgeBase
from pc_knowledge_base.store import Store, defaults, spaces
Expand Down
2 changes: 1 addition & 1 deletion tests/knowledge_base/test_ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import pytest
from PIL import Image, ImageDraw, ImageFont

sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "plugins" / "knowledge-base"))
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "plugins" / "knowledge-base" / "runtime"))
from pc_knowledge_base.content import parse


Expand Down
2 changes: 1 addition & 1 deletion tests/knowledge_base/test_parser_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from PIL import Image, ImageDraw, ImageFont
from pydantic import ValidationError

sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "plugins" / "knowledge-base"))
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "plugins" / "knowledge-base" / "runtime"))
from pc_knowledge_base import content
from pc_knowledge_base.parser_contract import (
CONTEXT_LOCATOR_MAX_BYTES,
Expand Down
2 changes: 1 addition & 1 deletion tests/knowledge_base/test_request_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import pytest

sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "plugins" / "knowledge-base"))
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "plugins" / "knowledge-base" / "runtime"))
from pc_knowledge_base.content import parse_document
from pc_knowledge_base.service import KnowledgeBase

Expand Down
2 changes: 1 addition & 1 deletion tests/knowledge_base/test_sql_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import pytest
from sqlalchemy import select, text

sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "plugins" / "knowledge-base"))
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "plugins" / "knowledge-base" / "runtime"))
from pc_knowledge_base.models import identity
from pc_knowledge_base.store import Store, chunks

Expand Down
Loading