Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

revert: take the plugin system back out of main

wizardchen6c83d7ab4792
diff --git a/.env.example b/.env.example
index 0df7082b0..13e9d634f 100644
--- a/.env.example
+++ b/.env.example
@@ -811,23 +811,3 @@ CONCURRENCY_POOL_SIZE=5
# BROWSERSKILL_INTERNAL_URL=http://10.0.0.12:8080
# 所有副本使用相同的随机密钥(至少 32 字符),从 Secret 注入,不提交到仓库。
# BROWSERSKILL_CLUSTER_SECRET=
-
-# ========== J4. 插件运行(可选) ==========
-# 代码插件(runtime.type: host)默认在每个 app 进程内运行。多机或想把 Python 插件隔开时,
-# 可以另起 `WeKnora plugin-host`(compose:--profile plugin-host;helm:pluginHost.enabled)。
-# plugin-host 与 app 共用数据库、Redis、存储和 SYSTEM_AES_KEY(或 JWT_SECRET),经 Redis 通告自己。
-# app 自己运行哪些 kind(binary,python);none 表示全部交给 plugin-host。默认本机能跑的都跑。
-# WEKNORA_PLUGIN_EMBEDDED_KINDS=none
-# 不在 app 本机运行的插件(remote、plugin-host 上的)回调 Host API 的地址。
-# WEKNORA_PLUGIN_HOST_API_URL=http://app:8080
-# 仅 plugin-host:运行哪些 kind、监听地址、app 访问它的地址(默认 http://<hostname>:<端口>)。
-# WEKNORA_PLUGIN_HOST_KINDS=python
-# WEKNORA_PLUGIN_HOST_ADDR=:8081
-# WEKNORA_PLUGIN_HOST_URL=http://plugin-host:8081
-# Python 插件使用的解释器(默认 PATH 中的 python3)。
-# WEKNORA_PLUGIN_PYTHON=python3
-# 插件进程的内存上限(Linux),用于没有声明 runtime.resources.memory 的插件。默认不限。
-# WEKNORA_PLUGIN_MEMORY_DEFAULT=1Gi
-# 委托给 WeKnora 的 cgroup v2 目录(Linux,需可写)。设置后每个插件进程进入各自的子 cgroup,
-# 按 runtime.resources 设置 memory.max 与 cpu.max;不设置时只限内存(rlimit),不限 CPU。
-# WEKNORA_PLUGIN_CGROUP=/sys/fs/cgroup/weknora-plugins
diff --git a/.github/workflows/go-lint.yml b/.github/workflows/go-lint.yml
index c95d3ec63..af10de964 100644
--- a/.github/workflows/go-lint.yml
+++ b/.github/workflows/go-lint.yml
@@ -58,10 +58,10 @@ jobs:
modules=()
if git diff --name-only --diff-filter=ACMR "$diff_range" -- \
.golangci.yml .github/workflows/go-lint.yml | grep -q .; then
- modules=("." "cli" "client" "pluginsdk")
+ modules=("." "cli" "client")
else
if git diff --name-only --diff-filter=ACMR "$diff_range" -- \
- '*.go' ':!cli/**' ':!client/**' ':!pluginsdk/**' go.mod go.sum | grep -q .; then
+ '*.go' ':!cli/**' ':!client/**' go.mod go.sum | grep -q .; then
modules+=(".")
fi
if git diff --name-only --diff-filter=ACMR "$diff_range" -- 'cli/**' | grep -q .; then
@@ -70,9 +70,6 @@ jobs:
if git diff --name-only --diff-filter=ACMR "$diff_range" -- 'client/**' | grep -q .; then
modules+=("client")
fi
- if git diff --name-only --diff-filter=ACMR "$diff_range" -- 'pluginsdk/**' | grep -q .; then
- modules+=("pluginsdk")
- fi
fi
json="["
first=1
diff --git a/.github/workflows/plugin-sdk.yml b/.github/workflows/plugin-sdk.yml
deleted file mode 100644
index f7e629d44..000000000
--- a/.github/workflows/plugin-sdk.yml
+++ /dev/null
@@ -1,85 +0,0 @@
-name: Plugin SDK
-
-# Builds and tests the pluginsdk/ Go module (extension protocol, Go SDK,
-# client and conformance suite) and the Python SDK in pluginsdk/python on
-# every push / PR that touches them. The Go conformance suite also runs the
-# Python SDK's test plugin.
-
-on:
- push:
- branches: [main]
- paths:
- - 'pluginsdk/**'
- - 'examples/plugins/notebooks/**'
- - 'examples/plugins/links/**'
- - 'examples/plugins/activity/**'
- - '.github/workflows/plugin-sdk.yml'
- pull_request:
- paths:
- - 'pluginsdk/**'
- - 'examples/plugins/notebooks/**'
- - 'examples/plugins/links/**'
- - 'examples/plugins/activity/**'
- - '.github/workflows/plugin-sdk.yml'
-
-defaults:
- run:
- working-directory: pluginsdk
- shell: bash
-
-permissions:
- contents: read
-
-jobs:
- test:
- name: test (${{ matrix.os }})
- runs-on: ${{ matrix.os }}
- strategy:
- fail-fast: false
- matrix:
- os: [ubuntu-latest, macos-latest, windows-latest]
- steps:
- - uses: actions/checkout@v6
- - uses: actions/setup-go@v6
- with:
- go-version: '1.26'
- cache-dependency-path: pluginsdk/go.mod
- - uses: actions/setup-python@v6
- with:
- python-version: '3.13'
- - name: go build
- run: go build ./...
- - name: go test
- run: go test -race ./...
- - name: go vet
- run: go vet ./...
-
- python:
- name: python ${{ matrix.python }} (${{ matrix.os }})
- runs-on: ${{ matrix.os }}
- strategy:
- fail-fast: false
- matrix:
- include:
- - { os: ubuntu-latest, python: '3.9' }
- - { os: ubuntu-latest, python: '3.13' }
- - { os: windows-latest, python: '3.13' }
- defaults:
- run:
- working-directory: pluginsdk/python
- # A job's run defaults replace the workflow's, shell included.
- shell: bash
- steps:
- - uses: actions/checkout@v6
- - uses: actions/setup-python@v6
- with:
- python-version: ${{ matrix.python }}
- - name: unittest
- run: python -W error::ResourceWarning -m unittest discover -s tests -v
- - name: example plugins
- run: |
- (cd ../../examples/plugins/notebooks && python -m unittest -v test_main)
- (cd ../../examples/plugins/links && python -m unittest -v test_main)
- (cd ../../examples/plugins/activity && python -m unittest -v test_main)
- - name: build the package
- run: python -m pip install build && python -m build
diff --git a/cmd/server/main.go b/cmd/server/main.go
index 3525fddfa..021a8bd25 100644
--- a/cmd/server/main.go
+++ b/cmd/server/main.go
@@ -41,9 +41,6 @@ import (
)
func main() {
- if code, ran := subcommand(); ran {
- os.Exit(code)
- }
// Set Gin mode
if os.Getenv("GIN_MODE") == "release" {
gin.SetMode(gin.ReleaseMode)
diff --git a/cmd/server/plugin_host.go b/cmd/server/plugin_host.go
deleted file mode 100644
index 3832015b8..000000000
--- a/cmd/server/plugin_host.go
+++ /dev/null
@@ -1,30 +0,0 @@
-package main
-
-import (
- "context"
- "os"
- "os/signal"
-
- "github.com/Tencent/WeKnora/internal/container"
- "github.com/Tencent/WeKnora/internal/logger"
-)
-
-// runPluginHost is `WeKnora plugin-host`: a standalone plugin host that
-// runs host plugins for the app nodes (see container.RunPluginHost).
-func runPluginHost() int {
- ctx, stop := signal.NotifyContext(context.Background(), shutdownSignals...)
- defer stop()
- if err := container.RunPluginHost(ctx); err != nil {
- logger.Errorf(context.Background(), "[plugin-host] %v", err)
- return 1
- }
- return 0
-}
-
-// subcommand runs a subcommand named on the command line, if any.
-func subcommand() (code int, ran bool) {
- if len(os.Args) > 1 && os.Args[1] == "plugin-host" {
- return runPluginHost(), true
- }
- return 0, false
-}
diff --git a/docker-compose.yml b/docker-compose.yml
index 439e02e51..0aca52475 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -381,11 +381,6 @@ services:
- OIDC_USER_INFO_MAPPING_EMAIL=${OIDC_USER_INFO_MAPPING_EMAIL:-}
# 飞书云文档解析模式 export:导出为docx,blocks:根据飞书云文档块解析为markdown。默认为:export
- FEISHU_DOCX_PARSE_MODE=${FEISHU_DOCX_PARSE_MODE:-export}
- # ========== 插件 ==========
- # app 自己运行哪些 kind 的宿主插件(binary,python;none 表示全部交给 plugin-host),默认本机能跑的都跑
- - WEKNORA_PLUGIN_EMBEDDED_KINDS=${WEKNORA_PLUGIN_EMBEDDED_KINDS:-}
- # 不在本机运行的插件(remote、plugin-host 上的)回调 Host API 的地址;启用 plugin-host 时设为 http://app:8080
- - WEKNORA_PLUGIN_HOST_API_URL=${WEKNORA_PLUGIN_HOST_API_URL:-}
depends_on:
redis:
condition: service_started
@@ -399,66 +394,6 @@ services:
extra_hosts:
- "host.docker.internal:host-gateway"
- # 独立插件宿主(可选,profile: plugin-host):在单独的容器里运行宿主插件,
- # 例如把 Python 插件与 app 隔开。与 app 共用镜像、数据库、Redis 和存储,
- # 通过 Redis 向 app 通告自己,app 用 SYSTEM_AES_KEY 派生的密钥签名调用它。
- # 启用:docker compose --profile plugin-host up -d,并在 .env 里设
- # WEKNORA_PLUGIN_EMBEDDED_KINDS=none(或 binary)
- # WEKNORA_PLUGIN_HOST_API_URL=http://app:8080
- plugin-host:
- image: wechatopenai/weknora-app:${WEKNORA_VERSION:-latest}
- container_name: WeKnora-plugin-host
- profiles:
- - plugin-host
- command: ["./WeKnora", "plugin-host"]
- volumes:
- # STORAGE_TYPE=local 时插件包存在这里
- - data-files:/data/files
- env_file:
- - .env
- environment:
- - GIN_MODE=${GIN_MODE:-release}
- - TZ=${TZ:-Asia/Shanghai}
- - LOG_LEVEL=${LOG_LEVEL:-info}
- - WEKNORA_PLUGIN_HOST_ADDR=:8081
- - WEKNORA_PLUGIN_HOST_URL=http://plugin-host:8081
- - WEKNORA_PLUGIN_HOST_KINDS=${WEKNORA_PLUGIN_HOST_KINDS:-}
- - WEKNORA_PLUGIN_HOST_API_URL=${WEKNORA_PLUGIN_HOST_API_URL:-http://app:8080}
- - DB_DRIVER=${DB_DRIVER:-postgres}
- - DB_HOST=${DB_HOST:-postgres}
- - DB_PORT=${DB_PORT:-5432}
- - DB_USER=${DB_USER:-}
- - DB_PASSWORD=${DB_PASSWORD:-}
- - DB_NAME=${DB_NAME:-}
- - STORAGE_TYPE=${STORAGE_TYPE:-local}
- - LOCAL_STORAGE_BASE_DIR=${LOCAL_STORAGE_BASE_DIR:-/data/files}
- - MINIO_ENDPOINT=${MINIO_ENDPOINT:-minio:9000}
- - MINIO_ACCESS_KEY_ID=${MINIO_ACCESS_KEY_ID:-minioadmin}
- - MINIO_SECRET_ACCESS_KEY=${MINIO_SECRET_ACCESS_KEY:-minioadmin}
- - REDIS_ADDR=${REDIS_ADDR:-redis:6379}
- - REDIS_USERNAME=${REDIS_USERNAME:-}
- - REDIS_PASSWORD=${REDIS_PASSWORD:-}
- - REDIS_DB=${REDIS_DB:-}
- - WEKNORA_REDIS_NAMESPACE=${WEKNORA_REDIS_NAMESPACE:-}
- - SYSTEM_AES_KEY=${SYSTEM_AES_KEY:-}
- - JWT_SECRET=${JWT_SECRET:-}
- - SSRF_WHITELIST=${SSRF_WHITELIST:-}
- healthcheck:
- test: ["CMD", "curl", "-f", "http://localhost:8081/healthz"]
- interval: 30s
- timeout: 10s
- retries: 3
- start_period: 30s
- depends_on:
- redis:
- condition: service_started
- postgres:
- condition: service_healthy
- networks:
- - WeKnora-network
- restart: unless-stopped
- stop_grace_period: 75s
-
# Sandbox 镜像:仅用于 build/pull,非常驻。Docker 沙箱默认关闭,需
# WEKNORA_SANDBOX_DOCKER_ENABLED=true 并挂载 docker.sock 后,app 才会按会话创建容器。
sandbox:
diff --git a/docker/Dockerfile.app b/docker/Dockerfile.app
index 0900cd476..21a272928 100644
--- a/docker/Dockerfile.app
+++ b/docker/Dockerfile.app
@@ -38,12 +38,10 @@ RUN if [ -n "$APK_MIRROR_ARG" ]; then \
# Install migrate tool
RUN go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
-# Copy go mod files. go.mod replace-points anydoc at ./third_party/anydoc-go
-# and the plugin SDK at ./pluginsdk, so their go.mod files must exist before
-# `go mod download`.
+# Copy go mod files. go.mod replace-points anydoc at ./third_party/anydoc-go,
+# so that module's go.mod must exist before `go mod download`.
COPY go.mod go.sum ./
COPY third_party/anydoc-go/go.mod third_party/anydoc-go/go.mod
-COPY pluginsdk/go.mod pluginsdk/go.mod
RUN --mount=type=cache,target=/go/pkg/mod go mod download
COPY cmd/download cmd/download
RUN go run cmd/download/duckdb/duckdb.go
diff --git a/examples/plugins/activity/main.py b/examples/plugins/activity/main.py
deleted file mode 100644
index a1c45574c..000000000
--- a/examples/plugins/activity/main.py
+++ /dev/null
@@ -1,87 +0,0 @@
-"""A WeKnora plugin that keeps an activity feed of the workspace, written
-with the Python SDK. It shows the three ways data reaches a plugin without a
-user asking: events WeKnora sends (at least once, so they are deduplicated
-by ID), a webhook other systems call, and a page that reads the feed."""
-
-from __future__ import annotations
-
-import hmac
-from datetime import datetime, timezone
-
-from weknora_plugin import ErrorCode, Plugin, PluginError, UIResponse, WebhookResponse
-
-plugin = Plugin("weknora-examples.activity", "1.0.0")
-
-FEED_KEY = "feed"
-MAX_ENTRIES = 100
-
-
-def store(call):
- host = call.host()
- if host is None:
- # Retryable: the Host API may just be unreachable for a moment.
- raise PluginError(ErrorCode.UNAVAILABLE, "the Host API is not available to this plugin")
- return host
-
-
-def describe(ev, include_answers: bool) -> dict | None:
- d = ev.data or {}
- title = d.get("title") or d.get("fileName") or d.get("knowledgeId", "")
- if ev.type == "knowledge.ingested":
- return {"kind": "ingested", "text": title}
- if ev.type == "knowledge.failed":
- return {"kind": "failed", "text": f"{title}: {d.get('error', '')}".strip(": ")}
- if ev.type == "knowledge.deleted":
- return {"kind": "deleted", "text": title}
- if ev.type == "chat.answered":
- text = (d.get("question") or "").strip()[:200]
- if include_answers:
- first = (d.get("answer") or "").strip().splitlines()
- text += f" → {first[0][:200]}" if first else ""
- return {"kind": "answered", "text": text}
- return None # a type added after this plugin was written
-
-
-def append(host, entry: dict) -> None:
- feed = host.kv_get(FEED_KEY, [])
- # Deliveries repeat after a failure; the event ID makes them harmless.
- if any(e.get("id") == entry["id"] for e in feed):
- return
- host.kv_put(FEED_KEY, ([entry] + feed)[:MAX_ENTRIES])
-
-
-def on_event(call, ev):
- entry = describe(ev, call.tenant.get("include_answers") is True)
- if entry is None:
- return
- entry.update(id=ev.id, at=(ev.occurred_at or datetime.now(timezone.utc)).isoformat())
- append(store(call), entry)
-
-
-def notes(call, req):
- secret = str(call.tenant.get("webhook_secret") or "")
- given = req.headers.get("X-Activity-Secret", "")
- if not secret or not hmac.compare_digest(secret, given):
- return WebhookResponse(status=401, body="bad or missing X-Activity-Secret")
- try:
- text = str((req.json() or {}).get("text") or "").strip()
- except ValueError:
- text = ""
- if not text:
- return WebhookResponse(status=400, body='send {"text": "..."}')
- now = datetime.now(timezone.utc)
- append(store(call), {"id": f"note-{now.timestamp()}", "kind": "note", "text": text[:500], "at": now.isoformat()})
- return {"ok": True}
-
-
-def ui(call, req):
- if req.method == "GET" and req.path == "/feed":
- return UIResponse(body=store(call).kv_get(FEED_KEY, []))
- return UIResponse(status=404, body={"error": "not found"})
-
-
-if __name__ == "__main__":
- plugin.serve()
diff --git a/examples/plugins/activity/package.sh b/examples/plugins/activity/package.sh
deleted file mode 100755
index 7de669958..000000000
--- a/examples/plugins/activity/package.sh
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/usr/bin/env bash
-# Packages the activity example plugin as a .wkp: plugin.yaml, schemas,
-# main.py, the page under ui/ with the bridge library, and the Python SDK
-# under vendor/.
-set -euo pipefail
-cd "$(dirname "$0")"
-version=$(sed -n 's/^version: //p' plugin.yaml)
-out=${1:-"weknora-examples-activity-${version}.wkp"}
-stage=$(mktemp -d)
-trap 'rm -rf "$stage"' EXIT
-cp plugin.yaml main.py "$stage"/
-cp -r ui schemas "$stage"/
-cp ../../../packages/plugin-ui/index.js "$stage/ui/weknora-plugin-ui.js"
-mkdir -p "$stage/vendor"
-cp -r ../../../pluginsdk/python/src/weknora_plugin "$stage/vendor/"
-find "$stage" -name __pycache__ -prune -exec rm -rf {} +
-rm -f "$out"
-(cd "$stage" && zip -qr - .) > "$out"
-echo "wrote $out"
diff --git a/examples/plugins/activity/plugin.yaml b/examples/plugins/activity/plugin.yaml
deleted file mode 100644
index 2674f1515..000000000
--- a/examples/plugins/activity/plugin.yaml
+++ /dev/null
@@ -1,30 +0,0 @@
-schemaVersion: 1
-id: weknora-examples.activity
-version: 1.0.0
-apiVersion: weknora.plugin/v1
-name: { en-US: Activity feed, zh-CN: 动态 }
-description:
- en-US: A feed of what happens in the workspace (documents ingested, failed, deleted, questions answered), plus notes other systems post through a webhook. Shows events and webhooks, written in Python.
- zh-CN: 空间动态:文档入库、失败、删除、问答完成,以及外部系统经 Webhook 推送的消息。演示事件与 Webhook,用 Python 编写。
-publisher: { id: weknora-examples, name: WeKnora examples }
-homepage: https://github.com/Tencent/WeKnora/tree/main/examples/plugins/activity
-license: MIT
-runtime: { type: host, kind: python, entry: main.py }
-config:
- tenant: schemas/tenant.yaml
-permissions:
- hostApi: [kv]
- # Answers include the question and the answer text.
- events: [knowledge.ingested, knowledge.failed, knowledge.deleted, chat.answered]
-contributes:
- pages:
- - id: feed
- name: { en-US: Activity, zh-CN: 动态 }
- description: { en-US: What happened in this workspace lately, zh-CN: 本空间最近的动态 }
- entry: ui/index.html
- webhooks:
- - id: notes
- name: { en-US: Post a note, zh-CN: 推送消息 }
- description:
- en-US: "POST {\"text\": \"...\"} with the header X-Activity-Secret set to the secret in this plugin's settings."
- zh-CN: "POST {\"text\": \"...\"},并带上请求头 X-Activity-Secret,值为本插件设置中的密钥。"
diff --git a/examples/plugins/activity/schemas/tenant.yaml b/examples/plugins/activity/schemas/tenant.yaml
deleted file mode 100644
index 015cbeb7a..000000000
--- a/examples/plugins/activity/schemas/tenant.yaml
+++ /dev/null
@@ -1,18 +0,0 @@
-type: object
-properties:
- webhook_secret:
- type: string
- title: Webhook secret
- description: Callers of the note webhook send it in the X-Activity-Secret header
- x-secret: true
- x-i18n:
- title: { zh-CN: Webhook 密钥 }
- description: { zh-CN: 调用推送消息 Webhook 时放在 X-Activity-Secret 请求头里 }
- include_answers:
- type: boolean
- title: Show answer text
- description: Keep the first line of each answer in the feed, not just the question
- default: false
- x-i18n:
- title: { zh-CN: 显示回答内容 }
- description: { zh-CN: 在动态里保留每个回答的第一行,而不只是问题 }
diff --git a/examples/plugins/activity/test_main.py b/examples/plugins/activity/test_main.py
deleted file mode 100644
index 4b8733ccb..000000000
--- a/examples/plugins/activity/test_main.py
+++ /dev/null
@@ -1,66 +0,0 @@
-import json
-import os
-import sys
-import unittest
-
-HERE = os.path.dirname(os.path.abspath(__file__))
-sys.path.insert(0, os.path.join(HERE, "..", "..", "..", "pluginsdk", "python", "src"))
-sys.path.insert(0, HERE)
-
-import main # noqa: E402
-from weknora_plugin import EventDelivery, UIRequest, WebhookRequest # noqa: E402
-from weknora_plugin.plugin import Call # noqa: E402
-
-
-class FakeHost:
- def __init__(self):
- self.data = {}
-
- def kv_get(self, key, default=None):
- return self.data.get(key, default)
-
- def kv_put(self, key, value, ttl=0):
- self.data[key] = value
-
-
-def call(**tenant):
- return Call({"context": {"tenantId": 1}, "config": {"tenant": tenant}})
-
-
-class ActivityTest(unittest.TestCase):
- def setUp(self):
- self.host = FakeHost()
- main.store = lambda c: self.host
-
- def feed(self):
- return self.host.data.get("feed", [])
-
- def test_events_are_recorded_once(self):
- ev = EventDelivery(id="e1", type="knowledge.ingested", data={"knowledgeId": "k1", "title": "Spec"})
- main.on_event(call(), ev)
- main.on_event(call(), ev) # a retry after a failure
- main.on_event(call(), EventDelivery(id="e2", type="some.future.type", data={}))
- self.assertEqual([(e["id"], e["kind"], e["text"]) for e in self.feed()], [("e1", "ingested", "Spec")])
-
- def test_answers(self):
- ev = EventDelivery(id="a", type="chat.answered", data={"question": "What is RAG?", "answer": "Retrieval.\nMore"})
- main.on_event(call(), ev)
- main.on_event(call(include_answers=True), EventDelivery(id="b", type="chat.answered", data=ev.data))
- self.assertEqual([e["text"] for e in self.feed()], ["What is RAG? → Retrieval.", "What is RAG?"])
-
- def test_note_webhook(self):
- req = WebhookRequest(method="POST", headers={"X-Activity-Secret": "s3"}, body=json.dumps({"text": "Deploy done"}).encode())
- self.assertEqual(main.notes(call(webhook_secret="s3"), req), {"ok": True})
- self.assertEqual(self.feed()[0]["text"], "Deploy done")
- self.assertEqual(main.notes(call(webhook_secret="other"), req).status, 401)
- self.assertEqual(main.notes(call(), req).status, 401)
- self.assertEqual(main.notes(call(webhook_secret="s3"), WebhookRequest(headers={"X-Activity-Secret": "s3"}, body=b"{}")).status, 400)
-
- def test_page_reads_the_feed(self):
- main.on_event(call(), EventDelivery(id="e1", type="knowledge.deleted", data={"title": "Old"}))
- self.assertEqual(main.ui(call(), UIRequest(method="GET", path="/feed")).body[0]["kind"], "deleted")
- self.assertEqual(main.ui(call(), UIRequest(method="GET", path="/other")).status, 404)
-
-
-if __name__ == "__main__":
- unittest.main()
diff --git a/examples/plugins/activity/ui/app.css b/examples/plugins/activity/ui/app.css
deleted file mode 100644
index 559e3cd9b..000000000
--- a/examples/plugins/activity/ui/app.css
+++ /dev/null
@@ -1,23 +0,0 @@
-:root {
- --fg: var(--wk-text-color-primary, #1f2329);
- --muted: var(--wk-text-color-secondary, #646a73);
- --line: var(--wk-component-border, #dee0e3);
- --card: var(--wk-bg-color-container, #fff);
- --brand: var(--wk-brand-color, #0052d9);
- --danger: var(--wk-error-color, #d54941);
- --ok: var(--wk-success-color, #2ba471);
- --radius: var(--wk-radius-default, 6px);
-}
-:root[data-theme='dark'] { color-scheme: dark; }
-body { margin: 0; font: 14px/1.5 var(--wk-font-family, system-ui, sans-serif); color: var(--fg); background: transparent; }
-main { padding: 4px 2px 12px; }
-.bar { display: flex; justify-content: flex-end; margin-bottom: 8px; }
-button { padding: 4px 12px; font: inherit; border-radius: var(--radius); cursor: pointer; border: 1px solid var(--line); background: var(--card); color: var(--fg); }
-.empty { color: var(--muted); padding: 24px 0; text-align: center; }
-ol { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
-li { display: flex; gap: 10px; align-items: baseline; padding: 8px 12px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); }
-.kind { flex: none; font-size: 12px; padding: 0 6px; border-radius: 4px; background: color-mix(in srgb, var(--brand) 12%, transparent); color: var(--brand); }
-.kind.failed, .kind.deleted { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); }
-.kind.ingested { color: var(--ok); background: color-mix(in srgb, var(--ok) 12%, transparent); }
-.text { flex: 1; min-width: 0; overflow-wrap: anywhere; }
-time { flex: none; color: var(--muted); font-size: 12px; }
diff --git a/examples/plugins/activity/ui/feed.js b/examples/plugins/activity/ui/feed.js
deleted file mode 100644
index b3e802f7b..000000000
--- a/examples/plugins/activity/ui/feed.js
+++ /dev/null
@@ -1,54 +0,0 @@
-import { connect } from './weknora-plugin-ui.js'
-
-const LABELS = {
- en: { ingested: 'Ingested', failed: 'Failed', deleted: 'Deleted', answered: 'Answered', note: 'Note', empty: 'Nothing yet.', refresh: 'Refresh' },
- zh: { ingested: '已入库', failed: '失败', deleted: '已删除', answered: '已回答', note: '消息', empty: '暂无动态。', refresh: '刷新' },
-}
-
-const wk = await connect()
-const app = document.getElementById('app')
-let text = pick(wk.context.locale)
-wk.on('locale', (locale) => {
- text = pick(locale)
- void load()
-})
-
-function pick(locale) {
- return String(locale || '').startsWith('zh') ? LABELS.zh : LABELS.en
-}
-
-function el(tag, props = {}, ...children) {
- const node = Object.assign(document.createElement(tag), props)
- node.append(...children)
- return node
-}
-
-async function load() {
- app.setAttribute('aria-busy', 'true')
- try {
- const { body } = await wk.get('/feed')
- render(Array.isArray(body) ? body : [])
- } catch (e) {
- app.replaceChildren(el('p', { className: 'empty', textContent: e.message }))
- } finally {
- app.removeAttribute('aria-busy')
- }
-}
-
-function render(feed) {
- const refresh = el('button', { type: 'button', textContent: text.refresh })
- refresh.addEventListener('click', () => void load())
- const list = el('ol')
- for (const entry of feed) {
- const at = new Date(entry.at)
- list.append(
- el('li', {},
- el('span', { className: `kind ${entry.kind}`, textContent: text[entry.kind] || entry.kind }),
- el('span', { className: 'text', textContent: entry.text }),
- el('time', { dateTime: entry.at, textContent: isNaN(at) ? '' : at.toLocaleString(wk.context.locale) })),
- )
- }
- app.replaceChildren(el('div', { className: 'bar' }, refresh), feed.length ? list : el('p', { className: 'empty', textContent: text.empty }))
-}
-
-await load()
diff --git a/examples/plugins/activity/ui/index.html b/examples/plugins/activity/ui/index.html
deleted file mode 100644
index 0cb54f79d..000000000
--- a/examples/plugins/activity/ui/index.html
+++ /dev/null
@@ -1,13 +0,0 @@
-<!doctype html>
-<html lang="en">
-<head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1" />
- <title>Activity</title>
- <link rel="stylesheet" href="app.css" />
-</head>
-<body>
- <main id="app" aria-busy="true"></main>
- <script type="module" src="feed.js"></script>
-</body>
-</html>
diff --git a/examples/plugins/jira/README.md b/examples/plugins/jira/README.md
deleted file mode 100644
index 5ef91e4d9..000000000
--- a/examples/plugins/jira/README.md
+++ /dev/null
@@ -1,64 +0,0 @@
-# Jira example plugin
-
-Syncs the issues of selected Jira Cloud projects, with their comments, into a
-knowledge base. It also ships a `jira-triage` skill that looks for duplicates,
-related work and owners among the synced issues.
-
-It is the fullest example plugin. It shows:
-
-- a connector with incremental sync, checkpoints and deletions;
-- agent tools the plugin serves itself (`search_issues`, `get_issue`), with
- result views: a table of issues, and the issue as Markdown;
-- an OAuth field (`x-oauth`): WeKnora runs Atlassian's consent flow and hands
- the plugin a fresh access token at every call;
-- dynamic choices (`x-options`): the sites the account can reach and the
- site's issue types;
-- form messages in the user's language (`call.Locale`);
-- a skill in the package.
-
-## Install
-
-1. Build the package with `./package.sh` and install it under **System
- administration → Plugin management**.
-2. To let workspaces sign in with their Atlassian accounts, register an app
- first. Without it, workspaces can still use an API token.
- 1. In the [Atlassian developer console](https://developer.atlassian.com/console/myapps/),
- create an OAuth 2.0 (3LO) app.
- 2. Under **Permissions → Jira API**, add `read:jira-work` and
- `read:jira-user`.
- 3. Under **Authorization**, set the callback URL to
- `<WeKnora address>/api/v1/plugin-oauth/callback`.
- 4. In the plugin's details in WeKnora, enter the app's client ID and
- secret.
-3. Workspace admins switch the plugin on under **Settings → Plugins**.
- - To sync, add a **Jira** data source to a knowledge base.
- - For the agent tools, fill in the plugin's configuration there (an
- Atlassian account or an API token). The tools then appear as the
- **Jira** MCP service, ready to add to agents.
-
-## What is synced
-
-Each issue becomes one Markdown document:
-
-- **Title:** `<KEY> <summary>`.
-- **Facts:** type, status, priority, resolution, assignee, reporter, labels,
- dates, parent, and a link.
-- **Description**, converted from Atlassian Document Format.
-- **Comments**, when enabled.
-
-Attachments are not synced.
-
-**Incremental syncs** fetch the issues updated since the newest one seen, in
-the user's time zone, as JQL requires.
-
-**Full syncs** also remove the documents of issues that were deleted, moved
-out of the selected projects, or no longer match the filters.
-
-## Test
-
-```bash
-go test ./examples/plugins/jira/
-```
-
-The tests run the plugin against a fake Jira Cloud site, both directly with
-an API token and through the OAuth gateway.
diff --git a/examples/plugins/jira/adf.go b/examples/plugins/jira/adf.go
deleted file mode 100644
index fceebbfdc..000000000
--- a/examples/plugins/jira/adf.go
+++ /dev/null
@@ -1,213 +0,0 @@
-package main
-
-import (
- "encoding/json"
- "fmt"
- "strings"
-)
-
-// adfNode is a node of the Atlassian Document Format, the JSON Jira Cloud
-// returns rich text in (descriptions, comments).
-type adfNode struct {
- Type string `json:"type"`
- Text string `json:"text"`
- Attrs map[string]any `json:"attrs"`
- Marks []adfMark `json:"marks"`
- Content []adfNode `json:"content"`
-}
-
-type adfMark struct {
- Type string `json:"type"`
- Attrs map[string]any `json:"attrs"`
-}
-
-// adfToMarkdown renders ADF as Markdown. Unknown nodes keep their text, so
-// nothing a new node type holds is lost from search. Plain strings (Jira's
-// v2 API, old comments) pass through.
-func adfToMarkdown(raw json.RawMessage) string {
- if len(raw) == 0 || string(raw) == "null" {
- return ""
- }
- var s string
- if json.Unmarshal(raw, &s) == nil {
- return strings.TrimSpace(s)
- }
- var doc adfNode
- if err := json.Unmarshal(raw, &doc); err != nil {
- return ""
- }
- var b strings.Builder
- blocks(&b, doc.Content, "")
- return strings.TrimSpace(b.String())
-}
-
-// blocks writes block nodes, each line prefixed with indent (list nesting,
-// quotes).
-func blocks(b *strings.Builder, nodes []adfNode, indent string) {
- for _, n := range nodes {
- switch n.Type {
- case "paragraph":
- line(b, indent, inline(n.Content))
- case "heading":
- level := 1
- if v, ok := n.Attrs["level"].(float64); ok && v >= 1 && v <= 6 {
- level = int(v)
- }
- line(b, indent, strings.Repeat("#", level)+" "+inline(n.Content))
- case "bulletList", "orderedList":
- for i, item := range n.Content {
- marker := "- "
- if n.Type == "orderedList" {
- marker = fmt.Sprintf("%d. ", i+1)
- }
- listItem(b, item, indent, marker)
- }
- b.WriteString("\n")
- case "codeBlock":
- lang, _ := n.Attrs["language"].(string)
- b.WriteString(indent + "```" + lang + "\n")
- for _, l := range strings.Split(plain(n.Content), "\n") {
- b.WriteString(indent + l + "\n")
- }
- b.WriteString(indent + "```\n\n")
- case "blockquote":
- blocks(b, n.Content, indent+"> ")
- case "rule":
- line(b, indent, "---")
- case "table":
- table(b, n, indent)
- case "panel", "expand", "nestedExpand", "layoutSection", "layoutColumn", "bodiedExtension":
- if title, _ := n.Attrs["title"].(string); title != "" {
- line(b, indent, "**"+title+"**")
- }
- blocks(b, n.Content, indent)
- case "mediaSingle", "mediaGroup", "media":
- // Attachments are not synced; say where one was.
- line(b, indent, "[attachment]")
- default:
- if len(n.Content) > 0 {
- blocks(b, n.Content, indent)
- } else if t := inline([]adfNode{n}); t != "" {
- line(b, indent, t)
- }
- }
- }
-}
-
-func line(b *strings.Builder, indent, text string) {
- b.WriteString(indent + text + "\n\n")
-}
-
-func listItem(b *strings.Builder, item adfNode, indent, marker string) {
- first := true
- for _, child := range item.Content {
- switch child.Type {
- case "paragraph":
- prefix := indent + strings.Repeat(" ", len(marker))
- if first {
- prefix = indent + marker
- }
- b.WriteString(prefix + inline(child.Content) + "\n")
- default:
- // Nested lists and other blocks go one level in.
- var sub strings.Builder
- blocks(&sub, []adfNode{child}, indent+" ")
- b.WriteString(strings.TrimRight(sub.String(), "\n") + "\n")
- }
- first = false
- }
-}
-
-func table(b *strings.Builder, n adfNode, indent string) {
- for i, row := range n.Content {
- cells := make([]string, 0, len(row.Content))
- for _, cell := range row.Content {
- var parts []string
- for _, p := range cell.Content {
- if t := strings.TrimSpace(inline(p.Content)); t != "" {
- parts = append(parts, t)
- }
- }
- cells = append(cells, strings.ReplaceAll(strings.Join(parts, " "), "|", "\\|"))
- }
- b.WriteString(indent + "| " + strings.Join(cells, " | ") + " |\n")
- if i == 0 {
- b.WriteString(indent + "|" + strings.Repeat(" --- |", len(cells)) + "\n")
- }
- }
- b.WriteString("\n")
-}
-
-// inline renders inline nodes with their marks.
-func inline(nodes []adfNode) string {
- var b strings.Builder
- for _, n := range nodes {
- switch n.Type {
- case "text":
- b.WriteString(marked(n.Text, n.Marks))
- case "hardBreak":
- b.WriteString(" \n")
- case "mention":
- text, _ := n.Attrs["text"].(string)
- if !strings.HasPrefix(text, "@") {
- text = "@" + text
- }
- b.WriteString(text)
- case "emoji":
- text, _ := n.Attrs["text"].(string)
- if text == "" {
- text, _ = n.Attrs["shortName"].(string)
- }
- b.WriteString(text)
- case "inlineCard", "blockCard":
- u, _ := n.Attrs["url"].(string)
- b.WriteString(u)
- case "status":
- text, _ := n.Attrs["text"].(string)
- b.WriteString("[" + text + "]")
- case "date":
- if ts, ok := n.Attrs["timestamp"].(string); ok {
- b.WriteString(ts)
- }
- default:
- b.WriteString(inline(n.Content))
- }
- }
- return b.String()
-}
-
-func marked(text string, marks []adfMark) string {
- if strings.TrimSpace(text) == "" {
- return text
- }
- for _, m := range marks {
- switch m.Type {
- case "strong":
- text = "**" + text + "**"
- case "em":
- text = "*" + text + "*"
- case "strike":
- text = "~~" + text + "~~"
- case "code":
- text = "`" + text + "`"
- case "link":
- if href, _ := m.Attrs["href"].(string); href != "" {
- text = "[" + text + "](" + href + ")"
- }
- }
- }
- return text
-}
-
-// plain is the text of nodes without formatting (code blocks).
-func plain(nodes []adfNode) string {
- var b strings.Builder
- for _, n := range nodes {
- if n.Type == "hardBreak" {
- b.WriteString("\n")
- }
- b.WriteString(n.Text)
- b.WriteString(plain(n.Content))
- }
- return b.String()
-}
diff --git a/examples/plugins/jira/client.go b/examples/plugins/jira/client.go
deleted file mode 100644
index cfbc7d972..000000000
--- a/examples/plugins/jira/client.go
+++ /dev/null
@@ -1,374 +0,0 @@
-package main
-
-import (
- "bytes"
- "context"
- "encoding/base64"
- "encoding/json"
- "fmt"
- "io"
- "net/http"
- "net/url"
- "strconv"
- "strings"
- "time"
-
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// gatewayURL is Atlassian's API gateway, where OAuth tokens are used. Tests
-// point it at a fake.
-var gatewayURL = "https://api.atlassian.com"
-
-// httpClient honors HTTPS_PROXY: a host plugin's traffic must go through the
-// plugin host's egress proxy.
-var httpClient = &http.Client{
- Timeout: 30 * time.Second,
- Transport: &http.Transport{Proxy: http.ProxyFromEnvironment},
-}
-
-// Credential fields, as the instance schema names them.
-type credentials struct {
- Auth string `json:"auth"`
- Account string `json:"account"` // an OAuth access token by the time it reaches us
- Site string `json:"site"` // cloud ID
- BaseURL string `json:"base_url"`
- Email string `json:"email"`
- APIToken string `json:"api_token"`
-}
-
-// client calls one Jira Cloud site's REST API.
-type client struct {
- api string // REST root: the site, or the gateway's /ex/jira/{cloudId}
- siteURL string // for links to issues
- auth string // Authorization header
-}
-
-// site is a Jira site an OAuth token can reach.
-type site struct {
- ID string `json:"id"`
- URL string `json:"url"`
- Name string `json:"name"`
-}
-
-// accessibleSites lists the sites an OAuth token was granted.
-func accessibleSites(ctx context.Context, token string) ([]site, error) {
- c := &client{api: gatewayURL, auth: "Bearer " + token}
- var sites []site
- err := c.do(ctx, http.MethodGet, "/oauth/token/accessible-resources", nil, &sites)
- return sites, err
-}
-
-// newClient checks the credentials and connects to the site they name.
-// Problems with the form come back as invalid_config on its fields.
-func newClient(ctx context.Context, cr credentials, locale string) (*client, error) {
- switch cr.Auth {
- case "", "oauth":
- if cr.Account == "" {
- return nil, pluginapi.InvalidConfig(say(locale, msgConnectAccount),
- map[string]string{"credentials.account": "required"})
- }
- if cr.Site == "" {
- return nil, pluginapi.InvalidConfig(say(locale, msgChooseSite),
- map[string]string{"credentials.site": "required"})
- }
- sites, err := accessibleSites(ctx, cr.Account)
- if err != nil {
- return nil, err
- }
- for _, s := range sites {
- if s.ID == cr.Site {
- return &client{
- api: gatewayURL + "/ex/jira/" + url.PathEscape(s.ID),
- siteURL: strings.TrimSuffix(s.URL, "/"),
- auth: "Bearer " + cr.Account,
- }, nil
- }
- }
- return nil, pluginapi.InvalidConfig(say(locale, msgSiteGone),
- map[string]string{"credentials.site": "not granted"})
- case "token":
- base := strings.TrimSuffix(strings.TrimSpace(cr.BaseURL), "/")
- fields := map[string]string{}
- if base == "" {
- fields["credentials.base_url"] = "required"
- }
- if cr.Email == "" {
- fields["credentials.email"] = "required"
- }
- if cr.APIToken == "" {
- fields["credentials.api_token"] = "required"
- }
- if len(fields) > 0 {
- return nil, pluginapi.InvalidConfig(say(locale, msgTokenFields), fields)
- }
- basic := cr.Email + ":" + cr.APIToken
- return &client{
- api: base, siteURL: base,
- auth: "Basic " + base64.StdEncoding.EncodeToString([]byte(basic)),
- }, nil
- default:
- return nil, pluginapi.InvalidConfig(say(locale, msgUnknownAuth),
- map[string]string{"credentials.auth": "enum"})
- }
-}
-
-// do sends one request and decodes a JSON answer. Jira's failures map to
-// protocol errors: WeKnora retries unavailable and rate_limited, and shows
-// unauthorized as broken credentials.
-func (c *client) do(ctx context.Context, method, path string, body, out any) error {
- var rd io.Reader
- if body != nil {
- b, err := json.Marshal(body)
- if err != nil {
- return err
- }
- rd = bytes.NewReader(b)
- }
- req, err := http.NewRequestWithContext(ctx, method, c.api+path, rd)
- if err != nil {
- return err
- }
- req.Header.Set("Authorization", c.auth)
- req.Header.Set("Accept", "application/json")
- if body != nil {
- req.Header.Set("Content-Type", "application/json")
- }
- resp, err := httpClient.Do(req)
- if err != nil {
- return pluginapi.Errorf(pluginapi.CodeUnavailable, "jira: %v", err)
- }
- defer func() { _ = resp.Body.Close() }()
- data, err := io.ReadAll(io.LimitReader(resp.Body, 32<<20))
- if err != nil {
- return pluginapi.Errorf(pluginapi.CodeUnavailable, "jira: %v", err)
- }
- if resp.StatusCode >= 300 {
- return statusError(resp, data)
- }
- if out == nil {
- return nil
- }
- if err := json.Unmarshal(data, out); err != nil {
- return fmt.Errorf("jira %s: decode: %w", path, err)
- }
- return nil
-}
-
-func statusError(resp *http.Response, body []byte) error {
- msg := jiraMessage(body)
- switch {
- case resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden:
- return pluginapi.Errorf(
- pluginapi.CodeUnauthorized,
- "jira refused the credentials (%d): %s",
- resp.StatusCode,
- msg,
- )
- case resp.StatusCode == http.StatusTooManyRequests:
- e := pluginapi.Errorf(pluginapi.CodeRateLimited, "jira rate limit: %s", msg)
- if s, err := strconv.Atoi(resp.Header.Get("Retry-After")); err == nil && s > 0 {
- e.Details = &pluginapi.ErrorDetails{RetryAfter: s}
- }
- return e
- case resp.StatusCode >= 500:
- return pluginapi.Errorf(pluginapi.CodeUnavailable, "jira %d: %s", resp.StatusCode, msg)
- case resp.StatusCode == http.StatusNotFound:
- return pluginapi.Errorf(pluginapi.CodeNotFound, "jira 404: %s", msg)
- case resp.StatusCode == http.StatusBadRequest:
- // Mostly a JQL filter Jira does not accept.
- return pluginapi.InvalidConfig("jira rejected the query: "+msg,
- map[string]string{"settings.jql": msg})
- default:
- return fmt.Errorf("jira %d: %s", resp.StatusCode, msg)
- }
-}
-
-// jiraMessage extracts Jira's error text ({"errorMessages": [...]}).
-func jiraMessage(body []byte) string {
- var e struct {
- ErrorMessages []string `json:"errorMessages"`
- Errors map[string]string `json:"errors"`
- Message string `json:"message"`
- }
- if json.Unmarshal(body, &e) == nil {
- parts := append([]string{}, e.ErrorMessages...)
- for k, v := range e.Errors {
- parts = append(parts, k+": "+v)
- }
- if e.Message != "" {
- parts = append(parts, e.Message)
- }
- if len(parts) > 0 {
- return strings.Join(parts, "; ")
- }
- }
- s := strings.TrimSpace(string(body))
- if len(s) > 200 {
- s = s[:200]
- }
- return s
-}
-
-// myself is the signed-in user. Its time zone is the one JQL dates are in.
-type myself struct {
- AccountID string `json:"accountId"`
- DisplayName string `json:"displayName"`
- TimeZone string `json:"timeZone"`
-}
-
-func (c *client) myself(ctx context.Context) (*myself, error) {
- var me myself
- if err := c.do(ctx, http.MethodGet, "/rest/api/3/myself", nil, &me); err != nil {
- return nil, err
- }
- return &me, nil
-}
-
-type project struct {
- ID string `json:"id"`
- Key string `json:"key"`
- Name string `json:"name"`
- Description string `json:"description"`
- Style string `json:"style"`
-}
-
-// projects lists every project the user can browse.
-func (c *client) projects(ctx context.Context) ([]project, error) {
- var out []project
- for start := 0; ; {
- var page struct {
- Values []project `json:"values"`
- IsLast bool `json:"isLast"`
- }
- q := url.Values{"startAt": {strconv.Itoa(start)}, "maxResults": {"50"}, "orderBy": {"name"}}
- if err := c.do(ctx, http.MethodGet, "/rest/api/3/project/search?"+q.Encode(), nil, &page); err != nil {
- return nil, err
- }
- out = append(out, page.Values...)
- if page.IsLast || len(page.Values) == 0 {
- return out, nil
- }
- start += len(page.Values)
- }
-}
-
-type issueType struct {
- Name string `json:"name"`
- Subtask bool `json:"subtask"`
-}
-
-func (c *client) issueTypes(ctx context.Context) ([]issueType, error) {
- var out []issueType
- err := c.do(ctx, http.MethodGet, "/rest/api/3/issuetype", nil, &out)
- return out, err
-}
-
-// issue is the part of a Jira issue the connector turns into a document.
-type issue struct {
- ID string `json:"id"`
- Key string `json:"key"`
- Fields struct {
- Summary string `json:"summary"`
- Description json.RawMessage `json:"description"`
- Status *named `json:"status"`
- IssueType *named `json:"issuetype"`
- Priority *named `json:"priority"`
- Resolution *named `json:"resolution"`
- Assignee *user `json:"assignee"`
- Reporter *user `json:"reporter"`
- Labels []string `json:"labels"`
- Created jiraTime `json:"created"`
- Updated jiraTime `json:"updated"`
- Project *project `json:"project"`
- Parent *struct {
- Key string `json:"key"`
- } `json:"parent"`
- Comment *struct {
- Comments []comment `json:"comments"`
- Total int `json:"total"`
- } `json:"comment"`
- } `json:"fields"`
-}
-
-type named struct {
- Name string `json:"name"`
-}
-
-type user struct {
- DisplayName string `json:"displayName"`
-}
-
-type comment struct {
- Author *user `json:"author"`
- Created jiraTime `json:"created"`
- Body json.RawMessage `json:"body"`
-}
-
-// jiraTime parses Jira's timestamps ("2026-09-26T10:04:05.123+0800").
-type jiraTime struct{ time.Time }
-
-func (t *jiraTime) UnmarshalJSON(b []byte) error {
- var s string
- if err := json.Unmarshal(b, &s); err != nil || s == "" {
- return nil
- }
- for _, layout := range []string{"2006-01-02T15:04:05.000-0700", time.RFC3339Nano} {
- if v, err := time.Parse(layout, s); err == nil {
- t.Time = v
- return nil
- }
- }
- return nil
-}
-
-var searchFields = []string{
- "summary", "description", "status", "issuetype", "priority", "resolution", "assignee", "reporter",
- "labels", "created", "updated", "project", "parent", "comment",
-}
-
-// searchPage runs JQL for at most limit issues, without comments, and says
-// whether more matched.
-func (c *client) searchPage(ctx context.Context, jql string, limit int) ([]issue, bool, error) {
- body := map[string]any{"jql": jql, "maxResults": limit, "fields": searchFields[:len(searchFields)-1]}
- var page struct {
- Issues []issue `json:"issues"`
- NextPageToken string `json:"nextPageToken"`
- IsLast bool `json:"isLast"`
- }
- if err := c.do(ctx, http.MethodPost, "/rest/api/3/search/jql", body, &page); err != nil {
- return nil, false, err
- }
- return page.Issues, !page.IsLast && page.NextPageToken != "", nil
-}
-
-// search runs JQL a page at a time (the enhanced search API, which pages
-// with a token). fn sees each page; returning an error stops the search.
-func (c *client) search(ctx context.Context, jql string, withComments bool, fn func([]issue) error) error {
- fields := searchFields
- if !withComments {
- fields = fields[:len(fields)-1]
- }
- token := ""
- for {
- body := map[string]any{"jql": jql, "maxResults": 50, "fields": fields}
- if token != "" {
- body["nextPageToken"] = token
- }
- var page struct {
- Issues []issue `json:"issues"`
- NextPageToken string `json:"nextPageToken"`
- IsLast bool `json:"isLast"`
- }
- if err := c.do(ctx, http.MethodPost, "/rest/api/3/search/jql", body, &page); err != nil {
- return err
- }
- if err := fn(page.Issues); err != nil {
- return err
- }
- if page.IsLast || page.NextPageToken == "" || len(page.Issues) == 0 {
- return nil
- }
- token = page.NextPageToken
- }
-}
diff --git a/examples/plugins/jira/icon.svg b/examples/plugins/jira/icon.svg
deleted file mode 100644
index 7d7b29415..000000000
--- a/examples/plugins/jira/icon.svg
+++ /dev/null
@@ -1 +0,0 @@
-<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><defs><linearGradient id="a" x1="15.5" y1="9.5" x2="10.4" y2="14.7" gradientUnits="userSpaceOnUse"><stop offset=".18" stop-color="#0052cc"/><stop offset="1" stop-color="#2684ff"/></linearGradient><linearGradient id="b" x1="16.6" y1="22.4" x2="21.7" y2="17.3" gradientUnits="userSpaceOnUse"><stop offset=".18" stop-color="#0052cc"/><stop offset="1" stop-color="#2684ff"/></linearGradient></defs><path fill="#2684ff" d="M28.2 15.1 17.3 4.2 16.2 3.2 8 11.4l-3.8 3.7a1 1 0 0 0 0 1.4l7.5 7.5 4.5 4.5 8.2-8.2.1-.1 3.7-3.7a1 1 0 0 0 0-1.4ZM16.2 19.5 12.4 15.8l3.8-3.8 3.7 3.8Z"/><path fill="url(#a)" d="M16.2 12a6.3 6.3 0 0 1 0-8.8L8 11.4l4.4 4.4Z"/><path fill="url(#b)" d="m19.9 15.8-3.7 3.7a6.3 6.3 0 0 1 0 8.9l8.2-8.2Z"/></svg>
diff --git a/examples/plugins/jira/jira_test.go b/examples/plugins/jira/jira_test.go
deleted file mode 100644
index cc8c7774a..000000000
--- a/examples/plugins/jira/jira_test.go
+++ /dev/null
@@ -1,589 +0,0 @@
-package main
-
-import (
- "bytes"
- "context"
- "encoding/json"
- "fmt"
- "go/parser"
- "go/token"
- "net/http"
- "net/http/httptest"
- "os"
- "path/filepath"
- "regexp"
- "sort"
- "strconv"
- "strings"
- "sync"
- "testing"
- "time"
-
- pluginclient "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/conformance"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-const (
- oauthToken = "access-token-1"
- cloudID = "cloud-1"
- email = "[email protected]"
- apiToken = "api-token"
-)
-
-var shanghai, _ = time.LoadLocation("Asia/Shanghai")
-
-// fakeJira is a Jira Cloud site reachable both directly (API token) and
-// through the OAuth gateway (/ex/jira/{cloudId}).
-type fakeJira struct {
- *httptest.Server
- mu sync.Mutex
- issues map[string]map[string]any // id → issue JSON
- jql []string
-}
-
-func newFakeJira(t *testing.T) *fakeJira {
- f := &fakeJira{issues: map[string]map[string]any{}}
- mux := http.NewServeMux()
- mux.HandleFunc("GET /oauth/token/accessible-resources", func(w http.ResponseWriter, r *http.Request) {
- if r.Header.Get("Authorization") != "Bearer "+oauthToken {
- http.Error(w, `{"message":"bad token"}`, http.StatusUnauthorized)
- return
- }
- writeJSON(w, []site{{ID: cloudID, URL: f.URL, Name: "Acme"}})
- })
- api := http.NewServeMux()
- api.HandleFunc("GET /rest/api/3/myself", func(w http.ResponseWriter, _ *http.Request) {
- writeJSON(w, myself{AccountID: "a1", DisplayName: "Me", TimeZone: "Asia/Shanghai"})
- })
- api.HandleFunc("GET /rest/api/3/project/search", func(w http.ResponseWriter, r *http.Request) {
- all := []project{{Key: "ENG", Name: "Engineering"}, {Key: "OPS", Name: "Operations"}}
- start, _ := strconv.Atoi(r.URL.Query().Get("startAt"))
- // One per page, to exercise paging.
- writeJSON(w, map[string]any{"values": all[start : start+1], "isLast": start+1 == len(all)})
- })
- api.HandleFunc("GET /rest/api/3/issuetype", func(w http.ResponseWriter, _ *http.Request) {
- writeJSON(w, []issueType{{Name: "Task"}, {Name: "Bug"}, {Name: "Bug"}, {Name: "Sub-task", Subtask: true}})
- })
- api.HandleFunc("POST /rest/api/3/search/jql", f.search)
- api.HandleFunc("GET /rest/api/3/issue/{key}", func(w http.ResponseWriter, r *http.Request) {
- f.mu.Lock()
- defer f.mu.Unlock()
- for _, is := range f.issues {
- if is["key"] == r.PathValue("key") {
- writeJSON(w, is)
- return
- }
- }
- http.Error(w, `{"errorMessages":["Issue does not exist or you do not have permission to see it."]}`,
- http.StatusNotFound)
- })
- mux.Handle("/ex/jira/"+cloudID+"/", http.StripPrefix("/ex/jira/"+cloudID, bearer(api)))
- mux.Handle("/rest/", basic(api))
- f.Server = httptest.NewServer(mux)
- t.Cleanup(f.Close)
- old := gatewayURL
- gatewayURL = f.URL
- t.Cleanup(func() { gatewayURL = old })
- return f
-}
-
-func bearer(h http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if r.Header.Get("Authorization") != "Bearer "+oauthToken {
- http.Error(w, `{"errorMessages":["token"]}`, http.StatusUnauthorized)
- return
- }
- h.ServeHTTP(w, r)
- })
-}
-
-func basic(h http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if u, p, ok := r.BasicAuth(); !ok || u != email || p != apiToken {
- http.Error(w, `{"errorMessages":["Client must be authenticated"]}`, http.StatusUnauthorized)
- return
- }
- h.ServeHTTP(w, r)
- })
-}
-
-func writeJSON(w http.ResponseWriter, v any) {
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(v)
-}
-
-var (
- projectClause = regexp.MustCompile(`project = "([A-Z]+)"`)
- updatedClause = regexp.MustCompile(`updated >= "([0-9/: ]+)"`)
-)
-
-// search understands the clauses the connector writes and pages two
-// issues at a time, oldest update first.
-func (f *fakeJira) search(w http.ResponseWriter, r *http.Request) {
- var in struct {
- JQL string `json:"jql"`
- NextPageToken string `json:"nextPageToken"`
- Fields []string `json:"fields"`
- }
- _ = json.NewDecoder(r.Body).Decode(&in)
- if strings.Contains(in.JQL, "BROKEN") {
- http.Error(w, `{"errorMessages":["Error in the JQL Query"]}`, http.StatusBadRequest)
- return
- }
- f.mu.Lock()
- defer f.mu.Unlock()
- f.jql = append(f.jql, in.JQL)
- key := ""
- if m := projectClause.FindStringSubmatch(in.JQL); m != nil {
- key = m[1]
- }
- var since time.Time
- if m := updatedClause.FindStringSubmatch(in.JQL); m != nil {
- since, _ = time.ParseInLocation("2006/01/02 15:04", m[1], shanghai)
- }
- var list []map[string]any
- for _, is := range f.issues {
- fields := is["fields"].(map[string]any)
- updated, _ := time.Parse("2006-01-02T15:04:05.000-0700", fields["updated"].(string))
- if strings.HasPrefix(is["key"].(string), key) && !updated.Before(since) {
- list = append(list, is)
- }
- }
- sort.Slice(list, func(i, j int) bool {
- updated := func(is map[string]any) string { return is["fields"].(map[string]any)["updated"].(string) }
- return updated(list[i]) < updated(list[j])
- })
- start, _ := strconv.Atoi(in.NextPageToken)
- end := min(start+2, len(list))
- page := map[string]any{"issues": list[start:end], "isLast": end == len(list)}
- if end < len(list) {
- page["nextPageToken"] = strconv.Itoa(end)
- }
- writeJSON(w, page)
-}
-
-func (f *fakeJira) put(id, key, summary, updated string) {
- f.mu.Lock()
- defer f.mu.Unlock()
- f.issues[id] = map[string]any{"id": id, "key": key, "fields": map[string]any{
- "summary": summary,
- "updated": updated,
- "created": "2026-09-01T09:00:00.000+0800",
- "status": map[string]any{"name": "In Progress"},
- "description": map[string]any{"type": "doc", "version": 1, "content": []any{
- map[string]any{"type": "paragraph", "content": []any{
- map[string]any{"type": "text", "text": "Steps to "},
- map[string]any{"type": "text", "text": "reproduce", "marks": []any{map[string]any{"type": "strong"}}},
- }},
- }},
- "comment": map[string]any{"total": 3, "comments": []any{map[string]any{
- "author": map[string]any{"displayName": "Ann"},
- "created": "2026-09-02T09:00:00.000+0800",
- "body": map[string]any{
- "type": "doc",
- "content": []any{
- map[string]any{
- "type": "paragraph",
- "content": []any{map[string]any{"type": "text", "text": "Seen on staging"}},
- },
- },
- },
- }}},
- }}
-}
-
-func (f *fakeJira) remove(id string) {
- f.mu.Lock()
- defer f.mu.Unlock()
- delete(f.issues, id)
-}
-
-func (f *fakeJira) lastJQL() string {
- f.mu.Lock()
- defer f.mu.Unlock()
- return f.jql[len(f.jql)-1]
-}
-
-// run is the plugin behind an HTTP server, called like WeKnora calls it.
-type run struct {
- t *testing.T
- client *pluginclient.Client
-}
-
-func newRun(t *testing.T) *run {
- srv := httptest.NewServer(newPlugin().Handler())
- t.Cleanup(srv.Close)
- return &run{t: t, client: pluginclient.New(srv.URL, nil, nil)}
-}
-
-func tokenInstance(f *fakeJira, settings map[string]any) map[string]any {
- return map[string]any{
- "credentials": map[string]any{"auth": "token", "base_url": f.URL + "/", "email": email, "api_token": apiToken},
- "settings": settings,
- }
-}
-
-func oauthInstance(site string) map[string]any {
- // WeKnora has already swapped the "oauth:<id>" reference for a token.
- return map[string]any{
- "credentials": map[string]any{"auth": "oauth", "account": oauthToken, "site": site},
- "settings": map[string]any{},
- }
-}
-
-type fetched struct {
- items []pluginapi.FetchedItem
- checkpoints int
- cursor *pluginapi.Cursor
-}
-
-func (r *run) fetch(instance map[string]any, mode string, cursor *pluginapi.Cursor, projects ...string) fetched {
- r.t.Helper()
- var out fetched
- end, err := r.client.Stream(context.Background(), pluginapi.ConnectorFetchPath("jira"),
- pluginapi.Envelope{Config: pluginapi.Config{Instance: instance}},
- pluginapi.FetchInput{Mode: mode, Cursor: cursor, ResourceIDs: projects},
- func(ev pluginapi.Event) error {
- switch ev.Type {
- case pluginapi.EventItem:
- var it pluginapi.FetchedItem
- if err := json.Unmarshal(ev.Data, &it); err != nil {
- return err
- }
- out.items = append(out.items, it)
- case pluginapi.EventCheckpoint:
- out.checkpoints++
- }
- return nil
- })
- if err != nil {
- r.t.Fatalf("fetch: %v", err)
- }
- out.cursor = &pluginapi.Cursor{}
- if err := json.Unmarshal(end, out.cursor); err != nil {
- r.t.Fatal(err)
- }
- return out
-}
-
-func (r *run) call(path string, instance map[string]any, input, out any) error {
- return r.client.Call(context.Background(), path,
- pluginapi.Envelope{Config: pluginapi.Config{Instance: instance}}, input, out)
-}
-
-func summary(items []pluginapi.FetchedItem) []string {
- var out []string
- for _, it := range items {
- if it.IsDeleted {
- out = append(out, "-"+it.ExternalID)
- } else {
- out = append(out, it.ExternalID+" "+it.Title)
- }
- }
- return out
-}
-
-func TestSyncIsIncrementalAndReportsDeletions(t *testing.T) {
- f := newFakeJira(t)
- f.put("101", "ENG-1", "Login fails", "2026-09-20T10:00:00.000+0800")
- f.put("102", "ENG-2", "Slow search", "2026-09-21T10:00:30.000+0800")
- f.put("103", "ENG-3", "Crash on save", "2026-09-22T10:00:00.000+0800")
- f.put("201", "OPS-1", "Not selected", "2026-09-22T10:00:00.000+0800")
- r := newRun(t)
- inst := tokenInstance(f, map[string]any{"issue_types": []string{"Bug", `Odd "type"`}, "jql": "labels = x"})
-
- full := r.fetch(inst, pluginapi.FetchFull, nil, "ENG")
- if got := summary(full.items); strings.Join(
- got,
- "|",
- ) != "101 ENG-1 Login fails|102 ENG-2 Slow search|103 ENG-3 Crash on save" {
- t.Fatalf("full sync = %v", got)
- }
- if full.checkpoints != 2 {
- t.Fatalf("a checkpoint per page, got %d", full.checkpoints)
- }
- wantJQL := `project = "ENG" AND issuetype in ("Bug", "Odd \"type\"") AND (labels = x) ORDER BY updated ASC`
- if jql := f.lastJQL(); jql != wantJQL {
- t.Fatalf("jql = %s", jql)
- }
- doc := string(full.items[0].Content)
- for _, want := range []string{
- "# ENG-1: Login fails", "- **Status:** In Progress", "- **Link:** " + f.URL + "/browse/ENG-1",
- "## Description\n\nSteps to **reproduce**", "### Ann · 2026-09-02 09:00 +0800\n\nSeen on staging",
- "_2 more comments are not included._",
- } {
- if !strings.Contains(doc, want) {
- t.Errorf("document lacks %q:\n%s", want, doc)
- }
- }
- if it := full.items[0]; it.SourceResourceID != "ENG" || it.Metadata["jira_key"] != "ENG-1" ||
- it.FileName != "ENG-1 Login fails.md" || it.UpdatedAt == nil {
- t.Fatalf("item = %+v", it)
- }
-
- // Incremental: from the newest update seen, in the user's time zone.
- f.put("102", "ENG-2", "Slow search (again)", "2026-09-25T08:00:00.000+0800")
- inc := r.fetch(inst, pluginapi.FetchIncremental, full.cursor, "ENG")
- if !strings.Contains(f.lastJQL(), `updated >= "2026/09/22 10:00"`) {
- t.Fatalf("incremental jql = %s", f.lastJQL())
- }
- if got := summary(inc.items); strings.Join(got, "|") != "103 ENG-3 Crash on save|102 ENG-2 Slow search (again)" {
- t.Fatalf("incremental = %v", got)
- }
-
- // A full sync reports what disappeared, once.
- f.remove("101")
- again := r.fetch(inst, pluginapi.FetchFull, inc.cursor, "ENG")
- if got := summary(again.items); got[len(got)-1] != "-101" || len(got) != 3 {
- t.Fatalf("full sync after a deletion = %v", got)
- }
- once := r.fetch(inst, pluginapi.FetchFull, again.cursor, "ENG")
- for _, it := range once.items {
- if it.IsDeleted {
- t.Fatalf("deletion reported twice: %v", summary(once.items))
- }
- }
-}
-
-func TestOAuthAccountsReachTheirSites(t *testing.T) {
- f := newFakeJira(t)
- f.put("101", "ENG-1", "Login fails", "2026-09-20T10:00:00.000+0800")
- r := newRun(t)
-
- var sites pluginapi.OptionsOutput
- err := r.call(pluginapi.OptionsPath("sites"), oauthInstance(""), pluginapi.OptionsInput{Field: "site"}, &sites)
- if err != nil {
- t.Fatal(err)
- }
- if len(sites.Options) != 1 || sites.Options[0].Value != cloudID || sites.Options[0].Label != "Acme" {
- t.Fatalf("sites = %+v", sites)
- }
- var types pluginapi.OptionsOutput
- err = r.call(pluginapi.OptionsPath("issue_types"), oauthInstance(cloudID), pluginapi.OptionsInput{}, &types)
- if err != nil {
- t.Fatal(err)
- }
- if fmt.Sprint(types.Options) != "[{Bug } {Sub-task } {Task }]" {
- t.Fatalf("issue types = %v", types.Options)
- }
- var res pluginapi.ListResourcesOutput
- listPath := pluginapi.ConnectorListResourcesPath("jira")
- err = r.call(listPath, oauthInstance(cloudID), pluginapi.ListResourcesInput{}, &res)
- if err != nil {
- t.Fatal(err)
- }
- if len(res.Resources) != 2 || res.Resources[1].ExternalID != "OPS" || res.Resources[0].URL != f.URL+"/browse/ENG" {
- t.Fatalf("projects = %+v", res.Resources)
- }
- got := r.fetch(oauthInstance(cloudID), pluginapi.FetchFull, nil, "ENG")
- if len(got.items) != 1 || got.items[0].URL != f.URL+"/browse/ENG-1" {
- t.Fatalf("oauth sync = %v", summary(got.items))
- }
-}
-
-func TestProblemsReachTheForm(t *testing.T) {
- f := newFakeJira(t)
- r := newRun(t)
- cases := []struct {
- name string
- instance map[string]any
- code pluginapi.ErrorCode
- }{
- {"no account", map[string]any{"credentials": map[string]any{"auth": "oauth"}}, pluginapi.CodeInvalidConfig},
- {"site not granted", oauthInstance("cloud-2"), pluginapi.CodeInvalidConfig},
- {
- "expired token",
- map[string]any{"credentials": map[string]any{"auth": "oauth", "account": "old", "site": cloudID}},
- pluginapi.CodeUnauthorized,
- },
- {
- "missing token fields",
- map[string]any{"credentials": map[string]any{"auth": "token"}},
- pluginapi.CodeInvalidConfig,
- },
- {"wrong api token", map[string]any{"credentials": map[string]any{
- "auth": "token", "base_url": f.URL, "email": email, "api_token": "nope",
- }}, pluginapi.CodeUnauthorized},
- }
- for _, tc := range cases {
- err := r.call(pluginapi.ConnectorValidatePath("jira"), tc.instance, struct{}{}, nil)
- if pe, ok := pluginapi.AsError(err); !ok || pe.Code != tc.code {
- t.Errorf("%s: %v", tc.name, err)
- }
- }
- // Messages about the form speak the user's language.
- err := r.client.Call(context.Background(), pluginapi.OptionsPath("sites"), pluginapi.Envelope{
- Context: pluginapi.Context{Locale: "zh-CN"},
- Config: pluginapi.Config{Instance: map[string]any{"credentials": map[string]any{"auth": "oauth"}}},
- }, pluginapi.OptionsInput{}, nil)
- if pe, ok := pluginapi.AsError(err); !ok || pe.Message != msgConnectAccount.zh {
- t.Fatalf("zh-CN hint = %v", err)
- }
- if err := r.call(pluginapi.ConnectorValidatePath("jira"), tokenInstance(f, nil), struct{}{}, nil); err != nil {
- t.Fatalf("valid token: %v", err)
- }
- _, err = r.client.Stream(
- context.Background(),
- pluginapi.ConnectorFetchPath("jira"),
- pluginapi.Envelope{Config: pluginapi.Config{Instance: tokenInstance(f, map[string]any{"jql": "BROKEN"})}},
- pluginapi.FetchInput{
- Mode: pluginapi.FetchFull,
- ResourceIDs: []string{"ENG"},
- },
- func(pluginapi.Event) error { return nil },
- )
- if pe, ok := pluginapi.AsError(err); !ok || pe.Code != pluginapi.CodeInvalidConfig ||
- !strings.Contains(pe.Message, "JQL") {
- t.Fatalf("bad jql = %v", err)
- }
-}
-
-func TestRateLimitsAreRetryable(t *testing.T) {
- srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
- w.Header().Set("Retry-After", "7")
- w.WriteHeader(http.StatusTooManyRequests)
- }))
- defer srv.Close()
- c := &client{api: srv.URL}
- _, err := c.myself(context.Background())
- pe, ok := pluginapi.AsError(err)
- if !ok || pe.Code != pluginapi.CodeRateLimited || !pe.Retryable || pe.Details.RetryAfter != 7 {
- t.Fatalf("429 = %v", err)
- }
-}
-
-func TestADFToMarkdown(t *testing.T) {
- doc, err := os.ReadFile("testdata/adf.json")
- if err != nil {
- t.Fatal(err)
- }
- want := "## Plan\n\n- one\n 1. nested\n- [docs](https://example.com) by @Ann\n\n```go\nx := 1\n```\n\n" +
- "| a\\|b |\n| --- |\n| 1 |\n\n[attachment]\n\nkept"
- if got := adfToMarkdown(doc); got != want {
- t.Fatalf("got:\n%s\nwant:\n%s", got, want)
- }
- if adfToMarkdown(json.RawMessage(`"plain"`)) != "plain" || adfToMarkdown(nil) != "" {
- t.Fatal("plain strings pass through")
- }
-}
-
-func TestJQLDatesUseTheUsersZone(t *testing.T) {
- since := time.Date(2026, 9, 22, 2, 0, 59, 0, time.UTC)
- got := buildJQL("ENG", settings{}, since, shanghai)
- if got != `project = "ENG" AND updated >= "2026/09/22 10:00" ORDER BY updated ASC` {
- t.Fatal(got)
- }
-}
-
-// The example must stay an external plugin: the SDK and ordinary libraries,
-// nothing from WeKnora's internals.
-func TestPluginImportsNoInternals(t *testing.T) {
- files, _ := filepath.Glob("*.go")
- fset := token.NewFileSet()
- for _, f := range files {
- if strings.HasSuffix(f, "_test.go") {
- continue
- }
- src, err := os.ReadFile(f)
- if err != nil {
- t.Fatal(err)
- }
- parsed, err := parser.ParseFile(fset, f, src, parser.ImportsOnly)
- if err != nil {
- t.Fatal(err)
- }
- for _, imp := range parsed.Imports {
- if strings.Contains(imp.Path.Value, "github.com/Tencent/WeKnora/internal") {
- t.Errorf("%s imports %s", f, imp.Path.Value)
- }
- }
- }
-}
-
-func TestPluginPassesConformance(t *testing.T) {
- srv := httptest.NewServer(newPlugin().Handler())
- defer srv.Close()
- rep := conformance.Run(context.Background(), conformance.Target{
- Client: pluginclient.New(srv.URL, nil, nil),
- Raw: func(ctx context.Context, path string, body []byte) (*http.Response, error) {
- req, _ := http.NewRequestWithContext(ctx, http.MethodPost, srv.URL+path, bytes.NewReader(body))
- return http.DefaultClient.Do(req)
- },
- })
- for _, r := range rep.Results {
- if !r.Passed {
- t.Errorf("%s: %s", r.Name, r.Detail)
- }
- }
-}
-
-func TestAgentTools(t *testing.T) {
- f := newFakeJira(t)
- f.put("101", "ENG-1", "Login fails", "2026-09-20T10:00:00.000+0800")
- f.put("102", "ENG-2", "Login is slow", "2026-09-21T10:00:00.000+0800")
- f.put("103", "ENG-3", "Other", "2026-09-22T10:00:00.000+0800")
- r := newRun(t)
- tenant := map[string]any{"auth": "token", "base_url": f.URL, "email": email, "api_token": apiToken}
- call := func(tool string, args string, tenant map[string]any, locale string) pluginapi.ToolResult {
- t.Helper()
- req := pluginapi.MCPRequest{
- JSONRPC: "2.0", ID: json.RawMessage(`1`), Method: "tools/call",
- Params: json.RawMessage(`{"name":"` + tool + `","arguments":` + args + `}`),
- }
- var out pluginapi.MCPResponse
- env := pluginapi.Envelope{Context: pluginapi.Context{Locale: locale}, Config: pluginapi.Config{Tenant: tenant}}
- if err := r.client.Call(context.Background(), pluginapi.MCPPath(toolsServer), env, req, &out); err != nil {
- t.Fatal(err)
- }
- var res pluginapi.ToolResult
- if out.Error != nil || json.Unmarshal(out.Result, &res) != nil {
- t.Fatalf("%s: %+v", tool, out)
- }
- return res
- }
-
- res := call("search_issues", `{"text":"login","project":"ENG","limit":1}`, tenant, "")
- if f.lastJQL() != `project = "ENG" AND text ~ "login" ORDER BY updated DESC` || res.IsError {
- t.Fatalf("jql = %s, %+v", f.lastJQL(), res)
- }
- rows, _ := json.Marshal(res.StructuredContent)
- if !strings.Contains(string(rows), `"key":"ENG-1"`) || !strings.Contains(string(rows), `"more":true`) ||
- !strings.Contains(res.Content[0].Text, "- ENG-1 [In Progress] Login fails (unassigned") {
- t.Fatalf("search = %s\n%s", rows, res.Content[0].Text)
- }
- call("search_issues", `{"jql":"assignee = currentUser()"}`, tenant, "")
- if f.lastJQL() != "assignee = currentUser() ORDER BY updated DESC" {
- t.Fatalf("jql = %s", f.lastJQL())
- }
- if res := call("search_issues", `{}`, tenant, ""); !res.IsError {
- t.Fatal("a search needs jql or text")
- }
-
- res = call("get_issue", `{"key":"eng-2"}`, tenant, "")
- md, _ := json.Marshal(res.StructuredContent)
- if res.IsError || !strings.HasPrefix(res.Content[0].Text, "# ENG-2: Login is slow") ||
- !strings.Contains(string(md), `"url":"`+f.URL+`/browse/ENG-2"`) {
- t.Fatalf("get_issue = %+v", res)
- }
- if res := call("get_issue", `{"key":"ENG-9"}`, tenant, ""); !res.IsError ||
- !strings.Contains(res.Content[0].Text, "does not exist") {
- t.Fatalf("missing issue = %+v", res)
- }
- if res := call("get_issue", `{"key":"DROP TABLE"}`, tenant, ""); !res.IsError {
- t.Fatal("a malformed key reaches Jira")
- }
- res = call("search_issues", `{"text":"x"}`, map[string]any{"auth": "oauth"}, "zh-CN")
- if !res.IsError || !strings.Contains(res.Content[0].Text, "本空间尚未连接 Jira") {
- t.Fatalf("unconfigured = %+v", res)
- }
-
- // The workspace form offers sites and issue types too.
- var sites pluginapi.OptionsOutput
- err := r.client.Call(context.Background(), pluginapi.OptionsPath("sites"),
- pluginapi.Envelope{Config: pluginapi.Config{Tenant: map[string]any{"auth": "oauth", "account": oauthToken}}},
- pluginapi.OptionsInput{Field: "site", Scope: pluginapi.OptionsScopeTenant}, &sites)
- if err != nil || len(sites.Options) != 1 || sites.Options[0].Value != cloudID {
- t.Fatalf("tenant sites = %+v, %v", sites, err)
- }
-}
diff --git a/examples/plugins/jira/main.go b/examples/plugins/jira/main.go
deleted file mode 100644
index 652b00521..000000000
--- a/examples/plugins/jira/main.go
+++ /dev/null
@@ -1,471 +0,0 @@
-// Command jira syncs Jira Cloud issues into WeKnora knowledge bases. It is the
-// fullest example plugin:
-//
-// - a data source connector with incremental sync, checkpoints and
-// deletions;
-// - an OAuth field (x-oauth): WeKnora runs Atlassian's consent flow with
-// the platform's OAuth app and hands the plugin a fresh access token;
-// - dynamic choices (x-options): the sites an account can reach and the
-// site's issue types;
-// - a skill shipped in the package.
-//
-// Build a package with ./package.sh; install it under System administration
-// → Plugin management.
-package main
-
-import (
- "context"
- "encoding/json"
- "fmt"
- "log"
- "slices"
- "sort"
- "strings"
- "time"
- "unicode/utf8"
-
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// Version must match plugin.yaml.
-const Version = "1.0.0"
-
-func main() {
- if err := newPlugin().Serve(); err != nil {
- log.Fatal(err)
- }
-}
-
-func newPlugin() *pluginsdk.Plugin {
- p := pluginsdk.New(pluginsdk.Info{ID: "weknora-examples.jira", Version: Version})
- p.Connector("jira", connector{})
- p.Options("sites", siteOptions)
- p.Options("issue_types", issueTypeOptions)
- registerTools(p)
- return p
-}
-
-// settings are the connector's x-group: settings fields.
-type settings struct {
- IssueTypes []string `json:"issue_types"`
- JQL string `json:"jql"`
- IncludeComments *bool `json:"include_comments"`
-}
-
-func (s settings) comments() bool { return s.IncludeComments == nil || *s.IncludeComments }
-
-func decode(cfg pluginsdk.ConnectorConfig) (credentials, settings, error) {
- var cr credentials
- var st settings
- if err := remarshal(cfg.Credentials, &cr); err != nil {
- return cr, st, pluginapi.InvalidConfig("credentials: "+err.Error(), nil)
- }
- if err := remarshal(cfg.Settings, &st); err != nil {
- return cr, st, pluginapi.InvalidConfig("settings: "+err.Error(), nil)
- }
- return cr, st, nil
-}
-
-func remarshal(in, out any) error {
- b, err := json.Marshal(in)
- if err != nil {
- return err
- }
- return json.Unmarshal(b, out)
-}
-
-type connector struct{}
-
-// Validate signs in to the site.
-func (connector) Validate(ctx context.Context, call *pluginsdk.Call, cfg pluginsdk.ConnectorConfig) error {
- cr, _, err := decode(cfg)
- if err != nil {
- return err
- }
- c, err := newClient(ctx, cr, call.Locale)
- if err != nil {
- return err
- }
- _, err = c.myself(ctx)
- return err
-}
-
-// ListResources offers the projects the user can browse.
-func (connector) ListResources(
- ctx context.Context, call *pluginsdk.Call, cfg pluginsdk.ConnectorConfig, parentID string,
-) ([]pluginapi.Resource, error) {
- if parentID != "" {
- return nil, nil
- }
- cr, _, err := decode(cfg)
- if err != nil {
- return nil, err
- }
- c, err := newClient(ctx, cr, call.Locale)
- if err != nil {
- return nil, err
- }
- projects, err := c.projects(ctx)
- if err != nil {
- return nil, err
- }
- out := make([]pluginapi.Resource, 0, len(projects))
- for _, p := range projects {
- out = append(out, pluginapi.Resource{
- ExternalID: p.Key, Name: p.Name, Type: "project", Description: p.Key,
- URL: c.siteURL + "/browse/" + p.Key,
- })
- }
- return out, nil
-}
-
-// syncState is the cursor: per project, the newest update seen (where the
-// next incremental sync starts) and the issues it holds (so a full sync can
-// tell which were deleted).
-type syncState struct {
- Projects map[string]*projectState `json:"projects"`
-}
-
-type projectState struct {
- Since time.Time `json:"since"`
- IDs []string `json:"ids"`
-}
-
-func stateFrom(c *pluginapi.Cursor) syncState {
- st := syncState{Projects: map[string]*projectState{}}
- if c != nil {
- _ = remarshal(c.State, &st)
- if st.Projects == nil {
- st.Projects = map[string]*projectState{}
- }
- }
- return st
-}
-
-func (s syncState) cursor(now time.Time) pluginapi.Cursor {
- var state map[string]any
- _ = remarshal(s, &state)
- return pluginapi.Cursor{LastSyncTime: &now, State: state}
-}
-
-// Fetch syncs the selected projects. Issues come oldest update first, so the
-// cursor can advance with every page; a full sync also reports the issues
-// that disappeared (deleted, moved out, or no longer matching the filter).
-func (connector) Fetch(
- ctx context.Context, call *pluginsdk.Call, cfg pluginsdk.ConnectorConfig, in pluginapi.FetchInput,
- s *pluginsdk.Stream,
-) (*pluginapi.Cursor, error) {
- cr, st, err := decode(cfg)
- if err != nil {
- return nil, err
- }
- keys := in.ResourceIDs
- if len(keys) == 0 {
- keys = cfg.ResourceIDs
- }
- if len(keys) == 0 {
- return nil, pluginapi.InvalidConfig(say(call.Locale, msgNoProjects), nil)
- }
- c, err := newClient(ctx, cr, call.Locale)
- if err != nil {
- return nil, err
- }
- me, err := c.myself(ctx)
- if err != nil {
- return nil, err
- }
- // JQL reads dates in the user's time zone.
- loc, err := time.LoadLocation(me.TimeZone)
- if err != nil || me.TimeZone == "" {
- loc = time.UTC
- }
-
- prev := stateFrom(in.Cursor)
- incremental := in.Mode == pluginapi.FetchIncremental && in.Cursor != nil
- // Checkpoints are complete snapshots: projects not reached yet keep
- // their previous state.
- next := syncState{Projects: map[string]*projectState{}}
- for _, key := range keys {
- if p := prev.Projects[key]; p != nil {
- cp := *p
- next.Projects[key] = &cp
- } else {
- next.Projects[key] = &projectState{}
- }
- }
- seen := map[string]bool{}
- now := time.Now().UTC()
-
- for _, key := range keys {
- ps := next.Projects[key]
- var since time.Time
- if incremental {
- since = ps.Since
- }
- known := map[string]bool{}
- for _, id := range ps.IDs {
- known[id] = true
- }
- _ = s.Progress("syncing " + key)
- err := c.search(ctx, buildJQL(key, st, since, loc), st.comments(), func(page []issue) error {
- for i := range page {
- is := &page[i]
- if err := s.Item(c.item(is, key)); err != nil {
- return err
- }
- seen[is.ID], known[is.ID] = true, true
- if is.Fields.Updated.After(ps.Since) {
- ps.Since = is.Fields.Updated.Time
- }
- }
- ps.IDs = sortedKeys(known)
- return s.Checkpoint(next.cursor(now))
- })
- if err != nil {
- return nil, err
- }
- }
-
- if !incremental {
- // Everything was listed: what the last sync had and this one did
- // not see is gone.
- for _, key := range keys {
- var kept []string
- if p := prev.Projects[key]; p != nil {
- for _, id := range p.IDs {
- if seen[id] {
- continue
- }
- gone := pluginapi.FetchedItem{ExternalID: id, IsDeleted: true, SourceResourceID: key}
- if err := s.Item(gone); err != nil {
- return nil, err
- }
- }
- }
- for _, id := range next.Projects[key].IDs {
- if seen[id] {
- kept = append(kept, id)
- }
- }
- next.Projects[key].IDs = kept
- }
- }
- cur := next.cursor(now)
- return &cur, nil
-}
-
-func sortedKeys(m map[string]bool) []string {
- out := make([]string, 0, len(m))
- for k := range m {
- out = append(out, k)
- }
- sort.Strings(out)
- return out
-}
-
-// buildJQL is one project's query: the settings' filters, the issues updated
-// since the last sync (to the minute, JQL's precision), oldest first.
-func buildJQL(key string, st settings, since time.Time, loc *time.Location) string {
- parts := []string{"project = " + quote(key)}
- if len(st.IssueTypes) > 0 {
- types := make([]string, len(st.IssueTypes))
- for i, t := range st.IssueTypes {
- types[i] = quote(t)
- }
- parts = append(parts, "issuetype in ("+strings.Join(types, ", ")+")")
- }
- if extra := strings.TrimSpace(st.JQL); extra != "" {
- parts = append(parts, "("+extra+")")
- }
- if !since.IsZero() {
- parts = append(parts, "updated >= "+quote(since.In(loc).Format("2006/01/02 15:04")))
- }
- return strings.Join(parts, " AND ") + " ORDER BY updated ASC"
-}
-
-func quote(s string) string {
- return `"` + strings.NewReplacer(`\`, `\\`, `"`, `\"`).Replace(s) + `"`
-}
-
-// item turns an issue into a Markdown document.
-func (c *client) item(is *issue, projectKey string) pluginapi.FetchedItem {
- f := &is.Fields
- url := c.issueURL(is.Key)
- created, updated := f.Created.Time, f.Updated.Time
- return pluginapi.FetchedItem{
- ExternalID: is.ID,
- Title: is.Key + " " + f.Summary,
- Content: []byte(c.markdown(is)),
- ContentType: "text/markdown",
- FileName: fileName(is.Key + " " + f.Summary),
- URL: url,
- CreatedAt: nonZero(created),
- UpdatedAt: nonZero(updated),
- SourceResourceID: projectKey,
- Metadata: map[string]string{
- "channel": "jira", "jira_key": is.Key, "project": projectKey, "status": nameOf(f.Status),
- "issue_type": nameOf(f.IssueType), "assignee": userOf(f.Assignee), "priority": nameOf(f.Priority),
- "labels": strings.Join(f.Labels, ","),
- },
- }
-}
-
-func (c *client) issueURL(key string) string { return c.siteURL + "/browse/" + key }
-
-// markdown renders an issue: its facts, description and comments.
-func (c *client) markdown(is *issue) string {
- f := &is.Fields
- var b strings.Builder
- fmt.Fprintf(&b, "# %s: %s\n\n", is.Key, f.Summary)
- facts := [][2]string{
- {"Type", nameOf(f.IssueType)},
- {"Status", nameOf(f.Status)},
- {"Priority", nameOf(f.Priority)},
- {"Resolution", nameOf(f.Resolution)},
- {"Assignee", userOf(f.Assignee)},
- {"Reporter", userOf(f.Reporter)},
- {"Labels", strings.Join(f.Labels, ", ")},
- {"Created", stamp(f.Created)},
- {"Updated", stamp(f.Updated)},
- }
- if f.Parent != nil {
- facts = append(facts, [2]string{"Parent", f.Parent.Key})
- }
- for _, kv := range facts {
- if kv[1] != "" {
- fmt.Fprintf(&b, "- **%s:** %s\n", kv[0], kv[1])
- }
- }
- fmt.Fprintf(&b, "- **Link:** %s\n", c.issueURL(is.Key))
- if desc := adfToMarkdown(f.Description); desc != "" {
- b.WriteString("\n## Description\n\n" + desc + "\n")
- }
- if f.Comment != nil && len(f.Comment.Comments) > 0 {
- b.WriteString("\n## Comments\n")
- for _, cm := range f.Comment.Comments {
- fmt.Fprintf(&b, "\n### %s · %s\n\n%s\n", userOf(cm.Author), stamp(cm.Created), adfToMarkdown(cm.Body))
- }
- if more := f.Comment.Total - len(f.Comment.Comments); more > 0 {
- fmt.Fprintf(&b, "\n_%d more comments are not included._\n", more)
- }
- }
- return b.String()
-}
-
-func nameOf(n *named) string {
- if n == nil {
- return ""
- }
- return n.Name
-}
-
-func userOf(u *user) string {
- if u == nil {
- return ""
- }
- return u.DisplayName
-}
-
-func stamp(t jiraTime) string {
- if t.IsZero() {
- return ""
- }
- return t.Format("2006-01-02 15:04 -0700")
-}
-
-func nonZero(t time.Time) *time.Time {
- if t.IsZero() {
- return nil
- }
- return &t
-}
-
-const maxFileNameBytes = 200
-
-var fileNameReplacer = strings.NewReplacer(
- "/", "_", "\\", "_", ":", "_", "*", "_", "?", "_", "\"", "_", "<", "_", ">", "_", "|", "_",
- "\n", " ", "\r", " ", "\t", " ",
-)
-
-// fileName makes a title safe as a file name: hostile punctuation replaced,
-// at most 200 bytes without splitting a rune.
-func fileName(title string) string {
- name := strings.TrimSpace(fileNameReplacer.Replace(title))
- if name == "" {
- return "issue.md"
- }
- for len(name) > maxFileNameBytes {
- _, size := utf8.DecodeLastRuneInString(name)
- name = name[:len(name)-size]
- }
- return name + ".md"
-}
-
-// formCredentials are the connection fields of the form being filled: a
-// data source's credentials, or the workspace's connection for the tools.
-func formCredentials(call *pluginsdk.Call, in pluginapi.OptionsInput) (credentials, error) {
- var cr credentials
- if in.Scope == pluginapi.OptionsScopeTenant {
- return cr, remarshal(call.Config.Tenant, &cr)
- }
- var cfg pluginsdk.ConnectorConfig
- if err := call.DecodeInstance(&cfg); err != nil {
- return cr, err
- }
- cr, _, err := decode(cfg)
- return cr, err
-}
-
-// siteOptions lists the Jira sites the connected account granted.
-func siteOptions(ctx context.Context, call *pluginsdk.Call, in pluginapi.OptionsInput) ([]pluginapi.Option, error) {
- cr, err := formCredentials(call, in)
- if err != nil {
- return nil, err
- }
- if cr.Account == "" {
- return nil, pluginapi.InvalidConfig(say(call.Locale, msgConnectAccount),
- map[string]string{"credentials.account": "required"})
- }
- sites, err := accessibleSites(ctx, cr.Account)
- if err != nil {
- return nil, err
- }
- out := make([]pluginapi.Option, 0, len(sites))
- for _, s := range sites {
- out = append(out, pluginapi.Option{Value: s.ID, Label: s.Name, Description: s.URL})
- }
- return out, nil
-}
-
-// issueTypeOptions lists the site's issue types by name (JQL matches names).
-func issueTypeOptions(
- ctx context.Context,
- call *pluginsdk.Call,
- in pluginapi.OptionsInput,
-) ([]pluginapi.Option, error) {
- cr, err := formCredentials(call, in)
- if err != nil {
- return nil, err
- }
- c, err := newClient(ctx, cr, call.Locale)
- if err != nil {
- return nil, err
- }
- types, err := c.issueTypes(ctx)
- if err != nil {
- return nil, err
- }
- var names []string
- for _, t := range types {
- if !slices.Contains(names, t.Name) {
- names = append(names, t.Name)
- }
- }
- sort.Strings(names)
- out := make([]pluginapi.Option, len(names))
- for i, n := range names {
- out[i] = pluginapi.Option{Value: n}
- }
- return out, nil
-}
diff --git a/examples/plugins/jira/messages.go b/examples/plugins/jira/messages.go
deleted file mode 100644
index 4b2489dbf..000000000
--- a/examples/plugins/jira/messages.go
+++ /dev/null
@@ -1,27 +0,0 @@
-package main
-
-import "strings"
-
-// Messages the form shows, in the user's language (call.Locale). Errors
-// that quote Jira stay in Jira's words.
-type message struct{ en, zh string }
-
-var (
- msgConnectAccount = message{"Connect an Atlassian account first.", "请先连接 Atlassian 账号。"}
- msgChooseSite = message{"Choose a site.", "请选择站点。"}
- msgSiteGone = message{"The account cannot reach this site any more.", "该账号已无法访问此站点。"}
- msgTokenFields = message{"Enter the site URL, email and API token first.", "请先填写站点地址、邮箱和 API 令牌。"}
- msgUnknownAuth = message{"Unknown sign-in method.", "未知的登录方式。"}
- msgNoProjects = message{"Select at least one project to sync.", "请至少选择一个要同步的项目。"}
- msgToolsNotSetUp = message{
- "Jira is not connected for this workspace: an admin sets it up in Settings → Plugins → Jira.",
- "本空间尚未连接 Jira:请管理员在「设置 → 插件 → Jira」中配置。",
- }
-)
-
-func say(locale string, m message) string {
- if strings.HasPrefix(strings.ToLower(locale), "zh") {
- return m.zh
- }
- return m.en
-}
diff --git a/examples/plugins/jira/package.sh b/examples/plugins/jira/package.sh
deleted file mode 100755
index f9219fe29..000000000
--- a/examples/plugins/jira/package.sh
+++ /dev/null
@@ -1,21 +0,0 @@
-#!/usr/bin/env bash
-# Builds the Jira example plugin for the platforms WeKnora runs on and zips it
-# into a .wkp package: plugin.yaml, schemas, the skill, icon and bin/<os>-<arch>/jira.
-set -euo pipefail
-cd "$(dirname "$0")"
-version=$(sed -n 's/^version: //p' plugin.yaml)
-out=${1:-"weknora-examples-jira-${version}.wkp"}
-stage=$(mktemp -d)
-trap 'rm -rf "$stage"' EXIT
-cp plugin.yaml icon.svg "$stage"/
-cp -r schemas skills "$stage"/
-for target in linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64; do
- os=${target%/*}
- arch=${target#*/}
- bin="$stage/bin/${os}-${arch}/jira"
- [ "$os" = windows ] && bin="$bin.exe"
- CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -trimpath -ldflags="-s -w" -o "$bin" .
-done
-rm -f "$out"
-(cd "$stage" && zip -qr - .) > "$out"
-echo "wrote $out"
diff --git a/examples/plugins/jira/plugin.yaml b/examples/plugins/jira/plugin.yaml
deleted file mode 100644
index c6ffecab6..000000000
--- a/examples/plugins/jira/plugin.yaml
+++ /dev/null
@@ -1,62 +0,0 @@
-schemaVersion: 1
-id: weknora-examples.jira
-version: 1.0.0
-apiVersion: weknora.plugin/v1
-name: { en-US: Jira, zh-CN: Jira }
-description:
- en-US: Sync Jira Cloud issues, with their comments, into a knowledge base; agent tools to search and read issues; and a skill for triaging them. Connects with Atlassian OAuth or an API token; shows dynamic form options and OAuth fields.
- zh-CN: 把 Jira Cloud 的问题(含评论)同步到知识库,提供搜索和读取问题的 Agent 工具,以及问题分诊技能。支持 Atlassian OAuth 授权或 API 令牌;演示表单动态选项与 OAuth 字段。
-publisher: { id: weknora-examples, name: WeKnora examples }
-homepage: https://github.com/Tencent/WeKnora/tree/main/examples/plugins/jira
-license: MIT
-icon: icon.svg
-runtime:
- type: host
- kind: binary
- entry: "bin/{os}-{arch}/jira"
- resources: { cpu: 500m, memory: 256Mi }
-permissions:
- # The OAuth API gateway and the sites themselves. WeKnora, not the plugin,
- # talks to auth.atlassian.com.
- egress: [api.atlassian.com, "*.atlassian.net"]
-config:
- # The platform's Atlassian OAuth 2.0 (3LO) app. Without it, workspaces can
- # still connect with an API token.
- system: schemas/system.yaml
- # The connection the agent tools use in this workspace.
- tenant: schemas/tenant.yaml
-contributes:
- connectors:
- - id: jira
- name: { en-US: Jira, zh-CN: Jira }
- description:
- en-US: Issues of the selected Jira Cloud projects, with comments
- zh-CN: 同步所选 Jira Cloud 项目的问题及评论
- icon: icon.svg
- capabilities: [incremental, streaming]
- instanceSchema: schemas/connector.yaml
- mcpServers:
- # No url: the plugin serves these tools itself.
- - id: tools
- name: { en-US: Jira, zh-CN: Jira }
- description:
- en-US: Search Jira issues with JQL or text, and read one issue with its comments
- zh-CN: 用 JQL 或关键词搜索 Jira 问题,读取单个问题及其评论
- toolViews:
- search_issues:
- view: table
- items: issues
- columns:
- - { field: key, title: { en-US: Key, zh-CN: 编号 }, link: url }
- - { field: summary, title: { en-US: Summary, zh-CN: 摘要 } }
- - { field: status, title: { en-US: Status, zh-CN: 状态 } }
- - { field: assignee, title: { en-US: Assignee, zh-CN: 负责人 } }
- - { field: updated, title: { en-US: Updated, zh-CN: 更新时间 } }
- get_issue: { view: markdown, field: markdown }
- skills:
- - id: jira-triage
- name: { en-US: Jira triage, zh-CN: Jira 问题分诊 }
- description:
- en-US: Find duplicates, related work and owners of a Jira issue in the synced issues
- zh-CN: 在已同步的问题中查找重复、相关工作和负责人
- path: skills/jira-triage
diff --git a/examples/plugins/jira/schemas/connector.yaml b/examples/plugins/jira/schemas/connector.yaml
deleted file mode 100644
index f45fec6d0..000000000
--- a/examples/plugins/jira/schemas/connector.yaml
+++ /dev/null
@@ -1,99 +0,0 @@
-type: object
-# Fields hidden by x-visible-if are not required.
-required: [auth, account, site, base_url, email, api_token]
-properties:
- auth:
- type: string
- title: Sign in with
- default: oauth
- x-order: 1
- oneOf:
- - const: oauth
- title: Atlassian account (OAuth)
- x-i18n: { title: { zh-CN: Atlassian 账号(OAuth) } }
- - const: token
- title: API token
- x-i18n: { title: { zh-CN: API 令牌 } }
- x-i18n:
- title: { zh-CN: 登录方式 }
- account:
- type: string
- title: Atlassian account
- description: Needs the platform's OAuth app (plugin management → Jira)
- x-order: 2
- x-visible-if: { auth: oauth }
- x-oauth:
- authorizeUrl: https://auth.atlassian.com/authorize
- tokenUrl: https://auth.atlassian.com/oauth/token
- scopes: [read:jira-work, read:jira-user, offline_access]
- clientId: ${system.client_id}
- clientSecret: ${system.client_secret}
- params: { audience: api.atlassian.com, prompt: consent }
- x-i18n:
- title: { zh-CN: Atlassian 账号 }
- description: { zh-CN: 需要平台先配置 OAuth 应用(插件管理 → Jira) }
- site:
- type: string
- title: Site
- description: The Jira site the account can reach
- x-order: 3
- x-visible-if: { auth: oauth }
- x-options: { name: sites, dependsOn: [account] }
- x-i18n:
- title: { zh-CN: 站点 }
- description: { zh-CN: 该账号可访问的 Jira 站点 }
- base_url:
- type: string
- title: Site URL
- x-order: 4
- x-visible-if: { auth: token }
- x-placeholder: https://your-team.atlassian.net
- pattern: "^https://[A-Za-z0-9-]+\\.atlassian\\.net/?$"
- x-i18n:
- title: { zh-CN: 站点地址 }
- email:
- type: string
- title: Email
- x-order: 5
- x-visible-if: { auth: token }
- format: email
- x-i18n:
- title: { zh-CN: 邮箱 }
- api_token:
- type: string
- title: API token
- description: Create one at id.atlassian.com → Security → API tokens
- x-order: 6
- x-secret: true
- x-visible-if: { auth: token }
- x-i18n:
- title: { zh-CN: API 令牌 }
- description: { zh-CN: 在 id.atlassian.com → 安全 → API 令牌 中创建 }
- issue_types:
- type: array
- items: { type: string }
- title: Issue types
- description: Only these types; leave empty for all
- x-order: 10
- x-group: settings
- x-options: { name: issue_types, dependsOn: [auth, site, base_url, email] }
- x-i18n:
- title: { zh-CN: 问题类型 }
- description: { zh-CN: 只同步这些类型,留空表示全部 }
- jql:
- type: string
- title: Extra JQL filter
- description: "Added to each project's query, e.g. statusCategory != Done"
- x-order: 11
- x-group: settings
- x-i18n:
- title: { zh-CN: 附加 JQL 条件 }
- description: { zh-CN: "追加到每个项目的查询,例如 statusCategory != Done" }
- include_comments:
- type: boolean
- title: Include comments
- default: true
- x-order: 12
- x-group: settings
- x-i18n:
- title: { zh-CN: 包含评论 }
diff --git a/examples/plugins/jira/schemas/system.yaml b/examples/plugins/jira/schemas/system.yaml
deleted file mode 100644
index eeef01988..000000000
--- a/examples/plugins/jira/schemas/system.yaml
+++ /dev/null
@@ -1,17 +0,0 @@
-type: object
-properties:
- client_id:
- type: string
- title: OAuth client ID
- description: "Client ID of an OAuth 2.0 (3LO) app in the Atlassian developer console, with the Jira API scopes read:jira-work and read:jira-user and the callback URL <WeKnora address>/api/v1/plugin-oauth/callback"
- x-order: 1
- x-i18n:
- title: { zh-CN: OAuth 客户端 ID }
- description: { zh-CN: "在 Atlassian 开发者控制台创建的 OAuth 2.0 (3LO) 应用的 Client ID。应用需开通 Jira API 的 read:jira-work 与 read:jira-user 权限,回调地址填 <WeKnora 地址>/api/v1/plugin-oauth/callback" }
- client_secret:
- type: string
- title: OAuth client secret
- x-secret: true
- x-order: 2
- x-i18n:
- title: { zh-CN: OAuth 客户端密钥 }
diff --git a/examples/plugins/jira/schemas/tenant.yaml b/examples/plugins/jira/schemas/tenant.yaml
deleted file mode 100644
index 3db0f9751..000000000
--- a/examples/plugins/jira/schemas/tenant.yaml
+++ /dev/null
@@ -1,73 +0,0 @@
-# The connection agent tools use in this workspace. Data sources have their
-# own, so each knowledge base can sync with a different account.
-type: object
-# Fields hidden by x-visible-if are not required.
-required: [auth, account, site, base_url, email, api_token]
-properties:
- auth:
- type: string
- title: Sign in with
- default: oauth
- x-order: 1
- oneOf:
- - const: oauth
- title: Atlassian account (OAuth)
- x-i18n: { title: { zh-CN: Atlassian 账号(OAuth) } }
- - const: token
- title: API token
- x-i18n: { title: { zh-CN: API 令牌 } }
- x-i18n:
- title: { zh-CN: 登录方式 }
- account:
- type: string
- title: Atlassian account
- description: Needs the platform's OAuth app (plugin management → Jira)
- x-order: 2
- x-visible-if: { auth: oauth }
- x-oauth:
- authorizeUrl: https://auth.atlassian.com/authorize
- tokenUrl: https://auth.atlassian.com/oauth/token
- scopes: [read:jira-work, read:jira-user, offline_access]
- clientId: ${system.client_id}
- clientSecret: ${system.client_secret}
- params: { audience: api.atlassian.com, prompt: consent }
- x-i18n:
- title: { zh-CN: Atlassian 账号 }
- description: { zh-CN: 需要平台先配置 OAuth 应用(插件管理 → Jira) }
- site:
- type: string
- title: Site
- description: The Jira site the account can reach
- x-order: 3
- x-visible-if: { auth: oauth }
- x-options: { name: sites, dependsOn: [account] }
- x-i18n:
- title: { zh-CN: 站点 }
- description: { zh-CN: 该账号可访问的 Jira 站点 }
- base_url:
- type: string
- title: Site URL
- x-order: 4
- x-visible-if: { auth: token }
- x-placeholder: https://your-team.atlassian.net
- pattern: "^https://[A-Za-z0-9-]+\\.atlassian\\.net/?$"
- x-i18n:
- title: { zh-CN: 站点地址 }
- email:
- type: string
- title: Email
- x-order: 5
- x-visible-if: { auth: token }
- format: email
- x-i18n:
- title: { zh-CN: 邮箱 }
- api_token:
- type: string
- title: API token
- description: Create one at id.atlassian.com → Security → API tokens
- x-order: 6
- x-secret: true
- x-visible-if: { auth: token }
- x-i18n:
- title: { zh-CN: API 令牌 }
- description: { zh-CN: 在 id.atlassian.com → 安全 → API 令牌 中创建 }
diff --git a/examples/plugins/jira/skills/jira-triage/SKILL.md b/examples/plugins/jira/skills/jira-triage/SKILL.md
deleted file mode 100644
index 27b5a927e..000000000
--- a/examples/plugins/jira/skills/jira-triage/SKILL.md
+++ /dev/null
@@ -1,40 +0,0 @@
----
-name: jira-triage
-description: Triage a Jira issue against the issues synced into the knowledge base - find duplicates, related work, likely owners and a suggested priority. Use when the user pastes a Jira key or a bug report and asks whether it is known, who should take it, or how urgent it is.
----
-# Jira triage
-
-The Jira data source stores each issue as one Markdown document titled
-`<KEY> <summary>`. It starts with a fact list (Type, Status, Priority,
-Resolution, Assignee, Reporter, Labels, Created, Updated, Parent, Link), then
-`## Description` and `## Comments`.
-
-## Steps
-
-1. **Understand the report.** If the user gave a key (`ENG-123`), find that
- issue first with `search_knowledge` on the key and read it with
- `read_document`. Otherwise work from the text they pasted. Note the
- component, the symptom, error messages and the version or environment.
-2. **Look for duplicates.** Search with the symptom in a few phrasings and
- with any exact error message. Read the closest matches. Treat as a
- duplicate only when the symptom and the conditions both match, not only
- the area.
-3. **Find related work.** Look for open issues in the same area, recent
- fixes (Resolution set, Updated recently) and parent epics.
-4. **Suggest an owner.** Prefer the assignee of the duplicates or of recent
- fixes in the same area. Say how many issues support the suggestion.
-5. **Suggest a priority.** Compare with the priorities of similar issues and
- weigh impact (data loss, security, many users) over annoyance.
-
-## Answer
-
-Reply in the user's language with:
-
-- **Verdict:** duplicate of `<KEY>`, related to `<KEYS>`, or new.
-- **Evidence:** one line per issue: key, summary, status, and why it
- matches. Link each with the Link from its document.
-- **Owner** and **priority** suggestions, with the reason.
-- **Missing information** the reporter should add, if any.
-
-Only cite issues you read. The knowledge base may lag Jira by one sync; say so
-when the newest issue you found is older than a day.
diff --git a/examples/plugins/jira/testdata/adf.json b/examples/plugins/jira/testdata/adf.json
deleted file mode 100644
index 0c0a7dc37..000000000
--- a/examples/plugins/jira/testdata/adf.json
+++ /dev/null
@@ -1,160 +0,0 @@
-{
- "type": "doc",
- "version": 1,
- "content": [
- {
- "type": "heading",
- "attrs": {
- "level": 2
- },
- "content": [
- {
- "type": "text",
- "text": "Plan"
- }
- ]
- },
- {
- "type": "bulletList",
- "content": [
- {
- "type": "listItem",
- "content": [
- {
- "type": "paragraph",
- "content": [
- {
- "type": "text",
- "text": "one"
- }
- ]
- },
- {
- "type": "orderedList",
- "content": [
- {
- "type": "listItem",
- "content": [
- {
- "type": "paragraph",
- "content": [
- {
- "type": "text",
- "text": "nested"
- }
- ]
- }
- ]
- }
- ]
- }
- ]
- },
- {
- "type": "listItem",
- "content": [
- {
- "type": "paragraph",
- "content": [
- {
- "type": "text",
- "text": "docs",
- "marks": [
- {
- "type": "link",
- "attrs": {
- "href": "https://example.com"
- }
- }
- ]
- },
- {
- "type": "text",
- "text": " by "
- },
- {
- "type": "mention",
- "attrs": {
- "text": "@Ann"
- }
- }
- ]
- }
- ]
- }
- ]
- },
- {
- "type": "codeBlock",
- "attrs": {
- "language": "go"
- },
- "content": [
- {
- "type": "text",
- "text": "x := 1"
- }
- ]
- },
- {
- "type": "table",
- "content": [
- {
- "type": "tableRow",
- "content": [
- {
- "type": "tableHeader",
- "content": [
- {
- "type": "paragraph",
- "content": [
- {
- "type": "text",
- "text": "a|b"
- }
- ]
- }
- ]
- }
- ]
- },
- {
- "type": "tableRow",
- "content": [
- {
- "type": "tableCell",
- "content": [
- {
- "type": "paragraph",
- "content": [
- {
- "type": "text",
- "text": "1"
- }
- ]
- }
- ]
- }
- ]
- }
- ]
- },
- {
- "type": "mediaSingle",
- "content": [
- {
- "type": "media"
- }
- ]
- },
- {
- "type": "someFutureNode",
- "content": [
- {
- "type": "text",
- "text": "kept"
- }
- ]
- }
- ]
-}
diff --git a/examples/plugins/jira/tools.go b/examples/plugins/jira/tools.go
deleted file mode 100644
index a0ec8980f..000000000
--- a/examples/plugins/jira/tools.go
+++ /dev/null
@@ -1,172 +0,0 @@
-package main
-
-import (
- "context"
- "encoding/json"
- "fmt"
- "net/http"
- "net/url"
- "regexp"
- "strings"
-
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// toolsServer is the mcpServers contribution the plugin serves itself.
-const toolsServer = "tools"
-
-func registerTools(p *pluginsdk.Plugin) {
- p.Tool(toolsServer, pluginapi.Tool{
- Name: "search_issues",
- Title: "Search Jira issues",
- Description: "Search Jira issues, newest update first. Give jql for precise queries " +
- `(e.g. project = ENG AND status != Done AND assignee = currentUser()), ` +
- "or text to match summaries, descriptions and comments.",
- InputSchema: json.RawMessage(`{"type":"object","properties":{
- "jql":{"type":"string","description":"A JQL query; takes precedence over text and project"},
- "text":{"type":"string","description":"Words to search for"},
- "project":{"type":"string","description":"A project key to limit text search to"},
- "limit":{"type":"integer","minimum":1,"maximum":50,"default":20}}}`),
- Annotations: &pluginapi.ToolAnnotations{ReadOnlyHint: true},
- }, searchIssuesTool)
- p.Tool(toolsServer, pluginapi.Tool{
- Name: "get_issue",
- Title: "Read a Jira issue",
- Description: "Read one Jira issue by key (e.g. ENG-123): its facts, description and comments.",
- InputSchema: json.RawMessage(`{"type":"object","required":["key"],"properties":{
- "key":{"type":"string","description":"The issue key"}}}`),
- Annotations: &pluginapi.ToolAnnotations{ReadOnlyHint: true},
- }, getIssueTool)
-}
-
-// toolClient connects with the workspace's connection (config.tenant).
-func toolClient(ctx context.Context, call *pluginsdk.Call) (*client, error) {
- var cr credentials
- if err := remarshal(call.Config.Tenant, &cr); err != nil {
- return nil, err
- }
- c, err := newClient(ctx, cr, call.Locale)
- if pe, ok := pluginapi.AsError(err); ok && pe.Code == pluginapi.CodeInvalidConfig {
- return nil, fmt.Errorf("%s %s", say(call.Locale, msgToolsNotSetUp), pe.Message)
- }
- return c, err
-}
-
-// issueRow is one search result, as the result view shows it.
-type issueRow struct {
- Key string `json:"key"`
- Summary string `json:"summary"`
- Status string `json:"status"`
- Type string `json:"type"`
- Priority string `json:"priority"`
- Assignee string `json:"assignee"`
- Updated string `json:"updated"`
- URL string `json:"url"`
-}
-
-func searchIssuesTool(ctx context.Context, call *pluginsdk.Call, args json.RawMessage) (*pluginapi.ToolResult, error) {
- var in struct {
- JQL string `json:"jql"`
- Text string `json:"text"`
- Project string `json:"project"`
- Limit int `json:"limit"`
- }
- if err := json.Unmarshal(args, &in); err != nil {
- return nil, err
- }
- jql, err := toolJQL(in.JQL, in.Text, in.Project)
- if err != nil {
- return nil, err
- }
- if in.Limit <= 0 || in.Limit > 50 {
- in.Limit = 20
- }
- c, err := toolClient(ctx, call)
- if err != nil {
- return nil, err
- }
- issues, more, err := c.searchPage(ctx, jql, in.Limit)
- if err != nil {
- return nil, err
- }
- rows := make([]issueRow, len(issues))
- var text strings.Builder
- fmt.Fprintf(&text, "%d issue(s) for %s", len(issues), jql)
- if more {
- text.WriteString(" (more exist; narrow the query)")
- }
- text.WriteString("\n")
- for i := range issues {
- f := &issues[i].Fields
- rows[i] = issueRow{
- Key: issues[i].Key, Summary: f.Summary, Status: nameOf(f.Status), Type: nameOf(f.IssueType),
- Priority: nameOf(f.Priority), Assignee: userOf(f.Assignee), Updated: stamp(f.Updated),
- URL: c.issueURL(issues[i].Key),
- }
- r := rows[i]
- fmt.Fprintf(&text, "- %s [%s] %s (%s, %s) %s\n", r.Key, r.Status, r.Summary,
- firstNonEmpty(r.Assignee, "unassigned"), r.Updated, r.URL)
- }
- return pluginapi.StructuredResult(map[string]any{"jql": jql, "issues": rows, "more": more}, text.String()), nil
-}
-
-var issueKeyPattern = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9_]*-[0-9]+$`)
-
-func getIssueTool(ctx context.Context, call *pluginsdk.Call, args json.RawMessage) (*pluginapi.ToolResult, error) {
- var in struct {
- Key string `json:"key"`
- }
- if err := json.Unmarshal(args, &in); err != nil {
- return nil, err
- }
- key := strings.ToUpper(strings.TrimSpace(in.Key))
- if !issueKeyPattern.MatchString(key) {
- return pluginapi.ToolError("%q is not an issue key like ENG-123", in.Key), nil
- }
- c, err := toolClient(ctx, call)
- if err != nil {
- return nil, err
- }
- var is issue
- q := url.Values{"fields": {strings.Join(searchFields, ",")}}
- if err := c.do(ctx, http.MethodGet, "/rest/api/3/issue/"+url.PathEscape(key)+"?"+q.Encode(), nil, &is); err != nil {
- if pe, ok := pluginapi.AsError(err); ok && pe.Code == pluginapi.CodeNotFound {
- return pluginapi.ToolError("issue %s does not exist or is not visible to this account", key), nil
- }
- return nil, err
- }
- md := c.markdown(&is)
- return pluginapi.StructuredResult(map[string]any{"key": is.Key, "url": c.issueURL(is.Key), "markdown": md}, md), nil
-}
-
-// toolJQL is the query a search runs: the model's JQL as it is, or a text
-// search, newest update first.
-func toolJQL(jql, text, project string) (string, error) {
- if jql = strings.TrimSpace(jql); jql != "" {
- if !strings.Contains(strings.ToUpper(jql), "ORDER BY") {
- jql += " ORDER BY updated DESC"
- }
- return jql, nil
- }
- var parts []string
- if p := strings.TrimSpace(project); p != "" {
- parts = append(parts, "project = "+quote(p))
- }
- if t := strings.TrimSpace(text); t != "" {
- parts = append(parts, "text ~ "+quote(t))
- }
- if len(parts) == 0 {
- return "", fmt.Errorf("give jql, or text to search for")
- }
- return strings.Join(parts, " AND ") + " ORDER BY updated DESC", nil
-}
-
-func firstNonEmpty(values ...string) string {
- for _, v := range values {
- if v != "" {
- return v
- }
- }
- return ""
-}
diff --git a/examples/plugins/links/main.py b/examples/plugins/links/main.py
deleted file mode 100644
index 184a1c1de..000000000
--- a/examples/plugins/links/main.py
+++ /dev/null
@@ -1,78 +0,0 @@
-"""A WeKnora plugin with pages, written with the Python SDK: a toolbox page
-listing the workspace's links, a settings section where admins edit them,
-and a tab on each knowledge base with links about it. The pages run
-sandboxed in the browser and reach this code only through the bridge
-(wk.get / wk.put), which WeKnora relays as UI requests."""
-
-from __future__ import annotations
-
-import re
-from urllib.parse import urlparse
-
-from weknora_plugin import ErrorCode, Plugin, PluginError, UIResponse
-
-plugin = Plugin("weknora-examples.links", "1.0.0")
-
-MAX_LINKS = 200
-EDITORS = {"contributor", "admin", "owner"}
-KB_PATH = re.compile(r"^/kb/([A-Za-z0-9_-]{1,64})/links$")
-
-
-def clean_links(value) -> list:
- """Validates links from a page: [{title, url}], http(s) only."""
- if not isinstance(value, list) or len(value) > MAX_LINKS:
- raise ValueError(f"send a list of at most {MAX_LINKS} links")
- out = []
- for item in value:
- if not isinstance(item, dict):
- raise ValueError("each link is an object with a title and a url")
- title = str(item.get("title") or "").strip()[:120]
- url = str(item.get("url") or "").strip()
- parsed = urlparse(url)
- if parsed.scheme not in ("http", "https") or not parsed.netloc:
- raise ValueError(f"{url or 'an empty url'} is not an http(s) link")
- out.append({"title": title or parsed.netloc, "url": url[:2000]})
- return out
-
-
-def store(call):
- host = call.host()
- if host is None:
- raise PluginError(ErrorCode.UNAVAILABLE, "the Host API is not available to this plugin")
- return host
-
-
-def handle(call, req) -> UIResponse:
- host = store(call)
- if req.path == "/links":
- if req.method == "GET":
- return UIResponse(body=host.kv_get("links", []))
- # Only the settings section edits the workspace's links; WeKnora
- # lets admins alone open it.
- if req.method == "PUT" and req.mount == "settingsSections/manage":
- return save(host, "links", req.body)
- m = KB_PATH.match(req.path)
- if m and req.mount == "kbTabs/links":
- key = f"kb:{m.group(1)}"
- if req.method == "GET":
- return UIResponse(body=host.kv_get(key, []))
- if req.method == "PUT":
- if req.role not in EDITORS:
- return UIResponse(status=403, body={"error": "only editors of the workspace can change links"})
- return save(host, key, req.body)
- return UIResponse(status=404, body={"error": f"no {req.method} {req.path} here"})
-
-
-def save(host, key, body) -> UIResponse:
- try:
- links = clean_links(body)
- except ValueError as e:
- return UIResponse(status=400, body={"error": str(e)})
- host.kv_put(key, links)
- return UIResponse(body=links)
-
-
-plugin.ui(handle)
-
-if __name__ == "__main__":
- plugin.serve()
diff --git a/examples/plugins/links/package.sh b/examples/plugins/links/package.sh
deleted file mode 100755
index 18d1b053d..000000000
--- a/examples/plugins/links/package.sh
+++ /dev/null
@@ -1,18 +0,0 @@
-#!/usr/bin/env bash
-# Packages the links example plugin as a .wkp: plugin.yaml, main.py, the
-# pages under ui/ with the bridge library, and the Python SDK under vendor/.
-set -euo pipefail
-cd "$(dirname "$0")"
-version=$(sed -n 's/^version: //p' plugin.yaml)
-out=${1:-"weknora-examples-links-${version}.wkp"}
-stage=$(mktemp -d)
-trap 'rm -rf "$stage"' EXIT
-cp plugin.yaml main.py "$stage"/
-cp -r ui "$stage"/
-cp ../../../packages/plugin-ui/index.js "$stage/ui/weknora-plugin-ui.js"
-mkdir -p "$stage/vendor"
-cp -r ../../../pluginsdk/python/src/weknora_plugin "$stage/vendor/"
-find "$stage" -name __pycache__ -prune -exec rm -rf {} +
-rm -f "$out"
-(cd "$stage" && zip -qr - .) > "$out"
-echo "wrote $out"
diff --git a/examples/plugins/links/plugin.yaml b/examples/plugins/links/plugin.yaml
deleted file mode 100644
index 007a1a6e4..000000000
--- a/examples/plugins/links/plugin.yaml
+++ /dev/null
@@ -1,31 +0,0 @@
-schemaVersion: 1
-id: weknora-examples.links
-version: 1.0.0
-apiVersion: weknora.plugin/v1
-name: { en-US: Team links, zh-CN: 团队链接 }
-description:
- en-US: A page of links for the workspace and a tab of links on each knowledge base. Shows plugin pages, written in Python.
- zh-CN: 空间的常用链接页,以及每个知识库上的链接页签。演示插件页面,用 Python 编写。
-publisher: { id: weknora-examples, name: WeKnora examples }
-homepage: https://github.com/Tencent/WeKnora/tree/main/examples/plugins/links
-license: MIT
-runtime: { type: host, kind: python, entry: main.py }
-permissions:
- # Links live in the plugin's key-value store, per workspace.
- hostApi: [kv]
-contributes:
- pages:
- - id: links
- name: { en-US: Team links, zh-CN: 团队链接 }
- description: { en-US: Links the workspace shares, zh-CN: 空间共享的常用链接 }
- entry: ui/index.html
- settingsSections:
- - id: manage
- name: { en-US: Team links, zh-CN: 团队链接 }
- entry: ui/manage.html
- # minRole defaults to admin for settings sections
- kbTabs:
- - id: links
- name: { en-US: Links, zh-CN: 链接 }
- description: { en-US: Links about this knowledge base, zh-CN: 与本知识库相关的链接 }
- entry: ui/kb.html
diff --git a/examples/plugins/links/test_main.py b/examples/plugins/links/test_main.py
deleted file mode 100644
index 0726c619e..000000000
--- a/examples/plugins/links/test_main.py
+++ /dev/null
@@ -1,56 +0,0 @@
-import os
-import sys
-import unittest
-
-HERE = os.path.dirname(os.path.abspath(__file__))
-sys.path.insert(0, os.path.join(HERE, "..", "..", "..", "pluginsdk", "python", "src"))
-sys.path.insert(0, HERE)
-
-import main # noqa: E402
-from weknora_plugin import UIRequest # noqa: E402
-
-
-class FakeHost:
- def __init__(self):
- self.data = {}
-
- def kv_get(self, key, default=None):
- return self.data.get(key, default)
-
- def kv_put(self, key, value, ttl=0):
- self.data[key] = value
-
-
-class LinksTest(unittest.TestCase):
- def setUp(self):
- self.host = FakeHost()
- main.store = lambda call: self.host
-
- def req(self, mount, method, path, body=None, role="viewer"):
- return main.handle(None, UIRequest(mount=mount, method=method, path=path, body=body, role=role))
-
- def test_workspace_links(self):
- self.assertEqual(self.req("pages/links", "GET", "/links").body, [])
- links = [{"title": "", "url": "https://github.com/Tencent/WeKnora"}]
- # Only the settings section (admins) writes the workspace's links.
- self.assertEqual(self.req("pages/links", "PUT", "/links", links, role="owner").status, 404)
- saved = self.req("settingsSections/manage", "PUT", "/links", links, role="admin")
- self.assertEqual(saved.body, [{"title": "github.com", "url": "https://github.com/Tencent/WeKnora"}])
- self.assertEqual(self.req("pages/links", "GET", "/links").body, saved.body)
-
- def test_kb_links(self):
- path = "/kb/kb-1/links"
- self.assertEqual(self.req("kbTabs/links", "PUT", path, [], role="viewer").status, 403)
- self.assertEqual(self.req("kbTabs/links", "PUT", path, [{"title": "Spec", "url": "http://x.example"}], role="contributor").status, 200)
- self.assertEqual(self.req("kbTabs/links", "GET", path).body[0]["title"], "Spec")
- self.assertEqual(self.req("kbTabs/links", "GET", "/kb/kb-2/links").body, [])
- self.assertEqual(self.req("pages/links", "GET", path).status, 404)
-
- def test_bad_links(self):
- for body in ({"url": "x"}, [{"url": "javascript:alert(1)"}], [{"url": "ftp://x"}], [{}] * 201):
- resp = self.req("settingsSections/manage", "PUT", "/links", body, role="admin")
- self.assertEqual(resp.status, 400, body)
-
-
-if __name__ == "__main__":
- unittest.main()
diff --git a/examples/plugins/links/ui/app.css b/examples/plugins/links/ui/app.css
deleted file mode 100644
index d875d4902..000000000
--- a/examples/plugins/links/ui/app.css
+++ /dev/null
@@ -1,41 +0,0 @@
-/* Colours and fonts come from the app theme (--wk-* variables, set by the
- bridge), with fallbacks for opening the page on its own. */
-:root {
- color-scheme: light;
- --fg: var(--wk-text-color-primary, #1f2329);
- --muted: var(--wk-text-color-secondary, #646a73);
- --line: var(--wk-component-border, #dee0e3);
- --card: var(--wk-bg-color-container, #fff);
- --brand: var(--wk-brand-color, #0052d9);
- --danger: var(--wk-error-color, #d54941);
- --radius: var(--wk-radius-default, 6px);
-}
-:root[data-theme='dark'] { color-scheme: dark; }
-* { box-sizing: border-box; }
-body {
- margin: 0;
- font: 14px/1.5 var(--wk-font-family, system-ui, sans-serif);
- color: var(--fg);
- background: transparent;
-}
-main { padding: 4px 2px 12px; }
-.empty { color: var(--muted); padding: 24px 0; text-align: center; }
-ul.links { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
-.links li {
- display: flex; align-items: center; gap: 10px;
- padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--card);
-}
-.links a { color: var(--brand); text-decoration: none; font-weight: 500; }
-.links a:hover { text-decoration: underline; }
-.links .url { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
-form { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
-input {
- flex: 1; min-width: 140px; padding: 6px 10px; font: inherit; color: var(--fg);
- background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
-}
-button {
- padding: 6px 14px; font: inherit; border-radius: var(--radius); cursor: pointer;
- border: 1px solid var(--brand); background: var(--brand); color: #fff;
-}
-button.remove { border-color: transparent; background: transparent; color: var(--danger); padding: 4px 8px; }
-button:disabled { opacity: 0.6; cursor: default; }
diff --git a/examples/plugins/links/ui/index.html b/examples/plugins/links/ui/index.html
deleted file mode 100644
index d71690eba..000000000
--- a/examples/plugins/links/ui/index.html
+++ /dev/null
@@ -1,13 +0,0 @@
-<!doctype html>
-<html lang="en">
-<head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1" />
- <title>Team links</title>
- <link rel="stylesheet" href="app.css" />
-</head>
-<body data-mode="view">
- <main id="app" aria-busy="true"></main>
- <script type="module" src="links.js"></script>
-</body>
-</html>
diff --git a/examples/plugins/links/ui/kb.html b/examples/plugins/links/ui/kb.html
deleted file mode 100644
index f1214a575..000000000
--- a/examples/plugins/links/ui/kb.html
+++ /dev/null
@@ -1,13 +0,0 @@
-<!doctype html>
-<html lang="en">
-<head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1" />
- <title>Team links</title>
- <link rel="stylesheet" href="app.css" />
-</head>
-<body data-mode="kb">
- <main id="app" aria-busy="true"></main>
- <script type="module" src="links.js"></script>
-</body>
-</html>
diff --git a/examples/plugins/links/ui/links.js b/examples/plugins/links/ui/links.js
deleted file mode 100644
index f2bdbb408..000000000
--- a/examples/plugins/links/ui/links.js
+++ /dev/null
@@ -1,95 +0,0 @@
-// The three pages of the plugin. They run in a sandboxed iframe: no network,
-// no storage of the app. Everything goes through the bridge (wk.*), which
-// the app relays to main.py.
-import { connect } from './weknora-plugin-ui.js'
-
-const TEXT = {
- en: {
- empty: 'No links yet.', emptyManage: 'No links yet. Add the first one below.',
- title: 'Title', url: 'https://…', add: 'Add', remove: 'Remove', saved: 'Links saved',
- confirm: 'Remove this link?', readOnly: 'Only editors of the workspace can change these links.',
- },
- zh: {
- empty: '还没有链接。', emptyManage: '还没有链接,在下方添加第一个。',
- title: '标题', url: 'https://…', add: '添加', remove: '删除', saved: '链接已保存',
- confirm: '删除这个链接?', readOnly: '只有空间的编辑者可以修改这些链接。',
- },
-}
-
-const wk = await connect()
-const app = document.getElementById('app')
-const mode = document.body.dataset.mode
-let text = pick(wk.context.locale)
-wk.on('locale', (locale) => {
- text = pick(locale)
- void load()
-})
-// A knowledge base tab follows the app to another knowledge base.
-wk.on('init', () => void load())
-
-function pick(locale) {
- return String(locale || '').startsWith('zh') ? TEXT.zh : TEXT.en
-}
-
-function path() {
- return mode === 'kb' ? `/kb/${wk.context.context.knowledgeBaseId}/links` : '/links'
-}
-
-function el(tag, props = {}, ...children) {
- const node = Object.assign(document.createElement(tag), props)
- node.append(...children)
- return node
-}
-
-async function load() {
- app.setAttribute('aria-busy', 'true')
- try {
- const { body } = await wk.get(path())
- render(Array.isArray(body) ? body : [])
- } catch (e) {
- app.replaceChildren(el('p', { className: 'empty', textContent: e.message }))
- } finally {
- app.removeAttribute('aria-busy')
- }
-}
-
-async function save(links) {
- try {
- const { body } = await wk.put(path(), links)
- await wk.toast(text.saved, 'success')
- render(body)
- } catch (e) {
- await wk.toast(e.message, 'error')
- }
-}
-
-function render(links) {
- const editable = mode === 'manage' || (mode === 'kb' && wk.context.role !== 'viewer')
- const list = el('ul', { className: 'links' })
- links.forEach((link, i) => {
- const a = el('a', { href: link.url, target: '_blank', rel: 'noopener noreferrer', textContent: link.title })
- const item = el('li', {}, a, el('span', { className: 'url', textContent: link.url }))
- if (editable) {
- const remove = el('button', { className: 'remove', type: 'button', textContent: text.remove })
- remove.addEventListener('click', async () => {
- if (await wk.confirm(text.confirm)) await save(links.filter((_, j) => j !== i))
- })
- item.append(remove)
- }
- list.append(item)
- })
- const nodes = links.length ? [list] : [el('p', { className: 'empty', textContent: mode === 'manage' ? text.emptyManage : text.empty })]
- if (editable) {
- const title = el('input', { placeholder: text.title, maxLength: 120 })
- const url = el('input', { placeholder: text.url, type: 'url', required: true })
- const form = el('form', {}, title, url, el('button', { type: 'submit', textContent: text.add }))
- form.addEventListener('submit', async (event) => {
- event.preventDefault()
- await save([...links, { title: title.value, url: url.value }])
- })
- nodes.push(form)
- }
- app.replaceChildren(...nodes)
-}
-
-await load()
diff --git a/examples/plugins/links/ui/manage.html b/examples/plugins/links/ui/manage.html
deleted file mode 100644
index 9a63b9d8a..000000000
--- a/examples/plugins/links/ui/manage.html
+++ /dev/null
@@ -1,13 +0,0 @@
-<!doctype html>
-<html lang="en">
-<head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1" />
- <title>Team links</title>
- <link rel="stylesheet" href="app.css" />
-</head>
-<body data-mode="manage">
- <main id="app" aria-busy="true"></main>
- <script type="module" src="links.js"></script>
-</body>
-</html>
diff --git a/examples/plugins/notebooks/main.py b/examples/plugins/notebooks/main.py
deleted file mode 100644
index 11a71ee62..000000000
--- a/examples/plugins/notebooks/main.py
+++ /dev/null
@@ -1,106 +0,0 @@
-"""A WeKnora parser plugin, written with the Python SDK: Jupyter notebooks
-(.ipynb) become Markdown. Markdown cells are kept as they are, code cells
-become fenced blocks, text outputs follow their cell and PNG/JPEG charts are
-handed to WeKnora as images."""
-
-from __future__ import annotations
-
-import base64
-import json
-
-from weknora_plugin import ErrorCode, ParsedImage, ParseOutput, Plugin, PluginError
-
-plugin = Plugin("weknora-examples.notebooks", "1.0.0")
-
-IMAGE_TYPES = {"image/png": "png", "image/jpeg": "jpg"}
-
-
-def _text(value) -> str:
- """Notebook strings are either a string or a list of lines."""
- if isinstance(value, list):
- return "".join(value)
- return value or ""
-
-
-def _fence(body: str, lang: str = "") -> str:
- # A fence longer than any backtick run inside, so code cannot close it.
- longest, run = 0, 0
- for ch in body:
- run = run + 1 if ch == "`" else 0
- longest = max(longest, run)
- ticks = "`" * max(3, longest + 1)
- return f"{ticks}{lang}\n{body.rstrip()}\n{ticks}"
-
-
-def convert(nb: dict, include_outputs: bool = True, max_output_chars: int = 2000) -> ParseOutput:
- lang = ((nb.get("metadata") or {}).get("kernelspec") or {}).get("language") or (
- (nb.get("metadata") or {}).get("language_info") or {}
- ).get("name", "")
- parts, images = [], []
- for i, cell in enumerate(nb.get("cells") or []):
- kind, source = cell.get("cell_type"), _text(cell.get("source")).strip()
- if kind == "markdown" and source:
- parts.append(source)
- elif kind == "code":
- if source:
- parts.append(_fence(source, lang))
- if include_outputs:
- parts.extend(_outputs(i, cell.get("outputs") or [], images, max_output_chars))
- elif kind == "raw" and source:
- parts.append(_fence(source))
- title = next((p.splitlines()[0][2:].strip() for p in parts if p.startswith("# ")), "")
- meta = {"cells": str(len(nb.get("cells") or [])), "language": lang}
- if title:
- meta["title"] = title
- return ParseOutput(markdown="\n\n".join(parts) + "\n", images=images, metadata=meta)
-
-
-def _outputs(cell: int, outputs: list, images: list, limit: int) -> list:
- parts = []
- for j, out in enumerate(outputs):
- kind = out.get("output_type")
- if kind == "stream":
- text = _text(out.get("text"))
- elif kind == "error":
- text = f"{out.get('ename', 'Error')}: {out.get('evalue', '')}"
- else:
- data = out.get("data") or {}
- image = next((m for m in IMAGE_TYPES if m in data), None)
- if image:
- ref = f"images/cell{cell}-{j}.{IMAGE_TYPES[image]}"
- try:
- images.append(ParsedImage(original_ref=ref, data=base64.b64decode(_text(data[image])), mime_type=image))
- parts.append(f"![Output of cell {cell + 1}]({ref})")
- except ValueError:
- pass
- continue
- if "text/markdown" in data:
- parts.append(_text(data["text/markdown"]).strip())
- continue
- text = _text(data.get("text/plain"))
- text = text.strip()
- if text:
- if limit and len(text) > limit:
- text = text[:limit] + "\n…"
- parts.append(_fence(text, "text"))
- return parts
-
-
-def parse(call, doc):
- try:
- nb = json.loads(doc.content.decode("utf-8-sig"))
- except (UnicodeDecodeError, ValueError) as e:
- raise PluginError(ErrorCode.BAD_REQUEST, f"{doc.file_name or 'file'} is not a Jupyter notebook: {e}") from None
- if not isinstance(nb, dict) or "cells" not in nb:
- raise PluginError(ErrorCode.BAD_REQUEST, f"{doc.file_name or 'file'} is not a Jupyter notebook (no cells)")
- cfg = call.tenant
- return convert(
- nb,
- include_outputs=cfg.get("include_outputs", True) is not False,
- max_output_chars=int(cfg.get("max_output_chars", 2000) or 0),
- )
-
-
-if __name__ == "__main__":
- plugin.serve()
diff --git a/examples/plugins/notebooks/package.sh b/examples/plugins/notebooks/package.sh
deleted file mode 100755
index e94ba822e..000000000
--- a/examples/plugins/notebooks/package.sh
+++ /dev/null
@@ -1,18 +0,0 @@
-#!/usr/bin/env bash
-# Packages the notebooks example plugin as a .wkp: plugin.yaml, schemas,
-# main.py and the Python SDK vendored under vendor/. Nothing is compiled;
-# WeKnora runs main.py with its own python3.
-set -euo pipefail
-cd "$(dirname "$0")"
-version=$(sed -n 's/^version: //p' plugin.yaml)
-out=${1:-"weknora-examples-notebooks-${version}.wkp"}
-stage=$(mktemp -d)
-trap 'rm -rf "$stage"' EXIT
-cp plugin.yaml main.py "$stage"/
-cp -r schemas "$stage"/
-mkdir -p "$stage/vendor"
-cp -r ../../../pluginsdk/python/src/weknora_plugin "$stage/vendor/"
-find "$stage" -name __pycache__ -prune -exec rm -rf {} +
-rm -f "$out"
-(cd "$stage" && zip -qr - .) > "$out"
-echo "wrote $out"
diff --git a/examples/plugins/notebooks/plugin.yaml b/examples/plugins/notebooks/plugin.yaml
deleted file mode 100644
index bcbe15180..000000000
--- a/examples/plugins/notebooks/plugin.yaml
+++ /dev/null
@@ -1,22 +0,0 @@
-schemaVersion: 1
-id: weknora-examples.notebooks
-version: 1.0.0
-apiVersion: weknora.plugin/v1
-name: { en-US: Jupyter notebooks, zh-CN: Jupyter 笔记本 }
-description:
- en-US: Parse Jupyter notebooks into Markdown with their code, text outputs and charts. Written in Python.
- zh-CN: 把 Jupyter 笔记本解析为 Markdown,保留代码、文本输出和图表。用 Python 编写。
-publisher: { id: weknora-examples, name: WeKnora examples }
-homepage: https://github.com/Tencent/WeKnora/tree/main/examples/plugins/notebooks
-license: MIT
-runtime: { type: host, kind: python, entry: main.py }
-config:
- tenant: schemas/tenant.yaml
-contributes:
- parsers:
- - id: ipynb
- name: { en-US: Jupyter notebooks, zh-CN: Jupyter 笔记本 }
- description:
- en-US: Markdown and code cells, text outputs and PNG/JPEG charts
- zh-CN: Markdown 与代码单元格、文本输出和 PNG/JPEG 图表
- fileTypes: [ipynb]
diff --git a/examples/plugins/notebooks/schemas/tenant.yaml b/examples/plugins/notebooks/schemas/tenant.yaml
deleted file mode 100644
index 2c43e2a73..000000000
--- a/examples/plugins/notebooks/schemas/tenant.yaml
+++ /dev/null
@@ -1,19 +0,0 @@
-type: object
-properties:
- include_outputs:
- type: boolean
- title: Include cell outputs
- description: Index what code cells printed and plotted, not just the code
- default: true
- x-i18n:
- title: { zh-CN: 包含单元格输出 }
- description: { zh-CN: 除代码外,也索引代码单元格的打印内容和图表 }
- max_output_chars:
- type: integer
- title: Output length limit
- description: Characters kept from each text output
- default: 2000
- minimum: 0
- x-i18n:
- title: { zh-CN: 输出长度上限 }
- description: { zh-CN: 每段文本输出保留的字符数 }
diff --git a/examples/plugins/notebooks/test_main.py b/examples/plugins/notebooks/test_main.py
deleted file mode 100644
index f23d93ccb..000000000
--- a/examples/plugins/notebooks/test_main.py
+++ /dev/null
@@ -1,73 +0,0 @@
-import base64
-import json
-import os
-import sys
-import unittest
-
-HERE = os.path.dirname(os.path.abspath(__file__))
-sys.path.insert(0, os.path.join(HERE, "..", "..", "..", "pluginsdk", "python", "src"))
-sys.path.insert(0, HERE)
-
-from main import convert, plugin # noqa: E402
-from weknora_plugin import ErrorCode, ParseInput, PluginError # noqa: E402
-from weknora_plugin.plugin import Call # noqa: E402
-
-PNG = base64.b64encode(b"\x89PNG fake").decode()
-
-NOTEBOOK = {
- "metadata": {"kernelspec": {"language": "python"}},
- "cells": [
- {"cell_type": "markdown", "source": ["# Sales report\n", "Quarterly numbers."]},
- {
- "cell_type": "code",
- "source": "print('total', 42)\nx = '```'",
- "outputs": [
- {"output_type": "stream", "name": "stdout", "text": ["total 42\n"]},
- {"output_type": "display_data", "data": {"image/png": PNG, "text/plain": "<Figure>"}},
- {"output_type": "execute_result", "data": {"text/plain": ["'" + "a" * 50 + "'"]}},
- {"output_type": "error", "ename": "ValueError", "evalue": "bad"},
- ],
- },
- {"cell_type": "code", "source": "", "outputs": []},
- ],
-}
-
-
-class ConvertTest(unittest.TestCase):
- def test_cells_and_outputs(self):
- out = convert(NOTEBOOK, max_output_chars=20)
- md = out.markdown
- self.assertTrue(md.startswith("# Sales report\nQuarterly numbers.\n\n````python\nprint('total', 42)"))
- self.assertIn("```text\ntotal 42\n```", md)
- self.assertIn("![Output of cell 2](images/cell1-1.png)", md)
- self.assertIn("'" + "a" * 19 + "\n…", md)
- self.assertIn("ValueError: bad", md)
- self.assertEqual([(i.original_ref, i.data, i.mime_type) for i in out.images], [("images/cell1-1.png", b"\x89PNG fake", "image/png")])
- self.assertEqual(out.metadata, {"cells": "3", "language": "python", "title": "Sales report"})
-
- def test_without_outputs(self):
- out = convert(NOTEBOOK, include_outputs=False)
- self.assertNotIn("total 42", out.markdown)
- self.assertEqual(out.images, [])
-
-
-class ParseTest(unittest.TestCase):
- def call(self, tenant=None):
- return Call({"context": {"tenantId": 1}, "config": {"tenant": tenant or {}}})
-
- def test_parse(self):
- doc = ParseInput(file_name="r.ipynb", file_type="ipynb", content=json.dumps(NOTEBOOK).encode())
- parse = plugin._parsers["ipynb"]
- self.assertIn("total 42", parse(self.call(), doc).markdown)
- self.assertNotIn("total 42", parse(self.call({"include_outputs": False}), doc).markdown)
-
- def test_not_a_notebook(self):
- parse = plugin._parsers["ipynb"]
- for content in (b"WeKnora conformance check\n", b"[]", b"\xff\xfe"):
- with self.assertRaises(PluginError) as ctx:
- parse(self.call(), ParseInput(file_name="x.ipynb", content=content))
- self.assertEqual(ctx.exception.code, ErrorCode.BAD_REQUEST)
-
-
-if __name__ == "__main__":
- unittest.main()
diff --git a/examples/plugins/rss/feeds.go b/examples/plugins/rss/feeds.go
deleted file mode 100644
index a01e1aace..000000000
--- a/examples/plugins/rss/feeds.go
+++ /dev/null
@@ -1,282 +0,0 @@
-package main
-
-import (
- "bytes"
- "context"
- "crypto/sha256"
- "encoding/hex"
- "fmt"
- "io"
- "maps"
- "net/http"
- "net/url"
- "strings"
- "time"
- "unicode/utf8"
-
- readability "codeberg.org/readeck/go-readability/v2"
- htmltomd "github.com/JohannesKaufmann/html-to-markdown/v2"
- "github.com/mmcdole/gofeed"
-)
-
-const (
- requestTimeout = 20 * time.Second
- maxFeedSize = 10 << 20
- maxArticleSize = 5 << 20
- defaultUserAgent = "Mozilla/5.0 (compatible; WeKnora-RSS/1.0; +https://weknora.weixin.qq.com)"
- maxFileNameBytes = 200
-)
-
-// config is the data source configuration: feed URLs in settings, optional
-// auth headers (which may carry secrets) in credentials.
-type config struct {
- feedURLs []string
- authHeaders map[string]string
-}
-
-func parseConfig(settings, credentials map[string]any) (*config, error) {
- raw, _ := settings["feed_urls"].(string)
- if strings.TrimSpace(raw) == "" {
- // Older rows kept the URLs with the credentials.
- raw, _ = credentials["feed_urls"].(string)
- }
- cfg := &config{feedURLs: splitURLs(raw)}
- if len(cfg.feedURLs) == 0 {
- return nil, errMissingFeeds
- }
- headers, _ := credentials["auth_headers"].(string)
- cfg.authHeaders = parseHeaders(headers)
- return cfg, nil
-}
-
-// splitURLs splits on newlines and commas, trims, dedupes (order kept).
-func splitURLs(raw string) []string {
- parts := strings.FieldsFunc(raw, func(r rune) bool { return r == '\n' || r == '\r' || r == ',' })
- seen := map[string]bool{}
- var out []string
- for _, u := range parts {
- u = strings.TrimSpace(u)
- if u == "" || seen[u] {
- continue
- }
- seen[u] = true
- out = append(out, u)
- }
- return out
-}
-
-func parseHeaders(raw string) map[string]string {
- headers := map[string]string{}
- for _, line := range strings.Split(raw, "\n") {
- name, value, ok := strings.Cut(strings.TrimSpace(line), ":")
- name = strings.TrimSpace(name)
- if !ok || name == "" {
- continue
- }
- headers[name] = strings.TrimSpace(value)
- }
- if len(headers) == 0 {
- return nil
- }
- return headers
-}
-
-// fetcher downloads feeds and article pages. Outbound traffic goes through
-// the plugin host's egress proxy (HTTPS_PROXY), which refuses private
-// addresses, so the plugin needs no SSRF logic of its own.
-type fetcher struct {
- http *http.Client
- headers map[string]string
-}
-
-func newFetcher(headers map[string]string) *fetcher {
- return &fetcher{http: &http.Client{Timeout: requestTimeout}, headers: headers}
-}
-
-// fetch attaches auth headers only to feed fetches; article pages on
-// third-party sites must not receive feed credentials.
-func (f *fetcher) fetch(ctx context.Context, rawURL string, maxSize int64, withAuth bool) ([]byte, error) {
- u, err := url.Parse(rawURL)
- if err != nil || (u.Scheme != "http" && u.Scheme != "https") {
- return nil, fmt.Errorf("invalid URL %q", rawURL)
- }
- req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
- if err != nil {
- return nil, err
- }
- if withAuth {
- for k, v := range f.headers {
- req.Header.Set(k, v)
- }
- }
- if req.Header.Get("User-Agent") == "" {
- req.Header.Set("User-Agent", defaultUserAgent)
- }
- if req.Header.Get("Accept") == "" {
- req.Header.Set("Accept", "application/rss+xml, application/atom+xml, application/xml, text/xml, "+
- "application/json, text/html;q=0.9, */*;q=0.8")
- }
- resp, err := f.http.Do(req)
- if err != nil {
- return nil, fmt.Errorf("fetch failed: %w", err)
- }
- defer func() { _ = resp.Body.Close() }()
- if resp.StatusCode < 200 || resp.StatusCode >= 300 {
- return nil, fmt.Errorf("HTTP %d %s", resp.StatusCode, resp.Status)
- }
- body, err := io.ReadAll(io.LimitReader(resp.Body, maxSize+1))
- if err != nil {
- return nil, fmt.Errorf("read body failed: %w", err)
- }
- if int64(len(body)) > maxSize {
- return nil, fmt.Errorf("response exceeds maximum size (%d bytes)", maxSize)
- }
- return body, nil
-}
-
-func (f *fetcher) feed(ctx context.Context, feedURL string) (*gofeed.Feed, error) {
- data, err := f.fetch(ctx, feedURL, maxFeedSize, true)
- if err != nil {
- return nil, err
- }
- feed, err := gofeed.NewParser().Parse(bytes.NewReader(data))
- if err != nil {
- return nil, fmt.Errorf("parse feed: %w", err)
- }
- return feed, nil
-}
-
-// article returns the readability-cleaned main content of a page as HTML.
-func (f *fetcher) article(ctx context.Context, articleURL string) (string, string, error) {
- body, err := f.fetch(ctx, articleURL, maxArticleSize, false)
- if err != nil {
- return "", "", err
- }
- pageURL, _ := url.Parse(articleURL)
- art, err := readability.FromReader(bytes.NewReader(body), pageURL)
- if err != nil {
- return "", "", fmt.Errorf("readability parse: %w", err)
- }
- if art.Node == nil {
- return "", "", fmt.Errorf("no readable content extracted")
- }
- var buf bytes.Buffer
- if err := art.RenderHTML(&buf); err != nil {
- return "", "", fmt.Errorf("render article html: %w", err)
- }
- return buf.String(), art.Title(), nil
-}
-
-// cursorState is the incremental sync state, the same shape the builtin
-// connector keeps: feed URL → item ID → content fingerprint, and feed URL →
-// item ID → feed-only signal that lets unchanged entries skip article fetches.
-type cursorState struct {
- FeedItems map[string]map[string]string
- FeedSignals map[string]map[string]string
-}
-
-func newCursorState() *cursorState {
- return &cursorState{FeedItems: map[string]map[string]string{}, FeedSignals: map[string]map[string]string{}}
-}
-
-func cursorFromState(state map[string]any) *cursorState {
- c := newCursorState()
- read := func(key string, into map[string]map[string]string) {
- feeds, _ := state[key].(map[string]any)
- for feedURL, items := range feeds {
- m, _ := items.(map[string]any)
- into[feedURL] = map[string]string{}
- for id, v := range m {
- if s, ok := v.(string); ok {
- into[feedURL][id] = s
- }
- }
- }
- }
- read("feed_items", c.FeedItems)
- read("feed_signals", c.FeedSignals)
- return c
-}
-
-func (c *cursorState) toState() map[string]any {
- return map[string]any{"feed_items": c.FeedItems, "feed_signals": c.FeedSignals}
-}
-
-func (c *cursorState) copyFeed(prev *cursorState, feedURL string) {
- if prev == nil {
- return
- }
- if src := prev.FeedItems[feedURL]; len(src) > 0 {
- c.FeedItems[feedURL] = maps.Clone(src)
- }
- if src := prev.FeedSignals[feedURL]; len(src) > 0 {
- c.FeedSignals[feedURL] = maps.Clone(src)
- }
-}
-
-func contentFingerprint(markdown string) string {
- sum := sha256.Sum256([]byte(markdown))
- return "h:" + hex.EncodeToString(sum[:])[:16]
-}
-
-func feedSignal(item *gofeed.Item, feedContent string) string {
- var b strings.Builder
- b.WriteString(item.GUID + "\n" + item.Link + "\n" + item.Title + "\n")
- if item.UpdatedParsed != nil && !item.UpdatedParsed.IsZero() {
- b.WriteString(item.UpdatedParsed.UTC().Format(time.RFC3339))
- }
- b.WriteByte('\n')
- if item.PublishedParsed != nil && !item.PublishedParsed.IsZero() {
- b.WriteString(item.PublishedParsed.UTC().Format(time.RFC3339))
- }
- b.WriteByte('\n')
- b.WriteString(feedContent)
- sum := sha256.Sum256([]byte(b.String()))
- return "s:" + hex.EncodeToString(sum[:])[:16]
-}
-
-func firstNonEmpty(values ...string) string {
- for _, v := range values {
- if strings.TrimSpace(v) != "" {
- return v
- }
- }
- return ""
-}
-
-func htmlToMarkdown(html string) string {
- if strings.TrimSpace(html) == "" {
- return ""
- }
- md, err := htmltomd.ConvertString(html)
- if err != nil || strings.TrimSpace(md) == "" {
- return strings.TrimSpace(html)
- }
- return strings.TrimSpace(md)
-}
-
-var fileNameReplacer = strings.NewReplacer(
- "/", "_", "\\", "_", ":", "_", "*", "_", "?", "_", "\"", "_", "<", "_", ">", "_", "|", "_",
-)
-
-// fileName makes a feed title safe to use as a file name, the way WeKnora's
-// builtin connectors do: hostile punctuation replaced, at most 200 bytes
-// without splitting a rune.
-func fileName(title string) string {
- title = strings.TrimSpace(strings.NewReplacer("\n", " ", "\r", " ", "\t", " ").Replace(title))
- if title == "" {
- return "untitled.md"
- }
- name := fileNameReplacer.Replace(title)
- if len(name) > maxFileNameBytes {
- name = name[:maxFileNameBytes]
- for len(name) > 0 {
- r, size := utf8.DecodeLastRuneInString(name)
- if r != utf8.RuneError || size != 1 {
- break
- }
- name = name[:len(name)-1]
- }
- }
- return name + ".md"
-}
diff --git a/examples/plugins/rss/icon.svg b/examples/plugins/rss/icon.svg
deleted file mode 100644
index 485e6cec7..000000000
--- a/examples/plugins/rss/icon.svg
+++ /dev/null
@@ -1 +0,0 @@
-<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect width="24" height="24" rx="5" fill="#f26522"/><circle cx="6.5" cy="17.5" r="2" fill="#fff"/><path d="M4.5 10.5a9 9 0 0 1 9 9h-2.6a6.4 6.4 0 0 0-6.4-6.4z" fill="#fff"/><path d="M4.5 4.5a15 15 0 0 1 15 15h-2.6A12.4 12.4 0 0 0 4.5 7.1z" fill="#fff"/></svg>
diff --git a/examples/plugins/rss/main.go b/examples/plugins/rss/main.go
deleted file mode 100644
index d1aaa55e1..000000000
--- a/examples/plugins/rss/main.go
+++ /dev/null
@@ -1,229 +0,0 @@
-// Command rss is WeKnora's RSS/Atom/JSON feed connector written as an
-// external plugin with the Go SDK. It behaves like the builtin rss connector
-// (a test syncs the same feeds through both) and shows how a code plugin is
-// built: it imports only the SDK and ordinary libraries, never WeKnora's
-// internals.
-//
-// Build a package with ./package.sh; install it under System administration
-// → Plugin management.
-package main
-
-import (
- "context"
- "errors"
- "fmt"
- "log"
- "strings"
- "time"
-
- "github.com/mmcdole/gofeed"
-
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// Version must match plugin.yaml.
-const Version = "1.0.0"
-
-var errMissingFeeds = pluginapi.InvalidConfig(
- "feed_urls is required",
- map[string]string{"settings.feed_urls": "required"},
-)
-
-func main() {
- p := pluginsdk.New(pluginsdk.Info{ID: "weknora-examples.rss", Version: Version})
- p.Connector("rss", &connector{logf: p.Logger().Warn})
- if err := p.Serve(); err != nil {
- log.Fatal(err)
- }
-}
-
-type connector struct {
- logf func(msg string, args ...any)
-}
-
-func (c *connector) warn(msg string, args ...any) {
- if c.logf != nil {
- c.logf(msg, args...)
- }
-}
-
-// Validate fetches and parses every configured feed.
-func (c *connector) Validate(ctx context.Context, _ *pluginsdk.Call, in pluginsdk.ConnectorConfig) error {
- cfg, err := parseConfig(in.Settings, in.Credentials)
- if err != nil {
- return err
- }
- f := newFetcher(cfg.authHeaders)
- for _, u := range cfg.feedURLs {
- if _, err := f.feed(ctx, u); err != nil {
- return pluginapi.InvalidConfig(fmt.Sprintf("feed %s: %v", u, err),
- map[string]string{"settings.feed_urls": err.Error()})
- }
- }
- return nil
-}
-
-// ListResources lists one resource per feed. A feed that fails still
-// appears, named by its URL, so the user can deselect it.
-func (c *connector) ListResources(
- ctx context.Context, _ *pluginsdk.Call, in pluginsdk.ConnectorConfig, parentID string,
-) ([]pluginapi.Resource, error) {
- if parentID != "" {
- return nil, nil
- }
- cfg, err := parseConfig(in.Settings, in.Credentials)
- if err != nil {
- return nil, err
- }
- f := newFetcher(cfg.authHeaders)
- out := make([]pluginapi.Resource, 0, len(cfg.feedURLs))
- for _, u := range cfg.feedURLs {
- res := pluginapi.Resource{ExternalID: u, Type: "feed", Name: u, URL: u}
- feed, err := f.feed(ctx, u)
- if err != nil {
- res.Description = "fetch failed: " + err.Error()
- out = append(out, res)
- continue
- }
- if t := strings.TrimSpace(feed.Title); t != "" {
- res.Name = t
- }
- res.Description = strings.TrimSpace(feed.Description)
- if feed.Link != "" {
- res.URL = feed.Link
- }
- res.ModifiedAt = feed.UpdatedParsed
- res.Metadata = map[string]any{"item_count": len(feed.Items)}
- out = append(out, res)
- }
- return out, nil
-}
-
-// Fetch syncs the selected feeds (all configured ones when none are
-// selected). Incremental runs skip entries whose feed signal and content are
-// unchanged; deletions are never emitted, since feeds drop old items as a
-// matter of course.
-func (c *connector) Fetch(
- ctx context.Context,
- _ *pluginsdk.Call,
- in pluginsdk.ConnectorConfig,
- req pluginapi.FetchInput,
- s *pluginsdk.Stream,
-) (*pluginapi.Cursor, error) {
- cfg, err := parseConfig(in.Settings, in.Credentials)
- if err != nil {
- return nil, err
- }
- feedURLs := req.ResourceIDs
- if len(feedURLs) == 0 {
- feedURLs = cfg.feedURLs
- }
- var prev *cursorState
- incremental := req.Mode == pluginapi.FetchIncremental && req.Cursor != nil
- if incremental {
- prev = cursorFromState(req.Cursor.State)
- }
- f := newFetcher(cfg.authHeaders)
- next := newCursorState()
- now := time.Now().UTC()
- var failures []string
-
- for _, feedURL := range feedURLs {
- feed, err := f.feed(ctx, feedURL)
- if err != nil {
- c.warn("feed failed", "url", feedURL, "err", err)
- failures = append(failures, fmt.Sprintf("%s: %v", feedURL, err))
- next.copyFeed(prev, feedURL)
- continue
- }
- next.FeedItems[feedURL] = map[string]string{}
- next.FeedSignals[feedURL] = map[string]string{}
- var prevItems, prevSignals map[string]string
- if prev != nil {
- prevItems, prevSignals = prev.FeedItems[feedURL], prev.FeedSignals[feedURL]
- }
- for _, item := range feed.Items {
- if item == nil {
- continue
- }
- itemID := firstNonEmpty(item.GUID, item.Link, item.Title)
- if itemID == "" {
- continue
- }
- feedContent := firstNonEmpty(item.Content, item.Description)
- sig := feedSignal(item, feedContent)
- if incremental && prevItems[itemID] != "" && prevSignals[itemID] == sig {
- next.FeedItems[feedURL][itemID] = prevItems[itemID]
- next.FeedSignals[feedURL][itemID] = sig
- continue
- }
- fetched, fp := c.resolve(ctx, f, feed, item, feedURL, itemID, feedContent)
- next.FeedItems[feedURL][itemID] = fp
- next.FeedSignals[feedURL][itemID] = sig
- if incremental && prevItems[itemID] == fp {
- continue
- }
- if err := s.Item(fetched); err != nil {
- return nil, err
- }
- }
- // Each finished feed is a safe place to resume from.
- if err := s.Checkpoint(pluginapi.Cursor{LastSyncTime: &now, State: next.toState()}); err != nil {
- return nil, err
- }
- }
- if len(failures) > 0 && len(failures) == len(feedURLs) {
- return nil, pluginapi.Errorf(pluginapi.CodeUnavailable, "all feeds failed: %s", strings.Join(failures, "; "))
- }
- if len(failures) > 0 {
- _ = s.Log("warn", "some feeds failed: "+strings.Join(failures, "; "))
- }
- return &pluginapi.Cursor{LastSyncTime: &now, State: next.toState()}, nil
-}
-
-// resolve builds one item: the article's full text when it can be fetched,
-// the feed's own content otherwise, as Markdown.
-func (c *connector) resolve(
- ctx context.Context, f *fetcher, feed *gofeed.Feed, item *gofeed.Item, feedURL, itemID, feedContent string,
-) (pluginapi.FetchedItem, string) {
- title := firstNonEmpty(item.Title, "untitled")
- contentHTML := feedContent
- if strings.TrimSpace(item.Link) != "" {
- html, articleTitle, err := f.article(ctx, item.Link)
- if err == nil {
- contentHTML = html
- if item.Title == "" && articleTitle != "" {
- title = articleTitle
- }
- } else if !errors.Is(err, context.Canceled) {
- c.warn("full-text fetch failed, using feed content", "url", item.Link, "err", err)
- }
- }
- content := htmlToMarkdown(contentHTML)
- updated := time.Now().UTC()
- switch {
- case item.UpdatedParsed != nil && !item.UpdatedParsed.IsZero():
- updated = *item.UpdatedParsed
- case item.PublishedParsed != nil && !item.PublishedParsed.IsZero():
- updated = *item.PublishedParsed
- }
- author := ""
- if item.Author != nil {
- author = item.Author.Name
- }
- return pluginapi.FetchedItem{
- ExternalID: feedURL + ":" + itemID,
- Title: title,
- Content: []byte(content),
- ContentType: "text/markdown",
- FileName: fileName(title),
- URL: item.Link,
- UpdatedAt: &updated,
- SourceResourceID: feedURL,
- Metadata: map[string]string{
- "channel": "rss", "feed_url": feedURL, "feed_title": feed.Title,
- "guid": item.GUID, "link": item.Link, "author": author,
- },
- }, contentFingerprint(content)
-}
diff --git a/examples/plugins/rss/package.sh b/examples/plugins/rss/package.sh
deleted file mode 100755
index 914d51645..000000000
--- a/examples/plugins/rss/package.sh
+++ /dev/null
@@ -1,21 +0,0 @@
-#!/usr/bin/env bash
-# Builds the RSS example plugin for the platforms WeKnora runs on and zips it
-# into a .wkp package: plugin.yaml, schemas, icon and bin/<os>-<arch>/rss.
-set -euo pipefail
-cd "$(dirname "$0")"
-version=$(sed -n 's/^version: //p' plugin.yaml)
-out=${1:-"weknora-examples-rss-${version}.wkp"}
-stage=$(mktemp -d)
-trap 'rm -rf "$stage"' EXIT
-cp plugin.yaml icon.svg "$stage"/
-cp -r schemas "$stage"/
-for target in linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64; do
- os=${target%/*}
- arch=${target#*/}
- bin="$stage/bin/${os}-${arch}/rss"
- [ "$os" = windows ] && bin="$bin.exe"
- CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -trimpath -ldflags="-s -w" -o "$bin" .
-done
-rm -f "$out"
-(cd "$stage" && zip -qr - .) > "$out"
-echo "wrote $out"
diff --git a/examples/plugins/rss/parity_test.go b/examples/plugins/rss/parity_test.go
deleted file mode 100644
index d546d92ce..000000000
--- a/examples/plugins/rss/parity_test.go
+++ /dev/null
@@ -1,279 +0,0 @@
-package main
-
-import (
- "bytes"
- "context"
- "encoding/json"
- "fmt"
- "go/parser"
- "go/token"
- "net/http"
- "net/http/httptest"
- "os"
- "path/filepath"
- "sort"
- "strings"
- "sync"
- "testing"
- "time"
-
- builtin "github.com/Tencent/WeKnora/internal/datasource/connector/rss"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/utils"
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/conformance"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// feedSite serves two feeds and the article pages they link to. Changing an
-// entry's text simulates a feed update between syncs.
-type feedSite struct {
- *httptest.Server
- mu sync.Mutex
- secondA string
-}
-
-func newFeedSite(t *testing.T) *feedSite {
- s := &feedSite{secondA: "Second post, first version."}
- mux := http.NewServeMux()
- mux.HandleFunc("/a.xml", func(w http.ResponseWriter, r *http.Request) {
- s.mu.Lock()
- second := s.secondA
- s.mu.Unlock()
- w.Header().Set("Content-Type", "application/rss+xml")
- _, _ = fmt.Fprintf(w, `<?xml version="1.0"?><rss version="2.0"><channel><title>Feed A</title><link>%[1]s</link>
-<description>First feed</description>
-<item><title>Hello: world</title><link>%[1]s/articles/1</link><guid>a-1</guid>
-<pubDate>Mon, 02 Jan 2006 15:04:05 GMT</pubDate><description>Teaser one</description></item>
-<item><title>Second</title><guid>a-2</guid><pubDate>Tue, 03 Jan 2006 15:04:05 GMT</pubDate>
-<description><![CDATA[<p>%[2]s</p>]]></description></item>
-</channel></rss>`, "http://"+r.Host, second)
- })
- mux.HandleFunc("/b.atom", func(w http.ResponseWriter, _ *http.Request) {
- w.Header().Set("Content-Type", "application/atom+xml")
- _, _ = fmt.Fprintf(w, `<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom">
-<title>Feed B</title><id>urn:b</id><updated>2006-01-04T00:00:00Z</updated>
-<entry><title>Atom entry</title><id>urn:b:1</id><updated>2006-01-04T00:00:00Z</updated>
-<author><name>Ada</name></author><content type="html">&lt;h2&gt;Atom&lt;/h2&gt;&lt;p&gt;body&lt;/p&gt;</content></entry>
-</feed>`)
- })
- mux.HandleFunc("/articles/1", func(w http.ResponseWriter, _ *http.Request) {
- _, _ = fmt.Fprint(w, `<html><head><title>Article one</title></head><body><nav>menu</nav><article>
-<h1>Hello world</h1><p>The full text of the first article is long enough for a readability extractor to keep it.
-It carries several sentences, so the page is recognised as the main content and not as chrome.</p>
-<p>A second paragraph makes the article clearly the densest block on the page.</p></article></body></html>`)
- })
- s.Server = httptest.NewServer(mux)
- t.Cleanup(s.Close)
- return s
-}
-
-// syncBoth runs the builtin connector and the plugin over the same config.
-type harness struct {
- t *testing.T
- builtin *builtin.Connector
- plugin *client.Client
- cfg *types.DataSourceConfig
-}
-
-func newHarness(t *testing.T) (*harness, *feedSite) {
- t.Helper()
- site := newFeedSite(t)
- // The builtin connector fetches through the SSRF-safe client.
- t.Setenv("SSRF_WHITELIST", "127.0.0.1")
- utils.ResetSSRFWhitelistForTest()
- t.Cleanup(utils.ResetSSRFWhitelistForTest)
-
- p := pluginsdk.New(pluginsdk.Info{ID: "weknora-examples.rss", Version: Version})
- p.Connector("rss", &connector{})
- srv := httptest.NewServer(p.Handler())
- t.Cleanup(srv.Close)
- return &harness{
- t: t, builtin: builtin.NewConnector(), plugin: client.New(srv.URL, nil, nil),
- cfg: &types.DataSourceConfig{
- Type: types.ConnectorTypeRSS,
- Settings: map[string]any{"feed_urls": site.URL + "/a.xml\n" + site.URL + "/b.atom"},
- },
- }, site
-}
-
-func (h *harness) pluginFetch(mode string, cursor *types.SyncCursor) ([]types.FetchedItem, *types.SyncCursor) {
- h.t.Helper()
- instance := map[string]any{"settings": h.cfg.Settings, "credentials": map[string]any{}}
- in := pluginapi.FetchInput{Mode: mode}
- if cursor != nil {
- in.Cursor = &pluginapi.Cursor{State: cursor.ConnectorCursor}
- }
- var items []types.FetchedItem
- end, err := h.plugin.Stream(context.Background(), pluginapi.ConnectorFetchPath("rss"),
- pluginapi.Envelope{Config: pluginapi.Config{Instance: instance}}, in, func(ev pluginapi.Event) error {
- if ev.Type != pluginapi.EventItem {
- return nil
- }
- var it pluginapi.FetchedItem
- if err := json.Unmarshal(ev.Data, &it); err != nil {
- return err
- }
- items = append(items, types.FetchedItem{
- ExternalID: it.ExternalID, Title: it.Title, Content: it.Content, ContentType: it.ContentType,
- FileName: it.FileName, URL: it.URL, UpdatedAt: *it.UpdatedAt, Metadata: it.Metadata,
- SourceResourceID: it.SourceResourceID,
- })
- return nil
- })
- if err != nil {
- h.t.Fatalf("plugin fetch: %v", err)
- }
- var c pluginapi.Cursor
- if err := json.Unmarshal(end, &c); err != nil {
- h.t.Fatal(err)
- }
- return items, &types.SyncCursor{ConnectorCursor: c.State}
-}
-
-// comparable renders items in a stable form; UpdatedAt of an undated item is
-// "now" on both sides, so it is compared only when the feed dated the item.
-func comparable(items []types.FetchedItem) []string {
- var out []string
- for _, it := range items {
- meta, _ := json.Marshal(it.Metadata)
- updated := it.UpdatedAt.UTC().Format(time.RFC3339)
- if time.Since(it.UpdatedAt) < time.Hour {
- updated = "now"
- }
- out = append(out, strings.Join([]string{
- it.ExternalID, it.Title, it.ContentType, it.FileName, it.URL, updated, it.SourceResourceID,
- string(meta), string(it.Content),
- }, " | "))
- }
- sort.Strings(out)
- return out
-}
-
-func sameItems(t *testing.T, label string, want, got []types.FetchedItem) {
- t.Helper()
- w, g := comparable(want), comparable(got)
- if strings.Join(w, "\n") != strings.Join(g, "\n") {
- t.Fatalf("%s differ\nbuiltin:\n%s\n\nplugin:\n%s", label, strings.Join(w, "\n"), strings.Join(g, "\n"))
- }
-}
-
-func TestPluginMatchesBuiltinConnector(t *testing.T) {
- ctx := context.Background()
- h, site := newHarness(t)
-
- want, err := h.builtin.FetchAll(ctx, h.cfg, nil)
- if err != nil {
- t.Fatal(err)
- }
- got, pluginCursor := h.pluginFetch(pluginapi.FetchFull, nil)
- if len(want) != 3 {
- t.Fatalf("builtin fetched %d items, want 3", len(want))
- }
- sameItems(t, "full sync", want, got)
- if !strings.Contains(
- string(got[0].Content)+string(got[1].Content)+string(got[2].Content),
- "full text of the first article",
- ) {
- t.Fatal("the article page should replace the feed teaser")
- }
-
- // An incremental run with nothing changed emits nothing on either side.
- _, builtinCursor, err := h.builtin.FetchIncremental(ctx, h.cfg, nil)
- if err != nil {
- t.Fatal(err)
- }
- unchanged, _, err := h.builtin.FetchIncremental(ctx, h.cfg, builtinCursor)
- if err != nil || len(unchanged) != 0 {
- t.Fatalf("builtin incremental without changes = %d items, %v", len(unchanged), err)
- }
- if again, _ := h.pluginFetch(pluginapi.FetchIncremental, pluginCursor); len(again) != 0 {
- t.Fatalf("plugin incremental without changes = %d items", len(again))
- }
-
- // One entry changes: both emit exactly that entry.
- site.mu.Lock()
- site.secondA = "Second post, edited."
- site.mu.Unlock()
- changedWant, _, err := h.builtin.FetchIncremental(ctx, h.cfg, builtinCursor)
- if err != nil {
- t.Fatal(err)
- }
- changedGot, _ := h.pluginFetch(pluginapi.FetchIncremental, pluginCursor)
- if len(changedWant) != 1 {
- t.Fatalf("builtin incremental after an edit = %d items", len(changedWant))
- }
- sameItems(t, "incremental sync", changedWant, changedGot)
-}
-
-func TestPluginValidatesLikeTheBuiltin(t *testing.T) {
- h, site := newHarness(t)
- ctx := context.Background()
- call := func(settings map[string]any) error {
- return h.plugin.Call(ctx, pluginapi.ConnectorValidatePath("rss"), pluginapi.Envelope{Config: pluginapi.Config{
- Instance: map[string]any{"settings": settings, "credentials": map[string]any{}},
- }}, nil, nil)
- }
- if err := call(h.cfg.Settings); err != nil {
- t.Fatalf("valid feeds rejected: %v", err)
- }
- for name, settings := range map[string]map[string]any{
- "no feeds": {},
- "missing feed": {"feed_urls": site.URL + "/nope.xml"},
- } {
- err := call(settings)
- e, ok := pluginapi.AsError(err)
- if !ok || e.Code != pluginapi.CodeInvalidConfig {
- t.Errorf("%s: want invalid_config, got %v", name, err)
- }
- cfg := &types.DataSourceConfig{Settings: settings}
- if h.builtin.Validate(ctx, cfg) == nil {
- t.Errorf("%s: the builtin accepts what the plugin rejects", name)
- }
- }
-}
-
-// The example must stay an external plugin: the SDK and ordinary libraries,
-// nothing from WeKnora's internals.
-func TestPluginImportsNoInternals(t *testing.T) {
- files, _ := filepath.Glob("*.go")
- fset := token.NewFileSet()
- for _, f := range files {
- if strings.HasSuffix(f, "_test.go") {
- continue
- }
- src, err := os.ReadFile(f)
- if err != nil {
- t.Fatal(err)
- }
- parsed, err := parser.ParseFile(fset, f, src, parser.ImportsOnly)
- if err != nil {
- t.Fatal(err)
- }
- for _, imp := range parsed.Imports {
- if strings.Contains(imp.Path.Value, "github.com/Tencent/WeKnora/internal") {
- t.Errorf("%s imports %s", f, imp.Path.Value)
- }
- }
- }
-}
-
-func TestPluginPassesConformance(t *testing.T) {
- p := pluginsdk.New(pluginsdk.Info{ID: "weknora-examples.rss", Version: Version})
- p.Connector("rss", &connector{})
- srv := httptest.NewServer(p.Handler())
- defer srv.Close()
- rep := conformance.Run(context.Background(), conformance.Target{
- Client: client.New(srv.URL, nil, nil),
- Raw: func(ctx context.Context, path string, body []byte) (*http.Response, error) {
- req, _ := http.NewRequestWithContext(ctx, http.MethodPost, srv.URL+path, bytes.NewReader(body))
- return http.DefaultClient.Do(req)
- },
- })
- for _, r := range rep.Results {
- if !r.Passed {
- t.Errorf("%s: %s", r.Name, r.Detail)
- }
- }
-}
diff --git a/examples/plugins/rss/plugin.yaml b/examples/plugins/rss/plugin.yaml
deleted file mode 100644
index 39991cf08..000000000
--- a/examples/plugins/rss/plugin.yaml
+++ /dev/null
@@ -1,26 +0,0 @@
-schemaVersion: 1
-id: weknora-examples.rss
-version: 1.0.0
-apiVersion: weknora.plugin/v1
-name: { en-US: RSS (plugin), zh-CN: RSS 订阅(插件版) }
-description:
- en-US: Sync RSS, Atom and JSON feeds into a knowledge base. The builtin RSS connector, rebuilt as a code plugin.
- zh-CN: 把 RSS、Atom、JSON Feed 同步到知识库。内置 RSS 连接器的插件实现。
-publisher: { id: weknora-examples, name: WeKnora examples }
-homepage: https://github.com/Tencent/WeKnora/tree/main/examples/plugins/rss
-license: MIT
-icon: icon.svg
-runtime: { type: host, kind: binary, entry: "bin/{os}-{arch}/rss" }
-permissions:
- # Feed items link to pages anywhere; private addresses stay unreachable.
- egress: ["*"]
-contributes:
- connectors:
- - id: rss
- name: { en-US: RSS (plugin), zh-CN: RSS 订阅(插件版) }
- description:
- en-US: Articles from RSS, Atom and JSON feeds, as full text
- zh-CN: 抓取 RSS、Atom、JSON Feed 的文章全文
- icon: icon.svg
- capabilities: [incremental, streaming]
- instanceSchema: schemas/instance.yaml
diff --git a/examples/plugins/rss/schemas/instance.yaml b/examples/plugins/rss/schemas/instance.yaml
deleted file mode 100644
index e1675ceca..000000000
--- a/examples/plugins/rss/schemas/instance.yaml
+++ /dev/null
@@ -1,22 +0,0 @@
-type: object
-required: [feed_urls]
-properties:
- feed_urls:
- type: string
- title: Feed URLs
- description: One feed per line
- x-widget: textarea
- x-group: settings
- x-placeholder: https://example.com/feed.xml
- x-i18n:
- title: { zh-CN: 订阅地址 }
- description: { zh-CN: 每行一个 }
- auth_headers:
- type: string
- title: Request headers
- description: "Optional, one per line as Name: Value; sent to feed URLs only"
- x-secret: true
- x-widget: textarea
- x-i18n:
- title: { zh-CN: 请求头 }
- description: { zh-CN: "可选,每行一个,格式为 名称: 值;只发给订阅地址" }
diff --git a/examples/plugins/subtitles/main.go b/examples/plugins/subtitles/main.go
deleted file mode 100644
index bce45fbc2..000000000
--- a/examples/plugins/subtitles/main.go
+++ /dev/null
@@ -1,130 +0,0 @@
-// Command subtitles is an example parser plugin: it turns SRT and WebVTT
-// subtitle files into a Markdown transcript, one paragraph per cue with its
-// start time. It also keeps a per-workspace count of parsed files through
-// the Host API key-value store, to show how a plugin keeps state without a
-// database.
-//
-// Build a package with ./package.sh.
-package main
-
-import (
- "bufio"
- "bytes"
- "context"
- "fmt"
- "log"
- "regexp"
- "strings"
- "time"
-
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// Version must match plugin.yaml.
-const Version = "1.0.0"
-
-func main() {
- p := pluginsdk.New(pluginsdk.Info{ID: "weknora-examples.subtitles", Version: Version})
- p.Parser("subtitles", pluginsdk.ParserFunc(parse))
- if err := p.Serve(); err != nil {
- log.Fatal(err)
- }
-}
-
-// timing matches a cue timing line: 00:01:02,500 --> 00:01:04,000 (SRT) or
-// 01:02.500 --> 01:04.000 (WebVTT, hours optional).
-var timing = regexp.MustCompile(`^\s*((?:\d+:)?\d{2}:\d{2}[.,]\d{3})\s*-->\s*`)
-
-// tags strips WebVTT / SRT inline markup such as <i> or <v Speaker>.
-var tags = regexp.MustCompile(`<[^>]*>`)
-
-type cue struct {
- start string
- text []string
-}
-
-func parse(ctx context.Context, call *pluginsdk.Call, in pluginapi.ParseInput) (*pluginapi.ParseOutput, error) {
- if len(in.Content) == 0 {
- return nil, pluginapi.Errorf(pluginapi.CodeInvalidConfig, "the subtitle file is empty")
- }
- cues := parseCues(in.Content)
- if len(cues) == 0 {
- return nil, pluginapi.Errorf(
- pluginapi.CodeInvalidConfig,
- "no subtitle cues found; is this an .srt or .vtt file?",
- )
- }
- title := strings.TrimSpace(in.Title)
- if title == "" {
- title = strings.TrimSuffix(in.FileName, "."+in.FileType)
- }
- var md strings.Builder
- fmt.Fprintf(&md, "# %s\n\n", title)
- for _, c := range cues {
- fmt.Fprintf(&md, "**[%s]** %s\n\n", shortTime(c.start), strings.Join(c.text, " "))
- }
- countParsed(ctx, call)
- return &pluginapi.ParseOutput{
- Markdown: strings.TrimSpace(md.String()) + "\n",
- Metadata: map[string]string{"cues": fmt.Sprint(len(cues))},
- }, nil
-}
-
-func parseCues(data []byte) []cue {
- data = bytes.TrimPrefix(data, []byte("\xef\xbb\xbf"))
- var out []cue
- var cur *cue
- sc := bufio.NewScanner(bytes.NewReader(data))
- sc.Buffer(make([]byte, 0, 64<<10), 1<<20)
- for sc.Scan() {
- line := strings.TrimRight(sc.Text(), "\r")
- if m := timing.FindStringSubmatch(line); m != nil {
- out = append(out, cue{start: m[1]})
- cur = &out[len(out)-1]
- continue
- }
- if strings.TrimSpace(line) == "" {
- cur = nil
- continue
- }
- if cur == nil {
- continue // cue numbers, WEBVTT header, NOTE blocks
- }
- if text := strings.TrimSpace(tags.ReplaceAllString(line, "")); text != "" {
- cur.text = append(cur.text, text)
- }
- }
- kept := out[:0]
- for _, c := range out {
- if len(c.text) > 0 {
- kept = append(kept, c)
- }
- }
- return kept
-}
-
-// shortTime drops milliseconds and a zero hour: 00:01:02,500 → 01:02.
-func shortTime(t string) string {
- t = strings.NewReplacer(",", ".").Replace(t)
- if i := strings.IndexByte(t, '.'); i >= 0 {
- t = t[:i]
- }
- return strings.TrimPrefix(t, "00:")
-}
-
-// countParsed bumps the workspace's parsed-file counter. It is best effort:
-// a missing grant or an unreachable host must not fail the parse.
-func countParsed(ctx context.Context, call *pluginsdk.Call) {
- host := call.Host()
- if host == nil {
- return
- }
- ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
- defer cancel()
- var n int
- if _, err := host.KVGet(ctx, "stats/parsed", &n); err != nil {
- return
- }
- _ = host.KVPut(ctx, "stats/parsed", n+1, 0)
-}
diff --git a/examples/plugins/subtitles/main_test.go b/examples/plugins/subtitles/main_test.go
deleted file mode 100644
index 66ee0f3c9..000000000
--- a/examples/plugins/subtitles/main_test.go
+++ /dev/null
@@ -1,146 +0,0 @@
-package main
-
-import (
- "bytes"
- "context"
- "go/parser"
- "go/token"
- "net/http"
- "net/http/httptest"
- "os"
- "path/filepath"
- "strings"
- "testing"
-
- "github.com/gin-gonic/gin"
- "github.com/google/uuid"
- "gorm.io/driver/sqlite"
- "gorm.io/gorm"
-
- "github.com/Tencent/WeKnora/internal/application/repository"
- "github.com/Tencent/WeKnora/internal/plugin/hostapi"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/conformance"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-const srt = "\ufeff1\r\n00:00:01,000 --> 00:00:03,500\r\nHello <i>world</i>.\r\n\r\n" +
- "2\r\n00:01:02,250 --> 00:01:04,000\r\nSecond line\r\ncontinues here.\r\n"
-
-const vtt = `WEBVTT
-
-NOTE a comment
-
-01:02.500 --> 01:04.000
-<v Ada>Plugins run out of process.
-
-1:00:00.000 --> 1:00:02.000 align:start
-Bye.
-`
-
-func TestParseSRTAndVTT(t *testing.T) {
- ctx := context.Background()
- out, err := parse(
- ctx,
- &pluginsdk.Call{},
- pluginapi.ParseInput{FileName: "talk.srt", FileType: "srt", Content: []byte(srt)},
- )
- if err != nil {
- t.Fatal(err)
- }
- want := "# talk\n\n**[00:01]** Hello world.\n\n**[01:02]** Second line continues here.\n"
- if out.Markdown != want || out.Metadata["cues"] != "2" {
- t.Fatalf("srt markdown = %q", out.Markdown)
- }
- out, err = parse(
- ctx,
- &pluginsdk.Call{},
- pluginapi.ParseInput{FileName: "t.vtt", FileType: "vtt", Title: "Talk", Content: []byte(vtt)},
- )
- if err != nil {
- t.Fatal(err)
- }
- if !strings.Contains(out.Markdown, "# Talk") ||
- !strings.Contains(out.Markdown, "**[01:02]** Plugins run out of process.") ||
- !strings.Contains(out.Markdown, "**[1:00:00]** Bye.") {
- t.Fatalf("vtt markdown = %q", out.Markdown)
- }
- for _, bad := range []string{"", "just some text\nwith no cues\n"} {
- _, err := parse(
- ctx,
- &pluginsdk.Call{},
- pluginapi.ParseInput{FileName: "x.srt", FileType: "srt", Content: []byte(bad)},
- )
- if e, ok := pluginapi.AsError(err); !ok || e.Code != pluginapi.CodeInvalidConfig || e.Retryable {
- t.Errorf("%q: want a permanent invalid_config error, got %v", bad, err)
- }
- }
-}
-
-func TestParseCountsThroughTheHostAPI(t *testing.T) {
- gin.SetMode(gin.TestMode)
- db, err := gorm.Open(sqlite.Open("file:"+uuid.NewString()+"?mode=memory&cache=shared"), &gorm.Config{})
- if err != nil {
- t.Fatal(err)
- }
- if err := db.AutoMigrate(&types.PluginKV{}); err != nil {
- t.Fatal(err)
- }
- iss := hostapi.NewIssuer([]byte("k"))
- r := gin.New()
- hostapi.NewHandler(iss, hostapi.NewKV(repository.NewPluginKVRepository(db))).Register(r)
- srv := httptest.NewServer(r)
- defer srv.Close()
-
- tok, _, _ := iss.Issue("weknora-examples.subtitles", Version, 3, []string{"kv"})
- call := &pluginsdk.Call{
- Context: pluginapi.Context{TenantID: 3, Host: &pluginapi.HostAccess{URL: srv.URL, Token: tok}},
- }
- in := pluginapi.ParseInput{FileName: "a.srt", FileType: "srt", Content: []byte(srt)}
- for range 2 {
- if _, err := parse(context.Background(), call, in); err != nil {
- t.Fatal(err)
- }
- }
- var n int
- if ok, err := call.Host().KVGet(context.Background(), "stats/parsed", &n); !ok || err != nil || n != 2 {
- t.Fatalf("parsed count = %d (%v, %v)", n, ok, err)
- }
-}
-
-func TestConformsAndStaysExternal(t *testing.T) {
- p := pluginsdk.New(pluginsdk.Info{ID: "weknora-examples.subtitles", Version: Version})
- p.Parser("subtitles", pluginsdk.ParserFunc(parse))
- srv := httptest.NewServer(p.Handler())
- defer srv.Close()
- rep := conformance.Run(context.Background(), conformance.Target{
- Client: client.New(srv.URL, nil, nil),
- Raw: func(ctx context.Context, path string, body []byte) (*http.Response, error) {
- req, _ := http.NewRequestWithContext(ctx, http.MethodPost, srv.URL+path, bytes.NewReader(body))
- return http.DefaultClient.Do(req)
- },
- })
- for _, res := range rep.Results {
- if !res.Passed {
- t.Errorf("%s: %s", res.Name, res.Detail)
- }
- }
- files, _ := filepath.Glob("*.go")
- for _, f := range files {
- if strings.HasSuffix(f, "_test.go") {
- continue
- }
- src, _ := os.ReadFile(f)
- parsed, err := parser.ParseFile(token.NewFileSet(), f, src, parser.ImportsOnly)
- if err != nil {
- t.Fatal(err)
- }
- for _, imp := range parsed.Imports {
- if strings.Contains(imp.Path.Value, "WeKnora/internal") {
- t.Errorf("%s imports %s", f, imp.Path.Value)
- }
- }
- }
-}
diff --git a/examples/plugins/subtitles/package.sh b/examples/plugins/subtitles/package.sh
deleted file mode 100755
index 09f3d236e..000000000
--- a/examples/plugins/subtitles/package.sh
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/usr/bin/env bash
-# Builds the subtitles example plugin for the platforms WeKnora runs on and zips it
-# into a .wkp package: plugin.yaml and bin/<os>-<arch>/subtitles.
-set -euo pipefail
-cd "$(dirname "$0")"
-version=$(sed -n 's/^version: //p' plugin.yaml)
-out=${1:-"weknora-examples-subtitles-${version}.wkp"}
-stage=$(mktemp -d)
-trap 'rm -rf "$stage"' EXIT
-cp plugin.yaml "$stage"/
-for target in linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64; do
- os=${target%/*}
- arch=${target#*/}
- bin="$stage/bin/${os}-${arch}/subtitles"
- [ "$os" = windows ] && bin="$bin.exe"
- CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -trimpath -ldflags="-s -w" -o "$bin" .
-done
-rm -f "$out"
-(cd "$stage" && zip -qr - .) > "$out"
-echo "wrote $out"
diff --git a/examples/plugins/subtitles/plugin.yaml b/examples/plugins/subtitles/plugin.yaml
deleted file mode 100644
index 86252e3fc..000000000
--- a/examples/plugins/subtitles/plugin.yaml
+++ /dev/null
@@ -1,23 +0,0 @@
-schemaVersion: 1
-id: weknora-examples.subtitles
-version: 1.0.0
-apiVersion: weknora.plugin/v1
-name: { en-US: Subtitles, zh-CN: 字幕解析 }
-description:
- en-US: Parse SRT and WebVTT subtitles into a searchable transcript.
- zh-CN: 把 SRT、WebVTT 字幕解析为可检索的文字稿。
-publisher: { id: weknora-examples, name: WeKnora examples }
-homepage: https://github.com/Tencent/WeKnora/tree/main/examples/plugins/subtitles
-license: MIT
-runtime: { type: host, kind: binary, entry: "bin/{os}-{arch}/subtitles" }
-permissions:
- # Counts parsed files per workspace in the Host API key-value store.
- hostApi: [kv]
-contributes:
- parsers:
- - id: subtitles
- name: { en-US: Subtitles, zh-CN: 字幕解析 }
- description:
- en-US: SRT and WebVTT subtitles as a transcript with cue times
- zh-CN: 带时间点的字幕文字稿
- fileTypes: [srt, vtt]
diff --git a/frontend/src/api/agent/index.ts b/frontend/src/api/agent/index.ts
index 35b8ea595..457e1316f 100644
--- a/frontend/src/api/agent/index.ts
+++ b/frontend/src/api/agent/index.ts
@@ -1,4 +1,3 @@
-import type { ConfigSchema } from '@/components/schema-form/schema'
import { get, post, put, del } from "../../utils/request";
// 智能体配置
@@ -348,35 +347,6 @@ export interface IMChannelOverview {
updated_at: string;
}
-// IMPlatformInfo describes one IM platform (internal/im.PlatformInfo): its
-// connection modes, console links and the credential form.
-export interface IMPlatformInfo {
- id: string
- name: string
- names?: Record<string, string>
- order: number
- /** Supported connection modes, default first. */
- modes: Array<IMChannel['mode']>
- mode_labels?: Record<string, string>
- mode_hint_key?: string
- supports_thread: boolean
- links?: Array<{ url: string; title: string; title_key?: string }>
- config_schema?: ConfigSchema
-}
-
-/**
- * One channel for the editor, credentials included: plain fields as stored,
- * set secrets as "***". Send them back unchanged to keep the stored secret.
- */
-export function getIMChannel(id: string) {
- return get<{ data: IMChannel }>(`/api/v1/im-channels/${id}`);
-}
-
-/** The IM platforms this deployment has adapters for, in display order. */
-export function listIMPlatforms() {
- return get<{ data: IMPlatformInfo[] }>('/api/v1/im-channels/platforms');
-}
-
export function listAllIMChannels() {
return get<{ data: IMChannelOverview[] }>('/api/v1/im-channels');
}
diff --git a/frontend/src/api/datasource/index.ts b/frontend/src/api/datasource/index.ts
index e3f867920..19096307f 100644
--- a/frontend/src/api/datasource/index.ts
+++ b/frontend/src/api/datasource/index.ts
@@ -1,4 +1,3 @@
-import type { ConfigSchema } from '@/components/schema-form/schema'
import { get, post, put, del } from '../../utils/request'
// --- Types ---
@@ -73,20 +72,6 @@ export interface ConnectorMeta {
priority: number
auth_type: string
capabilities: string[]
- // Setup guide next to the credential form.
- doc_url?: string
- permission_doc_url?: string
- permission_page_url?: string
- required_permissions?: string[]
- // Credential form: describes config.credentials for this connector.
- config_schema?: ConfigSchema
- // Settings form (config.settings) for connectors whose settings UI is
- // not built in, such as plugin connectors.
- settings_schema?: ConfigSchema
- // Localized name / description of plugin connectors, keyed by locale.
- names?: Record<string, string>
- descriptions?: Record<string, string>
- plugin_id?: string
}
export interface Resource {
@@ -103,9 +88,8 @@ export interface Resource {
// --- API calls ---
-/** Connectors this deployment can create, with their credential forms. */
export function getConnectorTypes() {
- return get<ConnectorMeta[]>('/api/v1/datasource/types')
+ return get('/api/v1/datasource/types')
}
export function listDataSources(kbId: string) {
@@ -133,12 +117,8 @@ export function validateConnection(id: string) {
}
// Validate credentials without persisting (during creation or credential replacement).
-export function validateCredentials(
- type: string,
- credentials: Record<string, any>,
- settings?: Record<string, any>,
-) {
- return post('/api/v1/datasource/validate-credentials', { type, credentials, settings })
+export function validateCredentials(type: string, credentials: Record<string, any>) {
+ return post('/api/v1/datasource/validate-credentials', { type, credentials })
}
// listResources lists selectable resources for a data source. Pass parentId to
diff --git a/frontend/src/api/initialization/index.ts b/frontend/src/api/initialization/index.ts
index fb9748357..9f020cd83 100644
--- a/frontend/src/api/initialization/index.ts
+++ b/frontend/src/api/initialization/index.ts
@@ -1,4 +1,3 @@
-import type { ConfigSchema } from '@/components/schema-form/schema';
import { get, post, put } from '../../utils/request';
import i18n from '@/i18n'
import type { ModelCapabilities, ModelSpecOverride, ReasoningEffortLevel } from '../model'
@@ -662,8 +661,6 @@ export interface ModelProviderOption {
defaultUrls: Record<string, string>; // 按模型类型区分的默认 URL
modelTypes: string[]; // 支持的模型类型
extraFields?: ModelProviderExtraField[];
- // extraFields as a config schema over extra_config (secrets included).
- configSchema?: ConfigSchema;
credentialLabels?: ModelProviderCredentialLabel[];
models?: ModelCatalogEntry[];
thinking?: ModelProviderThinking;
diff --git a/frontend/src/api/mcp-service.ts b/frontend/src/api/mcp-service.ts
index f806026d9..106446d86 100644
--- a/frontend/src/api/mcp-service.ts
+++ b/frontend/src/api/mcp-service.ts
@@ -7,7 +7,7 @@ export interface MCPService {
description: string
usage_instructions?: string
enabled: boolean
- transport_type: 'sse' | 'http-streamable' | 'stdio' | 'plugin'
+ transport_type: 'sse' | 'http-streamable' | 'stdio'
url?: string // Optional: required for SSE/HTTP Streamable
headers?: Record<string, string>
auth_config?: {
@@ -41,10 +41,6 @@ export interface MCPService {
}
env_vars?: Record<string, string> // Environment variables for stdio transport
is_builtin?: boolean // Whether this is a builtin MCP service
- /** Installed plugin providing the service (read-only, never stored). */
- plugin_id?: string
- /** Why a plugin service is off, typically missing workspace configuration. */
- plugin_error?: string
// Per-field "configured?" map embedded on the main response (server-side
// dto.MCPServiceResponse.Credentials). Drives the CredentialResource card
// without a follow-up GET. Absent for builtin services.
diff --git a/frontend/src/api/plugin.ts b/frontend/src/api/plugin.ts
deleted file mode 100644
index 00b28a710..000000000
--- a/frontend/src/api/plugin.ts
+++ /dev/null
@@ -1,196 +0,0 @@
-import { get, post, put } from '@/utils/request'
-
-import type { ConfigSchema, ConfigValue } from '@/components/schema-form/schema'
-import type { LocalizedText } from '@/utils/localizedText'
-
-export type { LocalizedText } from '@/utils/localizedText'
-
-export type ExtensionPoint =
- | 'modelVendors'
- | 'connectors'
- | 'imChannels'
- | 'webSearch'
- | 'tools'
- | 'parsers'
- | 'skills'
- | 'mcpServers'
- | 'pages'
- | 'settingsSections'
- | 'kbTabs'
- | 'webhooks'
-
-export interface PluginContribution {
- id: string
- name: LocalizedText
- description?: LocalizedText
- icon?: string
- aliases?: string[]
- capabilities?: string[]
- order?: number
- /** Package path of a skill directory or model vendor file. */
- path?: string
- /** Remote MCP server an installed plugin contributes. */
- mcp?: { url: string; transport?: string; headers?: Record<string, string> }
- /** The HTML page of a UI contribution (pages, settingsSections, kbTabs), under ui/. */
- entry?: string
- /** Workspace role a UI contribution needs. */
- minRole?: 'viewer' | 'contributor' | 'admin' | 'owner'
-}
-
-/** A contribution as GET /plugins/contributions lists it. */
-export interface ListedContribution extends PluginContribution {
- pluginId: string
- qualifiedId: string
- version?: string
- enabled: boolean
-}
-
-export interface ContributionListing {
- points: Array<{ point: ExtensionPoint; thirdParty: boolean; declarative: boolean }>
- contributions: Partial<Record<ExtensionPoint, ListedContribution[]>>
-}
-
-export interface PluginPermissions {
- egress?: string[]
- hostApi?: string[]
- events?: string[]
-}
-
-/** A plugin's manifest (internal/plugin/manifest.Manifest). */
-export interface PluginManifest {
- schemaVersion: number
- id: string
- version: string
- name: LocalizedText
- description?: LocalizedText
- publisher: { id: string; name?: string; url?: string }
- icon?: string
- homepage?: string
- license?: string
- builtin?: boolean
- required?: boolean
- engines?: { weknora?: string }
- runtime: { type: string }
- permissions?: PluginPermissions
- config?: {
- system?: string
- tenant?: string
- systemSchema?: ConfigSchema
- tenantSchema?: ConfigSchema
- }
- contributes: Partial<Record<ExtensionPoint, PluginContribution[]>>
-}
-
-/** A plugin as the current workspace sees it. */
-export interface TenantPlugin {
- manifest: PluginManifest
- enabled: boolean
- updatedAt?: string
-}
-
-/** One running instance of a plugin, for the detail view. */
-export interface PluginInstance {
- node: string
- version: string
- state: 'starting' | 'ready' | 'degraded' | 'stopped'
- error?: string
- updatedAt: string
-}
-
-/** A plugin configuration: its schema and values, secrets redacted. */
-export interface PluginConfig {
- schema: ConfigSchema
- values: ConfigValue
- updatedAt?: string
-}
-
-export function listPlugins() {
- return get<{ data: TenantPlugin[] }>('/api/v1/plugins')
-}
-
-export function getPlugin(id: string) {
- return get<{ data: TenantPlugin & { instances: PluginInstance[]; instanceError?: string } }>(
- `/api/v1/plugins/${encodeURIComponent(id)}`,
- )
-}
-
-/** Turns a plugin on or off for the current workspace (Admin+). */
-export function setPluginEnabled(id: string, enabled: boolean) {
- return put<{ data: TenantPlugin }>(`/api/v1/plugins/${encodeURIComponent(id)}/enabled`, { enabled })
-}
-
-/** Every contribution the deployment has, with the workspace's switches. */
-export function listContributions(point?: ExtensionPoint) {
- return get<{ data: ContributionListing }>('/api/v1/plugins/contributions', point ? { params: { point } } : undefined)
-}
-
-/** What a plugin page's request returned. */
-export interface PluginPageResponse {
- status: number
- body?: unknown
-}
-
-/** Relays a request from a plugin page to the plugin's backend. */
-export function pluginPageRequest(
- pluginId: string,
- req: { mount: string; method: string; path: string; body?: unknown },
-) {
- return post<{ data: PluginPageResponse }>(`/api/v1/plugins/${encodeURIComponent(pluginId)}/ui-request`, req)
-}
-
-/** A webhook of a plugin with this workspace's URL (Admin+: the URL is a secret). */
-export interface PluginWebhook {
- id: string
- name: LocalizedText
- description?: LocalizedText
- /** Path on this deployment; `url` is the full URL when the server knows its public address. */
- path: string
- url?: string
-}
-
-export function listPluginWebhooks(id: string) {
- return get<{ data: PluginWebhook[] }>(`/api/v1/plugins/${encodeURIComponent(id)}/webhooks`)
-}
-
-/** The workspace's configuration of a plugin (Admin+). */
-export function getPluginConfig(id: string) {
- return get<{ data: PluginConfig }>(`/api/v1/plugins/${encodeURIComponent(id)}/config`)
-}
-
-/** Saves the workspace's configuration; "***" keeps a stored secret (Admin+). */
-export function updatePluginConfig(id: string, values: ConfigValue) {
- return put<{ data: PluginConfig }>(`/api/v1/plugins/${encodeURIComponent(id)}/config`, { values })
-}
-
-/** Which configuration a plugin form edits. */
-export type PluginFormScope = 'system' | 'tenant' | 'instance'
-
-export interface PluginOptionsRequest {
- name: string
- field: string
- scope: PluginFormScope
- /** "<point>/<id>" of the instance form ("connectors/jira"). */
- contribution?: string
- /** The instance being edited; its stored secrets fill in redacted ones. */
- instanceId?: string
- /** What the form holds now, in the scope's shape. */
- values: ConfigValue
- query?: string
-}
-
-/** A field's choices from the plugin (x-options). */
-export function pluginFormOptions(pluginId: string, req: PluginOptionsRequest) {
- return post<{ data: { options: Array<{ value: string; label?: string; description?: string }> } }>(
- `/api/v1/plugins/${encodeURIComponent(pluginId)}/options`, req,
- )
-}
-
-/** Starts connecting an x-oauth field; the tokens stay on the server. */
-export function startPluginOAuth(
- pluginId: string,
- req: { scope: PluginFormScope; contribution?: string; field: string },
-) {
- return post<{ data: { authorizeUrl: string; state: string; redirectUri: string } }>(
- `/api/v1/plugins/${encodeURIComponent(pluginId)}/oauth/start`, req,
- )
-}
diff --git a/frontend/src/api/system/index.ts b/frontend/src/api/system/index.ts
index 978564371..fa717354b 100644
--- a/frontend/src/api/system/index.ts
+++ b/frontend/src/api/system/index.ts
@@ -107,10 +107,6 @@ export interface ParserEngineInfo {
FileTypes: string[]
Available?: boolean
UnavailableReason?: string
- /** Set on engines an installed plugin provides (Name is then acme.ocr/ocr). */
- PluginID?: string
- /** Localized names of plugin engines, keyed by locale; "default" as fallback. */
- DisplayNames?: Record<string, string>
}
/** 解析引擎配置(引擎连接参数存空间;聊天附件解析策略在智能体中配置) */
diff --git a/frontend/src/api/system/plugins.ts b/frontend/src/api/system/plugins.ts
deleted file mode 100644
index b7f99aa77..000000000
--- a/frontend/src/api/system/plugins.ts
+++ /dev/null
@@ -1,133 +0,0 @@
-// System-admin plugin installation (/api/v1/system/admin/plugins).
-import { del, get, post, postUpload, put } from '@/utils/request'
-
-import type { ConfigValue } from '@/components/schema-form/schema'
-import type { PluginConfig, PluginManifest } from '@/api/plugin'
-
-const BASE = '/api/v1/system/admin/plugins'
-const PACKAGE_TIMEOUT = 5 * 60 * 1000
-
-/** What installing a package would do. */
-export type InstallChange = 'install' | 'upgrade' | 'downgrade' | 'reinstall'
-
-export interface PluginPreview {
- manifest: PluginManifest
- digest: string
- size: number
- change: InstallChange
- installedVersion?: string
-}
-
-export interface PluginVersion {
- plugin_id: string
- version: string
- digest: string
- manifest: PluginManifest
- size: number
- created_by: string
- created_at: string
-}
-
-/** This node's report on an installed plugin. */
-export interface PluginNodeStatus {
- version: string
- // degraded: loaded, but its process crashed or fails health checks and is
- // being restarted.
- state: 'ready' | 'failed' | 'degraded'
- error?: string
- updatedAt: string
-}
-
-export interface InstalledPlugin {
- id: string
- source: { kind: 'upload' | 'url'; url?: string }
- active_version: string
- desired_state: 'enabled' | 'disabled'
- runtime: string
- granted_perms: PluginManifest['permissions']
- created_by: string
- created_at: string
- updated_at: string
- manifest?: PluginManifest
- versions: PluginVersion[]
- node?: PluginNodeStatus
- /** Where a remote plugin's service runs. */
- remote_url?: string
- /** A remote plugin's new signing secret, only in the response that issued it. */
- issuedSecret?: string
-}
-
-/** A package to inspect or install: an uploaded file or a URL. */
-export type PackageSource = { file: File } | { url: string }
-
-function packageForm(file: File, digest?: string, remoteUrl?: string) {
- const form = new FormData()
- form.append('file', file)
- if (digest) form.append('digest', digest)
- if (remoteUrl) form.append('remote_url', remoteUrl)
- return form
-}
-
-export function listInstalledPlugins() {
- return get<{ data: InstalledPlugin[] }>(BASE)
-}
-
-export function getInstalledPlugin(id: string) {
- return get<{ data: InstalledPlugin }>(`${BASE}/${encodeURIComponent(id)}`)
-}
-
-/** Reads a package and reports what installing it would do, changing nothing. */
-export function inspectPluginPackage(source: PackageSource) {
- if ('file' in source) {
- return postUpload(`${BASE}/inspect`, packageForm(source.file), undefined, { timeout: PACKAGE_TIMEOUT }) as Promise<{
- data: PluginPreview
- }>
- }
- return post<{ data: PluginPreview }>(`${BASE}/inspect`, { url: source.url }, { timeout: PACKAGE_TIMEOUT })
-}
-
-/**
- * Installs the package reviewed with inspect; digest pins it to that package.
- * A remote plugin also needs the URL of its service (optional on upgrades).
- */
-export function installPluginPackage(source: PackageSource, digest: string, remoteUrl?: string) {
- if ('file' in source) {
- return postUpload(BASE, packageForm(source.file, digest, remoteUrl), undefined, {
- timeout: PACKAGE_TIMEOUT,
- }) as Promise<{ data: InstalledPlugin }>
- }
- return post<{ data: InstalledPlugin }>(
- BASE,
- { url: source.url, digest, remote_url: remoteUrl || undefined },
- { timeout: PACKAGE_TIMEOUT },
- )
-}
-
-export function setInstalledPluginEnabled(id: string, enabled: boolean) {
- return put<{ data: InstalledPlugin }>(`${BASE}/${encodeURIComponent(id)}/enabled`, { enabled })
-}
-
-export function activatePluginVersion(id: string, version: string) {
- return put<{ data: InstalledPlugin }>(`${BASE}/${encodeURIComponent(id)}/active-version`, { version })
-}
-
-export function setPluginRemoteUrl(id: string, url: string) {
- return put<{ data: InstalledPlugin }>(`${BASE}/${encodeURIComponent(id)}/remote-url`, { url })
-}
-
-/** Issues a new signing secret; the response carries it in issuedSecret. */
-export function rotatePluginSecret(id: string) {
- return post<{ data: InstalledPlugin }>(`${BASE}/${encodeURIComponent(id)}/secret/rotate`, {})
-}
-
-export function uninstallPlugin(id: string) {
- return del(`${BASE}/${encodeURIComponent(id)}`)
-}
-
-export function getPluginSystemConfig(id: string) {
- return get<{ data: PluginConfig }>(`${BASE}/${encodeURIComponent(id)}/config`)
-}
-
-export function updatePluginSystemConfig(id: string, values: ConfigValue) {
- return put<{ data: PluginConfig }>(`${BASE}/${encodeURIComponent(id)}/config`, { values })
-}
diff --git a/frontend/src/api/web-search-provider.ts b/frontend/src/api/web-search-provider.ts
index 7c4ae3655..425c40134 100644
--- a/frontend/src/api/web-search-provider.ts
+++ b/frontend/src/api/web-search-provider.ts
@@ -1,4 +1,3 @@
-import type { ConfigSchema } from '@/components/schema-form/schema'
import { get, post, put, del } from '@/utils/request'
// WebSearchProviderEntity represents a configured web search provider instance
@@ -6,8 +5,7 @@ export interface WebSearchProviderEntity {
id?: string
tenant_id?: number
name: string
- // A builtin provider ID, or a plugin provider's qualified ID (acme.search/brave).
- provider: 'brave' | 'bing' | 'google' | 'duckduckgo' | 'tavily' | 'ollama' | 'baidu' | 'searxng' | 'keenable' | 'zhipu' | 'metaso' | 'exa' | 'bocha' | 'serply' | (string & {})
+ provider: 'brave' | 'bing' | 'google' | 'duckduckgo' | 'tavily' | 'ollama' | 'baidu' | 'searxng' | 'keenable' | 'zhipu' | 'metaso' | 'exa' | 'bocha' | 'serply'
description?: string
parameters: {
// api_key is never returned by the server in this shape; it lives behind
@@ -39,12 +37,6 @@ export interface WebSearchProviderTypeInfo {
description?: string
docs_url?: string
config_fields?: WebSearchProviderConfigField[]
- // The parameters form as a config schema; the settings drawer renders it.
- config_schema?: ConfigSchema
- // Plugin provider types: localized names, an icon (data: URI), the plugin.
- names?: Record<string, string>
- icon?: string
- plugin_id?: string
}
export interface WebSearchProviderConfigField {
diff --git a/frontend/src/components/IMChannelPanel.vue b/frontend/src/components/IMChannelPanel.vue
index c05d4eb64..77e956f8f 100644
--- a/frontend/src/components/IMChannelPanel.vue
+++ b/frontend/src/components/IMChannelPanel.vue
@@ -119,7 +119,7 @@
</template>
<t-option v-for="item in platformOptions" :key="item.value" :value="item.value" :label="item.label">
<div class="im-platform-select-option">
- <img v-if="item.logo" :src="item.logo" :alt="item.label" class="im-platform-select-option__icon" />
+ <img :src="item.logo" :alt="item.label" class="im-platform-select-option__icon" />
<span>{{ item.label }}</span>
</div>
</t-option>
@@ -146,19 +146,28 @@
<!-- Step 2: Connection -->
<div v-else-if="wizardStep === 1" class="im-step-body">
- <section v-if="connectionModes.length > 0" class="setting-drawer__section im-drawer__section">
+ <section v-if="formData.platform !== 'wechat'" class="setting-drawer__section im-drawer__section">
<h4 class="setting-drawer__section-title">{{ $t('agentEditor.im.sectionAccess') }}</h4>
<div class="form-item">
<label class="form-label required">{{ $t('agentEditor.im.mode') }}</label>
<div class="option-chips">
- <button v-for="mode in connectionModes" :key="mode" type="button" class="option-chip"
- :class="{ 'option-chip--active': formData.mode === mode }"
- @click="formData.mode = mode">
- {{ modeLabel(mode) }}
+ <button type="button" class="option-chip"
+ :class="{ 'option-chip--active': formData.mode === 'websocket' }"
+ :disabled="formData.platform === 'mattermost'"
+ @click="formData.mode = 'websocket'">
+ {{ formData.platform === 'dingtalk' ? 'Stream' : 'WebSocket' }}
+ </button>
+ <button v-if="formData.platform !== 'dingtalk' && formData.platform !== 'qqbot'" type="button" class="option-chip" :class="{ 'option-chip--active': formData.mode === 'webhook' }"
+ @click="formData.mode = 'webhook'">
+ Webhook
</button>
</div>
- <p class="form-desc">{{ modeHint }}</p>
+ <p class="form-desc">
+ {{ formData.platform === 'mattermost' ? $t('agentEditor.im.mattermostModeHint') :
+ formData.platform === 'yunzhijia' ? $t('agentEditor.im.yunzhijiaModeHint') :
+ $t('agentEditor.im.modeHint') }}
+ </p>
</div>
<div class="form-item">
@@ -240,19 +249,292 @@
<section class="setting-drawer__section im-drawer__section">
<h4 class="setting-drawer__section-title">{{ $t('agentEditor.im.sectionCredentials') }}</h4>
<div class="drawer-form">
- <div v-if="currentPlatform?.links?.length" class="platform-link-hint">
- <a v-for="link in currentPlatform.links" :key="link.url" :href="link.url" target="_blank"
- rel="noopener noreferrer" class="doc-link">
- {{ link.title_key && te(link.title_key) ? t(link.title_key) : link.title }}
- <t-icon name="link" class="link-icon" />
- </a>
- <span class="hint-text">{{ $t('agentEditor.im.consoleTip') }}</span>
- </div>
+ <!-- WeCom credentials -->
+ <template v-if="formData.platform === 'wecom'">
+ <div class="platform-link-hint">
+ <a href="https://work.weixin.qq.com/" target="_blank" rel="noopener noreferrer" class="doc-link">
+ {{ $t('agentEditor.im.wecomConsole') }}
+ <t-icon name="link" class="link-icon" />
+ </a>
+ <span class="hint-text">{{ $t('agentEditor.im.consoleTip') }}</span>
+ </div>
+ <template v-if="formData.mode === 'websocket'">
+ <div class="form-item">
+ <label class="form-label">Bot ID</label>
+ <t-input v-model="formData.credentials.bot_id" placeholder="Bot ID" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">Bot Secret</label>
+ <t-input v-model="formData.credentials.bot_secret" type="password" placeholder="Bot Secret" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">WebSocket Endpoint</label>
+ <t-input v-model="formData.credentials.ws_endpoint" placeholder="wss://openws.work.weixin.qq.com" />
+ <p class="form-desc">{{ $t('agentEditor.im.wecomWSEndpointHint') }}</p>
+ </div>
+ </template>
+ <template v-else>
+ <div class="form-item">
+ <label class="form-label">Corp ID</label>
+ <t-input v-model="formData.credentials.corp_id" placeholder="Corp ID" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">Agent Secret</label>
+ <t-input v-model="formData.credentials.agent_secret" type="password" placeholder="Agent Secret" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">Token</label>
+ <t-input v-model="formData.credentials.token" placeholder="Token" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">EncodingAESKey</label>
+ <t-input v-model="formData.credentials.encoding_aes_key" placeholder="EncodingAESKey" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">Corp Agent ID</label>
+ <t-input-number v-model="formData.credentials.corp_agent_id" placeholder="Corp Agent ID"
+ style="width: 100%;" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">API Base URL</label>
+ <t-input v-model="formData.credentials.api_base_url" placeholder="https://qyapi.weixin.qq.com" />
+ <p class="form-desc">{{ $t('agentEditor.im.wecomAPIBaseURLHint') }}</p>
+ </div>
+ </template>
+ </template>
+
+ <!-- Feishu / Lark credentials — same fields, different open platform -->
+ <template v-if="formData.platform === 'feishu' || formData.platform === 'lark'">
+ <div class="platform-link-hint">
+ <a :href="openPlatformConsole.url" target="_blank" rel="noopener noreferrer" class="doc-link">
+ {{ $t(openPlatformConsole.labelKey) }}
+ <t-icon name="link" class="link-icon" />
+ </a>
+ <span class="hint-text">{{ $t('agentEditor.im.consoleTip') }}</span>
+ </div>
+ <div class="form-item">
+ <label class="form-label">App ID</label>
+ <t-input v-model="formData.credentials.app_id" placeholder="App ID" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">App Secret</label>
+ <t-input v-model="formData.credentials.app_secret" type="password" placeholder="App Secret" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">Base URL</label>
+ <t-input v-model="formData.credentials.api_base_url" placeholder="https://open.feishu.cn" />
+ <p class="form-desc">{{ $t('agentEditor.im.feishuAPIBaseURLHint') }}</p>
+ </div>
+ <template v-if="formData.mode === 'webhook'">
+ <div class="form-item">
+ <label class="form-label">Verification Token</label>
+ <t-input v-model="formData.credentials.verification_token" placeholder="Verification Token" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">Encrypt Key</label>
+ <t-input v-model="formData.credentials.encrypt_key" type="password" placeholder="Encrypt Key" />
+ </div>
+ </template>
+ </template>
+
+ <!-- Slack credentials -->
+ <template v-if="formData.platform === 'slack'">
+ <div class="platform-link-hint">
+ <a href="https://api.slack.com/apps" target="_blank" rel="noopener noreferrer" class="doc-link">
+ {{ $t('agentEditor.im.slackConsole') }}
+ <t-icon name="link" class="link-icon" />
+ </a>
+ <span class="hint-text">{{ $t('agentEditor.im.consoleTip') }}</span>
+ </div>
+ <template v-if="formData.mode === 'websocket'">
+ <div class="form-item">
+ <label class="form-label">App Token</label>
+ <t-input v-model="formData.credentials.app_token" type="password" placeholder="xapp-..." />
+ </div>
+ <div class="form-item">
+ <label class="form-label">Bot Token</label>
+ <t-input v-model="formData.credentials.bot_token" type="password" placeholder="xoxb-..." />
+ </div>
+ </template>
+ <template v-else>
+ <div class="form-item">
+ <label class="form-label">Bot Token</label>
+ <t-input v-model="formData.credentials.bot_token" type="password" placeholder="xoxb-..." />
+ </div>
+ <div class="form-item">
+ <label class="form-label">Signing Secret</label>
+ <t-input v-model="formData.credentials.signing_secret" type="password" placeholder="Signing Secret" />
+ </div>
+ </template>
+ </template>
+
+ <!-- Telegram credentials -->
+ <template v-if="formData.platform === 'telegram'">
+ <div class="platform-link-hint">
+ <a href="https://t.me/BotFather" target="_blank" rel="noopener noreferrer" class="doc-link">
+ {{ $t('agentEditor.im.telegramConsole') }}
+ <t-icon name="link" class="link-icon" />
+ </a>
+ <span class="hint-text">{{ $t('agentEditor.im.consoleTip') }}</span>
+ </div>
+ <div class="form-item">
+ <label class="form-label">Bot Token</label>
+ <t-input v-model="formData.credentials.bot_token" type="password" placeholder="123456789:AABBccdd..." />
+ </div>
+ <template v-if="formData.mode === 'webhook'">
+ <div class="form-item">
+ <label class="form-label">Secret Token</label>
+ <t-input v-model="formData.credentials.secret_token" type="password"
+ placeholder="Secret Token (optional)" />
+ </div>
+ </template>
+ </template>
+
+ <!-- DingTalk credentials -->
+ <template v-if="formData.platform === 'dingtalk'">
+ <div class="platform-link-hint">
+ <a href="https://open.dingtalk.com/" target="_blank" rel="noopener noreferrer" class="doc-link">
+ {{ $t('agentEditor.im.dingtalkConsole') }}
+ <t-icon name="link" class="link-icon" />
+ </a>
+ <span class="hint-text">{{ $t('agentEditor.im.consoleTip') }}</span>
+ </div>
+ <div class="form-item">
+ <label class="form-label">Client ID (AppKey)</label>
+ <t-input v-model="formData.credentials.client_id" placeholder="Client ID / AppKey" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">Client Secret (AppSecret)</label>
+ <t-input v-model="formData.credentials.client_secret" type="password"
+ placeholder="Client Secret / AppSecret" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">{{ $t('agentEditor.im.dingtalkCardTemplateId') }}</label>
+ <t-input v-model="formData.credentials.card_template_id"
+ placeholder="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.schema" />
+ <p class="form-desc">{{ $t('agentEditor.im.dingtalkCardTemplateIdHint') }}</p>
+ </div>
+ </template>
+
+ <!-- QQBot credentials -->
+ <template v-if="formData.platform === 'qqbot'">
+ <div class="platform-link-hint">
+ <a href="https://q.qq.com/" target="_blank" rel="noopener noreferrer" class="doc-link">
+ {{ $t('agentEditor.im.qqbotConsole') }}
+ <t-icon name="link" class="link-icon" />
+ </a>
+ <span class="hint-text">{{ $t('agentEditor.im.consoleTip') }}</span>
+ </div>
+ <div class="form-item">
+ <label class="form-label">App ID</label>
+ <t-input v-model="formData.credentials.app_id" placeholder="QQBot App ID" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">App Secret</label>
+ <t-input v-model="formData.credentials.client_secret" type="password" placeholder="QQBot App Secret" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">API Base URL</label>
+ <t-input v-model="formData.credentials.api_base_url" placeholder="https://api.sgroup.qq.com" />
+ <p class="form-desc">{{ $t('agentEditor.im.qqbotAPIBaseURLHint') }}</p>
+ </div>
+ <div class="form-item">
+ <label class="form-label">Gateway URL</label>
+ <t-input v-model="formData.credentials.gateway_url" placeholder="wss://api.sgroup.qq.com/websocket/" />
+ <p class="form-desc">{{ $t('agentEditor.im.qqbotGatewayURLHint') }}</p>
+ </div>
+ </template>
- <!-- Credentials from the platform's config schema; mode-specific fields follow formData.mode. -->
- <SchemaForm v-if="currentPlatform?.config_schema" v-model="formData.credentials"
- :schema="currentPlatform.config_schema" :context="{ mode: formData.mode }"
- :errors="credentialErrors" />
+ <!-- Mattermost credentials -->
+ <template v-if="formData.platform === 'mattermost'">
+ <div class="platform-link-hint">
+ <a href="https://developers.mattermost.com/integrate/webhooks/outgoing/" target="_blank"
+ rel="noopener noreferrer" class="doc-link">
+ {{ $t('agentEditor.im.mattermostConsole') }}
+ <t-icon name="link" class="link-icon" />
+ </a>
+ <span class="hint-text">{{ $t('agentEditor.im.consoleTip') }}</span>
+ </div>
+ <div class="form-item">
+ <label class="form-label">Site URL</label>
+ <t-input v-model="formData.credentials.site_url" placeholder="https://mattermost.example.com" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">Bot Token</label>
+ <t-input v-model="formData.credentials.bot_token" type="password" placeholder="Bot Token" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">Outgoing Webhook Token</label>
+ <t-input v-model="formData.credentials.outgoing_token" type="password"
+ placeholder="Token from Outgoing Webhook" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">Bot User ID</label>
+ <t-input v-model="formData.credentials.bot_user_id" placeholder="Optional — filter bot self-messages" />
+ </div>
+ <div class="settings-group">
+ <div class="setting-row setting-row--last">
+ <div class="setting-info">
+ <label>{{ $t('agentEditor.im.mattermostPostToMain') }}</label>
+ <p class="desc">{{ $t('agentEditor.im.mattermostPostToMainHint') }}</p>
+ </div>
+ <div class="setting-control">
+ <t-switch :value="!!formData.credentials.post_to_main" size="small"
+ @change="(v: boolean) => { formData.credentials.post_to_main = v }" />
+ </div>
+ </div>
+ </div>
+ </template>
+
+ <!-- Yunzhijia credentials -->
+ <template v-if="formData.platform === 'yunzhijia'">
+ <div class="form-item">
+ <label class="form-label required">{{ $t('agentEditor.im.yunzhijiaSendMsgUrl') }}</label>
+ <t-input v-model="formData.credentials.send_msg_url"
+ placeholder="https://www.yunzhijia.com/gateway/robot/webhook/send?yzjtype=0&yzjtoken=..." />
+ <p class="form-desc">
+ {{ $t('agentEditor.im.yunzhijiaSendMsgUrlHint') }}
+ <a href="https://www.yunzhijia.com/opendocs/docs.html#/guide/im/robot" target="_blank"
+ rel="noopener noreferrer" class="doc-link">
+ {{ $t('agentEditor.im.yunzhijiaRobotDoc') }}
+ </a>
+ </p>
+ </div>
+ <div class="form-item">
+ <label class="form-label">{{ $t('agentEditor.im.yunzhijiaSecret') }}</label>
+ <t-input v-model="formData.credentials.secret" type="password"
+ :placeholder="$t('agentEditor.im.yunzhijiaSecretPlaceholder')" />
+ <p class="form-desc">{{ $t('agentEditor.im.yunzhijiaSecretHint') }}</p>
+ </div>
+ <div class="form-item">
+ <label class="form-label">{{ $t('agentEditor.im.yunzhijiaAppId') }}</label>
+ <t-input v-model="formData.credentials.app_id"
+ :placeholder="$t('agentEditor.im.yunzhijiaAppIdPlaceholder')" />
+ <p class="form-desc">
+ {{ $t('agentEditor.im.yunzhijiaAppCredentialHint') }}
+ <a href="https://www.yunzhijia.com/developers/" target="_blank" rel="noopener noreferrer"
+ class="doc-link">
+ {{ $t('agentEditor.im.yunzhijiaImageDoc') }}
+ </a>
+ </p>
+ </div>
+ <div class="form-item">
+ <label class="form-label">{{ $t('agentEditor.im.yunzhijiaAppSecret') }}</label>
+ <t-input v-model="formData.credentials.app_secret" type="password"
+ :placeholder="$t('agentEditor.im.yunzhijiaAppSecretPlaceholder')" />
+ </div>
+ <div class="form-item">
+ <label class="form-label">{{ $t('agentEditor.im.yunzhijiaTimeout') }}</label>
+ <t-input-number v-model="formData.credentials.timeout_seconds" placeholder="10" :min="1" :max="60"
+ style="width: 100%;" />
+ <p class="form-desc">{{ $t('agentEditor.im.yunzhijiaTimeoutHint') }}</p>
+ </div>
+ <div class="form-item">
+ <label class="form-label required">{{ $t('agentEditor.im.yunzhijiaAllowedHostSuffix') }}</label>
+ <t-input v-model="formData.credentials.allowed_webhook_host_suffix" placeholder="yunzhijia.com" />
+ <p class="form-desc">{{ $t('agentEditor.im.yunzhijiaAllowedHostSuffixHint') }}</p>
+ </div>
+ </template>
<!-- WeChat credentials (QR code binding) -->
<template v-if="formData.platform === 'wechat'">
@@ -312,16 +594,13 @@ import { copyWithToast } from '@/utils/clipboard';
import { normalizeOptionalString } from '@/utils/optionalString';
import {
listIMChannels, createIMChannel, updateIMChannel, deleteIMChannel, toggleIMChannel,
- getWeChatQRCode, pollWeChatQRCodeStatus, listAllIMChannels, listIMPlatforms, getIMChannel,
- type IMChannelOverview, type CustomAgent, type IMPlatformInfo,
+ getWeChatQRCode, pollWeChatQRCodeStatus, listAllIMChannels,
+ type IMChannelOverview, type CustomAgent,
} from '@/api/agent';
import { useChatResourcesStore } from '@/stores/chatResources';
import type { IMChannel } from '@/api/agent';
import { useAuthStore } from '@/stores/auth';
import SettingDrawer from '@/components/settings/SettingDrawer.vue';
-import SchemaForm from '@/components/schema-form/SchemaForm.vue';
-import { applyDefaults, schemaAt, validateConfig, type FieldError } from '@/components/schema-form/schema';
-import { useSchemaText } from '@/components/schema-form/useSchemaText';
import IntegrationsAgentFilter from '@/components/IntegrationsAgentFilter.vue';
import wecomLogo from '@/assets/img/im/wecom.svg';
import feishuLogo from '@/assets/img/im/feishu.svg';
@@ -352,43 +631,8 @@ const PLATFORM_LOGO: Record<string, string> = {
const platformLogo = (platform: string): string => (platform ? PLATFORM_LOGO[platform] || '' : '');
-const { t, te, locale } = useI18n();
+const { t } = useI18n();
const authStore = useAuthStore();
-const schemaText = useSchemaText();
-
-// Platforms with a registered adapter, from the backend: modes, links and
-// the credential form of each.
-const platforms = ref<IMPlatformInfo[]>([]);
-
-async function loadPlatforms() {
- try {
- const res = await listIMPlatforms();
- platforms.value = res.data || [];
- } catch (e: any) {
- MessagePlugin.error(e?.message || t('common.operationFailed'));
- }
-}
-
-const currentPlatform = computed(() => platforms.value.find((p) => p.id === formData.value.platform));
-
-// Modes offered as chips; long polling (WeChat) runs without a choice.
-const connectionModes = computed(() =>
- (currentPlatform.value?.modes || []).filter((m) => m !== 'longpoll'),
-);
-
-function modeLabel(mode: string): string {
- const custom = currentPlatform.value?.mode_labels?.[mode];
- if (custom) return custom;
- return mode === 'webhook' ? 'Webhook' : 'WebSocket';
-}
-
-const modeHint = computed(() => {
- const key = currentPlatform.value?.mode_hint_key;
- return key && te(key) ? t(key) : t('agentEditor.im.modeHint');
-});
-
-// Field errors of the last save attempt, shown under each credential.
-const credentialErrors = ref<FieldError[]>([]);
const filterAgentId = defineModel<string>('filterAgentId', { default: '' });
@@ -424,9 +668,18 @@ const drawerConfirmText = computed(() =>
wizardStep.value < stepTitles.value.length - 1 ? t('common.next') : t('common.save'),
);
-const platformOptions = computed(() =>
- platforms.value.map((p) => ({ value: p.id as IMPlatform, label: platformLabel(p.id), logo: platformLogo(p.id) })),
-);
+const platformOptions = computed(() => ([
+ { value: 'wecom' as IMPlatform, label: t('agentEditor.im.wecom'), logo: wecomLogo },
+ { value: 'feishu' as IMPlatform, label: t('agentEditor.im.feishu'), logo: feishuLogo },
+ { value: 'lark' as IMPlatform, label: t('agentEditor.im.lark'), logo: larkLogo },
+ { value: 'slack' as IMPlatform, label: t('agentEditor.im.slack'), logo: slackLogo },
+ { value: 'telegram' as IMPlatform, label: t('agentEditor.im.telegram'), logo: telegramLogo },
+ { value: 'dingtalk' as IMPlatform, label: t('agentEditor.im.dingtalk'), logo: dingtalkLogo },
+ { value: 'mattermost' as IMPlatform, label: t('agentEditor.im.mattermost'), logo: mattermostLogo },
+ { value: 'wechat' as IMPlatform, label: t('agentEditor.im.wechat'), logo: wechatLogo },
+ { value: 'qqbot' as IMPlatform, label: t('agentEditor.im.qqbot'), logo: qqbotLogo },
+ { value: 'yunzhijia' as IMPlatform, label: t('agentEditor.im.yunzhijia'), logo: yunzhijiaLogo },
+]));
const localeOptions = computed(() => ([
{ value: '' as IMLocale, label: t('agentEditor.im.replyLanguageDefault') },
@@ -437,6 +690,14 @@ const localeOptions = computed(() => ([
{ value: 'ru-RU' as IMLocale, label: 'Русский' },
]));
+// Feishu and Lark are the same product on separate clouds, so each has its own
+// open platform console. Bots must be created on the one matching the channel.
+const openPlatformConsole = computed(() =>
+ formData.value.platform === 'lark'
+ ? { url: 'https://open.larksuite.com/', labelKey: 'agentEditor.im.larkConsole' }
+ : { url: 'https://open.feishu.cn/', labelKey: 'agentEditor.im.feishuConsole' },
+);
+
const drawerTitle = computed(() => {
if (editingChannel.value) {
return formData.value.name?.trim() || t('agentEditor.im.unnamed');
@@ -526,9 +787,7 @@ function agentForChannel(channel: IMChannel | IMChannelOverview): CustomAgent |
function platformLabel(platform: string): string {
const key = `agentEditor.im.${platform}`;
- if (te(key)) return t(key);
- const info = platforms.value.find((p) => p.id === platform);
- return info?.names?.[locale.value] || info?.name || platform;
+ return t(key);
}
function defaultChannelName(platform: string = formData.value.platform): string {
@@ -540,15 +799,19 @@ function resolvedChannelName(): string {
}
function platformSupportsThread(platform: string): boolean {
- return !!platforms.value.find((p) => p.id === platform)?.supports_thread;
+ return ['slack', 'mattermost', 'feishu', 'lark', 'telegram', 'yunzhijia'].includes(platform);
}
watch(
() => formData.value.platform,
(p) => {
- const modes = currentPlatform.value?.modes || [];
- if (modes.length > 0 && !modes.includes(formData.value.mode)) formData.value.mode = modes[0];
- applyCredentialDefaults();
+ if (p === 'dingtalk' || p === 'qqbot') formData.value.mode = 'websocket';
+ if (p === 'mattermost') {
+ formData.value.mode = 'webhook';
+ if (typeof formData.value.credentials.post_to_main !== 'boolean') {
+ formData.value.credentials.post_to_main = false;
+ }
+ }
if (!platformSupportsThread(p)) {
formData.value.session_mode = 'user';
}
@@ -565,50 +828,41 @@ const wechatBound = computed(() => {
function onPlatformChange(val: string | number | boolean) {
if (editingChannel.value) return;
formData.value.credentials = defaultCredentials();
- credentialErrors.value = [];
stopWeChatPolling();
wechatQRContent.value = '';
wechatQRImgUrl.value = '';
wechatQRCode.value = '';
wechatQRStatus.value = '';
- // Each platform starts on its default mode; long polling (WeChat) only
- // supports full output.
- const info = platforms.value.find((p) => p.id === val);
- formData.value.mode = info?.modes?.[0] || 'websocket';
- formData.value.output_mode = formData.value.mode === 'longpoll' ? 'full' : 'stream';
- applyCredentialDefaults();
+ // WeChat uses fixed mode/output
+ if (val === 'wechat') {
+ formData.value.mode = 'longpoll';
+ formData.value.output_mode = 'full';
+ } else if (val === 'mattermost' || val === 'yunzhijia') {
+ formData.value.mode = 'webhook';
+ formData.value.output_mode = 'stream';
+ if (val === 'yunzhijia') {
+ formData.value.credentials = {
+ timeout_seconds: 10,
+ allowed_webhook_host_suffix: 'yunzhijia.com',
+ };
+ }
+ } else {
+ formData.value.mode = 'websocket';
+ formData.value.output_mode = 'stream';
+ }
if (!channelNameTouched.value) {
formData.value.name = defaultChannelName(String(val));
}
}
-// Fills the platform's schema defaults (Yunzhijia's host suffix and timeout,
-// Mattermost's post_to_main) into missing credentials.
-function applyCredentialDefaults() {
- const schema = currentPlatform.value?.config_schema;
- if (!schema) return;
- const filled = applyDefaults(schema, formData.value.credentials);
- // Assign only on change: a no-op write would still trigger the watcher that
- // clears the errors checkCredentials just set.
- if (JSON.stringify(filled) !== JSON.stringify(formData.value.credentials)) {
- formData.value.credentials = filled;
+function normalizeYunzhijiaCredentials() {
+ if (formData.value.platform !== 'yunzhijia') return;
+ if (!formData.value.credentials.allowed_webhook_host_suffix) {
+ formData.value.credentials.allowed_webhook_host_suffix = 'yunzhijia.com';
+ }
+ if (!formData.value.credentials.timeout_seconds) {
+ formData.value.credentials.timeout_seconds = 10;
}
-}
-
-// Checks the credentials against the platform schema for the chosen mode,
-// marking failing fields and warning with the first.
-function checkCredentials(): boolean {
- const schema = currentPlatform.value?.config_schema;
- if (!schema) return true;
- credentialErrors.value = validateConfig(schema, formData.value.credentials, {
- context: { mode: formData.value.mode },
- });
- const first = credentialErrors.value[0];
- if (!first) return true;
- const field = schemaAt(schema, first.path);
- const label = field ? schemaText(field, 'title') : first.path;
- MessagePlugin.warning(`${label}: ${t(`schemaForm.errors.${first.code}`)}`);
- return false;
}
async function startWeChatBinding() {
@@ -724,15 +978,16 @@ function openDrawer(channel: IMChannel | IMChannelOverview) {
async function editChannel(channel: IMChannel | IMChannelOverview) {
wizardStep.value = 0;
- // List responses carry no credentials; load the channel with its
- // credentials (secrets redacted) so the form shows what is configured and a
- // save does not send an empty credential map.
let fullChannel: IMChannel | null = null;
- try {
- const res = await getIMChannel(channel.id);
- fullChannel = res.data || null;
- } catch {
- fullChannel = null;
+ if (!('credentials' in channel)) {
+ try {
+ const res = await listIMChannels(channel.agent_id);
+ fullChannel = (res.data || []).find((item) => item.id === channel.id) || null;
+ } catch {
+ fullChannel = null;
+ }
+ } else {
+ fullChannel = channel as IMChannel;
}
if (!fullChannel) {
MessagePlugin.error(t('common.operationFailed'));
@@ -752,8 +1007,7 @@ async function editChannel(channel: IMChannel | IMChannelOverview) {
knowledge_base_id: fullChannel.knowledge_base_id || '',
credentials: { ...fullChannel.credentials },
};
- applyCredentialDefaults();
- credentialErrors.value = [];
+ normalizeYunzhijiaCredentials();
showCreateDialog.value = true;
}
@@ -788,8 +1042,15 @@ async function handleSave() {
MessagePlugin.warning(t('agentEditor.im.wechatScanBind'));
return;
}
- applyCredentialDefaults();
- if (!checkCredentials()) return;
+ if (formData.value.platform === 'yunzhijia') {
+ // normalize fills in the default allowed host suffix, so only the send URL
+ // needs explicit validation here.
+ normalizeYunzhijiaCredentials();
+ if (!String(formData.value.credentials.send_msg_url || '').trim()) {
+ MessagePlugin.warning(t('agentEditor.im.yunzhijiaSendMsgUrlRequired'));
+ return;
+ }
+ }
if (editingChannel.value) {
await updateIMChannel(editingChannel.value.id, {
@@ -854,11 +1115,8 @@ async function handleDelete(id: string) {
onMounted(() => {
loadChannels();
- loadPlatforms();
});
-watch(() => formData.value.credentials, () => { credentialErrors.value = []; }, { deep: true });
-
watch(filterAgentId, (id) => {
if (!showCreateDialog.value && !editingChannel.value && id) {
formData.value.target_agent_id = id;
diff --git a/frontend/src/components/ModelEditorDialog.vue b/frontend/src/components/ModelEditorDialog.vue
index 64e3fa1b0..ed53f38a3 100644
--- a/frontend/src/components/ModelEditorDialog.vue
+++ b/frontend/src/components/ModelEditorDialog.vue
@@ -346,14 +346,38 @@
<div v-if="secretExtraField && !isEdit && formData.provider !== 'weknoracloud'" class="form-item">
<label class="form-label" :class="{ required: secretExtraField.required }">{{ extraFieldDisplayLabel(secretExtraField) }}</label>
<t-input v-model="formData.appSecret" type="password"
- :placeholder="extraFieldDisplayPlaceholder(secretExtraField)" autocomplete="new-password" spellcheck="false">
+ :placeholder="secretExtraField.placeholder || ''" autocomplete="new-password" spellcheck="false">
<template #prefix-icon><t-icon name="lock-on" /></template>
</t-input>
- <p v-if="extraFieldDisplayPlaceholder(secretExtraField)" class="form-desc">{{ extraFieldDisplayPlaceholder(secretExtraField) }}</p>
+ <p v-if="secretExtraField.placeholder" class="form-desc">{{ secretExtraField.placeholder }}</p>
</div>
- <!-- 厂商声明的其余额外字段:按厂商 configSchema 渲染,值存入 extra_config[key] -->
- <SchemaForm v-model="extraConfigModel" :schema="plainExtraSchema" />
+ <!-- 厂商声明的其余额外字段:按 type 动态渲染,值存入 extra_config[key] -->
+ <div v-for="field in plainExtraFields" :key="field.key" class="form-item">
+ <label class="form-label" :class="{ required: field.required }">{{ extraFieldDisplayLabel(field) }}</label>
+ <div v-if="field.type === 'boolean'" class="vision-toggle">
+ <t-switch :model-value="extraConfigBool(field.key)"
+ @update:model-value="(v: boolean) => setExtraConfig(field.key, v ? 'true' : 'false')" />
+ <span v-if="extraFieldDisplayPlaceholder(field)" class="form-desc form-desc--inline">{{ extraFieldDisplayPlaceholder(field) }}</span>
+ </div>
+ <t-select v-else-if="field.type === 'select'" :model-value="formData.extraConfig[field.key] || ''"
+ :placeholder="extraFieldDisplayPlaceholder(field)" clearable
+ @update:model-value="(v: string) => setExtraConfig(field.key, v)">
+ <t-option v-for="opt in (field.options || [])" :key="opt.value" :value="opt.value"
+ :label="extraFieldDisplayOptionLabel(opt)" />
+ </t-select>
+ <t-input v-else-if="field.type === 'number'" :model-value="formData.extraConfig[field.key] || ''"
+ type="number" :placeholder="extraFieldDisplayPlaceholder(field)"
+ @update:model-value="(v: string | number) => setExtraConfig(field.key, String(v ?? ''))" />
+ <t-input v-else-if="field.type === 'password'" :model-value="formData.extraConfig[field.key] || ''"
+ type="password" :placeholder="extraFieldDisplayPlaceholder(field)" autocomplete="new-password" spellcheck="false"
+ @update:model-value="(v: string) => setExtraConfig(field.key, v)">
+ <template #prefix-icon><t-icon name="lock-on" /></template>
+ </t-input>
+ <t-input v-else :model-value="formData.extraConfig[field.key] || ''"
+ :placeholder="extraFieldDisplayPlaceholder(field)"
+ @update:model-value="(v: string) => setExtraConfig(field.key, v)" />
+ </div>
<!-- 自定义 HTTP Header(类似 OpenAI Python SDK 的 extra_headers) -->
<div v-if="formData.provider !== 'weknoracloud'" class="form-item">
@@ -588,7 +612,7 @@ import {
checkOllamaModels, checkRemoteModel, testEmbeddingModel, checkRerankModel, checkASRModel, listOllamaModels,
downloadOllamaModel, getDownloadProgress, checkOllamaStatus, resolveModelCatalog,
type OllamaModelInfo, type ModelProviderOption, type ModelProviderExtraField,
- type ModelCatalogEntry,
+ type ModelProviderExtraFieldOption, type ModelCatalogEntry,
type ResolvedModelCatalog,
} from '@/api/initialization'
import {
@@ -604,9 +628,9 @@ import { useModelProvidersStore } from '@/stores/modelProviders'
import {
credentialLabelForModelType,
extraFieldLabel,
+ extraFieldOptionLabel,
extraFieldPlaceholder,
extraFieldsForModelType,
- plainExtraConfigSchema,
pickLocalized,
providerDescription,
providerIcon,
@@ -616,7 +640,6 @@ import { levelLabelKey, supportedLevels } from '@/utils/reasoningEffort'
import { DEFAULT_MODEL_CONTEXT_WINDOW, formatTokenCount } from '@/utils/contextWindow'
import { copyWithToast } from '@/utils/clipboard'
import SettingDrawer from '@/components/settings/SettingDrawer.vue'
-import SchemaForm from '@/components/schema-form/SchemaForm.vue'
import CredentialResource, {
type CredentialFieldDef,
type CredentialResourceApi,
@@ -774,6 +797,8 @@ const matchTriggerWidth = (triggerElement: HTMLElement) => ({
const extraFieldDisplayLabel = (field: ModelProviderExtraField) => extraFieldLabel(field, currentLocale.value)
const extraFieldDisplayPlaceholder = (field: ModelProviderExtraField) =>
extraFieldPlaceholder(field, currentLocale.value)
+const extraFieldDisplayOptionLabel = (option: ModelProviderExtraFieldOption) =>
+ extraFieldOptionLabel(option, currentLocale.value)
/**
* Vendors whose API is not a bearer-token API name their first credential
@@ -816,22 +841,6 @@ const secretExtraField = computed<ModelProviderExtraField | undefined>(() =>
const plainExtraFields = computed<ModelProviderExtraField[]>(() =>
visibleExtraFields.value.filter(field => !isSecretExtraField(field)),
)
-// The same plain fields as a config schema, rendered by <SchemaForm>.
-const plainExtraSchema = computed(() =>
- plainExtraConfigSchema(selectedProvider.value?.configSchema, activeModelType.value, RESERVED_EXTRA_CONFIG_KEYS),
-)
-// extra_config is a map of strings; empty values are dropped as the user
-// clears them, so a cleared field falls back to the vendor default.
-const extraConfigModel = computed<Record<string, unknown>>({
- get: () => formData.value.extraConfig || {},
- set: (value) => {
- const next: Record<string, string> = {}
- for (const [key, v] of Object.entries(value)) {
- if (v !== undefined && v !== null && v !== '') next[key] = String(v)
- }
- formData.value.extraConfig = next
- },
-})
/** extra_config keys that belong to the connection, not to the vendor. */
const VENDOR_NEUTRAL_EXTRA_CONFIG_KEYS = ['api', 'remote_model_name'] as const
@@ -854,6 +863,11 @@ const setExtraConfig = (key: string, value: string | null | undefined) => {
formData.value.extraConfig = next
}
+const extraConfigBool = (key: string) => {
+ const raw = (formData.value.extraConfig?.[key] || '').trim().toLowerCase()
+ return raw === 'true' || raw === '1' || raw === 'yes'
+}
+
/** Pre-fill vendor defaults for fields the user has not touched. */
const applyExtraFieldDefaults = () => {
for (const field of plainExtraFields.value) {
diff --git a/frontend/src/components/modelEditorFlow.test.ts b/frontend/src/components/modelEditorFlow.test.ts
index 401f5a29c..08d0d449e 100644
--- a/frontend/src/components/modelEditorFlow.test.ts
+++ b/frontend/src/components/modelEditorFlow.test.ts
@@ -872,25 +872,34 @@ test('every vendor-declared string in the form goes through a locale resolver',
const template = descriptor.template?.content ?? ''
assert.ok(template.length > 0, 'the editor template should have been parsed')
- // Plain extra fields render through <SchemaForm>, which resolves x-i18n
- // titles, placeholders and option labels (schema-form/schema.test.ts).
- assert.match(
- template, /<SchemaForm v-model="extraConfigModel" :schema="plainExtraSchema"/,
- 'plain extra fields should render from the vendor config schema',
- )
-
- // The secret field still has its own input (it fills the app_secret slot),
- // so its placeholder must go through the resolver rather than be read raw.
- for (const pattern of [/secretExtraField\.placeholder/, /field\.placeholder/]) {
+ // `field` is the extra-field loop variable, so any read of its placeholder
+ // is a vendor string rendered without resolving the locale. One input
+ // branch per field type plus the secret field means it is easy to bind four
+ // and miss the fifth, which is exactly what happened.
+ for (const pattern of [
+ /:placeholder="field\.placeholder/,
+ /\{\{\s*field\.placeholder\s*\}\}/,
+ /v-if="field\.placeholder"/,
+ ]) {
assert.equal(
pattern.test(template), false,
- `template reads a vendor placeholder directly (${pattern}); use extraFieldDisplayPlaceholder`,
+ `template reads field.placeholder directly (${pattern}); use extraFieldDisplayPlaceholder`,
)
}
- assert.ok(
- template.includes('extraFieldDisplayPlaceholder(secretExtraField)'),
- 'the secret field placeholder must use the resolver',
+
+ // Options: scope to the extra-field select, because `opt` is also the loop
+ // variable of the model-type list and the catalog model list, whose labels
+ // come from i18n and from the catalog's own names.
+ const optionLoop = /v-for="opt in \(field\.options \|\| \[\]\)"[^>]*/.exec(template)
+ assert.ok(optionLoop, 'the extra-field select should loop over field.options')
+ assert.match(
+ optionLoop[0], /:label="extraFieldDisplayOptionLabel\(opt\)"/,
+ 'extra-field option labels must resolve the locale',
)
+
+ // And the placeholder resolver is referenced, so the checks above cannot be
+ // satisfied by dropping the placeholders altogether.
+ assert.ok(template.includes('extraFieldDisplayPlaceholder('), 'placeholders must use the resolver')
})
test('the chat protocol override is offered only on chat and vision rows', () => {
diff --git a/frontend/src/components/schema-form/SchemaForm.vue b/frontend/src/components/schema-form/SchemaForm.vue
deleted file mode 100644
index 9202961e0..000000000
--- a/frontend/src/components/schema-form/SchemaForm.vue
+++ /dev/null
@@ -1,100 +0,0 @@
-<template>
- <div class="schema-form">
- <SchemaFormFields
- :schema="schema"
- :value="modelValue ?? {}"
- path=""
- :group="group"
- :secret-mode="secretMode"
- :errors="errors"
- :disabled="disabled"
- :context="context"
- @update:value="$emit('update:modelValue', $event)"
- >
- <template #secret="slotProps"><slot name="secret" v-bind="slotProps" /></template>
- </SchemaFormFields>
- </div>
-</template>
-
-<script setup lang="ts">
-import type { ConfigSchema, ConfigValue, FieldError, SecretSlotProps } from './schema'
-import SchemaFormFields from './SchemaFormFields.vue'
-
-/**
- * Renders a config schema (internal/plugin/configschema) as form fields.
- *
- * The root uses display: contents, so each field is a direct child of the
- * surrounding drawer section and picks up its spacing. Pass `group` to render
- * one x-group per section. With secretMode "slot", secret fields render the
- * `secret` slot instead of an input, for forms whose secrets are edited
- * through a /credentials subresource.
- */
-withDefaults(
- defineProps<{
- schema: ConfigSchema
- modelValue: ConfigValue | undefined
- group?: string
- secretMode?: 'input' | 'slot'
- errors?: FieldError[]
- disabled?: boolean
- /** Values "$"-prefixed x-visible-if keys read, e.g. { mode } for IM credentials. */
- context?: ConfigValue
- }>(),
- { group: undefined, secretMode: 'input', errors: () => [], disabled: false, context: undefined },
-)
-
-defineEmits<{ 'update:modelValue': [value: ConfigValue] }>()
-
-defineSlots<{ secret?: (props: SecretSlotProps) => unknown }>()
-</script>
-
-<style lang="less" scoped>
-.schema-form {
- display: contents;
-}
-
-// Same drawer form conventions as ModelEditorDialog / WebSearchSettings.
-:deep(.form-item) {
- margin-bottom: 0;
-}
-
-:deep(.form-label) {
- display: block;
- margin-bottom: 6px;
- font-size: var(--app-text-md);
- font-weight: 500;
- color: var(--td-text-color-primary);
- line-height: 1.4;
-
- &.required::before {
- content: '*';
- color: var(--td-error-color);
- margin-right: 4px;
- font-weight: 500;
- line-height: 1;
- }
-}
-
-:deep(.form-desc),
-:deep(.form-error) {
- margin: 4px 0 0;
- font-size: var(--app-text-sm);
- line-height: 1.5;
-}
-
-:deep(.form-desc) {
- color: var(--td-text-color-placeholder);
-}
-
-:deep(.form-error) {
- color: var(--td-error-color);
-}
-
-:deep(.t-input),
-:deep(.t-select),
-:deep(.t-textarea),
-:deep(.t-input-number) {
- width: 100%;
- font-size: var(--app-text-md);
-}
-</style>
diff --git a/frontend/src/components/schema-form/SchemaFormFields.vue b/frontend/src/components/schema-form/SchemaFormFields.vue
deleted file mode 100644
index c25d4c385..000000000
--- a/frontend/src/components/schema-form/SchemaFormFields.vue
+++ /dev/null
@@ -1,114 +0,0 @@
-<template>
- <template v-for="field in fields" :key="field.key">
- <!-- Nested objects render their fields inline, in their own order. -->
- <SchemaFormFields
- v-if="field.schema.type === 'object'"
- :schema="field.schema"
- :value="childObject(field.key)"
- :path="join(field.key)"
- :secret-mode="secretMode"
- :errors="errors"
- :disabled="disabled"
- :context="context"
- @update:value="set(field.key, $event)"
- >
- <template #secret="slotProps"><slot name="secret" v-bind="slotProps" /></template>
- </SchemaFormFields>
- <div v-else class="form-item" :data-field="join(field.key)">
- <label class="form-label" :class="{ required: field.required }">{{ text(field.schema, 'title') || field.key }}</label>
- <slot
- v-if="field.schema['x-secret'] && secretMode === 'slot'"
- name="secret"
- :path="join(field.key)"
- :schema="field.schema"
- :label="text(field.schema, 'title') || field.key"
- :required="field.required"
- />
- <component
- :is="resolveWidget(field.schema)"
- v-else
- :model-value="value[field.key]"
- :path="join(field.key)"
- :schema="field.schema"
- :placeholder="text(field.schema, 'placeholder')"
- :disabled="disabled"
- :required="field.required"
- @update:model-value="set(field.key, $event)"
- />
- <p v-if="errorFor(field.key)" class="form-error">{{ errorFor(field.key) }}</p>
- <p v-else-if="text(field.schema, 'description')" class="form-desc">{{ text(field.schema, 'description') }}</p>
- </div>
- </template>
-</template>
-
-<script setup lang="ts">
-import { computed } from 'vue'
-import { useI18n } from 'vue-i18n'
-
-import {
- HIDDEN_WIDGET,
- inGroup,
- isPlainObject,
- isVisible,
- orderedFields,
- type ConfigSchema,
- type ConfigValue,
- type FieldError,
- type SecretSlotProps,
-} from './schema'
-import { useSchemaText } from './useSchemaText'
-import { resolveWidget } from './widgets'
-
-const props = defineProps<{
- schema: ConfigSchema
- value: ConfigValue
- /** Dotted path of this level; '' for the root. */
- path: string
- /** Only fields of this x-group are shown at the root level. */
- group?: string
- secretMode: 'input' | 'slot'
- errors?: FieldError[]
- disabled?: boolean
- /** Values "$"-prefixed x-visible-if keys read. */
- context?: ConfigValue
-}>()
-
-const emit = defineEmits<{ 'update:value': [value: ConfigValue] }>()
-
-defineSlots<{ secret?: (props: SecretSlotProps) => unknown }>()
-
-const { t, te } = useI18n()
-const text = useSchemaText()
-
-const fields = computed(() =>
- orderedFields(props.schema).filter(
- f =>
- f.schema['x-widget'] !== HIDDEN_WIDGET &&
- isVisible(f.schema, props.value, props.context) &&
- (props.path !== '' || inGroup(f.schema, props.group)),
- ),
-)
-
-function join(key: string) {
- return props.path ? `${props.path}.${key}` : key
-}
-
-function childObject(key: string): ConfigValue {
- const v = props.value[key]
- return isPlainObject(v) ? v : {}
-}
-
-function set(key: string, v: unknown) {
- const next: ConfigValue = { ...props.value }
- if (v === undefined) delete next[key]
- else next[key] = v
- emit('update:value', next)
-}
-
-function errorFor(key: string): string {
- const e = props.errors?.find(err => err.path === join(key))
- if (!e) return ''
- const k = `schemaForm.errors.${e.code}`
- return te(k) ? t(k) : e.message || e.code
-}
-</script>
diff --git a/frontend/src/components/schema-form/pluginSource.ts b/frontend/src/components/schema-form/pluginSource.ts
deleted file mode 100644
index d56bd2d13..000000000
--- a/frontend/src/components/schema-form/pluginSource.ts
+++ /dev/null
@@ -1,50 +0,0 @@
-import { pluginFormOptions, startPluginOAuth, type PluginFormScope } from '@/api/plugin'
-
-import type { ConfigValue } from './schema'
-import { valueAt, type SchemaFormSource } from './source'
-
-export interface PluginFormTarget {
- pluginId: string
- scope: PluginFormScope
- /** "<point>/<id>" for instance forms. */
- contribution?: string
- instanceId?: string
-}
-
-export interface PluginFormBinding {
- target: () => PluginFormTarget | undefined
- /** What the form holds now, in the shape the plugin's schema has. */
- values: () => ConfigValue
- /** A field's value by its path in the plugin's schema; defaults to a lookup in values(). */
- dependency?: (path: string) => unknown
- /** Maps a form path to the plugin schema's path, for forms that nest fields. */
- field?: (path: string) => string
-}
-
-/** A SchemaFormSource that asks the plugin behind a form. */
-export function pluginFormSource(b: PluginFormBinding): SchemaFormSource {
- const need = () => {
- const t = b.target()
- if (!t) throw new Error('no plugin behind this form')
- return t
- }
- const field = (path: string) => (b.field ? b.field(path) : path)
- return {
- dependency: path => (b.dependency ? b.dependency(path) : valueAt(b.values(), path)),
- async options(path, source, query) {
- const t = need()
- const res = await pluginFormOptions(t.pluginId, {
- name: source.name, field: field(path), scope: t.scope, contribution: t.contribution,
- instanceId: t.instanceId, values: b.values(), query: query || undefined,
- })
- return res.data?.options ?? []
- },
- async startOAuth(path) {
- const t = need()
- const res = await startPluginOAuth(t.pluginId, {
- scope: t.scope, contribution: t.contribution, field: field(path),
- })
- return res.data
- },
- }
-}
diff --git a/frontend/src/components/schema-form/schema.test.ts b/frontend/src/components/schema-form/schema.test.ts
deleted file mode 100644
index 680c590a8..000000000
--- a/frontend/src/components/schema-form/schema.test.ts
+++ /dev/null
@@ -1,165 +0,0 @@
-import assert from 'node:assert/strict'
-import test from 'node:test'
-
-import {
- REDACTED_SECRET,
- choicesOf,
- inGroup,
- isVisible,
- orderedFields,
- resolveText,
- schemaAt,
- secretPaths,
- validateConfig,
- applyDefaults,
- type ConfigSchema,
- type Translator,
-} from './schema'
-
-// Same shape as the backend's web search schema for Zhipu.
-const zhipu: ConfigSchema = {
- type: 'object',
- required: ['api_key'],
- properties: {
- proxy_url: { type: 'string', format: 'uri', 'x-group': 'options', 'x-order': 50 },
- api_key: {
- type: 'string', 'x-secret': true, 'x-widget': 'password', 'x-group': 'credentials', 'x-order': 20,
- 'x-i18n-keys': { title: 'webSearchSettings.apiKeyLabel' }, title: 'API key',
- },
- extra_config: {
- type: 'object', 'x-group': 'credentials', 'x-order': 40, required: ['content_size'],
- properties: {
- content_size: {
- type: 'string', default: 'medium', title: 'Content size',
- oneOf: [{ const: 'medium', title: 'Medium' }, { const: 'high', title: 'High' }],
- },
- },
- },
- },
-}
-
-const tr = (messages: Record<string, string>, locale = 'zh-CN'): Translator => ({
- t: key => messages[key] ?? key,
- te: key => key in messages,
- locale,
-})
-
-test('orderedFields sorts by x-order then key and marks required fields', () => {
- const fields = orderedFields(zhipu)
- assert.deepEqual(fields.map(f => f.key), ['api_key', 'extra_config', 'proxy_url'])
- assert.equal(fields[0].required, true)
- assert.equal(fields[2].required, false)
- const tie = orderedFields({ type: 'object', properties: { b: {}, a: {} } })
- assert.deepEqual(tie.map(f => f.key), ['a', 'b'])
-})
-
-test('isVisible and inGroup', () => {
- const token: ConfigSchema = { type: 'string', 'x-visible-if': { mode: 'token', port: 8080 } }
- assert.equal(isVisible(token, { mode: 'token', port: 8080 }), true)
- assert.equal(isVisible(token, { mode: 'basic', port: 8080 }), false)
- assert.equal(isVisible(token, undefined), false)
- assert.equal(isVisible({ type: 'string' }, undefined), true)
- assert.equal(inGroup(zhipu.properties!.api_key, 'credentials'), true)
- assert.equal(inGroup(zhipu.properties!.api_key, 'options'), false)
- assert.equal(inGroup(zhipu.properties!.api_key, undefined), true)
- assert.equal(inGroup({ type: 'string' }, ''), true)
-})
-
-test('resolveText prefers a known frontend key, then x-i18n, then plain text', () => {
- const apiKey = zhipu.properties!.api_key
- assert.equal(resolveText(apiKey, 'title', tr({ 'webSearchSettings.apiKeyLabel': 'API 密钥' })), 'API 密钥')
- assert.equal(resolveText(apiKey, 'title', tr({})), 'API key')
-
- const plugin: ConfigSchema = {
- title: 'Region',
- 'x-placeholder': 'e.g. bj',
- 'x-i18n': { title: { 'zh-CN': '地域' }, placeholder: { 'zh-CN': '例如 bj' } },
- }
- assert.equal(resolveText(plugin, 'title', tr({})), '地域')
- assert.equal(resolveText(plugin, 'title', tr({}, 'zh-TW')), '地域', 'same language falls back')
- assert.equal(resolveText(plugin, 'title', tr({}, 'en-US')), 'Region')
- assert.equal(resolveText(plugin, 'placeholder', tr({}, 'en-US')), 'e.g. bj')
- assert.equal(resolveText(plugin, 'placeholder', tr({})), '例如 bj')
- assert.equal(resolveText(plugin, 'description', tr({})), '')
-})
-
-test('applyDefaults fills missing values recursively without overwriting', () => {
- assert.deepEqual(applyDefaults(zhipu, {}), { extra_config: { content_size: 'medium' } })
- assert.deepEqual(
- applyDefaults(zhipu, { proxy_url: 'http://p', extra_config: { content_size: 'high' } }),
- { proxy_url: 'http://p', extra_config: { content_size: 'high' } },
- )
- const input = { extra_config: {} }
- applyDefaults(zhipu, input)
- assert.deepEqual(input, { extra_config: {} }, 'input is not mutated')
-})
-
-test('choicesOf reads oneOf, then enum', () => {
- assert.deepEqual(choicesOf(zhipu.properties!.extra_config.properties!.content_size).map(c => c.value), ['medium', 'high'])
- assert.deepEqual(choicesOf({ enum: ['a', 'b'] }).map(c => c.schema.title), ['a', 'b'])
- assert.deepEqual(choicesOf({ type: 'string' }), [])
-})
-
-test('secretPaths walks nested objects', () => {
- const s: ConfigSchema = {
- type: 'object',
- properties: {
- token: { type: 'string', 'x-secret': true },
- auth: { type: 'object', properties: { password: { type: 'string', 'x-secret': true } } },
- },
- }
- assert.deepEqual(secretPaths(s), ['auth.password', 'token'])
-})
-
-test('validateConfig mirrors the backend rules', () => {
- assert.deepEqual(validateConfig(zhipu, { api_key: 'k', extra_config: { content_size: 'high' } }), [])
- assert.deepEqual(
- validateConfig(zhipu, { extra_config: { content_size: 'huge' }, proxy_url: 'not a url' }),
- [
- { path: 'api_key', code: 'required' },
- { path: 'extra_config.content_size', code: 'enum' },
- { path: 'proxy_url', code: 'format' },
- ],
- )
- assert.deepEqual(
- validateConfig(zhipu, { extra_config: { content_size: 'high' } }, { skipSecrets: true }),
- [],
- 'secrets edited elsewhere are not required here',
- )
- assert.deepEqual(validateConfig(zhipu, { api_key: REDACTED_SECRET, extra_config: { content_size: 'high' } }), [])
-
- const hidden: ConfigSchema = {
- type: 'object',
- required: ['mode', 'token'],
- properties: {
- mode: { type: 'string' },
- token: { type: 'string', minLength: 8, 'x-visible-if': { mode: 'token' } },
- size: { type: 'integer', minimum: 1, maximum: 10 },
- },
- }
- assert.deepEqual(validateConfig(hidden, { mode: 'basic' }), [])
- assert.deepEqual(validateConfig(hidden, { mode: 'token', token: 'short', size: 2.5 }), [
- { path: 'size', code: 'type' },
- { path: 'token', code: 'min_length' },
- ])
- assert.deepEqual(validateConfig(hidden, { mode: 'basic', size: 11 }), [{ path: 'size', code: 'maximum' }])
-})
-
-test('schemaAt follows dotted paths', () => {
- assert.equal(schemaAt(zhipu, 'extra_config.content_size')?.default, 'medium')
- assert.equal(schemaAt(zhipu, 'api_key')?.['x-secret'], true)
- assert.equal(schemaAt(zhipu, 'extra_config.missing'), undefined)
- assert.equal(schemaAt(zhipu, 'api_key.deeper'), undefined)
-})
-
-test('"$" keys in x-visible-if read the context', () => {
- const s: ConfigSchema = {
- type: 'object',
- required: ['signing_secret'],
- properties: { signing_secret: { type: 'string', 'x-visible-if': { $mode: 'webhook' } } },
- }
- assert.equal(isVisible(s.properties!.signing_secret, {}, { mode: 'webhook' }), true)
- assert.equal(isVisible(s.properties!.signing_secret, { mode: 'webhook' }, { mode: 'websocket' }), false)
- assert.deepEqual(validateConfig(s, {}, { context: { mode: 'websocket' } }), [])
- assert.deepEqual(validateConfig(s, {}, { context: { mode: 'webhook' } }), [{ path: 'signing_secret', code: 'required' }])
-})
diff --git a/frontend/src/components/schema-form/schema.ts b/frontend/src/components/schema-form/schema.ts
deleted file mode 100644
index 45ccd778f..000000000
--- a/frontend/src/components/schema-form/schema.ts
+++ /dev/null
@@ -1,320 +0,0 @@
-// Config schemas as the backend serves them (internal/plugin/configschema):
-// a JSON Schema subset plus x- keywords for the form. This module is the
-// pure part of <SchemaForm>: ordering, visibility, text lookup, defaults and
-// validation, kept free of Vue so it runs under node:test.
-
-export type SchemaType = 'object' | 'string' | 'number' | 'integer' | 'boolean' | 'array'
-
-/** Text slots a field can localize. */
-export type TextSlot = 'title' | 'description' | 'placeholder'
-
-export interface ConfigSchema {
- type?: SchemaType
- title?: string
- description?: string
- properties?: Record<string, ConfigSchema>
- required?: string[]
- items?: ConfigSchema
- default?: unknown
- const?: unknown
- enum?: unknown[]
- oneOf?: ConfigSchema[]
- minLength?: number
- maxLength?: number
- pattern?: string
- minimum?: number
- maximum?: number
- format?: string
- 'x-secret'?: boolean
- 'x-widget'?: string
- 'x-placeholder'?: string
- 'x-i18n'?: Partial<Record<TextSlot, Record<string, string>>>
- 'x-i18n-keys'?: Partial<Record<TextSlot, string>>
- 'x-visible-if'?: Record<string, unknown>
- 'x-group'?: string
- 'x-order'?: number
- 'x-model-types'?: string[]
- 'x-options'?: OptionsSource
- 'x-oauth'?: OAuthSpec
-}
-
-/** x-options: the field's choices come from the plugin. */
-export interface OptionsSource {
- /** The plugin's options endpoint. */
- name: string
- /** Dotted paths of fields the choices depend on; they reload when those change. */
- dependsOn?: string[]
- /** Ask the plugin with what the user types. */
- search?: boolean
-}
-
-/** x-oauth: the field holds a connection WeKnora authorized. */
-export interface OAuthSpec {
- authorizeUrl: string
- tokenUrl?: string
- scopes?: string[]
-}
-
-/** Prefix of an x-oauth field's value (same as configschema.OAuthRefPrefix). */
-export const OAUTH_REF_PREFIX = 'oauth:'
-
-export function isOAuthRef(v: unknown): v is string {
- return typeof v === 'string' && v.length > OAUTH_REF_PREFIX.length && v.startsWith(OAUTH_REF_PREFIX)
-}
-
-export type ConfigValue = Record<string, unknown>
-
-/** Same codes as configschema.FieldError on the backend. */
-export type FieldErrorCode =
- | 'required' | 'type' | 'enum' | 'min_length' | 'max_length'
- | 'pattern' | 'minimum' | 'maximum' | 'format'
-
-export interface FieldError {
- path: string
- code: FieldErrorCode
- message?: string
-}
-
-/** x-widget value that keeps a declared field out of the form. */
-export const HIDDEN_WIDGET = 'hidden'
-
-/** Same value the backend sends for a set secret. */
-export const REDACTED_SECRET = '***'
-
-/** Props of the `secret` slot of <SchemaForm> in secretMode "slot". */
-export interface SecretSlotProps {
- path: string
- schema: ConfigSchema
- label: string
- required: boolean
-}
-
-export interface SchemaField {
- key: string
- schema: ConfigSchema
- required: boolean
-}
-
-/** Properties in form order: x-order, then key. */
-export function orderedFields(schema: ConfigSchema | undefined): SchemaField[] {
- const props = schema?.properties ?? {}
- const required = new Set(schema?.required ?? [])
- return Object.keys(props)
- .sort((a, b) => {
- const oa = props[a]['x-order'] ?? 0
- const ob = props[b]['x-order'] ?? 0
- return oa !== ob ? oa - ob : a < b ? -1 : a > b ? 1 : 0
- })
- .map(key => ({ key, schema: props[key], required: required.has(key) }))
-}
-
-/** The schema node at a dotted path ("auth.token"), or undefined. */
-export function schemaAt(schema: ConfigSchema, path: string): ConfigSchema | undefined {
- let node: ConfigSchema | undefined = schema
- for (const key of path.split('.')) {
- node = node?.properties?.[key]
- if (!node) return undefined
- }
- return node
-}
-
-/**
- * x-visible-if: every listed sibling must equal the given value. Keys that
- * start with "$" ("$mode") are read from context instead: values of the object
- * the configuration lives in, such as an IM channel's mode.
- */
-export function isVisible(
- schema: ConfigSchema,
- siblings: ConfigValue | undefined,
- context?: ConfigValue,
-): boolean {
- const cond = schema['x-visible-if']
- if (!cond) return true
- return Object.entries(cond).every(([key, want]) => {
- const got = key.startsWith('$') ? context?.[key.slice(1)] : siblings?.[key]
- return looselyEqual(got, want)
- })
-}
-
-/** Whether a field belongs to a group filter; no filter shows everything. */
-export function inGroup(schema: ConfigSchema, group: string | undefined): boolean {
- return group === undefined || (schema['x-group'] ?? '') === group
-}
-
-/** The choices a select can offer: oneOf entries, else enum values. */
-export function choicesOf(schema: ConfigSchema): Array<{ value: unknown; schema: ConfigSchema }> {
- if (schema.oneOf?.length) return schema.oneOf.map(c => ({ value: c.const, schema: c }))
- return (schema.enum ?? []).map(v => ({ value: v, schema: { title: String(v) } }))
-}
-
-export interface Translator {
- t: (key: string) => string
- te: (key: string) => boolean
- locale: string
-}
-
-/**
- * Resolves a text slot: a frontend locale key when it exists, then the
- * schema's own translation for the locale (or its language), then the plain
- * text. Builtins lean on keys; plugins ship their text in x-i18n.
- */
-export function resolveText(schema: ConfigSchema, slot: TextSlot, tr: Translator): string {
- const key = schema['x-i18n-keys']?.[slot]
- if (key && tr.te(key)) return tr.t(key)
- const table = schema['x-i18n']?.[slot]
- if (table) {
- if (table[tr.locale]) return table[tr.locale]
- const lang = tr.locale.split('-')[0]
- const match = Object.keys(table).sort().find(k => k.split('-')[0] === lang && table[k])
- if (match) return table[match]
- }
- if (slot === 'placeholder') return schema['x-placeholder'] ?? ''
- return schema[slot] ?? ''
-}
-
-/**
- * Fills defaults into a copy of value: missing scalars take their default and
- * nested objects are filled recursively. Existing values are never replaced.
- */
-export function applyDefaults(schema: ConfigSchema, value: ConfigValue | undefined): ConfigValue {
- const out: ConfigValue = { ...(value ?? {}) }
- for (const { key, schema: prop } of orderedFields(schema)) {
- if (prop.type === 'object') {
- const child = out[key]
- out[key] = applyDefaults(prop, isPlainObject(child) ? child : undefined)
- continue
- }
- if ((out[key] === undefined || out[key] === '') && prop.default !== undefined) {
- out[key] = prop.default
- }
- }
- return out
-}
-
-/** Dotted paths of every secret field. */
-export function secretPaths(schema: ConfigSchema, prefix = ''): string[] {
- const out: string[] = []
- for (const { key, schema: prop } of orderedFields(schema)) {
- const path = prefix ? `${prefix}.${key}` : key
- if (prop['x-secret']) out.push(path)
- if (prop.type === 'object') out.push(...secretPaths(prop, path))
- }
- return out
-}
-
-export interface ValidateOptions {
- /** Skip secret fields entirely (they are edited elsewhere, e.g. /credentials). */
- skipSecrets?: boolean
- /** Values "$"-prefixed x-visible-if keys read (see isVisible). */
- context?: ConfigValue
-}
-
-/**
- * Mirrors configschema.Validate: required fields hidden by x-visible-if are
- * not required, undeclared keys are allowed, and a redacted secret counts as
- * present.
- */
-export function validateConfig(
- schema: ConfigSchema,
- value: ConfigValue | undefined,
- opts: ValidateOptions = {},
-): FieldError[] {
- const errors: FieldError[] = []
- validateObject(schema, value ?? {}, '', errors, opts)
- return errors
-}
-
-function validateObject(
- schema: ConfigSchema,
- value: ConfigValue,
- path: string,
- errors: FieldError[],
- opts: ValidateOptions,
-) {
- for (const { key, schema: prop, required } of orderedFields(schema)) {
- if (!isVisible(prop, value, opts.context)) continue
- if (opts.skipSecrets && prop['x-secret']) continue
- const fieldPath = path ? `${path}.${key}` : key
- const v = value[key]
- if (v === undefined || v === null || v === '') {
- if (required) errors.push({ path: fieldPath, code: 'required' })
- continue
- }
- validateValue(prop, v, fieldPath, errors, opts)
- }
-}
-
-function validateValue(
- schema: ConfigSchema,
- v: unknown,
- path: string,
- errors: FieldError[],
- opts: ValidateOptions,
-) {
- const add = (code: FieldErrorCode) => errors.push({ path, code })
- switch (schema.type) {
- case 'object':
- if (!isPlainObject(v)) return add('type')
- return validateObject(schema, v, path, errors, opts)
- case 'array':
- if (!Array.isArray(v)) return add('type')
- v.forEach((item, i) => schema.items && validateValue(schema.items, item, `${path}[${i}]`, errors, opts))
- return
- case 'string': {
- if (typeof v !== 'string') return add('type')
- if (schema['x-secret'] && v === REDACTED_SECRET) return
- if (schema['x-oauth']) {
- if (!isOAuthRef(v)) add('format')
- return
- }
- const n = [...v].length
- if (schema.minLength !== undefined && n < schema.minLength) add('min_length')
- if (schema.maxLength !== undefined && n > schema.maxLength) add('max_length')
- if (schema.pattern && !safeRegExp(schema.pattern)?.test(v)) add('pattern')
- if (schema.format === 'uri' && !isAbsoluteURL(v)) add('format')
- if (schema.format === 'email' && !/^[^\s@]+@[^\s@]+$/.test(v)) add('format')
- break
- }
- case 'number':
- case 'integer': {
- if (typeof v !== 'number' || Number.isNaN(v)) return add('type')
- if (schema.type === 'integer' && !Number.isInteger(v)) return add('type')
- if (schema.minimum !== undefined && v < schema.minimum) add('minimum')
- if (schema.maximum !== undefined && v > schema.maximum) add('maximum')
- break
- }
- case 'boolean':
- if (typeof v !== 'boolean') return add('type')
- break
- }
- // x-options choices are the plugin's to check.
- const choices = schema['x-options'] ? [] : choicesOf(schema)
- if (choices.length && !choices.some(c => looselyEqual(c.value, v))) add('enum')
-}
-
-function isAbsoluteURL(v: string): boolean {
- try {
- const u = new URL(v)
- return !!u.protocol && !!u.host
- } catch {
- return false
- }
-}
-
-function safeRegExp(pattern: string): RegExp | null {
- try {
- return new RegExp(pattern)
- } catch {
- return null
- }
-}
-
-export function isPlainObject(v: unknown): v is ConfigValue {
- return typeof v === 'object' && v !== null && !Array.isArray(v)
-}
-
-function looselyEqual(a: unknown, b: unknown): boolean {
- if (typeof a === 'number' || typeof b === 'number') return Number(a) === Number(b) && a !== '' && b !== ''
- if (isPlainObject(a) || Array.isArray(a)) return JSON.stringify(a) === JSON.stringify(b)
- return a === b
-}
diff --git a/frontend/src/components/schema-form/source.test.ts b/frontend/src/components/schema-form/source.test.ts
deleted file mode 100644
index 7a76d0f8f..000000000
--- a/frontend/src/components/schema-form/source.test.ts
+++ /dev/null
@@ -1,63 +0,0 @@
-import assert from 'node:assert/strict'
-import test from 'node:test'
-
-import { isOAuthRef, validateConfig, type ConfigSchema } from './schema'
-import { dependencyKey, isOAuthResult, optionsFailure, valueAt, type SchemaFormSource } from './source'
-
-const schema: ConfigSchema = {
- type: 'object',
- required: ['account'],
- properties: {
- account: { type: 'string', 'x-oauth': { authorizeUrl: 'https://auth.example.com/authorize' } },
- project: { type: 'string', enum: ['a'], 'x-options': { name: 'projects', dependsOn: ['account'] } },
- },
-}
-
-test('x-oauth fields hold a connection reference; x-options choices are the plugin\'s to check', () => {
- assert.equal(isOAuthRef('oauth:0b7c'), true)
- for (const v of ['oauth:', 'token', 1, undefined]) assert.equal(isOAuthRef(v), false)
- assert.deepEqual(validateConfig(schema, { account: 'oauth:1', project: 'listed-by-plugin' }), [])
- assert.deepEqual(validateConfig(schema, {}), [{ path: 'account', code: 'required' }])
- assert.deepEqual(validateConfig(schema, { account: 'a-token' }), [{ path: 'account', code: 'format' }])
-})
-
-test('OAuth results count only from the popup, the callback origin and the same state', () => {
- const popup = {}
- const start = { authorizeUrl: 'https://auth.example.com', state: 's1', redirectUri: 'https://wk.example.com/api/v1/plugin-oauth/callback' }
- const data = { type: 'weknora-plugin-oauth', state: 's1', ok: true, connection: 'oauth:1' }
- const ev = { data, origin: 'https://wk.example.com', source: popup }
- assert.equal(isOAuthResult(ev, popup, start), true)
- assert.equal(isOAuthResult({ ...ev, source: {} }, popup, start), false)
- assert.equal(isOAuthResult({ ...ev, origin: 'https://evil.example.com' }, popup, start), false)
- assert.equal(isOAuthResult({ ...ev, data: { ...data, state: 's2' } }, popup, start), false)
- assert.equal(isOAuthResult({ ...ev, data: { ...data, type: 'other' } }, popup, start), false)
- assert.equal(isOAuthResult({ ...ev, data: null }, popup, start), false)
- assert.equal(isOAuthResult(ev, popup, { ...start, redirectUri: 'not a url' }), false)
-})
-
-test('choices reload only when the fields they depend on change', () => {
- const values: Record<string, unknown> = { auth: { site: 'a' }, other: 1 }
- const source: SchemaFormSource = {
- dependency: path => valueAt(values, path),
- options: async () => [],
- startOAuth: async () => ({ authorizeUrl: '', state: '', redirectUri: '' }),
- }
- const spec = { name: 'projects', dependsOn: ['auth.site', 'missing'] }
- const before = dependencyKey(source, spec)
- values.other = 2
- assert.equal(dependencyKey(source, spec), before)
- values.auth = { site: 'b' }
- assert.notEqual(dependencyKey(source, spec), before)
- assert.equal(dependencyKey(source, { name: 'projects' }), '')
- assert.equal(dependencyKey(undefined, spec), '')
- assert.equal(valueAt({ a: [1] }, 'a.0'), undefined)
-})
-
-test('a plugin asking for more input is a hint, other failures are errors', () => {
- assert.deepEqual(optionsFailure({ status: 400, message: 'connect first', error: { code: 'invalid_config', message: 'connect first' } }),
- { hint: 'connect first' })
- assert.deepEqual(optionsFailure({ status: 400, message: 'bad', error: { code: 'unauthorized', message: 'token expired' } }),
- { error: 'token expired' })
- assert.deepEqual(optionsFailure({ status: 502, message: 'plugin down' }), { error: 'plugin down' })
- assert.deepEqual(optionsFailure(null), { error: '' })
-})
diff --git a/frontend/src/components/schema-form/source.ts b/frontend/src/components/schema-form/source.ts
deleted file mode 100644
index 6afe85464..000000000
--- a/frontend/src/components/schema-form/source.ts
+++ /dev/null
@@ -1,105 +0,0 @@
-import { inject, provide, type InjectionKey } from 'vue'
-
-import type { OptionsSource } from './schema'
-
-/** One choice a plugin offers for an x-options field. */
-export interface FieldOption {
- value: string
- label?: string
- description?: string
-}
-
-/** What starting an OAuth connection returned. */
-export interface OAuthStart {
- authorizeUrl: string
- state: string
- /** Where the provider sends the browser back; its origin posts the result. */
- redirectUri: string
-}
-
-/**
- * Where plugin-backed widgets (x-options, x-oauth) get their data. The page
- * hosting a plugin form provides one; without it those fields fall back to
- * plain inputs.
- */
-export interface SchemaFormSource {
- /** Current value of a field another depends on (x-options dependsOn). */
- dependency(path: string): unknown
- options(field: string, source: OptionsSource, query: string): Promise<FieldOption[]>
- startOAuth(field: string): Promise<OAuthStart>
-}
-
-const key: InjectionKey<SchemaFormSource> = Symbol('schema-form-source')
-
-export function provideSchemaFormSource(source: SchemaFormSource) {
- provide(key, source)
-}
-
-export function useSchemaFormSource(): SchemaFormSource | undefined {
- return inject(key, undefined)
-}
-
-/** The OAuth result the callback page posts to the form that opened it. */
-export interface OAuthMessage {
- type: 'weknora-plugin-oauth'
- state: string
- ok: boolean
- connection?: string
- error?: string
-}
-
-/**
- * Whether a message event is the result of this authorization: from the
- * popup, from the callback page's origin, for this state.
- */
-export function isOAuthResult(
- ev: { data: unknown; origin: string; source: unknown },
- popup: unknown,
- start: OAuthStart,
-): ev is { data: OAuthMessage; origin: string; source: unknown } {
- let origin = ''
- try {
- origin = new URL(start.redirectUri).origin
- } catch {
- return false
- }
- const d = ev.data as Partial<OAuthMessage> | null
- return (
- ev.source === popup &&
- ev.origin === origin &&
- !!d &&
- d.type === 'weknora-plugin-oauth' &&
- d.state === start.state
- )
-}
-
-/**
- * Sorts a failed options request: a plugin answering invalid_config says the
- * form lacks what the list needs (no token yet), which the form shows as a
- * hint; anything else is an error worth retrying.
- */
-export function optionsFailure(e: unknown): { hint?: string; error?: string } {
- const err = e as { message?: string; error?: { code?: string; message?: string } } | null
- const message = err?.error?.message || err?.message || ''
- if (err?.error?.code === 'invalid_config') return { hint: message }
- return { error: message }
-}
-
-/** The values dependsOn names, keyed by path, for reload decisions. */
-export function dependencyKey(source: SchemaFormSource | undefined, spec: OptionsSource | undefined): string {
- if (!source || !spec?.dependsOn?.length) return ''
- return JSON.stringify(spec.dependsOn.map(p => source.dependency(p) ?? null))
-}
-
-/**
- * Looks a dotted path up in an object ("auth.site"); for forms that keep
- * their values in one object.
- */
-export function valueAt(values: Record<string, unknown> | undefined, path: string): unknown {
- let v: unknown = values
- for (const k of path.split('.')) {
- if (typeof v !== 'object' || v === null || Array.isArray(v)) return undefined
- v = (v as Record<string, unknown>)[k]
- }
- return v
-}
diff --git a/frontend/src/components/schema-form/useSchemaText.ts b/frontend/src/components/schema-form/useSchemaText.ts
deleted file mode 100644
index eb4f870d0..000000000
--- a/frontend/src/components/schema-form/useSchemaText.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-import { computed } from 'vue'
-import { useI18n } from 'vue-i18n'
-
-import { resolveText, type ConfigSchema, type TextSlot, type Translator } from './schema'
-
-/** Binds resolveText to the active vue-i18n locale. */
-export function useSchemaText() {
- const { t, te, locale } = useI18n()
- const tr = computed<Translator>(() => ({
- t: key => t(key),
- te: key => te(key),
- locale: locale.value,
- }))
- return (schema: ConfigSchema, slot: TextSlot) => resolveText(schema, slot, tr.value)
-}
diff --git a/frontend/src/components/schema-form/widgets/HeadersWidget.vue b/frontend/src/components/schema-form/widgets/HeadersWidget.vue
deleted file mode 100644
index 9d65d548c..000000000
--- a/frontend/src/components/schema-form/widgets/HeadersWidget.vue
+++ /dev/null
@@ -1,116 +0,0 @@
-<template>
- <div class="headers-widget">
- <div v-for="(row, idx) in rows" :key="idx" class="headers-widget__row">
- <t-input
- v-model="row.key"
- class="headers-widget__key"
- :placeholder="t('model.editor.customHeadersKeyPlaceholder')"
- :disabled="disabled"
- autocomplete="off"
- spellcheck="false"
- @change="emitValue"
- />
- <t-input
- v-model="row.value"
- class="headers-widget__value"
- :placeholder="t('model.editor.customHeadersValuePlaceholder')"
- :disabled="disabled"
- autocomplete="off"
- spellcheck="false"
- @change="emitValue"
- />
- <t-button
- variant="text"
- shape="square"
- size="small"
- class="headers-widget__remove"
- :aria-label="t('common.delete')"
- :disabled="disabled"
- @click="remove(idx)"
- >
- <t-icon name="close" />
- </t-button>
- </div>
- <t-button variant="text" size="small" theme="primary" :disabled="disabled" @click="add">
- <template #icon><t-icon name="add" /></template>
- {{ t('model.editor.customHeadersAdd') }}
- </t-button>
- </div>
-</template>
-
-<script setup lang="ts">
-import { ref, watch } from 'vue'
-import { useI18n } from 'vue-i18n'
-
-import { parseHeaders, serializeHeaders, type HeaderRow } from './headers'
-import type { WidgetProps } from './types'
-
-// Edits "Name: Value" lines (RSS auth headers) as key/value rows. Rows stay
-// local so a half-typed row without a name survives; only named rows reach
-// the stored value.
-const props = defineProps<WidgetProps>()
-const emit = defineEmits<{ 'update:modelValue': [value: unknown] }>()
-const { t } = useI18n()
-
-const rows = ref<HeaderRow[]>(parseHeaders(props.modelValue))
-
-watch(
- () => props.modelValue,
- v => {
- if ((v ?? '') !== serializeHeaders(rows.value)) rows.value = parseHeaders(v)
- },
-)
-
-function emitValue() {
- const text = serializeHeaders(rows.value)
- emit('update:modelValue', text || undefined)
-}
-
-function add() {
- rows.value.push({ key: '', value: '' })
-}
-
-function remove(idx: number) {
- rows.value.splice(idx, 1)
- emitValue()
-}
-</script>
-
-<style lang="less" scoped>
-.headers-widget {
- display: flex;
- flex-direction: column;
- align-items: flex-start;
- gap: 8px;
-}
-
-.headers-widget__row {
- display: flex;
- align-items: center;
- gap: 8px;
- width: 100%;
-}
-
-.headers-widget__key {
- flex: 0 0 38%;
-}
-
-.headers-widget__value {
- flex: 1;
-}
-
-.headers-widget__remove {
- flex-shrink: 0;
- width: 32px;
- height: 32px;
- padding: 0;
- color: var(--td-text-color-placeholder);
- border-radius: var(--app-radius-sm);
- transition: all var(--app-motion-fast) ease;
-
- &:hover {
- background: var(--td-error-color-light);
- color: var(--td-error-color);
- }
-}
-</style>
diff --git a/frontend/src/components/schema-form/widgets/NumberWidget.vue b/frontend/src/components/schema-form/widgets/NumberWidget.vue
deleted file mode 100644
index f54438227..000000000
--- a/frontend/src/components/schema-form/widgets/NumberWidget.vue
+++ /dev/null
@@ -1,38 +0,0 @@
-<template>
- <t-input-number
- :model-value="numeric"
- :placeholder="placeholder"
- :disabled="disabled"
- :min="schema.minimum"
- :max="schema.maximum"
- :decimal-places="schema.type === 'integer' ? 0 : undefined"
- theme="normal"
- @update:model-value="onChange"
- />
-</template>
-
-<script setup lang="ts">
-import { computed } from 'vue'
-
-import type { WidgetProps } from './types'
-
-const props = defineProps<WidgetProps>()
-const emit = defineEmits<{ 'update:modelValue': [value: unknown] }>()
-
-// A string-typed field (model vendor extra_config is a map of strings) keeps
-// storing strings; the widget only changes how it is edited.
-const numeric = computed(() => {
- const v = props.modelValue
- if (v === undefined || v === null || v === '') return undefined
- const n = Number(v)
- return Number.isNaN(n) ? undefined : n
-})
-
-function onChange(v: number | string | undefined) {
- if (v === undefined || v === null || v === '') {
- emit('update:modelValue', undefined)
- return
- }
- emit('update:modelValue', props.schema.type === 'string' ? String(v) : Number(v))
-}
-</script>
diff --git a/frontend/src/components/schema-form/widgets/OAuthWidget.vue b/frontend/src/components/schema-form/widgets/OAuthWidget.vue
deleted file mode 100644
index c517ac36a..000000000
--- a/frontend/src/components/schema-form/widgets/OAuthWidget.vue
+++ /dev/null
@@ -1,126 +0,0 @@
-<template>
- <div class="oauth-widget">
- <div class="oauth-widget__row">
- <span class="oauth-widget__status" :class="{ 'is-connected': connected }">
- <t-icon :name="connected ? 'check-circle-filled' : 'link'" />
- {{ connected ? t('schemaForm.oauth.connected') : t('schemaForm.oauth.notConnected') }}
- </span>
- <t-button
- size="small"
- :variant="connected ? 'outline' : 'base'"
- theme="primary"
- :loading="busy"
- :disabled="disabled || !source"
- @click="connect"
- >
- {{ connected ? t('schemaForm.oauth.reconnect') : t('schemaForm.oauth.connect') }}
- </t-button>
- <t-button v-if="connected && !required" size="small" variant="text" :disabled="disabled || busy" @click="clear">
- {{ t('schemaForm.oauth.disconnect') }}
- </t-button>
- </div>
- <p v-if="error" class="oauth-widget__error">{{ error }}</p>
- </div>
-</template>
-
-<script setup lang="ts">
-import { computed, onBeforeUnmount, ref } from 'vue'
-import { useI18n } from 'vue-i18n'
-
-import { isOAuthRef } from '../schema'
-import { isOAuthResult, useSchemaFormSource, type OAuthStart } from '../source'
-import type { WidgetProps } from './types'
-
-// Connects an account through the plugin's OAuth app (x-oauth). The popup
-// runs the provider's consent screen; WeKnora keeps the tokens and the field
-// only holds "oauth:<connection>".
-const props = defineProps<WidgetProps>()
-const emit = defineEmits<{ 'update:modelValue': [value: unknown] }>()
-const { t } = useI18n()
-
-const source = useSchemaFormSource()
-const connected = computed(() => isOAuthRef(props.modelValue))
-const busy = ref(false)
-const error = ref('')
-let cleanup: (() => void) | undefined
-
-async function connect() {
- if (!source) return
- cleanup?.()
- error.value = ''
- // Open the window inside the click so popup blockers let it through.
- const popup = window.open('', 'weknora-plugin-oauth', 'popup,width=560,height=720')
- if (!popup) {
- error.value = t('schemaForm.oauth.popupBlocked')
- return
- }
- busy.value = true
- let start: OAuthStart
- try {
- start = await source.startOAuth(props.path ?? '')
- } catch (e) {
- popup.close()
- busy.value = false
- error.value = (e as { message?: string })?.message || t('schemaForm.oauth.failed')
- return
- }
- popup.location.href = start.authorizeUrl
-
- const onMessage = (ev: MessageEvent) => {
- if (!isOAuthResult(ev, popup, start)) return
- done()
- if (ev.data.ok && isOAuthRef(ev.data.connection)) emit('update:modelValue', ev.data.connection)
- else error.value = ev.data.error || t('schemaForm.oauth.failed')
- }
- const closed = setInterval(() => {
- if (popup.closed) done()
- }, 500)
- const done = () => {
- window.removeEventListener('message', onMessage)
- clearInterval(closed)
- busy.value = false
- cleanup = undefined
- }
- cleanup = () => {
- done()
- if (!popup.closed) popup.close()
- }
- window.addEventListener('message', onMessage)
-}
-
-function clear() {
- error.value = ''
- emit('update:modelValue', undefined)
-}
-
-onBeforeUnmount(() => cleanup?.())
-</script>
-
-<style lang="less" scoped>
-.oauth-widget__row {
- display: flex;
- align-items: center;
- gap: 8px;
- flex-wrap: wrap;
-}
-
-.oauth-widget__status {
- display: inline-flex;
- align-items: center;
- gap: 4px;
- margin-right: auto;
- color: var(--td-text-color-secondary);
- font-size: var(--app-text-md);
-
- &.is-connected {
- color: var(--td-success-color);
- }
-}
-
-.oauth-widget__error {
- margin: 4px 0 0;
- color: var(--td-error-color);
- font-size: var(--app-text-sm);
- line-height: 1.5;
-}
-</style>
diff --git a/frontend/src/components/schema-form/widgets/OptionsWidget.vue b/frontend/src/components/schema-form/widgets/OptionsWidget.vue
deleted file mode 100644
index d31aed383..000000000
--- a/frontend/src/components/schema-form/widgets/OptionsWidget.vue
+++ /dev/null
@@ -1,177 +0,0 @@
-<template>
- <div class="options-widget">
- <t-select
- v-if="source"
- :model-value="selected"
- :options="selectOptions"
- :placeholder="placeholder"
- :disabled="disabled"
- :clearable="!required"
- :multiple="multiple"
- :loading="loading"
- filterable
- :on-search="spec?.search ? onSearch : undefined"
- @popup-visible-change="onPopup"
- @update:model-value="onChange"
- >
- <template #empty>
- <div class="options-widget__empty">{{ loading ? t('schemaForm.options.loading') : t('schemaForm.options.empty') }}</div>
- </template>
- </t-select>
- <t-input
- v-else
- :model-value="Array.isArray(modelValue) ? modelValue.join(', ') : ((modelValue as string | undefined) ?? '')"
- :placeholder="placeholder"
- :disabled="disabled"
- @update:model-value="onText"
- />
- <p v-if="hint" class="options-widget__hint">{{ hint }}</p>
- <p v-if="error" class="options-widget__error">
- {{ error }}
- <t-link theme="primary" size="small" @click="load(query)">{{ t('schemaForm.options.retry') }}</t-link>
- </p>
- </div>
-</template>
-
-<script setup lang="ts">
-import { computed, onMounted, ref, watch } from 'vue'
-import { useI18n } from 'vue-i18n'
-
-import { dependencyKey, optionsFailure, useSchemaFormSource, type FieldOption } from '../source'
-import type { WidgetProps } from './types'
-
-// Choices the plugin lists for this field (x-options), reloaded when the
-// fields they depend on change. Values the plugin no longer lists stay
-// selectable so opening a form never drops a saved choice.
-const props = defineProps<WidgetProps>()
-const emit = defineEmits<{ 'update:modelValue': [value: unknown] }>()
-const { t } = useI18n()
-
-const source = useSchemaFormSource()
-const spec = computed(() => props.schema['x-options'])
-const multiple = computed(() => props.schema.type === 'array')
-
-const options = ref<FieldOption[]>([])
-const loading = ref(false)
-const error = ref('')
-// What the plugin needs before it can list anything ("connect an account
-// first"): shown quietly, and cleared by the next load.
-const hint = ref('')
-const query = ref('')
-let loadedKey: string | null = null
-let seq = 0
-
-const selected = computed(() => {
- const v = props.modelValue
- if (multiple.value) return Array.isArray(v) ? v : []
- return typeof v === 'string' ? v : undefined
-})
-
-const selectOptions = computed(() => {
- const out = options.value.map(o => ({ value: o.value, label: o.label || o.value, title: o.description }))
- const known = new Set(out.map(o => o.value))
- const current = Array.isArray(selected.value) ? selected.value : selected.value ? [selected.value] : []
- for (const v of current) {
- if (typeof v === 'string' && !known.has(v)) out.push({ value: v, label: v, title: undefined })
- }
- return out
-})
-
-async function load(q = '') {
- if (!source || !spec.value) return
- const mine = ++seq
- loading.value = true
- error.value = ''
- hint.value = ''
- try {
- const got = await source.options(props.path ?? '', spec.value, q)
- if (mine === seq) options.value = got
- } catch (e) {
- if (mine === seq) {
- options.value = []
- const failure = optionsFailure(e)
- if (failure.hint !== undefined) hint.value = failure.hint
- else error.value = failure.error || t('schemaForm.options.failed')
- }
- } finally {
- if (mine === seq) loading.value = false
- }
-}
-
-const depKey = computed(() => dependencyKey(source, spec.value))
-
-onMounted(() => {
- loadedKey = depKey.value
- void load()
-})
-
-// Typing into a field the choices depend on reloads them once it settles.
-let timer: ReturnType<typeof setTimeout> | undefined
-watch(depKey, key => {
- clearTimeout(timer)
- timer = setTimeout(() => {
- if (key === loadedKey) return
- loadedKey = key
- void load(query.value)
- }, 400)
-})
-
-function onPopup(visible: boolean) {
- if (visible && error.value && !loading.value) void load(query.value)
-}
-
-let searchTimer: ReturnType<typeof setTimeout> | undefined
-function onSearch(q: string) {
- query.value = q
- clearTimeout(searchTimer)
- searchTimer = setTimeout(() => void load(q), 300)
-}
-
-function onChange(v: unknown) {
- if (multiple.value) {
- emit('update:modelValue', Array.isArray(v) && v.length ? v : undefined)
- return
- }
- emit('update:modelValue', v === '' || v === null ? undefined : v)
-}
-
-function onText(v: string) {
- if (!multiple.value) {
- emit('update:modelValue', v || undefined)
- return
- }
- const list = v.split(',').map(s => s.trim()).filter(Boolean)
- emit('update:modelValue', list.length ? list : undefined)
-}
-</script>
-
-<style lang="less" scoped>
-.options-widget {
- display: flex;
- flex-direction: column;
- gap: 4px;
-}
-
-.options-widget__empty {
- padding: 8px 12px;
- color: var(--td-text-color-placeholder);
- font-size: var(--app-text-sm);
-}
-
-.options-widget__hint {
- margin: 0;
- color: var(--td-text-color-placeholder);
- font-size: var(--app-text-sm);
- line-height: 1.5;
-}
-
-.options-widget__error {
- display: flex;
- align-items: center;
- gap: 8px;
- margin: 0;
- color: var(--td-error-color);
- font-size: var(--app-text-sm);
- line-height: 1.5;
-}
-</style>
diff --git a/frontend/src/components/schema-form/widgets/PasswordWidget.vue b/frontend/src/components/schema-form/widgets/PasswordWidget.vue
deleted file mode 100644
index db3d52f11..000000000
--- a/frontend/src/components/schema-form/widgets/PasswordWidget.vue
+++ /dev/null
@@ -1,19 +0,0 @@
-<template>
- <t-input
- :model-value="(modelValue as string | undefined) ?? ''"
- type="password"
- autocomplete="new-password"
- :placeholder="placeholder"
- :disabled="disabled"
- @update:model-value="$emit('update:modelValue', $event)"
- >
- <template #prefix-icon><t-icon name="lock-on" /></template>
- </t-input>
-</template>
-
-<script setup lang="ts">
-import type { WidgetProps } from './types'
-
-defineProps<WidgetProps>()
-defineEmits<{ 'update:modelValue': [value: unknown] }>()
-</script>
diff --git a/frontend/src/components/schema-form/widgets/SelectWidget.vue b/frontend/src/components/schema-form/widgets/SelectWidget.vue
deleted file mode 100644
index b0136c940..000000000
--- a/frontend/src/components/schema-form/widgets/SelectWidget.vue
+++ /dev/null
@@ -1,36 +0,0 @@
-<template>
- <t-select
- :model-value="modelValue as string | number | undefined"
- :placeholder="placeholder"
- :disabled="disabled"
- :clearable="!required"
- @update:model-value="$emit('update:modelValue', $event)"
- >
- <t-option
- v-for="choice in choices"
- :key="String(choice.value)"
- :value="choice.value as string | number"
- :label="choice.label"
- />
- </t-select>
-</template>
-
-<script setup lang="ts">
-import { computed } from 'vue'
-
-import { choicesOf } from '../schema'
-import { useSchemaText } from '../useSchemaText'
-import type { WidgetProps } from './types'
-
-const props = defineProps<WidgetProps>()
-defineEmits<{ 'update:modelValue': [value: unknown] }>()
-
-const text = useSchemaText()
-
-const choices = computed(() =>
- choicesOf(props.schema).map(c => ({
- value: c.value,
- label: text(c.schema, 'title') || String(c.value),
- })),
-)
-</script>
diff --git a/frontend/src/components/schema-form/widgets/SwitchWidget.vue b/frontend/src/components/schema-form/widgets/SwitchWidget.vue
deleted file mode 100644
index e56839ff9..000000000
--- a/frontend/src/components/schema-form/widgets/SwitchWidget.vue
+++ /dev/null
@@ -1,20 +0,0 @@
-<template>
- <t-switch :model-value="checked" :disabled="disabled" @update:model-value="onChange" />
-</template>
-
-<script setup lang="ts">
-import { computed } from 'vue'
-
-import type { WidgetProps } from './types'
-
-const props = defineProps<WidgetProps>()
-const emit = defineEmits<{ 'update:modelValue': [value: unknown] }>()
-
-// String-typed booleans ("true" / "false") are stored as strings.
-const checked = computed(() => props.modelValue === true || props.modelValue === 'true')
-
-function onChange(v: unknown) {
- const on = v === true
- emit('update:modelValue', props.schema.type === 'string' ? String(on) : on)
-}
-</script>
diff --git a/frontend/src/components/schema-form/widgets/TextWidget.vue b/frontend/src/components/schema-form/widgets/TextWidget.vue
deleted file mode 100644
index ab972b83e..000000000
--- a/frontend/src/components/schema-form/widgets/TextWidget.vue
+++ /dev/null
@@ -1,16 +0,0 @@
-<template>
- <t-input
- :model-value="(modelValue as string | undefined) ?? ''"
- :placeholder="placeholder"
- :disabled="disabled"
- :maxlength="schema.maxLength"
- @update:model-value="$emit('update:modelValue', $event)"
- />
-</template>
-
-<script setup lang="ts">
-import type { WidgetProps } from './types'
-
-defineProps<WidgetProps>()
-defineEmits<{ 'update:modelValue': [value: unknown] }>()
-</script>
diff --git a/frontend/src/components/schema-form/widgets/TextareaWidget.vue b/frontend/src/components/schema-form/widgets/TextareaWidget.vue
deleted file mode 100644
index 42125af6d..000000000
--- a/frontend/src/components/schema-form/widgets/TextareaWidget.vue
+++ /dev/null
@@ -1,17 +0,0 @@
-<template>
- <t-textarea
- :model-value="(modelValue as string | undefined) ?? ''"
- :placeholder="placeholder"
- :disabled="disabled"
- :maxlength="schema.maxLength"
- :autosize="{ minRows: 3, maxRows: 10 }"
- @update:model-value="$emit('update:modelValue', $event)"
- />
-</template>
-
-<script setup lang="ts">
-import type { WidgetProps } from './types'
-
-defineProps<WidgetProps>()
-defineEmits<{ 'update:modelValue': [value: unknown] }>()
-</script>
diff --git a/frontend/src/components/schema-form/widgets/headers.ts b/frontend/src/components/schema-form/widgets/headers.ts
deleted file mode 100644
index 73d77ea69..000000000
--- a/frontend/src/components/schema-form/widgets/headers.ts
+++ /dev/null
@@ -1,25 +0,0 @@
-export interface HeaderRow {
- key: string
- value: string
-}
-
-/** Parses "Name: Value" lines; lines without a colon keep an empty value. */
-export function parseHeaders(text: unknown): HeaderRow[] {
- if (typeof text !== 'string' || !text.trim()) return []
- return text
- .split('\n')
- .filter(line => line.trim())
- .map(line => {
- const i = line.indexOf(':')
- if (i < 0) return { key: line.trim(), value: '' }
- return { key: line.slice(0, i).trim(), value: line.slice(i + 1).trim() }
- })
-}
-
-/** Serializes rows back to "Name: Value" lines, skipping rows without a name. */
-export function serializeHeaders(rows: HeaderRow[]): string {
- return rows
- .filter(r => r.key.trim())
- .map(r => `${r.key.trim()}: ${r.value}`)
- .join('\n')
-}
diff --git a/frontend/src/components/schema-form/widgets/index.ts b/frontend/src/components/schema-form/widgets/index.ts
deleted file mode 100644
index 714962a93..000000000
--- a/frontend/src/components/schema-form/widgets/index.ts
+++ /dev/null
@@ -1,52 +0,0 @@
-import type { Component } from 'vue'
-
-import type { ConfigSchema } from '../schema'
-import HeadersWidget from './HeadersWidget.vue'
-import NumberWidget from './NumberWidget.vue'
-import OAuthWidget from './OAuthWidget.vue'
-import OptionsWidget from './OptionsWidget.vue'
-import PasswordWidget from './PasswordWidget.vue'
-import SelectWidget from './SelectWidget.vue'
-import SwitchWidget from './SwitchWidget.vue'
-import TextWidget from './TextWidget.vue'
-import TextareaWidget from './TextareaWidget.vue'
-
-/**
- * Form controls by x-widget name. Only the frontend registers widgets;
- * plugins pick one by name, and anything richer belongs in a plugin page.
- */
-const widgets = new Map<string, Component>([
- ['text', TextWidget],
- ['password', PasswordWidget],
- ['textarea', TextareaWidget],
- ['number', NumberWidget],
- ['select', SelectWidget],
- ['switch', SwitchWidget],
- // "Name: Value" lines edited as rows (RSS auth headers).
- ['headers', HeadersWidget],
- // Choices loaded from the plugin (x-options).
- ['options', OptionsWidget],
- // A connect button for an OAuth connection (x-oauth).
- ['oauth', OAuthWidget],
-])
-
-export function registerWidget(name: string, component: Component) {
- widgets.set(name, component)
-}
-
-/** The widget name for a field: x-widget when known, else inferred. */
-export function widgetName(schema: ConfigSchema): string {
- const explicit = schema['x-widget']
- if (explicit && widgets.has(explicit)) return explicit
- if (schema['x-oauth']) return 'oauth'
- if (schema['x-secret']) return 'password'
- if (schema['x-options']) return 'options'
- if (schema.oneOf?.length || schema.enum?.length) return 'select'
- if (schema.type === 'boolean') return 'switch'
- if (schema.type === 'number' || schema.type === 'integer') return 'number'
- return 'text'
-}
-
-export function resolveWidget(schema: ConfigSchema): Component {
- return widgets.get(widgetName(schema)) ?? TextWidget
-}
diff --git a/frontend/src/components/schema-form/widgets/types.ts b/frontend/src/components/schema-form/widgets/types.ts
deleted file mode 100644
index 9cfbf64b6..000000000
--- a/frontend/src/components/schema-form/widgets/types.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import type { ConfigSchema } from '../schema'
-
-/** Props every widget receives. Widgets emit update:modelValue. */
-export interface WidgetProps {
- modelValue: unknown
- /** Dotted path of the field in its form. */
- path?: string
- schema: ConfigSchema
- placeholder?: string
- disabled?: boolean
- required?: boolean
-}
diff --git a/frontend/src/config/settingsAccess.test.ts b/frontend/src/config/settingsAccess.test.ts
index 1ddc425b7..dbe44dc90 100644
--- a/frontend/src/config/settingsAccess.test.ts
+++ b/frontend/src/config/settingsAccess.test.ts
@@ -34,6 +34,6 @@ test('personal skill environment variables are visible to every member', () => {
test('system administration settings stay explicitly system-admin-only', () => {
assert.deepEqual(
[...SYSTEM_ADMIN_SETTINGS_SECTIONS],
- ['system-global', 'model-catalog', 'runtime-queues', 'platform-api-keys', 'system-audit-log', 'plugin-admin'],
+ ['system-global', 'model-catalog', 'runtime-queues', 'platform-api-keys', 'system-audit-log'],
)
})
diff --git a/frontend/src/config/settingsAccess.ts b/frontend/src/config/settingsAccess.ts
index 683bbedd1..5950c7c5c 100644
--- a/frontend/src/config/settingsAccess.ts
+++ b/frontend/src/config/settingsAccess.ts
@@ -49,5 +49,4 @@ export const SYSTEM_ADMIN_SETTINGS_SECTIONS = new Set([
'runtime-queues',
'platform-api-keys',
'system-audit-log',
- 'plugin-admin',
])
diff --git a/frontend/src/config/toolbox.test.ts b/frontend/src/config/toolbox.test.ts
index 7539cf93b..3059169fa 100644
--- a/frontend/src/config/toolbox.test.ts
+++ b/frontend/src/config/toolbox.test.ts
@@ -95,7 +95,6 @@ test('settings no longer render moved panels, and old bookmarks reach toolbox',
assert.match(router, /to\.path === '\/platform\/settings' && isToolboxSection\(to\.query\.section\)/)
assert.match(router, /toolboxLocation\(to\.query\.section/)
const page = readFileSync(new URL('../views/toolbox/Toolbox.vue', import.meta.url), 'utf8')
- // Unknown sections (and plugin pages that are gone) fall back to the first tool.
- assert.match(page, /if \(!selectedItem\.value && !selectedPage\.value && !pendingPage && fallback\) \{\s*void router\.replace\(toolboxLocation\(fallback\.key\)\)/)
+ assert.match(page, /if \(!selectedItem\.value && fallback\) void router\.replace\(toolboxLocation\(fallback\.key\)\)/)
assert.match(page, /:key="sandboxId"\s+:initial-sandbox-id="sandboxId"/)
})
diff --git a/frontend/src/extensions/SettingsNavIcon.vue b/frontend/src/extensions/SettingsNavIcon.vue
deleted file mode 100644
index 52d4adeda..000000000
--- a/frontend/src/extensions/SettingsNavIcon.vue
+++ /dev/null
@@ -1,35 +0,0 @@
-<template>
- <!-- 网络搜索:地球 -->
- <svg v-if="icon.kind === 'builtin' && icon.name === 'globe'" width="17" height="17" viewBox="0 0 18 18" fill="none"
- xmlns="http://www.w3.org/2000/svg" class="nav-icon">
- <circle cx="9" cy="9" r="7" stroke="currentColor" stroke-width="1.2" fill="none" />
- <path d="M 9 2 A 3.5 7 0 0 0 9 16" stroke="currentColor" stroke-width="1.2" fill="none" />
- <path d="M 9 2 A 3.5 7 0 0 1 9 16" stroke="currentColor" stroke-width="1.2" fill="none" />
- <line x1="2.94" y1="5.5" x2="15.06" y2="5.5" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" />
- <line x1="2.94" y1="12.5" x2="15.06" y2="12.5" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" />
- </svg>
- <!-- WeKnora Cloud:W -->
- <svg v-else-if="icon.kind === 'builtin' && icon.name === 'weknora'" width="17" height="17" viewBox="0 0 18 18"
- fill="none" xmlns="http://www.w3.org/2000/svg" class="nav-icon">
- <rect x="1.5" y="1.5" width="15" height="15" rx="3.5" stroke="currentColor" stroke-width="1.2" fill="none" />
- <path d="M4.5 5.5L6.5 12.5L9 7.5L11.5 12.5L13.5 5.5" stroke="currentColor" stroke-width="1.3"
- stroke-linecap="round" stroke-linejoin="round" fill="none" />
- </svg>
- <!-- 沙箱:隔离运行窗口 -->
- <svg v-else-if="icon.kind === 'builtin' && icon.name === 'sandbox'" width="17" height="17" viewBox="0 0 18 18"
- fill="none" xmlns="http://www.w3.org/2000/svg" class="nav-icon">
- <rect x="2.5" y="3" width="13" height="12" rx="2" stroke="currentColor" stroke-width="1.2" fill="none" />
- <path d="M2.5 6.5h13" stroke="currentColor" stroke-width="1.2" />
- <path d="M5.5 10h4M5.5 12.5h2.5" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" />
- </svg>
- <span v-else-if="icon.kind === 'emoji'" class="nav-icon nav-icon-emoji">{{ icon.value }}</span>
- <t-icon v-else :name="icon.kind === 'tdesign' ? icon.name : 'setting'" class="nav-icon" />
-</template>
-
-<script setup lang="ts">
-import type { SettingsNavIcon } from './settingsSections'
-
-// Renders a settings section's sidebar icon. Styling comes from the
-// .nav-icon rules of the settings modal.
-defineProps<{ icon: SettingsNavIcon }>()
-</script>
diff --git a/frontend/src/extensions/builtin/settings.ts b/frontend/src/extensions/builtin/settings.ts
deleted file mode 100644
index 582899c70..000000000
--- a/frontend/src/extensions/builtin/settings.ts
+++ /dev/null
@@ -1,103 +0,0 @@
-// Registers the builtin settings sections (settingsCatalog.ts) with their
-// components, the same way a plugin registers its pages.
-import type { Component } from 'vue'
-
-import i18n from '@/i18n'
-import { useDeploymentCapabilitiesStore } from '@/stores/deploymentCapabilities'
-import { hostSkillsOnly } from '@/utils/skillTarget'
-import IntegrationSettingsSection from '@/views/integrations/IntegrationSettingsSection.vue'
-import ChatHistorySettings from '@/views/settings/ChatHistorySettings.vue'
-import EnvVarSettings from '@/views/settings/EnvVarSettings.vue'
-import GeneralSettings from '@/views/settings/GeneralSettings.vue'
-import MemorySettings from '@/views/settings/MemorySettings.vue'
-import MemoryWorkspaceSettings from '@/views/settings/MemoryWorkspaceSettings.vue'
-import ModelSettings from '@/views/settings/ModelSettings.vue'
-import OllamaSettings from '@/views/settings/OllamaSettings.vue'
-import ParserEngineSettings from '@/views/settings/ParserEngineSettings.vue'
-import PluginCenter from '@/views/settings/PluginCenter.vue'
-import SandboxSettings from '@/views/settings/SandboxSettings.vue'
-import StorageBackendSettings from '@/views/settings/StorageBackendSettings.vue'
-import SystemInfo from '@/views/settings/SystemInfo.vue'
-import TenantInfo from '@/views/settings/TenantInfo.vue'
-import TenantMembers from '@/views/settings/TenantMembers.vue'
-import UserProfile from '@/views/settings/UserProfile.vue'
-import VectorStoreSettings from '@/views/settings/VectorStoreSettings.vue'
-import WebSearchSettings from '@/views/settings/WebSearchSettings.vue'
-import WeKnoraCloudSettings from '@/views/settings/WeKnoraCloudSettings.vue'
-import ModelCatalog from '@/views/system/ModelCatalog.vue'
-import PlatformAPIKeys from '@/views/system/PlatformAPIKeys.vue'
-import PluginManagement from '@/views/system/PluginManagement.vue'
-import RuntimeQueues from '@/views/system/RuntimeQueues.vue'
-import SystemAuditLog from '@/views/system/SystemAuditLog.vue'
-import SystemSettings from '@/views/system/SystemSettings.vue'
-
-import { CORE_PLUGIN_ID, settingsGroups, settingsSections } from '../settingsSections'
-import { BUILTIN_SETTINGS_GROUPS, BUILTIN_SETTINGS_SECTIONS, type BuiltinSettingsSection } from './settingsCatalog'
-
-const COMPONENTS: Record<string, Component> = {
- general: GeneralSettings,
- userprofile: UserProfile,
- mymemory: MemorySettings,
- envvars: EnvVarSettings,
- tenant: TenantInfo,
- members: TenantMembers,
- chathistory: ChatHistorySettings,
- memory: MemoryWorkspaceSettings,
- models: ModelSettings,
- ollama: OllamaSettings,
- weknoracloud: WeKnoraCloudSettings,
- vectorstore: VectorStoreSettings,
- parser: ParserEngineSettings,
- storage: StorageBackendSettings,
- sandbox: SandboxSettings,
- websearch: WebSearchSettings,
- plugins: PluginCenter,
- 'system-global': SystemSettings,
- 'model-catalog': ModelCatalog,
- 'runtime-queues': RuntimeQueues,
- 'platform-api-keys': PlatformAPIKeys,
- 'system-audit-log': SystemAuditLog,
- 'plugin-admin': PluginManagement,
- system: SystemInfo,
-}
-
-const t = (key: string) => i18n.global.t(key)
-
-/** Host-only skill deployments name the page after the host's secrets. */
-function envVarsLabel(): string {
- const caps = useDeploymentCapabilitiesStore()
- const hostSkills = hostSkillsOnly(caps.isSupported('settings.sandbox.remote'), caps.isSupported('settings.sandbox.host'))
- const hostKey = 'envVarSettings.host.title'
- return hostSkills && i18n.global.te(hostKey) ? t(hostKey) : t('envVarSettings.title')
-}
-
-function labelOf(section: BuiltinSettingsSection): () => string {
- if (section.key === 'envvars') return envVarsLabel
- return section.literal ? () => section.label : () => t(section.label)
-}
-
-let registered = false
-
-/** Registers the builtin settings groups and sections once. */
-export function registerBuiltinSettings() {
- if (registered) return
- registered = true
- for (const group of BUILTIN_SETTINGS_GROUPS) {
- settingsGroups.register({ key: group.key, order: group.order, label: () => t(group.label) })
- }
- for (const section of BUILTIN_SETTINGS_SECTIONS) {
- const tab = section.integrationTab
- settingsSections.register({
- key: section.key,
- group: section.group,
- order: section.order,
- label: labelOf(section),
- icon: section.icon,
- component: tab ? IntegrationSettingsSection : COMPONENTS[section.key],
- props: tab ? () => ({ tab }) : undefined,
- layout: section.layout,
- pluginId: CORE_PLUGIN_ID,
- access: section.access,
- })
- }
-}
diff --git a/frontend/src/extensions/builtin/settingsCatalog.ts b/frontend/src/extensions/builtin/settingsCatalog.ts
deleted file mode 100644
index 5e7a9e7f1..000000000
--- a/frontend/src/extensions/builtin/settingsCatalog.ts
+++ /dev/null
@@ -1,120 +0,0 @@
-// The builtin settings sections as data: which page sits in which sidebar
-// group, in what order, with what icon and access rules. Components are
-// attached in settings.ts, so this file stays importable under node:test.
-import { INTEGRATION_PREVIEW_ITEMS, INTEGRATION_TAB_CAPABILITY, INTEGRATION_TAB_MIN_ROLE } from '../../config/integrations'
-import { SETTINGS_SECTION_CAPABILITY, skillSettingsSupported } from '../../config/deploymentCapabilities'
-import { SETTINGS_SECTION_MIN_ROLE, SYSTEM_ADMIN_SETTINGS_SECTIONS } from '../../config/settingsAccess'
-import { integrationSectionKey } from '../../config/settingsRoute'
-
-import type { SettingsNavIcon, SettingsSection, SettingsSectionAccess } from '../settingsSections'
-
-/** A sidebar group; `label` is a locale key. */
-export interface BuiltinSettingsGroup {
- key: string
- order: number
- label: string
-}
-
-/** A builtin section; `label` is a locale key unless `literal` is set. */
-export interface BuiltinSettingsSection {
- key: string
- group: string
- order: number
- label: string
- literal?: boolean
- icon: SettingsNavIcon
- layout?: SettingsSection['layout']
- /** Integration tab rendered by IntegrationSettingsSection. */
- integrationTab?: string
- access: SettingsSectionAccess
-}
-
-/** Sidebar groups: account → workspace → models → integrations → data → plugins → system → platform. */
-export const BUILTIN_SETTINGS_GROUPS: BuiltinSettingsGroup[] = [
- { key: 'account', order: 10, label: 'settings.navGroups.account' },
- { key: 'workspace', order: 20, label: 'settings.navGroups.workspace' },
- { key: 'models_runtime', order: 30, label: 'settings.navGroups.modelsRuntime' },
- { key: 'integrations', order: 40, label: 'integrations.title' },
- { key: 'data_extensions', order: 50, label: 'settings.navGroups.dataExtensions' },
- { key: 'plugins', order: 55, label: 'pluginCenter.navGroup' },
- { key: 'system_administration', order: 60, label: 'settings.navGroups.systemAdministration' },
- { key: 'platform', order: 70, label: 'settings.navGroups.platform' },
-]
-
-const icon = (name: string): SettingsNavIcon => ({ kind: 'tdesign', name })
-
-/** Access for a workspace section, from the shared role and capability tables. */
-function access(key: string): SettingsSectionAccess {
- if (SYSTEM_ADMIN_SETTINGS_SECTIONS.has(key)) {
- return { systemAdminOnly: true, capability: SETTINGS_SECTION_CAPABILITY[key] }
- }
- return { minRole: SETTINGS_SECTION_MIN_ROLE[key] ?? 'viewer', capability: SETTINGS_SECTION_CAPABILITY[key] }
-}
-
-type Row = Omit<BuiltinSettingsSection, 'group' | 'order' | 'access'> & { access?: SettingsSectionAccess }
-
-const ROWS: Record<string, Row[]> = {
- account: [
- { key: 'general', label: 'general.title', icon: icon('setting') },
- { key: 'userprofile', label: 'userProfile.title', icon: icon('user') },
- { key: 'mymemory', label: 'memorySettings.title', icon: icon('bookmark') },
- // Label switches to envVarSettings.host.title on host-only skill setups.
- {
- key: 'envvars', label: 'envVarSettings.title', icon: icon('key'),
- access: { minRole: SETTINGS_SECTION_MIN_ROLE.envvars, supported: skillSettingsSupported },
- },
- ],
- workspace: [
- { key: 'tenant', label: 'settings.tenantInfo', icon: icon('user-circle') },
- { key: 'members', label: 'tenantMember.title', icon: icon('usergroup'), layout: 'wide' },
- { key: 'chathistory', label: 'chatHistorySettings.title', icon: icon('chat') },
- { key: 'memory', label: 'memoryWorkspaceSettings.title', icon: icon('bulletpoint') },
- ],
- models_runtime: [
- { key: 'models', label: 'settings.modelManagement', icon: icon('control-platform') },
- { key: 'ollama', label: 'Ollama', literal: true, icon: icon('server') },
- { key: 'weknoracloud', label: 'WeKnora Cloud', literal: true, icon: { kind: 'builtin', name: 'weknora' } },
- ],
- integrations: INTEGRATION_PREVIEW_ITEMS.map((item) => ({
- key: integrationSectionKey(item.key),
- label: `integrations.tabs.${item.key}`,
- icon: item.icon.type === 'icon' ? icon(item.icon.name) : { kind: 'emoji' as const, value: item.icon.value },
- layout: 'full' as const,
- integrationTab: item.key,
- access: { minRole: INTEGRATION_TAB_MIN_ROLE[item.key], capability: INTEGRATION_TAB_CAPABILITY[item.key] },
- })),
- data_extensions: [
- { key: 'vectorstore', label: 'settings.vectorStoreEngine', icon: icon('data-base') },
- { key: 'parser', label: 'settings.parserEngine', icon: icon('file-search') },
- { key: 'storage', label: 'settings.storageEngine', icon: icon('cloud') },
- { key: 'sandbox', label: 'settings.sandbox.title', icon: { kind: 'builtin', name: 'sandbox' } },
- { key: 'websearch', label: 'settings.webSearchConfig', icon: { kind: 'builtin', name: 'globe' } },
- ],
- // Everyone can see which plugins the workspace has; only admins switch them.
- plugins: [
- { key: 'plugins', label: 'pluginCenter.title', icon: icon('app') },
- ],
- system_administration: [
- { key: 'system-global', label: 'settings.system', icon: icon('server') },
- { key: 'model-catalog', label: 'modelCatalog.title', icon: icon('control-platform') },
- { key: 'runtime-queues', label: 'settings.taskQueue', icon: icon('queue') },
- { key: 'platform-api-keys', label: 'platformApiKeys.title', icon: icon('secured') },
- { key: 'system-audit-log', label: 'system.globalSettings.audit.tabLabel', icon: icon('history') },
- { key: 'plugin-admin', label: 'pluginAdmin.title', icon: icon('app') },
- ],
- platform: [
- { key: 'system', label: 'settings.versionInfo', icon: icon('info-circle') },
- ],
-}
-
-/** Every builtin section, in sidebar order within each group. */
-export const BUILTIN_SETTINGS_SECTIONS: BuiltinSettingsSection[] = Object.entries(ROWS).flatMap(([group, rows]) =>
- rows.map((row, i) => ({
- ...row,
- group,
- order: (i + 1) * 10,
- access: row.access ?? access(row.key),
- // System-admin pages use the full content width.
- layout: SYSTEM_ADMIN_SETTINGS_SECTIONS.has(row.key) ? 'full' : row.layout,
- })),
-)
diff --git a/frontend/src/extensions/builtin/toolResultViews.ts b/frontend/src/extensions/builtin/toolResultViews.ts
deleted file mode 100644
index 4ba048009..000000000
--- a/frontend/src/extensions/builtin/toolResultViews.ts
+++ /dev/null
@@ -1,83 +0,0 @@
-// The chat renderings of WeKnora's own tools, keyed by the display_type the
-// backend puts on each tool result.
-import type { Component } from 'vue'
-
-import ChunkDetail from '@/views/chat/components/tool-results/ChunkDetail.vue'
-import DatabaseQuery from '@/views/chat/components/tool-results/DatabaseQuery.vue'
-import DocumentInfo from '@/views/chat/components/tool-results/DocumentInfo.vue'
-import GraphQueryResults from '@/views/chat/components/tool-results/GraphQueryResults.vue'
-import GrepResults from '@/views/chat/components/tool-results/GrepResults.vue'
-import KnowledgeBaseList from '@/views/chat/components/tool-results/KnowledgeBaseList.vue'
-import KnowledgeChunksList from '@/views/chat/components/tool-results/KnowledgeChunksList.vue'
-import McpToolResult from '@/views/chat/components/tool-results/McpToolResult.vue'
-import PlanDisplay from '@/views/chat/components/tool-results/PlanDisplay.vue'
-import PluginToolView from '@/views/chat/components/tool-results/PluginToolView.vue'
-import ReadSkillResult from '@/views/chat/components/tool-results/ReadSkillResult.vue'
-import RelatedChunks from '@/views/chat/components/tool-results/RelatedChunks.vue'
-import SandboxFilesResult from '@/views/chat/components/tool-results/SandboxFilesResult.vue'
-import SearchResults from '@/views/chat/components/tool-results/SearchResults.vue'
-import ShellExecResult from '@/views/chat/components/tool-results/ShellExecResult.vue'
-import ThinkingDisplay from '@/views/chat/components/tool-results/ThinkingDisplay.vue'
-import WebFetchResults from '@/views/chat/components/tool-results/WebFetchResults.vue'
-import WebSearchResults from '@/views/chat/components/tool-results/WebSearchResults.vue'
-import WikiEditResult from '@/views/chat/components/tool-results/WikiEditResult.vue'
-import WriteSandboxFileResult from '@/views/chat/components/tool-results/WriteSandboxFileResult.vue'
-
-import { CORE_PLUGIN_ID } from '../registry'
-import { toolResultViews, type ToolResultView } from '../toolResultViews'
-
-type View = Omit<ToolResultView, 'key' | 'pluginId'>
-
-const dataOnly = (component: Component): View => ({ component })
-
-const VIEWS: Record<string, View> = {
- search_results: { component: SearchResults, props: (c) => ({ data: c.data, arguments: c.arguments }) },
- chunk_detail: dataOnly(ChunkDetail),
- related_chunks: dataOnly(RelatedChunks),
- knowledge_base_list: dataOnly(KnowledgeBaseList),
- document_info: dataOnly(DocumentInfo),
- graph_query_results: dataOnly(GraphQueryResults),
- thinking: dataOnly(ThinkingDisplay),
- plan: dataOnly(PlanDisplay),
- database_query: dataOnly(DatabaseQuery),
- web_search_results: dataOnly(WebSearchResults),
- web_fetch_results: dataOnly(WebFetchResults),
- grep_results: dataOnly(GrepResults),
- knowledge_chunks_list: dataOnly(KnowledgeChunksList),
- wiki_write_page: dataOnly(WikiEditResult),
- wiki_replace_text: dataOnly(WikiEditResult),
- wiki_rename_page: dataOnly(WikiEditResult),
- wiki_delete_page: dataOnly(WikiEditResult),
- shell_exec: {
- component: ShellExecResult,
- props: (c) => ({ data: c.data, output: c.output, arguments: c.arguments }),
- },
- list_sandbox_files: dataOnly(SandboxFilesResult),
- write_sandbox_file: dataOnly(WriteSandboxFileResult),
- edit_sandbox_file: dataOnly(WriteSandboxFileResult),
- read_skill: dataOnly(ReadSkillResult),
- mcp_discovery: {
- component: McpToolResult,
- props: (c) => ({ discovery: true, data: c.data, output: c.output, arguments: c.arguments, success: c.success }),
- },
- // Results of plugin tools, shown with the view the plugin declared.
- plugin_tool_view: {
- component: PluginToolView,
- props: (c) => ({ data: c.data, output: c.output, arguments: c.arguments, success: c.success }),
- },
- mcp_call: {
- component: McpToolResult,
- props: (c) => ({ discovery: false, data: c.data, output: c.output, arguments: c.arguments, success: c.success }),
- },
-}
-
-let registered = false
-
-/** Registers the builtin tool result views once. */
-export function registerBuiltinToolResultViews() {
- if (registered) return
- registered = true
- for (const [key, view] of Object.entries(VIEWS)) {
- toolResultViews.register({ key, pluginId: CORE_PLUGIN_ID, ...view })
- }
-}
diff --git a/frontend/src/extensions/pluginFrame/PluginFrame.vue b/frontend/src/extensions/pluginFrame/PluginFrame.vue
deleted file mode 100644
index 539b02a2f..000000000
--- a/frontend/src/extensions/pluginFrame/PluginFrame.vue
+++ /dev/null
@@ -1,192 +0,0 @@
-<template>
- <div class="plugin-frame" :class="{ 'plugin-frame--fill': fill }">
- <iframe
- ref="frame"
- :key="src"
- class="plugin-frame__iframe"
- :src="src"
- :title="title"
- sandbox="allow-scripts allow-forms allow-popups allow-popups-to-escape-sandbox"
- referrerpolicy="no-referrer"
- :style="fill ? undefined : { height: `${height}px` }"
- />
- <div v-if="!ready" class="plugin-frame__status">
- <t-loading v-if="!stalled" size="small" />
- <span v-else>{{ t('pluginPages.notResponding') }}</span>
- </div>
- </div>
-</template>
-
-<script setup lang="ts">
-import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
-import { useI18n } from 'vue-i18n'
-import { useRouter } from 'vue-router'
-import { DialogPlugin, MessagePlugin } from 'tdesign-vue-next'
-
-import { pluginPageRequest } from '@/api/plugin'
-import { useAuthStore } from '@/stores/auth'
-import { getApiBaseUrl } from '@/utils/api-base'
-import { localizedText } from '@/utils/localizedText'
-
-import { BridgeCallError, createBridgeHost, readTheme, type BridgeInit } from './bridgeHost'
-import { pageFileUrl, type FramePage } from './pluginPages'
-
-// One plugin page in a sandboxed iframe: an opaque origin (no
-// allow-same-origin), so it cannot read WeKnora's storage or call its API.
-// It talks to the app only through the bridge, which answers messages from
-// this iframe alone. Links it opens in a new window leave the sandbox: the
-// new window has its own origin and no way back into the app.
-const props = withDefaults(
- defineProps<{
- page: FramePage
- /** What the mount tells the page, e.g. { knowledgeBaseId }. */
- context?: Record<string, unknown>
- /** Fill the container instead of growing with the page's content. */
- fill?: boolean
- }>(),
- { context: () => ({}), fill: false },
-)
-const emit = defineEmits<{ close: [] }>()
-
-const { t, locale } = useI18n()
-const auth = useAuthStore()
-const router = useRouter()
-const frame = ref<HTMLIFrameElement | null>(null)
-const height = ref(240)
-const ready = ref(false)
-const stalled = ref(false)
-
-const src = computed(() => pageFileUrl(getApiBaseUrl(), props.page))
-const title = computed(() => localizedText(props.page.name, locale.value))
-
-const initData = (): BridgeInit => ({
- pluginId: props.page.pluginId,
- version: props.page.version,
- mount: props.page.mount,
- locale: locale.value,
- role: auth.canAccessAllTenants ? 'admin' : auth.currentTenantRole || 'viewer',
- theme: readTheme(),
- context: { ...props.context },
-})
-
-const host = createBridgeHost({
- frame: () => frame.value?.contentWindow,
- init: () => {
- ready.value = true
- return initData()
- },
- handlers: {
- async apiRequest({ method, path, body }) {
- try {
- const res = await pluginPageRequest(props.page.pluginId, { mount: props.page.mount, method, path, body })
- return { status: res.data.status, body: res.data.body ?? null }
- } catch (e: any) {
- throw new BridgeCallError(e?.message || t('pluginPages.requestFailed'), e?.status)
- }
- },
- toast(message, theme) {
- MessagePlugin[theme](message)
- },
- confirm(message, heading) {
- return new Promise<boolean>((resolve) => {
- const dialog = DialogPlugin.confirm({
- header: heading || title.value,
- body: message,
- onConfirm: () => {
- dialog.destroy()
- resolve(true)
- },
- onClose: () => {
- dialog.destroy()
- resolve(false)
- },
- })
- })
- },
- navigate(path) {
- void router.push(path)
- },
- resize(h) {
- height.value = Math.max(h, 48)
- },
- close() {
- emit('close')
- },
- },
-})
-
-const onMessage = (event: MessageEvent) => host.handle(event)
-let themeObserver: MutationObserver | null = null
-let stallTimer: ReturnType<typeof setTimeout> | undefined
-
-// A page that never says hello (a script error, a blocked module) would
-// spin forever; say so after a while.
-function watchStall() {
- ready.value = false
- stalled.value = false
- clearTimeout(stallTimer)
- stallTimer = setTimeout(() => {
- stalled.value = !ready.value
- }, 10_000)
-}
-
-onMounted(() => {
- window.addEventListener('message', onMessage)
- themeObserver = new MutationObserver(() => host.send('theme', readTheme()))
- themeObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['theme-mode'] })
- watchStall()
-})
-
-onBeforeUnmount(() => {
- window.removeEventListener('message', onMessage)
- themeObserver?.disconnect()
- clearTimeout(stallTimer)
-})
-
-watch(src, watchStall)
-watch(locale, (value) => host.send('locale', value))
-// A mount whose context changes (another knowledge base) re-initialises the
-// page with it.
-watch(
- () => JSON.stringify(props.context),
- () => {
- if (ready.value) host.send('init', initData())
- },
-)
-</script>
-
-<style lang="less" scoped>
-.plugin-frame {
- position: relative;
- width: 100%;
-
- &--fill {
- flex: 1;
- min-height: 0;
- display: flex;
- }
-
- &__iframe {
- display: block;
- width: 100%;
- border: none;
- background: transparent;
- }
-
- &--fill &__iframe {
- flex: 1;
- height: 100%;
- }
-
- &__status {
- position: absolute;
- inset: 0;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: var(--app-text-sm);
- color: var(--td-text-color-placeholder);
- pointer-events: none;
- }
-}
-</style>
diff --git a/frontend/src/extensions/pluginFrame/bridgeHost.test.ts b/frontend/src/extensions/pluginFrame/bridgeHost.test.ts
deleted file mode 100644
index 933eddfa2..000000000
--- a/frontend/src/extensions/pluginFrame/bridgeHost.test.ts
+++ /dev/null
@@ -1,113 +0,0 @@
-import assert from 'node:assert/strict'
-import test from 'node:test'
-
-// The page's side of the bridge, as plugins ship it.
-import { BridgeError, connect } from '../../../../packages/plugin-ui/index.js'
-import { createBridgeHost, createRateLimiter, isAppPath, type BridgeHandlers } from './bridgeHost'
-
-type Listener = (event: { data: unknown; source: unknown }) => void
-
-/** A window that other windows post to; `source` is set by the pairing. */
-class FakeWindow {
- listeners: Listener[] = []
- parent: FakeWindow = this
- from: FakeWindow | null = null
- document = undefined
- addEventListener(_type: string, fn: Listener) {
- this.listeners.push(fn)
- }
- postMessage(data: unknown) {
- const source = this.from
- const copy = structuredClone(data)
- setTimeout(() => this.listeners.forEach((fn) => fn({ data: copy, source })), 0)
- }
-}
-
-function setup(overrides: Partial<BridgeHandlers> = {}, burst = 30) {
- const app = new FakeWindow()
- const frame = new FakeWindow()
- frame.parent = app
- app.from = frame // what reaches the app comes from the frame
- frame.from = app
- const calls: string[] = []
- const handlers: BridgeHandlers = {
- apiRequest: async (p) => {
- calls.push(`api ${p.method} ${p.path} ${JSON.stringify(p.body ?? null)}`)
- return p.path === '/missing' ? { status: 404, body: { error: 'no such link' } } : { status: 200, body: { ok: true } }
- },
- toast: (m, t) => calls.push(`toast ${t} ${m}`),
- confirm: async (m) => m === 'yes?',
- navigate: (p) => calls.push(`navigate ${p}`),
- resize: (h) => calls.push(`resize ${h}`),
- close: () => calls.push('close'),
- ...overrides,
- }
- const host = createBridgeHost({
- frame: () => frame as unknown as Window,
- init: () => ({
- pluginId: 'acme.links', version: '1.0.0', mount: 'pages/links', locale: 'zh-CN', role: 'viewer',
- theme: { mode: 'dark', tokens: { 'brand-color': '#0052d9' } }, context: { knowledgeBaseId: 'kb1' },
- }),
- handlers,
- burst,
- perSecond: 0.001,
- })
- app.addEventListener('message', (e) => host.handle(e as unknown as MessageEvent))
- return { app, frame, host, calls }
-}
-
-test('a page connects, calls its backend and the app', async () => {
- const { frame, calls } = setup()
- const wk = await connect({ window: frame as unknown as Window, autoResize: false, applyTheme: false })
- assert.equal(wk.context.mount, 'pages/links')
- assert.deepEqual(wk.context.context, { knowledgeBaseId: 'kb1' })
-
- assert.deepEqual(await wk.put('/links', { a: 1 }), { status: 200, body: { ok: true } })
- await assert.rejects(wk.get('/missing'), (e: unknown) => e instanceof BridgeError && e.status === 404 && e.message === 'no such link')
- assert.equal((await wk.get('/missing', { throwOnError: false })).status, 404)
-
- await wk.toast('saved', 'success')
- assert.equal(await wk.confirm('yes?'), true)
- assert.equal(await wk.confirm('no?'), false)
- await wk.navigate('/platform/knowledge-bases')
- await assert.rejects(wk.navigate('//evil.example'), /path of the app/)
- await wk.resize(321.4)
- await wk.close()
- assert.deepEqual(calls, [
- 'api PUT /links {"a":1}',
- 'api GET /missing null',
- 'api GET /missing null',
- 'toast success saved',
- 'navigate /platform/knowledge-bases',
- 'resize 322',
- 'close',
- ])
-})
-
-test('the app ignores other windows and rate-limits a page', async () => {
- const { app, frame, calls } = setup({}, 2)
- const wk = await connect({ window: frame as unknown as Window, autoResize: false, applyTheme: false })
-
- // A message that does not come from the plugin's frame is dropped.
- const stranger = new FakeWindow()
- app.listeners.forEach((fn) =>
- fn({ data: { weknora: 1, kind: 'request', id: 99, method: 'ui.close', params: {} }, source: stranger }),
- )
- assert.equal(calls.length, 0)
-
- await wk.toast('one')
- await wk.toast('two')
- await assert.rejects(wk.toast('three'), /too many requests/)
-})
-
-test('helpers', () => {
- assert.equal(isAppPath('/platform/x'), true)
- assert.equal(isAppPath('//x.com'), false)
- assert.equal(isAppPath('https://x.com'), false)
- let t = 0
- const allow = createRateLimiter(1, 1, () => t)
- assert.equal(allow(), true)
- assert.equal(allow(), false)
- t = 1000
- assert.equal(allow(), true)
-})
diff --git a/frontend/src/extensions/pluginFrame/bridgeHost.ts b/frontend/src/extensions/pluginFrame/bridgeHost.ts
deleted file mode 100644
index 14c661c19..000000000
--- a/frontend/src/extensions/pluginFrame/bridgeHost.ts
+++ /dev/null
@@ -1,214 +0,0 @@
-// The app's side of the plugin page bridge (packages/plugin-ui is the
-// page's side). A plugin page runs in a sandboxed iframe with an opaque
-// origin; it can only post messages to the app, and the app answers only
-// messages that come from that iframe, at a bounded rate.
-
-/** Protocol version; also marks bridge messages. */
-export const BRIDGE_PROTOCOL = 1
-
-export interface BridgeTheme {
- mode: 'light' | 'dark'
- tokens: Record<string, string>
-}
-
-/** What the page receives in `init`. */
-export interface BridgeInit {
- pluginId: string
- version: string
- mount: string
- locale: string
- /** The user's workspace role, for what the page offers; the server decides. */
- role: string
- theme: BridgeTheme
- context: Record<string, unknown>
-}
-
-export interface ApiResult {
- status: number
- body: unknown
-}
-
-/** What the app does for each call a page makes. */
-export interface BridgeHandlers {
- apiRequest(params: { method: string; path: string; body?: unknown }): Promise<ApiResult>
- toast(message: string, theme: ToastTheme): void
- confirm(message: string, title?: string): Promise<boolean>
- navigate(path: string): void
- resize(height: number): void
- close(): void
-}
-
-export type ToastTheme = 'info' | 'success' | 'warning' | 'error'
-
-export interface BridgeHostOptions {
- /** The iframe's window; messages from any other source are ignored. */
- frame: () => Window | null | undefined
- init: () => BridgeInit
- handlers: BridgeHandlers
- /** Calls allowed in a burst and per second after it. */
- burst?: number
- perSecond?: number
- now?: () => number
-}
-
-interface RequestMessage {
- weknora: number
- kind: 'request'
- id: number
- method: string
- params?: unknown
-}
-
-export class BridgeCallError extends Error {
- constructor(
- message: string,
- readonly status?: number,
- ) {
- super(message)
- }
-}
-
-const MAX_TOAST = 300
-const MAX_HEIGHT = 20_000
-const TOAST_THEMES = new Set<ToastTheme>(['info', 'success', 'warning', 'error'])
-
-function isObject(v: unknown): v is Record<string, unknown> {
- return v !== null && typeof v === 'object' && !Array.isArray(v)
-}
-
-/** A path inside the app: "/platform/...", never another origin. */
-export function isAppPath(path: unknown): path is string {
- return typeof path === 'string' && path.startsWith('/') && !path.startsWith('//') && !path.includes('\\')
-}
-
-/** A token bucket: `burst` calls at once, refilled at `perSecond`. */
-export function createRateLimiter(burst: number, perSecond: number, now: () => number = Date.now) {
- let tokens = burst
- let last = now()
- return () => {
- const t = now()
- tokens = Math.min(burst, tokens + ((t - last) / 1000) * perSecond)
- last = t
- if (tokens < 1) return false
- tokens -= 1
- return true
- }
-}
-
-export interface BridgeHost {
- /** Feed every window `message` event here. */
- handle(event: MessageEvent): void
- /** Sends an event to the page (init, theme, locale). */
- send(name: string, data: unknown): void
-}
-
-export function createBridgeHost(opts: BridgeHostOptions): BridgeHost {
- const allow = createRateLimiter(opts.burst ?? 30, opts.perSecond ?? 10, opts.now)
- const post = (msg: Record<string, unknown>) => {
- // The page's origin is opaque ("null"), so it cannot be named as the
- // target; only that window receives the message.
- opts.frame()?.postMessage({ weknora: BRIDGE_PROTOCOL, ...msg }, '*')
- }
- const send = (name: string, data: unknown) => post({ kind: 'event', name, data })
- const reply = (id: number, result: unknown) => post({ kind: 'response', id, ok: true, result })
- const fail = (id: number, err: unknown) => {
- const message = err instanceof Error ? err.message : String(err)
- const status = err instanceof BridgeCallError ? err.status : undefined
- post({ kind: 'response', id, ok: false, error: { message, status } })
- }
-
- const dispatch = async (method: string, params: Record<string, unknown>): Promise<unknown> => {
- const h = opts.handlers
- switch (method) {
- case 'api.request': {
- const verb = typeof params.method === 'string' ? params.method.toUpperCase() : 'GET'
- if (!/^[A-Z]{3,7}$/.test(verb)) throw new BridgeCallError('bad method', 400)
- if (typeof params.path !== 'string' || !params.path.startsWith('/')) {
- throw new BridgeCallError('path must start with /', 400)
- }
- return h.apiRequest({ method: verb, path: params.path, body: params.body })
- }
- case 'ui.toast': {
- if (typeof params.message !== 'string' || !params.message) throw new BridgeCallError('message is required', 400)
- const theme = TOAST_THEMES.has(params.theme as ToastTheme) ? (params.theme as ToastTheme) : 'info'
- h.toast(params.message.slice(0, MAX_TOAST), theme)
- return null
- }
- case 'ui.confirm': {
- if (typeof params.message !== 'string' || !params.message) throw new BridgeCallError('message is required', 400)
- const title = typeof params.title === 'string' ? params.title.slice(0, 100) : undefined
- return h.confirm(params.message.slice(0, MAX_TOAST), title)
- }
- case 'ui.navigate':
- if (!isAppPath(params.path)) throw new BridgeCallError('path must be a path of the app', 400)
- h.navigate(params.path)
- return null
- case 'ui.resize': {
- const height = Number(params.height)
- if (!Number.isFinite(height) || height < 0) throw new BridgeCallError('height must be a number', 400)
- h.resize(Math.min(Math.ceil(height), MAX_HEIGHT))
- return null
- }
- case 'ui.close':
- h.close()
- return null
- default:
- throw new BridgeCallError(`unknown method ${method}`, 404)
- }
- }
-
- return {
- send,
- handle(event: MessageEvent) {
- const frame = opts.frame()
- if (!frame || event.source !== frame) return
- const data = event.data as unknown
- if (!isObject(data) || data.weknora !== BRIDGE_PROTOCOL) return
- if (data.kind === 'hello') {
- send('init', opts.init())
- return
- }
- if (data.kind !== 'request') return
- const msg = data as unknown as RequestMessage
- if (typeof msg.id !== 'number' || typeof msg.method !== 'string') return
- if (!allow()) {
- fail(msg.id, new BridgeCallError('too many requests', 429))
- return
- }
- dispatch(msg.method, isObject(msg.params) ? msg.params : {}).then(
- (result) => reply(msg.id, result ?? null),
- (err) => fail(msg.id, err),
- )
- },
- }
-}
-
-/** The app's design tokens a page may use, read from the root element. */
-export const THEME_TOKENS = [
- 'brand-color',
- 'brand-color-light',
- 'text-color-primary',
- 'text-color-secondary',
- 'text-color-placeholder',
- 'bg-color-page',
- 'bg-color-container',
- 'bg-color-secondarycontainer',
- 'component-border',
- 'border-level-1-color',
- 'error-color',
- 'warning-color',
- 'success-color',
- 'radius-default',
- 'font-family',
-] as const
-
-export function readTheme(doc: Document = document): BridgeTheme {
- const root = doc.documentElement
- const style = getComputedStyle(root)
- const tokens: Record<string, string> = {}
- for (const name of THEME_TOKENS) {
- const value = style.getPropertyValue(`--td-${name}`).trim()
- if (value) tokens[name] = value
- }
- return { mode: root.getAttribute('theme-mode') === 'dark' ? 'dark' : 'light', tokens }
-}
diff --git a/frontend/src/extensions/pluginFrame/pluginPages.test.ts b/frontend/src/extensions/pluginFrame/pluginPages.test.ts
deleted file mode 100644
index 29be51450..000000000
--- a/frontend/src/extensions/pluginFrame/pluginPages.test.ts
+++ /dev/null
@@ -1,44 +0,0 @@
-import assert from 'node:assert/strict'
-import test from 'node:test'
-
-import type { ContributionListing, ListedContribution } from '../../api/plugin'
-import { findPage, pageFileUrl, pagesOf } from './pluginPages'
-
-const c = (over: Partial<ListedContribution>): ListedContribution => ({
- id: 'links', name: { default: 'Links' }, pluginId: 'acme.links', qualifiedId: 'acme.links/links',
- version: '1.0.0', enabled: true, entry: 'ui/index.html', ...over,
-})
-
-const listing: ContributionListing = {
- points: [],
- contributions: {
- pages: [
- c({ id: 'b', qualifiedId: 'acme.links/b', order: 2 }),
- c({ id: 'a', qualifiedId: 'acme.links/a', order: 1, icon: 'ui/icon.svg' }),
- c({ id: 'off', qualifiedId: 'acme.links/off', enabled: false }),
- c({ id: 'noentry', qualifiedId: 'acme.links/noentry', entry: undefined }),
- ],
- settingsSections: [c({ id: 'admin', qualifiedId: 'acme.links/admin', icon: 'icon.svg' })],
- },
-}
-
-test('pagesOf keeps enabled pages in order, with role defaults', () => {
- const pages = pagesOf(listing, 'pages')
- assert.deepEqual(pages.map((p) => p.key), ['plugin:acme.links/a', 'plugin:acme.links/b'])
- assert.equal(pages[0].mount, 'pages/a')
- assert.equal(pages[0].minRole, 'viewer')
- assert.equal(pages[0].icon, 'ui/icon.svg')
-
- const [section] = pagesOf(listing, 'settingsSections')
- assert.equal(section.minRole, 'admin')
- // Only files under ui/ are served, so other icons are dropped.
- assert.equal(section.icon, undefined)
- assert.deepEqual(pagesOf(null, 'kbTabs'), [])
- assert.equal(findPage(pages, 'plugin:acme.links/b')?.id, 'b')
-})
-
-test('pageFileUrl escapes each segment', () => {
- const page = pagesOf(listing, 'pages')[0]
- assert.equal(pageFileUrl('/app', page), '/app/api/v1/plugin-ui/assets/acme.links/1.0.0/ui/index.html')
- assert.equal(pageFileUrl('', page, 'ui/a b.js'), '/api/v1/plugin-ui/assets/acme.links/1.0.0/ui/a%20b.js')
-})
diff --git a/frontend/src/extensions/pluginFrame/pluginPages.ts b/frontend/src/extensions/pluginFrame/pluginPages.ts
deleted file mode 100644
index 3ff5f4634..000000000
--- a/frontend/src/extensions/pluginFrame/pluginPages.ts
+++ /dev/null
@@ -1,77 +0,0 @@
-import type { ContributionListing, ListedContribution, PluginContribution } from '@/api/plugin'
-import type { LocalizedText } from '@/utils/localizedText'
-
-/** The points whose contributions are sandboxed plugin pages. */
-export const PAGE_POINTS = ['pages', 'settingsSections', 'kbTabs'] as const
-export type PagePoint = (typeof PAGE_POINTS)[number]
-
-export type PageRole = NonNullable<PluginContribution['minRole']>
-
-/** One plugin page the workspace can show. */
-export interface PluginPage {
- /** Unique across mounts: "plugin:<pluginId>/<id>". */
- key: string
- pluginId: string
- version: string
- id: string
- qualifiedId: string
- point: PagePoint
- /** What the page's requests name: "<point>/<id>". */
- mount: string
- name: LocalizedText
- description?: LocalizedText
- /** Icon file under ui/, if the plugin ships one. */
- icon?: string
- entry: string
- minRole: PageRole
- order: number
-}
-
-/** What a frame needs of a page: tool result pages are not listed pages. */
-export type FramePage = Pick<PluginPage, 'pluginId' | 'version' | 'mount' | 'entry' | 'name'>
-
-/** The role a page needs when the plugin names none. */
-export function defaultMinRole(point: PagePoint): PageRole {
- return point === 'settingsSections' ? 'admin' : 'viewer'
-}
-
-function toPage(point: PagePoint, c: ListedContribution): PluginPage | null {
- if (!c.enabled || !c.entry || !c.version) return null
- return {
- key: `plugin:${c.qualifiedId}`,
- pluginId: c.pluginId,
- version: c.version,
- id: c.id,
- qualifiedId: c.qualifiedId,
- point,
- mount: `${point}/${c.id}`,
- name: c.name,
- description: c.description,
- icon: c.icon?.startsWith('ui/') ? c.icon : undefined,
- entry: c.entry,
- minRole: c.minRole ?? defaultMinRole(point),
- order: c.order ?? 0,
- }
-}
-
-/** The enabled pages of one point, in display order. */
-export function pagesOf(listing: ContributionListing | null | undefined, point: PagePoint): PluginPage[] {
- const out: PluginPage[] = []
- for (const c of listing?.contributions?.[point] ?? []) {
- const page = toPage(point, c)
- if (page) out.push(page)
- }
- return out.sort((a, b) => a.order - b.order || a.qualifiedId.localeCompare(b.qualifiedId))
-}
-
-/** The URL of a file of a plugin's pages (the entry by default). */
-export function pageFileUrl(apiBase: string, page: Pick<PluginPage, 'pluginId' | 'version' | 'entry'>, file = page.entry) {
- const seg = (s: string) => encodeURIComponent(s)
- const path = file.split('/').map(seg).join('/')
- return `${apiBase}/api/v1/plugin-ui/assets/${seg(page.pluginId)}/${seg(page.version)}/${path}`
-}
-
-/** The page a key names, among pages. */
-export function findPage(pages: readonly PluginPage[], key: string): PluginPage | undefined {
- return pages.find((p) => p.key === key)
-}
diff --git a/frontend/src/extensions/pluginFrame/settingsSync.ts b/frontend/src/extensions/pluginFrame/settingsSync.ts
deleted file mode 100644
index fb00d93f7..000000000
--- a/frontend/src/extensions/pluginFrame/settingsSync.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-import { markRaw } from 'vue'
-
-import i18n from '@/i18n'
-import { localizedText } from '@/utils/localizedText'
-
-import { settingsSections } from '../settingsSections'
-import PluginFrame from './PluginFrame.vue'
-import type { PluginPage } from './pluginPages'
-
-const registered = new Map<string, () => void>()
-
-/**
- * Makes the settings registry hold exactly these plugin sections: new ones
- * are registered under the plugins group, ones no longer enabled removed.
- */
-export function syncPluginSettingsSections(pages: readonly PluginPage[]) {
- const wanted = new Map(pages.map((p) => [p.key, p]))
- for (const [key, unregister] of registered) {
- if (!wanted.has(key)) {
- unregister()
- registered.delete(key)
- }
- }
- for (const page of pages) {
- if (registered.has(page.key)) continue
- registered.set(
- page.key,
- settingsSections.register({
- key: page.key,
- group: 'plugins',
- order: 100 + page.order,
- label: () => localizedText(page.name, i18n.global.locale.value),
- icon: { kind: 'tdesign', name: 'app' },
- component: markRaw(PluginFrame),
- props: () => ({ page }),
- pluginId: page.pluginId,
- access: { minRole: page.minRole },
- }),
- )
- }
-}
diff --git a/frontend/src/extensions/pluginToolView.test.ts b/frontend/src/extensions/pluginToolView.test.ts
deleted file mode 100644
index abd752f23..000000000
--- a/frontend/src/extensions/pluginToolView.test.ts
+++ /dev/null
@@ -1,71 +0,0 @@
-import assert from 'node:assert/strict'
-import test from 'node:test'
-
-import {
- cardsModel,
- display,
- kvModel,
- markdownSource,
- safeHref,
- tableModel,
- toolArguments,
- valueAt,
- type ToolViewSpec,
-} from './pluginToolView'
-
-const result = {
- total: 2,
- issues: [
- { key: 'ENG-1', summary: 'Login fails', url: 'https://acme.atlassian.net/browse/ENG-1', fields: { status: 'Open' }, labels: ['sso', 'web'] },
- { key: 'ENG-2', summary: 'Evil', url: 'javascript:alert(1)', fields: { status: 'Done' } },
- ],
-}
-
-test('tables take their rows from the items path and link only to http(s)', () => {
- const spec: ToolViewSpec = {
- view: 'table',
- items: 'issues',
- columns: [
- { field: 'key', link: 'url' },
- { field: 'summary', title: { default: 'Summary', 'zh-CN': '摘要' } },
- { field: 'fields.status' },
- { field: 'labels' },
- ],
- }
- const t = tableModel(spec, result, 'zh-CN')
- assert.deepEqual(t.headers, ['key', '摘要', 'fields.status', 'labels'])
- assert.deepEqual(t.rows[0], [
- { text: 'ENG-1', href: 'https://acme.atlassian.net/browse/ENG-1' },
- { text: 'Login fails', href: undefined },
- { text: 'Open', href: undefined },
- { text: 'sso, web', href: undefined },
- ])
- assert.equal(t.rows[1][0].href, undefined)
- assert.deepEqual(tableModel({ ...spec, items: 'missing' }, result, 'en-US').rows, [])
-})
-
-test('cards, kv and markdown views read their fields', () => {
- const cards = cardsModel({ view: 'cards', items: 'issues', title: 'key', subtitle: 'fields.status', link: 'url' }, result)
- assert.deepEqual(cards[0], {
- title: 'ENG-1', subtitle: 'Open', body: '', href: 'https://acme.atlassian.net/browse/ENG-1',
- })
- const kv = kvModel({ view: 'kv', items: 'issues.0', title: 'key', link: 'url' }, { issues: { 0: result.issues[0] } }, 'en-US')
- assert.equal(kv.title, 'ENG-1')
- assert.deepEqual(kv.rows.map((r) => r.label), ['summary', 'fields', 'labels'])
- assert.equal(kv.rows[1].text, '{"status":"Open"}')
- const picked = kvModel({ view: 'kv', fields: [{ field: 'total', title: { default: 'Total' } }] }, result, 'en-US')
- assert.deepEqual(picked.rows, [{ label: 'Total', text: '2', href: undefined }])
- assert.equal(markdownSource({ view: 'markdown', field: 'md' }, { md: '# Hi' }, 'text'), '# Hi')
- assert.equal(markdownSource({ view: 'markdown' }, {}, 'text'), 'text')
-})
-
-test('helpers', () => {
- assert.equal(valueAt({ a: { b: 1 } }, 'a.b'), 1)
- assert.equal(valueAt({ a: [1] }, 'a.0'), undefined)
- assert.equal(display(null), '')
- assert.equal(display(false), 'false')
- assert.equal(safeHref('ftp://x'), undefined)
- assert.deepEqual(toolArguments({ service: 's', tool: 't', arguments: { q: 1 } }), { q: 1 })
- assert.deepEqual(toolArguments({ q: 1 }), { q: 1 })
- assert.deepEqual(toolArguments(undefined), {})
-})
diff --git a/frontend/src/extensions/pluginToolView.ts b/frontend/src/extensions/pluginToolView.ts
deleted file mode 100644
index f1b191276..000000000
--- a/frontend/src/extensions/pluginToolView.ts
+++ /dev/null
@@ -1,157 +0,0 @@
-// Plugin tool result views (toolViews in plugin.yaml): a plugin maps its
-// tool's structured result onto one of these generic views, so the chat
-// can show it without running plugin code. This module is the pure part of
-// PluginToolView.vue.
-import { localizedText, type LocalizedText } from '../utils/localizedText'
-
-export type ToolViewKind = 'table' | 'cards' | 'kv' | 'markdown' | 'json' | 'page'
-
-export interface ToolViewColumn {
- field: string
- title?: LocalizedText
- link?: string
-}
-
-/** Same shape as manifest.ToolView on the backend. */
-export interface ToolViewSpec {
- view: ToolViewKind
- items?: string
- columns?: ToolViewColumn[]
- title?: string
- subtitle?: string
- body?: string
- link?: string
- fields?: ToolViewColumn[]
- field?: string
- entry?: string
-}
-
-export interface Cell {
- text: string
- href?: string
-}
-
-/** Field at a dotted path ("fields.status"); undefined when absent. */
-export function valueAt(value: unknown, path: string | undefined): unknown {
- if (!path) return value
- let v: unknown = value
- for (const key of path.split('.')) {
- if (typeof v !== 'object' || v === null || Array.isArray(v)) return undefined
- v = (v as Record<string, unknown>)[key]
- }
- return v
-}
-
-/** A value as a cell's text: scalars as they are, lists joined, objects as JSON. */
-export function display(value: unknown): string {
- if (value === undefined || value === null) return ''
- if (typeof value === 'string') return value
- if (typeof value === 'number' || typeof value === 'boolean') return String(value)
- if (Array.isArray(value) && value.every((v) => typeof v !== 'object' || v === null)) {
- return value.map(display).join(', ')
- }
- try {
- return JSON.stringify(value)
- } catch {
- return String(value)
- }
-}
-
-/** Links only to http(s): plugin data must not inject javascript: URLs. */
-export function safeHref(value: unknown): string | undefined {
- if (typeof value !== 'string') return undefined
- try {
- const u = new URL(value)
- return u.protocol === 'https:' || u.protocol === 'http:' ? u.href : undefined
- } catch {
- return undefined
- }
-}
-
-function list(structured: unknown, path: string | undefined): unknown[] {
- const v = valueAt(structured, path)
- return Array.isArray(v) ? v : []
-}
-
-export interface TableModel {
- headers: string[]
- rows: Cell[][]
-}
-
-export function tableModel(spec: ToolViewSpec, structured: unknown, locale: string): TableModel {
- const columns = spec.columns ?? []
- return {
- headers: columns.map((c) => localizedText(c.title, locale) || c.field),
- rows: list(structured, spec.items).map((item) =>
- columns.map((c) => ({ text: display(valueAt(item, c.field)), href: safeHref(valueAt(item, c.link)) })),
- ),
- }
-}
-
-export interface CardModel {
- title: string
- subtitle: string
- body: string
- href?: string
-}
-
-export function cardsModel(spec: ToolViewSpec, structured: unknown): CardModel[] {
- return list(structured, spec.items).map((item) => ({
- title: display(valueAt(item, spec.title)),
- subtitle: spec.subtitle ? display(valueAt(item, spec.subtitle)) : '',
- body: spec.body ? display(valueAt(item, spec.body)) : '',
- href: safeHref(valueAt(item, spec.link)),
- }))
-}
-
-export interface KVModel {
- title: string
- href?: string
- rows: Array<{ label: string } & Cell>
-}
-
-export function kvModel(spec: ToolViewSpec, structured: unknown, locale: string): KVModel {
- const obj = valueAt(structured, spec.items)
- const record = typeof obj === 'object' && obj !== null && !Array.isArray(obj) ? (obj as Record<string, unknown>) : {}
- const fields: ToolViewColumn[] = spec.fields?.length
- ? spec.fields
- : Object.keys(record)
- .filter((k) => k !== spec.title && k !== spec.link)
- .map((field) => ({ field }))
- return {
- title: spec.title ? display(valueAt(record, spec.title)) : '',
- href: safeHref(valueAt(record, spec.link)),
- rows: fields
- .map((f) => ({
- label: localizedText(f.title, locale) || f.field,
- text: display(valueAt(record, f.field)),
- href: safeHref(valueAt(record, f.link)),
- }))
- .filter((r) => r.text !== ''),
- }
-}
-
-/** The Markdown a markdown view shows: its field, else the text for the model. */
-export function markdownSource(spec: ToolViewSpec, structured: unknown, output: string): string {
- const v = spec.field ? valueAt(structured, spec.field) : undefined
- return typeof v === 'string' ? v : output
-}
-
-/**
- * The arguments the tool was called with. Through the call_mcp_tool proxy
- * the real arguments sit under "arguments".
- */
-export function toolArguments(args: Record<string, unknown> | undefined): Record<string, unknown> {
- const inner = args?.arguments
- if (inner && typeof inner === 'object' && !Array.isArray(inner)) return inner as Record<string, unknown>
- return args ?? {}
-}
-
-export interface PluginToolViewData {
- plugin_view?: ToolViewSpec
- plugin_id?: string
- plugin_version?: string
- mcp_server?: string
- mcp_tool?: string
- structured?: unknown
-}
diff --git a/frontend/src/extensions/registry.test.ts b/frontend/src/extensions/registry.test.ts
deleted file mode 100644
index 03f1d7cc4..000000000
--- a/frontend/src/extensions/registry.test.ts
+++ /dev/null
@@ -1,26 +0,0 @@
-import assert from 'node:assert/strict'
-import test from 'node:test'
-import { computed } from 'vue'
-
-import { createRegistry } from './registry'
-
-test('registry registers, rejects duplicates and unregisters', () => {
- const r = createRegistry<{ key: string; n: number }>('thing')
- const off = r.register({ key: 'a', n: 1 })
- r.register({ key: 'b', n: 2 })
- assert.deepEqual(r.items.map((i) => i.key), ['a', 'b'])
- assert.equal(r.get('b')?.n, 2)
- assert.throws(() => r.register({ key: 'a', n: 3 }), /thing "a" is already registered/)
- off()
- assert.deepEqual(r.items.map((i) => i.key), ['b'])
- r.unregister('missing')
- assert.equal(r.items.length, 1)
-})
-
-test('registry reads are reactive', () => {
- const r = createRegistry<{ key: string }>('thing')
- const count = computed(() => r.items.length)
- assert.equal(count.value, 0)
- r.register({ key: 'x' })
- assert.equal(count.value, 1)
-})
diff --git a/frontend/src/extensions/registry.ts b/frontend/src/extensions/registry.ts
deleted file mode 100644
index b2113bd49..000000000
--- a/frontend/src/extensions/registry.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-import { shallowReactive } from 'vue'
-
-/** Plugin ID of what every WeKnora build ships (builtin contributions). */
-export const CORE_PLUGIN_ID = 'weknora.core'
-
-/** Anything a registry holds is addressed by a unique key. */
-export interface Keyed {
- key: string
-}
-
-/**
- * An extension registry: builtins register at startup, plugins at runtime,
- * and the UI renders whatever is registered. Reads are reactive, so a plugin
- * enabled later shows up without a reload.
- */
-export interface Registry<T extends Keyed> {
- /** Adds an item; returns a function that removes it again. */
- register(item: T): () => void
- unregister(key: string): void
- get(key: string): T | undefined
- /** Every registered item, in registration order. */
- readonly items: readonly T[]
-}
-
-export function createRegistry<T extends Keyed>(name: string): Registry<T> {
- const items = shallowReactive<T[]>([])
- const unregister = (key: string) => {
- const i = items.findIndex((item) => item.key === key)
- if (i >= 0) items.splice(i, 1)
- }
- return {
- register(item) {
- if (items.some((existing) => existing.key === item.key)) {
- throw new Error(`${name} "${item.key}" is already registered`)
- }
- items.push(item)
- return () => unregister(item.key)
- },
- unregister,
- get: (key) => items.find((item) => item.key === key),
- get items() {
- return items
- },
- }
-}
diff --git a/frontend/src/extensions/settingsSections.ts b/frontend/src/extensions/settingsSections.ts
deleted file mode 100644
index 4fd3c94d9..000000000
--- a/frontend/src/extensions/settingsSections.ts
+++ /dev/null
@@ -1,101 +0,0 @@
-import type { Component } from 'vue'
-
-import type { DeploymentCapabilityKey, DeploymentCapabilityMap } from '@/config/deploymentCapabilities'
-import type { SettingsRoleKey } from '@/config/settingsAccess'
-
-import { createRegistry } from './registry'
-
-export { CORE_PLUGIN_ID } from './registry'
-
-/** How a section is drawn in the settings sidebar. */
-export type SettingsNavIcon =
- | { kind: 'tdesign'; name: string }
- | { kind: 'emoji'; value: string }
- /** Hand-drawn icons kept in SettingsNavIcon.vue. */
- | { kind: 'builtin'; name: 'globe' | 'weknora' | 'sandbox' }
-
-export interface SettingsGroup {
- key: string
- order: number
- label: () => string
-}
-
-/** Who may see a section and whether this deployment supports it. */
-export interface SettingsSectionAccess {
- minRole?: SettingsRoleKey
- systemAdminOnly?: boolean
- capability?: DeploymentCapabilityKey
- /** Overrides `capability` when support depends on several capabilities. */
- supported?: (capabilities: DeploymentCapabilityMap) => boolean
-}
-
-/** One page of the settings modal. */
-export interface SettingsSection {
- /** Also the `?section=` value in the URL. */
- key: string
- group: string
- /** Sort order within the group (lower first). */
- order: number
- label: () => string
- icon: SettingsNavIcon
- component: Component
- props?: () => Record<string, unknown>
- /** Sub-entries under the section in the sidebar. */
- children?: Array<{ key: string; label: string }>
- /** Content width: wide for tables, full for pages with their own layout. */
- layout?: 'wide' | 'full'
- pluginId: string
- access: SettingsSectionAccess
-}
-
-export const settingsGroups = createRegistry<SettingsGroup>('settings group')
-export const settingsSections = createRegistry<SettingsSection>('settings section')
-
-/** What the current user and deployment allow, for sectionAllowed. */
-export interface SettingsAccessContext {
- isSystemAdmin: boolean
- canAccessAllTenants: boolean
- hasRole: (role: SettingsRoleKey) => boolean
- isSupported: (capability?: DeploymentCapabilityKey) => boolean
- capabilities: DeploymentCapabilityMap
-}
-
-/** Whether the user may open a section at all (role and system-admin gates). */
-export function sectionPermitted(section: SettingsSection, ctx: SettingsAccessContext): boolean {
- const { access } = section
- if (access.systemAdminOnly) return ctx.isSystemAdmin
- if (ctx.canAccessAllTenants) return true
- return ctx.hasRole(access.minRole ?? 'viewer')
-}
-
-/** Whether this deployment supports a section. */
-export function sectionSupported(section: SettingsSection, ctx: SettingsAccessContext): boolean {
- const { access } = section
- if (access.supported) return access.supported(ctx.capabilities)
- return ctx.isSupported(access.capability)
-}
-
-export interface SettingsNavGroup {
- key: string
- label: string
- items: SettingsSection[]
-}
-
-/**
- * Sidebar groups in group order, each with its visible sections in section
- * order. Groups without a visible section are dropped, and so are sections
- * whose group is not registered.
- */
-export function groupSections(
- groups: readonly SettingsGroup[],
- visible: readonly SettingsSection[],
-): SettingsNavGroup[] {
- return [...groups]
- .sort((a, b) => a.order - b.order)
- .map((group) => ({
- key: group.key,
- label: group.label(),
- items: visible.filter((s) => s.group === group.key).sort((a, b) => a.order - b.order),
- }))
- .filter((group) => group.items.length > 0)
-}
diff --git a/frontend/src/extensions/toolResultViews.ts b/frontend/src/extensions/toolResultViews.ts
deleted file mode 100644
index 3af469c44..000000000
--- a/frontend/src/extensions/toolResultViews.ts
+++ /dev/null
@@ -1,31 +0,0 @@
-import type { Component } from 'vue'
-
-import { createRegistry } from './registry'
-
-/** What a tool result view gets to render one tool call. */
-export interface ToolResultContext {
- displayType: string
- data: Record<string, any>
- output: string
- arguments: Record<string, any>
- success?: boolean
-}
-
-/**
- * Renders the tool results whose display_type equals `key`. Tools that
- * declare no view (or an unknown one) fall back to the raw output.
- */
-export interface ToolResultView {
- key: string
- component: Component
- /** Props for the component; defaults to { data }. */
- props?: (ctx: ToolResultContext) => Record<string, unknown>
- pluginId: string
-}
-
-export const toolResultViews = createRegistry<ToolResultView>('tool result view')
-
-/** The props a view receives for one tool call. */
-export function toolResultProps(view: ToolResultView, ctx: ToolResultContext): Record<string, unknown> {
- return view.props ? view.props(ctx) : { data: ctx.data }
-}
diff --git a/frontend/src/i18n/backendSchemaKeys.ts b/frontend/src/i18n/backendSchemaKeys.ts
deleted file mode 100644
index d64d1031d..000000000
--- a/frontend/src/i18n/backendSchemaKeys.ts
+++ /dev/null
@@ -1,85 +0,0 @@
-// Locale keys the backend names: x-i18n-keys in builtin config schemas (web
-// search parameters, data source and IM credentials), IM platform link titles
-// and mode hints. Only the backend references them, so the i18n audit counts this list as used; otherwise pruning would
-// drop the labels. internal/plugin/builtin/i18n_keys_test.go keeps it in step
-// with the backend, sorted.
-export const BACKEND_SCHEMA_I18N_KEYS = [
- 'agentEditor.im.dingtalkCardTemplateId',
- 'agentEditor.im.dingtalkCardTemplateIdHint',
- 'agentEditor.im.dingtalkConsole',
- 'agentEditor.im.feishuAPIBaseURLHint',
- 'agentEditor.im.feishuConsole',
- 'agentEditor.im.larkConsole',
- 'agentEditor.im.mattermostConsole',
- 'agentEditor.im.mattermostModeHint',
- 'agentEditor.im.mattermostPostToMain',
- 'agentEditor.im.mattermostPostToMainHint',
- 'agentEditor.im.qqbotAPIBaseURLHint',
- 'agentEditor.im.qqbotConsole',
- 'agentEditor.im.qqbotGatewayURLHint',
- 'agentEditor.im.slackConsole',
- 'agentEditor.im.telegramConsole',
- 'agentEditor.im.wecomAPIBaseURLHint',
- 'agentEditor.im.wecomConsole',
- 'agentEditor.im.wecomWSEndpointHint',
- 'agentEditor.im.yunzhijiaAllowedHostSuffix',
- 'agentEditor.im.yunzhijiaAllowedHostSuffixHint',
- 'agentEditor.im.yunzhijiaAppCredentialHint',
- 'agentEditor.im.yunzhijiaAppId',
- 'agentEditor.im.yunzhijiaAppIdPlaceholder',
- 'agentEditor.im.yunzhijiaAppSecret',
- 'agentEditor.im.yunzhijiaAppSecretPlaceholder',
- 'agentEditor.im.yunzhijiaImageDoc',
- 'agentEditor.im.yunzhijiaModeHint',
- 'agentEditor.im.yunzhijiaRobotDoc',
- 'agentEditor.im.yunzhijiaSecret',
- 'agentEditor.im.yunzhijiaSecretHint',
- 'agentEditor.im.yunzhijiaSecretPlaceholder',
- 'agentEditor.im.yunzhijiaSendMsgUrl',
- 'agentEditor.im.yunzhijiaSendMsgUrlHint',
- 'agentEditor.im.yunzhijiaTimeout',
- 'agentEditor.im.yunzhijiaTimeoutHint',
- 'credential.inputPlaceholder',
- 'datasource.field.apiToken',
- 'datasource.field.appId',
- 'datasource.field.appSecret',
- 'datasource.field.authHeaders',
- 'datasource.field.authHeadersHint',
- 'datasource.field.baseUrl',
- 'datasource.field.baseUrlHint',
- 'datasource.field.clientId',
- 'datasource.field.clientSecret',
- 'datasource.field.confluenceApiToken',
- 'datasource.field.confluenceBaseUrl',
- 'datasource.field.confluenceEdition',
- 'datasource.field.confluenceEditionCloud',
- 'datasource.field.confluenceEditionServer',
- 'datasource.field.confluencePassword',
- 'datasource.field.confluenceUsername',
- 'datasource.field.imaApiKey',
- 'datasource.field.imaClientId',
- 'datasource.field.integrationToken',
- 'datasource.field.operatorId',
- 'datasource.field.operatorIdHint',
- 'datasource.gitlab.accessToken',
- 'datasource.gitlab.baseUrl',
- 'webSearchSettings.apiKeyLabel',
- 'webSearchSettings.apiKeyOptionalLabel',
- 'webSearchSettings.apiKeyPlaceholder',
- 'webSearchSettings.baseUrlLabel',
- 'webSearchSettings.baseUrlPlaceholder',
- 'webSearchSettings.configFields.contentHigh',
- 'webSearchSettings.configFields.contentMedium',
- 'webSearchSettings.configFields.contentSize',
- 'webSearchSettings.configFields.contentSizeDesc',
- 'webSearchSettings.configFields.searchEngine',
- 'webSearchSettings.configFields.searchEngineDesc',
- 'webSearchSettings.configFields.searchPro',
- 'webSearchSettings.configFields.searchQuark',
- 'webSearchSettings.configFields.searchSogou',
- 'webSearchSettings.configFields.searchStd',
- 'webSearchSettings.engineIdLabel',
- 'webSearchSettings.proxyUrlHelp',
- 'webSearchSettings.proxyUrlLabel',
- 'webSearchSettings.proxyUrlPlaceholder',
-] as const
diff --git a/frontend/src/i18n/localeKeyAudit.ts b/frontend/src/i18n/localeKeyAudit.ts
index 101f4ef7c..c3079374d 100644
--- a/frontend/src/i18n/localeKeyAudit.ts
+++ b/frontend/src/i18n/localeKeyAudit.ts
@@ -14,7 +14,6 @@ import {
REGISTERED_AUDIT_ACTION_ENTRIES,
} from './auditActionRegistry.ts'
import { AUDIT_ACTION_LOCALE_DEFAULTS, getAuditActionLocaleDefault } from './auditActionLocaleDefaults.ts'
-import { BACKEND_SCHEMA_I18N_KEYS } from './backendSchemaKeys.ts'
import { writeLocaleModule } from './localeSerialize.ts'
import enUS from './locales/en-US.ts'
@@ -116,11 +115,6 @@ const EXTRA_PREFIXES = [
// knowledgeEditor.wikiBrowser.gallery.attr.builtin_caption …, and
// everything else falls back to the contract wording.
'knowledgeEditor.wikiBrowser.gallery.attr.',
- // Connector names and descriptions are keyed by the connector type the
- // backend registry lists (views/knowledge/settings/connectorLabels.ts);
- // plugin connectors fall back to their own names.
- 'datasource.connector.',
- 'datasource.connectorDesc.',
] as const
/** Keys that must survive pruning even when static analysis misses them. */
@@ -291,8 +285,7 @@ function addTemplatePattern(usage: I18nUsage, template: string): void {
export function collectI18nUsageFromSources(rootDir = SOURCE_ROOT): I18nUsage {
const usage: I18nUsage = {
- // Backend config schemas name these keys; count them as used.
- staticKeys: new Set<string>(BACKEND_SCHEMA_I18N_KEYS),
+ staticKeys: new Set<string>(),
prefixes: new Set<string>(EXTRA_PREFIXES),
segmentPatterns: [],
}
diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts
index 408620448..edb99262e 100755
--- a/frontend/src/i18n/locales/en-US.ts
+++ b/frontend/src/i18n/locales/en-US.ts
@@ -812,8 +812,6 @@ export default {
channelGitLab: 'GitLab',
channelIma: 'Tencent IMA',
channelUpload: 'Upload',
- channelRss: 'RSS',
- channelExternal: 'External source',
channelManual: 'Manual',
channelUrl: 'Web',
channelUnknown: 'Unknown',
@@ -1881,8 +1879,6 @@ export default {
},
},
skills: {
- pluginSection: 'From enabled plugins',
- pluginSectionHint: 'Pick a skill a plugin provides to add it to the library; then install it into sandboxes like any other skill.',
title: 'Skill Management',
description: 'Skills live in the workspace catalog. Register them first, then install onto one or more sandboxes. An agent can only enable skills that are ready on its sandbox.',
helpTooltip: 'A catalog skill does not have to be installed anywhere. Scripts only run after the skill is installed into the sandbox image the agent uses. Docker, Cube, and E2B images are not interchangeable — install once per sandbox.',
@@ -2253,18 +2249,6 @@ export default {
baseUrlLabel: 'Instance URL',
baseUrlPlaceholder: 'https://searxng.example.com',
apiKeyPlaceholder: 'Enter API key',
- configFields: {
- searchEngine: 'Search engine',
- searchEngineDesc: 'Select the Zhipu search engine and per-request price tier.',
- searchStd: 'Standard · ¥0.01/request',
- searchPro: 'Pro · ¥0.03/request',
- searchSogou: 'Sogou · ¥0.05/request',
- searchQuark: 'Quark · ¥0.05/request',
- contentSize: 'Content size',
- contentSizeDesc: 'Medium returns concise summaries; high returns more context.',
- contentMedium: 'Medium',
- contentHigh: 'High'
- },
toasts: {
providerCreated: 'Search provider created',
providerUpdated: 'Search provider updated',
@@ -2733,209 +2717,6 @@ export default {
parseCurrentKnowledgeBaseFailed: 'Failed to parse current knowledge base'
}
},
- pluginAdmin: {
- runtime: {
- declarative: 'Declarative',
- host: 'Local process',
- remote: 'Remote service'
- },
- title: 'Plugin management',
- description: 'Install and manage plugins for the whole platform. Every workspace sees an installed plugin, and each one enables it for itself; disabling a plugin here unloads it on every node.',
- installButton: 'Install plugin',
- empty: 'No plugins installed yet',
- loadFailed: 'Failed to load installed plugins',
- saveFailed: 'Failed to save',
- enabledToast: 'Enabled platform-wide',
- disabledToast: 'Disabled platform-wide',
- platformSwitch: 'Enable or disable platform-wide',
- publisher: 'Publisher',
- contributions: 'What it adds',
- permissions: 'Permissions and remote access',
- state: {
- degraded: 'Restarting',
- running: 'Running',
- failed: 'Failed to load',
- disabled: 'Disabled',
- pending: 'Loading'
- },
- nodeState: {
- ready: 'Ready',
- starting: 'Starting',
- degraded: 'Degraded',
- stopped: 'Not loaded'
- },
- source: {
- upload: 'Upload',
- url: 'URL'
- },
- change: {
- install: 'New install',
- upgrade: 'Upgrade from v{from}',
- downgrade: 'Downgrade from v{from}',
- reinstall: 'Reinstall'
- },
- permission: {
- anyHost: 'Any public host',
- remote: 'Remote service',
- egress: 'Network egress',
- hostApi: 'WeKnora API',
- events: 'Events'
- },
- secret: {
- title: 'Plugin signing secret',
- description: 'Set this secret as the plugin service\'s WEKNORA_PLUGIN_SECRET environment variable; the service uses it to verify that requests come from WeKnora. It is shown only once and cannot be viewed again after you close this.',
- copy: 'Copy',
- copied: 'Copied',
- done: 'I have saved it'
- },
- install: {
- title: 'Install plugin',
- description: 'Upload a .wkp package or give its download URL, review it, then install.',
- sourceSection: 'Package',
- mode: {
- upload: 'Upload file',
- url: 'From URL'
- },
- fileLabel: 'Package file',
- chooseFile: 'Choose file',
- noFile: 'No file chosen',
- fileHint: 'A .wkp file (a zip with plugin.yaml), up to 64 MB.',
- urlLabel: 'Download URL',
- urlHint: 'The server downloads this URL; private network addresses are refused.',
- remoteUrlLabel: 'Service URL',
- remoteUrlHint: 'The plugin service\'s HTTP(S) address. Private network hosts must be listed in SSRF_WHITELIST.',
- remoteUrlKeep: 'On an upgrade, leave empty to keep the current URL',
- reviewSection: 'Review before installing',
- noPermissions: 'This plugin asks for no extra permissions.',
- configNotice: 'This plugin needs configuration: platform settings are in its details after installing, and workspace admins fill in workspace settings in Plugins.',
- tenantNotice: 'Once installed the plugin is visible to every workspace but disabled until a workspace admin enables it.',
- digest: 'Package digest',
- inspect: 'Inspect package',
- confirm: {
- install: 'Install',
- upgrade: 'Upgrade',
- downgrade: 'Install older version',
- reinstall: 'Reinstall'
- },
- inspectFailed: 'Could not read the package',
- failed: 'Install failed',
- done: 'Installed {name}'
- },
- detail: {
- overview: 'Overview',
- runtime: 'Runtime',
- source: 'Source',
- homepage: 'Homepage',
- license: 'License',
- engines: 'Compatible versions',
- nodes: 'Nodes',
- noNodes: 'No node has the plugin loaded',
- versions: 'Versions',
- active: 'Active',
- activate: 'Switch to this version',
- rollback: 'Roll back to this version',
- activateConfirm: 'Make v{version} the active version? Every node reloads the plugin.',
- activated: 'Switched to v{version}',
- activateFailed: 'Failed to switch versions',
- systemConfig: 'Platform settings',
- systemConfigHint: 'Apply to every workspace, such as a service region or a platform-wide credential.',
- configLoadFailed: 'Failed to load platform settings',
- configSaved: 'Platform settings saved',
- configSaveFailed: 'Failed to save platform settings',
- danger: 'Uninstall',
- uninstallHint: 'Removes the plugin and every stored version. Workspaces\' switches and settings are kept and come back if you reinstall.',
- uninstall: 'Uninstall plugin',
- uninstallConfirm: 'Uninstall? Every node unloads the plugin right away.',
- uninstalled: 'Plugin uninstalled',
- uninstallFailed: 'Uninstall failed',
- remote: 'Remote service',
- remoteUrl: 'Service URL',
- editUrl: 'Change',
- urlSaved: 'Service URL updated',
- urlSaveFailed: 'Failed to update the service URL',
- rotateSecret: 'Rotate secret',
- rotateHint: 'Issues a new signing secret. Calls to the service fail until it is given the new one.',
- rotateConfirm: 'Rotate the secret? The old one stops working immediately.',
- rotateFailed: 'Failed to rotate the secret'
- }
- },
- pluginToolView: {
- empty: 'No results'
- },
- pluginPages: {
- notResponding: 'The plugin page is not responding; it may have failed to load.',
- requestFailed: 'The plugin request failed',
- fromPlugin: 'Provided by the plugin {id}'
- },
- pluginCenter: {
- installed: 'Installed',
- configure: 'Configure',
- webhooks: 'Webhook URLs',
- webhooksHint: 'Set these URLs up in the third-party service so it sends its events to this plugin. They carry this workspace\'s secret; keep them private.',
- copy: 'Copy',
- webhookCopied: 'Webhook URL copied',
- configTitle: 'Configure {name}',
- configDescription: 'This workspace\'s settings for the plugin, such as its API key. Secrets are stored encrypted and never shown again.',
- configLoadFailed: 'Failed to load the plugin configuration',
- configSaved: 'Plugin configuration saved',
- configSaveFailed: 'Failed to save the plugin configuration',
- navGroup: 'Extensions',
- title: 'Plugins',
- description: 'Every plugin this deployment provides: builtins and the ones a system administrator installed. Enable or disable them for this workspace: a disabled plugin\'s integrations leave the type lists and cannot be created, while existing ones keep working. Installed plugins start disabled until a workspace admin enables them.',
- searchPlaceholder: 'Search plugins, IDs or integrations',
- allPoints: 'All',
- empty: 'No matching plugins',
- builtin: 'Builtin',
- required: 'Required',
- requiredHint: 'WeKnora needs this plugin; it cannot be disabled',
- adminOnly: 'Only workspace admins can enable or disable plugins',
- loadFailed: 'Failed to load plugins',
- saveFailed: 'Failed to save the plugin setting',
- enabledToast: 'Plugin enabled',
- disabledToast: 'Plugin disabled',
- points: {
- modelVendors: 'Model vendors',
- connectors: 'Data sources',
- imChannels: 'IM channels',
- webSearch: 'Web search',
- tools: 'Agent tools',
- parsers: 'Document parsing',
- skills: 'Skills',
- mcpServers: 'MCP servers',
- pages: 'Pages',
- settingsSections: 'Settings sections',
- kbTabs: 'Knowledge base tabs',
- webhooks: 'Webhooks'
- }
- },
- schemaForm: {
- errors: {
- required: 'This field is required',
- type: 'Invalid value',
- enum: 'Not one of the allowed values',
- min_length: 'Too short',
- max_length: 'Too long',
- pattern: 'Does not match the expected format',
- minimum: 'Value is too small',
- maximum: 'Value is too large',
- format: 'Enter a full address such as https://example.com'
- },
- options: {
- loading: 'Loading…',
- empty: 'The plugin offers no choices',
- retry: 'Retry',
- failed: 'Could not load the choices'
- },
- oauth: {
- connected: 'Connected',
- notConnected: 'Not connected',
- connect: 'Connect',
- reconnect: 'Reconnect',
- disconnect: 'Disconnect',
- popupBlocked: 'The browser blocked the sign-in window. Allow pop-ups and try again.',
- failed: 'Authorization failed'
- }
- },
common: {
add: 'Add',
me: 'Me',
@@ -5558,9 +5339,6 @@ export default {
}
},
mcpSettings: {
- transportPlugin: 'Built into plugin',
- fromPlugin: 'Plugin',
- pluginNotConfigured: 'The plugin is not configured yet; fill in its settings in Plugins to use it',
addUsageInstructions: "Add usage instructions",
noUsageInstructions: "No usage instructions yet",
title: 'MCP Services',
@@ -7080,7 +6858,6 @@ export default {
resourceHint: 'Select the spaces or folders to sync',
untitled: 'Untitled',
resourceLoadFailed: 'Failed to load resources',
- typesLoadFailed: 'Failed to load data source types',
noResources: 'No wiki spaces found',
noResourcesDesc: 'The app needs wiki access via a group chat to fetch content',
noResourcesDesc_notion: 'The app needs Notion page access permissions to fetch content',
diff --git a/frontend/src/i18n/locales/ja-JP.ts b/frontend/src/i18n/locales/ja-JP.ts
index 45cf25caa..19d84e93a 100644
--- a/frontend/src/i18n/locales/ja-JP.ts
+++ b/frontend/src/i18n/locales/ja-JP.ts
@@ -812,8 +812,6 @@ export default {
channelGitLab: 'GitLab',
channelIma: 'Tencent IMA',
channelUpload: 'アップロード',
- channelRss: 'RSS',
- channelExternal: '外部データソース',
channelManual: '手動作成',
channelUrl: 'Web',
channelUnknown: '不明',
@@ -1881,8 +1879,6 @@ export default {
},
},
skills: {
- pluginSection: '有効なプラグインから',
- pluginSectionHint: 'プラグインが提供するスキルを選んでライブラリに追加し、他のスキルと同様にサンドボックスへインストールできます。',
title: 'スキル管理',
description: 'スキルはワークスペースのカタログに登録されます。まず登録し、その後1つ以上のサンドボックスにインストールしてください。エージェントは、自身のサンドボックスで準備完了しているスキルのみ有効化できます。',
helpTooltip: 'カタログのスキルは、どこかにインストールされている必要はありません。スクリプトは、エージェントが使用するサンドボックスイメージにスキルがインストールされて初めて実行されます。Docker、Cube、E2Bのイメージには互換性がないため、サンドボックスごとにインストールしてください。',
@@ -2253,18 +2249,6 @@ export default {
baseUrlLabel: 'インスタンスURL',
baseUrlPlaceholder: 'https://searxng.example.com',
apiKeyPlaceholder: 'APIキーを入力',
- configFields: {
- searchEngine: '検索エンジン',
- searchEngineDesc: 'Zhipu の検索エンジンとリクエストごとの料金プランを選択します。',
- searchStd: 'スタンダード · ¥0.01/回',
- searchPro: 'プロ · ¥0.03/回',
- searchSogou: 'Sogou · ¥0.05/回',
- searchQuark: 'Quark · ¥0.05/回',
- contentSize: '要約の長さ',
- contentSizeDesc: '中は簡潔な要約、詳細はより多くの文脈を返します。',
- contentMedium: '中',
- contentHigh: '詳細'
- },
toasts: {
providerCreated: '検索プロバイダを作成しました',
providerUpdated: '検索プロバイダを更新しました',
@@ -2733,209 +2717,6 @@ export default {
parseCurrentKnowledgeBaseFailed: '現在のナレッジベースの解析に失敗しました'
}
},
- pluginAdmin: {
- runtime: {
- declarative: '宣言型',
- host: 'ローカルプロセス',
- remote: 'リモートサービス'
- },
- title: 'プラグイン管理',
- description: 'プラットフォーム全体のプラグインをインストール・管理します。インストールしたプラグインはすべてのワークスペースに表示され、各ワークスペースで個別に有効化します。ここで無効にすると全ノードでアンロードされます。',
- installButton: 'プラグインをインストール',
- empty: 'インストール済みのプラグインはありません',
- loadFailed: 'インストール済みプラグインの読み込みに失敗しました',
- saveFailed: '保存に失敗しました',
- enabledToast: 'プラットフォーム全体で有効にしました',
- disabledToast: 'プラットフォーム全体で無効にしました',
- platformSwitch: 'プラットフォーム全体で有効/無効',
- publisher: '発行者',
- contributions: '提供する機能',
- permissions: '権限と外部アクセス',
- state: {
- degraded: '再起動中',
- running: '稼働中',
- failed: '読み込み失敗',
- disabled: '無効',
- pending: '読み込み中'
- },
- nodeState: {
- ready: '準備完了',
- starting: '起動中',
- degraded: '異常',
- stopped: '未読み込み'
- },
- source: {
- upload: 'アップロード',
- url: 'URL'
- },
- change: {
- install: '新規インストール',
- upgrade: 'v{from} からアップグレード',
- downgrade: 'v{from} からダウングレード',
- reinstall: '再インストール'
- },
- permission: {
- anyHost: '任意の公開ホスト',
- remote: 'リモートサービス',
- egress: 'ネットワーク送信',
- hostApi: 'WeKnora API',
- events: 'イベント'
- },
- secret: {
- title: 'プラグイン署名シークレット',
- description: 'このシークレットをプラグインサービスの環境変数 WEKNORA_PLUGIN_SECRET に設定してください。サービスはこれでリクエストが WeKnora からのものか確認します。表示されるのは今回だけで、閉じると再表示できません。',
- copy: 'コピー',
- copied: 'コピーしました',
- done: '保存しました'
- },
- install: {
- title: 'プラグインをインストール',
- description: '.wkp パッケージをアップロードするかダウンロード URL を指定し、確認してからインストールします。',
- sourceSection: 'パッケージ',
- mode: {
- upload: 'ファイルをアップロード',
- url: 'URL から'
- },
- fileLabel: 'パッケージファイル',
- chooseFile: 'ファイルを選択',
- noFile: 'ファイル未選択',
- fileHint: '.wkp ファイル(plugin.yaml を含む zip)、最大 64 MB。',
- urlLabel: 'ダウンロード URL',
- urlHint: 'サーバーがこの URL からダウンロードします。プライベートネットワークのアドレスは拒否されます。',
- remoteUrlLabel: 'サービス URL',
- remoteUrlHint: 'プラグインサービスの HTTP(S) アドレス。プライベートネットワークのホストは SSRF_WHITELIST に追加する必要があります。',
- remoteUrlKeep: 'アップグレード時は空欄のままで現在の URL を引き続き使用します',
- reviewSection: 'インストール前の確認',
- noPermissions: 'このプラグインは追加の権限を要求しません。',
- configNotice: 'このプラグインには設定が必要です。プラットフォーム設定はインストール後の詳細で、ワークスペース設定は各ワークスペース管理者がプラグインセンターで入力します。',
- tenantNotice: 'インストール後、プラグインはすべてのワークスペースに表示されますが、ワークスペース管理者が有効にするまで無効です。',
- digest: 'パッケージダイジェスト',
- inspect: 'パッケージを確認',
- confirm: {
- install: 'インストール',
- upgrade: 'アップグレード',
- downgrade: '旧バージョンをインストール',
- reinstall: '再インストール'
- },
- inspectFailed: 'パッケージを読み込めませんでした',
- failed: 'インストールに失敗しました',
- done: '{name} をインストールしました'
- },
- detail: {
- overview: '概要',
- runtime: 'ランタイム',
- source: '取得元',
- homepage: 'ホームページ',
- license: 'ライセンス',
- engines: '対応バージョン',
- nodes: 'ノード',
- noNodes: 'このプラグインを読み込んでいるノードはありません',
- versions: 'バージョン',
- active: '使用中',
- activate: 'このバージョンに切り替え',
- rollback: 'このバージョンにロールバック',
- activateConfirm: 'v{version} を使用中のバージョンにしますか?すべてのノードで再読み込みされます。',
- activated: 'v{version} に切り替えました',
- activateFailed: 'バージョンの切り替えに失敗しました',
- systemConfig: 'プラットフォーム設定',
- systemConfigHint: 'すべてのワークスペースに適用されます(サービスリージョンや共通の認証情報など)。',
- configLoadFailed: 'プラットフォーム設定の読み込みに失敗しました',
- configSaved: 'プラットフォーム設定を保存しました',
- configSaveFailed: 'プラットフォーム設定の保存に失敗しました',
- danger: 'アンインストール',
- uninstallHint: 'プラグインと保存済みの全バージョンを削除します。各ワークスペースのスイッチと設定は保持され、再インストールすると復元されます。',
- uninstall: 'プラグインをアンインストール',
- uninstallConfirm: 'アンインストールしますか?すべてのノードで直ちにアンロードされます。',
- uninstalled: 'プラグインをアンインストールしました',
- uninstallFailed: 'アンインストールに失敗しました',
- remote: 'リモートサービス',
- remoteUrl: 'サービス URL',
- editUrl: '変更',
- urlSaved: 'サービス URL を更新しました',
- urlSaveFailed: 'サービス URL の更新に失敗しました',
- rotateSecret: 'シークレットをローテーション',
- rotateHint: '新しい署名シークレットを発行します。サービスに新しいシークレットを設定するまで呼び出しは失敗します。',
- rotateConfirm: 'ローテーションしますか?古いシークレットは直ちに無効になります。',
- rotateFailed: 'シークレットのローテーションに失敗しました'
- }
- },
- pluginToolView: {
- empty: '結果がありません'
- },
- pluginPages: {
- notResponding: 'プラグインページが応答しません。読み込みに失敗した可能性があります。',
- requestFailed: 'プラグインへのリクエストに失敗しました',
- fromPlugin: 'プラグイン {id} が提供'
- },
- pluginCenter: {
- installed: 'インストール済み',
- configure: '設定',
- webhooks: 'Webhook URL',
- webhooksHint: 'サードパーティのサービスにこれらの URL を設定すると、イベントがこのプラグインに送られます。このワークスペースの秘密を含むため、公開しないでください。',
- copy: 'コピー',
- webhookCopied: 'Webhook URL をコピーしました',
- configTitle: '{name} の設定',
- configDescription: 'このワークスペースでプラグインを使うための設定(API キーなど)。シークレットは暗号化して保存され、再表示されません。',
- configLoadFailed: 'プラグイン設定の読み込みに失敗しました',
- configSaved: 'プラグイン設定を保存しました',
- configSaveFailed: 'プラグイン設定の保存に失敗しました',
- navGroup: '拡張',
- title: 'プラグイン',
- description: 'このデプロイが提供するすべてのプラグイン(組み込みと、システム管理者がインストールしたもの)。このワークスペースで有効化・無効化できます。無効にしたプラグインの連携は種類一覧から外れ新規作成できなくなりますが、既存のものは引き続き動作します。インストールされたプラグインは、ワークスペース管理者が有効にするまで無効です。',
- searchPlaceholder: 'プラグイン名・ID・連携を検索',
- allPoints: 'すべて',
- empty: '一致するプラグインはありません',
- builtin: '組み込み',
- required: '必須',
- requiredHint: 'WeKnora に必須のため無効にできません',
- adminOnly: 'プラグインの有効化・無効化はワークスペース管理者のみ可能です',
- loadFailed: 'プラグインを読み込めませんでした',
- saveFailed: 'プラグイン設定を保存できませんでした',
- enabledToast: 'プラグインを有効にしました',
- disabledToast: 'プラグインを無効にしました',
- points: {
- modelVendors: 'モデルベンダー',
- connectors: 'データソース',
- imChannels: 'IM チャネル',
- webSearch: 'Web 検索',
- tools: 'エージェントツール',
- parsers: 'ドキュメント解析',
- skills: 'スキル',
- mcpServers: 'MCP サーバー',
- pages: 'ページ',
- settingsSections: '設定セクション',
- kbTabs: 'ナレッジベースのタブ',
- webhooks: 'Webhook'
- }
- },
- schemaForm: {
- errors: {
- required: 'この項目は必須です',
- type: '値が正しくありません',
- enum: '選択可能な値ではありません',
- min_length: '短すぎます',
- max_length: '長すぎます',
- pattern: '形式が正しくありません',
- minimum: '値が小さすぎます',
- maximum: '値が大きすぎます',
- format: 'https://example.com のような完全なアドレスを入力してください'
- },
- options: {
- loading: '読み込み中…',
- empty: '選択肢がありません',
- retry: '再試行',
- failed: '選択肢を読み込めませんでした'
- },
- oauth: {
- connected: '接続済み',
- notConnected: '未接続',
- connect: 'アカウントを接続',
- reconnect: '再接続',
- disconnect: '切断',
- popupBlocked: 'ブラウザが認可ウィンドウをブロックしました。ポップアップを許可して再試行してください。',
- failed: '認可に失敗しました'
- }
- },
common: {
add: '追加',
me: '自分',
@@ -5558,9 +5339,6 @@ export default {
}
},
mcpSettings: {
- transportPlugin: 'プラグイン内蔵',
- fromPlugin: 'プラグイン',
- pluginNotConfigured: 'プラグインが未設定です。プラグインセンターで設定してから使用してください',
addUsageInstructions: "使用方法を追加",
noUsageInstructions: "使用方法は未入力です",
title: 'MCPサービス',
@@ -7080,7 +6858,6 @@ export default {
resourceHint: '同期するスペースまたはフォルダを選択してください',
untitled: '無題',
resourceLoadFailed: 'リソースの読み込みに失敗しました',
- typesLoadFailed: 'データソースの種類を読み込めませんでした',
noResources: 'Wikiスペースが見つかりません',
noResourcesDesc: 'コンテンツを取得するには、グループチャット経由でアプリにWikiのアクセス権限を付与する必要があります',
noResourcesDesc_notion: 'コンテンツを取得するには、アプリにNotionページのアクセス権限が必要です',
diff --git a/frontend/src/i18n/locales/ko-KR.ts b/frontend/src/i18n/locales/ko-KR.ts
index 01c8efad7..62dde99c3 100755
--- a/frontend/src/i18n/locales/ko-KR.ts
+++ b/frontend/src/i18n/locales/ko-KR.ts
@@ -995,7 +995,6 @@ export default {
resourceHint: '동기화할 공간/폴더를 선택하세요',
untitled: '제목 없음',
resourceLoadFailed: '리소스 목록 로드 실패',
- typesLoadFailed: '데이터 소스 유형을 불러오지 못했습니다',
noResources: '동기화 가능한 위키 공간을 찾을 수 없습니다',
noResourcesDesc: '앱이 콘텐츠를 가져오려면 그룹 채팅을 통해 위키 접근 권한을 얻어야 합니다',
noResourcesDesc_notion: '앱이 콘텐츠를 가져오려면 Notion 페이지 접근 권한이 필요합니다',
@@ -2663,9 +2662,6 @@ export default {
}
},
mcpSettings: {
- transportPlugin: '플러그인 내장',
- fromPlugin: '플러그인',
- pluginNotConfigured: '플러그인이 아직 설정되지 않았습니다. 플러그인 센터에서 설정한 뒤 사용하세요',
addUsageInstructions: "사용 안내 추가",
noUsageInstructions: "아직 사용 안내가 없습니다",
title: 'MCP 서비스 관리',
@@ -5252,209 +5248,6 @@ export default {
belongsToOrg: '스페이스: ',
noCompatibleKbForAgent: '현재 에이전트의 도구와 범위 내 지식베이스의 기능이 일치하지 않아 참조할 수 있는 지식베이스가 없습니다.'
},
- pluginAdmin: {
- runtime: {
- declarative: '선언형',
- host: '로컬 프로세스',
- remote: '원격 서비스'
- },
- title: '플러그인 관리',
- description: '플랫폼 전체의 플러그인을 설치하고 관리합니다. 설치된 플러그인은 모든 워크스페이스에 보이며 각 워크스페이스가 직접 활성화합니다. 여기서 비활성화하면 모든 노드에서 언로드됩니다.',
- installButton: '플러그인 설치',
- empty: '설치된 플러그인이 없습니다',
- loadFailed: '설치된 플러그인을 불러오지 못했습니다',
- saveFailed: '저장하지 못했습니다',
- enabledToast: '플랫폼 전체에서 활성화했습니다',
- disabledToast: '플랫폼 전체에서 비활성화했습니다',
- platformSwitch: '플랫폼 전체 활성화 / 비활성화',
- publisher: '게시자',
- contributions: '제공 기능',
- permissions: '권한 및 외부 접근',
- state: {
- degraded: '재시작 중',
- running: '실행 중',
- failed: '로드 실패',
- disabled: '비활성',
- pending: '로드 중'
- },
- nodeState: {
- ready: '준비됨',
- starting: '시작 중',
- degraded: '이상',
- stopped: '로드되지 않음'
- },
- source: {
- upload: '업로드',
- url: 'URL'
- },
- change: {
- install: '새로 설치',
- upgrade: 'v{from}에서 업그레이드',
- downgrade: 'v{from}에서 다운그레이드',
- reinstall: '재설치'
- },
- permission: {
- anyHost: '모든 공개 호스트',
- remote: '원격 서비스',
- egress: '네트워크 송신',
- hostApi: 'WeKnora API',
- events: '이벤트'
- },
- secret: {
- title: '플러그인 서명 비밀 키',
- description: '이 비밀 키를 플러그인 서비스의 환경 변수 WEKNORA_PLUGIN_SECRET으로 설정하세요. 서비스는 이를 사용해 요청이 WeKnora에서 온 것인지 확인합니다. 이번에만 표시되며 닫은 후에는 다시 볼 수 없습니다.',
- copy: '복사',
- copied: '복사했습니다',
- done: '저장했습니다'
- },
- install: {
- title: '플러그인 설치',
- description: '.wkp 패키지를 업로드하거나 다운로드 URL을 입력하고, 검토한 뒤 설치합니다.',
- sourceSection: '패키지',
- mode: {
- upload: '파일 업로드',
- url: 'URL에서'
- },
- fileLabel: '패키지 파일',
- chooseFile: '파일 선택',
- noFile: '선택된 파일 없음',
- fileHint: '.wkp 파일(plugin.yaml이 포함된 zip), 최대 64 MB.',
- urlLabel: '다운로드 URL',
- urlHint: '서버가 이 URL에서 다운로드합니다. 사설 네트워크 주소는 거부됩니다.',
- remoteUrlLabel: '서비스 URL',
- remoteUrlHint: '플러그인 서비스의 HTTP(S) 주소입니다. 사설 네트워크 호스트는 SSRF_WHITELIST에 추가해야 합니다.',
- remoteUrlKeep: '업그레이드 시 비워 두면 현재 URL을 계속 사용합니다',
- reviewSection: '설치 전 검토',
- noPermissions: '이 플러그인은 추가 권한을 요청하지 않습니다.',
- configNotice: '이 플러그인은 설정이 필요합니다. 플랫폼 설정은 설치 후 상세 화면에서, 워크스페이스 설정은 각 워크스페이스 관리자가 플러그인 센터에서 입력합니다.',
- tenantNotice: '설치 후 플러그인은 모든 워크스페이스에 보이지만 워크스페이스 관리자가 활성화하기 전까지 비활성 상태입니다.',
- digest: '패키지 다이제스트',
- inspect: '패키지 검사',
- confirm: {
- install: '설치',
- upgrade: '업그레이드',
- downgrade: '이전 버전 설치',
- reinstall: '재설치'
- },
- inspectFailed: '패키지를 읽을 수 없습니다',
- failed: '설치에 실패했습니다',
- done: '{name}을(를) 설치했습니다'
- },
- detail: {
- overview: '개요',
- runtime: '런타임',
- source: '출처',
- homepage: '홈페이지',
- license: '라이선스',
- engines: '호환 버전',
- nodes: '노드',
- noNodes: '이 플러그인을 로드한 노드가 없습니다',
- versions: '버전',
- active: '사용 중',
- activate: '이 버전으로 전환',
- rollback: '이 버전으로 롤백',
- activateConfirm: 'v{version}을(를) 사용 중인 버전으로 할까요? 모든 노드가 플러그인을 다시 로드합니다.',
- activated: 'v{version}(으)로 전환했습니다',
- activateFailed: '버전을 전환하지 못했습니다',
- systemConfig: '플랫폼 설정',
- systemConfigHint: '모든 워크스페이스에 적용됩니다(예: 서비스 리전, 플랫폼 공용 자격 증명).',
- configLoadFailed: '플랫폼 설정을 불러오지 못했습니다',
- configSaved: '플랫폼 설정을 저장했습니다',
- configSaveFailed: '플랫폼 설정을 저장하지 못했습니다',
- danger: '제거',
- uninstallHint: '플러그인과 저장된 모든 버전을 삭제합니다. 워크스페이스의 스위치와 설정은 유지되며 재설치하면 복원됩니다.',
- uninstall: '플러그인 제거',
- uninstallConfirm: '제거할까요? 모든 노드에서 즉시 언로드됩니다.',
- uninstalled: '플러그인을 제거했습니다',
- uninstallFailed: '제거하지 못했습니다',
- remote: '원격 서비스',
- remoteUrl: '서비스 URL',
- editUrl: '변경',
- urlSaved: '서비스 URL을 업데이트했습니다',
- urlSaveFailed: '서비스 URL을 업데이트하지 못했습니다',
- rotateSecret: '비밀 키 교체',
- rotateHint: '새 서명 비밀 키를 발급합니다. 서비스에 새 키를 설정하기 전까지 호출이 실패합니다.',
- rotateConfirm: '비밀 키를 교체할까요? 이전 키는 즉시 무효화됩니다.',
- rotateFailed: '비밀 키를 교체하지 못했습니다'
- }
- },
- pluginToolView: {
- empty: '결과가 없습니다'
- },
- pluginPages: {
- notResponding: '플러그인 페이지가 응답하지 않습니다. 로드에 실패했을 수 있습니다.',
- requestFailed: '플러그인 요청에 실패했습니다',
- fromPlugin: '플러그인 {id} 제공'
- },
- pluginCenter: {
- installed: '설치됨',
- configure: '설정',
- webhooks: 'Webhook URL',
- webhooksHint: '타사 서비스에 이 URL을 설정하면 이벤트가 이 플러그인으로 전송됩니다. 이 워크스페이스의 비밀이 포함되어 있으니 공개하지 마세요.',
- copy: '복사',
- webhookCopied: 'Webhook URL을 복사했습니다',
- configTitle: '{name} 설정',
- configDescription: '이 워크스페이스에서 플러그인을 사용하기 위한 설정(예: API 키). 비밀 값은 암호화되어 저장되며 다시 표시되지 않습니다.',
- configLoadFailed: '플러그인 설정을 불러오지 못했습니다',
- configSaved: '플러그인 설정을 저장했습니다',
- configSaveFailed: '플러그인 설정을 저장하지 못했습니다',
- navGroup: '확장',
- title: '플러그인',
- description: '이 배포에서 제공하는 모든 플러그인(내장 플러그인과 시스템 관리자가 설치한 플러그인). 이 워크스페이스에서 활성화하거나 비활성화할 수 있습니다. 비활성화된 플러그인의 연동은 유형 목록에서 빠지고 새로 만들 수 없지만 기존 항목은 계속 동작합니다. 설치된 플러그인은 워크스페이스 관리자가 활성화하기 전까지 비활성 상태입니다.',
- searchPlaceholder: '플러그인 이름, ID 또는 연동 검색',
- allPoints: '전체',
- empty: '일치하는 플러그인이 없습니다',
- builtin: '내장',
- required: '필수',
- requiredHint: 'WeKnora에 필요한 플러그인이라 끌 수 없습니다',
- adminOnly: '워크스페이스 관리자만 플러그인을 켜거나 끌 수 있습니다',
- loadFailed: '플러그인을 불러오지 못했습니다',
- saveFailed: '플러그인 설정을 저장하지 못했습니다',
- enabledToast: '플러그인을 켰습니다',
- disabledToast: '플러그인을 껐습니다',
- points: {
- modelVendors: '모델 공급사',
- connectors: '데이터 소스',
- imChannels: 'IM 채널',
- webSearch: '웹 검색',
- tools: '에이전트 도구',
- parsers: '문서 파싱',
- skills: '스킬',
- mcpServers: 'MCP 서버',
- pages: '페이지',
- settingsSections: '설정 섹션',
- kbTabs: '지식 베이스 탭',
- webhooks: 'Webhook'
- }
- },
- schemaForm: {
- errors: {
- required: '필수 항목입니다',
- type: '값이 올바르지 않습니다',
- enum: '허용된 값이 아닙니다',
- min_length: '너무 짧습니다',
- max_length: '너무 깁니다',
- pattern: '형식이 올바르지 않습니다',
- minimum: '값이 너무 작습니다',
- maximum: '값이 너무 큽니다',
- format: 'https://example.com 과 같은 전체 주소를 입력하세요'
- },
- options: {
- loading: '불러오는 중…',
- empty: '선택할 수 있는 항목이 없습니다',
- retry: '다시 시도',
- failed: '선택 항목을 불러오지 못했습니다'
- },
- oauth: {
- connected: '연결됨',
- notConnected: '연결되지 않음',
- connect: '계정 연결',
- reconnect: '다시 연결',
- disconnect: '연결 해제',
- popupBlocked: '브라우저가 인증 창을 차단했습니다. 팝업을 허용한 뒤 다시 시도하세요.',
- failed: '인증에 실패했습니다'
- }
- },
common: {
add: '추가',
me: '나',
@@ -6011,18 +5804,6 @@ export default {
baseUrlLabel: '인스턴스 URL',
baseUrlPlaceholder: 'https://searxng.example.com',
apiKeyPlaceholder: 'API 키를 입력하세요',
- configFields: {
- searchEngine: '검색 엔진',
- searchEngineDesc: 'Zhipu 검색 엔진과 요청당 요금 등급을 선택합니다.',
- searchStd: '기본 · ¥0.01/회',
- searchPro: '프로 · ¥0.03/회',
- searchSogou: 'Sogou · ¥0.05/회',
- searchQuark: 'Quark · ¥0.05/회',
- contentSize: '요약 길이',
- contentSizeDesc: '보통은 간결한 요약을, 자세히는 더 많은 맥락을 반환합니다.',
- contentMedium: '보통',
- contentHigh: '자세히'
- },
toasts: {
providerCreated: '검색 엔진 프로바이더가 생성되었습니다',
providerUpdated: '검색 엔진 프로바이더가 업데이트되었습니다',
@@ -6449,8 +6230,6 @@ export default {
},
},
skills: {
- pluginSection: '활성화된 플러그인에서',
- pluginSectionHint: '플러그인이 제공하는 스킬을 선택해 라이브러리에 추가한 뒤, 다른 스킬처럼 샌드박스에 설치할 수 있습니다.',
title: '스킬 관리',
description: '스킬은 워크스페이스 카탈로그에 속합니다. 먼저 등록한 뒤 하나 이상의 샌드박스에 설치할 수 있습니다. 에이전트는 현재 샌드박스에서 준비된 스킬만 사용할 수 있습니다.',
helpTooltip: '카탈로그 스킬은 아무 샌드박스에도 설치하지 않아도 됩니다. 스크립트를 실행하려면 에이전트가 쓰는 샌드박스 이미지에 설치해야 합니다. Docker, Cube, E2B 이미지는 호환되지 않으므로 샌드박스마다 따로 설치합니다.',
@@ -7373,8 +7152,6 @@ export default {
channelGitLab: 'GitLab',
channelIma: 'Tencent IMA',
channelUpload: '업로드',
- channelRss: 'RSS',
- channelExternal: '외부 데이터 소스',
channelManual: '수동',
channelUrl: '웹',
channelUnknown: '알 수 없음',
diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts
index 0af84311e..ebce12dd0 100755
--- a/frontend/src/i18n/locales/ru-RU.ts
+++ b/frontend/src/i18n/locales/ru-RU.ts
@@ -995,7 +995,6 @@ export default {
resourceHint: 'Выберите пространства или папки для синхронизации',
untitled: 'Без названия',
resourceLoadFailed: 'Не удалось загрузить список ресурсов',
- typesLoadFailed: 'Не удалось загрузить типы источников данных',
noResources: 'Пространства вики не найдены',
noResourcesDesc: 'Приложению требуется доступ к вики через групповой чат для получения контента',
noResourcesDesc_notion: 'Приложению требуются права доступа к странице Notion для получения контента',
@@ -2663,9 +2662,6 @@ export default {
}
},
mcpSettings: {
- transportPlugin: 'Встроено в плагин',
- fromPlugin: 'Плагин',
- pluginNotConfigured: 'Плагин ещё не настроен; заполните его настройки в разделе «Плагины»',
addUsageInstructions: "Добавить инструкции",
noUsageInstructions: "Инструкции пока не добавлены",
title: 'Сервисы MCP',
@@ -5252,209 +5248,6 @@ export default {
belongsToOrg: 'Пространство: ',
noCompatibleKbForAgent: 'Инструменты текущего агента не соответствуют возможностям ни одной базы знаний в области видимости — нечего упомянуть.'
},
- pluginAdmin: {
- runtime: {
- declarative: 'Декларативный',
- host: 'Локальный процесс',
- remote: 'Удалённый сервис'
- },
- title: 'Управление плагинами',
- description: 'Установка и управление плагинами всей платформы. Установленный плагин виден всем рабочим пространствам, и каждое включает его самостоятельно; отключение здесь выгружает плагин на всех узлах.',
- installButton: 'Установить плагин',
- empty: 'Плагины ещё не установлены',
- loadFailed: 'Не удалось загрузить установленные плагины',
- saveFailed: 'Не удалось сохранить',
- enabledToast: 'Включён для всей платформы',
- disabledToast: 'Отключён для всей платформы',
- platformSwitch: 'Включить или отключить для всей платформы',
- publisher: 'Издатель',
- contributions: 'Что добавляет',
- permissions: 'Разрешения и внешний доступ',
- state: {
- degraded: 'Перезапуск',
- running: 'Работает',
- failed: 'Ошибка загрузки',
- disabled: 'Отключён',
- pending: 'Загружается'
- },
- nodeState: {
- ready: 'Готов',
- starting: 'Запускается',
- degraded: 'Сбой',
- stopped: 'Не загружен'
- },
- source: {
- upload: 'Загрузка',
- url: 'URL'
- },
- change: {
- install: 'Новая установка',
- upgrade: 'Обновление с v{from}',
- downgrade: 'Откат с v{from}',
- reinstall: 'Переустановка'
- },
- permission: {
- anyHost: 'Любой публичный хост',
- remote: 'Удалённый сервис',
- egress: 'Исходящая сеть',
- hostApi: 'API WeKnora',
- events: 'События'
- },
- secret: {
- title: 'Секрет подписи плагина',
- description: 'Укажите этот секрет в переменной окружения WEKNORA_PLUGIN_SECRET сервиса плагина: по нему сервис проверяет, что запросы приходят от WeKnora. Секрет показывается только один раз и после закрытия недоступен.',
- copy: 'Копировать',
- copied: 'Скопировано',
- done: 'Я сохранил его'
- },
- install: {
- title: 'Установка плагина',
- description: 'Загрузите пакет .wkp или укажите URL для скачивания, проверьте и установите.',
- sourceSection: 'Пакет',
- mode: {
- upload: 'Загрузить файл',
- url: 'По URL'
- },
- fileLabel: 'Файл пакета',
- chooseFile: 'Выбрать файл',
- noFile: 'Файл не выбран',
- fileHint: 'Файл .wkp (zip с plugin.yaml), до 64 МБ.',
- urlLabel: 'URL для скачивания',
- urlHint: 'Сервер скачает этот URL; адреса частных сетей запрещены.',
- remoteUrlLabel: 'URL сервиса',
- remoteUrlHint: 'HTTP(S)-адрес сервиса плагина. Хосты частной сети должны быть указаны в SSRF_WHITELIST.',
- remoteUrlKeep: 'При обновлении оставьте пустым, чтобы сохранить текущий URL',
- reviewSection: 'Проверка перед установкой',
- noPermissions: 'Плагин не запрашивает дополнительных разрешений.',
- configNotice: 'Плагину нужны настройки: настройки платформы — в его карточке после установки, настройки рабочего пространства заполняют его администраторы в разделе «Плагины».',
- tenantNotice: 'После установки плагин виден всем рабочим пространствам, но выключен, пока администратор пространства его не включит.',
- digest: 'Дайджест пакета',
- inspect: 'Проверить пакет',
- confirm: {
- install: 'Установить',
- upgrade: 'Обновить',
- downgrade: 'Установить старую версию',
- reinstall: 'Переустановить'
- },
- inspectFailed: 'Не удалось прочитать пакет',
- failed: 'Не удалось установить',
- done: 'Установлен {name}'
- },
- detail: {
- overview: 'Обзор',
- runtime: 'Среда выполнения',
- source: 'Источник',
- homepage: 'Сайт',
- license: 'Лицензия',
- engines: 'Совместимые версии',
- nodes: 'Узлы',
- noNodes: 'Ни один узел не загрузил плагин',
- versions: 'Версии',
- active: 'Активная',
- activate: 'Переключиться на эту версию',
- rollback: 'Откатиться к этой версии',
- activateConfirm: 'Сделать v{version} активной версией? Все узлы перезагрузят плагин.',
- activated: 'Переключено на v{version}',
- activateFailed: 'Не удалось переключить версию',
- systemConfig: 'Настройки платформы',
- systemConfigHint: 'Действуют во всех рабочих пространствах, например регион сервиса или общий ключ платформы.',
- configLoadFailed: 'Не удалось загрузить настройки платформы',
- configSaved: 'Настройки платформы сохранены',
- configSaveFailed: 'Не удалось сохранить настройки платформы',
- danger: 'Удаление',
- uninstallHint: 'Удаляет плагин и все сохранённые версии. Переключатели и настройки рабочих пространств сохраняются и вернутся при повторной установке.',
- uninstall: 'Удалить плагин',
- uninstallConfirm: 'Удалить? Все узлы сразу выгрузят плагин.',
- uninstalled: 'Плагин удалён',
- uninstallFailed: 'Не удалось удалить',
- remote: 'Удалённый сервис',
- remoteUrl: 'URL сервиса',
- editUrl: 'Изменить',
- urlSaved: 'URL сервиса обновлён',
- urlSaveFailed: 'Не удалось обновить URL сервиса',
- rotateSecret: 'Сменить секрет',
- rotateHint: 'Выпускает новый секрет подписи. Вызовы сервиса будут завершаться ошибкой, пока он не получит новый секрет.',
- rotateConfirm: 'Сменить секрет? Старый сразу перестанет действовать.',
- rotateFailed: 'Не удалось сменить секрет'
- }
- },
- pluginToolView: {
- empty: 'Нет результатов'
- },
- pluginPages: {
- notResponding: 'Страница плагина не отвечает; возможно, она не загрузилась.',
- requestFailed: 'Запрос к плагину не выполнен',
- fromPlugin: 'Предоставлено плагином {id}'
- },
- pluginCenter: {
- installed: 'Установлен',
- configure: 'Настроить',
- webhooks: 'Адреса вебхуков',
- webhooksHint: 'Укажите эти адреса в стороннем сервисе, чтобы он отправлял события этому плагину. Они содержат секрет рабочего пространства; не публикуйте их.',
- copy: 'Копировать',
- webhookCopied: 'Адрес вебхука скопирован',
- configTitle: 'Настройки: {name}',
- configDescription: 'Настройки плагина для этого рабочего пространства, например API-ключ. Секреты хранятся в зашифрованном виде и больше не показываются.',
- configLoadFailed: 'Не удалось загрузить настройки плагина',
- configSaved: 'Настройки плагина сохранены',
- configSaveFailed: 'Не удалось сохранить настройки плагина',
- navGroup: 'Расширения',
- title: 'Плагины',
- description: 'Все плагины этого развёртывания: встроенные и установленные системным администратором. Включайте и отключайте их для этого рабочего пространства: интеграции отключённого плагина исчезают из списков типов и не создаются, а существующие продолжают работать. Установленные плагины выключены, пока администратор рабочего пространства их не включит.',
- searchPlaceholder: 'Поиск по названию, ID или интеграции',
- allPoints: 'Все',
- empty: 'Подходящих плагинов нет',
- builtin: 'Встроенный',
- required: 'Обязательный',
- requiredHint: 'Этот плагин нужен WeKnora, его нельзя отключить',
- adminOnly: 'Включать и отключать плагины могут только администраторы пространства',
- loadFailed: 'Не удалось загрузить плагины',
- saveFailed: 'Не удалось сохранить настройку плагина',
- enabledToast: 'Плагин включен',
- disabledToast: 'Плагин отключен',
- points: {
- modelVendors: 'Поставщики моделей',
- connectors: 'Источники данных',
- imChannels: 'IM-каналы',
- webSearch: 'Веб-поиск',
- tools: 'Инструменты агента',
- parsers: 'Разбор документов',
- skills: 'Навыки',
- mcpServers: 'MCP-серверы',
- pages: 'Страницы',
- settingsSections: 'Разделы настроек',
- kbTabs: 'Вкладки базы знаний',
- webhooks: 'Вебхуки'
- }
- },
- schemaForm: {
- errors: {
- required: 'Обязательное поле',
- type: 'Недопустимое значение',
- enum: 'Недопустимый вариант',
- min_length: 'Слишком коротко',
- max_length: 'Слишком длинно',
- pattern: 'Неверный формат',
- minimum: 'Слишком маленькое значение',
- maximum: 'Слишком большое значение',
- format: 'Введите полный адрес, например https://example.com'
- },
- options: {
- loading: 'Загрузка…',
- empty: 'Плагин не предлагает вариантов',
- retry: 'Повторить',
- failed: 'Не удалось загрузить варианты'
- },
- oauth: {
- connected: 'Подключено',
- notConnected: 'Не подключено',
- connect: 'Подключить',
- reconnect: 'Переподключить',
- disconnect: 'Отключить',
- popupBlocked: 'Браузер заблокировал окно авторизации. Разрешите всплывающие окна и повторите попытку.',
- failed: 'Не удалось авторизоваться'
- }
- },
common: {
add: 'Добавить',
me: 'Я',
@@ -6011,18 +5804,6 @@ export default {
baseUrlLabel: 'URL экземпляра',
baseUrlPlaceholder: 'https://searxng.example.com',
apiKeyPlaceholder: 'Введите API-ключ',
- configFields: {
- searchEngine: 'Поисковая система',
- searchEngineDesc: 'Выберите поисковую систему Zhipu и тариф за запрос.',
- searchStd: 'Стандарт · ¥0.01/запрос',
- searchPro: 'Pro · ¥0.03/запрос',
- searchSogou: 'Sogou · ¥0.05/запрос',
- searchQuark: 'Quark · ¥0.05/запрос',
- contentSize: 'Объём сводки',
- contentSizeDesc: 'Средний — краткие сводки; подробный — больше контекста.',
- contentMedium: 'Средний',
- contentHigh: 'Подробный'
- },
toasts: {
providerCreated: 'Поисковый провайдер создан',
providerUpdated: 'Поисковый провайдер обновлён',
@@ -6449,8 +6230,6 @@ export default {
},
},
skills: {
- pluginSection: 'Из включённых плагинов',
- pluginSectionHint: 'Выберите навык, который предоставляет плагин, чтобы добавить его в библиотеку, а затем установите в песочницы, как любой другой навык.',
title: 'Управление навыками',
description: 'Навыки живут в каталоге пространства. Их можно только зарегистрировать или установить в одну или несколько песочниц. Агент включает только навыки, которые готовы в выбранной песочнице.',
helpTooltip: 'Навык из каталога можно не устанавливать никуда. Скрипты запускаются только после установки в образ песочницы агента. Образы Docker, Cube и E2B несовместимы — устанавливайте отдельно в каждую песочницу.',
@@ -7373,8 +7152,6 @@ export default {
channelGitLab: 'GitLab',
channelIma: 'Tencent IMA',
channelUpload: 'Загрузка',
- channelRss: 'RSS',
- channelExternal: 'Внешний источник',
channelManual: 'Вручную',
channelUrl: 'Веб',
channelUnknown: 'Неизвестно',
diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts
index c87da71d3..f8301a076 100755
--- a/frontend/src/i18n/locales/zh-CN.ts
+++ b/frontend/src/i18n/locales/zh-CN.ts
@@ -997,7 +997,6 @@ export default {
resourceHint: '选择要同步的内容空间/文件夹',
untitled: '无标题',
resourceLoadFailed: '加载资源列表失败',
- typesLoadFailed: '加载数据源类型失败',
noResources: '未找到可同步的知识库空间',
noResourcesDesc: '应用需要通过群聊获得知识库访问权限才能拉取内容',
noResourcesDesc_notion: '应用需要获得 Notion 页面的访问权限才能拉取内容',
@@ -2665,9 +2664,6 @@ export default {
}
},
mcpSettings: {
- transportPlugin: '插件内置',
- fromPlugin: '插件',
- pluginNotConfigured: '插件尚未配置,请在插件中心填写配置后使用',
addUsageInstructions: "添加使用说明",
noUsageInstructions: "尚未填写使用说明",
title: 'MCP 服务管理',
@@ -5254,209 +5250,6 @@ export default {
belongsToOrg: '所属空间:',
noCompatibleKbForAgent: '当前智能体的工具与作用域内知识库的能力不匹配,暂无可引用的知识库。'
},
- pluginAdmin: {
- runtime: {
- declarative: '声明式',
- host: '本机进程',
- remote: '远程服务'
- },
- title: '插件管理',
- description: '安装与管理本平台的插件。安装后所有空间都能看到,但每个空间需自行启用;停用会在所有节点卸载该插件。',
- installButton: '安装插件',
- empty: '还没有安装任何插件',
- loadFailed: '加载已安装插件失败',
- saveFailed: '保存失败',
- enabledToast: '已在全平台启用',
- disabledToast: '已在全平台停用',
- platformSwitch: '全平台启用 / 停用',
- publisher: '发布者',
- contributions: '提供的能力',
- permissions: '权限与外部访问',
- state: {
- degraded: '重启中',
- running: '运行中',
- failed: '加载失败',
- disabled: '已停用',
- pending: '加载中'
- },
- nodeState: {
- ready: '就绪',
- starting: '启动中',
- degraded: '异常',
- stopped: '未加载'
- },
- source: {
- upload: '上传',
- url: 'URL'
- },
- change: {
- install: '新安装',
- upgrade: '从 v{from} 升级',
- downgrade: '从 v{from} 降级',
- reinstall: '重新安装'
- },
- permission: {
- anyHost: '任意公网地址',
- remote: '访问远程服务',
- egress: '网络出口',
- hostApi: '调用 WeKnora API',
- events: '订阅事件'
- },
- secret: {
- title: '插件签名密钥',
- description: '把此密钥配置为插件服务的环境变量 WEKNORA_PLUGIN_SECRET,服务据此确认请求来自 WeKnora。密钥只显示这一次,关闭后无法再查看。',
- copy: '复制',
- copied: '已复制',
- done: '我已保存'
- },
- install: {
- title: '安装插件',
- description: '上传 .wkp 插件包或填写下载地址,审阅后安装。',
- sourceSection: '插件包',
- mode: {
- upload: '上传文件',
- url: '从 URL 下载'
- },
- fileLabel: '插件包文件',
- chooseFile: '选择文件',
- noFile: '未选择文件',
- fileHint: '.wkp 文件(包含 plugin.yaml 的 zip),最大 64 MB。',
- urlLabel: '下载地址',
- urlHint: '服务端会下载该地址;不允许内网地址。',
- remoteUrlLabel: '服务地址',
- remoteUrlHint: '插件服务的 HTTP(S) 地址。内网地址需要加入 SSRF_WHITELIST。',
- remoteUrlKeep: '升级时可留空,沿用当前地址',
- reviewSection: '安装前审阅',
- noPermissions: '该插件不申请任何额外权限。',
- configNotice: '该插件需要配置:平台配置在安装后的详情中填写,空间配置由各空间管理员在插件中心填写。',
- tenantNotice: '安装后插件对所有空间可见,但默认停用,由各空间管理员自行启用。',
- digest: '包摘要',
- inspect: '检查插件包',
- confirm: {
- install: '安装',
- upgrade: '升级',
- downgrade: '降级安装',
- reinstall: '重新安装'
- },
- inspectFailed: '无法读取插件包',
- failed: '安装失败',
- done: '已安装 {name}'
- },
- detail: {
- overview: '概览',
- runtime: '运行方式',
- source: '来源',
- homepage: '主页',
- license: '许可证',
- engines: '兼容版本',
- nodes: '节点状态',
- noNodes: '当前没有节点加载该插件',
- versions: '版本',
- active: '当前版本',
- activate: '切换到此版本',
- rollback: '回滚到此版本',
- activateConfirm: '将当前版本切换为 v{version}?所有节点会重新加载。',
- activated: '已切换到 v{version}',
- activateFailed: '切换版本失败',
- systemConfig: '平台配置',
- systemConfigHint: '对所有空间生效,例如服务区域或平台统一的凭证。',
- configLoadFailed: '加载平台配置失败',
- configSaved: '平台配置已保存',
- configSaveFailed: '保存平台配置失败',
- danger: '卸载',
- uninstallHint: '删除插件及其全部版本;各空间的开关和配置会保留,重新安装后恢复。',
- uninstall: '卸载插件',
- uninstallConfirm: '确定卸载?所有节点会立即卸载该插件。',
- uninstalled: '插件已卸载',
- uninstallFailed: '卸载失败',
- remote: '远程服务',
- remoteUrl: '服务地址',
- editUrl: '修改',
- urlSaved: '服务地址已更新',
- urlSaveFailed: '更新服务地址失败',
- rotateSecret: '轮换密钥',
- rotateHint: '生成新的签名密钥。服务换上新密钥之前,对它的调用会失败。',
- rotateConfirm: '确定轮换?旧密钥立即失效。',
- rotateFailed: '轮换密钥失败'
- }
- },
- pluginToolView: {
- empty: '没有结果'
- },
- pluginPages: {
- notResponding: '插件页面没有响应,可能加载失败。',
- requestFailed: '插件请求失败',
- fromPlugin: '由插件 {id} 提供'
- },
- pluginCenter: {
- installed: '已安装',
- configure: '配置',
- webhooks: 'Webhook 地址',
- webhooksHint: '在第三方系统里配置这些地址,它们会把事件推送给本插件。地址包含本空间的密钥,请勿公开。',
- copy: '复制',
- webhookCopied: '已复制 Webhook 地址',
- configTitle: '配置 {name}',
- configDescription: '本空间使用该插件时的配置,例如 API 密钥。密钥加密保存,不会再次显示。',
- configLoadFailed: '加载插件配置失败',
- configSaved: '插件配置已保存',
- configSaveFailed: '保存插件配置失败',
- navGroup: '扩展',
- title: '插件',
- description: '本部署提供的所有插件,含内置插件与平台管理员安装的插件。在这里为当前空间启用或停用:停用后其集成不再出现在类型列表中、不能新建,已有的照常工作。安装的插件默认停用,需空间管理员启用。',
- searchPlaceholder: '搜索插件名称、ID 或集成',
- allPoints: '全部',
- empty: '没有匹配的插件',
- builtin: '内置',
- required: '必需',
- requiredHint: 'WeKnora 运行所必需,不能停用',
- adminOnly: '仅空间管理员可以启用或停用插件',
- loadFailed: '加载插件列表失败',
- saveFailed: '保存插件设置失败',
- enabledToast: '插件已启用',
- disabledToast: '插件已停用',
- points: {
- modelVendors: '模型厂商',
- connectors: '数据源',
- imChannels: 'IM 渠道',
- webSearch: '联网搜索',
- tools: 'Agent 工具',
- parsers: '文档解析',
- skills: '技能',
- mcpServers: 'MCP 服务',
- pages: '页面',
- settingsSections: '设置页',
- kbTabs: '知识库页签',
- webhooks: 'Webhook'
- }
- },
- schemaForm: {
- errors: {
- required: '此项为必填',
- type: '格式不正确',
- enum: '不是可选的值',
- min_length: '长度过短',
- max_length: '长度过长',
- pattern: '格式不符合要求',
- minimum: '数值过小',
- maximum: '数值过大',
- format: '请输入完整的地址,如 https://example.com'
- },
- options: {
- loading: '加载中…',
- empty: '插件没有可选项',
- retry: '重试',
- failed: '加载可选项失败'
- },
- oauth: {
- connected: '已连接',
- notConnected: '未连接',
- connect: '连接账号',
- reconnect: '重新连接',
- disconnect: '断开',
- popupBlocked: '浏览器拦截了授权窗口,请允许弹出窗口后重试',
- failed: '授权失败'
- }
- },
common: {
add: '添加',
me: '我',
@@ -6013,18 +5806,6 @@ export default {
baseUrlLabel: '实例地址',
baseUrlPlaceholder: 'https://searxng.example.com',
apiKeyPlaceholder: '请输入 API 密钥',
- configFields: {
- searchEngine: '搜索引擎',
- searchEngineDesc: '选择智谱搜索引擎及对应的单次调用价格档位。',
- searchStd: '基础版 · ¥0.01/次',
- searchPro: '高级版 · ¥0.03/次',
- searchSogou: '搜狗 · ¥0.05/次',
- searchQuark: '夸克 · ¥0.05/次',
- contentSize: '摘要长度',
- contentSizeDesc: '中等返回精简摘要;详细返回更多上下文。',
- contentMedium: '中等',
- contentHigh: '详细'
- },
toasts: {
providerCreated: '搜索引擎配置已创建',
providerUpdated: '搜索引擎配置已更新',
@@ -6451,8 +6232,6 @@ export default {
},
},
skills: {
- pluginSection: '来自已启用的插件',
- pluginSectionHint: '选择一个插件提供的技能加入技能库,之后可像其他技能一样安装到沙箱。',
title: '技能管理',
description: '技能属于空间目录,可以只登记,也可以装到一份或多份沙箱。智能体只能启用当前沙箱里已就绪的技能。',
helpTooltip: '目录里的技能可以不装任何沙箱。脚本要跑起来,必须装进智能体所用的那份沙箱镜像。Docker、Cube、E2B 互不通用,装到几份就要装几次。',
@@ -7375,8 +7154,6 @@ export default {
channelGitLab: 'GitLab',
channelIma: '腾讯 IMA',
channelUpload: '上传',
- channelRss: 'RSS 订阅',
- channelExternal: '外部数据源',
channelManual: '手动',
channelUrl: '网页',
channelUnknown: '未知',
diff --git a/frontend/src/stores/auth.ts b/frontend/src/stores/auth.ts
index fbb4d396b..488bd45d3 100644
--- a/frontend/src/stores/auth.ts
+++ b/frontend/src/stores/auth.ts
@@ -11,14 +11,12 @@ import { BUILTIN_QUICK_ANSWER_ID } from '@/api/agent'
import { useChatResourcesStore } from '@/stores/chatResources'
import { useEditorResourcesStore } from '@/stores/editorResources'
import { useOrganizationStore } from '@/stores/organization'
-import { usePluginPagesStore } from '@/stores/pluginPages'
import { createSequencedRefresh } from '@/stores/sequencedRefresh'
/** 登出时丢弃 Pinia 内的空间级资源缓存,避免 SPA 重登复用上一账号数据。 */
function clearSessionResourceCaches() {
useChatResourcesStore().invalidate()
useEditorResourcesStore().invalidate()
- usePluginPagesStore().invalidate()
useOrganizationStore().clearState()
}
diff --git a/frontend/src/stores/modelProvidersState.test.ts b/frontend/src/stores/modelProvidersState.test.ts
index 06423d6bc..ed1c29e21 100644
--- a/frontend/src/stores/modelProvidersState.test.ts
+++ b/frontend/src/stores/modelProvidersState.test.ts
@@ -12,7 +12,6 @@ import {
loadProvidersForType,
mergeProviderIndex,
normalizeModelType,
- plainExtraConfigSchema,
pickLocalized,
providerDescription,
providerIcon,
@@ -259,22 +258,3 @@ test('extraFieldPlaceholder resolves the locale and tolerates no placeholder', (
)
assert.equal(extraFieldPlaceholder({ key: 'k', label: 'k', type: 'string' }, 'zh-CN'), '')
})
-
-test('plainExtraConfigSchema keeps plain fields for the model type', () => {
- const schema = {
- type: 'object' as const,
- required: ['region', 'secret_key'],
- properties: {
- region: { type: 'string' as const, oneOf: [{ const: 'bj', title: 'Beijing' }] },
- secret_key: { type: 'string' as const, 'x-secret': true, 'x-model-types': ['Rerank'] },
- instruction: { type: 'string' as const, 'x-model-types': ['Rerank'] },
- thinking_control: { type: 'string' as const },
- },
- }
- const rerank = plainExtraConfigSchema(schema, 'rerank', new Set(['thinking_control']))
- assert.deepEqual(Object.keys(rerank.properties!).sort(), ['instruction', 'region'])
- assert.deepEqual(rerank.required, ['region'])
- const chat = plainExtraConfigSchema(schema, 'chat', new Set(['thinking_control']))
- assert.deepEqual(Object.keys(chat.properties!), ['region'])
- assert.deepEqual(plainExtraConfigSchema(undefined, 'chat').properties, {})
-})
diff --git a/frontend/src/stores/modelProvidersState.ts b/frontend/src/stores/modelProvidersState.ts
index c79c06295..202b43d90 100644
--- a/frontend/src/stores/modelProvidersState.ts
+++ b/frontend/src/stores/modelProvidersState.ts
@@ -11,7 +11,6 @@ import type {
ModelProviderExtraField,
ModelProviderOption,
} from '@/api/initialization'
-import type { ConfigSchema } from '@/components/schema-form/schema'
/** Editor model types → backend ModelType names used by ExtraField.model_types. */
const FRONTEND_TO_BACKEND_MODEL_TYPE: Record<string, string> = {
@@ -42,30 +41,6 @@ export function extraFieldAppliesTo(field: Pick<ModelProviderExtraField, 'model_
return restricted.some((entry) => normalizeModelType(entry) === wanted)
}
-/**
- * A vendor's config schema narrowed to the plain extra_config fields shown at
- * one model type. Secret fields are left out (they fill the app_secret
- * credential slot, edited separately), and so are keys the editor controls
- * itself.
- */
-export function plainExtraConfigSchema(
- schema: ConfigSchema | null | undefined,
- modelType: string,
- reservedKeys: ReadonlySet<string> = new Set(),
-): ConfigSchema {
- const properties: Record<string, ConfigSchema> = {}
- for (const [key, prop] of Object.entries(schema?.properties ?? {})) {
- if (prop['x-secret'] || reservedKeys.has(key)) continue
- if (!extraFieldAppliesTo({ model_types: prop['x-model-types'] }, modelType)) continue
- properties[key] = prop
- }
- return {
- type: 'object',
- properties,
- required: (schema?.required ?? []).filter((key) => key in properties),
- }
-}
-
/** Extra fields to render for a provider at one model type. */
export function extraFieldsForModelType(
fields: ReadonlyArray<ModelProviderExtraField> | null | undefined,
diff --git a/frontend/src/stores/pluginPages.ts b/frontend/src/stores/pluginPages.ts
deleted file mode 100644
index f943b8959..000000000
--- a/frontend/src/stores/pluginPages.ts
+++ /dev/null
@@ -1,55 +0,0 @@
-import { computed, ref } from 'vue'
-import { defineStore } from 'pinia'
-
-import { listContributions, type ContributionListing } from '@/api/plugin'
-import { useAuthStore } from '@/stores/auth'
-import { pagesOf, type PagePoint, type PluginPage } from '@/extensions/pluginFrame/pluginPages'
-
-import { createCachedResource } from './resourceCache'
-
-/**
- * The plugin pages of the current workspace (toolbox pages, settings
- * sections, knowledge base tabs). No TTL: reloaded when the workspace
- * changes, and invalidated when a plugin is switched on or off.
- */
-export const usePluginPagesStore = defineStore('pluginPages', () => {
- const listing = ref<ContributionListing | null>(null)
- const loadedFor = ref<string | number | null>(null)
- const auth = useAuthStore()
-
- const resource = createCachedResource(
- async () => (await listContributions()).data,
- (value) => {
- listing.value = value
- },
- )
-
- /** Loads the pages for the current workspace, if not loaded yet. */
- async function ensure(force = false) {
- const tenant = auth.currentTenantId ?? null
- if (tenant !== loadedFor.value) {
- resource.invalidate()
- listing.value = null
- loadedFor.value = tenant
- }
- if (force || !resource.isLoaded()) await resource.ensure(force)
- }
-
- function invalidate() {
- resource.invalidate()
- loadedFor.value = null
- }
-
- const visible = (point: PagePoint) =>
- computed<PluginPage[]>(() =>
- pagesOf(listing.value, point).filter((p) => auth.canAccessAllTenants || auth.hasRole(p.minRole)),
- )
-
- return {
- ensure,
- invalidate,
- pages: visible('pages'),
- settingsSections: visible('settingsSections'),
- kbTabs: visible('kbTabs'),
- }
-})
diff --git a/frontend/src/types/tool-results.ts b/frontend/src/types/tool-results.ts
index 4a75016d7..2dac12514 100644
--- a/frontend/src/types/tool-results.ts
+++ b/frontend/src/types/tool-results.ts
@@ -32,8 +32,7 @@ export type DisplayType =
| 'edit_sandbox_file'
| 'read_skill'
| 'mcp_discovery'
- | 'mcp_call'
- | 'plugin_tool_view';
+ | 'mcp_call';
// Search result item
export interface SearchResultItem {
diff --git a/frontend/src/utils/localizedText.test.ts b/frontend/src/utils/localizedText.test.ts
deleted file mode 100644
index 90e9e3685..000000000
--- a/frontend/src/utils/localizedText.test.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import assert from 'node:assert/strict'
-import test from 'node:test'
-
-import { localizedText } from './localizedText'
-
-test('localizedText resolves exact locale, then language, then default', () => {
- const text = { default: 'Feishu', 'zh-CN': '飞书' }
- assert.equal(localizedText(text, 'zh-CN'), '飞书')
- assert.equal(localizedText(text, 'zh-TW'), '飞书')
- assert.equal(localizedText(text, 'en-US'), 'Feishu')
- assert.equal(localizedText(undefined, 'en-US'), '')
-})
diff --git a/frontend/src/utils/localizedText.ts b/frontend/src/utils/localizedText.ts
deleted file mode 100644
index 6be9a9c58..000000000
--- a/frontend/src/utils/localizedText.ts
+++ /dev/null
@@ -1,26 +0,0 @@
-/** Text with locale variants, as plugin manifests serve it: { default, 'zh-CN': ... }. */
-export type LocalizedText = { default: string } & Record<string, string>
-
-/** Resolves localized text for a locale: exact, then same language, then default. */
-export function localizedText(text: LocalizedText | undefined, locale: string): string {
- if (!text) return ''
- if (text[locale]) return text[locale]
- const lang = locale.split('-')[0]
- const match = Object.keys(text).sort().find((k) => k !== 'default' && k.split('-')[0] === lang && text[k])
- return match ? text[match] : text.default
-}
-
-/**
- * Picks a variant from a locale map without a default ({ 'zh-CN': ... }, as
- * plugin connectors and provider types send names): exact, then same
- * language, else undefined.
- */
-export function pickLocale(variants: Record<string, string> | undefined, locale: string): string | undefined {
- if (!variants) return undefined
- if (variants[locale]) return variants[locale]
- const lang = locale.split('-')[0].toLowerCase()
- const key = Object.keys(variants)
- .sort()
- .find((k) => k.split('-')[0].toLowerCase() === lang)
- return key ? variants[key] : undefined
-}
diff --git a/frontend/src/utils/tool-capabilities.ts b/frontend/src/utils/tool-capabilities.ts
index aa1680e97..6f7ff91a9 100644
--- a/frontend/src/utils/tool-capabilities.ts
+++ b/frontend/src/utils/tool-capabilities.ts
@@ -23,8 +23,7 @@
*
* IMPORTANT: keep this list aligned with backend tool definitions
* (`internal/agent/tools/`; this map mirrors
- * `internal/agent/tools/capabilities.go`, and
- * `capabilities_frontend_test.go` there fails when the two differ). A tool missing from this map
+ * `internal/agent/tools/capabilities.go`). A tool missing from this map
* defaults to "always available" so new tools don't silently start disabled.
*/
diff --git a/frontend/src/views/chat/components/AgentStreamDisplay.vue b/frontend/src/views/chat/components/AgentStreamDisplay.vue
index 14e344eb3..76862d98e 100644
--- a/frontend/src/views/chat/components/AgentStreamDisplay.vue
+++ b/frontend/src/views/chat/components/AgentStreamDisplay.vue
@@ -1221,9 +1221,6 @@ const formatToolResultContent = (value: unknown): string => {
const isMcpTool = (toolName?: string | null): boolean => String(toolName || '').startsWith('mcp_');
const resolveToolDisplayType = (event: any): DisplayType | undefined => {
- // A plugin's own result view beats the generic MCP call view, also when
- // the tool was reached through call_mcp_tool.
- if (isPluginToolView(event)) return 'plugin_tool_view'
const mcpType = getMcpToolDisplayType(event?.tool_name)
if (mcpType) return mcpType
if (event?.display_type) return event.display_type as DisplayType
@@ -1323,13 +1320,8 @@ const buildToolResultReference = (
}];
};
-// A plugin tool whose result has a view expands in place to show it,
-// instead of opening the text-only references drawer MCP tools use.
-const isPluginToolView = (event: any): boolean =>
- event?.display_type === 'plugin_tool_view' || event?.tool_data?.display_type === 'plugin_tool_view';
-
function getToolReferenceItems(event: any): KnowledgeReferenceLike[] {
- if (!event || event.pending || isPluginToolView(event)) return [];
+ if (!event || event.pending) return [];
const toolName = event.tool_name;
const toolData = event.tool_data;
@@ -2247,7 +2239,6 @@ const isReferenceDrawerTool = (toolName?: string | null): boolean =>
toolName === 'wiki_read_source_doc';
const hasExpandableResults = (event: any): boolean => {
- if (isPluginToolView(event)) return !event.pending;
if (isReferenceDrawerTool(event?.tool_name)) return false;
return hasResults(event);
};
diff --git a/frontend/src/views/chat/components/ToolResultRenderer.vue b/frontend/src/views/chat/components/ToolResultRenderer.vue
index d21b31931..26efe10f4 100644
--- a/frontend/src/views/chat/components/ToolResultRenderer.vue
+++ b/frontend/src/views/chat/components/ToolResultRenderer.vue
@@ -1,7 +1,78 @@
<template>
<div class="tool-result-renderer">
- <!-- Views come from the tool result registry: builtins plus plugin views. -->
- <component :is="view.component" v-if="view" v-bind="viewProps" />
+ <!-- Search Results -->
+ <SearchResults v-if="displayType === 'search_results'" :data="toolData as SearchResultsData"
+ :arguments="toolArguments" />
+
+ <!-- Chunk Detail -->
+ <ChunkDetail v-else-if="displayType === 'chunk_detail'" :data="toolData as ChunkDetailData" />
+
+ <!-- Related Chunks -->
+ <RelatedChunks v-else-if="displayType === 'related_chunks'" :data="toolData as RelatedChunksData" />
+
+ <!-- Knowledge Base List -->
+ <KnowledgeBaseList v-else-if="displayType === 'knowledge_base_list'" :data="toolData as KnowledgeBaseListData" />
+
+ <!-- Document Info -->
+ <DocumentInfo v-else-if="displayType === 'document_info'" :data="toolData as DocumentInfoData" />
+
+ <!-- Graph Query Results -->
+ <GraphQueryResults v-else-if="displayType === 'graph_query_results'" :data="toolData as GraphQueryResultsData" />
+
+ <!-- Thinking Display -->
+ <ThinkingDisplay v-else-if="displayType === 'thinking'" :data="toolData as ThinkingData" />
+
+ <!-- Plan Display -->
+ <PlanDisplay v-else-if="displayType === 'plan'" :data="toolData as PlanData" />
+
+ <!-- Database Query Display -->
+ <DatabaseQuery v-else-if="displayType === 'database_query'" :data="toolData as DatabaseQueryData" />
+
+ <!-- Web Search Results Display -->
+ <WebSearchResults v-else-if="displayType === 'web_search_results'" :data="toolData as WebSearchResultsData" />
+
+ <!-- Web Fetch Results Display -->
+ <WebFetchResults v-else-if="displayType === 'web_fetch_results'" :data="toolData as WebFetchResultsData" />
+
+ <!-- Grep Results Display -->
+ <GrepResults v-else-if="displayType === 'grep_results'" :data="toolData as GrepResultsData" />
+
+ <!-- Knowledge Chunks List -->
+ <KnowledgeChunksList v-else-if="displayType === 'knowledge_chunks_list'"
+ :data="toolData as KnowledgeChunksListData" />
+
+ <!-- Wiki Edit Results Display -->
+ <WikiEditResult
+ v-else-if="displayType === 'wiki_write_page' || displayType === 'wiki_replace_text' || displayType === 'wiki_rename_page' || displayType === 'wiki_delete_page'"
+ :data="toolData as WikiEditData" />
+
+ <ShellExecResult
+ v-else-if="displayType === 'shell_exec'"
+ :data="toolData as ShellExecData"
+ :output="output"
+ :arguments="toolArguments"
+ />
+
+ <SandboxFilesResult
+ v-else-if="displayType === 'list_sandbox_files'"
+ :data="toolData as ListSandboxFilesData"
+ />
+
+ <WriteSandboxFileResult
+ v-else-if="displayType === 'write_sandbox_file' || displayType === 'edit_sandbox_file'"
+ :data="toolData as WriteSandboxFileData"
+ />
+
+ <ReadSkillResult
+ v-else-if="displayType === 'read_skill'"
+ :data="toolData as ReadSkillData"
+ />
+
+ <McpToolResult
+ v-else-if="displayType === 'mcp_discovery' || displayType === 'mcp_call'"
+ :discovery="displayType === 'mcp_discovery'" :data="toolData"
+ :output="output" :arguments="toolArguments" :success="success"
+ />
<!-- Fallback: Display raw output -->
<div v-else class="fallback-output">
@@ -17,11 +88,47 @@
<script setup lang="ts">
import { computed } from 'vue';
-import type { DisplayType } from '@/types/tool-results';
-import { registerBuiltinToolResultViews } from '@/extensions/builtin/toolResultViews';
-import { toolResultProps, toolResultViews } from '@/extensions/toolResultViews';
-
-registerBuiltinToolResultViews();
+import type {
+ DisplayType,
+ SearchResultsData,
+ ChunkDetailData,
+ RelatedChunksData,
+ KnowledgeBaseListData,
+ DocumentInfoData,
+ GraphQueryResultsData,
+ ThinkingData,
+ PlanData,
+ DatabaseQueryData,
+ WebSearchResultsData,
+ WebFetchResultsData,
+ GrepResultsData,
+ KnowledgeChunksListData,
+ WikiEditData,
+ ShellExecData,
+ ListSandboxFilesData,
+ WriteSandboxFileData,
+ ReadSkillData
+} from '@/types/tool-results';
+
+import SearchResults from './tool-results/SearchResults.vue';
+import ChunkDetail from './tool-results/ChunkDetail.vue';
+import RelatedChunks from './tool-results/RelatedChunks.vue';
+import KnowledgeBaseList from './tool-results/KnowledgeBaseList.vue';
+import DocumentInfo from './tool-results/DocumentInfo.vue';
+import GraphQueryResults from './tool-results/GraphQueryResults.vue';
+import ThinkingDisplay from './tool-results/ThinkingDisplay.vue';
+import PlanDisplay from './tool-results/PlanDisplay.vue';
+import DatabaseQuery from './tool-results/DatabaseQuery.vue';
+import WebSearchResults from './tool-results/WebSearchResults.vue';
+import WebFetchResults from './tool-results/WebFetchResults.vue';
+import GrepResults from './tool-results/GrepResults.vue';
+import KnowledgeChunksList from './tool-results/KnowledgeChunksList.vue';
+import WikiEditResult from './tool-results/WikiEditResult.vue';
+import ShellExecResult from './tool-results/ShellExecResult.vue';
+import SandboxFilesResult from './tool-results/SandboxFilesResult.vue';
+import WriteSandboxFileResult from './tool-results/WriteSandboxFileResult.vue';
+import ReadSkillResult from './tool-results/ReadSkillResult.vue';
+import McpToolResult from './tool-results/McpToolResult.vue';
interface Props {
success?: boolean;
@@ -33,20 +140,10 @@ interface Props {
const props = withDefaults(defineProps<Props>(), { success: undefined });
+const displayType = computed(() => props.displayType);
+const toolData = computed(() => props.toolData || {});
const output = computed(() => props.output || '');
-
-const view = computed(() => (props.displayType ? toolResultViews.get(props.displayType) : undefined));
-const viewProps = computed(() =>
- view.value
- ? toolResultProps(view.value, {
- displayType: props.displayType ?? '',
- data: props.toolData || {},
- output: output.value,
- arguments: props.arguments || {},
- success: props.success,
- })
- : {},
-);
+const toolArguments = computed(() => props.arguments || {});
</script>
<style lang="less" scoped>
diff --git a/frontend/src/views/chat/components/tool-results/PluginToolView.vue b/frontend/src/views/chat/components/tool-results/PluginToolView.vue
deleted file mode 100644
index ebd2c15f1..000000000
--- a/frontend/src/views/chat/components/tool-results/PluginToolView.vue
+++ /dev/null
@@ -1,252 +0,0 @@
-<template>
- <div class="plugin-tool-view">
- <div v-if="success === false" class="plugin-tool-view__error" role="alert">
- <t-icon name="error-circle" />
- <pre>{{ output }}</pre>
- </div>
-
- <template v-else-if="spec.view === 'table'">
- <p v-if="!table.rows.length" class="plugin-tool-view__empty">{{ t('pluginToolView.empty') }}</p>
- <div v-else class="plugin-tool-view__table-wrap">
- <table class="plugin-tool-view__table">
- <thead>
- <tr><th v-for="(h, i) in table.headers" :key="i">{{ h }}</th></tr>
- </thead>
- <tbody>
- <tr v-for="(row, r) in table.rows" :key="r">
- <td v-for="(cell, c) in row" :key="c">
- <a v-if="cell.href" :href="cell.href" target="_blank" rel="noopener noreferrer">{{ cell.text }}</a>
- <template v-else>{{ cell.text }}</template>
- </td>
- </tr>
- </tbody>
- </table>
- </div>
- </template>
-
- <template v-else-if="spec.view === 'cards'">
- <p v-if="!cards.length" class="plugin-tool-view__empty">{{ t('pluginToolView.empty') }}</p>
- <div v-else class="plugin-tool-view__cards">
- <article v-for="(card, i) in cards" :key="i" class="plugin-tool-view__card">
- <a v-if="card.href" class="plugin-tool-view__card-title" :href="card.href" target="_blank" rel="noopener noreferrer">{{ card.title }}</a>
- <div v-else class="plugin-tool-view__card-title">{{ card.title }}</div>
- <div v-if="card.subtitle" class="plugin-tool-view__card-subtitle">{{ card.subtitle }}</div>
- <p v-if="card.body" class="plugin-tool-view__card-body">{{ card.body }}</p>
- </article>
- </div>
- </template>
-
- <template v-else-if="spec.view === 'kv'">
- <div v-if="kv.title" class="plugin-tool-view__kv-title">
- <a v-if="kv.href" :href="kv.href" target="_blank" rel="noopener noreferrer">{{ kv.title }}</a>
- <template v-else>{{ kv.title }}</template>
- </div>
- <dl class="plugin-tool-view__kv">
- <template v-for="(row, i) in kv.rows" :key="i">
- <dt>{{ row.label }}</dt>
- <dd>
- <a v-if="row.href" :href="row.href" target="_blank" rel="noopener noreferrer">{{ row.text }}</a>
- <template v-else>{{ row.text }}</template>
- </dd>
- </template>
- </dl>
- </template>
-
- <!-- eslint-disable-next-line vue/no-v-html -- sanitized by renderDocumentPreviewMarkdown -->
- <div v-else-if="spec.view === 'markdown'" class="plugin-tool-view__markdown" v-html="markdownHtml" />
-
- <PluginFrame v-else-if="spec.view === 'page' && page" :page="page" :context="pageContext" />
-
- <pre v-else class="plugin-tool-view__json">{{ json }}</pre>
- </div>
-</template>
-
-<script setup lang="ts">
-import { computed } from 'vue'
-import { useI18n } from 'vue-i18n'
-
-import PluginFrame from '@/extensions/pluginFrame/PluginFrame.vue'
-import type { FramePage } from '@/extensions/pluginFrame/pluginPages'
-import {
- cardsModel,
- kvModel,
- markdownSource,
- tableModel,
- toolArguments,
- type PluginToolViewData,
- type ToolViewSpec,
-} from '@/extensions/pluginToolView'
-import { renderDocumentPreviewMarkdown } from '@/utils/documentPreviewMarkdown'
-
-// A plugin tool's structured result, shown with the view its plugin
-// declared in toolViews: generic views need no plugin code; a page view runs
-// the plugin's own page in a sandboxed frame.
-const props = defineProps<{
- data: PluginToolViewData & Record<string, unknown>
- output?: string
- arguments?: Record<string, unknown>
- success?: boolean
-}>()
-
-const { t, locale } = useI18n()
-
-const spec = computed<ToolViewSpec>(() => props.data.plugin_view ?? { view: 'json' })
-const structured = computed(() => props.data.structured)
-const output = computed(() => props.output || '')
-
-const table = computed(() => tableModel(spec.value, structured.value, locale.value))
-const cards = computed(() => cardsModel(spec.value, structured.value))
-const kv = computed(() => kvModel(spec.value, structured.value, locale.value))
-const markdownHtml = computed(() =>
- renderDocumentPreviewMarkdown(markdownSource(spec.value, structured.value, output.value)),
-)
-const json = computed(() => {
- try {
- return JSON.stringify(structured.value, null, 2)
- } catch {
- return String(structured.value)
- }
-})
-
-const page = computed<FramePage | null>(() => {
- const d = props.data
- if (!d.plugin_id || !d.plugin_version || !d.mcp_server || !spec.value.entry) return null
- return {
- pluginId: d.plugin_id,
- version: d.plugin_version,
- // Requests from the page answer to the tool's server.
- mount: `mcpServers/${d.mcp_server}`,
- entry: spec.value.entry,
- name: { default: d.mcp_tool || d.plugin_id },
- }
-})
-const pageContext = computed(() => ({
- tool: props.data.mcp_tool,
- arguments: toolArguments(props.arguments),
- result: structured.value,
-}))
-</script>
-
-<style lang="less" scoped>
-.plugin-tool-view {
- margin: 8px 0;
- font-size: var(--app-text-md);
- color: var(--td-text-color-primary);
-}
-
-.plugin-tool-view__empty {
- margin: 0;
- color: var(--td-text-color-placeholder);
-}
-
-.plugin-tool-view__error {
- display: flex;
- gap: 8px;
- color: var(--td-error-color);
-
- pre {
- margin: 0;
- white-space: pre-wrap;
- word-break: break-word;
- }
-}
-
-.plugin-tool-view__table-wrap {
- max-height: 360px;
- overflow: auto;
- border: 1px solid var(--td-component-stroke);
- border-radius: var(--app-radius-sm);
-}
-
-.plugin-tool-view__table {
- width: 100%;
- border-collapse: collapse;
-
- th,
- td {
- padding: 6px 10px;
- text-align: left;
- vertical-align: top;
- border-bottom: 1px solid var(--td-component-stroke);
- }
-
- th {
- position: sticky;
- top: 0;
- font-weight: 500;
- color: var(--td-text-color-secondary);
- background: var(--td-bg-color-secondarycontainer);
- }
-
- tr:last-child td {
- border-bottom: none;
- }
-}
-
-.plugin-tool-view__cards {
- display: flex;
- flex-direction: column;
- gap: 8px;
-}
-
-.plugin-tool-view__card {
- padding: 8px 12px;
- border: 1px solid var(--td-component-stroke);
- border-radius: var(--app-radius-sm);
-}
-
-.plugin-tool-view__card-title {
- font-weight: 500;
-}
-
-.plugin-tool-view__card-subtitle {
- margin-top: 2px;
- font-size: var(--app-text-sm);
- color: var(--td-text-color-secondary);
-}
-
-.plugin-tool-view__card-body {
- margin: 4px 0 0;
- white-space: pre-wrap;
-}
-
-.plugin-tool-view__kv-title {
- margin-bottom: 6px;
- font-weight: 500;
-}
-
-.plugin-tool-view__kv {
- display: grid;
- grid-template-columns: max-content 1fr;
- gap: 4px 16px;
- margin: 0;
-
- dt {
- color: var(--td-text-color-secondary);
- }
-
- dd {
- margin: 0;
- word-break: break-word;
- }
-}
-
-.plugin-tool-view__json {
- max-height: 360px;
- margin: 0;
- padding: 8px 12px;
- overflow: auto;
- font-size: var(--app-text-sm);
- background: var(--td-bg-color-secondarycontainer);
- border-radius: var(--app-radius-sm);
-}
-
-a {
- color: var(--td-brand-color);
- text-decoration: none;
-
- &:hover {
- text-decoration: underline;
- }
-}
-</style>
diff --git a/frontend/src/views/knowledge/KnowledgeBase.vue b/frontend/src/views/knowledge/KnowledgeBase.vue
index 15c5d802f..15b931f86 100644
--- a/frontend/src/views/knowledge/KnowledgeBase.vue
+++ b/frontend/src/views/knowledge/KnowledgeBase.vue
@@ -81,14 +81,10 @@ import {
type DocumentSortValue,
} from './documentSorting';
import { useI18n } from 'vue-i18n';
-import PluginFrame from '@/extensions/pluginFrame/PluginFrame.vue';
-import { findPage } from '@/extensions/pluginFrame/pluginPages';
-import { usePluginPagesStore } from '@/stores/pluginPages';
-import { localizedText } from '@/utils/localizedText';
import { useMarqueeSelect } from '@/hooks/useMarqueeSelect';
import type { ParserEngineInfo } from '@/api/system';
const route = useRoute();
-const { t, locale } = useI18n();
+const { t } = useI18n();
const kbId = computed(() => (route.params as any).kbId as string || '');
const kbInfo = ref<any>(null);
const uploadSourceRef = ref<InstanceType<typeof KbUploadSourceDropdown> | null>(null);
@@ -97,19 +93,9 @@ const docListLoading = ref(true);
const isFAQ = computed(() => (kbInfo.value?.type || '') === 'faq');
const isWiki = computed(() => !!kbInfo.value?.indexing_strategy?.wiki_enabled);
const validTabs = ['documents', 'wiki', 'graph', 'gallery'] as const
-// Plugin tabs are keyed "plugin:<plugin>/<tab>".
-type KbTab = typeof validTabs[number] | `plugin:${string}`
-const isPluginTab = (tab: unknown): tab is `plugin:${string}` => typeof tab === 'string' && tab.startsWith('plugin:')
-const initTab: KbTab = validTabs.includes(route.query.tab as any) || isPluginTab(route.query.tab)
- ? (route.query.tab as KbTab)
- : 'documents'
+type KbTab = typeof validTabs[number]
+const initTab = validTabs.includes(route.query.tab as any) ? (route.query.tab as KbTab) : 'documents'
const activeKbTab = ref<KbTab>(initTab);
-const pluginPages = usePluginPagesStore()
-const pluginPagesLoaded = ref(false)
-void pluginPages.ensure().catch(() => {}).finally(() => {
- pluginPagesLoaded.value = true
-})
-const activePluginTab = computed(() => (isPluginTab(activeKbTab.value) ? findPage(pluginPages.kbTabs, activeKbTab.value) : undefined))
// Wiki 状态用于面包屑上的索引中指示。父组件自行拉取,避免依赖 WikiBrowser 挂载状态
// (用户切到"文档" tab 时 WikiBrowser 会卸载,这里仍需持续反映后台索引进度)。
@@ -139,19 +125,11 @@ const kbViewTabs = computed(() => {
)
}
tabs.push({ key: 'gallery', icon: 'image', label: t(`${w}.tabGallery`), tip: t(`${w}.tabGalleryTip`) })
- for (const page of pluginPages.kbTabs) {
- const label = localizedText(page.name, locale.value)
- const tip = page.description ? localizedText(page.description, locale.value) : label
- tabs.push({ key: page.key as KbTab, icon: 'app', label, tip })
- }
return tabs
})
-const shownKbTab = computed<KbTab>(() => {
- if (kbViewTabs.value.some((tab) => tab.key === activeKbTab.value)) return activeKbTab.value
- // A plugin tab named in the URL waits for the plugin tabs to load.
- if (isPluginTab(activeKbTab.value) && !pluginPagesLoaded.value) return activeKbTab.value
- return 'documents'
-})
+const shownKbTab = computed<KbTab>(() =>
+ kbViewTabs.value.some((tab) => tab.key === activeKbTab.value) ? activeKbTab.value : 'documents',
+)
const onWikiStatusChange = (payload: { pendingTasks: number; isActive: boolean; pendingIssues: number }) => {
wikiStatus.value = payload
}
@@ -2363,13 +2341,7 @@ const handleKBEditorSuccess = (kbIdValue: string) => {
<!-- wiki/graph tabs only exist on wiki KBs; a stale tab (?tab= or one
carried over from a previous KB) falls back to documents. -->
- <!-- A plugin's tab: its page in a sandboxed frame, told which KB it is on. -->
- <div v-if="activePluginTab && kbId" class="plugin-tab-area">
- <PluginFrame :page="activePluginTab" :context="{ knowledgeBaseId: kbId }" fill
- @close="activeKbTab = 'documents'" />
- </div>
-
- <template v-if="shownKbTab === 'documents' || (!isWiki && activeKbTab !== 'gallery' && !activePluginTab && !isPluginTab(shownKbTab))">
+ <template v-if="activeKbTab === 'documents' || (!isWiki && activeKbTab !== 'gallery')">
<div class="knowledge-main">
<KbFolderTree v-if="showFolderTree && !folderTreeCollapsed" :tree="folderTree" :selected-path="selectedFolderPath"
:loading="folderTreeLoading" :can-edit="canEdit" :root-label="kbInfo?.name"
@@ -2745,13 +2717,6 @@ const handleKBEditorSuccess = (kbIdValue: string) => {
overflow: hidden;
}
-.plugin-tab-area {
- flex: 1;
- min-height: 0;
- display: flex;
- padding-top: 12px;
-}
-
// Directory navigation and the document content share the available width.
.knowledge-main {
display: flex;
diff --git a/frontend/src/views/knowledge/components/DocumentListView.vue b/frontend/src/views/knowledge/components/DocumentListView.vue
index 79f12b38b..6e94d74e8 100644
--- a/frontend/src/views/knowledge/components/DocumentListView.vue
+++ b/frontend/src/views/knowledge/components/DocumentListView.vue
@@ -109,9 +109,6 @@ const getSourceInfo = (item: KnowledgeItem): { icon: string; label: string } =>
if (ch === 'dingtalk') return { icon: 'cloud-download', label: t('knowledgeBase.channelDingtalk') };
if (ch === 'slack') return { icon: 'cloud-download', label: t('knowledgeBase.channelSlack') };
if (ch === 'im') return { icon: 'cloud-download', label: t('knowledgeBase.channelIm') };
- if (ch === 'rss') return { icon: 'cloud-download', label: t('knowledgeBase.channelRss') };
- // Plugin connectors ingest under their qualified type ID (acme.feeds/feed).
- if (ch && ch.includes('/')) return { icon: 'cloud-download', label: t('knowledgeBase.channelExternal') };
if (item.type === 'url') return { icon: 'link', label: t('knowledgeBase.channelUrl') };
if (item.type === 'manual') return { icon: 'edit', label: t('knowledgeBase.channelManual') };
return { icon: 'upload', label: t('knowledgeBase.channelUpload') };
diff --git a/frontend/src/views/knowledge/settings/DataSourceEditorDialog.test.ts b/frontend/src/views/knowledge/settings/DataSourceEditorDialog.test.ts
index 33752f2ca..e6002c92d 100644
--- a/frontend/src/views/knowledge/settings/DataSourceEditorDialog.test.ts
+++ b/frontend/src/views/knowledge/settings/DataSourceEditorDialog.test.ts
@@ -9,9 +9,6 @@ import ts from 'typescript'
import { createRenderer, h, nextTick, reactive, ref } from 'vue'
const require = createRequire(import.meta.url)
-// The dialog validates credentials with the real schema helpers.
-const schemaModule = require('../../../components/schema-form/schema.ts')
-const sourceModule = require('../../../components/schema-form/source.ts')
const filename = fileURLToPath(new URL('./DataSourceEditorDialog.vue', import.meta.url))
const { descriptor } = parse(readFileSync(filename, 'utf8'), { filename })
const script = compileScript(descriptor, { id: 'datasource-editor-test' }).content
@@ -35,23 +32,6 @@ async function fixture({
const calls: Array<{ method: string; args: any[] }> = []
let storedToken = configured ? 'expired-token' : ''
const api = {
- // Same credential form the backend serves for GitLab.
- async getConnectorTypes() {
- return [{
- type: 'gitlab',
- config_schema: {
- type: 'object',
- required: ['base_url', 'access_token'],
- properties: {
- base_url: { type: 'string', title: 'GitLab URL', 'x-order': 1 },
- access_token: { type: 'string', title: 'Personal access token', 'x-secret': true, 'x-order': 2 },
- },
- },
- }, { type: 'yuque', config_schema: { type: 'object', properties: {} } }, {
- type: 'acme.jira/jira', plugin_id: 'acme.jira',
- config_schema: { type: 'object', properties: { site: { type: 'string' } } },
- }]
- },
async validateCredentials(type: string, credentials: Record<string, string>) {
calls.push({ method: 'validateCredentials', args: [type, { ...credentials }] })
if (credentials.access_token !== 'rotated-token') throw new Error('gitlab API /user: status 401')
@@ -80,32 +60,13 @@ async function fixture({
},
})
const exports: any = {}
- let formBinding: any
runInNewContext(compiled, {
exports,
require(name: string) {
if (name === 'vue') return require('vue')
- if (name === 'vue-i18n') {
- return { useI18n: () => ({ t: (key: string) => key, te: () => true, locale: { value: 'en-US' } }) }
- }
- if (name === './connectorLabels') {
- return {
- connectorName: (d: { type: string }) => `datasource.connector.${d.type}`,
- connectorDescription: (d: { type: string }) => `datasource.connectorDesc.${d.type}`,
- }
- }
+ if (name === 'vue-i18n') return { useI18n: () => ({ t: (key: string) => key }) }
if (name === 'tdesign-vue-next') return { MessagePlugin: { warning() {}, success() {}, error() {} } }
if (name === '@/api/datasource') return api
- if (name === '@/components/schema-form/schema') return schemaModule
- if (name === '@/components/schema-form/source') {
- return { valueAt: sourceModule.valueAt, provideSchemaFormSource() {} }
- }
- if (name === '@/components/schema-form/pluginSource') {
- return { pluginFormSource: (b: unknown) => { formBinding = b; return {} } }
- }
- if (name === '@/components/schema-form/useSchemaText') {
- return { useSchemaText: () => (schema: { title?: string }) => schema.title ?? '' }
- }
return { default: {} }
},
URL, console,
@@ -128,9 +89,7 @@ async function fixture({
vm.form.config.credentials = { base_url: 'https://gitlab.example.com', access_token: token }
await nextTick()
}
- return {
- vm, calls, replace, formBinding, storedToken: () => storedToken, close: () => app.unmount(),
- }
+ return { vm, calls, replace, storedToken: () => storedToken, close: () => app.unmount() }
}
test('rotated GitLab credentials are tested without updating the saved data source', async () => {
@@ -201,8 +160,7 @@ test('an existing data source with no saved credentials tests the entered token'
test('new GitLab data sources continue to test credentials without persistence', async () => {
const f = await fixture({ create: true })
try {
- await f.vm.loadConnectorTypes()
- f.vm.selectType(f.vm.connectorTypes.find((def: any) => def.type === 'gitlab'))
+ f.vm.selectType(f.vm.connectorDefs.find((def: any) => def.type === 'gitlab'))
await f.replace()
await f.vm.testConnection()
assert.equal(f.vm.testResult, 'success')
@@ -210,25 +168,11 @@ test('new GitLab data sources continue to test credentials without persistence',
} finally { f.close() }
})
-test('a missing required credential blocks the connection test and marks the field', async () => {
- const f = await fixture({ create: true })
- try {
- await f.vm.loadConnectorTypes()
- f.vm.selectType(f.vm.connectorTypes.find((def: any) => def.type === 'gitlab'))
- f.vm.form.config.credentials = { base_url: 'https://gitlab.example.com' }
- await nextTick() // the user typed before clicking; edits clear old errors
- await f.vm.testConnection()
- assert.deepEqual(f.calls, [], 'nothing reaches the backend')
- assert.deepEqual(JSON.stringify(f.vm.credentialErrors), JSON.stringify([{ path: 'access_token', code: 'required' }]))
- } finally { f.close() }
-})
-
test('a new Yuque data source adopts the TOC folder layout, but not the filter', async () => {
const f = await fixture({ create: true })
try {
assert.equal(f.vm.form.config.settings.folder_mode, undefined)
- await f.vm.loadConnectorTypes()
- f.vm.selectType(f.vm.connectorTypes.find((def: any) => def.type === 'yuque'))
+ f.vm.selectType(f.vm.connectorDefs.find((def: any) => def.type === 'yuque'))
assert.equal(f.vm.form.config.settings.folder_mode, 'toc')
// The layout is a presentation choice; toc_only decides what may enter the
// knowledge base, so a new source is deliberately left without it.
@@ -283,21 +227,3 @@ test('Cloud hierarchy limitation stays visible after an empty space expansion',
assert.equal(f.vm.visibleTree.some((row: any) => row.noticeAfter), true)
} finally { f.close() }
})
-
-test('plugin connector forms ask the plugin behind the connector type', async () => {
- const f = await fixture({ type: 'acme.jira/jira', settings: { project: 'p1' } })
- try {
- await new Promise((resolve) => setTimeout(resolve, 0))
- assert.deepEqual(JSON.parse(JSON.stringify(f.formBinding.target())), {
- pluginId: 'acme.jira', scope: 'instance', contribution: 'connectors/jira', instanceId: 'source-one',
- })
- f.vm.form.config.credentials = { site: 'acme' }
- assert.equal(f.formBinding.dependency('site'), 'acme')
- assert.equal(f.formBinding.dependency('project'), 'p1')
- assert.deepEqual(JSON.parse(JSON.stringify(f.formBinding.values())), {
- credentials: { site: 'acme' }, settings: { project: 'p1' }, resourceIds: [],
- })
- f.vm.form.type = 'gitlab'
- assert.equal(f.formBinding.target(), undefined)
- } finally { f.close() }
-})
diff --git a/frontend/src/views/knowledge/settings/DataSourceEditorDialog.vue b/frontend/src/views/knowledge/settings/DataSourceEditorDialog.vue
index b322b9282..27249eaad 100644
--- a/frontend/src/views/knowledge/settings/DataSourceEditorDialog.vue
+++ b/frontend/src/views/knowledge/settings/DataSourceEditorDialog.vue
@@ -13,25 +13,11 @@ import {
deleteDataSource,
putDataSourceCredentials,
deleteDataSourceCredentials,
- getConnectorTypes,
- type ConnectorMeta,
type DataSource,
type Resource,
} from '@/api/datasource'
-import SchemaForm from '@/components/schema-form/SchemaForm.vue'
-import {
- applyDefaults,
- schemaAt,
- validateConfig,
- type ConfigSchema,
- type FieldError,
-} from '@/components/schema-form/schema'
-import { pluginFormSource } from '@/components/schema-form/pluginSource'
-import { provideSchemaFormSource, valueAt } from '@/components/schema-form/source'
-import { useSchemaText } from '@/components/schema-form/useSchemaText'
import SettingDrawer from '@/components/settings/SettingDrawer.vue'
import DataSourceTypeIcon from './DataSourceTypeIcon.vue'
-import { connectorDescription, connectorName } from './connectorLabels'
import { getDatasourceIconUrl } from './datasourceIcons'
const props = defineProps<{
@@ -41,7 +27,7 @@ const props = defineProps<{
const visible = defineModel<boolean>('visible', { default: false })
const emit = defineEmits<{ saved: [] }>()
-const { t, te, locale } = useI18n()
+const { t } = useI18n()
const isEdit = computed(() => !!props.dataSource)
const step = ref(0)
@@ -117,10 +103,35 @@ function cancelReplaceCredentials() {
replaceCredentialsMode.value = false
pendingRemoveCredentials.value = false
form.value.config.credentials = {}
+ rssAuthHeaders.value = []
testResult.value = credentialsConfigured.value ? 'success' : ''
testErrorMsg.value = ''
}
+interface CustomHeaderItem {
+ key: string
+ value: string
+}
+
+const rssAuthHeaders = ref<CustomHeaderItem[]>([])
+
+function serializeAuthHeaders(items: CustomHeaderItem[]): string {
+ return items
+ .filter(h => h.key.trim())
+ .map(h => `${h.key.trim()}: ${h.value}`)
+ .join('\n')
+}
+
+function syncRssAuthHeadersToCredentials() {
+ if (form.value.type !== 'rss') return
+ const serialized = serializeAuthHeaders(rssAuthHeaders.value)
+ if (serialized) {
+ form.value.config.credentials.auth_headers = serialized
+ } else {
+ delete form.value.config.credentials.auth_headers
+ }
+}
+
// Feed URLs may still live in credentials on older rows (not returned by the
// API). The backend copies them into settings on read; fall back to the
// selected feed resource IDs when settings are still empty.
@@ -136,6 +147,14 @@ function hydrateRssFeedUrlsFromConfig(config: { settings?: Record<string, any>;
return { ...settings, feed_urls: ids.join('\n') }
}
+function addRssAuthHeader() {
+ rssAuthHeaders.value.push({ key: '', value: '' })
+}
+
+function removeRssAuthHeader(idx: number) {
+ rssAuthHeaders.value.splice(idx, 1)
+}
+
function needsConnectionTest(): boolean {
return !(isEdit.value && credentialsConfigured.value && !replaceCredentialsMode.value)
}
@@ -521,79 +540,212 @@ const schedulePresets = computed(() => [
{ label: t('datasource.schedule24h'), value: '0 0 2 * * *' },
])
-// --- Connectors ---
-// The connectors this deployment can create, with their setup guides and
-// credential forms (config_schema), come from /datasource/types.
-const connectorTypes = ref<ConnectorMeta[]>([])
+// --- Connector definitions ---
+interface ConnectorDef {
+ type: string
+ available: boolean
+ docUrl: string
+ permissionDocUrl: string
+ permissionPageUrl: string
+ requiredPermissions: string[]
+ fields: {
+ key: string
+ labelKey: string
+ placeholder: string
+ secret?: boolean
+ optional?: boolean
+ hintKey?: string
+ multiline?: boolean
+ fieldType?: 'custom_headers'
+ }[]
+}
+
+const connectorDefs = computed<ConnectorDef[]>(() => [
+ {
+ type: 'feishu',
+ available: true,
+ docUrl: 'https://open.feishu.cn/app',
+ permissionDocUrl: 'https://open.feishu.cn/document/server-docs/docs/wiki-v2/wiki-overview',
+ permissionPageUrl: 'https://open.feishu.cn/app',
+ requiredPermissions: [
+ 'wiki:wiki:readonly',
+ 'drive:drive:readonly',
+ 'drive:export:readonly',
+ 'docx:document:readonly',
+ ],
+ fields: [
+ { key: 'app_id', labelKey: 'datasource.field.appId', placeholder: 'cli_xxxx' },
+ { key: 'app_secret', labelKey: 'datasource.field.appSecret', placeholder: '', secret: true },
+ { key: 'base_url', labelKey: 'datasource.field.baseUrl', placeholder: 'https://open.feishu.cn', optional: true, hintKey: 'datasource.field.baseUrlHint' },
+ ],
+ },
+ {
+ // Lark is Feishu's international cloud. Same wiki/docx/drive APIs and the
+ // same scope identifiers, but a separate console, tenant and app — an app
+ // created on open.feishu.cn cannot read a Lark wiki.
+ type: 'lark',
+ available: true,
+ docUrl: 'https://open.larksuite.com/app',
+ permissionDocUrl: 'https://open.larksuite.com/document/server-docs/docs/wiki-v2/wiki-overview',
+ permissionPageUrl: 'https://open.larksuite.com/app',
+ requiredPermissions: [
+ 'wiki:wiki:readonly',
+ 'drive:drive:readonly',
+ 'drive:export:readonly',
+ 'docx:document:readonly',
+ ],
+ fields: [
+ { key: 'app_id', labelKey: 'datasource.field.appId', placeholder: 'cli_xxxx' },
+ { key: 'app_secret', labelKey: 'datasource.field.appSecret', placeholder: '', secret: true },
+ { key: 'base_url', labelKey: 'datasource.field.baseUrl', placeholder: 'https://open.feishu.cn', optional: true, hintKey: 'datasource.field.baseUrlHint' },
+ ],
+ },
+ {
+ // Feishu Drive (云盘) mode: sync documents/files under a user-supplied Drive
+ // folder_token. Same auth as the wiki connector but no wiki:wiki:readonly
+ // scope - Drive only needs drive + export + docx.
+ type: 'feishu_drive',
+ available: true,
+ docUrl: 'https://open.feishu.cn/app',
+ permissionDocUrl: 'https://open.feishu.cn/document/server-docs/docs/drive-v1/file/list',
+ permissionPageUrl: 'https://open.feishu.cn/app',
+ requiredPermissions: [
+ 'drive:drive:readonly',
+ 'drive:export:readonly',
+ 'docx:document:readonly',
+ ],
+ fields: [
+ { key: 'app_id', labelKey: 'datasource.field.appId', placeholder: 'cli_xxxx' },
+ { key: 'app_secret', labelKey: 'datasource.field.appSecret', placeholder: '', secret: true },
+ { key: 'base_url', labelKey: 'datasource.field.baseUrl', placeholder: 'https://open.feishu.cn', optional: true, hintKey: 'datasource.field.baseUrlHint' },
+ ],
+ },
+ {
+ // Lark Drive: international counterpart of feishu_drive.
+ type: 'lark_drive',
+ available: true,
+ docUrl: 'https://open.larksuite.com/app',
+ permissionDocUrl: 'https://open.larksuite.com/document/server-docs/docs/drive-v1/file/list',
+ permissionPageUrl: 'https://open.larksuite.com/app',
+ requiredPermissions: [
+ 'drive:drive:readonly',
+ 'drive:export:readonly',
+ 'docx:document:readonly',
+ ],
+ fields: [
+ { key: 'app_id', labelKey: 'datasource.field.appId', placeholder: 'cli_xxxx' },
+ { key: 'app_secret', labelKey: 'datasource.field.appSecret', placeholder: '', secret: true },
+ { key: 'base_url', labelKey: 'datasource.field.baseUrl', placeholder: 'https://open.larksuite.com', optional: true, hintKey: 'datasource.field.baseUrlHint' },
+ ],
+ },
+ {
+ type: 'notion',
+ available: true,
+ docUrl: 'https://www.notion.so/my-integrations',
+ permissionDocUrl: '',
+ permissionPageUrl: '',
+ requiredPermissions: [],
+ fields: [
+ { key: 'api_key', labelKey: 'datasource.field.integrationToken', placeholder: 'ntn_xxxx', secret: true },
+ ],
+ },
+ {
+ type: 'confluence',
+ available: true,
+ docUrl: 'https://developer.atlassian.com/cloud/confluence/rest/',
+ permissionDocUrl: 'https://developer.atlassian.com/cloud/confluence/rest/',
+ permissionPageUrl: 'https://id.atlassian.com/manage-profile/security/api-tokens',
+ requiredPermissions: [],
+ fields: [
+ { key: 'base_url', labelKey: 'datasource.field.confluenceBaseUrl', placeholder: 'https://confluence.example.com or https://team.atlassian.net/wiki' },
+ { key: 'username', labelKey: 'datasource.field.confluenceUsername', placeholder: 'name or email' },
+ { key: 'password', labelKey: 'datasource.field.confluencePassword', placeholder: 'Server/DC password', secret: true },
+ { key: 'api_token', labelKey: 'datasource.field.confluenceApiToken', placeholder: 'Cloud API token', secret: true },
+ ],
+ },
+ {
+ type: 'yuque',
+ available: true,
+ docUrl: 'https://www.yuque.com/yuque/developer/api',
+ permissionDocUrl: 'https://www.yuque.com/yuque/developer/api',
+ permissionPageUrl: 'https://www.yuque.com/settings/tokens',
+ requiredPermissions: [
+ 'repo:read',
+ 'doc:read',
+ ],
+ fields: [
+ { key: 'api_token', labelKey: 'datasource.field.apiToken', placeholder: '', secret: true },
+ { key: 'base_url', labelKey: 'datasource.field.baseUrl', placeholder: 'https://www.yuque.com', optional: true, hintKey: 'datasource.field.baseUrlHint' },
+ ],
+ },
+ {
+ type: 'dingtalk',
+ available: true,
+ docUrl: 'https://open.dingtalk.com/document/development/knowledge-base-overview',
+ permissionDocUrl: 'https://open.dingtalk.com/document/development/get-knowledge-base-list',
+ permissionPageUrl: 'https://open-dev.dingtalk.com/',
+ requiredPermissions: [
+ 'Wiki.Workspace.Read',
+ 'Wiki.Node.Read',
+ 'Storage.File.Read',
+ ],
+ fields: [
+ { key: 'client_id', labelKey: 'datasource.field.clientId', placeholder: 'dingxxxxxxxx' },
+ { key: 'client_secret', labelKey: 'datasource.field.clientSecret', placeholder: '', secret: true },
+ { key: 'operator_id', labelKey: 'datasource.field.operatorId', placeholder: '', hintKey: 'datasource.field.operatorIdHint' },
+ ],
+ },
+ {
+ // Tencent IMA (ima.qq.com). Uses the OpenAPI at /openapi/wiki/v1 with two
+ // static headers (ima-openapi-clientid + ima-openapi-apikey); no OAuth.
+ type: 'ima',
+ available: true,
+ docUrl: 'https://ima.qq.com/agent-interface',
+ permissionDocUrl: 'https://ima.qq.com/agent-interface',
+ permissionPageUrl: 'https://ima.qq.com/agent-interface',
+ requiredPermissions: [],
+ fields: [
+ { key: 'client_id', labelKey: 'datasource.field.imaClientId', placeholder: '', secret: true },
+ { key: 'api_key', labelKey: 'datasource.field.imaApiKey', placeholder: '', secret: true },
+ { key: 'base_url', labelKey: 'datasource.field.baseUrl', placeholder: 'https://ima.qq.com', optional: true, hintKey: 'datasource.field.baseUrlHint' },
+ ],
+ },
+ {
+ type: 'rss',
+ available: true,
+ docUrl: '',
+ permissionDocUrl: '',
+ permissionPageUrl: '',
+ requiredPermissions: [],
+ fields: [
+ { key: 'auth_headers', labelKey: 'datasource.field.authHeaders', placeholder: '', optional: true, hintKey: 'datasource.field.authHeadersHint', fieldType: 'custom_headers' },
+ ],
+ },
+ {
+ type: 'gitlab', available: true, docUrl: '', permissionDocUrl: '', permissionPageUrl: '', requiredPermissions: [],
+ fields: [
+ { key: 'base_url', labelKey: 'datasource.gitlab.baseUrl', placeholder: 'https://gitlab.example.com' },
+ { key: 'access_token', labelKey: 'datasource.gitlab.accessToken', placeholder: '', secret: true },
+ ],
+ },
+])
-async function loadConnectorTypes() {
- if (connectorTypes.value.length > 0) return
- try {
- connectorTypes.value = [...(await getConnectorTypes())]
- } catch (e: any) {
- MessagePlugin.error(e?.message || t('datasource.typesLoadFailed'))
- }
-}
-const currentDef = computed(() => connectorTypes.value.find(d => d.type === form.value.type))
+const currentDef = computed(() => connectorDefs.value.find(d => d.type === form.value.type))
-const EMPTY_SCHEMA: ConfigSchema = { type: 'object', properties: {} }
-const credentialSchema = computed<ConfigSchema>(() => currentDef.value?.config_schema ?? EMPTY_SCHEMA)
-// Settings form of connectors without a built-in settings UI (plugins).
-const settingsSchema = computed<ConfigSchema | null>(() => currentDef.value?.settings_schema ?? null)
-const settingsErrors = ref<FieldError[]>([])
+const displayedCredentialFields = computed(() => {
+ const fields = currentDef.value?.fields || []
+ if (form.value.type !== "confluence") return fields
-// Plugin connector fields with x-options / x-oauth ask the plugin, with the
-// instance the form holds (the stored credentials fill in what edit mode hides).
-provideSchemaFormSource(pluginFormSource({
- target: () => {
- const def = currentDef.value
- if (!def?.plugin_id || !def.type.startsWith(`${def.plugin_id}/`)) return undefined
- return {
- pluginId: def.plugin_id,
- scope: 'instance',
- contribution: `connectors/${def.type.slice(def.plugin_id.length + 1)}`,
- instanceId: props.dataSource?.id,
- }
- },
- values: () => ({
- credentials: form.value.config.credentials,
- settings: form.value.config.settings,
- resourceIds: form.value.config.resource_ids,
- }),
- dependency: path => valueAt(form.value.config.credentials, path) ?? valueAt(form.value.config.settings, path),
-}))
-const labelI18n = computed(() => ({ t: (k: string) => t(k), te: (k: string) => te(k), locale: locale.value }))
-const labelOf = (def: ConnectorMeta) => connectorName(def, labelI18n.value)
-
-// Field errors of the last credential check, shown under each field.
-const credentialErrors = ref<FieldError[]>([])
-const schemaText = useSchemaText()
-
-watch(() => form.value.config.credentials, () => { credentialErrors.value = [] }, { deep: true })
-
-// Checks the credential form against its schema. On failure it marks the
-// fields and warns with the first one, as the editor always has.
-function checkCredentials(): boolean {
- credentialErrors.value = validateConfig(credentialSchema.value, form.value.config.credentials)
- settingsErrors.value = settingsSchema.value ? validateConfig(settingsSchema.value, form.value.config.settings) : []
- const inCredentials = credentialErrors.value.length > 0
- const first = credentialErrors.value[0] ?? settingsErrors.value[0]
- if (!first) return true
- const schema = inCredentials ? credentialSchema.value : (settingsSchema.value as ConfigSchema)
- const field = schemaAt(schema, first.path)
- const label = field ? schemaText(field, 'title') : first.path
- MessagePlugin.warning(
- first.code === 'required'
- ? `${label} ${t('datasource.isRequired')}`
- : `${label}: ${t(`schemaForm.errors.${first.code}`)}`,
- )
- return false
-}
+ return fields.filter((field) => {
+ if (field.key === "password") return form.value.config.credentials.edition !== "cloud"
+ if (field.key === "api_token") return form.value.config.credentials.edition === "cloud"
+ return true
+ })
+})
// --- Drawer lifecycle ---
watch(visible, async (v) => {
- if (v) void loadConnectorTypes()
if (!v) {
if (!isEdit.value && tempDsId.value) {
try {
@@ -620,6 +772,7 @@ watch(visible, async (v) => {
driveFolderToken.value = ''
driveFolderTokenError.value = ''
driveRootLoaded.value = false
+ rssAuthHeaders.value = []
gitlabProjects.value = []
if (isEdit.value && props.dataSource) {
@@ -693,6 +846,18 @@ watch(
{ deep: true },
)
+watch(
+ rssAuthHeaders,
+ () => {
+ syncRssAuthHeadersToCredentials()
+ if (needsConnectionTest()) {
+ testResult.value = ''
+ testErrorMsg.value = ''
+ }
+ },
+ { deep: true },
+)
+
watch(
() => form.value.config.settings.feed_urls,
() => {
@@ -703,16 +868,11 @@ watch(
},
)
-function selectType(def: ConnectorMeta) {
+function selectType(def: ConnectorDef) {
+ if (!def.available) return
form.value.type = def.type
- form.value.name = labelOf(def)
- // Schema defaults, e.g. Confluence starts on the Server edition.
- form.value.config.credentials = applyDefaults(def.config_schema ?? EMPTY_SCHEMA, {})
- if (def.settings_schema) {
- form.value.config.settings = applyDefaults(def.settings_schema, form.value.config.settings ?? {})
- }
- settingsErrors.value = []
- credentialErrors.value = []
+ form.value.name = t(`datasource.connector.${def.type}`)
+ form.value.config.credentials = def.type === "confluence" ? { edition: "server" } : {}
if (def.type === 'confluence') {
form.value.config.settings = { ...form.value.config.settings, edition: 'server' }
}
@@ -723,15 +883,24 @@ function selectType(def: ConnectorMeta) {
form.value.config.settings = { ...form.value.config.settings, folder_mode: 'toc' }
}
if (isGitLabConnector(def.type)) addGitLabProject()
+ rssAuthHeaders.value = []
step.value = 1
}
// --- Test connection ---
async function testConnection() {
+ syncRssAuthHeadersToCredentials()
syncConfluencePublicFieldsToSettings()
if (!validateRssFeedUrls()) return
if (!isEdit.value || !credentialsConfigured.value || replaceCredentialsMode.value) {
- if (!checkCredentials()) return
+ const fields = displayedCredentialFields.value
+ for (const f of fields) {
+ if (f.optional || f.fieldType === 'custom_headers') continue
+ if (!form.value.config.credentials[f.key]) {
+ MessagePlugin.warning(`${t(f.labelKey)} ${t('datasource.isRequired')}`)
+ return
+ }
+ }
}
testing.value = true
@@ -753,7 +922,7 @@ async function testConnection() {
// validate-credentials is credentials-only; feed URLs live in settings.
creds.feed_urls = form.value.config.settings.feed_urls
}
- await validateCredentials(form.value.type, creds, form.value.config.settings)
+ await validateCredentials(form.value.type, creds)
}
testResult.value = 'success'
MessagePlugin.success(t('datasource.testSuccess'))
@@ -918,11 +1087,21 @@ function validateRssFeedUrls(): boolean {
}
function validateStep1Fields(): boolean {
+ syncRssAuthHeadersToCredentials()
if (!validateRssFeedUrls()) return false
if (isEdit.value && credentialsConfigured.value && !replaceCredentialsMode.value) {
return true
}
- return checkCredentials()
+
+ const fields = displayedCredentialFields.value
+ for (const f of fields) {
+ if (f.optional || f.fieldType === 'custom_headers') continue
+ if (!form.value.config.credentials[f.key]) {
+ MessagePlugin.warning(`${t(f.labelKey)} ${t('datasource.isRequired')}`)
+ return false
+ }
+ }
+ return true
}
async function nextStep() {
@@ -994,6 +1173,7 @@ function buildConfigPayload(): Record<string, unknown> {
// the whole submit on failure so we don't leave the row partially saved.
async function commitCredentialsIfNeeded(dsId: string): Promise<boolean> {
if (!isEdit.value || !replaceCredentialsMode.value) return true
+ syncRssAuthHeadersToCredentials()
syncConfluencePublicFieldsToSettings()
const filled = Object.entries(form.value.config.credentials).filter(
([, v]) => typeof v === 'string' ? v !== '' : v != null,
@@ -1004,6 +1184,7 @@ async function commitCredentialsIfNeeded(dsId: string): Promise<boolean> {
credentialsConfigured.value = true
replaceCredentialsMode.value = false
form.value.config.credentials = {}
+ rssAuthHeaders.value = []
return true
} catch (e: any) {
MessagePlugin.error(e?.message || e?.error || t('credential.saveFailed'))
@@ -1173,9 +1354,9 @@ const drawerConfirmText = computed(() => {
@confirm="handleDrawerConfirm"
@cancel="handleClose"
>
- <template v-if="form.type && (getDatasourceIconUrl(form.type) || currentDef?.icon)" #headerIcon>
+ <template v-if="form.type && getDatasourceIconUrl(form.type)" #headerIcon>
<img
- :src="getDatasourceIconUrl(form.type) || currentDef?.icon"
+ :src="getDatasourceIconUrl(form.type)"
:alt="form.type"
class="datasource-header-icon__img"
>
@@ -1239,17 +1420,19 @@ const drawerConfirmText = computed(() => {
<h4 class="setting-drawer__section-title">{{ t('datasource.step.selectType') }}</h4>
<div class="ds-type-grid">
<button
- v-for="def in connectorTypes"
+ v-for="def in connectorDefs"
:key="def.type"
type="button"
- class="ds-type-card"
+ :class="['ds-type-card', { disabled: !def.available }]"
+ :disabled="!def.available"
@click="selectType(def)"
>
<div class="ds-type-header">
- <DataSourceTypeIcon :type="def.type" :icon-url="def.icon" :label="labelOf(def)" :size="20" />
- <span class="ds-type-name">{{ labelOf(def) }}</span>
+ <DataSourceTypeIcon :type="def.type" :size="20" />
+ <span class="ds-type-name">{{ t(`datasource.connector.${def.type}`) }}</span>
+ <span v-if="!def.available" class="ds-type-soon">{{ t('datasource.comingSoon') }}</span>
</div>
- <div class="ds-type-desc">{{ connectorDescription(def, labelI18n) }}</div>
+ <div class="ds-type-desc">{{ t(`datasource.connectorDesc.${def.type}`) }}</div>
</button>
</div>
</section>
@@ -1257,7 +1440,7 @@ const drawerConfirmText = computed(() => {
<!-- Step 1: Credentials -->
<template v-if="step === 1">
<div
- v-if="currentDef?.required_permissions?.length"
+ v-if="currentDef && currentDef.requiredPermissions.length > 0"
class="ds-setup-guide ds-setup-guide--standalone"
>
<button
@@ -1290,7 +1473,7 @@ const drawerConfirmText = computed(() => {
<span class="ds-setup-step__desc">
<template v-if="!t(`datasource.prereqStep2Desc_${form.type}`)">
<code
- v-for="perm in currentDef.required_permissions"
+ v-for="perm in currentDef.requiredPermissions"
:key="perm"
class="ds-perm-tag"
>{{ perm }}</code>
@@ -1306,8 +1489,8 @@ const drawerConfirmText = computed(() => {
</li>
</ol>
<a
- v-if="currentDef.permission_page_url"
- :href="currentDef.permission_page_url"
+ v-if="currentDef.permissionPageUrl"
+ :href="currentDef.permissionPageUrl"
target="_blank"
rel="noopener"
class="doc-link ds-setup-guide__link"
@@ -1321,11 +1504,11 @@ const drawerConfirmText = computed(() => {
<section class="setting-drawer__section">
<h4 class="setting-drawer__section-title">{{ t('datasource.sectionBasic') }}</h4>
- <div v-if="currentDef?.doc_url" class="inline-alert">
+ <div v-if="currentDef?.docUrl" class="inline-alert">
<t-icon name="info-circle-filled" class="inline-alert__icon" />
<span class="inline-alert__text">{{ t('datasource.docHint') }}</span>
<a
- :href="currentDef.doc_url"
+ :href="currentDef.docUrl"
target="_blank"
rel="noopener"
class="inline-alert__action doc-link"
@@ -1419,17 +1602,81 @@ const drawerConfirmText = computed(() => {
</div>
<template v-else-if="credentialsInputVisible">
- <SchemaForm
- v-model="form.config.credentials"
- :schema="credentialSchema"
- :errors="credentialErrors"
- />
- <SchemaForm
- v-if="settingsSchema"
- v-model="form.config.settings"
- :schema="settingsSchema"
- :errors="settingsErrors"
- />
+ <div v-if="form.type === 'confluence'" class="form-item">
+ <label class="form-label">{{ t('datasource.field.confluenceEdition') }}</label>
+ <t-select v-model="form.config.credentials.edition">
+ <t-option value="server" :label="t('datasource.field.confluenceEditionServer')" />
+ <t-option value="cloud" :label="t('datasource.field.confluenceEditionCloud')" />
+ </t-select>
+ </div>
+ <div
+ v-for="field in displayedCredentialFields"
+ :key="field.key"
+ class="form-item"
+ >
+ <template v-if="field.fieldType === 'custom_headers'">
+ <div class="custom-headers-header">
+ <label class="form-label" style="margin-bottom: 0;">{{ t(field.labelKey) }}</label>
+ <t-button variant="text" size="small" theme="primary" @click="addRssAuthHeader">
+ <template #icon><t-icon name="add" /></template>
+ {{ t('model.editor.customHeadersAdd') }}
+ </t-button>
+ </div>
+ <p v-if="field.hintKey" class="form-desc custom-headers-desc">{{ t(field.hintKey) }}</p>
+ <div v-if="rssAuthHeaders.length > 0" class="custom-headers-list">
+ <div v-for="(item, idx) in rssAuthHeaders" :key="idx" class="custom-header-row">
+ <t-input
+ v-model="item.key"
+ :placeholder="t('model.editor.customHeadersKeyPlaceholder')"
+ class="custom-header-key"
+ autocomplete="off"
+ spellcheck="false"
+ />
+ <t-input
+ v-model="item.value"
+ :placeholder="t('model.editor.customHeadersValuePlaceholder')"
+ class="custom-header-value"
+ autocomplete="off"
+ spellcheck="false"
+ />
+ <t-button
+ variant="text"
+ shape="square"
+ size="small"
+ class="custom-header-remove"
+ :aria-label="t('common.delete')"
+ @click="removeRssAuthHeader(idx)"
+ >
+ <t-icon name="close" />
+ </t-button>
+ </div>
+ </div>
+ </template>
+ <template v-else>
+ <label class="form-label" :class="{ required: !field.optional }">
+ {{ t(field.labelKey) }}
+ </label>
+ <t-textarea
+ v-if="field.multiline"
+ v-model="form.config.credentials[field.key]"
+ :placeholder="field.placeholder || t('credential.inputPlaceholder')"
+ :autosize="{ minRows: 2, maxRows: 6 }"
+ autocomplete="off"
+ spellcheck="false"
+ />
+ <t-input
+ v-else
+ v-model="form.config.credentials[field.key]"
+ :placeholder="field.placeholder || t('credential.inputPlaceholder')"
+ :type="field.secret ? 'password' : 'text'"
+ autocomplete="off"
+ spellcheck="false"
+ >
+ <template v-if="field.secret" #prefix-icon><t-icon name="lock-on" /></template>
+ </t-input>
+ <p v-if="field.hintKey" class="form-desc">{{ t(field.hintKey) }}</p>
+ </template>
+ </div>
<div v-if="isEdit && replaceCredentialsMode" class="credential-edit-actions">
<t-button size="small" variant="text" @click="cancelReplaceCredentials">
{{ t('common.cancel') }}
@@ -1622,8 +1869,8 @@ const drawerConfirmText = computed(() => {
{{ t('datasource.retryLoadResources') }}
</button>
<a
- v-if="currentDef?.permission_doc_url"
- :href="currentDef.permission_doc_url"
+ v-if="currentDef?.permissionDocUrl"
+ :href="currentDef.permissionDocUrl"
target="_blank"
rel="noopener"
class="doc-link"
@@ -1836,6 +2083,11 @@ const drawerConfirmText = computed(() => {
color: inherit;
}
+.ds-type-card.disabled {
+ opacity: 0.5;
+ cursor: not-allowed;
+}
+
.ds-type-header {
display: flex;
align-items: center;
@@ -1848,6 +2100,14 @@ const drawerConfirmText = computed(() => {
font-weight: 600;
}
+.ds-type-soon {
+ font-size: var(--app-text-2xs);
+ color: var(--td-text-color-placeholder);
+ background: var(--td-bg-color-component);
+ padding: 1px 6px;
+ border-radius: 3px;
+}
+
.ds-type-desc {
font-size: var(--app-text-xs);
color: var(--td-text-color-secondary);
@@ -2487,6 +2747,55 @@ const drawerConfirmText = computed(() => {
gap: 16px;
}
+.custom-headers-header {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ margin-bottom: 6px;
+}
+
+.custom-headers-desc {
+ margin: 0 0 10px 0;
+ font-size: var(--app-text-sm);
+ line-height: 1.5;
+ color: var(--td-text-color-placeholder);
+}
+
+.custom-headers-list {
+ display: flex;
+ flex-direction: column;
+ gap: 8px;
+}
+
+.custom-header-row {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+
+ .custom-header-key {
+ flex: 0 0 38%;
+ }
+
+ .custom-header-value {
+ flex: 1;
+ }
+
+ .custom-header-remove {
+ flex-shrink: 0;
+ width: 32px;
+ height: 32px;
+ padding: 0;
+ color: var(--td-text-color-placeholder);
+ border-radius: var(--app-radius-sm);
+ transition: all 0.18s ease;
+
+ &:hover {
+ background: var(--td-error-color-light);
+ color: var(--td-error-color);
+ }
+ }
+}
+
.ds-empty-retry {
padding: 0;
border: none;
diff --git a/frontend/src/views/knowledge/settings/DataSourceSettings.vue b/frontend/src/views/knowledge/settings/DataSourceSettings.vue
index e912c586d..34e7d0ea7 100644
--- a/frontend/src/views/knowledge/settings/DataSourceSettings.vue
+++ b/frontend/src/views/knowledge/settings/DataSourceSettings.vue
@@ -3,36 +3,22 @@ import { ref, computed, onMounted, onBeforeUnmount } from 'vue'
import { MessagePlugin } from 'tdesign-vue-next'
import { useI18n } from 'vue-i18n'
import {
- getConnectorTypes,
listDataSources,
deleteDataSource,
triggerSync,
pauseDataSource,
resumeDataSource,
- type ConnectorMeta,
type DataSource,
} from '@/api/datasource'
import { humanizeCron, relativeTime } from '@/utils/cronHumanize'
import DataSourceEditorDialog from './DataSourceEditorDialog.vue'
import DataSourceSyncLogs from './DataSourceSyncLogs.vue'
import DataSourceTypeIcon from './DataSourceTypeIcon.vue'
-import { connectorName } from './connectorLabels'
import { useAuthStore } from '@/stores/auth'
const props = defineProps<{ kbId: string }>()
const emit = defineEmits<{ (e: 'count', value: number): void }>()
-const { t, te, locale } = useI18n()
-
-// Connector metadata names plugin connectors, which have no locale keys.
-const connectorMeta = ref<Record<string, ConnectorMeta>>({})
-async function loadConnectorMeta() {
- try {
- const list = await getConnectorTypes()
- connectorMeta.value = Object.fromEntries([...list].map((m) => [m.type, m]))
- } catch {
- connectorMeta.value = {}
- }
-}
+const { t } = useI18n()
const authStore = useAuthStore()
// 后端 /datasource 的 list/logs 是 Viewer+,但所有写操作(POST/PUT/DELETE
@@ -148,7 +134,7 @@ function syncModeLabel(mode: string) {
}
function connectorLabel(type: string) {
- return connectorName(connectorMeta.value[type] ?? { type }, { t, te, locale: locale.value })
+ return t(`datasource.connector.${type}`) || type
}
function scheduleLabel(cron: string) {
@@ -191,10 +177,7 @@ function onEditorSaved() {
loadList()
}
-onMounted(() => {
- void loadConnectorMeta()
- void loadList()
-})
+onMounted(loadList)
onBeforeUnmount(stopPolling)
</script>
@@ -223,12 +206,7 @@ onBeforeUnmount(stopPolling)
@click="canManageDataSource ? openEdit(ds) : undefined"
>
<div class="ds-card__badge">
- <DataSourceTypeIcon
- :type="ds.type"
- :icon-url="connectorMeta[ds.type]?.icon"
- :label="connectorLabel(ds.type)"
- variant="badge"
- />
+ <DataSourceTypeIcon :type="ds.type" variant="badge" />
</div>
<div class="ds-card__body">
<div class="ds-card__header">
diff --git a/frontend/src/views/knowledge/settings/DataSourceTypeIcon.vue b/frontend/src/views/knowledge/settings/DataSourceTypeIcon.vue
index 2ced5b9bb..2de050e88 100644
--- a/frontend/src/views/knowledge/settings/DataSourceTypeIcon.vue
+++ b/frontend/src/views/knowledge/settings/DataSourceTypeIcon.vue
@@ -3,16 +3,10 @@ import { getDatasourceIconUrl, datasourceIconMap } from './datasourceIcons'
const props = withDefaults(defineProps<{
type: string
- /** Icon of a plugin connector (a data: URI from its metadata). */
- iconUrl?: string
- /** Display name, whose first letter stands in when there is no icon. */
- label?: string
size?: number
/** inline: 类型选择等小尺寸场景;badge: 嵌入 ds-card__badge 等父级徽章容器 */
variant?: 'inline' | 'badge'
}>(), {
- iconUrl: '',
- label: '',
size: 20,
variant: 'inline',
})
@@ -32,7 +26,7 @@ function fallbackText(type: string) {
case 'ima':
return 'I'
default:
- return (props.label || type).trim().slice(0, 1).toUpperCase() || '?'
+ return type.slice(0, 1).toUpperCase() || '?'
}
}
</script>
@@ -50,13 +44,6 @@ function fallbackText(type: string) {
class="ds-type-icon__img"
:style="variant === 'inline' ? { width: `${size}px`, height: `${size}px` } : undefined"
>
- <img
- v-else-if="iconUrl"
- :src="iconUrl"
- :alt="label || type"
- class="ds-type-icon__img"
- :style="variant === 'inline' ? { width: `${size}px`, height: `${size}px` } : undefined"
- >
<span v-else class="ds-type-icon-fallback">{{ fallbackText(type) }}</span>
</span>
</template>
diff --git a/frontend/src/views/knowledge/settings/KBParserSettings.vue b/frontend/src/views/knowledge/settings/KBParserSettings.vue
index 7332b3dfd..107eb2dbd 100644
--- a/frontend/src/views/knowledge/settings/KBParserSettings.vue
+++ b/frontend/src/views/knowledge/settings/KBParserSettings.vue
@@ -66,7 +66,6 @@
</template>
<script setup lang="ts">
-import { pickLocale } from '@/utils/localizedText'
import { ref, watch, computed, onMounted, onUnmounted } from 'vue'
import { useI18n } from 'vue-i18n'
import { type ParserEngineInfo } from '@/api/system'
@@ -74,13 +73,10 @@ import { useEditorResourcesStore } from '@/stores/editorResources'
import { useUIStore } from '@/stores/ui'
import { storeToRefs } from 'pinia'
-const { t, locale } = useI18n()
+const { t } = useI18n()
const editorResources = useEditorResourcesStore()
function getEngineDisplayName(engineName: string): string {
- // Plugin engines name themselves.
- const names = parserEngines.value.find((e) => e.Name === engineName)?.DisplayNames
- if (names) return pickLocale(names, locale.value) || names.default || engineName
const key = `kbSettings.parser.engines.${engineName}.name`
const translated = t(key)
return translated !== key ? translated : engineName
diff --git a/frontend/src/views/knowledge/settings/connectorLabels.test.ts b/frontend/src/views/knowledge/settings/connectorLabels.test.ts
deleted file mode 100644
index fa41587c1..000000000
--- a/frontend/src/views/knowledge/settings/connectorLabels.test.ts
+++ /dev/null
@@ -1,33 +0,0 @@
-import assert from 'node:assert/strict'
-import test from 'node:test'
-
-import { connectorDescription, connectorName, isPluginChannel, pickLocale } from './connectorLabels'
-
-const keys: Record<string, string> = { 'datasource.connector.rss': 'RSS', 'datasource.connectorDesc.rss': 'Feeds' }
-const i18n = (locale: string) => ({ t: (k: string) => keys[k] ?? k, te: (k: string) => k in keys, locale })
-
-test('builtins use their locale keys', () => {
- assert.equal(connectorName({ type: 'rss', name: 'ignored' }, i18n('zh-CN')), 'RSS')
- assert.equal(connectorDescription({ type: 'rss' }, i18n('en-US')), 'Feeds')
-})
-
-test('plugin connectors use their own localized names', () => {
- const plugin = {
- type: 'acme.feeds/feed',
- name: 'ACME Feed',
- names: { 'en-US': 'ACME Feed', 'zh-CN': 'ACME 订阅' },
- description: 'Feeds',
- }
- assert.equal(connectorName(plugin, i18n('zh-CN')), 'ACME 订阅')
- assert.equal(connectorName(plugin, i18n('zh-TW')), 'ACME 订阅')
- assert.equal(connectorName(plugin, i18n('ja-JP')), 'ACME Feed')
- assert.equal(connectorDescription(plugin, i18n('ja-JP')), 'Feeds')
- assert.equal(connectorName({ type: 'acme.x/y' }, i18n('en-US')), 'acme.x/y')
-})
-
-test('pickLocale and plugin channels', () => {
- assert.equal(pickLocale({ 'zh-CN': 'a' }, 'zh-HK'), 'a')
- assert.equal(pickLocale(undefined, 'en-US'), undefined)
- assert.equal(isPluginChannel('acme.feeds/feed'), true)
- assert.equal(isPluginChannel('rss'), false)
-})
diff --git a/frontend/src/views/knowledge/settings/connectorLabels.ts b/frontend/src/views/knowledge/settings/connectorLabels.ts
deleted file mode 100644
index 01c0c1339..000000000
--- a/frontend/src/views/knowledge/settings/connectorLabels.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-// Display names of data source connectors. Builtins have locale keys
-// (datasource.connector.<type>); plugin connectors bring localized names in
-// their metadata. Kept free of Vue so it runs under node:test.
-import { pickLocale } from '../../../utils/localizedText'
-
-export { pickLocale }
-
-/** The parts of connector metadata labels need. */
-export interface ConnectorLabelSource {
- type: string
- name?: string
- description?: string
- names?: Record<string, string>
- descriptions?: Record<string, string>
-}
-
-/** The subset of vue-i18n the helpers use. */
-export interface LabelI18n {
- t: (key: string) => string
- te: (key: string) => boolean
- locale: string
-}
-
-/** A connector's name: locale key, plugin variant, plugin default, type. */
-export function connectorName(src: ConnectorLabelSource, i18n: LabelI18n): string {
- const key = `datasource.connector.${src.type}`
- if (i18n.te(key)) return i18n.t(key)
- return pickLocale(src.names, i18n.locale) || src.name || src.type
-}
-
-/** A connector's description, the same way; empty when there is none. */
-export function connectorDescription(src: ConnectorLabelSource, i18n: LabelI18n): string {
- const key = `datasource.connectorDesc.${src.type}`
- if (i18n.te(key)) return i18n.t(key)
- return pickLocale(src.descriptions, i18n.locale) || src.description || ''
-}
-
-/** Channels plugin connectors ingest under: their qualified type ID. */
-export function isPluginChannel(channel: string | undefined): boolean {
- return !!channel && channel.includes('/')
-}
diff --git a/frontend/src/views/settings/McpSettings.vue b/frontend/src/views/settings/McpSettings.vue
index ea39b0dcf..d6e5807b2 100644
--- a/frontend/src/views/settings/McpSettings.vue
+++ b/frontend/src/views/settings/McpSettings.vue
@@ -25,10 +25,7 @@
<t-icon name="tools" size="14px" />
</div>
<h3 class="service-card__title" :title="service.name">{{ service.name }}</h3>
- <span v-if="service.plugin_id" class="service-card__builtin" :title="service.plugin_id">
- {{ $t('mcpSettings.fromPlugin') }}
- </span>
- <span v-else-if="service.is_builtin" class="service-card__builtin">{{ $t('mcpSettings.builtin') }}</span>
+ <span v-if="service.is_builtin" class="service-card__builtin">{{ $t('mcpSettings.builtin') }}</span>
<div v-if="authStore.hasRole('admin')" class="service-card__actions">
<button type="button" class="service-card__icon-btn" :title="$t('common.edit')"
:aria-label="`${service.name} · ${$t('common.edit')}`" @click="handleEdit(service)">
@@ -41,10 +38,6 @@
</button>
</div>
</div>
- <p v-if="service.plugin_error" class="service-card__plugin-error" :title="service.plugin_error">
- <t-icon name="error-circle" size="14px" />
- {{ $t('mcpSettings.pluginNotConfigured') }}
- </p>
<p v-if="serviceUsage(service)" class="service-card__desc" :title="serviceUsage(service)">
{{ serviceUsage(service).replace(/\s+/g, ' ') }}
</p>
@@ -73,7 +66,7 @@
<span class="service-card__type">{{ getTransportTypeLabel(service.transport_type) }}</span>
</div>
<component :is="authStore.hasRole('admin') && !service.is_builtin ? 'button' : 'span'"
- class="service-card__status" :class="{ 'is-enabled': isOn(service) }"
+ class="service-card__status" :class="{ 'is-enabled': service.enabled || service.is_builtin }"
:type="authStore.hasRole('admin') && !service.is_builtin ? 'button' : undefined"
:role="authStore.hasRole('admin') && !service.is_builtin ? 'switch' : undefined"
:aria-checked="authStore.hasRole('admin') && !service.is_builtin ? service.enabled : undefined"
@@ -83,7 +76,7 @@
@click="handleToggleEnabled(service)">
<t-loading v-if="togglingIds.has(service.id)" size="12px" />
<span v-else class="service-card__status-dot" aria-hidden="true" />
- {{ $t(isOn(service) ? 'common.on' : 'common.off') }}
+ {{ $t(service.enabled || service.is_builtin ? 'common.on' : 'common.off') }}
</component>
</div>
</div>
@@ -143,9 +136,6 @@ const currentService = ref<MCPService | null>(null)
const dialogInitialStep = ref<0 | 1>(0)
const togglingIds = ref(new Set<string>())
const serviceUsage = (service: MCPService) => service.usage_instructions?.trim() || service.description?.trim() || ''
-// Builtin services are always on; a plugin service is on once the workspace
-// has configured the plugin.
-const isOn = (service: MCPService) => service.enabled || (!!service.is_builtin && !service.plugin_id)
// Load MCP services
const loadServices = async () => {
@@ -241,9 +231,6 @@ const getTransportTypeLabel = (transportType: string) => {
return 'HTTP Streamable'
case 'stdio':
return 'Stdio'
- case 'plugin':
- // Served by the plugin itself: there is no URL to show.
- return t('mcpSettings.transportPlugin')
default:
return transportType
}
@@ -403,15 +390,6 @@ defineExpose({ openAdd: handleAdd })
color: var(--td-text-color-placeholder);
}
-.service-card__plugin-error {
- display: flex;
- align-items: center;
- gap: 4px;
- margin: 0;
- font-size: var(--app-text-sm);
- color: var(--td-warning-color);
-}
-
.service-card__type {
flex-shrink: 0;
font-size: var(--app-text-xs);
diff --git a/frontend/src/views/settings/ParserEngineSettings.vue b/frontend/src/views/settings/ParserEngineSettings.vue
index e046eb566..436d255ee 100644
--- a/frontend/src/views/settings/ParserEngineSettings.vue
+++ b/frontend/src/views/settings/ParserEngineSettings.vue
@@ -409,7 +409,6 @@
</template>
<script setup lang="ts">
-import { pickLocale } from '@/utils/localizedText'
import { ref, computed, onMounted, nextTick } from 'vue'
import { useI18n } from 'vue-i18n'
import { useUIStore } from '@/stores/ui'
@@ -426,7 +425,7 @@ import {
} from '@/api/system'
import { getWeKnoraCloudStatus } from '@/api/model'
-const { t, locale } = useI18n()
+const { t } = useI18n()
const uiStore = useUIStore()
const authStore = useAuthStore()
@@ -547,9 +546,6 @@ function engineInitial(engineName: string): string {
}
function getEngineDisplayName(engineName: string): string {
- // Plugin engines name themselves.
- const names = engines.value.find((e) => e.Name === engineName)?.DisplayNames
- if (names) return pickLocale(names, locale.value) || names.default || engineName
const key = `kbSettings.parser.engines.${engineName}.name`
const translated = t(key)
return translated !== key ? translated : engineName
diff --git a/frontend/src/views/settings/PluginCenter.vue b/frontend/src/views/settings/PluginCenter.vue
deleted file mode 100644
index e97f16c79..000000000
--- a/frontend/src/views/settings/PluginCenter.vue
+++ /dev/null
@@ -1,450 +0,0 @@
-<template>
- <div class="plugin-center">
- <div class="section-header">
- <h2>{{ t('pluginCenter.title') }}</h2>
- <p class="section-description">{{ t('pluginCenter.description') }}</p>
- </div>
-
- <div class="plugin-center__toolbar">
- <t-input v-model="query" class="plugin-center__search" clearable :placeholder="t('pluginCenter.searchPlaceholder')">
- <template #prefix-icon><t-icon name="search" /></template>
- </t-input>
- <div class="option-chips">
- <button type="button" class="option-chip" :class="{ 'option-chip--active': point === '' }" @click="point = ''">
- {{ t('pluginCenter.allPoints') }}
- </button>
- <button
- v-for="p in EXTENSION_POINTS"
- :key="p"
- type="button"
- class="option-chip"
- :class="{ 'option-chip--active': point === p }"
- @click="point = p"
- >
- {{ t(`pluginCenter.points.${p}`) }}
- </button>
- </div>
- </div>
-
- <div v-if="loading" class="plugin-center__state"><t-loading size="small" /></div>
- <div v-else-if="visible.length === 0" class="plugin-center__state">
- <t-empty :description="t('pluginCenter.empty')" />
- </div>
- <div v-else class="plugin-list">
- <div v-for="p in visible" :key="p.manifest.id" class="plugin-card" :class="{ 'plugin-card--off': !p.enabled }">
- <div class="plugin-card__badge">{{ initial(p) }}</div>
- <div class="plugin-card__body">
- <div class="plugin-card__title">
- <span class="plugin-card__name">{{ nameOf(p) }}</span>
- <t-tag v-if="p.manifest.builtin" size="small" variant="light">{{ t('pluginCenter.builtin') }}</t-tag>
- <t-tag v-else size="small" variant="light" theme="warning">{{ t('pluginCenter.installed') }}</t-tag>
- <t-tag v-if="p.manifest.required" size="small" variant="light" theme="primary">
- {{ t('pluginCenter.required') }}
- </t-tag>
- </div>
- <div class="plugin-card__meta">{{ p.manifest.id }} · v{{ p.manifest.version }}</div>
- <div v-if="descriptionOf(p)" class="plugin-card__desc">{{ descriptionOf(p) }}</div>
- <div class="plugin-card__contribs">
- <span v-for="s in contributionSummary(p.manifest)" :key="s.point" class="plugin-card__contrib">
- {{ t(`pluginCenter.points.${s.point}`) }} × {{ s.count }}
- </span>
- </div>
- </div>
- <div class="plugin-card__actions">
- <t-tooltip :content="switchTooltip(p)" :disabled="!switchTooltip(p)">
- <t-switch
- :model-value="p.enabled"
- :disabled="!canManage || p.manifest.required || pending.has(p.manifest.id)"
- :loading="pending.has(p.manifest.id)"
- @update:model-value="(v: boolean) => toggle(p, v)"
- />
- </t-tooltip>
- <t-button
- v-if="canManage && canConfigure(p.manifest)"
- size="small"
- variant="text"
- theme="primary"
- @click="openConfig(p)"
- >
- {{ t('pluginCenter.configure') }}
- </t-button>
- </div>
- </div>
- </div>
-
- <SettingDrawer
- v-model:visible="configOpen"
- :title="t('pluginCenter.configTitle', { name: configPlugin ? nameOf(configPlugin) : '' })"
- :description="t('pluginCenter.configDescription')"
- icon="setting"
- :confirm-loading="configSaving"
- :confirm-disabled="!configSchema || configSaving"
- :hide-footer="!configSchema"
- @confirm="saveConfig"
- >
- <div v-if="configLoading" class="plugin-center__state"><t-loading size="small" /></div>
- <template v-else>
- <section v-if="configSchema" class="setting-drawer__section">
- <SchemaForm v-model="configValues" :schema="configSchema" :errors="configErrors" />
- </section>
- <section v-if="configWebhooks.length" class="setting-drawer__section">
- <h4 class="setting-drawer__section-title">{{ t('pluginCenter.webhooks') }}</h4>
- <p class="plugin-center__hint">{{ t('pluginCenter.webhooksHint') }}</p>
- <div v-for="hook in configWebhooks" :key="hook.id" class="plugin-center__webhook">
- <div class="plugin-center__webhook-name">{{ localizedText(hook.name, locale) }}</div>
- <p v-if="hook.description" class="plugin-center__hint">{{ localizedText(hook.description, locale) }}</p>
- <div class="plugin-center__webhook-url">
- <code>{{ webhookUrl(hook, origin) }}</code>
- <t-button size="small" variant="text" theme="primary"
- @click="copyWithToast(webhookUrl(hook, origin), 'pluginCenter.webhookCopied')">
- {{ t('pluginCenter.copy') }}
- </t-button>
- </div>
- </div>
- </section>
- </template>
- </SettingDrawer>
- </div>
-</template>
-
-<script setup lang="ts">
-import { computed, onMounted, ref } from 'vue'
-import { useI18n } from 'vue-i18n'
-import { MessagePlugin } from 'tdesign-vue-next'
-
-import {
- getPluginConfig,
- listPluginWebhooks,
- listPlugins,
- setPluginEnabled,
- updatePluginConfig,
- type ExtensionPoint,
- type PluginWebhook,
- type TenantPlugin,
-} from '@/api/plugin'
-import SchemaForm from '@/components/schema-form/SchemaForm.vue'
-import { pluginFormSource } from '@/components/schema-form/pluginSource'
-import { provideSchemaFormSource } from '@/components/schema-form/source'
-import { validateConfig, type ConfigSchema, type ConfigValue, type FieldError } from '@/components/schema-form/schema'
-import SettingDrawer from '@/components/settings/SettingDrawer.vue'
-import { useAuthStore } from '@/stores/auth'
-import { usePluginPagesStore } from '@/stores/pluginPages'
-import { localizedText } from '@/utils/localizedText'
-import { copyWithToast } from '@/utils/clipboard'
-
-import {
- EXTENSION_POINTS,
- canConfigure,
- contributionSummary,
- filterPlugins,
- hasTenantConfig,
- hasWebhooks,
- webhookUrl,
-} from './pluginCenterState'
-
-// Lists the plugins this deployment knows — builtins included — and lets an
-// admin turn them off for the workspace. A disabled plugin's integrations drop
-// out of the type lists; existing instances keep working.
-const { t, locale } = useI18n()
-const authStore = useAuthStore()
-
-const plugins = ref<TenantPlugin[]>([])
-const loading = ref(false)
-const query = ref('')
-const point = ref<ExtensionPoint | ''>('')
-const pending = ref(new Set<string>())
-
-const canManage = computed(() => authStore.canAccessAllTenants || authStore.hasRole('admin'))
-
-const visible = computed(() =>
- filterPlugins(plugins.value, { query: query.value, point: point.value, locale: locale.value }),
-)
-
-const nameOf = (p: TenantPlugin) => localizedText(p.manifest.name, locale.value)
-const descriptionOf = (p: TenantPlugin) => localizedText(p.manifest.description, locale.value)
-const initial = (p: TenantPlugin) => (nameOf(p).trim().charAt(0) || '?').toUpperCase()
-
-function switchTooltip(p: TenantPlugin): string {
- if (p.manifest.required) return t('pluginCenter.requiredHint')
- if (!canManage.value) return t('pluginCenter.adminOnly')
- return ''
-}
-
-async function load() {
- loading.value = true
- try {
- const res = await listPlugins()
- plugins.value = res.data || []
- } catch (e: any) {
- MessagePlugin.error(e?.message || t('pluginCenter.loadFailed'))
- } finally {
- loading.value = false
- }
-}
-
-const pluginPages = usePluginPagesStore()
-
-async function toggle(p: TenantPlugin, enabled: boolean) {
- const id = p.manifest.id
- pending.value = new Set([...pending.value, id])
- try {
- const res = await setPluginEnabled(id, enabled)
- const updated = res.data
- plugins.value = plugins.value.map((x) => (x.manifest.id === id ? { ...x, ...updated } : x))
- // The plugin's pages, settings sections and tabs come and go with it.
- void pluginPages.ensure(true).catch(() => {})
- MessagePlugin.success(enabled ? t('pluginCenter.enabledToast') : t('pluginCenter.disabledToast'))
- } catch (e: any) {
- MessagePlugin.error(e?.message || t('pluginCenter.saveFailed'))
- } finally {
- const next = new Set(pending.value)
- next.delete(id)
- pending.value = next
- }
-}
-
-// Workspace configuration of an installed plugin, such as its API key.
-const configOpen = ref(false)
-const configPlugin = ref<TenantPlugin | null>(null)
-const configSchema = ref<ConfigSchema | null>(null)
-const configValues = ref<ConfigValue>({})
-const configErrors = ref<FieldError[]>([])
-const configLoading = ref(false)
-const configSaving = ref(false)
-const configWebhooks = ref<PluginWebhook[]>([])
-
-// x-options / x-oauth fields of the workspace configuration ask the plugin.
-provideSchemaFormSource(pluginFormSource({
- target: () => (configPlugin.value ? { pluginId: configPlugin.value.manifest.id, scope: 'tenant' } : undefined),
- values: () => configValues.value,
-}))
-const origin = window.location.origin
-
-async function openConfig(p: TenantPlugin) {
- configPlugin.value = p
- configSchema.value = null
- configValues.value = {}
- configErrors.value = []
- configWebhooks.value = []
- configOpen.value = true
- configLoading.value = true
- try {
- const [config, hooks] = await Promise.all([
- hasTenantConfig(p.manifest) ? getPluginConfig(p.manifest.id) : null,
- hasWebhooks(p.manifest) ? listPluginWebhooks(p.manifest.id) : null,
- ])
- if (config) {
- configSchema.value = config.data.schema
- configValues.value = config.data.values ?? {}
- }
- configWebhooks.value = hooks?.data ?? []
- } catch (e: any) {
- MessagePlugin.error(e?.message || t('pluginCenter.configLoadFailed'))
- } finally {
- configLoading.value = false
- }
-}
-
-async function saveConfig() {
- const p = configPlugin.value
- if (!p || !configSchema.value) return
- configErrors.value = validateConfig(configSchema.value, configValues.value)
- if (configErrors.value.length) return
- configSaving.value = true
- try {
- const res = await updatePluginConfig(p.manifest.id, configValues.value)
- configValues.value = res.data.values ?? {}
- MessagePlugin.success(t('pluginCenter.configSaved'))
- configOpen.value = false
- } catch (e: any) {
- const details = e?.error?.details
- if (Array.isArray(details)) configErrors.value = details
- MessagePlugin.error(e?.message || t('pluginCenter.configSaveFailed'))
- } finally {
- configSaving.value = false
- }
-}
-
-onMounted(load)
-</script>
-
-<style lang="less" scoped>
-@import (reference) '@/components/css/provider-card.less';
-@import (reference) '@/components/css/settings-section.less';
-
-.plugin-center {
- width: 100%;
-}
-
-.section-header {
- .settings-section-header();
-}
-
-.plugin-center__toolbar {
- display: flex;
- flex-direction: column;
- gap: 12px;
- margin-bottom: 16px;
-}
-
-.plugin-center__search {
- max-width: 360px;
-}
-
-// Segmented point filter, same look as the IM channel mode switch.
-.option-chips {
- display: inline-flex;
- flex-wrap: wrap;
- align-self: flex-start;
- gap: 4px;
- padding: 3px;
- border-radius: var(--app-radius-md);
- background: var(--td-bg-color-secondarycontainer);
-}
-
-.option-chip {
- border: none;
- background: transparent;
- color: var(--td-text-color-secondary);
- font: inherit;
- font-size: var(--app-text-sm);
- line-height: 1.3;
- padding: 5px 10px;
- border-radius: var(--app-radius-sm);
- cursor: pointer;
- transition: background var(--app-motion-fast) ease, color var(--app-motion-fast) ease;
- white-space: nowrap;
-
- &:hover {
- color: var(--td-text-color-primary);
- }
-
- &--active {
- background: var(--td-bg-color-container);
- color: var(--td-brand-color);
- font-weight: 500;
- box-shadow: var(--td-shadow-1);
- }
-}
-
-.plugin-center__hint {
- margin: 0 0 8px;
- font-size: var(--app-text-sm);
- line-height: 1.5;
- color: var(--td-text-color-placeholder);
-}
-
-.plugin-center__webhook {
- display: flex;
- flex-direction: column;
- gap: 4px;
-
- & + & {
- margin-top: 12px;
- }
-}
-
-.plugin-center__webhook-name {
- font-size: var(--app-text-sm);
- font-weight: 500;
- color: var(--td-text-color-primary);
-}
-
-.plugin-center__webhook-url {
- display: flex;
- align-items: center;
- gap: 8px;
- min-width: 0;
-
- code {
- flex: 1;
- min-width: 0;
- padding: 6px 8px;
- font-size: var(--app-text-xs);
- word-break: break-all;
- color: var(--td-text-color-secondary);
- background: var(--td-bg-color-secondarycontainer);
- border-radius: var(--app-radius-sm);
- }
-}
-
-.plugin-center__state {
- display: flex;
- justify-content: center;
- padding: 32px 0;
-}
-
-.plugin-list {
- display: grid;
- grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
- gap: 12px;
-}
-
-.plugin-card {
- .provider-card();
- align-items: flex-start;
-
- &--off {
- .plugin-card__badge,
- .plugin-card__body {
- opacity: 0.55;
- }
- }
-}
-
-.plugin-card__badge {
- .provider-card-badge();
- .provider-card-badge-color(#0052d9);
-}
-
-.plugin-card__body {
- .provider-card-body();
- gap: 4px;
-}
-
-.plugin-card__title {
- display: flex;
- align-items: center;
- gap: 6px;
- min-width: 0;
-}
-
-.plugin-card__name {
- .provider-card-title();
-}
-
-.plugin-card__meta {
- font-family: var(--app-font-family-mono);
- font-size: var(--app-text-xs);
- color: var(--td-text-color-placeholder);
-}
-
-.plugin-card__desc {
- font-size: var(--app-text-sm);
- color: var(--td-text-color-secondary);
- line-height: 1.5;
-}
-
-.plugin-card__contribs {
- display: flex;
- flex-wrap: wrap;
- gap: 6px;
- margin-top: 4px;
-}
-
-.plugin-card__actions {
- display: flex;
- flex-direction: column;
- align-items: flex-end;
- gap: 6px;
- flex: none;
-}
-
-.plugin-card__contrib {
- font-size: var(--app-text-xs);
- color: var(--td-text-color-secondary);
- background: var(--td-bg-color-secondarycontainer);
- border-radius: var(--app-radius-xs);
- padding: 1px 6px;
-}
-</style>
diff --git a/frontend/src/views/settings/Settings.vue b/frontend/src/views/settings/Settings.vue
index aecd7ba39..6db181402 100644
--- a/frontend/src/views/settings/Settings.vue
+++ b/frontend/src/views/settings/Settings.vue
@@ -9,8 +9,37 @@
'has-submenu': item.children && item.children.length > 0,
'expanded': expandedMenus.includes(item.key)
}]" @click="handleNavClick(item)">
- <SettingsNavIcon :icon="item.icon" />
- <span class="nav-label">{{ item.label() }}</span>
+ <!-- 网络搜索使用自定义 SVG 图标 -->
+ <svg v-if="item.key === 'websearch'" width="17" height="17" viewBox="0 0 18 18" fill="none"
+ xmlns="http://www.w3.org/2000/svg" class="nav-icon">
+ <circle cx="9" cy="9" r="7" stroke="currentColor" stroke-width="1.2" fill="none" />
+ <path d="M 9 2 A 3.5 7 0 0 0 9 16" stroke="currentColor" stroke-width="1.2" fill="none" />
+ <path d="M 9 2 A 3.5 7 0 0 1 9 16" stroke="currentColor" stroke-width="1.2" fill="none" />
+ <line x1="2.94" y1="5.5" x2="15.06" y2="5.5" stroke="currentColor" stroke-width="1.2"
+ stroke-linecap="round" />
+ <line x1="2.94" y1="12.5" x2="15.06" y2="12.5" stroke="currentColor" stroke-width="1.2"
+ stroke-linecap="round" />
+ </svg>
+ <!-- WeKnora Cloud 使用自定义 W 图标 -->
+ <svg v-else-if="item.key === 'weknoracloud'" width="17" height="17" viewBox="0 0 18 18"
+ fill="none" xmlns="http://www.w3.org/2000/svg" class="nav-icon">
+ <rect x="1.5" y="1.5" width="15" height="15" rx="3.5" stroke="currentColor" stroke-width="1.2"
+ fill="none" />
+ <path d="M4.5 5.5L6.5 12.5L9 7.5L11.5 12.5L13.5 5.5" stroke="currentColor" stroke-width="1.3"
+ stroke-linecap="round" stroke-linejoin="round" fill="none" />
+ </svg>
+ <!-- 沙箱:隔离运行窗口,避免和 Ollama / 系统设置共用 server -->
+ <svg v-else-if="item.key === 'sandbox'" width="17" height="17" viewBox="0 0 18 18" fill="none"
+ xmlns="http://www.w3.org/2000/svg" class="nav-icon">
+ <rect x="2.5" y="3" width="13" height="12" rx="2" stroke="currentColor" stroke-width="1.2"
+ fill="none" />
+ <path d="M2.5 6.5h13" stroke="currentColor" stroke-width="1.2" />
+ <path d="M5.5 10h4M5.5 12.5h2.5" stroke="currentColor" stroke-width="1.2"
+ stroke-linecap="round" />
+ </svg>
+ <span v-else-if="item.emoji" class="nav-icon nav-icon-emoji">{{ item.emoji }}</span>
+ <t-icon v-else :name="item.icon" class="nav-icon" />
+ <span class="nav-label">{{ item.label }}</span>
<t-icon v-if="item.children && item.children.length > 0"
:name="expandedMenus.includes(item.key) ? 'chevron-down' : 'chevron-right'"
class="expand-icon" />
@@ -30,8 +59,8 @@
</template>
</template>
<div class="content-wrapper" :class="{
- 'content-wrapper--wide': currentEntry?.layout === 'wide',
- 'content-wrapper--full': currentEntry?.layout === 'full',
+ 'content-wrapper--wide': currentSection === 'members',
+ 'content-wrapper--full': SYSTEM_ADMIN_SECTIONS.has(currentSection) || isIntegrationSection(currentSection),
}">
<!-- 角色不允许访问当前 section(deep-link 进来 / 跨空间切换后角色降级)—— 优先于具体 section 渲染。
正常导航走 navItems filter 不会到这里,但 watch(navItems) 的 fallback 会在角色降级
@@ -43,10 +72,120 @@
<div class="role-denied-title">{{ $t('settings.roleDenied.title') }}</div>
<div class="role-denied-desc">{{ $t('settings.roleDenied.desc') }}</div>
</div>
- <!-- Sections come from the settings registry: builtins plus plugin pages. -->
- <div v-else-if="currentEntry" class="section">
- <component :is="currentEntry.component" v-bind="currentEntry.props?.() ?? {}" />
- </div>
+ <template v-else>
+ <!-- 常规设置 -->
+ <div v-if="currentSection === 'general'" class="section">
+ <GeneralSettings />
+ </div>
+
+ <!-- Ollama 设置 -->
+ <div v-if="currentSection === 'ollama'" class="section">
+ <OllamaSettings />
+ </div>
+
+ <!-- WeKnora Cloud -->
+ <div v-if="currentSection === 'weknoracloud'" class="section">
+ <WeKnoraCloudSettings />
+ </div>
+
+ <!-- 模型配置 -->
+ <div v-if="currentSection === 'models'" class="section">
+ <ModelSettings />
+ </div>
+
+ <!-- 网络搜索配置 -->
+ <div v-if="currentSection === 'websearch'" class="section">
+ <WebSearchSettings />
+ </div>
+
+ <!-- 消息管理 -->
+ <div v-if="currentSection === 'chathistory'" class="section">
+ <ChatHistorySettings />
+ </div>
+
+ <!-- 长期记忆(空间级开关) -->
+ <div v-if="currentSection === 'memory'" class="section">
+ <MemoryWorkspaceSettings />
+ </div>
+
+ <!-- 我的记忆(个人记忆管理) -->
+ <div v-if="currentSection === 'mymemory'" class="section">
+ <MemorySettings />
+ </div>
+
+ <!-- 沙箱密钥(成员自己的技能 / 沙箱密钥) -->
+ <div v-if="currentSection === 'envvars'" class="section">
+ <EnvVarSettings />
+ </div>
+
+ <!-- 向量数据库引擎 -->
+ <div v-if="currentSection === 'vectorstore'" class="section">
+ <VectorStoreSettings />
+ </div>
+
+ <!-- 解析引擎 -->
+ <div v-if="currentSection === 'parser'" class="section">
+ <ParserEngineSettings />
+ </div>
+
+ <!-- 存储引擎 -->
+ <div v-if="currentSection === 'storage'" class="section">
+ <StorageBackendSettings />
+ </div>
+
+ <!-- 沙箱 -->
+ <div v-if="currentSection === 'sandbox'" class="section">
+ <SandboxSettings />
+ </div>
+
+ <!-- 系统信息 -->
+ <div v-if="currentSection === 'system'" class="section">
+ <SystemInfo />
+ </div>
+
+ <!-- 系统管理员可见的全局运行时设置 -->
+ <div v-if="currentSection === 'system-global'" class="section">
+ <SystemSettings />
+ </div>
+
+ <div v-if="currentSection === 'model-catalog'" class="section">
+ <ModelCatalog />
+ </div>
+
+ <!-- 系统管理员可见的任务队列运行状态 -->
+ <div v-if="currentSection === 'runtime-queues'" class="section">
+ <RuntimeQueues />
+ </div>
+
+ <div v-if="currentSection === 'platform-api-keys'" class="section">
+ <PlatformAPIKeys />
+ </div>
+
+ <div v-if="currentSection === 'system-audit-log'" class="section">
+ <SystemAuditLog />
+ </div>
+
+ <!-- 用户信息(账户基础信息:ID / 用户名 / 邮箱 / 注册时间)。
+ 用户的基本信息不该跟 owner 权限绑定。 -->
+ <div v-if="currentSection === 'userprofile'" class="section">
+ <UserProfile />
+ </div>
+
+ <!-- 空间信息 -->
+ <div v-if="currentSection === 'tenant'" class="section">
+ <TenantInfo />
+ </div>
+
+ <!-- 成员管理 (#1303 PR 3) -->
+ <div v-if="currentSection === 'members'" class="section">
+ <TenantMembers />
+ </div>
+
+ <!-- 发布集成 -->
+ <div v-if="isIntegrationSection(currentSection)" class="section">
+ <IntegrationSettingsSection :tab="integrationTabFromSection(currentSection)" />
+ </div>
+ </template>
</div>
</SettingsModalShell>
</template>
@@ -62,23 +201,46 @@ import { useI18n } from 'vue-i18n'
import { MessagePlugin } from 'tdesign-vue-next'
import { useModalShell } from '@/composables/useModalShell'
import SettingsModalShell from '@/components/SettingsModalShell.vue'
-import SettingsNavIcon from '@/extensions/SettingsNavIcon.vue'
-import { registerBuiltinSettings } from '@/extensions/builtin/settings'
-import { syncPluginSettingsSections } from '@/extensions/pluginFrame/settingsSync'
-import { usePluginPagesStore } from '@/stores/pluginPages'
+import SystemInfo from './SystemInfo.vue'
+import TenantInfo from './TenantInfo.vue'
+import UserProfile from './UserProfile.vue'
+import GeneralSettings from './GeneralSettings.vue'
+import ModelSettings from './ModelSettings.vue'
+import ModelCatalog from '../system/ModelCatalog.vue'
+import OllamaSettings from './OllamaSettings.vue'
+import WebSearchSettings from './WebSearchSettings.vue'
+import ChatHistorySettings from './ChatHistorySettings.vue'
+import MemorySettings from './MemorySettings.vue'
+import EnvVarSettings from './EnvVarSettings.vue'
+import MemoryWorkspaceSettings from './MemoryWorkspaceSettings.vue'
+import VectorStoreSettings from './VectorStoreSettings.vue'
+import ParserEngineSettings from './ParserEngineSettings.vue'
+import StorageBackendSettings from './StorageBackendSettings.vue'
+import SandboxSettings from './SandboxSettings.vue'
+import WeKnoraCloudSettings from './WeKnoraCloudSettings.vue'
+import TenantMembers from './TenantMembers.vue'
+import SystemSettings from '@/views/system/SystemSettings.vue'
+import RuntimeQueues from '@/views/system/RuntimeQueues.vue'
+import PlatformAPIKeys from '@/views/system/PlatformAPIKeys.vue'
+import SystemAuditLog from '@/views/system/SystemAuditLog.vue'
+import IntegrationSettingsSection from '@/views/integrations/IntegrationSettingsSection.vue'
+import {
+ INTEGRATION_PREVIEW_ITEMS,
+ INTEGRATION_TAB_CAPABILITY,
+ INTEGRATION_TAB_MIN_ROLE,
+} from '@/config/integrations'
import {
- groupSections,
- sectionPermitted,
- sectionSupported,
- settingsGroups,
- settingsSections,
- type SettingsAccessContext,
- type SettingsSection,
-} from '@/extensions/settingsSections'
-import { SYSTEM_ADMIN_SETTINGS_SECTIONS } from '@/config/settingsAccess'
+ SETTINGS_SECTION_MIN_ROLE,
+ SYSTEM_ADMIN_SETTINGS_SECTIONS,
+} from '@/config/settingsAccess'
+import { SETTINGS_SECTION_CAPABILITY, skillSettingsSupported } from '@/config/deploymentCapabilities'
import { isToolboxSection, toolboxLocation } from '@/config/toolbox'
+import { hostSkillsOnly } from '@/utils/skillTarget'
import {
buildSettingsRouteQuery,
+ integrationSectionKey,
+ integrationTabFromSection,
+ isIntegrationSection,
normalizeSettingsSection as normalizeSettingsSectionFromQuery,
settingsQueryUnchanged,
} from '@/config/settingsRoute'
@@ -88,34 +250,51 @@ const router = useRouter()
const uiStore = useUIStore()
const authStore = useAuthStore()
const deploymentCapabilities = useDeploymentCapabilitiesStore()
-
-const { t } = useI18n()
-
-// Sections are registered, not listed here: builtins first, plugins later.
-registerBuiltinSettings()
-// Plugins' own settings sections join the plugins group once loaded.
-const pluginPages = usePluginPagesStore()
-const pluginPagesLoaded = ref(false)
-void pluginPages.ensure().catch(() => {}).finally(() => {
- pluginPagesLoaded.value = true
-})
-watch(() => pluginPages.settingsSections, syncPluginSettingsSections, { immediate: true })
+const { t, te } = useI18n()
+const hostSkills = computed(() => hostSkillsOnly(
+ deploymentCapabilities.isSupported('settings.sandbox.remote'),
+ deploymentCapabilities.isSupported('settings.sandbox.host'),
+))
+function envNavLabel(): string {
+ const hostKey = 'envVarSettings.host.title'
+ if (hostSkills.value && te(hostKey)) return t(hostKey)
+ return t('envVarSettings.title')
+}
const currentSection = ref<string>('general')
const currentSubSection = ref<string>('')
const expandedMenus = ref<string[]>([])
-// Who may see a section comes from each section's access rules (for builtins,
-// settingsAccess.ts and deploymentCapabilities.ts, aligned with the guards in
-// internal/router). They only decide whether the UI offers the entry; the
-// server checks every route itself.
-const accessContext = computed<SettingsAccessContext>(() => ({
- isSystemAdmin: authStore.isSystemAdmin,
- canAccessAllTenants: authStore.canAccessAllTenants,
- hasRole: (role) => authStore.hasRole(role),
- isSupported: (capability) => deploymentCapabilities.isSupported(capability),
- capabilities: deploymentCapabilities.capabilities,
-}))
+type NavItem = {
+ key: string
+ icon: string
+ label: string
+ emoji?: string
+ children?: Array<{ key: string; label: string }>
+}
+
+type NavGroup = {
+ key: string
+ label: string
+ items: NavItem[]
+}
+
+// 设置二级导航的最低可见角色来自 settingsAccess.ts,和
+// internal/router/router.go 的守卫矩阵对齐。
+// 以「页面里至少有 1 个有意义的写操作所要求的最低角色」为基准,把基础设
+// 施配置(models 写、ollama 下载、websearch 写、parser/storage/vector/mcp
+// CRUD、sandbox 连接、skills 安装、chat-history 配置)统一收到 admin;只读类(general / system info /
+// tenant-info / members 名册)保留 viewer 可见;最高敏感的 reset api
+// key 是 owner-only。改这张表前请在 router.go 里复核对应路由组。
+//
+// 特别说明:
+// - chathistory 页面唯一的「启用消息索引」开关 PUT /tenants/kv/chat-history-config
+// 后端走 g.Admin()。给 viewer/contributor 看到入口、点开开关、保存时
+// 403,体验很差,所以入口本身归 admin。
+// - models 列表 viewer 可读,页面内的「+ 添加模型 / 编辑 / 删除」按钮在
+// ModelSettings.vue 里另用 hasRole('admin') 自己 gate,所以入口保留
+// viewer 是合理的(contributor 也能浏览模型列表)。
+const SYSTEM_ADMIN_SECTIONS = SYSTEM_ADMIN_SETTINGS_SECTIONS
const normalizeSettingsSection = (section: string) => {
return normalizeSettingsSectionFromQuery(section, route.query.tab as string | undefined)
@@ -132,28 +311,129 @@ const syncSettingsRoute = (sectionKey: string) => {
}
const isSectionSupported = (key: string): boolean => {
- const section = settingsSections.get(key)
- return !!section && sectionSupported(section, accessContext.value)
+ if (isIntegrationSection(key)) {
+ return deploymentCapabilities.isSupported(
+ INTEGRATION_TAB_CAPABILITY[integrationTabFromSection(key)],
+ )
+ }
+ if (key === 'skills' || key === 'envvars') {
+ return skillSettingsSupported(deploymentCapabilities.capabilities)
+ }
+ return deploymentCapabilities.isSupported(SETTINGS_SECTION_CAPABILITY[key])
}
const canSeeSection = (key: string): boolean => {
- const section = settingsSections.get(key)
- // An unknown key (stale deep link) falls through to the fallback watchers.
- return !section || sectionPermitted(section, accessContext.value)
+ if (isIntegrationSection(key)) {
+ const min = INTEGRATION_TAB_MIN_ROLE[integrationTabFromSection(key)]
+ if (!min) return true
+ if (authStore.canAccessAllTenants) return true
+ return authStore.hasRole(min)
+ }
+ if (SYSTEM_ADMIN_SECTIONS.has(key)) {
+ return authStore.isSystemAdmin
+ }
+ const min = SETTINGS_SECTION_MIN_ROLE[key] ?? 'viewer'
+ // canAccessAllTenants(superuser)和路由层一样必须 bypass,否则 cross-tenant
+ // 管理员看不到自己有权操作的入口(参考 TenantMembers.vue 的 canManage)。
+ if (authStore.canAccessAllTenants) return true
+ return authStore.hasRole(min)
}
-const currentEntry = computed<SettingsSection | undefined>(() => settingsSections.get(currentSection.value))
-
-const navItems = computed<SettingsSection[]>(() => {
+const navItems = computed(() => {
+ // 一律走 SETTINGS_SECTION_MIN_ROLE 表,避免 ad-hoc isAdmin/isOwner 散落在多处。
+ // 服务端在每条路由上仍以 g.Viewer/Admin/Owner 为准,这里只决定 UI 是
+ // 否露入口;改动入口规则请同步更新 settingsAccess.ts 和对应后端路由。
+ const integrationItems: NavItem[] = INTEGRATION_PREVIEW_ITEMS.map((item) => ({
+ key: integrationSectionKey(item.key),
+ icon: item.icon.type === 'icon' ? item.icon.name : 'integration',
+ emoji: item.icon.type === 'emoji' ? item.icon.value : undefined,
+ label: t(`integrations.tabs.${item.key}`),
+ }))
+ const all: NavItem[] = [
+ { key: 'general', icon: 'setting', label: t('general.title') },
+ { key: 'ollama', icon: 'server', label: 'Ollama' },
+ { key: 'weknoracloud', icon: '', label: 'WeKnora Cloud' },
+ { key: 'models', icon: 'control-platform', label: t('settings.modelManagement') },
+ { key: 'websearch', icon: 'search', label: t('settings.webSearchConfig') },
+ { key: 'chathistory', icon: 'chat', label: t('chatHistorySettings.title') },
+ { key: 'memory', icon: 'bulletpoint', label: t('memoryWorkspaceSettings.title') },
+ { key: 'vectorstore', icon: 'data-base', label: t('settings.vectorStoreEngine') },
+ { key: 'parser', icon: 'file-search', label: t('settings.parserEngine') },
+ { key: 'storage', icon: 'cloud', label: t('settings.storageEngine') },
+ { key: 'sandbox', icon: 'code', label: t('settings.sandbox.title') },
+ { key: 'system', icon: 'info-circle', label: t('settings.versionInfo') },
+ { key: 'system-global', icon: 'server', label: t('settings.system') },
+ { key: 'model-catalog', icon: 'control-platform', label: t('modelCatalog.title') },
+ { key: 'runtime-queues', icon: 'queue', label: t('settings.taskQueue') },
+ { key: 'platform-api-keys', icon: 'secured', label: t('platformApiKeys.title') },
+ { key: 'system-audit-log', icon: 'history', label: t('system.globalSettings.audit.tabLabel') },
+ { key: 'userprofile', icon: 'user', label: t('userProfile.title') },
+ { key: 'mymemory', icon: 'bookmark', label: t('memorySettings.title') },
+ { key: 'envvars', icon: 'key', label: envNavLabel() },
+ { key: 'tenant', icon: 'user-circle', label: t('settings.tenantInfo') },
+ { key: 'members', icon: 'usergroup', label: t('tenantMember.title') },
+ ...integrationItems,
+ ]
// currentTenantRole 为空表示「membership 还没加载」—— 比起渲染整套
- // viewer 入口然后角色一返回又消失,先卡住不渲染更稳。
- if (!authStore.currentTenantRole && !authStore.canAccessAllTenants) return []
- return settingsSections.items.filter((section) =>
- sectionPermitted(section, accessContext.value) && sectionSupported(section, accessContext.value),
- )
+ // viewer 入口然后角色一返回又消失,先卡住不渲染更稳,跟原先 members
+ // 入口的策略一致。
+ if (!authStore.currentTenantRole && !authStore.canAccessAllTenants) {
+ return [] as NavItem[]
+ }
+ return all.filter((it) => canSeeSection(it.key) && isSectionSupported(it.key))
})
-const navGroups = computed(() => groupSections(settingsGroups.items, navItems.value))
+const navGroups = computed<NavGroup[]>(() => {
+ const itemMap = new Map(navItems.value.map((item) => [item.key, item]))
+ const pickItems = (keys: string[]) => keys.map((key) => itemMap.get(key)).filter(Boolean) as NavItem[]
+ // 分组:账户 → 空间 → 模型 → 发布集成 → 数据与扩展 → 系统管理 → 平台
+ // 关键调整:把个人偏好(general)和用户信息收进「账户」;
+ // 把空间内功能开关(chathistory)从「平台」挪到「空间」;
+ // 把检索引擎和外部集成合并为「数据与扩展」,避免两个 2~3 项的窄分组。
+ return [
+ {
+ key: 'account',
+ label: t('settings.navGroups.account'),
+ items: pickItems(['general', 'userprofile', 'mymemory', 'envvars']),
+ },
+ {
+ key: 'workspace',
+ label: t('settings.navGroups.workspace'),
+ items: pickItems(['tenant', 'members', 'chathistory', 'memory']),
+ },
+ {
+ key: 'models_runtime',
+ label: t('settings.navGroups.modelsRuntime'),
+ items: pickItems(['models', 'ollama', 'weknoracloud']),
+ },
+ {
+ key: 'integrations',
+ label: t('integrations.title'),
+ items: pickItems(INTEGRATION_PREVIEW_ITEMS.map((item) => integrationSectionKey(item.key))),
+ },
+ {
+ key: 'data_extensions',
+ label: t('settings.navGroups.dataExtensions'),
+ items: pickItems([
+ 'vectorstore',
+ 'parser',
+ 'storage',
+ 'sandbox',
+ 'websearch',
+ ]),
+ },
+ {
+ key: 'system_administration',
+ label: t('settings.navGroups.systemAdministration'),
+ items: pickItems(['system-global', 'model-catalog', 'runtime-queues', 'platform-api-keys', 'system-audit-log']),
+ },
+ {
+ key: 'platform',
+ label: t('settings.navGroups.platform'),
+ items: pickItems(['system']),
+ },
+ ].filter((group) => group.items.length > 0)
+})
// 导航项点击处理
const handleNavClick = (item: any) => {
@@ -205,7 +485,7 @@ const handleClose = () => {
// 如果当前路由是设置页,返回上一页
if (route.path === '/platform/settings') {
const sec = route.query.section
- if (typeof sec === 'string' && SYSTEM_ADMIN_SETTINGS_SECTIONS.has(sec)) {
+ if (sec === 'model-catalog' || sec === 'system-global' || sec === 'runtime-queues' || sec === 'platform-api-keys' || sec === 'system-audit-log') {
router.push('/platform/knowledge-bases')
} else {
router.back()
@@ -256,7 +536,7 @@ watch(() => uiStore.settingsInitialSection, (section) => {
}, { immediate: true })
watch(
- () => [visible.value, route.path, route.query.section, deploymentCapabilities.loaded, pluginPagesLoaded.value] as const,
+ () => [visible.value, route.path, route.query.section, deploymentCapabilities.loaded] as const,
([isVisible, path, section, capabilitiesLoaded]) => {
if (!isVisible || path !== '/platform/settings') return
if (typeof section !== 'string') {
@@ -267,9 +547,7 @@ watch(
section,
typeof route.query.tab === 'string' ? route.query.tab : undefined,
)
- // A plugin section registers once the plugin pages load.
- const pendingPlugin = normalizedSection.startsWith('plugin:') && !pluginPagesLoaded.value
- if (capabilitiesLoaded && !pendingPlugin && !isSectionSupported(normalizedSection)) {
+ if (capabilitiesLoaded && !isSectionSupported(normalizedSection)) {
MessagePlugin.warning(t('settings.capabilityUnavailable'))
const fallback = navItems.value[0]?.key || 'general'
currentSection.value = fallback
@@ -286,9 +564,7 @@ watch(
// 切换空间后角色可能变化,原本可见的 admin-only 面板可能消失。
// 如果 currentSection 落到了不再显示的 key 上,就回退到第一个可见项。
-watch([navItems, pluginPagesLoaded], ([items]) => {
- // A plugin section in the URL waits for the plugin sections to register.
- if (currentSection.value.startsWith('plugin:') && !pluginPagesLoaded.value) return
+watch(navItems, (items) => {
if (!items.some((item) => item.key === currentSection.value)) {
const fallback = items[0]?.key || 'general'
currentSection.value = fallback
diff --git a/frontend/src/views/settings/SkillSettings.vue b/frontend/src/views/settings/SkillSettings.vue
index 222c9adc7..719bf4cae 100644
--- a/frontend/src/views/settings/SkillSettings.vue
+++ b/frontend/src/views/settings/SkillSettings.vue
@@ -188,22 +188,6 @@
</article>
<template v-if="addStep === 0">
- <section v-if="pluginSkills.length" class="setting-drawer__section">
- <h4 class="setting-drawer__section-title">{{ $t('settings.skills.pluginSection') }}</h4>
- <p class="installer-model-hint">{{ $t('settings.skills.pluginSectionHint') }}</p>
- <div class="plugin-skill-list">
- <button v-for="s in pluginSkills" :key="s.source" type="button" class="plugin-skill"
- :class="{ 'is-selected': sourceInput === s.source }" :disabled="addBusy || !!registeredCatalog"
- :aria-pressed="sourceInput === s.source" @click="pickPluginSkill(s.source)">
- <span class="plugin-skill__head">
- <span class="plugin-skill__name">{{ s.name }}</span>
- <span class="plugin-skill__plugin">{{ s.plugin }}</span>
- </span>
- <span v-if="s.description" class="plugin-skill__desc">{{ s.description }}</span>
- </button>
- </div>
- </section>
-
<section class="setting-drawer__section">
<h4 class="setting-drawer__section-title">{{ $t('settings.sandbox.skillSourceSection') }}</h4>
<p class="installer-model-hint">{{ $t('settings.sandbox.skillSourceSectionHint', { size: maxSkillBundleMB })
@@ -367,8 +351,6 @@ import SandboxSkillsPanel from '@/components/SandboxSkillsPanel.vue'
import SkillFilesDrawer from '@/components/SkillFilesDrawer.vue'
import SandboxBackendBadge from '@/components/settings/SandboxBackendBadge.vue'
import SettingDrawer from '@/components/settings/SettingDrawer.vue'
-import { listPlugins } from '@/api/plugin'
-import { pluginSkillChoices } from './pluginCenterState'
import ModelSelector from '@/components/ModelSelector.vue'
import { useConfirmDelete } from '@/components/settings/useConfirmDelete'
import { useConfigSkillInstallProgress } from '@/composables/useConfigSkillInstallProgress'
@@ -406,7 +388,7 @@ const props = defineProps<{
}>()
const emit = defineEmits<{ count: [value: number] }>()
-const { t, te, locale } = useI18n()
+const { t, te } = useI18n()
const uiStore = useUIStore()
const deploymentCapabilities = useDeploymentCapabilitiesStore()
const confirmDelete = useConfirmDelete()
@@ -952,30 +934,10 @@ function resetAddWizard() {
async function openAdd() {
resetAddWizard()
- void loadPluginSkills()
await loadInstallerModel()
showAdd.value = true
}
-// Skills of the plugins this workspace enabled register through the same
-// source path as a pasted link, as "plugin:<plugin>/<skill>".
-const pluginSkills = ref<ReturnType<typeof pluginSkillChoices>>([])
-
-async function loadPluginSkills() {
- try {
- const res = await listPlugins()
- pluginSkills.value = pluginSkillChoices(res.data || [], locale.value)
- } catch {
- pluginSkills.value = []
- }
-}
-
-function pickPluginSkill(source: string) {
- sourceInput.value = sourceInput.value === source ? '' : source
- pendingFile.value = null
- if (fileInputRef.value) fileInputRef.value.value = ''
-}
-
function canJumpAddStep(index: number) {
if (index === addStep.value) return false
return Boolean(registeredCatalog.value) || index < addStep.value
@@ -2096,73 +2058,6 @@ onUnmounted(() => {
width: 100%;
}
-.plugin-skill-list {
- display: grid;
- grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
- gap: 8px;
-}
-
-.plugin-skill {
- display: flex;
- flex-direction: column;
- align-items: flex-start;
- gap: 4px;
- padding: 10px 12px;
- border: 1px solid var(--td-component-stroke);
- border-radius: var(--app-radius-md);
- background: var(--td-bg-color-container);
- font: inherit;
- text-align: left;
- cursor: pointer;
- transition: border-color var(--app-motion-fast) ease, background var(--app-motion-fast) ease;
-
- &:hover:not(:disabled) {
- border-color: var(--td-brand-color);
- }
-
- &.is-selected {
- border-color: var(--td-brand-color);
- background: color-mix(in srgb, var(--td-brand-color) 6%, transparent);
- }
-
- &:disabled {
- cursor: not-allowed;
- opacity: 0.6;
- }
-
- &__head {
- display: flex;
- align-items: baseline;
- gap: 6px;
- min-width: 0;
- max-width: 100%;
- }
-
- &__name {
- font-size: var(--app-text-md);
- font-weight: 500;
- color: var(--td-text-color-primary);
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
-
- &__plugin {
- flex: none;
- font-size: var(--app-text-xs);
- color: var(--td-text-color-placeholder);
- }
-
- &__desc {
- font-size: var(--app-text-sm);
- color: var(--td-text-color-secondary);
- display: -webkit-box;
- -webkit-line-clamp: 2;
- -webkit-box-orient: vertical;
- overflow: hidden;
- }
-}
-
.file-input-hidden {
display: none;
}
diff --git a/frontend/src/views/settings/WebSearchSettings.vue b/frontend/src/views/settings/WebSearchSettings.vue
index ddf42a501..5010602bf 100644
--- a/frontend/src/views/settings/WebSearchSettings.vue
+++ b/frontend/src/views/settings/WebSearchSettings.vue
@@ -202,42 +202,96 @@
</div>
</section>
- <!-- Section 2 — 连接配置:按类型的 config_schema 渲染 credentials 分组 -->
- <section v-if="hasGroup('credentials')" class="setting-drawer__section">
+ <!-- Section 2 — 连接配置(base url / api key / engine id),仅当任意字段需要时渲染 -->
+ <section
+ v-if="selectedProviderType?.requires_api_key || selectedProviderType?.supports_optional_api_key || selectedProviderType?.requires_engine_id || selectedProviderType?.requires_base_url || selectedProviderType?.config_fields?.length"
+ class="setting-drawer__section"
+ >
<h4 class="setting-drawer__section-title">{{ t('webSearchSettings.credentialsSection', '连接配置') }}</h4>
+ <div v-if="selectedProviderType?.requires_base_url" class="form-item">
+ <label class="form-label required">{{ t('webSearchSettings.baseUrlLabel') }}</label>
+ <t-input
+ v-model="providerForm.parameters.base_url"
+ :placeholder="t('webSearchSettings.baseUrlPlaceholder')"
+ />
+ </div>
+
<!--
Edit 模式下凭证由 CredentialResource 管理(独立的 /credentials
- 子资源调用),不与本表单 submit 耦合;Create 模式下 SchemaForm
- 直接渲染密码输入框。
+ 子资源调用),不与本表单 submit 耦合;Create 模式下用 plain
+ password input + lock prefix-icon,与 ModelEditorDialog 一致。
-->
- <SchemaForm
- v-model="providerForm.parameters"
- :schema="parametersSchema"
- group="credentials"
- :secret-mode="editingProvider?.id ? 'slot' : 'input'"
- :errors="formErrors"
+ <div v-if="selectedProviderType?.requires_api_key || selectedProviderType?.supports_optional_api_key" class="form-item">
+ <label class="form-label" :class="{ required: selectedProviderType?.requires_api_key }">
+ {{ selectedProviderType?.supports_optional_api_key && !selectedProviderType?.requires_api_key
+ ? t('webSearchSettings.apiKeyOptionalLabel', 'API Key(可选)')
+ : t('webSearchSettings.apiKeyLabel') }}
+ </label>
+ <CredentialResource
+ v-if="editingProvider?.id"
+ :api="credentialApi"
+ :fields="credentialFields"
+ :meta="credentialMeta"
+ />
+ <t-input
+ v-else
+ v-model="providerForm.parameters.api_key"
+ type="password"
+ :placeholder="apiKeyPlaceholder"
+ >
+ <template #prefix-icon><t-icon name="lock-on" /></template>
+ </t-input>
+ </div>
+
+ <div v-if="selectedProviderType?.requires_engine_id" class="form-item">
+ <label class="form-label required">{{ t('webSearchSettings.engineIdLabel') }}</label>
+ <t-input
+ v-model="providerForm.parameters.engine_id"
+ :placeholder="t('webSearchSettings.engineIdLabel')"
+ />
+ </div>
+
+ <div
+ v-for="field in selectedProviderType?.config_fields || []"
+ :key="field.key"
+ class="form-item"
>
- <template #secret>
- <CredentialResource
- :api="credentialApi"
- :fields="credentialFields"
- :meta="credentialMeta"
+ <label class="form-label" :class="{ required: field.required }">
+ {{ configFieldText(field.label_key, field.label) }}
+ </label>
+ <t-select
+ v-if="field.type === 'select'"
+ v-model="providerForm.parameters.extra_config[field.key]"
+ >
+ <t-option
+ v-for="option in field.options || []"
+ :key="option.value"
+ :value="option.value"
+ :label="configFieldText(option.label_key, option.label)"
/>
- </template>
- </SchemaForm>
+ </t-select>
+ <p v-if="field.description" class="form-desc">
+ {{ configFieldText(field.description_key, field.description) }}
+ </p>
+ </div>
</section>
<!-- Section 3 — 选项(代理 / 默认) -->
- <section v-if="selectedProviderType" class="setting-drawer__section">
+ <section
+ v-if="selectedProviderType?.supports_proxy || selectedProviderType"
+ class="setting-drawer__section"
+ >
<h4 class="setting-drawer__section-title">{{ t('webSearchSettings.optionsSection', '选项') }}</h4>
- <SchemaForm
- v-model="providerForm.parameters"
- :schema="parametersSchema"
- group="options"
- :errors="formErrors"
- />
+ <div v-if="selectedProviderType?.supports_proxy" class="form-item">
+ <label class="form-label">{{ t('webSearchSettings.proxyUrlLabel') }}</label>
+ <t-input
+ v-model="providerForm.parameters.proxy_url"
+ :placeholder="t('webSearchSettings.proxyUrlPlaceholder')"
+ />
+ <p class="form-desc">{{ t('webSearchSettings.proxyUrlHelp') }}</p>
+ </div>
<div class="form-item">
<label class="form-label">{{ t('webSearchSettings.setAsDefault') }}</label>
@@ -270,19 +324,6 @@ import {
type WebSearchCredentialField,
} from '@/api/web-search-provider'
import SettingDrawer from '@/components/settings/SettingDrawer.vue'
-import SchemaForm from '@/components/schema-form/SchemaForm.vue'
-import { pluginFormSource } from '@/components/schema-form/pluginSource'
-import { provideSchemaFormSource } from '@/components/schema-form/source'
-import {
- inGroup,
- isPlainObject,
- orderedFields,
- applyDefaults,
- validateConfig,
- type ConfigSchema,
- type ConfigValue,
- type FieldError,
-} from '@/components/schema-form/schema'
import CredentialResource, {
type CredentialFieldDef,
type CredentialResourceApi,
@@ -290,10 +331,9 @@ import CredentialResource, {
import { useConfirmDelete } from '@/components/settings/useConfirmDelete'
import { useAuthStore } from '@/stores/auth'
import { useChatResourcesStore } from '@/stores/chatResources'
-import { pickLocale } from '@/utils/localizedText'
import { providerLogo } from './providerLogos'
-const { t, locale } = useI18n()
+const { t } = useI18n()
const authStore = useAuthStore()
const chatResources = useChatResourcesStore()
const confirmDelete = useConfirmDelete()
@@ -313,25 +353,26 @@ const formRef = ref<any>()
// is initialized so the watch's source function doesn't trip on TDZ).
const lastTestOk = ref<boolean | null>(null)
-// parameters follows the selected type's config_schema: api_key, engine_id,
-// base_url, proxy_url and extra_config, whichever the type declares.
const providerForm = ref<{
name: string
provider: string
description: string
- parameters: ConfigValue
+ parameters: {
+ api_key?: string
+ engine_id?: string
+ base_url?: string
+ proxy_url?: string
+ extra_config: Record<string, string>
+ }
is_default: boolean
}>({
name: '',
provider: 'duckduckgo',
description: '',
- parameters: {},
+ parameters: { extra_config: {} },
is_default: false,
})
-// Field errors from the last save attempt, shown under each field.
-const formErrors = ref<FieldError[]>([])
-
// Invalidate the cached test result whenever the user edits a connection
// field. Set up after providerForm is declared so the watch's source
// function — which dereferences providerForm.value on first run — doesn't
@@ -340,51 +381,22 @@ const formErrors = ref<FieldError[]>([])
watch(
() => [
providerForm.value.provider,
- JSON.stringify({ ...providerForm.value.parameters, proxy_url: undefined }),
+ providerForm.value.parameters?.api_key,
+ providerForm.value.parameters?.engine_id,
+ providerForm.value.parameters?.base_url,
+ JSON.stringify(providerForm.value.parameters?.extra_config || {}),
],
() => { lastTestOk.value = null },
)
-// Any edit clears the errors of the last save attempt.
-watch(() => JSON.stringify(providerForm.value.parameters), () => { formErrors.value = [] })
-
// ===== Computed =====
const selectedProviderType = computed(() => {
return providerTypes.value.find(pt => pt.id === providerForm.value.provider)
})
-const EMPTY_SCHEMA: ConfigSchema = { type: 'object', properties: {} }
-
-const parametersSchema = computed<ConfigSchema>(() => selectedProviderType.value?.config_schema ?? EMPTY_SCHEMA)
-
-// Plugin providers' x-options / x-oauth fields ask the plugin. Its schema is
-// flat: api_key plus fields the form keeps under extra_config.
-const EXTRA = 'extra_config.'
-function pluginInstanceValues(): ConfigValue {
- const { extra_config: extra, ...rest } = providerForm.value.parameters
- return { ...rest, ...(extra && typeof extra === 'object' ? (extra as ConfigValue) : {}) }
-}
-provideSchemaFormSource(pluginFormSource({
- target: () => {
- const type = selectedProviderType.value
- if (!type?.plugin_id || !type.id.startsWith(`${type.plugin_id}/`)) return undefined
- return {
- pluginId: type.plugin_id,
- scope: 'instance',
- contribution: `webSearch/${type.id.slice(type.plugin_id.length + 1)}`,
- instanceId: editingProvider.value?.id,
- }
- },
- values: pluginInstanceValues,
- field: path => (path.startsWith(EXTRA) ? path.slice(EXTRA.length) : path),
-}))
-
-const hasGroup = (group: string) => orderedFields(parametersSchema.value).some(f => inGroup(f.schema, group))
-
-// Validation for the parameters. In edit mode secrets are committed through
-// <CredentialResource>, and the stored key stands in for an empty input.
-const parameterErrors = () =>
- validateConfig(parametersSchema.value, providerForm.value.parameters, { skipSecrets: !!editingProvider.value })
+// Create-mode placeholder (edit mode replaces the input with
+// <CredentialResource>, which has its own placeholder).
+const apiKeyPlaceholder = computed(() => t('webSearchSettings.apiKeyPlaceholder'))
const credentialFields = computed<CredentialFieldDef<WebSearchCredentialField>[]>(() => [
{ key: 'api_key', label: t('webSearchSettings.apiKeyLabel') as string },
@@ -423,7 +435,7 @@ const drawerClass = computed(() => {
// list card showed for the same provider id.
const drawerLogo = computed(() => {
const id = providerForm.value.provider
- return id ? resolveLogo(id) ?? null : null
+ return id ? providerLogo('websearch', id) : null
})
const drawerLogoStyle = computed((): Record<string, string> => {
@@ -437,9 +449,14 @@ const drawerLogoStyle = computed((): Record<string, string> => {
// can fire with no fresh api_key because the backend will fall back to
// the stored credential. Free providers don't show the button at all.
const canTestConnection = computed(() => {
- if (!selectedProviderType.value) return false
+ const pt = selectedProviderType.value
+ if (!pt) return false
if (editingProvider.value) return true
- return parameterErrors().length === 0
+ if (pt.requires_api_key && !providerForm.value.parameters.api_key) return false
+ if (pt.requires_engine_id && !providerForm.value.parameters.engine_id) return false
+ if (pt.requires_base_url && !providerForm.value.parameters.base_url) return false
+ if (pt.config_fields?.some(field => field.required && !providerForm.value.parameters.extra_config?.[field.key])) return false
+ return true
})
// 卡片首字母徽章。复用 providerType 信息表,让多字节缩写也走同一处。
@@ -449,13 +466,7 @@ const providerInitial = (providerId: string) => {
}
// 见 VectorStoreSettings 的同名注释:返回 --logo-url 给 ::before 用 mask 渲染。
-// Plugin provider types carry their own icon.
-const resolveLogo = (providerId: string) => {
- const bundled = providerLogo('websearch', providerId)
- if (bundled) return bundled
- const icon = providerTypes.value.find((p) => p.id === providerId)?.icon
- return icon ? { mode: 'color' as const, url: icon } : undefined
-}
+const resolveLogo = (providerId: string) => providerLogo('websearch', providerId)
const badgeClass = (providerId: string) => {
const m = resolveLogo(providerId)?.mode
@@ -475,17 +486,25 @@ const providerTypeLabel = (providerId: string) => {
return providerTypes.value.find(p => p.id === providerId)?.name || providerId
}
-// Fills a type's schema defaults (e.g. Zhipu's search engine) into parameters.
-const parametersWithDefaults = (providerId: string, parameters: ConfigValue = {}) => {
- const schema = providerTypes.value.find(p => p.id === providerId)?.config_schema
- return schema ? applyDefaults(schema, parameters) : { ...parameters }
+const configFieldText = (key: string | undefined, fallback: string) => {
+ return key ? t(key, fallback) : fallback
+}
+
+const providerConfigDefaults = (providerId: string) => {
+ const fields = providerTypes.value.find(p => p.id === providerId)?.config_fields || []
+ return Object.fromEntries(
+ fields
+ .filter(field => field.default !== undefined)
+ .map(field => [field.key, field.default as string]),
+ )
}
// ===== Methods =====
const onProviderTypeChange = () => {
- providerForm.value.parameters = parametersWithDefaults(providerForm.value.provider)
+ providerForm.value.parameters = {
+ extra_config: providerConfigDefaults(providerForm.value.provider),
+ }
lastTestOk.value = null
- formErrors.value = []
}
// 走 chatResources 的共享快照并强刷:增删改之后对话输入栏读到的就是最新列表,
@@ -501,11 +520,7 @@ const loadProviderEntities = async () => {
const loadProviderTypes = async () => {
try {
- // Plugin provider types name themselves per locale.
- providerTypes.value = (await listWebSearchProviderTypes()).map((pt) => ({
- ...pt,
- name: pickLocale(pt.names, locale.value) || pt.name,
- }))
+ providerTypes.value = await listWebSearchProviderTypes()
} catch (error) {
console.error('Failed to load provider types:', error)
}
@@ -517,11 +532,12 @@ const openAddDialog = () => {
name: '',
provider: providerTypes.value[0]?.id || 'duckduckgo',
description: '',
- parameters: parametersWithDefaults(providerTypes.value[0]?.id || 'duckduckgo'),
+ parameters: {
+ extra_config: providerConfigDefaults(providerTypes.value[0]?.id || 'duckduckgo'),
+ },
is_default: providerEntities.value.length === 0
}
lastTestOk.value = null
- formErrors.value = []
showAddProviderDialog.value = true
}
@@ -531,13 +547,21 @@ const editProvider = (entity: WebSearchProviderEntity) => {
name: entity.name,
provider: entity.provider,
description: entity.description || '',
- // Never pre-fill the api_key — even the redacted placeholder from the
- // server is ignored so that "non-empty means user typed it" holds.
- parameters: parametersWithDefaults(entity.provider, { ...(entity.parameters || {}), api_key: undefined }),
+ parameters: {
+ // Never pre-fill the api_key — even the redacted placeholder from the
+ // server is ignored so that "non-empty means user typed it" holds.
+ api_key: '',
+ engine_id: entity.parameters?.engine_id || '',
+ base_url: entity.parameters?.base_url || '',
+ proxy_url: entity.parameters?.proxy_url || '',
+ extra_config: {
+ ...providerConfigDefaults(entity.provider),
+ ...(entity.parameters?.extra_config || {}),
+ },
+ },
is_default: entity.is_default || false,
}
lastTestOk.value = null
- formErrors.value = []
showAddProviderDialog.value = true
}
@@ -548,15 +572,27 @@ const saveProvider = async () => {
MessagePlugin.warning(typeof firstError === 'string' ? firstError : 'Please check the form fields')
return
}
- formErrors.value = parameterErrors()
- if (formErrors.value.length > 0) return
saving.value = true
try {
// Build the parameters payload. api_key only flows in on initial
// create — edit mode commits credentials through <CredentialResource>
// (a dedicated PUT /credentials call) before this save runs.
- const paramsOut = buildParametersPayload(providerForm.value.parameters, !editingProvider.value)
+ const paramsOut: WebSearchProviderEntity['parameters'] = {
+ engine_id: providerForm.value.parameters.engine_id,
+ base_url: providerForm.value.parameters.base_url,
+ proxy_url: providerForm.value.parameters.proxy_url,
+ }
+ const extraConfig = Object.fromEntries(
+ Object.entries(providerForm.value.parameters.extra_config || {})
+ .filter(([, value]) => value !== ''),
+ )
+ if (Object.keys(extraConfig).length > 0) {
+ paramsOut.extra_config = extraConfig
+ }
+ if (!editingProvider.value && providerForm.value.parameters.api_key) {
+ paramsOut.api_key = providerForm.value.parameters.api_key
+ }
const data: Partial<WebSearchProviderEntity> = {
name: providerForm.value.name.trim() || selectedProviderType.value?.name || providerForm.value.provider,
@@ -582,22 +618,6 @@ const saveProvider = async () => {
}
}
-// Drops empty values (an empty extra_config entry means "unset") and, in edit
-// mode, the api_key, which is committed through <CredentialResource>.
-const buildParametersPayload = (parameters: ConfigValue, includeSecrets: boolean) => {
- const out: Record<string, unknown> = {}
- for (const [key, value] of Object.entries(parameters)) {
- if (key === 'api_key' && !includeSecrets) continue
- if (isPlainObject(value)) {
- const nested = Object.fromEntries(Object.entries(value).filter(([, v]) => v !== '' && v !== undefined))
- if (Object.keys(nested).length > 0) out[key] = nested
- continue
- }
- if (value !== undefined && value !== '') out[key] = value
- }
- return out as WebSearchProviderEntity['parameters']
-}
-
const deleteProvider = (entity: WebSearchProviderEntity) => {
confirmDelete({
body: t('webSearchSettings.deleteConfirm'),
diff --git a/frontend/src/views/settings/pluginCenterState.test.ts b/frontend/src/views/settings/pluginCenterState.test.ts
deleted file mode 100644
index 86c4bc517..000000000
--- a/frontend/src/views/settings/pluginCenterState.test.ts
+++ /dev/null
@@ -1,76 +0,0 @@
-import assert from 'node:assert/strict'
-import test from 'node:test'
-
-import type { TenantPlugin } from '../../api/plugin'
-import { contributionSummary, filterPlugins, hasTenantConfig, pluginSkillChoices } from './pluginCenterState'
-
-const plugin = (id: string, name: string, contributes: TenantPlugin['manifest']['contributes'], zh?: string): TenantPlugin => ({
- enabled: true,
- manifest: {
- schemaVersion: 1, id, version: '1.0.0', publisher: { id: 'weknora' }, runtime: { type: 'builtin' },
- name: zh ? { default: name, 'zh-CN': zh } : { default: name },
- contributes,
- },
-})
-
-const feishu = plugin('weknora.feishu', 'Feishu / Lark', {
- connectors: [{ id: 'feishu', name: { default: 'Feishu' } }, { id: 'lark', name: { default: 'Lark' } }],
- imChannels: [{ id: 'feishu', name: { default: 'Feishu' } }],
-}, '飞书 / Lark')
-const bing = plugin('weknora.bing', 'Bing', { webSearch: [{ id: 'bing', name: { default: 'Bing' } }] })
-const openai = plugin('weknora.openai', 'OpenAI', { modelVendors: [{ id: 'openai', name: { default: 'OpenAI' } }] })
-
-test('contributionSummary counts per point in point order', () => {
- assert.deepEqual(contributionSummary(feishu.manifest), [
- { point: 'connectors', count: 2 },
- { point: 'imChannels', count: 1 },
- ])
-})
-
-test('filterPlugins matches names, IDs and contributions, and filters by point', () => {
- const all = [openai, feishu, bing]
- const names = (list: TenantPlugin[]) => list.map((p) => p.manifest.id)
- assert.deepEqual(names(filterPlugins(all, { query: '', point: '', locale: 'en-US' })),
- ['weknora.bing', 'weknora.feishu', 'weknora.openai'])
- assert.deepEqual(names(filterPlugins(all, { query: 'lark', point: '', locale: 'en-US' })), ['weknora.feishu'])
- assert.deepEqual(names(filterPlugins(all, { query: '飞书', point: '', locale: 'zh-CN' })), ['weknora.feishu'])
- assert.deepEqual(names(filterPlugins(all, { query: '', point: 'webSearch', locale: 'en-US' })), ['weknora.bing'])
- assert.deepEqual(names(filterPlugins(all, { query: 'bing', point: 'connectors', locale: 'en-US' })), [])
-})
-
-test('hasTenantConfig needs a tenant schema with fields', () => {
- const m = feishu.manifest
- assert.equal(hasTenantConfig(m), false)
- assert.equal(hasTenantConfig({ ...m, config: { tenantSchema: { type: 'object', properties: {} } } }), false)
- assert.equal(hasTenantConfig({
- ...m, config: { tenantSchema: { type: 'object', properties: { api_key: { type: 'string' } } } },
- }), true)
-})
-
-test('pluginSkillChoices lists skills of enabled plugins with their install source', () => {
- const kit = plugin('acme.kit', 'ACME Kit', {
- skills: [
- { id: 'triage', name: { default: 'Triage', 'zh-CN': '分诊' }, description: { default: 'Sort issues' } },
- { id: 'audit', name: { default: 'Audit' } },
- ],
- })
- const off = { ...plugin('acme.off', 'Off', { skills: [{ id: 'x', name: { default: 'X' } }] }), enabled: false }
- assert.deepEqual(pluginSkillChoices([kit, off, bing], 'en-US').map((c) => [c.source, c.name]), [
- ['plugin:acme.kit/audit', 'Audit'],
- ['plugin:acme.kit/triage', 'Triage'],
- ])
- const zh = pluginSkillChoices([kit], 'zh-CN').find((c) => c.source === 'plugin:acme.kit/triage')
- assert.equal(zh?.name, '分诊')
- assert.equal(zh?.description, 'Sort issues')
-})
-
-test('canConfigure covers webhooks, and webhookUrl prefers the server URL', async () => {
- const { canConfigure, hasWebhooks, webhookUrl } = await import('./pluginCenterState')
- const bare = plugin('acme.bare', 'Bare', {}).manifest
- assert.equal(canConfigure(bare), false)
- const hooks = plugin('acme.hooks', 'Hooks', { webhooks: [{ id: 'inbox', name: { default: 'Inbox' } }] }).manifest
- assert.equal(hasWebhooks(hooks), true)
- assert.equal(canConfigure(hooks), true)
- assert.equal(webhookUrl({ path: '/api/v1/plugin-callbacks/a/b/c' }, 'https://x.example'), 'https://x.example/api/v1/plugin-callbacks/a/b/c')
- assert.equal(webhookUrl({ path: '/p', url: 'https://public.example/p' }, 'https://x.example'), 'https://public.example/p')
-})
diff --git a/frontend/src/views/settings/pluginCenterState.ts b/frontend/src/views/settings/pluginCenterState.ts
deleted file mode 100644
index 3314465fd..000000000
--- a/frontend/src/views/settings/pluginCenterState.ts
+++ /dev/null
@@ -1,91 +0,0 @@
-// Pure helpers behind PluginCenter.vue, kept free of Vue so they run under
-// node:test.
-import type { ExtensionPoint, PluginManifest, TenantPlugin } from '../../api/plugin'
-import { localizedText } from '../../utils/localizedText'
-
-/** Extension points in the order the plugin center shows them. */
-export const EXTENSION_POINTS: ExtensionPoint[] = [
- 'modelVendors', 'connectors', 'imChannels', 'webSearch', 'tools', 'parsers', 'skills', 'mcpServers',
- 'pages', 'settingsSections', 'kbTabs', 'webhooks',
-]
-
-/** Whether the workspace can configure the plugin (it declares config.tenant). */
-export function hasTenantConfig(m: PluginManifest): boolean {
- return !!m.config?.tenantSchema?.properties && Object.keys(m.config.tenantSchema.properties).length > 0
-}
-
-/** Whether the plugin has webhooks, whose URLs admins set up elsewhere. */
-export function hasWebhooks(m: PluginManifest): boolean {
- return (m.contributes?.webhooks?.length ?? 0) > 0
-}
-
-/** Whether the plugin's configuration drawer has anything to show. */
-export function canConfigure(m: PluginManifest): boolean {
- return hasTenantConfig(m) || hasWebhooks(m)
-}
-
-/** A webhook's full URL: the server's, or this page's origin with the path. */
-export function webhookUrl(hook: { path: string; url?: string }, origin: string): string {
- return hook.url || `${origin}${hook.path}`
-}
-
-/**
- * Skills of enabled plugins, for the skill catalog's "from plugin" picker.
- * Each carries the install source the backend understands.
- */
-export function pluginSkillChoices(
- list: readonly TenantPlugin[],
- locale: string,
-): Array<{ source: string; name: string; plugin: string; description: string }> {
- const out: Array<{ source: string; name: string; plugin: string; description: string }> = []
- for (const p of list) {
- if (!p.enabled) continue
- for (const c of p.manifest.contributes.skills ?? []) {
- out.push({
- source: `plugin:${p.manifest.id}/${c.id}`,
- name: localizedText(c.name, locale) || c.id,
- plugin: localizedText(p.manifest.name, locale) || p.manifest.id,
- description: localizedText(c.description, locale),
- })
- }
- }
- return out.sort((a, b) => a.name.localeCompare(b.name, locale))
-}
-
-/** How many contributions a plugin makes at each point, in point order. */
-export function contributionSummary(m: PluginManifest): Array<{ point: ExtensionPoint; count: number }> {
- return EXTENSION_POINTS
- .map((point) => ({ point, count: m.contributes[point]?.length ?? 0 }))
- .filter((s) => s.count > 0)
-}
-
-export interface PluginFilter {
- query: string
- /** Only plugins contributing to this point; empty for all. */
- point: ExtensionPoint | ''
- locale: string
-}
-
-/**
- * Plugins matching a search and point filter, sorted by display name. The
- * query matches the plugin name, ID and the names of its contributions, so
- * searching "lark" finds the Feishu plugin.
- */
-export function filterPlugins(list: readonly TenantPlugin[], f: PluginFilter): TenantPlugin[] {
- const q = f.query.trim().toLowerCase()
- const matches = (p: TenantPlugin) => {
- const m = p.manifest
- if (f.point && !(m.contributes[f.point]?.length)) return false
- if (!q) return true
- const texts = [m.id, localizedText(m.name, f.locale), m.name.default]
- for (const point of EXTENSION_POINTS) {
- for (const c of m.contributes[point] ?? []) {
- texts.push(c.id, localizedText(c.name, f.locale))
- }
- }
- return texts.some((t) => t.toLowerCase().includes(q))
- }
- return list
- .filter(matches)
- .sort((a, b) => localizedText(a.manifest.name, f.locale).localeCompare(localizedText(b.manifest.name, f.locale), f.locale))
-}
diff --git a/frontend/src/views/settings/settingsNavigation.test.ts b/frontend/src/views/settings/settingsNavigation.test.ts
index e840c68de..2e9c04a7e 100644
--- a/frontend/src/views/settings/settingsNavigation.test.ts
+++ b/frontend/src/views/settings/settingsNavigation.test.ts
@@ -1,73 +1,46 @@
import assert from 'node:assert/strict'
+import { readFileSync } from 'node:fs'
import test from 'node:test'
-
+import { runInNewContext } from 'node:vm'
+import ts from 'typescript'
+import { computed } from 'vue'
import { INTEGRATION_PREVIEW_ITEMS } from '../../config/integrations'
import { integrationSectionKey } from '../../config/settingsRoute'
-import { BUILTIN_SETTINGS_GROUPS, BUILTIN_SETTINGS_SECTIONS } from '../../extensions/builtin/settingsCatalog'
-import {
- groupSections,
- sectionPermitted,
- type SettingsAccessContext,
- type SettingsSection,
-} from '../../extensions/settingsSections'
-
-// The sidebar is built from the builtin catalog through the same grouping
-// function Settings.vue uses.
-const groups = BUILTIN_SETTINGS_GROUPS.map((g) => ({ key: g.key, order: g.order, label: () => g.label }))
-const sections = BUILTIN_SETTINGS_SECTIONS.map((s) => ({ ...s, label: () => s.label }) as unknown as SettingsSection)
-function menu(visibleKeys: string[], group: string): string[] {
- const visible = sections.filter((s) => visibleKeys.includes(s.key))
- return groupSections(groups, visible).find((g) => g.key === group)?.items.map((s) => s.key) ?? []
+// Exercise the actual sidebar grouping without mounting the settings panels.
+const source = readFileSync(new URL('./Settings.vue', import.meta.url), 'utf8')
+const script = source.match(/<script setup lang="ts">([\s\S]*?)<\/script>/)![1]!
+const ast = ts.createSourceFile('Settings.ts', script, ts.ScriptTarget.Latest, true)
+const grouping = ast.statements.find((statement) =>
+ ts.isVariableStatement(statement) && statement.declarationList.declarations.some(
+ (declaration) => ts.isIdentifier(declaration.name) && declaration.name.text === 'navGroups',
+ ),
+)
+assert.ok(grouping, 'Settings must define sidebar groups')
+const compiled = ts.transpileModule(grouping.getText(ast), {
+ compilerOptions: { target: ts.ScriptTarget.ES2022 },
+}).outputText
+
+function integrationMenu(visibleKeys: string[]): string[] {
+ const groups = runInNewContext(`${compiled}\nnavGroups.value`, {
+ computed,
+ navItems: { value: visibleKeys.map((key) => ({ key })) },
+ t: (key: string) => key,
+ integrationSectionKey,
+ INTEGRATION_PREVIEW_ITEMS,
+ }) as Array<{ key: string; items: Array<{ key: string }> }>
+ const integrationGroup = groups.find((group) => group.key === 'integrations')
+ return Array.from(integrationGroup?.items ?? [], (item) => item.key)
}
const integrationKeys = INTEGRATION_PREVIEW_ITEMS.map((item) => integrationSectionKey(item.key))
test('settings sidebar includes every registered integration, including CLI, in navigation order', () => {
- assert.deepEqual(menu(['general', ...integrationKeys], 'integrations'), integrationKeys)
+ assert.deepEqual(integrationMenu(['general', ...integrationKeys]), integrationKeys)
})
test('settings sidebar preserves visibility filtering without hiding CLI', () => {
const visible = integrationKeys.filter((key) => key !== 'integration-api' && key !== 'integration-im')
- assert.deepEqual(menu(visible, 'integrations'), visible)
- assert.deepEqual(menu(['general'], 'integrations'), [])
-})
-
-test('groups keep their order and drop when empty', () => {
- const all = sections.map((s) => s.key)
- assert.deepEqual(
- groupSections(groups, sections.filter((s) => all.includes(s.key))).map((g) => g.key),
- [
- 'account', 'workspace', 'models_runtime', 'integrations', 'data_extensions', 'plugins',
- 'system_administration', 'platform',
- ],
- )
- assert.deepEqual(groupSections(groups, sections.filter((s) => s.key === 'system')).map((g) => g.key), ['platform'])
- assert.deepEqual(menu(all, 'account'), ['general', 'userprofile', 'mymemory', 'envvars'])
- assert.deepEqual(menu(all, 'data_extensions'), ['vectorstore', 'parser', 'storage', 'sandbox', 'websearch'])
-})
-
-test('section keys are unique', () => {
- const keys = BUILTIN_SETTINGS_SECTIONS.map((s) => s.key)
- assert.equal(new Set(keys).size, keys.length)
-})
-
-test('access follows the shared role tables', () => {
- const ctx = (role: 'viewer' | 'admin', isSystemAdmin = false): SettingsAccessContext => {
- const rank = { viewer: 0, contributor: 1, admin: 2, owner: 3 }
- return {
- isSystemAdmin,
- canAccessAllTenants: false,
- hasRole: (min) => rank[role] >= rank[min],
- isSupported: () => true,
- capabilities: {},
- }
- }
- const byKey = new Map(sections.map((s) => [s.key, s]))
- assert.equal(sectionPermitted(byKey.get('models')!, ctx('viewer')), true)
- assert.equal(sectionPermitted(byKey.get('websearch')!, ctx('viewer')), false)
- assert.equal(sectionPermitted(byKey.get('websearch')!, ctx('admin')), true)
- assert.equal(sectionPermitted(byKey.get('integration-api')!, ctx('admin')), false, 'API integration is owner-only')
- assert.equal(sectionPermitted(byKey.get('runtime-queues')!, ctx('admin')), false)
- assert.equal(sectionPermitted(byKey.get('runtime-queues')!, ctx('viewer', true)), true)
+ assert.deepEqual(integrationMenu(visible), visible)
+ assert.deepEqual(integrationMenu(['general']), [])
})
diff --git a/frontend/src/views/system/PluginManagement.vue b/frontend/src/views/system/PluginManagement.vue
deleted file mode 100644
index 34ec71e8b..000000000
--- a/frontend/src/views/system/PluginManagement.vue
+++ /dev/null
@@ -1,287 +0,0 @@
-<template>
- <div class="plugin-admin">
- <header class="section-header">
- <div class="section-header__row">
- <div>
- <h2>{{ t('pluginAdmin.title') }}</h2>
- <p class="section-description">{{ t('pluginAdmin.description') }}</p>
- </div>
- <t-button theme="primary" @click="installOpen = true">
- <template #icon><t-icon name="add" /></template>
- {{ t('pluginAdmin.installButton') }}
- </t-button>
- </div>
- </header>
-
- <div v-if="loading" class="plugin-admin__state"><t-loading size="small" /></div>
- <div v-else-if="plugins.length === 0" class="plugin-admin__state plugin-admin__state--empty">
- <t-empty :description="t('pluginAdmin.empty')" />
- <t-button variant="outline" @click="installOpen = true">
- <template #icon><t-icon name="add" /></template>
- {{ t('pluginAdmin.installButton') }}
- </t-button>
- </div>
- <div v-else class="plugin-list">
- <div
- v-for="p in plugins"
- :key="p.id"
- class="plugin-card"
- :class="{ 'plugin-card--off': p.desired_state === 'disabled' }"
- role="button"
- tabindex="0"
- @click="openDetail(p)"
- @keydown.enter="openDetail(p)"
- >
- <div class="plugin-card__badge">{{ initial(p) }}</div>
- <div class="plugin-card__body">
- <div class="plugin-card__title">
- <span class="plugin-card__name">{{ nameOf(p) }}</span>
- <t-tooltip :content="p.node?.error" :disabled="!p.node?.error">
- <t-tag size="small" variant="light" :theme="stateTheme(p)">
- {{ t(`pluginAdmin.state.${installedState(p)}`) }}
- </t-tag>
- </t-tooltip>
- </div>
- <div class="plugin-card__meta">{{ p.id }} · v{{ p.active_version }} · {{ runtimeLabel(p.runtime) }}</div>
- <div v-if="descriptionOf(p)" class="plugin-card__desc">{{ descriptionOf(p) }}</div>
- <div v-if="p.manifest" class="plugin-card__contribs">
- <span v-for="s in contributionSummary(p.manifest)" :key="s.point" class="plugin-card__contrib">
- {{ t(`pluginCenter.points.${s.point}`) }} × {{ s.count }}
- </span>
- </div>
- </div>
- <div class="plugin-card__actions" @click.stop @keydown.enter.stop>
- <t-tooltip :content="t('pluginAdmin.platformSwitch')">
- <t-switch
- :model-value="p.desired_state === 'enabled'"
- :loading="pending.has(p.id)"
- :disabled="pending.has(p.id)"
- @update:model-value="(v: boolean) => toggle(p, v)"
- />
- </t-tooltip>
- </div>
- </div>
- </div>
-
- <PluginInstallDrawer v-model:visible="installOpen" @installed="upsert" />
- <t-dialog
- v-model:visible="secretOpen"
- :header="t('pluginAdmin.secret.title')"
- :confirm-btn="{ content: t('pluginAdmin.secret.copy'), theme: 'primary' }"
- :cancel-btn="{ content: t('pluginAdmin.secret.done') }"
- :close-on-overlay-click="false"
- @confirm="copyWithToast(issuedSecret, 'pluginAdmin.secret.copied')"
- @closed="issuedSecret = ''"
- >
- <p>{{ t('pluginAdmin.secret.description') }}</p>
- <t-textarea :value="issuedSecret" readonly autosize />
- </t-dialog>
- <PluginDetailDrawer
- v-model:visible="detailOpen"
- :plugin="selected"
- @changed="upsert"
- @removed="remove"
- />
- </div>
-</template>
-
-<script setup lang="ts">
-import { computed, onMounted, ref } from 'vue'
-import { useI18n } from 'vue-i18n'
-import { MessagePlugin } from 'tdesign-vue-next'
-
-import { listInstalledPlugins, setInstalledPluginEnabled, type InstalledPlugin } from '@/api/system/plugins'
-import { copyWithToast } from '@/utils/clipboard'
-import { localizedText } from '@/utils/localizedText'
-
-import { contributionSummary } from '../settings/pluginCenterState'
-import PluginDetailDrawer from './plugins/PluginDetailDrawer.vue'
-import PluginInstallDrawer from './plugins/PluginInstallDrawer.vue'
-import { installedState } from './pluginManagementState'
-
-// Platform plugin management: what is installed, on which version, whether it
-// loads. Installing makes a plugin available to every workspace; each
-// workspace still turns it on in its own plugin center.
-const { t, te, locale } = useI18n()
-const runtimeLabel = (rt: string) => (te(`pluginAdmin.runtime.${rt}`) ? t(`pluginAdmin.runtime.${rt}`) : rt)
-
-const plugins = ref<InstalledPlugin[]>([])
-const loading = ref(false)
-const pending = ref(new Set<string>())
-const installOpen = ref(false)
-const detailOpen = ref(false)
-// A remote plugin's signing secret, shown once after it is issued.
-const secretOpen = ref(false)
-const issuedSecret = ref('')
-const selectedId = ref('')
-const selected = computed(() => plugins.value.find((p) => p.id === selectedId.value) ?? null)
-
-const nameOf = (p: InstalledPlugin) => (p.manifest ? localizedText(p.manifest.name, locale.value) : p.id)
-const descriptionOf = (p: InstalledPlugin) => (p.manifest ? localizedText(p.manifest.description, locale.value) : '')
-const initial = (p: InstalledPlugin) => (nameOf(p).trim().charAt(0) || '?').toUpperCase()
-
-function stateTheme(p: InstalledPlugin) {
- switch (installedState(p)) {
- case 'running':
- return 'success'
- case 'failed':
- return 'danger'
- case 'degraded':
- return 'warning'
- default:
- return 'default'
- }
-}
-
-async function load() {
- loading.value = true
- try {
- const res = await listInstalledPlugins()
- plugins.value = res.data || []
- } catch (e: any) {
- MessagePlugin.error(e?.message || t('pluginAdmin.loadFailed'))
- } finally {
- loading.value = false
- }
-}
-
-function upsert(received: InstalledPlugin) {
- const { issuedSecret: secret, ...p } = received
- if (secret) {
- issuedSecret.value = secret
- secretOpen.value = true
- }
- const i = plugins.value.findIndex((x) => x.id === p.id)
- if (i >= 0) plugins.value.splice(i, 1, p)
- else plugins.value = [...plugins.value, p].sort((a, b) => a.id.localeCompare(b.id))
-}
-
-function remove(id: string) {
- plugins.value = plugins.value.filter((p) => p.id !== id)
-}
-
-function openDetail(p: InstalledPlugin) {
- selectedId.value = p.id
- detailOpen.value = true
-}
-
-async function toggle(p: InstalledPlugin, enabled: boolean) {
- pending.value = new Set([...pending.value, p.id])
- try {
- const res = await setInstalledPluginEnabled(p.id, enabled)
- upsert(res.data)
- MessagePlugin.success(enabled ? t('pluginAdmin.enabledToast') : t('pluginAdmin.disabledToast'))
- } catch (e: any) {
- MessagePlugin.error(e?.message || t('pluginAdmin.saveFailed'))
- } finally {
- const next = new Set(pending.value)
- next.delete(p.id)
- pending.value = next
- }
-}
-
-onMounted(load)
-</script>
-
-<style lang="less" scoped>
-@import (reference) '@/components/css/provider-card.less';
-@import (reference) '@/components/css/settings-section.less';
-
-.plugin-admin {
- width: 100%;
-}
-
-.section-header {
- .settings-section-header();
-
- &__row {
- display: flex;
- align-items: flex-start;
- justify-content: space-between;
- gap: 16px;
- }
-}
-
-.plugin-admin__state {
- display: flex;
- justify-content: center;
- padding: 32px 0;
-
- &--empty {
- flex-direction: column;
- align-items: center;
- gap: 12px;
- }
-}
-
-.plugin-list {
- display: grid;
- grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
- gap: 12px;
-}
-
-.plugin-card {
- .provider-card();
- align-items: flex-start;
- cursor: pointer;
-
- &--off {
- .plugin-card__badge,
- .plugin-card__body {
- opacity: 0.55;
- }
- }
-}
-
-.plugin-card__badge {
- .provider-card-badge();
- .provider-card-badge-color(#0052d9);
-}
-
-.plugin-card__body {
- .provider-card-body();
- gap: 4px;
-}
-
-.plugin-card__title {
- display: flex;
- align-items: center;
- gap: 6px;
- min-width: 0;
-}
-
-.plugin-card__name {
- .provider-card-title();
-}
-
-.plugin-card__meta {
- font-family: var(--app-font-family-mono);
- font-size: var(--app-text-xs);
- color: var(--td-text-color-placeholder);
-}
-
-.plugin-card__desc {
- font-size: var(--app-text-sm);
- color: var(--td-text-color-secondary);
- line-height: 1.5;
-}
-
-.plugin-card__contribs {
- display: flex;
- flex-wrap: wrap;
- gap: 6px;
- margin-top: 4px;
-}
-
-.plugin-card__contrib {
- font-size: var(--app-text-xs);
- color: var(--td-text-color-secondary);
- background: var(--td-bg-color-secondarycontainer);
- border-radius: var(--app-radius-xs);
- padding: 1px 6px;
-}
-
-.plugin-card__actions {
- flex: none;
-}
-</style>
diff --git a/frontend/src/views/system/pluginManagementState.test.ts b/frontend/src/views/system/pluginManagementState.test.ts
deleted file mode 100644
index 548c5cb56..000000000
--- a/frontend/src/views/system/pluginManagementState.test.ts
+++ /dev/null
@@ -1,91 +0,0 @@
-import assert from 'node:assert/strict'
-import test from 'node:test'
-
-import type { PluginManifest } from '../../api/plugin'
-import type { InstalledPlugin, PluginVersion } from '../../api/system/plugins'
-import {
- compareVersions,
- contributionLines,
- formatBytes,
- hasSystemConfig,
- installedState,
- isPackageUrl,
- permissionLines,
- remoteHosts,
- remoteUrlReady,
- shortDigest,
- sortVersions,
-} from './pluginManagementState'
-
-const manifest: PluginManifest = {
- schemaVersion: 1,
- id: 'acme.search',
- version: '1.2.0',
- name: { default: 'ACME Search' },
- publisher: { id: 'acme' },
- runtime: { type: 'declarative' },
- permissions: { egress: ['*.acme.example'], events: ['knowledge.created'] },
- config: { systemSchema: { type: 'object', properties: { region: { type: 'string' } } } },
- contributes: {
- mcpServers: [{ id: 'search', name: { default: 'Search', 'zh-CN': '搜索' }, mcp: { url: 'https://mcp.acme.example/mcp' } }],
- skills: [{ id: 'triage', name: { default: 'Triage' }, path: 'skills/triage' }],
- },
-}
-
-test('formatBytes and shortDigest', () => {
- assert.equal(formatBytes(512), '512 B')
- assert.equal(formatBytes(1536), '1.5 KB')
- assert.equal(formatBytes(20 * 1024 * 1024), '20 MB')
- assert.equal(shortDigest('sha256:0123456789abcdef'), '0123456789ab')
-})
-
-test('contributionLines follow point order and carry the detail', () => {
- assert.deepEqual(contributionLines(manifest, 'zh-CN'), [
- { point: 'skills', id: 'acme.search/triage', name: 'Triage', detail: 'skills/triage' },
- { point: 'mcpServers', id: 'acme.search/search', name: '搜索', detail: 'https://mcp.acme.example/mcp' },
- ])
-})
-
-test('permissionLines and remoteHosts flatten what the review shows', () => {
- assert.deepEqual(permissionLines(manifest.permissions), [
- { kind: 'egress', value: '*.acme.example' },
- { kind: 'events', value: 'knowledge.created' },
- ])
- assert.deepEqual(permissionLines(undefined), [])
- assert.deepEqual(remoteHosts(manifest), ['mcp.acme.example'])
-})
-
-test('versions sort newest first by semver, not by string', () => {
- const v = (version: string) => ({ version } as PluginVersion)
- assert.deepEqual(sortVersions([v('1.9.0'), v('1.10.0'), v('1.2.3')]).map((x) => x.version), ['1.10.0', '1.9.0', '1.2.3'])
- assert.ok(compareVersions('2.0.0', '1.99.99') > 0)
- assert.equal(compareVersions('1.0.0+build', '1.0.0'), 0)
-})
-
-test('installedState reads desired state and the node report', () => {
- const base = { desired_state: 'enabled' } as InstalledPlugin
- assert.equal(installedState({ ...base, desired_state: 'disabled' }), 'disabled')
- assert.equal(installedState(base), 'pending')
- assert.equal(installedState({ ...base, node: { version: '1', state: 'ready', updatedAt: '' } }), 'running')
- assert.equal(installedState({ ...base, node: { version: '1', state: 'failed', updatedAt: '' } }), 'failed')
- assert.equal(installedState({ ...base, node: { version: '1', state: 'degraded', updatedAt: '' } }), 'degraded')
-})
-
-test('isPackageUrl and hasSystemConfig', () => {
- assert.equal(isPackageUrl('https://example.com/p.wkp'), true)
- assert.equal(isPackageUrl('ftp://example.com/p.wkp'), false)
- assert.equal(isPackageUrl('not a url'), false)
- assert.equal(hasSystemConfig(manifest), true)
- assert.equal(hasSystemConfig(undefined), false)
-})
-
-test('remoteUrlReady asks a new remote plugin for its service URL', () => {
- const remote = (change: string) => ({ manifest: { runtime: { type: 'remote' } }, change })
- assert.equal(remoteUrlReady(remote('install'), ''), false)
- assert.equal(remoteUrlReady(remote('install'), 'plugins.example.com'), false)
- assert.equal(remoteUrlReady(remote('install'), 'https://plugins.example.com'), true)
- assert.equal(remoteUrlReady(remote('upgrade'), ''), true)
- assert.equal(remoteUrlReady(remote('upgrade'), 'nope'), false)
- assert.equal(remoteUrlReady({ manifest: { runtime: { type: 'host' } }, change: 'install' }, ''), true)
- assert.equal(remoteUrlReady(null, ''), true)
-})
diff --git a/frontend/src/views/system/pluginManagementState.ts b/frontend/src/views/system/pluginManagementState.ts
deleted file mode 100644
index 34aa154a2..000000000
--- a/frontend/src/views/system/pluginManagementState.ts
+++ /dev/null
@@ -1,140 +0,0 @@
-// Pure helpers behind PluginManagement.vue, kept free of Vue so they run
-// under node:test.
-import type { ExtensionPoint, PluginManifest, PluginPermissions } from '../../api/plugin'
-import type { InstalledPlugin, PluginVersion } from '../../api/system/plugins'
-import { localizedText } from '../../utils/localizedText'
-import { EXTENSION_POINTS } from '../settings/pluginCenterState'
-
-/** Human-readable size: 1.2 MB. */
-export function formatBytes(n: number): string {
- if (!Number.isFinite(n) || n < 0) return '-'
- if (n < 1024) return `${n} B`
- const units = ['KB', 'MB', 'GB']
- let v = n / 1024
- let i = 0
- while (v >= 1024 && i < units.length - 1) {
- v /= 1024
- i++
- }
- return `${v.toFixed(v < 10 ? 1 : 0)} ${units[i]}`
-}
-
-/** The short form of a sha256 digest shown next to versions. */
-export function shortDigest(digest: string): string {
- const hex = digest.replace(/^sha256:/, '')
- return hex.slice(0, 12)
-}
-
-/** One line of what a package would add, for the install review. */
-export interface ContributionLine {
- point: ExtensionPoint
- id: string
- name: string
- /** The MCP server URL, or the package path of a skill / vendor file. */
- detail: string
-}
-
-export function contributionLines(m: PluginManifest, locale: string): ContributionLine[] {
- const out: ContributionLine[] = []
- for (const point of EXTENSION_POINTS) {
- for (const c of m.contributes[point] ?? []) {
- out.push({
- point,
- id: `${m.id}/${c.id}`,
- name: localizedText(c.name, locale) || c.id,
- detail: c.mcp?.url ?? c.path ?? '',
- })
- }
- }
- return out
-}
-
-/** Permission kinds a manifest can request, in review order. */
-export type PermissionKind = keyof PluginPermissions
-
-/** The permissions a package asks for, flattened for review. */
-export function permissionLines(p: PluginPermissions | undefined): Array<{ kind: PermissionKind; value: string }> {
- const out: Array<{ kind: PermissionKind; value: string }> = []
- for (const kind of ['egress', 'hostApi', 'events'] as PermissionKind[]) {
- for (const value of p?.[kind] ?? []) out.push({ kind, value })
- }
- return out
-}
-
-/** Hosts a package's MCP servers talk to, which the review calls out. */
-export function remoteHosts(m: PluginManifest): string[] {
- const hosts = new Set<string>()
- for (const c of m.contributes.mcpServers ?? []) {
- if (!c.mcp?.url) continue
- try {
- hosts.add(new URL(c.mcp.url).host)
- } catch {
- hosts.add(c.mcp.url)
- }
- }
- return [...hosts].sort()
-}
-
-function semverParts(v: string): number[] {
- return v.split(/[-+]/)[0].split('.').map((x) => Number.parseInt(x, 10) || 0)
-}
-
-/** Compares two semantic versions by major.minor.patch. */
-export function compareVersions(a: string, b: string): number {
- const pa = semverParts(a)
- const pb = semverParts(b)
- for (let i = 0; i < 3; i++) {
- if ((pa[i] ?? 0) !== (pb[i] ?? 0)) return (pa[i] ?? 0) - (pb[i] ?? 0)
- }
- return 0
-}
-
-/** Stored versions, newest first. */
-export function sortVersions(list: readonly PluginVersion[]): PluginVersion[] {
- return [...list].sort((a, b) => compareVersions(b.version, a.version))
-}
-
-/** Overall state of an installed plugin on the node that answered. */
-export type InstalledState = 'running' | 'degraded' | 'failed' | 'disabled' | 'pending'
-
-export function installedState(p: InstalledPlugin): InstalledState {
- if (p.desired_state === 'disabled') return 'disabled'
- if (!p.node) return 'pending'
- switch (p.node.state) {
- case 'ready':
- return 'running'
- case 'degraded':
- return 'degraded'
- default:
- return 'failed'
- }
-}
-
-/** The egress grant meaning any public host. */
-export const EGRESS_ANY_HOST = '*'
-
-/** Whether a string can be sent as a package URL. */
-/**
- * Whether the install drawer can go ahead with a remote plugin's service URL:
- * a new install needs one, an upgrade may keep the registered one.
- */
-export function remoteUrlReady(preview: { manifest: { runtime?: { type: string } }; change: string } | null, raw: string) {
- if (preview?.manifest.runtime?.type !== 'remote') return true
- if (raw.trim() === '') return preview.change !== 'install'
- return isPackageUrl(raw)
-}
-
-export function isPackageUrl(raw: string): boolean {
- try {
- const u = new URL(raw.trim())
- return (u.protocol === 'https:' || u.protocol === 'http:') && !!u.host
- } catch {
- return false
- }
-}
-
-/** Whether the plugin declares a platform-wide configuration. */
-export function hasSystemConfig(m: PluginManifest | undefined): boolean {
- const props = m?.config?.systemSchema?.properties
- return !!props && Object.keys(props).length > 0
-}
diff --git a/frontend/src/views/system/plugins/PluginDetailDrawer.vue b/frontend/src/views/system/plugins/PluginDetailDrawer.vue
deleted file mode 100644
index 85b3a2f78..000000000
--- a/frontend/src/views/system/plugins/PluginDetailDrawer.vue
+++ /dev/null
@@ -1,428 +0,0 @@
-<template>
- <SettingDrawer
- :visible="visible"
- :title="title"
- :description="plugin ? `${plugin.id} · v${plugin.active_version}` : ''"
- icon="app"
- width="640px"
- :confirm-text="t('common.save')"
- :confirm-loading="saving"
- :confirm-disabled="!systemSchema || saving"
- :hide-footer="!systemSchema"
- @update:visible="(v: boolean) => emit('update:visible', v)"
- @confirm="saveConfig"
- >
- <template v-if="plugin">
- <section class="setting-drawer__section">
- <h4 class="setting-drawer__section-title">{{ t('pluginAdmin.detail.overview') }}</h4>
- <dl class="facts">
- <dt>{{ t('pluginAdmin.publisher') }}</dt>
- <dd>{{ manifest?.publisher.name || manifest?.publisher.id }}</dd>
- <dt>{{ t('pluginAdmin.detail.runtime') }}</dt>
- <dd>{{ te(`pluginAdmin.runtime.${plugin.runtime}`) ? t(`pluginAdmin.runtime.${plugin.runtime}`) : plugin.runtime }}</dd>
- <dt>{{ t('pluginAdmin.detail.source') }}</dt>
- <dd>
- {{ t(`pluginAdmin.source.${plugin.source?.kind ?? 'upload'}`) }}
- <code v-if="plugin.source?.url" class="facts__code">{{ plugin.source.url }}</code>
- </dd>
- <template v-if="manifest?.homepage">
- <dt>{{ t('pluginAdmin.detail.homepage') }}</dt>
- <dd><a :href="manifest.homepage" target="_blank" rel="noopener noreferrer">{{ manifest.homepage }}</a></dd>
- </template>
- <template v-if="manifest?.license">
- <dt>{{ t('pluginAdmin.detail.license') }}</dt>
- <dd>{{ manifest.license }}</dd>
- </template>
- <template v-if="manifest?.engines?.weknora">
- <dt>{{ t('pluginAdmin.detail.engines') }}</dt>
- <dd><code class="facts__code">{{ manifest.engines.weknora }}</code></dd>
- </template>
- </dl>
- <ul v-if="contributions.length" class="line-list">
- <li v-for="c in contributions" :key="c.id">
- <span class="line-list__tag">{{ t(`pluginCenter.points.${c.point}`) }}</span>
- <span>{{ c.name }}</span>
- <code v-if="c.detail" class="line-list__muted">{{ c.detail }}</code>
- </li>
- </ul>
- </section>
-
- <section class="setting-drawer__section">
- <h4 class="setting-drawer__section-title">{{ t('pluginAdmin.detail.nodes') }}</h4>
- <p v-if="instanceError" class="form-desc">{{ instanceError }}</p>
- <p v-else-if="instances.length === 0" class="form-desc">{{ t('pluginAdmin.detail.noNodes') }}</p>
- <ul v-else class="line-list">
- <li v-for="n in instances" :key="n.node">
- <t-tag size="small" variant="light" :theme="n.state === 'ready' ? 'success' : 'danger'">
- {{ t(`pluginAdmin.nodeState.${n.state}`) }}
- </t-tag>
- <code>{{ n.node }}</code>
- <span class="line-list__muted">v{{ n.version }}</span>
- <span v-if="n.error" class="line-list__error">{{ n.error }}</span>
- </li>
- </ul>
- </section>
-
- <section v-if="plugin.runtime === 'remote'" class="setting-drawer__section">
- <h4 class="setting-drawer__section-title">{{ t('pluginAdmin.detail.remote') }}</h4>
- <div class="remote-row">
- <span class="remote-row__label">{{ t('pluginAdmin.detail.remoteUrl') }}</span>
- <template v-if="editingUrl">
- <t-input v-model="urlDraft" size="small" class="remote-row__input" :disabled="savingUrl" @enter="saveUrl" />
- <t-button size="small" theme="primary" :loading="savingUrl" :disabled="!isPackageUrl(urlDraft)" @click="saveUrl">
- {{ t('common.save') }}
- </t-button>
- <t-button size="small" variant="text" :disabled="savingUrl" @click="editingUrl = false">
- {{ t('common.cancel') }}
- </t-button>
- </template>
- <template v-else>
- <code class="remote-row__value">{{ plugin.remote_url }}</code>
- <t-button size="small" variant="text" theme="primary" @click="startEditUrl">
- {{ t('pluginAdmin.detail.editUrl') }}
- </t-button>
- </template>
- </div>
- <div class="danger-row">
- <span class="form-desc">{{ t('pluginAdmin.detail.rotateHint') }}</span>
- <t-popconfirm :content="t('pluginAdmin.detail.rotateConfirm')" @confirm="rotate">
- <t-button variant="outline" :loading="rotating">{{ t('pluginAdmin.detail.rotateSecret') }}</t-button>
- </t-popconfirm>
- </div>
- </section>
-
- <section class="setting-drawer__section">
- <h4 class="setting-drawer__section-title">{{ t('pluginAdmin.detail.versions') }}</h4>
- <ul class="line-list">
- <li v-for="v in versions" :key="v.version">
- <span class="version">v{{ v.version }}</span>
- <code class="line-list__muted">{{ shortDigest(v.digest) }}</code>
- <span class="line-list__muted">{{ formatBytes(v.size) }} · {{ formatDate(v.created_at) }}</span>
- <t-tag v-if="v.version === plugin.active_version" size="small" variant="light" theme="primary">
- {{ t('pluginAdmin.detail.active') }}
- </t-tag>
- <t-popconfirm
- v-else
- :content="t('pluginAdmin.detail.activateConfirm', { version: v.version })"
- @confirm="activate(v.version)"
- >
- <t-button size="small" variant="text" theme="primary" :loading="activating === v.version">
- {{ compareVersions(v.version, plugin.active_version) < 0 ? t('pluginAdmin.detail.rollback') : t('pluginAdmin.detail.activate') }}
- </t-button>
- </t-popconfirm>
- </li>
- </ul>
- </section>
-
- <section v-if="systemSchema" class="setting-drawer__section">
- <h4 class="setting-drawer__section-title">{{ t('pluginAdmin.detail.systemConfig') }}</h4>
- <p class="form-desc">{{ t('pluginAdmin.detail.systemConfigHint') }}</p>
- <SchemaForm v-model="configValues" :schema="systemSchema" :errors="configErrors" />
- </section>
-
- <section class="setting-drawer__section">
- <h4 class="setting-drawer__section-title">{{ t('pluginAdmin.detail.danger') }}</h4>
- <div class="danger-row">
- <span class="form-desc">{{ t('pluginAdmin.detail.uninstallHint') }}</span>
- <t-popconfirm theme="danger" :content="t('pluginAdmin.detail.uninstallConfirm')" @confirm="uninstall">
- <t-button theme="danger" variant="outline" :loading="uninstalling">
- {{ t('pluginAdmin.detail.uninstall') }}
- </t-button>
- </t-popconfirm>
- </div>
- </section>
- </template>
- </SettingDrawer>
-</template>
-
-<script setup lang="ts">
-import { computed, ref, watch } from 'vue'
-import { useI18n } from 'vue-i18n'
-import { MessagePlugin } from 'tdesign-vue-next'
-
-import SettingDrawer from '@/components/settings/SettingDrawer.vue'
-import SchemaForm from '@/components/schema-form/SchemaForm.vue'
-import { pluginFormSource } from '@/components/schema-form/pluginSource'
-import { provideSchemaFormSource } from '@/components/schema-form/source'
-import { validateConfig, type ConfigSchema, type ConfigValue, type FieldError } from '@/components/schema-form/schema'
-import { getPlugin, type PluginInstance } from '@/api/plugin'
-import {
- activatePluginVersion,
- getPluginSystemConfig,
- rotatePluginSecret,
- setPluginRemoteUrl,
- uninstallPlugin,
- updatePluginSystemConfig,
- type InstalledPlugin,
-} from '@/api/system/plugins'
-import { localizedText } from '@/utils/localizedText'
-
-import {
- compareVersions,
- contributionLines,
- formatBytes,
- hasSystemConfig,
- isPackageUrl,
- shortDigest,
- sortVersions,
-} from '../pluginManagementState'
-
-const props = defineProps<{ visible: boolean; plugin: InstalledPlugin | null }>()
-const emit = defineEmits<{
- 'update:visible': [value: boolean]
- changed: [plugin: InstalledPlugin]
- removed: [id: string]
-}>()
-
-const { t, te, locale } = useI18n()
-
-const manifest = computed(() => props.plugin?.manifest)
-const title = computed(() => (manifest.value ? localizedText(manifest.value.name, locale.value) : props.plugin?.id ?? ''))
-const contributions = computed(() => (manifest.value ? contributionLines(manifest.value, locale.value) : []))
-const versions = computed(() => sortVersions(props.plugin?.versions ?? []))
-
-const instances = ref<PluginInstance[]>([])
-const instanceError = ref('')
-const systemSchema = ref<ConfigSchema | null>(null)
-const configValues = ref<ConfigValue>({})
-
-// x-options / x-oauth fields of the platform configuration ask the plugin.
-provideSchemaFormSource(pluginFormSource({
- target: () => (props.plugin ? { pluginId: props.plugin.id, scope: 'system' } : undefined),
- values: () => configValues.value,
-}))
-const configErrors = ref<FieldError[]>([])
-const saving = ref(false)
-const activating = ref('')
-const uninstalling = ref(false)
-const editingUrl = ref(false)
-const urlDraft = ref('')
-const savingUrl = ref(false)
-const rotating = ref(false)
-
-const formatDate = (s: string) => (s ? new Date(s).toLocaleString(locale.value) : '')
-
-async function loadNodes(id: string) {
- instanceError.value = ''
- try {
- const res = await getPlugin(id)
- instances.value = res.data.instances ?? []
- instanceError.value = res.data.instanceError ?? ''
- } catch {
- // A plugin disabled platform-wide is not in the catalog; it runs nowhere.
- instances.value = []
- }
-}
-
-async function loadConfig(id: string) {
- systemSchema.value = null
- configErrors.value = []
- if (!hasSystemConfig(manifest.value)) return
- try {
- const res = await getPluginSystemConfig(id)
- systemSchema.value = res.data.schema
- configValues.value = res.data.values ?? {}
- } catch (e: any) {
- MessagePlugin.error(e?.message || t('pluginAdmin.detail.configLoadFailed'))
- }
-}
-
-watch(
- () => [props.visible, props.plugin?.id, props.plugin?.active_version, props.plugin?.desired_state] as const,
- ([visible, id]) => {
- if (!visible || !id) return
- editingUrl.value = false
- void loadNodes(id)
- void loadConfig(id)
- },
- { immediate: true },
-)
-
-async function saveConfig() {
- if (!props.plugin || !systemSchema.value) return
- configErrors.value = validateConfig(systemSchema.value, configValues.value, { skipSecrets: true })
- if (configErrors.value.length) return
- saving.value = true
- try {
- const res = await updatePluginSystemConfig(props.plugin.id, configValues.value)
- configValues.value = res.data.values ?? {}
- MessagePlugin.success(t('pluginAdmin.detail.configSaved'))
- } catch (e: any) {
- const details = e?.details ?? e?.error?.details
- if (Array.isArray(details)) configErrors.value = details
- MessagePlugin.error(e?.message || t('pluginAdmin.detail.configSaveFailed'))
- } finally {
- saving.value = false
- }
-}
-
-async function activate(version: string) {
- if (!props.plugin) return
- activating.value = version
- try {
- const res = await activatePluginVersion(props.plugin.id, version)
- emit('changed', res.data)
- MessagePlugin.success(t('pluginAdmin.detail.activated', { version }))
- } catch (e: any) {
- MessagePlugin.error(e?.message || t('pluginAdmin.detail.activateFailed'))
- } finally {
- activating.value = ''
- }
-}
-
-function startEditUrl() {
- urlDraft.value = props.plugin?.remote_url ?? ''
- editingUrl.value = true
-}
-
-async function saveUrl() {
- if (!props.plugin || !isPackageUrl(urlDraft.value)) return
- savingUrl.value = true
- try {
- const res = await setPluginRemoteUrl(props.plugin.id, urlDraft.value.trim())
- editingUrl.value = false
- emit('changed', res.data)
- MessagePlugin.success(t('pluginAdmin.detail.urlSaved'))
- } catch (e: any) {
- MessagePlugin.error(e?.message || t('pluginAdmin.detail.urlSaveFailed'))
- } finally {
- savingUrl.value = false
- }
-}
-
-// The new secret travels in the changed plugin; the page shows it once.
-async function rotate() {
- if (!props.plugin) return
- rotating.value = true
- try {
- const res = await rotatePluginSecret(props.plugin.id)
- emit('changed', res.data)
- } catch (e: any) {
- MessagePlugin.error(e?.message || t('pluginAdmin.detail.rotateFailed'))
- } finally {
- rotating.value = false
- }
-}
-
-async function uninstall() {
- if (!props.plugin) return
- const id = props.plugin.id
- uninstalling.value = true
- try {
- await uninstallPlugin(id)
- MessagePlugin.success(t('pluginAdmin.detail.uninstalled'))
- emit('removed', id)
- emit('update:visible', false)
- } catch (e: any) {
- MessagePlugin.error(e?.message || t('pluginAdmin.detail.uninstallFailed'))
- } finally {
- uninstalling.value = false
- }
-}
-</script>
-
-<style lang="less" scoped>
-.facts {
- display: grid;
- grid-template-columns: max-content 1fr;
- gap: 6px 16px;
- margin: 0;
- font-size: var(--app-text-sm);
-
- dt {
- color: var(--td-text-color-secondary);
- }
-
- dd {
- margin: 0;
- color: var(--td-text-color-primary);
- word-break: break-all;
- }
-
- &__code {
- font-size: var(--app-text-xs);
- color: var(--td-text-color-placeholder);
- margin-left: 6px;
- }
-}
-
-.line-list {
- margin: 0;
- padding: 0;
- list-style: none;
- display: flex;
- flex-direction: column;
- gap: 8px;
-
- li {
- display: flex;
- align-items: center;
- flex-wrap: wrap;
- gap: 8px;
- font-size: var(--app-text-sm);
- color: var(--td-text-color-primary);
- }
-
- &__tag {
- font-size: var(--app-text-xs);
- color: var(--td-text-color-secondary);
- background: var(--td-bg-color-secondarycontainer);
- border-radius: var(--app-radius-xs);
- padding: 1px 6px;
- }
-
- &__muted {
- font-size: var(--app-text-xs);
- color: var(--td-text-color-placeholder);
- word-break: break-all;
- }
-
- &__error {
- flex-basis: 100%;
- font-size: var(--app-text-xs);
- color: var(--td-error-color);
- }
-}
-
-.version {
- font-family: var(--app-font-family-mono);
- font-weight: 500;
-}
-
-.form-desc {
- margin: 0;
- font-size: var(--app-text-sm);
- line-height: 1.5;
- color: var(--td-text-color-placeholder);
-}
-
-.remote-row {
- display: flex;
- align-items: center;
- flex-wrap: wrap;
- gap: 8px;
- font-size: var(--app-text-sm);
-
- &__label {
- color: var(--td-text-color-secondary);
- }
-
- &__value {
- font-size: var(--app-text-xs);
- color: var(--td-text-color-primary);
- word-break: break-all;
- }
-
- &__input {
- flex: 1;
- min-width: 200px;
- }
-}
-
-.danger-row {
- display: flex;
- align-items: center;
- justify-content: space-between;
- gap: 16px;
-}
-</style>
diff --git a/frontend/src/views/system/plugins/PluginInstallDrawer.vue b/frontend/src/views/system/plugins/PluginInstallDrawer.vue
deleted file mode 100644
index adda8a542..000000000
--- a/frontend/src/views/system/plugins/PluginInstallDrawer.vue
+++ /dev/null
@@ -1,439 +0,0 @@
-<template>
- <SettingDrawer
- :visible="visible"
- :title="t('pluginAdmin.install.title')"
- :description="t('pluginAdmin.install.description')"
- icon="download"
- width="640px"
- :confirm-text="confirmText"
- :confirm-loading="busy"
- :confirm-disabled="!canConfirm"
- @update:visible="(v: boolean) => emit('update:visible', v)"
- @confirm="onConfirm"
- >
- <section class="setting-drawer__section">
- <h4 class="setting-drawer__section-title">{{ t('pluginAdmin.install.sourceSection') }}</h4>
- <div class="option-chips">
- <button
- v-for="m in (['upload', 'url'] as const)"
- :key="m"
- type="button"
- class="option-chip"
- :class="{ 'option-chip--active': mode === m }"
- :disabled="busy"
- @click="setMode(m)"
- >
- {{ t(`pluginAdmin.install.mode.${m}`) }}
- </button>
- </div>
-
- <div v-if="mode === 'upload'" class="form-item">
- <label class="form-label required">{{ t('pluginAdmin.install.fileLabel') }}</label>
- <div class="package-picker">
- <t-button variant="outline" :disabled="busy" @click="fileInput?.click()">
- <template #icon><t-icon name="upload" /></template>
- {{ t('pluginAdmin.install.chooseFile') }}
- </t-button>
- <span class="package-picker__name">{{ file?.name ?? t('pluginAdmin.install.noFile') }}</span>
- <input ref="fileInput" type="file" accept=".wkp,.zip" hidden @change="onFile" />
- </div>
- <p class="form-desc">{{ t('pluginAdmin.install.fileHint') }}</p>
- </div>
-
- <div v-else class="form-item">
- <label class="form-label required">{{ t('pluginAdmin.install.urlLabel') }}</label>
- <t-input
- v-model="url"
- :disabled="busy"
- placeholder="https://example.com/acme-search-1.0.0.wkp"
- @change="preview = null"
- />
- <p class="form-desc">{{ t('pluginAdmin.install.urlHint') }}</p>
- </div>
- </section>
-
- <section v-if="preview" class="setting-drawer__section">
- <h4 class="setting-drawer__section-title">{{ t('pluginAdmin.install.reviewSection') }}</h4>
- <div class="review-head">
- <div class="review-head__badge">{{ initial }}</div>
- <div class="review-head__text">
- <div class="review-head__name">
- {{ localizedText(preview.manifest.name, locale) }}
- <t-tag size="small" variant="light" :theme="changeTheme">
- {{ t(`pluginAdmin.change.${preview.change}`, { from: preview.installedVersion ?? '' }) }}
- </t-tag>
- </div>
- <div class="review-head__meta">
- {{ preview.manifest.id }} · v{{ preview.manifest.version }} · {{ formatBytes(preview.size) }}
- </div>
- <div class="review-head__meta">
- {{ t('pluginAdmin.publisher') }}: {{ preview.manifest.publisher.name || preview.manifest.publisher.id }}
- </div>
- <p v-if="description" class="review-head__desc">{{ description }}</p>
- </div>
- </div>
-
- <div class="review-block">
- <div class="review-block__title">{{ t('pluginAdmin.contributions') }}</div>
- <ul class="review-list">
- <li v-for="c in contributions" :key="c.id">
- <span class="review-list__point">{{ t(`pluginCenter.points.${c.point}`) }}</span>
- <span class="review-list__name">{{ c.name }}</span>
- <code v-if="c.detail" class="review-list__detail">{{ c.detail }}</code>
- </li>
- </ul>
- </div>
-
- <div class="review-block">
- <div class="review-block__title">{{ t('pluginAdmin.permissions') }}</div>
- <p v-if="permissions.length === 0 && hosts.length === 0" class="form-desc">
- {{ t('pluginAdmin.install.noPermissions') }}
- </p>
- <ul v-else class="review-list">
- <li v-for="h in hosts" :key="`host:${h}`">
- <span class="review-list__point">{{ t('pluginAdmin.permission.remote') }}</span>
- <code class="review-list__detail">{{ h }}</code>
- </li>
- <li v-for="p in permissions" :key="`${p.kind}:${p.value}`">
- <span class="review-list__point">{{ t(`pluginAdmin.permission.${p.kind}`) }}</span>
- <strong v-if="p.kind === 'egress' && p.value === EGRESS_ANY_HOST" class="review-list__warn">
- {{ t('pluginAdmin.permission.anyHost') }}
- </strong>
- <code v-else class="review-list__detail">{{ p.value }}</code>
- </li>
- </ul>
- </div>
-
- <div v-if="isRemote" class="form-item">
- <label class="form-label" :class="{ required: preview.change === 'install' }">
- {{ t('pluginAdmin.install.remoteUrlLabel') }}
- </label>
- <t-input v-model="remoteUrl" :disabled="busy" placeholder="https://plugins.example.com/acme-search" />
- <p class="form-desc">
- {{ t('pluginAdmin.install.remoteUrlHint') }}
- <template v-if="preview.change !== 'install'">{{ t('pluginAdmin.install.remoteUrlKeep') }}</template>
- </p>
- </div>
-
- <t-alert
- v-if="needsConfig"
- theme="info"
- class="review-alert"
- :message="t('pluginAdmin.install.configNotice')"
- />
- <t-alert theme="warning" class="review-alert" :message="t('pluginAdmin.install.tenantNotice')" />
- <p class="form-desc">{{ t('pluginAdmin.install.digest') }}: <code>{{ preview.digest }}</code></p>
- </section>
- </SettingDrawer>
-</template>
-
-<script setup lang="ts">
-import { computed, ref, watch } from 'vue'
-import { useI18n } from 'vue-i18n'
-import { MessagePlugin } from 'tdesign-vue-next'
-
-import SettingDrawer from '@/components/settings/SettingDrawer.vue'
-import {
- inspectPluginPackage,
- installPluginPackage,
- type InstalledPlugin,
- type PackageSource,
- type PluginPreview,
-} from '@/api/system/plugins'
-import { localizedText } from '@/utils/localizedText'
-
-import {
- EGRESS_ANY_HOST,
- contributionLines,
- formatBytes,
- hasSystemConfig,
- isPackageUrl,
- permissionLines,
- remoteHosts,
- remoteUrlReady,
-} from '../pluginManagementState'
-import { hasTenantConfig } from '../../settings/pluginCenterState'
-
-// Installing is two steps on purpose: inspect shows what the package would
-// add and reach, and install sends the reviewed digest so the server refuses
-// a package that changed in between (a URL that now serves something else).
-const props = defineProps<{ visible: boolean }>()
-const emit = defineEmits<{
- 'update:visible': [value: boolean]
- installed: [plugin: InstalledPlugin]
-}>()
-
-const { t, locale } = useI18n()
-
-const mode = ref<'upload' | 'url'>('upload')
-const file = ref<File | null>(null)
-const url = ref('')
-const remoteUrl = ref('')
-const fileInput = ref<HTMLInputElement | null>(null)
-const preview = ref<PluginPreview | null>(null)
-const busy = ref(false)
-
-watch(
- () => props.visible,
- (v) => {
- if (!v) return
- mode.value = 'upload'
- file.value = null
- url.value = ''
- remoteUrl.value = ''
- preview.value = null
- },
-)
-
-function setMode(m: 'upload' | 'url') {
- mode.value = m
- preview.value = null
-}
-
-function onFile(e: Event) {
- const input = e.target as HTMLInputElement
- file.value = input.files?.[0] ?? null
- preview.value = null
- input.value = ''
- if (file.value) void inspect()
-}
-
-const source = computed<PackageSource | null>(() => {
- if (mode.value === 'upload') return file.value ? { file: file.value } : null
- return isPackageUrl(url.value) ? { url: url.value.trim() } : null
-})
-
-const isRemote = computed(() => preview.value?.manifest.runtime?.type === 'remote')
-const canConfirm = computed(
- () => !!source.value && !busy.value && (!preview.value || remoteUrlReady(preview.value, remoteUrl.value)),
-)
-const confirmText = computed(() =>
- preview.value ? t(`pluginAdmin.install.confirm.${preview.value.change}`) : t('pluginAdmin.install.inspect'),
-)
-
-const contributions = computed(() => (preview.value ? contributionLines(preview.value.manifest, locale.value) : []))
-const permissions = computed(() => permissionLines(preview.value?.manifest.permissions))
-const hosts = computed(() => (preview.value ? remoteHosts(preview.value.manifest) : []))
-const description = computed(() => (preview.value ? localizedText(preview.value.manifest.description, locale.value) : ''))
-const initial = computed(() =>
- (preview.value ? localizedText(preview.value.manifest.name, locale.value) : '?').trim().charAt(0).toUpperCase(),
-)
-const needsConfig = computed(
- () => !!preview.value && (hasSystemConfig(preview.value.manifest) || hasTenantConfig(preview.value.manifest)),
-)
-const changeTheme = computed(() => {
- switch (preview.value?.change) {
- case 'upgrade':
- return 'success'
- case 'downgrade':
- return 'warning'
- default:
- return 'primary'
- }
-})
-
-async function inspect() {
- if (!source.value) return
- busy.value = true
- try {
- const res = await inspectPluginPackage(source.value)
- preview.value = res.data
- } catch (e: any) {
- preview.value = null
- MessagePlugin.error(e?.message || t('pluginAdmin.install.inspectFailed'))
- } finally {
- busy.value = false
- }
-}
-
-async function onConfirm() {
- if (!preview.value) {
- await inspect()
- return
- }
- if (!source.value) return
- busy.value = true
- try {
- const res = await installPluginPackage(
- source.value,
- preview.value.digest,
- isRemote.value ? remoteUrl.value.trim() : undefined,
- )
- MessagePlugin.success(t('pluginAdmin.install.done', { name: localizedText(preview.value.manifest.name, locale.value) }))
- emit('installed', res.data)
- emit('update:visible', false)
- } catch (e: any) {
- MessagePlugin.error(e?.message || t('pluginAdmin.install.failed'))
- } finally {
- busy.value = false
- }
-}
-</script>
-
-<style lang="less" scoped>
-@import (reference) '@/components/css/provider-card.less';
-
-.option-chips {
- display: inline-flex;
- align-self: flex-start;
- gap: 4px;
- padding: 3px;
- border-radius: var(--app-radius-md);
- background: var(--td-bg-color-secondarycontainer);
-}
-
-.option-chip {
- border: none;
- background: transparent;
- color: var(--td-text-color-secondary);
- font: inherit;
- font-size: var(--app-text-sm);
- padding: 5px 12px;
- border-radius: var(--app-radius-sm);
- cursor: pointer;
-
- &--active {
- background: var(--td-bg-color-container);
- color: var(--td-brand-color);
- font-weight: 500;
- box-shadow: var(--td-shadow-1);
- }
-
- &:disabled {
- cursor: not-allowed;
- }
-}
-
-.form-label {
- display: block;
- margin-bottom: 6px;
- font-size: var(--app-text-md);
- font-weight: 500;
- color: var(--td-text-color-primary);
-
- &.required::before {
- content: '*';
- color: var(--td-error-color);
- margin-right: 4px;
- }
-}
-
-.form-desc {
- margin: 4px 0 0;
- font-size: var(--app-text-sm);
- line-height: 1.5;
- color: var(--td-text-color-placeholder);
- word-break: break-all;
-}
-
-.package-picker {
- display: flex;
- align-items: center;
- gap: 10px;
- min-width: 0;
-
- &__name {
- font-size: var(--app-text-sm);
- color: var(--td-text-color-secondary);
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
-}
-
-.review-head {
- display: flex;
- gap: 12px;
-
- &__badge {
- .provider-card-badge();
- .provider-card-badge-color(#0052d9);
- }
-
- &__text {
- display: flex;
- flex-direction: column;
- gap: 2px;
- min-width: 0;
- }
-
- &__name {
- display: flex;
- align-items: center;
- gap: 8px;
- font-size: var(--app-text-lg);
- font-weight: 600;
- color: var(--td-text-color-primary);
- }
-
- &__meta {
- font-size: var(--app-text-xs);
- font-family: var(--app-font-family-mono);
- color: var(--td-text-color-placeholder);
- }
-
- &__desc {
- margin: 4px 0 0;
- font-size: var(--app-text-sm);
- color: var(--td-text-color-secondary);
- }
-}
-
-.review-block {
- display: flex;
- flex-direction: column;
- gap: 6px;
-
- &__title {
- font-size: var(--app-text-sm);
- font-weight: 500;
- color: var(--td-text-color-primary);
- }
-}
-
-.review-list {
- margin: 0;
- padding: 0;
- list-style: none;
- display: flex;
- flex-direction: column;
- gap: 6px;
-
- li {
- display: flex;
- align-items: baseline;
- flex-wrap: wrap;
- gap: 8px;
- font-size: var(--app-text-sm);
- }
-
- &__point {
- flex: none;
- font-size: var(--app-text-xs);
- color: var(--td-text-color-secondary);
- background: var(--td-bg-color-secondarycontainer);
- border-radius: var(--app-radius-xs);
- padding: 1px 6px;
- }
-
- &__name {
- color: var(--td-text-color-primary);
- }
-
- &__detail {
- font-size: var(--app-text-xs);
- color: var(--td-text-color-placeholder);
- word-break: break-all;
- }
-
- &__warn {
- font-size: var(--app-text-sm);
- font-weight: 500;
- color: var(--td-warning-color);
- }
-}
-
-.review-alert {
- margin-top: 4px;
-}
-</style>
diff --git a/frontend/src/views/toolbox/Toolbox.vue b/frontend/src/views/toolbox/Toolbox.vue
index 22da9c494..de69fed48 100644
--- a/frontend/src/views/toolbox/Toolbox.vue
+++ b/frontend/src/views/toolbox/Toolbox.vue
@@ -13,8 +13,7 @@
</t-button>
</div>
<p class="toolbox-subtitle" style="--wails-draggable: drag">
- <template v-if="selectedPage">{{ pageDescription(selectedPage) }}</template>
- <template v-else>{{ t(selectedItem?.description ?? 'toolbox.description') }}</template>
+ {{ t(selectedItem?.description ?? 'toolbox.description') }}
<t-tooltip v-if="selectedItem && 'help' in selectedItem" :content="t(selectedItem.help)" placement="bottom"
overlay-class-name="skill-settings__help-tooltip">
<t-icon name="help-circle" class="toolbox-help" :aria-label="t(selectedItem.help)" />
@@ -22,7 +21,7 @@
</p>
</header>
- <template v-if="visibleItems.length || pluginPages.pages.length">
+ <template v-if="visibleItems.length">
<div class="toolbox-tabs" role="tablist" :aria-label="t('toolbox.title')">
<button v-for="item in visibleItems" :key="item.key" type="button" role="tab" class="toolbox-tab"
:aria-selected="selectedItem?.key === item.key" @click="select(item.key)">
@@ -36,18 +35,9 @@
<span v-else-if="item.key !== 'browserconnection' && counts[item.key] !== undefined"
class="toolbox-tab-count">{{ counts[item.key] }}</span>
</button>
- <button v-for="page in pluginPages.pages" :key="page.key" type="button" role="tab" class="toolbox-tab"
- :aria-selected="selectedPage?.key === page.key" @click="selectPage(page.key)">
- <t-icon name="app" size="18px" />
- <span>{{ localizedText(page.name, locale) }}</span>
- </button>
</div>
- <section v-if="selectedPage" class="toolbox-main toolbox-main--plugin" role="tabpanel">
- <PluginFrame :page="selectedPage" fill @close="select(visibleItems[0]?.key)" />
- </section>
-
- <section v-else-if="selectedItem" class="toolbox-main" role="tabpanel">
+ <section v-if="selectedItem" class="toolbox-main" role="tabpanel">
<div class="toolbox-panel">
<SkillSettings v-if="selectedItem.key === 'skills'" ref="panel" :key="sandboxId"
:initial-sandbox-id="sandboxId" @count="counts.skills = $event" />
@@ -79,22 +69,13 @@ import {
import ResourceIcon from '@/components/icons/ResourceIcon.vue'
import BrowserIcon from '@/components/icons/BrowserIcon.vue'
import EmptyState from '@/components/EmptyState.vue'
-import PluginFrame from '@/extensions/pluginFrame/PluginFrame.vue'
-import { findPage, type PluginPage } from '@/extensions/pluginFrame/pluginPages'
-import { usePluginPagesStore } from '@/stores/pluginPages'
-import { localizedText } from '@/utils/localizedText'
import SkillSettings from '@/views/settings/SkillSettings.vue'
import McpSettings from '@/views/settings/McpSettings.vue'
import BrowserConnectionSettings from '@/views/settings/BrowserConnectionSettings.vue'
const route = useRoute()
const router = useRouter()
-const { t, locale } = useI18n()
-const pluginPages = usePluginPagesStore()
-const pluginPagesLoaded = ref(false)
-void pluginPages.ensure().catch(() => {}).finally(() => {
- pluginPagesLoaded.value = true
-})
+const { t } = useI18n()
const authStore = useAuthStore()
const browserConnection = useBrowserConnectionStore()
const capabilities = useDeploymentCapabilitiesStore()
@@ -109,14 +90,6 @@ const visibleItems = computed(() => TOOLBOX_ITEMS.filter((item) => canAccessTool
isSupported: (capability) => capabilities.isSupported(capability),
})))
const selectedItem = computed(() => visibleItems.value.find((item) => item.key === requestedSection.value))
-// Plugin pages are tabs too, keyed "plugin:<plugin>/<page>".
-const selectedPage = computed(() => findPage(pluginPages.pages, requestedSection.value))
-const pageDescription = (page: PluginPage) =>
- page.description ? localizedText(page.description, locale.value) : t('pluginPages.fromPlugin', { id: page.pluginId })
-const selectPage = (key: string) => {
- if (key === requestedSection.value) return
- void router.replace({ path: `/platform/toolbox/${encodeURIComponent(key)}` })
-}
const browserStatus = computed(() => {
if (!browserConnection.loaded || !browserConnection.enabled) return ''
@@ -124,20 +97,16 @@ const browserStatus = computed(() => {
return browserConnection.device ? 'offline' : 'notPaired'
})
-const select = (section: ToolboxSection | undefined) => {
- if (!section || section === requestedSection.value) return
+const select = (section: ToolboxSection) => {
+ if (section === requestedSection.value) return
void router.replace(toolboxLocation(section))
}
// The bare /toolbox URL, and tools lost to a role or workspace switch, land on
// the first tool the user can still open.
-watch([selectedItem, selectedPage, visibleItems], () => {
+watch([selectedItem, visibleItems], () => {
const fallback = visibleItems.value[0]
- // A plugin page URL waits for the pages to load before falling back.
- const pendingPage = requestedSection.value.startsWith('plugin:') && !pluginPagesLoaded.value
- if (!selectedItem.value && !selectedPage.value && !pendingPage && fallback) {
- void router.replace(toolboxLocation(fallback.key))
- }
+ if (!selectedItem.value && fallback) void router.replace(toolboxLocation(fallback.key))
}, { immediate: true })
// Tab badges for tools that are not open; the open panel keeps its own badge current.
@@ -352,13 +321,6 @@ watch(() => visibleItems.value.map((item) => item.key), (keys, previous = []) =>
padding: var(--app-space-5) 0 var(--app-space-8);
}
-// A plugin page fills the tab and scrolls itself.
-.toolbox-main--plugin {
- display: flex;
- overflow: hidden;
- padding-bottom: var(--app-space-5);
-}
-
.toolbox-panel {
// Panels are shared with the settings dialog; here the tab and page header
// already carry their title and description.
diff --git a/go.mod b/go.mod
index 1eec7edc6..20ae07003 100644
--- a/go.mod
+++ b/go.mod
@@ -100,7 +100,6 @@ require (
github.com/JohannesKaufmann/dom v0.2.0 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
- github.com/Tencent/WeKnora/pluginsdk v0.0.0-00010101000000-000000000000
github.com/andybalholm/brotli v1.2.0 // indirect
github.com/andybalholm/cascadia v1.3.3 // indirect
github.com/apache/arrow-go/v18 v18.5.1 // indirect
@@ -360,5 +359,3 @@ replace go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelg
// The anydoc Go bindings are not published yet (firecrawl/anydoc#30 is still
// open), so they are vendored. Drop this replace once upstream tags go/vX.Y.Z.
replace github.com/firecrawl/anydoc/go => ./third_party/anydoc-go
-
-replace github.com/Tencent/WeKnora/pluginsdk => ./pluginsdk
diff --git a/helm/templates/app.yaml b/helm/templates/app.yaml
index 40af2e66a..9b79ca83d 100644
--- a/helm/templates/app.yaml
+++ b/helm/templates/app.yaml
@@ -126,15 +126,6 @@ spec:
# whatever Engine API this process can reach (often host root).
- name: WEKNORA_SANDBOX_DOCKER_ENABLED
value: {{ .Values.app.env.WEKNORA_SANDBOX_DOCKER_ENABLED | default "false" | quote }}
- {{- if .Values.pluginHost.enabled }}
- # Host plugins of these kinds still run in the app pods; the rest
- # go to the plugin host pods. Plugins there reach the Host API
- # through the app Service.
- - name: WEKNORA_PLUGIN_EMBEDDED_KINDS
- value: {{ .Values.pluginHost.appKinds | quote }}
- - name: WEKNORA_PLUGIN_HOST_API_URL
- value: "http://app:{{ .Values.app.service.port }}"
- {{- end }}
{{- if .Values.neo4j.enabled }}
# Neo4j configuration (for GraphRAG)
# NEO4J_ENABLE 是知识图谱的唯一开关(Go 代码认它,不认 ENABLE_GRAPH_RAG)。
diff --git a/helm/templates/plugin-host.yaml b/helm/templates/plugin-host.yaml
deleted file mode 100644
index 279795d35..000000000
--- a/helm/templates/plugin-host.yaml
+++ /dev/null
@@ -1,157 +0,0 @@
-{{/*
-Copyright 2025 Tencent
-SPDX-License-Identifier: MIT
-
-WeKnora standalone plugin host (WeKnora plugin-host): runs host plugins for the
-app pods. Hosts announce themselves in Redis and app pods call them directly
-by pod IP through a gateway signed with a key derived from SYSTEM_AES_KEY, so
-no Service is needed.
-*/}}
-{{- if .Values.pluginHost.enabled }}
-apiVersion: apps/v1
-kind: Deployment
-metadata:
- name: {{ include "weknora.fullname" . }}-plugin-host
- namespace: {{ .Release.Namespace }}
- labels:
- {{- include "weknora.componentLabels" (dict "component" "plugin-host" "context" .) | nindent 4 }}
-spec:
- replicas: {{ .Values.pluginHost.replicaCount }}
- selector:
- matchLabels:
- {{- include "weknora.componentSelectorLabels" (dict "component" "plugin-host" "context" .) | nindent 6 }}
- template:
- metadata:
- labels:
- {{- include "weknora.componentSelectorLabels" (dict "component" "plugin-host" "context" .) | nindent 8 }}
- spec:
- {{- include "weknora.imagePullSecrets" . | nindent 6 }}
- serviceAccountName: {{ include "weknora.serviceAccountName" . }}
- # The host withdraws from Redis, then lets calls in flight finish.
- terminationGracePeriodSeconds: 75
- {{- with .Values.pluginHost.podSecurityContext | default .Values.global.podSecurityContext }}
- securityContext:
- {{- toYaml . | nindent 8 }}
- {{- end }}
- containers:
- - name: plugin-host
- image: {{ include "weknora.app.image" . }}
- imagePullPolicy: {{ .Values.app.image.pullPolicy }}
- args: ["./WeKnora", "plugin-host"]
- {{- with .Values.pluginHost.securityContext }}
- securityContext:
- {{- toYaml . | nindent 12 }}
- {{- end }}
- ports:
- - containerPort: 8081
- name: gateway
- protocol: TCP
- env:
- - name: POD_IP
- valueFrom:
- fieldRef:
- fieldPath: status.podIP
- - name: WEKNORA_PLUGIN_HOST_ADDR
- value: ":8081"
- - name: WEKNORA_PLUGIN_HOST_URL
- value: "http://$(POD_IP):8081"
- - name: WEKNORA_PLUGIN_HOST_KINDS
- value: {{ .Values.pluginHost.kinds | quote }}
- # Plugins granted Host API scopes call the app through its Service.
- - name: WEKNORA_PLUGIN_HOST_API_URL
- value: "http://app:{{ .Values.app.service.port }}"
- - name: TZ
- value: {{ .Values.app.env.TZ | quote }}
- # The same database, Redis, storage and secrets as the app.
- - name: DB_DRIVER
- value: "postgres"
- - name: DB_HOST
- value: "postgres"
- - name: DB_PORT
- value: "5432"
- - name: DB_USER
- valueFrom:
- secretKeyRef:
- name: {{ include "weknora.secretName" . }}
- key: DB_USER
- - name: DB_PASSWORD
- valueFrom:
- secretKeyRef:
- name: {{ include "weknora.secretName" . }}
- key: DB_PASSWORD
- - name: DB_NAME
- valueFrom:
- secretKeyRef:
- name: {{ include "weknora.secretName" . }}
- key: DB_NAME
- - name: REDIS_ADDR
- value: "redis:6379"
- - name: REDIS_USERNAME
- valueFrom:
- secretKeyRef:
- name: {{ include "weknora.secretName" . }}
- key: REDIS_USERNAME
- optional: true
- - name: REDIS_PASSWORD
- valueFrom:
- secretKeyRef:
- name: {{ include "weknora.secretName" . }}
- key: REDIS_PASSWORD
- - name: REDIS_DB
- value: "0"
- - name: JWT_SECRET
- valueFrom:
- secretKeyRef:
- name: {{ include "weknora.secretName" . }}
- key: JWT_SECRET
- - name: SYSTEM_AES_KEY
- valueFrom:
- secretKeyRef:
- name: {{ include "weknora.secretName" . }}
- key: SYSTEM_AES_KEY
- - name: STORAGE_TYPE
- value: {{ .Values.app.env.STORAGE_TYPE | quote }}
- - name: LOCAL_STORAGE_BASE_DIR
- value: {{ .Values.app.env.LOCAL_STORAGE_BASE_DIR | quote }}
- {{- with .Values.pluginHost.extraEnv }}
- {{- toYaml . | nindent 12 }}
- {{- end }}
- volumeMounts:
- # Plugin packages live in the app's storage; with STORAGE_TYPE=local
- # that is this volume, so it must be ReadWriteMany.
- - name: data-files
- mountPath: /data/files
- resources:
- {{- toYaml .Values.pluginHost.resources | nindent 12 }}
- livenessProbe:
- httpGet:
- path: /healthz
- port: gateway
- initialDelaySeconds: 20
- periodSeconds: 10
- readinessProbe:
- httpGet:
- path: /healthz
- port: gateway
- periodSeconds: 5
- volumes:
- - name: data-files
- {{- if .Values.dataFiles.persistence.enabled }}
- persistentVolumeClaim:
- claimName: {{ .Values.dataFiles.persistence.existingClaim | default (printf "%s-data-files" (include "weknora.fullname" .)) }}
- {{- else }}
- emptyDir: {}
- {{- end }}
- {{- with .Values.pluginHost.nodeSelector }}
- nodeSelector:
- {{- toYaml . | nindent 8 }}
- {{- end }}
- {{- with .Values.pluginHost.affinity }}
- affinity:
- {{- toYaml . | nindent 8 }}
- {{- end }}
- {{- with .Values.pluginHost.tolerations }}
- tolerations:
- {{- toYaml . | nindent 8 }}
- {{- end }}
-{{- end }}
diff --git a/helm/values.yaml b/helm/values.yaml
index f14873505..19c1b8534 100644
--- a/helm/values.yaml
+++ b/helm/values.yaml
@@ -146,45 +146,6 @@ app:
# -- Affinity rules
affinity: {}
-# -----------------------------------------------------------------------------
-# Plugin host (WeKnora plugin-host)
-# -----------------------------------------------------------------------------
-# Runs installed host plugins in their own pods instead of inside every app
-# pod. Uses the app image, database, Redis and storage. With
-# dataFiles.persistence and STORAGE_TYPE=local the data-files PVC must be
-# ReadWriteMany, since plugin packages live there.
-pluginHost:
- # -- Enable standalone plugin hosts
- enabled: false
-
- # -- Number of plugin host pods; each runs every plugin of its kinds
- replicaCount: 1
-
- # -- Kinds of host plugin the plugin hosts run (binary, python)
- kinds: "binary,python"
-
- # -- Kinds the app pods keep running themselves ("none" = all to plugin hosts)
- appKinds: "none"
-
- resources:
- requests:
- cpu: 100m
- memory: 256Mi
- limits:
- cpu: "2"
- memory: 2Gi
-
- podSecurityContext: {}
- securityContext:
- allowPrivilegeEscalation: false
-
- # -- Additional environment variables (e.g. storage credentials)
- extraEnv: []
-
- nodeSelector: {}
- tolerations: []
- affinity: {}
-
# -----------------------------------------------------------------------------
# Frontend (Web UI)
# -----------------------------------------------------------------------------
diff --git a/internal/agent/tools/capabilities.go b/internal/agent/tools/capabilities.go
index f3944ac21..273924883 100644
--- a/internal/agent/tools/capabilities.go
+++ b/internal/agent/tools/capabilities.go
@@ -2,8 +2,7 @@
//
// This file is the Go mirror of `frontend/src/utils/tool-capabilities.ts`.
// They MUST be kept in sync: whenever you add a tool to the registry with
-// specific KB requirements, update both maps. capabilities_frontend_test.go
-// fails when they differ.
+// specific KB requirements, update both maps.
//
// Why duplicate it? The frontend uses it to gray out tools and filter KBs
// in the agent editor and `@` mention menu; the backend uses it as the
diff --git a/internal/agent/tools/capabilities_frontend_test.go b/internal/agent/tools/capabilities_frontend_test.go
deleted file mode 100644
index 6f7b318a2..000000000
--- a/internal/agent/tools/capabilities_frontend_test.go
+++ /dev/null
@@ -1,84 +0,0 @@
-package tools
-
-import (
- "os"
- "regexp"
- "slices"
- "sort"
- "strings"
- "testing"
-)
-
-// frontendCapabilitiesFile is the frontend copy of ToolCapabilityRequirements.
-// The agent editor evaluates it synchronously in many places, so it stays a
-// copy; this test keeps the two from drifting.
-const frontendCapabilitiesFile = "../../../frontend/src/utils/tool-capabilities.ts"
-
-func parseFrontendRequirements(t *testing.T) map[string]ToolRequirement {
- t.Helper()
- data, err := os.ReadFile(frontendCapabilitiesFile)
- if err != nil {
- t.Fatalf("read %s: %v", frontendCapabilitiesFile, err)
- }
- src := string(data)
- start := strings.Index(src, "TOOL_CAPABILITY_REQUIREMENTS")
- end := strings.Index(src[start:], "\n};")
- if start < 0 || end < 0 {
- t.Fatal("TOOL_CAPABILITY_REQUIREMENTS not found in the frontend file")
- }
- body := src[start : start+end]
-
- entry := regexp.MustCompile(`(?m)^\s*([a-z_]+):\s*\{([^}]*)\},?`)
- list := func(fields, key string) []KBCapability {
- m := regexp.MustCompile(key + `:\s*\[([^\]]*)\]`).FindStringSubmatch(fields)
- if m == nil {
- return nil
- }
- var out []KBCapability
- for _, v := range regexp.MustCompile(`'([a-z]+)'`).FindAllStringSubmatch(m[1], -1) {
- out = append(out, KBCapability(v[1]))
- }
- return out
- }
- out := map[string]ToolRequirement{}
- for _, m := range entry.FindAllStringSubmatch(body, -1) {
- fields := m[2]
- out[m[1]] = ToolRequirement{
- AnyOf: list(fields, "anyOf"),
- AllOf: list(fields, "allOf"),
- ConsumesFiles: strings.Contains(fields, "consumesFiles: true"),
- Auxiliary: strings.Contains(fields, "auxiliary: true"),
- }
- }
- return out
-}
-
-func sortedCaps(c []KBCapability) []KBCapability {
- out := slices.Clone(c)
- sort.Slice(out, func(i, j int) bool { return out[i] < out[j] })
- return out
-}
-
-func TestFrontendToolCapabilitiesMatchBackend(t *testing.T) {
- front := parseFrontendRequirements(t)
- if len(front) == 0 {
- t.Fatal("parsed no frontend requirements")
- }
- for name, back := range ToolCapabilityRequirements {
- f, ok := front[name]
- if !ok {
- t.Errorf("%s: missing from %s", name, frontendCapabilitiesFile)
- continue
- }
- if !slices.Equal(sortedCaps(f.AnyOf), sortedCaps(back.AnyOf)) ||
- !slices.Equal(sortedCaps(f.AllOf), sortedCaps(back.AllOf)) ||
- f.ConsumesFiles != back.ConsumesFiles || f.Auxiliary != back.Auxiliary {
- t.Errorf("%s: frontend %+v, backend %+v", name, f, back)
- }
- }
- for name := range front {
- if _, ok := ToolCapabilityRequirements[name]; !ok {
- t.Errorf("%s: only in the frontend map", name)
- }
- }
-}
diff --git a/internal/agent/tools/mcp_tool.go b/internal/agent/tools/mcp_tool.go
index a3e552f5e..918a9765b 100644
--- a/internal/agent/tools/mcp_tool.go
+++ b/internal/agent/tools/mcp_tool.go
@@ -328,7 +328,6 @@ func (t *MCPTool) Execute(ctx context.Context, args json.RawMessage) (*types.Too
// double storage in memory and accidental exposure in logs/SSE.
data := make(map[string]interface{})
data["content_items"] = redactImageData(result.Content)
- addPluginToolView(data, t.service, t.mcpTool.Name, result.StructuredContent)
logger.GetLogger(ctx).Infof("MCP tool executed successfully: %s (images: %d)", t.mcpTool.Name, len(images))
diff --git a/internal/agent/tools/mcp_tool_view.go b/internal/agent/tools/mcp_tool_view.go
deleted file mode 100644
index 36118da9a..000000000
--- a/internal/agent/tools/mcp_tool_view.go
+++ /dev/null
@@ -1,38 +0,0 @@
-package tools
-
-import (
- "encoding/json"
-
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-// PluginToolViewDisplayType marks a tool result the chat renders with the
-// result view its plugin declared (toolViews in plugin.yaml).
-const PluginToolViewDisplayType = "plugin_tool_view"
-
-// maxPluginViewBytes caps the structured result kept for a view; larger
-// results show as plain output.
-const maxPluginViewBytes = 256 << 10
-
-// addPluginToolView attaches a plugin's result view and the structured
-// result it shows. The model still reads the text content.
-func addPluginToolView(data map[string]any, svc *types.MCPService, tool string, structured any) {
- if svc == nil || structured == nil {
- return
- }
- view, ok := svc.ToolViews[tool]
- if !ok {
- return
- }
- raw, err := json.Marshal(structured)
- if err != nil || len(raw) > maxPluginViewBytes {
- return
- }
- data["display_type"] = PluginToolViewDisplayType
- data["plugin_view"] = view
- data["plugin_id"] = svc.PluginID
- data["plugin_version"] = svc.PluginVersion
- data["mcp_server"] = svc.PluginServer
- data["mcp_tool"] = tool
- data["structured"] = json.RawMessage(raw)
-}
diff --git a/internal/agent/tools/mcp_tool_view_test.go b/internal/agent/tools/mcp_tool_view_test.go
deleted file mode 100644
index eb9258f87..000000000
--- a/internal/agent/tools/mcp_tool_view_test.go
+++ /dev/null
@@ -1,40 +0,0 @@
-package tools
-
-import (
- "encoding/json"
- "strings"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-func TestPluginToolViewTagsStructuredResults(t *testing.T) {
- svc := &types.MCPService{
- PluginID: "acme.jira", PluginVersion: "1.0.0",
- ToolViews: map[string]json.RawMessage{"search": json.RawMessage(`{"view":"table"}`)},
- }
- data := map[string]any{}
- addPluginToolView(data, svc, "search", map[string]any{"issues": []any{}})
- if data["display_type"] != PluginToolViewDisplayType || data["plugin_id"] != "acme.jira" ||
- data["mcp_tool"] != "search" || string(data["structured"].(json.RawMessage)) != `{"issues":[]}` {
- t.Fatalf("data = %v", data)
- }
- if !ShouldOmitRawToolOutput("", data) {
- t.Fatal("a rendered view replaces the raw output in the transcript")
- }
- for name, tc := range map[string]struct {
- tool string
- structured any
- }{
- "no view for the tool": {"other", map[string]any{}},
- "no structured content": {"search", nil},
- "too large": {"search", strings.Repeat("x", maxPluginViewBytes)},
- } {
- data := map[string]any{}
- addPluginToolView(data, svc, tc.tool, tc.structured)
- if len(data) != 0 {
- t.Errorf("%s: %v", name, data)
- }
- }
- addPluginToolView(data, nil, "search", map[string]any{})
-}
diff --git a/internal/application/repository/plugin.go b/internal/application/repository/plugin.go
deleted file mode 100644
index 2fe89cb9e..000000000
--- a/internal/application/repository/plugin.go
+++ /dev/null
@@ -1,90 +0,0 @@
-package repository
-
-import (
- "context"
- "errors"
-
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
- "gorm.io/gorm"
- "gorm.io/gorm/clause"
-)
-
-// pluginRepository stores installed plugins (plugins, plugin_versions;
-// migration 000116). Both tables stay small, so nothing paginates.
-type pluginRepository struct {
- db *gorm.DB
-}
-
-// NewPluginRepository wires the repository into the container.
-func NewPluginRepository(db *gorm.DB) interfaces.PluginRepository {
- return &pluginRepository{db: db}
-}
-
-func (r *pluginRepository) ListPlugins(ctx context.Context) ([]types.InstalledPlugin, error) {
- var rows []types.InstalledPlugin
- err := r.db.WithContext(ctx).Order("id ASC").Find(&rows).Error
- return rows, err
-}
-
-func (r *pluginRepository) GetPlugin(ctx context.Context, id string) (*types.InstalledPlugin, error) {
- var p types.InstalledPlugin
- err := r.db.WithContext(ctx).Where("id = ?", id).First(&p).Error
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return nil, nil
- }
- if err != nil {
- return nil, err
- }
- return &p, nil
-}
-
-// SavePlugin inserts or updates the row keyed by id.
-// pluginUpdateColumns are what saving an installed plugin overwrites: every
-// column but its identity and creation.
-var pluginUpdateColumns = []string{
- "owner_tenant_id", "source", "active_version", "desired_state", "runtime",
- "granted_perms", "system_config", "remote_url", "remote_secret", "updated_at",
-}
-
-func (r *pluginRepository) SavePlugin(ctx context.Context, p *types.InstalledPlugin) error {
- return r.db.WithContext(ctx).Clauses(clause.OnConflict{
- Columns: []clause.Column{{Name: "id"}},
- DoUpdates: clause.AssignmentColumns(pluginUpdateColumns),
- }).Create(p).Error
-}
-
-func (r *pluginRepository) DeletePlugin(ctx context.Context, id string) error {
- return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
- if err := tx.Where("plugin_id = ?", id).Delete(&types.PluginVersion{}).Error; err != nil {
- return err
- }
- return tx.Where("id = ?", id).Delete(&types.InstalledPlugin{}).Error
- })
-}
-
-func (r *pluginRepository) ListVersions(ctx context.Context, pluginID string) ([]types.PluginVersion, error) {
- var rows []types.PluginVersion
- err := r.db.WithContext(ctx).Where("plugin_id = ?", pluginID).Order("created_at DESC").Find(&rows).Error
- return rows, err
-}
-
-func (r *pluginRepository) GetVersion(ctx context.Context, pluginID, version string) (*types.PluginVersion, error) {
- var v types.PluginVersion
- err := r.db.WithContext(ctx).Where("plugin_id = ? AND version = ?", pluginID, version).First(&v).Error
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return nil, nil
- }
- if err != nil {
- return nil, err
- }
- return &v, nil
-}
-
-// SaveVersion inserts or updates the row keyed by (plugin_id, version).
-func (r *pluginRepository) SaveVersion(ctx context.Context, v *types.PluginVersion) error {
- return r.db.WithContext(ctx).Clauses(clause.OnConflict{
- Columns: []clause.Column{{Name: "plugin_id"}, {Name: "version"}},
- DoUpdates: clause.AssignmentColumns([]string{"digest", "manifest", "package_uri", "size", "created_by"}),
- }).Create(v).Error
-}
diff --git a/internal/application/repository/plugin_kv.go b/internal/application/repository/plugin_kv.go
deleted file mode 100644
index 064d72734..000000000
--- a/internal/application/repository/plugin_kv.go
+++ /dev/null
@@ -1,90 +0,0 @@
-package repository
-
-import (
- "context"
- "strings"
- "time"
-
- "gorm.io/gorm"
- "gorm.io/gorm/clause"
-
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
-)
-
-// pluginKVRepository stores plugins' Host API key-value data (plugin_kv,
-// migration 000117).
-type pluginKVRepository struct {
- db *gorm.DB
-}
-
-// NewPluginKVRepository wires the repository into the container.
-func NewPluginKVRepository(db *gorm.DB) interfaces.PluginKVRepository {
- return &pluginKVRepository{db: db}
-}
-
-// live restricts a query to entries that have not expired.
-func live(q *gorm.DB, now time.Time) *gorm.DB {
- return q.Where("expires_at IS NULL OR expires_at > ?", now)
-}
-
-func (r *pluginKVRepository) scope(ctx context.Context, pluginID string, tenantID uint64) *gorm.DB {
- return r.db.WithContext(ctx).Model(&types.PluginKV{}).
- Where("plugin_id = ? AND tenant_id = ?", pluginID, tenantID)
-}
-
-func (r *pluginKVRepository) Get(
- ctx context.Context, pluginID string, tenantID uint64, key string,
-) (*types.PluginKV, error) {
- var rows []types.PluginKV
- err := live(r.scope(ctx, pluginID, tenantID).Where("key = ?", key), time.Now()).Limit(1).Find(&rows).Error
- if err != nil || len(rows) == 0 {
- return nil, err
- }
- return &rows[0], nil
-}
-
-func (r *pluginKVRepository) Put(ctx context.Context, e *types.PluginKV) error {
- return r.db.WithContext(ctx).Clauses(clause.OnConflict{
- Columns: []clause.Column{{Name: "plugin_id"}, {Name: "tenant_id"}, {Name: "key"}},
- DoUpdates: clause.AssignmentColumns([]string{"value", "expires_at", "updated_at"}),
- }).Create(e).Error
-}
-
-func (r *pluginKVRepository) Delete(ctx context.Context, pluginID string, tenantID uint64, key string) (bool, error) {
- res := r.db.WithContext(ctx).
- Where("plugin_id = ? AND tenant_id = ? AND key = ?", pluginID, tenantID, key).
- Delete(&types.PluginKV{})
- return res.RowsAffected > 0, res.Error
-}
-
-// escapeLike makes a prefix match literally in LIKE.
-func escapeLike(s string) string {
- return strings.NewReplacer(`\`, `\\`, `%`, `\%`, `_`, `\_`).Replace(s)
-}
-
-func (r *pluginKVRepository) List(
- ctx context.Context, pluginID string, tenantID uint64, prefix, after string, limit int,
-) ([]types.PluginKV, error) {
- q := live(r.scope(ctx, pluginID, tenantID), time.Now())
- if prefix != "" {
- q = q.Where(`key LIKE ? ESCAPE '\'`, escapeLike(prefix)+"%")
- }
- if after != "" {
- q = q.Where("key > ?", after)
- }
- var rows []types.PluginKV
- err := q.Order("key ASC").Limit(limit).Find(&rows).Error
- return rows, err
-}
-
-func (r *pluginKVRepository) Count(ctx context.Context, pluginID string, tenantID uint64) (int64, error) {
- var n int64
- err := live(r.scope(ctx, pluginID, tenantID), time.Now()).Count(&n).Error
- return n, err
-}
-
-func (r *pluginKVRepository) DeleteExpired(ctx context.Context, now time.Time) (int64, error) {
- res := r.db.WithContext(ctx).Where("expires_at IS NOT NULL AND expires_at <= ?", now).Delete(&types.PluginKV{})
- return res.RowsAffected, res.Error
-}
diff --git a/internal/application/repository/plugin_oauth.go b/internal/application/repository/plugin_oauth.go
deleted file mode 100644
index f7c2236c3..000000000
--- a/internal/application/repository/plugin_oauth.go
+++ /dev/null
@@ -1,49 +0,0 @@
-package repository
-
-import (
- "context"
- "errors"
- "time"
-
- "gorm.io/gorm"
-
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
-)
-
-type pluginOAuthRepository struct{ db *gorm.DB }
-
-// NewPluginOAuthRepository creates the plugin OAuth connection repository.
-func NewPluginOAuthRepository(db *gorm.DB) interfaces.PluginOAuthRepository {
- return &pluginOAuthRepository{db: db}
-}
-
-func (r *pluginOAuthRepository) Create(ctx context.Context, c *types.PluginOAuthConnection) error {
- return r.db.WithContext(ctx).Create(c).Error
-}
-
-func (r *pluginOAuthRepository) Get(
- ctx context.Context, pluginID string, tenantID uint64, id string,
-) (*types.PluginOAuthConnection, error) {
- var c types.PluginOAuthConnection
- err := r.db.WithContext(ctx).
- Where("id = ? AND plugin_id = ? AND tenant_id = ?", id, pluginID, tenantID).
- First(&c).
- Error
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return nil, nil
- }
- if err != nil {
- return nil, err
- }
- return &c, nil
-}
-
-func (r *pluginOAuthRepository) UpdateToken(
- ctx context.Context, c *types.PluginOAuthConnection, prev time.Time,
-) (bool, error) {
- res := r.db.WithContext(ctx).Model(&types.PluginOAuthConnection{}).
- Where("id = ? AND updated_at = ?", c.ID, prev).
- Updates(map[string]any{"token": c.Token, "expires_at": c.ExpiresAt, "updated_at": c.UpdatedAt})
- return res.RowsAffected == 1, res.Error
-}
diff --git a/internal/application/repository/plugin_tenant_setting.go b/internal/application/repository/plugin_tenant_setting.go
deleted file mode 100644
index 316d8a4a2..000000000
--- a/internal/application/repository/plugin_tenant_setting.go
+++ /dev/null
@@ -1,57 +0,0 @@
-package repository
-
-import (
- "context"
-
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
- "gorm.io/gorm"
- "gorm.io/gorm/clause"
-)
-
-// pluginTenantSettingRepository stores per-tenant plugin switches
-// (plugin_tenant_settings, migration 000115). A tenant has at most one row
-// per plugin, and only for plugins it changed, so List does not paginate.
-type pluginTenantSettingRepository struct {
- db *gorm.DB
-}
-
-// NewPluginTenantSettingRepository wires the repository into the container.
-func NewPluginTenantSettingRepository(db *gorm.DB) interfaces.PluginTenantSettingRepository {
- return &pluginTenantSettingRepository{db: db}
-}
-
-func (r *pluginTenantSettingRepository) List(
- ctx context.Context, tenantID uint64,
-) ([]types.PluginTenantSetting, error) {
- var rows []types.PluginTenantSetting
- err := r.db.WithContext(ctx).Where("tenant_id = ?", tenantID).Order("plugin_id ASC").Find(&rows).Error
- return rows, err
-}
-
-func (r *pluginTenantSettingRepository) Get(
- ctx context.Context, tenantID uint64, pluginID string,
-) (*types.PluginTenantSetting, error) {
- var rows []types.PluginTenantSetting
- err := r.db.WithContext(ctx).Where("tenant_id = ? AND plugin_id = ?", tenantID, pluginID).Limit(1).Find(&rows).Error
- if err != nil || len(rows) == 0 {
- return nil, err
- }
- return &rows[0], nil
-}
-
-// Upsert writes the row keyed by (tenant_id, plugin_id). On conflict only
-// the given columns change, so the switch and the configuration can be
-// saved independently.
-func (r *pluginTenantSettingRepository) Upsert(
- ctx context.Context, s *types.PluginTenantSetting, columns ...string,
-) error {
- if len(columns) == 0 {
- columns = []string{"enabled", "config"}
- }
- columns = append(columns, "updated_by", "updated_at")
- return r.db.WithContext(ctx).Clauses(clause.OnConflict{
- Columns: []clause.Column{{Name: "tenant_id"}, {Name: "plugin_id"}},
- DoUpdates: clause.AssignmentColumns(columns),
- }).Create(s).Error
-}
diff --git a/internal/application/repository/plugin_test.go b/internal/application/repository/plugin_test.go
deleted file mode 100644
index 83e43d107..000000000
--- a/internal/application/repository/plugin_test.go
+++ /dev/null
@@ -1,163 +0,0 @@
-package repository
-
-import (
- "context"
- "sync"
- "testing"
- "time"
-
- "github.com/google/uuid"
- "github.com/stretchr/testify/require"
- "gorm.io/driver/sqlite"
- "gorm.io/gorm"
- "gorm.io/gorm/schema"
-
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-func TestPluginRepository(t *testing.T) {
- db, err := gorm.Open(sqlite.Open("file:"+uuid.NewString()+"?mode=memory&cache=shared"), &gorm.Config{})
- require.NoError(t, err)
- require.NoError(t, db.AutoMigrate(&types.InstalledPlugin{}, &types.PluginVersion{}))
- repo := NewPluginRepository(db)
- ctx := context.Background()
-
- got, err := repo.GetPlugin(ctx, "acme.kit")
- require.NoError(t, err)
- require.Nil(t, got, "missing plugins read as nil")
-
- now := time.Now()
- for _, v := range []string{"1.0.0", "1.1.0"} {
- require.NoError(t, repo.SaveVersion(ctx, &types.PluginVersion{
- PluginID: "acme.kit", Version: v, Digest: "sha256:" + v, Manifest: types.JSON(`{}`),
- PackageURI: "local://p-" + v, CreatedAt: now,
- }))
- now = now.Add(time.Second)
- }
- p := &types.InstalledPlugin{
- ID: "acme.kit", ActiveVersion: "1.0.0", DesiredState: types.PluginStateEnabled, Runtime: "declarative",
- Source: types.JSON(`{"kind":"upload"}`), GrantedPerms: types.JSON(`{}`),
- }
- require.NoError(t, repo.SavePlugin(ctx, p))
- p.ActiveVersion = "1.1.0"
- p.DesiredState = types.PluginStateDisabled
- p.RemoteURL, p.RemoteSecret = "https://plugins.example.com", "enc:v1:x"
- require.NoError(t, repo.SavePlugin(ctx, p), "save is an upsert")
-
- got, err = repo.GetPlugin(ctx, "acme.kit")
- require.NoError(t, err)
- require.Equal(t, "1.1.0", got.ActiveVersion)
- require.Equal(t, types.PluginStateDisabled, got.DesiredState)
- require.Equal(t, "https://plugins.example.com", got.RemoteURL)
- require.Equal(t, "enc:v1:x", got.RemoteSecret)
-
- versions, err := repo.ListVersions(ctx, "acme.kit")
- require.NoError(t, err)
- require.Len(t, versions, 2)
- require.Equal(t, "1.1.0", versions[0].Version, "newest first")
-
- v, err := repo.GetVersion(ctx, "acme.kit", "1.0.0")
- require.NoError(t, err)
- require.Equal(t, "local://p-1.0.0", v.PackageURI)
-
- require.NoError(t, repo.DeletePlugin(ctx, "acme.kit"))
- all, err := repo.ListPlugins(ctx)
- require.NoError(t, err)
- require.Empty(t, all)
- versions, err = repo.ListVersions(ctx, "acme.kit")
- require.NoError(t, err)
- require.Empty(t, versions, "deleting a plugin removes its versions")
-}
-
-func TestPluginTenantSettingUpsertColumns(t *testing.T) {
- db, err := gorm.Open(sqlite.Open("file:"+uuid.NewString()+"?mode=memory&cache=shared"), &gorm.Config{})
- require.NoError(t, err)
- require.NoError(t, db.AutoMigrate(&types.PluginTenantSetting{}))
- repo := NewPluginTenantSettingRepository(db)
- ctx := context.Background()
-
- got, err := repo.Get(ctx, 1, "acme.kit")
- require.NoError(t, err)
- require.Nil(t, got)
-
- require.NoError(t, repo.Upsert(ctx, &types.PluginTenantSetting{
- TenantID: 1, PluginID: "acme.kit", Enabled: true, UpdatedAt: time.Now(),
- }, "enabled"))
- require.NoError(t, repo.Upsert(ctx, &types.PluginTenantSetting{
- TenantID: 1, PluginID: "acme.kit", Enabled: false, Config: types.JSON(`{"region":"eu"}`), UpdatedAt: time.Now(),
- }, "config"))
- got, err = repo.Get(ctx, 1, "acme.kit")
- require.NoError(t, err)
- require.True(t, got.Enabled, "saving config must leave the switch alone")
- require.JSONEq(t, `{"region":"eu"}`, string(got.Config))
-
- require.NoError(t, repo.Upsert(ctx, &types.PluginTenantSetting{
- TenantID: 1, PluginID: "acme.kit", Enabled: false, UpdatedAt: time.Now(),
- }, "enabled"))
- got, _ = repo.Get(ctx, 1, "acme.kit")
- require.False(t, got.Enabled)
- require.JSONEq(t, `{"region":"eu"}`, string(got.Config), "flipping the switch must keep the config")
-}
-
-func TestPluginKVRepository(t *testing.T) {
- db, err := gorm.Open(sqlite.Open("file:"+uuid.NewString()+"?mode=memory&cache=shared"), &gorm.Config{})
- require.NoError(t, err)
- require.NoError(t, db.AutoMigrate(&types.PluginKV{}))
- repo := NewPluginKVRepository(db)
- ctx := context.Background()
- put := func(tenant uint64, key, value string, expires *time.Time) {
- t.Helper()
- require.NoError(t, repo.Put(ctx, &types.PluginKV{
- PluginID: "acme.x", TenantID: tenant, Key: key, Value: types.JSON(value),
- ExpiresAt: expires, UpdatedAt: time.Now(),
- }))
- }
- past := time.Now().Add(-time.Minute)
- put(1, "cursor:a", `1`, nil)
- put(1, "cursor:b", `2`, nil)
- put(1, "cursor_x", `3`, nil) // "_" must not act as a LIKE wildcard
- put(1, "old", `4`, &past)
- put(2, "cursor:a", `99`, nil)
- put(1, "cursor:a", `10`, nil) // upsert
-
- got, err := repo.Get(ctx, "acme.x", 1, "cursor:a")
- require.NoError(t, err)
- require.JSONEq(t, `10`, string(got.Value))
- got, err = repo.Get(ctx, "acme.x", 1, "old")
- require.NoError(t, err)
- require.Nil(t, got, "expired entries read as missing")
-
- list, err := repo.List(ctx, "acme.x", 1, "cursor:", "", 10)
- require.NoError(t, err)
- require.Len(t, list, 2)
- list, err = repo.List(ctx, "acme.x", 1, "cursor:", "cursor:a", 10)
- require.NoError(t, err)
- require.Len(t, list, 1)
- require.Equal(t, "cursor:b", list[0].Key)
-
- n, err := repo.Count(ctx, "acme.x", 1)
- require.NoError(t, err)
- require.EqualValues(t, 3, n)
- removed, err := repo.DeleteExpired(ctx, time.Now())
- require.NoError(t, err)
- require.EqualValues(t, 1, removed)
- ok, err := repo.Delete(ctx, "acme.x", 1, "cursor:b")
- require.NoError(t, err)
- require.True(t, ok)
- got, _ = repo.Get(ctx, "acme.x", 2, "cursor:a")
- require.JSONEq(t, `99`, string(got.Value), "tenants are separate partitions")
-}
-
-// A column added to installed plugins must be saved on update too, or
-// changing it silently does nothing.
-func TestPluginUpdateColumnsCoverTheTable(t *testing.T) {
- s, err := schema.Parse(&types.InstalledPlugin{}, &sync.Map{}, schema.NamingStrategy{})
- require.NoError(t, err)
- for _, name := range s.DBNames {
- switch name {
- case "id", "created_at", "created_by":
- continue
- }
- require.Contains(t, pluginUpdateColumns, name, "SavePlugin does not update %s", name)
- }
-}
diff --git a/internal/application/repository/retriever/weaviate/repository_test.go b/internal/application/repository/retriever/weaviate/repository_test.go
index 40ccae42b..37b7808d3 100644
--- a/internal/application/repository/retriever/weaviate/repository_test.go
+++ b/internal/application/repository/retriever/weaviate/repository_test.go
@@ -39,16 +39,13 @@ func (s *schemaTestServer) repository(t *testing.T) *weaviateRepository {
w.Header().Set("Content-Type", "application/json")
switch {
case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/v1/schema/"):
- // Read the state before the probe hook: a barrier in the hook
- // releases every worker at once, and one that reads after
- // another worker's create would see the class and never race.
+ if s.probe != nil {
+ s.probe()
+ }
s.mu.Lock()
s.probes++
exists := s.exists
s.mu.Unlock()
- if s.probe != nil {
- s.probe()
- }
if !exists {
w.WriteHeader(http.StatusNotFound)
return
diff --git a/internal/application/service/agent_service.go b/internal/application/service/agent_service.go
index 1ebe9abac..94de08e34 100644
--- a/internal/application/service/agent_service.go
+++ b/internal/application/service/agent_service.go
@@ -1216,33 +1216,115 @@ func (s *agentService) registerTools(
// logger.Infof(ctx, "Registering tools: %v, webSearchEnabled: %v", allowedTools, config.WebSearchEnabled)
// Register each allowed tool
- build := &toolBuildContext{
- ctx: ctx,
- config: config,
- rerankModel: rerankModel,
- sessionID: sessionID,
- wikiScopes: wikiScopes,
- wikiRoutes: wikiRoutes,
- wikiKBIDs: wikiKBIDs,
- writableWikiKBIDs: writableWikiKBIDs,
- }
for _, toolName := range allowedTools {
- if sandboxBoundTools[toolName] {
+ var toolToRegister types.Tool
+
+ switch toolName {
+ case tools.ToolThinking:
+ toolToRegister = tools.NewSequentialThinkingTool()
+ case tools.ToolTodoWrite:
+ toolToRegister = tools.NewTodoWriteTool()
+ case tools.ToolSearchKnowledge:
+ toolToRegister = tools.NewSearchKnowledgeTool(
+ s.knowledgeBaseService,
+ s.knowledgeService,
+ s.chunkService,
+ config.SearchTargets,
+ rerankModel,
+ s.cfg,
+ )
+ case tools.ToolReadDocument:
+ toolToRegister = tools.NewReadDocumentTool(s.knowledgeService, s.chunkService, config.SearchTargets)
+ case tools.ToolListDocuments:
+ toolToRegister = tools.NewListDocumentsTool(s.knowledgeService, config.SearchTargets)
+ case tools.ToolQueryKnowledgeGraph:
+ var chunkRepo interfaces.ChunkRepository
+ if s.chunkService != nil {
+ chunkRepo = s.chunkService.GetRepository()
+ }
+ toolToRegister = tools.NewQueryKnowledgeGraphTool(s.knowledgeBaseService, config.SearchTargets).
+ WithKnowledgeScope(s.knowledgeService).
+ WithGraph(s.graphRepo, chunkRepo)
+ case tools.ToolSearchConversations:
+ // The owner is captured from the caller's identity here, not read
+ // from the model's arguments, so no prompt can redirect the search
+ // at somebody else's conversations.
+ toolToRegister = tools.NewSearchConversationsTool(
+ s.messageService, types.SessionOwnerIDFromContext(ctx), sessionID)
+ case tools.ToolSearchMemory:
+ // Reaching this case means the memory switches were already
+ // checked above, where the tool is injected. Which memory space is
+ // read is resolved from the request context inside the service, so
+ // this tool needs no owner argument and none can be supplied.
+ toolToRegister = tools.NewSearchMemoryTool(s.memoryService)
+ case tools.ToolDatabaseQuery:
+ toolToRegister = tools.NewDatabaseQueryTool(s.db, config.SearchTargets)
+ case tools.ToolWebSearch:
+ toolToRegister = tools.NewWebSearchTool(
+ s.webSearchService,
+ config.WebSearchMaxResults,
+ config.WebSearchProviderID,
+ )
+ logger.Infof(ctx, "Registered web_search tool for session: %s, maxResults: %d, providerID: %s", sessionID, config.WebSearchMaxResults, config.WebSearchProviderID)
+
+ case tools.ToolWebFetch:
+ toolToRegister = tools.NewWebFetchTool()
+ logger.Infof(ctx, "Registered web_fetch tool for session: %s", sessionID)
+
+ case tools.ToolDataAnalysis:
+ toolToRegister = tools.NewDataAnalysisTool(s.knowledgeBaseService, s.knowledgeService, s.tenantService, s.fileService, s.duckdb, sessionID, s.storageResolver).
+ WithSearchTargets(config.SearchTargets)
+ logger.Infof(ctx, "Registered data_analysis tool for session: %s", sessionID)
+
+ case tools.ToolDataSchema:
+ toolToRegister = tools.NewDataSchemaTool(s.knowledgeService, s.chunkService.GetRepository()).
+ WithSearchTargets(config.SearchTargets)
+ logger.Infof(ctx, "Registered data_schema tool")
+
+ // Wiki tools — only registered when wiki KBs are detected
+ case tools.ToolWikiReadPage:
+ toolToRegister = tools.NewWikiReadPageTool(s.wikiPageService, s.knowledgeService, wikiScopes, wikiRoutes)
+ case tools.ToolWikiSearch:
+ toolToRegister = tools.NewWikiSearchTool(s.wikiPageService, s.knowledgeService, wikiScopes, wikiRoutes)
+ case tools.ToolWikiFlagIssue:
+ toolToRegister = tools.NewWikiFlagIssueTool(s.wikiPageService, writableWikiKBIDs, wikiRoutes).
+ WithKnowledgeScope(s.knowledgeService, config.SearchTargets)
+ case tools.ToolWikiReadIssue:
+ toolToRegister = tools.NewWikiReadIssueTool(s.wikiPageService, wikiKBIDs)
+ case tools.ToolWikiUpdateIssue:
+ toolToRegister = tools.NewWikiUpdateIssueTool(s.wikiPageService, writableWikiKBIDs)
+ case tools.ToolWikiWritePage:
+ toolToRegister = tools.NewWikiWritePageTool(
+ s.wikiPageService, writableWikiKBIDs, s.knowledgeService, wikiRoutes,
+ ).WithSearchTargets(config.SearchTargets)
+ case tools.ToolWikiReplaceText:
+ toolToRegister = tools.NewWikiReplaceTextTool(
+ s.wikiPageService, writableWikiKBIDs, s.knowledgeService, wikiRoutes,
+ ).WithSearchTargets(config.SearchTargets)
+ case tools.ToolWikiRenamePage:
+ toolToRegister = tools.NewWikiRenamePageTool(s.wikiPageService, writableWikiKBIDs, wikiRoutes)
+ case tools.ToolWikiDeletePage:
+ toolToRegister = tools.NewWikiDeletePageTool(s.wikiPageService, writableWikiKBIDs, wikiRoutes)
+
+ case tools.ToolShellExec, tools.ToolReadFile, tools.LegacyToolReadSkill, tools.LegacyToolExecuteSkillScript,
+ tools.ToolListSandboxFiles, tools.LegacyToolReadSandboxFile, tools.ToolWriteSandboxFile,
+ tools.ToolEditSandboxFile:
+ // Bound to the resolved sandbox manager in registerSandboxFileTools
+ // / registerSandboxShellIfAllowed / initializeSkillsManager.
+ // Listing them here would warn "Unknown tool: shell_exec" on every
+ // skill install, then register the real tool a few lines later.
continue
- }
- factory, ok := builtinToolFactories[toolName]
- if !ok {
+
+ default:
logger.Warnf(ctx, "Unknown tool: %s", toolName)
- continue
- }
- toolToRegister := factory(s, build)
- if toolToRegister == nil {
- continue
}
- if toolToRegister.Name() != toolName {
- logger.Warnf(ctx, "Tool name mismatch: expected %s, got %s", toolName, toolToRegister.Name())
+
+ if toolToRegister != nil {
+ if toolToRegister.Name() != toolName {
+ logger.Warnf(ctx, "Tool name mismatch: expected %s, got %s", toolName, toolToRegister.Name())
+ }
+ registry.RegisterTool(toolToRegister)
}
- registry.RegisterTool(toolToRegister)
}
logger.Infof(ctx, "Registered %d tools", len(registry.ListTools()))
diff --git a/internal/application/service/agent_tool_factories.go b/internal/application/service/agent_tool_factories.go
deleted file mode 100644
index bfaa6beb3..000000000
--- a/internal/application/service/agent_tool_factories.go
+++ /dev/null
@@ -1,137 +0,0 @@
-package service
-
-import (
- "context"
-
- "github.com/Tencent/WeKnora/internal/agent/tools"
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/models/rerank"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
-)
-
-// toolBuildContext is what building one agent tool may need from the run.
-type toolBuildContext struct {
- ctx context.Context
- config *types.AgentConfig
- rerankModel rerank.Reranker
- sessionID string
- wikiScopes []tools.WikiScope
- wikiRoutes *tools.WikiRouteResolver
- wikiKBIDs []string
- writableWikiKBIDs []string
-}
-
-// toolFactory builds one builtin tool for a run.
-type toolFactory func(s *agentService, b *toolBuildContext) types.Tool
-
-// builtinToolFactories builds each tool an agent allowlist may name. Adding a
-// builtin tool means adding its factory here; tools from plugins arrive as MCP
-// tools and are registered separately (registerMCPTools).
-var builtinToolFactories = map[string]toolFactory{
- tools.ToolThinking: func(*agentService, *toolBuildContext) types.Tool { return tools.NewSequentialThinkingTool() },
- tools.ToolTodoWrite: func(*agentService, *toolBuildContext) types.Tool { return tools.NewTodoWriteTool() },
- tools.ToolSearchKnowledge: func(s *agentService, b *toolBuildContext) types.Tool {
- return tools.NewSearchKnowledgeTool(
- s.knowledgeBaseService, s.knowledgeService, s.chunkService, b.config.SearchTargets, b.rerankModel, s.cfg,
- )
- },
- tools.ToolReadDocument: func(s *agentService, b *toolBuildContext) types.Tool {
- return tools.NewReadDocumentTool(s.knowledgeService, s.chunkService, b.config.SearchTargets)
- },
- tools.ToolListDocuments: func(s *agentService, b *toolBuildContext) types.Tool {
- return tools.NewListDocumentsTool(s.knowledgeService, b.config.SearchTargets)
- },
- tools.ToolQueryKnowledgeGraph: func(s *agentService, b *toolBuildContext) types.Tool {
- var chunkRepo interfaces.ChunkRepository
- if s.chunkService != nil {
- chunkRepo = s.chunkService.GetRepository()
- }
- return tools.NewQueryKnowledgeGraphTool(s.knowledgeBaseService, b.config.SearchTargets).
- WithKnowledgeScope(s.knowledgeService).
- WithGraph(s.graphRepo, chunkRepo)
- },
- // The owner is captured from the caller's identity here, not read from the
- // model's arguments, so no prompt can redirect the search at somebody
- // else's conversations.
- tools.ToolSearchConversations: func(s *agentService, b *toolBuildContext) types.Tool {
- return tools.NewSearchConversationsTool(s.messageService, types.SessionOwnerIDFromContext(b.ctx), b.sessionID)
- },
- // Reaching this factory means the memory switches were already checked
- // where the tool is injected. The memory space is resolved from the
- // request context inside the service, so the tool takes no owner.
- tools.ToolSearchMemory: func(s *agentService, _ *toolBuildContext) types.Tool {
- return tools.NewSearchMemoryTool(s.memoryService)
- },
- tools.ToolDatabaseQuery: func(s *agentService, b *toolBuildContext) types.Tool {
- return tools.NewDatabaseQueryTool(s.db, b.config.SearchTargets)
- },
- tools.ToolWebSearch: func(s *agentService, b *toolBuildContext) types.Tool {
- logger.Infof(b.ctx, "Registered web_search tool for session: %s, maxResults: %d, providerID: %s",
- b.sessionID, b.config.WebSearchMaxResults, b.config.WebSearchProviderID)
- return tools.NewWebSearchTool(s.webSearchService, b.config.WebSearchMaxResults, b.config.WebSearchProviderID)
- },
- tools.ToolWebFetch: func(_ *agentService, b *toolBuildContext) types.Tool {
- logger.Infof(b.ctx, "Registered web_fetch tool for session: %s", b.sessionID)
- return tools.NewWebFetchTool()
- },
- tools.ToolDataAnalysis: func(s *agentService, b *toolBuildContext) types.Tool {
- logger.Infof(b.ctx, "Registered data_analysis tool for session: %s", b.sessionID)
- return tools.NewDataAnalysisTool(
- s.knowledgeBaseService, s.knowledgeService, s.tenantService, s.fileService, s.duckdb,
- b.sessionID, s.storageResolver,
- ).WithSearchTargets(b.config.SearchTargets)
- },
- tools.ToolDataSchema: func(s *agentService, b *toolBuildContext) types.Tool {
- logger.Infof(b.ctx, "Registered data_schema tool")
- return tools.NewDataSchemaTool(s.knowledgeService, s.chunkService.GetRepository()).
- WithSearchTargets(b.config.SearchTargets)
- },
-
- // Wiki tools only make it into the allowlist when wiki KBs are in scope.
- tools.ToolWikiReadPage: func(s *agentService, b *toolBuildContext) types.Tool {
- return tools.NewWikiReadPageTool(s.wikiPageService, s.knowledgeService, b.wikiScopes, b.wikiRoutes)
- },
- tools.ToolWikiSearch: func(s *agentService, b *toolBuildContext) types.Tool {
- return tools.NewWikiSearchTool(s.wikiPageService, s.knowledgeService, b.wikiScopes, b.wikiRoutes)
- },
- tools.ToolWikiFlagIssue: func(s *agentService, b *toolBuildContext) types.Tool {
- return tools.NewWikiFlagIssueTool(s.wikiPageService, b.writableWikiKBIDs, b.wikiRoutes).
- WithKnowledgeScope(s.knowledgeService, b.config.SearchTargets)
- },
- tools.ToolWikiReadIssue: func(s *agentService, b *toolBuildContext) types.Tool {
- return tools.NewWikiReadIssueTool(s.wikiPageService, b.wikiKBIDs)
- },
- tools.ToolWikiUpdateIssue: func(s *agentService, b *toolBuildContext) types.Tool {
- return tools.NewWikiUpdateIssueTool(s.wikiPageService, b.writableWikiKBIDs)
- },
- tools.ToolWikiWritePage: func(s *agentService, b *toolBuildContext) types.Tool {
- return tools.NewWikiWritePageTool(s.wikiPageService, b.writableWikiKBIDs, s.knowledgeService, b.wikiRoutes).
- WithSearchTargets(b.config.SearchTargets)
- },
- tools.ToolWikiReplaceText: func(s *agentService, b *toolBuildContext) types.Tool {
- return tools.NewWikiReplaceTextTool(s.wikiPageService, b.writableWikiKBIDs, s.knowledgeService, b.wikiRoutes).
- WithSearchTargets(b.config.SearchTargets)
- },
- tools.ToolWikiRenamePage: func(s *agentService, b *toolBuildContext) types.Tool {
- return tools.NewWikiRenamePageTool(s.wikiPageService, b.writableWikiKBIDs, b.wikiRoutes)
- },
- tools.ToolWikiDeletePage: func(s *agentService, b *toolBuildContext) types.Tool {
- return tools.NewWikiDeletePageTool(s.wikiPageService, b.writableWikiKBIDs, b.wikiRoutes)
- },
-}
-
-// sandboxBoundTools are allowlist names bound to the resolved sandbox manager
-// (registerSandboxFileTools / registerSandboxShellIfAllowed /
-// initializeSkillsManager). They have no factory, and seeing them in the
-// allowlist is expected rather than an unknown tool.
-var sandboxBoundTools = map[string]bool{
- tools.ToolShellExec: true,
- tools.ToolReadFile: true,
- tools.LegacyToolReadSkill: true,
- tools.LegacyToolExecuteSkillScript: true,
- tools.ToolListSandboxFiles: true,
- tools.LegacyToolReadSandboxFile: true,
- tools.ToolWriteSandboxFile: true,
- tools.ToolEditSandboxFile: true,
-}
diff --git a/internal/application/service/agent_tool_factories_test.go b/internal/application/service/agent_tool_factories_test.go
deleted file mode 100644
index 42bbeb591..000000000
--- a/internal/application/service/agent_tool_factories_test.go
+++ /dev/null
@@ -1,31 +0,0 @@
-package service
-
-import (
- "testing"
-
- "github.com/Tencent/WeKnora/internal/agent/tools"
-)
-
-func TestEverySelectableToolHasAFactory(t *testing.T) {
- names := []string{tools.ToolWebSearch, tools.ToolWebFetch, tools.ToolSearchMemory}
- for _, def := range tools.AvailableToolDefinitions() {
- names = append(names, def.Name)
- }
- names = append(names, tools.DefaultAllowedTools()...)
- for _, name := range names {
- if _, ok := builtinToolFactories[name]; !ok {
- t.Errorf("%s has no factory in builtinToolFactories", name)
- }
- if sandboxBoundTools[name] {
- t.Errorf("%s is both selectable and sandbox-bound", name)
- }
- }
-}
-
-func TestFactoriesAndSandboxToolsDoNotOverlap(t *testing.T) {
- for name := range sandboxBoundTools {
- if _, ok := builtinToolFactories[name]; ok {
- t.Errorf("%s is sandbox-bound but also has a factory", name)
- }
- }
-}
diff --git a/internal/application/service/datasource_service.go b/internal/application/service/datasource_service.go
index cf9b49f60..fe106dfe1 100644
--- a/internal/application/service/datasource_service.go
+++ b/internal/application/service/datasource_service.go
@@ -658,9 +658,7 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
}
ds.ErrorMessage = syncLog.ErrorMessage
_ = s.dsRepo.Update(ctx, ds)
- // Retrying cannot bring a connector back (its plugin was removed or
- // disabled); the next scheduled or manual sync will try again.
- return fmt.Errorf("%w: %w", asynq.SkipRetry, err)
+ return err
}
// Parse configuration
@@ -1249,9 +1247,7 @@ func allFetchedItemsFailedError(result *types.SyncResult) error {
}
// ValidateCredentials tests connectivity using raw credentials without persisting anything.
-func (s *DataSourceService) ValidateCredentials(
- ctx context.Context, connectorType string, credentials, settings map[string]interface{},
-) error {
+func (s *DataSourceService) ValidateCredentials(ctx context.Context, connectorType string, credentials map[string]interface{}) error {
connector, err := s.connectorRegistry.Get(connectorType)
if err != nil {
return err
@@ -1259,7 +1255,6 @@ func (s *DataSourceService) ValidateCredentials(
config := &types.DataSourceConfig{
Type: connectorType,
Credentials: credentials,
- Settings: settings,
}
if err := connector.Validate(ctx, config); err != nil {
return err
diff --git a/internal/application/service/knowledge_delete.go b/internal/application/service/knowledge_delete.go
index 573e13a93..fbf175730 100644
--- a/internal/application/service/knowledge_delete.go
+++ b/internal/application/service/knowledge_delete.go
@@ -10,10 +10,8 @@ import (
"github.com/Tencent/WeKnora/internal/application/service/retriever"
apperrors "github.com/Tencent/WeKnora/internal/errors"
"github.com/Tencent/WeKnora/internal/logger"
- pluginevents "github.com/Tencent/WeKnora/internal/plugin/events"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
"github.com/hibiken/asynq"
"golang.org/x/sync/errgroup"
)
@@ -606,10 +604,6 @@ func (s *knowledgeService) executeKnowledgeDelete(plan *knowledgeDeletePlan, sin
for _, knowledge := range knowledges {
knowledgeIDs = append(knowledgeIDs, knowledge.ID)
titles = append(titles, knowledge.Title)
- pluginevents.Publish(ctx, tenantInfo.ID, pluginapi.EventKnowledgeDeleted, pluginapi.KnowledgeEventData{
- KnowledgeBaseID: kbID, KnowledgeID: knowledge.ID, Title: knowledge.Title,
- FileName: knowledge.FileName, FileType: knowledge.FileType, Source: knowledge.Source,
- })
}
details := map[string]any{"count": len(knowledgeIDs)}
if len(knowledgeIDs) <= 20 {
diff --git a/internal/application/service/knowledge_util.go b/internal/application/service/knowledge_util.go
index 1706a5be3..e520edbc6 100644
--- a/internal/application/service/knowledge_util.go
+++ b/internal/application/service/knowledge_util.go
@@ -10,7 +10,6 @@ import (
"mime/multipart"
"net/http"
"os"
- "slices"
"strings"
"time"
@@ -57,11 +56,8 @@ func isSupportedImportExtension(ext string) bool {
if ext == "" || ext == unknownFileType {
return false
}
- if _, ok := supportedImportFileExtensions[ext]; ok {
- return true
- }
- // Installed plugins' parser engines bring their own file types.
- return slices.Contains(docparser.PluginFileTypes(), ext)
+ _, ok := supportedImportFileExtensions[ext]
+ return ok
}
// isValidFileType checks if a filename's extension is supported for import.
diff --git a/internal/application/service/tenant_skill_catalog.go b/internal/application/service/tenant_skill_catalog.go
index b935c1139..ae8d5bc1d 100644
--- a/internal/application/service/tenant_skill_catalog.go
+++ b/internal/application/service/tenant_skill_catalog.go
@@ -210,12 +210,11 @@ func (s *TenantSkillService) RegisterCatalogFromArchive(
return s.upsertCatalogFromBundle(ctx, tenantID, bundle, archive, true)
}
-// RegisterCatalogFromSource fetches a public skill, or takes a skill of an
-// enabled plugin, and records it in the catalog.
+// RegisterCatalogFromSource fetches a public skill and records it in the catalog.
func (s *TenantSkillService) RegisterCatalogFromSource(
ctx context.Context, tenantID uint64, source string,
) (*types.TenantSkillCatalogEntity, error) {
- bundle, archive, err := s.resolveSkillSource(ctx, tenantID, source)
+ bundle, archive, err := fetchNormalizedSkillBundle(ctx, source, s.sourceHTTP)
if err != nil {
return nil, err
}
diff --git a/internal/application/service/tenant_skill_service.go b/internal/application/service/tenant_skill_service.go
index 69622e69c..ec7d786ad 100644
--- a/internal/application/service/tenant_skill_service.go
+++ b/internal/application/service/tenant_skill_service.go
@@ -56,19 +56,6 @@ const (
)
// TenantSkillService owns the skill image lifecycle for sandbox configs.
-// PluginSkillArchiver builds the bundle of a skill an installed plugin
-// provides, for a workspace that has the plugin enabled.
-type PluginSkillArchiver interface {
- Archive(ctx context.Context, tenantID uint64, source string) ([]byte, error)
-}
-
-// pluginSkillSourcePrefix marks an install source naming a plugin skill.
-const pluginSkillSourcePrefix = "plugin:"
-
-// SetPluginSkills lets installs name skills of installed plugins. It is
-// called once while the container is built.
-func (s *TenantSkillService) SetPluginSkills(a PluginSkillArchiver) { s.pluginSkills = a }
-
type TenantSkillService struct {
skills repository.TenantSkillRepository
configs repository.TenantSandboxConfigRepository
@@ -100,10 +87,6 @@ type TenantSkillService struct {
// default; tests inject httptest clients.
sourceHTTP *http.Client
- // pluginSkills turns a "plugin:<plugin>/<skill>" source into a bundle
- // from an installed plugin. Nil until the plugin runtime is wired.
- pluginSkills PluginSkillArchiver
-
// cleanupTimeout bounds one piece of compensating work. Injectable so a
// test can let an install outlast it, which every real install does.
cleanupTimeout time.Duration
diff --git a/internal/application/service/tenant_skill_source.go b/internal/application/service/tenant_skill_source.go
index 2b10e13b7..94b3b5503 100644
--- a/internal/application/service/tenant_skill_source.go
+++ b/internal/application/service/tenant_skill_source.go
@@ -106,36 +106,13 @@ func (s *TenantSkillService) InstallSkillFromSource(
return "", err
}
- bundle, archive, err := s.resolveSkillSource(ctx, tenantID, source)
+ bundle, archive, err := fetchNormalizedSkillBundle(ctx, source, s.sourceHTTP)
if err != nil {
return "", err
}
return s.installParsedSkill(ctx, tenantID, configID, bundle, archive, skillArchiveUploaded)
}
-// resolveSkillSource turns a source into a parsed bundle: a skill of an
-// installed plugin the workspace enabled ("plugin:<plugin>/<skill>"), or a
-// public skill fetched over the network.
-func (s *TenantSkillService) resolveSkillSource(
- ctx context.Context, tenantID uint64, source string,
-) (*SkillBundle, []byte, error) {
- if !strings.HasPrefix(source, pluginSkillSourcePrefix) {
- return fetchNormalizedSkillBundle(ctx, source, s.sourceHTTP)
- }
- if s.pluginSkills == nil {
- return nil, nil, fmt.Errorf("%w: plugins are not available", ErrSkillSourceInvalid)
- }
- archive, err := s.pluginSkills.Archive(ctx, tenantID, source)
- if err != nil {
- return nil, nil, fmt.Errorf("%w: %v", ErrSkillSourceInvalid, err)
- }
- bundle, err := ParseSkillBundle(archive)
- if err != nil {
- return nil, nil, err
- }
- return bundle, archive, nil
-}
-
func skillSourceHTTPClient(override *http.Client) *http.Client {
if override != nil {
return override
diff --git a/internal/application/service/tenant_skill_source_test.go b/internal/application/service/tenant_skill_source_test.go
index bc67b5e0f..e0703de88 100644
--- a/internal/application/service/tenant_skill_source_test.go
+++ b/internal/application/service/tenant_skill_source_test.go
@@ -1,9 +1,7 @@
package service
import (
- "context"
"encoding/json"
- "errors"
"fmt"
"net/http"
"net/http/httptest"
@@ -590,39 +588,3 @@ func TestFetchSkillArchiveRejectsOversizeBody(t *testing.T) {
require.ErrorIs(t, err, ErrSkillSourceInvalid)
require.ErrorContains(t, err, "1 MB")
}
-
-type fakePluginSkills struct {
- archive []byte
- err error
- asked string
-}
-
-func (f *fakePluginSkills) Archive(_ context.Context, _ uint64, source string) ([]byte, error) {
- f.asked = source
- return f.archive, f.err
-}
-
-func TestResolveSkillSourceTakesPluginSkills(t *testing.T) {
- ctx := context.Background()
- s := &TenantSkillService{}
- if _, _, err := s.resolveSkillSource(ctx, 1, "plugin:acme.kit/triage"); !errors.Is(err, ErrSkillSourceInvalid) {
- t.Fatalf("without the plugin runtime a plugin source is invalid, got %v", err)
- }
-
- plugins := &fakePluginSkills{archive: zipBundle(t, map[string]string{
- "SKILL.md": "---\nname: triage\ndescription: Triage issues.\n---\nSteps.",
- })}
- s.SetPluginSkills(plugins)
- bundle, archive, err := s.resolveSkillSource(ctx, 1, "plugin:acme.kit/triage")
- if err != nil || bundle.Name != "triage" || len(archive) == 0 {
- t.Fatalf("resolve = %+v, %v", bundle, err)
- }
- if plugins.asked != "plugin:acme.kit/triage" {
- t.Fatalf("archiver asked for %q", plugins.asked)
- }
-
- plugins.err = errors.New("no enabled plugin provides this skill")
- if _, _, err := s.resolveSkillSource(ctx, 1, "plugin:acme.kit/triage"); !errors.Is(err, ErrSkillSourceInvalid) {
- t.Fatalf("an unavailable plugin skill must be a bad source, got %v", err)
- }
-}
diff --git a/internal/application/service/web_search_provider.go b/internal/application/service/web_search_provider.go
index 105573f2c..336b2201d 100644
--- a/internal/application/service/web_search_provider.go
+++ b/internal/application/service/web_search_provider.go
@@ -14,46 +14,11 @@ import (
// webSearchProviderService implements interfaces.WebSearchProviderService
type webSearchProviderService struct {
repo interfaces.WebSearchProviderRepository
- // registry knows the provider types installed plugins contribute.
- registry *infra_web_search.Registry
}
// NewWebSearchProviderService creates a new web search provider service
-func NewWebSearchProviderService(
- repo interfaces.WebSearchProviderRepository, registry *infra_web_search.Registry,
-) interfaces.WebSearchProviderService {
- return &webSearchProviderService{repo: repo, registry: registry}
-}
-
-// validType accepts builtin types and the types of loaded plugins.
-func (s *webSearchProviderService) validType(t types.WebSearchProviderType) bool {
- if isValidProviderType(t) {
- return true
- }
- if s.registry == nil {
- return false
- }
- _, ok := s.registry.PluginType(string(t))
- return ok
-}
-
-// validateParams checks builtin parameters by type and plugin parameters
-// against the plugin's instance schema.
-func (s *webSearchProviderService) validateParams(
- t types.WebSearchProviderType, params types.WebSearchProviderParameters,
-) error {
- if s.registry != nil {
- if pt, ok := s.registry.PluginType(string(t)); ok {
- if err := validateOptionalProxyURL(params.ProxyURL); err != nil {
- return err
- }
- if pt.Validate != nil {
- return pt.Validate(params)
- }
- return nil
- }
- }
- return validateProviderParameters(t, params)
+func NewWebSearchProviderService(repo interfaces.WebSearchProviderRepository) interfaces.WebSearchProviderService {
+ return &webSearchProviderService{repo: repo}
}
// CreateProvider creates a new web search provider configuration.
@@ -62,11 +27,11 @@ func (s *webSearchProviderService) CreateProvider(ctx context.Context, provider
return fmt.Errorf("tenant ID is required")
}
- if !s.validType(provider.Provider) {
+ if !isValidProviderType(provider.Provider) {
return fmt.Errorf("invalid provider type: %s", provider.Provider)
}
- if err := s.validateParams(provider.Provider, provider.Parameters); err != nil {
+ if err := validateProviderParameters(provider.Provider, provider.Parameters); err != nil {
return err
}
@@ -87,7 +52,7 @@ func (s *webSearchProviderService) UpdateProvider(ctx context.Context, provider
}
// Validate provider type if set
- if provider.Provider != "" && !s.validType(provider.Provider) {
+ if provider.Provider != "" && !isValidProviderType(provider.Provider) {
return fmt.Errorf("invalid provider type: %s", provider.Provider)
}
@@ -98,7 +63,7 @@ func (s *webSearchProviderService) UpdateProvider(ctx context.Context, provider
}
if provider.Provider != "" {
- if err := s.validateParams(provider.Provider, provider.Parameters); err != nil {
+ if err := validateProviderParameters(provider.Provider, provider.Parameters); err != nil {
return err
}
}
diff --git a/internal/container/container.go b/internal/container/container.go
index df9d96b83..f2d811072 100644
--- a/internal/container/container.go
+++ b/internal/container/container.go
@@ -6,6 +6,7 @@ package container
import (
"context"
"database/sql"
+ "errors"
"fmt"
"io"
"net"
@@ -58,10 +59,29 @@ import (
"github.com/Tencent/WeKnora/internal/config"
"github.com/Tencent/WeKnora/internal/database"
"github.com/Tencent/WeKnora/internal/datasource"
+ confluenceConnector "github.com/Tencent/WeKnora/internal/datasource/connector/confluence"
+ dingtalkConnector "github.com/Tencent/WeKnora/internal/datasource/connector/dingtalk"
+ "github.com/Tencent/WeKnora/internal/datasource/connector/feishu/core"
+ "github.com/Tencent/WeKnora/internal/datasource/connector/feishu/drive"
+ "github.com/Tencent/WeKnora/internal/datasource/connector/feishu/wiki"
+ gitlabConnector "github.com/Tencent/WeKnora/internal/datasource/connector/gitlab"
+ imaConnector "github.com/Tencent/WeKnora/internal/datasource/connector/ima"
+ notionConnector "github.com/Tencent/WeKnora/internal/datasource/connector/notion"
+ rssConnector "github.com/Tencent/WeKnora/internal/datasource/connector/rss"
+ yuqueConnector "github.com/Tencent/WeKnora/internal/datasource/connector/yuque"
"github.com/Tencent/WeKnora/internal/event"
"github.com/Tencent/WeKnora/internal/handler"
"github.com/Tencent/WeKnora/internal/handler/session"
imPkg "github.com/Tencent/WeKnora/internal/im"
+ "github.com/Tencent/WeKnora/internal/im/dingtalk"
+ "github.com/Tencent/WeKnora/internal/im/feishu"
+ "github.com/Tencent/WeKnora/internal/im/mattermost"
+ "github.com/Tencent/WeKnora/internal/im/qqbot"
+ "github.com/Tencent/WeKnora/internal/im/slack"
+ "github.com/Tencent/WeKnora/internal/im/telegram"
+ "github.com/Tencent/WeKnora/internal/im/wechat"
+ "github.com/Tencent/WeKnora/internal/im/wecom"
+ "github.com/Tencent/WeKnora/internal/im/yunzhijia"
"github.com/Tencent/WeKnora/internal/infrastructure/docparser"
infra_web_search "github.com/Tencent/WeKnora/internal/infrastructure/web_search"
"github.com/Tencent/WeKnora/internal/logger"
@@ -71,17 +91,6 @@ import (
"github.com/Tencent/WeKnora/internal/models/embedding"
"github.com/Tencent/WeKnora/internal/models/limiter" // register built-in vendors
"github.com/Tencent/WeKnora/internal/models/utils/ollama"
- "github.com/Tencent/WeKnora/internal/plugin/activate"
- pluginbuiltin "github.com/Tencent/WeKnora/internal/plugin/builtin"
- plugindriver "github.com/Tencent/WeKnora/internal/plugin/driver"
- pluginevents "github.com/Tencent/WeKnora/internal/plugin/events"
- pluginmanifest "github.com/Tencent/WeKnora/internal/plugin/manifest"
- pluginoauth "github.com/Tencent/WeKnora/internal/plugin/oauth"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- pluginregistry "github.com/Tencent/WeKnora/internal/plugin/registry"
- pluginremote "github.com/Tencent/WeKnora/internal/plugin/remote"
- plugintenancy "github.com/Tencent/WeKnora/internal/plugin/tenancy"
- pluginwebhook "github.com/Tencent/WeKnora/internal/plugin/webhook"
"github.com/Tencent/WeKnora/internal/router"
"github.com/Tencent/WeKnora/internal/sandbox"
"github.com/Tencent/WeKnora/internal/storageallowlist"
@@ -153,10 +162,7 @@ func BuildContainer(container *dig.Container) *dig.Container {
must(container.Provide(repository.NewTenantInvitationRepository))
must(container.Provide(repository.NewAuditLogRepository))
must(container.Provide(repository.NewKnowledgeBaseRepository))
- // Documents reaching completed or failed raise plugin events.
- must(container.Provide(func(db *gorm.DB) interfaces.KnowledgeRepository {
- return pluginevents.WatchKnowledge(repository.NewKnowledgeRepository(db))
- }))
+ must(container.Provide(repository.NewKnowledgeRepository))
must(container.Provide(repository.NewKnowledgeSpanRepository))
must(container.Provide(repository.NewChunkRepository))
must(container.Provide(repository.NewKnowledgeTagRepository))
@@ -169,29 +175,7 @@ func BuildContainer(container *dig.Container) *dig.Container {
must(container.Provide(repository.NewSystemSettingRepository))
must(container.Provide(repository.NewModelCatalogRepository))
must(container.Provide(neo4jRepo.NewNeo4jRepository))
- // Installed plugins' MCP servers are listed alongside stored services.
- must(container.Provide(activate.NewMCPServers))
- must(container.Provide(activate.NewSkills))
- must(container.Provide(activate.NewModelVendors))
- must(container.Provide(newPluginHostManager))
- must(container.Provide(pluginremote.NewManager))
- must(container.Provide(newPluginHostPool))
- must(container.Provide(newPluginDelegation))
- must(container.Provide(newPluginInvoker))
- must(container.Provide(repository.NewPluginKVRepository))
- must(container.Provide(newPluginHostAPI))
- must(container.Provide(activate.NewWebSearch))
- must(container.Provide(activate.NewConnectors))
- must(container.Provide(activate.NewParsers))
- must(container.Provide(activate.NewUIPages))
- must(container.Provide(func(
- reg *pluginregistry.Registry, t *plugintenancy.Service, enq interfaces.TaskEnqueuer, iv *activate.Invoker,
- ) *pluginevents.Dispatcher {
- return pluginevents.NewDispatcher(reg, t, enq, iv)
- }))
- must(container.Provide(func(d *pluginevents.Dispatcher) interfaces.TaskHandler { return d },
- dig.Name("pluginEvents")))
- must(container.Provide(newMCPServiceRepository))
+ must(container.Provide(repository.NewMCPServiceRepository))
must(container.Provide(repository.NewMCPToolApprovalRepository))
must(container.Provide(repository.NewMCPOAuthRepository))
must(container.Provide(repository.NewTenantSandboxConfigRepository))
@@ -610,27 +594,6 @@ func BuildContainer(container *dig.Container) *dig.Container {
must(container.Provide(handler.NewEmbedChannelHandler))
must(container.Provide(handler.NewMCPEndpointHandler))
must(container.Provide(handler.NewWeKnoraCloudHandler))
- // Plugin catalog: builtins are described from what is registered above.
- must(container.Provide(newPluginRegistry))
- must(container.Provide(repository.NewPluginTenantSettingRepository))
- must(container.Provide(plugintenancy.NewService))
- must(container.Provide(func(s *plugintenancy.Service) interfaces.PluginGate { return s }))
- must(container.Invoke(installPluginGate))
- must(container.Provide(repository.NewPluginRepository))
- must(container.Invoke(bindPluginActivators))
- must(container.Provide(newPluginPackageStore))
- must(container.Provide(newPluginReconciler))
- must(container.Invoke(startPluginReconciler))
- must(container.Provide(newPluginInstaller))
- must(container.Provide(newPluginDrivers))
- must(container.Provide(handler.NewPluginHandler))
- must(container.Provide(handler.NewPluginAdminHandler))
- must(container.Provide(handler.NewPluginUIHandler))
- must(container.Provide(pluginwebhook.NewTokensFromEnv))
- must(container.Provide(handler.NewPluginWebhookHandler))
- must(container.Provide(repository.NewPluginOAuthRepository))
- must(container.Provide(pluginoauth.NewService))
- must(container.Provide(handler.NewPluginFormsHandler))
logger.Debugf(ctx, "[Container] HTTP handlers registered")
// Wire the chat package's local image resolver so multimodal chat can read
@@ -1844,56 +1807,37 @@ func NewDuckDB() (*sql.DB, error) {
// Each provider type is registered with its factory function that accepts parameters.
// Provider instances are created on-demand when tenants configure them.
func registerWebSearchProviders(registry *infra_web_search.Registry) {
- pluginbuiltin.RegisterWebSearchProviders(registry)
-}
-
-// newPluginRegistry builds the plugin registry from the builtins this process
-// has registered. It depends on the IM service so adapter factories are in
-// place before the platforms are read.
-func newPluginRegistry(
- connectors *datasource.ConnectorRegistry,
- webSearch *infra_web_search.Registry,
- imService *imPkg.Service,
-) (*pluginregistry.Registry, error) {
- return pluginbuiltin.NewRegistry(pluginbuiltin.Live{
- Version: handler.Version,
- Connectors: connectors,
- WebSearch: webSearch,
- IMPlatforms: imService.Platforms(),
- })
-}
-
-// newPluginDrivers returns the drivers that run plugins: builtins,
-// declarative packages, the embedded host and remote services; a kubernetes
-// driver joins this set.
-func newPluginDrivers(reg *pluginregistry.Registry, r *reconcile.Reconciler) *plugindriver.Set {
- return plugindriver.NewSet(plugindriver.NewBuiltin(reg.Plugin),
- r.Driver(pluginmanifest.RuntimeDeclarative), r.Driver(pluginmanifest.RuntimeHost),
- r.Driver(pluginmanifest.RuntimeRemote))
-}
-
-// installPluginGate gives the integration handlers the tenant plugin switches,
-// so a disabled plugin's integrations drop out of type listings and cannot
-// back new instances.
-func installPluginGate(
- gate interfaces.PluginGate,
- models *handler.ModelHandler,
- webSearch *handler.WebSearchProviderHandler,
- dataSources *handler.DataSourceHandler,
- imHandler *handler.IMHandler,
- system *handler.SystemHandler,
-) {
- gated := []interface{ SetPluginGate(interfaces.PluginGate) }{models, webSearch, dataSources, imHandler, system}
- for _, h := range gated {
- h.SetPluginGate(gate)
- }
+ registry.Register("duckduckgo", infra_web_search.NewDuckDuckGoProvider)
+ registry.Register("google", infra_web_search.NewGoogleProvider)
+ registry.Register("bing", infra_web_search.NewBingProvider)
+ registry.Register("tavily", infra_web_search.NewTavilyProvider)
+ registry.Register("ollama", infra_web_search.NewOllamaProvider)
+ registry.Register("baidu", infra_web_search.NewBaiduProvider)
+ registry.Register("searxng", infra_web_search.NewSearxngProvider)
+ registry.Register("keenable", infra_web_search.NewKeenableProvider)
+ registry.Register("zhipu", infra_web_search.NewZhipuProvider)
+ registry.Register("exa", infra_web_search.NewExaProvider)
+ registry.Register("metaso", infra_web_search.NewMetasoProvider)
+ registry.Register("bocha", infra_web_search.NewBochaProvider)
+ registry.Register("brave", infra_web_search.NewBraveProvider)
+ registry.Register("serply", infra_web_search.NewSerplyProvider)
}
// registerIMService registers adapter factories, loads enabled channels, and
// wires the process-lifetime shutdown hook. Each platform's factory lives in
// its own subpackage to keep this file focused on wiring.
func registerIMService(imService *imPkg.Service, cleaner interfaces.ResourceCleaner) {
- pluginbuiltin.RegisterIMAdapters(imService)
+ imService.RegisterAdapterFactory("wecom", wecom.NewFactory())
+ imService.RegisterAdapterFactory("feishu", feishu.NewFactory(feishu.RegionFeishu))
+ // Lark is Feishu's international cloud: same adapter, different host/tenant.
+ imService.RegisterAdapterFactory("lark", feishu.NewFactory(feishu.RegionLark))
+ imService.RegisterAdapterFactory("slack", slack.NewFactory())
+ imService.RegisterAdapterFactory("telegram", telegram.NewFactory())
+ imService.RegisterAdapterFactory("dingtalk", dingtalk.NewFactory())
+ imService.RegisterAdapterFactory("mattermost", mattermost.NewFactory())
+ imService.RegisterAdapterFactory("wechat", wechat.NewFactory())
+ imService.RegisterAdapterFactory("qqbot", qqbot.NewFactory())
+ imService.RegisterAdapterFactory("yunzhijia", yunzhijia.NewFactory())
// Load and start all enabled channels from database
if err := imService.LoadAndStartChannels(); err != nil {
@@ -1910,7 +1854,54 @@ func registerIMService(imService *imPkg.Service, cleaner interfaces.ResourceClea
// Aggregates registration errors via errors.Join so a misconfigured or duplicated connector fails
// container initialization loudly instead of silently disabling the feature at runtime.
func initConnectorRegistry() (*datasource.ConnectorRegistry, error) {
- return pluginbuiltin.NewConnectorRegistry()
+ registry := datasource.NewConnectorRegistry()
+
+ var errs error
+ if err := registry.Register(wiki.NewConnector(core.RegionFeishu)); err != nil {
+ errs = errors.Join(errs, fmt.Errorf("register feishu connector: %w", err))
+ }
+ // Lark is Feishu's international cloud: same connector, different host/tenant.
+ if err := registry.Register(wiki.NewConnector(core.RegionLark)); err != nil {
+ errs = errors.Join(errs, fmt.Errorf("register lark connector: %w", err))
+ }
+ // Feishu/Lark Drive (云盘) mode: different connector type so the registry
+ // dispatches to the Drive connector. Shares core.Client/Region/export logic
+ // with the wiki connector. See 飞书云盘数据源设计.md / ADR-0001.
+ if err := registry.Register(drive.NewDriveConnector(core.RegionFeishuDrive)); err != nil {
+ errs = errors.Join(errs, fmt.Errorf("register feishu_drive connector: %w", err))
+ }
+ if err := registry.Register(drive.NewDriveConnector(core.RegionLarkDrive)); err != nil {
+ errs = errors.Join(errs, fmt.Errorf("register lark_drive connector: %w", err))
+ }
+ if err := registry.Register(notionConnector.NewConnector()); err != nil {
+ errs = errors.Join(errs, fmt.Errorf("register notion connector: %w", err))
+ }
+ if err := registry.Register(confluenceConnector.NewConnector()); err != nil {
+ errs = errors.Join(errs, fmt.Errorf("register confluence connector: %w", err))
+ }
+ if err := registry.Register(yuqueConnector.NewConnector()); err != nil {
+ errs = errors.Join(errs, fmt.Errorf("register yuque connector: %w", err))
+ }
+ if err := registry.Register(dingtalkConnector.NewConnector()); err != nil {
+ errs = errors.Join(errs, fmt.Errorf("register dingtalk connector: %w", err))
+ }
+ if err := registry.Register(imaConnector.NewConnector()); err != nil {
+ errs = errors.Join(errs, fmt.Errorf("register ima connector: %w", err))
+ }
+ if err := registry.Register(rssConnector.NewConnector()); err != nil {
+ errs = errors.Join(errs, fmt.Errorf("register rss connector: %w", err))
+ }
+ if err := registry.Register(gitlabConnector.NewConnector()); err != nil {
+ errs = errors.Join(errs, fmt.Errorf("register gitlab connector: %w", err))
+ }
+
+ // Future connectors will be registered here:
+ // if err := registry.Register(githubConnector.NewConnector()); err != nil { ... }
+
+ if errs != nil {
+ return nil, errs
+ }
+ return registry, nil
}
// startDataSourceScheduler starts the data source cron scheduler and registers cleanup.
diff --git a/internal/container/plugin_host.go b/internal/container/plugin_host.go
deleted file mode 100644
index e9bb1e65b..000000000
--- a/internal/container/plugin_host.go
+++ /dev/null
@@ -1,152 +0,0 @@
-package container
-
-import (
- "context"
- "errors"
- "fmt"
- "net"
- "net/http"
- "net/url"
- "os"
- "strconv"
- "strings"
- "time"
-
- "github.com/Tencent/WeKnora/internal/application/repository"
- "github.com/Tencent/WeKnora/internal/config"
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/host"
- "github.com/Tencent/WeKnora/internal/plugin/hostpool"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- pluginregistry "github.com/Tencent/WeKnora/internal/plugin/registry"
-)
-
-// Standalone plugin host settings.
-const (
- envPluginHostAddr = "WEKNORA_PLUGIN_HOST_ADDR" // listen address, default :8081
- envPluginHostURL = "WEKNORA_PLUGIN_HOST_URL" // gateway URL app nodes use
- envPluginHostKinds = "WEKNORA_PLUGIN_HOST_KINDS" // kinds to run, default all available
-)
-
-// RunPluginHost runs this binary as a standalone plugin host
-// (weknora plugin-host) until ctx ends. It shares the app's database,
-// object storage and Redis, runs the installed host plugins of the kinds it
-// supports, announces them in Redis and serves them to app nodes through a
-// gateway signed with the cluster key. It never migrates the database;
-// app nodes do.
-func RunPluginHost(ctx context.Context) error {
- if _, set := os.LookupEnv("AUTO_MIGRATE"); !set {
- _ = os.Setenv("AUTO_MIGRATE", "false")
- }
- key, err := hostpool.ClusterKey()
- if err != nil {
- return err
- }
- rdb, err := initRedisClient()
- if err != nil {
- return err
- }
- if rdb == nil {
- return errors.New("a plugin host needs Redis (REDIS_ADDR) to announce itself to app nodes")
- }
- cfg, err := config.LoadConfig()
- if err != nil {
- return fmt.Errorf("load config: %w", err)
- }
- db, err := initDatabase(cfg)
- if err != nil {
- return fmt.Errorf("database: %w", err)
- }
- store, err := newPluginPackageStore(cfg)
- if err != nil {
- return fmt.Errorf("package storage: %w", err)
- }
-
- kinds := host.KindsFromEnv(envPluginHostKinds)
- if len(kinds) == 0 {
- return fmt.Errorf("%s leaves this plugin host nothing to run", envPluginHostKinds)
- }
- mgr := host.NewStandaloneManager(kinds)
- // Plugins call the Host API on an app node, not through their egress
- // proxy.
- hostAPI := strings.TrimSpace(os.Getenv("WEKNORA_PLUGIN_HOST_API_URL"))
- if u, err := url.Parse(hostAPI); err == nil && u.Hostname() != "" {
- mgr.SetDirectHosts(u.Hostname())
- }
- r := reconcile.New(reconcile.Options{
- Repo: repository.NewPluginRepository(db), Store: store, Registry: pluginregistry.New(), Redis: rdb,
- Activators: []reconcile.Activator{mgr}, Runtimes: []manifest.RuntimeType{manifest.RuntimeHost},
- Accept: func(m *manifest.Manifest) bool { return mgr.Runs(m.Runtime.Kind) }, Role: "plugin-host",
- })
- mgr.SetReporter(r)
-
- addr := os.Getenv(envPluginHostAddr)
- if addr == "" {
- addr = ":8081"
- }
- ln, err := net.Listen("tcp", addr)
- if err != nil {
- return fmt.Errorf("listen %s: %w", addr, err)
- }
- advertise, err := advertiseURL(ln.Addr())
- if err != nil {
- _ = ln.Close()
- return err
- }
- srv := &http.Server{Handler: mgr.Gateway(key), ReadHeaderTimeout: 10 * time.Second}
- serveErr := make(chan error, 1)
- go func() { serveErr <- srv.Serve(ln) }()
-
- runCtx, stop := context.WithCancel(ctx)
- defer stop()
- r.Start(runCtx)
- hostname, _ := os.Hostname()
- announced := make(chan struct{})
- go func() {
- hostpool.NewAnnouncer(rdb, mgr, r.NodeName(), advertise).Run(runCtx)
- close(announced)
- }()
- logger.Infof(ctx, "[plugin-host] %s runs %s plugins, serving app nodes at %s",
- hostname, strings.Join(kinds, ", "), advertise)
-
- select {
- case <-ctx.Done():
- case err := <-serveErr:
- stop()
- <-announced
- mgr.Close()
- return fmt.Errorf("gateway: %w", err)
- }
- // Withdraw first so app nodes stop picking this host, then let calls in
- // flight finish before the plugins stop.
- stop()
- <-announced
- shutdown, cancel := context.WithTimeout(context.Background(), 60*time.Second)
- defer cancel()
- if err := srv.Shutdown(shutdown); err != nil {
- logger.Warnf(ctx, "[plugin-host] shutdown: %v", err)
- }
- mgr.Close()
- return nil
-}
-
-// advertiseURL is the gateway URL app nodes use: WEKNORA_PLUGIN_HOST_URL,
-// or http://<hostname>:<port>, which suits compose and Kubernetes pod DNS.
-func advertiseURL(addr net.Addr) (string, error) {
- if u := strings.TrimSpace(os.Getenv(envPluginHostURL)); u != "" {
- if _, err := url.Parse(u); err != nil {
- return "", fmt.Errorf("%s: %w", envPluginHostURL, err)
- }
- return strings.TrimSuffix(u, "/"), nil
- }
- tcp, ok := addr.(*net.TCPAddr)
- if !ok {
- return "", fmt.Errorf("set %s", envPluginHostURL)
- }
- name, err := os.Hostname()
- if err != nil || name == "" {
- return "", fmt.Errorf("no hostname to advertise; set %s", envPluginHostURL)
- }
- return "http://" + net.JoinHostPort(name, strconv.Itoa(tcp.Port)), nil
-}
diff --git a/internal/container/plugin_runtime.go b/internal/container/plugin_runtime.go
deleted file mode 100644
index 71e4f4a80..000000000
--- a/internal/container/plugin_runtime.go
+++ /dev/null
@@ -1,237 +0,0 @@
-package container
-
-import (
- "context"
- "fmt"
- "os"
- "strings"
- "time"
-
- "github.com/redis/go-redis/v9"
- "go.uber.org/dig"
- "gorm.io/gorm"
-
- "github.com/Tencent/WeKnora/internal/application/repository"
- "github.com/Tencent/WeKnora/internal/application/service"
- "github.com/Tencent/WeKnora/internal/config"
- "github.com/Tencent/WeKnora/internal/handler"
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/activate"
- pluginevents "github.com/Tencent/WeKnora/internal/plugin/events"
- "github.com/Tencent/WeKnora/internal/plugin/host"
- "github.com/Tencent/WeKnora/internal/plugin/hostapi"
- "github.com/Tencent/WeKnora/internal/plugin/hostpool"
- "github.com/Tencent/WeKnora/internal/plugin/install"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- pluginoauth "github.com/Tencent/WeKnora/internal/plugin/oauth"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- pluginregistry "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/plugin/remote"
- plugintenancy "github.com/Tencent/WeKnora/internal/plugin/tenancy"
- "github.com/Tencent/WeKnora/internal/plugin/webhook"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
- "github.com/Tencent/WeKnora/pluginsdk/client"
-)
-
-// newPluginPackageStore keeps plugin packages in the deployment's object
-// storage. Packages belong to the platform, not a tenant, so they skip the
-// tenant resource catalog.
-func newPluginPackageStore(cfg *config.Config) (reconcile.PackageStore, error) {
- fs, err := initRawFileService(cfg)
- if err != nil {
- return nil, err
- }
- return reconcile.NewFileStore(fs), nil
-}
-
-// pluginActivators are the domains installed plugins contribute to.
-type pluginActivators struct {
- dig.In
-
- Host *host.Manager
- Remote *remote.Manager
- Delegation *pluginDelegation
- WebSearch *activate.WebSearch
- Connectors *activate.Connectors
- Parsers *activate.Parsers
- UIPages *activate.UIPages
- MCP *activate.MCPServers
- Skills *activate.Skills
- Vendors *activate.ModelVendors
- Invoker *activate.Invoker
-}
-
-// list orders the activators: the runtimes first, so a code plugin is
-// reachable before anything routes calls to it.
-func (a pluginActivators) list() []reconcile.Activator {
- return []reconcile.Activator{
- a.Host, a.Remote, a.Delegation, a.WebSearch, a.Connectors, a.Parsers, a.UIPages, a.Vendors, a.MCP, a.Skills,
- }
-}
-
-// newPluginHostAPI serves the Host API and gives calls a way back to it: the
-// embedded host's plugins reach this node on loopback, plugins elsewhere
-// (remote, on a plugin host) only through WEKNORA_PLUGIN_HOST_API_URL.
-func newPluginHostAPI(
- cfg *config.Config, iv *activate.Invoker, repo interfaces.PluginKVRepository, cleaner interfaces.ResourceCleaner,
- hooks *webhook.Tokens, oauth *pluginoauth.Service,
-) *hostapi.Handler {
- iv.SetWebhooks(hooks, webhook.PublicBase())
- iv.SetOAuth(oauth)
- issuer := hostapi.NewIssuerFromEnv()
- // Plugins on this node always use loopback; the public address is for
- // plugins elsewhere (remote, on a plugin host), which reach this node
- // the way the deployment routes to it.
- port := 8080
- if cfg != nil && cfg.Server != nil && cfg.Server.Port > 0 {
- port = cfg.Server.Port
- }
- iv.SetHostAPI(issuer, fmt.Sprintf("http://127.0.0.1:%d", port))
- iv.SetPublicHostAPI(strings.TrimSpace(os.Getenv("WEKNORA_PLUGIN_HOST_API_URL")))
- kv := hostapi.NewKV(repo)
- ctx, cancel := context.WithCancel(context.Background())
- kv.StartSweeper(ctx, 10*time.Minute)
- cleaner.RegisterWithName("PluginKVSweeper", func() error { cancel(); return nil })
- return hostapi.NewHandler(issuer, kv)
-}
-
-// bindPluginActivators hands the activators what they need once the plugin
-// services exist.
-func bindPluginActivators(
- a pluginActivators, t *plugintenancy.Service, repo interfaces.PluginRepository,
- skills *service.TenantSkillService,
-) {
- a.MCP.Bind(t, repo)
- a.MCP.SetInvoker(a.Invoker)
- a.Invoker.Bind(t, repo)
- a.Skills.Bind(t)
- skills.SetPluginSkills(a.Skills)
-}
-
-// newPluginHostManager is this node's embedded plugin host. It runs the
-// kinds WEKNORA_PLUGIN_EMBEDDED_KINDS names (default: every kind this
-// machine can run; "none" leaves all host plugins to standalone hosts).
-func newPluginHostManager() *host.Manager {
- m := host.NewManager()
- m.SetKinds(host.KindsFromEnv("WEKNORA_PLUGIN_EMBEDDED_KINDS"))
- return m
-}
-
-// newPluginHostPool finds standalone plugin hosts (weknora plugin-host) in
-// Redis. It is nil without Redis or a cluster key: host plugins then run on
-// this node or nowhere.
-func newPluginHostPool(rdb *redis.Client) *hostpool.Pool {
- if rdb == nil {
- return nil
- }
- key, err := hostpool.ClusterKey()
- if err != nil {
- logger.Warnf(context.Background(), "[plugin] standalone plugin hosts are off: %v", err)
- return nil
- }
- return hostpool.NewPool(rdb, key)
-}
-
-// newPluginInvoker reaches code plugins through this node's plugin host, a
-// standalone plugin host, or a remote plugin's registered URL.
-func newPluginInvoker(h *host.Manager, r *remote.Manager, pool *hostpool.Pool) *activate.Invoker {
- return activate.NewInvoker(pluginClients{host: h, remote: r, pool: pool})
-}
-
-// pluginClients finds a code plugin in whichever runtime serves it.
-type pluginClients struct {
- host *host.Manager
- remote *remote.Manager
- pool *hostpool.Pool
-}
-
-func (c pluginClients) Client(ctx context.Context, m *manifest.Manifest) (*client.Client, error) {
- switch {
- case c.remote.Owns(m.ID):
- return c.remote.Client(m.ID)
- case c.host.Local(m.ID) || c.pool == nil || m.Runtime.Type != manifest.RuntimeHost:
- return c.host.Client(m.ID)
- default:
- return c.pool.Client(ctx, m.ID, m.Version)
- }
-}
-
-func (c pluginClients) OnThisNode(pluginID string) bool { return c.host.Local(pluginID) }
-
-// pluginDelegation fails a host plugin this node leaves to standalone
-// plugin hosts when there are none to leave it to. With hosts configured it
-// only logs: they may start later, and calls say which plugin is missing.
-type pluginDelegation struct {
- host *host.Manager
- pool *hostpool.Pool
-}
-
-func newPluginDelegation(h *host.Manager, pool *hostpool.Pool) *pluginDelegation {
- return &pluginDelegation{host: h, pool: pool}
-}
-
-func (d *pluginDelegation) Name() string { return "plugin-hosts" }
-
-func (d *pluginDelegation) Activate(ctx context.Context, l *reconcile.Loaded) error {
- m := l.Manifest
- if m.Runtime.Type != manifest.RuntimeHost || d.host.Runs(m.Runtime.Kind) {
- return nil
- }
- if d.pool == nil {
- return fmt.Errorf("this node does not run %s plugins (WEKNORA_PLUGIN_EMBEDDED_KINDS) and no plugin host "+
- "is configured; run weknora plugin-host with Redis and the same SYSTEM_AES_KEY", m.Runtime.Kind)
- }
- if !d.pool.Runs(ctx, m.ID, m.Version) {
- logger.Infof(ctx, "[plugin] %s@%s waits for a plugin host that runs %s plugins",
- m.ID, m.Version, m.Runtime.Kind)
- }
- return nil
-}
-
-func (d *pluginDelegation) Deactivate(context.Context, string) error { return nil }
-
-func newMCPServiceRepository(db *gorm.DB, plugins *activate.MCPServers) interfaces.MCPServiceRepository {
- return plugins.Repository(repository.NewMCPServiceRepository(db))
-}
-
-func newPluginReconciler(
- repo interfaces.PluginRepository,
- store reconcile.PackageStore,
- reg *pluginregistry.Registry,
- rdb *redis.Client,
- activators pluginActivators,
-) *reconcile.Reconciler {
- return reconcile.New(reconcile.Options{
- Repo: repo, Store: store, Registry: reg, Redis: rdb, Activators: activators.list(),
- })
-}
-
-func newPluginInstaller(
- repo interfaces.PluginRepository,
- store reconcile.PackageStore,
- r *reconcile.Reconciler,
-) *install.Service {
- return install.NewService(repo, store, r, handler.Version).WithChecks(activate.CheckModelVendors)
-}
-
-// startPluginReconciler loads installed plugins before the server takes
-// traffic, then keeps this node in step with the others.
-func startPluginReconciler(
- r *reconcile.Reconciler, hostManager *host.Manager, remoteManager *remote.Manager,
- events *pluginevents.Dispatcher, cleaner interfaces.ResourceCleaner,
-) {
- pluginevents.SetDefault(events)
- if kinds := hostManager.Kinds(); len(kinds) > 0 {
- logger.Infof(context.Background(), "[plugin] this node runs %s host plugins", strings.Join(kinds, ", "))
- }
- hostManager.SetReporter(r)
- remoteManager.SetReporter(r)
- ctx, cancel := context.WithCancel(context.Background())
- r.Start(ctx)
- cleaner.RegisterWithName("PluginReconciler", func() error {
- cancel()
- hostManager.Close()
- remoteManager.Close()
- return nil
- })
-}
diff --git a/internal/container/plugin_runtime_test.go b/internal/container/plugin_runtime_test.go
deleted file mode 100644
index 78c269a82..000000000
--- a/internal/container/plugin_runtime_test.go
+++ /dev/null
@@ -1,72 +0,0 @@
-package container
-
-import (
- "context"
- "strings"
- "testing"
-
- "github.com/alicebob/miniredis/v2"
- "github.com/redis/go-redis/v9"
-
- "github.com/Tencent/WeKnora/internal/plugin/host"
- "github.com/Tencent/WeKnora/internal/plugin/hostpool"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/plugin/remote"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-func pythonPlugin() *reconcile.Loaded {
- return &reconcile.Loaded{Manifest: &manifest.Manifest{
- ID: "acme.py", Version: "1.0.0",
- Runtime: manifest.Runtime{Type: manifest.RuntimeHost, Kind: host.KindPython, Entry: "main.py"},
- }}
-}
-
-// A node that does not run a kind hands it to plugin hosts; without any it
-// says so instead of pretending the plugin loaded.
-func TestDelegationNeedsPluginHosts(t *testing.T) {
- ctx := context.Background()
- h := host.NewManager()
- h.SetKinds([]string{host.KindBinary})
-
- err := newPluginDelegation(h, nil).Activate(ctx, pythonPlugin())
- if err == nil || !strings.Contains(err.Error(), "no plugin host is configured") {
- t.Fatalf("without plugin hosts = %v", err)
- }
-
- mr := miniredis.RunT(t)
- pool := hostpool.NewPool(redis.NewClient(&redis.Options{Addr: mr.Addr()}), []byte("k"))
- if err := newPluginDelegation(h, pool).Activate(ctx, pythonPlugin()); err != nil {
- t.Fatalf("with plugin hosts configured, a missing one is not a load failure: %v", err)
- }
-
- h.SetKinds([]string{host.KindPython})
- if err := newPluginDelegation(h, nil).Activate(ctx, pythonPlugin()); err != nil {
- t.Fatalf("a kind the node runs needs no plugin host: %v", err)
- }
-}
-
-// Calls go to the pool only for host plugins this node does not run.
-func TestPluginClientsRouteToThePool(t *testing.T) {
- ctx := context.Background()
- h := host.NewManager()
- h.SetKinds([]string{host.KindBinary})
- mr := miniredis.RunT(t)
- pool := hostpool.NewPool(redis.NewClient(&redis.Options{Addr: mr.Addr()}), []byte("k"))
- clients := pluginClients{host: h, remote: remote.NewManager(), pool: pool}
-
- _, err := clients.Client(ctx, pythonPlugin().Manifest)
- if pe, ok := pluginapi.AsError(err); !ok || !strings.Contains(pe.Message, "no plugin host runs [email protected]") {
- t.Fatalf("a delegated plugin is looked up in the pool, got %v", err)
- }
- if clients.OnThisNode("acme.py") {
- t.Fatal("a delegated plugin is not on this node")
- }
-
- clients.pool = nil
- _, err = clients.Client(ctx, pythonPlugin().Manifest)
- if pe, ok := pluginapi.AsError(err); !ok || !strings.Contains(pe.Message, "not running on this node") {
- t.Fatalf("without a pool the node's own host answers, got %v", err)
- }
-}
diff --git a/internal/database/migration_sqlite_versioned_schema_test.go b/internal/database/migration_sqlite_versioned_schema_test.go
index 065659175..2e60e8ced 100644
--- a/internal/database/migration_sqlite_versioned_schema_test.go
+++ b/internal/database/migration_sqlite_versioned_schema_test.go
@@ -35,10 +35,6 @@ var versionedSQLiteTables = []string{
"tenant_skill_snapshots",
"tenant_skill_catalog",
"tenant_user_env_vars",
- "plugin_tenant_settings",
- "plugins",
- "plugin_versions",
- "plugin_kv",
}
// versionedSQLiteColumns maps each existing table to the columns that the
@@ -73,7 +69,7 @@ var versionedSQLiteColumns = map[string][]string{
}, // 000028
}
-const expectedSQLiteMigrationVersion = 38
+const expectedSQLiteMigrationVersion = 33
func TestSQLiteMigrationsCreateVersionedSchema(t *testing.T) {
repoRoot := sqliteRepoRoot(t)
diff --git a/internal/datasource/CONNECTOR_IMPLEMENTATION_GUIDE.md b/internal/datasource/CONNECTOR_IMPLEMENTATION_GUIDE.md
index 63fe83221..72772e46e 100644
--- a/internal/datasource/CONNECTOR_IMPLEMENTATION_GUIDE.md
+++ b/internal/datasource/CONNECTOR_IMPLEMENTATION_GUIDE.md
@@ -360,16 +360,6 @@ var ConnectorMetadataRegistry = map[string]ConnectorMetadata{
}
```
-### Credential form
-
-Describe the connector's `credentials` in `connectorForms`
-(`internal/datasource/connector_forms.go`): a config schema for the fields
-(mark secrets with `Secret`, use `VisibleIf` for fields that depend on
-another one) plus the setup-guide links and required permissions. The
-frontend renders the form from `GET /api/v1/datasource/types`, so no frontend
-change is needed. Frontend locale keys named in `I18nKeys` must be listed in
-`frontend/src/i18n/backendSchemaKeys.ts`; a test enforces it.
-
## Step 8: Test Your Connector
```go
diff --git a/internal/datasource/connector.go b/internal/datasource/connector.go
index fba146611..67568c4cc 100644
--- a/internal/datasource/connector.go
+++ b/internal/datasource/connector.go
@@ -2,12 +2,7 @@ package datasource
import (
"context"
- "fmt"
- "math"
- "sort"
- "sync"
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
"github.com/Tencent/WeKnora/internal/types"
)
@@ -123,21 +118,15 @@ type FullSyncWithCursor interface {
) ([]types.FetchedItem, *types.SyncCursor, error)
}
-// ConnectorRegistry manages the registration and lookup of available
-// connectors. Installed plugins add and remove connectors while the server
-// runs, so it is safe for concurrent use.
+// ConnectorRegistry manages the registration and lookup of available connectors
type ConnectorRegistry struct {
- mu sync.RWMutex
connectors map[string]Connector
- // plugins holds the metadata of connectors installed plugins registered.
- plugins map[string]ConnectorMetadata
}
// NewConnectorRegistry creates a new connector registry
func NewConnectorRegistry() *ConnectorRegistry {
return &ConnectorRegistry{
connectors: make(map[string]Connector),
- plugins: make(map[string]ConnectorMetadata),
}
}
@@ -149,51 +138,12 @@ func (r *ConnectorRegistry) Register(connector Connector) error {
if connector.Type() == "" {
return ErrConnectorTypeEmpty
}
- r.mu.Lock()
- defer r.mu.Unlock()
r.connectors[connector.Type()] = connector
return nil
}
-// RegisterPlugin adds or replaces a plugin's connector with the metadata it
-// is listed under. It refuses to shadow a builtin connector.
-func (r *ConnectorRegistry) RegisterPlugin(connector Connector, meta ConnectorMetadata) error {
- if connector == nil {
- return ErrConnectorNil
- }
- t := connector.Type()
- if t == "" {
- return ErrConnectorTypeEmpty
- }
- r.mu.Lock()
- defer r.mu.Unlock()
- if _, exists := r.connectors[t]; exists {
- if _, isPlugin := r.plugins[t]; !isPlugin {
- return fmt.Errorf("connector type %s already exists", t)
- }
- }
- meta.Type = t
- r.connectors[t] = connector
- r.plugins[t] = meta
- return nil
-}
-
-// Unregister removes a plugin's connector; builtins stay. Syncs already
-// running keep the connector they started with.
-func (r *ConnectorRegistry) Unregister(connectorType string) {
- r.mu.Lock()
- defer r.mu.Unlock()
- if _, isPlugin := r.plugins[connectorType]; !isPlugin {
- return
- }
- delete(r.plugins, connectorType)
- delete(r.connectors, connectorType)
-}
-
// Get retrieves a connector by type
func (r *ConnectorRegistry) Get(connectorType string) (Connector, error) {
- r.mu.RLock()
- defer r.mu.RUnlock()
connector, exists := r.connectors[connectorType]
if !exists {
return nil, ErrConnectorNotFound
@@ -203,8 +153,6 @@ func (r *ConnectorRegistry) Get(connectorType string) (Connector, error) {
// List returns all registered connector types
func (r *ConnectorRegistry) List() []string {
- r.mu.RLock()
- defer r.mu.RUnlock()
types := make([]string, 0, len(r.connectors))
for t := range r.connectors {
types = append(types, t)
@@ -221,26 +169,6 @@ type ConnectorMetadata struct {
Priority int `json:"priority"` // Priority order for UI display (lower = higher priority)
AuthType string `json:"auth_type"` // "oauth2", "api_key", "token", etc.
Capabilities []string `json:"capabilities"` // "incremental", "webhook", "deletion_sync", etc.
-
- // Setup guide shown next to the credential form: where to create the
- // app, which scopes to grant and where to grant them.
- DocURL string `json:"doc_url,omitempty"`
- PermissionDocURL string `json:"permission_doc_url,omitempty"`
- PermissionPageURL string `json:"permission_page_url,omitempty"`
- RequiredPermissions []string `json:"required_permissions,omitempty"`
- // ConfigSchema describes DataSourceConfig.Credentials for this
- // connector; the editor renders the credential form from it.
- ConfigSchema *configschema.Schema `json:"config_schema,omitempty"`
- // SettingsSchema describes DataSourceConfig.Settings (non-secret
- // options) for connectors whose settings form is not built into the
- // frontend, such as plugin connectors.
- SettingsSchema *configschema.Schema `json:"settings_schema,omitempty"`
- // Names and Descriptions localize Name and Description, keyed by
- // locale, for plugin connectors (builtins use frontend locale keys).
- Names map[string]string `json:"names,omitempty"`
- Descriptions map[string]string `json:"descriptions,omitempty"`
- // PluginID names the installed plugin providing the connector.
- PluginID string `json:"plugin_id,omitempty"`
}
// GetConnectorMetadata returns metadata for all available connectors
@@ -258,7 +186,7 @@ var ConnectorMetadataRegistry = map[string]ConnectorMetadata{
Type: types.ConnectorTypeLark,
Name: "Lark",
Description: "Sync documents, wikis, and content from Lark (Feishu international)",
- Priority: 1,
+ Priority: 0,
AuthType: "oauth2",
Capabilities: []string{"incremental", "deletion_sync"},
},
@@ -266,7 +194,7 @@ var ConnectorMetadataRegistry = map[string]ConnectorMetadata{
Type: types.ConnectorTypeFeishuDrive,
Name: "Feishu Drive (飞书云盘)",
Description: "Sync documents and files from a Feishu Drive folder",
- Priority: 2,
+ Priority: 0,
AuthType: "oauth2",
Capabilities: []string{"incremental", "deletion_sync"},
},
@@ -274,7 +202,7 @@ var ConnectorMetadataRegistry = map[string]ConnectorMetadata{
Type: types.ConnectorTypeLarkDrive,
Name: "Lark Drive",
Description: "Sync documents and files from a Lark Drive folder",
- Priority: 3,
+ Priority: 0,
AuthType: "oauth2",
Capabilities: []string{"incremental", "deletion_sync"},
},
@@ -282,7 +210,7 @@ var ConnectorMetadataRegistry = map[string]ConnectorMetadata{
Type: types.ConnectorTypeNotion,
Name: "Notion",
Description: "Sync pages and databases from Notion",
- Priority: 4,
+ Priority: 1,
AuthType: "api_key",
Capabilities: []string{"incremental"},
},
@@ -290,7 +218,7 @@ var ConnectorMetadataRegistry = map[string]ConnectorMetadata{
Type: types.ConnectorTypeConfluence,
Name: "Confluence",
Description: "Sync spaces and pages from Atlassian Confluence",
- Priority: 5,
+ Priority: 2,
AuthType: "api_key",
Capabilities: []string{"incremental", "deletion_sync"},
},
@@ -298,7 +226,7 @@ var ConnectorMetadataRegistry = map[string]ConnectorMetadata{
Type: types.ConnectorTypeYuque,
Name: "Yuque (语雀)",
Description: "Sync knowledge bases and documents from Yuque",
- Priority: 6,
+ Priority: 3,
AuthType: "api_key",
Capabilities: []string{"incremental"},
},
@@ -306,7 +234,7 @@ var ConnectorMetadataRegistry = map[string]ConnectorMetadata{
Type: types.ConnectorTypeIMA,
Name: "Tencent IMA (ima.qq.com)",
Description: "Sync knowledge bases and documents from Tencent IMA",
- Priority: 8,
+ Priority: 3,
AuthType: "api_key",
Capabilities: []string{"incremental", "deletion_sync"},
},
@@ -314,7 +242,7 @@ var ConnectorMetadataRegistry = map[string]ConnectorMetadata{
Type: types.ConnectorTypeGitHub,
Name: "GitHub",
Description: "Sync repositories, wikis, and issues from GitHub",
- Priority: 20,
+ Priority: 4,
AuthType: "oauth2",
Capabilities: []string{"incremental"},
},
@@ -322,7 +250,7 @@ var ConnectorMetadataRegistry = map[string]ConnectorMetadata{
Type: types.ConnectorTypeGoogleDrive,
Name: "Google Drive",
Description: "Sync documents and files from Google Drive",
- Priority: 21,
+ Priority: 5,
AuthType: "oauth2",
Capabilities: []string{"incremental"},
},
@@ -330,7 +258,7 @@ var ConnectorMetadataRegistry = map[string]ConnectorMetadata{
Type: types.ConnectorTypeOneDrive,
Name: "OneDrive / SharePoint",
Description: "Sync documents and files from Microsoft OneDrive",
- Priority: 22,
+ Priority: 6,
AuthType: "oauth2",
Capabilities: []string{"incremental"},
},
@@ -346,7 +274,7 @@ var ConnectorMetadataRegistry = map[string]ConnectorMetadata{
Type: types.ConnectorTypeWebCrawler,
Name: "Web Crawler (Sitemap)",
Description: "Crawl websites via Sitemap.xml",
- Priority: 23,
+ Priority: 9,
AuthType: "none",
Capabilities: []string{},
},
@@ -354,7 +282,7 @@ var ConnectorMetadataRegistry = map[string]ConnectorMetadata{
Type: types.ConnectorTypeSlack,
Name: "Slack",
Description: "Sync channel messages and files from Slack",
- Priority: 24,
+ Priority: 10,
AuthType: "oauth2",
Capabilities: []string{"incremental"},
},
@@ -362,7 +290,7 @@ var ConnectorMetadataRegistry = map[string]ConnectorMetadata{
Type: types.ConnectorTypeIMAP,
Name: "Email (IMAP)",
Description: "Sync email content from IMAP servers",
- Priority: 25,
+ Priority: 11,
AuthType: "password",
Capabilities: []string{},
},
@@ -370,7 +298,7 @@ var ConnectorMetadataRegistry = map[string]ConnectorMetadata{
Type: types.ConnectorTypeRSS,
Name: "RSS / Atom Feed",
Description: "Sync articles from RSS/Atom feeds",
- Priority: 9,
+ Priority: 12,
AuthType: "custom",
Capabilities: []string{"incremental"},
},
@@ -378,56 +306,30 @@ var ConnectorMetadataRegistry = map[string]ConnectorMetadata{
Type: types.ConnectorTypeGitLab,
Name: "GitLab",
Description: "Sync files from GitLab projects",
- Priority: 10,
+ Priority: 8,
AuthType: "token",
Capabilities: []string{"incremental", "hierarchical"},
},
}
-// ListAvailableConnectors returns the metadata of every known connector,
-// including ones not registered yet, with their credential forms, sorted by
-// priority and then type.
+// ListAvailableConnectors returns all available connector metadata
+// sorted by priority
func ListAvailableConnectors() []ConnectorMetadata {
metadata := make([]ConnectorMetadata, 0, len(ConnectorMetadataRegistry))
for _, meta := range ConnectorMetadataRegistry {
- metadata = append(metadata, withForm(meta))
+ metadata = append(metadata, meta)
}
- sort.Slice(metadata, func(i, j int) bool {
- if metadata[i].Priority != metadata[j].Priority {
- return metadata[i].Priority < metadata[j].Priority
- }
- return metadata[i].Type < metadata[j].Type
- })
- return metadata
-}
-// Metadata returns the metadata of the connectors registered here, in
-// ListAvailableConnectors order. A registered connector without an entry in
-// ConnectorMetadataRegistry is listed last under its type.
-func (r *ConnectorRegistry) Metadata() []ConnectorMetadata {
- r.mu.RLock()
- defer r.mu.RUnlock()
- out := make([]ConnectorMetadata, 0, len(r.connectors))
- seen := make(map[string]bool, len(r.connectors))
- for _, meta := range ListAvailableConnectors() {
- if _, ok := r.connectors[meta.Type]; ok {
- out = append(out, meta)
- seen[meta.Type] = true
+ // Sort by priority (insertion sort for simplicity)
+ for i := 1; i < len(metadata); i++ {
+ key := metadata[i]
+ j := i - 1
+ for j >= 0 && metadata[j].Priority > key.Priority {
+ metadata[j+1] = metadata[j]
+ j--
}
+ metadata[j+1] = key
}
- rest := make([]string, 0)
- for t := range r.connectors {
- if !seen[t] {
- rest = append(rest, t)
- }
- }
- sort.Strings(rest)
- for _, t := range rest {
- if meta, ok := r.plugins[t]; ok {
- out = append(out, meta)
- continue
- }
- out = append(out, ConnectorMetadata{Type: t, Name: t, Priority: math.MaxInt32})
- }
- return out
+
+ return metadata
}
diff --git a/internal/datasource/connector_forms.go b/internal/datasource/connector_forms.go
deleted file mode 100644
index 9542e0205..000000000
--- a/internal/datasource/connector_forms.go
+++ /dev/null
@@ -1,234 +0,0 @@
-package datasource
-
-import (
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-// connectorForm is what the data source editor needs to set one connector up:
-// the setup guide and the credential form. These lived in the frontend until
-// the editor moved to config schemas; keeping them next to the connector means
-// a new connector needs no frontend change.
-type connectorForm struct {
- docURL string
- permissionDocURL string
- permissionPageURL string
- requiredPermissions []string
- schema func() *configschema.Schema
-}
-
-// withForm fills a connector's setup guide and credential schema into its
-// metadata. Connectors without a form (not implemented yet) are unchanged.
-func withForm(meta ConnectorMetadata) ConnectorMetadata {
- form, ok := connectorForms[meta.Type]
- if !ok {
- return meta
- }
- meta.DocURL = form.docURL
- meta.PermissionDocURL = form.permissionDocURL
- meta.PermissionPageURL = form.permissionPageURL
- meta.RequiredPermissions = append([]string(nil), form.requiredPermissions...)
- meta.ConfigSchema = form.schema()
- return meta
-}
-
-// Scopes the Feishu / Lark apps must be granted. Lark is Feishu's
-// international cloud: same APIs and scope identifiers, separate console.
-var (
- feishuWikiPermissions = []string{
- "wiki:wiki:readonly", "drive:drive:readonly", "drive:export:readonly", "docx:document:readonly",
- }
- feishuDrivePermissions = []string{
- "drive:drive:readonly", "drive:export:readonly", "docx:document:readonly",
- }
-)
-
-var connectorForms = map[string]connectorForm{
- types.ConnectorTypeFeishu: {
- docURL: "https://open.feishu.cn/app",
- permissionDocURL: "https://open.feishu.cn/document/server-docs/docs/wiki-v2/wiki-overview",
- permissionPageURL: "https://open.feishu.cn/app",
- requiredPermissions: feishuWikiPermissions,
- schema: func() *configschema.Schema { return feishuSchema("https://open.feishu.cn") },
- },
- types.ConnectorTypeLark: {
- docURL: "https://open.larksuite.com/app",
- permissionDocURL: "https://open.larksuite.com/document/server-docs/docs/wiki-v2/wiki-overview",
- permissionPageURL: "https://open.larksuite.com/app",
- requiredPermissions: feishuWikiPermissions,
- schema: func() *configschema.Schema { return feishuSchema("https://open.larksuite.com") },
- },
- // Drive mode syncs a folder: no wiki scope needed.
- types.ConnectorTypeFeishuDrive: {
- docURL: "https://open.feishu.cn/app",
- permissionDocURL: "https://open.feishu.cn/document/server-docs/docs/drive-v1/file/list",
- permissionPageURL: "https://open.feishu.cn/app",
- requiredPermissions: feishuDrivePermissions,
- schema: func() *configschema.Schema { return feishuSchema("https://open.feishu.cn") },
- },
- types.ConnectorTypeLarkDrive: {
- docURL: "https://open.larksuite.com/app",
- permissionDocURL: "https://open.larksuite.com/document/server-docs/docs/drive-v1/file/list",
- permissionPageURL: "https://open.larksuite.com/app",
- requiredPermissions: feishuDrivePermissions,
- schema: func() *configschema.Schema { return feishuSchema("https://open.larksuite.com") },
- },
- types.ConnectorTypeNotion: {
- docURL: "https://www.notion.so/my-integrations",
- schema: func() *configschema.Schema {
- token := secretField("Integration Token", "datasource.field.integrationToken", "ntn_xxxx", 1)
- return configschema.Object().Set("api_key", token, true)
- },
- },
- types.ConnectorTypeConfluence: {
- docURL: "https://developer.atlassian.com/cloud/confluence/rest/",
- permissionDocURL: "https://developer.atlassian.com/cloud/confluence/rest/",
- permissionPageURL: "https://id.atlassian.com/manage-profile/security/api-tokens",
- schema: confluenceSchema,
- },
- types.ConnectorTypeYuque: {
- docURL: "https://www.yuque.com/yuque/developer/api",
- permissionDocURL: "https://www.yuque.com/yuque/developer/api",
- permissionPageURL: "https://www.yuque.com/settings/tokens",
- requiredPermissions: []string{"repo:read", "doc:read"},
- schema: func() *configschema.Schema {
- return configschema.Object().
- Set("api_token", secretField("API Token", "datasource.field.apiToken", "", 1), true).
- Set("base_url", baseURLField("https://www.yuque.com", 2), false)
- },
- },
- types.ConnectorTypeDingTalk: {
- docURL: "https://open.dingtalk.com/document/development/knowledge-base-overview",
- permissionDocURL: "https://open.dingtalk.com/document/development/get-knowledge-base-list",
- permissionPageURL: "https://open-dev.dingtalk.com/",
- requiredPermissions: []string{
- "Wiki.Workspace.Read", "Wiki.Node.Read", "Storage.File.Read",
- },
- schema: func() *configschema.Schema {
- operator := textField("Operator Union ID", "datasource.field.operatorId", "", 3)
- operator.Description = "Reads knowledge bases with this user's permissions."
- operator.I18nKeys["description"] = "datasource.field.operatorIdHint"
- return configschema.Object().
- Set("client_id", textField("Client ID", "datasource.field.clientId", "dingxxxxxxxx", 1), true).
- Set("client_secret", secretField("Client Secret", "datasource.field.clientSecret", "", 2), true).
- Set("operator_id", operator, true)
- },
- },
- // Tencent IMA: OpenAPI with two static headers, no OAuth.
- types.ConnectorTypeIMA: {
- docURL: "https://ima.qq.com/agent-interface",
- permissionDocURL: "https://ima.qq.com/agent-interface",
- permissionPageURL: "https://ima.qq.com/agent-interface",
- schema: func() *configschema.Schema {
- return configschema.Object().
- Set("client_id", secretField("IMA ClientID", "datasource.field.imaClientId", "", 1), true).
- Set("api_key", secretField("IMA APIKey", "datasource.field.imaApiKey", "", 2), true).
- Set("base_url", baseURLField("https://ima.qq.com", 3), false)
- },
- },
- types.ConnectorTypeRSS: {
- schema: func() *configschema.Schema {
- // "Name: Value" lines, edited as rows by the headers widget. Not
- // marked secret: the whole credential map is encrypted and never
- // returned, and the widget needs to read back what it wrote.
- headers := &configschema.Schema{
- Type: configschema.TypeString,
- Title: "Custom headers (optional)",
- Description: `For private feeds. One per line in "Name: Value" form.`,
- Widget: "headers",
- I18nKeys: map[string]string{
- "title": "datasource.field.authHeaders",
- "description": "datasource.field.authHeadersHint",
- },
- Order: 1,
- }
- return configschema.Object().Set("auth_headers", headers, false)
- },
- },
- types.ConnectorTypeGitLab: {
- schema: func() *configschema.Schema {
- baseURL := textField("GitLab URL", "datasource.gitlab.baseUrl", "https://gitlab.example.com", 1)
- return configschema.Object().
- Set("base_url", baseURL, true).
- Set("access_token", secretField("Personal access token", "datasource.gitlab.accessToken", "", 2), true)
- },
- },
-}
-
-func feishuSchema(defaultBaseURL string) *configschema.Schema {
- return configschema.Object().
- Set("app_id", textField("App ID", "datasource.field.appId", "cli_xxxx", 1), true).
- Set("app_secret", secretField("App Secret", "datasource.field.appSecret", "", 2), true).
- Set("base_url", baseURLField(defaultBaseURL, 3), false)
-}
-
-// confluenceSchema switches the secret with the edition: Server / Data Center
-// takes a password, Cloud an API token.
-func confluenceSchema() *configschema.Schema {
- edition := &configschema.Schema{
- Type: configschema.TypeString,
- Title: "Edition",
- Default: "server",
- Widget: "select",
- I18nKeys: map[string]string{"title": "datasource.field.confluenceEdition"},
- OneOf: []*configschema.Schema{
- {
- Const: "server", Title: "Server / Data Center",
- I18nKeys: map[string]string{"title": "datasource.field.confluenceEditionServer"},
- },
- {
- Const: "cloud", Title: "Cloud",
- I18nKeys: map[string]string{"title": "datasource.field.confluenceEditionCloud"},
- },
- },
- Order: 0,
- }
- password := secretField("Server/DC password", "datasource.field.confluencePassword", "Server/DC password", 4)
- password.VisibleIf = map[string]any{"edition": "server"}
- token := secretField("Cloud API token", "datasource.field.confluenceApiToken", "Cloud API token", 5)
- token.VisibleIf = map[string]any{"edition": "cloud"}
- return configschema.Object().
- Set("edition", edition, false).
- Set("base_url", textField(
- "Confluence URL", "datasource.field.confluenceBaseUrl",
- "https://confluence.example.com or https://team.atlassian.net/wiki", 1,
- ), true).
- Set("username", textField(
- "Username or email", "datasource.field.confluenceUsername", "name or email", 2,
- ), true).
- Set("password", password, true).
- Set("api_token", token, true)
-}
-
-// textField is a plain string input. An empty placeholder falls back to the
-// generic "Enter value" hint the editor always showed.
-func textField(title, titleKey, placeholder string, order int) *configschema.Schema {
- f := &configschema.Schema{
- Type: configschema.TypeString,
- Title: title,
- Placeholder: placeholder,
- I18nKeys: map[string]string{"title": titleKey},
- Order: order,
- }
- if placeholder == "" {
- f.Placeholder = "Enter value"
- f.I18nKeys["placeholder"] = "credential.inputPlaceholder"
- }
- return f
-}
-
-func secretField(title, titleKey, placeholder string, order int) *configschema.Schema {
- f := textField(title, titleKey, placeholder, order)
- f.Secret = true
- f.Widget = "password"
- return f
-}
-
-// baseURLField is the optional endpoint override for private deployments and
-// reverse proxies.
-func baseURLField(defaultURL string, order int) *configschema.Schema {
- f := textField("Base URL (optional)", "datasource.field.baseUrl", defaultURL, order)
- f.Description = "Leave empty to use the default public cloud address."
- f.I18nKeys["description"] = "datasource.field.baseUrlHint"
- return f
-}
diff --git a/internal/datasource/connector_forms_test.go b/internal/datasource/connector_forms_test.go
deleted file mode 100644
index c87c6990f..000000000
--- a/internal/datasource/connector_forms_test.go
+++ /dev/null
@@ -1,100 +0,0 @@
-package datasource
-
-import (
- "context"
- "strings"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-// implementedConnectors are the types container.initConnectorRegistry
-// registers; each needs a credential form.
-var implementedConnectors = []string{
- types.ConnectorTypeFeishu, types.ConnectorTypeLark, types.ConnectorTypeFeishuDrive,
- types.ConnectorTypeLarkDrive, types.ConnectorTypeNotion, types.ConnectorTypeConfluence,
- types.ConnectorTypeYuque, types.ConnectorTypeDingTalk, types.ConnectorTypeIMA,
- types.ConnectorTypeRSS, types.ConnectorTypeGitLab,
-}
-
-func TestEveryImplementedConnectorHasAValidForm(t *testing.T) {
- for _, typ := range implementedConnectors {
- if _, ok := ConnectorMetadataRegistry[typ]; !ok {
- t.Errorf("%s: no metadata", typ)
- continue
- }
- meta := withForm(ConnectorMetadataRegistry[typ])
- if meta.ConfigSchema == nil {
- t.Errorf("%s: no config schema", typ)
- continue
- }
- if err := meta.ConfigSchema.Check(); err != nil {
- t.Errorf("%s: invalid schema: %v", typ, err)
- }
- }
-}
-
-func TestConfluenceSchemaSwitchesSecretByEdition(t *testing.T) {
- s := confluenceSchema()
- base := map[string]any{"base_url": "https://c.example.com", "username": "bob"}
-
- server := map[string]any{"edition": "server", "base_url": base["base_url"], "username": "bob"}
- errs := configschema.Validate(s, server)
- if len(errs) != 1 || errs[0].Path != "password" {
- t.Fatalf("server edition must require the password only, got %v", errs)
- }
- cloud := map[string]any{"edition": "cloud", "base_url": base["base_url"], "username": "bob"}
- errs = configschema.Validate(s, cloud)
- if len(errs) != 1 || errs[0].Path != "api_token" {
- t.Fatalf("cloud edition must require the API token only, got %v", errs)
- }
- if got := s.SecretPaths(); strings.Join(got, ",") != "api_token,password" {
- t.Fatalf("secrets = %v", got)
- }
-}
-
-// fakeConnector is enough to register a type.
-type fakeConnector struct {
- Connector
- typ string
-}
-
-func (f fakeConnector) Type() string { return f.typ }
-
-func (fakeConnector) Validate(context.Context, *types.DataSourceConfig) error { return nil }
-
-func TestRegistryMetadataListsRegisteredOnly(t *testing.T) {
- r := NewConnectorRegistry()
- for _, typ := range []string{types.ConnectorTypeNotion, types.ConnectorTypeFeishu, "zz-custom"} {
- if err := r.Register(fakeConnector{typ: typ}); err != nil {
- t.Fatal(err)
- }
- }
- var got []string
- for _, m := range r.Metadata() {
- got = append(got, m.Type)
- }
- if strings.Join(got, ",") != "feishu,notion,zz-custom" {
- t.Fatalf("Metadata order = %v", got)
- }
- meta := r.Metadata()
- if meta[0].ConfigSchema == nil || len(meta[0].RequiredPermissions) == 0 || meta[0].DocURL == "" {
- t.Fatalf("feishu form missing: %+v", meta[0])
- }
- if meta[2].Name != "zz-custom" || meta[2].ConfigSchema != nil {
- t.Fatalf("unknown connector should fall back to its type: %+v", meta[2])
- }
-}
-
-func TestListAvailableConnectorsIsStable(t *testing.T) {
- first := ListAvailableConnectors()
- for range 5 {
- again := ListAvailableConnectors()
- for i := range first {
- if first[i].Type != again[i].Type {
- t.Fatalf("order changed at %d: %s vs %s", i, first[i].Type, again[i].Type)
- }
- }
- }
-}
diff --git a/internal/handler/datasource.go b/internal/handler/datasource.go
index d84aaad10..2f219d377 100644
--- a/internal/handler/datasource.go
+++ b/internal/handler/datasource.go
@@ -7,7 +7,6 @@ import (
"github.com/Tencent/WeKnora/internal/datasource"
"github.com/Tencent/WeKnora/internal/handler/dto"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/gin-gonic/gin"
@@ -15,22 +14,18 @@ import (
// DataSourceHandler handles HTTP requests for data source management
type DataSourceHandler struct {
- pluginGated
- service interfaces.DataSourceService
- kbService interfaces.KnowledgeBaseService
- connectors *datasource.ConnectorRegistry
+ service interfaces.DataSourceService
+ kbService interfaces.KnowledgeBaseService
}
// NewDataSourceHandler creates a new data source handler
func NewDataSourceHandler(
service interfaces.DataSourceService,
kbService interfaces.KnowledgeBaseService,
- connectors *datasource.ConnectorRegistry,
) *DataSourceHandler {
return &DataSourceHandler{
- service: service,
- kbService: kbService,
- connectors: connectors,
+ service: service,
+ kbService: kbService,
}
}
@@ -113,10 +108,6 @@ func (h *DataSourceHandler) CreateDataSource(c *gin.Context) {
c.JSON(status, gin.H{"error": msg})
return
}
- if !h.pluginFilter(c)(manifest.PointConnectors, req.Type) {
- c.JSON(http.StatusBadRequest, gin.H{"error": disabledIntegrationError})
- return
- }
// Enforce tenant isolation
req.TenantID = tenantID
@@ -326,18 +317,13 @@ func (h *DataSourceHandler) ValidateCredentials(c *gin.Context) {
var req struct {
Type string `json:"type" binding:"required"`
Credentials map[string]interface{} `json:"credentials" binding:"required"`
- Settings map[string]interface{} `json:"settings"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request: type and credentials are required"})
return
}
- if !h.pluginFilter(c)(manifest.PointConnectors, req.Type) {
- c.JSON(http.StatusBadRequest, gin.H{"error": disabledIntegrationError})
- return
- }
- if err := h.service.ValidateCredentials(ctx, req.Type, req.Credentials, req.Settings); err != nil {
+ if err := h.service.ValidateCredentials(ctx, req.Type, req.Credentials); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
@@ -621,18 +607,6 @@ func (h *DataSourceHandler) GetSyncLog(c *gin.Context) {
// @Success 200 {object} []datasource.ConnectorMetadata
// @Router /datasource/types [get]
func (h *DataSourceHandler) GetAvailableConnectors(c *gin.Context) {
- // Only connectors that can actually be created: the metadata table also
- // describes ones not implemented yet.
- if h.connectors == nil {
- c.JSON(http.StatusOK, []datasource.ConnectorMetadata{})
- return
- }
- enabled := h.pluginFilter(c)
- out := make([]datasource.ConnectorMetadata, 0)
- for _, meta := range h.connectors.Metadata() {
- if enabled(manifest.PointConnectors, meta.Type) {
- out = append(out, meta)
- }
- }
- c.JSON(http.StatusOK, out)
+ connectors := datasource.ListAvailableConnectors()
+ c.JSON(http.StatusOK, connectors)
}
diff --git a/internal/handler/datasource_schedule_test.go b/internal/handler/datasource_schedule_test.go
index d1e20ebe6..f03fcb230 100644
--- a/internal/handler/datasource_schedule_test.go
+++ b/internal/handler/datasource_schedule_test.go
@@ -44,7 +44,7 @@ func TestDataSourceScheduleAPIRejectsInvalidCron(t *testing.T) {
require.NoError(t, scheduler.AddOrUpdate(ds))
}
entries := scheduler.EntryCount()
- h := NewDataSourceHandler(svc, kbSvc, nil)
+ h := NewDataSourceHandler(svc, kbSvc)
router := newDataSourceTestRouter(h)
router.POST("/datasource", h.CreateDataSource)
router.PUT("/datasource/:id", h.UpdateDataSource)
diff --git a/internal/handler/datasource_test.go b/internal/handler/datasource_test.go
index b4e07068e..e632906d0 100644
--- a/internal/handler/datasource_test.go
+++ b/internal/handler/datasource_test.go
@@ -83,7 +83,7 @@ func TestDataSource_GetSyncLogs_ValidLimitWithinBounds(t *testing.T) {
return &types.KnowledgeBase{ID: "kb1", TenantID: 1}, nil
},
}
- h := NewDataSourceHandler(dsSvc, kbSvc, nil)
+ h := NewDataSourceHandler(dsSvc, kbSvc)
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/datasource/ds1/logs?limit=50&offset=25", nil)
@@ -116,7 +116,7 @@ func TestDataSource_GetSyncLogs_LimitExceedingMaximum(t *testing.T) {
return &types.KnowledgeBase{ID: "kb1", TenantID: 1}, nil
},
}
- h := NewDataSourceHandler(dsSvc, kbSvc, nil)
+ h := NewDataSourceHandler(dsSvc, kbSvc)
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/datasource/ds1/logs?limit=999", nil)
@@ -156,7 +156,7 @@ func TestDataSource_GetSyncLogs_MissingLimitDefaultsCorrectly(t *testing.T) {
return &types.KnowledgeBase{ID: "kb1", TenantID: 1}, nil
},
}
- h := NewDataSourceHandler(dsSvc, kbSvc, nil)
+ h := NewDataSourceHandler(dsSvc, kbSvc)
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/datasource/ds1/logs", nil)
@@ -189,7 +189,7 @@ func TestDataSource_GetSyncLogs_NonNumericLimitRejected(t *testing.T) {
return &types.KnowledgeBase{ID: "kb1", TenantID: 1}, nil
},
}
- h := NewDataSourceHandler(dsSvc, kbSvc, nil)
+ h := NewDataSourceHandler(dsSvc, kbSvc)
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/datasource/ds1/logs?limit=abc", nil)
@@ -216,7 +216,7 @@ func TestDataSource_GetSyncLogs_ZeroLimitRejected(t *testing.T) {
return &types.KnowledgeBase{ID: "kb1", TenantID: 1}, nil
},
}
- h := NewDataSourceHandler(dsSvc, kbSvc, nil)
+ h := NewDataSourceHandler(dsSvc, kbSvc)
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/datasource/ds1/logs?limit=0", nil)
@@ -243,7 +243,7 @@ func TestDataSource_GetSyncLogs_NegativeLimitRejected(t *testing.T) {
return &types.KnowledgeBase{ID: "kb1", TenantID: 1}, nil
},
}
- h := NewDataSourceHandler(dsSvc, kbSvc, nil)
+ h := NewDataSourceHandler(dsSvc, kbSvc)
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/datasource/ds1/logs?limit=-5", nil)
diff --git a/internal/handler/dto/mcp.go b/internal/handler/dto/mcp.go
index 558291281..1ac08fd53 100644
--- a/internal/handler/dto/mcp.go
+++ b/internal/handler/dto/mcp.go
@@ -36,12 +36,8 @@ type MCPServiceResponse struct {
StdioConfig *types.MCPStdioConfig `json:"stdio_config,omitempty"`
EnvVars types.MCPEnvVars `json:"env_vars,omitempty"`
IsBuiltin bool `json:"is_builtin"`
- // PluginID and PluginError describe a service an installed plugin
- // provides; see types.MCPService.
- PluginID string `json:"plugin_id,omitempty"`
- PluginError string `json:"plugin_error,omitempty"`
- CreatedAt time.Time `json:"created_at"`
- UpdatedAt time.Time `json:"updated_at"`
+ CreatedAt time.Time `json:"created_at"`
+ UpdatedAt time.Time `json:"updated_at"`
// Credentials is the per-field "configured?" map. Embedded on the main
// response so the credential UI doesn't need a follow-up GET. The
// frontend never sees the actual secret value — only whether one is
@@ -103,8 +99,6 @@ func NewMCPServiceResponse(ctx context.Context, svc *types.MCPService) *MCPServi
StdioConfig: svc.StdioConfig,
EnvVars: svc.EnvVars,
IsBuiltin: svc.IsBuiltin,
- PluginID: svc.PluginID,
- PluginError: svc.PluginError,
CreatedAt: svc.CreatedAt,
UpdatedAt: svc.UpdatedAt,
}
@@ -179,8 +173,6 @@ func NewSharedAgentMCPServiceResponses(svcs []*types.MCPService) []*MCPServiceRe
Enabled: s.Enabled,
TransportType: s.TransportType,
IsBuiltin: s.IsBuiltin,
- PluginID: s.PluginID,
- PluginError: s.PluginError,
})
}
return out
diff --git a/internal/handler/im.go b/internal/handler/im.go
index 8f8243f52..ad0d1810c 100644
--- a/internal/handler/im.go
+++ b/internal/handler/im.go
@@ -9,7 +9,6 @@ import (
"github.com/Tencent/WeKnora/internal/im"
"github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
"github.com/Tencent/WeKnora/internal/types"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
@@ -35,7 +34,6 @@ var invalidIMPlatformError = func() string {
// IMHandler handles IM platform callback requests and channel CRUD.
type IMHandler struct {
- pluginGated
imService *im.Service
}
@@ -46,57 +44,6 @@ func NewIMHandler(imService *im.Service) *IMHandler {
}
}
-// GetIMChannel godoc
-// @Summary 获取 IM 渠道
-// @Description 返回单个渠道供编辑:凭证中的密钥字段以 *** 代替
-// @Tags IM
-// @Produce json
-// @Param id path string true "渠道 ID"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Security ApiKeyAuth
-// @Router /im-channels/{id} [get]
-func (h *IMHandler) GetIMChannel(c *gin.Context) {
- tenantID, ok := c.Request.Context().Value(types.TenantIDContextKey).(uint64)
- if !ok {
- c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
- return
- }
- channel, err := h.imService.GetChannelByIDAndTenant(c.Param("id"), tenantID)
- if err != nil {
- c.JSON(http.StatusNotFound, gin.H{"error": "channel not found"})
- return
- }
- creds, err := im.RedactCredentials(channel.Platform, channel.Credentials)
- if err != nil {
- creds = map[string]any{}
- }
- c.JSON(http.StatusOK, gin.H{"data": struct {
- *im.IMChannel
- Credentials map[string]any `json:"credentials"`
- }{channel, creds}})
-}
-
-// ListIMPlatforms godoc
-// @Summary 列出 IM 平台
-// @Description 返回已注册的 IM 平台:支持的接入模式、控制台链接和凭证表单 Schema
-// @Tags IM
-// @Produce json
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Security ApiKeyAuth
-// @Router /im-channels/platforms [get]
-func (h *IMHandler) ListIMPlatforms(c *gin.Context) {
- enabled := h.pluginFilter(c)
- platforms := make([]im.PlatformInfo, 0)
- for _, p := range h.imService.PlatformInfos() {
- if enabled(manifest.PointIMChannels, p.ID) {
- platforms = append(platforms, p)
- }
- }
- c.JSON(http.StatusOK, gin.H{"success": true, "data": platforms})
-}
-
// ── Channel CRUD handlers ──
// CreateIMChannel creates a new IM channel for an agent.
@@ -129,10 +76,6 @@ func (h *IMHandler) CreateIMChannel(c *gin.Context) {
return
}
- if !h.pluginFilter(c)(manifest.PointIMChannels, req.Platform) {
- c.JSON(http.StatusBadRequest, gin.H{"error": disabledIntegrationError})
- return
- }
if !validIMPlatforms[req.Platform] {
c.JSON(http.StatusBadRequest, gin.H{"error": invalidIMPlatformError})
return
@@ -322,15 +265,7 @@ func (h *IMHandler) UpdateIMChannel(c *gin.Context) {
}
}
if req.Credentials != nil {
- // The editor loads credentials redacted (GetIMChannel), so merge
- // rather than replace: omitted keys and unchanged secrets keep their
- // stored values.
- merged, err := im.MergeCredentials(channel.Platform, channel.Credentials, req.Credentials)
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": "invalid credentials"})
- return
- }
- channel.Credentials = merged
+ channel.Credentials = req.Credentials
}
if req.Enabled != nil {
channel.Enabled = *req.Enabled
diff --git a/internal/handler/model.go b/internal/handler/model.go
index 690a31021..778546f5c 100644
--- a/internal/handler/model.go
+++ b/internal/handler/model.go
@@ -16,7 +16,6 @@ import (
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/models/api"
"github.com/Tencent/WeKnora/internal/models/chat"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
secutils "github.com/Tencent/WeKnora/internal/utils"
@@ -26,7 +25,6 @@ import (
// ModelHandler handles HTTP requests for model-related operations
// It implements the necessary methods to create, retrieve, update, and delete models
type ModelHandler struct {
- pluginGated
service interfaces.ModelService
}
@@ -100,10 +98,6 @@ func (h *ModelHandler) CreateModel(c *gin.Context) {
_ = c.Error(errors.NewBadRequestError(err.Error()))
return
}
- if p := req.Parameters.Provider; p != "" && !h.pluginFilter(c)(manifest.PointModelVendors, p) {
- _ = c.Error(errors.NewBadRequestError(disabledIntegrationError))
- return
- }
model := &types.Model{
TenantID: tenantID,
diff --git a/internal/handler/model_catalog.go b/internal/handler/model_catalog.go
index fcb1ec7fb..68ca7497c 100644
--- a/internal/handler/model_catalog.go
+++ b/internal/handler/model_catalog.go
@@ -13,8 +13,6 @@ import (
"github.com/Tencent/WeKnora/internal/models/api"
"github.com/Tencent/WeKnora/internal/models/providers"
modelruntime "github.com/Tencent/WeKnora/internal/models/runtime"
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
"github.com/Tencent/WeKnora/internal/types"
secutils "github.com/Tencent/WeKnora/internal/utils"
"github.com/gin-gonic/gin"
@@ -39,9 +37,6 @@ type ModelProviderDTO struct {
DefaultURLs map[string]string `json:"defaultUrls"`
ModelTypes []string `json:"modelTypes"`
ExtraFields []providers.ExtraField `json:"extraFields,omitempty"`
- // ConfigSchema describes ExtraFields as a config schema (the values of
- // extra_config). It carries the same fields; the editor moves to it.
- ConfigSchema *configschema.Schema `json:"configSchema,omitempty"`
// CredentialLabels rename the primary credential input for the model
// types that do not take a plain API key (signed rerank APIs).
CredentialLabels []providers.CredentialLabel `json:"credentialLabels,omitempty"`
@@ -129,9 +124,6 @@ func providerDTO(v *modelruntime.Provider, modelType types.ModelType, includeMod
CredentialLabels: v.CredentialLabels,
Order: v.Order,
}
- if len(v.ExtraFields) > 0 {
- dto.ConfigSchema = providers.ExtraFieldsSchema(v.ExtraFields)
- }
// Vendor-level thinking summary: resolve an unknown model so only the
// vendor defaults contribute.
if resolved, err := v.Resolve(
@@ -227,11 +219,7 @@ func (h *ModelHandler) ListModelProviders(c *gin.Context) {
// themselves (dto.NewModelResponse).
includeURLs := dto.CanViewIntegrationSecrets(ctx)
result := make([]ModelProviderDTO, 0, len(vendors))
- enabled := h.pluginFilter(c)
for _, v := range vendors {
- if !enabled(manifest.PointModelVendors, v.ID) {
- continue
- }
p := providerDTO(v, backendType, true)
if !includeURLs {
p.DefaultURLs = nil
diff --git a/internal/handler/plugin.go b/internal/handler/plugin.go
deleted file mode 100644
index 3ffac4e37..000000000
--- a/internal/handler/plugin.go
+++ /dev/null
@@ -1,306 +0,0 @@
-package handler
-
-import (
- stderrors "errors"
- "net/http"
-
- "github.com/gin-gonic/gin"
-
- "github.com/Tencent/WeKnora/internal/errors"
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
- "github.com/Tencent/WeKnora/internal/plugin/driver"
- "github.com/Tencent/WeKnora/internal/plugin/install"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/plugin/tenancy"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-// PluginHandler serves the read-only plugin catalog: every plugin this
-// process knows about and what each contributes. Builtins are listed like
-// any other plugin.
-type PluginHandler struct {
- registry *registry.Registry
- tenancy *tenancy.Service
- drivers *driver.Set
-}
-
-// NewPluginHandler creates a PluginHandler. Without a tenancy service every
-// plugin reads as enabled; without drivers no instance status is reported.
-func NewPluginHandler(registry *registry.Registry, tenancy *tenancy.Service, drivers *driver.Set) *PluginHandler {
- return &PluginHandler{registry: registry, tenancy: tenancy, drivers: drivers}
-}
-
-// PluginDetailDTO is one plugin with where and how its code runs.
-type PluginDetailDTO struct {
- tenancy.TenantPlugin
- Instances []driver.InstanceStatus `json:"instances"`
- // InstanceError explains an empty Instances (no driver, lookup failed).
- InstanceError string `json:"instanceError,omitempty"`
-}
-
-// tenantPlugins returns every plugin with the caller tenant's switch.
-func (h *PluginHandler) tenantPlugins(c *gin.Context) ([]tenancy.TenantPlugin, error) {
- tenantID := c.GetUint64(types.TenantIDContextKey.String())
- if h.tenancy == nil || tenantID == 0 {
- plugins := h.registry.Plugins()
- out := make([]tenancy.TenantPlugin, 0, len(plugins))
- for _, m := range plugins {
- out = append(out, tenancy.TenantPlugin{Manifest: m, Enabled: true})
- }
- return out, nil
- }
- return h.tenancy.List(c.Request.Context(), tenantID)
-}
-
-// PluginContributionDTO is one contribution with the plugin providing it.
-type PluginContributionDTO struct {
- manifest.Contribution
- PluginID string `json:"pluginId"`
- QualifiedID string `json:"qualifiedId"`
- // Version is the plugin's active version; page files are served under
- // it.
- Version string `json:"version,omitempty"`
- // Enabled reports whether the caller's tenant has the plugin enabled.
- Enabled bool `json:"enabled"`
-}
-
-// PluginContributionsDTO groups contributions by extension point.
-type PluginContributionsDTO struct {
- Points []manifest.PointInfo `json:"points"`
- Contributions map[manifest.Point][]PluginContributionDTO `json:"contributions"`
-}
-
-// ListPlugins godoc
-// @Summary 列出插件
-// @Description 列出当前进程已知的全部插件(含内置插件)、其贡献,以及当前空间是否启用
-// @Tags Plugin
-// @Produce json
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Security ApiKeyAuth
-// @Router /plugins [get]
-func (h *PluginHandler) ListPlugins(c *gin.Context) {
- plugins, err := h.tenantPlugins(c)
- if err != nil {
- _ = c.Error(errors.NewInternalServerError("failed to load plugin settings"))
- return
- }
- c.JSON(http.StatusOK, gin.H{"success": true, "data": plugins})
-}
-
-// GetPlugin godoc
-// @Summary 获取插件详情
-// @Tags Plugin
-// @Produce json
-// @Param id path string true "插件 ID,如 weknora.feishu"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Security ApiKeyAuth
-// @Router /plugins/{id} [get]
-func (h *PluginHandler) GetPlugin(c *gin.Context) {
- plugins, err := h.tenantPlugins(c)
- if err != nil {
- _ = c.Error(errors.NewInternalServerError("failed to load plugin settings"))
- return
- }
- for _, p := range plugins {
- if p.Manifest.ID == c.Param("id") {
- c.JSON(http.StatusOK, gin.H{"success": true, "data": h.detail(c, p)})
- return
- }
- }
- _ = c.Error(errors.NewNotFoundError("plugin not found"))
-}
-
-func (h *PluginHandler) detail(c *gin.Context, p tenancy.TenantPlugin) PluginDetailDTO {
- out := PluginDetailDTO{TenantPlugin: p, Instances: []driver.InstanceStatus{}}
- if h.drivers == nil {
- return out
- }
- d, err := h.drivers.For(p.Manifest.Runtime.Type)
- if err == nil {
- out.Instances, err = d.Status(c.Request.Context(), p.Manifest.ID)
- }
- if err != nil {
- out.InstanceError = err.Error()
- out.Instances = []driver.InstanceStatus{}
- }
- return out
-}
-
-// SetPluginEnabledRequest turns a plugin on or off for the workspace.
-type SetPluginEnabledRequest struct {
- Enabled *bool `json:"enabled" binding:"required"`
-}
-
-// SetPluginEnabled godoc
-// @Summary 启用或停用插件
-// @Description 为当前空间启用或停用插件;停用后其贡献不再出现在类型列表中、不能新建实例,已有实例不受影响。必需插件不能停用
-// @Tags Plugin
-// @Accept json
-// @Produce json
-// @Param id path string true "插件 ID"
-// @Param request body SetPluginEnabledRequest true "开关"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Security ApiKeyAuth
-// @Router /plugins/{id}/enabled [put]
-func (h *PluginHandler) SetPluginEnabled(c *gin.Context) {
- var req SetPluginEnabledRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- _ = c.Error(errors.NewBadRequestError("enabled is required"))
- return
- }
- tenantID := c.GetUint64(types.TenantIDContextKey.String())
- if h.tenancy == nil || tenantID == 0 {
- _ = c.Error(errors.NewBadRequestError("workspace context missing"))
- return
- }
- userID, _ := c.Request.Context().Value(types.UserIDContextKey).(string)
- err := h.tenancy.SetEnabled(c.Request.Context(), tenantID, c.Param("id"), *req.Enabled, userID)
- switch {
- case stderrors.Is(err, tenancy.ErrUnknownPlugin):
- _ = c.Error(errors.NewNotFoundError("plugin not found"))
- return
- case stderrors.Is(err, tenancy.ErrRequiredPlugin):
- _ = c.Error(errors.NewBadRequestError(err.Error()))
- return
- case err != nil:
- _ = c.Error(errors.NewInternalServerError("failed to save plugin setting"))
- return
- }
- h.GetPlugin(c)
-}
-
-// ListContributions godoc
-// @Summary 按扩展点列出贡献
-// @Description 返回各扩展点下的全部贡献;可用 point 参数只取一个扩展点
-// @Tags Plugin
-// @Produce json
-// @Param point query string false "扩展点,如 connectors"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Security ApiKeyAuth
-// @Router /plugins/contributions [get]
-func (h *PluginHandler) ListContributions(c *gin.Context) {
- points := manifest.Points()
- if p := c.Query("point"); p != "" {
- info, ok := manifest.LookupPoint(manifest.Point(p))
- if !ok {
- _ = c.Error(errors.NewBadRequestError("unknown extension point"))
- return
- }
- points = []manifest.PointInfo{info}
- }
- out := PluginContributionsDTO{
- Points: points,
- Contributions: make(map[manifest.Point][]PluginContributionDTO, len(points)),
- }
- enabled := map[string]bool{}
- plugins, err := h.tenantPlugins(c)
- if err != nil {
- _ = c.Error(errors.NewInternalServerError("failed to load plugin settings"))
- return
- }
- for _, p := range plugins {
- enabled[p.Manifest.ID] = p.Enabled
- }
- for _, info := range points {
- entries := h.registry.Contributions(info.Point)
- list := make([]PluginContributionDTO, 0, len(entries))
- for _, e := range entries {
- dto := PluginContributionDTO{
- Contribution: e.Contribution,
- PluginID: e.PluginID,
- QualifiedID: e.QualifiedID,
- Enabled: enabled[e.PluginID],
- }
- if m, ok := h.registry.Plugin(e.PluginID); ok {
- dto.Version = m.Version
- }
- list = append(list, dto)
- }
- out.Contributions[info.Point] = list
- }
- c.JSON(http.StatusOK, gin.H{"success": true, "data": out})
-}
-
-// configError maps a plugin configuration failure to an HTTP error.
-func configError(c *gin.Context, err error) {
- var fields configschema.FieldErrors
- switch {
- case stderrors.As(err, &fields):
- _ = c.Error(errors.NewBadRequestError(err.Error()).WithDetails(fields))
- case stderrors.Is(err, tenancy.ErrUnknownPlugin), stderrors.Is(err, install.ErrNotInstalled):
- _ = c.Error(errors.NewNotFoundError("plugin not found"))
- case stderrors.Is(err, tenancy.ErrNoTenantConfig), stderrors.Is(err, install.ErrNoSystemConfig):
- _ = c.Error(errors.NewBadRequestError(err.Error()))
- default:
- logger.Errorf(c.Request.Context(), "[plugin] configuration request failed: %v", err)
- _ = c.Error(errors.NewInternalServerError("failed to handle plugin configuration"))
- }
-}
-
-// PluginConfigRequest carries configuration values; secrets may be sent back
-// as "***" to keep them.
-type PluginConfigRequest struct {
- Values map[string]any `json:"values"`
-}
-
-// GetPluginConfig godoc
-// @Summary 获取插件的空间配置
-// @Description 返回插件的空间级配置 Schema 和当前值(密钥脱敏为 ***)
-// @Tags Plugin
-// @Produce json
-// @Param id path string true "插件 ID"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Security ApiKeyAuth
-// @Router /plugins/{id}/config [get]
-func (h *PluginHandler) GetPluginConfig(c *gin.Context) {
- tenantID := c.GetUint64(types.TenantIDContextKey.String())
- if h.tenancy == nil || tenantID == 0 {
- _ = c.Error(errors.NewBadRequestError("workspace context missing"))
- return
- }
- cfg, err := h.tenancy.Config(c.Request.Context(), tenantID, c.Param("id"))
- if err != nil {
- configError(c, err)
- return
- }
- c.JSON(http.StatusOK, gin.H{"success": true, "data": cfg})
-}
-
-// UpdatePluginConfig godoc
-// @Summary 保存插件的空间配置
-// @Description 按插件声明的 Schema 校验并保存空间级配置;密钥加密存储,回传 *** 表示保持不变
-// @Tags Plugin
-// @Accept json
-// @Produce json
-// @Param id path string true "插件 ID"
-// @Param request body PluginConfigRequest true "配置"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Security ApiKeyAuth
-// @Router /plugins/{id}/config [put]
-func (h *PluginHandler) UpdatePluginConfig(c *gin.Context) {
- var req PluginConfigRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- _ = c.Error(errors.NewBadRequestError("values are required"))
- return
- }
- tenantID := c.GetUint64(types.TenantIDContextKey.String())
- if h.tenancy == nil || tenantID == 0 {
- _ = c.Error(errors.NewBadRequestError("workspace context missing"))
- return
- }
- userID, _ := c.Request.Context().Value(types.UserIDContextKey).(string)
- cfg, err := h.tenancy.SetConfig(c.Request.Context(), tenantID, c.Param("id"), req.Values, userID)
- if err != nil {
- configError(c, err)
- return
- }
- c.JSON(http.StatusOK, gin.H{"success": true, "data": cfg})
-}
diff --git a/internal/handler/plugin_admin.go b/internal/handler/plugin_admin.go
deleted file mode 100644
index 7971c2bc2..000000000
--- a/internal/handler/plugin_admin.go
+++ /dev/null
@@ -1,354 +0,0 @@
-package handler
-
-import (
- stderrors "errors"
- "io"
- "net/http"
- "strings"
-
- "github.com/gin-gonic/gin"
-
- "github.com/Tencent/WeKnora/internal/errors"
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/install"
- "github.com/Tencent/WeKnora/internal/plugin/pkg"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-// PluginAdminHandler lets system administrators install and manage plugins
-// for the whole platform.
-type PluginAdminHandler struct {
- service *install.Service
-}
-
-// NewPluginAdminHandler creates a PluginAdminHandler.
-func NewPluginAdminHandler(service *install.Service) *PluginAdminHandler {
- return &PluginAdminHandler{service: service}
-}
-
-// PluginPackageRequest locates a package by URL; uploads send the archive as
-// the multipart "file" field and the digest as a form field instead.
-type PluginPackageRequest struct {
- URL string `json:"url"`
- // Digest pins an install to the package reviewed with inspect.
- Digest string `json:"digest"`
- // RemoteURL is where a remote plugin's service runs.
- RemoteURL string `json:"remote_url"`
-}
-
-// packageInput is a package with what its install request said about it.
-type packageInput struct {
- data []byte
- source install.Source
- digest string
- remoteURL string
-}
-
-// readPackage returns the package archive from an upload or a URL.
-func (h *PluginAdminHandler) readPackage(c *gin.Context) (packageInput, bool) {
- if strings.HasPrefix(c.ContentType(), "application/json") {
- limitJSONBody(c, skillSourceJSONMaxBytes)
- var req PluginPackageRequest
- if err := c.ShouldBindJSON(&req); err != nil || strings.TrimSpace(req.URL) == "" {
- _ = c.Error(errors.NewBadRequestError("url or an uploaded file is required"))
- return packageInput{}, false
- }
- data, err := h.service.FetchURL(c.Request.Context(), strings.TrimSpace(req.URL))
- if err != nil {
- h.fail(c, err)
- return packageInput{}, false
- }
- return packageInput{
- data: data, source: install.Source{Kind: "url", URL: strings.TrimSpace(req.URL)},
- digest: req.Digest, remoteURL: strings.TrimSpace(req.RemoteURL),
- }, true
- }
-
- limitUploadBody(c, pkg.MaxArchiveBytes)
- file, header, err := c.Request.FormFile("file")
- if err != nil {
- if isRequestBodyTooLarge(err) {
- _ = c.Error(errors.NewBadRequestError("plugin package is too large"))
- } else {
- _ = c.Error(errors.NewBadRequestError("file is required"))
- }
- return packageInput{}, false
- }
- defer func() { _ = file.Close() }()
- data, err := io.ReadAll(io.LimitReader(file, pkg.MaxArchiveBytes+1))
- if err != nil {
- _ = c.Error(errors.NewBadRequestError("failed to read the uploaded package"))
- return packageInput{}, false
- }
- if len(data) > pkg.MaxArchiveBytes {
- _ = c.Error(errors.NewBadRequestError("plugin package is too large"))
- return packageInput{}, false
- }
- return packageInput{
- data: data, source: install.Source{Kind: "upload", URL: header.Filename},
- digest: c.PostForm("digest"), remoteURL: strings.TrimSpace(c.PostForm("remote_url")),
- }, true
-}
-
-func (h *PluginAdminHandler) fail(c *gin.Context, err error) {
- var invalid *install.InvalidError
- switch {
- case stderrors.As(err, &invalid):
- _ = c.Error(errors.NewBadRequestError(err.Error()))
- case stderrors.Is(err, install.ErrNotInstalled):
- _ = c.Error(errors.NewNotFoundError("plugin is not installed"))
- default:
- logger.Errorf(c.Request.Context(), "[plugin] admin request failed: %v", err)
- _ = c.Error(errors.NewInternalServerError("plugin operation failed"))
- }
-}
-
-func (h *PluginAdminHandler) ok(c *gin.Context, data any) {
- c.JSON(http.StatusOK, gin.H{"success": true, "data": data})
-}
-
-// InspectPlugin godoc
-// @Summary 检查插件包
-// @Description 解析上传的插件包(multipart file)或 URL(JSON {url}),返回清单、摘要和安装后的变化,不做任何修改
-// @Tags System
-// @Accept multipart/form-data,json
-// @Produce json
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Router /system/admin/plugins/inspect [post]
-func (h *PluginAdminHandler) InspectPlugin(c *gin.Context) {
- in, ok := h.readPackage(c)
- if !ok {
- return
- }
- preview, err := h.service.Inspect(c.Request.Context(), in.data)
- if err != nil {
- h.fail(c, err)
- return
- }
- h.ok(c, preview)
-}
-
-// InstallPlugin godoc
-// @Summary 安装或升级插件
-// @Description 安装插件包(multipart file + digest,或 JSON {url, digest})。digest 取自 inspect,
-// @Description 保证安装的就是审阅过的包。安装后全平台可用,各空间需自行启用。
-// @Description remote 插件还需 remote_url(服务地址);首次安装的响应里 issuedSecret 是签名密钥,只返回这一次
-// @Tags System
-// @Accept multipart/form-data,json
-// @Produce json
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Router /system/admin/plugins [post]
-func (h *PluginAdminHandler) InstallPlugin(c *gin.Context) {
- in, ok := h.readPackage(c)
- if !ok {
- return
- }
- userID, _ := c.Request.Context().Value(types.UserIDContextKey).(string)
- view, err := h.service.Install(c.Request.Context(), install.Request{
- Data: in.data, Source: in.source, ExpectedDigest: in.digest, UserID: userID, RemoteURL: in.remoteURL,
- })
- if err != nil {
- h.fail(c, err)
- return
- }
- h.ok(c, view)
-}
-
-// ListInstalledPlugins godoc
-// @Summary 列出已安装插件
-// @Description 列出系统管理员安装的插件(不含内置插件),含版本和各节点加载状态
-// @Tags System
-// @Produce json
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Router /system/admin/plugins [get]
-func (h *PluginAdminHandler) ListInstalledPlugins(c *gin.Context) {
- views, err := h.service.List(c.Request.Context())
- if err != nil {
- h.fail(c, err)
- return
- }
- h.ok(c, views)
-}
-
-// GetInstalledPlugin godoc
-// @Summary 获取已安装插件
-// @Tags System
-// @Produce json
-// @Param id path string true "插件 ID"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Router /system/admin/plugins/{id} [get]
-func (h *PluginAdminHandler) GetInstalledPlugin(c *gin.Context) {
- view, err := h.service.Get(c.Request.Context(), c.Param("id"))
- if err != nil {
- h.fail(c, err)
- return
- }
- h.ok(c, view)
-}
-
-// SetInstalledPluginEnabled godoc
-// @Summary 全平台启用或停用插件
-// @Description 停用后所有节点卸载该插件;各空间的开关和配置保留
-// @Tags System
-// @Accept json
-// @Produce json
-// @Param id path string true "插件 ID"
-// @Param request body SetPluginEnabledRequest true "开关"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Router /system/admin/plugins/{id}/enabled [put]
-func (h *PluginAdminHandler) SetInstalledPluginEnabled(c *gin.Context) {
- var req SetPluginEnabledRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- _ = c.Error(errors.NewBadRequestError("enabled is required"))
- return
- }
- view, err := h.service.SetEnabled(c.Request.Context(), c.Param("id"), *req.Enabled)
- if err != nil {
- h.fail(c, err)
- return
- }
- h.ok(c, view)
-}
-
-// ActivatePluginVersionRequest picks a stored version.
-type ActivatePluginVersionRequest struct {
- Version string `json:"version" binding:"required"`
-}
-
-// ActivatePluginVersion godoc
-// @Summary 切换插件版本
-// @Description 把已存储的某个版本设为当前版本(回滚或重新升级)
-// @Tags System
-// @Accept json
-// @Produce json
-// @Param id path string true "插件 ID"
-// @Param request body ActivatePluginVersionRequest true "版本"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Router /system/admin/plugins/{id}/active-version [put]
-func (h *PluginAdminHandler) ActivatePluginVersion(c *gin.Context) {
- var req ActivatePluginVersionRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- _ = c.Error(errors.NewBadRequestError("version is required"))
- return
- }
- view, err := h.service.Activate(c.Request.Context(), c.Param("id"), req.Version)
- if err != nil {
- h.fail(c, err)
- return
- }
- h.ok(c, view)
-}
-
-// SetPluginRemoteURLRequest moves a remote plugin.
-type SetPluginRemoteURLRequest struct {
- URL string `json:"url" binding:"required"`
-}
-
-// SetPluginRemoteURL godoc
-// @Summary 修改远程插件的服务地址
-// @Description 私有网络中的地址需要加入 SSRF_WHITELIST
-// @Tags System
-// @Accept json
-// @Produce json
-// @Param id path string true "插件 ID"
-// @Param request body SetPluginRemoteURLRequest true "服务地址"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Router /system/admin/plugins/{id}/remote-url [put]
-func (h *PluginAdminHandler) SetPluginRemoteURL(c *gin.Context) {
- var req SetPluginRemoteURLRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- _ = c.Error(errors.NewBadRequestError("url is required"))
- return
- }
- view, err := h.service.SetRemoteURL(c.Request.Context(), c.Param("id"), strings.TrimSpace(req.URL))
- if err != nil {
- h.fail(c, err)
- return
- }
- h.ok(c, view)
-}
-
-// RotatePluginSecret godoc
-// @Summary 轮换远程插件的签名密钥
-// @Description 新密钥只在本次响应的 issuedSecret 中返回;服务换上新密钥前调用会失败
-// @Tags System
-// @Produce json
-// @Param id path string true "插件 ID"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Router /system/admin/plugins/{id}/secret/rotate [post]
-func (h *PluginAdminHandler) RotatePluginSecret(c *gin.Context) {
- view, err := h.service.RotateSecret(c.Request.Context(), c.Param("id"))
- if err != nil {
- h.fail(c, err)
- return
- }
- h.ok(c, view)
-}
-
-// UninstallPlugin godoc
-// @Summary 卸载插件
-// @Description 删除插件及其全部版本;各空间的开关和配置保留,重新安装后恢复
-// @Tags System
-// @Produce json
-// @Param id path string true "插件 ID"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Router /system/admin/plugins/{id} [delete]
-func (h *PluginAdminHandler) UninstallPlugin(c *gin.Context) {
- if err := h.service.Uninstall(c.Request.Context(), c.Param("id")); err != nil {
- h.fail(c, err)
- return
- }
- c.JSON(http.StatusOK, gin.H{"success": true})
-}
-
-// GetPluginSystemConfig godoc
-// @Summary 获取插件的平台配置
-// @Description 返回插件的平台级配置 Schema 和当前值(密钥脱敏为 ***)
-// @Tags System
-// @Produce json
-// @Param id path string true "插件 ID"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Router /system/admin/plugins/{id}/config [get]
-func (h *PluginAdminHandler) GetPluginSystemConfig(c *gin.Context) {
- cfg, err := h.service.GetSystemConfig(c.Request.Context(), c.Param("id"))
- if err != nil {
- configError(c, err)
- return
- }
- h.ok(c, cfg)
-}
-
-// UpdatePluginSystemConfig godoc
-// @Summary 保存插件的平台配置
-// @Description 按插件声明的 Schema 校验并保存平台级配置,对所有空间生效
-// @Tags System
-// @Accept json
-// @Produce json
-// @Param id path string true "插件 ID"
-// @Param request body PluginConfigRequest true "配置"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Router /system/admin/plugins/{id}/config [put]
-func (h *PluginAdminHandler) UpdatePluginSystemConfig(c *gin.Context) {
- var req PluginConfigRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- _ = c.Error(errors.NewBadRequestError("values are required"))
- return
- }
- cfg, err := h.service.SetSystemConfig(c.Request.Context(), c.Param("id"), req.Values)
- if err != nil {
- configError(c, err)
- return
- }
- h.ok(c, cfg)
-}
diff --git a/internal/handler/plugin_forms.go b/internal/handler/plugin_forms.go
deleted file mode 100644
index 72c3254ad..000000000
--- a/internal/handler/plugin_forms.go
+++ /dev/null
@@ -1,366 +0,0 @@
-package handler
-
-import (
- "encoding/json"
- stderrors "errors"
- "html/template"
- "net/http"
- "net/url"
- "os"
- "strings"
-
- "github.com/gin-gonic/gin"
-
- "github.com/Tencent/WeKnora/internal/errors"
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/activate"
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
- "github.com/Tencent/WeKnora/internal/plugin/install"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- pluginoauth "github.com/Tencent/WeKnora/internal/plugin/oauth"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/plugin/tenancy"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// PluginFormsHandler serves what plugin forms need from the plugin while
-// they are being filled: dynamic choices (x-options) and OAuth connections
-// (x-oauth).
-type PluginFormsHandler struct {
- registry *registry.Registry
- tenancy *tenancy.Service
- invoker *activate.Invoker
- plugins interfaces.PluginRepository
- dataSources interfaces.DataSourceRepository
- webSearch interfaces.WebSearchProviderRepository
- oauth *pluginoauth.Service
-}
-
-// NewPluginFormsHandler creates the handler.
-func NewPluginFormsHandler(
- reg *registry.Registry, t *tenancy.Service, iv *activate.Invoker, plugins interfaces.PluginRepository,
- dataSources interfaces.DataSourceRepository, webSearch interfaces.WebSearchProviderRepository,
- oauth *pluginoauth.Service,
-) *PluginFormsHandler {
- return &PluginFormsHandler{
- registry: reg, tenancy: t, invoker: iv, plugins: plugins, dataSources: dataSources, webSearch: webSearch,
- oauth: oauth,
- }
-}
-
-// PluginOptionsRequest asks a plugin for a field's choices.
-type PluginOptionsRequest struct {
- Name string `json:"name" binding:"required"`
- Field string `json:"field" binding:"required"`
- // Scope is system, tenant or instance.
- Scope string `json:"scope" binding:"required"`
- // Contribution is "<point>/<id>" of the instance ("connectors/jira").
- Contribution string `json:"contribution"`
- // InstanceID is the instance being edited; its stored secrets fill in
- // the fields the form did not change.
- InstanceID string `json:"instanceId"`
- // Values are what the form holds now, in the scope's shape.
- Values map[string]any `json:"values"`
- Query string `json:"query"`
-}
-
-// Options godoc
-// @Summary 插件表单的动态选项
-// @Description 用表单当前的值(未修改的密钥用已存的值补齐)向插件取某个字段的可选项(x-options)
-// @Tags Plugins
-// @Accept json
-// @Produce json
-// @Param id path string true "插件 ID"
-// @Param request body PluginOptionsRequest true "请求"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Router /plugins/{id}/options [post]
-func (h *PluginFormsHandler) Options(c *gin.Context) {
- limitJSONBody(c, 1<<20)
- var req PluginOptionsRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- _ = c.Error(errors.NewBadRequestError("name, field and scope are required"))
- return
- }
- ctx := c.Request.Context()
- m, ok := h.codePlugin(c, c.Param("id"), req.Scope)
- if !ok {
- return
- }
- tenantID := c.GetUint64(types.TenantIDContextKey.String())
- values := req.Values
- if values == nil {
- values = map[string]any{}
- }
- var override activate.ConfigOverride
- var instance map[string]any
- switch req.Scope {
- case pluginapi.OptionsScopeSystem:
- stored, _, err := install.OpenSystemConfig(ctx, h.plugins, m)
- if err != nil {
- h.fail(c, err)
- return
- }
- override.System = mergeForm(m.Config.SystemSchema, stored, values)
- case pluginapi.OptionsScopeTenant:
- stored, _, err := h.tenancy.OpenConfig(ctx, tenantID, m.ID)
- if err != nil {
- h.fail(c, err)
- return
- }
- override.Tenant = mergeForm(m.Config.TenantSchema, stored, values)
- case pluginapi.OptionsScopeInstance:
- var err error
- if instance, err = h.instanceValues(c, tenantID, req, values); err != nil {
- h.fail(c, err)
- return
- }
- default:
- _ = c.Error(errors.NewBadRequestError("scope must be system, tenant or instance"))
- return
- }
- in := pluginapi.OptionsInput{Field: req.Field, Scope: req.Scope, Contribution: req.Contribution, Query: req.Query}
- var out pluginapi.OptionsOutput
- err := h.invoker.CallOverriding(ctx, m, pluginapi.OptionsPath(req.Name), instance, override, in, &out)
- if err != nil {
- h.fail(c, err)
- return
- }
- if out.Options == nil {
- out.Options = []pluginapi.Option{}
- }
- c.JSON(http.StatusOK, gin.H{"success": true, "data": out})
-}
-
-// codePlugin finds a plugin with code the caller may ask for this scope.
-func (h *PluginFormsHandler) codePlugin(c *gin.Context, id, scope string) (*manifest.Manifest, bool) {
- ctx := c.Request.Context()
- m, ok := h.registry.Plugin(id)
- if !ok || (m.Runtime.Type != manifest.RuntimeHost && m.Runtime.Type != manifest.RuntimeRemote) {
- _ = c.Error(errors.NewNotFoundError("no such plugin with code"))
- return nil, false
- }
- if scope == pluginapi.OptionsScopeSystem {
- if !types.IsSystemAdminFromContext(ctx) {
- c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "Forbidden: system administrators only"})
- return nil, false
- }
- return m, true
- }
- tenantID := c.GetUint64(types.TenantIDContextKey.String())
- if on, err := h.tenancy.PluginEnabled(ctx, tenantID, id); err != nil || !on {
- _ = c.Error(errors.NewNotFoundError("the plugin is not enabled in this workspace"))
- return nil, false
- }
- return m, true
-}
-
-// mergeForm fills the fields a form left redacted ("***") or empty with the
-// stored values.
-func mergeForm(rawSchema json.RawMessage, stored, values map[string]any) map[string]any {
- if len(rawSchema) == 0 {
- return values
- }
- schema, err := configschema.Parse(rawSchema)
- if err != nil {
- return values
- }
- return configschema.Merge(schema, stored, values)
-}
-
-// instanceValues is the instance the form holds, with the stored instance's
-// secrets filled in where the form has none (forms never receive secrets).
-func (h *PluginFormsHandler) instanceValues(
- c *gin.Context, tenantID uint64, req PluginOptionsRequest, values map[string]any,
-) (map[string]any, error) {
- point, _, _ := strings.Cut(req.Contribution, "/")
- switch manifest.Point(point) {
- case manifest.PointConnectors:
- creds, _ := values["credentials"].(map[string]any)
- if creds == nil {
- creds = map[string]any{}
- }
- if req.InstanceID != "" {
- ds, err := h.dataSources.FindByID(c.Request.Context(), req.InstanceID)
- if err != nil || ds == nil || ds.TenantID != tenantID {
- return nil, errInstanceNotFound
- }
- cfg, err := ds.ParseConfig()
- if err != nil {
- return nil, err
- }
- for k, v := range cfg.Credentials {
- if s, _ := creds[k].(string); s == "" || s == types.RedactedSecretPlaceholder {
- creds[k] = v
- }
- }
- }
- out := map[string]any{
- "credentials": creds,
- "settings": values["settings"],
- "resourceIds": values["resourceIds"],
- }
- if out["settings"] == nil {
- out["settings"] = map[string]any{}
- }
- return out, nil
- case manifest.PointWebSearch:
- out := map[string]any{}
- for k, v := range values {
- out[k] = v
- }
- if key, _ := out["api_key"].(string); req.InstanceID != "" &&
- (key == "" || key == types.RedactedSecretPlaceholder) {
- p, err := h.webSearch.GetByID(c.Request.Context(), tenantID, req.InstanceID)
- if err != nil || p == nil {
- return nil, errInstanceNotFound
- }
- out["api_key"] = p.Parameters.APIKey
- }
- return out, nil
- default:
- return values, nil
- }
-}
-
-var errInstanceNotFound = stderrors.New("instance not found")
-
-// fail maps a plugin form failure: the plugin's own field problems go back
-// to the form as they are.
-func (h *PluginFormsHandler) fail(c *gin.Context, err error) {
- if stderrors.Is(err, errInstanceNotFound) {
- _ = c.Error(errors.NewNotFoundError("instance not found"))
- return
- }
- if pe, ok := pluginapi.AsError(err); ok {
- switch pe.Code {
- case pluginapi.CodeInvalidConfig, pluginapi.CodeUnauthorized, pluginapi.CodeBadRequest:
- body := gin.H{"success": false, "error": gin.H{"code": string(pe.Code), "message": pe.Message}}
- if pe.Details != nil && len(pe.Details.Fields) > 0 {
- body["error"].(gin.H)["fields"] = pe.Details.Fields
- }
- c.JSON(http.StatusBadRequest, body)
- return
- case pluginapi.CodeNotFound:
- _ = c.Error(errors.NewNotFoundError(pe.Message))
- return
- }
- }
- logger.Warnf(c.Request.Context(), "[plugin] form request to %s failed: %v", c.Param("id"), err)
- c.JSON(http.StatusBadGateway, gin.H{"success": false, "error": gin.H{"message": err.Error()}})
-}
-
-// PluginOAuthStartRequest starts connecting an x-oauth field.
-type PluginOAuthStartRequest struct {
- Scope string `json:"scope" binding:"required"`
- Contribution string `json:"contribution"`
- Field string `json:"field" binding:"required"`
-}
-
-// OAuthStart godoc
-// @Summary 开始插件字段的 OAuth 授权
-// @Description 为 x-oauth 字段生成授权地址;授权完成后回调页把连接 ID 交回表单,令牌只保存在服务端
-// @Tags Plugins
-// @Accept json
-// @Produce json
-// @Param id path string true "插件 ID"
-// @Param request body PluginOAuthStartRequest true "字段"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Router /plugins/{id}/oauth/start [post]
-func (h *PluginFormsHandler) OAuthStart(c *gin.Context) {
- var req PluginOAuthStartRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- _ = c.Error(errors.NewBadRequestError("scope and field are required"))
- return
- }
- m, ok := h.codePlugin(c, c.Param("id"), req.Scope)
- if !ok {
- return
- }
- target := pluginoauth.Target{
- PluginID: m.ID, Scope: req.Scope, Contribution: req.Contribution, Field: req.Field,
- }
- if req.Scope != pluginapi.OptionsScopeSystem {
- target.TenantID = c.GetUint64(types.TenantIDContextKey.String())
- }
- userID, _ := types.UserIDFromContext(c.Request.Context())
- origin, base := appOrigin(c)
- url, state, err := h.oauth.Start(c.Request.Context(), target, userID, origin, base)
- if err != nil {
- _ = c.Error(errors.NewBadRequestError(err.Error()))
- return
- }
- c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{
- "authorizeUrl": url, "state": state, "redirectUri": base + pluginoauth.CallbackPath,
- }})
-}
-
-// appOrigin is the app's origin (where the callback page reports back) and
-// the base URL authorization servers send the browser to: APP_EXTERNAL_URL
-// when set, else the origin the browser called from.
-func appOrigin(c *gin.Context) (origin, base string) {
- if ext := strings.TrimSuffix(strings.TrimSpace(os.Getenv("APP_EXTERNAL_URL")), "/"); ext != "" {
- if u, err := url.Parse(ext); err == nil && u.Host != "" {
- return u.Scheme + "://" + u.Host, ext
- }
- }
- if o := c.GetHeader("Origin"); strings.HasPrefix(o, "http://") || strings.HasPrefix(o, "https://") {
- return o, o
- }
- scheme := "http"
- if c.Request.TLS != nil || strings.EqualFold(c.GetHeader("X-Forwarded-Proto"), "https") {
- scheme = "https"
- }
- host := c.Request.Host
- if fh := c.GetHeader("X-Forwarded-Host"); fh != "" {
- host = fh
- }
- o := scheme + "://" + host
- return o, o
-}
-
-var oauthDoneTemplate = template.Must(template.New("oauth").Parse(`<!doctype html>
-<html><head><meta charset="utf-8"><title>WeKnora</title></head>
-<body style="font:14px system-ui,sans-serif;padding:24px">
-<p id="msg">{{.Message}}</p>
-<script>
-(function () {
- var msg = {
- type: 'weknora-plugin-oauth', state: {{.State}}, ok: {{.OK}},
- connection: {{.Connection}}, error: {{.Error}}
- };
- if (window.opener && {{.Origin}}) {
- window.opener.postMessage(msg, {{.Origin}});
- window.close();
- }
-})();
-</script>
-</body></html>`))
-
-// OAuthCallback is where authorization servers send the browser back. It
-// needs no login (the state names the pending authorization) and answers
-// with a page that hands the result to the form that opened it.
-func (h *PluginFormsHandler) OAuthCallback(c *gin.Context) {
- res := h.oauth.Complete(c.Request.Context(), c.Query("state"), c.Query("code"), c.Query("error"))
- data := map[string]any{
- "State": res.State, "Origin": res.Origin, "OK": res.Err == nil, "Connection": "", "Error": "",
- "Message": "Connected. You can close this window.",
- }
- if res.Err != nil {
- data["Error"], data["Message"] = res.Err.Error(), res.Err.Error()
- } else {
- data["Connection"] = configschema.OAuthRefPrefix + res.ConnectionID
- }
- c.Header("Content-Type", "text/html; charset=utf-8")
- c.Header("Cache-Control", "no-store")
- c.Header("Referrer-Policy", "no-referrer")
- status := http.StatusOK
- if res.Err != nil {
- status = http.StatusBadRequest
- }
- c.Status(status)
- _ = oauthDoneTemplate.Execute(c.Writer, data)
-}
diff --git a/internal/handler/plugin_forms_test.go b/internal/handler/plugin_forms_test.go
deleted file mode 100644
index 8c5cada5e..000000000
--- a/internal/handler/plugin_forms_test.go
+++ /dev/null
@@ -1,147 +0,0 @@
-package handler
-
-import (
- "context"
- "encoding/json"
- "net/http"
- "net/http/httptest"
- "strings"
- "testing"
-
- "github.com/gin-gonic/gin"
-
- "github.com/Tencent/WeKnora/internal/middleware"
- "github.com/Tencent/WeKnora/internal/plugin/activate"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- pluginoauth "github.com/Tencent/WeKnora/internal/plugin/oauth"
- "github.com/Tencent/WeKnora/internal/plugin/plugintest"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/plugin/tenancy"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-func formsEngine(t *testing.T) (*gin.Engine, *plugintest.MemTenantSettings) {
- t.Helper()
- gin.SetMode(gin.TestMode)
- tenantSchema, _ := json.Marshal(map[string]any{"type": "object", "properties": map[string]any{
- "site": map[string]any{"type": "string"},
- "token": map[string]any{"type": "string", "x-secret": true},
- "project": map[string]any{
- "type": "string",
- "x-options": map[string]any{"name": "projects", "dependsOn": []string{"site", "token"}},
- },
- }})
- m := &manifest.Manifest{
- SchemaVersion: manifest.SchemaVersion, ID: "acme.jira", Version: "1.0.0", APIVersion: pluginapi.APIVersion,
- Name: manifest.Text("Jira", nil), Publisher: manifest.Publisher{ID: "acme"},
- Runtime: manifest.Runtime{Type: manifest.RuntimeHost, Kind: "binary", Entry: "bin/x"},
- Contributes: manifest.Contributions{manifest.PointWebSearch: {{ID: "x", Name: manifest.Text("X", nil)}}},
- Config: manifest.ConfigSchemas{TenantSchema: tenantSchema},
- }
- reg := registry.New()
- if err := reg.Register(m); err != nil {
- t.Fatal(err)
- }
- settings := &plugintest.MemTenantSettings{}
- ten := tenancy.NewService(reg, settings)
- ctx := context.Background()
- _ = settings.Upsert(ctx, &types.PluginTenantSetting{TenantID: 7, PluginID: "acme.jira", Enabled: true})
- stored := map[string]any{"site": "old", "token": "stored-secret"}
- if _, err := ten.SetConfig(ctx, 7, "acme.jira", stored, "u"); err != nil {
- t.Fatal(err)
- }
-
- p := pluginsdk.New(pluginsdk.Info{ID: "acme.jira", Version: "1.0.0"})
- p.Options(
- "projects",
- func(_ context.Context, call *pluginsdk.Call, in pluginapi.OptionsInput) ([]pluginapi.Option, error) {
- if call.Config.Tenant["token"] != "stored-secret" {
- return nil, pluginapi.Errorf(pluginapi.CodeUnauthorized, "bad token %v", call.Config.Tenant["token"])
- }
- site, _ := call.Config.Tenant["site"].(string)
- return []pluginapi.Option{{Value: site + "/" + in.Query, Label: in.Field}}, nil
- },
- )
- srv := httptest.NewServer(p.Handler())
- t.Cleanup(srv.Close)
- h := NewPluginFormsHandler(reg, ten, activate.NewInvoker(oneClient{client.New(srv.URL, nil, nil)}),
- plugintest.NewMemRepo(), nil, nil, nil)
- r := gin.New()
- r.Use(middleware.ErrorHandler())
- r.POST("/plugins/:id/options", func(c *gin.Context) {
- c.Set(types.TenantIDContextKey.String(), uint64(7))
- h.Options(c)
- })
- return r, settings
-}
-
-func TestPluginFormOptions(t *testing.T) {
- r, settings := formsEngine(t)
- post := func(body string) *httptest.ResponseRecorder {
- return call(
- r,
- http.MethodPost,
- "/plugins/acme.jira/options",
- body,
- map[string]string{"Content-Type": "application/json"},
- )
- }
-
- // The form's unsaved site is used; its redacted token is the stored one.
- w := post(
- `{"name":"projects","field":"project","scope":"tenant","query":"q","values":{"site":"new","token":"***"}}`,
- )
- var resp struct {
- Data pluginapi.OptionsOutput `json:"data"`
- }
- if w.Code != http.StatusOK || json.Unmarshal(w.Body.Bytes(), &resp) != nil ||
- len(
- resp.Data.Options,
- ) != 1 || resp.Data.Options[0].Value != "new/q" || resp.Data.Options[0].Label != "project" {
- t.Fatalf("options = %d %s", w.Code, w.Body)
- }
-
- // A token the user typed replaces the stored one; the plugin's refusal
- // reaches the form as a 400.
- w = post(`{"name":"projects","field":"project","scope":"tenant","values":{"token":"typed"}}`)
- if w.Code != http.StatusBadRequest ||
- !strings.Contains(w.Body.String(), "unauthorized") {
- t.Fatalf("typed token = %d %s", w.Code, w.Body)
- }
- if w := post(`{"name":"nope","field":"project","scope":"tenant"}`); w.Code != http.StatusNotFound {
- t.Fatalf("unknown source = %d %s", w.Code, w.Body)
- }
- if w := post(`{"name":"projects","field":"project","scope":"system"}`); w.Code != http.StatusForbidden {
- t.Fatalf("system scope for a workspace admin = %d", w.Code)
- }
- if w := post(`{"name":"projects","field":"project","scope":"other"}`); w.Code != http.StatusBadRequest {
- t.Fatalf("bad scope = %d", w.Code)
- }
- _ = settings.Upsert(context.Background(),
- &types.PluginTenantSetting{TenantID: 7, PluginID: "acme.jira", Enabled: false}, "enabled")
- if w := post(`{"name":"projects","field":"project","scope":"tenant"}`); w.Code != http.StatusNotFound {
- t.Fatalf("a switched-off plugin = %d", w.Code)
- }
-}
-
-func TestPluginOAuthCallbackPage(t *testing.T) {
- gin.SetMode(gin.TestMode)
- reg := registry.New()
- h := NewPluginFormsHandler(reg, nil, nil, nil, nil, nil,
- pluginoauth.NewService(reg, plugintest.NewMemRepo(), nil, nil))
- r := gin.New()
- r.GET(pluginoauth.CallbackPath, h.OAuthCallback)
- w := call(r, http.MethodGet, pluginoauth.CallbackPath+"?state=%3C%2Fscript%3E&code=x", "", nil)
- body := w.Body.String()
- if w.Code != http.StatusBadRequest || w.Header().Get("Cache-Control") != "no-store" ||
- strings.Contains(body, "</script>'") || !strings.Contains(body, "ok: false") {
- t.Fatalf("callback = %d %s", w.Code, body)
- }
- // Without a known state there is no origin to post the result to.
- if !strings.Contains(body, `window.opener && ""`) {
- t.Fatalf("callback posts to an origin: %s", body)
- }
-}
diff --git a/internal/handler/plugin_gate.go b/internal/handler/plugin_gate.go
deleted file mode 100644
index 343ab3b17..000000000
--- a/internal/handler/plugin_gate.go
+++ /dev/null
@@ -1,39 +0,0 @@
-package handler
-
-import (
- "github.com/gin-gonic/gin"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
-)
-
-// pluginGated gives an integration handler the tenant's plugin switches.
-// It is set after construction (SetPluginGate) so the many handler tests that
-// build handlers directly need no gate; without one everything is enabled.
-type pluginGated struct {
- gate interfaces.PluginGate
-}
-
-// SetPluginGate installs the tenant plugin switches.
-func (g *pluginGated) SetPluginGate(gate interfaces.PluginGate) { g.gate = gate }
-
-// pluginFilter returns whether a contribution is enabled for the caller's
-// tenant. A disabled plugin's contributions are left out of type listings
-// and cannot back new instances; existing instances keep working.
-func (g *pluginGated) pluginFilter(c *gin.Context) func(manifest.Point, string) bool {
- tenantID := c.GetUint64(types.TenantIDContextKey.String())
- if tenantID == 0 {
- if v, ok := c.Request.Context().Value(types.TenantIDContextKey).(uint64); ok {
- tenantID = v
- }
- }
- if g.gate == nil || tenantID == 0 {
- return func(manifest.Point, string) bool { return true }
- }
- return g.gate.EnabledFilter(c.Request.Context(), tenantID)
-}
-
-// disabledIntegrationError is the message for creating an instance of a
-// contribution whose plugin the workspace has disabled.
-const disabledIntegrationError = "this integration is disabled for the workspace; enable its plugin first"
diff --git a/internal/handler/plugin_test.go b/internal/handler/plugin_test.go
deleted file mode 100644
index f874d5577..000000000
--- a/internal/handler/plugin_test.go
+++ /dev/null
@@ -1,177 +0,0 @@
-package handler
-
-import (
- "encoding/json"
- "net/http"
- "net/http/httptest"
- "strings"
- "testing"
-
- "github.com/gin-gonic/gin"
-
- "github.com/Tencent/WeKnora/internal/middleware"
- "github.com/Tencent/WeKnora/internal/plugin/driver"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/plugintest"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/plugin/tenancy"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-func newPluginHandlerTestRouter(t *testing.T) *gin.Engine {
- t.Helper()
- reg := registry.New()
- err := reg.Register(&manifest.Manifest{
- SchemaVersion: manifest.SchemaVersion,
- ID: "weknora.feishu",
- Version: "1.0.0",
- Name: manifest.Text("Feishu", map[string]string{"zh-CN": "飞书"}),
- Publisher: manifest.Publisher{ID: manifest.BuiltinPublisher},
- Builtin: true,
- Runtime: manifest.Runtime{Type: manifest.RuntimeBuiltin},
- Contributes: manifest.Contributions{
- manifest.PointConnectors: {{ID: "feishu", Name: manifest.Text("Feishu", nil), Aliases: []string{"feishu"}}},
- manifest.PointIMChannels: {{ID: "feishu", Name: manifest.Text("Feishu", nil), Aliases: []string{"feishu"}}},
- },
- })
- if err != nil {
- t.Fatal(err)
- }
- gin.SetMode(gin.TestMode)
- r := gin.New()
- r.Use(middleware.ErrorHandler())
- h := NewPluginHandler(reg, nil, driver.NewSet(driver.NewBuiltin(reg.Plugin)))
- r.GET("/plugins", h.ListPlugins)
- r.GET("/plugins/contributions", h.ListContributions)
- r.GET("/plugins/:id", h.GetPlugin)
- return r
-}
-
-func getJSON(t *testing.T, r *gin.Engine, path string, out any) int {
- t.Helper()
- w := httptest.NewRecorder()
- r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil))
- if out != nil && w.Code == http.StatusOK {
- if err := json.Unmarshal(w.Body.Bytes(), out); err != nil {
- t.Fatalf("decode %s: %v (%s)", path, err, w.Body.String())
- }
- }
- return w.Code
-}
-
-func TestPluginHandlerListsPluginsAndContributions(t *testing.T) {
- r := newPluginHandlerTestRouter(t)
-
- var list struct {
- Data []struct {
- Manifest manifest.Manifest `json:"manifest"`
- Enabled bool `json:"enabled"`
- } `json:"data"`
- }
- if code := getJSON(t, r, "/plugins", &list); code != http.StatusOK {
- t.Fatalf("list status = %d", code)
- }
- if len(list.Data) != 1 || list.Data[0].Manifest.Name.Resolve("zh-CN") != "飞书" || !list.Data[0].Enabled {
- t.Fatalf("unexpected plugins: %+v", list.Data)
- }
-
- var contribs struct {
- Data struct {
- Points []manifest.PointInfo `json:"points"`
- Contributions map[string][]map[string]any `json:"contributions"`
- } `json:"data"`
- }
- if code := getJSON(t, r, "/plugins/contributions?point=connectors", &contribs); code != http.StatusOK {
- t.Fatalf("contributions status = %d", code)
- }
- if len(contribs.Data.Points) != 1 || len(contribs.Data.Contributions) != 1 {
- t.Fatalf("point filter not applied: %+v", contribs.Data)
- }
- got := contribs.Data.Contributions["connectors"]
- if len(got) != 1 || got[0]["qualifiedId"] != "weknora.feishu/feishu" || got[0]["pluginId"] != "weknora.feishu" {
- t.Fatalf("unexpected connectors: %+v", got)
- }
-}
-
-func TestPluginHandlerErrors(t *testing.T) {
- r := newPluginHandlerTestRouter(t)
- if code := getJSON(t, r, "/plugins/acme.missing", nil); code != http.StatusNotFound {
- t.Fatalf("missing plugin status = %d, want 404", code)
- }
- if code := getJSON(t, r, "/plugins/contributions?point=widgets", nil); code != http.StatusBadRequest {
- t.Fatalf("unknown point status = %d, want 400", code)
- }
- var detail struct {
- Data struct {
- Enabled bool `json:"enabled"`
- Instances []driver.InstanceStatus `json:"instances"`
- } `json:"data"`
- }
- if code := getJSON(t, r, "/plugins/weknora.feishu", &detail); code != http.StatusOK {
- t.Fatalf("get plugin status = %d", code)
- }
- if !detail.Data.Enabled || len(detail.Data.Instances) != 1 || detail.Data.Instances[0].State != driver.StateReady {
- t.Fatalf("builtin plugin detail = %+v", detail.Data)
- }
-}
-
-func TestPluginHandlerTogglesPluginsPerTenant(t *testing.T) {
- reg := registry.New()
- builtinPlugin := func(id string, required bool, point manifest.Point, localID string) *manifest.Manifest {
- return &manifest.Manifest{
- SchemaVersion: manifest.SchemaVersion, ID: id, Version: "1.0.0", Name: manifest.Text(id, nil),
- Publisher: manifest.Publisher{ID: manifest.BuiltinPublisher}, Builtin: true, Required: required,
- Runtime: manifest.Runtime{Type: manifest.RuntimeBuiltin},
- Contributes: manifest.Contributions{
- point: {{ID: localID, Name: manifest.Text(localID, nil)}},
- },
- }
- }
- for _, m := range []*manifest.Manifest{
- builtinPlugin("weknora.notion", false, manifest.PointConnectors, "notion"),
- builtinPlugin("weknora.agent-tools", true, manifest.PointTools, "thinking"),
- } {
- if err := reg.Register(m); err != nil {
- t.Fatal(err)
- }
- }
- settings := &plugintest.MemTenantSettings{}
- h := NewPluginHandler(reg, tenancy.NewService(reg, settings), nil)
- gin.SetMode(gin.TestMode)
- r := gin.New()
- r.Use(middleware.ErrorHandler(), func(c *gin.Context) { c.Set(types.TenantIDContextKey.String(), uint64(7)) })
- r.PUT("/plugins/:id/enabled", h.SetPluginEnabled)
- r.GET("/plugins/contributions", h.ListContributions)
-
- put := func(id, body string) int {
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodPut, "/plugins/"+id+"/enabled", strings.NewReader(body))
- req.Header.Set("Content-Type", "application/json")
- r.ServeHTTP(w, req)
- return w.Code
- }
- if code := put("weknora.notion", `{"enabled":false}`); code != http.StatusOK {
- t.Fatalf("disable status = %d", code)
- }
- if code := put("weknora.agent-tools", `{"enabled":false}`); code != http.StatusBadRequest {
- t.Fatalf("disabling a required plugin = %d, want 400", code)
- }
- if code := put("acme.missing", `{"enabled":false}`); code != http.StatusNotFound {
- t.Fatalf("unknown plugin = %d, want 404", code)
- }
- if code := put("weknora.notion", `{}`); code != http.StatusBadRequest {
- t.Fatalf("missing enabled = %d, want 400", code)
- }
-
- var contribs struct {
- Data struct {
- Contributions map[string][]map[string]any `json:"contributions"`
- } `json:"data"`
- }
- if code := getJSON(t, r, "/plugins/contributions?point=connectors", &contribs); code != http.StatusOK {
- t.Fatalf("contributions status = %d", code)
- }
- if got := contribs.Data.Contributions["connectors"]; len(got) != 1 || got[0]["enabled"] != false {
- t.Fatalf("disabled plugin's contribution should read disabled: %v", got)
- }
-}
diff --git a/internal/handler/plugin_ui.go b/internal/handler/plugin_ui.go
deleted file mode 100644
index 039002c3e..000000000
--- a/internal/handler/plugin_ui.go
+++ /dev/null
@@ -1,178 +0,0 @@
-package handler
-
-import (
- "encoding/json"
- stderrors "errors"
- "mime"
- "net/http"
- "os"
- "path/filepath"
- "strings"
-
- "github.com/gin-gonic/gin"
-
- "github.com/Tencent/WeKnora/internal/errors"
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/activate"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/tenancy"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// PluginUIAssetsPrefix is where plugin page files are served:
-// {prefix}/{pluginId}/{version}/ui/...
-const PluginUIAssetsPrefix = "/api/v1/plugin-ui/assets"
-
-// pluginPageCSP confines a plugin page to its own files. It can reach
-// nothing over the network (connect-src 'none'): everything goes through the
-// bridge. The page also runs in an iframe sandbox without
-// allow-same-origin, so it cannot read WeKnora's storage.
-const pluginPageCSP = "default-src 'none'; script-src 'self' 'unsafe-inline'; " +
- "style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; " +
- "media-src 'self' data: blob:; connect-src 'none'; form-action 'none'; base-uri 'none'; " +
- "frame-ancestors 'self'"
-
-// maxUIRequestBody bounds what a page can send in one request.
-const maxUIRequestBody = 1 << 20
-
-// PluginUIHandler serves plugin pages and relays their requests to the
-// plugin.
-type PluginUIHandler struct {
- pages *activate.UIPages
- invoker *activate.Invoker
- tenancy *tenancy.Service
- // roleGuard enforces a page's minRole with the same rules as the route
- // guards (API keys, cross-tenant superusers, the RBAC switch).
- roleGuard func(types.TenantRole) gin.HandlerFunc
-}
-
-// NewPluginUIHandler creates the handler.
-func NewPluginUIHandler(pages *activate.UIPages, invoker *activate.Invoker, t *tenancy.Service) *PluginUIHandler {
- return &PluginUIHandler{pages: pages, invoker: invoker, tenancy: t}
-}
-
-// SetRoleGuard supplies the role check; routes set it when they register.
-func (h *PluginUIHandler) SetRoleGuard(g func(types.TenantRole) gin.HandlerFunc) { h.roleGuard = g }
-
-// ServeAsset serves one file of a plugin's pages. It needs no login: the
-// sandboxed iframe loading it has no credentials, and page files are part of
-// an installed package, like the app's own scripts.
-func (h *PluginUIHandler) ServeAsset(c *gin.Context) {
- full, err := h.pages.File(c.Param("id"), c.Param("version"), strings.TrimPrefix(c.Param("path"), "/"))
- if err != nil {
- c.Status(http.StatusNotFound)
- return
- }
- ext := strings.ToLower(filepath.Ext(full))
- ctype := mime.TypeByExtension(ext)
- switch ext {
- case ".js", ".mjs":
- ctype = "text/javascript; charset=utf-8"
- case ".html":
- ctype = "text/html; charset=utf-8"
- }
- if ctype == "" {
- ctype = "application/octet-stream"
- }
- hdr := c.Writer.Header()
- hdr.Set("Content-Type", ctype)
- hdr.Set("Content-Security-Policy", pluginPageCSP)
- hdr.Set("X-Content-Type-Options", "nosniff")
- hdr.Set("Referrer-Policy", "no-referrer")
- // The sandboxed page has an opaque origin: module scripts it loads
- // from here are cross-origin requests.
- hdr.Set("Access-Control-Allow-Origin", "*")
- hdr.Set("Cache-Control", "public, max-age=300")
- f, err := os.Open(full)
- if err != nil {
- c.Status(http.StatusNotFound)
- return
- }
- defer func() { _ = f.Close() }()
- info, err := f.Stat()
- if err != nil {
- c.Status(http.StatusNotFound)
- return
- }
- // ServeContent, not ServeFile: ServeFile redirects .../index.html.
- http.ServeContent(c.Writer, c.Request, filepath.Base(full), info.ModTime(), f)
-}
-
-// PluginUIRequest is what a page sent through the bridge.
-type PluginUIRequest struct {
- Mount string `json:"mount" binding:"required"`
- Method string `json:"method"`
- Path string `json:"path"`
- Body json.RawMessage `json:"body,omitempty"`
-}
-
-// Request godoc
-// @Summary 转发插件页面的请求
-// @Description 插件页面在沙箱 iframe 中运行,只能经宿主桥发请求;这里校验空间已启用该插件、调用者满足页面的 minRole,再转给插件后端
-// @Tags Plugins
-// @Accept json
-// @Produce json
-// @Param id path string true "插件 ID"
-// @Param request body PluginUIRequest true "页面请求"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Router /plugins/{id}/ui-request [post]
-func (h *PluginUIHandler) Request(c *gin.Context) {
- limitJSONBody(c, maxUIRequestBody)
- var req PluginUIRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- _ = c.Error(errors.NewBadRequestError("mount is required"))
- return
- }
- pluginID := c.Param("id")
- mount, err := h.pages.Mount(pluginID, req.Mount)
- if err != nil {
- _ = c.Error(errors.NewNotFoundError("no such plugin page"))
- return
- }
- ctx := c.Request.Context()
- tenantID := c.GetUint64(types.TenantIDContextKey.String())
- qualified := manifest.QualifiedID(pluginID, mount.Contribution.ID)
- if h.tenancy != nil && !h.tenancy.ContributionEnabled(ctx, tenantID, mount.Point, qualified) {
- _ = c.Error(errors.NewNotFoundError("the plugin is not enabled in this workspace"))
- return
- }
- if need := types.TenantRole(mount.MinRole()); need != types.TenantRoleViewer && h.roleGuard != nil {
- if h.roleGuard(need)(c); c.IsAborted() {
- return
- }
- }
- rt := mount.Manifest.Runtime.Type
- if rt != manifest.RuntimeHost && rt != manifest.RuntimeRemote {
- _ = c.Error(errors.NewNotFoundError("this plugin's pages make no requests"))
- return
- }
- method := strings.ToUpper(strings.TrimSpace(req.Method))
- if method == "" {
- method = http.MethodGet
- }
- path := req.Path
- if !strings.HasPrefix(path, "/") {
- path = "/" + path
- }
- in := pluginapi.UIRequest{
- Mount: req.Mount, Method: method, Path: path, Body: req.Body,
- Role: string(types.TenantRoleFromContext(ctx)),
- }
- var out pluginapi.UIResponse
- if err := h.invoker.Call(ctx, mount.Manifest, pluginapi.UIRequestPath, nil, in, &out); err != nil {
- var pe *pluginapi.Error
- if stderrors.As(err, &pe) && pe.Code == pluginapi.CodeNotFound {
- _ = c.Error(errors.NewNotFoundError("this plugin's pages make no requests"))
- return
- }
- logger.Warnf(ctx, "[plugin] page request to %s failed: %v", pluginID, err)
- c.JSON(http.StatusBadGateway, gin.H{"success": false, "error": gin.H{"message": err.Error()}})
- return
- }
- if out.Status == 0 {
- out.Status = http.StatusOK
- }
- c.JSON(http.StatusOK, gin.H{"success": true, "data": out})
-}
diff --git a/internal/handler/plugin_ui_test.go b/internal/handler/plugin_ui_test.go
deleted file mode 100644
index ea76d3b00..000000000
--- a/internal/handler/plugin_ui_test.go
+++ /dev/null
@@ -1,153 +0,0 @@
-package handler
-
-import (
- "bytes"
- "context"
- "encoding/json"
- "net/http"
- "net/http/httptest"
- "os"
- "path/filepath"
- "strings"
- "testing"
-
- "github.com/gin-gonic/gin"
-
- "github.com/Tencent/WeKnora/internal/plugin/activate"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// oneClient serves every plugin from one SDK handler.
-type oneClient struct{ c *client.Client }
-
-func (o oneClient) Client(context.Context, *manifest.Manifest) (*client.Client, error) {
- return o.c, nil
-}
-func (o oneClient) OnThisNode(string) bool { return true }
-
-func uiPlugin(t *testing.T, rt manifest.RuntimeType) *reconcile.Loaded {
- t.Helper()
- dir := t.TempDir()
- for name, body := range map[string]string{
- "ui/index.html": "<!doctype html><p>links</p>", "ui/app.js": "export {}", "main.py": "secret",
- } {
- full := filepath.Join(dir, filepath.FromSlash(name))
- _ = os.MkdirAll(filepath.Dir(full), 0o755)
- if err := os.WriteFile(full, []byte(body), 0o644); err != nil {
- t.Fatal(err)
- }
- }
- m := &manifest.Manifest{
- ID: "acme.ui", Version: "1.0.0", Runtime: manifest.Runtime{Type: rt},
- Contributes: manifest.Contributions{
- manifest.PointPages: {{ID: "links", Entry: "ui/index.html"}},
- manifest.PointSettingsSections: {{ID: "admin", Entry: "ui/index.html"}},
- },
- }
- return &reconcile.Loaded{Manifest: m, Dir: dir}
-}
-
-func uiEngine(t *testing.T, rt manifest.RuntimeType, role types.TenantRole) *gin.Engine {
- t.Helper()
- gin.SetMode(gin.TestMode)
- pages := activate.NewUIPages()
- if err := pages.Activate(context.Background(), uiPlugin(t, rt)); err != nil {
- t.Fatal(err)
- }
- p := pluginsdk.New(pluginsdk.Info{ID: "acme.ui", Version: "1.0.0"})
- p.UI(func(_ context.Context, _ *pluginsdk.Call, in pluginapi.UIRequest) (*pluginapi.UIResponse, error) {
- return pluginsdk.UIJSON(201, map[string]string{
- "mount": in.Mount, "method": in.Method, "path": in.Path, "role": in.Role,
- })
- })
- srv := httptest.NewServer(p.Handler())
- t.Cleanup(srv.Close)
- h := NewPluginUIHandler(pages, activate.NewInvoker(oneClient{client.New(srv.URL, nil, nil)}), nil)
- h.SetRoleGuard(func(need types.TenantRole) gin.HandlerFunc {
- return func(c *gin.Context) {
- if !role.HasPermission(need) {
- c.AbortWithStatus(http.StatusForbidden)
- }
- }
- })
- r := gin.New()
- r.Use(func(c *gin.Context) {
- c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), types.TenantRoleContextKey, role))
- c.Next()
- if len(c.Errors) > 0 && !c.Writer.Written() {
- c.Status(http.StatusNotFound)
- }
- })
- r.GET(PluginUIAssetsPrefix+"/:id/:version/*path", h.ServeAsset)
- r.POST("/plugins/:id/ui-request", h.Request)
- return r
-}
-
-func TestPluginPageFiles(t *testing.T) {
- r := uiEngine(t, manifest.RuntimeHost, types.TenantRoleViewer)
- get := func(path string) *httptest.ResponseRecorder {
- w := httptest.NewRecorder()
- r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, PluginUIAssetsPrefix+path, nil))
- return w
- }
- w := get("/acme.ui/1.0.0/ui/index.html")
- if w.Code != 200 || !strings.HasPrefix(w.Header().Get("Content-Type"), "text/html") ||
- !strings.Contains(w.Header().Get("Content-Security-Policy"), "connect-src 'none'") ||
- w.Header().Get("X-Content-Type-Options") != "nosniff" {
- t.Fatalf("index = %d %v", w.Code, w.Header())
- }
- w = get("/acme.ui/1.0.0/ui/app.js")
- if w.Code != 200 || !strings.HasPrefix(w.Header().Get("Content-Type"), "text/javascript") ||
- w.Header().Get("Access-Control-Allow-Origin") != "*" {
- t.Fatalf("module = %d %v", w.Code, w.Header())
- }
- for _, path := range []string{
- "/acme.ui/1.0.0/main.py", "/acme.ui/1.0.0/ui/../main.py", "/acme.ui/0.9.0/ui/index.html",
- "/acme.other/1.0.0/ui/index.html", "/acme.ui/1.0.0/ui/missing.html",
- } {
- if w := get(path); w.Code != http.StatusNotFound {
- t.Errorf("%s = %d, want 404", path, w.Code)
- }
- }
-}
-
-func uiRequest(r *gin.Engine, body string) *httptest.ResponseRecorder {
- w := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodPost, "/plugins/acme.ui/ui-request", bytes.NewBufferString(body))
- req.Header.Set("Content-Type", "application/json")
- r.ServeHTTP(w, req)
- return w
-}
-
-func TestPluginPageRequests(t *testing.T) {
- r := uiEngine(t, manifest.RuntimeHost, types.TenantRoleViewer)
- w := uiRequest(r, `{"mount":"pages/links","method":"put","path":"links","body":{"a":1}}`)
- var resp struct {
- Data pluginapi.UIResponse `json:"data"`
- }
- if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &resp) != nil || resp.Data.Status != 201 ||
- string(resp.Data.Body) != `{"method":"PUT","mount":"pages/links","path":"/links","role":"viewer"}` {
- t.Fatalf("page request = %d %s", w.Code, w.Body)
- }
- if w := uiRequest(r, `{"mount":"settingsSections/admin","path":"/"}`); w.Code != http.StatusForbidden {
- t.Fatalf("a viewer reached an admin page: %d", w.Code)
- }
- if w := uiRequest(r, `{"mount":"pages/nope","path":"/"}`); w.Code != http.StatusNotFound {
- t.Fatalf("unknown mount = %d", w.Code)
- }
-
- admin := uiEngine(t, manifest.RuntimeHost, types.TenantRoleAdmin)
- if w := uiRequest(admin, `{"mount":"settingsSections/admin","path":"/"}`); w.Code != 200 {
- t.Fatalf("admin page for an admin = %d %s", w.Code, w.Body)
- }
-
- static := uiEngine(t, manifest.RuntimeDeclarative, types.TenantRoleViewer)
- if w := uiRequest(static, `{"mount":"pages/links","path":"/"}`); w.Code != http.StatusNotFound {
- t.Fatalf("a plugin without code = %d", w.Code)
- }
-}
diff --git a/internal/handler/plugin_webhook.go b/internal/handler/plugin_webhook.go
deleted file mode 100644
index 5eb3f26a5..000000000
--- a/internal/handler/plugin_webhook.go
+++ /dev/null
@@ -1,193 +0,0 @@
-package handler
-
-import (
- "context"
- "io"
- "net/http"
- "strings"
- "sync"
-
- "github.com/gin-gonic/gin"
- "golang.org/x/time/rate"
-
- "github.com/Tencent/WeKnora/internal/errors"
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/activate"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/plugin/tenancy"
- "github.com/Tencent/WeKnora/internal/plugin/webhook"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// maxWebhookBody bounds one inbound call.
-const maxWebhookBody = 1 << 20
-
-// Per-URL rate: a burst, then a steady rate.
-const (
- webhookBurst = 40
- webhookPerSec = 20
-)
-
-// PluginWebhookHandler receives third parties' calls to plugin webhooks and
-// relays them to the plugin, and shows workspace admins their URLs.
-type PluginWebhookHandler struct {
- registry *registry.Registry
- tenancy *tenancy.Service
- invoker *activate.Invoker
- tokens *webhook.Tokens
-
- mu sync.Mutex
- limiters map[string]*rate.Limiter
-}
-
-// NewPluginWebhookHandler creates the handler.
-func NewPluginWebhookHandler(
- reg *registry.Registry, t *tenancy.Service, iv *activate.Invoker, tokens *webhook.Tokens,
-) *PluginWebhookHandler {
- return &PluginWebhookHandler{
- registry: reg,
- tenancy: t,
- invoker: iv,
- tokens: tokens,
- limiters: map[string]*rate.Limiter{},
- }
-}
-
-func (h *PluginWebhookHandler) allow(token string) bool {
- h.mu.Lock()
- defer h.mu.Unlock()
- l, ok := h.limiters[token]
- if !ok {
- if len(h.limiters) > 10000 {
- h.limiters = map[string]*rate.Limiter{} // bounded memory; limits restart
- }
- l = rate.NewLimiter(webhookPerSec, webhookBurst)
- h.limiters[token] = l
- }
- return l.Allow()
-}
-
-// webhookOf finds webhook id among a loaded plugin's contributions.
-func (h *PluginWebhookHandler) webhookOf(pluginID, id string) (*manifest.Manifest, *manifest.Contribution) {
- m, ok := h.registry.Plugin(pluginID)
- if !ok || (m.Runtime.Type != manifest.RuntimeHost && m.Runtime.Type != manifest.RuntimeRemote) {
- return nil, nil
- }
- for i, c := range m.Contributes[manifest.PointWebhooks] {
- if c.ID == id {
- return m, &m.Contributes[manifest.PointWebhooks][i]
- }
- }
- return nil, nil
-}
-
-// Receive relays one inbound call. It needs no login: the URL's token names
-// the workspace and proves WeKnora issued the URL. Everything that is not
-// a live webhook of a plugin the workspace has on answers 404.
-func (h *PluginWebhookHandler) Receive(c *gin.Context) {
- pluginID, hookID, token := c.Param("id"), c.Param("hook"), c.Param("token")
- tenantID, ok := h.tokens.Verify(pluginID, hookID, token)
- if !ok {
- c.Status(http.StatusNotFound)
- return
- }
- m, _ := h.webhookOf(pluginID, hookID)
- if m == nil {
- c.Status(http.StatusNotFound)
- return
- }
- ctx := c.Request.Context()
- if on, err := h.tenancy.PluginEnabled(ctx, tenantID, pluginID); err != nil || !on {
- c.Status(http.StatusNotFound)
- return
- }
- if !h.allow(token) {
- c.Status(http.StatusTooManyRequests)
- return
- }
- body, err := io.ReadAll(io.LimitReader(c.Request.Body, maxWebhookBody+1))
- if err != nil || len(body) > maxWebhookBody {
- c.Status(http.StatusRequestEntityTooLarge)
- return
- }
- headers := map[string]string{}
- for k, v := range c.Request.Header {
- if len(v) > 0 && !strings.EqualFold(k, "Cookie") {
- headers[k] = v[0]
- }
- }
- path := c.Param("path")
- if path == "" {
- path = "/"
- }
- in := pluginapi.WebhookRequest{
- Method: c.Request.Method, Path: path, Query: c.Request.URL.RawQuery, Headers: headers, Body: body,
- }
- var out pluginapi.WebhookResponse
- ctx = context.WithValue(ctx, types.TenantIDContextKey, tenantID)
- if err := h.invoker.Call(ctx, m, pluginapi.WebhookPath(hookID), nil, in, &out); err != nil {
- logger.Warnf(ctx, "[plugin] webhook %s/%s for tenant %d: %v", pluginID, hookID, tenantID, err)
- status := http.StatusBadGateway
- if pe, ok := pluginapi.AsError(err); ok && pe.Retryable {
- status = http.StatusServiceUnavailable
- }
- c.Status(status)
- return
- }
- status := out.Status
- if status < 100 || status > 599 {
- status = http.StatusOK
- }
- ctype := out.ContentType
- if ctype == "" {
- ctype = "application/octet-stream"
- }
- c.Data(status, ctype, out.Body)
-}
-
-// PluginWebhookDTO is one webhook of a plugin, with the workspace's URL.
-type PluginWebhookDTO struct {
- ID string `json:"id"`
- Name manifest.LocalizedText `json:"name"`
- Description manifest.LocalizedText `json:"description,omitzero"`
- // Path is the URL path; URL is the full URL when WeKnora knows its
- // public address (APP_EXTERNAL_URL).
- Path string `json:"path"`
- URL string `json:"url,omitempty"`
-}
-
-// List godoc
-// @Summary 插件 Webhook 地址
-// @Description 返回本空间该插件各个 Webhook 的地址,供在第三方系统中配置;地址含密钥,仅管理员可见
-// @Tags Plugins
-// @Produce json
-// @Param id path string true "插件 ID"
-// @Success 200 {object} map[string]interface{}
-// @Security Bearer
-// @Router /plugins/{id}/webhooks [get]
-func (h *PluginWebhookHandler) List(c *gin.Context) {
- pluginID := c.Param("id")
- m, ok := h.registry.Plugin(pluginID)
- if !ok {
- _ = c.Error(errors.NewNotFoundError("plugin not found"))
- return
- }
- tenantID := c.GetUint64(types.TenantIDContextKey.String())
- base := webhook.PublicBase()
- out := []PluginWebhookDTO{}
- for _, w := range m.Contributes[manifest.PointWebhooks] {
- dto := PluginWebhookDTO{
- ID: w.ID,
- Name: w.Name,
- Description: w.Description,
- Path: h.tokens.Path(m.ID, w.ID, tenantID),
- }
- if base != "" {
- dto.URL = base + dto.Path
- }
- out = append(out, dto)
- }
- c.JSON(http.StatusOK, gin.H{"success": true, "data": out})
-}
diff --git a/internal/handler/plugin_webhook_test.go b/internal/handler/plugin_webhook_test.go
deleted file mode 100644
index 779a4c322..000000000
--- a/internal/handler/plugin_webhook_test.go
+++ /dev/null
@@ -1,140 +0,0 @@
-package handler
-
-import (
- "bytes"
- "context"
- "encoding/json"
- "net/http"
- "net/http/httptest"
- "strings"
- "testing"
-
- "github.com/gin-gonic/gin"
-
- "github.com/Tencent/WeKnora/internal/plugin/activate"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/plugintest"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/plugin/tenancy"
- "github.com/Tencent/WeKnora/internal/plugin/webhook"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-func webhookEngine(t *testing.T) (*gin.Engine, *webhook.Tokens, *plugintest.MemTenantSettings) {
- t.Helper()
- gin.SetMode(gin.TestMode)
- reg := registry.New()
- m := &manifest.Manifest{
- SchemaVersion: manifest.SchemaVersion, ID: "acme.hooks", Version: "1.0.0", APIVersion: pluginapi.APIVersion,
- Name: manifest.Text("Hooks", nil), Publisher: manifest.Publisher{ID: "acme"},
- Runtime: manifest.Runtime{Type: manifest.RuntimeHost, Kind: "binary", Entry: "bin/x"},
- Contributes: manifest.Contributions{
- manifest.PointWebhooks: {{ID: "inbox", Name: manifest.Text("Inbox", nil)}},
- },
- }
- if err := reg.Register(m); err != nil {
- t.Fatal(err)
- }
- settings := &plugintest.MemTenantSettings{}
- _ = settings.Upsert(
- context.Background(),
- &types.PluginTenantSetting{TenantID: 7, PluginID: "acme.hooks", Enabled: true},
- )
-
- p := pluginsdk.New(pluginsdk.Info{ID: "acme.hooks", Version: "1.0.0"})
- p.Webhook(
- "inbox",
- func(
- _ context.Context, call *pluginsdk.Call, req pluginapi.WebhookRequest,
- ) (*pluginapi.WebhookResponse, error) {
- if req.Headers["X-Signature"] != "ok" {
- return &pluginapi.WebhookResponse{Status: 401}, nil
- }
- body, _ := json.Marshal(map[string]any{
- "tenant": call.TenantID, "method": req.Method, "path": req.Path, "query": req.Query,
- "body": string(req.Body), "cookie": req.Headers["Cookie"],
- })
- return &pluginapi.WebhookResponse{Status: 202, ContentType: "application/json", Body: body}, nil
- },
- )
- srv := httptest.NewServer(p.Handler())
- t.Cleanup(srv.Close)
- tokens := webhook.NewTokens([]byte("k"))
- h := NewPluginWebhookHandler(reg, tenancy.NewService(reg, settings),
- activate.NewInvoker(oneClient{client.New(srv.URL, nil, nil)}), tokens)
- r := gin.New()
- r.Any(webhook.PathPrefix+"/:id/:hook/:token", h.Receive)
- r.Any(webhook.PathPrefix+"/:id/:hook/:token/*path", h.Receive)
- r.GET("/plugins/:id/webhooks", func(c *gin.Context) {
- c.Set(types.TenantIDContextKey.String(), uint64(7))
- h.List(c)
- })
- return r, tokens, settings
-}
-
-func call(r *gin.Engine, method, path string, body string, headers map[string]string) *httptest.ResponseRecorder {
- w := httptest.NewRecorder()
- req := httptest.NewRequest(method, path, bytes.NewBufferString(body))
- for k, v := range headers {
- req.Header.Set(k, v)
- }
- r.ServeHTTP(w, req)
- return w
-}
-
-func TestPluginWebhooksRelayCalls(t *testing.T) {
- r, tokens, settings := webhookEngine(t)
- url := tokens.Path("acme.hooks", "inbox", 7)
- signed := map[string]string{"X-Signature": "ok", "Cookie": "session=secret"}
-
- w := call(r, http.MethodPost, url+"/issues?since=1", `{"id":1}`, signed)
- if w.Code != 202 || w.Header().Get("Content-Type") != "application/json" {
- t.Fatalf("relay = %d %s", w.Code, w.Body)
- }
- var got map[string]any
- _ = json.Unmarshal(w.Body.Bytes(), &got)
- if got["tenant"] != float64(7) || got["path"] != "/issues" || got["query"] != "since=1" ||
- got["body"] != `{"id":1}` || got["cookie"] != "" {
- t.Fatalf("plugin saw %v", got)
- }
- if w := call(r, http.MethodPost, url, `{}`, nil); w.Code != 401 {
- t.Fatalf("the plugin's answer is relayed: %d", w.Code)
- }
-
- for _, bad := range []string{
- tokens.Path("acme.hooks", "inbox", 8) + "x",
- strings.Replace(url, "/inbox/", "/other/", 1),
- webhook.PathPrefix + "/acme.hooks/inbox/7.nope",
- tokens.Path("acme.nope", "inbox", 7),
- } {
- if w := call(r, http.MethodPost, bad, `{}`, signed); w.Code != http.StatusNotFound {
- t.Errorf("%s = %d", bad, w.Code)
- }
- }
- _ = settings.Upsert(
- context.Background(),
- &types.PluginTenantSetting{TenantID: 7, PluginID: "acme.hooks", Enabled: false},
- )
- if w := call(r, http.MethodPost, url, `{}`, signed); w.Code != http.StatusNotFound {
- t.Fatalf("a switched-off plugin's webhook = %d", w.Code)
- }
-}
-
-func TestPluginWebhookList(t *testing.T) {
- r, tokens, _ := webhookEngine(t)
- t.Setenv("APP_EXTERNAL_URL", "https://weknora.example.com/")
- w := call(r, http.MethodGet, "/plugins/acme.hooks/webhooks", "", nil)
- var resp struct {
- Data []PluginWebhookDTO `json:"data"`
- }
- if json.Unmarshal(w.Body.Bytes(), &resp) != nil || len(resp.Data) != 1 {
- t.Fatalf("list = %s", w.Body)
- }
- want := tokens.Path("acme.hooks", "inbox", 7)
- if resp.Data[0].Path != want || resp.Data[0].URL != "https://weknora.example.com"+want {
- t.Fatalf("webhook = %+v", resp.Data[0])
- }
-}
diff --git a/internal/handler/sandbox_skill.go b/internal/handler/sandbox_skill.go
index 8029f72cc..6d75f1cc9 100644
--- a/internal/handler/sandbox_skill.go
+++ b/internal/handler/sandbox_skill.go
@@ -358,9 +358,8 @@ type skillSourceRequest struct {
// Source is exactly one of: "@owner/slug" or a slash-free slug (ClawHub),
// a github.com / gitlab.com / skills.sh / clawhub / skillhub page URL, a
// ClawHub skills-sh catalog page or "skills-sh:owner/repo/slug" locator, or
- // a direct zip/SKILL.md URL, or "plugin:<plugin id>/<skill id>" for a skill
- // an installed plugin provides. Bare "owner/slug" is rejected: it is both
- // a ClawHub id and a GitHub repo. The fetch carries no credential.
+ // a direct zip/SKILL.md URL. Bare "owner/slug" is rejected: it is both a
+ // ClawHub id and a GitHub repo. The fetch carries no credential.
Source string `json:"source"`
}
diff --git a/internal/handler/session/qa.go b/internal/handler/session/qa.go
index 96b031638..63aa971c6 100644
--- a/internal/handler/session/qa.go
+++ b/internal/handler/session/qa.go
@@ -20,7 +20,6 @@ import (
"github.com/Tencent/WeKnora/internal/event"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/models/api"
- pluginevents "github.com/Tencent/WeKnora/internal/plugin/events"
"github.com/Tencent/WeKnora/internal/sandbox"
"github.com/Tencent/WeKnora/internal/storageurl"
"github.com/Tencent/WeKnora/internal/stream"
@@ -28,7 +27,6 @@ import (
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
secutils "github.com/Tencent/WeKnora/internal/utils"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
@@ -1927,16 +1925,6 @@ func (h *Handler) completeAssistantMessage(
logger.Errorf(ctx, "Failed to persist assistant message %s: %v", assistantMessage.ID, err)
return err
}
- // The stop button completes the message a first time without the
- // question; the run's own completion follows with it.
- if userQuery != "" {
- tenantID, _ := types.TenantIDFromContext(ctx)
- userID, _ := types.UserIDFromContext(ctx)
- pluginevents.Publish(ctx, tenantID, pluginapi.EventChatAnswered, pluginapi.ChatEventData{
- SessionID: assistantMessage.SessionID, MessageID: assistantMessage.ID, AgentID: assistantMessage.AgentID,
- UserID: userID, Question: userQuery, Answer: assistantMessage.Content,
- })
- }
// Asynchronously index the Q&A pair into the chat history knowledge base for vector search.
// Use WithoutCancel so the goroutine survives after the HTTP request context is done.
diff --git a/internal/handler/system.go b/internal/handler/system.go
index a5e1bae46..40cabdc53 100644
--- a/internal/handler/system.go
+++ b/internal/handler/system.go
@@ -24,7 +24,6 @@ import (
"github.com/Tencent/WeKnora/internal/infrastructure/docparser"
"github.com/Tencent/WeKnora/internal/logger"
modellimiter "github.com/Tencent/WeKnora/internal/models/limiter"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
"github.com/Tencent/WeKnora/internal/runtime"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
@@ -39,7 +38,6 @@ type runtimeKnowledgeCanceller interface {
// SystemHandler handles system-related requests
type SystemHandler struct {
- pluginGated
cfg *config.Config
neo4jDriver neo4j.Driver
documentReader interfaces.DocumentReader
@@ -422,18 +420,7 @@ func (h *SystemHandler) ListParserEngines(c *gin.Context) {
connected := reader != nil && reader.IsConnected()
remoteEngines := h.fetchRemoteEngines(c.Request.Context(), reader, overrides)
engines := docparser.ListAllEngines(connected, overrides, remoteEngines)
- // Engines of plugins the workspace has turned off leave the list.
- enabled := h.pluginFilter(c)
- visible := engines[:0]
- for _, e := range engines {
- if enabled(manifest.PointParsers, e.Name) {
- visible = append(visible, e)
- }
- }
- c.JSON(200, gin.H{
- "code": 0, "msg": "success", "data": visible,
- "docreader_addr": docreaderAddr, "docreader_transport": docreaderTransport, "connected": connected,
- })
+ c.JSON(200, gin.H{"code": 0, "msg": "success", "data": engines, "docreader_addr": docreaderAddr, "docreader_transport": docreaderTransport, "connected": connected})
}
// ReconnectDocReader reconnects the document converter to a new (or same) DocReader address.
diff --git a/internal/handler/web_search_provider.go b/internal/handler/web_search_provider.go
index a7908351c..c57137191 100644
--- a/internal/handler/web_search_provider.go
+++ b/internal/handler/web_search_provider.go
@@ -9,7 +9,6 @@ import (
"github.com/Tencent/WeKnora/internal/handler/dto"
infra_web_search "github.com/Tencent/WeKnora/internal/infrastructure/web_search"
"github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
secutils "github.com/Tencent/WeKnora/internal/utils"
@@ -18,7 +17,6 @@ import (
// WebSearchProviderHandler handles HTTP requests for web search provider CRUD
type WebSearchProviderHandler struct {
- pluginGated
repo interfaces.WebSearchProviderRepository
service interfaces.WebSearchProviderService
registry *infra_web_search.Registry
@@ -93,11 +91,6 @@ func (h *WebSearchProviderHandler) CreateProvider(c *gin.Context) {
return
}
- if !h.pluginFilter(c)(manifest.PointWebSearch, string(req.Provider)) {
- _ = c.Error(errors.NewBadRequestError(disabledIntegrationError))
- return
- }
-
logger.Infof(ctx, "Creating web search provider: tenant=%d, name=%s, type=%s",
tenantID, secutils.SanitizeForLog(req.Name), secutils.SanitizeForLog(string(req.Provider)))
@@ -327,20 +320,9 @@ func (h *WebSearchProviderHandler) DeleteProvider(c *gin.Context) {
// @Security ApiKeyAuth
// @Router /web-search-providers/types [get]
func (h *WebSearchProviderHandler) ListProviderTypes(c *gin.Context) {
- enabled := h.pluginFilter(c)
- all := types.GetWebSearchProviderTypes()
- if h.registry != nil {
- all = append(all, h.registry.PluginTypes()...)
- }
- out := make([]types.WebSearchProviderTypeInfo, 0, len(all))
- for _, info := range all {
- if enabled(manifest.PointWebSearch, info.ID) {
- out = append(out, info)
- }
- }
c.JSON(http.StatusOK, gin.H{
"success": true,
- "data": out,
+ "data": types.GetWebSearchProviderTypes(),
})
}
@@ -411,11 +393,6 @@ func (h *WebSearchProviderHandler) TestProviderRaw(c *gin.Context) {
return
}
- if !h.pluginFilter(c)(manifest.PointWebSearch, req.Provider) {
- _ = c.Error(errors.NewBadRequestError(disabledIntegrationError))
- return
- }
-
if err := h.doTestSearch(ctx, req.Provider, req.Parameters); err != nil {
logger.Warnf(ctx, "Web search provider test failed: %v", err)
c.JSON(http.StatusOK, gin.H{"success": false, "error": err.Error()})
diff --git a/internal/im/credentials.go b/internal/im/credentials.go
index 64c63d5fb..73e6af886 100644
--- a/internal/im/credentials.go
+++ b/internal/im/credentials.go
@@ -3,8 +3,6 @@ package im
import (
"encoding/json"
"strings"
-
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
)
// ParseCredentials parses the JSONB credentials field into a map.
@@ -49,62 +47,3 @@ func GetBool(creds map[string]any, key string) bool {
return false
}
}
-
-// secretKeys returns the credential keys a platform's schema marks secret.
-func secretKeys(platform string) map[string]bool {
- out := map[string]bool{}
- if s := LookupPlatformInfo(platform).ConfigSchema; s != nil {
- for key, prop := range s.Properties {
- if prop.Secret {
- out[key] = true
- }
- }
- }
- return out
-}
-
-// RedactCredentials returns a channel's credentials for the editor: plain
-// fields as stored, set secrets replaced by configschema.RedactedPlaceholder.
-func RedactCredentials(platform string, raw []byte) (map[string]any, error) {
- creds, err := ParseCredentials(raw)
- if err != nil {
- return nil, err
- }
- secrets := secretKeys(platform)
- for key, v := range creds {
- if s, ok := v.(string); ok && s != "" && secrets[key] {
- creds[key] = configschema.RedactedPlaceholder
- }
- }
- return creds, nil
-}
-
-// MergeCredentials applies credentials sent by the editor to the stored ones.
-// Keys the update leaves out keep their stored value, and so does a secret
-// sent empty or as the redaction placeholder, since the editor never saw it.
-// A plain field sent empty is cleared. An empty update changes nothing, which
-// keeps an edit that never loaded the credentials from wiping them.
-func MergeCredentials(platform string, stored, incoming []byte) ([]byte, error) {
- update, err := ParseCredentials(incoming)
- if err != nil {
- return nil, err
- }
- if len(update) == 0 {
- return stored, nil
- }
- out, err := ParseCredentials(stored)
- if err != nil {
- // Unreadable stored credentials cannot be merged into; take the update.
- out = map[string]any{}
- }
- secrets := secretKeys(platform)
- for key, v := range update {
- if secrets[key] {
- if s, ok := v.(string); v == nil || (ok && (s == "" || s == configschema.RedactedPlaceholder)) {
- continue
- }
- }
- out[key] = v
- }
- return json.Marshal(out)
-}
diff --git a/internal/im/credentials_merge_test.go b/internal/im/credentials_merge_test.go
deleted file mode 100644
index c15090b80..000000000
--- a/internal/im/credentials_merge_test.go
+++ /dev/null
@@ -1,64 +0,0 @@
-package im
-
-import (
- "encoding/json"
- "testing"
-)
-
-func mustJSON(t *testing.T, v any) []byte {
- t.Helper()
- b, err := json.Marshal(v)
- if err != nil {
- t.Fatal(err)
- }
- return b
-}
-
-func TestRedactCredentialsHidesOnlySecrets(t *testing.T) {
- stored := mustJSON(t, map[string]any{"site_url": "https://mm", "bot_token": "tok", "post_to_main": true})
- got, err := RedactCredentials("mattermost", stored)
- if err != nil {
- t.Fatal(err)
- }
- if got["site_url"] != "https://mm" || got["bot_token"] != "***" || got["post_to_main"] != true {
- t.Fatalf("redacted = %v", got)
- }
-}
-
-func TestMergeCredentials(t *testing.T) {
- stored := mustJSON(t, map[string]any{
- "site_url": "https://mm", "bot_token": "tok", "outgoing_token": "out", "bot_user_id": "u1",
- })
- merge := func(update map[string]any) map[string]any {
- t.Helper()
- out, err := MergeCredentials("mattermost", stored, mustJSON(t, update))
- if err != nil {
- t.Fatal(err)
- }
- var m map[string]any
- if err := json.Unmarshal(out, &m); err != nil {
- t.Fatal(err)
- }
- return m
- }
-
- // An edit that never loaded the credentials must not wipe them.
- if got := merge(map[string]any{}); got["bot_token"] != "tok" || got["site_url"] != "https://mm" {
- t.Fatalf("empty update changed credentials: %v", got)
- }
- // The redacted form comes back with one plain field changed.
- got := merge(map[string]any{
- "site_url": "https://new", "bot_token": "***", "outgoing_token": "", "bot_user_id": "",
- })
- want := map[string]any{"site_url": "https://new", "bot_token": "tok", "outgoing_token": "out", "bot_user_id": ""}
- for k, v := range want {
- if got[k] != v {
- t.Errorf("%s = %v, want %v (all: %v)", k, got[k], v, got)
- }
- }
- // A new secret replaces the stored one.
- got = merge(map[string]any{"bot_token": "new-tok"})
- if got["bot_token"] != "new-tok" || got["outgoing_token"] != "out" {
- t.Fatalf("secret replacement = %v", got)
- }
-}
diff --git a/internal/im/platforms.go b/internal/im/platforms.go
deleted file mode 100644
index 68eb2caef..000000000
--- a/internal/im/platforms.go
+++ /dev/null
@@ -1,342 +0,0 @@
-package im
-
-import (
- "sort"
-
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
-)
-
-// Channel modes. The first mode a platform lists is its default.
-const (
- ModeWebSocket = "websocket"
- ModeWebhook = "webhook"
- ModeLongPoll = "longpoll"
-)
-
-// PlatformInfo describes one IM platform for the channel editor: how it is
-// named, which connection modes it supports and what credentials it takes.
-type PlatformInfo struct {
- ID string `json:"id"`
- // Name is the default (English) name; Names holds localized variants.
- Name string `json:"name"`
- Names map[string]string `json:"names,omitempty"`
- // Order sorts platforms in the UI (lower first).
- Order int `json:"order"`
- // Modes lists the supported connection modes, default first.
- Modes []string `json:"modes"`
- // ModeLabels renames a mode for this platform (DingTalk calls its
- // WebSocket connection "Stream").
- ModeLabels map[string]string `json:"mode_labels,omitempty"`
- // ModeHintKey is the frontend locale key of the hint under the mode
- // choice; empty uses the generic hint.
- ModeHintKey string `json:"mode_hint_key,omitempty"`
- // SupportsThread reports whether replies can stay in a thread, which
- // enables the per-thread session mode.
- SupportsThread bool `json:"supports_thread"`
- // Links point at the platform's console and docs.
- Links []PlatformLink `json:"links,omitempty"`
- // ConfigSchema describes IMChannel.Credentials. Fields that apply to one
- // connection mode carry x-visible-if on "$mode", read from the channel
- // rather than from the credentials.
- ConfigSchema *configschema.Schema `json:"config_schema,omitempty"`
-}
-
-// PlatformLink is a link shown above the credential form.
-type PlatformLink struct {
- URL string `json:"url"`
- Title string `json:"title"`
- // TitleKey is the frontend locale key of the title.
- TitleKey string `json:"title_key,omitempty"`
-}
-
-// LookupPlatformInfo returns the metadata of a platform. A platform without
-// an entry gets its ID as name and WebSocket as its only mode, so a newly
-// registered adapter still shows up.
-func LookupPlatformInfo(id string) PlatformInfo {
- if build, ok := platformInfos[id]; ok {
- return build()
- }
- return PlatformInfo{ID: id, Name: id, Order: 1000, Modes: []string{ModeWebSocket}}
-}
-
-// KnownPlatformInfos returns the metadata of every platform this build
-// describes, whether or not its adapter is registered, sorted by ID.
-func KnownPlatformInfos() []PlatformInfo {
- out := make([]PlatformInfo, 0, len(platformInfos))
- for _, build := range platformInfos {
- out = append(out, build())
- }
- sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
- return out
-}
-
-// Platforms returns the platforms that have a registered adapter factory,
-// sorted by ID.
-func (s *Service) Platforms() []string {
- s.mu.RLock()
- defer s.mu.RUnlock()
- out := make([]string, 0, len(s.adapterFactories))
- for p := range s.adapterFactories {
- out = append(out, p)
- }
- sort.Strings(out)
- return out
-}
-
-// PlatformInfos returns the metadata of every registered platform in display
-// order.
-func (s *Service) PlatformInfos() []PlatformInfo {
- ids := s.Platforms()
- out := make([]PlatformInfo, 0, len(ids))
- for _, id := range ids {
- out = append(out, LookupPlatformInfo(id))
- }
- sort.SliceStable(out, func(i, j int) bool { return out[i].Order < out[j].Order })
- return out
-}
-
-// platformInfos builds each platform's metadata. Names follow the frontend's
-// agentEditor.im.* labels; field hints reuse its locale keys.
-var platformInfos = map[string]func() PlatformInfo{
- "wecom": func() PlatformInfo {
- wsEndpoint := text("WebSocket Endpoint", "wss://openws.work.weixin.qq.com", 3)
- describe(wsEndpoint, "Optional. Custom WebSocket address for private WeCom deployments.",
- "agentEditor.im.wecomWSEndpointHint")
- apiBase := text("API Base URL", "https://qyapi.weixin.qq.com", 16)
- describe(apiBase, "Optional. Custom API base URL for private WeCom deployments.",
- "agentEditor.im.wecomAPIBaseURLHint")
- agentID := &configschema.Schema{Type: configschema.TypeInteger, Title: "Corp Agent ID", Order: 15}
- return PlatformInfo{
- ID: "wecom", Name: "WeCom", Names: map[string]string{"zh-CN": "企业微信"}, Order: 10,
- Modes: []string{ModeWebSocket, ModeWebhook},
- Links: []PlatformLink{console("https://work.weixin.qq.com/", "WeCom Admin Console", "wecomConsole")},
- ConfigSchema: configschema.Object().
- Set("bot_id", onlyIn(ModeWebSocket, text("Bot ID", "Bot ID", 1)), false).
- Set("bot_secret", onlyIn(ModeWebSocket, secret("Bot Secret", "Bot Secret", 2)), false).
- Set("ws_endpoint", onlyIn(ModeWebSocket, wsEndpoint), false).
- Set("corp_id", onlyIn(ModeWebhook, text("Corp ID", "Corp ID", 11)), false).
- Set("agent_secret", onlyIn(ModeWebhook, secret("Agent Secret", "Agent Secret", 12)), false).
- Set("token", onlyIn(ModeWebhook, text("Token", "Token", 13)), false).
- Set("encoding_aes_key", onlyIn(ModeWebhook, text("EncodingAESKey", "EncodingAESKey", 14)), false).
- Set("corp_agent_id", onlyIn(ModeWebhook, agentID), false).
- Set("api_base_url", onlyIn(ModeWebhook, apiBase), false),
- }
- },
- "feishu": func() PlatformInfo {
- return feishuPlatform("feishu", "Feishu", "飞书", 20,
- console("https://open.feishu.cn/", "Feishu Open Platform", "feishuConsole"))
- },
- // Lark is Feishu's international cloud: same credentials, separate console.
- "lark": func() PlatformInfo {
- return feishuPlatform("lark", "Lark", "Lark(飞书国际版)", 30,
- console("https://open.larksuite.com/", "Lark Open Platform", "larkConsole"))
- },
- "dingtalk": func() PlatformInfo {
- card := text("Card Template ID (optional)", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.schema", 3)
- card.I18nKeys = map[string]string{"title": "agentEditor.im.dingtalkCardTemplateId"}
- describe(card, "Create an AI Card template to stream replies with a typewriter effect.",
- "agentEditor.im.dingtalkCardTemplateIdHint")
- return PlatformInfo{
- ID: "dingtalk", Name: "DingTalk", Names: map[string]string{"zh-CN": "钉钉"}, Order: 60,
- Modes: []string{ModeWebSocket},
- ModeLabels: map[string]string{ModeWebSocket: "Stream"},
- Links: []PlatformLink{
- console("https://open.dingtalk.com/", "DingTalk Open Platform", "dingtalkConsole"),
- },
- ConfigSchema: configschema.Object().
- Set("client_id", text("Client ID (AppKey)", "Client ID / AppKey", 1), false).
- Set("client_secret", secret("Client Secret (AppSecret)", "Client Secret / AppSecret", 2), false).
- Set("card_template_id", card, false),
- }
- },
- // WeChat binds by scanning a QR code; the editor runs that flow itself
- // and there is nothing to type. The bound fields are declared hidden so
- // the token is still redacted and kept like any other secret.
- "wechat": func() PlatformInfo {
- hidden := func(title string, secret bool) *configschema.Schema {
- return &configschema.Schema{Type: configschema.TypeString, Title: title, Widget: "hidden", Secret: secret}
- }
- return PlatformInfo{
- ID: "wechat", Name: "WeChat", Names: map[string]string{"zh-CN": "微信"}, Order: 80,
- Modes: []string{ModeLongPoll},
- ConfigSchema: configschema.Object().
- Set("bot_token", hidden("Bot Token", true), false).
- Set("ilink_bot_id", hidden("iLink Bot ID", false), false).
- Set("ilink_user_id", hidden("iLink User ID", false), false),
- }
- },
- "qqbot": func() PlatformInfo {
- apiBase := text("API Base URL", "https://api.sgroup.qq.com", 3)
- describe(apiBase, "Optional. Leave empty to use the default production API endpoint.",
- "agentEditor.im.qqbotAPIBaseURLHint")
- gateway := text("Gateway URL", "wss://api.sgroup.qq.com/websocket/", 4)
- describe(gateway, "Optional. Leave empty to fetch the common WebSocket gateway automatically.",
- "agentEditor.im.qqbotGatewayURLHint")
- return PlatformInfo{
- ID: "qqbot", Name: "QQBot", Order: 90,
- Modes: []string{ModeWebSocket},
- Links: []PlatformLink{console("https://q.qq.com/", "QQ Open Platform", "qqbotConsole")},
- ConfigSchema: configschema.Object().
- Set("app_id", text("App ID", "QQBot App ID", 1), false).
- Set("client_secret", secret("App Secret", "QQBot App Secret", 2), false).
- Set("api_base_url", apiBase, false).
- Set("gateway_url", gateway, false),
- }
- },
- "yunzhijia": yunzhijiaPlatform,
- "slack": func() PlatformInfo {
- return PlatformInfo{
- ID: "slack", Name: "Slack", Order: 40,
- Modes: []string{ModeWebSocket, ModeWebhook},
- SupportsThread: true,
- Links: []PlatformLink{console("https://api.slack.com/apps", "Slack API Console", "slackConsole")},
- ConfigSchema: configschema.Object().
- Set("app_token", onlyIn(ModeWebSocket, secret("App Token", "xapp-...", 1)), false).
- Set("bot_token", secret("Bot Token", "xoxb-...", 2), false).
- Set("signing_secret", onlyIn(ModeWebhook, secret("Signing Secret", "Signing Secret", 3)), false),
- }
- },
- "telegram": func() PlatformInfo {
- return PlatformInfo{
- ID: "telegram", Name: "Telegram", Order: 50,
- Modes: []string{ModeWebSocket, ModeWebhook},
- SupportsThread: true,
- Links: []PlatformLink{console("https://t.me/BotFather", "Telegram BotFather", "telegramConsole")},
- ConfigSchema: configschema.Object().
- Set("bot_token", secret("Bot Token", "123456789:AABBccdd...", 1), false).
- Set("secret_token", onlyIn(ModeWebhook, secret("Secret Token", "Secret Token (optional)", 2)), false),
- }
- },
- "mattermost": func() PlatformInfo {
- postToMain := &configschema.Schema{
- Type: configschema.TypeBoolean,
- Title: "Post replies in channel timeline",
- Default: false,
- Order: 5,
- }
- postToMain.I18nKeys = map[string]string{"title": "agentEditor.im.mattermostPostToMain"}
- describe(postToMain, "When on, bot replies are new top-level posts instead of thread replies.",
- "agentEditor.im.mattermostPostToMainHint")
- return PlatformInfo{
- ID: "mattermost", Name: "Mattermost", Order: 70,
- Modes: []string{ModeWebhook},
- ModeHintKey: "agentEditor.im.mattermostModeHint",
- SupportsThread: true,
- Links: []PlatformLink{console(
- "https://developers.mattermost.com/integrate/webhooks/outgoing/",
- "Mattermost integrations", "mattermostConsole",
- )},
- ConfigSchema: configschema.Object().
- Set("site_url", text("Site URL", "https://mattermost.example.com", 1), false).
- Set("bot_token", secret("Bot Token", "Bot Token", 2), false).
- Set("outgoing_token", secret("Outgoing Webhook Token", "Token from Outgoing Webhook", 3), false).
- Set("bot_user_id", text("Bot User ID", "Optional — filter bot self-messages", 4), false).
- Set("post_to_main", postToMain, false),
- }
- },
-}
-
-func feishuPlatform(id, name, zhName string, order int, link PlatformLink) PlatformInfo {
- apiBase := text("Base URL", "https://open.feishu.cn", 3)
- describe(apiBase, "Optional. Enter a reverse proxy URL if the server reaches Feishu through one.",
- "agentEditor.im.feishuAPIBaseURLHint")
- return PlatformInfo{
- ID: id, Name: name, Names: map[string]string{"zh-CN": zhName}, Order: order,
- Modes: []string{ModeWebSocket, ModeWebhook},
- SupportsThread: true,
- Links: []PlatformLink{link},
- ConfigSchema: configschema.Object().
- Set("app_id", text("App ID", "App ID", 1), false).
- Set("app_secret", secret("App Secret", "App Secret", 2), false).
- Set("api_base_url", apiBase, false).
- Set("verification_token", onlyIn(ModeWebhook, text("Verification Token", "Verification Token", 4)), false).
- Set("encrypt_key", onlyIn(ModeWebhook, secret("Encrypt Key", "Encrypt Key", 5)), false),
- }
-}
-
-func yunzhijiaPlatform() PlatformInfo {
- sendURL := text("Send Message URL",
- "https://www.yunzhijia.com/gateway/robot/webhook/send?yzjtype=0&yzjtoken=...", 1)
- sendURL.I18nKeys = map[string]string{"title": "agentEditor.im.yunzhijiaSendMsgUrl"}
- describe(sendURL, "Used for robot replies.", "agentEditor.im.yunzhijiaSendMsgUrlHint")
-
- sign := secret("Signature Secret (optional)", "HmacSHA1 signature secret from Yunzhijia robot settings", 2)
- localize(sign, "agentEditor.im.yunzhijiaSecret", "agentEditor.im.yunzhijiaSecretPlaceholder")
- describe(sign, "If set, incoming callbacks are verified with an HmacSHA1 signature.",
- "agentEditor.im.yunzhijiaSecretHint")
-
- appID := text("App ID (image download)", "Yunzhijia Open Platform App ID", 3)
- localize(appID, "agentEditor.im.yunzhijiaAppId", "agentEditor.im.yunzhijiaAppIdPlaceholder")
- describe(appID, "Used to obtain appAccessToken and download images users send.",
- "agentEditor.im.yunzhijiaAppCredentialHint")
-
- appSecret := secret("App Secret (image download)", "Yunzhijia Open Platform App Secret", 4)
- localize(appSecret, "agentEditor.im.yunzhijiaAppSecret", "agentEditor.im.yunzhijiaAppSecretPlaceholder")
-
- minTimeout, maxTimeout := 1.0, 60.0
- timeout := &configschema.Schema{
- Type: configschema.TypeInteger, Title: "HTTP Timeout (seconds)", Default: 10,
- Minimum: &minTimeout, Maximum: &maxTimeout, Placeholder: "10", Order: 5,
- I18nKeys: map[string]string{"title": "agentEditor.im.yunzhijiaTimeout"},
- }
- describe(timeout, "Timeout for sending replies via Send Message URL.", "agentEditor.im.yunzhijiaTimeoutHint")
-
- suffix := text("Allowed Host Suffix", "yunzhijia.com", 6)
- suffix.Default = "yunzhijia.com"
- suffix.I18nKeys = map[string]string{"title": "agentEditor.im.yunzhijiaAllowedHostSuffix"}
- describe(suffix, "Restrict the Send Message URL host to this suffix.",
- "agentEditor.im.yunzhijiaAllowedHostSuffixHint")
-
- return PlatformInfo{
- ID: "yunzhijia", Name: "Yunzhijia", Names: map[string]string{"zh-CN": "云之家"}, Order: 100,
- Modes: []string{ModeWebhook, ModeWebSocket},
- ModeHintKey: "agentEditor.im.yunzhijiaModeHint",
- SupportsThread: true,
- Links: []PlatformLink{
- console("https://www.yunzhijia.com/opendocs/docs.html#/guide/im/robot",
- "Robot messaging docs", "yunzhijiaRobotDoc"),
- console("https://www.yunzhijia.com/developers/", "Developer platform (images)", "yunzhijiaImageDoc"),
- },
- ConfigSchema: configschema.Object().
- Set("send_msg_url", sendURL, true).
- Set("secret", sign, false).
- Set("app_id", appID, false).
- Set("app_secret", appSecret, false).
- Set("timeout_seconds", timeout, false).
- Set("allowed_webhook_host_suffix", suffix, true),
- }
-}
-
-func text(title, placeholder string, order int) *configschema.Schema {
- return &configschema.Schema{Type: configschema.TypeString, Title: title, Placeholder: placeholder, Order: order}
-}
-
-// secret is a password input. IM credentials are returned to admins as
-// stored, so the form shows the saved value masked rather than redacted.
-func secret(title, placeholder string, order int) *configschema.Schema {
- s := text(title, placeholder, order)
- s.Secret = true
- s.Widget = "password"
- return s
-}
-
-func onlyIn(mode string, s *configschema.Schema) *configschema.Schema {
- s.VisibleIf = map[string]any{"$mode": mode}
- return s
-}
-
-func describe(s *configschema.Schema, description, key string) {
- s.Description = description
- if s.I18nKeys == nil {
- s.I18nKeys = map[string]string{}
- }
- s.I18nKeys["description"] = key
-}
-
-func localize(s *configschema.Schema, titleKey, placeholderKey string) {
- s.I18nKeys = map[string]string{"title": titleKey, "placeholder": placeholderKey}
-}
-
-func console(url, title, key string) PlatformLink {
- return PlatformLink{URL: url, Title: title, TitleKey: "agentEditor.im." + key}
-}
diff --git a/internal/im/platforms_test.go b/internal/im/platforms_test.go
deleted file mode 100644
index 4fdc952e5..000000000
--- a/internal/im/platforms_test.go
+++ /dev/null
@@ -1,87 +0,0 @@
-package im
-
-import (
- "testing"
-
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
-)
-
-// registeredPlatforms are the adapters container.registerIMService wires.
-var registeredPlatforms = []string{
- "wecom", "feishu", "lark", "slack", "telegram", "dingtalk", "mattermost", "wechat", "qqbot", "yunzhijia",
-}
-
-func TestEveryPlatformHasValidMetadata(t *testing.T) {
- for _, id := range registeredPlatforms {
- info := LookupPlatformInfo(id)
- if info.Order >= 1000 {
- t.Errorf("%s: no metadata entry", id)
- continue
- }
- if len(info.Modes) == 0 {
- t.Errorf("%s: no connection mode", id)
- }
- if info.ConfigSchema == nil {
- t.Errorf("%s: no config schema", id)
- continue
- }
- if err := info.ConfigSchema.Check(); err != nil {
- t.Errorf("%s: invalid schema: %v", id, err)
- }
- // Mode-dependent fields may only name modes the platform supports.
- for key, prop := range info.ConfigSchema.Properties {
- if mode, ok := prop.VisibleIf["$mode"]; ok && !contains(info.Modes, mode.(string)) {
- t.Errorf("%s.%s: visible only in unsupported mode %q", id, key, mode)
- }
- }
- }
-}
-
-func TestWeComFieldsFollowTheMode(t *testing.T) {
- s := LookupPlatformInfo("wecom").ConfigSchema
- visibleIn := func(mode string) map[string]bool {
- out := map[string]bool{}
- for key, prop := range s.Properties {
- if m, ok := prop.VisibleIf["$mode"]; !ok || m == mode {
- out[key] = true
- }
- }
- return out
- }
- ws, hook := visibleIn(ModeWebSocket), visibleIn(ModeWebhook)
- if !ws["bot_id"] || ws["corp_id"] || !hook["corp_id"] || hook["bot_secret"] {
- t.Fatalf("websocket=%v webhook=%v", ws, hook)
- }
-}
-
-func TestYunzhijiaRequiresTheSendURL(t *testing.T) {
- s := LookupPlatformInfo("yunzhijia").ConfigSchema
- errs := configschema.ValidateInContext(s, map[string]any{
- "allowed_webhook_host_suffix": "yunzhijia.com", "timeout_seconds": 10.0,
- }, map[string]any{"mode": ModeWebhook})
- if len(errs) != 1 || errs[0].Path != "send_msg_url" || errs[0].Code != configschema.CodeRequired {
- t.Fatalf("want send_msg_url required, got %v", errs)
- }
- errs = configschema.Validate(s, map[string]any{
- "send_msg_url": "https://x", "allowed_webhook_host_suffix": "yunzhijia.com", "timeout_seconds": 99.0,
- })
- if len(errs) != 1 || errs[0].Code != configschema.CodeMaximum {
- t.Fatalf("want timeout maximum error, got %v", errs)
- }
-}
-
-func TestUnknownPlatformFallsBack(t *testing.T) {
- info := LookupPlatformInfo("brand-new")
- if info.Name != "brand-new" || len(info.Modes) != 1 {
- t.Fatalf("fallback = %+v", info)
- }
-}
-
-func contains(list []string, v string) bool {
- for _, x := range list {
- if x == v {
- return true
- }
- }
- return false
-}
diff --git a/internal/infrastructure/docparser/engine_registry.go b/internal/infrastructure/docparser/engine_registry.go
index f378fd000..998792182 100644
--- a/internal/infrastructure/docparser/engine_registry.go
+++ b/internal/infrastructure/docparser/engine_registry.go
@@ -3,9 +3,6 @@ package docparser
import (
"context"
"fmt"
- "sort"
- "strings"
- "sync"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
@@ -44,102 +41,25 @@ type ReaderDeps struct {
WeKnoraCloudCredentials func(ctx context.Context) *types.WeKnoraCloudCredentials
}
-// enginesMu guards the registries: plugin engines come and go while the
-// server runs.
-var enginesMu sync.RWMutex
-
// localEngines holds all locally registered parser engines, in registration
// order — which is also the order the engine list is shown in.
var localEngines []EngineRegistration
-// pluginEngines holds engines installed plugins provide, by qualified name
-// ("acme.ocr/ocr"). They are kept apart from localEngines so the builtin
-// plugin catalog never describes them as builtins.
-var pluginEngines = map[string]EngineRegistration{}
-
-// PluginEngineInfo is what a plugin engine adds to the engine list.
-type PluginEngineInfo interface {
- PluginID() string
- // DisplayNames localizes the engine's name, keyed by locale.
- DisplayNames() map[string]string
-}
-
// RegisterEngine adds an engine to the local registry. Called from init().
func RegisterEngine(e EngineRegistration) {
- enginesMu.Lock()
- defer enginesMu.Unlock()
localEngines = append(localEngines, e)
}
-// RegisterPluginEngine adds or replaces a plugin's engine. It refuses a name
-// a builtin engine uses.
-func RegisterPluginEngine(e EngineRegistration) error {
- enginesMu.Lock()
- defer enginesMu.Unlock()
- for _, builtin := range localEngines {
- if builtin.Name() == e.Name() {
- return fmt.Errorf("parser engine %s already exists", e.Name())
- }
- }
- pluginEngines[e.Name()] = e
- return nil
-}
-
-// UnregisterPluginEngine removes a plugin's engine.
-func UnregisterPluginEngine(name string) {
- enginesMu.Lock()
- defer enginesMu.Unlock()
- delete(pluginEngines, name)
-}
-
-// Engines returns the builtin engines in registration order.
-func Engines() []EngineRegistration {
- enginesMu.RLock()
- defer enginesMu.RUnlock()
- out := make([]EngineRegistration, len(localEngines))
- copy(out, localEngines)
- return out
-}
-
-// pluginEngineList returns the plugin engines sorted by name.
-func pluginEngineList() []EngineRegistration {
- enginesMu.RLock()
- defer enginesMu.RUnlock()
- out := make([]EngineRegistration, 0, len(pluginEngines))
- for _, e := range pluginEngines {
- out = append(out, e)
- }
- sort.Slice(out, func(i, j int) bool { return out[i].Name() < out[j].Name() })
- return out
-}
-
-// PluginFileTypes returns the file types plugin engines parse, so uploads
-// of those types are accepted.
-func PluginFileTypes() []string {
- var out []string
- for _, e := range pluginEngineList() {
- out = append(out, e.FileTypes(true)...)
- }
- return out
-}
-
-// lookupEngine returns the registered engine with this name.
+// lookupEngine returns the locally registered engine with this name.
func lookupEngine(name string) (EngineRegistration, bool) {
- enginesMu.RLock()
- defer enginesMu.RUnlock()
for _, engine := range localEngines {
if engine.Name() == name {
return engine, true
}
}
- e, ok := pluginEngines[name]
- return e, ok
+ return nil, false
}
-// isPluginEngineName reports whether a name is a qualified plugin
-// contribution ID, which no builtin or docreader engine uses.
-func isPluginEngineName(name string) bool { return strings.Contains(name, "/") }
-
// NewReader builds the reader for an engine.
//
// An empty engine name means "no explicit choice": simple formats are handled
@@ -152,11 +72,6 @@ func NewReader(
if registration, ok := lookupEngine(engine); ok {
return registration.NewReader(ctx, deps)
}
- if isPluginEngineName(engine) {
- // Its plugin was uninstalled or disabled: the Python docreader does
- // not know it either, so say so instead of sending it there.
- return nil, errEngineUnavailable(engine, "its plugin is not installed or not running")
- }
if engine == "" && !isURL && IsSimpleFormat(fileType) {
return &SimpleFormatReader{}, nil
}
@@ -190,11 +105,10 @@ func ListAllEngines(
remoteMap[re.Name] = re
}
- locals := Engines()
- seen := make(map[string]bool, len(locals))
- result := make([]types.ParserEngineInfo, 0, len(locals)+len(remoteEngines))
+ seen := make(map[string]bool, len(localEngines))
+ result := make([]types.ParserEngineInfo, 0, len(localEngines)+len(remoteEngines))
- for _, e := range locals {
+ for _, e := range localEngines {
name := e.Name()
seen[name] = true
@@ -227,18 +141,6 @@ func ListAllEngines(
result = append(result, re)
}
- for _, e := range pluginEngineList() {
- available, reason := e.CheckAvailable(docreaderConnected, overrides)
- info := types.ParserEngineInfo{
- Name: e.Name(), Description: e.Description(), FileTypes: e.FileTypes(docreaderConnected),
- Available: available, UnavailableReason: reason,
- }
- if meta, ok := e.(PluginEngineInfo); ok {
- info.PluginID, info.DisplayNames = meta.PluginID(), meta.DisplayNames()
- }
- result = append(result, info)
- }
-
return result
}
diff --git a/internal/infrastructure/web_search/registry.go b/internal/infrastructure/web_search/registry.go
index fcadc6684..16e273959 100644
--- a/internal/infrastructure/web_search/registry.go
+++ b/internal/infrastructure/web_search/registry.go
@@ -2,7 +2,6 @@ package web_search
import (
"fmt"
- "sort"
"sync"
"github.com/Tencent/WeKnora/internal/types"
@@ -17,72 +16,14 @@ type ProviderFactory func(params types.WebSearchProviderParameters) (interfaces.
// Instances are created on-demand with tenant-specific parameters.
type Registry struct {
factories map[string]ProviderFactory
- // plugins describes the types installed plugins registered; builtin
- // types are described by types.GetWebSearchProviderTypes.
- plugins map[string]PluginType
- mu sync.RWMutex
-}
-
-// PluginType is a provider type an installed plugin contributes.
-type PluginType struct {
- Info types.WebSearchProviderTypeInfo
- // Validate checks instance parameters against the plugin's schema.
- Validate func(params types.WebSearchProviderParameters) error
+ mu sync.RWMutex
}
// NewRegistry creates a new web search provider registry
func NewRegistry() *Registry {
return &Registry{
factories: make(map[string]ProviderFactory),
- plugins: make(map[string]PluginType),
- }
-}
-
-// RegisterPlugin adds or replaces a plugin's provider type. It refuses to
-// shadow a builtin type.
-func (r *Registry) RegisterPlugin(t PluginType, factory ProviderFactory) error {
- r.mu.Lock()
- defer r.mu.Unlock()
- id := t.Info.ID
- if _, exists := r.factories[id]; exists {
- if _, isPlugin := r.plugins[id]; !isPlugin {
- return fmt.Errorf("web search provider type %s already exists", id)
- }
- }
- r.factories[id] = factory
- r.plugins[id] = t
- return nil
-}
-
-// Unregister removes a plugin's provider type; builtins stay.
-func (r *Registry) Unregister(id string) {
- r.mu.Lock()
- defer r.mu.Unlock()
- if _, isPlugin := r.plugins[id]; !isPlugin {
- return
- }
- delete(r.plugins, id)
- delete(r.factories, id)
-}
-
-// PluginType returns a plugin provider type.
-func (r *Registry) PluginType(id string) (PluginType, bool) {
- r.mu.RLock()
- defer r.mu.RUnlock()
- t, ok := r.plugins[id]
- return t, ok
-}
-
-// PluginTypes returns the plugin provider types, sorted by ID.
-func (r *Registry) PluginTypes() []types.WebSearchProviderTypeInfo {
- r.mu.RLock()
- defer r.mu.RUnlock()
- out := make([]types.WebSearchProviderTypeInfo, 0, len(r.plugins))
- for _, t := range r.plugins {
- out = append(out, t.Info)
}
- sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
- return out
}
// Register registers a provider type factory by ID
@@ -92,18 +33,6 @@ func (r *Registry) Register(id string, factory ProviderFactory) {
r.factories[id] = factory
}
-// Types returns the registered provider type IDs, sorted.
-func (r *Registry) Types() []string {
- r.mu.RLock()
- defer r.mu.RUnlock()
- out := make([]string, 0, len(r.factories))
- for id := range r.factories {
- out = append(out, id)
- }
- sort.Strings(out)
- return out
-}
-
// CreateProvider creates a provider instance by type with the given parameters.
func (r *Registry) CreateProvider(providerType string, params types.WebSearchProviderParameters) (interfaces.WebSearchProvider, error) {
r.mu.RLock()
diff --git a/internal/mcp/client.go b/internal/mcp/client.go
index c98b2c89c..baa42d1b5 100644
--- a/internal/mcp/client.go
+++ b/internal/mcp/client.go
@@ -231,15 +231,7 @@ func NewMCPClient(config *ClientConfig) (MCPClient, error) {
// Stdio transport is disabled for security reasons (potential command injection vulnerabilities)
return nil, fmt.Errorf("stdio transport is disabled for security reasons; please use SSE or HTTP Streamable transport instead")
default:
- factory, ok := registeredTransport(config.Service.TransportType)
- if !ok {
- return nil, ErrUnsupportedTransport
- }
- t, err := factory(config.Service)
- if err != nil {
- return nil, err
- }
- mcpClient = client.NewClient(t)
+ return nil, ErrUnsupportedTransport
}
instance := &mcpGoClient{
@@ -299,7 +291,7 @@ func buildOAuthConfig(config *ClientConfig, httpClient *http.Client) (transport.
// onConnectionLost callback when the connection is lost
func (c *mcpGoClient) onConnectionLost(err error) {
_ = c.Disconnect()
- logger.Warnf(context.Background(), "MCP server connection has been lost, %s, error:%v", target(c.service), err)
+ logger.Warnf(context.Background(), "MCP server connection has been lost, URL:%s, error:%v", *c.service.URL, err)
}
// checkErrorAndDisconnectIfNeeded checks for transport errors that indicate the
@@ -363,7 +355,7 @@ func (c *mcpGoClient) Connect(ctx context.Context) error {
logger.GetLogger(ctx).Infof("MCP stdio client connected: %s %v",
c.service.StdioConfig.Command, c.service.StdioConfig.Args)
} else {
- logger.GetLogger(ctx).Infof("MCP client connected to %s", target(c.service))
+ logger.GetLogger(ctx).Infof("MCP client connected to %s", *c.service.URL)
}
return nil
}
@@ -625,9 +617,8 @@ func (c *mcpGoClient) CallTool(ctx context.Context, name string, args map[string
}
return &CallToolResult{
- IsError: result.IsError,
- Content: content,
- StructuredContent: result.StructuredContent,
+ IsError: result.IsError,
+ Content: content,
}, nil
}
diff --git a/internal/mcp/transports.go b/internal/mcp/transports.go
deleted file mode 100644
index 43f01c8ea..000000000
--- a/internal/mcp/transports.go
+++ /dev/null
@@ -1,44 +0,0 @@
-package mcp
-
-import (
- "sync"
-
- "github.com/mark3labs/mcp-go/client/transport"
-
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-// TransportFactory builds the transport of a service whose transport type
-// another package provides, such as MCP servers plugins serve themselves.
-type TransportFactory func(svc *types.MCPService) (transport.Interface, error)
-
-var (
- transportsMu sync.RWMutex
- transports = map[types.MCPTransportType]TransportFactory{}
-)
-
-// RegisterTransport makes NewMCPClient build services of a transport type
-// with f. Registering again replaces the factory.
-func RegisterTransport(kind types.MCPTransportType, f TransportFactory) {
- transportsMu.Lock()
- defer transportsMu.Unlock()
- transports[kind] = f
-}
-
-func registeredTransport(kind types.MCPTransportType) (TransportFactory, bool) {
- transportsMu.RLock()
- defer transportsMu.RUnlock()
- f, ok := transports[kind]
- return f, ok
-}
-
-// target names what a client talks to, for logs.
-func target(svc *types.MCPService) string {
- if svc.URL != nil && *svc.URL != "" {
- return "URL:" + *svc.URL
- }
- if svc.PluginID != "" {
- return "plugin:" + svc.PluginID + "/" + svc.PluginServer
- }
- return "transport:" + string(svc.TransportType)
-}
diff --git a/internal/mcp/types.go b/internal/mcp/types.go
index 02d30a329..8316a04c0 100644
--- a/internal/mcp/types.go
+++ b/internal/mcp/types.go
@@ -45,8 +45,6 @@ type ServerInfo struct {
type CallToolResult struct {
Content []ContentItem `json:"content"`
IsError bool `json:"isError,omitempty"`
- // StructuredContent is the tool's structured result, if it returns one.
- StructuredContent any `json:"structuredContent,omitempty"`
}
// ContentItem represents a content item in tool result
diff --git a/internal/models/providers/config_schema.go b/internal/models/providers/config_schema.go
deleted file mode 100644
index 61f2b1978..000000000
--- a/internal/models/providers/config_schema.go
+++ /dev/null
@@ -1,64 +0,0 @@
-package providers
-
-import "github.com/Tencent/WeKnora/internal/plugin/configschema"
-
-// ExtraFieldsSchema describes a vendor's extra fields as a config schema.
-//
-// The values live in models.parameters.extra_config, a map of strings, so
-// every field is a string: number and boolean fields only pick a widget, and
-// a boolean is the choice between "true" and "false". A field is secret when
-// it says so or is a password input, which is how the model editor already
-// treats it.
-func ExtraFieldsSchema(fields []ExtraField) *configschema.Schema {
- s := configschema.Object()
- for i, f := range fields {
- prop := &configschema.Schema{
- Type: configschema.TypeString,
- Title: f.Label,
- Placeholder: f.Placeholder,
- Secret: f.Secret || f.Type == "password",
- Order: i,
- }
- if f.Default != "" {
- prop.Default = f.Default
- }
- addI18n(prop, "title", f.Labels)
- addI18n(prop, "placeholder", f.Placeholders)
- for _, mt := range f.ModelTypes {
- prop.ModelTypes = append(prop.ModelTypes, string(mt))
- }
- switch f.Type {
- case "password":
- prop.Widget = "password"
- case "number":
- prop.Widget = "number"
- case "boolean":
- prop.Widget = "switch"
- prop.OneOf = []*configschema.Schema{{Const: "true"}, {Const: "false"}}
- case "select":
- prop.Widget = "select"
- for _, o := range f.Options {
- choice := &configschema.Schema{Const: o.Value, Title: o.Label}
- addI18n(choice, "title", o.Labels)
- prop.OneOf = append(prop.OneOf, choice)
- }
- }
- s.Set(f.Key, prop, f.Required)
- }
- return s
-}
-
-func addI18n(s *configschema.Schema, key string, locales map[string]string) {
- for locale, text := range locales {
- if text == "" {
- continue
- }
- if s.I18n == nil {
- s.I18n = map[string]map[string]string{}
- }
- if s.I18n[key] == nil {
- s.I18n[key] = map[string]string{}
- }
- s.I18n[key][locale] = text
- }
-}
diff --git a/internal/models/providers/config_schema_test.go b/internal/models/providers/config_schema_test.go
deleted file mode 100644
index b6e67c9a5..000000000
--- a/internal/models/providers/config_schema_test.go
+++ /dev/null
@@ -1,73 +0,0 @@
-package providers
-
-import (
- "testing"
-
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-func TestExtraFieldsSchemaCoversEveryBuiltin(t *testing.T) {
- for _, d := range Builtins() {
- if len(d.ExtraFields) == 0 {
- continue
- }
- s := ExtraFieldsSchema(d.ExtraFields)
- if err := s.Check(); err != nil {
- t.Errorf("%s: schema invalid: %v", d.ID, err)
- }
- if len(s.Properties) != len(d.ExtraFields) {
- t.Errorf("%s: %d properties for %d fields", d.ID, len(s.Properties), len(d.ExtraFields))
- }
- for _, f := range d.ExtraFields {
- p := s.Properties[f.Key]
- if p == nil {
- t.Errorf("%s: field %s missing", d.ID, f.Key)
- continue
- }
- if p.Secret != (f.Secret || f.Type == "password") {
- t.Errorf("%s.%s: secret = %v", d.ID, f.Key, p.Secret)
- }
- }
- // Defaults are valid values of their own field.
- defaults := map[string]any{}
- for key, p := range s.Properties {
- if p.Default != nil {
- defaults[key] = p.Default
- }
- }
- for _, e := range configschema.Validate(s, defaults) {
- if e.Code != configschema.CodeRequired {
- t.Errorf("%s: default fails validation: %v", d.ID, e)
- }
- }
- }
-}
-
-func TestExtraFieldsSchemaMapping(t *testing.T) {
- s := ExtraFieldsSchema([]ExtraField{
- {
- Key: "region", Label: "Region", Labels: map[string]string{"zh-CN": "地域"}, Type: "select", Required: true,
- Options: []ExtraFieldOption{{Label: "Beijing", Labels: map[string]string{"zh-CN": "北京"}, Value: "bj"}},
- },
- {Key: "sk", Label: "Secret Key", Type: "password", ModelTypes: []types.ModelType{types.ModelTypeRerank}},
- {Key: "stream", Label: "Stream", Type: "boolean", Default: "true"},
- })
- region := s.Properties["region"]
- if region.Widget != "select" || len(region.OneOf) != 1 || region.OneOf[0].Const != "bj" ||
- region.OneOf[0].I18n["title"]["zh-CN"] != "北京" || region.I18n["title"]["zh-CN"] != "地域" {
- t.Fatalf("select field = %+v", region)
- }
- if len(s.Required) != 1 || s.Required[0] != "region" {
- t.Fatalf("required = %v", s.Required)
- }
- sk := s.Properties["sk"]
- if !sk.Secret || sk.Widget != "password" ||
- len(sk.ModelTypes) != 1 || sk.ModelTypes[0] != string(types.ModelTypeRerank) {
- t.Fatalf("password field = %+v", sk)
- }
- if errs := configschema.Validate(s, map[string]any{"region": "bj", "stream": "yes"}); len(errs) != 1 ||
- errs[0].Path != "stream" || errs[0].Code != configschema.CodeEnum {
- t.Fatalf("boolean must accept only true/false strings, got %v", errs)
- }
-}
diff --git a/internal/models/runtime/overlay.go b/internal/models/runtime/overlay.go
index 5cc11d70a..3f4210041 100644
--- a/internal/models/runtime/overlay.go
+++ b/internal/models/runtime/overlay.go
@@ -151,9 +151,20 @@ func (rt *Runtime) applyOverlayValidated(data []byte, baseDir string, validate f
}
vendor, exists := next[id]
if !exists {
- vendor = newOverlayVendor(id)
+ vendor = &Provider{Definition: &providers.Definition{
+ ID: id,
+ Name: id,
+ API: api.APIOpenAICompletions,
+ DefaultBaseURLs: map[types.ModelType]string{},
+ ModelTypes: []types.ModelType{
+ types.ModelTypeKnowledgeQA,
+ },
+ RequiresAuth: true,
+ Auth: providers.AuthBearer,
+ Order: 1000,
+ }, catalog: catalog.New(nil)}
}
- if err := applyOverlayProvider(vendor, p, baseDir, interpolateEnv); err != nil {
+ if err := applyOverlayProvider(vendor, p, baseDir); err != nil {
return fmt.Errorf("provider %s: %w", id, err)
}
normalizeProvider(vendor)
@@ -169,26 +180,7 @@ func (rt *Runtime) applyOverlayValidated(data []byte, baseDir string, validate f
return nil
}
-// newOverlayVendor is the starting point of a vendor an overlay declares.
-func newOverlayVendor(id string) *Provider {
- return &Provider{Definition: &providers.Definition{
- ID: id,
- Name: id,
- API: api.APIOpenAICompletions,
- DefaultBaseURLs: map[types.ModelType]string{},
- ModelTypes: []types.ModelType{
- types.ModelTypeKnowledgeQA,
- },
- RequiresAuth: true,
- Auth: providers.AuthBearer,
- Order: 1000,
- }, catalog: catalog.New(nil)}
-}
-
-// applyOverlayProvider patches v. expand rewrites URLs, the API key and
-// header values: the deployment overlay expands environment variables, a
-// plugin's definition is taken literally.
-func applyOverlayProvider(v *Provider, p OverlayProvider, baseDir string, expand func(string) string) error {
+func applyOverlayProvider(v *Provider, p OverlayProvider, baseDir string) error {
entries := v.Models()
if p.Name != "" {
v.Name = p.Name
@@ -212,22 +204,22 @@ func applyOverlayProvider(v *Provider, p OverlayProvider, baseDir string, expand
v.API = p.API
}
if p.BaseURL != "" {
- v.DefaultBaseURLs[types.ModelTypeKnowledgeQA] = expand(p.BaseURL)
+ v.DefaultBaseURLs[types.ModelTypeKnowledgeQA] = interpolateEnv(p.BaseURL)
}
for k, u := range p.BaseURLs {
t, ok := models.ParseModelType(k)
if !ok {
return fmt.Errorf("unknown model type %q in base_urls", k)
}
- v.DefaultBaseURLs[t] = expand(u)
+ v.DefaultBaseURLs[t] = interpolateEnv(u)
}
if p.APIKey != "" {
- v.DefaultAPIKey = expand(p.APIKey)
+ v.DefaultAPIKey = interpolateEnv(p.APIKey)
}
if len(p.Headers) > 0 {
headers := make(map[string]string, len(p.Headers))
for k, val := range p.Headers {
- headers[k] = expand(val)
+ headers[k] = interpolateEnv(val)
}
v.Headers = headers
}
diff --git a/internal/models/runtime/plugin.go b/internal/models/runtime/plugin.go
deleted file mode 100644
index 332564684..000000000
--- a/internal/models/runtime/plugin.go
+++ /dev/null
@@ -1,80 +0,0 @@
-package runtime
-
-import (
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "strings"
-)
-
-// RegisterPlugin adds a vendor an installed plugin declares. The definition
-// uses the deployment overlay format for one provider, with two limits: it
-// is taken literally (no ${ENV} expansion, which would hand a plugin the
-// server's environment) and it carries no deployment API key, since every
-// workspace brings its own. It cannot replace a built-in or operator
-// vendor; registering the same plugin vendor again replaces it. baseDir
-// resolves the icon path inside the plugin package.
-//
-// The vendor joins the baseline, so later deployment overlay reloads keep
-// it and may patch it like a built-in.
-func (rt *Runtime) RegisterPlugin(id string, definition []byte, baseDir string) error {
- id = strings.ToLower(strings.TrimSpace(id))
- if id == "" {
- return errors.New("vendor id is empty")
- }
- var p OverlayProvider
- dec := json.NewDecoder(bytesReader(definition))
- dec.DisallowUnknownFields()
- if err := dec.Decode(&p); err != nil {
- return fmt.Errorf("parse vendor definition: %w", err)
- }
- if err := dec.Decode(new(any)); err != io.EOF {
- return errors.New("parse vendor definition: expected a single JSON document")
- }
- if p.APIKey != "" {
- return errors.New("a plugin vendor cannot set api_key; workspaces supply their own keys")
- }
- vendor := newOverlayVendor(id)
- if err := applyOverlayProvider(vendor, p, baseDir, func(s string) string { return s }); err != nil {
- return err
- }
- normalizeProvider(vendor)
- if err := validateDefinition(vendor); err != nil {
- return err
- }
-
- rt.mu.Lock()
- defer rt.mu.Unlock()
- if _, taken := rt.providers[id]; taken && !rt.plugins[id] {
- return fmt.Errorf("vendor %s already exists", id)
- }
- if rt.plugins == nil {
- rt.plugins = map[string]bool{}
- }
- rt.plugins[id] = true
- rt.providers[id] = vendor
- rt.builtins[id] = vendor.clone()
- return nil
-}
-
-// Unregister removes a vendor RegisterPlugin added. Other vendors stay.
-func (rt *Runtime) Unregister(id string) {
- id = strings.ToLower(strings.TrimSpace(id))
- rt.mu.Lock()
- defer rt.mu.Unlock()
- if !rt.plugins[id] {
- return
- }
- delete(rt.plugins, id)
- delete(rt.providers, id)
- delete(rt.builtins, id)
-}
-
-// RegisterPlugin adds a plugin vendor to the default runtime.
-func RegisterPlugin(id string, definition []byte, baseDir string) error {
- return Default().RegisterPlugin(id, definition, baseDir)
-}
-
-// Unregister removes a plugin vendor from the default runtime.
-func Unregister(id string) { Default().Unregister(id) }
diff --git a/internal/models/runtime/plugin_test.go b/internal/models/runtime/plugin_test.go
deleted file mode 100644
index 4b1024532..000000000
--- a/internal/models/runtime/plugin_test.go
+++ /dev/null
@@ -1,57 +0,0 @@
-package runtime
-
-import (
- "strings"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-func TestRegisterPluginVendor(t *testing.T) {
- t.Setenv("SECRET_FOR_TEST", "leaked")
- rt := New()
- def := `{"name":"ACME AI","base_url":"https://api.acme.example/${SECRET_FOR_TEST}/v1",
- "headers":{"X-Leak":"$SECRET_FOR_TEST"},"model_types":["chat","embedding"],
- "models":[{"id":"acme-large","context_window":128000}]}`
- if err := rt.RegisterPlugin("acme.ai/acme", []byte(def), t.TempDir()); err != nil {
- t.Fatalf("RegisterPlugin: %v", err)
- }
- v, ok := rt.Get("acme.ai/acme")
- if !ok {
- t.Fatal("vendor not registered")
- }
- if url := v.DefaultBaseURLs[types.ModelTypeKnowledgeQA]; strings.Contains(url, "leaked") ||
- strings.Contains(v.Headers["X-Leak"], "leaked") {
- t.Fatalf("a plugin must not read the server environment: %s %v", url, v.Headers)
- }
- if _, ok := v.FindModel("acme-large", types.ModelTypeKnowledgeQA); !ok {
- t.Fatal("catalog entry missing")
- }
-
- // A deployment overlay reload keeps plugin vendors.
- if err := rt.Reload([]byte(`{"providers":{}}`), ""); err != nil {
- t.Fatal(err)
- }
- if _, ok := rt.Get("acme.ai/acme"); !ok {
- t.Fatal("reload dropped the plugin vendor")
- }
-
- if err := rt.RegisterPlugin("openai", []byte(`{"name":"Fake"}`), ""); err == nil {
- t.Fatal("a plugin must not replace a built-in vendor")
- }
- if err := rt.RegisterPlugin("acme.ai/keyed", []byte(`{"api_key":"sk-1"}`), ""); err == nil {
- t.Fatal("a plugin vendor must not carry a deployment key")
- }
- if err := rt.RegisterPlugin("acme.ai/typo", []byte(`{"nmae":"x"}`), ""); err == nil {
- t.Fatal("unknown keys must be rejected")
- }
-
- rt.Unregister("openai")
- if _, ok := rt.Get("openai"); !ok {
- t.Fatal("Unregister must not remove built-ins")
- }
- rt.Unregister("acme.ai/acme")
- if _, ok := rt.Get("acme.ai/acme"); ok {
- t.Fatal("Unregister left the vendor")
- }
-}
diff --git a/internal/models/runtime/registry.go b/internal/models/runtime/registry.go
index c5460d81d..c7606f30f 100644
--- a/internal/models/runtime/registry.go
+++ b/internal/models/runtime/registry.go
@@ -48,8 +48,6 @@ type Runtime struct {
mu sync.RWMutex
providers map[string]*Provider
builtins map[string]*Provider
- // plugins marks the vendors installed plugins added (RegisterPlugin).
- plugins map[string]bool
}
// New explicitly composes fresh built-in definitions and independent catalogs.
diff --git a/internal/plugin/activate/connectors.go b/internal/plugin/activate/connectors.go
deleted file mode 100644
index f04640260..000000000
--- a/internal/plugin/activate/connectors.go
+++ /dev/null
@@ -1,354 +0,0 @@
-package activate
-
-import (
- "context"
- "encoding/json"
- "fmt"
- "sort"
- "sync"
- "time"
-
- "github.com/Tencent/WeKnora/internal/datasource"
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// Connectors registers code plugins' data source connectors. A plugin
-// connector's instanceSchema splits in two: fields in x-group "settings"
-// are non-secret options (DataSourceConfig.Settings), everything else is
-// credentials (encrypted at rest).
-type Connectors struct {
- iv *Invoker
- registry *datasource.ConnectorRegistry
-
- mu sync.Mutex
- registered map[string][]string
-}
-
-// NewConnectors creates the connector activator.
-func NewConnectors(iv *Invoker, registry *datasource.ConnectorRegistry) *Connectors {
- return &Connectors{iv: iv, registry: registry, registered: map[string][]string{}}
-}
-
-// Name implements reconcile.Activator.
-func (a *Connectors) Name() string { return "connectors" }
-
-// settingsGroup marks instanceSchema fields that are settings, not
-// credentials.
-const settingsGroup = "settings"
-
-// Activate implements reconcile.Activator: all of a plugin's connectors or
-// none.
-func (a *Connectors) Activate(_ context.Context, l *reconcile.Loaded) error {
- var ids []string
- for _, c := range l.Manifest.Contributes[manifest.PointConnectors] {
- id := manifest.QualifiedID(l.Manifest.ID, c.ID)
- meta, err := connectorMetadata(l, c, id)
- if err == nil {
- rc := &remoteConnector{iv: a.iv, m: l.Manifest, local: c.ID, typeID: id}
- err = a.registry.RegisterPlugin(rc, meta)
- }
- if err != nil {
- for _, done := range ids {
- a.registry.Unregister(done)
- }
- return fmt.Errorf("connector %s: %w", c.ID, err)
- }
- ids = append(ids, id)
- }
- a.mu.Lock()
- a.registered[l.Manifest.ID] = ids
- a.mu.Unlock()
- return nil
-}
-
-// Deactivate implements reconcile.Activator.
-func (a *Connectors) Deactivate(_ context.Context, pluginID string) error {
- a.mu.Lock()
- ids := a.registered[pluginID]
- delete(a.registered, pluginID)
- a.mu.Unlock()
- for _, id := range ids {
- a.registry.Unregister(id)
- }
- return nil
-}
-
-func connectorMetadata(
- l *reconcile.Loaded,
- c manifest.Contribution,
- id string,
-) (datasource.ConnectorMetadata, error) {
- src, err := instanceSchema(l.Package, c)
- if err != nil {
- return datasource.ConnectorMetadata{}, err
- }
- creds, settings := splitConnectorSchema(src)
- icon, err := iconDataURI(l.Package, c.Icon)
- if err != nil {
- return datasource.ConnectorMetadata{}, err
- }
- caps := c.Capabilities
- if caps == nil {
- caps = []string{}
- }
- return datasource.ConnectorMetadata{
- Type: id, Name: c.Name.Default, Names: c.Name.Locales,
- Description: c.Description.Default, Descriptions: c.Description.Locales,
- Icon: icon, Priority: 1000 + c.Order, AuthType: "plugin", Capabilities: caps,
- ConfigSchema: creds, SettingsSchema: settings, PluginID: l.Manifest.ID,
- }, nil
-}
-
-// splitConnectorSchema separates settings fields from credential fields.
-func splitConnectorSchema(src *configschema.Schema) (creds, settings *configschema.Schema) {
- if src == nil {
- return nil, nil
- }
- required := map[string]bool{}
- for _, k := range src.Required {
- required[k] = true
- }
- creds, settings = configschema.Object(), configschema.Object()
- keys := make([]string, 0, len(src.Properties))
- for k := range src.Properties {
- keys = append(keys, k)
- }
- sort.Strings(keys)
- for _, k := range keys {
- prop := *src.Properties[k]
- if prop.Group == settingsGroup && !prop.Secret {
- prop.Group = ""
- settings.Set(k, &prop, required[k])
- continue
- }
- creds.Set(k, &prop, required[k])
- }
- if len(creds.Properties) == 0 {
- creds = nil
- }
- if len(settings.Properties) == 0 {
- settings = nil
- }
- return creds, settings
-}
-
-// remoteConnector is a plugin connector. It streams, so the sync service
-// interleaves fetch, ingest and checkpoint and resumes after a timeout.
-type remoteConnector struct {
- iv *Invoker
- m *manifest.Manifest
- local string
- typeID string
-}
-
-var _ datasource.FullStreamingConnector = (*remoteConnector)(nil)
-
-func (r *remoteConnector) Type() string { return r.typeID }
-
-func instanceOf(cfg *types.DataSourceConfig) map[string]any {
- if cfg == nil {
- return map[string]any{}
- }
- return map[string]any{
- "credentials": nonNil(cfg.Credentials),
- "settings": nonNil(cfg.Settings),
- "resourceIds": cfg.ResourceIDs,
- }
-}
-
-func nonNil(m map[string]any) map[string]any {
- if m == nil {
- return map[string]any{}
- }
- return m
-}
-
-// Validate implements datasource.Connector.
-func (r *remoteConnector) Validate(ctx context.Context, cfg *types.DataSourceConfig) error {
- ctx, cancel := withDefaultTimeout(ctx, metadataTimeout)
- defer cancel()
- return r.iv.Call(ctx, r.m, pluginapi.ConnectorValidatePath(r.local), instanceOf(cfg), nil, nil)
-}
-
-// ListResources implements datasource.Connector.
-func (r *remoteConnector) ListResources(
- ctx context.Context, cfg *types.DataSourceConfig, parentID string,
-) ([]types.Resource, error) {
- ctx, cancel := withDefaultTimeout(ctx, metadataTimeout)
- defer cancel()
- var out pluginapi.ListResourcesOutput
- err := r.iv.Call(ctx, r.m, pluginapi.ConnectorListResourcesPath(r.local), instanceOf(cfg),
- pluginapi.ListResourcesInput{ParentID: parentID}, &out)
- if err != nil {
- return nil, err
- }
- res := make([]types.Resource, 0, len(out.Resources))
- for _, x := range out.Resources {
- item := types.Resource{
- ExternalID: x.ExternalID, Name: x.Name, Type: x.Type, Description: x.Description, URL: x.URL,
- ParentID: x.ParentID, HasChildren: x.HasChildren, Metadata: x.Metadata,
- }
- if x.ModifiedAt != nil {
- item.ModifiedAt = *x.ModifiedAt
- }
- res = append(res, item)
- }
- return res, nil
-}
-
-// ResolveResourceAncestors implements datasource.Connector.
-func (r *remoteConnector) ResolveResourceAncestors(
- ctx context.Context, cfg *types.DataSourceConfig, resourceIDs []string,
-) ([]string, error) {
- ctx, cancel := withDefaultTimeout(ctx, metadataTimeout)
- defer cancel()
- var out pluginapi.ResolveAncestorsOutput
- err := r.iv.Call(ctx, r.m, pluginapi.ConnectorResolveAncestorsPath(r.local), instanceOf(cfg),
- pluginapi.ResolveAncestorsInput{ResourceIDs: resourceIDs}, &out)
- return out.Ancestors, err
-}
-
-// collector gathers a stream for the non-streaming entry points.
-type collector struct {
- items []types.FetchedItem
- cursor *types.SyncCursor
-}
-
-func (c *collector) Emit(_ context.Context, item types.FetchedItem) error {
- c.items = append(c.items, item)
- return nil
-}
-
-func (c *collector) Checkpoint(_ context.Context, cursor *types.SyncCursor) error {
- c.cursor = cursor
- return nil
-}
-
-// FetchAll implements datasource.Connector.
-func (r *remoteConnector) FetchAll(
- ctx context.Context, cfg *types.DataSourceConfig, resourceIDs []string,
-) ([]types.FetchedItem, error) {
- c := &collector{}
- scoped := *cfg
- if len(resourceIDs) > 0 {
- scoped.ResourceIDs = resourceIDs
- }
- _, err := r.fetch(ctx, &scoped, pluginapi.FetchFull, nil, c)
- return c.items, err
-}
-
-// FetchIncremental implements datasource.Connector.
-func (r *remoteConnector) FetchIncremental(
- ctx context.Context, cfg *types.DataSourceConfig, cursor *types.SyncCursor,
-) ([]types.FetchedItem, *types.SyncCursor, error) {
- c := &collector{}
- next, err := r.fetch(ctx, cfg, pluginapi.FetchIncremental, cursor, c)
- return c.items, next, err
-}
-
-// FetchStream implements datasource.StreamingConnector: incremental from a
-// cursor, full without one.
-func (r *remoteConnector) FetchStream(
- ctx context.Context, cfg *types.DataSourceConfig, cursor *types.SyncCursor, h datasource.StreamHandler,
-) (*types.SyncCursor, error) {
- mode := pluginapi.FetchIncremental
- if cursor == nil {
- mode = pluginapi.FetchFull
- }
- return r.fetch(ctx, cfg, mode, cursor, h)
-}
-
-// FetchFullStream implements datasource.FullStreamingConnector: every item
-// again, with the previous cursor for deletion reconciliation.
-func (r *remoteConnector) FetchFullStream(
- ctx context.Context, cfg *types.DataSourceConfig, cursor *types.SyncCursor, h datasource.StreamHandler,
-) (*types.SyncCursor, error) {
- return r.fetch(ctx, cfg, pluginapi.FetchFull, cursor, h)
-}
-
-func (r *remoteConnector) fetch(
- ctx context.Context, cfg *types.DataSourceConfig, mode string, cursor *types.SyncCursor,
- h datasource.StreamHandler,
-) (*types.SyncCursor, error) {
- in := pluginapi.FetchInput{Mode: mode, Cursor: toPluginCursor(cursor)}
- if cfg != nil {
- in.ResourceIDs = cfg.ResourceIDs
- }
- end, err := r.iv.Stream(ctx, r.m, pluginapi.ConnectorFetchPath(r.local), instanceOf(cfg), in,
- func(ev pluginapi.Event) error {
- switch ev.Type {
- case pluginapi.EventItem:
- var it pluginapi.FetchedItem
- if err := json.Unmarshal(ev.Data, &it); err != nil {
- return fmt.Errorf("decode fetched item: %w", err)
- }
- return h.Emit(ctx, fromPluginItem(it))
- case pluginapi.EventCheckpoint:
- var c pluginapi.Cursor
- if err := json.Unmarshal(ev.Data, &c); err != nil {
- return fmt.Errorf("decode checkpoint: %w", err)
- }
- return h.Checkpoint(ctx, fromPluginCursor(&c))
- }
- return nil
- })
- if err != nil {
- return nil, err
- }
- var final pluginapi.Cursor
- if len(end) > 0 {
- if err := json.Unmarshal(end, &final); err != nil {
- return nil, fmt.Errorf("decode final cursor: %w", err)
- }
- }
- next := fromPluginCursor(&final)
- if next.LastSyncTime.IsZero() {
- next.LastSyncTime = time.Now()
- }
- return next, nil
-}
-
-func toPluginCursor(c *types.SyncCursor) *pluginapi.Cursor {
- if c == nil {
- return nil
- }
- out := &pluginapi.Cursor{State: c.ConnectorCursor}
- if !c.LastSyncTime.IsZero() {
- t := c.LastSyncTime
- out.LastSyncTime = &t
- }
- return out
-}
-
-func fromPluginCursor(c *pluginapi.Cursor) *types.SyncCursor {
- out := &types.SyncCursor{ConnectorCursor: c.State}
- if c.LastSyncTime != nil {
- out.LastSyncTime = *c.LastSyncTime
- }
- if out.ConnectorCursor == nil {
- out.ConnectorCursor = map[string]any{}
- }
- return out
-}
-
-func fromPluginItem(it pluginapi.FetchedItem) types.FetchedItem {
- out := types.FetchedItem{
- ExternalID: it.ExternalID, Title: it.Title, Content: it.Content, ContentType: it.ContentType,
- FileName: it.FileName, URL: it.URL, Metadata: it.Metadata, IsDeleted: it.IsDeleted,
- SourceResourceID: it.SourceResourceID,
- }
- if it.UpdatedAt != nil {
- out.UpdatedAt = *it.UpdatedAt
- }
- if it.CreatedAt != nil {
- out.CreatedAt = *it.CreatedAt
- }
- if out.ContentType == "" {
- out.ContentType = "text/markdown"
- }
- return out
-}
diff --git a/internal/plugin/activate/connectors_test.go b/internal/plugin/activate/connectors_test.go
deleted file mode 100644
index 3fbe184be..000000000
--- a/internal/plugin/activate/connectors_test.go
+++ /dev/null
@@ -1,202 +0,0 @@
-package activate
-
-import (
- "context"
- "errors"
- "fmt"
- "net/http/httptest"
- "strings"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/datasource"
- "github.com/Tencent/WeKnora/internal/plugin/pkg"
- "github.com/Tencent/WeKnora/internal/plugin/plugintest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-const feedPluginManifest = `schemaVersion: 1
-id: acme.feeds
-version: 1.0.0
-apiVersion: weknora.plugin/v1
-name: { en-US: ACME Feeds }
-publisher: { id: acme }
-runtime: { type: host, kind: binary, entry: bin/feeds }
-contributes:
- connectors:
- - id: feed
- name: { en-US: ACME Feed, zh-CN: ACME 订阅 }
- capabilities: [incremental]
- instanceSchema: schemas/feed.yaml
-`
-
-const feedSchema = `type: object
-required: [feed_urls]
-properties:
- token: { type: string, x-secret: true }
- feed_urls: { type: string, x-widget: textarea, x-group: settings }
-`
-
-// feedPlugin serves numbered items and resumes from the cursor.
-type feedPlugin struct{}
-
-func (feedPlugin) Validate(_ context.Context, _ *pluginsdk.Call, cfg pluginsdk.ConnectorConfig) error {
- if cfg.Settings["feed_urls"] == nil {
- return pluginapi.InvalidConfig("feed_urls is required", map[string]string{"settings.feed_urls": "required"})
- }
- if cfg.Credentials["token"] != "t-1" {
- return pluginapi.Errorf(pluginapi.CodeUnauthorized, "bad token")
- }
- return nil
-}
-
-func (feedPlugin) ListResources(
- _ context.Context,
- _ *pluginsdk.Call,
- cfg pluginsdk.ConnectorConfig,
- parent string,
-) ([]pluginapi.Resource, error) {
- if parent != "" {
- return nil, nil
- }
- var out []pluginapi.Resource
- for _, u := range strings.Split(fmt.Sprint(cfg.Settings["feed_urls"]), "\n") {
- out = append(out, pluginapi.Resource{ExternalID: u, Name: u, Type: "feed"})
- }
- return out, nil
-}
-
-func (feedPlugin) Fetch(
- _ context.Context,
- _ *pluginsdk.Call,
- _ pluginsdk.ConnectorConfig,
- in pluginapi.FetchInput,
- s *pluginsdk.Stream,
-) (*pluginapi.Cursor, error) {
- start := 0
- if in.Mode == pluginapi.FetchIncremental && in.Cursor != nil {
- start = int(in.Cursor.State["seen"].(float64))
- }
- for i := start; i < 3; i++ {
- item := pluginapi.FetchedItem{
- ExternalID: fmt.Sprint("post-", i), Title: fmt.Sprint("Post ", i), Content: []byte("# hi"),
- }
- if err := s.Item(item); err != nil {
- return nil, err
- }
- if err := s.Checkpoint(pluginapi.Cursor{State: map[string]any{"seen": i + 1}}); err != nil {
- return nil, err
- }
- }
- return &pluginapi.Cursor{State: map[string]any{"seen": 3}}, nil
-}
-
-type recordingHandler struct {
- items []string
- checkpoints int
-}
-
-func (h *recordingHandler) Emit(_ context.Context, item types.FetchedItem) error {
- h.items = append(h.items, item.ExternalID+"|"+item.ContentType)
- return nil
-}
-
-func (h *recordingHandler) Checkpoint(context.Context, *types.SyncCursor) error {
- h.checkpoints++
- return nil
-}
-
-func TestPluginConnector(t *testing.T) {
- ctx := context.Background()
- plugin := pluginsdk.New(pluginsdk.Info{ID: "acme.feeds", Version: "1.0.0"})
- plugin.Connector("feed", feedPlugin{})
- srv := httptest.NewServer(plugin.Handler())
- defer srv.Close()
-
- p, err := pkg.Open(plugintest.Zip(t, map[string]string{
- "plugin.yaml": feedPluginManifest, "bin/feeds": "x", "schemas/feed.yaml": feedSchema,
- }))
- if err != nil {
- t.Fatal(err)
- }
- registry := datasource.NewConnectorRegistry()
- a := NewConnectors(NewInvoker(fakeClients{client.New(srv.URL, nil, nil)}), registry)
- if err := a.Activate(ctx, &reconcile.Loaded{Manifest: p.Manifest, Package: p}); err != nil {
- t.Fatalf("Activate: %v", err)
- }
-
- var meta datasource.ConnectorMetadata
- for _, m := range registry.Metadata() {
- if m.Type == "acme.feeds/feed" {
- meta = m
- }
- }
- if meta.Names["zh-CN"] != "ACME 订阅" || meta.PluginID != "acme.feeds" ||
- meta.ConfigSchema.Properties["token"] == nil || meta.SettingsSchema.Properties["feed_urls"] == nil ||
- meta.ConfigSchema.Properties["feed_urls"] != nil {
- t.Fatalf("metadata = %+v", meta)
- }
-
- conn, err := registry.Get("acme.feeds/feed")
- if err != nil {
- t.Fatal(err)
- }
- cfg := &types.DataSourceConfig{
- Credentials: map[string]any{"token": "t-1"},
- Settings: map[string]any{"feed_urls": "https://a.example/rss\nhttps://b.example/rss"},
- }
- err = conn.Validate(ctx, &types.DataSourceConfig{Credentials: map[string]any{"token": "t-1"}})
- var pe *pluginapi.Error
- if err == nil || err.Error() != "feed_urls is required" ||
- !errors.As(err, &pe) || pe.Code != pluginapi.CodeInvalidConfig {
- t.Fatalf("Validate without settings = %v; want the plugin's message with its code reachable", err)
- }
- if err := conn.Validate(ctx, cfg); err != nil {
- t.Fatalf("Validate: %v", err)
- }
- res, err := conn.ListResources(ctx, cfg, "")
- if err != nil || len(res) != 2 || res[1].ExternalID != "https://b.example/rss" {
- t.Fatalf("ListResources = %+v, %v", res, err)
- }
-
- streaming := conn.(datasource.StreamingConnector)
- h := &recordingHandler{}
- cursor, err := streaming.FetchStream(ctx, cfg, nil, h)
- if err != nil {
- t.Fatal(err)
- }
- if strings.Join(h.items, ",") != "post-0|text/markdown,post-1|text/markdown,post-2|text/markdown" ||
- h.checkpoints != 3 || cursor.ConnectorCursor["seen"].(float64) != 3 || cursor.LastSyncTime.IsZero() {
- t.Fatalf("items=%v checkpoints=%d cursor=%+v", h.items, h.checkpoints, cursor)
- }
- cursor.ConnectorCursor["seen"] = float64(2)
- items, next, err := conn.FetchIncremental(ctx, cfg, cursor)
- if err != nil || len(items) != 1 || items[0].ExternalID != "post-2" ||
- next.ConnectorCursor["seen"].(float64) != 3 {
- t.Fatalf("incremental = %+v %+v %v", items, next, err)
- }
-
- if err := a.Deactivate(ctx, "acme.feeds"); err != nil {
- t.Fatal(err)
- }
- if _, err := registry.Get("acme.feeds/feed"); err == nil {
- t.Fatal("connector survived Deactivate")
- }
-}
-
-func TestPluginConnectorCannotShadowABuiltin(t *testing.T) {
- registry := datasource.NewConnectorRegistry()
- if err := registry.Register(&remoteConnector{typeID: "rss"}); err != nil {
- t.Fatal(err)
- }
- if err := registry.RegisterPlugin(&remoteConnector{typeID: "rss"}, datasource.ConnectorMetadata{}); err == nil {
- t.Fatal("a plugin must not replace a builtin connector")
- }
- registry.Unregister("rss")
- if _, err := registry.Get("rss"); err != nil {
- t.Fatal("Unregister must not remove builtins")
- }
-}
diff --git a/internal/plugin/activate/invoke.go b/internal/plugin/activate/invoke.go
deleted file mode 100644
index bf0a5055d..000000000
--- a/internal/plugin/activate/invoke.go
+++ /dev/null
@@ -1,309 +0,0 @@
-package activate
-
-import (
- "context"
- "sync"
- "time"
-
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
- "github.com/Tencent/WeKnora/internal/plugin/install"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/tenancy"
- "github.com/Tencent/WeKnora/internal/plugin/webhook"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// ClientSource reaches running code plugins wherever they run: this node's
-// plugin host, a standalone plugin host, a remote service.
-type ClientSource interface {
- Client(ctx context.Context, m *manifest.Manifest) (*client.Client, error)
- // OnThisNode reports whether a plugin runs on this node, where the
- // loopback Host API address reaches WeKnora.
- OnThisNode(pluginID string) bool
-}
-
-// Invoker is how the remote adapters call a code plugin: it finds the
-// process and builds the protocol envelope from the request context and the
-// plugin's decrypted system and tenant configuration.
-type Invoker struct {
- clients ClientSource
-
- mu sync.RWMutex
- tenancy *tenancy.Service
- plugins interfaces.PluginRepository
- // tokens and hostURL give plugins granted Host API scopes a way back;
- // plugins off this node (remote, on a plugin host) use publicHostURL.
- tokens TokenIssuer
- hostURL string
- publicHostURL string
- // webhooks and publicBase give plugins their workspace's webhook URLs.
- webhooks *webhook.Tokens
- publicBase string
- oauth OAuthResolver
-}
-
-// OAuthResolver turns a form field's "oauth:<id>" into an access token.
-type OAuthResolver interface {
- AccessToken(ctx context.Context, pluginID string, tenantID uint64, ref string) (string, error)
-}
-
-// SetOAuth lets calls carry fresh access tokens in place of OAuth
-// references.
-func (iv *Invoker) SetOAuth(r OAuthResolver) {
- iv.mu.Lock()
- iv.oauth = r
- iv.mu.Unlock()
-}
-
-// resolveOAuth returns a configuration with every "oauth:<id>" string
-// replaced by the connection's access token, copying what it changes so a
-// caller's cached configuration keeps its references. A connection that is
-// gone or cannot be refreshed becomes "": the plugin sees no token and
-// reports unauthorized.
-func (iv *Invoker) resolveOAuth(
- ctx context.Context, pluginID string, tenantID uint64, cfg map[string]any,
-) map[string]any {
- iv.mu.RLock()
- r := iv.oauth
- iv.mu.RUnlock()
- if r == nil || cfg == nil {
- return cfg
- }
- var walk func(v any) any
- walk = func(v any) any {
- switch t := v.(type) {
- case string:
- if !configschema.IsOAuthRef(t) {
- return t
- }
- tok, err := r.AccessToken(ctx, pluginID, tenantID, t)
- if err != nil {
- logger.Warnf(ctx, "[plugin] %s: OAuth connection %s: %v", pluginID, t, err)
- return ""
- }
- return tok
- case map[string]any:
- out := make(map[string]any, len(t))
- for k, x := range t {
- out[k] = walk(x)
- }
- return out
- case []any:
- out := make([]any, len(t))
- for i, x := range t {
- out[i] = walk(x)
- }
- return out
- }
- return v
- }
- return walk(cfg).(map[string]any)
-}
-
-// SetWebhooks lets calls carry the workspace's webhook URLs, when WeKnora's
-// public address (publicBase) is known.
-func (iv *Invoker) SetWebhooks(tokens *webhook.Tokens, publicBase string) {
- iv.mu.Lock()
- iv.webhooks, iv.publicBase = tokens, publicBase
- iv.mu.Unlock()
-}
-
-// TokenIssuer signs the Host API token of one call.
-type TokenIssuer interface {
- Issue(pluginID, version string, tenantID uint64, scopes []string) (string, time.Time, error)
-}
-
-// SetHostAPI lets calls carry Host API access: where plugins reach WeKnora
-// and how their tokens are signed.
-func (iv *Invoker) SetHostAPI(tokens TokenIssuer, url string) {
- iv.mu.Lock()
- iv.tokens, iv.hostURL = tokens, url
- iv.mu.Unlock()
-}
-
-// SetPublicHostAPI is where remote plugins reach the Host API. Without it
-// their calls carry no Host API access.
-func (iv *Invoker) SetPublicHostAPI(url string) {
- iv.mu.Lock()
- iv.publicHostURL = url
- iv.mu.Unlock()
-}
-
-// NewInvoker creates an Invoker; Bind completes it.
-func NewInvoker(clients ClientSource) *Invoker { return &Invoker{clients: clients} }
-
-// Bind supplies the tenant configuration and installed plugin rows.
-func (iv *Invoker) Bind(t *tenancy.Service, plugins interfaces.PluginRepository) {
- iv.mu.Lock()
- iv.tenancy, iv.plugins = t, plugins
- iv.mu.Unlock()
-}
-
-// Default timeouts per call kind, used when the caller set no deadline.
-const (
- searchTimeout = 10 * time.Second
- metadataTimeout = time.Minute
-)
-
-// withDefaultTimeout bounds ctx unless it already has a deadline.
-func withDefaultTimeout(ctx context.Context, d time.Duration) (context.Context, context.CancelFunc) {
- if _, ok := ctx.Deadline(); ok {
- return context.WithCancel(ctx)
- }
- return context.WithTimeout(ctx, d)
-}
-
-// Envelope builds the call envelope for a plugin with an instance config.
-func (iv *Invoker) Envelope(
- ctx context.Context,
- m *manifest.Manifest,
- instance map[string]any,
-) (pluginapi.Envelope, error) {
- env := pluginapi.Envelope{Config: pluginapi.Config{Instance: instance}}
- if tenantID, ok := types.TenantIDFromContext(ctx); ok {
- env.Context.TenantID = tenantID
- }
- if userID, ok := ctx.Value(types.UserIDContextKey).(string); ok {
- env.Context.UserID = userID
- }
- if lang, ok := types.LanguageFromContext(ctx); ok {
- env.Context.Locale = lang
- }
- if rid, ok := ctx.Value(types.RequestIDContextKey).(string); ok {
- env.Context.RequestID = rid
- }
- if d, ok := ctx.Deadline(); ok {
- env.Context.Deadline = &d
- }
- iv.mu.RLock()
- t, plugins, tokens, hostURL := iv.tenancy, iv.plugins, iv.tokens, iv.hostURL
- if !iv.clients.OnThisNode(m.ID) {
- hostURL = iv.publicHostURL
- }
- hooks, publicBase := iv.webhooks, iv.publicBase
- iv.mu.RUnlock()
- if hooks != nil && publicBase != "" && env.Context.TenantID != 0 {
- for _, c := range m.Contributes[manifest.PointWebhooks] {
- if env.Context.Webhooks == nil {
- env.Context.Webhooks = map[string]string{}
- }
- env.Context.Webhooks[c.ID] = publicBase + hooks.Path(m.ID, c.ID, env.Context.TenantID)
- }
- }
- if tokens != nil && hostURL != "" && env.Context.TenantID != 0 && len(m.Permissions.HostAPI) > 0 {
- token, _, err := tokens.Issue(m.ID, m.Version, env.Context.TenantID, m.Permissions.HostAPI)
- if err != nil {
- return env, err
- }
- env.Context.Host = &pluginapi.HostAccess{URL: hostURL, Token: token}
- }
- if plugins != nil && len(m.Config.SystemSchema) > 0 {
- sys, _, err := install.OpenSystemConfig(ctx, plugins, m)
- if err != nil {
- return env, err
- }
- env.Config.System = sys
- }
- if t != nil && env.Context.TenantID != 0 && len(m.Config.TenantSchema) > 0 {
- cfg, _, err := t.OpenConfig(ctx, env.Context.TenantID, m.ID)
- if err != nil {
- return env, err
- }
- env.Config.Tenant = cfg
- }
- // System connections belong to the platform (tenant 0).
- env.Config.System = iv.resolveOAuth(ctx, m.ID, 0, env.Config.System)
- env.Config.Tenant = iv.resolveOAuth(ctx, m.ID, env.Context.TenantID, env.Config.Tenant)
- env.Config.Instance = iv.resolveOAuth(ctx, m.ID, env.Context.TenantID, env.Config.Instance)
- return env, nil
-}
-
-// callError presents a plugin's error by its message, the way WeKnora's own
-// errors read in the UI; the protocol code stays reachable through
-// errors.As(err, **pluginapi.Error).
-type callError struct {
- pluginID string
- err *pluginapi.Error
-}
-
-func (e *callError) Error() string {
- if e.err.Code == pluginapi.CodeUnavailable {
- return "plugin " + e.pluginID + " is unavailable: " + e.err.Message
- }
- return e.err.Message
-}
-
-func (e *callError) Unwrap() error { return e.err }
-
-func present(pluginID string, err error) error {
- if pe, ok := pluginapi.AsError(err); ok {
- return &callError{pluginID: pluginID, err: pe}
- }
- return err
-}
-
-// Call makes a unary call to a plugin.
-func (iv *Invoker) Call(
- ctx context.Context, m *manifest.Manifest, path string, instance map[string]any, input, out any,
-) error {
- c, err := iv.clients.Client(ctx, m)
- if err != nil {
- return present(m.ID, err)
- }
- env, err := iv.Envelope(ctx, m, instance)
- if err != nil {
- return err
- }
- return present(m.ID, c.Call(ctx, path, env, input, out))
-}
-
-// ConfigOverride replaces configuration scopes for one call: a form being
-// edited asks the plugin with what it holds, not what is stored. A nil scope
-// keeps the stored one.
-type ConfigOverride struct {
- System map[string]any
- Tenant map[string]any
-}
-
-// CallOverriding is Call with some configuration scopes replaced.
-func (iv *Invoker) CallOverriding(
- ctx context.Context, m *manifest.Manifest, path string, instance map[string]any, override ConfigOverride,
- input, out any,
-) error {
- c, err := iv.clients.Client(ctx, m)
- if err != nil {
- return present(m.ID, err)
- }
- env, err := iv.Envelope(ctx, m, instance)
- if err != nil {
- return err
- }
- if override.System != nil {
- env.Config.System = iv.resolveOAuth(ctx, m.ID, 0, override.System)
- }
- if override.Tenant != nil {
- env.Config.Tenant = iv.resolveOAuth(ctx, m.ID, env.Context.TenantID, override.Tenant)
- }
- return present(m.ID, c.Call(ctx, path, env, input, out))
-}
-
-// Stream makes a streaming call to a plugin.
-func (iv *Invoker) Stream(
- ctx context.Context, m *manifest.Manifest, path string, instance map[string]any, input any,
- fn func(pluginapi.Event) error,
-) ([]byte, error) {
- c, err := iv.clients.Client(ctx, m)
- if err != nil {
- return nil, present(m.ID, err)
- }
- env, err := iv.Envelope(ctx, m, instance)
- if err != nil {
- return nil, err
- }
- end, err := c.Stream(ctx, path, env, input, fn)
- return end, present(m.ID, err)
-}
diff --git a/internal/plugin/activate/invoke_test.go b/internal/plugin/activate/invoke_test.go
deleted file mode 100644
index 1169d7147..000000000
--- a/internal/plugin/activate/invoke_test.go
+++ /dev/null
@@ -1,67 +0,0 @@
-package activate
-
-import (
- "context"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/plugin/hostapi"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/webhook"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-func TestEnvelopeCarriesHostAccessOnlyForGrantedPlugins(t *testing.T) {
- iss := hostapi.NewIssuer([]byte("k"))
- iv := NewInvoker(fakeClients{})
- iv.SetHostAPI(iss, "http://127.0.0.1:9")
- ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(7))
-
- granted := &manifest.Manifest{
- ID: "acme.x", Version: "1.0.0", Permissions: manifest.Permissions{HostAPI: []string{"kv"}},
- }
- env, err := iv.Envelope(ctx, granted, nil)
- if err != nil || env.Context.Host == nil || env.Context.Host.URL != "http://127.0.0.1:9" {
- t.Fatalf("envelope = %+v, %v", env.Context, err)
- }
- claims, err := iss.Verify(env.Context.Host.Token)
- if err != nil || claims.PluginID != "acme.x" || claims.TenantID != 7 || !claims.Has("kv") {
- t.Fatalf("token claims = %+v, %v", claims, err)
- }
-
- plain := &manifest.Manifest{ID: "acme.y", Version: "1.0.0"}
- if env, _ := iv.Envelope(ctx, plain, nil); env.Context.Host != nil {
- t.Fatal("a plugin without Host API scopes must get no token")
- }
- if env, _ := iv.Envelope(context.Background(), granted, nil); env.Context.Host != nil {
- t.Fatal("a call without a tenant must get no token")
- }
-
- remote := *granted
- remote.ID = "acme.remote"
- if env, _ := iv.Envelope(ctx, &remote, nil); env.Context.Host != nil {
- t.Fatal("a remote plugin must not be sent to this node's loopback address")
- }
- iv.SetPublicHostAPI("https://weknora.example.com")
- if env, _ := iv.Envelope(ctx, &remote, nil); env.Context.Host == nil ||
- env.Context.Host.URL != "https://weknora.example.com" {
- t.Fatalf("remote envelope = %+v", env.Context)
- }
-}
-
-func TestEnvelopeCarriesWebhookURLs(t *testing.T) {
- iv := NewInvoker(fakeClients{})
- m := &manifest.Manifest{ID: "acme.hooks", Contributes: manifest.Contributions{
- manifest.PointWebhooks: {{ID: "inbox"}},
- }}
- ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(7))
- if env, _ := iv.Envelope(ctx, m, nil); env.Context.Webhooks != nil {
- t.Fatal("no URLs without a public address")
- }
- tokens := webhook.NewTokens([]byte("k"))
- iv.SetWebhooks(tokens, "https://weknora.example.com")
- env, err := iv.Envelope(ctx, m, nil)
- want := "https://weknora.example.com" + tokens.Path("acme.hooks", "inbox", 7)
- if err != nil || env.Context.Webhooks["inbox"] != want {
- t.Fatalf("webhooks = %v, %v", env.Context.Webhooks, err)
- }
-}
diff --git a/internal/plugin/activate/mcp.go b/internal/plugin/activate/mcp.go
deleted file mode 100644
index 0e7300cf8..000000000
--- a/internal/plugin/activate/mcp.go
+++ /dev/null
@@ -1,443 +0,0 @@
-// Package activate connects installed plugins' declarative contributions to
-// the domains that use them: MCP servers, skills and model vendors.
-package activate
-
-import (
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "sort"
- "sync"
- "time"
-
- "github.com/google/uuid"
-
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/install"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/plugin/tenancy"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
-)
-
-// ErrPluginService is returned when a caller tries to change an MCP service
-// a plugin provides.
-var ErrPluginService = errors.New("this MCP service is provided by a plugin and cannot be changed")
-
-// mcpNamespace seeds the IDs of plugin MCP services.
-var mcpNamespace = uuid.MustParse("5b0c8e5e-4f7c-4a55-9d0e-6f1d0f4c7a21")
-
-type mcpServer struct {
- manifest *manifest.Manifest
- contrib manifest.Contribution
- qualifiedID string
- activatedAt time.Time
-}
-
-// MCPServers makes the MCP servers of installed plugins appear as read-only
-// MCP services in every workspace that enabled the plugin. Each workspace
-// gets its own service ID, because headers carry its own configuration.
-type MCPServers struct {
- mu sync.RWMutex
- tenancy *tenancy.Service
- plugins interfaces.PluginRepository
- servers map[string][]mcpServer // plugin ID → servers
- iv *Invoker
- // directories are the tool directory snapshots of plugin services, by
- // plugin ID. They cannot be stored (mcp_metadata references stored
- // services) and are cheap to list again, so each node keeps its own and
- // drops a plugin's when it is loaded again or unloaded.
- directories map[string]map[string]*types.MCPMetadata
-}
-
-// NewMCPServers creates the MCP server activator. It lists nothing until
-// Bind gives it the tenant switches: the MCP repository it decorates is
-// needed long before the plugin services exist.
-func NewMCPServers() *MCPServers {
- return &MCPServers{servers: map[string][]mcpServer{}, directories: map[string]map[string]*types.MCPMetadata{}}
-}
-
-// Bind supplies the tenant switches and configuration and the installed
-// plugin rows (for platform configuration).
-func (a *MCPServers) Bind(t *tenancy.Service, plugins interfaces.PluginRepository) {
- a.mu.Lock()
- a.tenancy, a.plugins = t, plugins
- a.mu.Unlock()
-}
-
-// Name implements reconcile.Activator.
-func (a *MCPServers) Name() string { return "mcpServers" }
-
-// Activate implements reconcile.Activator.
-func (a *MCPServers) Activate(_ context.Context, l *reconcile.Loaded) error {
- var list []mcpServer
- now := time.Now()
- for _, c := range l.Manifest.Contributes[manifest.PointMCPServers] {
- list = append(list, mcpServer{
- manifest: l.Manifest, contrib: c, qualifiedID: l.Manifest.ID + "/" + c.ID, activatedAt: now,
- })
- }
- a.mu.Lock()
- defer a.mu.Unlock()
- delete(a.directories, l.Manifest.ID)
- if len(list) == 0 {
- delete(a.servers, l.Manifest.ID)
- } else {
- a.servers[l.Manifest.ID] = list
- }
- return nil
-}
-
-// Deactivate implements reconcile.Activator.
-func (a *MCPServers) Deactivate(_ context.Context, pluginID string) error {
- a.mu.Lock()
- delete(a.servers, pluginID)
- delete(a.directories, pluginID)
- a.mu.Unlock()
- return nil
-}
-
-// serviceID is a plugin MCP service's ID in one workspace. It is stable
-// across nodes and restarts, so agents can keep it in their configuration.
-func serviceID(tenantID uint64, qualifiedID string) string {
- return uuid.NewSHA1(mcpNamespace, fmt.Appendf(nil, "%d/%s", tenantID, qualifiedID)).String()
-}
-
-func (a *MCPServers) snapshot() []mcpServer {
- a.mu.RLock()
- defer a.mu.RUnlock()
- if a.tenancy == nil {
- return nil
- }
- var out []mcpServer
- for _, list := range a.servers {
- out = append(out, list...)
- }
- sort.Slice(out, func(i, j int) bool { return out[i].qualifiedID < out[j].qualifiedID })
- return out
-}
-
-// Services returns the plugin MCP services a workspace sees.
-func (a *MCPServers) Services(ctx context.Context, tenantID uint64) []*types.MCPService {
- servers := a.snapshot()
- if len(servers) == 0 {
- return nil
- }
- enabled := a.tenancy.EnabledFilter(ctx, tenantID)
- var out []*types.MCPService
- for _, s := range servers {
- if !enabled(manifest.PointMCPServers, s.qualifiedID) {
- continue
- }
- out = append(out, a.service(ctx, tenantID, s))
- }
- return out
-}
-
-// service builds one workspace's view of a plugin MCP server. A server whose
-// headers cannot be filled (the workspace has not configured the plugin yet)
-// is listed disabled, so agents skip it and the UI can say why.
-func (a *MCPServers) service(ctx context.Context, tenantID uint64, s mcpServer) *types.MCPService {
- svc := &types.MCPService{
- ID: serviceID(tenantID, s.qualifiedID),
- TenantID: tenantID,
- Name: s.contrib.Name.Default,
- Description: s.contrib.Description.Default,
- Enabled: true,
- IsBuiltin: true,
- PluginID: s.manifest.ID,
- PluginVersion: s.manifest.Version,
- PluginServer: s.contrib.ID,
- ToolViews: toolViews(s.contrib),
- CreatedAt: s.activatedAt,
- UpdatedAt: s.activatedAt,
- }
- if s.contrib.ServedByPlugin() {
- // Configuration travels with each call, so the connection never
- // goes stale when it changes.
- svc.TransportType = types.MCPTransportPlugin
- return svc
- }
- url := s.contrib.MCP.URL
- svc.URL = &url
- svc.TransportType = types.MCPTransportType(s.contrib.MCP.Transport)
- if svc.TransportType == "" {
- svc.TransportType = types.MCPTransportHTTPStreamable
- }
- headers, updated, err := a.headers(ctx, tenantID, s)
- if err != nil {
- logger.Debugf(ctx, "[plugin] MCP server %s in tenant %d: %v", s.qualifiedID, tenantID, err)
- svc.Enabled = false
- svc.PluginError = err.Error()
- return svc
- }
- svc.Headers = headers
- if updated.After(svc.UpdatedAt) {
- // The MCP manager reconnects when UpdatedAt moves, so a changed
- // credential takes effect on the next call.
- svc.UpdatedAt = updated
- }
- return svc
-}
-
-func (a *MCPServers) headers(ctx context.Context, tenantID uint64, s mcpServer) (types.MCPHeaders, time.Time, error) {
- var (
- tenantCfg, systemCfg map[string]any
- updated time.Time
- loadErr error
- )
- lookup := func(scope, key string) (string, bool) {
- var cfg map[string]any
- switch scope {
- case manifest.ScopeConfig:
- if tenantCfg == nil && loadErr == nil {
- var at time.Time
- tenantCfg, at, loadErr = a.tenancy.OpenConfig(ctx, tenantID, s.manifest.ID)
- updated = later(updated, at)
- }
- cfg = tenantCfg
- case manifest.ScopeSystem:
- if systemCfg == nil && loadErr == nil {
- var at time.Time
- systemCfg, at, loadErr = install.OpenSystemConfig(ctx, a.plugins, s.manifest)
- updated = later(updated, at)
- }
- cfg = systemCfg
- }
- v, ok := cfg[key]
- if !ok || v == nil {
- return "", false
- }
- return fmt.Sprint(v), true
- }
- out := types.MCPHeaders{}
- for name, value := range s.contrib.MCP.Headers {
- expanded, err := manifest.ExpandTemplate(value, lookup)
- if loadErr != nil {
- return nil, updated, loadErr
- }
- if err != nil {
- return nil, updated, err
- }
- out[name] = expanded
- }
- return out, updated, nil
-}
-
-// toolViews are a contribution's result views as the chat reads them.
-func toolViews(c manifest.Contribution) map[string]json.RawMessage {
- if len(c.ToolViews) == 0 {
- return nil
- }
- out := make(map[string]json.RawMessage, len(c.ToolViews))
- for name, v := range c.ToolViews {
- if b, err := json.Marshal(v); err == nil {
- out[name] = b
- }
- }
- return out
-}
-
-func later(a, b time.Time) time.Time {
- if b.After(a) {
- return b
- }
- return a
-}
-
-// find returns the plugin MCP service with an ID in a workspace.
-func (a *MCPServers) find(ctx context.Context, tenantID uint64, id string) *types.MCPService {
- for _, svc := range a.Services(ctx, tenantID) {
- if svc.ID == id {
- return svc
- }
- }
- return nil
-}
-
-// owns reports whether an ID belongs to a plugin MCP server in a workspace,
-// enabled or not.
-func (a *MCPServers) owns(tenantID uint64, id string) bool {
- for _, s := range a.snapshot() {
- if serviceID(tenantID, s.qualifiedID) == id {
- return true
- }
- }
- return false
-}
-
-// Repository wraps the MCP service repository so plugin MCP services are
-// listed and resolved alongside stored ones and cannot be modified.
-func (a *MCPServers) Repository(inner interfaces.MCPServiceRepository) interfaces.MCPServiceRepository {
- return &mcpRepository{MCPServiceRepository: inner, plugins: a}
-}
-
-type mcpRepository struct {
- interfaces.MCPServiceRepository
- plugins *MCPServers
-}
-
-func (r *mcpRepository) GetByID(ctx context.Context, tenantID uint64, id string) (*types.MCPService, error) {
- if svc := r.plugins.find(ctx, tenantID, id); svc != nil {
- return svc, nil
- }
- return r.MCPServiceRepository.GetByID(ctx, tenantID, id)
-}
-
-func (r *mcpRepository) List(ctx context.Context, tenantID uint64) ([]*types.MCPService, error) {
- stored, err := r.MCPServiceRepository.List(ctx, tenantID)
- if err != nil {
- return nil, err
- }
- return append(stored, r.plugins.Services(ctx, tenantID)...), nil
-}
-
-func (r *mcpRepository) ListEnabled(ctx context.Context, tenantID uint64) ([]*types.MCPService, error) {
- stored, err := r.MCPServiceRepository.ListEnabled(ctx, tenantID)
- if err != nil {
- return nil, err
- }
- for _, svc := range r.plugins.Services(ctx, tenantID) {
- if svc.Enabled {
- stored = append(stored, svc)
- }
- }
- return stored, nil
-}
-
-func (r *mcpRepository) ListByIDs(ctx context.Context, tenantID uint64, ids []string) ([]*types.MCPService, error) {
- stored, err := r.MCPServiceRepository.ListByIDs(ctx, tenantID, ids)
- if err != nil {
- return nil, err
- }
- want := make(map[string]bool, len(ids))
- for _, id := range ids {
- want[id] = true
- }
- for _, svc := range r.plugins.Services(ctx, tenantID) {
- if want[svc.ID] {
- stored = append(stored, svc)
- }
- }
- return stored, nil
-}
-
-func (r *mcpRepository) Update(ctx context.Context, svc *types.MCPService) error {
- if r.plugins.owns(svc.TenantID, svc.ID) {
- return ErrPluginService
- }
- return r.MCPServiceRepository.Update(ctx, svc)
-}
-
-func (r *mcpRepository) Delete(ctx context.Context, tenantID uint64, id string) error {
- if r.plugins.owns(tenantID, id) {
- return ErrPluginService
- }
- return r.MCPServiceRepository.Delete(ctx, tenantID, id)
-}
-
-// ownerOf is the plugin providing a service ID in a workspace, or "".
-func (a *MCPServers) ownerOf(tenantID uint64, id string) string {
- for _, s := range a.snapshot() {
- if serviceID(tenantID, s.qualifiedID) == id {
- return s.manifest.ID
- }
- }
- return ""
-}
-
-func directoryKey(tenantID uint64, serviceID, principal string) string {
- return fmt.Sprintf("%d/%s/%s", tenantID, serviceID, principal)
-}
-
-// The repository also stores tool directory snapshots
-// (interfaces.MCPMetadataRepository): plugin services' in memory, the rest
-// in the wrapped repository. Agents discover MCP tools through them.
-var _ interfaces.MCPMetadataRepository = (*mcpRepository)(nil)
-
-func (r *mcpRepository) inner() (interfaces.MCPMetadataRepository, bool) {
- m, ok := r.MCPServiceRepository.(interfaces.MCPMetadataRepository)
- return m, ok
-}
-
-// GetMetadata implements interfaces.MCPMetadataRepository.
-func (r *mcpRepository) GetMetadata(
- ctx context.Context, tenantID uint64, serviceID, principal string,
-) (*types.MCPMetadata, error) {
- if owner := r.plugins.ownerOf(tenantID, serviceID); owner != "" {
- a := r.plugins
- a.mu.RLock()
- defer a.mu.RUnlock()
- if m := a.directories[owner][directoryKey(tenantID, serviceID, principal)]; m != nil {
- cp := *m
- return &cp, nil
- }
- return nil, nil
- }
- if inner, ok := r.inner(); ok {
- return inner.GetMetadata(ctx, tenantID, serviceID, principal)
- }
- return nil, nil
-}
-
-// ListMetadataSummaries implements interfaces.MCPMetadataRepository.
-func (r *mcpRepository) ListMetadataSummaries(
- ctx context.Context, tenantID uint64, principals []string,
-) ([]*types.MCPMetadataSummary, error) {
- var out []*types.MCPMetadataSummary
- if inner, ok := r.inner(); ok {
- rows, err := inner.ListMetadataSummaries(ctx, tenantID, principals)
- if err != nil {
- return nil, err
- }
- out = rows
- }
- wanted := map[string]bool{}
- for _, p := range principals {
- wanted[p] = true
- }
- a := r.plugins
- a.mu.RLock()
- defer a.mu.RUnlock()
- for _, dirs := range a.directories {
- for _, m := range dirs {
- if m.TenantID == tenantID && wanted[m.Principal] {
- out = append(out, &types.MCPMetadataSummary{
- ServiceID: m.ServiceID, Principal: m.Principal, ConfigFingerprint: m.ConfigFingerprint,
- ToolCount: len(m.Tools), SyncedAt: m.SyncedAt, ServerName: m.ServerName,
- })
- }
- }
- }
- return out, nil
-}
-
-// SaveMetadata implements interfaces.MCPMetadataRepository.
-func (r *mcpRepository) SaveMetadata(ctx context.Context, snapshot *types.MCPMetadata) error {
- owner := r.plugins.ownerOf(snapshot.TenantID, snapshot.ServiceID)
- if owner == "" {
- inner, ok := r.inner()
- if !ok {
- return types.ErrMCPMetadataStorage
- }
- return inner.SaveMetadata(ctx, snapshot)
- }
- a := r.plugins
- a.mu.Lock()
- defer a.mu.Unlock()
- dirs := a.directories[owner]
- if dirs == nil {
- dirs = map[string]*types.MCPMetadata{}
- a.directories[owner] = dirs
- }
- key := directoryKey(snapshot.TenantID, snapshot.ServiceID, snapshot.Principal)
- // Like the stored table: an older refresh must not overwrite a newer one.
- if prev := dirs[key]; prev != nil && prev.SyncedAt.After(snapshot.SyncedAt) {
- return nil
- }
- cp := *snapshot
- dirs[key] = &cp
- return nil
-}
diff --git a/internal/plugin/activate/mcp_test.go b/internal/plugin/activate/mcp_test.go
deleted file mode 100644
index d44a0c127..000000000
--- a/internal/plugin/activate/mcp_test.go
+++ /dev/null
@@ -1,237 +0,0 @@
-package activate
-
-import (
- "context"
- "errors"
- "testing"
- "time"
-
- "github.com/Tencent/WeKnora/internal/plugin/install"
- "github.com/Tencent/WeKnora/internal/plugin/plugintest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/plugin/tenancy"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
-)
-
-const searchManifest = `schemaVersion: 1
-id: acme.search
-version: 1.0.0
-name: { en-US: ACME Search }
-publisher: { id: acme }
-runtime: { type: declarative }
-config: { tenant: tenant.yaml, system: system.yaml }
-contributes:
- mcpServers:
- - id: search
- name: ACME Search
- mcp:
- url: https://mcp.acme.example/mcp
- headers:
- Authorization: Bearer ${config.api_key}
- X-Region: ${system.region}
-`
-
-type env struct {
- ctx context.Context
- mcp *MCPServers
- tenancy *tenancy.Service
- installer *install.Service
- stored *fakeMCPRepo
- repo *mcpRepository
-}
-
-// fakeMCPRepo stores one ordinary service per tenant.
-type fakeMCPRepo struct {
- updates int
- saved *types.MCPMetadata
-}
-
-func (f *fakeMCPRepo) own(tenantID uint64) *types.MCPService {
- return &types.MCPService{ID: "stored", TenantID: tenantID, Name: "Mine", Enabled: true}
-}
-
-func (f *fakeMCPRepo) Create(context.Context, *types.MCPService) error { return nil }
-func (f *fakeMCPRepo) GetByID(_ context.Context, t uint64, id string) (*types.MCPService, error) {
- if id == "stored" {
- return f.own(t), nil
- }
- return nil, nil
-}
-
-func (f *fakeMCPRepo) List(_ context.Context, t uint64) ([]*types.MCPService, error) {
- return []*types.MCPService{f.own(t)}, nil
-}
-
-func (f *fakeMCPRepo) ListEnabled(_ context.Context, t uint64) ([]*types.MCPService, error) {
- return []*types.MCPService{f.own(t)}, nil
-}
-
-func (f *fakeMCPRepo) ListByIDs(_ context.Context, t uint64, ids []string) ([]*types.MCPService, error) {
- for _, id := range ids {
- if id == "stored" {
- return []*types.MCPService{f.own(t)}, nil
- }
- }
- return nil, nil
-}
-func (f *fakeMCPRepo) Update(context.Context, *types.MCPService) error { f.updates++; return nil }
-func (f *fakeMCPRepo) Delete(context.Context, uint64, string) error { return nil }
-
-func setup(t *testing.T) *env {
- t.Helper()
- ctx := context.Background()
- plugins, store, reg := plugintest.NewMemRepo(), &plugintest.MemStore{}, registry.New()
- ten := tenancy.NewService(reg, &plugintest.MemTenantSettings{})
- mcp := NewMCPServers()
- mcp.Bind(ten, plugins)
- r := reconcile.New(reconcile.Options{
- Repo: plugins, Store: store, Registry: reg, CacheDir: t.TempDir(), Activators: []reconcile.Activator{mcp},
- })
- installer := install.NewService(plugins, store, r, "")
- pkg := plugintest.Zip(t, map[string]string{
- "plugin.yaml": searchManifest,
- "tenant.yaml": "type: object\nproperties: { api_key: { type: string, x-secret: true } }\nrequired: [api_key]\n",
- "system.yaml": "type: object\nproperties: { region: { type: string } }\n",
- })
- if _, err := installer.Install(ctx, install.Request{Data: pkg}); err != nil {
- t.Fatal(err)
- }
- stored := &fakeMCPRepo{}
- return &env{
- ctx: ctx, mcp: mcp, tenancy: ten, installer: installer, stored: stored,
- repo: mcp.Repository(stored).(*mcpRepository),
- }
-}
-
-func TestPluginMCPServiceFollowsTenantSwitchAndConfig(t *testing.T) {
- e := setup(t)
- list, _ := e.repo.List(e.ctx, 1)
- if len(list) != 1 {
- t.Fatalf("a plugin the tenant has not enabled must stay hidden, got %d services", len(list))
- }
- if err := e.tenancy.SetEnabled(e.ctx, 1, "acme.search", true, "u"); err != nil {
- t.Fatal(err)
- }
-
- list, _ = e.repo.List(e.ctx, 1)
- if len(list) != 2 {
- t.Fatalf("want stored + plugin service, got %d", len(list))
- }
- svc := list[1]
- if svc.PluginID != "acme.search" || !svc.IsBuiltin || svc.Enabled || svc.PluginError == "" {
- t.Fatalf("an unconfigured plugin service must be listed disabled: %+v", svc)
- }
- enabled, _ := e.repo.ListEnabled(e.ctx, 1)
- if len(enabled) != 1 {
- t.Fatal("agents must not get an unconfigured plugin service")
- }
-
- if _, err := e.installer.SetSystemConfig(e.ctx, "acme.search", map[string]any{"region": "eu"}); err != nil {
- t.Fatal(err)
- }
- if _, err := e.tenancy.SetConfig(e.ctx, 1, "acme.search", map[string]any{"api_key": "k-1"}, "u"); err != nil {
- t.Fatal(err)
- }
- got, err := e.repo.GetByID(e.ctx, 1, svc.ID)
- if err != nil || got == nil || !got.Enabled {
- t.Fatalf("GetByID = %+v, %v", got, err)
- }
- if got.Headers["Authorization"] != "Bearer k-1" || got.Headers["X-Region"] != "eu" {
- t.Fatalf("headers = %v", got.Headers)
- }
- if !got.UpdatedAt.After(svc.UpdatedAt) {
- t.Fatal("changing configuration must move UpdatedAt so the MCP client reconnects")
- }
- byIDs, _ := e.repo.ListByIDs(e.ctx, 1, []string{"stored", svc.ID})
- if len(byIDs) != 2 {
- t.Fatalf("ListByIDs = %d", len(byIDs))
- }
-
- // Each tenant has its own service ID and configuration.
- if err := e.tenancy.SetEnabled(e.ctx, 2, "acme.search", true, "u"); err != nil {
- t.Fatal(err)
- }
- other, _ := e.repo.List(e.ctx, 2)
- if other[1].ID == svc.ID || other[1].Enabled {
- t.Fatalf("tenant 2 = %+v", other[1])
- }
-
- if err := e.repo.Update(e.ctx, got); !errors.Is(err, ErrPluginService) {
- t.Fatalf("Update = %v", err)
- }
- if err := e.repo.Delete(e.ctx, 1, svc.ID); !errors.Is(err, ErrPluginService) {
- t.Fatalf("Delete = %v", err)
- }
-
- if _, err := e.installer.SetEnabled(e.ctx, "acme.search", false); err != nil {
- t.Fatal(err)
- }
- if list, _ := e.repo.List(e.ctx, 1); len(list) != 1 {
- t.Fatal("disabling the plugin platform-wide must remove its services")
- }
-}
-
-func (f *fakeMCPRepo) GetMetadata(_ context.Context, _ uint64, id, _ string) (*types.MCPMetadata, error) {
- if f.saved != nil && f.saved.ServiceID == id {
- return f.saved, nil
- }
- return nil, nil
-}
-
-func (f *fakeMCPRepo) ListMetadataSummaries(context.Context, uint64, []string) ([]*types.MCPMetadataSummary, error) {
- if f.saved == nil {
- return nil, nil
- }
- return []*types.MCPMetadataSummary{{ServiceID: f.saved.ServiceID}}, nil
-}
-
-func (f *fakeMCPRepo) SaveMetadata(_ context.Context, m *types.MCPMetadata) error {
- f.saved = m
- return nil
-}
-
-// Agents discover MCP tools through stored directory snapshots. Plugin
-// services have no row to reference, so the wrapper keeps theirs.
-func TestPluginMCPDirectories(t *testing.T) {
- e := setup(t)
- if err := e.tenancy.SetEnabled(e.ctx, 1, "acme.search", true, "u"); err != nil {
- t.Fatal(err)
- }
- meta, ok := e.mcp.Repository(e.stored).(interfaces.MCPMetadataRepository)
- if !ok {
- t.Fatal("the wrapped repository hides directory storage")
- }
- id := serviceID(1, "acme.search/search")
- older := time.Now().Add(-time.Minute)
- first := &types.MCPMetadata{
- TenantID: 1, ServiceID: id, Tools: []*types.MCPTool{{Name: "search"}}, SyncedAt: time.Now(),
- }
- if err := meta.SaveMetadata(e.ctx, first); err != nil {
- t.Fatal(err)
- }
- // An older refresh arriving late does not win.
- _ = meta.SaveMetadata(e.ctx, &types.MCPMetadata{TenantID: 1, ServiceID: id, SyncedAt: older})
- got, err := meta.GetMetadata(e.ctx, 1, id, "")
- if err != nil || got == nil || len(got.Tools) != 1 {
- t.Fatalf("plugin directory = %+v, %v", got, err)
- }
- if other, _ := meta.GetMetadata(e.ctx, 2, id, ""); other != nil {
- t.Fatal("another workspace reads the directory")
- }
- if err := meta.SaveMetadata(e.ctx, &types.MCPMetadata{TenantID: 1, ServiceID: "stored"}); err != nil ||
- e.stored.saved == nil {
- t.Fatalf("stored services' directories go to the database: %v", err)
- }
- sums, _ := meta.ListMetadataSummaries(e.ctx, 1, []string{""})
- if len(sums) != 2 {
- t.Fatalf("summaries = %d", len(sums))
- }
- if err := e.mcp.Deactivate(e.ctx, "acme.search"); err != nil {
- t.Fatal(err)
- }
- if len(e.mcp.directories) != 0 {
- t.Fatal("an unloaded plugin keeps its directories")
- }
-}
diff --git a/internal/plugin/activate/mcp_transport.go b/internal/plugin/activate/mcp_transport.go
deleted file mode 100644
index 05af1abbc..000000000
--- a/internal/plugin/activate/mcp_transport.go
+++ /dev/null
@@ -1,92 +0,0 @@
-package activate
-
-import (
- "context"
- "encoding/json"
- "fmt"
-
- "github.com/mark3labs/mcp-go/client/transport"
- mcpgo "github.com/mark3labs/mcp-go/mcp"
-
- "github.com/Tencent/WeKnora/internal/mcp"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// SetInvoker lets the activator reach MCP servers plugins serve themselves
-// (mcpServers without a url): the MCP client talks to them through plugin
-// calls, which carry the workspace's context and configuration.
-func (a *MCPServers) SetInvoker(iv *Invoker) {
- a.mu.Lock()
- a.iv = iv
- a.mu.Unlock()
- mcp.RegisterTransport(types.MCPTransportPlugin, a.newTransport)
-}
-
-func (a *MCPServers) newTransport(svc *types.MCPService) (transport.Interface, error) {
- a.mu.RLock()
- iv := a.iv
- var found *mcpServer
- for _, s := range a.servers[svc.PluginID] {
- if s.contrib.ID == svc.PluginServer {
- s := s
- found = &s
- }
- }
- a.mu.RUnlock()
- if iv == nil || found == nil {
- return nil, fmt.Errorf("plugin MCP server %s/%s is not loaded", svc.PluginID, svc.PluginServer)
- }
- return &pluginTransport{iv: iv, m: found.manifest, server: found.contrib.ID, tenantID: svc.TenantID}, nil
-}
-
-// pluginTransport carries MCP messages as plugin calls. Plugin servers are
-// stateless: there is no session to start or close, and notifications
-// (initialized, cancelled) need no delivery.
-type pluginTransport struct {
- iv *Invoker
- m *manifest.Manifest
- server string
- tenantID uint64
-}
-
-func (t *pluginTransport) Start(context.Context) error { return nil }
-
-func (t *pluginTransport) SendRequest(
- ctx context.Context, req transport.JSONRPCRequest,
-) (*transport.JSONRPCResponse, error) {
- id, err := json.Marshal(req.ID)
- if err != nil {
- return nil, err
- }
- in := pluginapi.MCPRequest{JSONRPC: "2.0", ID: id, Method: req.Method}
- if req.Params != nil {
- if in.Params, err = json.Marshal(req.Params); err != nil {
- return nil, err
- }
- }
- // The service belongs to one workspace; calls run in its context even
- // when the MCP manager connects without one.
- ctx = context.WithValue(ctx, types.TenantIDContextKey, t.tenantID)
- var out pluginapi.MCPResponse
- if err := t.iv.Call(ctx, t.m, pluginapi.MCPPath(t.server), nil, in, &out); err != nil {
- return nil, err
- }
- resp := &transport.JSONRPCResponse{JSONRPC: "2.0", ID: req.ID, Result: out.Result}
- if out.Error != nil {
- resp.Error = &mcpgo.JSONRPCErrorDetails{Code: out.Error.Code, Message: out.Error.Message}
- }
- return resp, nil
-}
-
-func (t *pluginTransport) SendNotification(context.Context, mcpgo.JSONRPCNotification) error {
- return nil
-}
-
-func (t *pluginTransport) SetNotificationHandler(func(mcpgo.JSONRPCNotification)) {}
-
-func (t *pluginTransport) Close() error { return nil }
-
-//nolint:revive // mcp-go's transport.Interface names it so.
-func (t *pluginTransport) GetSessionId() string { return "" }
diff --git a/internal/plugin/activate/mcp_transport_test.go b/internal/plugin/activate/mcp_transport_test.go
deleted file mode 100644
index c561d014d..000000000
--- a/internal/plugin/activate/mcp_transport_test.go
+++ /dev/null
@@ -1,129 +0,0 @@
-package activate
-
-import (
- "context"
- "encoding/json"
- "net/http/httptest"
- "strings"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/mcp"
- "github.com/Tencent/WeKnora/internal/plugin/pkg"
- "github.com/Tencent/WeKnora/internal/plugin/plugintest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/plugin/tenancy"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-const toolsManifest = `schemaVersion: 1
-id: acme.tools
-version: 1.2.0
-apiVersion: weknora.plugin/v1
-name: { en-US: ACME Tools }
-publisher: { id: acme }
-runtime: { type: host, kind: binary, entry: bin/tools }
-config: { tenant: tenant.yaml }
-contributes:
- mcpServers:
- - id: issues
- name: ACME Issues
- toolViews:
- search:
- view: table
- items: issues
- columns: [key, { field: summary, title: { en-US: Summary }, link: url }]
-`
-
-// A tool a plugin serves itself is reached through plugin calls: the MCP
-// client sees an ordinary server, and each call carries the workspace.
-func TestPluginServedMCPServer(t *testing.T) {
- ctx := context.Background()
- p := pluginsdk.New(pluginsdk.Info{ID: "acme.tools", Version: "1.2.0"})
- p.Tool("issues", pluginapi.Tool{Name: "search", Description: "Search issues"},
- func(_ context.Context, call *pluginsdk.Call, args json.RawMessage) (*pluginapi.ToolResult, error) {
- var in struct{ Q string }
- _ = json.Unmarshal(args, &in)
- return pluginapi.StructuredResult(map[string]any{
- "issues": []map[string]any{{"key": "ENG-1", "summary": in.Q}},
- "tenant": call.TenantID, "site": call.Config.Tenant["site"],
- }, "one issue"), nil
- })
- srv := httptest.NewServer(p.Handler())
- defer srv.Close()
-
- opened, err := pkg.Open(plugintest.Zip(t, map[string]string{
- "plugin.yaml": toolsManifest, "bin/tools": "x",
- "tenant.yaml": "type: object\nproperties: { site: { type: string } }\n",
- }))
- if err != nil {
- t.Fatal(err)
- }
- reg := registry.New()
- if err := reg.Register(opened.Manifest); err != nil {
- t.Fatal(err)
- }
- settings := &plugintest.MemTenantSettings{}
- ten := tenancy.NewService(reg, settings)
- plugins := plugintest.NewMemRepo()
- _ = settings.Upsert(ctx, &types.PluginTenantSetting{TenantID: 7, PluginID: "acme.tools", Enabled: true})
- if _, err := ten.SetConfig(ctx, 7, "acme.tools", map[string]any{"site": "acme.example"}, "u"); err != nil {
- t.Fatal(err)
- }
- iv := NewInvoker(fakeClients{client.New(srv.URL, nil, nil)})
- iv.Bind(ten, plugins)
- a := NewMCPServers()
- a.Bind(ten, plugins)
- a.SetInvoker(iv)
- if err := a.Activate(ctx, &reconcile.Loaded{Manifest: opened.Manifest, Package: opened}); err != nil {
- t.Fatal(err)
- }
-
- svcs := a.Services(ctx, 7)
- if len(svcs) != 1 {
- t.Fatalf("services = %d", len(svcs))
- }
- svc := svcs[0]
- if svc.TransportType != types.MCPTransportPlugin || svc.URL != nil || !svc.Enabled ||
- svc.PluginVersion != "1.2.0" || !strings.Contains(string(svc.ToolViews["search"]), `"link":"url"`) {
- t.Fatalf("service = %+v", svc)
- }
-
- c, err := mcp.NewMCPClient(&mcp.ClientConfig{Service: svc})
- if err != nil {
- t.Fatal(err)
- }
- // The manager connects without a workspace in the context.
- if err := c.Connect(ctx); err != nil {
- t.Fatal(err)
- }
- if _, err := c.Initialize(ctx); err != nil {
- t.Fatal(err)
- }
- tools, err := c.ListTools(ctx)
- if err != nil || len(tools) != 1 || tools[0].Name != "search" {
- t.Fatalf("tools = %+v, %v", tools, err)
- }
- res, err := c.CallTool(ctx, "search", map[string]any{"q": "login"})
- if err != nil || res.IsError || res.Content[0].Text != "one issue" {
- t.Fatalf("call = %+v, %v", res, err)
- }
- got, _ := json.Marshal(res.StructuredContent)
- if string(got) != `{"issues":[{"key":"ENG-1","summary":"login"}],"site":"acme.example","tenant":7}` {
- t.Fatalf("structured = %s", got)
- }
-
- // Another workspace does not see a plugin it has not switched on.
- if len(a.Services(ctx, 8)) != 0 {
- t.Fatal("tenant 8 sees the plugin's server")
- }
- if err := a.Deactivate(ctx, "acme.tools"); err != nil {
- t.Fatal(err)
- }
- if _, err := mcp.NewMCPClient(&mcp.ClientConfig{Service: svc}); err == nil {
- t.Fatal("an unloaded plugin's server still connects")
- }
-}
diff --git a/internal/plugin/activate/parsers.go b/internal/plugin/activate/parsers.go
deleted file mode 100644
index b2e8430f2..000000000
--- a/internal/plugin/activate/parsers.go
+++ /dev/null
@@ -1,152 +0,0 @@
-package activate
-
-import (
- "context"
- "errors"
- "fmt"
- "path"
- "strings"
- "sync"
- "time"
-
- "github.com/Tencent/WeKnora/internal/infrastructure/docparser"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// parseTimeout bounds one plugin parse when the caller set no deadline.
-const parseTimeout = 10 * time.Minute
-
-// Parsers registers code plugins' document parsers as parser engines under
-// their qualified IDs, next to the builtin engines and the docreader's.
-type Parsers struct {
- iv *Invoker
-
- mu sync.Mutex
- registered map[string][]string
-}
-
-// NewParsers creates the parser activator.
-func NewParsers(iv *Invoker) *Parsers {
- return &Parsers{iv: iv, registered: map[string][]string{}}
-}
-
-// Name implements reconcile.Activator.
-func (a *Parsers) Name() string { return "parsers" }
-
-// Activate implements reconcile.Activator: all of a plugin's parsers or none.
-func (a *Parsers) Activate(_ context.Context, l *reconcile.Loaded) error {
- var ids []string
- for _, c := range l.Manifest.Contributes[manifest.PointParsers] {
- e := &pluginEngine{
- iv: a.iv, m: l.Manifest, local: c.ID, name: manifest.QualifiedID(l.Manifest.ID, c.ID),
- description: c.Description.Default, names: displayNames(c.Name), fileTypes: c.FileTypes,
- }
- if err := docparser.RegisterPluginEngine(e); err != nil {
- for _, done := range ids {
- docparser.UnregisterPluginEngine(done)
- }
- return fmt.Errorf("parser %s: %w", c.ID, err)
- }
- ids = append(ids, e.name)
- }
- a.mu.Lock()
- a.registered[l.Manifest.ID] = ids
- a.mu.Unlock()
- return nil
-}
-
-// Deactivate implements reconcile.Activator.
-func (a *Parsers) Deactivate(_ context.Context, pluginID string) error {
- a.mu.Lock()
- ids := a.registered[pluginID]
- delete(a.registered, pluginID)
- a.mu.Unlock()
- for _, id := range ids {
- docparser.UnregisterPluginEngine(id)
- }
- return nil
-}
-
-func displayNames(t manifest.LocalizedText) map[string]string {
- out := map[string]string{}
- for k, v := range t.Locales {
- out[k] = v
- }
- if t.Default != "" {
- out["default"] = t.Default
- }
- return out
-}
-
-// pluginEngine is a plugin parser as a docparser engine.
-type pluginEngine struct {
- iv *Invoker
- m *manifest.Manifest
- local, name string
- description string
- names map[string]string
- fileTypes []string
-}
-
-var (
- _ docparser.EngineRegistration = (*pluginEngine)(nil)
- _ docparser.PluginEngineInfo = (*pluginEngine)(nil)
-)
-
-func (e *pluginEngine) Name() string { return e.name }
-func (e *pluginEngine) Description() string { return e.description }
-func (e *pluginEngine) FileTypes(bool) []string { return e.fileTypes }
-func (e *pluginEngine) PluginID() string { return e.m.ID }
-func (e *pluginEngine) DisplayNames() map[string]string { return e.names }
-func (e *pluginEngine) CheckAvailable(bool, map[string]string) (bool, string) {
- if _, err := e.iv.clients.Client(context.Background(), e.m); err != nil {
- return false, err.Error()
- }
- return true, ""
-}
-
-// NewReader implements docparser.EngineRegistration. deps.Overrides carry
-// other engines' credentials and are deliberately not passed on; the
-// plugin's own settings travel in its system and tenant configuration.
-func (e *pluginEngine) NewReader(context.Context, docparser.ReaderDeps) (interfaces.DocReader, error) {
- return &remoteParser{engine: e}, nil
-}
-
-// remoteParser reads one document through the plugin.
-type remoteParser struct{ engine *pluginEngine }
-
-// Read implements interfaces.DocReader. A retryable plugin failure (the
-// plugin is down, rate limited) is returned as an error so the task is
-// retried; any other failure is final and reported in the result.
-func (r *remoteParser) Read(ctx context.Context, req *types.ReadRequest) (*types.ReadResult, error) {
- ctx, cancel := withDefaultTimeout(ctx, parseTimeout)
- defer cancel()
- in := pluginapi.ParseInput{
- FileName: req.FileName, FileType: strings.ToLower(strings.TrimPrefix(req.FileType, ".")),
- Content: req.FileContent, URL: req.URL, Title: req.Title,
- }
- var out pluginapi.ParseOutput
- err := r.engine.iv.Call(ctx, r.engine.m, pluginapi.ParsePath(r.engine.local), nil, in, &out)
- if err != nil {
- var pe *pluginapi.Error
- if errors.As(err, &pe) && !pe.Retryable {
- return &types.ReadResult{Error: err.Error()}, nil
- }
- return nil, err
- }
- res := &types.ReadResult{MarkdownContent: out.Markdown, Metadata: out.Metadata}
- for _, img := range out.Images {
- if img.OriginalRef == "" || len(img.Data) == 0 {
- continue
- }
- res.ImageRefs = append(res.ImageRefs, types.ImageRef{
- Filename: path.Base(img.OriginalRef), OriginalRef: img.OriginalRef,
- MimeType: img.MimeType, ImageData: img.Data,
- })
- }
- return res, nil
-}
diff --git a/internal/plugin/activate/parsers_test.go b/internal/plugin/activate/parsers_test.go
deleted file mode 100644
index 5468f9be5..000000000
--- a/internal/plugin/activate/parsers_test.go
+++ /dev/null
@@ -1,120 +0,0 @@
-package activate
-
-import (
- "context"
- "net/http/httptest"
- "slices"
- "strings"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/infrastructure/docparser"
- "github.com/Tencent/WeKnora/internal/plugin/pkg"
- "github.com/Tencent/WeKnora/internal/plugin/plugintest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-const ocrManifest = `schemaVersion: 1
-id: acme.ocr
-version: 1.0.0
-apiVersion: weknora.plugin/v1
-name: { en-US: ACME OCR }
-publisher: { id: acme }
-runtime: { type: host, kind: binary, entry: bin/ocr }
-contributes:
- parsers:
- - id: ocr
- name: { en-US: ACME OCR, zh-CN: ACME 识别 }
- fileTypes: [tiff, pdf]
-`
-
-func TestPluginParser(t *testing.T) {
- ctx := context.Background()
- plugin := pluginsdk.New(pluginsdk.Info{ID: "acme.ocr", Version: "1.0.0"})
- plugin.Parser("ocr", pluginsdk.ParserFunc(
- func(_ context.Context, _ *pluginsdk.Call, in pluginapi.ParseInput) (*pluginapi.ParseOutput, error) {
- switch string(in.Content) {
- case "busy":
- return nil, pluginapi.Errorf(pluginapi.CodeRateLimited, "slow down")
- case "broken":
- return nil, pluginapi.Errorf(pluginapi.CodeInvalidConfig, "not a scanned document")
- }
- return &pluginapi.ParseOutput{
- Markdown: "# " + in.FileName + "\n\n![scan](page-1.png)",
- Images: []pluginapi.ParsedImage{
- {OriginalRef: "page-1.png", MimeType: "image/png", Data: []byte{1, 2}},
- },
- Metadata: map[string]string{"pages": "1"},
- }, nil
- }))
- srv := httptest.NewServer(plugin.Handler())
- defer srv.Close()
-
- p, err := pkg.Open(plugintest.Zip(t, map[string]string{"plugin.yaml": ocrManifest, "bin/ocr": "x"}))
- if err != nil {
- t.Fatal(err)
- }
- a := NewParsers(NewInvoker(fakeClients{client.New(srv.URL, nil, nil)}))
- if err := a.Activate(ctx, &reconcile.Loaded{Manifest: p.Manifest, Package: p}); err != nil {
- t.Fatalf("Activate: %v", err)
- }
- defer func() { _ = a.Deactivate(ctx, "acme.ocr") }()
-
- var listed *types.ParserEngineInfo
- for _, e := range docparser.ListAllEngines(false, nil, nil) {
- if e.Name == "acme.ocr/ocr" {
- listed = &e
- }
- }
- if listed == nil || !listed.Available || listed.PluginID != "acme.ocr" ||
- listed.DisplayNames["zh-CN"] != "ACME 识别" ||
- !slices.Equal(listed.FileTypes, []string{"tiff", "pdf"}) {
- t.Fatalf("listed = %+v", listed)
- }
- if !slices.Contains(docparser.PluginFileTypes(), "tiff") {
- t.Fatal("plugin file types must be importable")
- }
-
- reader, err := docparser.NewReader(ctx, "acme.ocr/ocr", "tiff", false, docparser.ReaderDeps{
- Overrides: map[string]string{"mineru_api_key": "must-not-leak"},
- })
- if err != nil {
- t.Fatal(err)
- }
- res, err := reader.Read(
- ctx,
- &types.ReadRequest{FileName: "scan.tiff", FileType: ".TIFF", FileContent: []byte("img")},
- )
- if err != nil || !strings.HasPrefix(res.MarkdownContent, "# scan.tiff") || res.Metadata["pages"] != "1" ||
- len(
- res.ImageRefs,
- ) != 1 || res.ImageRefs[0].OriginalRef != "page-1.png" || res.ImageRefs[0].Filename != "page-1.png" {
- t.Fatalf("Read = %+v, %v", res, err)
- }
-
- if _, err := reader.Read(ctx, &types.ReadRequest{FileContent: []byte("busy")}); err == nil {
- t.Fatal("a retryable plugin error must come back as an error, so the task retries")
- }
- res, err = reader.Read(ctx, &types.ReadRequest{FileContent: []byte("broken")})
- if err != nil || res.Error != "not a scanned document" {
- t.Fatalf("a permanent plugin error must fail the document: %+v, %v", res, err)
- }
-
- if err := a.Deactivate(ctx, "acme.ocr"); err != nil {
- t.Fatal(err)
- }
- if _, err := docparser.NewReader(ctx, "acme.ocr/ocr", "tiff", false, docparser.ReaderDeps{}); err == nil ||
- !strings.Contains(err.Error(), "not installed or not running") {
- t.Fatalf("a removed plugin engine must not fall through to the docreader: %v", err)
- }
-}
-
-func TestPluginParserCannotShadowABuiltin(t *testing.T) {
- e := &pluginEngine{name: "simple"}
- if err := docparser.RegisterPluginEngine(e); err == nil {
- t.Fatal("a plugin engine must not take a builtin engine's name")
- }
-}
diff --git a/internal/plugin/activate/skills.go b/internal/plugin/activate/skills.go
deleted file mode 100644
index 74bf15c2a..000000000
--- a/internal/plugin/activate/skills.go
+++ /dev/null
@@ -1,110 +0,0 @@
-package activate
-
-import (
- "archive/zip"
- "bytes"
- "context"
- "errors"
- "fmt"
- "path"
- "strings"
- "sync"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/plugin/tenancy"
-)
-
-// SkillSourcePrefix marks a skill install source that names a plugin skill:
-// "plugin:<plugin id>/<skill id>".
-const SkillSourcePrefix = "plugin:"
-
-// ErrUnknownSkill is returned for a plugin skill no loaded, enabled plugin
-// provides.
-var ErrUnknownSkill = errors.New("no enabled plugin provides this skill")
-
-// Skills offers the skills of installed plugins for installation into a
-// workspace's sandbox, through the same pipeline as an uploaded skill: the
-// sandbox is where skills run, so that is where a plugin skill has to go.
-type Skills struct {
- mu sync.RWMutex
- tenancy *tenancy.Service
- loaded map[string]*reconcile.Loaded
-}
-
-// NewSkills creates the skills activator; Bind completes it.
-func NewSkills() *Skills { return &Skills{loaded: map[string]*reconcile.Loaded{}} }
-
-// Bind supplies the tenant switches.
-func (a *Skills) Bind(t *tenancy.Service) {
- a.mu.Lock()
- a.tenancy = t
- a.mu.Unlock()
-}
-
-// Name implements reconcile.Activator.
-func (a *Skills) Name() string { return "skills" }
-
-// Activate implements reconcile.Activator.
-func (a *Skills) Activate(_ context.Context, l *reconcile.Loaded) error {
- a.mu.Lock()
- defer a.mu.Unlock()
- if len(l.Manifest.Contributes[manifest.PointSkills]) == 0 {
- delete(a.loaded, l.Manifest.ID)
- return nil
- }
- a.loaded[l.Manifest.ID] = l
- return nil
-}
-
-// Deactivate implements reconcile.Activator.
-func (a *Skills) Deactivate(_ context.Context, pluginID string) error {
- a.mu.Lock()
- delete(a.loaded, pluginID)
- a.mu.Unlock()
- return nil
-}
-
-// Archive returns a plugin skill as a skill bundle (a zip with SKILL.md at
-// its root) for a workspace that has the plugin enabled. source is the
-// qualified skill ID, with or without SkillSourcePrefix.
-func (a *Skills) Archive(ctx context.Context, tenantID uint64, source string) ([]byte, error) {
- qualified := strings.TrimPrefix(source, SkillSourcePrefix)
- pluginID, localID, ok := strings.Cut(qualified, "/")
- if !ok {
- return nil, fmt.Errorf("%w: %q is not <plugin>/<skill>", ErrUnknownSkill, qualified)
- }
- a.mu.RLock()
- l, loaded := a.loaded[pluginID]
- t := a.tenancy
- a.mu.RUnlock()
- if !loaded || t == nil || !t.ContributionEnabled(ctx, tenantID, manifest.PointSkills, qualified) {
- return nil, fmt.Errorf("%w: %s", ErrUnknownSkill, qualified)
- }
- for _, c := range l.Manifest.Contributes[manifest.PointSkills] {
- if c.ID == localID {
- return zipDir(l, c.Path)
- }
- }
- return nil, fmt.Errorf("%w: %s", ErrUnknownSkill, qualified)
-}
-
-func zipDir(l *reconcile.Loaded, dir string) ([]byte, error) {
- var buf bytes.Buffer
- zw := zip.NewWriter(&buf)
- prefix := strings.TrimSuffix(path.Clean(dir), "/") + "/"
- for _, name := range l.Package.Files(dir) {
- data, _ := l.Package.ReadFile(name)
- w, err := zw.Create(strings.TrimPrefix(name, prefix))
- if err != nil {
- return nil, err
- }
- if _, err := w.Write(data); err != nil {
- return nil, err
- }
- }
- if err := zw.Close(); err != nil {
- return nil, err
- }
- return buf.Bytes(), nil
-}
diff --git a/internal/plugin/activate/skills_test.go b/internal/plugin/activate/skills_test.go
deleted file mode 100644
index f875632b0..000000000
--- a/internal/plugin/activate/skills_test.go
+++ /dev/null
@@ -1,53 +0,0 @@
-package activate
-
-import (
- "context"
- "errors"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/application/service"
- "github.com/Tencent/WeKnora/internal/plugin/plugintest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/plugin/tenancy"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-func TestPluginSkillArchiveInstallsLikeAnUpload(t *testing.T) {
- ctx := context.Background()
- repo, store, reg := plugintest.NewMemRepo(), &plugintest.MemStore{}, registry.New()
- ten := tenancy.NewService(reg, &plugintest.MemTenantSettings{})
- skills := NewSkills()
- skills.Bind(ten)
- r := reconcile.New(reconcile.Options{
- Repo: repo, Store: store, Registry: reg, CacheDir: t.TempDir(), Activators: []reconcile.Activator{skills},
- })
- plugintest.Install(t, repo, store, plugintest.KitPackageWith(t, "1.0.0",
- "Triage incoming issues by severity."), types.PluginStateEnabled)
- if err := r.Reconcile(ctx); err != nil {
- t.Fatal(err)
- }
-
- if _, err := skills.Archive(ctx, 1, "plugin:acme.kit/triage"); !errors.Is(err, ErrUnknownSkill) {
- t.Fatalf("a workspace without the plugin enabled must be refused, got %v", err)
- }
- if err := ten.SetEnabled(ctx, 1, "acme.kit", true, "u"); err != nil {
- t.Fatal(err)
- }
- archive, err := skills.Archive(ctx, 1, "plugin:acme.kit/triage")
- if err != nil {
- t.Fatalf("Archive: %v", err)
- }
- bundle, err := service.ParseSkillBundle(archive)
- if err != nil {
- t.Fatalf("the archive must be a valid skill bundle: %v", err)
- }
- if bundle.Name != "triage" {
- t.Fatalf("bundle = %+v", bundle)
- }
- for _, bad := range []string{"plugin:acme.kit/nope", "plugin:acme.kit", "plugin:other.kit/triage"} {
- if _, err := skills.Archive(ctx, 1, bad); !errors.Is(err, ErrUnknownSkill) {
- t.Errorf("Archive(%s) = %v", bad, err)
- }
- }
-}
diff --git a/internal/plugin/activate/ui.go b/internal/plugin/activate/ui.go
deleted file mode 100644
index 6e96d3309..000000000
--- a/internal/plugin/activate/ui.go
+++ /dev/null
@@ -1,116 +0,0 @@
-package activate
-
-import (
- "context"
- "errors"
- "os"
- "path"
- "strings"
- "sync"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/utils"
-)
-
-// ErrNoPage is a page file or mount that does not exist.
-var ErrNoPage = errors.New("no such plugin page")
-
-// UIPages keeps the pages of loaded plugins (pages, settingsSections,
-// kbTabs, and tool result pages of mcpServers): where their files are, and
-// which mounts exist.
-type UIPages struct {
- mu sync.RWMutex
- loaded map[string]uiPlugin
-}
-
-type uiPlugin struct {
- m *manifest.Manifest
- dir string
-}
-
-// NewUIPages creates the page activator.
-func NewUIPages() *UIPages { return &UIPages{loaded: map[string]uiPlugin{}} }
-
-// Name implements reconcile.Activator.
-func (a *UIPages) Name() string { return "ui" }
-
-// Activate implements reconcile.Activator.
-func (a *UIPages) Activate(_ context.Context, l *reconcile.Loaded) error {
- has := false
- for point, list := range l.Manifest.Contributes {
- has = has || manifest.IsUIPoint(point)
- for _, c := range list {
- has = has || (point == manifest.PointMCPServers && c.HasToolPages())
- }
- }
- a.mu.Lock()
- defer a.mu.Unlock()
- if has {
- a.loaded[l.Manifest.ID] = uiPlugin{m: l.Manifest, dir: l.Dir}
- } else {
- delete(a.loaded, l.Manifest.ID)
- }
- return nil
-}
-
-// Deactivate implements reconcile.Activator.
-func (a *UIPages) Deactivate(_ context.Context, pluginID string) error {
- a.mu.Lock()
- delete(a.loaded, pluginID)
- a.mu.Unlock()
- return nil
-}
-
-// File returns the path on disk of a page file of the loaded version. Only
-// files under manifest.UIRoot are served.
-func (a *UIPages) File(pluginID, version, rel string) (string, error) {
- a.mu.RLock()
- p, ok := a.loaded[pluginID]
- a.mu.RUnlock()
- if !ok || p.m.Version != version {
- return "", ErrNoPage
- }
- rel = path.Clean("/" + rel)[1:]
- if !strings.HasPrefix(rel, manifest.UIRoot) {
- return "", ErrNoPage
- }
- full, err := utils.SafeJoinUnderBase(p.dir, rel)
- if err != nil {
- return "", ErrNoPage
- }
- info, err := os.Stat(full)
- if err != nil || !info.Mode().IsRegular() {
- return "", ErrNoPage
- }
- return full, nil
-}
-
-// Mount is one page of a loaded plugin.
-type Mount struct {
- Manifest *manifest.Manifest
- Point manifest.Point
- Contribution manifest.Contribution
-}
-
-// MinRole is the workspace role the page needs.
-func (m Mount) MinRole() string { return manifest.UIMinRole(m.Point, m.Contribution) }
-
-// Mount finds a page by "<point>/<id>".
-func (a *UIPages) Mount(pluginID, mount string) (Mount, error) {
- a.mu.RLock()
- p, ok := a.loaded[pluginID]
- a.mu.RUnlock()
- point, id, found := strings.Cut(mount, "/")
- toolPages := manifest.Point(point) == manifest.PointMCPServers
- if !ok || !found || (!manifest.IsUIPoint(manifest.Point(point)) && !toolPages) {
- return Mount{}, ErrNoPage
- }
- for _, c := range p.m.Contributes[manifest.Point(point)] {
- // A tool result page answers to its server: "mcpServers/<id>".
- if c.ID == id && (!toolPages || c.HasToolPages()) {
- return Mount{Manifest: p.m, Point: manifest.Point(point), Contribution: c}, nil
- }
- }
- return Mount{}, ErrNoPage
-}
diff --git a/internal/plugin/activate/vendors.go b/internal/plugin/activate/vendors.go
deleted file mode 100644
index a336ffbeb..000000000
--- a/internal/plugin/activate/vendors.go
+++ /dev/null
@@ -1,141 +0,0 @@
-package activate
-
-import (
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "os"
- "path"
- "path/filepath"
- "sync"
-
- "gopkg.in/yaml.v3"
-
- modelruntime "github.com/Tencent/WeKnora/internal/models/runtime"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/pkg"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
-)
-
-// ModelVendors registers the model vendors of installed plugins with the
-// model runtime. A plugin vendor's ID is its qualified contribution ID
-// ("acme.ai/acme"), so it can never shadow a built-in vendor and the tenant
-// plugin switch resolves it directly.
-type ModelVendors struct {
- rt *modelruntime.Runtime
-
- mu sync.Mutex
- registered map[string][]string // plugin ID → vendor IDs
-}
-
-// NewModelVendors creates the model vendor activator on the default runtime.
-func NewModelVendors() *ModelVendors {
- return &ModelVendors{rt: modelruntime.Default(), registered: map[string][]string{}}
-}
-
-// Name implements reconcile.Activator.
-func (a *ModelVendors) Name() string { return "modelVendors" }
-
-// Activate implements reconcile.Activator. It registers all of a plugin's
-// vendors or none.
-func (a *ModelVendors) Activate(_ context.Context, l *reconcile.Loaded) error {
- var ids []string
- for _, c := range l.Manifest.Contributes[manifest.PointModelVendors] {
- id := manifest.QualifiedID(l.Manifest.ID, c.ID)
- def, err := vendorDefinition(l.Package, c)
- if err == nil {
- err = a.rt.RegisterPlugin(id, def, filepath.Join(l.Dir, filepath.FromSlash(path.Dir(c.Path))))
- }
- if err != nil {
- for _, done := range ids {
- a.rt.Unregister(done)
- }
- return fmt.Errorf("model vendor %s: %w", c.ID, err)
- }
- ids = append(ids, id)
- }
- a.mu.Lock()
- a.registered[l.Manifest.ID] = ids
- a.mu.Unlock()
- return nil
-}
-
-// Deactivate implements reconcile.Activator.
-func (a *ModelVendors) Deactivate(_ context.Context, pluginID string) error {
- a.mu.Lock()
- ids := a.registered[pluginID]
- delete(a.registered, pluginID)
- a.mu.Unlock()
- for _, id := range ids {
- a.rt.Unregister(id)
- }
- return nil
-}
-
-// vendorDefinition reads one vendor file (YAML or JSON) as overlay JSON,
-// taking the name and description from the manifest when the file has none.
-func vendorDefinition(p *pkg.Package, c manifest.Contribution) ([]byte, error) {
- raw, ok := p.ReadFile(c.Path)
- if !ok {
- return nil, fmt.Errorf("%s is not in the package", c.Path)
- }
- var def map[string]any
- if err := yaml.Unmarshal(raw, &def); err != nil {
- return nil, fmt.Errorf("parse %s: %w", c.Path, err)
- }
- if def == nil {
- return nil, fmt.Errorf("%s is empty", c.Path)
- }
- if _, ok := def["name"]; !ok && c.Name.Default != "" {
- def["name"] = c.Name.Default
- if len(c.Name.Locales) > 0 {
- def["names"] = c.Name.Locales
- }
- }
- if _, ok := def["description"]; !ok && c.Description.Default != "" {
- def["description"] = c.Description.Default
- if len(c.Description.Locales) > 0 {
- def["descriptions"] = c.Description.Locales
- }
- }
- return json.Marshal(def)
-}
-
-// CheckModelVendors verifies at install time that every vendor a package
-// declares would register, against a scratch runtime.
-func CheckModelVendors(p *pkg.Package) error {
- contribs := p.Manifest.Contributes[manifest.PointModelVendors]
- if len(contribs) == 0 {
- return nil
- }
- dir, err := os.MkdirTemp("", "weknora-vendor-check-")
- if err != nil {
- return err
- }
- defer func() { _ = os.RemoveAll(dir) }()
- // Icons are read from disk, so the files they may name are written out.
- for _, name := range p.Files("") {
- data, _ := p.ReadFile(name)
- target := filepath.Join(dir, filepath.FromSlash(name))
- if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
- return err
- }
- if err := os.WriteFile(target, data, 0o644); err != nil {
- return err
- }
- }
- scratch := modelruntime.New()
- var errs []error
- for _, c := range contribs {
- def, err := vendorDefinition(p, c)
- if err == nil {
- err = scratch.RegisterPlugin(manifest.QualifiedID(p.Manifest.ID, c.ID), def,
- filepath.Join(dir, filepath.FromSlash(path.Dir(c.Path))))
- }
- if err != nil {
- errs = append(errs, fmt.Errorf("model vendor %s: %w", c.ID, err))
- }
- }
- return errors.Join(errs...)
-}
diff --git a/internal/plugin/activate/vendors_test.go b/internal/plugin/activate/vendors_test.go
deleted file mode 100644
index 57cee805a..000000000
--- a/internal/plugin/activate/vendors_test.go
+++ /dev/null
@@ -1,92 +0,0 @@
-package activate
-
-import (
- "context"
- "strings"
- "testing"
-
- modelruntime "github.com/Tencent/WeKnora/internal/models/runtime"
- "github.com/Tencent/WeKnora/internal/plugin/pkg"
- "github.com/Tencent/WeKnora/internal/plugin/plugintest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-const vendorManifest = `schemaVersion: 1
-id: acme.ai
-version: 1.0.0
-name: { en-US: ACME AI }
-publisher: { id: acme }
-runtime: { type: declarative }
-contributes:
- modelVendors:
- - id: acme
- name: { en-US: ACME AI, zh-CN: ACME 智能 }
- path: vendors/acme.yaml
-`
-
-func vendorPackage(t *testing.T, vendorYAML string) []byte {
- return plugintest.Zip(t, map[string]string{
- "plugin.yaml": vendorManifest,
- "vendors/acme.yaml": vendorYAML,
- "vendors/acme.svg": `<svg xmlns="http://www.w3.org/2000/svg"></svg>`,
- })
-}
-
-func TestModelVendorsFollowThePlugin(t *testing.T) {
- ctx := context.Background()
- rt := modelruntime.New()
- vendors := &ModelVendors{rt: rt, registered: map[string][]string{}}
- repo, store := plugintest.NewMemRepo(), &plugintest.MemStore{}
- r := reconcile.New(reconcile.Options{
- Repo: repo, Store: store, Registry: registry.New(), CacheDir: t.TempDir(),
- Activators: []reconcile.Activator{vendors},
- })
- plugintest.Install(t, repo, store, vendorPackage(t, `
-base_url: https://api.acme.example/v1
-icon: acme.svg
-model_types: [chat, embedding]
-models:
- - { id: acme-large, context_window: 128000 }
-`), types.PluginStateEnabled)
- if err := r.Reconcile(ctx); err != nil {
- t.Fatalf("Reconcile: %v", err)
- }
- v, ok := rt.Get("acme.ai/acme")
- if !ok {
- t.Fatal("vendor not registered")
- }
- if v.Name != "ACME AI" || v.Names["zh-CN"] != "ACME 智能" || len(v.Icon) == 0 {
- t.Fatalf("vendor = name %q names %v icon %d bytes", v.Name, v.Names, len(v.Icon))
- }
-
- row, _ := repo.GetPlugin(ctx, "acme.ai")
- row.DesiredState = types.PluginStateDisabled
- _ = repo.SavePlugin(ctx, row)
- _ = r.Reconcile(ctx)
- if _, ok := rt.Get("acme.ai/acme"); ok {
- t.Fatal("disabling the plugin must remove its vendor")
- }
-}
-
-func TestCheckModelVendorsRejectsBrokenDefinitions(t *testing.T) {
- for name, def := range map[string]string{
- "unknown key": "base_url: https://x\nnmae: typo\n",
- "deploy key": "api_key: sk-123\n",
- "bad api": "api: carrier-pigeon\n",
- "bad icon": "icon: ../../etc/passwd\n",
- } {
- p, err := pkg.Open(vendorPackage(t, def))
- if err != nil {
- t.Fatalf("%s: Open: %v", name, err)
- }
- if err := CheckModelVendors(p); err == nil || !strings.Contains(err.Error(), "model vendor acme") {
- t.Errorf("%s: want a vendor error, got %v", name, err)
- }
- }
- p, _ := pkg.Open(vendorPackage(t, "base_url: https://api.acme.example/v1\nicon: acme.svg\n"))
- if err := CheckModelVendors(p); err != nil {
- t.Fatalf("valid vendor rejected: %v", err)
- }
-}
diff --git a/internal/plugin/activate/websearch.go b/internal/plugin/activate/websearch.go
deleted file mode 100644
index f82d1184d..000000000
--- a/internal/plugin/activate/websearch.go
+++ /dev/null
@@ -1,278 +0,0 @@
-package activate
-
-import (
- "context"
- "encoding/base64"
- "encoding/json"
- "fmt"
- "path"
- "sort"
- "strings"
- "sync"
-
- "gopkg.in/yaml.v3"
-
- infra_web_search "github.com/Tencent/WeKnora/internal/infrastructure/web_search"
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/pkg"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// WebSearch registers code plugins' web search providers as provider types.
-// A plugin provider stores its configuration in the same parameters as a
-// builtin one: the secret api_key and string fields under extra_config, so
-// encryption, redaction and the credentials endpoint work unchanged.
-type WebSearch struct {
- iv *Invoker
- registry *infra_web_search.Registry
-
- mu sync.Mutex
- registered map[string][]string // plugin ID → provider type IDs
-}
-
-// NewWebSearch creates the web search activator.
-func NewWebSearch(iv *Invoker, registry *infra_web_search.Registry) *WebSearch {
- return &WebSearch{iv: iv, registry: registry, registered: map[string][]string{}}
-}
-
-// Name implements reconcile.Activator.
-func (a *WebSearch) Name() string { return "webSearch" }
-
-// Activate implements reconcile.Activator: all of a plugin's providers or
-// none.
-func (a *WebSearch) Activate(_ context.Context, l *reconcile.Loaded) error {
- var ids []string
- for _, c := range l.Manifest.Contributes[manifest.PointWebSearch] {
- id := manifest.QualifiedID(l.Manifest.ID, c.ID)
- pt, err := webSearchType(l, c, id)
- if err == nil {
- m, local := l.Manifest, c.ID
- err = a.registry.RegisterPlugin(
- pt,
- func(params types.WebSearchProviderParameters) (interfaces.WebSearchProvider, error) {
- return &remoteSearch{
- iv: a.iv,
- m: m,
- local: local,
- typeID: id,
- instance: webSearchInstance(params),
- }, nil
- },
- )
- }
- if err != nil {
- for _, done := range ids {
- a.registry.Unregister(done)
- }
- return fmt.Errorf("webSearch %s: %w", c.ID, err)
- }
- ids = append(ids, id)
- }
- a.mu.Lock()
- a.registered[l.Manifest.ID] = ids
- a.mu.Unlock()
- return nil
-}
-
-// Deactivate implements reconcile.Activator.
-func (a *WebSearch) Deactivate(_ context.Context, pluginID string) error {
- a.mu.Lock()
- ids := a.registered[pluginID]
- delete(a.registered, pluginID)
- a.mu.Unlock()
- for _, id := range ids {
- a.registry.Unregister(id)
- }
- return nil
-}
-
-// webSearchType describes a plugin provider for the type list.
-func webSearchType(l *reconcile.Loaded, c manifest.Contribution, id string) (infra_web_search.PluginType, error) {
- src, err := instanceSchema(l.Package, c)
- if err != nil {
- return infra_web_search.PluginType{}, err
- }
- schema, requiresKey, err := webSearchSchema(src)
- if err != nil {
- return infra_web_search.PluginType{}, err
- }
- icon, err := iconDataURI(l.Package, c.Icon)
- if err != nil {
- return infra_web_search.PluginType{}, err
- }
- info := types.WebSearchProviderTypeInfo{
- ID: id, Name: c.Name.Default, Names: c.Name.Locales, Description: c.Description.Default,
- RequiresAPIKey: requiresKey, Icon: icon, PluginID: l.Manifest.ID, ConfigSchema: schema,
- }
- validate := func(params types.WebSearchProviderParameters) error {
- value := map[string]any{"api_key": params.APIKey}
- extra := map[string]any{}
- for k, v := range params.ExtraConfig {
- extra[k] = v
- }
- value["extra_config"] = extra
- if errs := configschema.Validate(schema, value); len(errs) > 0 {
- return errs
- }
- return nil
- }
- return infra_web_search.PluginType{Info: info, Validate: validate}, nil
-}
-
-// webSearchSchema maps a plugin's instance schema onto the provider
-// parameters: api_key stays top-level (the only secret), everything else
-// moves under extra_config, which stores strings.
-func webSearchSchema(src *configschema.Schema) (*configschema.Schema, bool, error) {
- out := configschema.Object()
- if src == nil {
- return out, false, nil
- }
- required := map[string]bool{}
- for _, k := range src.Required {
- required[k] = true
- }
- extra := &configschema.Schema{
- Type: configschema.TypeObject, Group: "credentials", Order: 40, Properties: map[string]*configschema.Schema{},
- }
- var keys []string
- for k := range src.Properties {
- keys = append(keys, k)
- }
- sort.Strings(keys)
- requiresKey := false
- for _, k := range keys {
- prop := *src.Properties[k]
- if k == "api_key" {
- prop.Type, prop.Secret, prop.Group = configschema.TypeString, true, "credentials"
- out.Set("api_key", &prop, required[k])
- requiresKey = required[k]
- continue
- }
- if prop.Secret {
- return nil, false, fmt.Errorf("instanceSchema field %s is secret; only api_key may be", k)
- }
- if prop.Type != "" && prop.Type != configschema.TypeString {
- return nil, false, fmt.Errorf(
- "instanceSchema field %s must be a string (web search settings are stored as strings)",
- k,
- )
- }
- prop.Type, prop.Group = configschema.TypeString, ""
- extra.Set(k, &prop, required[k])
- }
- if len(extra.Properties) > 0 {
- out.Set("extra_config", extra, len(extra.Required) > 0)
- }
- return out, requiresKey, nil
-}
-
-// webSearchInstance is what the plugin receives as config.instance.
-func webSearchInstance(params types.WebSearchProviderParameters) map[string]any {
- out := map[string]any{}
- if params.APIKey != "" {
- out["api_key"] = params.APIKey
- }
- for k, v := range params.ExtraConfig {
- out[k] = v
- }
- return out
-}
-
-// instanceSchema reads a contribution's instanceSchema file (JSON or YAML).
-func instanceSchema(p *pkg.Package, c manifest.Contribution) (*configschema.Schema, error) {
- if c.InstanceSchema == "" || p == nil {
- return nil, nil
- }
- raw, ok := p.ReadFile(c.InstanceSchema)
- if !ok {
- return nil, fmt.Errorf("instanceSchema %s is not in the package", c.InstanceSchema)
- }
- var v any
- if err := yaml.Unmarshal(raw, &v); err != nil {
- return nil, fmt.Errorf("instanceSchema %s: %w", c.InstanceSchema, err)
- }
- b, err := json.Marshal(v)
- if err != nil {
- return nil, err
- }
- s, err := configschema.Parse(b)
- if err != nil {
- return nil, fmt.Errorf("instanceSchema %s: %w", c.InstanceSchema, err)
- }
- return s, nil
-}
-
-// maxIconBytes caps an icon inlined into type listings.
-const maxIconBytes = 64 << 10
-
-// iconDataURI inlines a package icon (SVG or PNG) as a data: URI.
-func iconDataURI(p *pkg.Package, file string) (string, error) {
- if file == "" || p == nil {
- return "", nil
- }
- b, ok := p.ReadFile(file)
- if !ok {
- return "", fmt.Errorf("icon %s is not in the package", file)
- }
- if len(b) > maxIconBytes {
- return "", fmt.Errorf("icon %s is over %d bytes", file, maxIconBytes)
- }
- var mime string
- switch strings.ToLower(path.Ext(file)) {
- case ".svg":
- mime = "image/svg+xml"
- case ".png":
- mime = "image/png"
- default:
- return "", fmt.Errorf("icon %s must be .svg or .png", file)
- }
- return "data:" + mime + ";base64," + base64.StdEncoding.EncodeToString(b), nil
-}
-
-// remoteSearch is one configured plugin provider.
-type remoteSearch struct {
- iv *Invoker
- m *manifest.Manifest
- local string
- typeID string
- instance map[string]any
-}
-
-// Name implements interfaces.WebSearchProvider.
-func (r *remoteSearch) Name() string { return r.typeID }
-
-// Search implements interfaces.WebSearchProvider.
-func (r *remoteSearch) Search(
- ctx context.Context, query string, maxResults int, includeDate bool,
-) ([]*types.WebSearchResult, error) {
- return r.SearchWithFilters(ctx, query, maxResults, includeDate, types.WebSearchFilters{})
-}
-
-// SearchWithFilters implements interfaces.FilteredWebSearchProvider; the
-// plugin must fail rather than ignore a filter it cannot honour.
-func (r *remoteSearch) SearchWithFilters(
- ctx context.Context, query string, maxResults int, includeDate bool, filters types.WebSearchFilters,
-) ([]*types.WebSearchResult, error) {
- ctx, cancel := withDefaultTimeout(ctx, searchTimeout)
- defer cancel()
- in := pluginapi.SearchInput{
- Query: query, MaxResults: maxResults, IncludeDate: includeDate,
- Region: filters.Country, Freshness: filters.Freshness,
- }
- var out pluginapi.SearchOutput
- if err := r.iv.Call(ctx, r.m, pluginapi.SearchPath(r.local), r.instance, in, &out); err != nil {
- return nil, err
- }
- results := make([]*types.WebSearchResult, 0, len(out.Results))
- for _, h := range out.Results {
- results = append(results, &types.WebSearchResult{
- Title: h.Title, URL: h.URL, Snippet: h.Snippet, Content: h.Content,
- Source: r.typeID, Age: h.Age, PublishedAt: h.PublishedAt,
- })
- }
- return results, nil
-}
diff --git a/internal/plugin/activate/websearch_test.go b/internal/plugin/activate/websearch_test.go
deleted file mode 100644
index 5cb187d80..000000000
--- a/internal/plugin/activate/websearch_test.go
+++ /dev/null
@@ -1,159 +0,0 @@
-package activate
-
-import (
- "context"
- "fmt"
- "net/http/httptest"
- "strings"
- "testing"
-
- infra_web_search "github.com/Tencent/WeKnora/internal/infrastructure/web_search"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/pkg"
- "github.com/Tencent/WeKnora/internal/plugin/plugintest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-const searchPluginManifest = `schemaVersion: 1
-id: acme.search
-version: 1.0.0
-apiVersion: weknora.plugin/v1
-name: { en-US: ACME Search }
-publisher: { id: acme }
-runtime: { type: host, kind: binary, entry: bin/search }
-contributes:
- webSearch:
- - id: brave
- name: { en-US: ACME Brave, zh-CN: ACME 勇敢搜索 }
- icon: icons/brave.svg
- instanceSchema: schemas/brave.yaml
-`
-
-// fakeClients serves one plugin from an in-process SDK handler.
-type fakeClients struct{ c *client.Client }
-
-func (f fakeClients) Client(context.Context, *manifest.Manifest) (*client.Client, error) {
- return f.c, nil
-}
-
-// OnThisNode is true unless the manifest's runtime is remote, like the
-// embedded host.
-func (f fakeClients) OnThisNode(id string) bool { return id != "acme.remote" }
-
-func searchLoaded(t *testing.T, schema string) *reconcile.Loaded {
- t.Helper()
- p, err := pkg.Open(plugintest.Zip(t, map[string]string{
- "plugin.yaml": searchPluginManifest,
- "bin/search": "x",
- "icons/brave.svg": `<svg xmlns="http://www.w3.org/2000/svg"/>`,
- "schemas/brave.yaml": schema,
- }))
- if err != nil {
- t.Fatal(err)
- }
- return &reconcile.Loaded{Manifest: p.Manifest, Package: p}
-}
-
-const braveSchema = `type: object
-required: [api_key, market]
-properties:
- api_key: { type: string, title: API key }
- market: { type: string, enum: [en-US, zh-CN], default: en-US }
-`
-
-func TestPluginWebSearchProvider(t *testing.T) {
- ctx := context.Background()
- plugin := pluginsdk.New(pluginsdk.Info{ID: "acme.search", Version: "1.0.0"})
- plugin.WebSearch(
- "brave",
- pluginsdk.WebSearchFunc(
- func(_ context.Context, call *pluginsdk.Call, in pluginapi.SearchInput) (*pluginapi.SearchOutput, error) {
- if call.Config.Instance["api_key"] != "k-1" || call.Config.Instance["market"] != "zh-CN" {
- return nil, pluginapi.Errorf(
- pluginapi.CodeUnauthorized,
- "bad instance config %v",
- call.Config.Instance,
- )
- }
- if in.Region != "" && in.Region != "CN" {
- return nil, pluginapi.Errorf(pluginapi.CodeInvalidConfig, "region %s unsupported", in.Region)
- }
- return &pluginapi.SearchOutput{Results: []pluginapi.SearchResult{{
- Title: in.Query, URL: fmt.Sprint("https://acme.example/?tenant=", call.TenantID),
- }}}, nil
- },
- ),
- )
- srv := httptest.NewServer(plugin.Handler())
- defer srv.Close()
-
- registry := infra_web_search.NewRegistry()
- a := NewWebSearch(NewInvoker(fakeClients{client.New(srv.URL, nil, nil)}), registry)
- if err := a.Activate(ctx, searchLoaded(t, braveSchema)); err != nil {
- t.Fatalf("Activate: %v", err)
- }
-
- pt, ok := registry.PluginType("acme.search/brave")
- if !ok {
- t.Fatal("type not registered")
- }
- info := pt.Info
- if info.Names["zh-CN"] != "ACME 勇敢搜索" || !info.RequiresAPIKey ||
- !strings.HasPrefix(info.Icon, "data:image/svg+xml;base64,") ||
- info.PluginID != "acme.search" {
- t.Fatalf("info = %+v", info)
- }
- if ak := info.ConfigSchema.Properties["api_key"]; ak == nil || !ak.Secret || ak.Group != "credentials" {
- t.Fatalf("api_key schema = %+v", ak)
- }
- if ec := info.ConfigSchema.Properties["extra_config"]; ec == nil || ec.Properties["market"] == nil {
- t.Fatalf("extra_config schema = %+v", ec)
- }
- if err := pt.Validate(types.WebSearchProviderParameters{APIKey: "k"}); err == nil {
- t.Fatal("a missing required extra_config field must fail validation")
- }
- params := types.WebSearchProviderParameters{APIKey: "k-1", ExtraConfig: map[string]string{"market": "zh-CN"}}
- if err := pt.Validate(params); err != nil {
- t.Fatalf("valid params rejected: %v", err)
- }
-
- provider, err := registry.CreateProvider("acme.search/brave", params)
- if err != nil {
- t.Fatal(err)
- }
- tctx := context.WithValue(ctx, types.TenantIDContextKey, uint64(7))
- res, err := provider.Search(tctx, "weknora", 3, false)
- if err != nil || len(res) != 1 || res[0].Title != "weknora" || res[0].URL != "https://acme.example/?tenant=7" ||
- res[0].Source != "acme.search/brave" {
- t.Fatalf("search = %+v, %v", res, err)
- }
- filtered := provider.(interfaces.FilteredWebSearchProvider)
- if _, err := filtered.SearchWithFilters(tctx, "q", 3, false, types.WebSearchFilters{Country: "DE"}); err == nil ||
- !strings.Contains(err.Error(), "region DE unsupported") {
- t.Fatalf("an unsupported filter must fail, got %v", err)
- }
-
- if err := a.Deactivate(ctx, "acme.search"); err != nil {
- t.Fatal(err)
- }
- if _, ok := registry.PluginType("acme.search/brave"); ok {
- t.Fatal("type survived Deactivate")
- }
-}
-
-func TestPluginWebSearchSchemaRules(t *testing.T) {
- a := NewWebSearch(NewInvoker(fakeClients{}), infra_web_search.NewRegistry())
- for name, schema := range map[string]string{
- "secret field": "type: object\nproperties: { token: { type: string, x-secret: true } }\n",
- "number field": "type: object\nproperties: { limit: { type: integer } }\n",
- } {
- if err := a.Activate(context.Background(), searchLoaded(t, schema)); err == nil {
- t.Errorf("%s: want an activation error", name)
- }
- }
-}
diff --git a/internal/plugin/builtin/builtin.go b/internal/plugin/builtin/builtin.go
deleted file mode 100644
index 446e910a8..000000000
--- a/internal/plugin/builtin/builtin.go
+++ /dev/null
@@ -1,232 +0,0 @@
-// Package builtin describes the capabilities compiled into WeKnora as builtin
-// plugins, so the plugin registry lists them next to third-party ones.
-//
-// Contributions are grouped into one plugin per vendor: Feishu's wiki and
-// drive connectors and its IM channel all belong to weknora.feishu, which is
-// the granularity a tenant enables or disables and the granularity a
-// third-party plugin ships at.
-package builtin
-
-import (
- "sort"
- "strings"
-
- "github.com/Tencent/WeKnora/internal/agent/tools"
- "github.com/Tencent/WeKnora/internal/datasource"
- "github.com/Tencent/WeKnora/internal/im"
- "github.com/Tencent/WeKnora/internal/models/providers"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-// Sources is what builtin plugins are described from: the metadata each
-// domain already keeps, limited to what is actually registered in this
-// process.
-type Sources struct {
- // Version is the WeKnora build version; builtins share it.
- Version string
- ModelVendors []*providers.Definition
- Connectors []datasource.ConnectorMetadata
- IMPlatforms []im.PlatformInfo
- WebSearch []types.WebSearchProviderTypeInfo
- Tools []tools.AvailableTool
- Parsers []Parser
-}
-
-// Parser is the metadata of one locally registered parser engine.
-type Parser struct {
- Name string
- Description string
-}
-
-// Plugin IDs that group contributions without a vendor of their own.
-const (
- agentToolsKey = "agent-tools"
- docParserKey = "docparser"
-)
-
-// vendorOverrides maps a contribution ID to its vendor where the two differ.
-var vendorOverrides = map[manifest.Point]map[string]string{
- manifest.PointConnectors: {
- types.ConnectorTypeLark: "feishu",
- types.ConnectorTypeFeishuDrive: "feishu",
- types.ConnectorTypeLarkDrive: "feishu",
- },
- manifest.PointIMChannels: {"lark": "feishu"},
- manifest.PointParsers: {
- "builtin": docParserKey,
- "simple": docParserKey,
- "anydoc": docParserKey,
- "mineru_cloud": "mineru",
- "paddleocr_vl": "paddleocr",
- "paddleocr_vl_cloud": "paddleocr",
- },
-}
-
-// vendorNames names plugins that group several contributions. A vendor absent
-// here takes the name of its first contribution, model vendors first.
-var vendorNames = map[string]manifest.LocalizedText{
- "feishu": manifest.Text("Feishu / Lark", map[string]string{"zh-CN": "飞书 / Lark"}),
- "dingtalk": manifest.Text("DingTalk", map[string]string{"zh-CN": "钉钉"}),
- "mineru": manifest.Text("MinerU", nil),
- "paddleocr": manifest.Text("PaddleOCR-VL", nil),
- docParserKey: manifest.Text("Document parsing", map[string]string{"zh-CN": "文档解析"}),
- agentToolsKey: manifest.Text("Agent tools", map[string]string{"zh-CN": "Agent 内置工具"}),
-}
-
-// requiredVendors are builtins a tenant may not disable: without them agents
-// and document import stop working altogether.
-var requiredVendors = map[string]bool{agentToolsKey: true, docParserKey: true}
-
-// Manifests describes every builtin capability in src as builtin plugins,
-// sorted by plugin ID.
-func Manifests(src Sources) []*manifest.Manifest {
- b := &builder{version: normalizeVersion(src.Version), plugins: make(map[string]*manifest.Manifest)}
- // Model vendors go first so a vendor that also offers web search (Zhipu)
- // is named after its model brand.
- for _, d := range src.ModelVendors {
- b.add(manifest.PointModelVendors, d.ID, manifest.Contribution{
- ID: d.ID,
- Name: manifest.Text(d.Name, d.Names),
- Description: manifest.Text(d.Description, d.Descriptions),
- Order: d.Order,
- Extra: map[string]any{"modelTypes": d.ModelTypes},
- })
- }
- for _, c := range src.Connectors {
- b.add(manifest.PointConnectors, c.Type, manifest.Contribution{
- ID: c.Type,
- Name: manifest.Text(c.Name, nil),
- Description: manifest.Text(c.Description, nil),
- Icon: c.Icon,
- Capabilities: c.Capabilities,
- Order: c.Priority,
- Extra: map[string]any{"authType": c.AuthType},
- })
- }
- for _, p := range src.IMPlatforms {
- b.add(manifest.PointIMChannels, p.ID, manifest.Contribution{
- ID: p.ID,
- Name: manifest.Text(p.Name, p.Names),
- Order: p.Order,
- })
- }
- for i, w := range src.WebSearch {
- b.add(manifest.PointWebSearch, w.ID, manifest.Contribution{
- ID: w.ID,
- Name: manifest.Text(w.Name, nil),
- Description: manifest.Text(w.Description, nil),
- Order: i,
- })
- }
- for i, t := range src.Tools {
- b.addTo(agentToolsKey, manifest.PointTools, t.Name, manifest.Contribution{
- ID: t.Name,
- Name: manifest.Text(t.Label, nil),
- Description: manifest.Text(t.Description, nil),
- Order: i,
- })
- }
- for i, p := range src.Parsers {
- b.add(manifest.PointParsers, p.Name, manifest.Contribution{
- ID: p.Name,
- Name: manifest.Text(p.Name, nil),
- Description: manifest.Text(p.Description, nil),
- Order: i,
- })
- }
- return b.result()
-}
-
-// Register adds every builtin in src to r.
-func Register(r *registry.Registry, src Sources) error {
- for _, m := range Manifests(src) {
- if err := r.Register(m); err != nil {
- return err
- }
- }
- return nil
-}
-
-type builder struct {
- version string
- plugins map[string]*manifest.Manifest
-}
-
-func (b *builder) add(point manifest.Point, id string, c manifest.Contribution) {
- vendor := id
- if v, ok := vendorOverrides[point][id]; ok {
- vendor = v
- }
- b.addTo(vendor, point, id, c)
-}
-
-// addTo files a contribution under a vendor's plugin. The contribution keeps
-// its bare ID as alias, since that is what existing rows store.
-func (b *builder) addTo(vendor string, point manifest.Point, alias string, c manifest.Contribution) {
- c.Aliases = []string{alias}
- id := pluginID(vendor)
- m, ok := b.plugins[id]
- if !ok {
- name, named := vendorNames[vendor]
- if !named {
- name = c.Name
- }
- m = &manifest.Manifest{
- SchemaVersion: manifest.SchemaVersion,
- ID: id,
- Version: b.version,
- Name: name,
- Publisher: manifest.Publisher{ID: manifest.BuiltinPublisher, Name: "WeKnora"},
- Builtin: true,
- Required: requiredVendors[vendor],
- Runtime: manifest.Runtime{Type: manifest.RuntimeBuiltin},
- Contributes: manifest.Contributions{},
- }
- b.plugins[id] = m
- }
- m.Contributes[point] = append(m.Contributes[point], c)
-}
-
-func (b *builder) result() []*manifest.Manifest {
- out := make([]*manifest.Manifest, 0, len(b.plugins))
- for _, m := range b.plugins {
- // A plugin with a single contribution borrows its description.
- if all := allContributions(m); len(all) == 1 {
- m.Description = all[0].Description
- }
- out = append(out, m)
- }
- sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
- return out
-}
-
-func allContributions(m *manifest.Manifest) []manifest.Contribution {
- var out []manifest.Contribution
- for _, list := range m.Contributes {
- out = append(out, list...)
- }
- return out
-}
-
-// devVersion stands in when the build carries no usable version (local
-// builds report "unknown").
-const devVersion = "0.0.0-dev"
-
-// normalizeVersion turns a build version ("v0.8.2", "0.8.2") into the bare
-// semantic version manifests require.
-func normalizeVersion(v string) string {
- v = strings.TrimPrefix(strings.TrimSpace(v), "v")
- probe := &manifest.Manifest{Version: v}
- if !probe.HasValidVersion() {
- return devVersion
- }
- return v
-}
-
-// pluginID turns a vendor key into a builtin plugin ID. Plugin IDs allow no
-// underscores, so azure_openai becomes weknora.azure-openai.
-func pluginID(vendor string) string {
- return manifest.BuiltinPublisher + "." + strings.ReplaceAll(vendor, "_", "-")
-}
diff --git a/internal/plugin/builtin/builtin_test.go b/internal/plugin/builtin/builtin_test.go
deleted file mode 100644
index 092f0819c..000000000
--- a/internal/plugin/builtin/builtin_test.go
+++ /dev/null
@@ -1,150 +0,0 @@
-package builtin
-
-import (
- "strings"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/agent/tools"
- "github.com/Tencent/WeKnora/internal/datasource"
- "github.com/Tencent/WeKnora/internal/im"
- "github.com/Tencent/WeKnora/internal/infrastructure/web_search"
- "github.com/Tencent/WeKnora/internal/models/providers"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-func sampleSources() Sources {
- return Sources{
- Version: "v0.8.2",
- ModelVendors: []*providers.Definition{
- {ID: "zhipu", Name: "Zhipu AI", Names: map[string]string{"zh-CN": "智谱"}},
- {ID: "azure_openai", Name: "Azure OpenAI"},
- },
- Connectors: []datasource.ConnectorMetadata{
- {Type: types.ConnectorTypeFeishu, Name: "Feishu (飞书)", Capabilities: []string{"incremental"}},
- {Type: types.ConnectorTypeLarkDrive, Name: "Lark Drive"},
- {Type: types.ConnectorTypeNotion, Name: "Notion", Priority: 1},
- },
- IMPlatforms: []im.PlatformInfo{im.LookupPlatformInfo("feishu"), im.LookupPlatformInfo("lark")},
- WebSearch: []types.WebSearchProviderTypeInfo{{ID: "zhipu", Name: "Zhipu Search"}, {ID: "bing", Name: "Bing"}},
- Tools: []tools.AvailableTool{{Name: "search_knowledge", Label: "检索知识库"}},
- Parsers: []Parser{{Name: "builtin"}, {Name: "simple"}, {Name: "mineru"}, {Name: "mineru_cloud"}},
- }
-}
-
-func TestManifestsGroupByVendor(t *testing.T) {
- byID := map[string]*manifest.Manifest{}
- for _, m := range Manifests(sampleSources()) {
- byID[m.ID] = m
- }
- want := []string{
- "weknora.zhipu", "weknora.azure-openai", "weknora.feishu", "weknora.notion",
- "weknora.bing", "weknora.agent-tools", "weknora.docparser", "weknora.mineru",
- }
- if len(byID) != len(want) {
- t.Fatalf("got %d plugins, want %d: %v", len(byID), len(want), byID)
- }
- for _, id := range want {
- if byID[id] == nil {
- t.Fatalf("missing plugin %s", id)
- }
- }
-
- feishu := byID["weknora.feishu"]
- if n := len(feishu.Contributes[manifest.PointConnectors]); n != 2 {
- t.Fatalf("feishu connectors = %d, want 2 (wiki + lark drive)", n)
- }
- if n := len(feishu.Contributes[manifest.PointIMChannels]); n != 2 {
- t.Fatalf("feishu IM channels = %d, want 2 (feishu + lark)", n)
- }
- if got := feishu.Name.Resolve("zh-CN"); got != "飞书 / Lark" {
- t.Fatalf("feishu name = %q", got)
- }
-
- zhipu := byID["weknora.zhipu"]
- if zhipu.Name.Resolve("zh-CN") != "智谱" {
- t.Fatalf("a vendor with a model brand is named after it, got %+v", zhipu.Name)
- }
- if len(zhipu.Contributes[manifest.PointWebSearch]) != 1 {
- t.Fatal("zhipu web search should join the zhipu plugin")
- }
-
- if !byID["weknora.agent-tools"].Required || !byID["weknora.docparser"].Required {
- t.Fatal("agent tools and the default parser must be required")
- }
- if byID["weknora.notion"].Required {
- t.Fatal("ordinary vendors must not be required")
- }
- if byID["weknora.bing"].Version != "0.8.2" {
- t.Fatalf("version = %q", byID["weknora.bing"].Version)
- }
-}
-
-func TestRegisterResolvesLegacyIDs(t *testing.T) {
- r := registry.New()
- if err := Register(r, sampleSources()); err != nil {
- t.Fatalf("Register: %v", err)
- }
- cases := []struct {
- point manifest.Point
- alias string
- want string
- }{
- {manifest.PointConnectors, "lark_drive", "weknora.feishu/lark_drive"},
- {manifest.PointIMChannels, "lark", "weknora.feishu/lark"},
- {manifest.PointModelVendors, "azure_openai", "weknora.azure-openai/azure_openai"},
- {manifest.PointParsers, "mineru_cloud", "weknora.mineru/mineru_cloud"},
- {manifest.PointTools, "search_knowledge", "weknora.agent-tools/search_knowledge"},
- }
- for _, c := range cases {
- e, ok := r.Resolve(c.point, c.alias)
- if !ok || e.QualifiedID != c.want {
- t.Errorf("Resolve(%s, %s) = %q, %v; want %q", c.point, c.alias, e.QualifiedID, ok, c.want)
- }
- }
-}
-
-func TestNormalizeVersion(t *testing.T) {
- for in, want := range map[string]string{
- "v0.8.2": "0.8.2", "0.8.2": "0.8.2", "unknown": devVersion, "": devVersion, "0.8": devVersion,
- } {
- if got := normalizeVersion(in); got != want {
- t.Errorf("normalizeVersion(%q) = %q, want %q", in, got, want)
- }
- }
-}
-
-func TestCollectSourcesKeepsOnlyRegistered(t *testing.T) {
- ws := web_search.NewRegistry()
- ws.Register("bing", nil)
- ws.Register("custom-search", nil)
- connectors := datasource.NewConnectorRegistry()
-
- src := CollectSources(Live{
- Version: "0.8.2",
- Connectors: connectors,
- WebSearch: ws,
- IMPlatforms: []string{"slack", "brand-new"},
- })
-
- var webSearch []string
- for _, w := range src.WebSearch {
- webSearch = append(webSearch, w.ID)
- }
- if strings.Join(webSearch, ",") != "bing,custom-search" {
- t.Fatalf("web search = %v, want the registered ones only, unknown last", webSearch)
- }
- if len(src.Connectors) != 0 {
- t.Fatalf("no connector is registered, got %v", src.Connectors)
- }
- if len(src.IMPlatforms) != 2 || src.IMPlatforms[1].Name != "brand-new" {
- t.Fatalf("IM platforms = %+v", src.IMPlatforms)
- }
- if len(src.Parsers) == 0 || len(src.Tools) == 0 || len(src.ModelVendors) == 0 {
- t.Fatal("process-wide registries (parsers, tools, model vendors) should be collected")
- }
- if _, err := NewRegistry(Live{Version: "0.8.2", WebSearch: ws}); err != nil {
- t.Fatalf("NewRegistry: %v", err)
- }
-}
diff --git a/internal/plugin/builtin/i18n_keys_test.go b/internal/plugin/builtin/i18n_keys_test.go
deleted file mode 100644
index 8b744cc43..000000000
--- a/internal/plugin/builtin/i18n_keys_test.go
+++ /dev/null
@@ -1,98 +0,0 @@
-package builtin
-
-import (
- "os"
- "path/filepath"
- "regexp"
- "slices"
- "sort"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/datasource"
- "github.com/Tencent/WeKnora/internal/im"
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-// backendSchemaKeysFile lists the frontend locale keys the backend names:
-// x-i18n-keys in builtin config schemas, IM platform link titles and mode
-// hints. The frontend i18n audit treats them as used, so pruning
-// never drops a label only the backend references.
-const backendSchemaKeysFile = "../../../frontend/src/i18n/backendSchemaKeys.ts"
-
-func collectI18nKeys(s *configschema.Schema, into map[string]bool) {
- if s == nil {
- return
- }
- for _, key := range s.I18nKeys {
- into[key] = true
- }
- for _, p := range s.Properties {
- collectI18nKeys(p, into)
- }
- for _, c := range s.OneOf {
- collectI18nKeys(c, into)
- }
- collectI18nKeys(s.Items, into)
-}
-
-func TestBackendSchemaKeysAreListedForTheFrontend(t *testing.T) {
- used := map[string]bool{}
- for _, info := range types.GetWebSearchProviderTypes() {
- collectI18nKeys(info.ConfigSchema, used)
- }
- for _, meta := range datasource.ListAvailableConnectors() {
- collectI18nKeys(meta.ConfigSchema, used)
- }
- for _, info := range im.KnownPlatformInfos() {
- collectI18nKeys(info.ConfigSchema, used)
- for _, link := range info.Links {
- if link.TitleKey != "" {
- used[link.TitleKey] = true
- }
- }
- if info.ModeHintKey != "" {
- used[info.ModeHintKey] = true
- }
- }
-
- data, err := os.ReadFile(filepath.FromSlash(backendSchemaKeysFile))
- if err != nil {
- t.Fatalf("read %s: %v", backendSchemaKeysFile, err)
- }
- listed := map[string]bool{}
- for _, m := range regexp.MustCompile(`'([A-Za-z0-9_.]+)'`).FindAllStringSubmatch(string(data), -1) {
- listed[m[1]] = true
- }
-
- var missing, stale []string
- for k := range used {
- if !listed[k] {
- missing = append(missing, k)
- }
- }
- for k := range listed {
- if !used[k] {
- stale = append(stale, k)
- }
- }
- sort.Strings(missing)
- sort.Strings(stale)
- if len(missing) > 0 {
- t.Errorf("add to %s: %v", backendSchemaKeysFile, missing)
- }
- if len(stale) > 0 {
- t.Errorf("no builtin schema uses these any more, drop them from %s: %v", backendSchemaKeysFile, stale)
- }
- if !slices.IsSorted(keysInFileOrder(string(data))) {
- t.Errorf("keep %s sorted", backendSchemaKeysFile)
- }
-}
-
-func keysInFileOrder(src string) []string {
- var out []string
- for _, m := range regexp.MustCompile(`'([A-Za-z0-9_.]+)'`).FindAllStringSubmatch(src, -1) {
- out = append(out, m[1])
- }
- return out
-}
diff --git a/internal/plugin/builtin/implementations.go b/internal/plugin/builtin/implementations.go
deleted file mode 100644
index 8eace0df3..000000000
--- a/internal/plugin/builtin/implementations.go
+++ /dev/null
@@ -1,103 +0,0 @@
-package builtin
-
-import (
- "errors"
- "fmt"
-
- "github.com/Tencent/WeKnora/internal/datasource"
- confluenceConnector "github.com/Tencent/WeKnora/internal/datasource/connector/confluence"
- dingtalkConnector "github.com/Tencent/WeKnora/internal/datasource/connector/dingtalk"
- "github.com/Tencent/WeKnora/internal/datasource/connector/feishu/core"
- "github.com/Tencent/WeKnora/internal/datasource/connector/feishu/drive"
- "github.com/Tencent/WeKnora/internal/datasource/connector/feishu/wiki"
- gitlabConnector "github.com/Tencent/WeKnora/internal/datasource/connector/gitlab"
- imaConnector "github.com/Tencent/WeKnora/internal/datasource/connector/ima"
- notionConnector "github.com/Tencent/WeKnora/internal/datasource/connector/notion"
- rssConnector "github.com/Tencent/WeKnora/internal/datasource/connector/rss"
- yuqueConnector "github.com/Tencent/WeKnora/internal/datasource/connector/yuque"
- "github.com/Tencent/WeKnora/internal/im"
- imDingtalk "github.com/Tencent/WeKnora/internal/im/dingtalk"
- imFeishu "github.com/Tencent/WeKnora/internal/im/feishu"
- imMattermost "github.com/Tencent/WeKnora/internal/im/mattermost"
- imQQBot "github.com/Tencent/WeKnora/internal/im/qqbot"
- imSlack "github.com/Tencent/WeKnora/internal/im/slack"
- imTelegram "github.com/Tencent/WeKnora/internal/im/telegram"
- imWechat "github.com/Tencent/WeKnora/internal/im/wechat"
- imWecom "github.com/Tencent/WeKnora/internal/im/wecom"
- imYunzhijia "github.com/Tencent/WeKnora/internal/im/yunzhijia"
- "github.com/Tencent/WeKnora/internal/infrastructure/web_search"
-)
-
-// This file registers the implementations behind the builtin plugins with
-// the domain registries, so every builtin capability is declared in this
-// package: implementations here, descriptions in builtin.go.
-
-// NewConnectorRegistry returns the data source registry holding every builtin
-// connector.
-func NewConnectorRegistry() (*datasource.ConnectorRegistry, error) {
- registry := datasource.NewConnectorRegistry()
- connectors := []datasource.Connector{
- wiki.NewConnector(core.RegionFeishu),
- // Lark is Feishu's international cloud: same connector, other host.
- wiki.NewConnector(core.RegionLark),
- // Drive (云盘) mode is its own type so the registry dispatches to the
- // Drive connector; it shares the Feishu client and export logic.
- drive.NewDriveConnector(core.RegionFeishuDrive),
- drive.NewDriveConnector(core.RegionLarkDrive),
- notionConnector.NewConnector(),
- confluenceConnector.NewConnector(),
- yuqueConnector.NewConnector(),
- dingtalkConnector.NewConnector(),
- imaConnector.NewConnector(),
- rssConnector.NewConnector(),
- gitlabConnector.NewConnector(),
- }
- var errs error
- for _, c := range connectors {
- if err := registry.Register(c); err != nil {
- errs = errors.Join(errs, fmt.Errorf("register %s connector: %w", c.Type(), err))
- }
- }
- if errs != nil {
- return nil, errs
- }
- return registry, nil
-}
-
-// RegisterWebSearchProviders registers every builtin web search provider.
-func RegisterWebSearchProviders(registry *web_search.Registry) {
- for id, factory := range map[string]web_search.ProviderFactory{
- "duckduckgo": web_search.NewDuckDuckGoProvider,
- "google": web_search.NewGoogleProvider,
- "bing": web_search.NewBingProvider,
- "tavily": web_search.NewTavilyProvider,
- "ollama": web_search.NewOllamaProvider,
- "baidu": web_search.NewBaiduProvider,
- "searxng": web_search.NewSearxngProvider,
- "keenable": web_search.NewKeenableProvider,
- "zhipu": web_search.NewZhipuProvider,
- "exa": web_search.NewExaProvider,
- "metaso": web_search.NewMetasoProvider,
- "bocha": web_search.NewBochaProvider,
- "brave": web_search.NewBraveProvider,
- "serply": web_search.NewSerplyProvider,
- } {
- registry.Register(id, factory)
- }
-}
-
-// RegisterIMAdapters registers every builtin IM platform adapter. Each
-// platform's factory lives in its own subpackage.
-func RegisterIMAdapters(s *im.Service) {
- s.RegisterAdapterFactory("wecom", imWecom.NewFactory())
- s.RegisterAdapterFactory("feishu", imFeishu.NewFactory(imFeishu.RegionFeishu))
- // Lark is Feishu's international cloud: same adapter, other host/tenant.
- s.RegisterAdapterFactory("lark", imFeishu.NewFactory(imFeishu.RegionLark))
- s.RegisterAdapterFactory("slack", imSlack.NewFactory())
- s.RegisterAdapterFactory("telegram", imTelegram.NewFactory())
- s.RegisterAdapterFactory("dingtalk", imDingtalk.NewFactory())
- s.RegisterAdapterFactory("mattermost", imMattermost.NewFactory())
- s.RegisterAdapterFactory("wechat", imWechat.NewFactory())
- s.RegisterAdapterFactory("qqbot", imQQBot.NewFactory())
- s.RegisterAdapterFactory("yunzhijia", imYunzhijia.NewFactory())
-}
diff --git a/internal/plugin/builtin/live.go b/internal/plugin/builtin/live.go
deleted file mode 100644
index a9f3fb58b..000000000
--- a/internal/plugin/builtin/live.go
+++ /dev/null
@@ -1,83 +0,0 @@
-package builtin
-
-import (
- "sort"
-
- "github.com/Tencent/WeKnora/internal/agent/tools"
- "github.com/Tencent/WeKnora/internal/datasource"
- "github.com/Tencent/WeKnora/internal/im"
- "github.com/Tencent/WeKnora/internal/infrastructure/docparser"
- "github.com/Tencent/WeKnora/internal/infrastructure/web_search"
- modelruntime "github.com/Tencent/WeKnora/internal/models/runtime"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-// Live is what this process has actually registered. Metadata tables list
-// some connectors and providers that are not wired up yet (GitHub, Google
-// Drive); only registered ones become contributions.
-type Live struct {
- Version string
- Connectors *datasource.ConnectorRegistry
- WebSearch *web_search.Registry
- IMPlatforms []string
-}
-
-// CollectSources gathers builtin metadata for what live has registered.
-func CollectSources(live Live) Sources {
- src := Sources{
- Version: live.Version,
- Tools: tools.AvailableToolDefinitions(),
- }
- for _, p := range modelruntime.List() {
- src.ModelVendors = append(src.ModelVendors, p.Definition)
- }
- if live.Connectors != nil {
- src.Connectors = live.Connectors.Metadata()
- }
- for _, p := range live.IMPlatforms {
- src.IMPlatforms = append(src.IMPlatforms, im.LookupPlatformInfo(p))
- }
- if live.WebSearch != nil {
- registered := toSet(live.WebSearch.Types())
- for _, info := range types.GetWebSearchProviderTypes() {
- if registered[info.ID] {
- src.WebSearch = append(src.WebSearch, info)
- delete(registered, info.ID)
- }
- }
- for _, id := range sortedKeys(registered) {
- src.WebSearch = append(src.WebSearch, types.WebSearchProviderTypeInfo{ID: id, Name: id})
- }
- }
- for _, e := range docparser.Engines() {
- src.Parsers = append(src.Parsers, Parser{Name: e.Name(), Description: e.Description()})
- }
- return src
-}
-
-// NewRegistry builds a plugin registry holding every builtin in live.
-func NewRegistry(live Live) (*registry.Registry, error) {
- r := registry.New()
- if err := Register(r, CollectSources(live)); err != nil {
- return nil, err
- }
- return r, nil
-}
-
-func toSet(values []string) map[string]bool {
- out := make(map[string]bool, len(values))
- for _, v := range values {
- out[v] = true
- }
- return out
-}
-
-func sortedKeys(m map[string]bool) []string {
- out := make([]string, 0, len(m))
- for k := range m {
- out = append(out, k)
- }
- sort.Strings(out)
- return out
-}
diff --git a/internal/plugin/configschema/configschema_test.go b/internal/plugin/configschema/configschema_test.go
deleted file mode 100644
index 0862b84a0..000000000
--- a/internal/plugin/configschema/configschema_test.go
+++ /dev/null
@@ -1,264 +0,0 @@
-package configschema
-
-import (
- "encoding/json"
- "strings"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/utils"
-)
-
-const testAESKey = "0123456789abcdef0123456789abcdef"
-
-func intPtr(v int) *int { return &v }
-func floatPtr(v float64) *float64 { return &v }
-
-// jiraSchema is a connector-shaped schema: a URL, an auth mode, a token that
-// only applies to one mode, and a nested object holding a secret.
-func jiraSchema() *Schema {
- auth := Object().
- Set("user", &Schema{Type: TypeString}, false).
- Set("password", &Schema{Type: TypeString, Secret: true}, false)
- return Object().
- Set("base_url", &Schema{Type: TypeString, Format: "uri", Order: 1}, true).
- Set("mode", &Schema{Type: TypeString, OneOf: []*Schema{
- {Const: "token", Title: "API token"}, {Const: "basic", Title: "Basic"},
- }, Order: 2}, true).
- Set("token", &Schema{
- Type: TypeString, Secret: true, MinLength: intPtr(8),
- VisibleIf: map[string]any{"mode": "token"}, Order: 3,
- }, true).
- Set("page_size", &Schema{Type: TypeInteger, Minimum: floatPtr(1), Maximum: floatPtr(100)}, false).
- Set("auth", auth, false)
-}
-
-func TestCheckRejectsUnsupportedSchemas(t *testing.T) {
- bad := []struct {
- name string
- schema *Schema
- want string
- }{
- {"root not object", &Schema{Type: TypeString}, "root must be an object"},
- {"unknown type", Object().Set("x", &Schema{Type: "date"}, false), `unsupported type "date"`},
- {"secret number", Object().Set("x", &Schema{Type: TypeNumber, Secret: true}, false), "only allowed on strings"},
- {"secret in array", Object().Set("x", &Schema{
- Type: TypeArray, Items: &Schema{Type: TypeString, Secret: true},
- }, false), "secrets inside arrays"},
- {"required ghost", &Schema{Type: TypeObject, Required: []string{"ghost"}}, `"ghost" has no property`},
- {"bad pattern", Object().Set("x", &Schema{Type: TypeString, Pattern: "("}, false), "invalid pattern"},
- {"oneOf without const", Object().Set("x", &Schema{
- Type: TypeString, OneOf: []*Schema{{Title: "A"}},
- }, false), "must carry a const"},
- }
- for _, c := range bad {
- err := c.schema.Check()
- if err == nil || !strings.Contains(err.Error(), c.want) {
- t.Errorf("%s: got %v, want error containing %q", c.name, err, c.want)
- }
- }
- if err := jiraSchema().Check(); err != nil {
- t.Fatalf("valid schema rejected: %v", err)
- }
-}
-
-func TestParseRoundTrip(t *testing.T) {
- data, err := json.Marshal(jiraSchema())
- if err != nil {
- t.Fatal(err)
- }
- for _, key := range []string{`"x-secret":true`, `"x-visible-if":{"mode":"token"}`, `"oneOf"`} {
- if !strings.Contains(string(data), key) {
- t.Fatalf("marshalled schema lacks %s: %s", key, data)
- }
- }
- back, err := Parse(data)
- if err != nil {
- t.Fatalf("Parse: %v", err)
- }
- if got := back.SecretPaths(); strings.Join(got, ",") != "auth.password,token" {
- t.Fatalf("SecretPaths = %v", got)
- }
- if got := back.OrderedKeys(); strings.Join(got, ",") != "auth,page_size,base_url,mode,token" {
- t.Fatalf("OrderedKeys = %v", got)
- }
-}
-
-func TestValidate(t *testing.T) {
- s := jiraSchema()
- ok := map[string]any{
- "base_url": "https://acme.atlassian.net", "mode": "token", "token": "abcdefgh", "page_size": 50.0,
- }
- if errs := Validate(s, ok); errs != nil {
- t.Fatalf("valid config rejected: %v", errs)
- }
-
- // token is required only while mode is token.
- basic := map[string]any{"base_url": "https://acme.atlassian.net", "mode": "basic"}
- if errs := Validate(s, basic); errs != nil {
- t.Fatalf("hidden required field must not be required: %v", errs)
- }
-
- bad := map[string]any{
- "base_url": "not a url",
- "mode": "oauth",
- "token": "short",
- "page_size": 1.5,
- "auth": map[string]any{"user": 42},
- }
- errs := Validate(s, bad)
- got := map[string]string{}
- for _, e := range errs {
- got[e.Path] = e.Code
- }
- want := map[string]string{
- "base_url": CodeFormat, "mode": CodeEnum, "page_size": CodeType, "auth.user": CodeType,
- }
- for path, code := range want {
- if got[path] != code {
- t.Errorf("%s: code %q, want %q (all: %v)", path, got[path], code, errs)
- }
- }
- // mode is invalid, so token is hidden and not checked.
- if _, checked := got["token"]; checked {
- t.Errorf("token should be skipped while mode is not token: %v", errs)
- }
-
- missing := Validate(s, map[string]any{"mode": "token", "token": ""})
- if len(missing) != 2 || missing[0].Code != CodeRequired || missing[1].Code != CodeRequired {
- t.Fatalf("want base_url and token required, got %v", missing)
- }
- if !strings.Contains(missing.Error(), "base_url: is required") {
- t.Fatalf("error text = %q", missing.Error())
- }
-
- // A redacted secret counts as present and skips its own checks.
- if errs := Validate(s, map[string]any{
- "base_url": "https://acme.atlassian.net", "mode": "token", "token": RedactedPlaceholder,
- }); errs != nil {
- t.Fatalf("redacted secret rejected: %v", errs)
- }
-
- if errs := Validate(s, map[string]any{
- "base_url": "https://x.io", "mode": "basic", "page_size": 500.0,
- }); len(errs) != 1 || errs[0].Code != CodeMaximum {
- t.Fatalf("want maximum error, got %v", errs)
- }
-}
-
-func TestSealOpenRedactMerge(t *testing.T) {
- t.Setenv("SYSTEM_AES_KEY", testAESKey)
- s := jiraSchema()
- plain := map[string]any{
- "base_url": "https://acme.atlassian.net",
- "mode": "token",
- "token": "secret-token",
- "auth": map[string]any{"user": "bob", "password": "hunter2"},
- }
-
- sealed, err := Seal(s, plain)
- if err != nil {
- t.Fatal(err)
- }
- if plain["token"] != "secret-token" {
- t.Fatal("Seal must not mutate its input")
- }
- token, _ := sealed["token"].(string)
- password, _ := sealed["auth"].(map[string]any)["password"].(string)
- if !strings.HasPrefix(token, utils.EncPrefix) || !strings.HasPrefix(password, utils.EncPrefix) {
- t.Fatalf("secrets not encrypted: %v", sealed)
- }
- if sealed["base_url"] != plain["base_url"] || sealed["auth"].(map[string]any)["user"] != "bob" {
- t.Fatalf("non-secrets must pass through: %v", sealed)
- }
- // Sealing twice keeps the ciphertext.
- again, err := Seal(s, sealed)
- if err != nil || again["token"] != token {
- t.Fatalf("Seal is not idempotent: %v, %v", again["token"], err)
- }
- // Values sealed here read back through the helper every other table uses.
- if got, err := utils.DecryptStoredSecret(token); err != nil || got != "secret-token" {
- t.Fatalf("DecryptStoredSecret = %q, %v", got, err)
- }
-
- opened, err := Open(s, sealed)
- if err != nil {
- t.Fatal(err)
- }
- if opened["token"] != "secret-token" || opened["auth"].(map[string]any)["password"] != "hunter2" {
- t.Fatalf("Open = %v", opened)
- }
-
- redacted := Redact(s, sealed)
- if redacted["token"] != RedactedPlaceholder ||
- redacted["auth"].(map[string]any)["password"] != RedactedPlaceholder {
- t.Fatalf("Redact = %v", redacted)
- }
- if Redact(s, map[string]any{"token": ""})["token"] != "" {
- t.Fatal("an unset secret must stay empty so the UI can tell it apart")
- }
-
- // The client sends the redacted form back with one field changed.
- incoming := Redact(s, sealed)
- incoming["base_url"] = "https://new.atlassian.net"
- delete(incoming["auth"].(map[string]any), "password")
- merged := Merge(s, sealed, incoming)
- if merged["token"] != token || merged["auth"].(map[string]any)["password"] != password {
- t.Fatalf("Merge must keep stored secrets: %v", merged)
- }
- if merged["base_url"] != "https://new.atlassian.net" {
- t.Fatalf("Merge must apply changed fields: %v", merged)
- }
-
- replaced := Merge(s, sealed, map[string]any{"token": "new-token"})
- if replaced["token"] != "new-token" {
- t.Fatalf("a new secret must replace the stored one: %v", replaced)
- }
- if replaced["auth"].(map[string]any)["password"] != password {
- t.Fatalf("a nested secret the update leaves out must survive: %v", replaced)
- }
-}
-
-func TestOpenWithoutKey(t *testing.T) {
- t.Setenv("SYSTEM_AES_KEY", testAESKey)
- s := jiraSchema()
- sealed, err := Seal(s, map[string]any{"token": "secret-token", "mode": "token"})
- if err != nil {
- t.Fatal(err)
- }
-
- t.Setenv("SYSTEM_AES_KEY", "")
- if _, err := Open(s, sealed); err == nil || !strings.Contains(err.Error(), "decrypt token") {
- t.Fatalf("Open without key must fail loudly, got %v", err)
- }
- lenient, failed := OpenLenient(s, sealed)
- if lenient["token"] != "" || len(failed) != 1 || failed[0] != "token" {
- t.Fatalf("OpenLenient = %v, failed %v", lenient, failed)
- }
- if lenient["mode"] != "token" {
- t.Fatal("OpenLenient must keep the rest of the row")
- }
-
- // Legacy plaintext rows still open.
- legacy, err := Open(s, map[string]any{"token": "plain-legacy"})
- if err != nil || legacy["token"] != "plain-legacy" {
- t.Fatalf("legacy plaintext = %v, %v", legacy, err)
- }
-}
-
-func TestValidateInContextReadsDollarKeysFromContext(t *testing.T) {
- s := Object().
- Set("bot_token", &Schema{Type: TypeString}, true).
- Set("signing_secret", &Schema{Type: TypeString, VisibleIf: map[string]any{"$mode": "webhook"}}, true)
- value := map[string]any{"bot_token": "x"}
- if errs := ValidateInContext(s, value, map[string]any{"mode": "websocket"}); errs != nil {
- t.Fatalf("websocket mode hides the webhook secret: %v", errs)
- }
- errs := ValidateInContext(s, value, map[string]any{"mode": "webhook"})
- if len(errs) != 1 || errs[0].Path != "signing_secret" {
- t.Fatalf("webhook mode requires the signing secret, got %v", errs)
- }
- // Without a context the field is hidden, never wrongly required.
- if errs := Validate(s, value); errs != nil {
- t.Fatalf("Validate without context: %v", errs)
- }
-}
diff --git a/internal/plugin/configschema/schema.go b/internal/plugin/configschema/schema.go
deleted file mode 100644
index daa8af24e..000000000
--- a/internal/plugin/configschema/schema.go
+++ /dev/null
@@ -1,310 +0,0 @@
-// Package configschema describes configuration with a subset of JSON Schema
-// (draft 2020-12) plus a few x- keywords for the UI, and handles the secret
-// fields such a schema marks: encrypting them at rest, redacting them in
-// responses and keeping them when an update sends the redaction back.
-//
-// One schema drives both the form the frontend renders and the server-side
-// validation, replacing the per-integration field descriptors (model vendor
-// ExtraField, web search config fields, hardcoded connector and IM forms).
-package configschema
-
-import (
- "encoding/json"
- "errors"
- "fmt"
- "regexp"
- "sort"
- "strings"
-)
-
-// Types a schema node may declare.
-const (
- TypeObject = "object"
- TypeString = "string"
- TypeNumber = "number"
- TypeInteger = "integer"
- TypeBoolean = "boolean"
- TypeArray = "array"
-)
-
-// Schema is one node of a configuration schema. The root is an object whose
-// properties are the fields of a form.
-type Schema struct {
- Type string `json:"type,omitempty"`
- Title string `json:"title,omitempty"`
- Description string `json:"description,omitempty"`
-
- Properties map[string]*Schema `json:"properties,omitempty"`
- Required []string `json:"required,omitempty"`
- Items *Schema `json:"items,omitempty"`
-
- Default any `json:"default,omitempty"`
- Const any `json:"const,omitempty"`
- Enum []any `json:"enum,omitempty"`
- // OneOf lists labelled choices: each entry carries a const and a title.
- OneOf []*Schema `json:"oneOf,omitempty"`
-
- MinLength *int `json:"minLength,omitempty"`
- MaxLength *int `json:"maxLength,omitempty"`
- Pattern string `json:"pattern,omitempty"`
- Minimum *float64 `json:"minimum,omitempty"`
- Maximum *float64 `json:"maximum,omitempty"`
- // Format is "uri" or "email"; other formats are carried but not checked.
- Format string `json:"format,omitempty"`
-
- // Secret marks a string that is encrypted at rest, redacted in
- // responses, and kept when an update sends the redaction back.
- Secret bool `json:"x-secret,omitempty"`
- // Widget picks the form control (password, textarea, select, switch,
- // headers, ...). Empty means the frontend picks from the type; "hidden"
- // keeps a field out of the form while still declaring it (a secret set
- // by another flow, such as WeChat's QR binding).
- Widget string `json:"x-widget,omitempty"`
- Placeholder string `json:"x-placeholder,omitempty"`
- // I18n carries localized title / description / placeholder:
- // {"title": {"zh-CN": "..."}}.
- I18n map[string]map[string]string `json:"x-i18n,omitempty"`
- // I18nKeys names frontend locale keys for title / description /
- // placeholder. Builtins use them; the frontend falls back to I18n and
- // then to the plain text when a key is missing.
- I18nKeys map[string]string `json:"x-i18n-keys,omitempty"`
- // VisibleIf shows the field only while every listed sibling field
- // equals the given value.
- VisibleIf map[string]any `json:"x-visible-if,omitempty"`
- Group string `json:"x-group,omitempty"`
- // Order sorts fields in the form (lower first); ties sort by key.
- Order int `json:"x-order,omitempty"`
- // ModelTypes limits a model vendor field to some model types.
- ModelTypes []string `json:"x-model-types,omitempty"`
- // Options fills a field's choices from the plugin (its
- // /v1/options/{name} endpoint), for choices that depend on the
- // account: projects, spaces, channels.
- Options *OptionsSource `json:"x-options,omitempty"`
- // OAuth makes a string field an authorization: the form shows a
- // connect button, WeKnora runs the OAuth code flow and the field holds
- // a reference WeKnora swaps for a fresh access token at call time.
- OAuth *OAuthSpec `json:"x-oauth,omitempty"`
-}
-
-// OptionsSource names where a field's choices come from.
-type OptionsSource struct {
- // Name is the plugin's options endpoint: /v1/options/{name}.
- Name string `json:"name"`
- // DependsOn lists dotted paths of fields whose values the choices
- // depend on; the form reloads them when those change.
- DependsOn []string `json:"dependsOn,omitempty"`
- // Search asks the plugin with what the user types (large lists).
- Search bool `json:"search,omitempty"`
-}
-
-// OAuthSpec is an OAuth 2.0 authorization code flow.
-type OAuthSpec struct {
- AuthorizeURL string `json:"authorizeUrl"`
- TokenURL string `json:"tokenUrl"`
- Scopes []string `json:"scopes,omitempty"`
- // ClientID and ClientSecret are usually ${system.<key>}: the OAuth app
- // is registered once for the platform.
- ClientID string `json:"clientId"`
- ClientSecret string `json:"clientSecret,omitempty"`
- // PKCE adds a code challenge (S256).
- PKCE bool `json:"pkce,omitempty"`
- // Params are extra authorize parameters (audience, prompt).
- Params map[string]string `json:"params,omitempty"`
-}
-
-// Object returns an empty object schema.
-func Object() *Schema { return &Schema{Type: TypeObject, Properties: map[string]*Schema{}} }
-
-// Set adds a property and returns the parent for chaining. A required
-// property is also listed in Required.
-func (s *Schema) Set(key string, prop *Schema, required bool) *Schema {
- if s.Properties == nil {
- s.Properties = map[string]*Schema{}
- }
- s.Properties[key] = prop
- if required {
- s.Required = append(s.Required, key)
- }
- return s
-}
-
-// OrderedKeys returns property keys sorted by x-order, then by key.
-func (s *Schema) OrderedKeys() []string {
- keys := make([]string, 0, len(s.Properties))
- for k := range s.Properties {
- keys = append(keys, k)
- }
- sort.Slice(keys, func(i, j int) bool {
- oi, oj := s.Properties[keys[i]].Order, s.Properties[keys[j]].Order
- if oi != oj {
- return oi < oj
- }
- return keys[i] < keys[j]
- })
- return keys
-}
-
-// Parse decodes a schema and checks that it stays within the supported
-// subset.
-func Parse(data []byte) (*Schema, error) {
- var s Schema
- if err := json.Unmarshal(data, &s); err != nil {
- return nil, fmt.Errorf("decode config schema: %w", err)
- }
- if err := s.Check(); err != nil {
- return nil, err
- }
- return &s, nil
-}
-
-// Check verifies that a schema uses only what this package understands. The
-// root must be an object, since a configuration is always a set of fields.
-func (s *Schema) Check() error {
- if s.Type != TypeObject {
- return errors.New("config schema root must be an object")
- }
- var errs []error
- s.check("", &errs)
- return errors.Join(errs...)
-}
-
-func (s *Schema) check(path string, errs *[]error) {
- fail := func(format string, args ...any) {
- *errs = append(*errs, fmt.Errorf("%s: %s", displayPath(path), fmt.Sprintf(format, args...)))
- }
- switch s.Type {
- case TypeObject:
- for _, key := range s.Required {
- if _, ok := s.Properties[key]; !ok {
- fail("required field %q has no property", key)
- }
- }
- for _, key := range s.OrderedKeys() {
- s.Properties[key].check(joinPath(path, key), errs)
- }
- case TypeArray:
- if s.Items == nil {
- fail("array needs items")
- } else {
- if s.Items.containsSecret() {
- fail("secrets inside arrays are not supported")
- }
- s.Items.check(path+"[]", errs)
- }
- case TypeString, TypeNumber, TypeInteger, TypeBoolean:
- default:
- fail("unsupported type %q", s.Type)
- }
- if s.Secret && s.Type != TypeString {
- fail("x-secret is only allowed on strings")
- }
- if s.Pattern != "" {
- if _, err := regexp.Compile(s.Pattern); err != nil {
- fail("invalid pattern: %v", err)
- }
- }
- for _, choice := range s.OneOf {
- if choice.Const == nil {
- fail("oneOf entries must carry a const")
- }
- }
- if o := s.Options; o != nil {
- if !optionsNamePattern.MatchString(o.Name) {
- fail("x-options.name must be lowercase letters, digits, '_' or '-'")
- }
- if s.Type != TypeString && (s.Type != TypeArray || s.Items == nil || s.Items.Type != TypeString) {
- fail("x-options is only allowed on strings or arrays of strings")
- }
- }
- if o := s.OAuth; o != nil {
- if s.Type != TypeString {
- fail("x-oauth is only allowed on strings")
- }
- if s.Secret {
- fail("x-oauth fields hold a reference, not a secret; drop x-secret")
- }
- for name, u := range map[string]string{"authorizeUrl": o.AuthorizeURL, "tokenUrl": o.TokenURL} {
- if !strings.HasPrefix(u, "https://") && !strings.HasPrefix(u, "http://localhost") {
- fail("x-oauth.%s must be an https URL", name)
- }
- }
- if o.ClientID == "" {
- fail("x-oauth.clientId is required")
- }
- }
-}
-
-// OAuthRefPrefix starts the value of an x-oauth field: a reference to an
-// OAuth connection WeKnora keeps (its tokens never reach the browser).
-const OAuthRefPrefix = "oauth:"
-
-var oauthRefPattern = regexp.MustCompile(`^oauth:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`)
-
-// IsOAuthRef reports whether a value references an OAuth connection.
-func IsOAuthRef(v string) bool { return oauthRefPattern.MatchString(v) }
-
-// optionsNamePattern is an options endpoint name.
-var optionsNamePattern = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]{0,63}$`)
-
-func (s *Schema) containsSecret() bool {
- if s.Secret {
- return true
- }
- for _, p := range s.Properties {
- if p.containsSecret() {
- return true
- }
- }
- return s.Items != nil && s.Items.containsSecret()
-}
-
-// OAuthFields returns the x-oauth fields by dotted path. A nil schema has
-// none.
-func (s *Schema) OAuthFields() map[string]*OAuthSpec {
- out := map[string]*OAuthSpec{}
- if s == nil {
- return out
- }
- var walk func(n *Schema, path string)
- walk = func(n *Schema, path string) {
- if n.OAuth != nil {
- out[path] = n.OAuth
- }
- for key, p := range n.Properties {
- walk(p, joinPath(path, key))
- }
- }
- walk(s, "")
- return out
-}
-
-// SecretPaths lists the dotted paths of every secret field, sorted.
-func (s *Schema) SecretPaths() []string {
- var out []string
- var walk func(n *Schema, path string)
- walk = func(n *Schema, path string) {
- if n.Secret {
- out = append(out, path)
- }
- for key, p := range n.Properties {
- walk(p, joinPath(path, key))
- }
- }
- walk(s, "")
- sort.Strings(out)
- return out
-}
-
-func joinPath(parent, key string) string {
- if parent == "" {
- return key
- }
- return parent + "." + key
-}
-
-func displayPath(path string) string {
- if path == "" {
- return "(root)"
- }
- return path
-}
diff --git a/internal/plugin/configschema/secrets.go b/internal/plugin/configschema/secrets.go
deleted file mode 100644
index 65c26df10..000000000
--- a/internal/plugin/configschema/secrets.go
+++ /dev/null
@@ -1,189 +0,0 @@
-package configschema
-
-import (
- "fmt"
-
- "github.com/Tencent/WeKnora/internal/utils"
-)
-
-// RedactedPlaceholder replaces a set secret in responses. It equals
-// types.RedactedSecretPlaceholder, which every other integration already
-// sends, so the frontend treats both the same way.
-const RedactedPlaceholder = "***"
-
-// Seal returns a copy of value with every secret encrypted for storage. It
-// uses the same AES-GCM format (enc:v1:) as the existing integration tables,
-// so already-encrypted values pass through unchanged and values sealed here
-// read back through utils.DecryptStoredSecret. Without SYSTEM_AES_KEY secrets
-// stay in plaintext, as they do elsewhere.
-func Seal(s *Schema, value map[string]any) (map[string]any, error) {
- key := utils.GetAESKey()
- return transformSecrets(s, value, func(path, secret string) (string, error) {
- sealed, err := utils.EncryptAESGCM(secret, key)
- if err != nil {
- return "", fmt.Errorf("encrypt %s: %w", path, err)
- }
- return sealed, nil
- })
-}
-
-// Open returns a copy of value with every secret decrypted. It fails when a
-// secret cannot be decrypted (missing or rotated SYSTEM_AES_KEY), which is
-// what a caller about to use the credential wants.
-func Open(s *Schema, value map[string]any) (map[string]any, error) {
- return transformSecrets(s, value, func(path, secret string) (string, error) {
- plain, err := utils.DecryptStoredSecret(secret)
- if err != nil {
- return "", fmt.Errorf("decrypt %s: %w", path, err)
- }
- return plain, nil
- })
-}
-
-// OpenLenient decrypts like Open but blanks secrets that cannot be decrypted
-// and reports their paths, so a listing still loads and the UI can ask for
-// the credential again.
-func OpenLenient(s *Schema, value map[string]any) (map[string]any, []string) {
- var failed []string
- out, _ := transformSecrets(s, value, func(path, secret string) (string, error) {
- plain, ok := utils.DecryptStoredSecretLenient(secret)
- if !ok {
- failed = append(failed, path)
- }
- return plain, nil
- })
- return out, failed
-}
-
-// Redact returns a copy of value with every set secret replaced by
-// RedactedPlaceholder. Unset secrets stay empty so the UI can tell "not
-// configured" from "configured".
-func Redact(s *Schema, value map[string]any) map[string]any {
- out, _ := transformSecrets(s, value, func(_, _ string) (string, error) {
- return RedactedPlaceholder, nil
- })
- return out
-}
-
-// Merge applies an update to a stored configuration. Non-secret fields come
-// from incoming as sent. A secret that incoming leaves out, empties or sends
-// back as RedactedPlaceholder keeps its stored value, since the client never
-// saw it; clearing a secret is an explicit operation elsewhere, as it is for
-// the existing /credentials subresources.
-func Merge(s *Schema, stored, incoming map[string]any) map[string]any {
- out := deepCopyMap(incoming)
- if out == nil {
- out = map[string]any{}
- }
- mergeSecrets(s, stored, out)
- return out
-}
-
-func mergeSecrets(s *Schema, stored, out map[string]any) {
- for key, prop := range s.Properties {
- switch {
- case prop.Secret:
- incoming, _ := out[key].(string)
- if incoming != "" && incoming != RedactedPlaceholder {
- continue
- }
- if old, ok := stored[key]; ok {
- out[key] = old
- } else {
- delete(out, key)
- }
- case prop.Type == TypeObject && prop.containsSecret():
- storedChild, _ := stored[key].(map[string]any)
- outChild, ok := out[key].(map[string]any)
- if !ok {
- if storedChild == nil {
- continue
- }
- outChild = map[string]any{}
- out[key] = outChild
- }
- mergeSecrets(prop, storedChild, outChild)
- }
- }
-}
-
-// transformSecrets copies value and rewrites every non-empty secret string
-// through fn.
-func transformSecrets(
- s *Schema, value map[string]any, fn func(path, secret string) (string, error),
-) (map[string]any, error) {
- out := deepCopyMap(value)
- if out == nil {
- return nil, nil
- }
- if err := walkSecrets(s, out, "", fn); err != nil {
- return nil, err
- }
- return out, nil
-}
-
-func walkSecrets(s *Schema, obj map[string]any, path string, fn func(path, secret string) (string, error)) error {
- for key, prop := range s.Properties {
- v, ok := obj[key]
- if !ok {
- continue
- }
- fieldPath := joinPath(path, key)
- if prop.Secret {
- str, isStr := v.(string)
- if !isStr || str == "" {
- continue
- }
- rewritten, err := fn(fieldPath, str)
- if err != nil {
- return err
- }
- obj[key] = rewritten
- continue
- }
- if child, isObj := v.(map[string]any); isObj && prop.Type == TypeObject {
- if err := walkSecrets(prop, child, fieldPath, fn); err != nil {
- return err
- }
- }
- }
- return nil
-}
-
-func deepCopyMap(m map[string]any) map[string]any {
- if m == nil {
- return nil
- }
- out := make(map[string]any, len(m))
- for k, v := range m {
- out[k] = deepCopyValue(v)
- }
- return out
-}
-
-func deepCopyValue(v any) any {
- switch t := v.(type) {
- case map[string]any:
- return deepCopyMap(t)
- case []any:
- out := make([]any, len(t))
- for i, item := range t {
- out[i] = deepCopyValue(item)
- }
- return out
- default:
- return v
- }
-}
-
-// Update applies a client's edit to a stored (sealed) configuration: it keeps
-// secrets the client sent back redacted, validates the result and seals it
-// for storage. A validation failure is returned as FieldErrors.
-func Update(s *Schema, stored, incoming map[string]any) (map[string]any, error) {
- plain, _ := OpenLenient(s, stored)
- merged := Merge(s, plain, incoming)
- if errs := Validate(s, merged); len(errs) > 0 {
- return nil, errs
- }
- return Seal(s, merged)
-}
diff --git a/internal/plugin/configschema/secrets_test.go b/internal/plugin/configschema/secrets_test.go
deleted file mode 100644
index 5477c122d..000000000
--- a/internal/plugin/configschema/secrets_test.go
+++ /dev/null
@@ -1,28 +0,0 @@
-package configschema
-
-import (
- "errors"
- "testing"
-)
-
-func TestUpdateKeepsRedactedSecretsAndValidates(t *testing.T) {
- s := Object().
- Set("api_key", &Schema{Type: TypeString, Secret: true, MinLength: intPtr(4)}, true).
- Set("region", &Schema{Type: TypeString}, false)
- stored, err := Update(s, nil, map[string]any{"api_key": "secret-1", "region": "eu"})
- if err != nil {
- t.Fatal(err)
- }
- next, err := Update(s, stored, map[string]any{"api_key": RedactedPlaceholder, "region": "us"})
- if err != nil {
- t.Fatalf("a redacted secret must count as set: %v", err)
- }
- opened, err := Open(s, next)
- if err != nil || opened["api_key"] != "secret-1" || opened["region"] != "us" {
- t.Fatalf("opened = %v, %v", opened, err)
- }
- var fe FieldErrors
- if _, err := Update(s, nil, map[string]any{"region": "eu"}); !errors.As(err, &fe) {
- t.Fatalf("a missing required secret must fail validation, got %v", err)
- }
-}
diff --git a/internal/plugin/configschema/validate.go b/internal/plugin/configschema/validate.go
deleted file mode 100644
index cceb38032..000000000
--- a/internal/plugin/configschema/validate.go
+++ /dev/null
@@ -1,251 +0,0 @@
-package configschema
-
-import (
- "encoding/json"
- "fmt"
- "math"
- "net/mail"
- "net/url"
- "reflect"
- "regexp"
- "strings"
- "unicode/utf8"
-)
-
-// Error codes a FieldError carries. The frontend maps them to messages.
-const (
- CodeRequired = "required"
- CodeType = "type"
- CodeEnum = "enum"
- CodeMinLength = "min_length"
- CodeMaxLength = "max_length"
- CodePattern = "pattern"
- CodeMinimum = "minimum"
- CodeMaximum = "maximum"
- CodeFormat = "format"
-)
-
-// FieldError is one problem with one field. Path is dotted ("auth.token")
-// with "[i]" for array items.
-type FieldError struct {
- Path string `json:"path"`
- Code string `json:"code"`
- Message string `json:"message"`
-}
-
-// FieldErrors is every problem Validate found.
-type FieldErrors []FieldError
-
-func (e FieldErrors) Error() string {
- parts := make([]string, len(e))
- for i, fe := range e {
- parts[i] = fe.Path + ": " + fe.Message
- }
- return "invalid configuration: " + strings.Join(parts, "; ")
-}
-
-// Validate checks a configuration against its schema and returns every
-// problem, or nil. Fields the schema does not declare are allowed, so a
-// configuration written by a newer version still validates. A required field
-// hidden by x-visible-if is not required.
-func Validate(s *Schema, value map[string]any) FieldErrors {
- return ValidateInContext(s, value, nil)
-}
-
-// ValidateInContext is Validate for a configuration that lives inside a larger
-// object: x-visible-if keys starting with "$" ("$mode") are read from ctx
-// (without the "$") instead of from sibling fields. An IM channel's
-// credentials depend on the channel's mode this way.
-func ValidateInContext(s *Schema, value, ctx map[string]any) FieldErrors {
- var errs FieldErrors
- v := &validator{ctx: ctx, errs: &errs}
- v.object(s, value, "")
- if len(errs) == 0 {
- return nil
- }
- return errs
-}
-
-// validator carries the context through one validation.
-type validator struct {
- ctx map[string]any
- errs *FieldErrors
-}
-
-func (vd *validator) object(s *Schema, value map[string]any, path string) {
- errs := vd.errs
- required := make(map[string]bool, len(s.Required))
- for _, k := range s.Required {
- required[k] = true
- }
- for _, key := range s.OrderedKeys() {
- prop := s.Properties[key]
- if !visible(prop, value, vd.ctx) {
- continue
- }
- fieldPath := joinPath(path, key)
- val, present := value[key]
- if !present || val == nil || val == "" {
- if required[key] {
- *errs = append(*errs, FieldError{fieldPath, CodeRequired, "is required"})
- }
- continue
- }
- vd.value(prop, val, fieldPath)
- }
-}
-
-// visible evaluates x-visible-if against sibling values, or against ctx for
-// "$"-prefixed keys.
-func visible(s *Schema, siblings, ctx map[string]any) bool {
- for key, want := range s.VisibleIf {
- got := siblings[key]
- if name, fromCtx := strings.CutPrefix(key, "$"); fromCtx {
- got = ctx[name]
- }
- if !looselyEqual(got, want) {
- return false
- }
- }
- return true
-}
-
-func (vd *validator) value(s *Schema, v any, path string) {
- add := func(code, format string, args ...any) {
- *vd.errs = append(*vd.errs, FieldError{path, code, fmt.Sprintf(format, args...)})
- }
- switch s.Type {
- case TypeObject:
- obj, ok := v.(map[string]any)
- if !ok {
- add(CodeType, "must be an object")
- return
- }
- vd.object(s, obj, path)
- return
- case TypeArray:
- items, ok := v.([]any)
- if !ok {
- add(CodeType, "must be an array")
- return
- }
- for i, item := range items {
- vd.value(s.Items, item, fmt.Sprintf("%s[%d]", path, i))
- }
- return
- case TypeString:
- str, ok := v.(string)
- if !ok {
- add(CodeType, "must be a string")
- return
- }
- if s.OAuth != nil {
- if str != "" && !IsOAuthRef(str) {
- add(CodeFormat, "must be connected through its OAuth button")
- }
- return
- }
- validateString(s, str, add)
- case TypeNumber, TypeInteger:
- n, ok := toFloat(v)
- if !ok {
- add(CodeType, "must be a number")
- return
- }
- if s.Type == TypeInteger && n != math.Trunc(n) {
- add(CodeType, "must be an integer")
- return
- }
- if s.Minimum != nil && n < *s.Minimum {
- add(CodeMinimum, "must be at least %v", *s.Minimum)
- }
- if s.Maximum != nil && n > *s.Maximum {
- add(CodeMaximum, "must be at most %v", *s.Maximum)
- }
- case TypeBoolean:
- if _, ok := v.(bool); !ok {
- add(CodeType, "must be true or false")
- return
- }
- }
- if !allowedChoice(s, v) {
- add(CodeEnum, "is not one of the allowed values")
- }
-}
-
-func validateString(s *Schema, str string, add func(code, format string, args ...any)) {
- if s.Secret && str == RedactedPlaceholder {
- // An unchanged secret: nothing to check about the stored value.
- return
- }
- n := utf8.RuneCountInString(str)
- if s.MinLength != nil && n < *s.MinLength {
- add(CodeMinLength, "must be at least %d characters", *s.MinLength)
- }
- if s.MaxLength != nil && n > *s.MaxLength {
- add(CodeMaxLength, "must be at most %d characters", *s.MaxLength)
- }
- if s.Pattern != "" {
- if re, err := regexp.Compile(s.Pattern); err == nil && !re.MatchString(str) {
- add(CodePattern, "does not match the expected format")
- }
- }
- switch s.Format {
- case "uri":
- if u, err := url.Parse(str); err != nil || u.Scheme == "" || u.Host == "" {
- add(CodeFormat, "must be an absolute URL")
- }
- case "email":
- if _, err := mail.ParseAddress(str); err != nil {
- add(CodeFormat, "must be an email address")
- }
- }
-}
-
-func allowedChoice(s *Schema, v any) bool {
- if len(s.Enum) == 0 && len(s.OneOf) == 0 {
- return true
- }
- for _, e := range s.Enum {
- if looselyEqual(e, v) {
- return true
- }
- }
- for _, c := range s.OneOf {
- if looselyEqual(c.Const, v) {
- return true
- }
- }
- return false
-}
-
-// looselyEqual compares decoded JSON values, treating all numeric kinds as
-// equal when their values are.
-func looselyEqual(a, b any) bool {
- if fa, ok := toFloat(a); ok {
- fb, ok := toFloat(b)
- return ok && fa == fb
- }
- return reflect.DeepEqual(a, b)
-}
-
-func toFloat(v any) (float64, bool) {
- switch n := v.(type) {
- case float64:
- return n, true
- case float32:
- return float64(n), true
- case int:
- return float64(n), true
- case int64:
- return float64(n), true
- case int32:
- return float64(n), true
- case uint64:
- return float64(n), true
- case json.Number:
- f, err := n.Float64()
- return f, err == nil
- }
- return 0, false
-}
diff --git a/internal/plugin/driver/builtin.go b/internal/plugin/driver/builtin.go
deleted file mode 100644
index 587cdf73b..000000000
--- a/internal/plugin/driver/builtin.go
+++ /dev/null
@@ -1,61 +0,0 @@
-package driver
-
-import (
- "context"
- "errors"
- "os"
- "time"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
-)
-
-// Builtin serves plugins compiled into WeKnora: there is nothing to start,
-// every node runs them in process, and they are ready as long as the node is.
-type Builtin struct {
- node string
- started time.Time
- lookup func(pluginID string) (*manifest.Manifest, bool)
-}
-
-// NewBuiltin returns the builtin driver. lookup finds a registered plugin.
-func NewBuiltin(lookup func(pluginID string) (*manifest.Manifest, bool)) *Builtin {
- node, err := os.Hostname()
- if err != nil || node == "" {
- node = "local"
- }
- return &Builtin{node: node, started: time.Now(), lookup: lookup}
-}
-
-// Type implements Driver.
-func (b *Builtin) Type() manifest.RuntimeType { return manifest.RuntimeBuiltin }
-
-// Ensure implements Driver. Builtins are ready by construction; anything else
-// is a misrouted manifest.
-func (b *Builtin) Ensure(_ context.Context, m *manifest.Manifest) error {
- if !m.Builtin {
- return errors.New("the builtin driver only runs builtin plugins")
- }
- return nil
-}
-
-// Remove implements Driver. Builtins cannot be removed, only disabled.
-func (b *Builtin) Remove(context.Context, string, string) error {
- return errors.New("builtin plugins cannot be removed")
-}
-
-// Resolve implements Driver.
-func (b *Builtin) Resolve(_ context.Context, pluginID string, _ uint64) (Endpoint, error) {
- if _, ok := b.lookup(pluginID); !ok {
- return Endpoint{}, errors.New("unknown builtin plugin")
- }
- return Endpoint{Kind: EndpointInProcess}, nil
-}
-
-// Status implements Driver: one ready instance, this node.
-func (b *Builtin) Status(_ context.Context, pluginID string) ([]InstanceStatus, error) {
- m, ok := b.lookup(pluginID)
- if !ok {
- return nil, errors.New("unknown builtin plugin")
- }
- return []InstanceStatus{{Node: b.node, Version: m.Version, State: StateReady, UpdatedAt: b.started}}, nil
-}
diff --git a/internal/plugin/driver/driver.go b/internal/plugin/driver/driver.go
deleted file mode 100644
index 60ab4ada1..000000000
--- a/internal/plugin/driver/driver.go
+++ /dev/null
@@ -1,100 +0,0 @@
-// Package driver defines how plugin code is run and reached. A driver turns a
-// plugin's manifest into running instances (a child process of the plugin
-// host, a remote URL, a Kubernetes Deployment) and tells callers where to send
-// a request. Only the builtin driver exists today; the interface is fixed now
-// so the host, remote and kubernetes drivers slot in without touching callers.
-package driver
-
-import (
- "context"
- "fmt"
- "sync"
- "time"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
-)
-
-// EndpointKind says how to reach a plugin instance.
-type EndpointKind string
-
-const (
- // EndpointInProcess means the code is compiled into this process.
- EndpointInProcess EndpointKind = "in-process"
- // EndpointUnix is a unix socket of a child process on this node.
- EndpointUnix EndpointKind = "unix"
- // EndpointHTTP is an HTTP(S) base URL (plugin host gateway or remote).
- EndpointHTTP EndpointKind = "http"
-)
-
-// Endpoint is where one request to a plugin goes.
-type Endpoint struct {
- Kind EndpointKind `json:"kind"`
- Address string `json:"address,omitempty"`
-}
-
-// InstanceState is the health of one running instance.
-type InstanceState string
-
-// Instance states, from launch to shutdown.
-const (
- // StateStarting: launched, not yet passing health checks.
- StateStarting InstanceState = "starting"
- // StateReady: serving requests.
- StateReady InstanceState = "ready"
- // StateDegraded: running but failing health checks.
- StateDegraded InstanceState = "degraded"
- // StateStopped: not running.
- StateStopped InstanceState = "stopped"
-)
-
-// InstanceStatus reports one instance of a plugin, for the plugin center.
-type InstanceStatus struct {
- // Node is the WeKnora node or plugin host running the instance.
- Node string `json:"node"`
- Version string `json:"version"`
- State InstanceState `json:"state"`
- Error string `json:"error,omitempty"`
- UpdatedAt time.Time `json:"updatedAt"`
-}
-
-// Driver runs plugins of one runtime type.
-type Driver interface {
- // Type is the manifest runtime.type this driver serves.
- Type() manifest.RuntimeType
- // Ensure brings the plugin's current version to a runnable state. It is
- // idempotent: reconciling calls it again on every pass.
- Ensure(ctx context.Context, m *manifest.Manifest) error
- // Remove stops and cleans up one version of a plugin.
- Remove(ctx context.Context, pluginID, version string) error
- // Resolve picks the endpoint for one request, balancing across
- // instances. tenantID matters to drivers that isolate tenants.
- Resolve(ctx context.Context, pluginID string, tenantID uint64) (Endpoint, error)
- // Status lists the plugin's instances.
- Status(ctx context.Context, pluginID string) ([]InstanceStatus, error)
-}
-
-// Set holds one driver per runtime type.
-type Set struct {
- mu sync.RWMutex
- drivers map[manifest.RuntimeType]Driver
-}
-
-// NewSet returns a Set holding the given drivers.
-func NewSet(drivers ...Driver) *Set {
- s := &Set{drivers: make(map[manifest.RuntimeType]Driver)}
- for _, d := range drivers {
- s.drivers[d.Type()] = d
- }
- return s
-}
-
-// For returns the driver for a runtime type.
-func (s *Set) For(t manifest.RuntimeType) (Driver, error) {
- s.mu.RLock()
- defer s.mu.RUnlock()
- d, ok := s.drivers[t]
- if !ok {
- return nil, fmt.Errorf("no driver for runtime %q in this deployment", t)
- }
- return d, nil
-}
diff --git a/internal/plugin/driver/driver_test.go b/internal/plugin/driver/driver_test.go
deleted file mode 100644
index 7eb941cb1..000000000
--- a/internal/plugin/driver/driver_test.go
+++ /dev/null
@@ -1,47 +0,0 @@
-package driver
-
-import (
- "context"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
-)
-
-func TestBuiltinDriver(t *testing.T) {
- plugins := map[string]*manifest.Manifest{
- "weknora.bing": {ID: "weknora.bing", Version: "0.8.2", Builtin: true},
- }
- d := NewBuiltin(func(id string) (*manifest.Manifest, bool) { m, ok := plugins[id]; return m, ok })
- ctx := context.Background()
-
- if err := d.Ensure(ctx, plugins["weknora.bing"]); err != nil {
- t.Fatalf("Ensure builtin: %v", err)
- }
- if err := d.Ensure(ctx, &manifest.Manifest{ID: "acme.x"}); err == nil {
- t.Fatal("the builtin driver must refuse non-builtin manifests")
- }
- if err := d.Remove(ctx, "weknora.bing", "0.8.2"); err == nil {
- t.Fatal("builtins cannot be removed")
- }
- ep, err := d.Resolve(ctx, "weknora.bing", 1)
- if err != nil || ep.Kind != EndpointInProcess {
- t.Fatalf("Resolve = %+v, %v", ep, err)
- }
- if _, err := d.Resolve(ctx, "weknora.nope", 1); err == nil {
- t.Fatal("unknown plugin must not resolve")
- }
- st, err := d.Status(ctx, "weknora.bing")
- if err != nil || len(st) != 1 || st[0].State != StateReady || st[0].Version != "0.8.2" || st[0].Node == "" {
- t.Fatalf("Status = %+v, %v", st, err)
- }
-}
-
-func TestSet(t *testing.T) {
- s := NewSet(NewBuiltin(func(string) (*manifest.Manifest, bool) { return nil, false }))
- if d, err := s.For(manifest.RuntimeBuiltin); err != nil || d.Type() != manifest.RuntimeBuiltin {
- t.Fatalf("For(builtin) = %v, %v", d, err)
- }
- if _, err := s.For(manifest.RuntimeHost); err == nil {
- t.Fatal("no host driver is installed yet")
- }
-}
diff --git a/internal/plugin/events/events.go b/internal/plugin/events/events.go
deleted file mode 100644
index 04d173bd7..000000000
--- a/internal/plugin/events/events.go
+++ /dev/null
@@ -1,151 +0,0 @@
-// Package events delivers WeKnora's business events (a document ingested,
-// an answer given) to the plugins that subscribed to them in
-// permissions.events. Publishing enqueues one task per subscribed plugin
-// that the workspace has switched on; the task calls the plugin's
-// /v1/events and is retried with backoff while the plugin fails with a
-// retryable error, keeping the event ID (at least once).
-package events
-
-import (
- "context"
- "encoding/json"
- "fmt"
- "slices"
- "sync/atomic"
- "time"
-
- "github.com/google/uuid"
- "github.com/hibiken/asynq"
-
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/plugin/tenancy"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// TaskType is the task that delivers one event to one plugin.
-const TaskType = types.TypePluginEvent
-
-// Delivery bounds: retries back off (asynq's default, or the Lite
-// executor's), each attempt within its own timeout.
-const (
- maxRetry = 10
- deliveryTimeout = 2 * time.Minute
-)
-
-// payload is one queued delivery.
-type payload struct {
- PluginID string `json:"pluginId"`
- TenantID uint64 `json:"tenantId"`
- Event pluginapi.EventDelivery `json:"event"`
-}
-
-// Caller makes a call to a plugin (activate.Invoker); an interface so
-// services that publish events do not import the plugin runtime.
-type Caller interface {
- Call(ctx context.Context, m *manifest.Manifest, path string, instance map[string]any, input, out any) error
-}
-
-// Dispatcher fans events out to subscribed plugins and delivers them.
-type Dispatcher struct {
- registry *registry.Registry
- tenancy *tenancy.Service
- enqueue interfaces.TaskEnqueuer
- invoker Caller
- now func() time.Time
-}
-
-// NewDispatcher creates a Dispatcher.
-func NewDispatcher(
- reg *registry.Registry, t *tenancy.Service, enq interfaces.TaskEnqueuer, iv Caller,
-) *Dispatcher {
- return &Dispatcher{registry: reg, tenancy: t, enqueue: enq, invoker: iv, now: time.Now}
-}
-
-var current atomic.Pointer[Dispatcher]
-
-// SetDefault makes d the dispatcher Publish uses.
-func SetDefault(d *Dispatcher) { current.Store(d) }
-
-// Publish sends an event of a workspace to the plugins subscribed to it. It
-// does nothing until the plugin runtime starts, and never fails the caller:
-// a delivery it cannot queue is logged.
-func Publish(ctx context.Context, tenantID uint64, typ string, data any) {
- if d := current.Load(); d != nil && tenantID != 0 {
- d.Publish(ctx, tenantID, typ, data)
- }
-}
-
-// subscribed reports whether a loaded plugin wants an event and can take
-// it (it has code).
-func subscribed(m *manifest.Manifest, typ string) bool {
- rt := m.Runtime.Type
- return (rt == manifest.RuntimeHost || rt == manifest.RuntimeRemote) && slices.Contains(m.Permissions.Events, typ)
-}
-
-// Publish enqueues a delivery for every subscribed plugin the workspace has
-// switched on.
-func (d *Dispatcher) Publish(ctx context.Context, tenantID uint64, typ string, data any) {
- raw, err := json.Marshal(data)
- if err != nil {
- logger.Warnf(ctx, "[plugin] event %s: %v", typ, err)
- return
- }
- ev := pluginapi.EventDelivery{ID: uuid.NewString(), Type: typ, OccurredAt: d.now().UTC(), Data: raw}
- for _, m := range d.registry.Plugins() {
- if !subscribed(m, typ) {
- continue
- }
- if on, err := d.tenancy.PluginEnabled(ctx, tenantID, m.ID); err != nil || !on {
- if err != nil {
- logger.Warnf(ctx, "[plugin] event %s to %s: read switch: %v", typ, m.ID, err)
- }
- continue
- }
- body, _ := json.Marshal(payload{PluginID: m.ID, TenantID: tenantID, Event: ev})
- task := asynq.NewTask(TaskType, body)
- _, err := d.enqueue.Enqueue(task, asynq.Queue(types.QueueMaintenance), asynq.MaxRetry(maxRetry),
- asynq.Timeout(deliveryTimeout))
- if err != nil {
- logger.Warnf(ctx, "[plugin] event %s to %s: enqueue: %v", typ, m.ID, err)
- }
- }
-}
-
-// Handle delivers one event (the TaskType handler). An event is dropped when
-// the plugin is gone, no longer subscribed or switched off, or refuses it
-// for good; a retryable failure is returned so the task runs again.
-func (d *Dispatcher) Handle(ctx context.Context, task *asynq.Task) error {
- var p payload
- if err := json.Unmarshal(task.Payload(), &p); err != nil {
- return fmt.Errorf("decode plugin event: %w: %w", err, asynq.SkipRetry)
- }
- m, ok := d.registry.Plugin(p.PluginID)
- if !ok || !subscribed(m, p.Event.Type) {
- return nil
- }
- if on, err := d.tenancy.PluginEnabled(ctx, p.TenantID, p.PluginID); err != nil {
- return err
- } else if !on {
- return nil
- }
- ev := p.Event
- ev.Attempt = 1
- if n, ok := asynq.GetRetryCount(ctx); ok {
- ev.Attempt = n + 1
- }
- ctx = context.WithValue(ctx, types.TenantIDContextKey, p.TenantID)
- err := d.invoker.Call(ctx, m, pluginapi.EventsPath, nil, ev, nil)
- if err == nil {
- return nil
- }
- if pe, ok := pluginapi.AsError(err); ok && !pe.Retryable {
- logger.Warnf(ctx, "[plugin] %s refused event %s (%s): %v", p.PluginID, ev.ID, ev.Type, err)
- return nil
- }
- logger.Infof(ctx, "[plugin] event %s to %s failed (attempt %d), will retry: %v", ev.ID, p.PluginID, ev.Attempt, err)
- return err
-}
diff --git a/internal/plugin/events/events_test.go b/internal/plugin/events/events_test.go
deleted file mode 100644
index 2f94d4eb5..000000000
--- a/internal/plugin/events/events_test.go
+++ /dev/null
@@ -1,208 +0,0 @@
-package events
-
-import (
- "context"
- "encoding/json"
- "net/http/httptest"
- "sync"
- "testing"
-
- "github.com/hibiken/asynq"
-
- "github.com/Tencent/WeKnora/internal/plugin/activate"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/plugintest"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/plugin/tenancy"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-type queue struct {
- mu sync.Mutex
- tasks []*asynq.Task
-}
-
-func (q *queue) Enqueue(task *asynq.Task, _ ...asynq.Option) (*asynq.TaskInfo, error) {
- q.mu.Lock()
- defer q.mu.Unlock()
- q.tasks = append(q.tasks, task)
- return &asynq.TaskInfo{}, nil
-}
-
-func (q *queue) take() []*asynq.Task {
- q.mu.Lock()
- defer q.mu.Unlock()
- out := q.tasks
- q.tasks = nil
- return out
-}
-
-type oneClient struct{ c *client.Client }
-
-func (o oneClient) Client(context.Context, *manifest.Manifest) (*client.Client, error) {
- return o.c, nil
-}
-func (o oneClient) OnThisNode(string) bool { return true }
-
-type received struct {
- mu sync.Mutex
- events []pluginapi.EventDelivery
- tenant []uint64
-}
-
-func setup(t *testing.T) (*Dispatcher, *queue, *received, *plugintest.MemTenantSettings) {
- t.Helper()
- reg := registry.New()
- plugin := func(id string, events ...string) *manifest.Manifest {
- return &manifest.Manifest{
- SchemaVersion: manifest.SchemaVersion, ID: id, Version: "1.0.0", APIVersion: pluginapi.APIVersion,
- Name: manifest.Text(id, nil), Publisher: manifest.Publisher{ID: "acme"},
- Runtime: manifest.Runtime{Type: manifest.RuntimeHost, Kind: "binary", Entry: "bin/x"},
- Permissions: manifest.Permissions{Events: events},
- }
- }
- listener := plugin("acme.audit", pluginapi.EventKnowledgeIngested, pluginapi.EventKnowledgeFailed)
- other := plugin("acme.other", pluginapi.EventChatAnswered)
- for _, m := range []*manifest.Manifest{listener, other} {
- if err := reg.Register(m); err != nil {
- t.Fatal(err)
- }
- }
- settings := &plugintest.MemTenantSettings{}
- ctx := context.Background()
- for _, id := range []string{"acme.audit", "acme.other"} {
- _ = settings.Upsert(ctx, &types.PluginTenantSetting{TenantID: 7, PluginID: id, Enabled: true})
- }
-
- got := &received{}
- p := pluginsdk.New(pluginsdk.Info{ID: "acme.audit", Version: "1.0.0"})
- p.OnEvent(func(_ context.Context, call *pluginsdk.Call, ev pluginapi.EventDelivery) error {
- got.mu.Lock()
- got.events = append(got.events, ev)
- got.tenant = append(got.tenant, call.TenantID)
- got.mu.Unlock()
- var data pluginapi.KnowledgeEventData
- _ = json.Unmarshal(ev.Data, &data)
- switch data.KnowledgeID {
- case "down":
- return pluginapi.Errorf(pluginapi.CodeUnavailable, "try later")
- case "bad":
- return pluginapi.Errorf(pluginapi.CodeBadRequest, "never")
- }
- return nil
- })
- srv := httptest.NewServer(p.Handler())
- t.Cleanup(srv.Close)
- q := &queue{}
- d := NewDispatcher(reg, tenancy.NewService(reg, settings), q,
- activate.NewInvoker(oneClient{client.New(srv.URL, nil, nil)}))
- return d, q, got, settings
-}
-
-func TestPublishFansOutToSubscribedEnabledPlugins(t *testing.T) {
- ctx := context.Background()
- d, q, got, settings := setup(t)
-
- d.Publish(ctx, 7, pluginapi.EventKnowledgeIngested, pluginapi.KnowledgeEventData{KnowledgeID: "k1"})
- tasks := q.take()
- if len(tasks) != 1 || tasks[0].Type() != TaskType {
- t.Fatalf("tasks = %d", len(tasks))
- }
- if err := d.Handle(ctx, tasks[0]); err != nil {
- t.Fatal(err)
- }
- if len(got.events) != 1 || got.events[0].Type != pluginapi.EventKnowledgeIngested || got.tenant[0] != 7 ||
- got.events[0].Attempt != 1 || got.events[0].ID == "" {
- t.Fatalf("delivered = %+v tenants %v", got.events, got.tenant)
- }
-
- // Another workspace never switched the plugins on.
- d.Publish(ctx, 8, pluginapi.EventKnowledgeIngested, pluginapi.KnowledgeEventData{KnowledgeID: "k1"})
- if n := len(q.take()); n != 0 {
- t.Fatalf("an off plugin got %d deliveries", n)
- }
-
- // A plugin switched off after the event was queued does not get it.
- d.Publish(ctx, 7, pluginapi.EventKnowledgeIngested, pluginapi.KnowledgeEventData{KnowledgeID: "k2"})
- queued := q.take()
- _ = settings.Upsert(ctx, &types.PluginTenantSetting{TenantID: 7, PluginID: "acme.audit", Enabled: false})
- if err := d.Handle(ctx, queued[0]); err != nil || len(got.events) != 1 {
- t.Fatalf("delivered to a switched-off plugin: %v", err)
- }
-}
-
-func TestHandleRetriesOnlyRetryableFailures(t *testing.T) {
- ctx := context.Background()
- d, q, got, _ := setup(t)
- d.Publish(ctx, 7, pluginapi.EventKnowledgeFailed, pluginapi.KnowledgeEventData{KnowledgeID: "down"})
- d.Publish(ctx, 7, pluginapi.EventKnowledgeFailed, pluginapi.KnowledgeEventData{KnowledgeID: "bad"})
- tasks := q.take()
- if err := d.Handle(ctx, tasks[0]); err == nil {
- t.Fatal("a retryable failure must be retried")
- }
- if err := d.Handle(ctx, tasks[0]); err == nil || got.events[0].ID != got.events[1].ID {
- t.Fatal("a retry keeps the event ID")
- }
- if err := d.Handle(ctx, tasks[1]); err != nil {
- t.Fatalf("a refused event is dropped, got %v", err)
- }
-}
-
-// fakeKnowledge is the part of the repository the watch uses.
-type fakeKnowledge struct {
- interfaces.KnowledgeRepository
- rows map[string]*types.Knowledge
-}
-
-func (f *fakeKnowledge) GetKnowledgeByIDOnly(_ context.Context, id string) (*types.Knowledge, error) {
- k := *f.rows[id]
- return &k, nil
-}
-
-func (f *fakeKnowledge) UpdateKnowledge(_ context.Context, k *types.Knowledge) error {
- c := *k
- f.rows[k.ID] = &c
- return nil
-}
-
-func (f *fakeKnowledge) FinalizeSubtask(_ context.Context, id string) (int, bool, error) {
- f.rows[id].ParseStatus = types.ParseStatusCompleted
- return 0, true, nil
-}
-
-func TestWatchKnowledgePublishesTransitionsOnly(t *testing.T) {
- ctx := context.Background()
- d, q, _, _ := setup(t)
- SetDefault(d)
- t.Cleanup(func() { SetDefault(nil) })
- base := &fakeKnowledge{rows: map[string]*types.Knowledge{
- "k1": {ID: "k1", TenantID: 7, KnowledgeBaseID: "kb", ParseStatus: types.ParseStatusProcessing},
- "k2": {ID: "k2", TenantID: 7, KnowledgeBaseID: "kb", ParseStatus: types.ParseStatusFinalizing},
- }}
- repo := WatchKnowledge(base)
-
- k := *base.rows["k1"]
- k.ParseStatus, k.ErrorMessage = types.ParseStatusFailed, "boom"
- _ = repo.UpdateKnowledge(ctx, &k)
- _ = repo.UpdateKnowledge(ctx, &k) // already failed: no second event
- k.ParseStatus = types.ParseStatusProcessing
- _ = repo.UpdateKnowledge(ctx, &k) // not terminal
- _, _, _ = repo.FinalizeSubtask(ctx, "k2")
-
- var seen []string
- for _, task := range q.take() {
- var p payload
- _ = json.Unmarshal(task.Payload(), &p)
- var data pluginapi.KnowledgeEventData
- _ = json.Unmarshal(p.Event.Data, &data)
- seen = append(seen, p.Event.Type+":"+data.KnowledgeID+":"+data.Error)
- }
- want := []string{"knowledge.failed:k1:boom", "knowledge.ingested:k2:"}
- if len(seen) != 2 || seen[0] != want[0] || seen[1] != want[1] {
- t.Fatalf("published %v, want %v", seen, want)
- }
-}
diff --git a/internal/plugin/events/knowledge.go b/internal/plugin/events/knowledge.go
deleted file mode 100644
index 54fdec193..000000000
--- a/internal/plugin/events/knowledge.go
+++ /dev/null
@@ -1,122 +0,0 @@
-package events
-
-import (
- "context"
-
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// WatchKnowledge wraps the knowledge repository to publish
-// knowledge.ingested and knowledge.failed. A document reaches either state
-// through many code paths (enrichment finishing last, no enrichment to do,
-// a dozen failure branches), but only through these few writes, so the
-// events are raised here, on the transition only.
-func WatchKnowledge(repo interfaces.KnowledgeRepository) interfaces.KnowledgeRepository {
- return &knowledgeWatch{KnowledgeRepository: repo}
-}
-
-type knowledgeWatch struct {
- interfaces.KnowledgeRepository
-}
-
-func terminal(status string) bool {
- return status == types.ParseStatusCompleted || status == types.ParseStatusFailed
-}
-
-// publishKnowledge raises the event for a row that just reached status.
-func publishKnowledge(ctx context.Context, k *types.Knowledge) {
- if k == nil {
- return
- }
- data := pluginapi.KnowledgeEventData{
- KnowledgeBaseID: k.KnowledgeBaseID, KnowledgeID: k.ID, Title: k.Title,
- FileName: k.FileName, FileType: k.FileType, Source: k.Source,
- }
- switch k.ParseStatus {
- case types.ParseStatusCompleted:
- Publish(ctx, k.TenantID, pluginapi.EventKnowledgeIngested, data)
- case types.ParseStatusFailed:
- data.Error = k.ErrorMessage
- Publish(ctx, k.TenantID, pluginapi.EventKnowledgeFailed, data)
- }
-}
-
-// previous reads a row's status before a write that may change it, only
-// when events are on (the read is not free).
-func (w *knowledgeWatch) previous(ctx context.Context, id string) string {
- if current.Load() == nil {
- return ""
- }
- before, err := w.GetKnowledgeByIDOnly(ctx, id)
- if err != nil || before == nil {
- return ""
- }
- return before.ParseStatus
-}
-
-func (w *knowledgeWatch) UpdateKnowledge(ctx context.Context, k *types.Knowledge) error {
- if k == nil || !terminal(k.ParseStatus) {
- return w.KnowledgeRepository.UpdateKnowledge(ctx, k)
- }
- was := w.previous(ctx, k.ID)
- if err := w.KnowledgeRepository.UpdateKnowledge(ctx, k); err != nil {
- return err
- }
- if was != k.ParseStatus {
- publishKnowledge(ctx, k)
- }
- return nil
-}
-
-func (w *knowledgeWatch) UpdateKnowledgeForTransfer(ctx context.Context, before, after *types.Knowledge) error {
- if err := w.KnowledgeRepository.UpdateKnowledgeForTransfer(ctx, before, after); err != nil {
- return err
- }
- if after != nil && terminal(after.ParseStatus) && (before == nil || before.ParseStatus != after.ParseStatus) {
- publishKnowledge(ctx, after)
- }
- return nil
-}
-
-func (w *knowledgeWatch) UpdateKnowledgeColumns(ctx context.Context, id string, values map[string]interface{}) error {
- status, _ := values["parse_status"].(string)
- if !terminal(status) {
- return w.KnowledgeRepository.UpdateKnowledgeColumns(ctx, id, values)
- }
- was := w.previous(ctx, id)
- if err := w.KnowledgeRepository.UpdateKnowledgeColumns(ctx, id, values); err != nil {
- return err
- }
- if was != status {
- w.publishByID(ctx, id)
- }
- return nil
-}
-
-func (w *knowledgeWatch) FinalizeSubtask(ctx context.Context, id string) (int, bool, error) {
- n, promoted, err := w.KnowledgeRepository.FinalizeSubtask(ctx, id)
- if err == nil && promoted {
- w.publishByID(ctx, id)
- }
- return n, promoted, err
-}
-
-func (w *knowledgeWatch) CompleteProcessingWithoutSubtasks(ctx context.Context, id string) (bool, error) {
- completed, err := w.KnowledgeRepository.CompleteProcessingWithoutSubtasks(ctx, id)
- if err == nil && completed {
- w.publishByID(ctx, id)
- }
- return completed, err
-}
-
-func (w *knowledgeWatch) publishByID(ctx context.Context, id string) {
- if current.Load() == nil {
- return
- }
- k, err := w.GetKnowledgeByIDOnly(ctx, id)
- if err == nil {
- publishKnowledge(ctx, k)
- }
-}
diff --git a/internal/plugin/host/gateway.go b/internal/plugin/host/gateway.go
deleted file mode 100644
index 729596197..000000000
--- a/internal/plugin/host/gateway.go
+++ /dev/null
@@ -1,119 +0,0 @@
-package host
-
-import (
- "bytes"
- "encoding/json"
- "io"
- "net/http"
- "strings"
- "time"
-
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// GatewayPrefix starts the paths a plugin host serves its plugins under:
-// /p/{pluginId}/{version}/{protocol path}.
-const GatewayPrefix = "/p/"
-
-// maxGatewayBody bounds one relayed request: a parse call carries the whole
-// document.
-const maxGatewayBody = 512 << 20
-
-// Gateway serves the plugins this host runs to the other WeKnora nodes, at
-// /p/{pluginId}/{version}/... Requests must be signed with the cluster key
-// (pluginapi.Sign over the body); answers, streams included, are relayed as
-// the plugin gives them.
-func (m *Manager) Gateway(key []byte) http.Handler {
- mux := http.NewServeMux()
- mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, _ *http.Request) {
- w.WriteHeader(http.StatusOK)
- })
- mux.HandleFunc(GatewayPrefix, func(w http.ResponseWriter, r *http.Request) {
- m.inFlight.Add(1)
- defer m.inFlight.Add(-1)
- m.relay(w, r, key)
- })
- return mux
-}
-
-func gatewayError(w http.ResponseWriter, e *pluginapi.Error) {
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(e.Code.HTTPStatus())
- _ = json.NewEncoder(w).Encode(pluginapi.ErrorBody{Error: *e})
-}
-
-func (m *Manager) relay(w http.ResponseWriter, r *http.Request, key []byte) {
- body, err := io.ReadAll(io.LimitReader(r.Body, maxGatewayBody+1))
- if err != nil {
- gatewayError(w, pluginapi.Errorf(pluginapi.CodeBadRequest, "read request: %v", err))
- return
- }
- if len(body) > maxGatewayBody {
- gatewayError(w, pluginapi.Errorf(pluginapi.CodeBadRequest, "request is over %d bytes", maxGatewayBody))
- return
- }
- err = pluginapi.VerifySignature(key, r.Header.Get(pluginapi.TimestampHeader),
- r.Header.Get(pluginapi.SignatureHeader), body, time.Now())
- if err != nil {
- gatewayError(w, pluginapi.Errorf(pluginapi.CodeUnauthorized, "gateway: %v", err))
- return
- }
- parts := strings.SplitN(strings.TrimPrefix(r.URL.Path, GatewayPrefix), "/", 3)
- if len(parts) < 3 || parts[0] == "" || parts[1] == "" {
- gatewayError(w, pluginapi.Errorf(pluginapi.CodeNotFound, "no endpoint %s", r.URL.Path))
- return
- }
- id, version, path := parts[0], parts[1], "/"+parts[2]
- m.mu.Lock()
- p := m.procs[id]
- m.mu.Unlock()
- if p == nil || p.spec.m.Version != version {
- // The caller's view of this host is a heartbeat old; it retries
- // elsewhere.
- gatewayError(w, pluginapi.Errorf(pluginapi.CodeUnavailable, "this host does not run %s@%s", id, version))
- return
- }
- c, err := p.Client()
- if err != nil {
- if pe, ok := pluginapi.AsError(err); ok {
- gatewayError(w, pe)
- } else {
- gatewayError(w, pluginapi.Errorf(pluginapi.CodeUnavailable, "%v", err))
- }
- return
- }
- var sent []byte
- if len(body) > 0 {
- sent = body
- }
- resp, err := c.Raw(r.Context(), r.Method, path, sent, r.Header.Get(pluginapi.RequestIDHeader))
- if err != nil {
- gatewayError(w, pluginapi.Errorf(pluginapi.CodeUnavailable, "%v", err))
- return
- }
- defer func() { _ = resp.Body.Close() }()
- if ct := resp.Header.Get("Content-Type"); ct != "" {
- w.Header().Set("Content-Type", ct)
- }
- w.WriteHeader(resp.StatusCode)
- flusher, _ := w.(http.Flusher)
- buf := make([]byte, 32<<10)
- for {
- n, err := resp.Body.Read(buf)
- if n > 0 {
- if _, werr := w.Write(buf[:n]); werr != nil {
- return
- }
- // Stream events go out as the plugin writes them.
- if flusher != nil && bytes.IndexByte(buf[:n], '\n') >= 0 {
- flusher.Flush()
- }
- }
- if err != nil {
- if flusher != nil {
- flusher.Flush()
- }
- return
- }
- }
-}
diff --git a/internal/plugin/host/host.go b/internal/plugin/host/host.go
deleted file mode 100644
index 270de4327..000000000
--- a/internal/plugin/host/host.go
+++ /dev/null
@@ -1,284 +0,0 @@
-// Package host is the embedded plugin host: it runs host-runtime plugins as
-// child processes of this WeKnora node and hands out clients to reach them.
-// Each node runs its own copy of every enabled host plugin (the reconciler
-// activates it everywhere), so a call never leaves the node.
-//
-// A plugin process gets a private socket, a random token, an environment
-// built from scratch (no WeKnora secrets) and an egress proxy limited to the
-// hosts its manifest was granted. The host checks that the process is the
-// installed package, health-checks it and restarts it with backoff.
-package host
-
-import (
- "context"
- "fmt"
- "os"
- "os/exec"
- "sort"
- "strings"
- "sync"
- "sync/atomic"
-
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// StateReporter learns when a running plugin's health changes, so the
-// cluster view shows a crashed plugin as degraded.
-type StateReporter interface {
- ReportRuntime(pluginID string, healthy bool, err error)
-}
-
-// Manager runs host plugins. It is a reconcile.Activator and must come
-// before the activators that route calls to the plugins.
-type Manager struct {
- reporter StateReporter
- inFlight atomic.Int64
-
- mu sync.Mutex
- procs map[string]*process
- kinds map[string]bool
- // standalone hosts have no one to hand other kinds to.
- standalone bool
- // direct are hosts plugins reach without the egress proxy: the Host API
- // when it is not on this machine.
- direct []string
-}
-
-// NewManager creates an empty host that runs every kind this machine can.
-func NewManager() *Manager {
- m := &Manager{procs: map[string]*process{}}
- m.SetKinds(AvailableKinds())
- return m
-}
-
-// NewStandaloneManager creates the host of a standalone plugin host
-// (weknora plugin-host): it runs the given kinds and refuses the others.
-func NewStandaloneManager(kinds []string) *Manager {
- m := &Manager{procs: map[string]*process{}, standalone: true}
- m.SetKinds(kinds)
- return m
-}
-
-// AvailableKinds are the kinds this machine can run: binaries, and python
-// when an interpreter is installed.
-func AvailableKinds() []string {
- kinds := []string{KindBinary}
- if _, err := exec.LookPath(PythonCommand()); err == nil {
- kinds = append(kinds, KindPython)
- }
- return kinds
-}
-
-// KindsFromEnv reads a comma-separated list of kinds from an environment
-// variable: unset means every available kind, "none" means none.
-func KindsFromEnv(name string) []string {
- raw, ok := os.LookupEnv(name)
- raw = strings.TrimSpace(raw)
- if !ok || raw == "" {
- return AvailableKinds()
- }
- if raw == "none" {
- return nil
- }
- var out []string
- for _, k := range strings.Split(raw, ",") {
- if k = strings.TrimSpace(k); Supported(k) {
- out = append(out, k)
- }
- }
- return out
-}
-
-// SetKinds limits the kinds this host runs; plugins of other kinds are left
-// to plugin hosts elsewhere.
-func (m *Manager) SetKinds(kinds []string) {
- m.mu.Lock()
- defer m.mu.Unlock()
- m.kinds = map[string]bool{}
- for _, k := range kinds {
- m.kinds[k] = true
- }
-}
-
-// Kinds lists the kinds this host runs.
-func (m *Manager) Kinds() []string {
- m.mu.Lock()
- defer m.mu.Unlock()
- out := make([]string, 0, len(m.kinds))
- for k := range m.kinds {
- out = append(out, k)
- }
- sort.Strings(out)
- return out
-}
-
-// Runs reports whether this host runs plugins of a kind.
-func (m *Manager) Runs(kind string) bool {
- m.mu.Lock()
- defer m.mu.Unlock()
- return m.kinds[kind]
-}
-
-// SetDirectHosts names hosts plugins reach without the egress proxy, such
-// as the Host API's when it is on another machine. It applies to processes
-// started afterwards.
-func (m *Manager) SetDirectHosts(hosts ...string) {
- m.mu.Lock()
- m.direct = append([]string(nil), hosts...)
- m.mu.Unlock()
-}
-
-// SetReporter wires health changes to the reconciler.
-func (m *Manager) SetReporter(r StateReporter) {
- m.mu.Lock()
- m.reporter = r
- m.mu.Unlock()
-}
-
-// Name implements reconcile.Activator.
-func (m *Manager) Name() string { return "host" }
-
-// ActivatesInPlace implements reconcile.InPlaceActivator: an upgrade starts
-// the new process before the old one stops.
-func (m *Manager) ActivatesInPlace() {}
-
-// Activate starts a host plugin and returns once it is ready. Other runtimes
-// are ignored. A new version replaces the old process only after it started.
-func (m *Manager) Activate(ctx context.Context, l *reconcile.Loaded) error {
- if l.Manifest.Runtime.Type != manifest.RuntimeHost {
- return nil
- }
- if !Supported(l.Manifest.Runtime.Kind) {
- return fmt.Errorf("runtime.kind %q is not supported by this host; binary and python are",
- l.Manifest.Runtime.Kind)
- }
- if !m.Runs(l.Manifest.Runtime.Kind) {
- if m.standalone {
- return fmt.Errorf("this plugin host does not run %s plugins (it runs %s)",
- l.Manifest.Runtime.Kind, strings.Join(m.Kinds(), ", "))
- }
- return nil // a plugin host elsewhere runs it
- }
- id := l.Manifest.ID
- m.mu.Lock()
- direct := m.direct
- m.mu.Unlock()
- p, err := startProcess(spec{m: l.Manifest, dir: l.Dir, direct: direct}, func(s State, err error) {
- m.report(id, s, err)
- })
- if err != nil {
- return err
- }
- m.mu.Lock()
- old := m.procs[id]
- m.procs[id] = p
- m.mu.Unlock()
- if old != nil {
- old.stop()
- }
- logger.Infof(ctx, "[plugin] host started %s %s", id, l.Manifest.Version)
- return nil
-}
-
-// Deactivate implements reconcile.Activator: it stops the process.
-func (m *Manager) Deactivate(ctx context.Context, pluginID string) error {
- m.mu.Lock()
- p := m.procs[pluginID]
- delete(m.procs, pluginID)
- m.mu.Unlock()
- if p != nil {
- p.stop()
- logger.Infof(ctx, "[plugin] host stopped %s", pluginID)
- }
- return nil
-}
-
-func (m *Manager) report(pluginID string, s State, err error) {
- m.mu.Lock()
- r := m.reporter
- m.mu.Unlock()
- if r == nil {
- return
- }
- switch s {
- case StateReady:
- r.ReportRuntime(pluginID, true, nil)
- case StateDegraded:
- r.ReportRuntime(pluginID, false, err)
- }
-}
-
-// Client returns a client for a running plugin. The error is a retryable
-// pluginapi unavailable error while the plugin is starting or restarting.
-func (m *Manager) Client(pluginID string) (*client.Client, error) {
- m.mu.Lock()
- p := m.procs[pluginID]
- m.mu.Unlock()
- if p == nil {
- return nil, &pluginapi.Error{
- Code: pluginapi.CodeUnavailable, Message: fmt.Sprintf("plugin %s is not running on this node", pluginID),
- }
- }
- return p.Client()
-}
-
-// Local reports whether a plugin runs on this host.
-func (m *Manager) Local(pluginID string) bool {
- m.mu.Lock()
- defer m.mu.Unlock()
- _, ok := m.procs[pluginID]
- return ok
-}
-
-// Running is one plugin this host runs.
-type Running struct {
- ID string `json:"id"`
- Version string `json:"version"`
- Kind string `json:"kind"`
- State State `json:"state"`
-}
-
-// Running lists the plugins this host runs and their state.
-func (m *Manager) Running() []Running {
- m.mu.Lock()
- procs := make([]*process, 0, len(m.procs))
- for _, p := range m.procs {
- procs = append(procs, p)
- }
- m.mu.Unlock()
- out := make([]Running, 0, len(procs))
- for _, p := range procs {
- p.mu.RLock()
- st := p.state
- p.mu.RUnlock()
- out = append(out, Running{
- ID: p.spec.m.ID, Version: p.spec.m.Version, Kind: p.spec.m.Runtime.Kind, State: st,
- })
- }
- sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
- return out
-}
-
-// InFlight is how many gateway calls this host is answering.
-func (m *Manager) InFlight() int64 { return m.inFlight.Load() }
-
-// Close stops every plugin, for shutdown.
-func (m *Manager) Close() {
- m.mu.Lock()
- procs := m.procs
- m.procs = map[string]*process{}
- m.mu.Unlock()
- var wg sync.WaitGroup
- for _, p := range procs {
- wg.Add(1)
- go func() {
- defer wg.Done()
- p.stop()
- }()
- }
- wg.Wait()
-}
diff --git a/internal/plugin/host/host_test.go b/internal/plugin/host/host_test.go
deleted file mode 100644
index d62f1d7ed..000000000
--- a/internal/plugin/host/host_test.go
+++ /dev/null
@@ -1,297 +0,0 @@
-package host
-
-import (
- "context"
- "net"
- "net/http"
- "net/http/httptest"
- "net/url"
- "os"
- "os/exec"
- "path/filepath"
- "runtime"
- "strconv"
- "strings"
- "sync"
- "syscall"
- "testing"
- "time"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-var (
- buildOnce sync.Once
- binary string
- buildErr error
-)
-
-// echoBinary builds testdata/echoplugin once per test run.
-func echoBinary(t *testing.T) string {
- t.Helper()
- buildOnce.Do(func() {
- dir, err := os.MkdirTemp("", "echoplugin")
- if err != nil {
- buildErr = err
- return
- }
- binary = filepath.Join(dir, "echo")
- out, err := exec.Command("go", "build", "-o", binary, "./testdata/echoplugin").CombinedOutput()
- if err != nil {
- buildErr = err
- t.Logf("%s", out)
- }
- })
- if buildErr != nil {
- t.Fatalf("build echo plugin: %v", buildErr)
- }
- return binary
-}
-
-// install lays out an extracted package with the echo binary.
-func install(t *testing.T, version string, fakeVersion string) *reconcile.Loaded {
- t.Helper()
- dir := t.TempDir()
- bin := filepath.Join(dir, "bin", runtime.GOOS+"-"+runtime.GOARCH)
- if err := os.MkdirAll(bin, 0o755); err != nil {
- t.Fatal(err)
- }
- data, err := os.ReadFile(echoBinary(t))
- if err != nil {
- t.Fatal(err)
- }
- // Extraction writes plain files; the host must make the entry runnable.
- name := "echo"
- if runtime.GOOS == "windows" {
- name += ".exe"
- }
- if err := os.WriteFile(filepath.Join(bin, name), data, 0o644); err != nil {
- t.Fatal(err)
- }
- if fakeVersion != "" {
- _ = os.WriteFile(filepath.Join(dir, "version"), []byte(fakeVersion), 0o644)
- }
- m := &manifest.Manifest{
- SchemaVersion: manifest.SchemaVersion, ID: "acme.echo", Version: version, APIVersion: pluginapi.APIVersion,
- Name: manifest.Text("Echo", nil), Publisher: manifest.Publisher{ID: "acme"},
- Runtime: manifest.Runtime{Type: manifest.RuntimeHost, Kind: "binary", Entry: "bin/{os}-{arch}/echo"},
- Contributes: manifest.Contributions{
- manifest.PointWebSearch: {{ID: "echo", Name: manifest.Text("Echo", nil)}},
- },
- }
- return &reconcile.Loaded{Manifest: m, Dir: dir}
-}
-
-type reports struct {
- mu sync.Mutex
- log []string
-}
-
-func (r *reports) ReportRuntime(id string, healthy bool, _ error) {
- r.mu.Lock()
- defer r.mu.Unlock()
- r.log = append(r.log, id+":"+strconv.FormatBool(healthy))
-}
-
-func (r *reports) String() string {
- r.mu.Lock()
- defer r.mu.Unlock()
- return strings.Join(r.log, ",")
-}
-
-func fastTimings(t *testing.T) {
- t.Helper()
- oldBackoff, oldInterval, oldGrace := restartBackoffFloor, healthInterval, stopGrace
- restartBackoffFloor, healthInterval, stopGrace = 20*time.Millisecond, 100*time.Millisecond, 2*time.Second
- t.Cleanup(func() { restartBackoffFloor, healthInterval, stopGrace = oldBackoff, oldInterval, oldGrace })
-}
-
-func search(t *testing.T, m *Manager, q string) (string, error) {
- t.Helper()
- c, err := m.Client("acme.echo")
- if err != nil {
- return "", err
- }
- var out pluginapi.SearchOutput
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- err = c.Call(ctx, pluginapi.SearchPath("echo"), pluginapi.Envelope{}, pluginapi.SearchInput{Query: q}, &out)
- if err != nil {
- return "", err
- }
- return out.Results[0].Title, nil
-}
-
-func waitFor(t *testing.T, what string, cond func() bool) {
- t.Helper()
- deadline := time.Now().Add(10 * time.Second)
- for !cond() {
- if time.Now().After(deadline) {
- t.Fatalf("timed out waiting for %s", what)
- }
- time.Sleep(20 * time.Millisecond)
- }
-}
-
-func TestHostRunsRestartsAndStopsAPlugin(t *testing.T) {
- fastTimings(t)
- t.Setenv("DB_PASSWORD", "must-not-leak")
- ctx := context.Background()
- m := NewManager()
- rep := &reports{}
- m.SetReporter(rep)
- defer m.Close()
-
- if err := m.Activate(ctx, install(t, "1.0.0", "")); err != nil {
- t.Fatalf("Activate: %v", err)
- }
- if got, err := search(t, m, "hello"); err != nil || got != "hello" {
- t.Fatalf("search = %q, %v", got, err)
- }
- env, err := search(t, m, "env")
- if err != nil {
- t.Fatal(err)
- }
- if strings.Contains(env, "DB_PASSWORD") || !strings.Contains(env, "HTTPS_PROXY") ||
- !strings.Contains(env, pluginapi.EnvToken) {
- t.Fatalf("plugin environment = %s", env)
- }
-
- pid1, _ := search(t, m, "pid")
- _, _ = search(t, m, "crash")
- waitFor(t, "restart", func() bool {
- pid, err := search(t, m, "pid")
- return err == nil && pid != pid1
- })
- if !strings.Contains(rep.String(), "acme.echo:false") || !strings.HasSuffix(rep.String(), "acme.echo:true") {
- t.Fatalf("reports = %s", rep)
- }
-
- pid2, _ := search(t, m, "pid")
- if err := m.Deactivate(ctx, "acme.echo"); err != nil {
- t.Fatal(err)
- }
- if _, err := m.Client("acme.echo"); err == nil {
- t.Fatal("a stopped plugin must have no client")
- }
- if runtime.GOOS != "windows" {
- n, _ := strconv.Atoi(pid2)
- if proc, err := os.FindProcess(n); err == nil && proc.Signal(syscall.Signal(0)) == nil {
- t.Fatalf("process %d still runs after Deactivate", n)
- }
- }
-}
-
-func TestHostRefusesAProcessThatIsNotThePackage(t *testing.T) {
- fastTimings(t)
- m := NewManager()
- defer m.Close()
- err := m.Activate(context.Background(), install(t, "1.0.0", "9.9.9"))
- if err == nil || !strings.Contains(err.Error(), "reports [email protected]") {
- t.Fatalf("want a manifest mismatch, got %v", err)
- }
- l := install(t, "1.0.0", "")
- l.Manifest.Contributes[manifest.PointWebSearch] = append(l.Manifest.Contributes[manifest.PointWebSearch],
- manifest.Contribution{ID: "missing", Name: manifest.Text("Missing", nil)})
- if err := m.Activate(context.Background(), l); err == nil || !strings.Contains(err.Error(), "webSearch/missing") {
- t.Fatalf("want a missing contribution error, got %v", err)
- }
- l = install(t, "1.0.0", "")
- l.Manifest.Runtime.Entry = "bin/{os}-{arch}/nope"
- if err := m.Activate(context.Background(), l); err == nil || !strings.Contains(err.Error(), "has no") {
- t.Fatalf("want a missing build error, got %v", err)
- }
-}
-
-func TestUpgradeSwapsProcessesInPlace(t *testing.T) {
- fastTimings(t)
- ctx := context.Background()
- m := NewManager()
- defer m.Close()
- if err := m.Activate(ctx, install(t, "1.0.0", "")); err != nil {
- t.Fatal(err)
- }
- pid1, _ := search(t, m, "pid")
- if err := m.Activate(ctx, install(t, "1.1.0", "")); err != nil {
- t.Fatal(err)
- }
- pid2, err := search(t, m, "pid")
- if err != nil || pid2 == pid1 {
- t.Fatalf("after upgrade pid = %s (was %s), %v", pid2, pid1, err)
- }
-}
-
-func TestEgressProxyEnforcesThePolicy(t *testing.T) {
- upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- _, _ = w.Write([]byte("hello from " + r.Host))
- }))
- defer upstream.Close()
- p, err := startEgressProxy("acme.echo", []string{"api.allowed.test", "*.wild.test"}, t.Logf)
- if err != nil {
- t.Fatal(err)
- }
- defer p.Close()
- // Every name resolves to the test server; the policy is what differs.
- p.dial = func(ctx context.Context, network, _ string) (net.Conn, error) {
- var d net.Dialer
- return d.DialContext(ctx, network, upstream.Listener.Addr().String())
- }
- proxyURL, _ := url.Parse(p.URL())
- c := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
- for host, want := range map[string]int{
- "api.allowed.test": 200, "x.wild.test": 200, "wild.test": 403, "evil.test": 403,
- } {
- resp, err := c.Get("http://" + host + "/")
- if err != nil {
- t.Fatalf("%s: %v", host, err)
- }
- _ = resp.Body.Close()
- if resp.StatusCode != want {
- t.Errorf("%s: status %d, want %d", host, resp.StatusCode, want)
- }
- }
-}
-
-func TestEgressProxyTunnelsHTTPS(t *testing.T) {
- upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
- _, _ = w.Write([]byte("secure"))
- }))
- defer upstream.Close()
- p, err := startEgressProxy("acme.echo", []string{"*.wild.test"}, t.Logf)
- if err != nil {
- t.Fatal(err)
- }
- defer p.Close()
- p.dial = func(ctx context.Context, network, _ string) (net.Conn, error) {
- var d net.Dialer
- return d.DialContext(ctx, network, upstream.Listener.Addr().String())
- }
- proxyURL, _ := url.Parse(p.URL())
- tr := upstream.Client().Transport.(*http.Transport).Clone()
- tr.Proxy = http.ProxyURL(proxyURL)
- tr.TLSClientConfig.InsecureSkipVerify = true
- c := &http.Client{Transport: tr}
-
- resp, err := c.Get("https://api.wild.test/")
- if err != nil {
- t.Fatal(err)
- }
- body := make([]byte, 16)
- n, _ := resp.Body.Read(body)
- _ = resp.Body.Close()
- if string(body[:n]) != "secure" {
- t.Fatalf("tunnelled body = %q", body[:n])
- }
- if _, err := c.Get("https://evil.test/"); err == nil || !strings.Contains(err.Error(), "Forbidden") {
- t.Fatalf("CONNECT to a host outside the policy must be refused, got %v", err)
- }
-}
-
-func TestProtocolVersionsAgree(t *testing.T) {
- if manifest.ExtensionAPIVersion != pluginapi.APIVersion {
- t.Fatalf("manifest accepts %q, the protocol package speaks %q",
- manifest.ExtensionAPIVersion, pluginapi.APIVersion)
- }
-}
diff --git a/internal/plugin/host/limits.go b/internal/plugin/host/limits.go
deleted file mode 100644
index cec4ccea7..000000000
--- a/internal/plugin/host/limits.go
+++ /dev/null
@@ -1,40 +0,0 @@
-package host
-
-import (
- "os"
- "strings"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
-)
-
-// envMemoryDefault is the memory limit of plugins that declare none
-// (runtime.resources.memory), e.g. "1Gi". Unset: no limit. The cgroup
-// setting (envCgroup) is Linux-only.
-const envMemoryDefault = "WEKNORA_PLUGIN_MEMORY_DEFAULT"
-
-// limits is what a plugin process may use; zero means no limit.
-type limits struct {
- cpuMilli int64
- memoryBytes int64
-}
-
-// limitsFor reads a plugin's declared resources, with the platform default
-// memory for plugins that declare none. The manifest was validated, so
-// parse errors only mean "no limit".
-func limitsFor(m *manifest.Manifest) limits {
- var l limits
- if r := m.Runtime.Resources; r != nil {
- if r.CPU != "" {
- l.cpuMilli, _ = manifest.ParseCPU(r.CPU)
- }
- if r.Memory != "" {
- l.memoryBytes, _ = manifest.ParseMemory(r.Memory)
- }
- }
- if l.memoryBytes == 0 {
- if def := strings.TrimSpace(os.Getenv(envMemoryDefault)); def != "" {
- l.memoryBytes, _ = manifest.ParseMemory(def)
- }
- }
- return l
-}
diff --git a/internal/plugin/host/limits_linux.go b/internal/plugin/host/limits_linux.go
deleted file mode 100644
index d17cc3dc9..000000000
--- a/internal/plugin/host/limits_linux.go
+++ /dev/null
@@ -1,73 +0,0 @@
-//go:build linux
-
-package host
-
-import (
- "context"
- "fmt"
- "os"
- "path/filepath"
- "strconv"
- "strings"
-
- "golang.org/x/sys/unix"
-
- "github.com/Tencent/WeKnora/internal/logger"
-)
-
-// cpuPeriod is the cgroup cpu.max period, in microseconds.
-const cpuPeriod = 100000
-
-// envCgroup names a cgroup v2 directory delegated to WeKnora. Each plugin
-// process then runs in a child cgroup with memory.max and cpu.max set from
-// its resources. Unset: memory is capped with an rlimit only and CPU is not
-// capped.
-const envCgroup = "WEKNORA_PLUGIN_CGROUP"
-
-// applyLimits caps a started plugin process. Memory is capped with
-// RLIMIT_DATA, which needs no privileges and covers the heap and anonymous
-// mappings of Go, Python and Node; with a delegated cgroup, memory.max and
-// cpu.max apply as well. Failures are logged: an unlimited plugin still
-// runs. It returns a cleanup that removes the plugin's cgroup.
-
-func applyLimits(pid int, id string, l limits) func() {
- ctx := context.Background()
- if l.memoryBytes > 0 {
- lim := &unix.Rlimit{Cur: uint64(l.memoryBytes), Max: uint64(l.memoryBytes)}
- if err := unix.Prlimit(pid, unix.RLIMIT_DATA, lim, nil); err != nil {
- logger.Warnf(ctx, "[plugin] %s: cap memory: %v", id, err)
- }
- }
- root := strings.TrimSpace(os.Getenv(envCgroup))
- if root == "" || (l.memoryBytes == 0 && l.cpuMilli == 0) {
- return func() {}
- }
- dir := filepath.Join(root, fmt.Sprintf("plugin-%d", pid))
- if err := setupCgroup(dir, pid, l); err != nil {
- logger.Warnf(ctx, "[plugin] %s: cgroup %s: %v", id, dir, err)
- _ = os.Remove(dir)
- return func() {}
- }
- return func() { _ = os.Remove(dir) }
-}
-
-func setupCgroup(dir string, pid int, l limits) error {
- if err := os.Mkdir(dir, 0o755); err != nil && !os.IsExist(err) {
- return err
- }
- write := func(file, value string) error {
- return os.WriteFile(filepath.Join(dir, file), []byte(value), 0o644)
- }
- if l.memoryBytes > 0 {
- if err := write("memory.max", strconv.FormatInt(l.memoryBytes, 10)); err != nil {
- return err
- }
- }
- if l.cpuMilli > 0 {
- quota := l.cpuMilli * cpuPeriod / 1000
- if err := write("cpu.max", fmt.Sprintf("%d %d", quota, cpuPeriod)); err != nil {
- return err
- }
- }
- return write("cgroup.procs", strconv.Itoa(pid))
-}
diff --git a/internal/plugin/host/limits_linux_test.go b/internal/plugin/host/limits_linux_test.go
deleted file mode 100644
index a1968af05..000000000
--- a/internal/plugin/host/limits_linux_test.go
+++ /dev/null
@@ -1,56 +0,0 @@
-//go:build linux
-
-package host
-
-import (
- "fmt"
- "os"
- "os/exec"
- "path/filepath"
- "strings"
- "testing"
-)
-
-func TestApplyLimitsCapsMemory(t *testing.T) {
- cmd := exec.Command("sleep", "30")
- if err := cmd.Start(); err != nil {
- t.Skipf("no sleep: %v", err)
- }
- defer func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }()
-
- // A cgroup root we cannot write to is logged, not fatal.
- t.Setenv(envCgroup, filepath.Join(t.TempDir(), "not-a-cgroup"))
- release := applyLimits(cmd.Process.Pid, "acme.test", limits{memoryBytes: 64 << 20, cpuMilli: 500})
- defer release()
-
- raw, err := os.ReadFile(fmt.Sprintf("/proc/%d/limits", cmd.Process.Pid))
- if err != nil {
- t.Fatal(err)
- }
- for _, line := range strings.Split(string(raw), "\n") {
- if strings.HasPrefix(line, "Max data size") {
- if !strings.Contains(line, fmt.Sprint(64<<20)) {
- t.Fatalf("limit not applied: %s", line)
- }
- return
- }
- }
- t.Fatal("no data size limit line")
-}
-
-func TestSetupCgroupWritesLimits(t *testing.T) {
- // A plain directory stands in for a delegated cgroup: the files are
- // what the kernel would read.
- dir := filepath.Join(t.TempDir(), "plugin-1")
- if err := setupCgroup(dir, 4242, limits{memoryBytes: 1 << 30, cpuMilli: 1500}); err != nil {
- t.Fatal(err)
- }
- for file, want := range map[string]string{
- "memory.max": fmt.Sprint(1 << 30), "cpu.max": "150000 100000", "cgroup.procs": "4242",
- } {
- got, _ := os.ReadFile(filepath.Join(dir, file))
- if string(got) != want {
- t.Errorf("%s = %q, want %q", file, got, want)
- }
- }
-}
diff --git a/internal/plugin/host/limits_other.go b/internal/plugin/host/limits_other.go
deleted file mode 100644
index e27857d04..000000000
--- a/internal/plugin/host/limits_other.go
+++ /dev/null
@@ -1,6 +0,0 @@
-//go:build !linux
-
-package host
-
-// applyLimits is Linux only: elsewhere plugins run uncapped (desktop, dev).
-func applyLimits(int, string, limits) func() { return func() {} }
diff --git a/internal/plugin/host/limits_test.go b/internal/plugin/host/limits_test.go
deleted file mode 100644
index f28e3efe8..000000000
--- a/internal/plugin/host/limits_test.go
+++ /dev/null
@@ -1,25 +0,0 @@
-package host
-
-import (
- "testing"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
-)
-
-func TestLimitsFor(t *testing.T) {
- m := &manifest.Manifest{Runtime: manifest.Runtime{Resources: &manifest.Resources{CPU: "250m", Memory: "256Mi"}}}
- if l := limitsFor(m); l.cpuMilli != 250 || l.memoryBytes != 256<<20 {
- t.Fatalf("declared = %+v", l)
- }
- bare := &manifest.Manifest{}
- if l := limitsFor(bare); l != (limits{}) {
- t.Fatalf("no resources, no default = %+v", l)
- }
- t.Setenv(envMemoryDefault, "1Gi")
- if l := limitsFor(bare); l.memoryBytes != 1<<30 || l.cpuMilli != 0 {
- t.Fatalf("platform default = %+v", l)
- }
- if l := limitsFor(m); l.memoryBytes != 256<<20 {
- t.Fatalf("a declared limit wins over the default: %+v", l)
- }
-}
diff --git a/internal/plugin/host/proc_linux.go b/internal/plugin/host/proc_linux.go
deleted file mode 100644
index 1529fc109..000000000
--- a/internal/plugin/host/proc_linux.go
+++ /dev/null
@@ -1,16 +0,0 @@
-//go:build linux
-
-package host
-
-import (
- "os/exec"
- "syscall"
-)
-
-// configureChild puts the plugin in its own process group and has the kernel
-// kill it if WeKnora dies, so a crash never leaves orphaned plugins.
-func configureChild(cmd *exec.Cmd) {
- cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pdeathsig: syscall.SIGKILL}
-}
-
-func terminate(cmd *exec.Cmd) error { return cmd.Process.Signal(syscall.SIGTERM) }
diff --git a/internal/plugin/host/proc_unix.go b/internal/plugin/host/proc_unix.go
deleted file mode 100644
index 4007b020f..000000000
--- a/internal/plugin/host/proc_unix.go
+++ /dev/null
@@ -1,16 +0,0 @@
-//go:build unix && !linux
-
-package host
-
-import (
- "os/exec"
- "syscall"
-)
-
-// configureChild puts the plugin in its own process group, so a signal
-// meant for WeKnora's terminal does not reach it directly.
-func configureChild(cmd *exec.Cmd) {
- cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
-}
-
-func terminate(cmd *exec.Cmd) error { return cmd.Process.Signal(syscall.SIGTERM) }
diff --git a/internal/plugin/host/proc_windows.go b/internal/plugin/host/proc_windows.go
deleted file mode 100644
index 903de6b02..000000000
--- a/internal/plugin/host/proc_windows.go
+++ /dev/null
@@ -1,11 +0,0 @@
-//go:build windows
-
-package host
-
-import "os/exec"
-
-func configureChild(*exec.Cmd) {}
-
-// terminate kills the plugin: Windows has no SIGTERM for console-less
-// children.
-func terminate(cmd *exec.Cmd) error { return cmd.Process.Kill() }
diff --git a/internal/plugin/host/process.go b/internal/plugin/host/process.go
deleted file mode 100644
index ad9da8cf2..000000000
--- a/internal/plugin/host/process.go
+++ /dev/null
@@ -1,533 +0,0 @@
-package host
-
-import (
- "bufio"
- "context"
- "crypto/rand"
- "encoding/hex"
- "errors"
- "fmt"
- "io"
- "net"
- "os"
- "os/exec"
- "path"
- "path/filepath"
- "runtime"
- "strings"
- "sync"
- "time"
-
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/utils"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// Timing of a host plugin's life. Variables so tests can shrink them.
-var (
- handshakeTimeout = 30 * time.Second
- healthInterval = 15 * time.Second
- healthTimeout = 5 * time.Second
- unhealthyLimit = 3
- stopGrace = 10 * time.Second
- restartBackoffFloor = time.Second
- restartBackoffCeiling = time.Minute
-)
-
-// State of a supervised process.
-type State string
-
-// Process states reported to the reconciler.
-const (
- StateStarting State = "starting"
- StateReady State = "ready"
- StateDegraded State = "degraded"
- StateStopped State = "stopped"
-)
-
-// spec is everything needed to start one plugin version.
-type spec struct {
- m *manifest.Manifest
- dir string // extracted package
- // direct are hosts reached without the egress proxy.
- direct []string
-}
-
-// Kinds of host plugin this host runs.
-const (
- KindBinary = "binary"
- KindPython = "python"
-)
-
-// Supported reports whether this host runs a kind of host plugin.
-func Supported(kind string) bool { return kind == KindBinary || kind == KindPython }
-
-// PythonCommand is the interpreter python plugins run with:
-// WEKNORA_PLUGIN_PYTHON, or python3 (python on Windows) from PATH.
-func PythonCommand() string {
- if c := strings.TrimSpace(os.Getenv("WEKNORA_PLUGIN_PYTHON")); c != "" {
- return c
- }
- if runtime.GOOS == "windows" {
- return "python"
- }
- return "python3"
-}
-
-// EntryName is runtime.entry resolved for this machine: {os} and {arch}
-// filled in, and .exe added for Windows binaries.
-func EntryName(rt manifest.Runtime) string {
- rel := strings.NewReplacer("{os}", runtime.GOOS, "{arch}", runtime.GOARCH).Replace(rt.Entry)
- if rt.Kind == KindBinary && runtime.GOOS == "windows" && path.Ext(rel) == "" {
- rel += ".exe"
- }
- return rel
-}
-
-// entryPath resolves runtime.entry for this machine inside the package.
-func entryPath(m *manifest.Manifest, dir string) (string, error) {
- rel := EntryName(m.Runtime)
- p, err := utils.SafeJoinUnderBase(dir, rel)
- if err != nil {
- return "", fmt.Errorf("runtime.entry %q: %w", m.Runtime.Entry, err)
- }
- info, err := os.Stat(p)
- if err != nil {
- return "", fmt.Errorf("the package has no %s build (%s)", runtime.GOOS+"/"+runtime.GOARCH, rel)
- }
- if !info.Mode().IsRegular() {
- return "", fmt.Errorf("runtime.entry %s is not a file", rel)
- }
- if m.Runtime.Kind != KindBinary {
- return p, nil
- }
- // Packages are extracted without modes; the entry has to be executable.
- if err := os.Chmod(p, 0o755); err != nil {
- return "", err
- }
- return p, nil
-}
-
-// process is one running plugin version and its supervisor.
-type process struct {
- spec spec
- onState func(State, error)
-
- mu sync.RWMutex
- client *client.Client
- state State
- lastErr error
-
- cancel context.CancelFunc
- done chan struct{}
- proxy *egressProxy
- sockDir string
-}
-
-// launched is one started child.
-type launched struct {
- cmd *exec.Cmd
- client *client.Client
- exited chan error
-}
-
-// startProcess starts the plugin, waits until it is ready and keeps it
-// running until stop. It returns once the first start succeeded or failed.
-func startProcess(sp spec, onState func(State, error)) (*process, error) {
- entry, err := entryPath(sp.m, sp.dir)
- if err != nil {
- return nil, err
- }
- sockDir, err := socketDir()
- if err != nil {
- return nil, err
- }
- proxy, err := startEgressProxy(sp.m.ID, sp.m.Permissions.Egress, func(f string, a ...any) {
- logger.Warnf(context.Background(), f, a...)
- })
- if err != nil {
- _ = os.RemoveAll(sockDir)
- return nil, err
- }
- ctx, cancel := context.WithCancel(context.Background())
- p := &process{spec: sp, onState: onState, cancel: cancel, done: make(chan struct{}), proxy: proxy, sockDir: sockDir}
- p.setState(StateStarting, nil)
- first, err := p.launch(ctx, entry)
- if err != nil {
- cancel()
- proxy.Close()
- _ = os.RemoveAll(sockDir)
- close(p.done)
- p.setState(StateStopped, err)
- return nil, err
- }
- // Ready before Activate returns: callers route to the plugin right away.
- p.setClient(first.client)
- p.setState(StateReady, nil)
- go p.supervise(ctx, entry, first)
- return p, nil
-}
-
-// socketDir makes a short private directory for the socket: unix socket
-// paths are limited to about 100 bytes.
-func socketDir() (string, error) {
- base := os.Getenv("WEKNORA_PLUGIN_SOCKET_DIR")
- if base == "" {
- base = os.TempDir()
- }
- return os.MkdirTemp(base, "wkp")
-}
-
-func randomToken() string {
- b := make([]byte, 32)
- _, _ = rand.Read(b)
- return hex.EncodeToString(b)
-}
-
-// childEnv is the plugin's whole environment. It is built from scratch so
-// WeKnora's own secrets (database, AES key, vendor keys) never reach plugin
-// code.
-func (p *process) childEnv(network, socket, token string) []string {
- env := []string{
- pluginapi.EnvSocket + "=" + socket,
- pluginapi.EnvNetwork + "=" + network,
- pluginapi.EnvToken + "=" + token,
- pluginapi.EnvPluginID + "=" + p.spec.m.ID,
- pluginapi.EnvPluginVersion + "=" + p.spec.m.Version,
- "HOME=" + p.sockDir,
- "TMPDIR=" + p.sockDir,
- "HTTP_PROXY=" + p.proxy.URL(),
- "HTTPS_PROXY=" + p.proxy.URL(),
- "http_proxy=" + p.proxy.URL(),
- "https_proxy=" + p.proxy.URL(),
- }
- // The Host API is on this node's loopback, or on a host named direct;
- // the proxy is for the outside world.
- noProxy := strings.Join(append([]string{"127.0.0.1", "localhost", "::1"}, p.spec.direct...), ",")
- env = append(env, "NO_PROXY="+noProxy, "no_proxy="+noProxy)
- for _, k := range []string{"PATH", "LANG", "LC_ALL", "TZ", "SYSTEMROOT", "WINDIR"} {
- if v, ok := os.LookupEnv(k); ok {
- env = append(env, k+"="+v)
- }
- }
- if p.spec.m.Runtime.Kind == KindPython {
- // Dependencies are vendored into the package; the extracted package
- // is shared, so no bytecode is written into it.
- env = append(env,
- "PYTHONPATH="+filepath.Join(p.spec.dir, "vendor")+string(os.PathListSeparator)+p.spec.dir,
- "PYTHONDONTWRITEBYTECODE=1",
- "PYTHONIOENCODING=utf-8",
- )
- }
- return env
-}
-
-// listenAddress is where the plugin is told to listen: a private unix
-// socket, or a loopback port for Python on Windows, which has no unix
-// sockets.
-func (p *process) listenAddress() (network, address string) {
- if p.spec.m.Runtime.Kind == KindPython && runtime.GOOS == "windows" {
- return "tcp", "127.0.0.1:0"
- }
- return "unix", filepath.Join(p.sockDir, "p.sock")
-}
-
-// loopback reports whether a TCP handshake names this machine.
-func loopback(hs pluginapi.Handshake) bool {
- if hs.Network != "tcp" {
- return true
- }
- host, _, err := net.SplitHostPort(hs.Address)
- ip := net.ParseIP(host)
- return err == nil && ip != nil && ip.IsLoopback()
-}
-
-// command runs the entry: directly for binaries, with the interpreter for
-// python plugins (-s: without the user's site-packages, -u: unbuffered so
-// the handshake is not held back).
-func (p *process) command(entry string) *exec.Cmd {
- if p.spec.m.Runtime.Kind == KindPython {
- return exec.Command(PythonCommand(), "-s", "-u", entry)
- }
- return exec.Command(entry)
-}
-
-// launch starts the child and waits for handshake, health and a manifest
-// that matches the installed package.
-func (p *process) launch(ctx context.Context, entry string) (*launched, error) {
- network, socket := p.listenAddress()
- if network == "unix" {
- _ = os.Remove(socket)
- }
- token := randomToken()
- cmd := p.command(entry)
- cmd.Dir = p.spec.dir
- cmd.Env = p.childEnv(network, socket, token)
- configureChild(cmd)
- stdout, err := cmd.StdoutPipe()
- if err != nil {
- return nil, err
- }
- stderr, err := cmd.StderrPipe()
- if err != nil {
- return nil, err
- }
- if err := cmd.Start(); err != nil {
- return nil, fmt.Errorf("start %s: %w", filepath.Base(entry), err)
- }
- id := p.spec.m.ID + "@" + p.spec.m.Version
- releaseLimits := applyLimits(cmd.Process.Pid, id, limitsFor(p.spec.m))
- handshake := make(chan pluginapi.Handshake, 1)
- hsErr := make(chan error, 1)
- go forwardLogs(stderr, id, nil, nil)
- go forwardLogs(stdout, id, handshake, hsErr)
- exited := make(chan error, 1)
- go func() {
- err := cmd.Wait()
- releaseLimits()
- exited <- err
- }()
-
- kill := func() {
- stopChild(cmd, exited)
- }
- timer := time.NewTimer(handshakeTimeout)
- defer timer.Stop()
- var hs pluginapi.Handshake
- select {
- case hs = <-handshake:
- case err := <-hsErr:
- kill()
- return nil, err
- case err := <-exited:
- exited <- err // keep it for supervise
- return nil, fmt.Errorf("plugin exited before it was ready: %v", err)
- case <-timer.C:
- kill()
- return nil, fmt.Errorf("plugin did not print its handshake within %s", handshakeTimeout)
- case <-ctx.Done():
- kill()
- return nil, ctx.Err()
- }
- if hs.Network != network || (network == "unix" && hs.Address != socket) || !loopback(hs) {
- kill()
- return nil, fmt.Errorf("plugin listens on %s %s, not where it was told", hs.Network, hs.Address)
- }
- c := client.ForHandshake(hs, client.Bearer(token))
- cctx, cancel := context.WithTimeout(ctx, healthTimeout)
- defer cancel()
- if err := c.Health(cctx); err != nil {
- c.Close()
- kill()
- return nil, fmt.Errorf("plugin is not healthy: %w", err)
- }
- m, err := c.Manifest(cctx)
- if err != nil {
- c.Close()
- kill()
- return nil, fmt.Errorf("read plugin manifest: %w", err)
- }
- if err := CheckServedManifest(p.spec.m, m); err != nil {
- c.Close()
- kill()
- return nil, err
- }
- return &launched{cmd: cmd, client: c, exited: exited}, nil
-}
-
-// CheckServedManifest makes sure a running plugin is the package that was
-// installed and serves what the manifest promised.
-func CheckServedManifest(want *manifest.Manifest, got *pluginapi.Manifest) error {
- if got.ID != want.ID || got.Version != want.Version {
- return fmt.Errorf("plugin reports %s@%s, the package is %s@%s", got.ID, got.Version, want.ID, want.Version)
- }
- if got.APIVersion != pluginapi.APIVersion {
- return fmt.Errorf("plugin speaks %q, this WeKnora speaks %q", got.APIVersion, pluginapi.APIVersion)
- }
- if len(want.Permissions.Events) > 0 && len(got.Contributes["events"]) == 0 {
- return errors.New("plugin.yaml subscribes to events but the plugin handles none")
- }
- servesMCP := map[string]bool{}
- for _, id := range got.Contributes[string(manifest.PointMCPServers)] {
- servesMCP[id] = true
- }
- for _, c := range want.Contributes[manifest.PointMCPServers] {
- if c.ServedByPlugin() && !servesMCP[c.ID] {
- return fmt.Errorf("plugin.yaml declares the MCP server %s but the plugin does not serve it", c.ID)
- }
- }
- for point, contribs := range want.Contributes {
- if info, ok := manifest.LookupPoint(point); !ok || info.Declarative {
- continue
- }
- served := map[string]bool{}
- for _, id := range got.Contributes[string(point)] {
- served[id] = true
- }
- for _, c := range contribs {
- if !served[c.ID] {
- return fmt.Errorf("plugin.yaml declares %s/%s but the plugin does not serve it", point, c.ID)
- }
- }
- }
- return nil
-}
-
-// forwardLogs copies a child's output into WeKnora's logs. On stdout it
-// also watches for the handshake line.
-func forwardLogs(r io.Reader, id string, handshake chan<- pluginapi.Handshake, hsErr chan<- error) {
- sc := bufio.NewScanner(r)
- sc.Buffer(make([]byte, 0, 64<<10), 1<<20)
- seen := false
- for sc.Scan() {
- line := sc.Text()
- if handshake != nil && !seen {
- hs, ok, err := pluginapi.ParseHandshake(line)
- if ok {
- seen = true
- if err != nil {
- hsErr <- err
- } else {
- handshake <- hs
- }
- continue
- }
- }
- logger.Infof(context.Background(), "[plugin %s] %s", id, line)
- }
-}
-
-func (p *process) setState(s State, err error) {
- p.mu.Lock()
- changed := p.state != s || (err != nil) != (p.lastErr != nil)
- p.state, p.lastErr = s, err
- p.mu.Unlock()
- if changed && p.onState != nil {
- p.onState(s, err)
- }
-}
-
-func (p *process) setClient(c *client.Client) {
- p.mu.Lock()
- old := p.client
- p.client = c
- p.mu.Unlock()
- if old != nil && old != c {
- old.Close()
- }
-}
-
-// Client returns the client of the running child, or an unavailable error.
-func (p *process) Client() (*client.Client, error) {
- p.mu.RLock()
- defer p.mu.RUnlock()
- if p.client == nil || p.state != StateReady {
- msg := fmt.Sprintf("plugin %s is %s", p.spec.m.ID, p.state)
- if p.lastErr != nil {
- msg += ": " + p.lastErr.Error()
- }
- return nil, &pluginapi.Error{Code: pluginapi.CodeUnavailable, Message: msg, Retryable: true}
- }
- return p.client, nil
-}
-
-// supervise keeps the plugin running: health checks, restart with backoff
-// after a crash or repeated failed checks.
-func (p *process) supervise(ctx context.Context, entry string, cur *launched) {
- defer close(p.done)
- backoff := restartBackoffFloor
- for {
- p.setClient(cur.client)
- p.setState(StateReady, nil)
- exitErr := p.watch(ctx, cur)
- p.setClient(nil)
- if ctx.Err() != nil {
- p.setState(StateStopped, nil)
- return
- }
- logger.Warnf(ctx, "[plugin] %s stopped unexpectedly: %v; restarting in %s", p.spec.m.ID, exitErr, backoff)
- p.setState(StateDegraded, exitErr)
- for {
- select {
- case <-ctx.Done():
- p.setState(StateStopped, nil)
- return
- case <-time.After(backoff):
- }
- backoff = min(backoff*2, restartBackoffCeiling)
- next, err := p.launch(ctx, entry)
- if err == nil {
- cur = next
- backoff = restartBackoffFloor
- logger.Infof(ctx, "[plugin] %s restarted", p.spec.m.ID)
- break
- }
- if ctx.Err() != nil {
- p.setState(StateStopped, nil)
- return
- }
- logger.Warnf(ctx, "[plugin] %s restart failed: %v; next try in %s", p.spec.m.ID, err, backoff)
- p.setState(StateDegraded, err)
- }
- }
-}
-
-// watch returns when the child exits, fails its health checks, or ctx ends
-// (then it stops the child).
-func (p *process) watch(ctx context.Context, cur *launched) error {
- t := time.NewTicker(healthInterval)
- defer t.Stop()
- failures := 0
- for {
- select {
- case err := <-cur.exited:
- if err == nil {
- err = errors.New("exited")
- }
- return err
- case <-ctx.Done():
- stopChild(cur.cmd, cur.exited)
- return nil
- case <-t.C:
- hctx, cancel := context.WithTimeout(ctx, healthTimeout)
- err := cur.client.Health(hctx)
- cancel()
- if err == nil {
- failures = 0
- continue
- }
- failures++
- if failures >= unhealthyLimit {
- stopChild(cur.cmd, cur.exited)
- return fmt.Errorf("failed %d health checks: %w", failures, err)
- }
- }
- }
-}
-
-// stop ends the plugin and waits for it to exit.
-func (p *process) stop() {
- p.cancel()
- <-p.done
- p.proxy.Close()
- _ = os.RemoveAll(p.sockDir)
-}
-
-// stopChild asks the child to exit, then kills it after stopGrace. exited
-// is the channel its Wait result arrives on.
-func stopChild(cmd *exec.Cmd, exited chan error) {
- if cmd.Process == nil {
- return
- }
- _ = terminate(cmd)
- select {
- case err := <-exited:
- exited <- err
- case <-time.After(stopGrace):
- _ = cmd.Process.Kill()
- err := <-exited
- exited <- err
- }
-}
diff --git a/internal/plugin/host/proxy.go b/internal/plugin/host/proxy.go
deleted file mode 100644
index d5adb250b..000000000
--- a/internal/plugin/host/proxy.go
+++ /dev/null
@@ -1,192 +0,0 @@
-package host
-
-import (
- "context"
- "fmt"
- "io"
- "net"
- "net/http"
- "strings"
- "sync"
- "time"
-
- "github.com/Tencent/WeKnora/internal/utils"
-)
-
-// egressPolicy decides which hosts a plugin may reach: the manifest's
-// permissions.egress patterns ("api.example.com", "*.atlassian.net"), which
-// the system administrator accepted at install time.
-type egressPolicy struct {
- any bool // "*": every public host
- exact map[string]bool
- suffixes []string // ".atlassian.net"
-}
-
-func newEgressPolicy(patterns []string) egressPolicy {
- p := egressPolicy{exact: map[string]bool{}}
- for _, raw := range patterns {
- pat := strings.ToLower(strings.TrimSpace(raw))
- if pat == "" {
- continue
- }
- if pat == "*" {
- p.any = true
- continue
- }
- if rest, ok := strings.CutPrefix(pat, "*."); ok {
- p.suffixes = append(p.suffixes, "."+rest)
- continue
- }
- p.exact[pat] = true
- }
- return p
-}
-
-func (p egressPolicy) allows(host string) bool {
- if p.any {
- // The SSRF-safe dialer still refuses private addresses.
- return true
- }
- host = strings.ToLower(strings.TrimSuffix(host, "."))
- if p.exact[host] {
- return true
- }
- for _, s := range p.suffixes {
- if strings.HasSuffix(host, s) {
- return true
- }
- }
- return false
-}
-
-// egressProxy is the HTTP proxy a host plugin's traffic goes through
-// (HTTP_PROXY / HTTPS_PROXY in its environment). It forwards only to hosts
-// the plugin was granted and dials through the SSRF-safe dialer, so a
-// granted name that resolves to a private address is still refused unless
-// the operator whitelisted it. A plugin can ignore the proxy variables; on
-// Linux a network namespace can make the proxy the only way out.
-type egressProxy struct {
- pluginID string
- policy egressPolicy
- ln net.Listener
- srv *http.Server
- dial func(ctx context.Context, network, addr string) (net.Conn, error)
- logf func(format string, args ...any)
- wg sync.WaitGroup
-}
-
-func startEgressProxy(pluginID string, patterns []string, logf func(string, ...any)) (*egressProxy, error) {
- ln, err := net.Listen("tcp", "127.0.0.1:0")
- if err != nil {
- return nil, fmt.Errorf("start egress proxy: %w", err)
- }
- p := &egressProxy{
- pluginID: pluginID, policy: newEgressPolicy(patterns), ln: ln,
- dial: utils.SSRFSafeDialContext, logf: logf,
- }
- p.srv = &http.Server{Handler: p, ReadHeaderTimeout: 30 * time.Second}
- p.wg.Add(1)
- go func() {
- defer p.wg.Done()
- _ = p.srv.Serve(ln)
- }()
- return p, nil
-}
-
-// URL is the proxy address for the plugin's environment.
-func (p *egressProxy) URL() string { return "http://" + p.ln.Addr().String() }
-
-func (p *egressProxy) Close() {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- _ = p.srv.Shutdown(ctx)
- p.wg.Wait()
-}
-
-func (p *egressProxy) deny(w http.ResponseWriter, host string) {
- p.logf("[plugin] %s: egress to %s refused (not in permissions.egress)", p.pluginID, host)
- http.Error(w, fmt.Sprintf("egress to %s is not permitted for plugin %s", host, p.pluginID), http.StatusForbidden)
-}
-
-func (p *egressProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- if r.Method == http.MethodConnect {
- p.tunnel(w, r)
- return
- }
- if r.URL.Host == "" {
- http.Error(w, "not a proxy request", http.StatusBadRequest)
- return
- }
- host := r.URL.Hostname()
- if !p.policy.allows(host) {
- p.deny(w, host)
- return
- }
- out := r.Clone(r.Context())
- out.RequestURI = ""
- out.Header.Del("Proxy-Connection")
- out.Header.Del("Proxy-Authorization")
- tr := &http.Transport{DialContext: p.dial, ResponseHeaderTimeout: 2 * time.Minute}
- defer tr.CloseIdleConnections()
- resp, err := tr.RoundTrip(out)
- if err != nil {
- http.Error(w, err.Error(), http.StatusBadGateway)
- return
- }
- defer func() { _ = resp.Body.Close() }()
- for k, vs := range resp.Header {
- for _, v := range vs {
- w.Header().Add(k, v)
- }
- }
- w.WriteHeader(resp.StatusCode)
- _, _ = io.Copy(w, resp.Body)
-}
-
-// tunnel handles CONNECT, which HTTPS traffic uses: the proxy sees only the
-// host and port, which is what the policy needs.
-func (p *egressProxy) tunnel(w http.ResponseWriter, r *http.Request) {
- host, _, err := net.SplitHostPort(r.Host)
- if err != nil {
- http.Error(w, "bad CONNECT target", http.StatusBadRequest)
- return
- }
- if !p.policy.allows(host) {
- p.deny(w, host)
- return
- }
- ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
- upstream, err := p.dial(ctx, "tcp", r.Host)
- cancel()
- if err != nil {
- http.Error(w, err.Error(), http.StatusBadGateway)
- return
- }
- hj, ok := w.(http.Hijacker)
- if !ok {
- _ = upstream.Close()
- http.Error(w, "hijacking unsupported", http.StatusInternalServerError)
- return
- }
- client, buf, err := hj.Hijack()
- if err != nil {
- _ = upstream.Close()
- return
- }
- if _, err := client.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n")); err != nil {
- _ = upstream.Close()
- _ = client.Close()
- return
- }
- if buf.Reader.Buffered() > 0 {
- pending := make([]byte, buf.Reader.Buffered())
- _, _ = buf.Read(pending)
- _, _ = upstream.Write(pending)
- }
- go func() {
- _, _ = io.Copy(upstream, client)
- _ = upstream.Close()
- }()
- _, _ = io.Copy(client, upstream)
- _ = client.Close()
-}
diff --git a/internal/plugin/host/python_test.go b/internal/plugin/host/python_test.go
deleted file mode 100644
index 074f6066c..000000000
--- a/internal/plugin/host/python_test.go
+++ /dev/null
@@ -1,157 +0,0 @@
-package host
-
-import (
- "context"
- "io/fs"
- "os"
- "os/exec"
- "path/filepath"
- "strings"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// pythonEcho is the echo plugin written with the Python SDK.
-const pythonEcho = `import os
-from weknora_plugin import Plugin, SearchResult
-
-plugin = Plugin(os.environ["WEKNORA_PLUGIN_ID"], os.environ["WEKNORA_PLUGIN_VERSION"])
-
-
[email protected]_search("echo")
-def search(call, q):
- if q.query == "env":
- return [SearchResult(title=",".join(sorted(os.environ)), url="env")]
- if q.query == "crash":
- os._exit(3)
- if q.query == "pid":
- return [SearchResult(title=str(os.getpid()), url="pid")]
- return [SearchResult(title=q.query, url="echo")]
-
-
-if __name__ == "__main__":
- plugin.serve()
-`
-
-// installPython lays out an extracted python package: main.py and the SDK
-// vendored under vendor/, as a plugin author ships it.
-func installPython(t *testing.T) *reconcile.Loaded {
- t.Helper()
- if _, err := exec.LookPath(PythonCommand()); err != nil {
- t.Skipf("%s is not installed", PythonCommand())
- }
- dir := t.TempDir()
- sdk := filepath.Join("..", "..", "..", "pluginsdk", "python", "src", "weknora_plugin")
- err := filepath.WalkDir(sdk, func(p string, d fs.DirEntry, err error) error {
- if err != nil || d.IsDir() || filepath.Ext(p) != ".py" {
- return err
- }
- data, err := os.ReadFile(p)
- if err != nil {
- return err
- }
- target := filepath.Join(dir, "vendor", "weknora_plugin", filepath.Base(p))
- if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
- return err
- }
- return os.WriteFile(target, data, 0o644)
- })
- if err != nil {
- t.Fatal(err)
- }
- if err := os.WriteFile(filepath.Join(dir, "main.py"), []byte(pythonEcho), 0o644); err != nil {
- t.Fatal(err)
- }
- m := &manifest.Manifest{
- SchemaVersion: manifest.SchemaVersion, ID: "acme.echo", Version: "1.0.0", APIVersion: pluginapi.APIVersion,
- Name: manifest.Text("Echo", nil), Publisher: manifest.Publisher{ID: "acme"},
- Runtime: manifest.Runtime{Type: manifest.RuntimeHost, Kind: KindPython, Entry: "main.py"},
- Contributes: manifest.Contributions{
- manifest.PointWebSearch: {{ID: "echo", Name: manifest.Text("Echo", nil)}},
- },
- }
- return &reconcile.Loaded{Manifest: m, Dir: dir}
-}
-
-func TestHostRunsAPythonPlugin(t *testing.T) {
- fastTimings(t)
- t.Setenv("DB_PASSWORD", "must-not-leak")
- ctx := context.Background()
- m := NewManager()
- rep := &reports{}
- m.SetReporter(rep)
- defer m.Close()
-
- l := installPython(t)
- if err := m.Activate(ctx, l); err != nil {
- t.Fatalf("Activate: %v", err)
- }
- if got, err := search(t, m, "hello"); err != nil || got != "hello" {
- t.Fatalf("search = %q, %v", got, err)
- }
- env, err := search(t, m, "env")
- if err != nil {
- t.Fatal(err)
- }
- if strings.Contains(env, "DB_PASSWORD") || !strings.Contains(env, "PYTHONPATH") ||
- !strings.Contains(env, pluginapi.EnvToken) {
- t.Fatalf("plugin environment = %s", env)
- }
-
- pid1, _ := search(t, m, "pid")
- _, _ = search(t, m, "crash")
- waitFor(t, "restart", func() bool {
- pid, err := search(t, m, "pid")
- return err == nil && pid != pid1
- })
- if err := m.Deactivate(ctx, "acme.echo"); err != nil {
- t.Fatal(err)
- }
- if entries, _ := os.ReadDir(l.Dir); len(entries) != 2 {
- t.Fatalf("the plugin wrote into its package: %v", entries)
- }
-}
-
-func TestEntryName(t *testing.T) {
- py := manifest.Runtime{Kind: KindPython, Entry: "main.py"}
- if got := EntryName(py); got != "main.py" {
- t.Fatalf("python entry = %s", got)
- }
- bin := manifest.Runtime{Kind: KindBinary, Entry: "bin/{os}-{arch}/x"}
- if got := EntryName(bin); strings.Contains(got, "{") {
- t.Fatalf("binary entry = %s", got)
- }
- if !Supported(KindPython) || Supported("node") {
- t.Fatal("this host runs binaries and python")
- }
-}
-
-func TestKindsDecideWhereAPluginRuns(t *testing.T) {
- ctx := context.Background()
- l := install(t, "1.0.0", "")
-
- embedded := NewManager()
- embedded.SetKinds([]string{KindPython})
- defer embedded.Close()
- if err := embedded.Activate(ctx, l); err != nil || embedded.Local("acme.echo") {
- t.Fatalf("an app node hands other kinds on: %v", err)
- }
-
- standalone := NewStandaloneManager([]string{KindPython})
- defer standalone.Close()
- if err := standalone.Activate(ctx, l); err == nil || !strings.Contains(err.Error(), "does not run binary") {
- t.Fatalf("a standalone host refuses other kinds, got %v", err)
- }
-
- t.Setenv("WEKNORA_PLUGIN_EMBEDDED_KINDS", "none")
- if got := KindsFromEnv("WEKNORA_PLUGIN_EMBEDDED_KINDS"); len(got) != 0 {
- t.Fatalf("none = %v", got)
- }
- t.Setenv("WEKNORA_PLUGIN_EMBEDDED_KINDS", "binary, node")
- if got := KindsFromEnv("WEKNORA_PLUGIN_EMBEDDED_KINDS"); len(got) != 1 || got[0] != KindBinary {
- t.Fatalf("binary, node = %v", got)
- }
-}
diff --git a/internal/plugin/host/served_test.go b/internal/plugin/host/served_test.go
deleted file mode 100644
index 5ea347e42..000000000
--- a/internal/plugin/host/served_test.go
+++ /dev/null
@@ -1,30 +0,0 @@
-package host
-
-import (
- "strings"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-func TestServedManifestCoversOwnMCPServers(t *testing.T) {
- want := &manifest.Manifest{ID: "acme.tools", Version: "1.0.0", Contributes: manifest.Contributions{
- manifest.PointMCPServers: {
- {ID: "issues"},
- {ID: "remote", MCP: &manifest.MCPServer{URL: "https://mcp.example.com"}},
- },
- }}
- got := &pluginapi.Manifest{
- ID: "acme.tools", Version: "1.0.0", APIVersion: pluginapi.APIVersion,
- Contributes: map[string][]string{},
- }
- if err := CheckServedManifest(want, got); err == nil || !strings.Contains(err.Error(), "MCP server issues") {
- t.Fatalf("missing server = %v", err)
- }
- // Remote servers are not the plugin's to serve.
- got.Contributes["mcpServers"] = []string{"issues"}
- if err := CheckServedManifest(want, got); err != nil {
- t.Fatal(err)
- }
-}
diff --git a/internal/plugin/host/testdata/echoplugin/main.go b/internal/plugin/host/testdata/echoplugin/main.go
deleted file mode 100644
index c3e3d3ecd..000000000
--- a/internal/plugin/host/testdata/echoplugin/main.go
+++ /dev/null
@@ -1,44 +0,0 @@
-// Command echoplugin is a host plugin for the host package's tests. Its
-// behaviour is driven by the query it is asked and by a "version" file next
-// to it, so a test can install a package that lies about its version.
-package main
-
-import (
- "context"
- "log"
- "os"
- "sort"
- "strconv"
- "strings"
-
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-func main() {
- version := os.Getenv(pluginapi.EnvPluginVersion)
- if b, err := os.ReadFile("version"); err == nil {
- version = strings.TrimSpace(string(b))
- }
- p := pluginsdk.New(pluginsdk.Info{ID: os.Getenv(pluginapi.EnvPluginID), Version: version})
- p.WebSearch("echo", pluginsdk.WebSearchFunc(func(_ context.Context, _ *pluginsdk.Call, in pluginapi.SearchInput) (*pluginapi.SearchOutput, error) {
- switch in.Query {
- case "crash":
- os.Exit(3)
- case "env":
- var keys []string
- for _, kv := range os.Environ() {
- k, _, _ := strings.Cut(kv, "=")
- keys = append(keys, k)
- }
- sort.Strings(keys)
- return &pluginapi.SearchOutput{Results: []pluginapi.SearchResult{{Title: strings.Join(keys, ","), URL: "env"}}}, nil
- case "pid":
- return &pluginapi.SearchOutput{Results: []pluginapi.SearchResult{{Title: strconv.Itoa(os.Getpid()), URL: "pid"}}}, nil
- }
- return &pluginapi.SearchOutput{Results: []pluginapi.SearchResult{{Title: in.Query, URL: "echo"}}}, nil
- }))
- if err := p.Serve(); err != nil {
- log.Fatal(err)
- }
-}
diff --git a/internal/plugin/hostapi/hostapi_test.go b/internal/plugin/hostapi/hostapi_test.go
deleted file mode 100644
index cf5838a9a..000000000
--- a/internal/plugin/hostapi/hostapi_test.go
+++ /dev/null
@@ -1,111 +0,0 @@
-package hostapi
-
-import (
- "context"
- "net/http/httptest"
- "strings"
- "testing"
- "time"
-
- "github.com/gin-gonic/gin"
- "github.com/google/uuid"
- "github.com/stretchr/testify/require"
- "gorm.io/driver/sqlite"
- "gorm.io/gorm"
-
- "github.com/Tencent/WeKnora/internal/application/repository"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-func TestTokens(t *testing.T) {
- iss := NewIssuer([]byte("k"))
- tok, exp, err := iss.Issue("acme.x", "1.0.0", 7, []string{"kv"})
- require.NoError(t, err)
- require.WithinDuration(t, time.Now().Add(TokenTTL), exp, time.Second)
- c, err := iss.Verify(tok)
- require.NoError(t, err)
- require.Equal(t, uint64(7), c.TenantID)
- require.True(t, c.Has("kv"))
-
- _, err = NewIssuer([]byte("other")).Verify(tok)
- require.ErrorIs(t, err, ErrInvalidToken, "another key must not verify")
- later := NewIssuer([]byte("k"))
- later.now = func() time.Time { return time.Now().Add(TokenTTL + time.Minute) }
- _, err = later.Verify(tok)
- require.ErrorIs(t, err, ErrInvalidToken, "expired tokens must not verify")
- _, err = iss.Verify(tok[:len(tok)-2] + "xx")
- require.ErrorIs(t, err, ErrInvalidToken)
-}
-
-// hostCall is what a plugin's call would carry, pointing at a test server.
-func hostCall(t *testing.T, url string, iss *Issuer, tenant uint64, scopes ...string) *pluginsdk.Call {
- t.Helper()
- tok, _, err := iss.Issue("acme.x", "1.0.0", tenant, scopes)
- require.NoError(t, err)
- return &pluginsdk.Call{
- Context: pluginapi.Context{TenantID: tenant, Host: &pluginapi.HostAccess{URL: url, Token: tok}},
- }
-}
-
-func TestKVThroughTheSDK(t *testing.T) {
- gin.SetMode(gin.TestMode)
- db, err := gorm.Open(sqlite.Open("file:"+uuid.NewString()+"?mode=memory&cache=shared"), &gorm.Config{})
- require.NoError(t, err)
- require.NoError(t, db.AutoMigrate(&types.PluginKV{}))
- iss := NewIssuer([]byte("k"))
- h := NewHandler(iss, NewKV(repository.NewPluginKVRepository(db)))
- r := gin.New()
- h.Register(r)
- srv := httptest.NewServer(r)
- defer srv.Close()
- ctx := context.Background()
-
- host := hostCall(t, srv.URL, iss, 1, "kv").Host()
- require.NotNil(t, host)
- type cursor struct{ Page int }
- ok, err := host.KVGet(ctx, "cursor/a", &cursor{})
- require.NoError(t, err)
- require.False(t, ok)
- require.NoError(t, host.KVPut(ctx, "cursor/a", cursor{Page: 3}, 0))
- require.NoError(t, host.KVPut(ctx, "cursor/b", cursor{Page: 4}, time.Hour))
- var got cursor
- ok, err = host.KVGet(ctx, "cursor/a", &got)
- require.NoError(t, err)
- require.True(t, ok)
- require.Equal(t, 3, got.Page)
-
- page, err := host.KVList(ctx, "cursor/", "", 1)
- require.NoError(t, err)
- require.Len(t, page.Entries, 1)
- require.Equal(t, "cursor/a", page.Next)
- page, err = host.KVList(ctx, "cursor/", page.Next, 1)
- require.NoError(t, err)
- require.Equal(t, "cursor/b", page.Entries[0].Key)
- require.NotNil(t, page.Entries[0].ExpiresAt)
-
- // Another tenant sees nothing of tenant 1.
- other := hostCall(t, srv.URL, iss, 2, "kv").Host()
- ok, err = other.KVGet(ctx, "cursor/a", &got)
- require.NoError(t, err)
- require.False(t, ok)
-
- require.NoError(t, host.KVDelete(ctx, "cursor/a"))
- ok, _ = host.KVGet(ctx, "cursor/a", &got)
- require.False(t, ok)
-
- // Without the scope, or with a forged token, the call is refused.
- noScope := hostCall(t, srv.URL, iss, 1).Host()
- _, err = noScope.KVGet(ctx, "cursor/b", &got)
- require.ErrorContains(t, err, `not granted the "kv" scope`)
- forged := hostCall(t, srv.URL, NewIssuer([]byte("wrong")), 1, "kv").Host()
- _, err = forged.KVGet(ctx, "cursor/b", &got)
- require.ErrorContains(t, err, "invalid or expired")
-
- // Bad input is a bad request, not a crash.
- err = host.KVPut(ctx, strings.Repeat("k", 300), 1, 0)
- e, ok := pluginapi.AsError(err)
- require.True(t, ok)
- require.Equal(t, pluginapi.CodeBadRequest, e.Code)
-}
diff --git a/internal/plugin/hostapi/http.go b/internal/plugin/hostapi/http.go
deleted file mode 100644
index 423df13a6..000000000
--- a/internal/plugin/hostapi/http.go
+++ /dev/null
@@ -1,125 +0,0 @@
-package hostapi
-
-import (
- "encoding/json"
- "errors"
- "io"
- "net/http"
- "strconv"
- "strings"
-
- "github.com/gin-gonic/gin"
-
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// PathPrefix is where the Host API is served. The global auth middleware
-// lets it through; Handler authenticates plugin tokens itself.
-const PathPrefix = "/api/v1/plugin-host/"
-
-const claimsKey = "pluginHostClaims"
-
-// Handler serves the Host API.
-type Handler struct {
- issuer *Issuer
- kv *KV
-}
-
-// NewHandler creates the Host API handler.
-func NewHandler(issuer *Issuer, kv *KV) *Handler { return &Handler{issuer: issuer, kv: kv} }
-
-func writeErr(c *gin.Context, err error) {
- var e *pluginapi.Error
- if !errors.As(err, &e) {
- logger.Errorf(c.Request.Context(), "[plugin] host api %s: %v", c.Request.URL.Path, err)
- e = &pluginapi.Error{Code: pluginapi.CodeInternal, Message: "host api failed"}
- }
- c.AbortWithStatusJSON(e.Code.HTTPStatus(), pluginapi.ErrorBody{Error: *e})
-}
-
-// authenticate verifies the plugin token and the scope the route needs.
-func (h *Handler) authenticate(scope string) gin.HandlerFunc {
- return func(c *gin.Context) {
- token, ok := strings.CutPrefix(c.GetHeader("Authorization"), "Bearer ")
- if !ok || token == "" {
- writeErr(c, pluginapi.Errorf(pluginapi.CodeUnauthorized, "missing plugin token"))
- return
- }
- claims, err := h.issuer.Verify(token)
- if err != nil {
- writeErr(c, pluginapi.Errorf(pluginapi.CodeUnauthorized, "invalid or expired plugin token"))
- return
- }
- if !claims.Has(scope) {
- writeErr(
- c,
- pluginapi.Errorf(pluginapi.CodeUnauthorized, "the plugin was not granted the %q scope", scope),
- )
- return
- }
- c.Set(claimsKey, claims)
- c.Next()
- }
-}
-
-func claimsOf(c *gin.Context) *Claims {
- v, _ := c.Get(claimsKey)
- claims, _ := v.(*Claims)
- return claims
-}
-
-// Register mounts the Host API on the engine root.
-func (h *Handler) Register(r gin.IRouter) {
- g := r.Group(strings.TrimSuffix(PathPrefix, "/"))
- kv := g.Group("/kv", h.authenticate(ScopeKV))
- kv.GET("", h.kvGet)
- kv.PUT("", h.kvPut)
- kv.DELETE("", h.kvDelete)
- kv.GET("/list", h.kvList)
-}
-
-func (h *Handler) kvGet(c *gin.Context) {
- e, err := h.kv.Get(c.Request.Context(), claimsOf(c), c.Query("key"))
- if err != nil {
- writeErr(c, err)
- return
- }
- c.JSON(http.StatusOK, e)
-}
-
-func (h *Handler) kvPut(c *gin.Context) {
- var in pluginapi.KVPut
- body, err := io.ReadAll(io.LimitReader(c.Request.Body, pluginapi.KVMaxValueBytes+pluginapi.KVMaxKeyBytes+1024))
- if err == nil {
- err = json.Unmarshal(body, &in)
- }
- if err != nil {
- writeErr(c, pluginapi.Errorf(pluginapi.CodeBadRequest, "body must be {key, value, ttlSeconds}"))
- return
- }
- e, err := h.kv.Put(c.Request.Context(), claimsOf(c), in)
- if err != nil {
- writeErr(c, err)
- return
- }
- c.JSON(http.StatusOK, e)
-}
-
-func (h *Handler) kvDelete(c *gin.Context) {
- if err := h.kv.Delete(c.Request.Context(), claimsOf(c), c.Query("key")); err != nil {
- writeErr(c, err)
- return
- }
- c.Status(http.StatusNoContent)
-}
-
-func (h *Handler) kvList(c *gin.Context) {
- limit, _ := strconv.Atoi(c.Query("limit"))
- out, err := h.kv.List(c.Request.Context(), claimsOf(c), c.Query("prefix"), c.Query("after"), limit)
- if err != nil {
- writeErr(c, err)
- return
- }
- c.JSON(http.StatusOK, out)
-}
diff --git a/internal/plugin/hostapi/kv.go b/internal/plugin/hostapi/kv.go
deleted file mode 100644
index c5ebed55b..000000000
--- a/internal/plugin/hostapi/kv.go
+++ /dev/null
@@ -1,162 +0,0 @@
-package hostapi
-
-import (
- "context"
- "encoding/json"
- "strings"
- "time"
- "unicode"
- "unicode/utf8"
-
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// ScopeKV grants a plugin its own key-value store in the calling tenant.
-const ScopeKV = "kv"
-
-// KV is the plugins' key-value store. Every operation is confined to the
-// plugin and tenant of the token, so a plugin never sees another plugin's
-// or another workspace's data.
-type KV struct {
- repo interfaces.PluginKVRepository
- now func() time.Time
-}
-
-// NewKV creates the store.
-func NewKV(repo interfaces.PluginKVRepository) *KV { return &KV{repo: repo, now: time.Now} }
-
-func checkKey(key string) error {
- switch {
- case key == "":
- return pluginapi.Errorf(pluginapi.CodeBadRequest, "key is required")
- case len(key) > pluginapi.KVMaxKeyBytes:
- return pluginapi.Errorf(pluginapi.CodeBadRequest, "key is over %d bytes", pluginapi.KVMaxKeyBytes)
- case !utf8.ValidString(key) || strings.ContainsFunc(key, unicode.IsControl):
- return pluginapi.Errorf(pluginapi.CodeBadRequest, "key must be printable UTF-8")
- }
- return nil
-}
-
-func toEntry(e *types.PluginKV) pluginapi.KVEntry {
- return pluginapi.KVEntry{
- Key: e.Key,
- Value: json.RawMessage(e.Value),
- ExpiresAt: e.ExpiresAt,
- UpdatedAt: e.UpdatedAt,
- }
-}
-
-// Get reads a key.
-func (s *KV) Get(ctx context.Context, c *Claims, key string) (*pluginapi.KVEntry, error) {
- if err := checkKey(key); err != nil {
- return nil, err
- }
- e, err := s.repo.Get(ctx, c.PluginID, c.TenantID, key)
- if err != nil {
- return nil, err
- }
- if e == nil {
- return nil, pluginapi.Errorf(pluginapi.CodeNotFound, "no key %q", key)
- }
- out := toEntry(e)
- return &out, nil
-}
-
-// Put writes a key, refusing new keys past the per-tenant quota.
-func (s *KV) Put(ctx context.Context, c *Claims, in pluginapi.KVPut) (*pluginapi.KVEntry, error) {
- if err := checkKey(in.Key); err != nil {
- return nil, err
- }
- if len(in.Value) == 0 || !json.Valid(in.Value) {
- return nil, pluginapi.Errorf(pluginapi.CodeBadRequest, "value must be JSON")
- }
- if len(in.Value) > pluginapi.KVMaxValueBytes {
- return nil, pluginapi.Errorf(pluginapi.CodeBadRequest, "value is over %d bytes", pluginapi.KVMaxValueBytes)
- }
- if in.TTLSeconds < 0 {
- return nil, pluginapi.Errorf(pluginapi.CodeBadRequest, "ttlSeconds must not be negative")
- }
- existing, err := s.repo.Get(ctx, c.PluginID, c.TenantID, in.Key)
- if err != nil {
- return nil, err
- }
- if existing == nil {
- n, err := s.repo.Count(ctx, c.PluginID, c.TenantID)
- if err != nil {
- return nil, err
- }
- if n >= pluginapi.KVMaxKeys {
- return nil, pluginapi.Errorf(
- pluginapi.CodeRateLimited,
- "the store holds its maximum of %d keys",
- pluginapi.KVMaxKeys,
- )
- }
- }
- now := s.now()
- e := &types.PluginKV{
- PluginID: c.PluginID, TenantID: c.TenantID, Key: in.Key, Value: types.JSON(in.Value), UpdatedAt: now,
- }
- if in.TTLSeconds > 0 {
- exp := now.Add(time.Duration(in.TTLSeconds) * time.Second)
- e.ExpiresAt = &exp
- }
- if err := s.repo.Put(ctx, e); err != nil {
- return nil, err
- }
- out := toEntry(e)
- return &out, nil
-}
-
-// Delete removes a key; deleting a missing key is not an error.
-func (s *KV) Delete(ctx context.Context, c *Claims, key string) error {
- if err := checkKey(key); err != nil {
- return err
- }
- _, err := s.repo.Delete(ctx, c.PluginID, c.TenantID, key)
- return err
-}
-
-// List pages through keys with a prefix.
-func (s *KV) List(ctx context.Context, c *Claims, prefix, after string, limit int) (*pluginapi.KVList, error) {
- if limit <= 0 {
- limit = 100
- }
- limit = min(limit, pluginapi.KVMaxListLimit)
- rows, err := s.repo.List(ctx, c.PluginID, c.TenantID, prefix, after, limit+1)
- if err != nil {
- return nil, err
- }
- out := &pluginapi.KVList{Entries: []pluginapi.KVEntry{}}
- for i := range rows {
- if i == limit {
- out.Next = rows[i-1].Key
- break
- }
- out.Entries = append(out.Entries, toEntry(&rows[i]))
- }
- return out, nil
-}
-
-// StartSweeper deletes expired entries every interval until ctx ends.
-func (s *KV) StartSweeper(ctx context.Context, interval time.Duration) {
- go func() {
- t := time.NewTicker(interval)
- defer t.Stop()
- for {
- select {
- case <-ctx.Done():
- return
- case <-t.C:
- if n, err := s.repo.DeleteExpired(ctx, s.now()); err != nil {
- logger.Warnf(ctx, "[plugin] sweep expired kv: %v", err)
- } else if n > 0 {
- logger.Infof(ctx, "[plugin] swept %d expired kv entries", n)
- }
- }
- }
- }()
-}
diff --git a/internal/plugin/hostapi/token.go b/internal/plugin/hostapi/token.go
deleted file mode 100644
index 4449166e1..000000000
--- a/internal/plugin/hostapi/token.go
+++ /dev/null
@@ -1,110 +0,0 @@
-// Package hostapi is the Host API: how a code plugin calls back into
-// WeKnora. Every call to a plugin that was granted Host API scopes carries a
-// short-lived token bound to the plugin, the tenant and those scopes; the
-// plugin presents it to /api/v1/plugin-host/*.
-package hostapi
-
-import (
- "crypto/hmac"
- "crypto/rand"
- "crypto/sha256"
- "errors"
- "fmt"
- "os"
- "slices"
- "strings"
- "time"
-
- "github.com/golang-jwt/jwt/v5"
-
- "github.com/Tencent/WeKnora/internal/utils"
-)
-
-// Audience of Host API tokens; they are refused anywhere else.
-const Audience = "weknora-host-api"
-
-// TokenTTL is how long a token lives: one call and its follow-ups.
-const TokenTTL = 5 * time.Minute
-
-// Claims are what a Host API token asserts.
-type Claims struct {
- PluginID string `json:"plugin"`
- Version string `json:"ver"`
- TenantID uint64 `json:"tenant"`
- Scopes []string `json:"scopes"`
- jwt.RegisteredClaims
-}
-
-// Has reports whether the token grants a scope.
-func (c *Claims) Has(scope string) bool { return slices.Contains(c.Scopes, scope) }
-
-// Issuer signs and verifies Host API tokens.
-type Issuer struct {
- key []byte
- now func() time.Time
-}
-
-// NewIssuer signs with key. Nodes must share it for a token issued on one
-// node to verify on another.
-func NewIssuer(key []byte) *Issuer { return &Issuer{key: key, now: time.Now} }
-
-// NewIssuerFromEnv derives the signing key from the cluster-wide secrets:
-// SYSTEM_AES_KEY, else JWT_SECRET. Without either it uses a random key, which
-// only works while plugins call back the node that called them (the
-// embedded host always does).
-func NewIssuerFromEnv() *Issuer {
- var secret []byte
- switch {
- case utils.GetAESKey() != nil:
- secret = utils.GetAESKey()
- case strings.TrimSpace(os.Getenv("JWT_SECRET")) != "":
- secret = []byte(strings.TrimSpace(os.Getenv("JWT_SECRET")))
- default:
- secret = make([]byte, 32)
- _, _ = rand.Read(secret)
- }
- mac := hmac.New(sha256.New, secret)
- _, _ = mac.Write([]byte("weknora plugin host api tokens"))
- return NewIssuer(mac.Sum(nil))
-}
-
-// Issue signs a token for one call.
-func (i *Issuer) Issue(pluginID, version string, tenantID uint64, scopes []string) (string, time.Time, error) {
- now := i.now()
- exp := now.Add(TokenTTL)
- claims := Claims{
- PluginID: pluginID, Version: version, TenantID: tenantID, Scopes: scopes,
- RegisteredClaims: jwt.RegisteredClaims{
- Subject: "plugin:" + pluginID,
- Audience: jwt.ClaimStrings{Audience},
- IssuedAt: jwt.NewNumericDate(now),
- NotBefore: jwt.NewNumericDate(now.Add(-30 * time.Second)),
- ExpiresAt: jwt.NewNumericDate(exp),
- },
- }
- tok, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString(i.key)
- return tok, exp, err
-}
-
-// ErrInvalidToken is any token that does not verify.
-var ErrInvalidToken = errors.New("invalid plugin token")
-
-// Verify checks a token's signature, audience and lifetime.
-func (i *Issuer) Verify(token string) (*Claims, error) {
- var c Claims
- parsed, err := jwt.ParseWithClaims(token, &c, func(*jwt.Token) (any, error) {
- return i.key, nil
- },
- jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}),
- jwt.WithAudience(Audience),
- jwt.WithExpirationRequired(),
- jwt.WithTimeFunc(i.now),
- )
- if err != nil || !parsed.Valid {
- return nil, fmt.Errorf("%w: %v", ErrInvalidToken, err)
- }
- if c.PluginID == "" || c.TenantID == 0 || c.Subject != "plugin:"+c.PluginID {
- return nil, fmt.Errorf("%w: incomplete claims", ErrInvalidToken)
- }
- return &c, nil
-}
diff --git a/internal/plugin/hostpool/hostpool.go b/internal/plugin/hostpool/hostpool.go
deleted file mode 100644
index b69d69509..000000000
--- a/internal/plugin/hostpool/hostpool.go
+++ /dev/null
@@ -1,261 +0,0 @@
-// Package hostpool connects app nodes to standalone plugin hosts
-// (weknora plugin-host) without Kubernetes: each host announces itself and
-// the plugins it runs in Redis, and app nodes call the least busy host that
-// runs the version they need, through the host's signed gateway.
-package hostpool
-
-import (
- "context"
- "crypto/hmac"
- "crypto/sha256"
- "encoding/json"
- "errors"
- "fmt"
- "math/rand/v2"
- "net/http"
- "os"
- "runtime"
- "strings"
- "sync"
- "time"
-
- "github.com/redis/go-redis/v9"
-
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/host"
- "github.com/Tencent/WeKnora/internal/utils"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// Heartbeat timing: a host refreshes its record every HeartbeatInterval and
-// drops out HeartbeatTTL after its last one.
-const (
- HeartbeatInterval = 5 * time.Second
- HeartbeatTTL = 15 * time.Second
- // refreshAfter is how long an app node trusts its view of the hosts.
- refreshAfter = 2 * time.Second
-)
-
-const keyBase = "weknora:plugin-host:"
-
-func keyPrefix() string {
- if ns := strings.TrimSpace(os.Getenv("WEKNORA_REDIS_NAMESPACE")); ns != "" {
- return keyBase + ns + ":"
- }
- return keyBase
-}
-
-// ErrNoClusterKey means neither SYSTEM_AES_KEY nor JWT_SECRET is set, so
-// app nodes and plugin hosts share no secret to sign calls with.
-var ErrNoClusterKey = errors.New("plugin hosts need SYSTEM_AES_KEY or JWT_SECRET, the same on every node")
-
-// ClusterKey is the key app nodes sign gateway calls with, derived from the
-// secrets every node already shares.
-func ClusterKey() ([]byte, error) {
- var secret []byte
- switch {
- case utils.GetAESKey() != nil:
- secret = utils.GetAESKey()
- case strings.TrimSpace(os.Getenv("JWT_SECRET")) != "":
- secret = []byte(strings.TrimSpace(os.Getenv("JWT_SECRET")))
- default:
- return nil, ErrNoClusterKey
- }
- mac := hmac.New(sha256.New, secret)
- _, _ = mac.Write([]byte("weknora plugin host gateway"))
- return mac.Sum(nil), nil
-}
-
-// Info is one plugin host's announcement.
-type Info struct {
- ID string `json:"id"`
- URL string `json:"url"` // gateway base URL, reachable from app nodes
- OS string `json:"os"`
- // Arch and Kinds say what the host can run.
- Arch string `json:"arch"`
- Kinds []string `json:"kinds"`
- Plugins []host.Running `json:"plugins"`
- InFlight int64 `json:"inFlight"`
- UpdatedAt time.Time `json:"updatedAt"`
-}
-
-// runs reports whether the host serves a plugin version right now.
-func (i Info) runs(pluginID, version string) bool {
- for _, p := range i.Plugins {
- if p.ID == pluginID && p.Version == version && p.State == host.StateReady {
- return true
- }
- }
- return false
-}
-
-// Announcer keeps a plugin host's record in Redis.
-type Announcer struct {
- rdb *redis.Client
- mgr *host.Manager
- id string
- url string
-}
-
-// NewAnnouncer announces the host at url (its gateway, as app nodes reach
-// it) under id.
-func NewAnnouncer(rdb *redis.Client, mgr *host.Manager, id, url string) *Announcer {
- return &Announcer{rdb: rdb, mgr: mgr, id: id, url: strings.TrimSuffix(url, "/")}
-}
-
-func (a *Announcer) info() Info {
- return Info{
- ID: a.id, URL: a.url, OS: runtime.GOOS, Arch: runtime.GOARCH, Kinds: a.mgr.Kinds(),
- Plugins: a.mgr.Running(), InFlight: a.mgr.InFlight(), UpdatedAt: time.Now(),
- }
-}
-
-// Announce writes the record once.
-func (a *Announcer) Announce(ctx context.Context) error {
- b, err := json.Marshal(a.info())
- if err != nil {
- return err
- }
- return a.rdb.Set(ctx, keyPrefix()+a.id, b, HeartbeatTTL).Err()
-}
-
-// Run announces every HeartbeatInterval until ctx ends, then withdraws the
-// record so app nodes stop calling at once.
-func (a *Announcer) Run(ctx context.Context) {
- t := time.NewTicker(HeartbeatInterval)
- defer t.Stop()
- for {
- if err := a.Announce(ctx); err != nil && ctx.Err() == nil {
- logger.Warnf(ctx, "[plugin-host] heartbeat: %v", err)
- }
- select {
- case <-ctx.Done():
- wctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
- _ = a.rdb.Del(wctx, keyPrefix()+a.id).Err()
- cancel()
- return
- case <-t.C:
- }
- }
-}
-
-// Pool is an app node's view of the plugin hosts.
-type Pool struct {
- rdb *redis.Client
- key []byte
- http *http.Client
- now func() time.Time
-
- mu sync.Mutex
- hosts []Info
- fetched time.Time
- clients map[string]*client.Client
-}
-
-// NewPool reads plugin hosts from Redis and signs calls with key.
-func NewPool(rdb *redis.Client, key []byte) *Pool {
- return &Pool{
- rdb: rdb, key: key, now: time.Now, clients: map[string]*client.Client{},
- // Calls are bounded by their context; syncs stream for long.
- http: &http.Client{Transport: &http.Transport{MaxIdleConnsPerHost: 32, IdleConnTimeout: 90 * time.Second}},
- }
-}
-
-// Hosts lists the live plugin hosts.
-func (p *Pool) Hosts(ctx context.Context) ([]Info, error) {
- p.mu.Lock()
- defer p.mu.Unlock()
- if p.hosts != nil && p.now().Sub(p.fetched) < refreshAfter {
- return p.hosts, nil
- }
- hosts, err := p.fetch(ctx)
- if err != nil {
- if p.hosts != nil {
- logger.Warnf(ctx, "[plugin] read plugin hosts, keeping the last view: %v", err)
- return p.hosts, nil
- }
- return nil, err
- }
- p.hosts, p.fetched = hosts, p.now()
- return hosts, nil
-}
-
-func (p *Pool) fetch(ctx context.Context) ([]Info, error) {
- var keys []string
- iter := p.rdb.Scan(ctx, 0, keyPrefix()+"*", 100).Iterator()
- for iter.Next(ctx) {
- keys = append(keys, iter.Val())
- }
- if err := iter.Err(); err != nil {
- return nil, err
- }
- hosts := []Info{}
- if len(keys) == 0 {
- return hosts, nil
- }
- vals, err := p.rdb.MGet(ctx, keys...).Result()
- if err != nil {
- return nil, err
- }
- for _, v := range vals {
- s, ok := v.(string)
- if !ok {
- continue
- }
- var info Info
- if json.Unmarshal([]byte(s), &info) == nil && info.URL != "" &&
- p.now().Sub(info.UpdatedAt) < HeartbeatTTL {
- hosts = append(hosts, info)
- }
- }
- return hosts, nil
-}
-
-// Client reaches a plugin version on the least busy host that runs it.
-func (p *Pool) Client(ctx context.Context, pluginID, version string) (*client.Client, error) {
- hosts, err := p.Hosts(ctx)
- if err != nil {
- return nil, pluginapi.Errorf(pluginapi.CodeUnavailable, "read plugin hosts: %v", err)
- }
- var best []Info
- for _, h := range hosts {
- if !h.runs(pluginID, version) {
- continue
- }
- switch {
- case len(best) == 0 || h.InFlight < best[0].InFlight:
- best = []Info{h}
- case h.InFlight == best[0].InFlight:
- best = append(best, h)
- }
- }
- if len(best) == 0 {
- return nil, pluginapi.Errorf(pluginapi.CodeUnavailable,
- "no plugin host runs %s@%s; is weknora plugin-host up?", pluginID, version)
- }
- h := best[rand.IntN(len(best))] //nolint:gosec // load spreading, not security
- base := fmt.Sprintf("%s%s%s/%s", h.URL, host.GatewayPrefix, pluginID, version)
- p.mu.Lock()
- defer p.mu.Unlock()
- c, ok := p.clients[base]
- if !ok {
- c = client.New(base, p.http, client.Signed(p.key))
- p.clients[base] = c
- }
- return c, nil
-}
-
-// Runs reports whether some live host runs a plugin version.
-func (p *Pool) Runs(ctx context.Context, pluginID, version string) bool {
- hosts, err := p.Hosts(ctx)
- if err != nil {
- return false
- }
- for _, h := range hosts {
- if h.runs(pluginID, version) {
- return true
- }
- }
- return false
-}
diff --git a/internal/plugin/hostpool/hostpool_test.go b/internal/plugin/hostpool/hostpool_test.go
deleted file mode 100644
index c7c947f8a..000000000
--- a/internal/plugin/hostpool/hostpool_test.go
+++ /dev/null
@@ -1,179 +0,0 @@
-package hostpool
-
-import (
- "context"
- "net/http"
- "net/http/httptest"
- "os/exec"
- "path/filepath"
- "runtime"
- "testing"
- "time"
-
- "github.com/alicebob/miniredis/v2"
- "github.com/redis/go-redis/v9"
-
- "github.com/Tencent/WeKnora/internal/plugin/host"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// echoPackage builds the host package's echo test plugin into an extracted
-// package directory.
-func echoPackage(t *testing.T) *reconcile.Loaded {
- t.Helper()
- dir := t.TempDir()
- name := "echo"
- if runtime.GOOS == "windows" {
- name += ".exe"
- }
- bin := filepath.Join(dir, "bin", runtime.GOOS+"-"+runtime.GOARCH, name)
- out, err := exec.Command("go", "build", "-o", bin, "../host/testdata/echoplugin").CombinedOutput()
- if err != nil {
- t.Fatalf("build echo plugin: %v\n%s", err, out)
- }
- m := &manifest.Manifest{
- SchemaVersion: manifest.SchemaVersion, ID: "acme.echo", Version: "1.0.0", APIVersion: pluginapi.APIVersion,
- Name: manifest.Text("Echo", nil), Publisher: manifest.Publisher{ID: "acme"},
- Runtime: manifest.Runtime{Type: manifest.RuntimeHost, Kind: host.KindBinary, Entry: "bin/{os}-{arch}/echo"},
- Contributes: manifest.Contributions{
- manifest.PointWebSearch: {{ID: "echo", Name: manifest.Text("Echo", nil)}},
- },
- }
- return &reconcile.Loaded{Manifest: m, Dir: dir}
-}
-
-func search(ctx context.Context, c *client.Client, q string) (string, error) {
- var out pluginapi.SearchOutput
- err := c.Call(ctx, pluginapi.SearchPath("echo"), pluginapi.Envelope{}, pluginapi.SearchInput{Query: q}, &out)
- if err != nil {
- return "", err
- }
- return out.Results[0].Title, nil
-}
-
-func TestPoolReachesAPluginThroughAHost(t *testing.T) {
- ctx := context.Background()
- mr := miniredis.RunT(t)
- rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
- t.Setenv("SYSTEM_AES_KEY", "0123456789abcdef0123456789abcdef")
- key, err := ClusterKey()
- if err != nil {
- t.Fatal(err)
- }
-
- // The plugin host: a manager running the plugin behind its gateway.
- mgr := host.NewManager()
- defer mgr.Close()
- if err := mgr.Activate(ctx, echoPackage(t)); err != nil {
- t.Fatal(err)
- }
- gw := httptest.NewServer(mgr.Gateway(key))
- defer gw.Close()
- a := NewAnnouncer(rdb, mgr, "h1", gw.URL+"/")
- if err := a.Announce(ctx); err != nil {
- t.Fatal(err)
- }
-
- pool := NewPool(rdb, key)
- c, err := pool.Client(ctx, "acme.echo", "1.0.0")
- if err != nil {
- t.Fatal(err)
- }
- if got, err := search(ctx, c, "hello"); err != nil || got != "hello" {
- t.Fatalf("search via host = %q, %v", got, err)
- }
- if !pool.Runs(ctx, "acme.echo", "1.0.0") || pool.Runs(ctx, "acme.echo", "2.0.0") {
- t.Fatal("Runs should match the running version only")
- }
- if _, err := pool.Client(ctx, "acme.echo", "2.0.0"); !isCode(err, pluginapi.CodeUnavailable) {
- t.Fatalf("other version = %v", err)
- }
-
- // The gateway refuses calls without the cluster key, and relays a
- // version it does not run as unavailable.
- unsigned := client.New(gw.URL+"/p/acme.echo/1.0.0", nil, nil)
- if _, err := search(ctx, unsigned, "x"); !isCode(err, pluginapi.CodeUnauthorized) {
- t.Fatalf("unsigned = %v", err)
- }
- stale := client.New(gw.URL+"/p/acme.echo/0.9.0", nil, client.Signed(key))
- if _, err := search(ctx, stale, "x"); !isCode(err, pluginapi.CodeUnavailable) {
- t.Fatalf("stale version = %v", err)
- }
- resp, err := http.Get(gw.URL + "/healthz")
- if err != nil || resp.StatusCode != http.StatusOK {
- t.Fatalf("healthz = %v, %v", resp, err)
- }
- _ = resp.Body.Close()
-}
-
-func TestHostsAgeOutAndWithdraw(t *testing.T) {
- ctx := context.Background()
- mr := miniredis.RunT(t)
- rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
- mgr := host.NewManager()
- pool := NewPool(rdb, []byte("k"))
- now := time.Now()
- pool.now = func() time.Time { return now }
-
- runCtx, stop := context.WithCancel(ctx)
- done := make(chan struct{})
- go func() { NewAnnouncer(rdb, mgr, "h1", "http://h1:8081").Run(runCtx); close(done) }()
- waitFor(t, func() bool { return mr.Exists(keyPrefix() + "h1") })
- hosts, err := pool.Hosts(ctx)
- if err != nil || len(hosts) != 1 || hosts[0].URL != "http://h1:8081" || len(hosts[0].Kinds) == 0 {
- t.Fatalf("hosts = %+v, %v", hosts, err)
- }
-
- // A host that stops announcing drops out when its record expires.
- stop()
- <-done
- if mr.Exists(keyPrefix() + "h1") {
- t.Fatal("a stopped host must withdraw its record")
- }
- now = now.Add(refreshAfter)
- if hosts, _ := pool.Hosts(ctx); len(hosts) != 0 {
- t.Fatalf("hosts after withdrawal = %+v", hosts)
- }
-
- // A record that outlived its heartbeat (clock skew, a stuck host) is
- // ignored even if Redis still has it.
- _ = NewAnnouncer(rdb, mgr, "h2", "http://h2:8081").Announce(ctx)
- now = now.Add(refreshAfter + HeartbeatTTL)
- if hosts, _ := pool.Hosts(ctx); len(hosts) != 0 {
- t.Fatalf("stale hosts = %+v", hosts)
- }
-}
-
-func TestClusterKey(t *testing.T) {
- t.Setenv("SYSTEM_AES_KEY", "")
- t.Setenv("JWT_SECRET", "")
- if _, err := ClusterKey(); err == nil {
- t.Fatal("no shared secret must be an error")
- }
- t.Setenv("JWT_SECRET", "s")
- a, _ := ClusterKey()
- t.Setenv("JWT_SECRET", "t")
- b, _ := ClusterKey()
- if len(a) != 32 || string(a) == string(b) {
- t.Fatal("the key derives from the secret")
- }
-}
-
-func isCode(err error, code pluginapi.ErrorCode) bool {
- pe, ok := pluginapi.AsError(err)
- return ok && pe.Code == code
-}
-
-func waitFor(t *testing.T, cond func() bool) {
- t.Helper()
- deadline := time.Now().Add(5 * time.Second)
- for !cond() {
- if time.Now().After(deadline) {
- t.Fatal("timed out")
- }
- time.Sleep(10 * time.Millisecond)
- }
-}
diff --git a/internal/plugin/install/config.go b/internal/plugin/install/config.go
deleted file mode 100644
index 95587fc14..000000000
--- a/internal/plugin/install/config.go
+++ /dev/null
@@ -1,110 +0,0 @@
-package install
-
-import (
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "time"
-
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
-)
-
-// ErrNoSystemConfig is returned for a plugin without a config.system schema.
-var ErrNoSystemConfig = errors.New("this plugin has no platform configuration")
-
-// SystemConfig is a plugin's platform configuration as the UI sees it.
-type SystemConfig struct {
- Schema json.RawMessage `json:"schema"`
- // Values has secrets redacted.
- Values map[string]any `json:"values"`
-}
-
-func (s *Service) systemSchema(ctx context.Context, id string) (*types.InstalledPlugin, *configschema.Schema,
- json.RawMessage, error,
-) {
- view, err := s.Get(ctx, id)
- if err != nil {
- return nil, nil, nil, err
- }
- if view.Manifest == nil || len(view.Manifest.Config.SystemSchema) == 0 {
- return nil, nil, nil, ErrNoSystemConfig
- }
- raw := view.Manifest.Config.SystemSchema
- schema, err := configschema.Parse(raw)
- if err != nil {
- return nil, nil, nil, fmt.Errorf("plugin %s system schema: %w", id, err)
- }
- row := view.InstalledPlugin
- return &row, schema, raw, nil
-}
-
-func systemValues(row *types.InstalledPlugin) map[string]any {
- var v map[string]any
- if len(row.SystemConfig) == 0 || json.Unmarshal(row.SystemConfig, &v) != nil || v == nil {
- return map[string]any{}
- }
- return v
-}
-
-// GetSystemConfig returns a plugin's platform configuration, secrets
-// redacted.
-func (s *Service) GetSystemConfig(ctx context.Context, id string) (*SystemConfig, error) {
- row, schema, raw, err := s.systemSchema(ctx, id)
- if err != nil {
- return nil, err
- }
- return &SystemConfig{Schema: raw, Values: configschema.Redact(schema, systemValues(row))}, nil
-}
-
-// SetSystemConfig validates and stores a plugin's platform configuration.
-// Validation failures come back as configschema.FieldErrors.
-func (s *Service) SetSystemConfig(ctx context.Context, id string, values map[string]any) (*SystemConfig, error) {
- row, schema, _, err := s.systemSchema(ctx, id)
- if err != nil {
- return nil, err
- }
- sealed, err := configschema.Update(schema, systemValues(row), values)
- if err != nil {
- return nil, err
- }
- b, err := json.Marshal(sealed)
- if err != nil {
- return nil, err
- }
- row.SystemConfig = types.JSON(b)
- if err := s.repo.SavePlugin(ctx, row); err != nil {
- return nil, err
- }
- return s.GetSystemConfig(ctx, id)
-}
-
-// OpenSystemConfig returns the platform configuration of a loaded plugin with
-// secrets decrypted, for code about to use it, and when the row last
-// changed. m is the manifest the node runs, which carries the schema.
-func OpenSystemConfig(
- ctx context.Context, repo interfaces.PluginRepository, m *manifest.Manifest,
-) (map[string]any, time.Time, error) {
- if len(m.Config.SystemSchema) == 0 {
- return map[string]any{}, time.Time{}, nil
- }
- schema, err := configschema.Parse(m.Config.SystemSchema)
- if err != nil {
- return nil, time.Time{}, err
- }
- row, err := repo.GetPlugin(ctx, m.ID)
- if err != nil {
- return nil, time.Time{}, err
- }
- if row == nil {
- return nil, time.Time{}, ErrNotInstalled
- }
- values, err := configschema.Open(schema, systemValues(row))
- if err != nil {
- return nil, time.Time{}, err
- }
- return values, row.UpdatedAt, nil
-}
diff --git a/internal/plugin/install/install.go b/internal/plugin/install/install.go
deleted file mode 100644
index 882c774e1..000000000
--- a/internal/plugin/install/install.go
+++ /dev/null
@@ -1,553 +0,0 @@
-// Package install is how a system administrator manages installed plugins:
-// review a package, install or upgrade it, switch it on or off platform-wide,
-// roll back to a stored version, uninstall. It only writes rows and package
-// blobs; the reconciler on every node does the loading.
-package install
-
-import (
- "context"
- "crypto/rand"
- "encoding/hex"
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "net/http"
- "net/url"
- "os/exec"
- goruntime "runtime"
- "sort"
- "strings"
- "time"
-
- "golang.org/x/mod/semver"
-
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/host"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/pkg"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
- "github.com/Tencent/WeKnora/internal/utils"
-)
-
-// ErrNotInstalled is returned for a plugin ID with no installed row.
-var ErrNotInstalled = errors.New("plugin is not installed")
-
-// InvalidError is a request the administrator has to change: a bad package,
-// an unsupported runtime, a digest that no longer matches what was reviewed.
-type InvalidError struct{ Err error }
-
-func (e *InvalidError) Error() string { return e.Err.Error() }
-func (e *InvalidError) Unwrap() error { return e.Err }
-
-func invalid(format string, args ...any) error {
- return &InvalidError{Err: fmt.Errorf(format, args...)}
-}
-
-// supportedRuntimes are the runtimes an installed package may use today.
-var supportedRuntimes = map[manifest.RuntimeType]bool{
- manifest.RuntimeDeclarative: true,
- manifest.RuntimeHost: true,
- manifest.RuntimeRemote: true,
-}
-
-// checkHostRuntime makes sure this server can run a host plugin: a binary
-// built for its OS and architecture, or a Python entry and an interpreter.
-func checkHostRuntime(p *pkg.Package) error {
- rt := p.Manifest.Runtime
- if !host.Supported(rt.Kind) {
- return invalid("runtime.kind %q is not supported yet; host plugins must be binaries or python", rt.Kind)
- }
- entry := host.EntryName(rt)
- if _, ok := p.ReadFile(entry); !ok {
- if rt.Kind == host.KindBinary {
- return invalid("the package has no build for this server (%s/%s): %s is missing",
- goruntime.GOOS, goruntime.GOARCH, entry)
- }
- return invalid("the package has no %s (runtime.entry)", entry)
- }
- if rt.Kind == host.KindPython {
- if _, err := exec.LookPath(host.PythonCommand()); err != nil {
- return invalid("python plugins need %s on this server; install it or set WEKNORA_PLUGIN_PYTHON",
- host.PythonCommand())
- }
- }
- return nil
-}
-
-// Syncer is the node's reconciler as the installer uses it.
-type Syncer interface {
- Reconcile(ctx context.Context) error
- Notify(ctx context.Context)
- Status(pluginID string) (reconcile.Status, bool)
-}
-
-// Service manages installed plugins.
-type Service struct {
- repo interfaces.PluginRepository
- store reconcile.PackageStore
- sync Syncer
- hostVersion string
- client *http.Client
- checks []PackageCheck
-}
-
-// PackageCheck is a domain's install-time verdict on a package, such as
-// whether its model vendor definitions would load.
-type PackageCheck func(*pkg.Package) error
-
-// WithChecks adds install-time package checks.
-func (s *Service) WithChecks(checks ...PackageCheck) *Service {
- s.checks = append(s.checks, checks...)
- return s
-}
-
-// NewService creates a Service. hostVersion is the running WeKnora version
-// that engines ranges are checked against.
-func NewService(
- repo interfaces.PluginRepository, store reconcile.PackageStore, sync Syncer, hostVersion string,
-) *Service {
- cfg := utils.DefaultSSRFSafeHTTPClientConfig()
- cfg.Timeout = 2 * time.Minute
- return &Service{
- repo: repo, store: store, sync: sync, hostVersion: hostVersion,
- client: utils.NewSSRFSafeHTTPClient(cfg),
- }
-}
-
-// Change is what installing a package would do.
-type Change string
-
-// Changes a Preview reports.
-const (
- ChangeInstall Change = "install"
- ChangeUpgrade Change = "upgrade"
- ChangeDowngrade Change = "downgrade"
- ChangeReinstall Change = "reinstall"
-)
-
-// Preview is a package as the administrator reviews it before installing.
-type Preview struct {
- Manifest *manifest.Manifest `json:"manifest"`
- Digest string `json:"digest"`
- Size int64 `json:"size"`
- Change Change `json:"change"`
- // InstalledVersion is the active version when the plugin is installed.
- InstalledVersion string `json:"installedVersion,omitempty"`
-}
-
-// Source says where a package came from.
-type Source struct {
- Kind string `json:"kind"` // "upload" or "url"
- URL string `json:"url,omitempty"`
-}
-
-// View is an installed plugin with its versions and how this node runs it.
-type View struct {
- types.InstalledPlugin
- Manifest *manifest.Manifest `json:"manifest"`
- Versions []types.PluginVersion `json:"versions"`
- // Node is the plugin's state on the node that served the request.
- Node *reconcile.Status `json:"node,omitempty"`
- // IssuedSecret is a remote plugin's signing secret, returned only by
- // the call that created it: the service needs it to check requests.
- IssuedSecret string `json:"issuedSecret,omitempty"`
-}
-
-// Inspect opens a package and says what installing it would do.
-func (s *Service) Inspect(ctx context.Context, data []byte) (*Preview, error) {
- p, err := s.open(data)
- if err != nil {
- return nil, err
- }
- out := &Preview{Manifest: p.Manifest, Digest: p.Digest, Size: p.Size, Change: ChangeInstall}
- row, err := s.repo.GetPlugin(ctx, p.Manifest.ID)
- if err != nil {
- return nil, err
- }
- if row != nil {
- out.InstalledVersion = row.ActiveVersion
- switch c := semver.Compare("v"+p.Manifest.Version, "v"+row.ActiveVersion); {
- case c > 0:
- out.Change = ChangeUpgrade
- case c < 0:
- out.Change = ChangeDowngrade
- default:
- out.Change = ChangeReinstall
- }
- }
- return out, nil
-}
-
-// open validates a package for installation on this platform.
-func (s *Service) open(data []byte) (*pkg.Package, error) {
- p, err := pkg.Open(data)
- if err != nil {
- return nil, &InvalidError{Err: err}
- }
- if !supportedRuntimes[p.Manifest.Runtime.Type] {
- return nil, invalid("runtime %q is not supported yet; declarative, host and remote plugins can be installed",
- p.Manifest.Runtime.Type)
- }
- if p.Manifest.Runtime.Type == manifest.RuntimeHost {
- if err := checkHostRuntime(p); err != nil {
- return nil, err
- }
- }
- if err := p.Manifest.CheckEngines(s.hostVersion); err != nil {
- return nil, &InvalidError{Err: err}
- }
- for _, check := range s.checks {
- if err := check(p); err != nil {
- return nil, &InvalidError{Err: err}
- }
- }
- return p, nil
-}
-
-// Request installs or upgrades a plugin from a package.
-type Request struct {
- Data []byte
- Source Source
- // ExpectedDigest, when set, must match the package: it pins the install
- // to the package the administrator reviewed.
- ExpectedDigest string
- UserID string
- // RemoteURL is where a remote plugin's service runs. An upgrade may
- // leave it empty to keep the registered one.
- RemoteURL string
-}
-
-// Install stores the package as a version of its plugin and makes it the
-// active one. Installing makes the plugin available platform-wide; each
-// tenant still opts in. The permissions the manifest asks for are recorded
-// as granted.
-func (s *Service) Install(ctx context.Context, req Request) (*View, error) {
- p, err := s.open(req.Data)
- if err != nil {
- return nil, err
- }
- if req.ExpectedDigest != "" && req.ExpectedDigest != p.Digest {
- return nil, invalid("the package changed since it was reviewed (digest %s, reviewed %s)",
- p.Digest, req.ExpectedDigest)
- }
- m := p.Manifest
- existing, err := s.repo.GetVersion(ctx, m.ID, m.Version)
- if err != nil {
- return nil, err
- }
- if existing != nil && existing.Digest != p.Digest {
- return nil, invalid("version %s of %s is already installed with different contents; "+
- "publish the change under a new version", m.Version, m.ID)
- }
- if existing == nil {
- uri, err := s.store.Put(ctx, p.Digest, req.Data)
- if err != nil {
- return nil, err
- }
- manifestJSON, _ := json.Marshal(m)
- if err := s.repo.SaveVersion(ctx, &types.PluginVersion{
- PluginID: m.ID, Version: m.Version, Digest: p.Digest, Manifest: types.JSON(manifestJSON),
- PackageURI: uri, Size: p.Size, CreatedBy: req.UserID, CreatedAt: time.Now(),
- }); err != nil {
- return nil, err
- }
- }
-
- row, err := s.repo.GetPlugin(ctx, m.ID)
- if err != nil {
- return nil, err
- }
- if row == nil {
- row = &types.InstalledPlugin{ID: m.ID, DesiredState: types.PluginStateEnabled, CreatedBy: req.UserID}
- }
- var issued string
- if m.Runtime.Type == manifest.RuntimeRemote {
- if req.RemoteURL != "" {
- if err := checkRemoteURL(req.RemoteURL); err != nil {
- return nil, err
- }
- row.RemoteURL = strings.TrimSuffix(req.RemoteURL, "/")
- }
- if row.RemoteURL == "" {
- return nil, invalid("a remote plugin needs the URL of its service")
- }
- if row.RemoteSecret == "" {
- if issued, err = s.issueSecret(row); err != nil {
- return nil, err
- }
- }
- }
- source, _ := json.Marshal(req.Source)
- perms, _ := json.Marshal(m.Permissions)
- row.Source = types.JSON(source)
- row.ActiveVersion = m.Version
- row.Runtime = string(m.Runtime.Type)
- row.GrantedPerms = types.JSON(perms)
- if err := s.repo.SavePlugin(ctx, row); err != nil {
- return nil, err
- }
- logger.Infof(ctx, "[plugin] %s installed %s %s (%s)", req.UserID, m.ID, m.Version, p.Digest)
- v, err := s.apply(ctx, m.ID)
- if v != nil {
- v.IssuedSecret = issued
- }
- return v, err
-}
-
-// checkRemoteURL accepts an http(s) service URL that passes the SSRF rules;
-// a service on a private network needs its host in SSRF_WHITELIST.
-func checkRemoteURL(raw string) error {
- u, err := url.Parse(raw)
- if err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.Host == "" {
- return invalid("service URL must be an http(s) URL")
- }
- if u.User != nil || u.RawQuery != "" || u.Fragment != "" {
- return invalid("service URL must not carry credentials, a query or a fragment")
- }
- if err := utils.ValidateURLForSSRF(raw); err != nil {
- return invalid("service URL is not allowed (private hosts must be in SSRF_WHITELIST): %v", err)
- }
- return nil
-}
-
-// issueSecret gives a remote plugin a new signing secret, stored sealed,
-// and returns it in the clear for the administrator to hand to the service.
-func (s *Service) issueSecret(row *types.InstalledPlugin) (string, error) {
- b := make([]byte, 32)
- if _, err := rand.Read(b); err != nil {
- return "", err
- }
- secret := hex.EncodeToString(b)
- sealed, err := utils.EncryptAESGCM(secret, utils.GetAESKey())
- if err != nil {
- return "", err
- }
- row.RemoteSecret = sealed
- return secret, nil
-}
-
-// SetRemoteURL moves a remote plugin to another service URL.
-func (s *Service) SetRemoteURL(ctx context.Context, id, rawURL string) (*View, error) {
- row, err := s.remoteRow(ctx, id)
- if err != nil {
- return nil, err
- }
- if err := checkRemoteURL(rawURL); err != nil {
- return nil, err
- }
- row.RemoteURL = strings.TrimSuffix(rawURL, "/")
- if err := s.repo.SavePlugin(ctx, row); err != nil {
- return nil, err
- }
- return s.apply(ctx, id)
-}
-
-// RotateSecret replaces a remote plugin's signing secret. Calls fail until
-// the service is given the new one, which only this response shows.
-func (s *Service) RotateSecret(ctx context.Context, id string) (*View, error) {
- row, err := s.remoteRow(ctx, id)
- if err != nil {
- return nil, err
- }
- secret, err := s.issueSecret(row)
- if err != nil {
- return nil, err
- }
- if err := s.repo.SavePlugin(ctx, row); err != nil {
- return nil, err
- }
- v, err := s.apply(ctx, id)
- if v != nil {
- v.IssuedSecret = secret
- }
- return v, err
-}
-
-func (s *Service) remoteRow(ctx context.Context, id string) (*types.InstalledPlugin, error) {
- row, err := s.repo.GetPlugin(ctx, id)
- if err != nil {
- return nil, err
- }
- if row == nil {
- return nil, ErrNotInstalled
- }
- if row.Runtime != string(manifest.RuntimeRemote) {
- return nil, invalid("%s is not a remote plugin", id)
- }
- return row, nil
-}
-
-// FetchURL downloads a package over HTTP(S), refusing private addresses.
-func (s *Service) FetchURL(ctx context.Context, rawURL string) ([]byte, error) {
- u, err := url.Parse(rawURL)
- if err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.Host == "" {
- return nil, invalid("package URL must be an http(s) URL")
- }
- if err := utils.ValidateURLForSSRF(rawURL); err != nil {
- return nil, invalid("package URL is not allowed: %v", err)
- }
- req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
- if err != nil {
- return nil, invalid("package URL: %v", err)
- }
- resp, err := s.client.Do(req)
- if err != nil {
- return nil, invalid("download package: %v", err)
- }
- defer func() { _ = resp.Body.Close() }()
- if resp.StatusCode != http.StatusOK {
- return nil, invalid("download package: HTTP %d", resp.StatusCode)
- }
- data, err := io.ReadAll(io.LimitReader(resp.Body, pkg.MaxArchiveBytes+1))
- if err != nil {
- return nil, invalid("download package: %v", err)
- }
- if len(data) > pkg.MaxArchiveBytes {
- return nil, invalid("package is over the %d byte limit", pkg.MaxArchiveBytes)
- }
- return data, nil
-}
-
-// List returns every installed plugin.
-func (s *Service) List(ctx context.Context) ([]View, error) {
- rows, err := s.repo.ListPlugins(ctx)
- if err != nil {
- return nil, err
- }
- sort.Slice(rows, func(i, j int) bool { return rows[i].ID < rows[j].ID })
- out := make([]View, 0, len(rows))
- for _, row := range rows {
- v, err := s.view(ctx, row)
- if err != nil {
- return nil, err
- }
- out = append(out, *v)
- }
- return out, nil
-}
-
-// Get returns one installed plugin.
-func (s *Service) Get(ctx context.Context, id string) (*View, error) {
- row, err := s.repo.GetPlugin(ctx, id)
- if err != nil {
- return nil, err
- }
- if row == nil {
- return nil, ErrNotInstalled
- }
- return s.view(ctx, *row)
-}
-
-func (s *Service) view(ctx context.Context, row types.InstalledPlugin) (*View, error) {
- versions, err := s.repo.ListVersions(ctx, row.ID)
- if err != nil {
- return nil, err
- }
- v := &View{InstalledPlugin: row, Versions: versions}
- for _, pv := range versions {
- if pv.Version == row.ActiveVersion {
- var m manifest.Manifest
- if json.Unmarshal(pv.Manifest, &m) == nil {
- v.Manifest = &m
- }
- }
- }
- if st, ok := s.sync.Status(row.ID); ok {
- v.Node = &st
- }
- return v, nil
-}
-
-// SetEnabled switches an installed plugin on or off for the whole platform.
-// Off unloads it everywhere; tenant switches are kept for when it returns.
-func (s *Service) SetEnabled(ctx context.Context, id string, enabled bool) (*View, error) {
- row, err := s.repo.GetPlugin(ctx, id)
- if err != nil {
- return nil, err
- }
- if row == nil {
- return nil, ErrNotInstalled
- }
- row.DesiredState = types.PluginStateDisabled
- if enabled {
- row.DesiredState = types.PluginStateEnabled
- }
- if err := s.repo.SavePlugin(ctx, row); err != nil {
- return nil, err
- }
- return s.apply(ctx, id)
-}
-
-// Activate makes a stored version the active one: a rollback, or a return to
-// a newer version after one.
-func (s *Service) Activate(ctx context.Context, id, version string) (*View, error) {
- row, err := s.repo.GetPlugin(ctx, id)
- if err != nil {
- return nil, err
- }
- if row == nil {
- return nil, ErrNotInstalled
- }
- v, err := s.repo.GetVersion(ctx, id, version)
- if err != nil {
- return nil, err
- }
- if v == nil {
- return nil, invalid("version %s of %s is not stored", version, id)
- }
- var m manifest.Manifest
- if err := json.Unmarshal(v.Manifest, &m); err == nil {
- if err := m.CheckEngines(s.hostVersion); err != nil {
- return nil, &InvalidError{Err: err}
- }
- perms, _ := json.Marshal(m.Permissions)
- row.GrantedPerms = types.JSON(perms)
- }
- row.ActiveVersion = version
- if err := s.repo.SavePlugin(ctx, row); err != nil {
- return nil, err
- }
- return s.apply(ctx, id)
-}
-
-// Uninstall removes a plugin, its versions and their packages. Tenant
-// switches and configuration are kept, so reinstalling restores them.
-func (s *Service) Uninstall(ctx context.Context, id string) error {
- row, err := s.repo.GetPlugin(ctx, id)
- if err != nil {
- return err
- }
- if row == nil {
- return ErrNotInstalled
- }
- versions, err := s.repo.ListVersions(ctx, id)
- if err != nil {
- return err
- }
- if err := s.repo.DeletePlugin(ctx, id); err != nil {
- return err
- }
- if err := s.sync.Reconcile(ctx); err != nil {
- logger.Warnf(ctx, "[plugin] reconcile after uninstalling %s: %v", id, err)
- }
- s.sync.Notify(ctx)
- for _, v := range versions {
- if err := s.store.Delete(ctx, v.PackageURI); err != nil {
- logger.Warnf(ctx, "[plugin] delete package %s: %v", v.PackageURI, err)
- }
- }
- return nil
-}
-
-// apply reconciles this node now, tells the others, and returns the result.
-// A plugin that fails to load is still installed; its View says why.
-func (s *Service) apply(ctx context.Context, id string) (*View, error) {
- if err := s.sync.Reconcile(ctx); err != nil {
- logger.Warnf(ctx, "[plugin] reconcile after changing %s: %v", id, err)
- }
- s.sync.Notify(ctx)
- return s.Get(ctx, id)
-}
diff --git a/internal/plugin/install/install_test.go b/internal/plugin/install/install_test.go
deleted file mode 100644
index d88cff627..000000000
--- a/internal/plugin/install/install_test.go
+++ /dev/null
@@ -1,261 +0,0 @@
-package install
-
-import (
- "context"
- "errors"
- "os"
- goruntime "runtime"
- "strings"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/plugin/plugintest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/utils"
-)
-
-func newService(t *testing.T) (*Service, *plugintest.MemRepo, *plugintest.MemStore, *registry.Registry) {
- t.Helper()
- repo, store, reg := plugintest.NewMemRepo(), &plugintest.MemStore{}, registry.New()
- r := reconcile.New(reconcile.Options{Repo: repo, Store: store, Registry: reg, CacheDir: t.TempDir()})
- return NewService(repo, store, r, "0.5.0"), repo, store, reg
-}
-
-func TestInstallUpgradeRollbackUninstall(t *testing.T) {
- ctx := context.Background()
- s, repo, store, reg := newService(t)
-
- v1 := plugintest.KitPackage(t, "1.0.0")
- preview, err := s.Inspect(ctx, v1)
- if err != nil || preview.Change != ChangeInstall || preview.Manifest.ID != "acme.kit" {
- t.Fatalf("Inspect = %+v, %v", preview, err)
- }
- view, err := s.Install(ctx, Request{
- Data: v1, Source: Source{Kind: "upload"},
- ExpectedDigest: preview.Digest, UserID: "admin",
- })
- if err != nil {
- t.Fatalf("Install: %v", err)
- }
- if view.ActiveVersion != "1.0.0" || view.DesiredState != types.PluginStateEnabled ||
- view.Node == nil || view.Node.State != reconcile.StateReady {
- t.Fatalf("view = %+v node=%+v", view.InstalledPlugin, view.Node)
- }
- if _, ok := reg.Plugin("acme.kit"); !ok {
- t.Fatal("install must load the plugin on this node")
- }
-
- v2 := plugintest.KitPackage(t, "1.1.0")
- if p, _ := s.Inspect(ctx, v2); p.Change != ChangeUpgrade || p.InstalledVersion != "1.0.0" {
- t.Fatalf("upgrade preview = %+v", p)
- }
- if _, err := s.Install(ctx, Request{Data: v2, ExpectedDigest: preview.Digest}); !isInvalid(err) {
- t.Fatalf("a digest other than the reviewed one must be refused, got %v", err)
- }
- if _, err := s.Install(ctx, Request{Data: v2}); err != nil {
- t.Fatalf("upgrade: %v", err)
- }
- view, err = s.Activate(ctx, "acme.kit", "1.0.0")
- if err != nil || view.ActiveVersion != "1.0.0" || len(view.Versions) != 2 {
- t.Fatalf("rollback = %+v, %v", view, err)
- }
- if m, _ := reg.Plugin("acme.kit"); m.Version != "1.0.0" {
- t.Fatalf("registry runs %s after rollback", m.Version)
- }
-
- if view, err = s.SetEnabled(ctx, "acme.kit", false); err != nil || view.Node != nil {
- t.Fatalf("disable = %+v, %v", view, err)
- }
- if _, ok := reg.Plugin("acme.kit"); ok {
- t.Fatal("a disabled plugin must be unloaded")
- }
-
- if err := s.Uninstall(ctx, "acme.kit"); err != nil {
- t.Fatalf("Uninstall: %v", err)
- }
- if row, _ := repo.GetPlugin(ctx, "acme.kit"); row != nil {
- t.Fatal("row survived uninstall")
- }
- if len(store.Blobs) != 0 {
- t.Fatalf("packages survived uninstall: %d", len(store.Blobs))
- }
- if _, err := s.Get(ctx, "acme.kit"); !errors.Is(err, ErrNotInstalled) {
- t.Fatalf("Get after uninstall = %v", err)
- }
-}
-
-func TestInstallRejects(t *testing.T) {
- ctx := context.Background()
- s, _, _, _ := newService(t)
- if _, err := s.Install(ctx, Request{Data: []byte("nope")}); !isInvalid(err) {
- t.Fatalf("garbage must be invalid, got %v", err)
- }
-
- pkg := plugintest.KitPackage(t, "1.0.0")
- if _, err := s.Install(ctx, Request{Data: pkg}); err != nil {
- t.Fatal(err)
- }
- // Same version, different bytes.
- other := plugintest.KitPackageWith(t, "1.0.0", "changed")
- if _, err := s.Install(ctx, Request{Data: other}); !isInvalid(err) ||
- !strings.Contains(err.Error(), "new version") {
- t.Fatalf("want same-version conflict, got %v", err)
- }
- // Reinstalling the identical package is fine.
- if p, _ := s.Inspect(ctx, pkg); p.Change != ChangeReinstall {
- t.Fatalf("change = %s", p.Change)
- }
- if _, err := s.Install(ctx, Request{Data: pkg}); err != nil {
- t.Fatalf("reinstall: %v", err)
- }
-
- old := NewService(plugintest.NewMemRepo(), &plugintest.MemStore{}, nil, "0.1.0")
- engines := plugintest.KitPackageWith(t, "2.0.0", "", "engines: { weknora: \">=0.2.0\" }")
- if _, err := old.Inspect(ctx, engines); !isInvalid(err) || !strings.Contains(err.Error(), "needs WeKnora") {
- t.Fatalf("want engines error, got %v", err)
- }
-}
-
-func TestFetchURLRefusesPrivateAddresses(t *testing.T) {
- s, _, _, _ := newService(t)
- for _, u := range []string{"http://127.0.0.1:8080/p.wkp", "file:///etc/passwd", "http://169.254.169.254/x"} {
- if _, err := s.FetchURL(context.Background(), u); !isInvalid(err) {
- t.Errorf("FetchURL(%s) = %v, want refusal", u, err)
- }
- }
-}
-
-func isInvalid(err error) bool {
- var ie *InvalidError
- return errors.As(err, &ie)
-}
-
-func hostPackage(t *testing.T, kind, binPath string) []byte {
- return plugintest.Zip(t, map[string]string{
- "plugin.yaml": "schemaVersion: 1\nid: acme.search\nversion: 1.0.0\napiVersion: weknora.plugin/v1\n" +
- "name: { en-US: ACME Search }\npublisher: { id: acme }\n" +
- "runtime: { type: host, kind: " + kind + ", entry: \"bin/{os}-{arch}/search\" }\n" +
- "permissions: { egress: [api.acme.example] }\n" +
- "contributes:\n webSearch:\n - { id: search, name: ACME Search }\n",
- binPath: "binary",
- })
-}
-
-func TestInstallHostPlugins(t *testing.T) {
- ctx := context.Background()
- s, _, _, _ := newService(t)
- name := "search"
- if goruntime.GOOS == "windows" {
- name += ".exe"
- }
- here := "bin/" + goruntime.GOOS + "-" + goruntime.GOARCH + "/" + name
- p, err := s.Inspect(ctx, hostPackage(t, "binary", here))
- if err != nil || p.Manifest.Permissions.Egress[0] != "api.acme.example" {
- t.Fatalf("Inspect = %+v, %v", p, err)
- }
- if _, err := s.Inspect(ctx, hostPackage(t, "binary", "bin/plan9-mips/search")); !isInvalid(err) ||
- !strings.Contains(err.Error(), "no build for this server") {
- t.Fatalf("want a missing build error, got %v", err)
- }
- if _, err := s.Inspect(ctx, hostPackage(t, "node", here)); !isInvalid(err) ||
- !strings.Contains(err.Error(), "binaries or python") {
- t.Fatalf("want an unsupported kind error, got %v", err)
- }
-}
-
-func TestInstallPythonPlugins(t *testing.T) {
- ctx := context.Background()
- s, _, _, _ := newService(t)
- pkg := func(files map[string]string) []byte {
- files["plugin.yaml"] = "schemaVersion: 1\nid: acme.py\nversion: 1.0.0\napiVersion: weknora.plugin/v1\n" +
- "name: { en-US: ACME Py }\npublisher: { id: acme }\n" +
- "runtime: { type: host, kind: python, entry: main.py }\n" +
- "contributes:\n webSearch:\n - { id: search, name: ACME Search }\n"
- return plugintest.Zip(t, files)
- }
- if _, err := s.Inspect(ctx, pkg(map[string]string{})); !isInvalid(err) ||
- !strings.Contains(err.Error(), "no main.py") {
- t.Fatalf("want a missing entry error, got %v", err)
- }
- t.Setenv("WEKNORA_PLUGIN_PYTHON", "no-such-python-here")
- if _, err := s.Inspect(ctx, pkg(map[string]string{"main.py": "print()"})); !isInvalid(err) ||
- !strings.Contains(err.Error(), "no-such-python-here") {
- t.Fatalf("want a missing interpreter error, got %v", err)
- }
- t.Setenv("WEKNORA_PLUGIN_PYTHON", os.Args[0]) // any executable will do
- if _, err := s.Inspect(ctx, pkg(map[string]string{"main.py": "print()"})); err != nil {
- t.Fatal(err)
- }
-}
-
-func remotePackage(t *testing.T, version string) []byte {
- return plugintest.Zip(t, map[string]string{
- "plugin.yaml": "schemaVersion: 1\nid: acme.remote\nversion: " + version +
- "\napiVersion: weknora.plugin/v1\n" +
- "name: { en-US: ACME Remote }\npublisher: { id: acme }\nruntime: { type: remote }\n" +
- "contributes:\n webSearch:\n - { id: search, name: ACME Search }\n",
- })
-}
-
-func TestInstallRemotePlugins(t *testing.T) {
- ctx := context.Background()
- utils.SetSSRFWhitelistFromRaw("plugins.example.com")
- t.Cleanup(func() { utils.SetSSRFWhitelistFromRaw("") })
- t.Setenv("SYSTEM_AES_KEY", strings.Repeat("k", 32))
- s, repo, _, _ := newService(t)
-
- if _, err := s.Install(ctx, Request{Data: remotePackage(t, "1.0.0")}); !isInvalid(err) ||
- !strings.Contains(err.Error(), "URL of its service") {
- t.Fatalf("want a missing URL error, got %v", err)
- }
- refused := []string{"http://127.0.0.1:9000", "ftp://plugins.example.com", "https://u:[email protected]"}
- for _, u := range refused {
- if _, err := s.Install(ctx, Request{Data: remotePackage(t, "1.0.0"), RemoteURL: u}); !isInvalid(err) {
- t.Errorf("RemoteURL %s = %v, want refusal", u, err)
- }
- }
-
- view, err := s.Install(ctx, Request{
- Data: remotePackage(t, "1.0.0"), RemoteURL: "https://plugins.example.com/acme/",
- })
- if err != nil {
- t.Fatal(err)
- }
- if len(view.IssuedSecret) != 64 || view.RemoteURL != "https://plugins.example.com/acme" {
- t.Fatalf("view = %+v", view)
- }
- row, _ := repo.GetPlugin(ctx, "acme.remote")
- if plain, _ := utils.DecryptStoredSecret(row.RemoteSecret); !strings.HasPrefix(row.RemoteSecret, utils.EncPrefix) ||
- plain != view.IssuedSecret {
- t.Fatalf("stored secret %q does not seal the issued one", row.RemoteSecret)
- }
- first := view.IssuedSecret
-
- view, err = s.Install(ctx, Request{Data: remotePackage(t, "1.1.0")})
- if err != nil || view.IssuedSecret != "" || view.RemoteURL != "https://plugins.example.com/acme" {
- t.Fatalf("upgrade must keep the URL and secret: %+v, %v", view, err)
- }
-
- view, err = s.RotateSecret(ctx, "acme.remote")
- if err != nil || len(view.IssuedSecret) != 64 || view.IssuedSecret == first {
- t.Fatalf("rotate = %+v, %v", view, err)
- }
- if view, err = s.SetRemoteURL(ctx, "acme.remote", "https://plugins.example.com/v2"); err != nil ||
- view.RemoteURL != "https://plugins.example.com/v2" || view.IssuedSecret != "" {
- t.Fatalf("move = %+v, %v", view, err)
- }
- if _, err := s.SetRemoteURL(ctx, "acme.remote", "http://10.0.0.1"); !isInvalid(err) {
- t.Fatalf("private URL = %v", err)
- }
-
- if _, err := s.Install(ctx, Request{Data: plugintest.KitPackage(t, "1.0.0")}); err != nil {
- t.Fatal(err)
- }
- if _, err := s.RotateSecret(ctx, "acme.kit"); !isInvalid(err) {
- t.Fatalf("rotating a declarative plugin = %v", err)
- }
- if _, err := s.RotateSecret(ctx, "acme.none"); !errors.Is(err, ErrNotInstalled) {
- t.Fatalf("rotating a missing plugin = %v", err)
- }
-}
diff --git a/internal/plugin/manifest/engines.go b/internal/plugin/manifest/engines.go
deleted file mode 100644
index 8cefd207b..000000000
--- a/internal/plugin/manifest/engines.go
+++ /dev/null
@@ -1,62 +0,0 @@
-package manifest
-
-import (
- "fmt"
- "strings"
-
- "golang.org/x/mod/semver"
-)
-
-// comparator is one term of an engines range, such as ">=0.10.0".
-type comparator struct {
- op string
- version string // canonical, with the "v" prefix semver wants
-}
-
-// parseRange reads a space-separated list of comparators, all of which must
-// hold: ">=0.10.0 <1.0.0". A bare version means "=".
-func parseRange(r string) ([]comparator, error) {
- var out []comparator
- for _, term := range strings.Fields(r) {
- op := "="
- for _, candidate := range []string{">=", "<=", ">", "<", "="} {
- if strings.HasPrefix(term, candidate) {
- op, term = candidate, strings.TrimPrefix(term, candidate)
- break
- }
- }
- if !isStrictSemver(term) {
- return nil, fmt.Errorf("%q is not a semantic version", term)
- }
- out = append(out, comparator{op: op, version: "v" + term})
- }
- if len(out) == 0 {
- return nil, fmt.Errorf("range is empty")
- }
- return out, nil
-}
-
-// CheckEngines reports whether host (the running WeKnora version) satisfies
-// the manifest's engines.weknora range. Development builds without a release
-// version pass, as does a manifest without a range.
-func (m *Manifest) CheckEngines(host string) error {
- if m.Engines.WeKnora == "" {
- return nil
- }
- v := "v" + strings.TrimPrefix(strings.TrimSpace(host), "v")
- if !semver.IsValid(v) {
- return nil
- }
- terms, err := parseRange(m.Engines.WeKnora)
- if err != nil {
- return fmt.Errorf("engines.weknora: %w", err)
- }
- for _, c := range terms {
- cmp := semver.Compare(v, c.version)
- ok := map[string]bool{">=": cmp >= 0, "<=": cmp <= 0, ">": cmp > 0, "<": cmp < 0, "=": cmp == 0}[c.op]
- if !ok {
- return fmt.Errorf("plugin needs WeKnora %s, this is %s", m.Engines.WeKnora, strings.TrimPrefix(v, "v"))
- }
- }
- return nil
-}
diff --git a/internal/plugin/manifest/engines_test.go b/internal/plugin/manifest/engines_test.go
deleted file mode 100644
index 5c636d7a7..000000000
--- a/internal/plugin/manifest/engines_test.go
+++ /dev/null
@@ -1,26 +0,0 @@
-package manifest
-
-import "testing"
-
-func TestCheckEngines(t *testing.T) {
- cases := []struct {
- rng, host string
- ok bool
- }{
- {"", "0.3.0", true},
- {">=0.10.0 <1.0.0", "0.10.2", true},
- {">=0.10.0 <1.0.0", "v0.9.9", false},
- {">=0.10.0 <1.0.0", "1.0.0", false},
- {"0.10.0", "0.10.0", true},
- {">=9.0.0", "unknown", true}, // development build
- }
- for _, c := range cases {
- m := &Manifest{Engines: Engines{WeKnora: c.rng}}
- if err := m.CheckEngines(c.host); (err == nil) != c.ok {
- t.Errorf("CheckEngines(%q, %q) = %v, want ok=%v", c.rng, c.host, err, c.ok)
- }
- }
- if _, err := parseRange(">=1.0 <2"); err == nil {
- t.Error("partial versions must be rejected")
- }
-}
diff --git a/internal/plugin/manifest/manifest.go b/internal/plugin/manifest/manifest.go
deleted file mode 100644
index a22bc1641..000000000
--- a/internal/plugin/manifest/manifest.go
+++ /dev/null
@@ -1,514 +0,0 @@
-// Package manifest defines the plugin manifest: what a plugin is, how it runs,
-// what it may touch and what it contributes. Builtin capabilities are
-// described with the same type, so the rest of WeKnora sees one shape whether
-// a connector is compiled in or shipped by a third party.
-package manifest
-
-import (
- "bytes"
- "encoding/json"
- "errors"
- "fmt"
- "regexp"
- "slices"
- "strings"
-
- "golang.org/x/mod/semver"
- "gopkg.in/yaml.v3"
-
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// SchemaVersion is the manifest format version this build understands.
-const SchemaVersion = 1
-
-// BuiltinPublisher is the publisher of every plugin compiled into WeKnora.
-// Other manifests may not claim it.
-const BuiltinPublisher = "weknora"
-
-// RuntimeType says where a plugin's code runs.
-type RuntimeType string
-
-const (
- // RuntimeBuiltin runs compiled into WeKnora. Only builtin manifests use it.
- RuntimeBuiltin RuntimeType = "builtin"
- // RuntimeDeclarative has no code; WeKnora interprets the manifest itself.
- RuntimeDeclarative RuntimeType = "declarative"
- // RuntimeHost runs as a child process of the WeKnora plugin host.
- RuntimeHost RuntimeType = "host"
- // RuntimeRemote is an HTTP service somewhere else, registered by URL.
- RuntimeRemote RuntimeType = "remote"
- // RuntimeKubernetes runs as a Deployment + Service managed through the k8s API.
- RuntimeKubernetes RuntimeType = "kubernetes"
-)
-
-// Manifest is a plugin's plugin.yaml.
-type Manifest struct {
- SchemaVersion int `json:"schemaVersion" yaml:"schemaVersion"`
- ID string `json:"id" yaml:"id"`
- Version string `json:"version" yaml:"version"`
- // APIVersion is the extension protocol a code plugin speaks
- // (weknora.plugin/v1). Builtin and declarative plugins leave it empty.
- APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion"`
- Engines Engines `json:"engines,omitzero" yaml:"engines"`
- Name LocalizedText `json:"name" yaml:"name"`
- Description LocalizedText `json:"description,omitzero" yaml:"description"`
- Publisher Publisher `json:"publisher" yaml:"publisher"`
- Icon string `json:"icon,omitempty" yaml:"icon"`
- Homepage string `json:"homepage,omitempty" yaml:"homepage"`
- License string `json:"license,omitempty" yaml:"license"`
-
- // Builtin marks a plugin compiled into WeKnora. It cannot be set from a
- // manifest file.
- Builtin bool `json:"builtin,omitempty" yaml:"-"`
- // Required marks a builtin a tenant may not disable (the core agent
- // tools, the default parser).
- Required bool `json:"required,omitempty" yaml:"-"`
-
- Runtime Runtime `json:"runtime" yaml:"runtime"`
- Permissions Permissions `json:"permissions,omitzero" yaml:"permissions"`
- Config ConfigSchemas `json:"config,omitzero" yaml:"config"`
- Contributes Contributions `json:"contributes" yaml:"contributes"`
-}
-
-// Engines constrains which WeKnora versions a plugin runs on.
-type Engines struct {
- // WeKnora is a semver range such as ">=0.10.0 <1.0.0".
- WeKnora string `json:"weknora,omitempty" yaml:"weknora"`
-}
-
-// Publisher identifies who ships a plugin. ID is the first segment of the
-// plugin ID.
-type Publisher struct {
- ID string `json:"id" yaml:"id"`
- Name string `json:"name,omitempty" yaml:"name"`
- URL string `json:"url,omitempty" yaml:"url"`
-}
-
-// Runtime says how a plugin's code runs.
-type Runtime struct {
- Type RuntimeType `json:"type" yaml:"type"`
- // Kind is the process runtime for host plugins: binary, python or node.
- Kind string `json:"kind,omitempty" yaml:"kind"`
- // Entry is the command inside the package, with {os} and {arch}
- // placeholders for binaries.
- Entry string `json:"entry,omitempty" yaml:"entry"`
- // Singleton allows only one live instance across the cluster (long-lived
- // connections such as an IM WebSocket).
- Singleton bool `json:"singleton,omitempty" yaml:"singleton"`
- Resources *Resources `json:"resources,omitempty" yaml:"resources"`
-}
-
-// Resources caps a host plugin process.
-type Resources struct {
- CPU string `json:"cpu,omitempty" yaml:"cpu"`
- Memory string `json:"memory,omitempty" yaml:"memory"`
-}
-
-// Permissions is what a plugin asks for; an administrator confirms it at
-// install time.
-type Permissions struct {
- // Egress lists host patterns the plugin may reach ("*.atlassian.net").
- Egress []string `json:"egress,omitempty" yaml:"egress"`
- // HostAPI lists WeKnora API scopes the plugin may call back with.
- HostAPI []string `json:"hostApi,omitempty" yaml:"hostApi"`
- // Events lists asynchronous events the plugin subscribes to.
- Events []string `json:"events,omitempty" yaml:"events"`
-}
-
-// ConfigSchemas points at the JSON Schemas of plugin-level configuration.
-type ConfigSchemas struct {
- System string `json:"system,omitempty" yaml:"system"`
- Tenant string `json:"tenant,omitempty" yaml:"tenant"`
- // SystemSchema and TenantSchema are the schema files' contents as JSON,
- // filled in when a package is opened.
- SystemSchema json.RawMessage `json:"systemSchema,omitempty" yaml:"-"`
- TenantSchema json.RawMessage `json:"tenantSchema,omitempty" yaml:"-"`
-}
-
-// Contributions maps each extension point to what the plugin provides there.
-type Contributions map[Point][]Contribution
-
-// Contribution is one capability a plugin provides at one extension point,
-// such as one connector.
-type Contribution struct {
- // ID is local to the plugin; QualifiedID adds the plugin ID.
- ID string `json:"id" yaml:"id"`
- Name LocalizedText `json:"name" yaml:"name"`
- Description LocalizedText `json:"description,omitzero" yaml:"description"`
- Icon string `json:"icon,omitempty" yaml:"icon"`
- // Aliases are the short names builtins were stored under before plugins
- // existed ("feishu"). Rows keep them, so lookups must keep resolving
- // them. Only builtins may declare aliases.
- Aliases []string `json:"aliases,omitempty" yaml:"-"`
- Capabilities []string `json:"capabilities,omitempty" yaml:"capabilities"`
- // Order sorts contributions of one point in the UI (lower first); ties
- // keep registration order.
- Order int `json:"order,omitempty" yaml:"order"`
- // InstanceSchema points at the JSON Schema of one instance's
- // configuration (one data source, one IM channel).
- InstanceSchema string `json:"instanceSchema,omitempty" yaml:"instanceSchema"`
- // Path is a file or directory inside the package: the skill directory
- // (skills) or the vendor definition JSON (modelVendors).
- Path string `json:"path,omitempty" yaml:"path"`
- // MCP describes a remote MCP server (mcpServers). A plugin with code
- // may leave it out and serve the tools itself (pluginapi.MCPPath).
- MCP *MCPServer `json:"mcp,omitempty" yaml:"mcp"`
- // ToolViews map tool names of an MCP server to the view that shows
- // their structured results in the chat.
- ToolViews map[string]ToolView `json:"toolViews,omitempty" yaml:"toolViews"`
- // FileTypes are the lower-case extensions a parser handles ("pdf").
- FileTypes []string `json:"fileTypes,omitempty" yaml:"fileTypes"`
- // InstanceSchemaJSON is the instanceSchema file's content as JSON,
- // filled when the package is opened so every node can read it.
- InstanceSchemaJSON json.RawMessage `json:"instanceSchemaJson,omitempty" yaml:"-"`
- // Entry is the HTML page of a UI contribution (pages, settingsSections,
- // kbTabs), a path under UIRoot.
- Entry string `json:"entry,omitempty" yaml:"entry"`
- // MinRole is the workspace role a UI contribution needs: viewer (the
- // default for pages and tabs), contributor, admin (the default for
- // settings sections) or owner.
- MinRole string `json:"minRole,omitempty" yaml:"minRole"`
- // Extra carries point-specific metadata the generic fields do not cover.
- Extra map[string]any `json:"extra,omitempty" yaml:"extra"`
-}
-
-// UIMinRole is the role a UI contribution needs, with the point's default.
-func UIMinRole(point Point, c Contribution) string {
- if c.MinRole != "" {
- return c.MinRole
- }
- if point == PointSettingsSections {
- return "admin"
- }
- return "viewer"
-}
-
-// MCPServer is a remote MCP server a plugin contributes.
-type MCPServer struct {
- URL string `json:"url" yaml:"url"`
- // Transport is "sse" or "http-streamable" (the default).
- Transport string `json:"transport,omitempty" yaml:"transport"`
- // Headers are sent on every request. A value may reference the
- // workspace's plugin configuration as ${config.<key>} (how a plugin asks
- // each workspace for its own API key) or the platform's as
- // ${system.<key>}. The URL is fixed: configuration cannot redirect it.
- Headers map[string]string `json:"headers,omitempty" yaml:"headers"`
-}
-
-// MCP transports a plugin may declare.
-const (
- MCPTransportSSE = "sse"
- MCPTransportHTTPStreamable = "http-streamable"
-)
-
-// QualifiedID is the cluster-wide ID of a contribution: "<plugin>/<local>".
-func QualifiedID(pluginID, localID string) string {
- return pluginID + "/" + localID
-}
-
-var (
- pluginIDPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,62}\.[a-z0-9][a-z0-9-]{0,62}$`)
- contributionIDPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9_.-]{0,127}$`)
-)
-
-// isStrictSemver accepts full MAJOR.MINOR.PATCH versions with an optional
-// pre-release and build suffix. x/mod/semver alone also accepts "1.0".
-func isStrictSemver(v string) bool {
- withoutBuild, _, _ := strings.Cut(v, "+")
- return semver.IsValid("v"+v) && semver.Canonical("v"+v) == "v"+withoutBuild
-}
-
-// HasValidVersion reports whether Version is a full semantic version.
-func (m *Manifest) HasValidVersion() bool { return isStrictSemver(m.Version) }
-
-// Parse decodes a manifest from YAML (JSON is accepted too) and validates it.
-// Unknown keys are rejected so a typo does not silently drop a setting.
-func Parse(data []byte) (*Manifest, error) {
- dec := yaml.NewDecoder(bytes.NewReader(data))
- dec.KnownFields(true)
- var m Manifest
- if err := dec.Decode(&m); err != nil {
- return nil, fmt.Errorf("decode manifest: %w", err)
- }
- if err := m.Validate(); err != nil {
- return nil, err
- }
- return &m, nil
-}
-
-// Validate checks a manifest. It reports every problem at once so an author
-// fixes them in one pass.
-func (m *Manifest) Validate() error {
- var errs []error
- add := func(format string, args ...any) { errs = append(errs, fmt.Errorf(format, args...)) }
-
- if m.SchemaVersion != SchemaVersion {
- add("schemaVersion must be %d, got %d", SchemaVersion, m.SchemaVersion)
- }
- if !pluginIDPattern.MatchString(m.ID) {
- add("id %q must look like <publisher>.<name> in lowercase letters, digits and dashes", m.ID)
- }
- publisher, _, _ := strings.Cut(m.ID, ".")
- if m.Publisher.ID != publisher {
- add("publisher.id %q must match the id prefix %q", m.Publisher.ID, publisher)
- }
- if publisher == BuiltinPublisher && !m.Builtin {
- add("publisher %q is reserved for builtin plugins", BuiltinPublisher)
- }
- if !isStrictSemver(m.Version) {
- add("version %q is not a semantic version", m.Version)
- }
- if m.Name.IsZero() {
- add("name is required")
- }
- if m.Engines.WeKnora != "" {
- if _, err := parseRange(m.Engines.WeKnora); err != nil {
- add("engines.weknora: %v", err)
- }
- }
- m.validateRuntime(add)
- m.validatePermissions(add)
- m.validateContributions(add)
- return errors.Join(errs...)
-}
-
-func (m *Manifest) validateRuntime(add func(string, ...any)) {
- switch m.Runtime.Type {
- case RuntimeBuiltin:
- if !m.Builtin {
- add("runtime.type %q is reserved for builtin plugins", RuntimeBuiltin)
- }
- case RuntimeDeclarative:
- case RuntimeRemote, RuntimeKubernetes:
- m.validateAPIVersion(add)
- case RuntimeHost:
- m.validateAPIVersion(add)
- switch m.Runtime.Kind {
- case "binary", "python", "node":
- default:
- add("runtime.kind must be binary, python or node for host plugins, got %q", m.Runtime.Kind)
- }
- if m.Runtime.Entry == "" {
- add("runtime.entry is required for host plugins")
- }
- if r := m.Runtime.Resources; r != nil {
- if r.CPU != "" {
- if _, err := ParseCPU(r.CPU); err != nil {
- add("runtime.resources.%v", err)
- }
- }
- if r.Memory != "" {
- if _, err := ParseMemory(r.Memory); err != nil {
- add("runtime.resources.%v", err)
- }
- }
- }
- default:
- add("runtime.type %q is not one of builtin, declarative, host, remote, kubernetes", m.Runtime.Type)
- }
-}
-
-// ExtensionAPIVersion is the extension protocol this build speaks
-// (pluginsdk/pluginapi.APIVersion).
-const ExtensionAPIVersion = "weknora.plugin/v1"
-
-func (m *Manifest) validateAPIVersion(add func(string, ...any)) {
- if m.Builtin {
- return
- }
- if m.APIVersion != ExtensionAPIVersion {
- add("apiVersion must be %q for code plugins, got %q", ExtensionAPIVersion, m.APIVersion)
- }
-}
-
-// egressPattern is a host name, optionally with a leading "*." wildcard.
-var egressPattern = regexp.MustCompile(`^(\*\.)?([a-z0-9]([a-z0-9-]*[a-z0-9])?)(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$`)
-
-// EgressAnyHost in permissions.egress grants any public host (a feed reader
-// follows links anywhere). Private addresses stay unreachable either way.
-const EgressAnyHost = "*"
-
-// HostAPIScopes are the Host API scopes a plugin may ask for in
-// permissions.hostApi.
-var HostAPIScopes = []string{
- // kv: the plugin's own key-value store in each tenant.
- "kv",
-}
-
-func (m *Manifest) validatePermissions(add func(string, ...any)) {
- for _, s := range m.Permissions.HostAPI {
- if !slices.Contains(HostAPIScopes, s) {
- add("permissions.hostApi %q is not a Host API scope (known: %s)", s, strings.Join(HostAPIScopes, ", "))
- }
- }
- for _, e := range m.Permissions.Egress {
- if e != EgressAnyHost && !egressPattern.MatchString(strings.ToLower(e)) {
- add(
- "permissions.egress %q must be a host name (api.example.com), a wildcard (*.example.com) or *",
- e,
- )
- }
- }
- for _, e := range m.Permissions.Events {
- if !slices.Contains(pluginapi.EventTypes, e) {
- add("permissions.events %q is not an event type (known: %s)", e, strings.Join(pluginapi.EventTypes, ", "))
- }
- }
- if len(m.Permissions.Events) > 0 && m.Runtime.Type == RuntimeDeclarative {
- add("permissions.events needs code; declarative plugins cannot receive events")
- }
-}
-
-// storedTypeIDLimit is how long a qualified contribution ID may be where
-// instances store it in a varchar(50) column (data_sources.type,
-// web_search_providers.provider, knowledges.channel).
-var storedTypeIDLimit = map[Point]int{PointConnectors: 50, PointWebSearch: 50}
-
-func (m *Manifest) validateContributions(add func(string, ...any)) {
- // A plugin that only listens to events contributes nothing else.
- if len(m.Contributes) == 0 && len(m.Permissions.Events) == 0 {
- add("contributes must declare at least one contribution")
- }
- for point := range m.Contributes {
- if _, ok := LookupPoint(point); !ok {
- add("contributes.%s is not a known extension point", point)
- }
- }
- for _, info := range points {
- point := info.Point
- list, ok := m.Contributes[point]
- if !ok {
- continue
- }
- if !info.ThirdParty && !m.Builtin {
- add("contributes.%s is not open to third-party plugins yet", point)
- }
- if m.Runtime.Type == RuntimeDeclarative && !info.Declarative {
- add("contributes.%s needs code; declarative plugins cannot contribute to it", point)
- }
- seen := make(map[string]bool, len(list))
- for i, c := range list {
- where := fmt.Sprintf("contributes.%s[%d]", point, i)
- if !contributionIDPattern.MatchString(c.ID) {
- add("%s.id %q must be lowercase letters, digits, '_', '-' or '.'", where, c.ID)
- }
- if seen[c.ID] {
- add("%s.id %q is declared twice", where, c.ID)
- }
- seen[c.ID] = true
- if c.Name.IsZero() {
- add("%s.name is required", where)
- }
- if limit, ok := storedTypeIDLimit[point]; ok && !m.Builtin && len(QualifiedID(m.ID, c.ID)) > limit {
- add("%s: %q is longer than the %d characters instances store their type in",
- where, QualifiedID(m.ID, c.ID), limit)
- }
- if len(c.Aliases) > 0 && !m.Builtin {
- add("%s.aliases may only be declared by builtin plugins", where)
- }
- validateDeclarative(point, c, m.Builtin, m.Runtime.Type != RuntimeDeclarative, where, add)
- if len(c.ToolViews) > 0 {
- if point != PointMCPServers {
- add("%s.toolViews belong to mcpServers", where)
- }
- validateToolViews(c.ToolViews, where, add)
- }
- if IsUIPoint(point) {
- validateUI(c, where, add)
- }
- if point == PointParsers && !m.Builtin {
- validateFileTypes(c.FileTypes, where, add)
- }
- for _, alias := range c.Aliases {
- // Aliases share the ID alphabet, which has no '/', so an
- // alias can never shadow a qualified ID.
- if !contributionIDPattern.MatchString(alias) {
- add("%s alias %q must be lowercase letters, digits, '_', '-' or '.'", where, alias)
- }
- }
- }
- }
-}
-
-// validateDeclarative checks the fields a declarative contribution needs.
-// Builtins describe their implementation in code instead.
-// fileTypePattern is a lower-case file extension without the dot.
-var fileTypePattern = regexp.MustCompile(`^[a-z0-9]{1,16}$`)
-
-func validateFileTypes(types []string, where string, add func(string, ...any)) {
- if len(types) == 0 {
- add("%s.fileTypes is required: the extensions the parser handles, such as [pdf]", where)
- }
- for _, t := range types {
- if !fileTypePattern.MatchString(t) {
- add("%s.fileTypes %q must be a lower-case extension without the dot", where, t)
- }
- }
-}
-
-func validateDeclarative(point Point, c Contribution, builtin, code bool, where string, add func(string, ...any)) {
- if builtin {
- return
- }
- switch point {
- case PointSkills, PointModelVendors:
- if c.Path == "" {
- add("%s.path is required", where)
- } else if !isPackagePath(c.Path) {
- add("%s.path %q must be a relative path inside the package", where, c.Path)
- }
- case PointMCPServers:
- if c.ServedByPlugin() {
- if !code {
- add("%s.mcp.url is required: only a plugin with code can serve tools itself", where)
- } else if c.MCP != nil && (c.MCP.Transport != "" || len(c.MCP.Headers) > 0) {
- add("%s.mcp: transport and headers only apply to a remote url", where)
- }
- return
- }
- if !strings.HasPrefix(c.MCP.URL, "https://") && !strings.HasPrefix(c.MCP.URL, "http://") {
- add("%s.mcp.url must be an http(s) URL", where)
- }
- switch c.MCP.Transport {
- case "", MCPTransportSSE, MCPTransportHTTPStreamable:
- default:
- add("%s.mcp.transport must be sse or http-streamable", where)
- }
- if len(TemplateRefs(c.MCP.URL)) > 0 {
- add("%s.mcp.url cannot reference configuration; only headers can", where)
- }
- for name, value := range c.MCP.Headers {
- validateTemplate(value, fmt.Sprintf("%s.mcp.headers.%s", where, name), add)
- }
- }
-}
-
-func validateUI(c Contribution, where string, add func(string, ...any)) {
- switch {
- case c.Entry == "":
- add("%s.entry is required: the page's HTML file under %s", where, UIRoot)
- case !isPackagePath(c.Entry) || !strings.HasPrefix(c.Entry, UIRoot):
- add("%s.entry %q must be a file under %s", where, c.Entry, UIRoot)
- case !strings.HasSuffix(c.Entry, ".html"):
- add("%s.entry %q must be an .html file", where, c.Entry)
- }
- switch c.MinRole {
- case "", "viewer", "contributor", "admin", "owner":
- default:
- add("%s.minRole must be viewer, contributor, admin or owner", where)
- }
-}
-
-// isPackagePath reports whether p stays inside the package root.
-func isPackagePath(p string) bool {
- if p == "" || strings.HasPrefix(p, "/") || strings.Contains(p, "\\") {
- return false
- }
- for _, part := range strings.Split(p, "/") {
- if part == ".." {
- return false
- }
- }
- return true
-}
diff --git a/internal/plugin/manifest/manifest_test.go b/internal/plugin/manifest/manifest_test.go
deleted file mode 100644
index 46008d088..000000000
--- a/internal/plugin/manifest/manifest_test.go
+++ /dev/null
@@ -1,319 +0,0 @@
-package manifest
-
-import (
- "encoding/json"
- "fmt"
- "strings"
- "testing"
-)
-
-const jiraManifest = `
-schemaVersion: 1
-id: acme.jira
-version: 1.2.0
-apiVersion: weknora.plugin/v1
-engines: { weknora: ">=0.10.0" }
-name: { zh-CN: Jira 问题, en-US: Jira }
-publisher: { id: acme, name: ACME Inc. }
-runtime: { type: host, kind: binary, entry: "bin/{os}-{arch}/jira" }
-permissions:
- egress: ["*.atlassian.net"]
-contributes:
- connectors:
- - id: jira
- name: Jira
- capabilities: [incremental]
- instanceSchema: schemas/connector.json
-`
-
-// openToThirdParty opens a point for the duration of a test; no point is open
-// to third parties in P0, but the validation path still needs covering.
-func openToThirdParty(t *testing.T, p Point) {
- t.Helper()
- saved := points
- points = make([]PointInfo, len(saved))
- copy(points, saved)
- for i := range points {
- if points[i].Point == p {
- points[i].ThirdParty = true
- }
- }
- t.Cleanup(func() { points = saved })
-}
-
-func TestParseValidManifest(t *testing.T) {
- openToThirdParty(t, PointConnectors)
- m, err := Parse([]byte(jiraManifest))
- if err != nil {
- t.Fatalf("Parse: %v", err)
- }
- if m.ID != "acme.jira" || m.Runtime.Type != RuntimeHost {
- t.Fatalf("unexpected manifest: %+v", m)
- }
- if got := m.Name.Resolve("zh-CN"); got != "Jira 问题" {
- t.Fatalf("zh-CN name = %q", got)
- }
- if got := m.Name.Default; got != "Jira" {
- t.Fatalf("default name = %q, want the en-US variant", got)
- }
- c := m.Contributes[PointConnectors]
- if len(c) != 1 || c[0].ID != "jira" || c[0].Name.Default != "Jira" {
- t.Fatalf("unexpected connectors: %+v", c)
- }
-}
-
-func TestParseRejectsUnknownKeys(t *testing.T) {
- openToThirdParty(t, PointConnectors)
- _, err := Parse([]byte(jiraManifest + "\nruntimee: {}\n"))
- if err == nil || !strings.Contains(err.Error(), "runtimee") {
- t.Fatalf("want unknown-key error, got %v", err)
- }
-}
-
-func TestValidateReportsEveryProblem(t *testing.T) {
- m := &Manifest{
- SchemaVersion: 2,
- ID: "Bad_ID",
- Version: "1.0",
- Runtime: Runtime{Type: RuntimeHost},
- Contributes: Contributions{
- "widgets": {{ID: "x", Name: Text("X", nil)}},
- PointIMChannels: {{ID: "im", Name: Text("IM", nil)}},
- PointConnectors: {
- {ID: "dup", Name: Text("A", nil), Aliases: []string{"old"}},
- {ID: "dup"},
- },
- },
- }
- err := m.Validate()
- if err == nil {
- t.Fatal("want errors")
- }
- for _, want := range []string{
- "schemaVersion must be 1",
- `id "Bad_ID"`,
- `version "1.0"`,
- "name is required",
- "runtime.kind",
- "runtime.entry",
- "apiVersion must be",
- "contributes.widgets is not a known extension point",
- "contributes.imChannels is not open to third-party plugins yet",
- `"dup" is declared twice`,
- "contributes.connectors[1].name is required",
- "aliases may only be declared by builtin plugins",
- } {
- if !strings.Contains(err.Error(), want) {
- t.Errorf("error %q does not mention %q", err, want)
- }
- }
-}
-
-func TestBuiltinPublisherIsReserved(t *testing.T) {
- m := &Manifest{
- SchemaVersion: SchemaVersion,
- ID: "weknora.fake",
- Version: "1.0.0",
- Name: Text("Fake", nil),
- Publisher: Publisher{ID: BuiltinPublisher},
- Runtime: Runtime{Type: RuntimeBuiltin},
- Contributes: Contributions{PointTools: {{ID: "t", Name: Text("T", nil)}}},
- }
- err := m.Validate()
- if err == nil || !strings.Contains(err.Error(), "reserved for builtin plugins") {
- t.Fatalf("want reserved-publisher error, got %v", err)
- }
- m.Builtin = true
- if err := m.Validate(); err != nil {
- t.Fatalf("builtin manifest should validate: %v", err)
- }
-}
-
-func TestStrictSemver(t *testing.T) {
- for v, want := range map[string]bool{
- "1.2.0": true, "1.2.0-beta.1": true, "1.2.0+build.5": true,
- "1.2": false, "v1.2.0": false, "01.2.0": false, "": false,
- } {
- if got := isStrictSemver(v); got != want {
- t.Errorf("isStrictSemver(%q) = %v, want %v", v, got, want)
- }
- }
-}
-
-func TestLocalizedTextResolve(t *testing.T) {
- text := Text("Feishu", map[string]string{"zh-CN": "飞书", "ja-JP": ""})
- cases := map[string]string{
- "zh-CN": "飞书",
- "zh-TW": "飞书", // same language
- "zh": "飞书",
- "en-US": "Feishu",
- "ja-JP": "Feishu", // empty variants are dropped
- "": "Feishu",
- }
- for locale, want := range cases {
- if got := text.Resolve(locale); got != want {
- t.Errorf("Resolve(%q) = %q, want %q", locale, got, want)
- }
- }
-}
-
-func TestLocalizedTextJSONRoundTrip(t *testing.T) {
- data, err := json.Marshal(Text("Feishu", map[string]string{"zh-CN": "飞书"}))
- if err != nil {
- t.Fatal(err)
- }
- if string(data) != `{"default":"Feishu","zh-CN":"飞书"}` {
- t.Fatalf("marshal = %s", data)
- }
- var back LocalizedText
- if err := json.Unmarshal(data, &back); err != nil {
- t.Fatal(err)
- }
- if back.Default != "Feishu" || back.Locales["zh-CN"] != "飞书" {
- t.Fatalf("round trip = %+v", back)
- }
- var plain LocalizedText
- if err := json.Unmarshal([]byte(`"Jira"`), &plain); err != nil || plain.Default != "Jira" {
- t.Fatalf("plain string = %+v, %v", plain, err)
- }
-}
-
-func TestContributionOmitsEmptyDescription(t *testing.T) {
- data, err := json.Marshal(Contribution{ID: "x", Name: Text("X", nil)})
- if err != nil {
- t.Fatal(err)
- }
- if strings.Contains(string(data), "description") {
- t.Fatalf("empty description should be omitted: %s", data)
- }
-}
-
-func TestEgressPatterns(t *testing.T) {
- base := func(egress ...string) *Manifest {
- return &Manifest{
- SchemaVersion: SchemaVersion, ID: "acme.x", Version: "1.0.0", APIVersion: ExtensionAPIVersion,
- Name: Text("X", nil), Publisher: Publisher{ID: "acme"},
- Runtime: Runtime{Type: RuntimeHost, Kind: "binary", Entry: "bin/x"},
- Permissions: Permissions{Egress: egress},
- Contributes: Contributions{PointWebSearch: {{ID: "x", Name: Text("X", nil)}}},
- }
- }
- if err := base("api.example.com", "*.atlassian.net", "*").Validate(); err != nil {
- t.Fatalf("valid egress rejected: %v", err)
- }
- for _, bad := range []string{"**", "http://x.com", "x.com:443", "*.*.com", "10.0.0.0/8"} {
- if err := base(bad).Validate(); err == nil || !strings.Contains(err.Error(), "permissions.egress") {
- t.Errorf("egress %q: got %v", bad, err)
- }
- }
-}
-
-func TestStoredTypeIDLength(t *testing.T) {
- m := &Manifest{
- SchemaVersion: SchemaVersion, ID: "acme.a-rather-long-plugin-name", Version: "1.0.0",
- APIVersion: ExtensionAPIVersion, Name: Text("X", nil), Publisher: Publisher{ID: "acme"},
- Runtime: Runtime{Type: RuntimeHost, Kind: "binary", Entry: "bin/x"},
- Contributes: Contributions{PointConnectors: {{ID: "and-an-even-longer-connector-id", Name: Text("X", nil)}}},
- }
- if err := m.Validate(); err == nil || !strings.Contains(err.Error(), "longer than the 50 characters") {
- t.Fatalf("want a length error, got %v", err)
- }
-}
-
-func TestUIContributions(t *testing.T) {
- base := func(c Contribution) *Manifest {
- c.ID, c.Name = "links", Text("Links", nil)
- return &Manifest{
- SchemaVersion: SchemaVersion, ID: "acme.x", Version: "1.0.0",
- Name: Text("X", nil), Publisher: Publisher{ID: "acme"}, Runtime: Runtime{Type: RuntimeDeclarative},
- Contributes: Contributions{PointPages: {c}},
- }
- }
- if err := base(Contribution{Entry: "ui/index.html", MinRole: "contributor"}).Validate(); err != nil {
- t.Fatalf("a declarative plugin may have pages: %v", err)
- }
- for _, c := range []Contribution{
- {}, {Entry: "index.html"}, {Entry: "ui/../main.py"}, {Entry: "ui/app.js"}, {Entry: "ui/x.html", MinRole: "god"},
- } {
- if err := base(c).Validate(); err == nil {
- t.Errorf("%+v should be refused", c)
- }
- }
- if UIMinRole(PointSettingsSections, Contribution{}) != "admin" ||
- UIMinRole(PointPages, Contribution{}) != "viewer" ||
- UIMinRole(PointKBTabs, Contribution{MinRole: "owner"}) != "owner" {
- t.Fatal("min role defaults")
- }
-}
-
-func TestResourceAmounts(t *testing.T) {
- for in, want := range map[string]int64{"500m": 500, "0.5": 500, "2": 2000} {
- if got, err := ParseCPU(in); err != nil || got != want {
- t.Errorf("ParseCPU(%q) = %d, %v", in, got, err)
- }
- }
- for in, want := range map[string]int64{"512Mi": 512 << 20, "1Gi": 1 << 30, "500M": 500e6, "1024": 1024} {
- if got, err := ParseMemory(in); err != nil || got != want {
- t.Errorf("ParseMemory(%q) = %d, %v", in, got, err)
- }
- }
- for _, bad := range []string{"", "-1", "lots", "0m"} {
- if _, err := ParseCPU(bad); err == nil {
- t.Errorf("ParseCPU(%q) accepted", bad)
- }
- if _, err := ParseMemory(bad); err == nil {
- t.Errorf("ParseMemory(%q) accepted", bad)
- }
- }
-}
-
-func TestToolServersAndViews(t *testing.T) {
- base := `schemaVersion: 1
-id: acme.tools
-version: 1.0.0
-apiVersion: weknora.plugin/v1
-name: { en-US: Tools }
-publisher: { id: acme }
-runtime: %s
-contributes:
- mcpServers:
- - id: issues
- name: Issues
-%s`
- parse := func(runtime, rest string) error {
- _, err := Parse([]byte(fmt.Sprintf(base, runtime, rest)))
- return err
- }
- code := "{ type: host, kind: binary, entry: bin/x }"
- views := ` toolViews:
- search: { view: table, items: issues, columns: [key, { field: summary, title: { en-US: Summary }, link: url }] }
- get: { view: page, entry: ui/issue.html }
- stats: { view: kv }
-`
- if err := parse(code, views); err != nil {
- t.Fatalf("a code plugin serving its own tools: %v", err)
- }
- m, _ := Parse([]byte(fmt.Sprintf(base, code, views)))
- c := m.Contributes[PointMCPServers][0]
- if !c.ServedByPlugin() || c.ToolViews["search"].Columns[0].Field != "key" ||
- c.ToolViews["search"].Columns[1].Link != "url" {
- t.Fatalf("contribution = %+v", c)
- }
- for name, tc := range map[string]struct{ runtime, rest, want string }{
- "declarative needs a url": {"{ type: declarative }", "", "mcp.url is required"},
- "headers need a url": {code, " mcp: { headers: { X: y } }\n", "only apply to a remote url"},
- "table without columns": {code, " toolViews: { search: { view: table } }\n", "a table needs columns"},
- "cards without title": {code, " toolViews: { search: { view: cards } }\n", "cards need a title"},
- "page outside ui": {
- code, " toolViews: { get: { view: page, entry: x.html } }\n", "must be a file under ui/",
- },
- "unknown view": {code, " toolViews: { get: { view: chart } }\n", "view must be"},
- "bad path": {
- code, " toolViews: { get: { view: kv, title: \"a[0]\" } }\n", "dotted field path",
- },
- } {
- if err := parse(tc.runtime, tc.rest); err == nil || !strings.Contains(err.Error(), tc.want) {
- t.Errorf("%s: %v", name, err)
- }
- }
-}
diff --git a/internal/plugin/manifest/point.go b/internal/plugin/manifest/point.go
deleted file mode 100644
index af8af97a0..000000000
--- a/internal/plugin/manifest/point.go
+++ /dev/null
@@ -1,88 +0,0 @@
-package manifest
-
-// Point names an extension point: a kind of capability a plugin contributes.
-// The string is also the key under `contributes` in a manifest.
-type Point string
-
-// Extension points. A point is added here when its first contribution lands,
-// so a manifest can never declare a point nothing consumes.
-const (
- PointModelVendors Point = "modelVendors"
- PointConnectors Point = "connectors"
- PointIMChannels Point = "imChannels"
- PointWebSearch Point = "webSearch"
- PointTools Point = "tools"
- PointParsers Point = "parsers"
- // PointSkills contributes SKILL.md skill directories from the package.
- PointSkills Point = "skills"
- // PointMCPServers contributes MCP servers whose tools agents can use.
- PointMCPServers Point = "mcpServers"
- // PointPages contributes pages of their own, opened from the toolbox.
- PointPages Point = "pages"
- // PointSettingsSections contributes sections of the settings dialog.
- PointSettingsSections Point = "settingsSections"
- // PointKBTabs contributes tabs of the knowledge base page.
- PointKBTabs Point = "kbTabs"
- // PointWebhooks contributes inbound URLs third parties call, one per
- // workspace; WeKnora relays the calls to the plugin.
- PointWebhooks Point = "webhooks"
-)
-
-// UIRoot is the package directory plugin pages live in. Only files under it
-// are served to browsers.
-const UIRoot = "ui/"
-
-// IsUIPoint reports whether a point contributes a sandboxed page (an entry
-// HTML file under UIRoot) rather than a capability.
-func IsUIPoint(p Point) bool {
- return p == PointPages || p == PointSettingsSections || p == PointKBTabs
-}
-
-// PointInfo describes an extension point.
-type PointInfo struct {
- Point Point `json:"point"`
- // ThirdParty reports whether plugins other than builtins may contribute
- // to this point yet. Builtins always may.
- ThirdParty bool `json:"thirdParty"`
- // Declarative reports whether a plugin without code (runtime
- // declarative) can contribute here: the manifest and package files
- // describe the contribution completely.
- Declarative bool `json:"declarative"`
-}
-
-// points lists every known extension point in display order. Vector stores,
-// object storage and sandboxes are deliberately absent: they are
-// infrastructure owned by WeKnora itself, not extension points.
-var points = []PointInfo{
- {Point: PointModelVendors, ThirdParty: true, Declarative: true},
- {Point: PointConnectors, ThirdParty: true},
- {Point: PointIMChannels},
- {Point: PointWebSearch, ThirdParty: true},
- {Point: PointTools},
- {Point: PointParsers, ThirdParty: true},
- {Point: PointSkills, ThirdParty: true, Declarative: true},
- {Point: PointMCPServers, ThirdParty: true, Declarative: true},
- // Pages are static files; a plugin with code can also answer their
- // requests (the ui/request endpoint).
- {Point: PointPages, ThirdParty: true, Declarative: true},
- {Point: PointSettingsSections, ThirdParty: true, Declarative: true},
- {Point: PointKBTabs, ThirdParty: true, Declarative: true},
- {Point: PointWebhooks, ThirdParty: true},
-}
-
-// Points returns every known extension point in display order.
-func Points() []PointInfo {
- out := make([]PointInfo, len(points))
- copy(out, points)
- return out
-}
-
-// LookupPoint returns the info for a point name.
-func LookupPoint(p Point) (PointInfo, bool) {
- for _, info := range points {
- if info.Point == p {
- return info, true
- }
- }
- return PointInfo{}, false
-}
diff --git a/internal/plugin/manifest/resources.go b/internal/plugin/manifest/resources.go
deleted file mode 100644
index 0ce2e34bd..000000000
--- a/internal/plugin/manifest/resources.go
+++ /dev/null
@@ -1,59 +0,0 @@
-package manifest
-
-import (
- "fmt"
- "strconv"
- "strings"
-)
-
-// ParseCPU reads a CPU amount as Kubernetes writes it ("500m", "0.5", "2")
-// and returns it in millicores.
-func ParseCPU(s string) (int64, error) {
- s = strings.TrimSpace(s)
- if strings.HasSuffix(s, "m") {
- n, err := strconv.ParseInt(strings.TrimSuffix(s, "m"), 10, 64)
- if err != nil || n <= 0 {
- return 0, fmt.Errorf("cpu %q must be millicores like 500m", s)
- }
- return n, nil
- }
- f, err := strconv.ParseFloat(s, 64)
- if err != nil || f <= 0 {
- return 0, fmt.Errorf("cpu %q must be cores like 0.5 or millicores like 500m", s)
- }
- return int64(f * 1000), nil
-}
-
-var memUnits = []struct {
- suffix string
- factor int64
-}{
- {"Ki", 1 << 10},
- {"Mi", 1 << 20},
- {"Gi", 1 << 30},
- {"Ti", 1 << 40},
- {"K", 1e3},
- {"M", 1e6},
- {"G", 1e9},
- {"T", 1e12},
-}
-
-// ParseMemory reads a memory amount as Kubernetes writes it ("512Mi",
-// "1Gi", "500M") and returns bytes.
-func ParseMemory(s string) (int64, error) {
- s = strings.TrimSpace(s)
- for _, u := range memUnits {
- if strings.HasSuffix(s, u.suffix) {
- n, err := strconv.ParseInt(strings.TrimSuffix(s, u.suffix), 10, 64)
- if err != nil || n <= 0 {
- break
- }
- return n * u.factor, nil
- }
- }
- n, err := strconv.ParseInt(s, 10, 64)
- if err != nil || n <= 0 {
- return 0, fmt.Errorf("memory %q must be an amount like 512Mi or 1Gi", s)
- }
- return n, nil
-}
diff --git a/internal/plugin/manifest/template.go b/internal/plugin/manifest/template.go
deleted file mode 100644
index 953da7e14..000000000
--- a/internal/plugin/manifest/template.go
+++ /dev/null
@@ -1,64 +0,0 @@
-package manifest
-
-import (
- "fmt"
- "regexp"
- "strings"
-)
-
-// Config scopes a template may reference.
-const (
- // ScopeConfig is the workspace's plugin configuration (config.tenant).
- ScopeConfig = "config"
- // ScopeSystem is the platform-wide plugin configuration (config.system).
- ScopeSystem = "system"
-)
-
-// templateRef matches ${scope.key}.
-var templateRef = regexp.MustCompile(`\$\{([a-zA-Z]+)\.([a-zA-Z0-9_]+)\}`)
-
-// TemplateRef is one ${scope.key} in a templated value.
-type TemplateRef struct {
- Scope string
- Key string
-}
-
-// TemplateRefs lists the references in a templated value.
-func TemplateRefs(s string) []TemplateRef {
- var out []TemplateRef
- for _, m := range templateRef.FindAllStringSubmatch(s, -1) {
- out = append(out, TemplateRef{Scope: m[1], Key: m[2]})
- }
- return out
-}
-
-// ExpandTemplate replaces each ${scope.key} with lookup's answer. It fails
-// when a referenced value is missing or empty, so a request is never sent
-// with a half-filled credential.
-func ExpandTemplate(s string, lookup func(scope, key string) (string, bool)) (string, error) {
- var missing []string
- out := templateRef.ReplaceAllStringFunc(s, func(ref string) string {
- m := templateRef.FindStringSubmatch(ref)
- v, ok := lookup(m[1], m[2])
- if !ok || v == "" {
- missing = append(missing, m[1]+"."+m[2])
- }
- return v
- })
- if len(missing) > 0 {
- return "", fmt.Errorf("not configured: %s", strings.Join(missing, ", "))
- }
- return out, nil
-}
-
-func validateTemplate(value, where string, add func(string, ...any)) {
- for _, ref := range TemplateRefs(value) {
- if ref.Scope != ScopeConfig && ref.Scope != ScopeSystem {
- add("%s references ${%s.%s}; only ${config.<key>} and ${system.<key>} are allowed",
- where, ref.Scope, ref.Key)
- }
- }
- if rest := templateRef.ReplaceAllString(value, ""); strings.Contains(rest, "${") {
- add("%s has a malformed ${...} reference", where)
- }
-}
diff --git a/internal/plugin/manifest/template_test.go b/internal/plugin/manifest/template_test.go
deleted file mode 100644
index e8e6d331a..000000000
--- a/internal/plugin/manifest/template_test.go
+++ /dev/null
@@ -1,34 +0,0 @@
-package manifest
-
-import (
- "strings"
- "testing"
-)
-
-func TestExpandTemplate(t *testing.T) {
- values := map[string]string{"config.api_key": "k-1", "system.region": "eu"}
- lookup := func(scope, key string) (string, bool) { v, ok := values[scope+"."+key]; return v, ok }
-
- got, err := ExpandTemplate("Bearer ${config.api_key} @${system.region}", lookup)
- if err != nil || got != "Bearer k-1 @eu" {
- t.Fatalf("got %q, %v", got, err)
- }
- if _, err := ExpandTemplate("Bearer ${config.token}", lookup); err == nil ||
- !strings.Contains(err.Error(), "config.token") {
- t.Fatalf("a missing value must fail, got %v", err)
- }
-}
-
-func TestValidateTemplate(t *testing.T) {
- var errs []string
- add := func(f string, _ ...any) { errs = append(errs, f) }
- validateTemplate("Bearer ${config.api_key}", "h", add)
- if len(errs) != 0 {
- t.Fatalf("valid template rejected: %v", errs)
- }
- validateTemplate("${env.HOME}", "h", add)
- validateTemplate("${config.api-key}", "h", add)
- if len(errs) != 2 {
- t.Fatalf("want 2 errors, got %v", errs)
- }
-}
diff --git a/internal/plugin/manifest/text.go b/internal/plugin/manifest/text.go
deleted file mode 100644
index 68e9a37e9..000000000
--- a/internal/plugin/manifest/text.go
+++ /dev/null
@@ -1,129 +0,0 @@
-package manifest
-
-import (
- "encoding/json"
- "errors"
- "sort"
- "strings"
-
- "gopkg.in/yaml.v3"
-)
-
-// defaultLocaleKey is the key the default text travels under in the object
-// form, next to locale keys such as "zh-CN".
-const defaultLocaleKey = "default"
-
-// LocalizedText is user-facing text with optional per-locale variants.
-//
-// Manifests may write it as a plain string ("Jira") or as an object keyed by
-// locale ({"zh-CN": "飞书", "en-US": "Feishu"}). An object without a
-// "default" key falls back to en-US, then to the first locale in sort order,
-// so every non-empty value has a default. The API always emits the object
-// form, which keeps the frontend type to a single shape.
-type LocalizedText struct {
- Default string
- Locales map[string]string
-}
-
-// Text builds a LocalizedText from a default and optional locale variants.
-// Empty variants are dropped.
-func Text(def string, locales map[string]string) LocalizedText {
- t := LocalizedText{Default: def}
- for locale, v := range locales {
- if v == "" {
- continue
- }
- if t.Locales == nil {
- t.Locales = make(map[string]string, len(locales))
- }
- t.Locales[locale] = v
- }
- return t
-}
-
-// IsZero reports whether the text carries nothing at all.
-func (t LocalizedText) IsZero() bool {
- return t.Default == "" && len(t.Locales) == 0
-}
-
-// Resolve returns the text for a locale: the exact locale, then any variant of
-// the same language ("zh" for "zh-TW"), then the default.
-func (t LocalizedText) Resolve(locale string) string {
- if v, ok := t.Locales[locale]; ok {
- return v
- }
- if lang, _, _ := strings.Cut(locale, "-"); lang != "" {
- for _, key := range t.sortedLocales() {
- if keyLang, _, _ := strings.Cut(key, "-"); strings.EqualFold(keyLang, lang) {
- return t.Locales[key]
- }
- }
- }
- return t.Default
-}
-
-func (t LocalizedText) sortedLocales() []string {
- keys := make([]string, 0, len(t.Locales))
- for k := range t.Locales {
- keys = append(keys, k)
- }
- sort.Strings(keys)
- return keys
-}
-
-// MarshalJSON emits the object form: {"default": ..., "<locale>": ...}.
-func (t LocalizedText) MarshalJSON() ([]byte, error) {
- out := make(map[string]string, len(t.Locales)+1)
- for k, v := range t.Locales {
- out[k] = v
- }
- out[defaultLocaleKey] = t.Default
- return json.Marshal(out)
-}
-
-// UnmarshalJSON accepts either a string or a locale object.
-func (t *LocalizedText) UnmarshalJSON(data []byte) error {
- var s string
- if err := json.Unmarshal(data, &s); err == nil {
- *t = LocalizedText{Default: s}
- return nil
- }
- var m map[string]string
- if err := json.Unmarshal(data, &m); err != nil {
- return errors.New("localized text must be a string or an object of locale strings")
- }
- *t = fromMap(m)
- return nil
-}
-
-// UnmarshalYAML accepts either a scalar or a mapping of locale strings.
-func (t *LocalizedText) UnmarshalYAML(node *yaml.Node) error {
- switch node.Kind {
- case yaml.ScalarNode:
- *t = LocalizedText{Default: node.Value}
- return nil
- case yaml.MappingNode:
- var m map[string]string
- if err := node.Decode(&m); err != nil {
- return err
- }
- *t = fromMap(m)
- return nil
- default:
- return errors.New("localized text must be a string or a mapping of locale strings")
- }
-}
-
-func fromMap(m map[string]string) LocalizedText {
- def := m[defaultLocaleKey]
- delete(m, defaultLocaleKey)
- t := Text(def, m)
- if t.Default == "" {
- if v, ok := t.Locales["en-US"]; ok {
- t.Default = v
- } else if keys := t.sortedLocales(); len(keys) > 0 {
- t.Default = t.Locales[keys[0]]
- }
- }
- return t
-}
diff --git a/internal/plugin/manifest/toolview.go b/internal/plugin/manifest/toolview.go
deleted file mode 100644
index 8fcba3455..000000000
--- a/internal/plugin/manifest/toolview.go
+++ /dev/null
@@ -1,151 +0,0 @@
-package manifest
-
-import (
- "encoding/json"
- "fmt"
- "regexp"
- "sort"
- "strings"
-
- "gopkg.in/yaml.v3"
-)
-
-// ServedByPlugin reports whether an mcpServers contribution is served by
-// the plugin's own process (declared without mcp.url) rather than a remote
-// MCP endpoint.
-func (c Contribution) ServedByPlugin() bool { return c.MCP == nil || c.MCP.URL == "" }
-
-// HasToolPages reports whether an mcpServers contribution shows some tool
-// results in one of the plugin's pages.
-func (c Contribution) HasToolPages() bool {
- for _, v := range c.ToolViews {
- if v.View == ToolViewPage {
- return true
- }
- }
- return false
-}
-
-// Tool result views: how the chat shows a tool's structured result.
-const (
- ToolViewTable = "table"
- ToolViewCards = "cards"
- ToolViewKV = "kv"
- ToolViewMarkdown = "markdown"
- ToolViewJSON = "json"
- // ToolViewPage renders the result in one of the plugin's pages.
- ToolViewPage = "page"
-)
-
-// ToolView maps a tool's structuredContent to a result view the chat
-// renders; plugins bring no frontend code. Paths are dotted field paths
-// into the structured content ("issues", "fields.status"); an empty Items
-// path means the content itself.
-type ToolView struct {
- View string `json:"view" yaml:"view"`
- // Items is the list a table or cards show.
- Items string `json:"items,omitempty" yaml:"items"`
- // Columns of a table, in order.
- Columns []ToolViewColumn `json:"columns,omitempty" yaml:"columns"`
- // Title, Subtitle, Body and Link name the fields of each card; Title
- // and Link also head a kv view.
- Title string `json:"title,omitempty" yaml:"title"`
- Subtitle string `json:"subtitle,omitempty" yaml:"subtitle"`
- Body string `json:"body,omitempty" yaml:"body"`
- Link string `json:"link,omitempty" yaml:"link"`
- // Fields are the rows of a kv view (all top-level fields when empty).
- Fields []ToolViewColumn `json:"fields,omitempty" yaml:"fields"`
- // Field holds the Markdown of a markdown view (the text content when
- // empty).
- Field string `json:"field,omitempty" yaml:"field"`
- // Entry is the page of a page view, an .html file under ui/. It gets
- // the tool's arguments and result when it starts.
- Entry string `json:"entry,omitempty" yaml:"entry"`
-}
-
-// ToolViewColumn is one table column or kv row: a field, its label, and
-// optionally the field holding a URL to link it to.
-type ToolViewColumn struct {
- Field string `json:"field" yaml:"field"`
- Title LocalizedText `json:"title,omitzero" yaml:"title"`
- Link string `json:"link,omitempty" yaml:"link"`
-}
-
-// UnmarshalYAML accepts a bare field name as shorthand.
-func (c *ToolViewColumn) UnmarshalYAML(n *yaml.Node) error {
- if n.Kind == yaml.ScalarNode {
- c.Field = n.Value
- return nil
- }
- type plain ToolViewColumn
- return n.Decode((*plain)(c))
-}
-
-// UnmarshalJSON accepts a bare field name as shorthand.
-func (c *ToolViewColumn) UnmarshalJSON(b []byte) error {
- var s string
- if json.Unmarshal(b, &s) == nil {
- c.Field = s
- return nil
- }
- type plain ToolViewColumn
- return json.Unmarshal(b, (*plain)(c))
-}
-
-var (
- toolNamePattern = regexp.MustCompile(`^[A-Za-z0-9_.-]{1,128}$`)
- fieldPathPattern = regexp.MustCompile(`^[A-Za-z0-9_-]+(\.[A-Za-z0-9_-]+)*$`)
-)
-
-func validateToolViews(views map[string]ToolView, where string, add func(string, ...any)) {
- names := make([]string, 0, len(views))
- for name := range views {
- names = append(names, name)
- }
- sort.Strings(names)
- for _, name := range names {
- v := views[name]
- at := fmt.Sprintf("%s.toolViews.%s", where, name)
- if !toolNamePattern.MatchString(name) {
- add("%s: %q is not a tool name", at, name)
- }
- path := func(label, p string) {
- if p != "" && !fieldPathPattern.MatchString(p) {
- add("%s.%s %q must be a dotted field path", at, label, p)
- }
- }
- path("items", v.Items)
- for _, p := range []struct{ label, value string }{
- {"title", v.Title}, {"subtitle", v.Subtitle}, {"body", v.Body}, {"link", v.Link}, {"field", v.Field},
- } {
- path(p.label, p.value)
- }
- for i, c := range append(append([]ToolViewColumn{}, v.Columns...), v.Fields...) {
- if c.Field == "" {
- add("%s: column %d needs a field", at, i)
- }
- path("field", c.Field)
- path("link", c.Link)
- }
- switch v.View {
- case ToolViewTable:
- if len(v.Columns) == 0 {
- add("%s: a table needs columns", at)
- }
- case ToolViewCards:
- if v.Title == "" {
- add("%s: cards need a title field", at)
- }
- case ToolViewKV, ToolViewMarkdown, ToolViewJSON:
- case ToolViewPage:
- switch {
- case !isPackagePath(v.Entry) || !strings.HasPrefix(v.Entry, UIRoot):
- add("%s.entry %q must be a file under %s", at, v.Entry, UIRoot)
- case !strings.HasSuffix(v.Entry, ".html"):
- add("%s.entry %q must be an .html file", at, v.Entry)
- }
- default:
- add("%s.view must be table, cards, kv, markdown, json or page", at)
- }
- }
-}
diff --git a/internal/plugin/oauth/oauth.go b/internal/plugin/oauth/oauth.go
deleted file mode 100644
index 679cf2d7d..000000000
--- a/internal/plugin/oauth/oauth.go
+++ /dev/null
@@ -1,332 +0,0 @@
-// Package oauth runs the OAuth 2.0 authorization code flow behind plugin form
-// fields that declare x-oauth. The platform's OAuth app (client ID and
-// secret in the plugin's system configuration) authorizes an account; the
-// tokens are kept sealed in plugin_oauth_connections and the field holds
-// only "oauth:<connection id>". When WeKnora calls the plugin it swaps the
-// reference for a fresh access token, refreshing it when it is about to
-// expire.
-package oauth
-
-import (
- "context"
- "crypto/rand"
- "encoding/base64"
- "encoding/json"
- "errors"
- "fmt"
- "net/http"
- "strings"
- "time"
-
- "github.com/google/uuid"
- "github.com/redis/go-redis/v9"
- "golang.org/x/oauth2"
-
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
- "github.com/Tencent/WeKnora/internal/plugin/install"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
- "github.com/Tencent/WeKnora/internal/utils"
-)
-
-// CallbackPath is where authorization servers send the browser back.
-const CallbackPath = "/api/v1/plugin-oauth/callback"
-
-// refreshBefore refreshes tokens this close to expiring.
-const refreshBefore = time.Minute
-
-// Scopes a form edits, as pluginapi.OptionsScope*.
-const (
- ScopeSystem = "system"
- ScopeTenant = "tenant"
- ScopeInstance = "instance"
-)
-
-// ErrNotConnected means a reference names no usable connection.
-var ErrNotConnected = errors.New("the OAuth connection is gone; connect again")
-
-// Target is the form field an authorization is for.
-type Target struct {
- PluginID string
- // TenantID is 0 for the system configuration.
- TenantID uint64
- Scope string
- Contribution string // "<point>/<id>" for instance forms
- Field string // dotted path
-}
-
-// Service runs the flow and resolves references.
-type Service struct {
- registry *registry.Registry
- plugins interfaces.PluginRepository
- repo interfaces.PluginOAuthRepository
- states *stateStore
- http *http.Client
- now func() time.Time
-}
-
-// NewService creates the service; rdb may be nil (single node).
-func NewService(
- reg *registry.Registry, plugins interfaces.PluginRepository, repo interfaces.PluginOAuthRepository,
- rdb *redis.Client,
-) *Service {
- cfg := utils.DefaultSSRFSafeHTTPClientConfig()
- cfg.Timeout = 30 * time.Second
- return &Service{
- registry: reg, plugins: plugins, repo: repo, states: newStateStore(rdb),
- http: utils.NewSSRFSafeHTTPClient(cfg), now: time.Now,
- }
-}
-
-// spec finds the x-oauth spec of a target's field.
-func (s *Service) spec(m *manifest.Manifest, t Target) (*configschema.OAuthSpec, error) {
- var raw json.RawMessage
- switch t.Scope {
- case ScopeSystem:
- raw = m.Config.SystemSchema
- case ScopeTenant:
- raw = m.Config.TenantSchema
- case ScopeInstance:
- point, id, _ := strings.Cut(t.Contribution, "/")
- for _, c := range m.Contributes[manifest.Point(point)] {
- if c.ID == id {
- raw = c.InstanceSchemaJSON
- }
- }
- }
- if len(raw) == 0 {
- return nil, fmt.Errorf("no %s form %q in %s", t.Scope, t.Contribution, m.ID)
- }
- schema, err := configschema.Parse(raw)
- if err != nil {
- return nil, err
- }
- spec, ok := schema.OAuthFields()[t.Field]
- if !ok {
- return nil, fmt.Errorf("field %s has no x-oauth", t.Field)
- }
- return spec, nil
-}
-
-// config builds the OAuth client, with its credentials from the plugin's
-// system configuration.
-func (s *Service) config(ctx context.Context, m *manifest.Manifest, spec *configschema.OAuthSpec, redirect string) (
- *oauth2.Config, error,
-) {
- system, _, err := install.OpenSystemConfig(ctx, s.plugins, m)
- if err != nil {
- return nil, err
- }
- lookup := func(scope, key string) (string, bool) {
- if scope != manifest.ScopeSystem {
- return "", false
- }
- v, ok := system[key].(string)
- return v, ok && v != ""
- }
- id, err := manifest.ExpandTemplate(spec.ClientID, lookup)
- if err != nil {
- return nil, fmt.Errorf("the platform has not set up this plugin's OAuth app: %w", err)
- }
- secret := ""
- if spec.ClientSecret != "" {
- if secret, err = manifest.ExpandTemplate(spec.ClientSecret, lookup); err != nil {
- return nil, fmt.Errorf("the platform has not set up this plugin's OAuth app: %w", err)
- }
- }
- return &oauth2.Config{
- ClientID: id, ClientSecret: secret, Scopes: spec.Scopes, RedirectURL: redirect,
- Endpoint: oauth2.Endpoint{AuthURL: spec.AuthorizeURL, TokenURL: spec.TokenURL},
- }, nil
-}
-
-func randomToken() string {
- b := make([]byte, 32)
- _, _ = rand.Read(b)
- return base64.RawURLEncoding.EncodeToString(b)
-}
-
-// Start begins an authorization for a field and returns the URL to open and
-// the state that will come back. origin is the app's origin, where the
-// callback page reports the result; baseURL is where the authorization
-// server reaches WeKnora.
-func (s *Service) Start(ctx context.Context, t Target, userID, origin, baseURL string) (string, string, error) {
- m, ok := s.registry.Plugin(t.PluginID)
- if !ok {
- return "", "", fmt.Errorf("plugin %s is not loaded", t.PluginID)
- }
- spec, err := s.spec(m, t)
- if err != nil {
- return "", "", err
- }
- redirect := strings.TrimSuffix(baseURL, "/") + CallbackPath
- cfg, err := s.config(ctx, m, spec, redirect)
- if err != nil {
- return "", "", err
- }
- state := randomToken()
- st := pending{Target: t, UserID: userID, Origin: origin, Redirect: redirect}
- var opts []oauth2.AuthCodeOption
- if spec.PKCE {
- st.Verifier = oauth2.GenerateVerifier()
- opts = append(opts, oauth2.S256ChallengeOption(st.Verifier))
- }
- for k, v := range spec.Params {
- opts = append(opts, oauth2.SetAuthURLParam(k, v))
- }
- opts = append(opts, oauth2.AccessTypeOffline)
- if err := s.states.put(ctx, state, st); err != nil {
- return "", "", err
- }
- return cfg.AuthCodeURL(state, opts...), state, nil
-}
-
-// Result is how an authorization ended, for the callback page.
-type Result struct {
- State string
- Origin string
- ConnectionID string
- Err error
-}
-
-// Complete exchanges the code the authorization server sent back and keeps
-// the tokens as a new connection.
-func (s *Service) Complete(ctx context.Context, state, code, errParam string) Result {
- st, ok, err := s.states.take(ctx, state)
- if err != nil || !ok {
- return Result{State: state, Err: errors.New("this authorization expired or was already used; try again")}
- }
- res := Result{State: state, Origin: st.Origin}
- if errParam != "" {
- res.Err = fmt.Errorf("the authorization was refused: %s", errParam)
- return res
- }
- m, ok := s.registry.Plugin(st.Target.PluginID)
- if !ok {
- res.Err = fmt.Errorf("plugin %s is not loaded", st.Target.PluginID)
- return res
- }
- spec, err := s.spec(m, st.Target)
- if err == nil {
- var cfg *oauth2.Config
- if cfg, err = s.config(ctx, m, spec, st.Redirect); err == nil {
- var opts []oauth2.AuthCodeOption
- if st.Verifier != "" {
- opts = append(opts, oauth2.VerifierOption(st.Verifier))
- }
- var tok *oauth2.Token
- if tok, err = cfg.Exchange(context.WithValue(ctx, oauth2.HTTPClient, s.http), code, opts...); err == nil {
- res.ConnectionID, err = s.save(ctx, st, tok)
- }
- }
- }
- if err != nil {
- logger.Warnf(ctx, "[plugin] OAuth for %s %s: %v", st.Target.PluginID, st.Target.Field, err)
- res.Err = err
- }
- return res
-}
-
-func (s *Service) save(ctx context.Context, st pending, tok *oauth2.Token) (string, error) {
- sealed, err := seal(tok)
- if err != nil {
- return "", err
- }
- now := s.now()
- c := &types.PluginOAuthConnection{
- ID: uuid.NewString(), PluginID: st.Target.PluginID, TenantID: st.Target.TenantID, Scope: st.Target.Scope,
- Contribution: st.Target.Contribution, Field: st.Target.Field, Token: sealed, CreatedBy: st.UserID,
- CreatedAt: now, UpdatedAt: now,
- }
- if !tok.Expiry.IsZero() {
- exp := tok.Expiry
- c.ExpiresAt = &exp
- }
- return c.ID, s.repo.Create(ctx, c)
-}
-
-func seal(tok *oauth2.Token) (string, error) {
- raw, err := json.Marshal(tok)
- if err != nil {
- return "", err
- }
- return utils.EncryptAESGCM(string(raw), utils.GetAESKey())
-}
-
-func open(sealed string) (*oauth2.Token, error) {
- raw, err := utils.DecryptStoredSecret(sealed)
- if err != nil {
- return nil, err
- }
- var tok oauth2.Token
- return &tok, json.Unmarshal([]byte(raw), &tok)
-}
-
-// AccessToken resolves a field value "oauth:<id>" of a plugin in a tenant to
-// a current access token, refreshing it when it is about to expire.
-func (s *Service) AccessToken(ctx context.Context, pluginID string, tenantID uint64, ref string) (string, error) {
- id := strings.TrimPrefix(ref, configschema.OAuthRefPrefix)
- c, err := s.repo.Get(ctx, pluginID, tenantID, id)
- if err != nil {
- return "", err
- }
- if c == nil {
- return "", ErrNotConnected
- }
- tok, err := open(c.Token)
- if err != nil {
- return "", err
- }
- if tok.Expiry.IsZero() || tok.Expiry.After(s.now().Add(refreshBefore)) {
- return tok.AccessToken, nil
- }
- if tok.RefreshToken == "" {
- return "", ErrNotConnected
- }
- m, ok := s.registry.Plugin(pluginID)
- if !ok {
- return "", ErrNotConnected
- }
- spec, err := s.spec(m, Target{PluginID: pluginID, Scope: c.Scope, Contribution: c.Contribution, Field: c.Field})
- if err != nil {
- return "", err
- }
- cfg, err := s.config(ctx, m, spec, "")
- if err != nil {
- return "", err
- }
- // A token source only refreshes an invalid token; ours may still be
- // valid for seconds.
- stale := *tok
- stale.AccessToken = ""
- fresh, err := cfg.TokenSource(context.WithValue(ctx, oauth2.HTTPClient, s.http), &stale).Token()
- if err != nil {
- return "", fmt.Errorf("refresh the OAuth token: %w", err)
- }
- if fresh.RefreshToken == "" {
- fresh.RefreshToken = tok.RefreshToken
- }
- sealed, err := seal(fresh)
- if err != nil {
- return "", err
- }
- prev := c.UpdatedAt
- c.Token, c.UpdatedAt = sealed, s.now()
- if !fresh.Expiry.IsZero() {
- exp := fresh.Expiry
- c.ExpiresAt = &exp
- }
- saved, err := s.repo.UpdateToken(ctx, c, prev)
- if err != nil {
- return "", err
- }
- if !saved {
- // Another node refreshed first; its token is the current one (a
- // rotated refresh token makes ours unusable).
- return s.AccessToken(ctx, pluginID, tenantID, ref)
- }
- return fresh.AccessToken, nil
-}
diff --git a/internal/plugin/oauth/oauth_test.go b/internal/plugin/oauth/oauth_test.go
deleted file mode 100644
index 14bd867ec..000000000
--- a/internal/plugin/oauth/oauth_test.go
+++ /dev/null
@@ -1,198 +0,0 @@
-package oauth
-
-import (
- "context"
- "encoding/json"
- "fmt"
- "net/http"
- "net/http/httptest"
- "net/url"
- "strings"
- "sync"
- "sync/atomic"
- "testing"
- "time"
-
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/plugintest"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/utils"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-type memConnections struct {
- mu sync.Mutex
- rows map[string]types.PluginOAuthConnection
-}
-
-func (m *memConnections) Create(_ context.Context, c *types.PluginOAuthConnection) error {
- m.mu.Lock()
- defer m.mu.Unlock()
- m.rows[c.ID] = *c
- return nil
-}
-
-func (m *memConnections) Get(
- _ context.Context,
- pluginID string,
- tenantID uint64,
- id string,
-) (*types.PluginOAuthConnection, error) {
- m.mu.Lock()
- defer m.mu.Unlock()
- c, ok := m.rows[id]
- if !ok || c.PluginID != pluginID || c.TenantID != tenantID {
- return nil, nil
- }
- return &c, nil
-}
-
-func (m *memConnections) UpdateToken(_ context.Context, c *types.PluginOAuthConnection, prev time.Time) (bool, error) {
- m.mu.Lock()
- defer m.mu.Unlock()
- if !m.rows[c.ID].UpdatedAt.Equal(prev) {
- return false, nil
- }
- m.rows[c.ID] = *c
- return true, nil
-}
-
-// provider is a fake authorization server.
-type provider struct {
- *httptest.Server
- refreshes atomic.Int32
- verifier string
-}
-
-func newProvider(t *testing.T) *provider {
- p := &provider{}
- p.Server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- _ = r.ParseForm()
- if user, pass, _ := r.BasicAuth(); user != "app-id" || pass != "app-secret" {
- if r.Form.Get("client_id") != "app-id" || r.Form.Get("client_secret") != "app-secret" {
- http.Error(w, "bad client", http.StatusUnauthorized)
- return
- }
- }
- w.Header().Set("Content-Type", "application/json")
- switch r.Form.Get("grant_type") {
- case "authorization_code":
- if r.Form.Get("code") != "the-code" {
- http.Error(w, `{"error":"invalid_grant"}`, http.StatusBadRequest)
- return
- }
- p.verifier = r.Form.Get("code_verifier")
- _, _ = fmt.Fprint(w, `{"access_token":"at-1","refresh_token":"rt-1","token_type":"bearer","expires_in":30}`)
- case "refresh_token":
- n := p.refreshes.Add(1)
- _, _ = fmt.Fprintf(w, `{"access_token":"at-%d","token_type":"bearer","expires_in":3600}`, n+1)
- }
- }))
- t.Cleanup(p.Close)
- return p
-}
-
-func setup(t *testing.T, p *provider) *Service {
- t.Helper()
- utils.SetSSRFWhitelistFromRaw("127.0.0.1,localhost")
- t.Cleanup(func() { utils.SetSSRFWhitelistFromRaw("") })
- tenantSchema, _ := json.Marshal(map[string]any{
- "type": "object",
- "properties": map[string]any{"account": map[string]any{
- "type": "string", "x-oauth": map[string]any{
- "authorizeUrl": "https://auth.example.com/authorize",
- "tokenUrl": strings.Replace(p.URL, "127.0.0.1", "localhost", 1) + "/token",
- "scopes": []string{
- "read",
- }, "clientId": "${system.client_id}", "clientSecret": "${system.client_secret}",
- "pkce": true, "params": map[string]string{"audience": "api.example.com"},
- },
- }},
- })
- systemSchema, _ := json.Marshal(map[string]any{"type": "object", "properties": map[string]any{
- "client_id": map[string]any{
- "type": "string",
- }, "client_secret": map[string]any{"type": "string", "x-secret": true},
- }})
- m := &manifest.Manifest{
- SchemaVersion: manifest.SchemaVersion, ID: "acme.jira", Version: "1.0.0", APIVersion: pluginapi.APIVersion,
- Name: manifest.Text("Jira", nil), Publisher: manifest.Publisher{ID: "acme"},
- Runtime: manifest.Runtime{Type: manifest.RuntimeHost, Kind: "binary", Entry: "bin/x"},
- Contributes: manifest.Contributions{manifest.PointWebSearch: {{ID: "x", Name: manifest.Text("X", nil)}}},
- Config: manifest.ConfigSchemas{SystemSchema: systemSchema, TenantSchema: tenantSchema},
- }
- reg := registry.New()
- if err := reg.Register(m); err != nil {
- t.Fatal(err)
- }
- plugins := plugintest.NewMemRepo()
- sys, _ := json.Marshal(map[string]any{"client_id": "app-id", "client_secret": "app-secret"})
- _ = plugins.SavePlugin(context.Background(), &types.InstalledPlugin{ID: "acme.jira", SystemConfig: types.JSON(sys)})
- return NewService(reg, plugins, &memConnections{rows: map[string]types.PluginOAuthConnection{}}, nil)
-}
-
-func TestAuthorizeExchangeAndRefresh(t *testing.T) {
- ctx := context.Background()
- p := newProvider(t)
- s := setup(t, p)
- target := Target{PluginID: "acme.jira", TenantID: 7, Scope: ScopeTenant, Field: "account"}
-
- authURL, state, err := s.Start(ctx, target, "u1", "https://app.example.com", "https://weknora.example.com")
- if err != nil {
- t.Fatal(err)
- }
- u, _ := url.Parse(authURL)
- q := u.Query()
- if u.Host != "auth.example.com" || q.Get("client_id") != "app-id" || q.Get("state") != state ||
- q.Get("code_challenge_method") != "S256" || q.Get("audience") != "api.example.com" ||
- q.Get("redirect_uri") != "https://weknora.example.com"+CallbackPath || q.Get("scope") != "read" {
- t.Fatalf("authorize URL = %s", authURL)
- }
-
- res := s.Complete(ctx, state, "the-code", "")
- if res.Err != nil || res.Origin != "https://app.example.com" || res.ConnectionID == "" || p.verifier == "" {
- t.Fatalf("complete = %+v verifier %q", res, p.verifier)
- }
- if again := s.Complete(ctx, state, "the-code", ""); again.Err == nil {
- t.Fatal("a state must not be usable twice")
- }
- ref := configschema.OAuthRefPrefix + res.ConnectionID
- if !configschema.IsOAuthRef(ref) {
- t.Fatalf("ref %s is not a reference", ref)
- }
-
- // The first token expires within the refresh window: it is refreshed,
- // keeping the refresh token.
- tok, err := s.AccessToken(ctx, "acme.jira", 7, ref)
- if err != nil || tok != "at-2" || p.refreshes.Load() != 1 {
- t.Fatalf("token = %q, %v (refreshes %d)", tok, err, p.refreshes.Load())
- }
- if tok, _ := s.AccessToken(ctx, "acme.jira", 7, ref); tok != "at-2" || p.refreshes.Load() != 1 {
- t.Fatalf("a fresh token is reused, got %q", tok)
- }
- if _, err := s.AccessToken(ctx, "acme.jira", 8, ref); err == nil {
- t.Fatal("another workspace cannot use the connection")
- }
- if raw := s.repo.(*memConnections).rows[res.ConnectionID].Token; strings.Contains(raw, "at-") &&
- utils.GetAESKey() != nil {
- t.Fatal("tokens are stored sealed")
- }
-}
-
-func TestStartRefusesFieldsWithoutOAuth(t *testing.T) {
- s := setup(t, newProvider(t))
- for _, target := range []Target{
- {PluginID: "acme.jira", TenantID: 7, Scope: ScopeTenant, Field: "other"},
- {PluginID: "acme.jira", Scope: ScopeSystem, Field: "account"},
- {PluginID: "acme.nope", TenantID: 7, Scope: ScopeTenant, Field: "account"},
- } {
- if _, _, err := s.Start(context.Background(), target, "u", "https://a", "https://a"); err == nil {
- t.Errorf("%+v started", target)
- }
- }
- if res := s.Complete(context.Background(), "unknown", "code", ""); res.Err == nil {
- t.Fatal("an unknown state completed")
- }
-}
diff --git a/internal/plugin/oauth/state.go b/internal/plugin/oauth/state.go
deleted file mode 100644
index 2b612cacd..000000000
--- a/internal/plugin/oauth/state.go
+++ /dev/null
@@ -1,94 +0,0 @@
-package oauth
-
-import (
- "context"
- "encoding/json"
- "errors"
- "os"
- "strings"
- "sync"
- "time"
-
- "github.com/redis/go-redis/v9"
-)
-
-// stateTTL bounds an authorization from start to callback.
-const stateTTL = 10 * time.Minute
-
-// pending is an authorization in flight. It holds the PKCE verifier, a
-// secret, so it lives server side and the state parameter is a random key.
-type pending struct {
- Target Target `json:"target"`
- UserID string `json:"userId"`
- Origin string `json:"origin"`
- Redirect string `json:"redirect"`
- Verifier string `json:"verifier,omitempty"`
-}
-
-// stateStore keeps pending authorizations in Redis, so the callback may land
-// on any node, or in memory on a single node.
-type stateStore struct {
- rdb *redis.Client
-
- mu sync.Mutex
- mem map[string]memState
-}
-
-type memState struct {
- value pending
- expires time.Time
-}
-
-func newStateStore(rdb *redis.Client) *stateStore {
- return &stateStore{rdb: rdb, mem: map[string]memState{}}
-}
-
-func stateKey(state string) string {
- if ns := strings.TrimSpace(os.Getenv("WEKNORA_REDIS_NAMESPACE")); ns != "" {
- return "weknora:plugin_oauth_state:" + ns + ":" + state
- }
- return "weknora:plugin_oauth_state:" + state
-}
-
-func (s *stateStore) put(ctx context.Context, state string, p pending) error {
- if s.rdb != nil {
- raw, err := json.Marshal(p)
- if err != nil {
- return err
- }
- return s.rdb.Set(ctx, stateKey(state), raw, stateTTL).Err()
- }
- s.mu.Lock()
- defer s.mu.Unlock()
- now := time.Now()
- for k, v := range s.mem {
- if now.After(v.expires) {
- delete(s.mem, k)
- }
- }
- s.mem[state] = memState{value: p, expires: now.Add(stateTTL)}
- return nil
-}
-
-// take returns a pending authorization once: a state cannot be replayed.
-func (s *stateStore) take(ctx context.Context, state string) (pending, bool, error) {
- if s.rdb != nil {
- raw, err := s.rdb.GetDel(ctx, stateKey(state)).Bytes()
- if errors.Is(err, redis.Nil) {
- return pending{}, false, nil
- }
- if err != nil {
- return pending{}, false, err
- }
- var p pending
- return p, true, json.Unmarshal(raw, &p)
- }
- s.mu.Lock()
- defer s.mu.Unlock()
- v, ok := s.mem[state]
- delete(s.mem, state)
- if !ok || time.Now().After(v.expires) {
- return pending{}, false, nil
- }
- return v.value, true, nil
-}
diff --git a/internal/plugin/pkg/pkg.go b/internal/plugin/pkg/pkg.go
deleted file mode 100644
index 01f7202f7..000000000
--- a/internal/plugin/pkg/pkg.go
+++ /dev/null
@@ -1,318 +0,0 @@
-// Package pkg reads plugin packages: zip archives (.wkp) with plugin.yaml at
-// the root. Opening a package validates the manifest, checks that the files it
-// names exist, and fingerprints the archive, so an installer only ever stores
-// packages that will load.
-package pkg
-
-import (
- "archive/zip"
- "bytes"
- "crypto/sha256"
- "encoding/hex"
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "path"
- "sort"
- "strings"
-
- "gopkg.in/yaml.v3"
-
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
-)
-
-// ManifestFile is the manifest's name at the package root.
-const ManifestFile = "plugin.yaml"
-
-// Limits keep a hostile archive from exhausting memory or disk.
-const (
- MaxArchiveBytes = 64 << 20 // compressed upload
- MaxUncompressedBytes = 256 << 20 // all files together
- MaxFileBytes = 64 << 20 // one file
- MaxFiles = 4000
-)
-
-// Package is an opened, validated plugin package.
-type Package struct {
- Manifest *manifest.Manifest
- // Digest is "sha256:<hex>" of the archive bytes; packages are stored and
- // cached under it.
- Digest string
- // Size is the archive size in bytes.
- Size int64
- files map[string][]byte
-}
-
-// Open reads and validates a package archive. Archives whose content sits in
-// a single top-level directory (as GitHub source archives do) are accepted;
-// that directory becomes the package root.
-func Open(data []byte) (*Package, error) {
- if len(data) > MaxArchiveBytes {
- return nil, fmt.Errorf("package is %d bytes, over the %d byte limit", len(data), MaxArchiveBytes)
- }
- zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
- if err != nil {
- return nil, fmt.Errorf("package is not a zip archive: %w", err)
- }
- if len(zr.File) > MaxFiles {
- return nil, fmt.Errorf("package has %d files, over the %d file limit", len(zr.File), MaxFiles)
- }
-
- files := make(map[string][]byte, len(zr.File))
- var total int64
- for _, f := range zr.File {
- if f.FileInfo().IsDir() {
- continue
- }
- name, err := cleanName(f.Name)
- if err != nil {
- return nil, err
- }
- if f.FileInfo().Mode().Type() != 0 {
- return nil, fmt.Errorf("package entry %s is not a regular file", name)
- }
- if f.UncompressedSize64 > MaxFileBytes {
- return nil, fmt.Errorf("package entry %s is over the %d byte limit", name, MaxFileBytes)
- }
- b, err := readEntry(f)
- if err != nil {
- return nil, fmt.Errorf("read %s: %w", name, err)
- }
- total += int64(len(b))
- if total > MaxUncompressedBytes {
- return nil, fmt.Errorf("package expands to over %d bytes", MaxUncompressedBytes)
- }
- files[name] = b
- }
- files = stripSingleRoot(files)
-
- raw, ok := files[ManifestFile]
- if !ok {
- return nil, fmt.Errorf("package has no %s at its root", ManifestFile)
- }
- m, err := manifest.Parse(raw)
- if err != nil {
- return nil, err
- }
- sum := sha256.Sum256(data)
- p := &Package{Manifest: m, Digest: "sha256:" + hex.EncodeToString(sum[:]), Size: int64(len(data)), files: files}
- if err := p.checkReferences(); err != nil {
- return nil, err
- }
- if err := p.loadConfigSchemas(); err != nil {
- return nil, err
- }
- return p, nil
-}
-
-// loadConfigSchemas parses the config schema files (JSON or YAML) into the
-// manifest and checks that every ${scope.key} a template uses is declared.
-func (p *Package) loadConfigSchemas() error {
- m := p.Manifest
- var errs []error
- load := func(file string) (*configschema.Schema, json.RawMessage) {
- if file == "" {
- return nil, nil
- }
- raw, err := yamlToJSON(p.files[file])
- if err != nil {
- errs = append(errs, fmt.Errorf("config schema %s: %w", file, err))
- return nil, nil
- }
- s, err := configschema.Parse(raw)
- if err != nil {
- errs = append(errs, fmt.Errorf("config schema %s: %w", file, err))
- return nil, nil
- }
- return s, raw
- }
- system, systemRaw := load(m.Config.System)
- tenant, tenantRaw := load(m.Config.Tenant)
- if len(errs) > 0 {
- return errors.Join(errs...)
- }
- m.Config.SystemSchema, m.Config.TenantSchema = systemRaw, tenantRaw
- schemas := []*configschema.Schema{system, tenant}
- for point, list := range m.Contributes {
- for i := range list {
- if list[i].InstanceSchema == "" {
- continue
- }
- s, raw := load(list[i].InstanceSchema)
- m.Contributes[point][i].InstanceSchemaJSON = raw
- schemas = append(schemas, s)
- }
- }
- if len(errs) > 0 {
- return errors.Join(errs...)
- }
-
- declared := func(s *configschema.Schema, key string) bool {
- return s != nil && s.Properties[key] != nil
- }
- for _, c := range m.Contributes[manifest.PointMCPServers] {
- if c.MCP == nil {
- continue
- }
- for name, value := range c.MCP.Headers {
- for _, ref := range manifest.TemplateRefs(value) {
- ok := (ref.Scope == manifest.ScopeConfig && declared(tenant, ref.Key)) ||
- (ref.Scope == manifest.ScopeSystem && declared(system, ref.Key))
- if !ok {
- which := "config.tenant"
- if ref.Scope == manifest.ScopeSystem {
- which = "config.system"
- }
- errs = append(errs, fmt.Errorf(
- "mcpServers.%s header %s uses ${%s.%s}, which the %s schema does not declare",
- c.ID, name, ref.Scope, ref.Key, which))
- }
- }
- }
- }
- // An OAuth app is the platform's: its client ID and secret come from the
- // system configuration.
- for _, s := range schemas {
- for path, spec := range s.OAuthFields() {
- for _, v := range []string{spec.ClientID, spec.ClientSecret} {
- for _, ref := range manifest.TemplateRefs(v) {
- if ref.Scope != manifest.ScopeSystem || !declared(system, ref.Key) {
- errs = append(errs, fmt.Errorf(
- "%s: x-oauth client credentials may only use ${system.<key>} the config.system "+
- "schema declares, not ${%s.%s}",
- path, ref.Scope, ref.Key))
- }
- }
- }
- }
- }
- return errors.Join(errs...)
-}
-
-// yamlToJSON accepts a YAML (or JSON) document and returns it as JSON.
-func yamlToJSON(data []byte) (json.RawMessage, error) {
- var v any
- if err := yaml.Unmarshal(data, &v); err != nil {
- return nil, err
- }
- return json.Marshal(v)
-}
-
-// ReadFile returns one file of the package.
-func (p *Package) ReadFile(name string) ([]byte, bool) {
- b, ok := p.files[name]
- return b, ok
-}
-
-// Files returns the files under a directory (or the file itself), sorted,
-// with paths relative to the package root. An empty dir lists every file.
-func (p *Package) Files(dir string) []string {
- dir = strings.TrimSuffix(dir, "/")
- var out []string
- for name := range p.files {
- if dir == "" || name == dir || strings.HasPrefix(name, dir+"/") {
- out = append(out, name)
- }
- }
- sort.Strings(out)
- return out
-}
-
-// checkReferences verifies that the files a manifest names are in the package.
-func (p *Package) checkReferences() error {
- var errs []error
- need := func(what, name string) {
- if _, ok := p.files[name]; !ok {
- errs = append(errs, fmt.Errorf("%s %s is not in the package", what, name))
- }
- }
- m := p.Manifest
- if m.Icon != "" {
- need("icon", m.Icon)
- }
- if m.Config.System != "" {
- need("config schema", m.Config.System)
- }
- if m.Config.Tenant != "" {
- need("config schema", m.Config.Tenant)
- }
- for _, info := range manifest.Points() {
- for _, c := range m.Contributes[info.Point] {
- switch info.Point {
- case manifest.PointSkills:
- need("skill", path.Join(c.Path, "SKILL.md"))
- case manifest.PointModelVendors:
- need("model vendor definition", c.Path)
- }
- if manifest.IsUIPoint(info.Point) && c.Entry != "" {
- need("page", c.Entry)
- }
- for _, v := range c.ToolViews {
- if v.View == manifest.ToolViewPage {
- need("tool result page", v.Entry)
- }
- }
- if c.Icon != "" {
- need("icon", c.Icon)
- }
- if c.InstanceSchema != "" {
- need("config schema", c.InstanceSchema)
- }
- }
- }
- return errors.Join(errs...)
-}
-
-func readEntry(f *zip.File) ([]byte, error) {
- rc, err := f.Open()
- if err != nil {
- return nil, err
- }
- defer func() { _ = rc.Close() }()
- // Read one byte past the limit so a lying header cannot sneak by.
- b, err := io.ReadAll(io.LimitReader(rc, MaxFileBytes+1))
- if err != nil {
- return nil, err
- }
- if len(b) > MaxFileBytes {
- return nil, errors.New("entry is over the size limit")
- }
- return b, nil
-}
-
-// cleanName normalizes an archive entry name and rejects any that would
-// escape the package root.
-func cleanName(name string) (string, error) {
- n := strings.ReplaceAll(name, "\\", "/")
- if strings.HasPrefix(n, "/") || strings.Contains(n, ":") {
- return "", fmt.Errorf("package entry %q has an absolute path", name)
- }
- clean := path.Clean(n)
- if clean == ".." || strings.HasPrefix(clean, "../") || clean == "." {
- return "", fmt.Errorf("package entry %q escapes the package root", name)
- }
- return clean, nil
-}
-
-// stripSingleRoot drops a single top-level directory shared by every file,
-// unless the manifest already sits at the root.
-func stripSingleRoot(files map[string][]byte) map[string][]byte {
- if _, ok := files[ManifestFile]; ok || len(files) == 0 {
- return files
- }
- var root string
- for name := range files {
- top, _, found := strings.Cut(name, "/")
- if !found || (root != "" && top != root) {
- return files
- }
- root = top
- }
- out := make(map[string][]byte, len(files))
- for name, b := range files {
- out[strings.TrimPrefix(name, root+"/")] = b
- }
- return out
-}
diff --git a/internal/plugin/pkg/pkg_test.go b/internal/plugin/pkg/pkg_test.go
deleted file mode 100644
index c40c2cb39..000000000
--- a/internal/plugin/pkg/pkg_test.go
+++ /dev/null
@@ -1,140 +0,0 @@
-package pkg
-
-import (
- "archive/zip"
- "bytes"
- "strings"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
-)
-
-const tenantSchema = `type: object
-properties:
- api_key: { type: string, title: API key, x-secret: true }
-required: [api_key]
-`
-
-const kitManifest = `schemaVersion: 1
-id: acme.kit
-version: 1.0.0
-name: { en-US: ACME Kit, zh-CN: ACME 工具包 }
-publisher: { id: acme }
-runtime: { type: declarative }
-config: { tenant: config/tenant.yaml }
-contributes:
- skills:
- - { id: triage, name: Triage, path: skills/triage }
- mcpServers:
- - id: search
- name: ACME Search
- mcp: { url: "https://mcp.acme.example/mcp", headers: { Authorization: "Bearer ${config.api_key}" } }
-`
-
-// zipOf builds an archive from name → content; a nil content adds a directory.
-func zipOf(t *testing.T, files map[string]string) []byte {
- t.Helper()
- var buf bytes.Buffer
- zw := zip.NewWriter(&buf)
- for name, content := range files {
- w, err := zw.Create(name)
- if err != nil {
- t.Fatal(err)
- }
- if _, err := w.Write([]byte(content)); err != nil {
- t.Fatal(err)
- }
- }
- if err := zw.Close(); err != nil {
- t.Fatal(err)
- }
- return buf.Bytes()
-}
-
-func TestOpenValidPackage(t *testing.T) {
- data := zipOf(t, map[string]string{
- "plugin.yaml": kitManifest,
- "config/tenant.yaml": tenantSchema,
- "skills/triage/SKILL.md": "---\nname: triage\n---\nTriage issues.",
- "skills/triage/notes.txt": "x",
- })
- p, err := Open(data)
- if err != nil {
- t.Fatalf("Open: %v", err)
- }
- if p.Manifest.ID != "acme.kit" || !strings.HasPrefix(p.Digest, "sha256:") || p.Size != int64(len(data)) {
- t.Fatalf("unexpected package: %+v", p)
- }
- if got := p.Files("skills/triage"); strings.Join(got, ",") != "skills/triage/SKILL.md,skills/triage/notes.txt" {
- t.Fatalf("Files = %v", got)
- }
- if !strings.Contains(string(p.Manifest.Config.TenantSchema), `"x-secret":true`) {
- t.Fatalf("tenant schema = %s", p.Manifest.Config.TenantSchema)
- }
- mcp := p.Manifest.Contributes[manifest.PointMCPServers][0].MCP
- if mcp.Headers["Authorization"] != "Bearer ${config.api_key}" {
- t.Fatalf("mcp = %+v", mcp)
- }
- again, _ := Open(data)
- if again.Digest != p.Digest {
- t.Fatal("digest must be stable")
- }
-}
-
-func TestOpenStripsSingleRootDirectory(t *testing.T) {
- p, err := Open(zipOf(t, map[string]string{
- "acme-kit-main/plugin.yaml": kitManifest,
- "acme-kit-main/config/tenant.yaml": tenantSchema,
- "acme-kit-main/skills/triage/SKILL.md": "x",
- }))
- if err != nil {
- t.Fatalf("Open: %v", err)
- }
- if _, ok := p.ReadFile("skills/triage/SKILL.md"); !ok {
- t.Fatal("the shared root directory should become the package root")
- }
-}
-
-func TestOpenRejectsBadPackages(t *testing.T) {
- cases := map[string]struct {
- files map[string]string
- raw []byte
- want string
- }{
- "not a zip": {raw: []byte("hello"), want: "not a zip"},
- "no manifest": {files: map[string]string{"README.md": "x"}, want: "no plugin.yaml"},
- "zip slip": {files: map[string]string{"plugin.yaml": kitManifest, "../evil": "x"}, want: "escapes"},
- "absolute": {files: map[string]string{"plugin.yaml": kitManifest, "/etc/passwd": "x"}, want: "absolute"},
- "missing skill": {
- files: map[string]string{"plugin.yaml": kitManifest, "config/tenant.yaml": tenantSchema},
- want: "skill skills/triage/SKILL.md is not in the package",
- },
- "invalid manifest": {files: map[string]string{"plugin.yaml": "schemaVersion: 1\nid: Bad\n"}, want: "id"},
- "declarative with code point": {
- files: map[string]string{
- "plugin.yaml": strings.Replace(kitManifest,
- "contributes:\n", "contributes:\n connectors:\n - { id: jira, name: Jira }\n", 1),
- "config/tenant.yaml": tenantSchema,
- "skills/triage/SKILL.md": "x",
- },
- want: "contributes.connectors needs code",
- },
- }
- for name, c := range cases {
- data := c.raw
- if data == nil {
- data = zipOf(t, c.files)
- }
- _, err := Open(data)
- if err == nil || !strings.Contains(err.Error(), c.want) {
- t.Errorf("%s: got %v, want error containing %q", name, err, c.want)
- }
- }
-}
-
-func TestManifestRejectsEscapingPaths(t *testing.T) {
- bad := strings.Replace(kitManifest, "path: skills/triage", "path: ../skills", 1)
- if _, err := manifest.Parse([]byte(bad)); err == nil || !strings.Contains(err.Error(), "inside the package") {
- t.Fatalf("want path error, got %v", err)
- }
-}
diff --git a/internal/plugin/plugintest/plugintest.go b/internal/plugin/plugintest/plugintest.go
deleted file mode 100644
index 3a362ca2d..000000000
--- a/internal/plugin/plugintest/plugintest.go
+++ /dev/null
@@ -1,268 +0,0 @@
-// Package plugintest has in-memory fakes and sample packages for plugin tests.
-package plugintest
-
-import (
- "archive/zip"
- "bytes"
- "context"
- "encoding/json"
- "fmt"
- "sort"
- "sync"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/plugin/pkg"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-// MemRepo is an in-memory interfaces.PluginRepository.
-type MemRepo struct {
- mu sync.Mutex
- plugins map[string]types.InstalledPlugin
- versions map[string]types.PluginVersion
-}
-
-// NewMemRepo returns an empty in-memory PluginRepository.
-func NewMemRepo() *MemRepo {
- return &MemRepo{plugins: map[string]types.InstalledPlugin{}, versions: map[string]types.PluginVersion{}}
-}
-
-// ListPlugins returns every row.
-func (m *MemRepo) ListPlugins(context.Context) ([]types.InstalledPlugin, error) {
- m.mu.Lock()
- defer m.mu.Unlock()
- var out []types.InstalledPlugin
- for _, p := range m.plugins {
- out = append(out, p)
- }
- return out, nil
-}
-
-// GetPlugin returns (nil, nil) for an unknown ID.
-func (m *MemRepo) GetPlugin(_ context.Context, id string) (*types.InstalledPlugin, error) {
- m.mu.Lock()
- defer m.mu.Unlock()
- if p, ok := m.plugins[id]; ok {
- return &p, nil
- }
- return nil, nil
-}
-
-// SavePlugin upserts a row.
-func (m *MemRepo) SavePlugin(_ context.Context, p *types.InstalledPlugin) error {
- m.mu.Lock()
- defer m.mu.Unlock()
- m.plugins[p.ID] = *p
- return nil
-}
-
-// DeletePlugin removes a row and its versions.
-func (m *MemRepo) DeletePlugin(_ context.Context, id string) error {
- m.mu.Lock()
- defer m.mu.Unlock()
- delete(m.plugins, id)
- for k, v := range m.versions {
- if v.PluginID == id {
- delete(m.versions, k)
- }
- }
- return nil
-}
-
-// ListVersions returns a plugin's versions.
-func (m *MemRepo) ListVersions(_ context.Context, id string) ([]types.PluginVersion, error) {
- m.mu.Lock()
- defer m.mu.Unlock()
- var out []types.PluginVersion
- for _, v := range m.versions {
- if v.PluginID == id {
- out = append(out, v)
- }
- }
- return out, nil
-}
-
-// GetVersion returns (nil, nil) for an unknown version.
-func (m *MemRepo) GetVersion(_ context.Context, id, version string) (*types.PluginVersion, error) {
- m.mu.Lock()
- defer m.mu.Unlock()
- if v, ok := m.versions[id+"@"+version]; ok {
- return &v, nil
- }
- return nil, nil
-}
-
-// SaveVersion upserts a version.
-func (m *MemRepo) SaveVersion(_ context.Context, v *types.PluginVersion) error {
- m.mu.Lock()
- defer m.mu.Unlock()
- m.versions[v.PluginID+"@"+v.Version] = *v
- return nil
-}
-
-// MemStore is an in-memory package store.
-type MemStore struct {
- mu sync.Mutex
- Blobs map[string][]byte
-}
-
-// Put stores a blob under mem://<digest>.
-func (s *MemStore) Put(_ context.Context, digest string, data []byte) (string, error) {
- s.mu.Lock()
- defer s.mu.Unlock()
- if s.Blobs == nil {
- s.Blobs = map[string][]byte{}
- }
- uri := "mem://" + digest
- s.Blobs[uri] = data
- return uri, nil
-}
-
-// Get returns a stored blob.
-func (s *MemStore) Get(_ context.Context, uri string) ([]byte, error) {
- s.mu.Lock()
- defer s.mu.Unlock()
- b, ok := s.Blobs[uri]
- if !ok {
- return nil, fmt.Errorf("%s not found", uri)
- }
- return b, nil
-}
-
-// Delete drops a blob.
-func (s *MemStore) Delete(_ context.Context, uri string) error {
- s.mu.Lock()
- defer s.mu.Unlock()
- delete(s.Blobs, uri)
- return nil
-}
-
-// KitPackage builds the acme.kit declarative package at a version: one
-// skill, skills/triage. The same version always yields the same bytes.
-func KitPackage(t testing.TB, version string) []byte {
- return KitPackageWith(t, version, "")
-}
-
-// KitPackageWith is KitPackage with a different skill body and extra
-// top-level manifest lines.
-func KitPackageWith(t testing.TB, version, skillBody string, manifestLines ...string) []byte {
- t.Helper()
- if skillBody == "" {
- skillBody = version
- }
- manifest := "schemaVersion: 1\nid: acme.kit\nversion: " + version + "\n" +
- "name: { en-US: ACME Kit }\npublisher: { id: acme }\nruntime: { type: declarative }\n"
- for _, line := range manifestLines {
- manifest += line + "\n"
- }
- manifest += "contributes:\n skills:\n - { id: triage, name: Triage, path: skills/triage }\n"
- return Zip(t, map[string]string{
- "plugin.yaml": manifest,
- "skills/triage/SKILL.md": "---\nname: triage\ndescription: Triage issues by severity.\n---\n" + skillBody,
- })
-}
-
-// Zip builds an archive from name → content, entries in name order.
-func Zip(t testing.TB, files map[string]string) []byte {
- t.Helper()
- names := make([]string, 0, len(files))
- for name := range files {
- names = append(names, name)
- }
- sort.Strings(names)
- var buf bytes.Buffer
- zw := zip.NewWriter(&buf)
- for _, name := range names {
- w, err := zw.Create(name)
- if err != nil {
- t.Fatal(err)
- }
- if _, err := w.Write([]byte(files[name])); err != nil {
- t.Fatal(err)
- }
- }
- if err := zw.Close(); err != nil {
- t.Fatal(err)
- }
- return buf.Bytes()
-}
-
-// Install stores a package version and points the plugin row at it.
-func Install(t testing.TB, repo *MemRepo, store *MemStore, data []byte, state string) {
- t.Helper()
- ctx := context.Background()
- p, err := pkg.Open(data)
- if err != nil {
- t.Fatal(err)
- }
- uri, _ := store.Put(ctx, p.Digest, data)
- manifestJSON, _ := json.Marshal(p.Manifest)
- _ = repo.SaveVersion(ctx, &types.PluginVersion{
- PluginID: p.Manifest.ID, Version: p.Manifest.Version, Digest: p.Digest, PackageURI: uri,
- Manifest: types.JSON(manifestJSON),
- })
- _ = repo.SavePlugin(ctx, &types.InstalledPlugin{
- ID: p.Manifest.ID, ActiveVersion: p.Manifest.Version, DesiredState: state, Runtime: "declarative",
- })
-}
-
-// MemTenantSettings is an in-memory interfaces.PluginTenantSettingRepository.
-type MemTenantSettings struct {
- mu sync.Mutex
- rows map[string]types.PluginTenantSetting
-}
-
-func tenantKey(tenantID uint64, pluginID string) string {
- return fmt.Sprintf("%d/%s", tenantID, pluginID)
-}
-
-// List returns a tenant's rows.
-func (m *MemTenantSettings) List(_ context.Context, tenantID uint64) ([]types.PluginTenantSetting, error) {
- m.mu.Lock()
- defer m.mu.Unlock()
- var out []types.PluginTenantSetting
- for _, r := range m.rows {
- if r.TenantID == tenantID {
- out = append(out, r)
- }
- }
- return out, nil
-}
-
-// Get returns (nil, nil) for a row never written.
-func (m *MemTenantSettings) Get(
- _ context.Context, tenantID uint64, pluginID string,
-) (*types.PluginTenantSetting, error) {
- m.mu.Lock()
- defer m.mu.Unlock()
- if r, ok := m.rows[tenantKey(tenantID, pluginID)]; ok {
- return &r, nil
- }
- return nil, nil
-}
-
-// Upsert inserts a row or updates the given columns.
-func (m *MemTenantSettings) Upsert(_ context.Context, s *types.PluginTenantSetting, columns ...string) error {
- m.mu.Lock()
- defer m.mu.Unlock()
- if m.rows == nil {
- m.rows = map[string]types.PluginTenantSetting{}
- }
- key := tenantKey(s.TenantID, s.PluginID)
- row, ok := m.rows[key]
- if !ok || len(columns) == 0 {
- m.rows[key] = *s
- return nil
- }
- for _, c := range columns {
- switch c {
- case "enabled":
- row.Enabled = s.Enabled
- case "config":
- row.Config = s.Config
- }
- }
- row.UpdatedBy, row.UpdatedAt = s.UpdatedBy, s.UpdatedAt
- m.rows[key] = row
- return nil
-}
diff --git a/internal/plugin/reconcile/nodes.go b/internal/plugin/reconcile/nodes.go
deleted file mode 100644
index f86e89ec0..000000000
--- a/internal/plugin/reconcile/nodes.go
+++ /dev/null
@@ -1,154 +0,0 @@
-package reconcile
-
-import (
- "context"
- "encoding/json"
- "fmt"
- "os"
- "sort"
- "strings"
- "time"
-
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/driver"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
-)
-
-// NodeStatus is one node's report on a plugin.
-type NodeStatus struct {
- Node string `json:"node"`
- Status
- // SeenAt is when the node last confirmed the report.
- SeenAt time.Time `json:"seenAt"`
-}
-
-const statusKeyBase = "weknora:plugins:status"
-
-func statusKey(pluginID string) string {
- if ns := strings.TrimSpace(os.Getenv("WEKNORA_REDIS_NAMESPACE")); ns != "" {
- return statusKeyBase + ":" + ns + ":" + pluginID
- }
- return statusKeyBase + ":" + pluginID
-}
-
-// NodeName identifies this node in status reports.
-func (r *Reconciler) NodeName() string {
- host, _ := os.Hostname()
- if host == "" {
- host = "node"
- }
- if r.role != "" {
- host = r.role + ":" + host
- }
- return host + "/" + r.instanceID[:8]
-}
-
-// publishStatuses reports every plugin this node knows about to Redis, so
-// any node can show the whole cluster. Reports refresh on every pass; a node
-// that stops reporting ages out.
-func (r *Reconciler) publishStatuses(ctx context.Context) {
- if r.rdb == nil {
- return
- }
- r.statusMu.RLock()
- snapshot := make(map[string]Status, len(r.status))
- for id, s := range r.status {
- snapshot[id] = s
- }
- r.statusMu.RUnlock()
- node, now := r.NodeName(), time.Now()
- pipe := r.rdb.Pipeline()
- for id, s := range snapshot {
- b, _ := json.Marshal(NodeStatus{Node: node, Status: s, SeenAt: now})
- pipe.HSet(ctx, statusKey(id), node, b)
- pipe.Expire(ctx, statusKey(id), r.staleAfter()*2)
- }
- if _, err := pipe.Exec(ctx); err != nil {
- logger.Warnf(ctx, "[plugin] publish node status: %v", err)
- }
-}
-
-func (r *Reconciler) forgetStatus(ctx context.Context, pluginID string) {
- if r.rdb == nil {
- return
- }
- if err := r.rdb.HDel(ctx, statusKey(pluginID), r.NodeName()).Err(); err != nil {
- logger.Warnf(ctx, "[plugin] clear node status of %s: %v", pluginID, err)
- }
-}
-
-func (r *Reconciler) staleAfter() time.Duration { return 3 * r.interval }
-
-// NodeStatuses reports a plugin on every live node; without Redis, on this
-// node only.
-func (r *Reconciler) NodeStatuses(ctx context.Context, pluginID string) ([]NodeStatus, error) {
- local, hasLocal := r.Status(pluginID)
- if r.rdb == nil {
- if !hasLocal {
- return nil, nil
- }
- return []NodeStatus{{Node: r.NodeName(), Status: local, SeenAt: time.Now()}}, nil
- }
- fields, err := r.rdb.HGetAll(ctx, statusKey(pluginID)).Result()
- if err != nil {
- return nil, fmt.Errorf("read node status: %w", err)
- }
- var out []NodeStatus
- self := r.NodeName()
- for node, raw := range fields {
- var ns NodeStatus
- if json.Unmarshal([]byte(raw), &ns) != nil || node == self || time.Since(ns.SeenAt) > r.staleAfter() {
- continue
- }
- out = append(out, ns)
- }
- if hasLocal {
- out = append(out, NodeStatus{Node: self, Status: local, SeenAt: time.Now()})
- }
- sort.Slice(out, func(i, j int) bool { return out[i].Node < out[j].Node })
- return out, nil
-}
-
-// Driver returns the status driver for plugins the reconciler loads on every
-// node (declarative and host): an instance is a node that loaded the plugin,
-// with the runtime health the node reported.
-func (r *Reconciler) Driver(rt manifest.RuntimeType) driver.Driver { return nodeDriver{r: r, rt: rt} }
-
-type nodeDriver struct {
- r *Reconciler
- rt manifest.RuntimeType
-}
-
-func (d nodeDriver) Type() manifest.RuntimeType { return d.rt }
-
-// Ensure and Remove are the reconciler's job.
-func (nodeDriver) Ensure(context.Context, *manifest.Manifest) error { return nil }
-func (nodeDriver) Remove(context.Context, string, string) error { return nil }
-
-// Resolve is not how these plugins are reached: declarative ones have no
-// endpoint and host ones are reached through the node's host manager.
-func (d nodeDriver) Resolve(_ context.Context, pluginID string, _ uint64) (driver.Endpoint, error) {
- return driver.Endpoint{}, fmt.Errorf("%s plugin %s has no shared endpoint", d.rt, pluginID)
-}
-
-func (d nodeDriver) Status(ctx context.Context, pluginID string) ([]driver.InstanceStatus, error) {
- nodes, err := d.r.NodeStatuses(ctx, pluginID)
- if err != nil {
- return nil, err
- }
- out := make([]driver.InstanceStatus, 0, len(nodes))
- for _, n := range nodes {
- state := driver.StateReady
- switch n.State {
- case StateReady:
- case StateDegraded:
- state = driver.StateDegraded
- default:
- state = driver.StateStopped
- }
- out = append(out, driver.InstanceStatus{
- Node: n.Node, Version: n.Version, State: state, Error: n.Error, UpdatedAt: n.UpdatedAt,
- })
- }
- return out, nil
-}
diff --git a/internal/plugin/reconcile/nodes_test.go b/internal/plugin/reconcile/nodes_test.go
deleted file mode 100644
index 082edae3f..000000000
--- a/internal/plugin/reconcile/nodes_test.go
+++ /dev/null
@@ -1,64 +0,0 @@
-package reconcile
-
-import (
- "context"
- "testing"
- "time"
-
- "github.com/alicebob/miniredis/v2"
- "github.com/redis/go-redis/v9"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/plugintest"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-// Two nodes share a database and Redis: installing on one reaches the other
-// through the broadcast, and each sees the other's status.
-func TestNodesConvergeThroughRedis(t *testing.T) {
- mr := miniredis.RunT(t)
- rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
- repo, store := plugintest.NewMemRepo(), &plugintest.MemStore{}
- newNode := func() (*Reconciler, *registry.Registry) {
- reg := registry.New()
- return New(Options{
- Repo: repo, Store: store, Registry: reg, CacheDir: t.TempDir(), Redis: rdb,
- Interval: time.Hour,
- }), reg
- }
- a, _ := newNode()
- b, regB := newNode()
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- a.Start(ctx)
- b.Start(ctx)
- waitFor(t, func() bool { return mr.PubSubNumSub(channel())[channel()] == 2 })
-
- plugintest.Install(t, repo, store, plugintest.KitPackage(t, "1.0.0"), types.PluginStateEnabled)
- if err := a.Reconcile(ctx); err != nil {
- t.Fatal(err)
- }
- a.Notify(ctx)
- waitFor(t, func() bool { _, ok := regB.Plugin("acme.kit"); return ok })
-
- waitFor(t, func() bool {
- nodes, err := a.NodeStatuses(ctx, "acme.kit")
- return err == nil && len(nodes) == 2
- })
- instances, err := b.Driver(manifest.RuntimeDeclarative).Status(ctx, "acme.kit")
- if err != nil || len(instances) != 2 || instances[0].State != "ready" {
- t.Fatalf("instances = %+v, %v", instances, err)
- }
-}
-
-func waitFor(t *testing.T, cond func() bool) {
- t.Helper()
- deadline := time.Now().Add(5 * time.Second)
- for !cond() {
- if time.Now().After(deadline) {
- t.Fatal("condition not reached")
- }
- time.Sleep(10 * time.Millisecond)
- }
-}
diff --git a/internal/plugin/reconcile/reconcile.go b/internal/plugin/reconcile/reconcile.go
deleted file mode 100644
index 98ecbd384..000000000
--- a/internal/plugin/reconcile/reconcile.go
+++ /dev/null
@@ -1,511 +0,0 @@
-// Package reconcile makes every node run the plugins the database says should
-// run. Installing, upgrading, disabling or removing a plugin only writes rows;
-// each node's Reconciler then loads the active version of every enabled plugin
-// into the registry, hands its contributions to the domain activators, and
-// unloads everything else. A Redis broadcast makes peers reconcile at once; a
-// periodic pass catches anything a broadcast missed.
-package reconcile
-
-import (
- "context"
- "crypto/sha256"
- "encoding/hex"
- "encoding/json"
- "errors"
- "fmt"
- "os"
- "path/filepath"
- "sort"
- "strings"
- "sync"
- "time"
-
- "github.com/google/uuid"
- "github.com/redis/go-redis/v9"
-
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/pkg"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
- "github.com/Tencent/WeKnora/internal/utils"
-)
-
-// Loaded is a plugin version loaded on this node.
-type Loaded struct {
- Manifest *manifest.Manifest
- Package *pkg.Package
- // Dir is the package extracted on local disk, for consumers that read
- // files by path (skills).
- Dir string
- // Installed is the plugin's row: where a remote plugin runs and its
- // sealed secret.
- Installed types.InstalledPlugin
-}
-
-// Activator wires one domain to plugin contributions: it registers what a
-// loaded plugin contributes (skills, MCP servers, model vendors) and removes
-// it again. Activate is called again for each new version, after Deactivate
-// for the old one.
-type Activator interface {
- Name() string
- Activate(ctx context.Context, l *Loaded) error
- Deactivate(ctx context.Context, pluginID string) error
-}
-
-// InPlaceActivator swaps an upgraded plugin itself inside Activate (the host
-// starts the new process before stopping the old one), so it is not
-// deactivated first.
-type InPlaceActivator interface {
- Activator
- ActivatesInPlace()
-}
-
-// Status is how a plugin fares on this node.
-type Status struct {
- Version string `json:"version"`
- State string `json:"state"`
- Error string `json:"error,omitempty"`
- UpdatedAt time.Time `json:"updatedAt"`
-}
-
-// Node states reported in Status.
-const (
- StateReady = "ready"
- StateFailed = "failed"
- // StateDegraded: loaded, but its process crashed or fails health checks
- // and is being restarted.
- StateDegraded = "degraded"
-)
-
-// DefaultInterval is how often a node reconciles without being told to.
-const DefaultInterval = 30 * time.Second
-
-const channelBase = "weknora:plugins:changed"
-
-func channel() string {
- if ns := strings.TrimSpace(os.Getenv("WEKNORA_REDIS_NAMESPACE")); ns != "" {
- return channelBase + ":" + ns
- }
- return channelBase
-}
-
-type changeMessage struct {
- OriginID string `json:"origin_id"`
-}
-
-// Reconciler converges this node to the installed-plugin rows.
-type Reconciler struct {
- repo interfaces.PluginRepository
- store PackageStore
- registry *registry.Registry
- cacheDir string
- rdb *redis.Client
- activators []Activator
- instanceID string
- interval time.Duration
- runtimes map[string]bool
- accept func(*manifest.Manifest) bool
- role string
-
- mu sync.Mutex // serializes passes
- loaded map[string]*Loaded
- digests map[string]string // plugin ID → loaded digest and runtime target
- // retries holds plugins whose activation failed: a process that would
- // not start, a remote service that was down. They are tried again with
- // backoff until they load or change.
- retries map[string]retry
- now func() time.Time
- statusMu sync.RWMutex
- status map[string]Status
- runOnce sync.Once
-}
-
-// retry is when a failed activation is tried again.
-type retry struct {
- key string // the load key that failed
- attempts int
- next time.Time
-}
-
-// Backoff between attempts to activate a plugin that failed.
-const (
- retryFloor = 30 * time.Second
- retryCeiling = 10 * time.Minute
-)
-
-func retryDelay(attempts int) time.Duration {
- d := retryFloor
- for i := 1; i < attempts && d < retryCeiling; i++ {
- d *= 2
- }
- return min(d, retryCeiling)
-}
-
-// Options configures a Reconciler.
-type Options struct {
- Repo interfaces.PluginRepository
- Store PackageStore
- Registry *registry.Registry
- CacheDir string
- Redis *redis.Client // nil on single-node deployments
- Activators []Activator
- Interval time.Duration
- // Runtimes limits the node to plugins of these runtimes (a standalone
- // plugin host loads host plugins only); empty means all.
- Runtimes []manifest.RuntimeType
- // Accept further limits the node by the active version's manifest (a
- // plugin host that runs python plugins only); nil accepts all.
- Accept func(*manifest.Manifest) bool
- // Role names what the node is in status reports, e.g. "plugin-host".
- Role string
-}
-
-// New creates a Reconciler.
-func New(o Options) *Reconciler {
- if o.Interval <= 0 {
- o.Interval = DefaultInterval
- }
- if o.CacheDir == "" {
- o.CacheDir = DefaultCacheDir()
- }
- var runtimes map[string]bool
- if len(o.Runtimes) > 0 {
- runtimes = map[string]bool{}
- for _, rt := range o.Runtimes {
- runtimes[string(rt)] = true
- }
- }
- return &Reconciler{
- repo: o.Repo, store: o.Store, registry: o.Registry, cacheDir: o.CacheDir, rdb: o.Redis,
- activators: o.Activators, instanceID: uuid.NewString(), interval: o.Interval,
- runtimes: runtimes, accept: o.Accept, role: o.Role,
- loaded: map[string]*Loaded{}, digests: map[string]string{}, retries: map[string]retry{},
- status: map[string]Status{}, now: time.Now,
- }
-}
-
-// DefaultCacheDir is where packages are extracted unless
-// WEKNORA_PLUGIN_CACHE_DIR says otherwise.
-func DefaultCacheDir() string {
- if dir := strings.TrimSpace(os.Getenv("WEKNORA_PLUGIN_CACHE_DIR")); dir != "" {
- return dir
- }
- return filepath.Join(os.TempDir(), "weknora-plugins")
-}
-
-// Reconcile runs one pass. It keeps going past a plugin that fails to load,
-// records the failure in Status, and returns every failure joined.
-func (r *Reconciler) Reconcile(ctx context.Context) error {
- r.mu.Lock()
- defer r.mu.Unlock()
- rows, err := r.repo.ListPlugins(ctx)
- if err != nil {
- return fmt.Errorf("list installed plugins: %w", err)
- }
- var errs []error
- desired := map[string]bool{}
- for _, row := range rows {
- if row.DesiredState != types.PluginStateEnabled || (r.runtimes != nil && !r.runtimes[row.Runtime]) ||
- !r.accepts(ctx, row) {
- continue
- }
- desired[row.ID] = true
- if err := r.ensure(ctx, row); err != nil {
- errs = append(errs, fmt.Errorf("plugin %s: %w", row.ID, err))
- r.setStatus(row.ID, Status{Version: row.ActiveVersion, State: StateFailed, Error: err.Error()})
- }
- }
- for id := range r.loaded {
- if !desired[id] {
- r.unload(ctx, id)
- }
- }
- // A plugin that never loaded has a status but no digest.
- r.statusMu.Lock()
- for id := range r.status {
- if !desired[id] {
- delete(r.status, id)
- r.forgetStatus(ctx, id)
- }
- }
- r.statusMu.Unlock()
- r.publishStatuses(ctx)
- return errors.Join(errs...)
-}
-
-// accepts applies Options.Accept to a plugin's active version. A version
-// that cannot be read is accepted, so ensure reports why.
-func (r *Reconciler) accepts(ctx context.Context, row types.InstalledPlugin) bool {
- if r.accept == nil {
- return true
- }
- v, err := r.repo.GetVersion(ctx, row.ID, row.ActiveVersion)
- if err != nil || v == nil {
- return true
- }
- var m manifest.Manifest
- if json.Unmarshal(v.Manifest, &m) != nil {
- return true
- }
- return r.accept(&m)
-}
-
-// runtimeTarget identifies where a plugin runs beyond its package: a new
-// remote URL or secret reloads the plugin like a new version would.
-func runtimeTarget(row types.InstalledPlugin) string {
- if row.RemoteURL == "" && row.RemoteSecret == "" {
- return ""
- }
- sum := sha256.Sum256([]byte(row.RemoteURL + "\x00" + row.RemoteSecret))
- return hex.EncodeToString(sum[:8])
-}
-
-// ensure loads the active version of one plugin unless it already is.
-func (r *Reconciler) ensure(ctx context.Context, row types.InstalledPlugin) error {
- v, err := r.repo.GetVersion(ctx, row.ID, row.ActiveVersion)
- if err != nil {
- return err
- }
- if v == nil {
- return fmt.Errorf("version %s is not stored", row.ActiveVersion)
- }
- loadKey := v.Digest + "|" + runtimeTarget(row)
- if r.digests[row.ID] == loadKey {
- return nil
- }
- if rt, ok := r.retries[row.ID]; ok && rt.key == loadKey && r.now().Before(rt.next) {
- return nil // still failed; its status says why
- }
- data, err := r.store.Get(ctx, v.PackageURI)
- if err != nil {
- return err
- }
- p, err := pkg.Open(data)
- if err != nil {
- return err
- }
- if p.Digest != v.Digest {
- return fmt.Errorf("stored package digest %s does not match %s", p.Digest, v.Digest)
- }
- if p.Manifest.ID != row.ID {
- return fmt.Errorf("package is plugin %s, not %s", p.Manifest.ID, row.ID)
- }
- dir, err := r.extract(p)
- if err != nil {
- return err
- }
- if err := r.registry.Replace(p.Manifest); err != nil {
- return err
- }
- l := &Loaded{Manifest: p.Manifest, Package: p, Dir: dir, Installed: row}
- var errs []error
- for _, a := range r.activators {
- _, inPlace := a.(InPlaceActivator)
- if _, had := r.loaded[row.ID]; had && !inPlace {
- if err := a.Deactivate(ctx, row.ID); err != nil {
- errs = append(errs, fmt.Errorf("%s: deactivate previous version: %w", a.Name(), err))
- }
- }
- if err := a.Activate(ctx, l); err != nil {
- errs = append(errs, fmt.Errorf("%s: %w", a.Name(), err))
- }
- }
- r.loaded[row.ID] = l
- if err := errors.Join(errs...); err != nil {
- rt := r.retries[row.ID]
- if rt.key != loadKey {
- rt = retry{key: loadKey}
- }
- rt.attempts++
- rt.next = r.now().Add(retryDelay(rt.attempts))
- r.retries[row.ID] = rt
- delete(r.digests, row.ID)
- return err
- }
- r.digests[row.ID] = loadKey
- delete(r.retries, row.ID)
- logger.Infof(ctx, "[plugin] loaded %s %s", row.ID, row.ActiveVersion)
- r.setStatus(row.ID, Status{Version: row.ActiveVersion, State: StateReady})
- return nil
-}
-
-func (r *Reconciler) unload(ctx context.Context, id string) {
- // Reverse order: routes go before the processes they route to.
- for i := len(r.activators) - 1; i >= 0; i-- {
- a := r.activators[i]
- if err := a.Deactivate(ctx, id); err != nil {
- logger.Warnf(ctx, "[plugin] %s: deactivate %s: %v", a.Name(), id, err)
- }
- }
- if err := r.registry.Unregister(id); err != nil {
- logger.Warnf(ctx, "[plugin] unregister %s: %v", id, err)
- }
- delete(r.loaded, id)
- delete(r.digests, id)
- delete(r.retries, id)
- r.statusMu.Lock()
- delete(r.status, id)
- r.statusMu.Unlock()
- r.forgetStatus(ctx, id)
- logger.Infof(ctx, "[plugin] unloaded %s", id)
-}
-
-// extract writes the package under cacheDir/<digest>, once per digest.
-func (r *Reconciler) extract(p *pkg.Package) (string, error) {
- dir := filepath.Join(r.cacheDir, strings.TrimPrefix(p.Digest, "sha256:"))
- if _, err := os.Stat(filepath.Join(dir, pkg.ManifestFile)); err == nil {
- return dir, nil
- }
- tmp := dir + ".tmp-" + uuid.NewString()[:8]
- for _, name := range p.Files("") {
- data, _ := p.ReadFile(name)
- target, err := utils.SafeJoinUnderBase(tmp, name)
- if err != nil {
- _ = os.RemoveAll(tmp)
- return "", err
- }
- if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
- _ = os.RemoveAll(tmp)
- return "", err
- }
- if err := os.WriteFile(target, data, 0o644); err != nil {
- _ = os.RemoveAll(tmp)
- return "", err
- }
- }
- if err := os.Rename(tmp, dir); err != nil {
- _ = os.RemoveAll(tmp)
- // Another pass extracted the same digest first.
- if _, statErr := os.Stat(filepath.Join(dir, pkg.ManifestFile)); statErr == nil {
- return dir, nil
- }
- return "", err
- }
- return dir, nil
-}
-
-func (r *Reconciler) setStatus(id string, s Status) {
- s.UpdatedAt = time.Now()
- r.statusMu.Lock()
- r.status[id] = s
- r.statusMu.Unlock()
-}
-
-// ReportRuntime records a loaded plugin's runtime health (a host process that
-// crashed and is restarting, or recovered) and publishes it at once.
-func (r *Reconciler) ReportRuntime(pluginID string, healthy bool, err error) {
- r.statusMu.Lock()
- s, ok := r.status[pluginID]
- if !ok || s.State == StateFailed {
- r.statusMu.Unlock()
- return
- }
- s.State, s.Error = StateReady, ""
- if !healthy {
- s.State = StateDegraded
- if err != nil {
- s.Error = err.Error()
- }
- }
- s.UpdatedAt = time.Now()
- r.status[pluginID] = s
- r.statusMu.Unlock()
- r.publishStatuses(context.Background())
-}
-
-// Status reports how one plugin fares on this node.
-func (r *Reconciler) Status(pluginID string) (Status, bool) {
- r.statusMu.RLock()
- defer r.statusMu.RUnlock()
- s, ok := r.status[pluginID]
- return s, ok
-}
-
-// Loaded returns the plugins loaded on this node, sorted by ID.
-func (r *Reconciler) Loaded() []*Loaded {
- r.mu.Lock()
- defer r.mu.Unlock()
- out := make([]*Loaded, 0, len(r.loaded))
- for _, l := range r.loaded {
- out = append(out, l)
- }
- sort.Slice(out, func(i, j int) bool { return out[i].Manifest.ID < out[j].Manifest.ID })
- return out
-}
-
-// Notify tells peer nodes to reconcile. Best effort: the periodic pass
-// catches up when Redis is down or absent.
-func (r *Reconciler) Notify(ctx context.Context) {
- if r.rdb == nil {
- return
- }
- payload, _ := json.Marshal(changeMessage{OriginID: r.instanceID})
- if err := r.rdb.Publish(ctx, channel(), payload).Err(); err != nil {
- logger.Warnf(ctx, "[plugin] publish change: %v", err)
- }
-}
-
-// Start reconciles once, then keeps reconciling on broadcasts and on a timer
-// until ctx ends. Calling it twice has no effect.
-func (r *Reconciler) Start(ctx context.Context) {
- r.runOnce.Do(func() {
- if err := r.Reconcile(ctx); err != nil {
- logger.Warnf(ctx, "[plugin] initial reconcile: %v", err)
- }
- wake := make(chan struct{}, 1)
- if r.rdb != nil {
- go r.subscribe(ctx, wake)
- }
- go r.loop(ctx, wake)
- })
-}
-
-func (r *Reconciler) loop(ctx context.Context, wake <-chan struct{}) {
- t := time.NewTicker(r.interval)
- defer t.Stop()
- for {
- select {
- case <-ctx.Done():
- return
- case <-t.C:
- case <-wake:
- }
- if err := r.Reconcile(ctx); err != nil {
- logger.Warnf(ctx, "[plugin] reconcile: %v", err)
- }
- }
-}
-
-// subscribe forwards peer broadcasts to wake, reconnecting with backoff.
-func (r *Reconciler) subscribe(ctx context.Context, wake chan<- struct{}) {
- const maxBackoff = 30 * time.Second
- backoff := time.Second
- for ctx.Err() == nil {
- sub := r.rdb.Subscribe(ctx, channel())
- if _, err := sub.Receive(ctx); err != nil {
- _ = sub.Close()
- logger.Warnf(ctx, "[plugin] subscribe: %v (retry in %s)", err, backoff)
- select {
- case <-time.After(backoff):
- case <-ctx.Done():
- return
- }
- backoff = min(backoff*2, maxBackoff)
- continue
- }
- backoff = time.Second
- for msg := range sub.Channel() {
- var m changeMessage
- if json.Unmarshal([]byte(msg.Payload), &m) == nil && m.OriginID == r.instanceID {
- continue
- }
- select {
- case wake <- struct{}{}:
- default: // a pass is already pending
- }
- }
- _ = sub.Close()
- }
-}
diff --git a/internal/plugin/reconcile/reconcile_test.go b/internal/plugin/reconcile/reconcile_test.go
deleted file mode 100644
index b21f400d7..000000000
--- a/internal/plugin/reconcile/reconcile_test.go
+++ /dev/null
@@ -1,260 +0,0 @@
-package reconcile
-
-import (
- "context"
- "fmt"
- "os"
- "path/filepath"
- "strings"
- "testing"
- "time"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/plugintest"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-// recorder is an Activator that logs its calls.
-type recorder struct {
- calls []string
- fail bool
-}
-
-func (a *recorder) Name() string { return "recorder" }
-
-func (a *recorder) Activate(_ context.Context, l *Loaded) error {
- a.calls = append(a.calls, "activate "+l.Manifest.ID+"@"+l.Manifest.Version)
- if a.fail {
- return fmt.Errorf("boom")
- }
- return nil
-}
-
-func (a *recorder) Deactivate(_ context.Context, id string) error {
- a.calls = append(a.calls, "deactivate "+id)
- return nil
-}
-
-func TestReconcileLoadsUpgradesAndUnloads(t *testing.T) {
- ctx := context.Background()
- repo, store, reg, act := plugintest.NewMemRepo(), &plugintest.MemStore{}, registry.New(), &recorder{}
- r := New(Options{Repo: repo, Store: store, Registry: reg, CacheDir: t.TempDir(), Activators: []Activator{act}})
-
- plugintest.Install(t, repo, store, plugintest.KitPackage(t, "1.0.0"), types.PluginStateEnabled)
- if err := r.Reconcile(ctx); err != nil {
- t.Fatalf("Reconcile: %v", err)
- }
- if _, ok := reg.Resolve(manifest.PointSkills, "acme.kit/triage"); !ok {
- t.Fatal("skill contribution should be registered")
- }
- loaded := r.Loaded()
- if len(loaded) != 1 {
- t.Fatalf("loaded = %d", len(loaded))
- }
- skill, err := os.ReadFile(filepath.Join(loaded[0].Dir, "skills/triage/SKILL.md"))
- if err != nil || !strings.HasSuffix(string(skill), "1.0.0") {
- t.Fatalf("extracted skill = %q, %v", skill, err)
- }
- if s, _ := r.Status("acme.kit"); s.State != StateReady || s.Version != "1.0.0" {
- t.Fatalf("status = %+v", s)
- }
-
- // A second pass with nothing changed does nothing.
- _ = r.Reconcile(ctx)
- if len(act.calls) != 1 {
- t.Fatalf("calls = %v", act.calls)
- }
-
- plugintest.Install(t, repo, store, plugintest.KitPackage(t, "1.1.0"), types.PluginStateEnabled)
- if err := r.Reconcile(ctx); err != nil {
- t.Fatalf("Reconcile upgrade: %v", err)
- }
- if m, _ := reg.Plugin("acme.kit"); m.Version != "1.1.0" {
- t.Fatalf("registry has %s", m.Version)
- }
-
- row, _ := repo.GetPlugin(ctx, "acme.kit")
- row.DesiredState = types.PluginStateDisabled
- _ = repo.SavePlugin(ctx, row)
- if err := r.Reconcile(ctx); err != nil {
- t.Fatalf("Reconcile disable: %v", err)
- }
- if _, ok := reg.Plugin("acme.kit"); ok {
- t.Fatal("disabled plugin should be unregistered")
- }
- if _, ok := r.Status("acme.kit"); ok {
- t.Fatal("unloaded plugin should have no status")
- }
- want := "activate [email protected],deactivate acme.kit,activate [email protected],deactivate acme.kit"
- if got := strings.Join(act.calls, ","); got != want {
- t.Fatalf("calls = %s", got)
- }
-}
-
-func TestReconcileReportsFailures(t *testing.T) {
- ctx := context.Background()
- repo, store, reg := plugintest.NewMemRepo(), &plugintest.MemStore{}, registry.New()
- r := New(Options{Repo: repo, Store: store, Registry: reg, CacheDir: t.TempDir()})
-
- plugintest.Install(t, repo, store, plugintest.KitPackage(t, "1.0.0"), types.PluginStateEnabled)
- // Tamper with the stored package: the digest check must refuse it.
- for uri := range store.Blobs {
- store.Blobs[uri] = plugintest.KitPackage(t, "6.6.6")
- }
- err := r.Reconcile(ctx)
- if err == nil || !strings.Contains(err.Error(), "does not match") {
- t.Fatalf("want digest error, got %v", err)
- }
- if s, _ := r.Status("acme.kit"); s.State != StateFailed {
- t.Fatalf("status = %+v", s)
- }
- if _, ok := reg.Plugin("acme.kit"); ok {
- t.Fatal("a package that failed verification must not be registered")
- }
-}
-
-func TestActivatorFailureMarksPluginFailed(t *testing.T) {
- ctx := context.Background()
- repo, store := plugintest.NewMemRepo(), &plugintest.MemStore{}
- r := New(Options{
- Repo: repo, Store: store, Registry: registry.New(), CacheDir: t.TempDir(),
- Activators: []Activator{&recorder{fail: true}},
- })
- plugintest.Install(t, repo, store, plugintest.KitPackage(t, "1.0.0"), types.PluginStateEnabled)
- if err := r.Reconcile(ctx); err == nil || !strings.Contains(err.Error(), "recorder: boom") {
- t.Fatalf("want activator error, got %v", err)
- }
- if s, _ := r.Status("acme.kit"); s.State != StateFailed || !strings.Contains(s.Error, "boom") {
- t.Fatalf("status = %+v", s)
- }
-}
-
-// A plugin whose activation failed (a service that was down) is tried again
-// with backoff, and loads once the cause is gone.
-func TestFailedActivationIsRetriedWithBackoff(t *testing.T) {
- ctx := context.Background()
- repo, store, act := plugintest.NewMemRepo(), &plugintest.MemStore{}, &recorder{fail: true}
- r := New(Options{
- Repo: repo, Store: store, Registry: registry.New(), CacheDir: t.TempDir(), Activators: []Activator{act},
- })
- now := time.Unix(1000, 0)
- r.now = func() time.Time { return now }
- plugintest.Install(t, repo, store, plugintest.KitPackage(t, "1.0.0"), types.PluginStateEnabled)
-
- if err := r.Reconcile(ctx); err == nil {
- t.Fatal("want the activation error")
- }
- now = now.Add(retryFloor - time.Second)
- _ = r.Reconcile(ctx)
- if len(act.calls) != 1 {
- t.Fatalf("retried before the backoff: %v", act.calls)
- }
- now = now.Add(time.Second)
- _ = r.Reconcile(ctx)
- if len(act.calls) != 3 { // deactivate the half-loaded plugin, activate again
- t.Fatalf("calls = %v", act.calls)
- }
- now = now.Add(retryFloor)
- _ = r.Reconcile(ctx)
- if len(act.calls) != 3 {
- t.Fatalf("the second retry should wait twice as long: %v", act.calls)
- }
-
- act.fail = false
- now = now.Add(retryFloor)
- if err := r.Reconcile(ctx); err != nil {
- t.Fatal(err)
- }
- if s, _ := r.Status("acme.kit"); s.State != StateReady {
- t.Fatalf("status = %+v", s)
- }
- _ = r.Reconcile(ctx)
- if len(act.calls) != 5 {
- t.Fatalf("a loaded plugin should stay loaded: %v", act.calls)
- }
-
- // A failed plugin that is disabled is unloaded all the same.
- act.fail = true
- plugintest.Install(t, repo, store, plugintest.KitPackage(t, "1.1.0"), types.PluginStateEnabled)
- _ = r.Reconcile(ctx)
- row, _ := repo.GetPlugin(ctx, "acme.kit")
- row.DesiredState = types.PluginStateDisabled
- _ = repo.SavePlugin(ctx, row)
- _ = r.Reconcile(ctx)
- if len(r.Loaded()) != 0 {
- t.Fatal("a disabled plugin must be unloaded even if it failed")
- }
-}
-
-func TestRetryDelay(t *testing.T) {
- if retryDelay(1) != retryFloor || retryDelay(2) != 2*retryFloor || retryDelay(50) != retryCeiling {
- t.Fatalf("delays = %s %s %s", retryDelay(1), retryDelay(2), retryDelay(50))
- }
-}
-
-// A remote plugin moved to another URL, or given a new secret, is loaded
-// again so its runtime picks the change up.
-func TestReconcileReloadsOnRuntimeTargetChange(t *testing.T) {
- ctx := context.Background()
- repo, store, reg, act := plugintest.NewMemRepo(), &plugintest.MemStore{}, registry.New(), &recorder{}
- r := New(Options{Repo: repo, Store: store, Registry: reg, CacheDir: t.TempDir(), Activators: []Activator{act}})
-
- plugintest.Install(t, repo, store, plugintest.KitPackage(t, "1.0.0"), types.PluginStateEnabled)
- row, _ := repo.GetPlugin(ctx, "acme.kit")
- row.RemoteURL, row.RemoteSecret = "https://a.example.com", "s1"
- _ = repo.SavePlugin(ctx, row)
- if err := r.Reconcile(ctx); err != nil {
- t.Fatal(err)
- }
- if got := r.Loaded()[0].Installed.RemoteURL; got != "https://a.example.com" {
- t.Fatalf("Loaded carries URL %q", got)
- }
- _ = r.Reconcile(ctx)
-
- row.RemoteURL = "https://b.example.com"
- _ = repo.SavePlugin(ctx, row)
- _ = r.Reconcile(ctx)
- row.RemoteSecret = "s2"
- _ = repo.SavePlugin(ctx, row)
- _ = r.Reconcile(ctx)
-
- if len(act.calls) != 5 || act.calls[1] != "deactivate acme.kit" {
- t.Fatalf("calls = %v, want a reload per change", act.calls)
- }
- if got := r.Loaded()[0].Installed.RemoteSecret; got != "s2" {
- t.Fatalf("Loaded carries secret %q", got)
- }
-}
-
-// A standalone plugin host loads host plugins only.
-func TestRuntimesLimitWhatANodeLoads(t *testing.T) {
- ctx := context.Background()
- repo, store, reg, act := plugintest.NewMemRepo(), &plugintest.MemStore{}, registry.New(), &recorder{}
- r := New(Options{
- Repo: repo, Store: store, Registry: reg, CacheDir: t.TempDir(), Activators: []Activator{act},
- Runtimes: []manifest.RuntimeType{manifest.RuntimeHost}, Role: "plugin-host",
- })
- plugintest.Install(t, repo, store, plugintest.KitPackage(t, "1.0.0"), types.PluginStateEnabled)
- if err := r.Reconcile(ctx); err != nil {
- t.Fatal(err)
- }
- if len(act.calls) != 0 || len(r.Loaded()) != 0 {
- t.Fatalf("a declarative plugin was loaded: %v", act.calls)
- }
- if !strings.HasPrefix(r.NodeName(), "plugin-host:") {
- t.Fatalf("node name = %s", r.NodeName())
- }
-
- // Accept filters on the manifest: nothing is loaded, nothing reported.
- r = New(Options{
- Repo: repo, Store: store, Registry: registry.New(), CacheDir: t.TempDir(), Activators: []Activator{act},
- Accept: func(m *manifest.Manifest) bool { return m.Version != "1.0.0" },
- })
- if err := r.Reconcile(ctx); err != nil || len(r.Loaded()) != 0 {
- t.Fatalf("an unaccepted plugin was loaded: %v", err)
- }
- if _, ok := r.Status("acme.kit"); ok {
- t.Fatal("an unaccepted plugin has no status here")
- }
-}
diff --git a/internal/plugin/reconcile/store.go b/internal/plugin/reconcile/store.go
deleted file mode 100644
index a9da1db56..000000000
--- a/internal/plugin/reconcile/store.go
+++ /dev/null
@@ -1,52 +0,0 @@
-package reconcile
-
-import (
- "context"
- "fmt"
- "io"
- "strings"
-
- "github.com/Tencent/WeKnora/internal/types/interfaces"
-)
-
-// PackageStore keeps plugin package archives where every node can read them.
-type PackageStore interface {
- // Put stores an archive and returns the URI to read it back.
- Put(ctx context.Context, digest string, data []byte) (string, error)
- Get(ctx context.Context, uri string) ([]byte, error)
- Delete(ctx context.Context, uri string) error
-}
-
-// fileStore stores packages through a raw FileService (the deployment's
-// object storage, or local disk on single-node setups). Packages are not
-// tenant data, so they bypass the tenant resource catalog.
-type fileStore struct {
- fs interfaces.FileService
-}
-
-// NewFileStore returns a PackageStore on top of a raw FileService.
-func NewFileStore(fs interfaces.FileService) PackageStore {
- return &fileStore{fs: fs}
-}
-
-func (s *fileStore) Put(ctx context.Context, digest string, data []byte) (string, error) {
- name := "plugin-" + strings.TrimPrefix(digest, "sha256:") + ".wkp"
- uri, err := s.fs.SaveBytes(ctx, data, 0, name, false)
- if err != nil {
- return "", fmt.Errorf("store plugin package: %w", err)
- }
- return uri, nil
-}
-
-func (s *fileStore) Get(ctx context.Context, uri string) ([]byte, error) {
- rc, err := s.fs.GetFile(ctx, uri)
- if err != nil {
- return nil, fmt.Errorf("read plugin package: %w", err)
- }
- defer func() { _ = rc.Close() }()
- return io.ReadAll(rc)
-}
-
-func (s *fileStore) Delete(ctx context.Context, uri string) error {
- return s.fs.DeleteFile(ctx, uri)
-}
diff --git a/internal/plugin/registry/registry.go b/internal/plugin/registry/registry.go
deleted file mode 100644
index cb578e574..000000000
--- a/internal/plugin/registry/registry.go
+++ /dev/null
@@ -1,218 +0,0 @@
-// Package registry indexes the plugins a WeKnora process knows about and the
-// contributions they make, so callers can list everything at one extension
-// point or resolve one contribution by ID or legacy alias.
-package registry
-
-import (
- "fmt"
- "sort"
- "sync"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
-)
-
-// Entry is one contribution together with the plugin that provides it.
-type Entry struct {
- Point manifest.Point
- // PluginID is the providing plugin; QualifiedID is "<plugin>/<local>".
- PluginID string
- QualifiedID string
- Contribution manifest.Contribution
-
- seq int // registration order, the tie-breaker after Contribution.Order
-}
-
-// Registry is safe for concurrent use. Registered manifests must not be
-// mutated afterwards; the registry hands out the same pointers.
-type Registry struct {
- mu sync.RWMutex
- plugins map[string]*manifest.Manifest
- entries map[manifest.Point][]*Entry
- // index resolves qualified IDs and builtin aliases per point.
- index map[manifest.Point]map[string]*Entry
- seq int
-}
-
-// New returns an empty registry.
-func New() *Registry {
- return &Registry{
- plugins: make(map[string]*manifest.Manifest),
- entries: make(map[manifest.Point][]*Entry),
- index: make(map[manifest.Point]map[string]*Entry),
- }
-}
-
-// Register validates a manifest and adds it with all its contributions. It
-// fails without side effects when the plugin ID is taken or a contribution ID
-// or alias collides with one already registered at the same point.
-func (r *Registry) Register(m *manifest.Manifest) error {
- if err := m.Validate(); err != nil {
- return fmt.Errorf("plugin %s: %w", m.ID, err)
- }
- r.mu.Lock()
- defer r.mu.Unlock()
- if _, ok := r.plugins[m.ID]; ok {
- return fmt.Errorf("plugin %s is already registered", m.ID)
- }
- if err := r.checkKeysLocked(m, ""); err != nil {
- return err
- }
- r.addLocked(m)
- return nil
-}
-
-// Replace swaps the registered plugin with the same ID for m in one step, or
-// registers m if the ID is new: an upgrade never leaves a window where the
-// plugin's contributions are missing. Builtins cannot be replaced.
-func (r *Registry) Replace(m *manifest.Manifest) error {
- if err := m.Validate(); err != nil {
- return fmt.Errorf("plugin %s: %w", m.ID, err)
- }
- r.mu.Lock()
- defer r.mu.Unlock()
- if old, ok := r.plugins[m.ID]; ok && old.Builtin {
- return fmt.Errorf("builtin plugin %s cannot be replaced", m.ID)
- }
- if err := r.checkKeysLocked(m, m.ID); err != nil {
- return err
- }
- r.removeLocked(m.ID)
- r.addLocked(m)
- return nil
-}
-
-// Unregister removes a plugin and its contributions. Builtins cannot be
-// removed; removing an unknown ID is a no-op.
-func (r *Registry) Unregister(id string) error {
- r.mu.Lock()
- defer r.mu.Unlock()
- if m, ok := r.plugins[id]; ok && m.Builtin {
- return fmt.Errorf("builtin plugin %s cannot be removed", id)
- }
- r.removeLocked(id)
- return nil
-}
-
-// checkKeysLocked verifies that m's IDs and aliases are free, ignoring the
-// entries of the plugin being replaced.
-func (r *Registry) checkKeysLocked(m *manifest.Manifest, replacing string) error {
- pending := make(map[manifest.Point]map[string]string)
- for _, info := range manifest.Points() {
- for _, c := range m.Contributes[info.Point] {
- for _, key := range lookupKeys(m.ID, c) {
- if owner, ok := r.index[info.Point][key]; ok && owner.PluginID != replacing {
- return fmt.Errorf("%s %q of plugin %s collides with %s",
- info.Point, key, m.ID, owner.QualifiedID)
- }
- if other, ok := pending[info.Point][key]; ok {
- return fmt.Errorf("%s %q is claimed twice by plugin %s (%s)", info.Point, key, m.ID, other)
- }
- if pending[info.Point] == nil {
- pending[info.Point] = make(map[string]string)
- }
- pending[info.Point][key] = c.ID
- }
- }
- }
- return nil
-}
-
-func (r *Registry) addLocked(m *manifest.Manifest) {
- r.plugins[m.ID] = m
- for _, info := range manifest.Points() {
- for _, c := range m.Contributes[info.Point] {
- r.seq++
- e := &Entry{
- Point: info.Point,
- PluginID: m.ID,
- QualifiedID: manifest.QualifiedID(m.ID, c.ID),
- Contribution: c,
- seq: r.seq,
- }
- r.entries[info.Point] = append(r.entries[info.Point], e)
- if r.index[info.Point] == nil {
- r.index[info.Point] = make(map[string]*Entry)
- }
- for _, key := range lookupKeys(m.ID, c) {
- r.index[info.Point][key] = e
- }
- }
- }
-}
-
-func (r *Registry) removeLocked(id string) {
- if _, ok := r.plugins[id]; !ok {
- return
- }
- delete(r.plugins, id)
- for point, list := range r.entries {
- kept := list[:0]
- for _, e := range list {
- if e.PluginID != id {
- kept = append(kept, e)
- }
- }
- r.entries[point] = kept
- }
- for _, idx := range r.index {
- for key, e := range idx {
- if e.PluginID == id {
- delete(idx, key)
- }
- }
- }
-}
-
-func lookupKeys(pluginID string, c manifest.Contribution) []string {
- return append([]string{manifest.QualifiedID(pluginID, c.ID)}, c.Aliases...)
-}
-
-// Plugins returns every registered plugin sorted by ID.
-func (r *Registry) Plugins() []*manifest.Manifest {
- r.mu.RLock()
- defer r.mu.RUnlock()
- out := make([]*manifest.Manifest, 0, len(r.plugins))
- for _, m := range r.plugins {
- out = append(out, m)
- }
- sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
- return out
-}
-
-// Plugin returns one plugin by ID.
-func (r *Registry) Plugin(id string) (*manifest.Manifest, bool) {
- r.mu.RLock()
- defer r.mu.RUnlock()
- m, ok := r.plugins[id]
- return m, ok
-}
-
-// Contributions returns every contribution at a point, sorted by
-// Contribution.Order and then registration order.
-func (r *Registry) Contributions(point manifest.Point) []Entry {
- r.mu.RLock()
- defer r.mu.RUnlock()
- out := make([]Entry, 0, len(r.entries[point]))
- for _, e := range r.entries[point] {
- out = append(out, *e)
- }
- sort.SliceStable(out, func(i, j int) bool {
- if out[i].Contribution.Order != out[j].Contribution.Order {
- return out[i].Contribution.Order < out[j].Contribution.Order
- }
- return out[i].seq < out[j].seq
- })
- return out
-}
-
-// Resolve finds a contribution by qualified ID ("acme.jira/jira") or by a
-// builtin's legacy alias ("feishu").
-func (r *Registry) Resolve(point manifest.Point, id string) (Entry, bool) {
- r.mu.RLock()
- defer r.mu.RUnlock()
- e, ok := r.index[point][id]
- if !ok {
- return Entry{}, false
- }
- return *e, true
-}
diff --git a/internal/plugin/registry/registry_test.go b/internal/plugin/registry/registry_test.go
deleted file mode 100644
index 73bb3f827..000000000
--- a/internal/plugin/registry/registry_test.go
+++ /dev/null
@@ -1,170 +0,0 @@
-package registry
-
-import (
- "strings"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
-)
-
-func builtin(id string, contributes manifest.Contributions) *manifest.Manifest {
- return &manifest.Manifest{
- SchemaVersion: manifest.SchemaVersion,
- ID: id,
- Version: "1.0.0",
- Name: manifest.Text(id, nil),
- Publisher: manifest.Publisher{ID: manifest.BuiltinPublisher},
- Builtin: true,
- Runtime: manifest.Runtime{Type: manifest.RuntimeBuiltin},
- Contributes: contributes,
- }
-}
-
-func contribution(id string, order int, aliases ...string) manifest.Contribution {
- return manifest.Contribution{ID: id, Name: manifest.Text(id, nil), Order: order, Aliases: aliases}
-}
-
-func TestRegisterAndResolve(t *testing.T) {
- r := New()
- feishu := builtin("weknora.feishu", manifest.Contributions{
- manifest.PointConnectors: {
- contribution("feishu", 2, "feishu"),
- contribution("feishu_drive", 3, "feishu_drive"),
- },
- manifest.PointIMChannels: {contribution("feishu", 1, "feishu")},
- })
- notion := builtin("weknora.notion", manifest.Contributions{
- manifest.PointConnectors: {contribution("notion", 1, "notion")},
- })
- for _, m := range []*manifest.Manifest{feishu, notion} {
- if err := r.Register(m); err != nil {
- t.Fatalf("Register(%s): %v", m.ID, err)
- }
- }
-
- // The same alias may live at two different points.
- for _, point := range []manifest.Point{manifest.PointConnectors, manifest.PointIMChannels} {
- e, ok := r.Resolve(point, "feishu")
- if !ok || e.QualifiedID != "weknora.feishu/feishu" || e.PluginID != "weknora.feishu" {
- t.Fatalf("Resolve(%s, feishu) = %+v, %v", point, e, ok)
- }
- }
- if _, ok := r.Resolve(manifest.PointConnectors, "weknora.notion/notion"); !ok {
- t.Fatal("qualified ID should resolve")
- }
- if _, ok := r.Resolve(manifest.PointWebSearch, "feishu"); ok {
- t.Fatal("alias must not leak across points")
- }
-
- var got []string
- for _, e := range r.Contributions(manifest.PointConnectors) {
- got = append(got, e.QualifiedID)
- }
- want := "weknora.notion/notion,weknora.feishu/feishu,weknora.feishu/feishu_drive"
- if strings.Join(got, ",") != want {
- t.Fatalf("connector order = %v, want %s", got, want)
- }
-
- var ids []string
- for _, m := range r.Plugins() {
- ids = append(ids, m.ID)
- }
- if strings.Join(ids, ",") != "weknora.feishu,weknora.notion" {
- t.Fatalf("plugins = %v", ids)
- }
-}
-
-func TestRegisterRejectsCollisionsAtomically(t *testing.T) {
- r := New()
- if err := r.Register(builtin("weknora.feishu", manifest.Contributions{
- manifest.PointConnectors: {contribution("feishu", 0, "feishu")},
- })); err != nil {
- t.Fatal(err)
- }
-
- clash := builtin("weknora.lark", manifest.Contributions{
- manifest.PointConnectors: {contribution("lark", 0, "lark"), contribution("other", 0, "feishu")},
- })
- err := r.Register(clash)
- if err == nil || !strings.Contains(err.Error(), "collides with weknora.feishu/feishu") {
- t.Fatalf("want collision error, got %v", err)
- }
- if _, ok := r.Plugin("weknora.lark"); ok {
- t.Fatal("failed registration must not leave the plugin behind")
- }
- if _, ok := r.Resolve(manifest.PointConnectors, "lark"); ok {
- t.Fatal("failed registration must not leave aliases behind")
- }
-
- if err := r.Register(builtin("weknora.feishu", manifest.Contributions{
- manifest.PointTools: {contribution("x", 0)},
- })); err == nil {
- t.Fatal("duplicate plugin ID must be rejected")
- }
-
- self := builtin("weknora.self", manifest.Contributions{
- manifest.PointTools: {contribution("a", 0, "same"), contribution("b", 0, "same")},
- })
- if err := r.Register(self); err == nil || !strings.Contains(err.Error(), "claimed twice") {
- t.Fatalf("want self-collision error, got %v", err)
- }
-}
-
-func TestRegisterValidates(t *testing.T) {
- m := builtin("weknora.bad", manifest.Contributions{manifest.PointTools: {{ID: "Bad ID"}}})
- if err := New().Register(m); err == nil {
- t.Fatal("invalid manifest must be rejected")
- }
-}
-
-func thirdParty(id, version string, contributes manifest.Contributions) *manifest.Manifest {
- publisher, _, _ := strings.Cut(id, ".")
- return &manifest.Manifest{
- SchemaVersion: manifest.SchemaVersion, ID: id, Version: version, Name: manifest.Text(id, nil),
- Publisher: manifest.Publisher{ID: publisher}, Runtime: manifest.Runtime{Type: manifest.RuntimeDeclarative},
- Contributes: contributes,
- }
-}
-
-func TestReplaceAndUnregister(t *testing.T) {
- r := New()
- skill := func(id string) manifest.Contribution {
- return manifest.Contribution{ID: id, Name: manifest.Text(id, nil), Path: "skills/" + id}
- }
- if err := r.Register(builtin("weknora.core", manifest.Contributions{
- manifest.PointTools: {contribution("thinking", 0, "thinking")},
- })); err != nil {
- t.Fatal(err)
- }
- v1 := thirdParty("acme.kit", "1.0.0", manifest.Contributions{manifest.PointSkills: {skill("triage"), skill("old")}})
- if err := r.Replace(v1); err != nil {
- t.Fatalf("Replace as first install: %v", err)
- }
- v2 := thirdParty("acme.kit", "2.0.0", manifest.Contributions{manifest.PointSkills: {skill("triage"), skill("new")}})
- if err := r.Replace(v2); err != nil {
- t.Fatalf("upgrade: %v", err)
- }
- if m, _ := r.Plugin("acme.kit"); m.Version != "2.0.0" {
- t.Fatalf("version = %s", m.Version)
- }
- if _, ok := r.Resolve(manifest.PointSkills, "acme.kit/old"); ok {
- t.Fatal("contributions dropped by the upgrade must be gone")
- }
- if got := len(r.Contributions(manifest.PointSkills)); got != 2 {
- t.Fatalf("skills after upgrade = %d, want 2", got)
- }
- if err := r.Unregister("acme.kit"); err != nil {
- t.Fatal(err)
- }
- if _, ok := r.Plugin("acme.kit"); ok || len(r.Contributions(manifest.PointSkills)) != 0 {
- t.Fatal("unregister must remove the plugin and its contributions")
- }
- if err := r.Unregister("weknora.core"); err == nil {
- t.Fatal("builtins cannot be removed")
- }
- if err := r.Replace(builtin("weknora.core", manifest.Contributions{
- manifest.PointTools: {contribution("thinking", 0)},
- })); err == nil {
- t.Fatal("builtins cannot be replaced")
- }
-}
diff --git a/internal/plugin/remote/remote.go b/internal/plugin/remote/remote.go
deleted file mode 100644
index bf07af9dc..000000000
--- a/internal/plugin/remote/remote.go
+++ /dev/null
@@ -1,248 +0,0 @@
-// Package remote reaches remote-runtime plugins: HTTP services an
-// administrator runs somewhere else and registers by URL. Every request is
-// signed with the plugin's shared secret, so the service can tell WeKnora
-// from anyone else who can reach it.
-package remote
-
-import (
- "context"
- "errors"
- "fmt"
- "sync"
- "time"
-
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/host"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/utils"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-const (
- healthInterval = 15 * time.Second
- checkTimeout = 10 * time.Second
-)
-
-// Manager keeps a client for every loaded remote plugin and health-checks
-// it. It is a reconcile.Activator and, like the embedded host, must come
-// before the activators that route calls to the plugins.
-type Manager struct {
- newClient func(url string, secret []byte) *client.Client
- interval time.Duration
-
- mu sync.Mutex
- reporter host.StateReporter
- endpoints map[string]*endpoint
-}
-
-// endpoint is one registered remote plugin.
-type endpoint struct {
- m *manifest.Manifest
- c *client.Client
- cancel context.CancelFunc
- done chan struct{}
-
- mu sync.Mutex
- // err is why the service cannot be called right now: unreachable,
- // or serving something other than the installed package.
- err error
-}
-
-// NewManager creates a Manager whose clients refuse private addresses
-// unless SSRF_WHITELIST allows them.
-func NewManager() *Manager {
- cfg := utils.DefaultSSRFSafeHTTPClientConfig()
- cfg.Timeout = 0 // calls are bounded by their context; syncs stream
- cfg.SameOriginRedirectsOnly = true
- httpClient := utils.NewSSRFSafeHTTPClient(cfg)
- return &Manager{
- newClient: func(url string, secret []byte) *client.Client {
- return client.New(url, httpClient, client.Signed(secret))
- },
- interval: healthInterval,
- endpoints: map[string]*endpoint{},
- }
-}
-
-// SetReporter wires health changes to the reconciler.
-func (m *Manager) SetReporter(r host.StateReporter) {
- m.mu.Lock()
- m.reporter = r
- m.mu.Unlock()
-}
-
-// Name implements reconcile.Activator.
-func (m *Manager) Name() string { return "remote" }
-
-// ActivatesInPlace implements reconcile.InPlaceActivator: a new URL or
-// version takes over from the old endpoint without a gap.
-func (m *Manager) ActivatesInPlace() {}
-
-// Activate checks that the registered service is up and serves the
-// installed package, then keeps health-checking it. Other runtimes are
-// ignored.
-func (m *Manager) Activate(ctx context.Context, l *reconcile.Loaded) error {
- if l.Manifest.Runtime.Type != manifest.RuntimeRemote {
- return nil
- }
- url := l.Installed.RemoteURL
- if url == "" {
- return errors.New("no service URL is registered for this remote plugin")
- }
- if err := utils.ValidateURLForSSRF(url); err != nil {
- return fmt.Errorf("service URL is not allowed: %w", err)
- }
- secret, err := utils.DecryptStoredSecret(l.Installed.RemoteSecret)
- if err != nil {
- return fmt.Errorf("decrypt the plugin secret: %w", err)
- }
- if secret == "" {
- return errors.New("the remote plugin has no signing secret")
- }
- c := m.newClient(url, []byte(secret))
- cctx, cancel := context.WithTimeout(ctx, checkTimeout)
- err = verify(cctx, c, l.Manifest)
- cancel()
- if err != nil {
- c.Close()
- return err
- }
-
- id := l.Manifest.ID
- wctx, stop := context.WithCancel(context.Background())
- e := &endpoint{m: l.Manifest, c: c, cancel: stop, done: make(chan struct{})}
- m.mu.Lock()
- old := m.endpoints[id]
- m.endpoints[id] = e
- m.mu.Unlock()
- if old != nil {
- old.close()
- }
- go m.watch(wctx, id, e)
- logger.Infof(ctx, "[plugin] remote %s %s at %s", id, l.Manifest.Version, url)
- return nil
-}
-
-// verify checks that the service is healthy and is the installed package.
-func verify(ctx context.Context, c *client.Client, want *manifest.Manifest) error {
- if err := c.Health(ctx); err != nil {
- return fmt.Errorf("plugin service is not healthy: %w", err)
- }
- got, err := c.Manifest(ctx)
- if err != nil {
- return fmt.Errorf("read plugin manifest: %w", err)
- }
- return host.CheckServedManifest(want, got)
-}
-
-// watch health-checks the service until the endpoint is replaced or
-// removed. After an outage it checks the manifest again: the service may
-// have been redeployed with another version.
-func (m *Manager) watch(ctx context.Context, id string, e *endpoint) {
- defer close(e.done)
- t := time.NewTicker(m.interval)
- defer t.Stop()
- for {
- select {
- case <-ctx.Done():
- return
- case <-t.C:
- }
- cctx, cancel := context.WithTimeout(ctx, checkTimeout)
- var err error
- if e.failing() {
- err = verify(cctx, e.c, e.m)
- } else if err = e.c.Health(cctx); err != nil {
- err = fmt.Errorf("plugin service is not healthy: %w", err)
- }
- cancel()
- if ctx.Err() != nil {
- return
- }
- if e.setErr(err) {
- m.report(id, err)
- }
- }
-}
-
-func (m *Manager) report(id string, err error) {
- m.mu.Lock()
- r := m.reporter
- m.mu.Unlock()
- if r != nil {
- r.ReportRuntime(id, err == nil, err)
- }
-}
-
-func (e *endpoint) failing() bool {
- e.mu.Lock()
- defer e.mu.Unlock()
- return e.err != nil
-}
-
-// setErr records the latest check and says whether health changed.
-func (e *endpoint) setErr(err error) bool {
- e.mu.Lock()
- defer e.mu.Unlock()
- changed := (e.err == nil) != (err == nil)
- e.err = err
- return changed
-}
-
-func (e *endpoint) close() {
- e.cancel()
- <-e.done
- e.c.Close()
-}
-
-// Deactivate implements reconcile.Activator.
-func (m *Manager) Deactivate(ctx context.Context, pluginID string) error {
- m.mu.Lock()
- e := m.endpoints[pluginID]
- delete(m.endpoints, pluginID)
- m.mu.Unlock()
- if e != nil {
- e.close()
- logger.Infof(ctx, "[plugin] remote %s unregistered", pluginID)
- }
- return nil
-}
-
-// Client returns the client of a registered remote plugin. The error is a
-// retryable pluginapi unavailable error while the service fails its checks.
-func (m *Manager) Client(pluginID string) (*client.Client, error) {
- m.mu.Lock()
- e := m.endpoints[pluginID]
- m.mu.Unlock()
- if e == nil {
- return nil, pluginapi.Errorf(pluginapi.CodeUnavailable, "remote plugin %s is not registered", pluginID)
- }
- e.mu.Lock()
- err := e.err
- e.mu.Unlock()
- if err != nil {
- return nil, pluginapi.Errorf(pluginapi.CodeUnavailable, "%v", err)
- }
- return e.c, nil
-}
-
-// Owns says whether a plugin is registered here.
-func (m *Manager) Owns(pluginID string) bool {
- m.mu.Lock()
- defer m.mu.Unlock()
- _, ok := m.endpoints[pluginID]
- return ok
-}
-
-// Close unregisters every plugin, for shutdown.
-func (m *Manager) Close() {
- m.mu.Lock()
- eps := m.endpoints
- m.endpoints = map[string]*endpoint{}
- m.mu.Unlock()
- for _, e := range eps {
- e.close()
- }
-}
diff --git a/internal/plugin/remote/remote_test.go b/internal/plugin/remote/remote_test.go
deleted file mode 100644
index 1c9adf8d1..000000000
--- a/internal/plugin/remote/remote_test.go
+++ /dev/null
@@ -1,196 +0,0 @@
-package remote
-
-import (
- "bytes"
- "context"
- "errors"
- "io"
- "net/http"
- "net/http/httptest"
- "strings"
- "sync"
- "sync/atomic"
- "testing"
- "time"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/reconcile"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/utils"
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// service is a remote plugin that checks WeKnora's signature and can be
-// taken down.
-type service struct {
- *httptest.Server
- down atomic.Bool
-}
-
-func newService(t *testing.T, version, secret string) *service {
- t.Helper()
- p := pluginsdk.New(pluginsdk.Info{ID: "acme.search", Version: version})
- p.WebSearch("web", pluginsdk.WebSearchFunc(
- func(context.Context, *pluginsdk.Call, pluginapi.SearchInput) (*pluginapi.SearchOutput, error) {
- return &pluginapi.SearchOutput{}, nil
- }))
- h := p.Handler()
- s := &service{}
- s.Server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if s.down.Load() {
- http.Error(w, "down", http.StatusBadGateway)
- return
- }
- body, _ := io.ReadAll(r.Body)
- err := pluginapi.VerifySignature([]byte(secret), r.Header.Get(pluginapi.TimestampHeader),
- r.Header.Get(pluginapi.SignatureHeader), body, time.Now())
- if err != nil {
- http.Error(w, err.Error(), http.StatusUnauthorized)
- return
- }
- r.Body = io.NopCloser(bytes.NewReader(body))
- h.ServeHTTP(w, r)
- }))
- t.Cleanup(s.Close)
- return s
-}
-
-type reports struct {
- mu sync.Mutex
- got []bool
-}
-
-func (r *reports) ReportRuntime(_ string, healthy bool, _ error) {
- r.mu.Lock()
- r.got = append(r.got, healthy)
- r.mu.Unlock()
-}
-
-func (r *reports) seen() []bool {
- r.mu.Lock()
- defer r.mu.Unlock()
- return append([]bool(nil), r.got...)
-}
-
-func loaded(version, url, secret string) *reconcile.Loaded {
- return &reconcile.Loaded{
- Manifest: &manifest.Manifest{
- ID: "acme.search", Version: version, APIVersion: manifest.ExtensionAPIVersion,
- Runtime: manifest.Runtime{Type: manifest.RuntimeRemote},
- Contributes: map[manifest.Point][]manifest.Contribution{
- manifest.PointWebSearch: {{ID: "web"}},
- },
- },
- Installed: types.InstalledPlugin{ID: "acme.search", RemoteURL: url, RemoteSecret: secret},
- }
-}
-
-func allowLoopback(t *testing.T) {
- utils.SetSSRFWhitelistFromRaw("127.0.0.1")
- t.Cleanup(func() { utils.SetSSRFWhitelistFromRaw("") })
-}
-
-func TestActivateVerifiesAndSignsCalls(t *testing.T) {
- allowLoopback(t)
- ctx := context.Background()
- svc := newService(t, "1.0.0", "s3cret")
- m := NewManager()
- defer m.Close()
-
- if err := m.Activate(ctx, loaded("1.0.0", svc.URL, "wrong")); err == nil ||
- !strings.Contains(err.Error(), "not healthy") {
- t.Fatalf("wrong secret = %v", err)
- }
- if err := m.Activate(ctx, loaded("2.0.0", svc.URL, "s3cret")); err == nil ||
- !strings.Contains(err.Error(), "the package is [email protected]") {
- t.Fatalf("other version = %v", err)
- }
- if err := m.Activate(ctx, loaded("1.0.0", "", "s3cret")); err == nil {
- t.Fatal("no URL should fail")
- }
- if err := m.Activate(ctx, loaded("1.0.0", svc.URL, "s3cret")); err != nil {
- t.Fatal(err)
- }
- c, err := m.Client("acme.search")
- if err != nil {
- t.Fatal(err)
- }
- var out pluginapi.SearchOutput
- if err := c.Call(ctx, pluginapi.SearchPath("web"), pluginapi.Envelope{}, pluginapi.SearchInput{Query: "q"},
- &out); err != nil {
- t.Fatal(err)
- }
-
- if err := m.Deactivate(ctx, "acme.search"); err != nil {
- t.Fatal(err)
- }
- if _, err := m.Client("acme.search"); err == nil {
- t.Fatal("a removed plugin should have no client")
- }
-}
-
-func TestActivateRefusesPrivateAddresses(t *testing.T) {
- svc := newService(t, "1.0.0", "s3cret")
- m := NewManager()
- defer m.Close()
- err := m.Activate(context.Background(), loaded("1.0.0", svc.URL, "s3cret"))
- if err == nil || !strings.Contains(err.Error(), "not allowed") {
- t.Fatalf("loopback without whitelist = %v", err)
- }
-}
-
-func TestSealedSecret(t *testing.T) {
- allowLoopback(t)
- t.Setenv("SYSTEM_AES_KEY", strings.Repeat("k", 32))
- sealed, err := utils.EncryptAESGCM("s3cret", utils.GetAESKey())
- if err != nil {
- t.Fatal(err)
- }
- svc := newService(t, "1.0.0", "s3cret")
- m := NewManager()
- defer m.Close()
- if err := m.Activate(context.Background(), loaded("1.0.0", svc.URL, sealed)); err != nil {
- t.Fatal(err)
- }
-}
-
-func TestHealthChecksReportOutages(t *testing.T) {
- allowLoopback(t)
- svc := newService(t, "1.0.0", "s3cret")
- m := NewManager()
- m.interval = 10 * time.Millisecond
- rep := &reports{}
- m.SetReporter(rep)
- defer m.Close()
- if err := m.Activate(context.Background(), loaded("1.0.0", svc.URL, "s3cret")); err != nil {
- t.Fatal(err)
- }
-
- svc.down.Store(true)
- waitFor(t, func() bool { return len(rep.seen()) == 1 })
- var pe *pluginapi.Error
- if _, err := m.Client("acme.search"); !errors.As(err, &pe) || !pe.Retryable {
- t.Fatalf("client while down = %v", err)
- }
-
- svc.down.Store(false)
- waitFor(t, func() bool { return len(rep.seen()) == 2 })
- if got := rep.seen(); got[0] || !got[1] {
- t.Fatalf("reports = %v, want degraded then healthy", got)
- }
- if _, err := m.Client("acme.search"); err != nil {
- t.Fatal(err)
- }
-}
-
-func waitFor(t *testing.T, cond func() bool) {
- t.Helper()
- deadline := time.Now().Add(5 * time.Second)
- for !cond() {
- if time.Now().After(deadline) {
- t.Fatal("timed out")
- }
- time.Sleep(5 * time.Millisecond)
- }
-}
diff --git a/internal/plugin/tenancy/config.go b/internal/plugin/tenancy/config.go
deleted file mode 100644
index aee1ec0cd..000000000
--- a/internal/plugin/tenancy/config.go
+++ /dev/null
@@ -1,128 +0,0 @@
-package tenancy
-
-import (
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "time"
-
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-// ErrNoTenantConfig is returned for a plugin without a config.tenant schema.
-var ErrNoTenantConfig = errors.New("this plugin has no workspace configuration")
-
-// TenantConfig is a workspace's plugin configuration as the UI sees it.
-type TenantConfig struct {
- Schema json.RawMessage `json:"schema"`
- // Values has secrets redacted.
- Values map[string]any `json:"values"`
- UpdatedAt time.Time `json:"updatedAt,omitzero"`
-}
-
-func (s *Service) tenantSchema(pluginID string) (*configschema.Schema, json.RawMessage, error) {
- m, ok := s.registry.Plugin(pluginID)
- if !ok {
- return nil, nil, ErrUnknownPlugin
- }
- raw := m.Config.TenantSchema
- if len(raw) == 0 {
- return nil, nil, ErrNoTenantConfig
- }
- schema, err := configschema.Parse(raw)
- if err != nil {
- return nil, nil, fmt.Errorf("plugin %s tenant schema: %w", pluginID, err)
- }
- return schema, raw, nil
-}
-
-func storedValues(row *types.PluginTenantSetting) map[string]any {
- if row == nil || len(row.Config) == 0 {
- return map[string]any{}
- }
- var v map[string]any
- if json.Unmarshal(row.Config, &v) != nil || v == nil {
- return map[string]any{}
- }
- return v
-}
-
-// Config returns the tenant's configuration of a plugin, secrets redacted.
-func (s *Service) Config(ctx context.Context, tenantID uint64, pluginID string) (*TenantConfig, error) {
- schema, raw, err := s.tenantSchema(pluginID)
- if err != nil {
- return nil, err
- }
- row, err := s.repo.Get(ctx, tenantID, pluginID)
- if err != nil {
- return nil, err
- }
- out := &TenantConfig{Schema: raw, Values: configschema.Redact(schema, storedValues(row))}
- if row != nil {
- out.UpdatedAt = row.UpdatedAt
- }
- return out, nil
-}
-
-// SetConfig validates and stores the tenant's configuration of a plugin.
-// Secrets sent back redacted keep their stored value. Validation failures
-// come back as configschema.FieldErrors.
-func (s *Service) SetConfig(
- ctx context.Context, tenantID uint64, pluginID string, values map[string]any, updatedBy string,
-) (*TenantConfig, error) {
- schema, _, err := s.tenantSchema(pluginID)
- if err != nil {
- return nil, err
- }
- row, err := s.repo.Get(ctx, tenantID, pluginID)
- if err != nil {
- return nil, err
- }
- sealed, err := configschema.Update(schema, storedValues(row), values)
- if err != nil {
- return nil, err
- }
- b, err := json.Marshal(sealed)
- if err != nil {
- return nil, err
- }
- enabled := row != nil && row.Enabled
- if row == nil {
- m, _ := s.registry.Plugin(pluginID)
- enabled = enabledByDefault(m)
- }
- if err := s.repo.Upsert(ctx, &types.PluginTenantSetting{
- TenantID: tenantID, PluginID: pluginID, Enabled: enabled, Config: types.JSON(b),
- UpdatedBy: updatedBy, UpdatedAt: time.Now(),
- }, "config"); err != nil {
- return nil, err
- }
- return s.Config(ctx, tenantID, pluginID)
-}
-
-// OpenConfig returns the tenant's configuration with secrets decrypted, for
-// code about to use it, and when it last changed.
-func (s *Service) OpenConfig(ctx context.Context, tenantID uint64, pluginID string) (map[string]any, time.Time, error) {
- schema, _, err := s.tenantSchema(pluginID)
- if errors.Is(err, ErrNoTenantConfig) {
- return map[string]any{}, time.Time{}, nil
- }
- if err != nil {
- return nil, time.Time{}, err
- }
- row, err := s.repo.Get(ctx, tenantID, pluginID)
- if err != nil {
- return nil, time.Time{}, err
- }
- values, err := configschema.Open(schema, storedValues(row))
- if err != nil {
- return nil, time.Time{}, err
- }
- var updated time.Time
- if row != nil {
- updated = row.UpdatedAt
- }
- return values, updated, nil
-}
diff --git a/internal/plugin/tenancy/tenancy.go b/internal/plugin/tenancy/tenancy.go
deleted file mode 100644
index 8d4a7659a..000000000
--- a/internal/plugin/tenancy/tenancy.go
+++ /dev/null
@@ -1,144 +0,0 @@
-// Package tenancy applies per-tenant plugin switches: which plugins a tenant
-// has enabled, and so which contributions its integrations may offer.
-package tenancy
-
-import (
- "context"
- "errors"
- "time"
-
- "github.com/Tencent/WeKnora/internal/logger"
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/types"
- "github.com/Tencent/WeKnora/internal/types/interfaces"
-)
-
-var (
- // ErrUnknownPlugin is returned for a plugin ID the registry does not know.
- ErrUnknownPlugin = errors.New("unknown plugin")
- // ErrRequiredPlugin is returned when disabling a plugin WeKnora needs.
- ErrRequiredPlugin = errors.New("this plugin is required and cannot be disabled")
-)
-
-// Service reads and changes tenant plugin switches. It implements
-// interfaces.PluginGate.
-type Service struct {
- registry *registry.Registry
- repo interfaces.PluginTenantSettingRepository
-}
-
-// NewService creates a Service.
-func NewService(reg *registry.Registry, repo interfaces.PluginTenantSettingRepository) *Service {
- return &Service{registry: reg, repo: repo}
-}
-
-// TenantPlugin is a plugin as one tenant sees it.
-type TenantPlugin struct {
- Manifest *manifest.Manifest `json:"manifest"`
- Enabled bool `json:"enabled"`
- // UpdatedAt is when the tenant last changed the switch; zero if never.
- UpdatedAt time.Time `json:"updatedAt,omitzero"`
-}
-
-// List returns every known plugin with the tenant's switch.
-func (s *Service) List(ctx context.Context, tenantID uint64) ([]TenantPlugin, error) {
- rows, err := s.repo.List(ctx, tenantID)
- if err != nil {
- return nil, err
- }
- byID := make(map[string]types.PluginTenantSetting, len(rows))
- for _, r := range rows {
- byID[r.PluginID] = r
- }
- plugins := s.registry.Plugins()
- out := make([]TenantPlugin, 0, len(plugins))
- for _, m := range plugins {
- tp := TenantPlugin{Manifest: m, Enabled: enabledByDefault(m)}
- if r, ok := byID[m.ID]; ok {
- tp.Enabled = r.Enabled || m.Required
- tp.UpdatedAt = r.UpdatedAt
- }
- out = append(out, tp)
- }
- return out, nil
-}
-
-// PluginEnabled reports whether a tenant has a plugin switched on. Unlike
-// EnabledFilter it fails closed: it decides whether workspace data goes to
-// the plugin.
-func (s *Service) PluginEnabled(ctx context.Context, tenantID uint64, pluginID string) (bool, error) {
- m, ok := s.registry.Plugin(pluginID)
- if !ok {
- return false, nil
- }
- if m.Required {
- return true, nil
- }
- row, err := s.repo.Get(ctx, tenantID, pluginID)
- if err != nil {
- return false, err
- }
- if row == nil {
- return enabledByDefault(m), nil
- }
- return row.Enabled, nil
-}
-
-// SetEnabled turns a plugin on or off for a tenant.
-func (s *Service) SetEnabled(
- ctx context.Context, tenantID uint64, pluginID string, enabled bool, updatedBy string,
-) error {
- m, ok := s.registry.Plugin(pluginID)
- if !ok {
- return ErrUnknownPlugin
- }
- if !enabled && m.Required {
- return ErrRequiredPlugin
- }
- return s.repo.Upsert(ctx, &types.PluginTenantSetting{
- TenantID: tenantID,
- PluginID: pluginID,
- Enabled: enabled,
- UpdatedBy: updatedBy,
- UpdatedAt: time.Now(),
- }, "enabled")
-}
-
-// enabledByDefault is a plugin's switch in a tenant that never set it:
-// builtins are on, installed plugins wait for a tenant admin to opt in.
-func enabledByDefault(m *manifest.Manifest) bool { return m.Builtin }
-
-// EnabledFilter implements interfaces.PluginGate. If the switches cannot be
-// read it fails open: offering a disabled integration is recoverable,
-// hiding every integration on a database hiccup is not.
-func (s *Service) EnabledFilter(ctx context.Context, tenantID uint64) func(manifest.Point, string) bool {
- set := map[string]bool{}
- rows, err := s.repo.List(ctx, tenantID)
- if err != nil {
- logger.Warnf(ctx, "[plugin] read tenant %d plugin switches: %v; treating all as enabled", tenantID, err)
- }
- for _, r := range rows {
- set[r.PluginID] = r.Enabled
- }
- failOpen := err != nil
- return func(point manifest.Point, id string) bool {
- e, ok := s.registry.Resolve(point, id)
- if !ok {
- return true
- }
- m, ok := s.registry.Plugin(e.PluginID)
- if !ok || m.Required || failOpen {
- return true
- }
- if enabled, ok := set[e.PluginID]; ok {
- return enabled
- }
- return enabledByDefault(m)
- }
-}
-
-// ContributionEnabled checks one contribution; see EnabledFilter.
-func (s *Service) ContributionEnabled(ctx context.Context, tenantID uint64, point manifest.Point, id string) bool {
- return s.EnabledFilter(ctx, tenantID)(point, id)
-}
diff --git a/internal/plugin/tenancy/tenancy_test.go b/internal/plugin/tenancy/tenancy_test.go
deleted file mode 100644
index 4f7d9b776..000000000
--- a/internal/plugin/tenancy/tenancy_test.go
+++ /dev/null
@@ -1,243 +0,0 @@
-package tenancy
-
-import (
- "context"
- "errors"
- "testing"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/plugin/registry"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-type memRepo struct {
- rows map[uint64]map[string]types.PluginTenantSetting
- err error
-}
-
-func (m *memRepo) List(_ context.Context, tenantID uint64) ([]types.PluginTenantSetting, error) {
- if m.err != nil {
- return nil, m.err
- }
- var out []types.PluginTenantSetting
- for _, r := range m.rows[tenantID] {
- out = append(out, r)
- }
- return out, nil
-}
-
-func (m *memRepo) Get(_ context.Context, tenantID uint64, pluginID string) (*types.PluginTenantSetting, error) {
- if m.err != nil {
- return nil, m.err
- }
- if r, ok := m.rows[tenantID][pluginID]; ok {
- return &r, nil
- }
- return nil, nil
-}
-
-func (m *memRepo) Upsert(_ context.Context, s *types.PluginTenantSetting, columns ...string) error {
- if m.rows == nil {
- m.rows = map[uint64]map[string]types.PluginTenantSetting{}
- }
- if m.rows[s.TenantID] == nil {
- m.rows[s.TenantID] = map[string]types.PluginTenantSetting{}
- }
- row, exists := m.rows[s.TenantID][s.PluginID]
- if !exists || len(columns) == 0 {
- m.rows[s.TenantID][s.PluginID] = *s
- return nil
- }
- for _, c := range columns {
- switch c {
- case "enabled":
- row.Enabled = s.Enabled
- case "config":
- row.Config = s.Config
- }
- }
- row.UpdatedBy, row.UpdatedAt = s.UpdatedBy, s.UpdatedAt
- m.rows[s.TenantID][s.PluginID] = row
- return nil
-}
-
-func newService(t *testing.T) (*Service, *memRepo) {
- t.Helper()
- reg := registry.New()
- for _, m := range []*manifest.Manifest{
- builtin("weknora.feishu", false, manifest.Contributions{
- manifest.PointConnectors: {{ID: "feishu", Name: manifest.Text("Feishu", nil), Aliases: []string{"feishu"}}},
- manifest.PointIMChannels: {{ID: "feishu", Name: manifest.Text("Feishu", nil), Aliases: []string{"feishu"}}},
- }),
- builtin("weknora.agent-tools", true, manifest.Contributions{
- manifest.PointTools: {
- {ID: "thinking", Name: manifest.Text("Thinking", nil), Aliases: []string{"thinking"}},
- },
- }),
- } {
- if err := reg.Register(m); err != nil {
- t.Fatal(err)
- }
- }
- repo := &memRepo{}
- return NewService(reg, repo), repo
-}
-
-func builtin(id string, required bool, c manifest.Contributions) *manifest.Manifest {
- return &manifest.Manifest{
- SchemaVersion: manifest.SchemaVersion, ID: id, Version: "1.0.0", Name: manifest.Text(id, nil),
- Publisher: manifest.Publisher{ID: manifest.BuiltinPublisher}, Builtin: true, Required: required,
- Runtime: manifest.Runtime{Type: manifest.RuntimeBuiltin}, Contributes: c,
- }
-}
-
-func TestDisablingHidesEveryContributionOfThePlugin(t *testing.T) {
- s, _ := newService(t)
- ctx := context.Background()
- if !s.ContributionEnabled(ctx, 1, manifest.PointConnectors, "feishu") {
- t.Fatal("plugins start enabled")
- }
- if err := s.SetEnabled(ctx, 1, "weknora.feishu", false, "u1"); err != nil {
- t.Fatal(err)
- }
- filter := s.EnabledFilter(ctx, 1)
- if filter(manifest.PointConnectors, "feishu") || filter(manifest.PointIMChannels, "weknora.feishu/feishu") {
- t.Fatal("both contributions of a disabled plugin must be off, by alias and qualified ID")
- }
- if !s.ContributionEnabled(ctx, 2, manifest.PointConnectors, "feishu") {
- t.Fatal("another tenant is unaffected")
- }
- if !filter(manifest.PointConnectors, "unknown-type") {
- t.Fatal("unknown contributions count as enabled")
- }
-
- list, err := s.List(ctx, 1)
- if err != nil {
- t.Fatal(err)
- }
- for _, p := range list {
- if p.Manifest.ID == "weknora.feishu" && (p.Enabled || p.UpdatedAt.IsZero()) {
- t.Fatalf("list should show the switch: %+v", p)
- }
- }
-
- if err := s.SetEnabled(ctx, 1, "weknora.feishu", true, "u1"); err != nil {
- t.Fatal(err)
- }
- if !s.ContributionEnabled(ctx, 1, manifest.PointConnectors, "feishu") {
- t.Fatal("re-enabling restores the contribution")
- }
-}
-
-func TestSetEnabledRejectsRequiredAndUnknown(t *testing.T) {
- s, _ := newService(t)
- ctx := context.Background()
- if err := s.SetEnabled(ctx, 1, "weknora.agent-tools", false, "u1"); !errors.Is(err, ErrRequiredPlugin) {
- t.Fatalf("want ErrRequiredPlugin, got %v", err)
- }
- if err := s.SetEnabled(ctx, 1, "weknora.agent-tools", true, "u1"); err != nil {
- t.Fatalf("enabling a required plugin is a no-op, got %v", err)
- }
- if err := s.SetEnabled(ctx, 1, "acme.nope", false, "u1"); !errors.Is(err, ErrUnknownPlugin) {
- t.Fatalf("want ErrUnknownPlugin, got %v", err)
- }
-}
-
-func TestEnabledFilterFailsOpen(t *testing.T) {
- s, repo := newService(t)
- ctx := context.Background()
- if err := s.SetEnabled(ctx, 1, "weknora.feishu", false, "u1"); err != nil {
- t.Fatal(err)
- }
- repo.err = errors.New("db down")
- if !s.ContributionEnabled(ctx, 1, manifest.PointConnectors, "feishu") {
- t.Fatal("unreadable switches must not hide integrations")
- }
-}
-
-func TestInstalledPluginsWaitForTenantOptIn(t *testing.T) {
- s, _ := newService(t)
- ctx := context.Background()
- kit := &manifest.Manifest{
- SchemaVersion: manifest.SchemaVersion, ID: "acme.kit", Version: "1.0.0", Name: manifest.Text("Kit", nil),
- Publisher: manifest.Publisher{ID: "acme"}, Runtime: manifest.Runtime{Type: manifest.RuntimeDeclarative},
- Contributes: manifest.Contributions{
- manifest.PointMCPServers: {{
- ID: "search", Name: manifest.Text("Search", nil),
- MCP: &manifest.MCPServer{URL: "https://mcp.acme.example/mcp"},
- }},
- },
- }
- if err := s.registry.Register(kit); err != nil {
- t.Fatal(err)
- }
- if s.ContributionEnabled(ctx, 1, manifest.PointMCPServers, "acme.kit/search") {
- t.Fatal("an installed plugin must start disabled in every tenant")
- }
- if !s.ContributionEnabled(ctx, 1, manifest.PointConnectors, "feishu") {
- t.Fatal("builtins start enabled")
- }
- list, _ := s.List(ctx, 1)
- for _, p := range list {
- if p.Manifest.ID == "acme.kit" && p.Enabled {
- t.Fatal("List must report the installed plugin as disabled")
- }
- }
- if err := s.SetEnabled(ctx, 1, "acme.kit", true, "u1"); err != nil {
- t.Fatal(err)
- }
- if !s.ContributionEnabled(ctx, 1, manifest.PointMCPServers, "acme.kit/search") ||
- s.ContributionEnabled(ctx, 2, manifest.PointMCPServers, "acme.kit/search") {
- t.Fatal("opting in applies to that tenant only")
- }
-}
-
-func TestTenantConfigRoundTrip(t *testing.T) {
- s, _ := newService(t)
- ctx := context.Background()
- kit := &manifest.Manifest{
- SchemaVersion: manifest.SchemaVersion, ID: "acme.kit", Version: "1.0.0", Name: manifest.Text("Kit", nil),
- Publisher: manifest.Publisher{ID: "acme"}, Runtime: manifest.Runtime{Type: manifest.RuntimeDeclarative},
- Config: manifest.ConfigSchemas{
- TenantSchema: []byte(`{"type":"object","required":["api_key"],` +
- `"properties":{"api_key":{"type":"string","x-secret":true},"region":{"type":"string"}}}`),
- },
- Contributes: manifest.Contributions{
- manifest.PointMCPServers: {{
- ID: "search", Name: manifest.Text("Search", nil),
- MCP: &manifest.MCPServer{URL: "https://mcp.acme.example/mcp"},
- }},
- },
- }
- if err := s.registry.Register(kit); err != nil {
- t.Fatal(err)
- }
- if _, err := s.Config(ctx, 1, "weknora.feishu"); !errors.Is(err, ErrNoTenantConfig) {
- t.Fatalf("want ErrNoTenantConfig, got %v", err)
- }
- if _, err := s.SetConfig(ctx, 1, "acme.kit", map[string]any{"region": "eu"}, "u1"); err == nil {
- t.Fatal("a missing required secret must be rejected")
- }
- got, err := s.SetConfig(ctx, 1, "acme.kit", map[string]any{"api_key": "k-1", "region": "eu"}, "u1")
- if err != nil || got.Values["api_key"] != "***" || got.Values["region"] != "eu" {
- t.Fatalf("SetConfig = %+v, %v", got, err)
- }
- // Saving config must not flip the switch, and flipping the switch must
- // keep the config.
- if s.ContributionEnabled(ctx, 1, manifest.PointMCPServers, "acme.kit/search") {
- t.Fatal("configuring must not enable the plugin")
- }
- if err := s.SetEnabled(ctx, 1, "acme.kit", true, "u1"); err != nil {
- t.Fatal(err)
- }
- if _, err := s.SetConfig(ctx, 1, "acme.kit", map[string]any{"api_key": "***", "region": "us"}, "u1"); err != nil {
- t.Fatal(err)
- }
- values, _, err := s.OpenConfig(ctx, 1, "acme.kit")
- if err != nil || values["api_key"] != "k-1" || values["region"] != "us" {
- t.Fatalf("OpenConfig = %v, %v", values, err)
- }
- if !s.ContributionEnabled(ctx, 1, manifest.PointMCPServers, "acme.kit/search") {
- t.Fatal("saving config must keep the plugin enabled")
- }
-}
diff --git a/internal/plugin/webhook/webhook.go b/internal/plugin/webhook/webhook.go
deleted file mode 100644
index bdd4abf92..000000000
--- a/internal/plugin/webhook/webhook.go
+++ /dev/null
@@ -1,87 +0,0 @@
-// Package webhook gives each workspace a secret URL per plugin webhook.
-// Third parties call WeKnora there; the URL alone names the workspace, so
-// it carries the tenant ID and a MAC that only WeKnora can compute.
-package webhook
-
-import (
- "crypto/hmac"
- "crypto/rand"
- "crypto/sha256"
- "encoding/base64"
- "os"
- "strconv"
- "strings"
-
- "github.com/Tencent/WeKnora/internal/utils"
-)
-
-// PathPrefix is where webhooks are received:
-// {prefix}/{pluginId}/{webhookId}/{token}[/...].
-const PathPrefix = "/api/v1/plugin-callbacks"
-
-// macBytes is the length of a token's MAC.
-const macBytes = 16
-
-// Tokens signs and checks webhook URLs.
-type Tokens struct{ key []byte }
-
-// NewTokens signs with key.
-func NewTokens(key []byte) *Tokens { return &Tokens{key: key} }
-
-// NewTokensFromEnv derives the key from SYSTEM_AES_KEY or JWT_SECRET, the
-// same on every node. Without either, URLs change on restart.
-func NewTokensFromEnv() *Tokens {
- var secret []byte
- switch {
- case utils.GetAESKey() != nil:
- secret = utils.GetAESKey()
- case strings.TrimSpace(os.Getenv("JWT_SECRET")) != "":
- secret = []byte(strings.TrimSpace(os.Getenv("JWT_SECRET")))
- default:
- secret = make([]byte, 32)
- _, _ = rand.Read(secret)
- }
- mac := hmac.New(sha256.New, secret)
- _, _ = mac.Write([]byte("weknora plugin webhooks"))
- return &Tokens{key: mac.Sum(nil)}
-}
-
-func (t *Tokens) mac(pluginID, webhookID string, tenantID uint64) []byte {
- m := hmac.New(sha256.New, t.key)
- _, _ = m.Write([]byte(pluginID + "\x00" + webhookID + "\x00" + strconv.FormatUint(tenantID, 10)))
- return m.Sum(nil)[:macBytes]
-}
-
-// Token is the URL segment naming a workspace: "<tenant>.<mac>".
-func (t *Tokens) Token(pluginID, webhookID string, tenantID uint64) string {
- return strconv.FormatUint(tenantID, 36) + "." +
- base64.RawURLEncoding.EncodeToString(t.mac(pluginID, webhookID, tenantID))
-}
-
-// Verify returns the workspace a token names, if it is genuine.
-func (t *Tokens) Verify(pluginID, webhookID, token string) (uint64, bool) {
- tenant, sig, ok := strings.Cut(token, ".")
- if !ok {
- return 0, false
- }
- tenantID, err := strconv.ParseUint(tenant, 36, 64)
- if err != nil || tenantID == 0 {
- return 0, false
- }
- got, err := base64.RawURLEncoding.DecodeString(sig)
- if err != nil || !hmac.Equal(got, t.mac(pluginID, webhookID, tenantID)) {
- return 0, false
- }
- return tenantID, true
-}
-
-// Path is the URL path of a webhook for a workspace.
-func (t *Tokens) Path(pluginID, webhookID string, tenantID uint64) string {
- return PathPrefix + "/" + pluginID + "/" + webhookID + "/" + t.Token(pluginID, webhookID, tenantID)
-}
-
-// PublicBase is WeKnora's public address (APP_EXTERNAL_URL), without a
-// trailing slash; empty when not configured.
-func PublicBase() string {
- return strings.TrimSuffix(strings.TrimSpace(os.Getenv("APP_EXTERNAL_URL")), "/")
-}
diff --git a/internal/plugin/webhook/webhook_test.go b/internal/plugin/webhook/webhook_test.go
deleted file mode 100644
index 05b7b67fb..000000000
--- a/internal/plugin/webhook/webhook_test.go
+++ /dev/null
@@ -1,31 +0,0 @@
-package webhook
-
-import (
- "strings"
- "testing"
-)
-
-func TestTokens(t *testing.T) {
- tok := NewTokens([]byte("k"))
- token := tok.Token("acme.jira", "events", 42)
- if id, ok := tok.Verify("acme.jira", "events", token); !ok || id != 42 {
- t.Fatalf("verify = %d, %v", id, ok)
- }
- for _, bad := range []struct{ plugin, hook, token string }{
- {"acme.other", "events", token},
- {"acme.jira", "other", token},
- {"acme.jira", "events", strings.Replace(token, "16.", "17.", 1)},
- {"acme.jira", "events", "16"},
- {"acme.jira", "events", "0." + strings.SplitN(token, ".", 2)[1]},
- } {
- if _, ok := tok.Verify(bad.plugin, bad.hook, bad.token); ok {
- t.Errorf("%+v verified", bad)
- }
- }
- if _, ok := NewTokens([]byte("other")).Verify("acme.jira", "events", token); ok {
- t.Fatal("another key verified the token")
- }
- if p := tok.Path("acme.jira", "events", 42); !strings.HasPrefix(p, PathPrefix+"/acme.jira/events/16.") {
- t.Fatalf("path = %s", p)
- }
-}
diff --git a/internal/router/router.go b/internal/router/router.go
index d353241c9..e346c2673 100644
--- a/internal/router/router.go
+++ b/internal/router/router.go
@@ -20,9 +20,6 @@ import (
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/mcpserver"
"github.com/Tencent/WeKnora/internal/middleware"
- "github.com/Tencent/WeKnora/internal/plugin/hostapi"
- pluginoauth "github.com/Tencent/WeKnora/internal/plugin/oauth"
- "github.com/Tencent/WeKnora/internal/plugin/webhook"
"github.com/Tencent/WeKnora/internal/tracing/langfuse"
"github.com/Tencent/WeKnora/internal/types/interfaces"
@@ -93,12 +90,6 @@ type RouterParams struct {
RedisClient *redis.Client
DataSourceHandler *handler.DataSourceHandler
DataSourceCredentialsHandler *handler.DataSourceCredentialsHandler
- PluginHandler *handler.PluginHandler
- PluginAdminHandler *handler.PluginAdminHandler
- PluginHostAPI *hostapi.Handler
- PluginUIHandler *handler.PluginUIHandler
- PluginWebhookHandler *handler.PluginWebhookHandler
- PluginFormsHandler *handler.PluginFormsHandler
WeKnoraCloudHandler *handler.WeKnoraCloudHandler
WikiPageHandler *handler.WikiPageHandler
MemoryHandler *handler.MemoryHandler
@@ -213,29 +204,6 @@ func NewRouter(params RouterParams) *gin.Engine {
r.POST("/api/v1/local-browser/extension/authorize", params.SessionHandler.BrowserSkillAuthorize)
r.POST("/api/v1/local-browser/internal", params.SessionHandler.BrowserSkillInternal)
- // Host API: plugins call back with their own short-lived tokens, which
- // the handler verifies; user and API key auth do not apply.
- if params.PluginHostAPI != nil {
- params.PluginHostAPI.Register(r)
- }
- // Plugin page files load into sandboxed iframes that carry no
- // credentials; see PluginUIHandler.ServeAsset.
- if params.PluginUIHandler != nil {
- r.GET(handler.PluginUIAssetsPrefix+"/:id/:version/*path", params.PluginUIHandler.ServeAsset)
- }
- // Plugin webhooks: third parties call a secret per-workspace URL; the
- // handler checks it, so no login applies.
- // OAuth for plugin form fields: authorization servers return here
- // without a WeKnora login; the state names the pending authorization.
- if params.PluginFormsHandler != nil {
- r.GET(pluginoauth.CallbackPath, params.PluginFormsHandler.OAuthCallback)
- }
- if params.PluginWebhookHandler != nil {
- hooks := r.Group(webhook.PathPrefix)
- hooks.Any("/:id/:hook/:token", params.PluginWebhookHandler.Receive)
- hooks.Any("/:id/:hook/:token/*path", params.PluginWebhookHandler.Receive)
- }
-
// 认证中间件
r.Use(middleware.Auth(params.TenantService, params.UserService, params.TenantMemberService, params.TenantAPIKeyService, params.Config))
@@ -355,9 +323,6 @@ func NewRouter(params RouterParams) *gin.Engine {
RegisterEmbedChannelRoutes(v1, params.EmbedChannelHandler, rbacGuards)
RegisterMCPEndpointRoutes(v1, params.MCPEndpointHandler, rbacGuards)
RegisterDataSourceRoutes(v1, params.DataSourceHandler, params.DataSourceCredentialsHandler, rbacGuards)
- RegisterPluginRoutes(v1, params.PluginHandler, params.PluginUIHandler, params.PluginWebhookHandler,
- params.PluginFormsHandler, rbacGuards)
- RegisterPluginAdminRoutes(v1, params.PluginAdminHandler, rbacGuards)
RegisterWeKnoraCloudRoutes(v1, params.WeKnoraCloudHandler, rbacGuards)
RegisterWikiPageRoutes(v1, params.WikiPageHandler, rbacGuards)
RegisterMemoryRoutes(v1, params.MemoryHandler, rbacGuards)
diff --git a/internal/router/routes_agent.go b/internal/router/routes_agent.go
index 124b2070f..883213462 100644
--- a/internal/router/routes_agent.go
+++ b/internal/router/routes_agent.go
@@ -310,9 +310,6 @@ func RegisterIMChannelRoutes(r *gin.RouterGroup, imHandler *handler.IMHandler, g
channels := g.apiKeyGroup(r.Group("/im-channels"), apiKeyManageChannels(apiKeyFullAccess()))
{
channels.GET("", g.Viewer(), imHandler.ListAllIMChannels)
- channels.GET("/platforms", g.Viewer(), imHandler.ListIMPlatforms)
- // Credentials for the editor, secrets redacted — Admin+ like updates.
- channels.GET("/:id", g.Admin(), imHandler.GetIMChannel)
channels.PUT("/:id", g.Admin(), imHandler.UpdateIMChannel)
channels.DELETE("/:id", g.Admin(), imHandler.DeleteIMChannel)
channels.POST("/:id/toggle", g.Admin(), imHandler.ToggleIMChannel)
diff --git a/internal/router/routes_plugin.go b/internal/router/routes_plugin.go
deleted file mode 100644
index 18eb9f84d..000000000
--- a/internal/router/routes_plugin.go
+++ /dev/null
@@ -1,68 +0,0 @@
-package router
-
-import (
- "github.com/gin-gonic/gin"
-
- "github.com/Tencent/WeKnora/internal/handler"
- "github.com/Tencent/WeKnora/internal/middleware"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-// RegisterPluginRoutes registers the plugin catalog and the workspace's
-// plugin switches. Any member may read the catalog; only admins change the
-// switches. Like the other integration catalogs it stays closed to scoped
-// API keys.
-func RegisterPluginRoutes(
- r *gin.RouterGroup, h *handler.PluginHandler, ui *handler.PluginUIHandler, hooks *handler.PluginWebhookHandler,
- forms *handler.PluginFormsHandler, g *rbacGuards,
-) {
- plugins := g.apiKeyGroup(r.Group("/plugins"), apiKeyFullAccess())
- {
- plugins.GET("", g.Viewer(), h.ListPlugins)
- // Registered before /:id so the static segment wins.
- plugins.GET("/contributions", g.Viewer(), h.ListContributions)
- plugins.GET("/:id", g.Viewer(), h.GetPlugin)
- // Turning a plugin off hides its integrations workspace-wide — Admin+.
- plugins.PUT("/:id/enabled", g.Admin(), h.SetPluginEnabled)
- // Workspace configuration carries credentials — Admin+ to read too.
- plugins.GET("/:id/config", g.Admin(), h.GetPluginConfig)
- plugins.PUT("/:id/config", g.Admin(), h.UpdatePluginConfig)
- if ui != nil {
- // Any member may use a plugin's pages; a page's minRole is
- // checked per request, since it depends on the page.
- ui.SetRoleGuard(func(need types.TenantRole) gin.HandlerFunc { return middleware.RequireRole(need, g.cfg) })
- plugins.POST("/:id/ui-request", g.Viewer(), ui.Request)
- }
- // Webhook URLs carry their secret — Admin+.
- if hooks != nil {
- plugins.GET("/:id/webhooks", g.Admin(), hooks.List)
- }
- // Plugin forms are filled by workspace admins, or system admins
- // for the platform configuration (checked per request).
- if forms != nil {
- plugins.POST("/:id/options", g.AdminOrSystemAdmin(), forms.Options)
- plugins.POST("/:id/oauth/start", g.AdminOrSystemAdmin(), forms.OAuthStart)
- }
- }
-}
-
-// RegisterPluginAdminRoutes registers plugin installation for system
-// administrators. Installing changes every tenant's catalog, so API keys
-// stay default-denied like the rest of /system/admin.
-func RegisterPluginAdminRoutes(r *gin.RouterGroup, h *handler.PluginAdminHandler, g *rbacGuards) {
- plugins := r.Group("/system/admin/plugins", g.SystemAdmin())
- {
- plugins.GET("", h.ListInstalledPlugins)
- plugins.POST("", h.InstallPlugin)
- // Registered before /:id so the static segment wins.
- plugins.POST("/inspect", h.InspectPlugin)
- plugins.GET("/:id", h.GetInstalledPlugin)
- plugins.DELETE("/:id", h.UninstallPlugin)
- plugins.PUT("/:id/enabled", h.SetInstalledPluginEnabled)
- plugins.PUT("/:id/active-version", h.ActivatePluginVersion)
- plugins.PUT("/:id/remote-url", h.SetPluginRemoteURL)
- plugins.POST("/:id/secret/rotate", h.RotatePluginSecret)
- plugins.GET("/:id/config", h.GetPluginSystemConfig)
- plugins.PUT("/:id/config", h.UpdatePluginSystemConfig)
- }
-}
diff --git a/internal/router/routes_plugin_test.go b/internal/router/routes_plugin_test.go
deleted file mode 100644
index 2dfed1254..000000000
--- a/internal/router/routes_plugin_test.go
+++ /dev/null
@@ -1,41 +0,0 @@
-package router
-
-import (
- "net/http"
- "net/http/httptest"
- "testing"
-
- "github.com/gin-gonic/gin"
- "github.com/stretchr/testify/require"
-
- "github.com/Tencent/WeKnora/internal/config"
- "github.com/Tencent/WeKnora/internal/handler"
-)
-
-func TestPluginInstallationRequiresSystemAdmin(t *testing.T) {
- gin.SetMode(gin.TestMode)
- g := &rbacGuards{cfg: &config.Config{}}
- r := gin.New()
- RegisterPluginAdminRoutes(r.Group("/api/v1"), &handler.PluginAdminHandler{}, g)
- for _, tc := range []struct{ method, path string }{
- {http.MethodGet, "/api/v1/system/admin/plugins"},
- {http.MethodPost, "/api/v1/system/admin/plugins"},
- {http.MethodPost, "/api/v1/system/admin/plugins/inspect"},
- {http.MethodGet, "/api/v1/system/admin/plugins/acme.kit"},
- {http.MethodDelete, "/api/v1/system/admin/plugins/acme.kit"},
- {http.MethodPut, "/api/v1/system/admin/plugins/acme.kit/enabled"},
- {http.MethodPut, "/api/v1/system/admin/plugins/acme.kit/active-version"},
- {http.MethodGet, "/api/v1/system/admin/plugins/acme.kit/config"},
- {http.MethodPut, "/api/v1/system/admin/plugins/acme.kit/config"},
- } {
- t.Run(tc.method+tc.path, func(t *testing.T) {
- w := httptest.NewRecorder()
- r.ServeHTTP(w, httptest.NewRequest(tc.method, tc.path, nil))
- require.Equal(t, http.StatusForbidden, w.Code)
- if g.apiKeyAuthorizer != nil {
- _, declared := g.apiKeyAuthorizer.Lookup(tc.method, tc.path)
- require.False(t, declared, "API keys must remain default-denied")
- }
- })
- }
-}
diff --git a/internal/router/sync_task.go b/internal/router/sync_task.go
index 32f5e8fab..672080b3f 100644
--- a/internal/router/sync_task.go
+++ b/internal/router/sync_task.go
@@ -233,10 +233,8 @@ type SyncTaskParams struct {
KnowledgeAutoTag interfaces.TaskHandler `name:"knowledgeAutoTag"`
KnowledgeBaseProfile interfaces.TaskHandler `name:"knowledgeBaseProfile"`
WikiIngest interfaces.TaskHandler `name:"wikiIngest"`
- // PluginEvents delivers events to plugins (plugin:event).
- PluginEvents interfaces.TaskHandler `name:"pluginEvents"`
- TemporaryDocument interfaces.TemporaryDocumentService
- MemoryService interfaces.MemoryService
+ TemporaryDocument interfaces.TemporaryDocumentService
+ MemoryService interfaces.MemoryService
}
// RegisterSyncHandlers registers all task handlers on the SyncTaskExecutor.
@@ -268,6 +266,5 @@ func RegisterSyncHandlers(params SyncTaskParams) {
params.Executor.RegisterHandler(types.TypeWikiIngest, params.WikiIngest.Handle)
params.Executor.RegisterHandler(types.TypeWikiFinalize, params.WikiIngest.Handle)
params.Executor.RegisterHandler(types.TypeMemoryExtract, params.MemoryService.Handle)
- params.Executor.RegisterHandler(types.TypePluginEvent, params.PluginEvents.Handle)
logger.Infof(context.Background(), "[SyncTask] All task handlers registered (Lite mode, no Redis)")
}
diff --git a/internal/router/task.go b/internal/router/task.go
index 5bae995b3..6e9910675 100644
--- a/internal/router/task.go
+++ b/internal/router/task.go
@@ -45,12 +45,10 @@ type AsynqTaskParams struct {
KnowledgeAutoTag interfaces.TaskHandler `name:"knowledgeAutoTag"`
KnowledgeBaseProfile interfaces.TaskHandler `name:"knowledgeBaseProfile"`
WikiIngest interfaces.TaskHandler `name:"wikiIngest"`
- // PluginEvents delivers events to plugins (plugin:event).
- PluginEvents interfaces.TaskHandler `name:"pluginEvents"`
- TemporaryDocument interfaces.TemporaryDocumentService
- MemoryService interfaces.MemoryService
- DeadLetterRepo interfaces.TaskDeadLetterRepository
- SpanTracker service.SpanTracker
+ TemporaryDocument interfaces.TemporaryDocumentService
+ MemoryService interfaces.MemoryService
+ DeadLetterRepo interfaces.TaskDeadLetterRepository
+ SpanTracker service.SpanTracker
}
// defaultRedisOpTimeout is the previous hard-coded read timeout. The 100ms
@@ -265,7 +263,6 @@ func RunAsynqServer(params AsynqTaskParams) *asynq.ServeMux {
// Register extract handlers - router will dispatch to appropriate handler
mux.HandleFunc(types.TypeChunkExtract, params.ChunkExtractor.Handle)
- mux.HandleFunc(types.TypePluginEvent, params.PluginEvents.Handle)
mux.HandleFunc(types.TypeDataTableSummary, params.DataTableSummary.Handle)
// Register document processing handler
diff --git a/internal/types/docparser.go b/internal/types/docparser.go
index f9d2de673..bd780c1cc 100644
--- a/internal/types/docparser.go
+++ b/internal/types/docparser.go
@@ -51,10 +51,6 @@ type ParserEngineInfo struct {
FileTypes []string
Available bool
UnavailableReason string
- // PluginID and DisplayNames describe an engine an installed plugin
- // provides; DisplayNames is keyed by locale.
- PluginID string `json:",omitempty"`
- DisplayNames map[string]string `json:",omitempty"`
}
// --- Internal types used by chunking pipeline ---
diff --git a/internal/types/interfaces/datasource.go b/internal/types/interfaces/datasource.go
index abc5e745d..cdbb92a70 100644
--- a/internal/types/interfaces/datasource.go
+++ b/internal/types/interfaces/datasource.go
@@ -41,11 +41,7 @@ type DataSourceService interface {
// ValidateCredentials tests connectivity using raw credentials without persisting anything.
// This is used by the frontend "Test Connection" button before creating a data source.
- // ValidateCredentials tests unsaved credentials (and settings, which
- // some connectors need to reach the source) against the connector.
- ValidateCredentials(
- ctx context.Context, connectorType string, credentials, settings map[string]interface{},
- ) error
+ ValidateCredentials(ctx context.Context, connectorType string, credentials map[string]interface{}) error
// ListAvailableResources lists resources available for sync in the external system.
// parentID enables lazy loading: "" lists the top level, a resource ExternalID lists its children.
diff --git a/internal/types/interfaces/plugin.go b/internal/types/interfaces/plugin.go
deleted file mode 100644
index 44542dd6d..000000000
--- a/internal/types/interfaces/plugin.go
+++ /dev/null
@@ -1,78 +0,0 @@
-package interfaces
-
-import (
- "context"
- "time"
-
- "github.com/Tencent/WeKnora/internal/plugin/manifest"
- "github.com/Tencent/WeKnora/internal/types"
-)
-
-// PluginTenantSettingRepository stores per-tenant plugin switches.
-type PluginTenantSettingRepository interface {
- List(ctx context.Context, tenantID uint64) ([]types.PluginTenantSetting, error)
- // Get returns (nil, nil) when the tenant never changed the plugin.
- Get(ctx context.Context, tenantID uint64, pluginID string) (*types.PluginTenantSetting, error)
- // Upsert inserts the row, or updates only the given columns of an
- // existing one (every column when none are given).
- Upsert(ctx context.Context, setting *types.PluginTenantSetting, columns ...string) error
-}
-
-// PluginGate tells integrations which contributions a tenant has enabled.
-// A disabled plugin's contributions are left out of type listings and cannot
-// back new instances; existing instances keep working.
-type PluginGate interface {
- // EnabledFilter returns a predicate for one tenant, reading the
- // tenant's switches once so a listing checks many items cheaply. The
- // ID may be qualified or a builtin alias; contributions the registry
- // does not know count as enabled, leaving existence to the domain.
- EnabledFilter(ctx context.Context, tenantID uint64) func(point manifest.Point, id string) bool
-}
-
-// PluginRepository stores installed (non-builtin) plugins and their versions.
-type PluginRepository interface {
- ListPlugins(ctx context.Context) ([]types.InstalledPlugin, error)
- // GetPlugin returns (nil, nil) when the plugin is not installed.
- GetPlugin(ctx context.Context, id string) (*types.InstalledPlugin, error)
- SavePlugin(ctx context.Context, p *types.InstalledPlugin) error
- // DeletePlugin removes the plugin and every stored version.
- DeletePlugin(ctx context.Context, id string) error
- ListVersions(ctx context.Context, pluginID string) ([]types.PluginVersion, error)
- // GetVersion returns (nil, nil) when the version is not stored.
- GetVersion(ctx context.Context, pluginID, version string) (*types.PluginVersion, error)
- SaveVersion(ctx context.Context, v *types.PluginVersion) error
-}
-
-// PluginKVRepository stores plugins' Host API key-value data. Expired
-// entries read as missing.
-type PluginKVRepository interface {
- // Get returns (nil, nil) for a missing or expired key.
- Get(ctx context.Context, pluginID string, tenantID uint64, key string) (*types.PluginKV, error)
- Put(ctx context.Context, e *types.PluginKV) error
- // Delete reports whether the key existed.
- Delete(ctx context.Context, pluginID string, tenantID uint64, key string) (bool, error)
- // List returns live keys starting with prefix, after the given key, in
- // key order.
- List(
- ctx context.Context,
- pluginID string,
- tenantID uint64,
- prefix, after string,
- limit int,
- ) ([]types.PluginKV, error)
- // Count counts live keys of a plugin in a tenant.
- Count(ctx context.Context, pluginID string, tenantID uint64) (int64, error)
- // DeleteExpired removes entries that expired before now.
- DeleteExpired(ctx context.Context, now time.Time) (int64, error)
-}
-
-// PluginOAuthRepository stores plugin OAuth connections.
-type PluginOAuthRepository interface {
- Create(ctx context.Context, c *types.PluginOAuthConnection) error
- // Get returns (nil, nil) when the connection does not exist for this
- // plugin and tenant.
- Get(ctx context.Context, pluginID string, tenantID uint64, id string) (*types.PluginOAuthConnection, error)
- // UpdateToken saves a refreshed token unless another node refreshed it
- // since prev (the UpdatedAt read); it reports whether it saved.
- UpdateToken(ctx context.Context, c *types.PluginOAuthConnection, prev time.Time) (bool, error)
-}
diff --git a/internal/types/mcp.go b/internal/types/mcp.go
index fb4014f72..674e6cc99 100644
--- a/internal/types/mcp.go
+++ b/internal/types/mcp.go
@@ -19,9 +19,6 @@ const (
MCPTransportSSE MCPTransportType = "sse" // Server-Sent Events
MCPTransportHTTPStreamable MCPTransportType = "http-streamable" // HTTP Streamable
MCPTransportStdio MCPTransportType = "stdio" // Stdio (Standard Input/Output)
- // MCPTransportPlugin is an MCP server a plugin serves itself, reached
- // through plugin calls instead of a URL.
- MCPTransportPlugin MCPTransportType = "plugin"
)
// MCPService represents an MCP (Model Context Protocol) service configuration
@@ -42,23 +39,9 @@ type MCPService struct {
StdioConfig *MCPStdioConfig `json:"stdio_config,omitempty" gorm:"type:json"` // Required for stdio transport
EnvVars MCPEnvVars `json:"env_vars,omitempty" gorm:"type:json"` // Environment variables for stdio
IsBuiltin bool `json:"is_builtin" gorm:"default:false"` // Whether this is a builtin MCP service (visible to all workspaces)
- // PluginID names the installed plugin providing the service; such
- // services are never stored and read as builtin (read-only).
- PluginID string `json:"plugin_id,omitempty" gorm:"-"`
- // PluginError says why a plugin service is disabled, typically that the
- // workspace has not configured the plugin yet.
- PluginError string `json:"plugin_error,omitempty" gorm:"-"`
- // PluginVersion is the version of the plugin providing the service.
- PluginVersion string `json:"-" gorm:"-"`
- // PluginServer is the plugin's local ID of the server (its mcpServers
- // contribution); the plugin transport calls it by this ID.
- PluginServer string `json:"-" gorm:"-"`
- // ToolViews are the plugin's result views by tool name (manifest
- // ToolView as JSON), for the chat to render structured results.
- ToolViews map[string]json.RawMessage `json:"-" gorm:"-"`
- CreatedAt time.Time `json:"created_at"`
- UpdatedAt time.Time `json:"updated_at"`
- DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
+ CreatedAt time.Time `json:"created_at"`
+ UpdatedAt time.Time `json:"updated_at"`
+ DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
}
// EffectiveUsageInstructions preserves documentation on legacy services until
diff --git a/internal/types/plugin.go b/internal/types/plugin.go
deleted file mode 100644
index 6a6785e50..000000000
--- a/internal/types/plugin.go
+++ /dev/null
@@ -1,91 +0,0 @@
-package types
-
-import "time"
-
-// Desired states of an installed plugin.
-const (
- PluginStateEnabled = "enabled"
- PluginStateDisabled = "disabled"
-)
-
-// InstalledPlugin is a non-builtin plugin installed on the platform. Every
-// node converges to these rows: an enabled plugin's active version is loaded,
-// anything else is unloaded.
-type InstalledPlugin struct {
- ID string `json:"id" gorm:"type:varchar(128);primaryKey"`
- // OwnerTenantID is nil for platform plugins a system admin installed.
- OwnerTenantID *uint64 `json:"owner_tenant_id,omitempty"`
- // Source records where the package came from: {"kind":"upload"} or
- // {"kind":"url","url":"..."}.
- Source JSON `json:"source" gorm:"type:json"`
- ActiveVersion string `json:"active_version" gorm:"type:varchar(64)"`
- DesiredState string `json:"desired_state" gorm:"type:varchar(16)"`
- Runtime string `json:"runtime" gorm:"type:varchar(32)"`
- // GrantedPerms is the manifest permissions an administrator accepted.
- GrantedPerms JSON `json:"granted_perms" gorm:"type:json"`
- SystemConfig JSON `json:"system_config,omitempty" gorm:"type:json"`
- // RemoteURL is where a remote plugin is served; RemoteSecret (sealed)
- // signs the calls to it. Other runtimes leave both empty.
- RemoteURL string `json:"remote_url,omitempty" gorm:"type:varchar(1024)"`
- RemoteSecret string `json:"-" gorm:"type:text"`
- CreatedBy string `json:"created_by" gorm:"type:varchar(36)"`
- CreatedAt time.Time `json:"created_at"`
- UpdatedAt time.Time `json:"updated_at"`
-}
-
-// TableName pins the table so GORM's pluralizer cannot drift.
-func (InstalledPlugin) TableName() string { return "plugins" }
-
-// PluginVersion is one stored package version of an installed plugin.
-type PluginVersion struct {
- PluginID string `json:"plugin_id" gorm:"type:varchar(128);primaryKey"`
- Version string `json:"version" gorm:"type:varchar(64);primaryKey"`
- // Digest is "sha256:<hex>" of the package archive.
- Digest string `json:"digest" gorm:"type:varchar(80)"`
- Manifest JSON `json:"manifest" gorm:"type:json"`
- // PackageURI is where the archive is stored (FileService path).
- PackageURI string `json:"-" gorm:"type:varchar(1024)"`
- Size int64 `json:"size"`
- CreatedBy string `json:"created_by" gorm:"type:varchar(36)"`
- CreatedAt time.Time `json:"created_at"`
-}
-
-// TableName pins the table so GORM's pluralizer cannot drift.
-func (PluginVersion) TableName() string { return "plugin_versions" }
-
-// PluginKV is one entry of a plugin's key-value store (Host API kv),
-// partitioned by plugin and tenant.
-type PluginKV struct {
- PluginID string `json:"-" gorm:"type:varchar(128);primaryKey"`
- TenantID uint64 `json:"-" gorm:"primaryKey"`
- Key string `json:"key" gorm:"type:varchar(256);primaryKey"`
- Value JSON `json:"value" gorm:"type:json"`
- ExpiresAt *time.Time `json:"expiresAt,omitempty"`
- UpdatedAt time.Time `json:"updatedAt"`
-}
-
-// TableName pins the table so GORM's pluralizer cannot drift.
-func (PluginKV) TableName() string { return "plugin_kv" }
-
-// PluginOAuthConnection is an authorization a plugin form field made with
-// its OAuth button (x-oauth). The field holds "oauth:<ID>"; the tokens stay
-// here, sealed (enc:v1), and never reach the browser.
-type PluginOAuthConnection struct {
- ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
- PluginID string `json:"pluginId" gorm:"type:varchar(128);index:idx_plugin_oauth_connections_owner"`
- // TenantID is 0 for the platform (system configuration).
- TenantID uint64 `json:"-" gorm:"index:idx_plugin_oauth_connections_owner"`
- // Scope, Contribution and Field find the x-oauth spec again when the
- // token needs refreshing.
- Scope string `json:"scope" gorm:"type:varchar(16)"`
- Contribution string `json:"contribution" gorm:"type:varchar(160)"`
- Field string `json:"field" gorm:"type:varchar(256)"`
- Token string `json:"-" gorm:"type:text"`
- ExpiresAt *time.Time `json:"expiresAt,omitempty"`
- CreatedBy string `json:"-" gorm:"type:varchar(64)"`
- CreatedAt time.Time `json:"createdAt"`
- UpdatedAt time.Time `json:"updatedAt"`
-}
-
-// TableName pins the table so GORM's pluralizer cannot drift.
-func (PluginOAuthConnection) TableName() string { return "plugin_oauth_connections" }
diff --git a/internal/types/plugin_tenant_setting.go b/internal/types/plugin_tenant_setting.go
deleted file mode 100644
index c5b996e57..000000000
--- a/internal/types/plugin_tenant_setting.go
+++ /dev/null
@@ -1,19 +0,0 @@
-package types
-
-import "time"
-
-// PluginTenantSetting is one tenant's switch and configuration for one
-// plugin. A plugin without a row is enabled with no tenant configuration.
-type PluginTenantSetting struct {
- TenantID uint64 `json:"tenant_id" gorm:"primaryKey"`
- PluginID string `json:"plugin_id" gorm:"type:varchar(128);primaryKey"`
- Enabled bool `json:"enabled"`
- // Config is the tenant-level plugin configuration, secrets sealed per
- // the plugin's config schema. Unused until plugins declare one.
- Config JSON `json:"config,omitempty" gorm:"type:json"`
- UpdatedBy string `json:"updated_by" gorm:"type:varchar(36)"`
- UpdatedAt time.Time `json:"updated_at"`
-}
-
-// TableName pins the table so GORM's pluralizer cannot drift.
-func (PluginTenantSetting) TableName() string { return "plugin_tenant_settings" }
diff --git a/internal/types/task.go b/internal/types/task.go
index aface3d53..7f76dd4b0 100644
--- a/internal/types/task.go
+++ b/internal/types/task.go
@@ -234,7 +234,6 @@ type WorkerServerStat struct {
const (
TypeChunkExtract = "chunk:extract"
TypeDocumentProcess = "document:process" // 文档处理任务
- TypePluginEvent = "plugin:event" // 向插件投递业务事件
TypeFAQImport = "faq:import" // FAQ导入任务(包含dry run模式)
TypeQuestionGeneration = "question:generation" // 问题生成任务
TypeSummaryGeneration = "summary:generation" // 摘要生成任务
diff --git a/internal/types/web_search_provider.go b/internal/types/web_search_provider.go
index ddf35912c..8934f0e61 100644
--- a/internal/types/web_search_provider.go
+++ b/internal/types/web_search_provider.go
@@ -6,7 +6,6 @@ import (
"log"
"time"
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
"github.com/Tencent/WeKnora/internal/utils"
"github.com/google/uuid"
"gorm.io/gorm"
@@ -134,13 +133,6 @@ type WebSearchProviderTypeInfo struct {
ID string `json:"id"`
// Human-readable name
Name string `json:"name"`
- // Names localizes Name for plugin provider types, keyed by locale.
- Names map[string]string `json:"names,omitempty"`
- // Icon is an image (data: URI) for plugin provider types; builtin types
- // use the frontend's bundled logos.
- Icon string `json:"icon,omitempty"`
- // PluginID names the installed plugin providing the type.
- PluginID string `json:"plugin_id,omitempty"`
// Whether the provider requires an API key
RequiresAPIKey bool `json:"requires_api_key"`
// Whether the provider accepts an optional API key (keyless by default, but a
@@ -158,10 +150,6 @@ type WebSearchProviderTypeInfo struct {
DocsURL string `json:"docs_url,omitempty"`
// Provider-specific non-secret configuration rendered dynamically by the frontend.
ConfigFields []WebSearchProviderConfigField `json:"config_fields,omitempty"`
- // ConfigSchema describes the provider parameters (the fields above plus
- // the connection flags) as a config schema. Filled by
- // GetWebSearchProviderTypes.
- ConfigSchema *configschema.Schema `json:"config_schema,omitempty"`
}
// WebSearchProviderConfigField describes a non-secret provider-specific form field.
@@ -187,14 +175,6 @@ type WebSearchProviderConfigFieldOption struct {
// GetWebSearchProviderTypes returns metadata for all supported provider types.
func GetWebSearchProviderTypes() []WebSearchProviderTypeInfo {
- infos := webSearchProviderTypes()
- for i := range infos {
- infos[i].ConfigSchema = infos[i].BuildConfigSchema()
- }
- return infos
-}
-
-func webSearchProviderTypes() []WebSearchProviderTypeInfo {
return []WebSearchProviderTypeInfo{
{
ID: "brave", Name: "Brave Search", RequiresAPIKey: true, SupportsProxy: true,
diff --git a/internal/types/web_search_provider_schema.go b/internal/types/web_search_provider_schema.go
deleted file mode 100644
index 5e6d23455..000000000
--- a/internal/types/web_search_provider_schema.go
+++ /dev/null
@@ -1,119 +0,0 @@
-package types
-
-import "github.com/Tencent/WeKnora/internal/plugin/configschema"
-
-// Field groups of the web search form, matching its drawer sections.
-const (
- webSearchGroupCredentials = "credentials"
- webSearchGroupOptions = "options"
-)
-
-// BuildConfigSchema describes the parameters of one provider type
-// (WebSearchProviderParameters) as a config schema. The flags and config
-// fields on the type info are what the frontend renders from today; this is
-// the same form in the shape every integration moves to. Titles point at the
-// frontend's existing webSearchSettings.* strings.
-func (info WebSearchProviderTypeInfo) BuildConfigSchema() *configschema.Schema {
- s := configschema.Object()
- if info.RequiresBaseURL {
- s.Set("base_url", &configschema.Schema{
- Type: configschema.TypeString,
- Format: "uri",
- Title: "Instance URL",
- Placeholder: "https://searxng.example.com",
- I18nKeys: map[string]string{
- "title": "webSearchSettings.baseUrlLabel",
- "placeholder": "webSearchSettings.baseUrlPlaceholder",
- },
- Group: webSearchGroupCredentials,
- Order: 10,
- }, true)
- }
- if info.RequiresAPIKey || info.SupportsOptionalAPIKey {
- titleKey := "webSearchSettings.apiKeyLabel"
- if !info.RequiresAPIKey {
- titleKey = "webSearchSettings.apiKeyOptionalLabel"
- }
- s.Set("api_key", &configschema.Schema{
- Type: configschema.TypeString,
- Secret: true,
- Widget: "password",
- Title: "API key",
- I18nKeys: map[string]string{
- "title": titleKey,
- "placeholder": "webSearchSettings.apiKeyPlaceholder",
- },
- Group: webSearchGroupCredentials,
- Order: 20,
- }, info.RequiresAPIKey)
- }
- if info.RequiresEngineID {
- s.Set("engine_id", &configschema.Schema{
- Type: configschema.TypeString,
- Title: "Search engine ID",
- I18nKeys: map[string]string{"title": "webSearchSettings.engineIdLabel"},
- Group: webSearchGroupCredentials,
- Order: 30,
- }, true)
- }
- if len(info.ConfigFields) > 0 {
- extra := configschema.Object()
- extra.Group = webSearchGroupCredentials
- extra.Order = 40
- for i, f := range info.ConfigFields {
- extra.Set(f.Key, webSearchConfigFieldSchema(f, i), f.Required)
- }
- s.Set("extra_config", extra, false)
- }
- if info.SupportsProxy {
- s.Set("proxy_url", &configschema.Schema{
- Type: configschema.TypeString,
- Format: "uri",
- Title: "HTTP proxy",
- Description: "Leave empty to use the HTTP(S)_PROXY environment variables.",
- I18nKeys: map[string]string{
- "title": "webSearchSettings.proxyUrlLabel",
- "placeholder": "webSearchSettings.proxyUrlPlaceholder",
- "description": "webSearchSettings.proxyUrlHelp",
- },
- Group: webSearchGroupOptions,
- Order: 50,
- }, false)
- }
- return s
-}
-
-func webSearchConfigFieldSchema(f WebSearchProviderConfigField, order int) *configschema.Schema {
- prop := &configschema.Schema{
- Type: configschema.TypeString,
- Title: f.Label,
- Description: f.Description,
- Widget: f.Type,
- Order: order,
- }
- if f.Default != "" {
- prop.Default = f.Default
- }
- prop.I18nKeys = i18nKeys(map[string]string{"title": f.LabelKey, "description": f.DescriptionKey})
- for _, o := range f.Options {
- prop.OneOf = append(prop.OneOf, &configschema.Schema{
- Const: o.Value,
- Title: o.Label,
- I18nKeys: i18nKeys(map[string]string{"title": o.LabelKey}),
- })
- }
- return prop
-}
-
-// i18nKeys drops empty keys and returns nil when nothing is left.
-func i18nKeys(keys map[string]string) map[string]string {
- for k, v := range keys {
- if v == "" {
- delete(keys, k)
- }
- }
- if len(keys) == 0 {
- return nil
- }
- return keys
-}
diff --git a/internal/types/web_search_provider_schema_test.go b/internal/types/web_search_provider_schema_test.go
deleted file mode 100644
index 7d07f7889..000000000
--- a/internal/types/web_search_provider_schema_test.go
+++ /dev/null
@@ -1,72 +0,0 @@
-package types
-
-import (
- "testing"
-
- "github.com/Tencent/WeKnora/internal/plugin/configschema"
-)
-
-func TestRedactedPlaceholderMatchesConfigSchema(t *testing.T) {
- if RedactedSecretPlaceholder != configschema.RedactedPlaceholder {
- t.Fatal("configschema.RedactedPlaceholder must equal RedactedSecretPlaceholder")
- }
-}
-
-func TestWebSearchConfigSchemas(t *testing.T) {
- for _, info := range GetWebSearchProviderTypes() {
- s := info.ConfigSchema
- if s == nil {
- t.Fatalf("%s: no config schema", info.ID)
- }
- if err := s.Check(); err != nil {
- t.Errorf("%s: invalid schema: %v", info.ID, err)
- }
- _, hasKey := s.Properties["api_key"]
- if hasKey != (info.RequiresAPIKey || info.SupportsOptionalAPIKey) {
- t.Errorf("%s: api_key present = %v", info.ID, hasKey)
- }
- if hasKey && !s.Properties["api_key"].Secret {
- t.Errorf("%s: api_key must be secret", info.ID)
- }
- if _, ok := s.Properties["base_url"]; ok != info.RequiresBaseURL {
- t.Errorf("%s: base_url present = %v", info.ID, ok)
- }
- if _, ok := s.Properties["proxy_url"]; ok != info.SupportsProxy {
- t.Errorf("%s: proxy_url present = %v", info.ID, ok)
- }
- extra := s.Properties["extra_config"]
- if (extra != nil) != (len(info.ConfigFields) > 0) {
- t.Errorf("%s: extra_config present = %v", info.ID, extra != nil)
- }
- if extra != nil && len(extra.Properties) != len(info.ConfigFields) {
- t.Errorf("%s: %d extra fields for %d config fields", info.ID, len(extra.Properties), len(info.ConfigFields))
- }
- }
-}
-
-func TestWebSearchConfigSchemaValidatesStoredShape(t *testing.T) {
- var zhipu WebSearchProviderTypeInfo
- for _, info := range GetWebSearchProviderTypes() {
- if info.ID == "zhipu" {
- zhipu = info
- }
- }
- s := zhipu.ConfigSchema
- // The shape WebSearchProviderParameters marshals to.
- params := map[string]any{
- "api_key": "***",
- "extra_config": map[string]any{"search_engine": "search_pro", "content_size": "high"},
- }
- if errs := configschema.Validate(s, params); errs != nil {
- t.Fatalf("stored parameters rejected: %v", errs)
- }
- params["extra_config"] = map[string]any{"search_engine": "bogus", "content_size": "high"}
- errs := configschema.Validate(s, params)
- if len(errs) != 1 || errs[0].Path != "extra_config.search_engine" || errs[0].Code != configschema.CodeEnum {
- t.Fatalf("want enum error on extra_config.search_engine, got %v", errs)
- }
- if key := s.Properties["extra_config"].Properties["search_engine"].I18nKeys["title"]; key !=
- "webSearchSettings.configFields.searchEngine" {
- t.Fatalf("label key not carried over: %q", key)
- }
-}
diff --git a/migrations/sqlite/000034_plugin_tenant_settings.down.sql b/migrations/sqlite/000034_plugin_tenant_settings.down.sql
deleted file mode 100644
index d00ca9693..000000000
--- a/migrations/sqlite/000034_plugin_tenant_settings.down.sql
+++ /dev/null
@@ -1 +0,0 @@
-DROP TABLE IF EXISTS plugin_tenant_settings;
diff --git a/migrations/sqlite/000034_plugin_tenant_settings.up.sql b/migrations/sqlite/000034_plugin_tenant_settings.up.sql
deleted file mode 100644
index c857b7932..000000000
--- a/migrations/sqlite/000034_plugin_tenant_settings.up.sql
+++ /dev/null
@@ -1,10 +0,0 @@
--- Per-tenant plugin switches and configuration (versioned 000115).
-CREATE TABLE IF NOT EXISTS plugin_tenant_settings (
- tenant_id BIGINT NOT NULL,
- plugin_id VARCHAR(128) NOT NULL,
- enabled BOOLEAN NOT NULL DEFAULT 1,
- config TEXT,
- updated_by VARCHAR(36) NOT NULL DEFAULT '',
- updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (tenant_id, plugin_id)
-);
diff --git a/migrations/sqlite/000035_plugins.down.sql b/migrations/sqlite/000035_plugins.down.sql
deleted file mode 100644
index f5d53353f..000000000
--- a/migrations/sqlite/000035_plugins.down.sql
+++ /dev/null
@@ -1,2 +0,0 @@
-DROP TABLE IF EXISTS plugin_versions;
-DROP TABLE IF EXISTS plugins;
diff --git a/migrations/sqlite/000035_plugins.up.sql b/migrations/sqlite/000035_plugins.up.sql
deleted file mode 100644
index 936ad061a..000000000
--- a/migrations/sqlite/000035_plugins.up.sql
+++ /dev/null
@@ -1,26 +0,0 @@
--- Installed plugins and their versions (versioned 000116).
-CREATE TABLE IF NOT EXISTS plugins (
- id VARCHAR(128) PRIMARY KEY,
- owner_tenant_id BIGINT,
- source TEXT NOT NULL DEFAULT '{}',
- active_version VARCHAR(64) NOT NULL,
- desired_state VARCHAR(16) NOT NULL DEFAULT 'enabled',
- runtime VARCHAR(32) NOT NULL,
- granted_perms TEXT NOT NULL DEFAULT '{}',
- system_config TEXT,
- created_by VARCHAR(36) NOT NULL DEFAULT '',
- created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
- updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE TABLE IF NOT EXISTS plugin_versions (
- plugin_id VARCHAR(128) NOT NULL,
- version VARCHAR(64) NOT NULL,
- digest VARCHAR(80) NOT NULL,
- manifest TEXT NOT NULL,
- package_uri VARCHAR(1024) NOT NULL,
- size BIGINT NOT NULL DEFAULT 0,
- created_by VARCHAR(36) NOT NULL DEFAULT '',
- created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (plugin_id, version)
-);
diff --git a/migrations/sqlite/000036_plugin_kv.down.sql b/migrations/sqlite/000036_plugin_kv.down.sql
deleted file mode 100644
index e9a7aae49..000000000
--- a/migrations/sqlite/000036_plugin_kv.down.sql
+++ /dev/null
@@ -1 +0,0 @@
-DROP TABLE IF EXISTS plugin_kv;
diff --git a/migrations/sqlite/000036_plugin_kv.up.sql b/migrations/sqlite/000036_plugin_kv.up.sql
deleted file mode 100644
index 367cad7fc..000000000
--- a/migrations/sqlite/000036_plugin_kv.up.sql
+++ /dev/null
@@ -1,12 +0,0 @@
--- Host API key-value store of plugins (versioned 000117).
-CREATE TABLE IF NOT EXISTS plugin_kv (
- plugin_id VARCHAR(128) NOT NULL,
- tenant_id BIGINT NOT NULL,
- key VARCHAR(256) NOT NULL,
- value TEXT NOT NULL,
- expires_at DATETIME,
- updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (plugin_id, tenant_id, key)
-);
-
-CREATE INDEX IF NOT EXISTS idx_plugin_kv_expires_at ON plugin_kv (expires_at);
diff --git a/migrations/sqlite/000037_plugin_remote.down.sql b/migrations/sqlite/000037_plugin_remote.down.sql
deleted file mode 100644
index 75bb9215c..000000000
--- a/migrations/sqlite/000037_plugin_remote.down.sql
+++ /dev/null
@@ -1,2 +0,0 @@
-ALTER TABLE plugins DROP COLUMN remote_secret;
-ALTER TABLE plugins DROP COLUMN remote_url;
diff --git a/migrations/sqlite/000037_plugin_remote.up.sql b/migrations/sqlite/000037_plugin_remote.up.sql
deleted file mode 100644
index a872eed8d..000000000
--- a/migrations/sqlite/000037_plugin_remote.up.sql
+++ /dev/null
@@ -1,3 +0,0 @@
--- Remote plugin URL and signing secret (versioned 000118).
-ALTER TABLE plugins ADD COLUMN remote_url VARCHAR(1024) NOT NULL DEFAULT '';
-ALTER TABLE plugins ADD COLUMN remote_secret TEXT NOT NULL DEFAULT '';
diff --git a/migrations/sqlite/000038_plugin_oauth.down.sql b/migrations/sqlite/000038_plugin_oauth.down.sql
deleted file mode 100644
index b2e21570b..000000000
--- a/migrations/sqlite/000038_plugin_oauth.down.sql
+++ /dev/null
@@ -1 +0,0 @@
-DROP TABLE IF EXISTS plugin_oauth_connections;
diff --git a/migrations/sqlite/000038_plugin_oauth.up.sql b/migrations/sqlite/000038_plugin_oauth.up.sql
deleted file mode 100644
index 84d0916e4..000000000
--- a/migrations/sqlite/000038_plugin_oauth.up.sql
+++ /dev/null
@@ -1,16 +0,0 @@
--- OAuth connections of plugin forms (versioned 000119).
-CREATE TABLE IF NOT EXISTS plugin_oauth_connections (
- id VARCHAR(36) PRIMARY KEY,
- plugin_id VARCHAR(128) NOT NULL,
- tenant_id BIGINT NOT NULL,
- scope VARCHAR(16) NOT NULL,
- contribution VARCHAR(160) NOT NULL DEFAULT '',
- field VARCHAR(256) NOT NULL,
- token TEXT NOT NULL,
- expires_at DATETIME,
- created_by VARCHAR(64) NOT NULL DEFAULT '',
- created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
- updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE INDEX IF NOT EXISTS idx_plugin_oauth_connections_owner ON plugin_oauth_connections (plugin_id, tenant_id);
diff --git a/migrations/versioned/000115_plugin_tenant_settings.down.sql b/migrations/versioned/000115_plugin_tenant_settings.down.sql
deleted file mode 100644
index d00ca9693..000000000
--- a/migrations/versioned/000115_plugin_tenant_settings.down.sql
+++ /dev/null
@@ -1 +0,0 @@
-DROP TABLE IF EXISTS plugin_tenant_settings;
diff --git a/migrations/versioned/000115_plugin_tenant_settings.up.sql b/migrations/versioned/000115_plugin_tenant_settings.up.sql
deleted file mode 100644
index a92b6423a..000000000
--- a/migrations/versioned/000115_plugin_tenant_settings.up.sql
+++ /dev/null
@@ -1,17 +0,0 @@
--- Migration 000115: per-tenant plugin switches and configuration. A plugin
--- without a row is enabled; builtin plugins get rows too once a tenant
--- changes them.
-DO $$ BEGIN RAISE NOTICE '[Migration 000115] Creating plugin_tenant_settings'; END $$;
-
-CREATE TABLE IF NOT EXISTS plugin_tenant_settings (
- tenant_id BIGINT NOT NULL,
- plugin_id VARCHAR(128) NOT NULL,
- enabled BOOLEAN NOT NULL DEFAULT TRUE,
- config JSONB,
- updated_by VARCHAR(36) NOT NULL DEFAULT '',
- updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
- PRIMARY KEY (tenant_id, plugin_id)
-);
-
-COMMENT ON TABLE plugin_tenant_settings IS
- 'Per-tenant plugin enablement and tenant-level plugin configuration (secrets sealed per the plugin config schema).';
diff --git a/migrations/versioned/000116_plugins.down.sql b/migrations/versioned/000116_plugins.down.sql
deleted file mode 100644
index f5d53353f..000000000
--- a/migrations/versioned/000116_plugins.down.sql
+++ /dev/null
@@ -1,2 +0,0 @@
-DROP TABLE IF EXISTS plugin_versions;
-DROP TABLE IF EXISTS plugins;
diff --git a/migrations/versioned/000116_plugins.up.sql b/migrations/versioned/000116_plugins.up.sql
deleted file mode 100644
index 6a145e7ca..000000000
--- a/migrations/versioned/000116_plugins.up.sql
+++ /dev/null
@@ -1,35 +0,0 @@
--- Migration 000116: installed plugins and their versions. Builtin plugins
--- are compiled in and have no rows here.
-DO $$ BEGIN RAISE NOTICE '[Migration 000116] Creating plugins and plugin_versions'; END $$;
-
-CREATE TABLE IF NOT EXISTS plugins (
- id VARCHAR(128) PRIMARY KEY,
- -- NULL for platform plugins installed by a system admin.
- owner_tenant_id BIGINT,
- source JSONB NOT NULL DEFAULT '{}',
- active_version VARCHAR(64) NOT NULL,
- -- enabled | disabled: whether the plugin loads on any node.
- desired_state VARCHAR(16) NOT NULL DEFAULT 'enabled',
- runtime VARCHAR(32) NOT NULL,
- granted_perms JSONB NOT NULL DEFAULT '{}',
- system_config JSONB,
- created_by VARCHAR(36) NOT NULL DEFAULT '',
- created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
- updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
-);
-
-CREATE TABLE IF NOT EXISTS plugin_versions (
- plugin_id VARCHAR(128) NOT NULL,
- version VARCHAR(64) NOT NULL,
- digest VARCHAR(80) NOT NULL,
- manifest JSONB NOT NULL,
- -- Where the package archive is stored (FileService path).
- package_uri VARCHAR(1024) NOT NULL,
- size BIGINT NOT NULL DEFAULT 0,
- created_by VARCHAR(36) NOT NULL DEFAULT '',
- created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
- PRIMARY KEY (plugin_id, version)
-);
-
-COMMENT ON TABLE plugins IS 'Installed (non-builtin) plugins and their desired state; every node converges to it.';
-COMMENT ON TABLE plugin_versions IS 'Stored package versions of installed plugins, kept for rollback.';
diff --git a/migrations/versioned/000117_plugin_kv.down.sql b/migrations/versioned/000117_plugin_kv.down.sql
deleted file mode 100644
index e9a7aae49..000000000
--- a/migrations/versioned/000117_plugin_kv.down.sql
+++ /dev/null
@@ -1 +0,0 @@
-DROP TABLE IF EXISTS plugin_kv;
diff --git a/migrations/versioned/000117_plugin_kv.up.sql b/migrations/versioned/000117_plugin_kv.up.sql
deleted file mode 100644
index 8b4d2aee1..000000000
--- a/migrations/versioned/000117_plugin_kv.up.sql
+++ /dev/null
@@ -1,18 +0,0 @@
--- Migration 000117: the key-value store plugins use through the Host API.
--- Plugins keep no database of their own; their state lives here, partitioned
--- by plugin and tenant so a plugin cannot read another workspace's data.
-DO $$ BEGIN RAISE NOTICE '[Migration 000117] Creating plugin_kv'; END $$;
-
-CREATE TABLE IF NOT EXISTS plugin_kv (
- plugin_id VARCHAR(128) NOT NULL,
- tenant_id BIGINT NOT NULL,
- key VARCHAR(256) NOT NULL,
- value JSONB NOT NULL,
- expires_at TIMESTAMPTZ,
- updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
- PRIMARY KEY (plugin_id, tenant_id, key)
-);
-
-CREATE INDEX IF NOT EXISTS idx_plugin_kv_expires_at ON plugin_kv (expires_at) WHERE expires_at IS NOT NULL;
-
-COMMENT ON TABLE plugin_kv IS 'Host API key-value store of plugins, per plugin and tenant.';
diff --git a/migrations/versioned/000118_plugin_remote.down.sql b/migrations/versioned/000118_plugin_remote.down.sql
deleted file mode 100644
index c158d5daf..000000000
--- a/migrations/versioned/000118_plugin_remote.down.sql
+++ /dev/null
@@ -1,2 +0,0 @@
-ALTER TABLE plugins DROP COLUMN IF EXISTS remote_secret;
-ALTER TABLE plugins DROP COLUMN IF EXISTS remote_url;
diff --git a/migrations/versioned/000118_plugin_remote.up.sql b/migrations/versioned/000118_plugin_remote.up.sql
deleted file mode 100644
index 0140aab2f..000000000
--- a/migrations/versioned/000118_plugin_remote.up.sql
+++ /dev/null
@@ -1,7 +0,0 @@
--- Migration 000118: where a remote plugin runs and the secret its calls are
--- signed with. Only runtime.type remote plugins set them.
-DO $$ BEGIN RAISE NOTICE '[Migration 000118] Adding remote plugin columns'; END $$;
-
-ALTER TABLE plugins ADD COLUMN IF NOT EXISTS remote_url VARCHAR(1024) NOT NULL DEFAULT '';
--- Encrypted (enc:v1) HMAC secret shared with the plugin service.
-ALTER TABLE plugins ADD COLUMN IF NOT EXISTS remote_secret TEXT NOT NULL DEFAULT '';
diff --git a/migrations/versioned/000119_plugin_oauth.down.sql b/migrations/versioned/000119_plugin_oauth.down.sql
deleted file mode 100644
index b2e21570b..000000000
--- a/migrations/versioned/000119_plugin_oauth.down.sql
+++ /dev/null
@@ -1 +0,0 @@
-DROP TABLE IF EXISTS plugin_oauth_connections;
diff --git a/migrations/versioned/000119_plugin_oauth.up.sql b/migrations/versioned/000119_plugin_oauth.up.sql
deleted file mode 100644
index 6159b8799..000000000
--- a/migrations/versioned/000119_plugin_oauth.up.sql
+++ /dev/null
@@ -1,22 +0,0 @@
--- Migration 000119: OAuth connections of plugin forms (x-oauth). A form field
--- holds "oauth:<id>"; the tokens stay here, sealed, and WeKnora refreshes
--- them and hands the plugin a fresh access token at call time.
-DO $$ BEGIN RAISE NOTICE '[Migration 000119] Creating plugin_oauth_connections'; END $$;
-
-CREATE TABLE IF NOT EXISTS plugin_oauth_connections (
- id VARCHAR(36) PRIMARY KEY,
- plugin_id VARCHAR(128) NOT NULL,
- tenant_id BIGINT NOT NULL,
- scope VARCHAR(16) NOT NULL,
- contribution VARCHAR(160) NOT NULL DEFAULT '',
- field VARCHAR(256) NOT NULL,
- token TEXT NOT NULL,
- expires_at TIMESTAMPTZ,
- created_by VARCHAR(64) NOT NULL DEFAULT '',
- created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
- updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
-);
-
-CREATE INDEX IF NOT EXISTS idx_plugin_oauth_connections_owner ON plugin_oauth_connections (plugin_id, tenant_id);
-
-COMMENT ON TABLE plugin_oauth_connections IS 'OAuth tokens behind plugin form fields (x-oauth), sealed.';
diff --git a/packages/plugin-ui/README.md b/packages/plugin-ui/README.md
deleted file mode 100644
index 8de72c9f5..000000000
--- a/packages/plugin-ui/README.md
+++ /dev/null
@@ -1,61 +0,0 @@
-# @weknora/plugin-ui
-
-The page side of the bridge between a WeKnora plugin's pages and the app.
-It has no dependencies and needs no build step.
-
-WeKnora shows a plugin page in a sandboxed iframe:
-- The page has an opaque origin, so it cannot read the app's storage.
-- A CSP (`connect-src 'none'`) stops it reaching the network.
-
-Everything else goes through this bridge.
-
-```js
-import { connect } from './weknora-plugin-ui.js'
-
-const wk = await connect()
-wk.context.mount // "pages/links"
-wk.context.context // what the mount passes, e.g. { knowledgeBaseId }
-const { body } = await wk.get('/links') // the plugin's UI handler answers
-await wk.put('/links', [...body, { url }])
-await wk.toast('Saved', 'success')
-if (await wk.confirm('Remove it?')) { /* … */ }
-wk.navigate('/platform/knowledge-bases')
-```
-
-Ship `index.js` inside the plugin package, for example as
-`ui/weknora-plugin-ui.js`, and load your page script with
-`<script type="module">`.
-
-## What a page can do
-
-| Call | Does |
-| --- | --- |
-| `request(method, path, body)`, `get`, `post`, `put`, `delete` | Calls the plugin's own backend: the `UI` handler of the Go or Python SDK. Resolves `{status, body}`; non-2xx rejects unless `{ throwOnError: false }`. |
-| `toast(message, theme)` | A toast in the app: info, success, warning or error. |
-| `confirm(message, title?)` | A confirmation dialog; resolves true or false. |
-| `navigate(path)` | Opens a page of the app. |
-| `resize(height)` | Sets the frame height. `connect()` does this automatically unless `autoResize: false`. |
-| `close()` | Closes the page, where the mount allows it. |
-| `on('theme' \| 'locale' \| 'init', fn)` | Theme or language changed, or the mount's context changed (another knowledge base). |
-
-`connect()` applies the app's theme:
-- `data-theme="light|dark"` on `<html>`.
-- Design tokens as CSS variables, such as `--wk-brand-color`,
- `--wk-text-color-primary`, `--wk-bg-color-container` and
- `--wk-component-border`.
-
-## Where pages appear
-
-Declare pages in `plugin.yaml` with an `entry` under `ui/`:
-
-| Point | Shown | Default `minRole` |
-| --- | --- | --- |
-| `pages` | A tab of the toolbox | viewer |
-| `settingsSections` | A section of the settings dialog, under Plugins | admin |
-| `kbTabs` | A tab on each knowledge base; `context.knowledgeBaseId` | viewer |
-
-WeKnora checks `minRole` again on each request a page makes. It also
-passes the caller's role to the plugin, so the backend can refuse
-anything finer-grained.
-
-See `examples/plugins/links` for a plugin with all three.
diff --git a/packages/plugin-ui/index.d.ts b/packages/plugin-ui/index.d.ts
deleted file mode 100644
index a67dadcc5..000000000
--- a/packages/plugin-ui/index.d.ts
+++ /dev/null
@@ -1,63 +0,0 @@
-/** Protocol version of the bridge. */
-export const PROTOCOL: 1
-
-export interface Theme {
- mode: 'light' | 'dark'
- /** Design tokens of the app, applied as --wk-<name> CSS variables. */
- tokens: Record<string, string>
-}
-
-export interface PageContext {
- pluginId: string
- version: string
- /** "<point>/<id>", e.g. "pages/links". */
- mount: string
- locale: string
- /** The user's workspace role: viewer, contributor, admin or owner. For what the page offers; the plugin's backend gets the role WeKnora checked. */
- role: string
- theme: Theme
- /** What the mount passes: knowledgeBaseId for kbTabs. */
- context: Record<string, unknown>
-}
-
-export interface Response<T = unknown> {
- status: number
- body: T
-}
-
-export interface RequestOptions {
- /** Reject non-2xx answers (default true). */
- throwOnError?: boolean
-}
-
-export interface Bridge {
- context: PageContext
- request<T = unknown>(method: string, path: string, body?: unknown, opts?: RequestOptions): Promise<Response<T>>
- get<T = unknown>(path: string, opts?: RequestOptions): Promise<Response<T>>
- post<T = unknown>(path: string, body?: unknown, opts?: RequestOptions): Promise<Response<T>>
- put<T = unknown>(path: string, body?: unknown, opts?: RequestOptions): Promise<Response<T>>
- delete<T = unknown>(path: string, opts?: RequestOptions): Promise<Response<T>>
- toast(message: string, theme?: 'info' | 'success' | 'warning' | 'error'): Promise<void>
- confirm(message: string, title?: string): Promise<boolean>
- navigate(path: string): Promise<void>
- resize(height: number): Promise<void>
- close(): Promise<void>
- on(name: 'theme', fn: (theme: Theme) => void): () => void
- on(name: 'locale', fn: (locale: string) => void): () => void
- /** The mount's context changed (another knowledge base). */
- on(name: 'init', fn: (context: PageContext) => void): () => void
-}
-
-export interface ConnectOptions {
- autoResize?: boolean
- applyTheme?: boolean
- timeout?: number
- window?: Window
-}
-
-export class BridgeError extends Error {
- status?: number
-}
-
-export function connect(options?: ConnectOptions): Promise<Bridge>
-export function applyTheme(theme: Theme, doc?: Document): void
diff --git a/packages/plugin-ui/index.js b/packages/plugin-ui/index.js
deleted file mode 100644
index b7910356a..000000000
--- a/packages/plugin-ui/index.js
+++ /dev/null
@@ -1,174 +0,0 @@
-// @weknora/plugin-ui: the bridge between a WeKnora plugin page and the app.
-//
-// A plugin page runs in a sandboxed iframe with an opaque origin and a CSP
-// that forbids network access. Everything it needs goes through this bridge
-// as postMessage calls the app answers: requests to the plugin's backend,
-// toasts, confirmations, navigation, sizing. No dependencies; import it as
-// an ES module from the page (ship a copy under ui/).
-//
-// import { connect } from './weknora-plugin-ui.js'
-// const wk = await connect()
-// const { body } = await wk.get('/links')
-
-/** Protocol version; also marks bridge messages. */
-export const PROTOCOL = 1
-
-const DEFAULT_TIMEOUT = 60_000
-
-function isBridgeMessage(data) {
- return data !== null && typeof data === 'object' && data.weknora === PROTOCOL && typeof data.kind === 'string'
-}
-
-/** A failed bridge call: a refused request, or a non-2xx answer with `throwOnError`. */
-export class BridgeError extends Error {
- constructor(message, status) {
- super(message)
- this.name = 'BridgeError'
- this.status = status
- }
-}
-
-/**
- * Applies the app's theme to this document: `data-theme` on <html> and each
- * token as a CSS variable `--wk-<name>` (e.g. --wk-brand-color).
- */
-export function applyTheme(theme, doc = globalThis.document) {
- if (!doc || !theme) return
- const root = doc.documentElement
- root.setAttribute('data-theme', theme.mode === 'dark' ? 'dark' : 'light')
- for (const [name, value] of Object.entries(theme.tokens || {})) {
- if (/^[a-z0-9-]+$/.test(name) && typeof value === 'string') root.style.setProperty(`--wk-${name}`, value)
- }
-}
-
-/**
- * Connects to the app. Resolves once the app sent the page its context
- * (`init`). Options:
- * autoResize keep the frame as tall as the page (default true)
- * applyTheme apply the app theme as CSS variables (default true)
- * timeout ms to wait for each answer (default 60000)
- * window the page's window (tests)
- */
-export function connect(options = {}) {
- const win = options.window || globalThis.window
- const parent = win.parent
- const timeout = options.timeout || DEFAULT_TIMEOUT
- const pending = new Map()
- const listeners = new Map()
- let nextId = 1
- let bridge
-
- const emit = (name, data) => {
- for (const fn of listeners.get(name) || []) {
- try {
- fn(data)
- } catch (e) {
- console.error(e)
- }
- }
- }
-
- const call = (method, params) =>
- new Promise((resolve, reject) => {
- const id = nextId++
- const timer = setTimeout(() => {
- pending.delete(id)
- reject(new BridgeError(`${method} timed out`))
- }, timeout)
- pending.set(id, { resolve, reject, timer })
- parent.postMessage({ weknora: PROTOCOL, kind: 'request', id, method, params }, '*')
- })
-
- return new Promise((resolveConnect) => {
- win.addEventListener('message', (event) => {
- if (event.source !== parent || !isBridgeMessage(event.data)) return
- const msg = event.data
- if (msg.kind === 'response') {
- const p = pending.get(msg.id)
- if (!p) return
- pending.delete(msg.id)
- clearTimeout(p.timer)
- if (msg.ok) p.resolve(msg.result)
- else p.reject(new BridgeError(msg.error?.message || 'request failed', msg.error?.status))
- return
- }
- if (msg.kind !== 'event') return
- if (msg.name === 'init') {
- const first = !bridge
- bridge = bridge || makeBridge(msg.data)
- bridge.context = msg.data
- if (options.applyTheme !== false) applyTheme(msg.data.theme, win.document)
- if (first) {
- if (options.autoResize !== false) watchHeight(win, bridge)
- resolveConnect(bridge)
- } else {
- emit('init', msg.data)
- }
- return
- }
- if (msg.name === 'theme' && bridge) {
- bridge.context = { ...bridge.context, theme: msg.data }
- if (options.applyTheme !== false) applyTheme(msg.data, win.document)
- }
- if (msg.name === 'locale' && bridge) bridge.context = { ...bridge.context, locale: msg.data }
- emit(msg.name, msg.data)
- })
- parent.postMessage({ weknora: PROTOCOL, kind: 'hello' }, '*')
- })
-
- function makeBridge(context) {
- const request = async (method, path, body, opts = {}) => {
- const res = await call('api.request', { method, path, body })
- if (opts.throwOnError !== false && (res.status < 200 || res.status >= 300)) {
- const err = res.body && typeof res.body === 'object' ? res.body.error : undefined
- const message = typeof err === 'string' ? err : err?.message || `HTTP ${res.status}`
- throw new BridgeError(message, res.status)
- }
- return res
- }
- return {
- /** pluginId, version, mount, locale, theme and the mount's context (e.g. knowledgeBaseId). */
- context,
- /** Calls the plugin's backend (its UI handler). Resolves {status, body}. */
- request,
- get: (path, opts) => request('GET', path, undefined, opts),
- post: (path, body, opts) => request('POST', path, body, opts),
- put: (path, body, opts) => request('PUT', path, body, opts),
- delete: (path, opts) => request('DELETE', path, undefined, opts),
- /** Shows a toast in the app: info, success, warning or error. */
- toast: (message, theme = 'info') => call('ui.toast', { message, theme }),
- /** Asks the user to confirm; resolves true or false. */
- confirm: (message, title) => call('ui.confirm', { message, title }),
- /** Opens a page of the app, by path ("/platform/knowledge-bases"). */
- navigate: (path) => call('ui.navigate', { path }),
- /** Sets the frame height in pixels (autoResize does this for you). */
- resize: (height) => call('ui.resize', { height }),
- /** Closes the page, where the mount allows it. */
- close: () => call('ui.close', {}),
- /**
- * Listens to app events: theme, locale, and init (the mount's context
- * changed, e.g. another knowledge base). Returns an unsubscribe function.
- */
- on(name, fn) {
- if (!listeners.has(name)) listeners.set(name, new Set())
- listeners.get(name).add(fn)
- return () => listeners.get(name).delete(fn)
- },
- }
- }
-}
-
-function watchHeight(win, bridge) {
- const doc = win.document
- if (!doc || typeof win.ResizeObserver !== 'function') return
- let last = 0
- const report = () => {
- const height = Math.ceil(doc.documentElement.scrollHeight)
- if (height !== last) {
- last = height
- bridge.resize(height).catch(() => {})
- }
- }
- new win.ResizeObserver(report).observe(doc.documentElement)
- report()
-}
diff --git a/packages/plugin-ui/package.json b/packages/plugin-ui/package.json
deleted file mode 100644
index ccfa2341a..000000000
--- a/packages/plugin-ui/package.json
+++ /dev/null
@@ -1,13 +0,0 @@
-{
- "name": "@weknora/plugin-ui",
- "version": "0.1.0",
- "description": "Bridge between WeKnora plugin pages (sandboxed iframes) and the WeKnora app",
- "license": "MIT",
- "type": "module",
- "main": "index.js",
- "types": "index.d.ts",
- "exports": { ".": { "types": "./index.d.ts", "default": "./index.js" } },
- "files": ["index.js", "index.d.ts", "README.md"],
- "keywords": ["weknora", "plugin"],
- "repository": { "type": "git", "url": "https://github.com/Tencent/WeKnora.git", "directory": "packages/plugin-ui" }
-}
diff --git a/pluginsdk/README.md b/pluginsdk/README.md
deleted file mode 100644
index 61b500277..000000000
--- a/pluginsdk/README.md
+++ /dev/null
@@ -1,385 +0,0 @@
-# WeKnora plugin SDK (Go)
-
-Build code plugins for WeKnora: web search providers, data source
-connectors and document parsers that run as their own process. The module has no dependencies.
-
-| Package | What it is |
-| --- | --- |
-| `pluginapi` | The extension protocol v1: envelope, errors, stream events, messages. `openapi.yaml` describes it for other languages. |
-| `pluginsdk` | Write a plugin: register contributions, then `Serve`. |
-| `client` | Call a plugin, as WeKnora does. |
-| `conformance`, `cmd/weknora-plugin-conformance` | Check that a plugin speaks the protocol. |
-
-Writing Python? [`python/`](python/README.md) is the same SDK for Python
-3.9+, with only the standard library.
-
-## A connector in 30 lines
-
-```go
-package main
-
-import (
- "context"
- "log"
-
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-type notes struct{}
-
-func (notes) Validate(ctx context.Context, call *pluginsdk.Call, cfg pluginsdk.ConnectorConfig) error {
- if cfg.Credentials["token"] == nil {
- return pluginapi.InvalidConfig("token is required", map[string]string{"credentials.token": "required"})
- }
- return nil
-}
-
-func (notes) ListResources(ctx context.Context, call *pluginsdk.Call, cfg pluginsdk.ConnectorConfig, parent string) ([]pluginapi.Resource, error) {
- return []pluginapi.Resource{{ExternalID: "inbox", Name: "Inbox"}}, nil
-}
-
-func (notes) Fetch(ctx context.Context, call *pluginsdk.Call, cfg pluginsdk.ConnectorConfig, in pluginapi.FetchInput, s *pluginsdk.Stream) (*pluginapi.Cursor, error) {
- if err := s.Item(pluginapi.FetchedItem{ExternalID: "n1", Title: "First note", Content: []byte("# Hello")}); err != nil {
- return nil, err
- }
- return &pluginapi.Cursor{State: map[string]any{"after": "n1"}}, nil
-}
-
-func main() {
- p := pluginsdk.New(pluginsdk.Info{ID: "acme.notes", Version: "1.0.0"})
- p.Connector("notes", notes{})
- log.Fatal(p.Serve())
-}
-```
-
-What the SDK does for you:
-
-- **Handshake.** Started by WeKnora's plugin host, the plugin listens on the
- socket it is given and accepts only the host's token.
-- **Remote mode.** Run on its own, it listens on `WEKNORA_PLUGIN_ADDR` and
- verifies signed requests with `WEKNORA_PLUGIN_SECRET`.
-- **Per call.** Each call's deadline reaches your context. Returned
- `pluginapi` errors keep their code; anything else, and any panic, becomes
- `internal`.
-
-Every call carries the tenant and the configuration, secrets already
-decrypted, in three separate scopes:
-
-- `call.Config.System`: set by the system administrator.
-- `call.Config.Tenant`: set per workspace.
-- the instance: `cfg`, or `call.DecodeInstance`.
-
-Keep plugins stateless and never cache a tenant's credentials across calls.
-
-## Parsers
-
-`p.Parser(id, ...)` turns a document (`in.Content`, base64 on the wire) into
-Markdown. WeKnora chunks the text and stores the images the Markdown
-references: return them in `Images` with an `OriginalRef` matching the
-`![](...)` target. Declare the extensions it handles in `plugin.yaml`
-(`fileTypes: [srt, vtt]`). Return a retryable error (`unavailable`,
-`rate_limited`) to have the document retried later; any other error fails
-it for good.
-
-## Pages
-
-A plugin can add pages to the app:
-- `pages`: toolbox tabs.
-- `settingsSections`: sections of the settings dialog.
-- `kbTabs`: tabs on each knowledge base.
-
-Each is an HTML `entry` under `ui/` in the package. It runs in a sandboxed
-iframe and talks to the app through
-[`@weknora/plugin-ui`](../packages/plugin-ui). A page's requests reach the
-plugin's `UI` handler:
-
-```go
-p.UI(func(ctx context.Context, call *pluginsdk.Call, req pluginapi.UIRequest) (*pluginapi.UIResponse, error) {
- // req.Mount ("pages/links"), req.Method, req.Path, req.Body, req.Role
- return pluginsdk.UIJSON(200, links)
-})
-```
-
-WeKnora refuses a caller below the page's `minRole` before the call.
-The handler can check `req.Role` for anything finer.
-
-## Events
-
-List the events a plugin wants in `permissions.events`. The administrator
-sees them at install.
-
-| Event | When | Data |
-| --- | --- | --- |
-| `knowledge.ingested` | A document finished processing | `KnowledgeEventData` |
-| `knowledge.failed` | A document failed for good | `KnowledgeEventData` with `error` |
-| `knowledge.deleted` | A document was deleted | `KnowledgeEventData` |
-| `chat.answered` | An answer completed | `ChatEventData`, including the question and answer |
-
-```go
-p.OnEvent(func(ctx context.Context, call *pluginsdk.Call, ev pluginapi.EventDelivery) error {
- // ev.ID stays the same across retries: deduplicate on it.
- return nil
-})
-```
-
-Delivery is at least once, in the background:
-- A workspace receives events only while it has the plugin switched on.
-- A retryable error (`unavailable`, `rate_limited`) or a timeout brings
- the event back later with the same `ID` and a higher `Attempt`.
-- Any other error drops the event.
-- Ignore types you do not know: the list only grows.
-
-## Webhooks
-
-`contributes.webhooks` gives each workspace a secret URL per webhook,
-under `/api/v1/plugin-callbacks/`. Workspace admins copy it from the plugin
-center, and it is in `call.Context.Webhooks` when WeKnora knows its
-public address.
-
-```go
-p.Webhook("events", func(ctx context.Context, call *pluginsdk.Call, req pluginapi.WebhookRequest) (*pluginapi.WebhookResponse, error) {
- // call is the workspace the URL belongs to; verify the sender with its configuration.
- return &pluginapi.WebhookResponse{Status: 204}, nil
-})
-```
-
-WeKnora answers 404 unless all of these hold:
-- the URL is genuine;
-- the webhook exists;
-- the workspace has the plugin on.
-
-Bodies are limited to 1 MB, with 20 calls a second per URL.
-
-## Agent tools
-
-Agent tools are MCP tools. A plugin with code can serve them itself: declare
-an `mcpServers` contribution without `mcp.url`, then add the tools.
-
-```yaml
-contributes:
- mcpServers:
- - id: tools
- name: { en-US: ACME Issues }
- toolViews: # optional: how the chat shows structured results
- search_issues:
- view: table
- items: issues
- columns: [{ field: key, link: url }, summary, status]
-```
-
-```go
-p.Tool("tools", pluginapi.Tool{
- Name: "search_issues",
- Description: "Search issues",
- InputSchema: json.RawMessage(`{"type":"object","properties":{"text":{"type":"string"}}}`),
-}, func(ctx context.Context, call *pluginsdk.Call, args json.RawMessage) (*pluginapi.ToolResult, error) {
- // call.Config.Tenant is the workspace's plugin configuration.
- return pluginapi.StructuredResult(map[string]any{"issues": rows}, "3 issues ..."), nil
-})
-```
-
-How it works:
-- **Discovery.** Every workspace that switches the plugin on gets the server
- as a read-only MCP service. Agents use it like any MCP service, with the
- same per-tool approval settings.
-- **Transport.** Messages reach the plugin at `/v1/mcp/{id}` inside the usual
- envelope. Each call therefore carries the workspace's context and
- configuration (OAuth fields already swapped for tokens), and the server
- keeps no session.
-- **Results.** The model reads the text content. Return failures the model
- should see as `pluginapi.ToolError` (or any error); they become results with
- `isError`.
-- **`toolViews`** map a tool's `structuredContent` to a view, so the chat can
- show it without plugin code:
-
-| View | Shows | Fields |
-| --- | --- | --- |
-| `table` | A list as rows | `items`, `columns` (field, title, link) |
-| `cards` | A list as cards | `items`, `title`, `subtitle`, `body`, `link` |
-| `kv` | One object's fields | `items`, `title`, `link`, `fields` |
-| `markdown` | Rendered Markdown | `field` (the text content when empty) |
-| `json` | The data, formatted | - |
-| `page` | A page of the plugin (`entry` under `ui/`) | Gets `{tool, arguments, result}` as its context |
-
- Paths are dotted field paths into the structured content. A page's
- requests use the mount `mcpServers/<id>`.
-
-A plugin can also point `mcp.url` at an MCP server it runs elsewhere; then
-headers carry the configuration (`${config.<key>}`), and `toolViews` apply
-too.
-
-## Dynamic choices and OAuth
-
-Two schema keywords let a form ask the plugin while someone fills it in.
-
-**`x-options`** loads a field's choices from the plugin, for lists that
-depend on the account (projects, spaces, channels). It works on a string or
-an array of strings:
-
-```yaml
-project:
- type: string
- x-options: { name: projects, dependsOn: [site, token], search: true }
-```
-
-```go
-p.Options("projects", func(ctx context.Context, call *pluginsdk.Call, in pluginapi.OptionsInput) ([]pluginapi.Option, error) {
- // call.Config holds what the form holds now, secrets included.
- if call.Config.Tenant["token"] == nil {
- return nil, pluginapi.InvalidConfig("enter the token first", map[string]string{"token": "required"})
- }
- return []pluginapi.Option{{Value: "p1", Label: "Project " + in.Query}}, nil
-})
-```
-
-- `in.Scope` is `system`, `tenant` or `instance`; the values are in the
- matching part of `call.Config`.
-- Secrets the form shows redacted are filled in from the stored
- configuration.
-- The form reloads the list when a `dependsOn` field changes. With
- `search`, `in.Query` is what the user typed.
-- WeKnora does not check a saved value against the list; the plugin
- should.
-
-**`x-oauth`** turns a string field into an account connection. WeKnora runs
-the OAuth 2.0 authorization code flow and keeps the tokens:
-
-```yaml
-config:
- system: schemas/system.yaml # client_id, client_secret (x-secret)
-# schemas/tenant.yaml
-account:
- type: string
- x-oauth:
- authorizeUrl: https://auth.example.com/oauth/authorize
- tokenUrl: https://auth.example.com/oauth/token
- scopes: [read]
- clientId: ${system.client_id}
- clientSecret: ${system.client_secret}
- pkce: true
- params: { audience: api.example.com } # extra authorize parameters
-```
-
-- The form shows a connect button that opens the provider's consent page.
-- The field stores `oauth:<connection>`. At call time the plugin receives a
- fresh access token in its place; WeKnora refreshes it when it is about to
- expire. A connection that can no longer be refreshed arrives as `""`, so
- answer `unauthorized` then.
-- The platform registers the OAuth app with the provider, with the
- redirect URI `<WeKnora address>/api/v1/plugin-oauth/callback`, and enters
- its credentials in the plugin's platform configuration.
-- `tokenUrl` must be https. WeKnora calls it itself, so it needs no
- `permissions.egress` entry.
-
-## Calling back into WeKnora (Host API)
-
-A plugin that declares `permissions.hostApi` gets a short-lived token with
-each call; `call.Host()` returns a client for it, or nil without a grant.
-Scopes:
-
-- `kv`: a key-value store of the plugin, separate per workspace, so the
- plugin needs no database.
- - `KVGet`, `KVPut` (optional TTL), `KVDelete` and `KVList`.
- - Keys up to 256 bytes, JSON values up to 64 KB, 10,000 keys per
- workspace.
-
-The token is valid for a few minutes: use `call.Host()` within the call and
-don't keep it.
-
-## Packaging
-
-A package is a zip (`.wkp`) with `plugin.yaml` at its root:
-
-```yaml
-schemaVersion: 1
-id: acme.notes
-version: 1.0.0
-apiVersion: weknora.plugin/v1
-name: { en-US: ACME Notes }
-publisher: { id: acme }
-runtime: { type: host, kind: binary, entry: "bin/{os}-{arch}/notes" }
-permissions:
- egress: [api.acme.example] # hosts the plugin may reach; "*" = any public host
-contributes:
- connectors:
- - id: notes
- name: { en-US: ACME Notes }
- instanceSchema: schemas/notes.yaml # fields with x-group: settings are settings, the rest credentials
-```
-
-A few rules the manifest and host enforce:
-
-- **Binaries.** Build one per platform into `bin/<os>-<arch>/`; WeKnora
- runs the build for its own OS and architecture.
-- **Python.** A plugin can instead be Python source:
- - Declare `runtime: { type: host, kind: python, entry: main.py }`.
- - WeKnora runs the entry with its own `python3` (or
- `WEKNORA_PLUGIN_PYTHON`).
- - `vendor/` and the package root are on `PYTHONPATH`.
-- **Environment.** The process gets no environment from WeKnora beyond the
- `WEKNORA_PLUGIN_*` variables.
-- **Outbound traffic.** It goes through the host's egress proxy, which
- forwards only to the hosts in `permissions.egress` and never to private
- addresses.
-- **Resources.** `runtime.resources: { cpu: "500m", memory: 512Mi }`
- caps the process (Linux). Memory is always capped. CPU is capped only
- when the platform delegates a cgroup to WeKnora
- (`WEKNORA_PLUGIN_CGROUP`).
-- **IDs.** Contribution IDs are qualified with the plugin ID
- (`acme.notes/notes`); for connectors and web search that must stay within
- 50 characters.
-
-Complete plugins with their `package.sh`:
-
-- `examples/plugins/rss`: a connector.
-- `examples/plugins/subtitles`: a parser using the Host API.
-- `examples/plugins/notebooks`: a parser written in Python.
-- `examples/plugins/links`: pages (toolbox, settings, knowledge base tab), in Python.
-- `examples/plugins/activity`: events, a webhook and a page, in Python.
-- `examples/plugins/jira`: a Jira Cloud connector with an OAuth field, dynamic
- options, incremental sync with deletions; agent tools with result views; and
- a skill.
-
-## Testing
-
-```bash
-go run github.com/Tencent/WeKnora/pluginsdk/cmd/weknora-plugin-conformance -url http://localhost:8080 -secret $SECRET
-```
-
-In Go tests, run `conformance.Run` against `httptest.NewServer(p.Handler())`.
-
-## Installing
-
-In WeKnora: **System administration → Plugin management → Install plugin**.
-Upload the `.wkp` or give its URL, review what it adds and reaches, and
-install. Each workspace then enables it under **Settings → Plugins**.
-
-## Running as a remote service
-
-A plugin can also run as a service you deploy yourself, for instance in its
-own container or on another team's cluster. Its package then carries only
-`plugin.yaml` (plus schemas):
-
-```yaml
-runtime: { type: remote }
-```
-
-1. **Install.** Give the service URL when installing. WeKnora checks the
- service's `/v1/manifest` against the package: same ID, version and
- contributions.
-2. **Keep the secret.** Installing shows a signing secret once. Start the
- service with it as `WEKNORA_PLUGIN_SECRET` (and `WEKNORA_PLUGIN_ADDR`,
- default `:8080`). The SDK rejects requests without a valid signature.
-3. **Private hosts.** A service on a private network must be listed in
- WeKnora's `SSRF_WHITELIST`.
-4. **Host API.** Remote plugins get a Host API token only when
- `WEKNORA_PLUGIN_HOST_API_URL` tells WeKnora its address as the service
- sees it.
-
-The plugin detail page changes the URL and rotates the secret. After a
-rotation, calls fail until the service has the new secret.
-
-Upgrade the service and the package together. While the service reports a
-version other than the active package, calls are refused and the plugin
-shows as degraded.
diff --git a/pluginsdk/client/client.go b/pluginsdk/client/client.go
deleted file mode 100644
index 060899dd0..000000000
--- a/pluginsdk/client/client.go
+++ /dev/null
@@ -1,266 +0,0 @@
-// Package client calls a WeKnora code plugin over the extension protocol. It
-// is what WeKnora itself uses, and what the conformance suite tests plugins
-// with.
-package client
-
-import (
- "bufio"
- "bytes"
- "context"
- "encoding/json"
- "fmt"
- "io"
- "net"
- "net/http"
- "strconv"
- "strings"
- "time"
-
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// Auth adds credentials to a request.
-type Auth interface {
- Apply(req *http.Request, body []byte) error
-}
-
-// Bearer authenticates as the plugin host.
-type Bearer string
-
-// Apply implements Auth.
-func (b Bearer) Apply(req *http.Request, _ []byte) error {
- req.Header.Set("Authorization", "Bearer "+string(b))
- return nil
-}
-
-// Signed authenticates to a remote plugin with its shared secret.
-type Signed []byte
-
-// Apply implements Auth.
-func (s Signed) Apply(req *http.Request, body []byte) error {
- ts := time.Now().Unix()
- req.Header.Set(pluginapi.TimestampHeader, strconv.FormatInt(ts, 10))
- req.Header.Set(pluginapi.SignatureHeader, pluginapi.Sign(s, ts, body))
- return nil
-}
-
-// TransportError is a failure without a protocol answer: the plugin was
-// unreachable, answered something that is not the protocol, or broke off a
-// stream. It unwraps to a *pluginapi.Error (unavailable or internal) so
-// callers can treat every failure alike.
-type TransportError struct {
- Err *pluginapi.Error
-}
-
-func (e *TransportError) Error() string { return e.Err.Error() }
-func (e *TransportError) Unwrap() error { return e.Err }
-
-func transport(code pluginapi.ErrorCode, format string, args ...any) error {
- return &TransportError{Err: &pluginapi.Error{
- Code: code, Message: fmt.Sprintf(format, args...), Retryable: code == pluginapi.CodeUnavailable,
- }}
-}
-
-// Client calls one plugin process or service.
-type Client struct {
- base string
- http *http.Client
- auth Auth
-}
-
-// ForHandshake reaches a host plugin at the address its handshake names.
-// The returned client has no overall timeout: calls are bounded by their
-// context.
-func ForHandshake(h pluginapi.Handshake, auth Auth) *Client {
- if h.Network == "unix" {
- tr := &http.Transport{
- DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
- var d net.Dialer
- return d.DialContext(ctx, "unix", h.Address)
- },
- MaxIdleConnsPerHost: 16,
- IdleConnTimeout: 90 * time.Second,
- }
- return &Client{base: "http://plugin", http: &http.Client{Transport: tr}, auth: auth}
- }
- return New("http://"+h.Address, nil, auth)
-}
-
-// New reaches a plugin at an HTTP(S) base URL. A nil httpClient uses a
-// client without an overall timeout.
-func New(baseURL string, httpClient *http.Client, auth Auth) *Client {
- if httpClient == nil {
- httpClient = &http.Client{}
- }
- return &Client{base: strings.TrimSuffix(baseURL, "/"), http: httpClient, auth: auth}
-}
-
-// Close releases idle connections.
-func (c *Client) Close() { c.http.CloseIdleConnections() }
-
-func (c *Client) do(ctx context.Context, method, path string, body []byte, requestID string) (*http.Response, error) {
- var rd io.Reader
- if body != nil {
- rd = bytes.NewReader(body)
- }
- req, err := http.NewRequestWithContext(ctx, method, c.base+path, rd)
- if err != nil {
- return nil, err
- }
- req.Header.Set(pluginapi.ProtocolHeader, pluginapi.ProtocolVersion)
- if body != nil {
- req.Header.Set("Content-Type", "application/json")
- }
- if requestID != "" {
- req.Header.Set(pluginapi.RequestIDHeader, requestID)
- }
- if c.auth != nil {
- if err := c.auth.Apply(req, body); err != nil {
- return nil, err
- }
- }
- resp, err := c.http.Do(req)
- if err != nil {
- return nil, transport(pluginapi.CodeUnavailable, "%v", err)
- }
- return resp, nil
-}
-
-// Raw sends one request as the client would and returns the answer
-// unread, whatever its status: for gateways that relay a plugin's answers,
-// streams included. The caller closes the body.
-func (c *Client) Raw(ctx context.Context, method, path string, body []byte, requestID string) (*http.Response, error) {
- return c.do(ctx, method, path, body, requestID)
-}
-
-// decodeError turns a non-2xx answer into a protocol error.
-func decodeError(resp *http.Response) error {
- b, _ := io.ReadAll(io.LimitReader(resp.Body, 64<<10))
- var body pluginapi.ErrorBody
- if json.Unmarshal(b, &body) == nil && body.Error.Code != "" {
- return &body.Error
- }
- code := pluginapi.CodeInternal
- if resp.StatusCode == http.StatusServiceUnavailable || resp.StatusCode == http.StatusBadGateway {
- code = pluginapi.CodeUnavailable
- }
- return transport(code, "plugin answered HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(b)))
-}
-
-// Get reads a GET endpoint (manifest, health) into out.
-func (c *Client) Get(ctx context.Context, path string, out any) error {
- resp, err := c.do(ctx, http.MethodGet, path, nil, "")
- if err != nil {
- return err
- }
- defer func() { _ = resp.Body.Close() }()
- if resp.StatusCode/100 != 2 {
- return decodeError(resp)
- }
- return json.NewDecoder(resp.Body).Decode(out)
-}
-
-// Manifest reads GET /v1/manifest.
-func (c *Client) Manifest(ctx context.Context) (*pluginapi.Manifest, error) {
- var m pluginapi.Manifest
- return &m, c.Get(ctx, "/v1/manifest", &m)
-}
-
-// Health reads GET /v1/health.
-func (c *Client) Health(ctx context.Context) error {
- var h pluginapi.Health
- if err := c.Get(ctx, "/v1/health", &h); err != nil {
- return err
- }
- if h.Status != "ok" {
- return &pluginapi.Error{Code: pluginapi.CodeUnavailable, Message: "plugin reports " + h.Status, Retryable: true}
- }
- return nil
-}
-
-func envelopeBody(env pluginapi.Envelope, input any) ([]byte, error) {
- if input != nil {
- raw, err := json.Marshal(input)
- if err != nil {
- return nil, err
- }
- env.Input = raw
- }
- return json.Marshal(env)
-}
-
-// Call posts an envelope and decodes the output into out (nil discards it).
-func (c *Client) Call(ctx context.Context, path string, env pluginapi.Envelope, input, out any) error {
- body, err := envelopeBody(env, input)
- if err != nil {
- return err
- }
- resp, err := c.do(ctx, http.MethodPost, path, body, env.Context.RequestID)
- if err != nil {
- return err
- }
- defer func() { _ = resp.Body.Close() }()
- if resp.StatusCode/100 != 2 {
- return decodeError(resp)
- }
- var wrapped pluginapi.Output
- if err := json.NewDecoder(resp.Body).Decode(&wrapped); err != nil {
- return transport(pluginapi.CodeInternal, "decode plugin output: %v", err)
- }
- if out == nil || len(wrapped.Output) == 0 {
- return nil
- }
- return json.Unmarshal(wrapped.Output, out)
-}
-
-// MaxEventBytes bounds one NDJSON line (one fetched item).
-const MaxEventBytes = 64 << 20
-
-// Stream posts an envelope and hands each event to fn until the stream ends.
-// It returns the "end" event's data, the "error" event as an error, or an
-// error when the stream stops without either.
-func (c *Client) Stream(
- ctx context.Context, path string, env pluginapi.Envelope, input any,
- fn func(pluginapi.Event) error,
-) (json.RawMessage, error) {
- body, err := envelopeBody(env, input)
- if err != nil {
- return nil, err
- }
- resp, err := c.do(ctx, http.MethodPost, path, body, env.Context.RequestID)
- if err != nil {
- return nil, err
- }
- defer func() { _ = resp.Body.Close() }()
- if resp.StatusCode/100 != 2 {
- return nil, decodeError(resp)
- }
- sc := bufio.NewScanner(resp.Body)
- sc.Buffer(make([]byte, 0, 64<<10), MaxEventBytes)
- for sc.Scan() {
- line := bytes.TrimSpace(sc.Bytes())
- if len(line) == 0 {
- continue
- }
- var ev pluginapi.Event
- if err := json.Unmarshal(line, &ev); err != nil {
- return nil, transport(pluginapi.CodeInternal, "decode stream event: %v", err)
- }
- switch ev.Type {
- case pluginapi.EventEnd:
- return ev.Data, nil
- case pluginapi.EventError:
- if ev.Error == nil {
- return nil, &pluginapi.Error{Code: pluginapi.CodeInternal, Message: "plugin stream failed"}
- }
- return nil, ev.Error
- }
- if err := fn(ev); err != nil {
- return nil, err
- }
- }
- if err := sc.Err(); err != nil {
- return nil, transport(pluginapi.CodeUnavailable, "stream broken: %v", err)
- }
- return nil, transport(pluginapi.CodeUnavailable, "stream ended without an end event")
-}
diff --git a/pluginsdk/cmd/weknora-plugin-conformance/main.go b/pluginsdk/cmd/weknora-plugin-conformance/main.go
deleted file mode 100644
index 8c1cd2afd..000000000
--- a/pluginsdk/cmd/weknora-plugin-conformance/main.go
+++ /dev/null
@@ -1,83 +0,0 @@
-// Command weknora-plugin-conformance checks a running plugin against the
-// extension protocol.
-//
-// weknora-plugin-conformance -url https://plugin.example.com -secret $SECRET
-// weknora-plugin-conformance -socket /tmp/p.sock -token $TOKEN
-package main
-
-import (
- "bytes"
- "context"
- "encoding/json"
- "flag"
- "fmt"
- "net/http"
- "os"
- "time"
-
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/conformance"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-func main() {
- url := flag.String("url", "", "base URL of a remote plugin")
- socket := flag.String("socket", "", "unix socket of a plugin started by hand in host mode")
- token := flag.String("token", "", "host token (with -socket)")
- secret := flag.String("secret", "", "shared secret (with -url)")
- asJSON := flag.Bool("json", false, "print the report as JSON")
- timeout := flag.Duration("timeout", 30*time.Second, "per-check timeout")
- flag.Parse()
-
- var c, anon *client.Client
- var raw func(ctx context.Context, path string, body []byte) (*http.Response, error)
- switch {
- case *socket != "":
- hs := pluginapi.Handshake{Protocol: pluginapi.ProtocolVersion, Network: "unix", Address: *socket}
- c = client.ForHandshake(hs, client.Bearer(*token))
- anon = client.ForHandshake(hs, nil)
- case *url != "":
- var auth client.Auth
- if *secret != "" {
- auth = client.Signed(*secret)
- anon = client.New(*url, nil, nil)
- }
- c = client.New(*url, nil, auth)
- raw = func(ctx context.Context, path string, body []byte) (*http.Response, error) {
- req, err := http.NewRequestWithContext(ctx, http.MethodPost, *url+path, bytes.NewReader(body))
- if err != nil {
- return nil, err
- }
- if auth != nil {
- _ = auth.Apply(req, body)
- }
- return http.DefaultClient.Do(req)
- }
- default:
- fmt.Fprintln(os.Stderr, "give -url or -socket")
- os.Exit(2)
- }
-
- rep := conformance.Run(context.Background(), conformance.Target{
- Client: c, Unauthenticated: anon, Raw: raw, Timeout: *timeout,
- })
- if *asJSON {
- _ = json.NewEncoder(os.Stdout).Encode(rep)
- } else {
- fmt.Printf("plugin %s\n", rep.Plugin)
- for _, r := range rep.Results {
- mark := "PASS"
- if !r.Passed {
- mark = "FAIL"
- }
- fmt.Printf(" %s %s", mark, r.Name)
- if r.Detail != "" {
- fmt.Printf(" — %s", r.Detail)
- }
- fmt.Println()
- }
- }
- if !rep.Passed() {
- os.Exit(1)
- }
-}
diff --git a/pluginsdk/conformance/conformance.go b/pluginsdk/conformance/conformance.go
deleted file mode 100644
index db410b754..000000000
--- a/pluginsdk/conformance/conformance.go
+++ /dev/null
@@ -1,319 +0,0 @@
-// Package conformance checks that a plugin speaks the extension protocol:
-// it answers the discovery endpoints, rejects what it must reject, and
-// answers every endpoint it claims with either a well-formed output or a
-// well-formed protocol error. It does not judge the answers themselves; a
-// plugin's own tests do that.
-package conformance
-
-import (
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "net/http"
- "regexp"
- "time"
-
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// Result is one check.
-type Result struct {
- Name string `json:"name"`
- Passed bool `json:"passed"`
- Detail string `json:"detail,omitempty"`
-}
-
-// Report is every check against one plugin.
-type Report struct {
- Plugin string `json:"plugin"`
- Results []Result `json:"results"`
-}
-
-// Passed reports whether every check passed.
-func (r Report) Passed() bool {
- for _, res := range r.Results {
- if !res.Passed {
- return false
- }
- }
- return len(r.Results) > 0
-}
-
-// Target is the plugin under test.
-type Target struct {
- // Client reaches the plugin with valid credentials.
- Client *client.Client
- // Unauthenticated reaches it without credentials; nil skips the check
- // (a plugin served without authentication in a test harness).
- Unauthenticated *client.Client
- // Raw posts arbitrary bytes, for malformed-request checks; nil skips.
- Raw func(ctx context.Context, path string, body []byte) (*http.Response, error)
- // Timeout bounds each call (default 30s).
- Timeout time.Duration
-}
-
-var semver = regexp.MustCompile(`^\d+\.\d+\.\d+([-+].*)?$`)
-
-// Run checks a plugin.
-func Run(ctx context.Context, t Target) Report {
- if t.Timeout <= 0 {
- t.Timeout = 30 * time.Second
- }
- var rep Report
- check := func(name string, fn func(ctx context.Context) error) {
- cctx, cancel := context.WithTimeout(ctx, t.Timeout)
- defer cancel()
- res := Result{Name: name, Passed: true}
- if err := fn(cctx); err != nil {
- res.Passed, res.Detail = false, err.Error()
- }
- rep.Results = append(rep.Results, res)
- }
-
- var m *pluginapi.Manifest
- check("manifest", func(ctx context.Context) error {
- var err error
- if m, err = t.Client.Manifest(ctx); err != nil {
- return err
- }
- rep.Plugin = m.ID + "@" + m.Version
- switch {
- case m.ID == "":
- return fmt.Errorf("manifest has no id")
- case !semver.MatchString(m.Version):
- return fmt.Errorf("version %q is not semantic", m.Version)
- case m.APIVersion != pluginapi.APIVersion:
- return fmt.Errorf("apiVersion %q, want %q", m.APIVersion, pluginapi.APIVersion)
- case len(m.Contributes) == 0:
- return fmt.Errorf("manifest lists no contributions")
- }
- return nil
- })
- check("health", func(ctx context.Context) error { return t.Client.Health(ctx) })
- if t.Unauthenticated != nil {
- check("rejects unauthenticated calls", func(ctx context.Context) error {
- return wantCode(t.Unauthenticated.Health(ctx), pluginapi.CodeUnauthorized)
- })
- }
- check("unknown endpoint answers not_found", func(ctx context.Context) error {
- return wantCode(
- t.Client.Call(ctx, "/v1/no-such-endpoint", pluginapi.Envelope{}, nil, nil),
- pluginapi.CodeNotFound,
- )
- })
- check("unknown contribution answers not_found", func(ctx context.Context) error {
- return wantCode(t.Client.Call(ctx, pluginapi.SearchPath("no-such-provider"), pluginapi.Envelope{},
- pluginapi.SearchInput{Query: "x"}, nil), pluginapi.CodeNotFound)
- })
- if t.Raw != nil {
- check("malformed envelope answers bad_request", func(ctx context.Context) error {
- resp, err := t.Raw(ctx, "/v1/config/validate", []byte("{not json"))
- if err != nil {
- return err
- }
- defer func() { _ = resp.Body.Close() }()
- return wantErrorBody(resp, pluginapi.CodeBadRequest)
- })
- }
- check("config/validate answers", func(ctx context.Context) error {
- return protocolAnswer(t.Client.Call(ctx, "/v1/config/validate", envelope(), nil, nil))
- })
- if m == nil {
- return rep
- }
- for _, id := range m.Contributes["webSearch"] {
- check("webSearch/"+id+" search answers", func(ctx context.Context) error {
- var out pluginapi.SearchOutput
- err := t.Client.Call(
- ctx,
- pluginapi.SearchPath(id),
- envelope(),
- pluginapi.SearchInput{Query: "weknora", MaxResults: 3},
- &out,
- )
- if err == nil && out.Results == nil {
- return fmt.Errorf("output has no results array")
- }
- return protocolAnswer(err)
- })
- }
- for _, id := range m.Contributes["parsers"] {
- check("parsers/"+id+" parse answers", func(ctx context.Context) error {
- var out pluginapi.ParseOutput
- err := t.Client.Call(ctx, pluginapi.ParsePath(id), envelope(), pluginapi.ParseInput{
- FileName: "conformance.txt", FileType: "txt", Content: []byte("WeKnora conformance check\n"),
- }, &out)
- return protocolAnswer(err)
- })
- }
- if len(m.Contributes["events"]) > 0 {
- check("events accepts a delivery", func(ctx context.Context) error {
- // A type the plugin does not know: it must answer, not crash.
- ev := pluginapi.EventDelivery{
- ID: "conformance", Type: "weknora.conformance", OccurredAt: time.Now(), Attempt: 1,
- Data: json.RawMessage(`{}`),
- }
- return protocolAnswer(t.Client.Call(ctx, pluginapi.EventsPath, envelope(), ev, nil))
- })
- }
- for _, name := range m.Contributes["options"] {
- check("options/"+name+" answers", func(ctx context.Context) error {
- var out pluginapi.OptionsOutput
- in := pluginapi.OptionsInput{Field: "field", Scope: pluginapi.OptionsScopeTenant}
- err := t.Client.Call(ctx, pluginapi.OptionsPath(name), envelope(), in, &out)
- if err == nil && out.Options == nil {
- return fmt.Errorf("output has no options array")
- }
- return protocolAnswer(err)
- })
- }
- for _, id := range m.Contributes["mcpServers"] {
- check("mcp/"+id+" lists its tools", func(ctx context.Context) error {
- var init, list pluginapi.MCPResponse
- params := `{"protocolVersion":"` + pluginapi.MCPProtocolVersion + `","capabilities":{},` +
- `"clientInfo":{"name":"conformance","version":"1"}}`
- req := pluginapi.MCPRequest{
- JSONRPC: "2.0", ID: json.RawMessage(`1`), Method: "initialize", Params: json.RawMessage(params),
- }
- if err := t.Client.Call(ctx, pluginapi.MCPPath(id), envelope(), req, &init); err != nil {
- return err
- }
- if init.Error != nil || len(init.Result) == 0 {
- return fmt.Errorf("initialize failed: %+v", init.Error)
- }
- req = pluginapi.MCPRequest{JSONRPC: "2.0", ID: json.RawMessage(`2`), Method: "tools/list"}
- if err := t.Client.Call(ctx, pluginapi.MCPPath(id), envelope(), req, &list); err != nil {
- return err
- }
- var tools struct {
- Tools []pluginapi.Tool `json:"tools"`
- }
- if list.Error != nil || json.Unmarshal(list.Result, &tools) != nil || len(tools.Tools) == 0 {
- return fmt.Errorf("tools/list answered no tools")
- }
- for _, tool := range tools.Tools {
- var schema struct {
- Type string `json:"type"`
- }
- if tool.Name == "" || json.Unmarshal(tool.InputSchema, &schema) != nil || schema.Type != "object" {
- return fmt.Errorf("tool %q needs a name and an object inputSchema", tool.Name)
- }
- }
- return nil
- })
- }
- for _, id := range m.Contributes["webhooks"] {
- check("webhooks/"+id+" answers", func(ctx context.Context) error {
- var out pluginapi.WebhookResponse
- in := pluginapi.WebhookRequest{Method: http.MethodPost, Path: "/", Body: []byte(`{}`)}
- return protocolAnswer(t.Client.Call(ctx, pluginapi.WebhookPath(id), envelope(), in, &out))
- })
- }
- if len(m.Contributes["ui"]) > 0 {
- check("ui/request answers", func(ctx context.Context) error {
- var out pluginapi.UIResponse
- in := pluginapi.UIRequest{Mount: "pages/conformance", Method: http.MethodGet, Path: "/", Role: "viewer"}
- err := t.Client.Call(ctx, pluginapi.UIRequestPath, envelope(), in, &out)
- return protocolAnswer(err)
- })
- }
- for _, id := range m.Contributes["connectors"] {
- check("connectors/"+id+" validate answers", func(ctx context.Context) error {
- return protocolAnswer(t.Client.Call(ctx, pluginapi.ConnectorValidatePath(id), envelope(), nil, nil))
- })
- check("connectors/"+id+" list-resources answers", func(ctx context.Context) error {
- var out pluginapi.ListResourcesOutput
- err := t.Client.Call(
- ctx,
- pluginapi.ConnectorListResourcesPath(id),
- envelope(),
- pluginapi.ListResourcesInput{},
- &out,
- )
- if err == nil && out.Resources == nil {
- return fmt.Errorf("output has no resources array")
- }
- return protocolAnswer(err)
- })
- check("connectors/"+id+" resolve-ancestors answers", func(ctx context.Context) error {
- var out pluginapi.ResolveAncestorsOutput
- err := t.Client.Call(ctx, pluginapi.ConnectorResolveAncestorsPath(id), envelope(),
- pluginapi.ResolveAncestorsInput{ResourceIDs: []string{}}, &out)
- if err == nil && out.Ancestors == nil {
- return fmt.Errorf("output has no ancestors array")
- }
- return protocolAnswer(err)
- })
- check("connectors/"+id+" fetch stream terminates", func(ctx context.Context) error {
- _, err := t.Client.Stream(ctx, pluginapi.ConnectorFetchPath(id), envelope(),
- pluginapi.FetchInput{Mode: pluginapi.FetchFull}, func(ev pluginapi.Event) error {
- switch ev.Type {
- case pluginapi.EventItem, pluginapi.EventCheckpoint, pluginapi.EventProgress, pluginapi.EventLog:
- return nil
- }
- return fmt.Errorf("unknown event type %q", ev.Type)
- })
- return protocolAnswer(err)
- })
- }
- return rep
-}
-
-// envelope is a call with empty configuration: plugins must answer it with
-// an output or a protocol error, never a crash or garbage.
-func envelope() pluginapi.Envelope {
- return pluginapi.Envelope{Context: pluginapi.Context{TenantID: 1, RequestID: "conformance", Locale: "en-US"}}
-}
-
-// protocolAnswer accepts success or a protocol error the plugin chose, and
-// fails transport failures and internal errors: a plugin must answer an
-// empty configuration with invalid_config, not by crashing.
-func protocolAnswer(err error) error {
- if err == nil {
- return nil
- }
- var te *client.TransportError
- if errors.As(err, &te) {
- return fmt.Errorf("no protocol answer: %v", te)
- }
- e, ok := pluginapi.AsError(err)
- if !ok {
- return err
- }
- if e.Code == pluginapi.CodeInternal {
- return fmt.Errorf("answered internal: %s", e.Message)
- }
- return nil
-}
-
-func wantCode(err error, code pluginapi.ErrorCode) error {
- e, ok := pluginapi.AsError(err)
- if !ok {
- return fmt.Errorf("want a %s protocol error, got %v", code, err)
- }
- if e.Code != code {
- return fmt.Errorf("want %s, got %s: %s", code, e.Code, e.Message)
- }
- return nil
-}
-
-func wantErrorBody(resp *http.Response, code pluginapi.ErrorCode) error {
- b, _ := io.ReadAll(io.LimitReader(resp.Body, 64<<10))
- var body pluginapi.ErrorBody
- if err := json.Unmarshal(b, &body); err != nil {
- return fmt.Errorf("HTTP %d without a protocol error body: %s", resp.StatusCode, b)
- }
- if resp.StatusCode != code.HTTPStatus() || body.Error.Code != code {
- return fmt.Errorf(
- "want HTTP %d %s, got HTTP %d %s",
- code.HTTPStatus(),
- code,
- resp.StatusCode,
- body.Error.Code,
- )
- }
- return nil
-}
diff --git a/pluginsdk/conformance/conformance_test.go b/pluginsdk/conformance/conformance_test.go
deleted file mode 100644
index caa97695b..000000000
--- a/pluginsdk/conformance/conformance_test.go
+++ /dev/null
@@ -1,118 +0,0 @@
-package conformance_test
-
-import (
- "bytes"
- "context"
- "net/http"
- "net/http/httptest"
- "strings"
- "testing"
-
- "github.com/Tencent/WeKnora/pluginsdk"
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/conformance"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-type strictConnector struct{}
-
-func (strictConnector) Validate(_ context.Context, _ *pluginsdk.Call, cfg pluginsdk.ConnectorConfig) error {
- if cfg.Settings["url"] == nil {
- return pluginapi.InvalidConfig("url is required", map[string]string{"settings.url": "required"})
- }
- return nil
-}
-
-func (s strictConnector) ListResources(
- ctx context.Context,
- call *pluginsdk.Call,
- cfg pluginsdk.ConnectorConfig,
- _ string,
-) ([]pluginapi.Resource, error) {
- return nil, s.Validate(ctx, call, cfg)
-}
-
-func (s strictConnector) Fetch(
- ctx context.Context,
- call *pluginsdk.Call,
- cfg pluginsdk.ConnectorConfig,
- _ pluginapi.FetchInput,
- _ *pluginsdk.Stream,
-) (*pluginapi.Cursor, error) {
- return nil, s.Validate(ctx, call, cfg)
-}
-
-func target(t *testing.T, h http.Handler) conformance.Target {
- srv := httptest.NewServer(h)
- t.Cleanup(srv.Close)
- return conformance.Target{
- Client: client.New(srv.URL, nil, nil),
- Raw: func(ctx context.Context, path string, body []byte) (*http.Response, error) {
- req, _ := http.NewRequestWithContext(ctx, http.MethodPost, srv.URL+path, bytes.NewReader(body))
- return http.DefaultClient.Do(req)
- },
- }
-}
-
-func TestSDKPluginConforms(t *testing.T) {
- p := pluginsdk.New(pluginsdk.Info{ID: "acme.strict", Version: "1.0.0"})
- p.Connector("strict", strictConnector{})
- p.WebSearch(
- "none",
- pluginsdk.WebSearchFunc(
- func(context.Context, *pluginsdk.Call, pluginapi.SearchInput) (*pluginapi.SearchOutput, error) {
- return nil, pluginapi.InvalidConfig("api key required", map[string]string{"api_key": "required"})
- },
- ),
- )
- p.Parser(
- "text",
- pluginsdk.ParserFunc(
- func(_ context.Context, _ *pluginsdk.Call, in pluginapi.ParseInput) (*pluginapi.ParseOutput, error) {
- return &pluginapi.ParseOutput{Markdown: string(in.Content)}, nil
- },
- ),
- )
- rep := conformance.Run(context.Background(), target(t, p.Handler()))
- names := map[string]bool{}
- for _, r := range rep.Results {
- names[r.Name] = true
- if !r.Passed {
- t.Errorf("%s: %s", r.Name, r.Detail)
- }
- }
- if !names["parsers/text parse answers"] {
- t.Error("parsers must be checked")
- }
- if !rep.Passed() || rep.Plugin != "[email protected]" || len(rep.Results) < 10 {
- t.Fatalf("report = %+v", rep)
- }
-}
-
-func TestBrokenPluginFails(t *testing.T) {
- p := pluginsdk.New(pluginsdk.Info{ID: "acme.broken", Version: "1.0.0"})
- p.Connector("broken", strictConnector{})
- good := p.Handler()
- // A fetch that returns plain text and a list that crashes.
- broken := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- switch {
- case strings.HasSuffix(r.URL.Path, "/fetch"):
- _, _ = w.Write([]byte("oops"))
- case strings.HasSuffix(r.URL.Path, "/list-resources"):
- panic("nil map")
- default:
- good.ServeHTTP(w, r)
- }
- })
- rep := conformance.Run(context.Background(), target(t, broken))
- failed := map[string]bool{}
- for _, r := range rep.Results {
- if !r.Passed {
- failed[r.Name] = true
- }
- }
- if rep.Passed() || !failed["connectors/broken fetch stream terminates"] ||
- !failed["connectors/broken list-resources answers"] {
- t.Fatalf("failed checks = %v", failed)
- }
-}
diff --git a/pluginsdk/conformance/python_test.go b/pluginsdk/conformance/python_test.go
deleted file mode 100644
index 7c57d5ebd..000000000
--- a/pluginsdk/conformance/python_test.go
+++ /dev/null
@@ -1,175 +0,0 @@
-package conformance_test
-
-import (
- "bufio"
- "bytes"
- "context"
- "net"
- "net/http"
- "os"
- "os/exec"
- "path/filepath"
- "runtime"
- "testing"
- "time"
-
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/conformance"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// The Python SDK speaks the same protocol: its test plugin must pass the
-// suite in both modes, and a Go client must read its answers.
-
-func python(t *testing.T) string {
- t.Helper()
- names := []string{"python3", "python"}
- if runtime.GOOS == "windows" {
- names = []string{"python", "python3"} // python3 may be the Store alias
- }
- for _, name := range names {
- if p, err := exec.LookPath(name); err == nil {
- return p
- }
- }
- t.Skip("python3 is not installed")
- return ""
-}
-
-var fixture = filepath.Join("..", "python", "tests", "fixture.py")
-
-func startPython(t *testing.T, env ...string) *exec.Cmd {
- t.Helper()
- cmd := exec.Command(python(t), fixture)
- cmd.Env = append([]string{"PATH=" + os.Getenv("PATH")}, env...)
- cmd.Stderr = os.Stderr
- return cmd
-}
-
-func stopPython(t *testing.T, cmd *exec.Cmd) {
- t.Cleanup(func() {
- if runtime.GOOS == "windows" {
- _ = cmd.Process.Kill()
- } else {
- _ = cmd.Process.Signal(os.Interrupt)
- }
- done := make(chan struct{})
- go func() { _ = cmd.Wait(); close(done) }()
- select {
- case <-done:
- case <-time.After(10 * time.Second):
- _ = cmd.Process.Kill()
- }
- })
-}
-
-func report(t *testing.T, rep conformance.Report) {
- t.Helper()
- for _, r := range rep.Results {
- if !r.Passed {
- t.Errorf("%s: %s", r.Name, r.Detail)
- }
- }
- if len(rep.Results) < 13 {
- t.Fatalf("only %d checks ran", len(rep.Results))
- }
-}
-
-func TestPythonPluginConformsInHostMode(t *testing.T) {
- if runtime.GOOS == "windows" {
- t.Skip("the Python SDK serves host mode on unix sockets")
- }
- dir, err := os.MkdirTemp("", "wkpy")
- if err != nil {
- t.Fatal(err)
- }
- t.Cleanup(func() { _ = os.RemoveAll(dir) })
- sock := filepath.Join(dir, "p.sock")
- cmd := startPython(t, pluginapi.EnvSocket+"="+sock, pluginapi.EnvToken+"=tok")
- stdout, _ := cmd.StdoutPipe()
- if err := cmd.Start(); err != nil {
- t.Fatal(err)
- }
- stopPython(t, cmd)
- line, err := bufio.NewReader(stdout).ReadString('\n')
- if err != nil {
- t.Fatalf("no handshake: %v", err)
- }
- hs, ok, err := pluginapi.ParseHandshake(line)
- if !ok || err != nil || hs.Address != sock {
- t.Fatalf("handshake %q: %v", line, err)
- }
- c := client.ForHandshake(hs, client.Bearer("tok"))
- report(t, conformance.Run(context.Background(), conformance.Target{
- Client: c, Unauthenticated: client.ForHandshake(hs, nil),
- Raw: func(ctx context.Context, path string, body []byte) (*http.Response, error) {
- tr := &http.Transport{DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
- return (&net.Dialer{}).DialContext(ctx, "unix", sock)
- }}
- req, _ := http.NewRequestWithContext(ctx, http.MethodPost, "http://plugin"+path, bytes.NewReader(body))
- req.Header.Set("Authorization", "Bearer tok")
- return (&http.Client{Transport: tr}).Do(req)
- },
- }))
-
- // A Go client reads the Python stream and errors like a Go plugin's.
- var items int
- cursor, err := c.Stream(context.Background(), pluginapi.ConnectorFetchPath("notes"), pluginapi.Envelope{},
- pluginapi.FetchInput{Mode: pluginapi.FetchFull}, func(ev pluginapi.Event) error {
- if ev.Type == pluginapi.EventItem {
- items++
- }
- return nil
- })
- if err != nil || items != 3 || string(cursor) != `{"state":{"after":3}}` {
- t.Fatalf("fetch = %d items, cursor %s, %v", items, cursor, err)
- }
- var out pluginapi.ParseOutput
- err = c.Call(context.Background(), pluginapi.ParsePath("upper"), pluginapi.Envelope{},
- pluginapi.ParseInput{FileType: "txt", Content: []byte("hi")}, &out)
- if err != nil || len(out.Images) != 1 || string(out.Images[0].Data) != "\x89PNG" {
- t.Fatalf("parse = %+v, %v", out, err)
- }
- err = c.Call(context.Background(), pluginapi.SearchPath("echo"), pluginapi.Envelope{},
- pluginapi.SearchInput{Query: "down"}, nil)
- if pe, ok := pluginapi.AsError(err); !ok || pe.Code != pluginapi.CodeUnavailable || !pe.Retryable {
- t.Fatalf("search error = %v", err)
- }
-}
-
-func TestPythonPluginConformsInRemoteMode(t *testing.T) {
- ln, err := net.Listen("tcp", "127.0.0.1:0")
- if err != nil {
- t.Fatal(err)
- }
- addr := ln.Addr().String()
- _ = ln.Close()
- cmd := startPython(t, pluginapi.EnvAddr+"="+addr, pluginapi.EnvSecret+"=s3cret")
- if err := cmd.Start(); err != nil {
- t.Fatal(err)
- }
- stopPython(t, cmd)
- base := "http://" + addr
- c := client.New(base, nil, client.Signed("s3cret"))
- deadline := time.Now().Add(30 * time.Second)
- for {
- ctx, cancel := context.WithTimeout(context.Background(), time.Second)
- err := c.Health(ctx)
- cancel()
- if err == nil {
- break
- }
- if time.Now().After(deadline) {
- t.Fatalf("the Python plugin did not come up: %v", err)
- }
- time.Sleep(50 * time.Millisecond)
- }
- report(t, conformance.Run(context.Background(), conformance.Target{
- Client: c, Unauthenticated: client.New(base, nil, nil),
- Raw: func(ctx context.Context, path string, body []byte) (*http.Response, error) {
- req, _ := http.NewRequestWithContext(ctx, http.MethodPost, base+path, bytes.NewReader(body))
- _ = client.Signed("s3cret").Apply(req, body)
- return http.DefaultClient.Do(req)
- },
- }))
-}
diff --git a/pluginsdk/connector.go b/pluginsdk/connector.go
deleted file mode 100644
index 0013c6e64..000000000
--- a/pluginsdk/connector.go
+++ /dev/null
@@ -1,160 +0,0 @@
-package pluginsdk
-
-import (
- "context"
- "encoding/json"
- "net/http"
-
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// ConnectorConfig is a data source's instance configuration.
-type ConnectorConfig struct {
- Credentials map[string]any `json:"credentials"`
- Settings map[string]any `json:"settings"`
- ResourceIDs []string `json:"resourceIds"`
-}
-
-// Connector is a data source connector.
-type Connector interface {
- // Validate checks the configuration, typically by calling the source.
- // Return pluginapi.InvalidConfig to point at fields.
- Validate(ctx context.Context, call *Call, cfg ConnectorConfig) error
- // ListResources lists what can be synced under parentID ("" = top).
- ListResources(ctx context.Context, call *Call, cfg ConnectorConfig, parentID string) ([]pluginapi.Resource, error)
- // Fetch streams items changed since cursor (nil = everything) and
- // returns the cursor for the next sync. Checkpoint at page boundaries
- // so an interrupted sync resumes.
- Fetch(
- ctx context.Context,
- call *Call,
- cfg ConnectorConfig,
- in pluginapi.FetchInput,
- s *Stream,
- ) (*pluginapi.Cursor, error)
-}
-
-// AncestorResolver is implemented by connectors whose resource tree loads
-// lazily; see pluginapi.ResolveAncestorsInput. Others answer with nothing.
-type AncestorResolver interface {
- ResolveAncestors(ctx context.Context, call *Call, cfg ConnectorConfig, resourceIDs []string) ([]string, error)
-}
-
-// Connector registers connector id (contributes.connectors[].id).
-func (p *Plugin) Connector(id string, c Connector) { p.connectors[id] = c }
-
-func (p *Plugin) connector(w http.ResponseWriter, r *http.Request) (Connector, bool) {
- c, ok := p.connectors[r.PathValue("id")]
- if !ok {
- writeError(w, pluginapi.Errorf(pluginapi.CodeNotFound, "no connector %q", r.PathValue("id")))
- }
- return c, ok
-}
-
-func connectorConfig(call *Call) (ConnectorConfig, error) {
- var cfg ConnectorConfig
- if err := call.DecodeInstance(&cfg); err != nil {
- return cfg, pluginapi.Errorf(pluginapi.CodeBadRequest, "decode connector config: %v", err)
- }
- return cfg, nil
-}
-
-func (p *Plugin) routeConnectors(mux *http.ServeMux) {
- mux.HandleFunc("POST /v1/connectors/{id}/validate", func(w http.ResponseWriter, r *http.Request) {
- c, ok := p.connector(w, r)
- if !ok {
- return
- }
- p.unary(func(ctx context.Context, call *Call, _ json.RawMessage) (any, error) {
- cfg, err := connectorConfig(call)
- if err != nil {
- return nil, err
- }
- return struct{}{}, c.Validate(ctx, call, cfg)
- })(w, r)
- })
- mux.HandleFunc("POST /v1/connectors/{id}/list-resources", func(w http.ResponseWriter, r *http.Request) {
- c, ok := p.connector(w, r)
- if !ok {
- return
- }
- p.unary(func(ctx context.Context, call *Call, raw json.RawMessage) (any, error) {
- in, err := decodeInput[pluginapi.ListResourcesInput](raw)
- if err != nil {
- return nil, err
- }
- cfg, err := connectorConfig(call)
- if err != nil {
- return nil, err
- }
- res, err := c.ListResources(ctx, call, cfg, in.ParentID)
- if err != nil {
- return nil, err
- }
- if res == nil {
- res = []pluginapi.Resource{}
- }
- return pluginapi.ListResourcesOutput{Resources: res}, nil
- })(w, r)
- })
- mux.HandleFunc("POST /v1/connectors/{id}/resolve-ancestors", func(w http.ResponseWriter, r *http.Request) {
- c, ok := p.connector(w, r)
- if !ok {
- return
- }
- p.unary(func(ctx context.Context, call *Call, raw json.RawMessage) (any, error) {
- in, err := decodeInput[pluginapi.ResolveAncestorsInput](raw)
- if err != nil {
- return nil, err
- }
- out := pluginapi.ResolveAncestorsOutput{Ancestors: []string{}}
- resolver, ok := c.(AncestorResolver)
- if !ok {
- return out, nil
- }
- cfg, err := connectorConfig(call)
- if err != nil {
- return nil, err
- }
- ids, err := resolver.ResolveAncestors(ctx, call, cfg, in.ResourceIDs)
- if err != nil {
- return nil, err
- }
- if ids != nil {
- out.Ancestors = ids
- }
- return out, nil
- })(w, r)
- })
- mux.HandleFunc("POST /v1/connectors/{id}/fetch", func(w http.ResponseWriter, r *http.Request) {
- c, ok := p.connector(w, r)
- if !ok {
- return
- }
- env, err := readEnvelope(r)
- if err != nil {
- writeError(w, err)
- return
- }
- in, err := decodeInput[pluginapi.FetchInput](env.Input)
- if err != nil {
- writeError(w, err)
- return
- }
- call := &Call{Context: env.Context, Config: env.Config}
- cfg, err := connectorConfig(call)
- if err != nil {
- writeError(w, err)
- return
- }
- ctx, cancel := callContext(r.Context(), env)
- defer cancel()
- s := newStream(w)
- cursor, err := c.Fetch(ctx, call, cfg, in, s)
- if err != nil {
- s.fail(err)
- return
- }
- s.end(cursor)
- })
-}
diff --git a/pluginsdk/events.go b/pluginsdk/events.go
deleted file mode 100644
index 28a5ae9a0..000000000
--- a/pluginsdk/events.go
+++ /dev/null
@@ -1,69 +0,0 @@
-package pluginsdk
-
-import (
- "context"
- "encoding/json"
- "net/http"
-
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// EventHandler handles one event delivery. Deliveries repeat (at least
-// once): make it idempotent on ev.ID. Return a retryable error (unavailable,
-// rate_limited) to be called again later; any other error drops the event.
-// Ignore event types you do not know.
-type EventHandler func(ctx context.Context, call *Call, ev pluginapi.EventDelivery) error
-
-// OnEvent registers the handler of the events the plugin subscribed to in
-// permissions.events.
-func (p *Plugin) OnEvent(h EventHandler) { p.events = h }
-
-// WebhookHandler answers one inbound call to a webhook.
-type WebhookHandler func(
- ctx context.Context, call *Call, req pluginapi.WebhookRequest,
-) (*pluginapi.WebhookResponse, error)
-
-// Webhook registers webhook id (contributes.webhooks[].id). call carries the
-// workspace the URL belongs to and its configuration, so the handler can
-// verify the sender's signature with the workspace's secret.
-func (p *Plugin) Webhook(id string, h WebhookHandler) { p.webhooks[id] = h }
-
-func (p *Plugin) routeEvents(mux *http.ServeMux) {
- mux.HandleFunc("POST /v1/events", func(w http.ResponseWriter, r *http.Request) {
- if p.events == nil {
- writeError(w, pluginapi.Errorf(pluginapi.CodeNotFound, "this plugin handles no events"))
- return
- }
- p.unary(func(ctx context.Context, call *Call, raw json.RawMessage) (any, error) {
- ev, err := decodeInput[pluginapi.EventDelivery](raw)
- if err != nil {
- return nil, err
- }
- return struct{}{}, p.events(ctx, call, ev)
- })(w, r)
- })
- mux.HandleFunc("POST /v1/webhooks/{id}", func(w http.ResponseWriter, r *http.Request) {
- h, ok := p.webhooks[r.PathValue("id")]
- if !ok {
- writeError(w, pluginapi.Errorf(pluginapi.CodeNotFound, "no webhook %q", r.PathValue("id")))
- return
- }
- p.unary(func(ctx context.Context, call *Call, raw json.RawMessage) (any, error) {
- in, err := decodeInput[pluginapi.WebhookRequest](raw)
- if err != nil {
- return nil, err
- }
- out, err := h(ctx, call, in)
- if err != nil {
- return nil, err
- }
- if out == nil {
- out = &pluginapi.WebhookResponse{}
- }
- if out.Status == 0 {
- out.Status = http.StatusOK
- }
- return out, nil
- })(w, r)
- })
-}
diff --git a/pluginsdk/go.mod b/pluginsdk/go.mod
deleted file mode 100644
index bb24df1ba..000000000
--- a/pluginsdk/go.mod
+++ /dev/null
@@ -1,3 +0,0 @@
-module github.com/Tencent/WeKnora/pluginsdk
-
-go 1.24
diff --git a/pluginsdk/host.go b/pluginsdk/host.go
deleted file mode 100644
index d80d57154..000000000
--- a/pluginsdk/host.go
+++ /dev/null
@@ -1,114 +0,0 @@
-package pluginsdk
-
-import (
- "bytes"
- "context"
- "encoding/json"
- "io"
- "net/http"
- "net/url"
- "strconv"
- "strings"
- "time"
-
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// Host calls back into WeKnora on behalf of one call: the tenant and scopes
-// are those the call's token carries. It is only valid while the token lives
-// (a few minutes), so do not keep it beyond the call.
-type Host struct {
- base string
- token string
- http *http.Client
-}
-
-// Host returns the Host API client of this call, or nil when the plugin was
-// granted no Host API scopes.
-func (c *Call) Host() *Host {
- if c.Context.Host == nil || c.Context.Host.URL == "" || c.Context.Host.Token == "" {
- return nil
- }
- return &Host{
- base: strings.TrimSuffix(c.Context.Host.URL, "/"), token: c.Context.Host.Token,
- http: &http.Client{Timeout: 30 * time.Second},
- }
-}
-
-func (h *Host) do(ctx context.Context, method, path string, query url.Values, body, out any) error {
- var rd io.Reader
- if body != nil {
- b, err := json.Marshal(body)
- if err != nil {
- return err
- }
- rd = bytes.NewReader(b)
- }
- u := h.base + path
- if len(query) > 0 {
- u += "?" + query.Encode()
- }
- req, err := http.NewRequestWithContext(ctx, method, u, rd)
- if err != nil {
- return err
- }
- req.Header.Set("Authorization", "Bearer "+h.token)
- if body != nil {
- req.Header.Set("Content-Type", "application/json")
- }
- resp, err := h.http.Do(req)
- if err != nil {
- return pluginapi.Errorf(pluginapi.CodeUnavailable, "host api: %v", err)
- }
- defer func() { _ = resp.Body.Close() }()
- if resp.StatusCode/100 != 2 {
- var eb pluginapi.ErrorBody
- b, _ := io.ReadAll(io.LimitReader(resp.Body, 64<<10))
- if json.Unmarshal(b, &eb) == nil && eb.Error.Code != "" {
- return &eb.Error
- }
- return pluginapi.Errorf(pluginapi.CodeInternal, "host api answered HTTP %d", resp.StatusCode)
- }
- if out == nil || resp.StatusCode == http.StatusNoContent {
- return nil
- }
- return json.NewDecoder(resp.Body).Decode(out)
-}
-
-// KVGet reads key into v. ok is false when the key does not exist.
-func (h *Host) KVGet(ctx context.Context, key string, v any) (ok bool, err error) {
- var e pluginapi.KVEntry
- err = h.do(ctx, http.MethodGet, pluginapi.HostKVPath, url.Values{"key": {key}}, nil, &e)
- if pe, isPE := pluginapi.AsError(err); isPE && pe.Code == pluginapi.CodeNotFound {
- return false, nil
- }
- if err != nil {
- return false, err
- }
- return true, json.Unmarshal(e.Value, v)
-}
-
-// KVPut stores v (as JSON) under key; ttl 0 keeps it until deleted.
-func (h *Host) KVPut(ctx context.Context, key string, v any, ttl time.Duration) error {
- raw, err := json.Marshal(v)
- if err != nil {
- return err
- }
- in := pluginapi.KVPut{Key: key, Value: raw, TTLSeconds: int(ttl / time.Second)}
- return h.do(ctx, http.MethodPut, pluginapi.HostKVPath, nil, in, nil)
-}
-
-// KVDelete removes key; a missing key is not an error.
-func (h *Host) KVDelete(ctx context.Context, key string) error {
- return h.do(ctx, http.MethodDelete, pluginapi.HostKVPath, url.Values{"key": {key}}, nil, nil)
-}
-
-// KVList returns a page of keys with prefix after the given key.
-func (h *Host) KVList(ctx context.Context, prefix, after string, limit int) (*pluginapi.KVList, error) {
- q := url.Values{"prefix": {prefix}, "after": {after}}
- if limit > 0 {
- q.Set("limit", strconv.Itoa(limit))
- }
- var out pluginapi.KVList
- return &out, h.do(ctx, http.MethodGet, pluginapi.HostKVListPath, q, nil, &out)
-}
diff --git a/pluginsdk/mcp.go b/pluginsdk/mcp.go
deleted file mode 100644
index 1ef7c3395..000000000
--- a/pluginsdk/mcp.go
+++ /dev/null
@@ -1,136 +0,0 @@
-package pluginsdk
-
-import (
- "context"
- "encoding/json"
- "errors"
- "net/http"
- "sort"
-
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// ToolHandler runs one tool call. args are the arguments as the model sent
-// them; decode them into the shape of the tool's input schema. Return
-// pluginapi.ToolError (or any error) for failures the model should see.
-type ToolHandler func(ctx context.Context, call *Call, args json.RawMessage) (*pluginapi.ToolResult, error)
-
-type mcpServer struct {
- tools []pluginapi.Tool
- handlers map[string]ToolHandler
-}
-
-// Tool adds a tool to the MCP server the plugin serves as server
-// (contributes.mcpServers[].id, declared without a url). Agents in the
-// workspaces that switched the plugin on can call it like any MCP tool,
-// with the same approval settings; call carries the workspace's
-// configuration.
-func (p *Plugin) Tool(server string, tool pluginapi.Tool, h ToolHandler) {
- s := p.mcp[server]
- if s == nil {
- s = &mcpServer{handlers: map[string]ToolHandler{}}
- p.mcp[server] = s
- }
- if len(tool.InputSchema) == 0 {
- tool.InputSchema = json.RawMessage(`{"type":"object"}`)
- }
- if _, dup := s.handlers[tool.Name]; !dup {
- s.tools = append(s.tools, tool)
- }
- s.handlers[tool.Name] = h
-}
-
-func (p *Plugin) routeMCP(mux *http.ServeMux) {
- mux.HandleFunc("POST /v1/mcp/{id}", func(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- s, ok := p.mcp[id]
- if !ok {
- writeError(w, pluginapi.Errorf(pluginapi.CodeNotFound, "no MCP server %q", id))
- return
- }
- p.unary(func(ctx context.Context, call *Call, raw json.RawMessage) (any, error) {
- req, err := decodeInput[pluginapi.MCPRequest](raw)
- if err != nil {
- return nil, err
- }
- return p.answerMCP(ctx, call, id, s, req), nil
- })(w, r)
- })
-}
-
-func (p *Plugin) answerMCP(
- ctx context.Context, call *Call, id string, s *mcpServer, req pluginapi.MCPRequest,
-) pluginapi.MCPResponse {
- resp := pluginapi.MCPResponse{JSONRPC: "2.0", ID: req.ID}
- result := func(v any) pluginapi.MCPResponse {
- b, err := json.Marshal(v)
- if err != nil {
- resp.Error = &pluginapi.MCPError{Code: -32603, Message: err.Error()}
- return resp
- }
- resp.Result = b
- return resp
- }
- fail := func(code int, msg string) pluginapi.MCPResponse {
- resp.Error = &pluginapi.MCPError{Code: code, Message: msg}
- return resp
- }
- switch req.Method {
- case "initialize":
- return result(map[string]any{
- "protocolVersion": pluginapi.MCPProtocolVersion,
- "capabilities": map[string]any{"tools": map[string]any{}},
- "serverInfo": map[string]any{"name": p.info.ID + "/" + id, "version": p.info.Version},
- })
- case "ping":
- return result(struct{}{})
- // Tools only: clients that list resources or prompts anyway get none.
- case "resources/list":
- return result(map[string]any{"resources": []any{}})
- case "resources/templates/list":
- return result(map[string]any{"resourceTemplates": []any{}})
- case "prompts/list":
- return result(map[string]any{"prompts": []any{}})
- case "tools/list":
- tools := append([]pluginapi.Tool(nil), s.tools...)
- sort.Slice(tools, func(i, j int) bool { return tools[i].Name < tools[j].Name })
- return result(map[string]any{"tools": tools})
- case "tools/call":
- var params pluginapi.ToolCallParams
- if err := json.Unmarshal(req.Params, &params); err != nil {
- return fail(pluginapi.MCPInvalidParams, "params: "+err.Error())
- }
- h, ok := s.handlers[params.Name]
- if !ok || h == nil {
- return fail(pluginapi.MCPInvalidParams, "no tool "+params.Name)
- }
- args := params.Arguments
- if len(args) == 0 || string(args) == "null" {
- args = json.RawMessage(`{}`)
- }
- out, err := h(ctx, call, args)
- if err != nil {
- msg := err.Error()
- if pe, ok := pluginapi.AsError(err); ok {
- msg = pe.Message
- }
- if errors.Is(err, context.Canceled) {
- msg = "the call was cancelled"
- }
- out = pluginapi.ToolError("%s", msg)
- }
- if out == nil {
- out = pluginapi.TextResult("")
- }
- if out.Content == nil {
- out.Content = []pluginapi.ToolContent{}
- }
- return result(out)
- default:
- if len(req.ID) == 0 {
- // Notifications need no answer; the server keeps no state.
- return resp
- }
- return fail(pluginapi.MCPMethodNotFound, "method "+req.Method+" is not supported")
- }
-}
diff --git a/pluginsdk/openapi_test.go b/pluginsdk/openapi_test.go
deleted file mode 100644
index 78398dfa9..000000000
--- a/pluginsdk/openapi_test.go
+++ /dev/null
@@ -1,50 +0,0 @@
-package pluginsdk
-
-import (
- "os"
- "regexp"
- "sort"
- "strings"
- "testing"
-)
-
-// The OpenAPI description and the SDK's routes must list the same endpoints,
-// so a protocol change cannot land in one and not the other.
-func TestOpenAPIMatchesRoutes(t *testing.T) {
- raw, err := os.ReadFile("pluginapi/openapi.yaml")
- if err != nil {
- t.Fatal(err)
- }
- // Windows checkouts may carry CRLF line endings.
- spec := strings.ReplaceAll(string(raw), "\r\n", "\n")
- var documented []string
- for _, m := range regexp.MustCompile(`(?m)^ (/v1/[^:]+):$`).FindAllStringSubmatch(spec, -1) {
- documented = append(documented, m[1])
- }
- src, err := os.ReadFile("plugin.go")
- if err != nil {
- t.Fatal(err)
- }
- files := []string{"websearch.go", "connector.go", "parser.go", "ui.go", "events.go", "options.go", "mcp.go"}
- for _, f := range files {
- b, err := os.ReadFile(f)
- if err != nil {
- t.Fatal(err)
- }
- src = append(src, b...)
- }
- var routed []string
- route := regexp.MustCompile(`HandleFunc\(\s*"(?:GET|POST) (/v1/[^"]+)"`)
- for _, m := range route.FindAllStringSubmatch(string(src), -1) {
- routed = append(routed, m[1])
- }
- sort.Strings(documented)
- sort.Strings(routed)
- if strings.Join(documented, "\n") != strings.Join(routed, "\n") {
- t.Fatalf(
- "openapi.yaml documents:\n%s\n\nthe SDK routes:\n%s",
- strings.Join(documented, "\n"),
- strings.Join(routed, "\n"),
- )
- }
-}
diff --git a/pluginsdk/options.go b/pluginsdk/options.go
deleted file mode 100644
index b5e6b526a..000000000
--- a/pluginsdk/options.go
+++ /dev/null
@@ -1,42 +0,0 @@
-package pluginsdk
-
-import (
- "context"
- "encoding/json"
- "net/http"
-
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// OptionsHandler lists a field's choices. call.Config holds what the form
-// holds now; decode it with call.DecodeInstance for instance forms.
-type OptionsHandler func(ctx context.Context, call *Call, in pluginapi.OptionsInput) ([]pluginapi.Option, error)
-
-// Options registers the choices behind x-options {name: name}. Return
-// pluginapi.InvalidConfig when the form lacks what the list needs (a token
-// not entered yet): the form shows the message by the field.
-func (p *Plugin) Options(name string, h OptionsHandler) { p.options[name] = h }
-
-func (p *Plugin) routeOptions(mux *http.ServeMux) {
- mux.HandleFunc("POST /v1/options/{name}", func(w http.ResponseWriter, r *http.Request) {
- h, ok := p.options[r.PathValue("name")]
- if !ok {
- writeError(w, pluginapi.Errorf(pluginapi.CodeNotFound, "no options %q", r.PathValue("name")))
- return
- }
- p.unary(func(ctx context.Context, call *Call, raw json.RawMessage) (any, error) {
- in, err := decodeInput[pluginapi.OptionsInput](raw)
- if err != nil {
- return nil, err
- }
- opts, err := h(ctx, call, in)
- if err != nil {
- return nil, err
- }
- if opts == nil {
- opts = []pluginapi.Option{}
- }
- return pluginapi.OptionsOutput{Options: opts}, nil
- })(w, r)
- })
-}
diff --git a/pluginsdk/parser.go b/pluginsdk/parser.go
deleted file mode 100644
index 563d2cbbd..000000000
--- a/pluginsdk/parser.go
+++ /dev/null
@@ -1,55 +0,0 @@
-package pluginsdk
-
-import (
- "context"
- "encoding/json"
- "net/http"
-
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// Parser turns a document into Markdown. Return a non-retryable error (such
-// as pluginapi.CodeInvalidConfig or CodeNotFound) for a document that will
-// never parse; retryable ones (CodeUnavailable, CodeRateLimited) make
-// WeKnora try again later.
-type Parser interface {
- Parse(ctx context.Context, call *Call, in pluginapi.ParseInput) (*pluginapi.ParseOutput, error)
-}
-
-// ParserFunc adapts a function to Parser.
-type ParserFunc func(ctx context.Context, call *Call, in pluginapi.ParseInput) (*pluginapi.ParseOutput, error)
-
-// Parse implements Parser.
-func (f ParserFunc) Parse(ctx context.Context, call *Call, in pluginapi.ParseInput) (*pluginapi.ParseOutput, error) {
- return f(ctx, call, in)
-}
-
-// Parser registers parser id (contributes.parsers[].id).
-func (p *Plugin) Parser(id string, parser Parser) { p.parsers[id] = parser }
-
-func (p *Plugin) routeParsers(mux *http.ServeMux) {
- mux.HandleFunc("POST /v1/parsers/{id}/parse", func(w http.ResponseWriter, r *http.Request) {
- parser, ok := p.parsers[r.PathValue("id")]
- if !ok {
- writeError(w, pluginapi.Errorf(pluginapi.CodeNotFound, "no parser %q", r.PathValue("id")))
- return
- }
- p.unary(func(ctx context.Context, call *Call, raw json.RawMessage) (any, error) {
- in, err := decodeInput[pluginapi.ParseInput](raw)
- if err != nil {
- return nil, err
- }
- if len(in.Content) == 0 && in.URL == "" {
- return nil, pluginapi.Errorf(pluginapi.CodeBadRequest, "content or url is required")
- }
- out, err := parser.Parse(ctx, call, in)
- if err != nil {
- return nil, err
- }
- if out == nil {
- out = &pluginapi.ParseOutput{}
- }
- return out, nil
- })(w, r)
- })
-}
diff --git a/pluginsdk/plugin.go b/pluginsdk/plugin.go
deleted file mode 100644
index e30c7be52..000000000
--- a/pluginsdk/plugin.go
+++ /dev/null
@@ -1,374 +0,0 @@
-// Package pluginsdk builds WeKnora code plugins in Go. A plugin registers what
-// it contributes and calls Serve; the SDK speaks the extension protocol
-// (package pluginapi): the handshake with the plugin host, authentication,
-// routing, streaming and error encoding.
-//
-// func main() {
-// p := pluginsdk.New(pluginsdk.Info{ID: "acme.rss", Version: "1.0.0"})
-// p.Connector("rss", &rssConnector{})
-// if err := p.Serve(); err != nil {
-// log.Fatal(err)
-// }
-// }
-//
-// Started by the WeKnora plugin host, the plugin listens where the host says
-// and accepts only the host's token. Run on its own (a remote plugin), it
-// listens on WEKNORA_PLUGIN_ADDR and verifies request signatures with
-// WEKNORA_PLUGIN_SECRET.
-package pluginsdk
-
-import (
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "log/slog"
- "net"
- "net/http"
- "os"
- "os/signal"
- "sort"
- "strings"
- "syscall"
- "time"
-
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// Info identifies the plugin; it must match plugin.yaml.
-type Info struct {
- ID string
- Version string
-}
-
-// Call is one request's context and configuration. Configuration secrets are
-// already decrypted; do not keep them beyond the call.
-type Call struct {
- pluginapi.Context
- Config pluginapi.Config
-}
-
-// DecodeInstance decodes the instance configuration into v.
-func (c *Call) DecodeInstance(v any) error {
- return remarshal(c.Config.Instance, v)
-}
-
-// ConfigValidator checks the plugin's system and tenant configuration.
-type ConfigValidator func(ctx context.Context, call *Call) error
-
-// Plugin is a plugin under construction; register contributions, then Serve.
-type Plugin struct {
- info Info
- webSearch map[string]WebSearcher
- connectors map[string]Connector
- parsers map[string]Parser
- ui UIHandler
- events EventHandler
- webhooks map[string]WebhookHandler
- options map[string]OptionsHandler
- mcp map[string]*mcpServer
- validate ConfigValidator
- logger *slog.Logger
- // ShutdownTimeout bounds how long Serve waits for calls in flight after
- // a termination signal.
- ShutdownTimeout time.Duration
-}
-
-// New starts a plugin.
-func New(info Info) *Plugin {
- return &Plugin{
- info: info,
- webSearch: map[string]WebSearcher{},
- connectors: map[string]Connector{},
- parsers: map[string]Parser{},
- webhooks: map[string]WebhookHandler{},
- options: map[string]OptionsHandler{},
- mcp: map[string]*mcpServer{},
- logger: slog.New(slog.NewTextHandler(os.Stderr, nil)),
- ShutdownTimeout: 60 * time.Second,
- }
-}
-
-// Logger writes to stderr, which the plugin host forwards to WeKnora's logs.
-func (p *Plugin) Logger() *slog.Logger { return p.logger }
-
-// ValidateConfig registers the check behind POST /v1/config/validate.
-func (p *Plugin) ValidateConfig(fn ConfigValidator) { p.validate = fn }
-
-// Manifest is what the plugin reports at GET /v1/manifest.
-func (p *Plugin) Manifest() pluginapi.Manifest {
- m := pluginapi.Manifest{
- ID: p.info.ID, Version: p.info.Version, APIVersion: pluginapi.APIVersion,
- Contributes: map[string][]string{},
- }
- add := func(point string, ids []string) {
- if len(ids) > 0 {
- sort.Strings(ids)
- m.Contributes[point] = ids
- }
- }
- add("webSearch", keys(p.webSearch))
- add("connectors", keys(p.connectors))
- add("parsers", keys(p.parsers))
- add("webhooks", keys(p.webhooks))
- add("options", keys(p.options))
- add("mcpServers", keys(p.mcp))
- if p.events != nil {
- m.Contributes["events"] = []string{"handler"}
- }
- if p.ui != nil {
- m.Contributes["ui"] = []string{"request"}
- }
- return m
-}
-
-func keys[V any](m map[string]V) []string {
- out := make([]string, 0, len(m))
- for k := range m {
- out = append(out, k)
- }
- return out
-}
-
-// Handler serves the protocol. Serve wraps it with authentication; tests
-// and custom servers can use it directly.
-func (p *Plugin) Handler() http.Handler {
- mux := http.NewServeMux()
- mux.HandleFunc("GET /v1/manifest", func(w http.ResponseWriter, _ *http.Request) {
- writeJSON(w, http.StatusOK, p.Manifest())
- })
- mux.HandleFunc("GET /v1/health", func(w http.ResponseWriter, _ *http.Request) {
- writeJSON(w, http.StatusOK, pluginapi.Health{Status: "ok"})
- })
- mux.HandleFunc(
- "POST /v1/config/validate",
- p.unary(func(ctx context.Context, call *Call, _ json.RawMessage) (any, error) {
- if p.validate != nil {
- if err := p.validate(ctx, call); err != nil {
- return nil, err
- }
- }
- return struct{}{}, nil
- }),
- )
- p.routeWebSearch(mux)
- p.routeConnectors(mux)
- p.routeParsers(mux)
- p.routeUI(mux)
- p.routeEvents(mux)
- p.routeOptions(mux)
- p.routeMCP(mux)
- mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- writeError(w, pluginapi.Errorf(pluginapi.CodeNotFound, "no endpoint %s %s", r.Method, r.URL.Path))
- })
- return recoverer(p.logger, mux)
-}
-
-// unaryFunc answers one call with one output.
-type unaryFunc func(ctx context.Context, call *Call, input json.RawMessage) (any, error)
-
-func (p *Plugin) unary(fn unaryFunc) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- env, err := readEnvelope(r)
- if err != nil {
- writeError(w, err)
- return
- }
- ctx, cancel := callContext(r.Context(), env)
- defer cancel()
- out, err := fn(ctx, &Call{Context: env.Context, Config: env.Config}, env.Input)
- if err != nil {
- writeError(w, err)
- return
- }
- raw, err := json.Marshal(out)
- if err != nil {
- writeError(w, err)
- return
- }
- writeJSON(w, http.StatusOK, pluginapi.Output{Output: raw})
- }
-}
-
-func readEnvelope(r *http.Request) (*pluginapi.Envelope, error) {
- var env pluginapi.Envelope
- if err := json.NewDecoder(r.Body).Decode(&env); err != nil {
- return nil, pluginapi.Errorf(pluginapi.CodeBadRequest, "decode envelope: %v", err)
- }
- return &env, nil
-}
-
-// callContext applies the envelope deadline.
-func callContext(parent context.Context, env *pluginapi.Envelope) (context.Context, context.CancelFunc) {
- if env.Context.Deadline != nil {
- return context.WithDeadline(parent, *env.Context.Deadline)
- }
- return context.WithCancel(parent)
-}
-
-func decodeInput[T any](raw json.RawMessage) (T, error) {
- var v T
- if len(raw) == 0 {
- return v, nil
- }
- if err := json.Unmarshal(raw, &v); err != nil {
- return v, pluginapi.Errorf(pluginapi.CodeBadRequest, "decode input: %v", err)
- }
- return v, nil
-}
-
-func remarshal(from, to any) error {
- b, err := json.Marshal(from)
- if err != nil {
- return err
- }
- return json.Unmarshal(b, to)
-}
-
-func writeJSON(w http.ResponseWriter, status int, v any) {
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(status)
- _ = json.NewEncoder(w).Encode(v)
-}
-
-// toError maps any error to a protocol error.
-func toError(err error) *pluginapi.Error {
- if e, ok := pluginapi.AsError(err); ok {
- return e
- }
- if errors.Is(err, context.DeadlineExceeded) {
- return pluginapi.Errorf(pluginapi.CodeUnavailable, "deadline exceeded")
- }
- return &pluginapi.Error{Code: pluginapi.CodeInternal, Message: err.Error()}
-}
-
-func writeError(w http.ResponseWriter, err error) {
- e := toError(err)
- writeJSON(w, e.Code.HTTPStatus(), pluginapi.ErrorBody{Error: *e})
-}
-
-func recoverer(logger *slog.Logger, next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- defer func() {
- if v := recover(); v != nil {
- logger.Error("plugin panicked", "path", r.URL.Path, "panic", v)
- writeError(w, fmt.Errorf("plugin panicked: %v", v))
- }
- }()
- next.ServeHTTP(w, r)
- })
-}
-
-// Serve runs the plugin until SIGTERM or SIGINT, then drains calls in
-// flight. Started by the plugin host it listens where the host says and
-// prints the handshake; otherwise it serves as a remote plugin.
-func (p *Plugin) Serve() error {
- ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
- defer stop()
- return p.ServeContext(ctx)
-}
-
-// ServeContext is Serve with a caller-controlled lifetime.
-func (p *Plugin) ServeContext(ctx context.Context) error {
- ln, auth, handshake, err := p.listen()
- if err != nil {
- return err
- }
- srv := &http.Server{Handler: auth(p.Handler()), ReadHeaderTimeout: 10 * time.Second}
- errc := make(chan error, 1)
- go func() { errc <- srv.Serve(ln) }()
- if handshake != nil {
- // The host waits for this exact line; everything else on stdout is
- // treated as log output.
- _, _ = fmt.Fprintln(os.Stdout, handshake.String())
- } else {
- p.logger.Info("plugin serving", "addr", ln.Addr().String())
- }
- select {
- case err := <-errc:
- return err
- case <-ctx.Done():
- }
- shutdown, cancel := context.WithTimeout(context.Background(), p.ShutdownTimeout)
- defer cancel()
- return srv.Shutdown(shutdown)
-}
-
-type middleware func(http.Handler) http.Handler
-
-// listen picks host mode (socket + token from the environment) or remote
-// mode (address + signing secret).
-func (p *Plugin) listen() (net.Listener, middleware, *pluginapi.Handshake, error) {
- if addr := os.Getenv(pluginapi.EnvSocket); addr != "" {
- network := os.Getenv(pluginapi.EnvNetwork)
- if network == "" {
- network = "unix"
- }
- token := os.Getenv(pluginapi.EnvToken)
- if token == "" {
- return nil, nil, nil, fmt.Errorf("%s is set without %s", pluginapi.EnvSocket, pluginapi.EnvToken)
- }
- if network == "unix" {
- _ = os.Remove(addr)
- }
- ln, err := net.Listen(network, addr)
- if err != nil {
- return nil, nil, nil, fmt.Errorf("listen %s %s: %w", network, addr, err)
- }
- hs := &pluginapi.Handshake{Protocol: pluginapi.ProtocolVersion, Network: network, Address: ln.Addr().String()}
- return ln, bearerAuth(token), hs, nil
- }
- addr := os.Getenv(pluginapi.EnvAddr)
- if addr == "" {
- addr = ":8080"
- }
- secret := os.Getenv(pluginapi.EnvSecret)
- if secret == "" {
- return nil, nil, nil, fmt.Errorf(
- "a remote plugin needs %s (the secret shown when it was registered)",
- pluginapi.EnvSecret,
- )
- }
- ln, err := net.Listen("tcp", addr)
- if err != nil {
- return nil, nil, nil, err
- }
- return ln, signatureAuth([]byte(secret)), nil, nil
-}
-
-func bearerAuth(token string) middleware {
- want := "Bearer " + token
- return func(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if r.Header.Get("Authorization") != want {
- writeError(w, pluginapi.Errorf(pluginapi.CodeUnauthorized, "missing or wrong host token"))
- return
- }
- next.ServeHTTP(w, r)
- })
- }
-}
-
-// maxBody bounds a signed request body read into memory for verification.
-const maxBody = 32 << 20
-
-func signatureAuth(secret []byte) middleware {
- return func(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- body, err := io.ReadAll(io.LimitReader(r.Body, maxBody+1))
- if err != nil || len(body) > maxBody {
- writeError(w, pluginapi.Errorf(pluginapi.CodeBadRequest, "request body unreadable or too large"))
- return
- }
- err = pluginapi.VerifySignature(secret, r.Header.Get(pluginapi.TimestampHeader),
- r.Header.Get(pluginapi.SignatureHeader), body, time.Now())
- if err != nil {
- writeError(w, pluginapi.Errorf(pluginapi.CodeUnauthorized, "%v", err))
- return
- }
- r.Body = io.NopCloser(strings.NewReader(string(body)))
- next.ServeHTTP(w, r)
- })
- }
-}
diff --git a/pluginsdk/plugin_test.go b/pluginsdk/plugin_test.go
deleted file mode 100644
index 1c56b5b51..000000000
--- a/pluginsdk/plugin_test.go
+++ /dev/null
@@ -1,449 +0,0 @@
-package pluginsdk
-
-import (
- "bufio"
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "net/http/httptest"
- "os"
- "path/filepath"
- "strings"
- "testing"
- "time"
-
- "github.com/Tencent/WeKnora/pluginsdk/client"
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-type feedConnector struct{ items int }
-
-func (f *feedConnector) Validate(_ context.Context, _ *Call, cfg ConnectorConfig) error {
- if cfg.Settings["url"] == nil {
- return pluginapi.InvalidConfig("feed URL is required", map[string]string{"settings.url": "required"})
- }
- return nil
-}
-
-func (f *feedConnector) ListResources(
- _ context.Context,
- _ *Call,
- cfg ConnectorConfig,
- parentID string,
-) ([]pluginapi.Resource, error) {
- if parentID != "" {
- return nil, nil
- }
- return []pluginapi.Resource{{ExternalID: fmt.Sprint(cfg.Settings["url"]), Name: "Feed"}}, nil
-}
-
-func (f *feedConnector) Fetch(
- _ context.Context, _ *Call, _ ConnectorConfig, in pluginapi.FetchInput, s *Stream,
-) (*pluginapi.Cursor, error) {
- start := 0
- if in.Cursor != nil {
- start = int(in.Cursor.State["next"].(float64))
- }
- for i := start; i < f.items; i++ {
- item := pluginapi.FetchedItem{ExternalID: fmt.Sprint(i), Title: fmt.Sprint("item ", i), Content: []byte("body")}
- if err := s.Item(item); err != nil {
- return nil, err
- }
- if err := s.Checkpoint(pluginapi.Cursor{State: map[string]any{"next": i + 1}}); err != nil {
- return nil, err
- }
- if i == 2 && in.Mode == "fail-midway" {
- return nil, pluginapi.Errorf(pluginapi.CodeUnavailable, "upstream went away")
- }
- }
- return &pluginapi.Cursor{State: map[string]any{"next": f.items}}, nil
-}
-
-func testPlugin(t *testing.T) (*Plugin, *client.Client) {
- t.Helper()
- p := New(Info{ID: "acme.feed", Version: "1.0.0"})
- p.Connector("feed", &feedConnector{items: 5})
- p.WebSearch(
- "echo",
- WebSearchFunc(func(ctx context.Context, call *Call, in pluginapi.SearchInput) (*pluginapi.SearchOutput, error) {
- switch in.Query {
- case "panic":
- panic("boom")
- case "deadline":
- if ctx.Err() != nil {
- return nil, ctx.Err()
- }
- }
- return &pluginapi.SearchOutput{Results: []pluginapi.SearchResult{{
- Title: in.Query,
- URL: fmt.Sprint("https://example.com/?tenant=", call.TenantID,
- "&key=", call.Config.Instance["api_key"]),
- }}}, nil
- }),
- )
- srv := httptest.NewServer(p.Handler())
- t.Cleanup(srv.Close)
- return p, client.New(srv.URL, nil, nil)
-}
-
-func TestUnaryCallsAndErrors(t *testing.T) {
- ctx := context.Background()
- _, c := testPlugin(t)
-
- m, err := c.Manifest(ctx)
- if err != nil || m.ID != "acme.feed" || m.Contributes["connectors"][0] != "feed" ||
- m.APIVersion != pluginapi.APIVersion {
- t.Fatalf("manifest = %+v, %v", m, err)
- }
- if err := c.Health(ctx); err != nil {
- t.Fatal(err)
- }
-
- env := pluginapi.Envelope{
- Context: pluginapi.Context{TenantID: 7},
- Config: pluginapi.Config{Instance: map[string]any{"api_key": "k1"}},
- }
- var out pluginapi.SearchOutput
- if err := c.Call(ctx, pluginapi.SearchPath("echo"), env, pluginapi.SearchInput{Query: "hi"}, &out); err != nil {
- t.Fatal(err)
- }
- if len(out.Results) != 1 || out.Results[0].URL != "https://example.com/?tenant=7&key=k1" {
- t.Fatalf("search = %+v", out)
- }
-
- err = c.Call(ctx, pluginapi.ConnectorValidatePath("feed"), pluginapi.Envelope{}, nil, nil)
- if e, ok := pluginapi.AsError(err); !ok || e.Code != pluginapi.CodeInvalidConfig ||
- e.Details.Fields["settings.url"] != "required" {
- t.Fatalf("validate = %v", err)
- }
- err = c.Call(ctx, pluginapi.SearchPath("nope"), env, pluginapi.SearchInput{}, nil)
- if e, ok := pluginapi.AsError(err); !ok || e.Code != pluginapi.CodeNotFound {
- t.Fatalf("unknown provider = %v", err)
- }
- err = c.Call(ctx, pluginapi.SearchPath("echo"), env, pluginapi.SearchInput{Query: "panic"}, nil)
- if e, ok := pluginapi.AsError(err); !ok || e.Code != pluginapi.CodeInternal ||
- !strings.Contains(e.Message, "boom") {
- t.Fatalf("panic = %v", err)
- }
- past := time.Now().Add(-time.Second)
- env.Context.Deadline = &past
- err = c.Call(ctx, pluginapi.SearchPath("echo"), env, pluginapi.SearchInput{Query: "deadline"}, nil)
- if e, ok := pluginapi.AsError(err); !ok || e.Code != pluginapi.CodeUnavailable {
- t.Fatalf("the envelope deadline must reach the handler's context, got %v", err)
- }
-}
-
-func TestStreamingFetch(t *testing.T) {
- ctx := context.Background()
- _, c := testPlugin(t)
- env := pluginapi.Envelope{
- Config: pluginapi.Config{Instance: map[string]any{"settings": map[string]any{"url": "u"}}},
- }
-
- var items []string
- var last *pluginapi.Cursor
- end, err := c.Stream(ctx, pluginapi.ConnectorFetchPath("feed"), env,
- pluginapi.FetchInput{Mode: pluginapi.FetchFull, Cursor: &pluginapi.Cursor{State: map[string]any{"next": 3}}},
- func(ev pluginapi.Event) error {
- switch ev.Type {
- case pluginapi.EventItem:
- var it pluginapi.FetchedItem
- if err := remarshal(ev.Data, &it); err != nil {
- return err
- }
- items = append(items, it.ExternalID+":"+string(it.Content))
- case pluginapi.EventCheckpoint:
- last = &pluginapi.Cursor{}
- return remarshal(ev.Data, last)
- }
- return nil
- })
- if err != nil {
- t.Fatal(err)
- }
- if strings.Join(items, ",") != "3:body,4:body" || last.State["next"].(float64) != 5 ||
- !strings.Contains(string(end), `"next":5`) {
- t.Fatalf("items=%v last=%+v end=%s", items, last, end)
- }
-
- _, err = c.Stream(ctx, pluginapi.ConnectorFetchPath("feed"), env, pluginapi.FetchInput{Mode: "fail-midway"},
- func(pluginapi.Event) error { return nil })
- if e, ok := pluginapi.AsError(err); !ok || e.Code != pluginapi.CodeUnavailable || !e.Retryable {
- t.Fatalf("mid-stream failure = %v", err)
- }
-
- stop := errors.New("ingest failed")
- _, err = c.Stream(ctx, pluginapi.ConnectorFetchPath("feed"), env, pluginapi.FetchInput{},
- func(pluginapi.Event) error { return stop })
- if !errors.Is(err, stop) {
- t.Fatalf("a handler error must abort the stream, got %v", err)
- }
-}
-
-func TestAuthModes(t *testing.T) {
- ctx := context.Background()
- p, _ := testPlugin(t)
-
- bearer := httptest.NewServer(bearerAuth("tok")(p.Handler()))
- defer bearer.Close()
- if err := client.New(bearer.URL, nil, client.Bearer("wrong")).Health(ctx); !isCode(
- err,
- pluginapi.CodeUnauthorized,
- ) {
- t.Fatalf("wrong token = %v", err)
- }
- if err := client.New(bearer.URL, nil, client.Bearer("tok")).Health(ctx); err != nil {
- t.Fatal(err)
- }
-
- signed := httptest.NewServer(signatureAuth([]byte("s3cret"))(p.Handler()))
- defer signed.Close()
- if err := client.New(signed.URL, nil, client.Signed("other")).Health(ctx); !isCode(
- err,
- pluginapi.CodeUnauthorized,
- ) {
- t.Fatalf("wrong secret = %v", err)
- }
- var out pluginapi.SearchOutput
- err := client.New(signed.URL, nil, client.Signed("s3cret")).
- Call(ctx, pluginapi.SearchPath("echo"), pluginapi.Envelope{}, pluginapi.SearchInput{Query: "q"}, &out)
- if err != nil || len(out.Results) != 1 {
- t.Fatalf("signed call = %+v, %v", out, err)
- }
-}
-
-func isCode(err error, code pluginapi.ErrorCode) bool {
- e, ok := pluginapi.AsError(err)
- return ok && e.Code == code
-}
-
-// ServeContext in host mode listens on the socket from the environment,
-// prints the handshake and requires the token.
-func TestServeHostHandshake(t *testing.T) {
- dir, err := os.MkdirTemp("", "wkp")
- if err != nil {
- t.Fatal(err)
- }
- defer func() { _ = os.RemoveAll(dir) }()
- sock := filepath.Join(dir, "p.sock")
- t.Setenv(pluginapi.EnvSocket, sock)
- t.Setenv(pluginapi.EnvToken, "tok")
-
- r, w, _ := os.Pipe()
- stdout := os.Stdout
- os.Stdout = w
- defer func() { os.Stdout = stdout }()
-
- p, _ := testPlugin(t)
- ctx, cancel := context.WithCancel(context.Background())
- done := make(chan error, 1)
- go func() { done <- p.ServeContext(ctx) }()
-
- line, err := bufio.NewReader(r).ReadString('\n')
- if err != nil {
- t.Fatal(err)
- }
- os.Stdout = stdout
- hs, ok, err := pluginapi.ParseHandshake(line)
- if !ok || err != nil || hs.Network != "unix" || hs.Address != sock {
- t.Fatalf("handshake %q = %+v %v %v", line, hs, ok, err)
- }
- c := client.ForHandshake(hs, client.Bearer("tok"))
- if err := c.Health(context.Background()); err != nil {
- t.Fatal(err)
- }
- cancel()
- if err := <-done; err != nil {
- t.Fatalf("shutdown: %v", err)
- }
-}
-
-func TestUIRequests(t *testing.T) {
- ctx := context.Background()
- p := New(Info{ID: "acme.ui", Version: "1.0.0"})
- srv := httptest.NewServer(p.Handler())
- defer srv.Close()
- c := client.New(srv.URL, nil, nil)
- req := pluginapi.UIRequest{Mount: "pages/links", Method: "GET", Path: "/links", Role: "admin"}
- err := c.Call(ctx, pluginapi.UIRequestPath, pluginapi.Envelope{}, req, nil)
- if !isCode(err, pluginapi.CodeNotFound) {
- t.Fatalf("no handler = %v", err)
- }
-
- p.UI(func(_ context.Context, call *Call, in pluginapi.UIRequest) (*pluginapi.UIResponse, error) {
- if in.Path == "/missing" {
- return &pluginapi.UIResponse{Status: 404}, nil
- }
- return UIJSON(0, map[string]any{"mount": in.Mount, "path": in.Path, "role": in.Role, "tenant": call.TenantID})
- })
- if m := p.Manifest(); len(m.Contributes["ui"]) != 1 {
- t.Fatalf("manifest = %+v", m)
- }
- var out pluginapi.UIResponse
- env := pluginapi.Envelope{Context: pluginapi.Context{TenantID: 3}}
- if err := c.Call(ctx, pluginapi.UIRequestPath, env, req, &out); err != nil {
- t.Fatal(err)
- }
- if out.Status != 200 || string(out.Body) != `{"mount":"pages/links","path":"/links","role":"admin","tenant":3}` {
- t.Fatalf("out = %d %s", out.Status, out.Body)
- }
- req.Path = "/missing"
- if err := c.Call(ctx, pluginapi.UIRequestPath, env, req, &out); err != nil || out.Status != 404 {
- t.Fatalf("status = %d, %v", out.Status, err)
- }
-}
-
-func TestEventsAndWebhooks(t *testing.T) {
- ctx := context.Background()
- p := New(Info{ID: "acme.hooks", Version: "1.0.0"})
- var seen []string
- p.OnEvent(func(_ context.Context, call *Call, ev pluginapi.EventDelivery) error {
- if ev.Type == pluginapi.EventKnowledgeFailed {
- return pluginapi.Errorf(pluginapi.CodeUnavailable, "try later")
- }
- seen = append(seen, fmt.Sprintf("%s#%d@%d", ev.Type, ev.Attempt, call.TenantID))
- return nil
- })
- p.Webhook("jira", func(
- _ context.Context, _ *Call, req pluginapi.WebhookRequest,
- ) (*pluginapi.WebhookResponse, error) {
- if req.Headers["X-Signature"] != "ok" {
- return &pluginapi.WebhookResponse{Status: 401}, nil
- }
- body := append([]byte(req.Path+" "), req.Body...)
- return &pluginapi.WebhookResponse{ContentType: "text/plain", Body: body}, nil
- })
- m := p.Manifest()
- if len(m.Contributes["events"]) != 1 || m.Contributes["webhooks"][0] != "jira" {
- t.Fatalf("manifest = %+v", m)
- }
- srv := httptest.NewServer(p.Handler())
- defer srv.Close()
- c := client.New(srv.URL, nil, nil)
- env := pluginapi.Envelope{Context: pluginapi.Context{TenantID: 9}}
-
- ev := pluginapi.EventDelivery{ID: "e1", Type: pluginapi.EventKnowledgeIngested, Attempt: 2}
- if err := c.Call(ctx, pluginapi.EventsPath, env, ev, nil); err != nil || seen[0] != "knowledge.ingested#2@9" {
- t.Fatalf("event = %v, %v", seen, err)
- }
- ev.Type = pluginapi.EventKnowledgeFailed
- err := c.Call(ctx, pluginapi.EventsPath, env, ev, nil)
- if pe, ok := pluginapi.AsError(err); !ok || !pe.Retryable {
- t.Fatalf("retryable failure = %v", err)
- }
-
- var out pluginapi.WebhookResponse
- req := pluginapi.WebhookRequest{
- Method: "POST", Path: "/issue", Headers: map[string]string{"X-Signature": "ok"}, Body: []byte("hi"),
- }
- if err := c.Call(ctx, pluginapi.WebhookPath("jira"), env, req, &out); err != nil || out.Status != 200 ||
- string(out.Body) != "/issue hi" || out.ContentType != "text/plain" {
- t.Fatalf("webhook = %+v, %v", out, err)
- }
- if err := c.Call(ctx, pluginapi.WebhookPath("nope"), env, req, &out); !isCode(err, pluginapi.CodeNotFound) {
- t.Fatalf("unknown webhook = %v", err)
- }
-}
-
-func TestOptions(t *testing.T) {
- ctx := context.Background()
- p := New(Info{ID: "acme.opts", Version: "1.0.0"})
- p.Options("projects", func(_ context.Context, call *Call, in pluginapi.OptionsInput) ([]pluginapi.Option, error) {
- if call.Config.Tenant["token"] == nil {
- return nil, pluginapi.InvalidConfig("enter a token first", map[string]string{"token": "required"})
- }
- return []pluginapi.Option{{Value: "p1", Label: "Project " + in.Query}}, nil
- })
- if m := p.Manifest(); m.Contributes["options"][0] != "projects" {
- t.Fatalf("manifest = %+v", m)
- }
- srv := httptest.NewServer(p.Handler())
- defer srv.Close()
- c := client.New(srv.URL, nil, nil)
- in := pluginapi.OptionsInput{Field: "project", Scope: "tenant", Query: "x"}
- var out pluginapi.OptionsOutput
- err := c.Call(ctx, pluginapi.OptionsPath("projects"), pluginapi.Envelope{}, in, &out)
- if !isCode(err, pluginapi.CodeInvalidConfig) {
- t.Fatalf("without a token = %v", err)
- }
- env := pluginapi.Envelope{Config: pluginapi.Config{Tenant: map[string]any{"token": "t"}}}
- err = c.Call(ctx, pluginapi.OptionsPath("projects"), env, in, &out)
- if err != nil || out.Options[0].Label != "Project x" {
- t.Fatalf("options = %+v, %v", out, err)
- }
-}
-
-func TestTools(t *testing.T) {
- ctx := context.Background()
- p := New(Info{ID: "acme.tools", Version: "1.0.0"})
- p.Tool("issues", pluginapi.Tool{
- Name: "search", Description: "Search issues",
- InputSchema: json.RawMessage(`{"type":"object","properties":{"q":{"type":"string"}}}`),
- }, func(_ context.Context, call *Call, args json.RawMessage) (*pluginapi.ToolResult, error) {
- var in struct{ Q string }
- _ = json.Unmarshal(args, &in)
- if in.Q == "boom" {
- return nil, pluginapi.Errorf(pluginapi.CodeUnauthorized, "token expired")
- }
- return pluginapi.StructuredResult(map[string]any{"q": in.Q, "site": call.Config.Tenant["site"]}, ""), nil
- })
- p.Tool("issues", pluginapi.Tool{Name: "count", Description: "Count"}, nil)
- if m := p.Manifest(); len(m.Contributes["mcpServers"]) != 1 || m.Contributes["mcpServers"][0] != "issues" {
- t.Fatalf("manifest = %+v", m)
- }
- srv := httptest.NewServer(p.Handler())
- defer srv.Close()
- c := client.New(srv.URL, nil, nil)
- env := pluginapi.Envelope{Config: pluginapi.Config{Tenant: map[string]any{"site": "acme"}}}
- send := func(id, method, params string) pluginapi.MCPResponse {
- t.Helper()
- req := pluginapi.MCPRequest{JSONRPC: "2.0", Method: method}
- if id != "" {
- req.ID = json.RawMessage(id)
- }
- if params != "" {
- req.Params = json.RawMessage(params)
- }
- var out pluginapi.MCPResponse
- if err := c.Call(ctx, pluginapi.MCPPath("issues"), env, req, &out); err != nil {
- t.Fatalf("%s: %v", method, err)
- }
- return out
- }
-
- if r := send("1", "initialize", `{"protocolVersion":"2025-06-18"}`); r.Error != nil ||
- !strings.Contains(string(r.Result), `"tools"`) || string(r.ID) != "1" {
- t.Fatalf("initialize = %+v", r)
- }
- var list struct{ Tools []pluginapi.Tool }
- if r := send("2", "tools/list", ""); json.Unmarshal(r.Result, &list) != nil || len(list.Tools) != 2 ||
- list.Tools[0].Name != "count" || string(list.Tools[0].InputSchema) != `{"type":"object"}` {
- t.Fatalf("tools/list = %s", r.Result)
- }
- var res pluginapi.ToolResult
- r := send(`"a"`, "tools/call", `{"name":"search","arguments":{"q":"login"}}`)
- if json.Unmarshal(r.Result, &res) != nil || res.IsError || res.Content[0].Text != `{"q":"login","site":"acme"}` {
- t.Fatalf("tools/call = %s", r.Result)
- }
- r = send("3", "tools/call", `{"name":"search","arguments":{"q":"boom"}}`)
- if json.Unmarshal(r.Result, &res) != nil || !res.IsError || res.Content[0].Text != "token expired" {
- t.Fatalf("a failing tool = %s", r.Result)
- }
- if r := send("4", "tools/call", `{"name":"nope"}`); r.Error == nil || r.Error.Code != pluginapi.MCPInvalidParams {
- t.Fatalf("unknown tool = %+v", r)
- }
- if r := send("5", "resources/list", ""); r.Error != nil || string(r.Result) != `{"resources":[]}` {
- t.Fatalf("resources = %+v", r)
- }
- if r := send("6", "completion/complete", ""); r.Error == nil || r.Error.Code != pluginapi.MCPMethodNotFound {
- t.Fatalf("unsupported method = %+v", r)
- }
- if r := send("", "notifications/initialized", ""); r.Error != nil || len(r.Result) != 0 {
- t.Fatalf("notification = %+v", r)
- }
- err := c.Call(ctx, pluginapi.MCPPath("nope"), env, pluginapi.MCPRequest{JSONRPC: "2.0", Method: "ping"}, nil)
- if !isCode(err, pluginapi.CodeNotFound) {
- t.Fatalf("unknown server = %v", err)
- }
-}
diff --git a/pluginsdk/pluginapi/connector.go b/pluginsdk/pluginapi/connector.go
deleted file mode 100644
index 07cbc9fd1..000000000
--- a/pluginsdk/pluginapi/connector.go
+++ /dev/null
@@ -1,96 +0,0 @@
-package pluginapi
-
-import "time"
-
-// ConnectorValidatePath is the validate endpoint of connector id.
-func ConnectorValidatePath(id string) string { return "/v1/connectors/" + id + "/validate" }
-
-// ConnectorListResourcesPath is the list-resources endpoint of connector id.
-func ConnectorListResourcesPath(id string) string { return "/v1/connectors/" + id + "/list-resources" }
-
-// ConnectorResolveAncestorsPath is the resolve-ancestors endpoint of connector id.
-func ConnectorResolveAncestorsPath(id string) string {
- return "/v1/connectors/" + id + "/resolve-ancestors"
-}
-
-// ConnectorFetchPath is the streaming fetch endpoint of connector id.
-func ConnectorFetchPath(id string) string { return "/v1/connectors/" + id + "/fetch" }
-
-// The connector's instance configuration arrives in Envelope.Config.Instance
-// as {"credentials": {...}, "settings": {...}, "resourceIds": [...]}.
-
-// ListResourcesInput asks for the resources under ParentID ("" = top level).
-type ListResourcesInput struct {
- ParentID string `json:"parentId,omitempty"`
-}
-
-// ListResourcesOutput lists resources a user can pick for syncing.
-type ListResourcesOutput struct {
- Resources []Resource `json:"resources"`
-}
-
-// Resource is something a data source can sync (a space, folder, feed).
-type Resource struct {
- ExternalID string `json:"externalId"`
- Name string `json:"name"`
- Type string `json:"type,omitempty"`
- Description string `json:"description,omitempty"`
- URL string `json:"url,omitempty"`
- ModifiedAt *time.Time `json:"modifiedAt,omitempty"`
- ParentID string `json:"parentId,omitempty"`
- HasChildren bool `json:"hasChildren,omitempty"`
- Metadata map[string]any `json:"metadata,omitempty"`
-}
-
-// ResolveAncestorsInput asks which ancestors of the given resources a
-// lazily loaded picker must expand.
-type ResolveAncestorsInput struct {
- ResourceIDs []string `json:"resourceIds"`
-}
-
-// ResolveAncestorsOutput is the deduplicated set of ancestor IDs.
-type ResolveAncestorsOutput struct {
- Ancestors []string `json:"ancestors"`
-}
-
-// Fetch modes.
-const (
- FetchFull = "full"
- FetchIncremental = "incremental"
-)
-
-// FetchInput starts or resumes a sync. The answer is a stream of "item"
-// events (FetchedItem), "checkpoint" events (Cursor) at page boundaries and
-// an "end" event carrying the final Cursor.
-type FetchInput struct {
- Mode string `json:"mode"`
- // Cursor is the last checkpoint or final cursor; nil starts from scratch.
- Cursor *Cursor `json:"cursor,omitempty"`
- // ResourceIDs are the selected resources (also in config.instance).
- ResourceIDs []string `json:"resourceIds,omitempty"`
-}
-
-// Cursor is the connector's resumable state. WeKnora stores it and hands it
-// back; its content is the plugin's business. A checkpoint must be a
-// complete snapshot: resuming from it reproduces all progress so far.
-type Cursor struct {
- LastSyncTime *time.Time `json:"lastSyncTime,omitempty"`
- State map[string]any `json:"state,omitempty"`
-}
-
-// FetchedItem is one document fetched from the source.
-type FetchedItem struct {
- ExternalID string `json:"externalId"`
- Title string `json:"title"`
- // Content is the document body; Markdown preferred. JSON carries it as
- // base64.
- Content []byte `json:"content,omitempty"`
- ContentType string `json:"contentType,omitempty"`
- FileName string `json:"fileName,omitempty"`
- URL string `json:"url,omitempty"`
- UpdatedAt *time.Time `json:"updatedAt,omitempty"`
- CreatedAt *time.Time `json:"createdAt,omitempty"`
- Metadata map[string]string `json:"metadata,omitempty"`
- IsDeleted bool `json:"isDeleted,omitempty"`
- SourceResourceID string `json:"sourceResourceId,omitempty"`
-}
diff --git a/pluginsdk/pluginapi/doc.go b/pluginsdk/pluginapi/doc.go
deleted file mode 100644
index f22145c4b..000000000
--- a/pluginsdk/pluginapi/doc.go
+++ /dev/null
@@ -1,32 +0,0 @@
-// Package pluginapi is version 1 of the WeKnora extension protocol: the
-// messages WeKnora and a code plugin exchange over HTTP. It has no
-// dependencies so that plugins and WeKnora share one definition; openapi.yaml
-// in this directory describes the same protocol for other languages.
-//
-// Every call is a POST whose body is an Envelope and whose successful answer
-// is {"output": ...}. Streaming calls answer with NDJSON Events instead. A
-// failed call answers with a non-2xx status and an ErrorBody. Within a major
-// version the protocol only grows: new optional fields and new endpoints.
-// Both sides ignore fields they do not know.
-package pluginapi
-
-// ProtocolVersion is the major version this package speaks; it travels in
-// the ProtocolHeader of every request.
-const ProtocolVersion = "1"
-
-// APIVersion is the manifest apiVersion of plugins speaking this protocol.
-const APIVersion = "weknora.plugin/v1"
-
-// Request headers.
-const (
- ProtocolHeader = "X-WeKnora-Protocol"
- RequestIDHeader = "X-Request-Id"
- // SignatureHeader and TimestampHeader authenticate calls to remote
- // plugins: hex HMAC-SHA256 over "<timestamp>.<body>" with the shared
- // secret. Host plugins get a bearer token instead.
- SignatureHeader = "X-WeKnora-Signature"
- TimestampHeader = "X-WeKnora-Timestamp"
-)
-
-// NDJSONContentType is the content type of streaming answers.
-const NDJSONContentType = "application/x-ndjson"
diff --git a/pluginsdk/pluginapi/envelope.go b/pluginsdk/pluginapi/envelope.go
deleted file mode 100644
index 7e3bd9689..000000000
--- a/pluginsdk/pluginapi/envelope.go
+++ /dev/null
@@ -1,75 +0,0 @@
-package pluginapi
-
-import (
- "encoding/json"
- "time"
-)
-
-// Envelope is the body of every call. Plugins must be stateless: everything
-// about the tenant comes in the envelope, and credentials must not be cached
-// across calls.
-type Envelope struct {
- Context Context `json:"context"`
- Config Config `json:"config"`
- // Input is the endpoint-specific request, such as a SearchInput.
- Input json.RawMessage `json:"input,omitempty"`
-}
-
-// Context describes who is calling and until when.
-type Context struct {
- TenantID uint64 `json:"tenantId"`
- UserID string `json:"userId,omitempty"`
- Locale string `json:"locale,omitempty"`
- RequestID string `json:"requestId,omitempty"`
- // Deadline is when WeKnora stops waiting; plugins should give up by then.
- Deadline *time.Time `json:"deadline,omitempty"`
- // Host lets the plugin call back into WeKnora (Host API); absent when
- // the plugin was granted no scopes.
- Host *HostAccess `json:"host,omitempty"`
- // Webhooks are this workspace's URLs of the plugin's webhooks, by
- // webhook ID, for registering them with a third party. Present only
- // when WeKnora knows its public address.
- Webhooks map[string]string `json:"webhooks,omitempty"`
-}
-
-// HostAccess is where and how a plugin calls the WeKnora Host API.
-type HostAccess struct {
- URL string `json:"url"`
- // Token is a short-lived bearer token bound to this tenant and the
- // plugin's granted scopes.
- Token string `json:"token"`
-}
-
-// Config carries the three configuration scopes separately, secrets already
-// decrypted. They are never merged, so a key means the same thing wherever
-// it appears.
-type Config struct {
- // System is the platform-wide configuration (config.system).
- System map[string]any `json:"system,omitempty"`
- // Tenant is the workspace configuration (config.tenant).
- Tenant map[string]any `json:"tenant,omitempty"`
- // Instance is the configuration of the integration instance being used:
- // a data source, a web search provider.
- Instance map[string]any `json:"instance,omitempty"`
-}
-
-// Output wraps a successful answer.
-type Output struct {
- Output json.RawMessage `json:"output"`
-}
-
-// Manifest is what GET /v1/manifest answers: enough for WeKnora to check that
-// the process it started is the package it installed.
-type Manifest struct {
- ID string `json:"id"`
- Version string `json:"version"`
- APIVersion string `json:"apiVersion"`
- // Contributes lists the contribution IDs the process serves per
- // extension point, e.g. {"connectors": ["rss"]}.
- Contributes map[string][]string `json:"contributes"`
-}
-
-// Health is what GET /v1/health answers.
-type Health struct {
- Status string `json:"status"` // "ok"
-}
diff --git a/pluginsdk/pluginapi/errors.go b/pluginsdk/pluginapi/errors.go
deleted file mode 100644
index d525a7699..000000000
--- a/pluginsdk/pluginapi/errors.go
+++ /dev/null
@@ -1,90 +0,0 @@
-package pluginapi
-
-import (
- "errors"
- "fmt"
-)
-
-// ErrorCode classifies a failed call; WeKnora reacts to each differently.
-type ErrorCode string
-
-const (
- // CodeInvalidConfig means the configuration is wrong; Details.Fields says
- // where. WeKnora shows it on the form.
- CodeInvalidConfig ErrorCode = "invalid_config"
- // CodeUnauthorized means the credentials no longer work.
- CodeUnauthorized ErrorCode = "unauthorized"
- // CodeNotFound means the resource does not exist.
- CodeNotFound ErrorCode = "not_found"
- // CodeRateLimited means back off for Details.RetryAfter seconds.
- CodeRateLimited ErrorCode = "rate_limited"
- // CodeUnavailable means the upstream service is down; retry later.
- CodeUnavailable ErrorCode = "unavailable"
- // CodeBadRequest means the call itself is malformed (protocol error).
- CodeBadRequest ErrorCode = "bad_request"
- // CodeInternal means the plugin failed.
- CodeInternal ErrorCode = "internal"
-)
-
-// Error is a failed call. Plugins return it (or wrap it) to choose the code;
-// any other error becomes CodeInternal.
-type Error struct {
- Code ErrorCode `json:"code"`
- Message string `json:"message"`
- Retryable bool `json:"retryable,omitempty"`
- Details *ErrorDetails `json:"details,omitempty"`
-}
-
-// ErrorDetails refines some codes.
-type ErrorDetails struct {
- // Fields are per-field problems for CodeInvalidConfig, keyed by the
- // dotted field path.
- Fields map[string]string `json:"fields,omitempty"`
- // RetryAfter is seconds to wait for CodeRateLimited.
- RetryAfter int `json:"retryAfter,omitempty"`
-}
-
-// ErrorBody is the JSON body of a failed call.
-type ErrorBody struct {
- Error Error `json:"error"`
-}
-
-func (e *Error) Error() string { return fmt.Sprintf("%s: %s", e.Code, e.Message) }
-
-// Errorf builds an Error. Unavailable and rate-limited errors are retryable.
-func Errorf(code ErrorCode, format string, args ...any) *Error {
- return &Error{
- Code: code, Message: fmt.Sprintf(format, args...),
- Retryable: code == CodeUnavailable || code == CodeRateLimited,
- }
-}
-
-// InvalidConfig reports field problems, keyed by dotted path.
-func InvalidConfig(message string, fields map[string]string) *Error {
- return &Error{Code: CodeInvalidConfig, Message: message, Details: &ErrorDetails{Fields: fields}}
-}
-
-// AsError finds an *Error in err's chain.
-func AsError(err error) (*Error, bool) {
- var e *Error
- ok := errors.As(err, &e)
- return e, ok
-}
-
-// HTTPStatus is the status an error code travels with.
-func (c ErrorCode) HTTPStatus() int {
- switch c {
- case CodeInvalidConfig, CodeBadRequest:
- return 400
- case CodeUnauthorized:
- return 401
- case CodeNotFound:
- return 404
- case CodeRateLimited:
- return 429
- case CodeUnavailable:
- return 503
- default:
- return 500
- }
-}
diff --git a/pluginsdk/pluginapi/events.go b/pluginsdk/pluginapi/events.go
deleted file mode 100644
index e00aec505..000000000
--- a/pluginsdk/pluginapi/events.go
+++ /dev/null
@@ -1,94 +0,0 @@
-package pluginapi
-
-import (
- "encoding/json"
- "time"
-)
-
-// EventsPath receives the events a plugin subscribed to in
-// permissions.events. Delivery is at least once: a delivery that fails with
-// a retryable error (or times out) is sent again later with the same ID, so
-// handlers must be idempotent on ID. A non-retryable error drops it.
-const EventsPath = "/v1/events"
-
-// Event types a plugin can subscribe to. The set only grows.
-const (
- // EventKnowledgeIngested: a document finished processing and is
- // searchable. Data is KnowledgeEventData.
- EventKnowledgeIngested = "knowledge.ingested"
- // EventKnowledgeFailed: a document failed processing for good.
- // Data is KnowledgeEventData with Error.
- EventKnowledgeFailed = "knowledge.failed"
- // EventKnowledgeDeleted: a document was deleted. Data is
- // KnowledgeEventData.
- EventKnowledgeDeleted = "knowledge.deleted"
- // EventChatAnswered: an assistant answer was completed. Data is
- // ChatEventData, question and answer included.
- EventChatAnswered = "chat.answered"
-)
-
-// EventTypes lists every event type, for manifest validation.
-var EventTypes = []string{EventKnowledgeIngested, EventKnowledgeFailed, EventKnowledgeDeleted, EventChatAnswered}
-
-// EventDelivery is one event sent to a plugin. The tenant is the envelope's.
-type EventDelivery struct {
- // ID identifies the event; retries keep it (idempotency key).
- ID string `json:"id"`
- Type string `json:"type"`
- OccurredAt time.Time `json:"occurredAt"`
- // Attempt counts deliveries of this event, from 1.
- Attempt int `json:"attempt"`
- Data json.RawMessage `json:"data,omitempty"`
-}
-
-// KnowledgeEventData describes a document of a knowledge base.
-type KnowledgeEventData struct {
- KnowledgeBaseID string `json:"knowledgeBaseId"`
- KnowledgeID string `json:"knowledgeId"`
- Title string `json:"title,omitempty"`
- FileName string `json:"fileName,omitempty"`
- FileType string `json:"fileType,omitempty"`
- // Source is the URL a URL or data-source document came from.
- Source string `json:"source,omitempty"`
- // Error is why processing failed (knowledge.failed).
- Error string `json:"error,omitempty"`
-}
-
-// ChatEventData describes an answered question.
-type ChatEventData struct {
- SessionID string `json:"sessionId"`
- // MessageID is the assistant message.
- MessageID string `json:"messageId"`
- AgentID string `json:"agentId,omitempty"`
- UserID string `json:"userId,omitempty"`
- Question string `json:"question,omitempty"`
- Answer string `json:"answer"`
-}
-
-// WebhookPath answers calls to webhook id (contributes.webhooks). Third
-// parties call WeKnora at a secret per-workspace URL; WeKnora checks the
-// URL, finds the workspace and relays the call here with the workspace's
-// envelope. Verifying the sender (a signature header) is the plugin's job.
-func WebhookPath(id string) string { return "/v1/webhooks/" + id }
-
-// WebhookRequest is one inbound call.
-type WebhookRequest struct {
- Method string `json:"method"`
- // Path is what followed the webhook URL, starting with "/" ("/" if
- // nothing did).
- Path string `json:"path"`
- Query string `json:"query,omitempty"`
- // Headers are the request headers, one value each, cookies removed.
- Headers map[string]string `json:"headers,omitempty"`
- // Body is the raw body; JSON carries it as base64.
- Body []byte `json:"body,omitempty"`
-}
-
-// WebhookResponse is what the caller receives.
-type WebhookResponse struct {
- // Status defaults to 200.
- Status int `json:"status,omitempty"`
- // ContentType of Body, if any.
- ContentType string `json:"contentType,omitempty"`
- Body []byte `json:"body,omitempty"`
-}
diff --git a/pluginsdk/pluginapi/handshake.go b/pluginsdk/pluginapi/handshake.go
deleted file mode 100644
index e2ce7930b..000000000
--- a/pluginsdk/pluginapi/handshake.go
+++ /dev/null
@@ -1,63 +0,0 @@
-package pluginapi
-
-import (
- "fmt"
- "strings"
-)
-
-// Environment a host plugin is started with.
-const (
- // EnvSocket is where the plugin must listen (a unix socket path, or a
- // host:port when EnvNetwork is "tcp").
- EnvSocket = "WEKNORA_PLUGIN_SOCKET"
- // EnvNetwork is "unix" (default) or "tcp".
- EnvNetwork = "WEKNORA_PLUGIN_NETWORK"
- // EnvToken is the bearer token every call from the host carries.
- EnvToken = "WEKNORA_PLUGIN_TOKEN"
- // EnvPluginID and EnvPluginVersion name the package being run.
- EnvPluginID = "WEKNORA_PLUGIN_ID"
- EnvPluginVersion = "WEKNORA_PLUGIN_VERSION"
- // EnvAddr and EnvSecret configure a remote plugin (not started by a
- // host): where to listen and the shared secret calls are signed with.
- EnvAddr = "WEKNORA_PLUGIN_ADDR"
- EnvSecret = "WEKNORA_PLUGIN_SECRET"
-)
-
-// handshakePrefix starts the line a host plugin prints on stdout once it is
-// ready: WEKNORA_PLUGIN|1|unix|/path/to.sock
-const handshakePrefix = "WEKNORA_PLUGIN"
-
-// Handshake is the readiness line of a host plugin.
-type Handshake struct {
- Protocol string
- Network string // unix | tcp
- Address string
-}
-
-// String renders the handshake line (without a newline).
-func (h Handshake) String() string {
- return strings.Join([]string{handshakePrefix, h.Protocol, h.Network, h.Address}, "|")
-}
-
-// ParseHandshake reads a handshake line. ok is false for any other line,
-// which the host treats as log output.
-func ParseHandshake(line string) (h Handshake, ok bool, err error) {
- parts := strings.Split(strings.TrimSpace(line), "|")
- if len(parts) == 0 || parts[0] != handshakePrefix {
- return Handshake{}, false, nil
- }
- if len(parts) != 4 {
- return Handshake{}, true, fmt.Errorf("malformed handshake %q", line)
- }
- h = Handshake{Protocol: parts[1], Network: parts[2], Address: parts[3]}
- if h.Protocol != ProtocolVersion {
- return h, true, fmt.Errorf("plugin speaks protocol %s, host speaks %s", h.Protocol, ProtocolVersion)
- }
- if h.Network != "unix" && h.Network != "tcp" {
- return h, true, fmt.Errorf("unknown handshake network %q", h.Network)
- }
- if h.Address == "" {
- return h, true, fmt.Errorf("handshake has no address")
- }
- return h, true, nil
-}
diff --git a/pluginsdk/pluginapi/hostapi.go b/pluginsdk/pluginapi/hostapi.go
deleted file mode 100644
index 7fc7e827d..000000000
--- a/pluginsdk/pluginapi/hostapi.go
+++ /dev/null
@@ -1,48 +0,0 @@
-package pluginapi
-
-import (
- "encoding/json"
- "time"
-)
-
-// Host API: how a plugin calls back into WeKnora. The URL and a short-lived
-// bearer token arrive in Context.Host of each call; the token is bound to the
-// tenant of that call and the scopes the plugin was granted (manifest
-// permissions.hostApi). Errors use ErrorBody.
-
-// HostKVPath is the key-value endpoint (scope "kv"): GET and DELETE take
-// ?key=, PUT takes a KVPut body.
-const HostKVPath = "/api/v1/plugin-host/kv"
-
-// HostKVListPath lists keys: ?prefix=&after=&limit=.
-const HostKVListPath = "/api/v1/plugin-host/kv/list"
-
-// KVEntry is one stored value.
-type KVEntry struct {
- Key string `json:"key"`
- Value json.RawMessage `json:"value"`
- ExpiresAt *time.Time `json:"expiresAt,omitempty"`
- UpdatedAt time.Time `json:"updatedAt"`
-}
-
-// KVPut stores a value; TTLSeconds 0 keeps it until deleted.
-type KVPut struct {
- Key string `json:"key"`
- Value json.RawMessage `json:"value"`
- TTLSeconds int `json:"ttlSeconds,omitempty"`
-}
-
-// KVList is a page of keys in key order; pass Next as after for the next
-// page (empty when there is none).
-type KVList struct {
- Entries []KVEntry `json:"entries"`
- Next string `json:"next,omitempty"`
-}
-
-// Limits of the key-value store.
-const (
- KVMaxKeyBytes = 256
- KVMaxValueBytes = 64 << 10
- KVMaxKeys = 10000 // per plugin and tenant
- KVMaxListLimit = 1000
-)
diff --git a/pluginsdk/pluginapi/mcp.go b/pluginsdk/pluginapi/mcp.go
deleted file mode 100644
index 6b5302af4..000000000
--- a/pluginsdk/pluginapi/mcp.go
+++ /dev/null
@@ -1,114 +0,0 @@
-package pluginapi
-
-import (
- "encoding/json"
- "fmt"
-)
-
-// MCPPath is the MCP server a plugin serves itself: an mcpServers
-// contribution without a url. The input is one JSON-RPC 2.0 message of the
-// Model Context Protocol, the output its JSON-RPC response. Carrying MCP in
-// the envelope gives tool calls the workspace's context and configuration,
-// and the same authentication as every other call. Servers are stateless:
-// each message stands alone, with no session.
-func MCPPath(id string) string { return "/v1/mcp/" + id }
-
-// MCPProtocolVersion is the MCP revision plugins speak.
-const MCPProtocolVersion = "2025-06-18"
-
-// JSON-RPC error codes MCP servers answer with.
-const (
- MCPMethodNotFound = -32601
- MCPInvalidParams = -32602
-)
-
-// MCPRequest is a JSON-RPC request or notification (no ID).
-type MCPRequest struct {
- JSONRPC string `json:"jsonrpc"`
- ID json.RawMessage `json:"id,omitempty"`
- Method string `json:"method"`
- Params json.RawMessage `json:"params,omitempty"`
-}
-
-// MCPResponse is a JSON-RPC response.
-type MCPResponse struct {
- JSONRPC string `json:"jsonrpc"`
- ID json.RawMessage `json:"id,omitempty"`
- Result json.RawMessage `json:"result,omitempty"`
- Error *MCPError `json:"error,omitempty"`
-}
-
-// MCPError is a JSON-RPC error.
-type MCPError struct {
- Code int `json:"code"`
- Message string `json:"message"`
-}
-
-// Tool describes a tool for tools/list.
-type Tool struct {
- Name string `json:"name"`
- Title string `json:"title,omitempty"`
- Description string `json:"description"`
- // InputSchema is the JSON Schema of the arguments (type: object).
- InputSchema json.RawMessage `json:"inputSchema"`
- // OutputSchema describes StructuredContent, when the tool returns it.
- OutputSchema json.RawMessage `json:"outputSchema,omitempty"`
- Annotations *ToolAnnotations `json:"annotations,omitempty"`
-}
-
-// ToolAnnotations are hints about a tool's behavior.
-type ToolAnnotations struct {
- // ReadOnlyHint says the tool changes nothing.
- ReadOnlyHint bool `json:"readOnlyHint,omitempty"`
- // DestructiveHint says the tool may delete or overwrite.
- DestructiveHint *bool `json:"destructiveHint,omitempty"`
-}
-
-// ToolCallParams are the params of tools/call.
-type ToolCallParams struct {
- Name string `json:"name"`
- Arguments json.RawMessage `json:"arguments,omitempty"`
-}
-
-// ToolResult is what tools/call returns. The model reads Content;
-// StructuredContent is data for the tool's result view (toolViews).
-type ToolResult struct {
- Content []ToolContent `json:"content"`
- StructuredContent any `json:"structuredContent,omitempty"`
- // IsError marks a failure the model should see and may recover from.
- IsError bool `json:"isError,omitempty"`
-}
-
-// ToolContent is one content block: text, or an image (base64 Data).
-type ToolContent struct {
- Type string `json:"type"`
- Text string `json:"text,omitempty"`
- Data string `json:"data,omitempty"`
- MimeType string `json:"mimeType,omitempty"`
-}
-
-// TextResult is a result the model reads as text.
-func TextResult(text string) *ToolResult {
- return &ToolResult{Content: []ToolContent{{Type: "text", Text: text}}}
-}
-
-// StructuredResult returns data for the result view, and text for the model
-// (the data as JSON when text is empty, as MCP recommends).
-func StructuredResult(data any, text string) *ToolResult {
- if text == "" {
- b, err := json.Marshal(data)
- if err != nil {
- text = fmt.Sprint(data)
- } else {
- text = string(b)
- }
- }
- return &ToolResult{Content: []ToolContent{{Type: "text", Text: text}}, StructuredContent: data}
-}
-
-// ToolError is a failed call the model sees.
-func ToolError(format string, args ...any) *ToolResult {
- r := TextResult(fmt.Sprintf(format, args...))
- r.IsError = true
- return r
-}
diff --git a/pluginsdk/pluginapi/openapi.yaml b/pluginsdk/pluginapi/openapi.yaml
deleted file mode 100644
index 1f65c5b3d..000000000
--- a/pluginsdk/pluginapi/openapi.yaml
+++ /dev/null
@@ -1,636 +0,0 @@
-openapi: 3.1.0
-info:
- title: WeKnora extension protocol
- version: "1"
- description: |
- What WeKnora calls on a code plugin. Every POST takes an Envelope and
- answers {"output": ...}, or streams NDJSON Events. Failures answer a
- non-2xx status with an ErrorBody. Within a major version the protocol
- only grows; both sides ignore fields they do not know.
-
- Host plugins (started by the WeKnora plugin host) are called over the
- socket from their handshake with `Authorization: Bearer <token>`. Remote
- plugins are called over HTTPS and verify `X-WeKnora-Signature`:
- hex HMAC-SHA256 of "<X-WeKnora-Timestamp>.<body>" with the shared secret,
- rejecting timestamps more than 5 minutes off.
-servers:
- - url: http://plugin
-security:
- - hostToken: []
- - signature: []
-paths:
- /v1/manifest:
- get:
- summary: What this process is and serves
- responses:
- "200":
- description: The manifest
- content:
- application/json:
- schema: { $ref: "#/components/schemas/Manifest" }
- default: { $ref: "#/components/responses/Error" }
- /v1/health:
- get:
- summary: Liveness
- responses:
- "200":
- description: Healthy
- content:
- application/json:
- schema:
- type: object
- required: [status]
- properties: { status: { const: ok } }
- default: { $ref: "#/components/responses/Error" }
- /v1/config/validate:
- post:
- summary: Check the plugin's system and tenant configuration
- requestBody: { $ref: "#/components/requestBodies/Envelope" }
- responses:
- "200": { $ref: "#/components/responses/EmptyOutput" }
- default: { $ref: "#/components/responses/Error" }
- /v1/websearch/{id}/search:
- post:
- summary: Web search (WebSearchProvider.Search)
- parameters: [{ $ref: "#/components/parameters/ContributionID" }]
- requestBody:
- required: true
- content:
- application/json:
- schema:
- allOf:
- - $ref: "#/components/schemas/Envelope"
- - properties: { input: { $ref: "#/components/schemas/SearchInput" } }
- responses:
- "200":
- description: Results
- content:
- application/json:
- schema:
- type: object
- required: [output]
- properties: { output: { $ref: "#/components/schemas/SearchOutput" } }
- default: { $ref: "#/components/responses/Error" }
- /v1/connectors/{id}/validate:
- post:
- summary: Check a data source configuration (Connector.Validate)
- parameters: [{ $ref: "#/components/parameters/ContributionID" }]
- requestBody: { $ref: "#/components/requestBodies/Envelope" }
- responses:
- "200": { $ref: "#/components/responses/EmptyOutput" }
- default: { $ref: "#/components/responses/Error" }
- /v1/connectors/{id}/list-resources:
- post:
- summary: List syncable resources under a parent (Connector.ListResources)
- parameters: [{ $ref: "#/components/parameters/ContributionID" }]
- requestBody:
- required: true
- content:
- application/json:
- schema:
- allOf:
- - $ref: "#/components/schemas/Envelope"
- - properties:
- input:
- type: object
- properties: { parentId: { type: string } }
- responses:
- "200":
- description: Resources
- content:
- application/json:
- schema:
- type: object
- required: [output]
- properties:
- output:
- type: object
- required: [resources]
- properties:
- resources: { type: array, items: { $ref: "#/components/schemas/Resource" } }
- default: { $ref: "#/components/responses/Error" }
- /v1/connectors/{id}/resolve-ancestors:
- post:
- summary: Ancestors a lazy picker must expand to show a selection
- parameters: [{ $ref: "#/components/parameters/ContributionID" }]
- requestBody:
- required: true
- content:
- application/json:
- schema:
- allOf:
- - $ref: "#/components/schemas/Envelope"
- - properties:
- input:
- type: object
- properties: { resourceIds: { type: array, items: { type: string } } }
- responses:
- "200":
- description: Ancestor IDs
- content:
- application/json:
- schema:
- type: object
- required: [output]
- properties:
- output:
- type: object
- required: [ancestors]
- properties: { ancestors: { type: array, items: { type: string } } }
- default: { $ref: "#/components/responses/Error" }
- /v1/connectors/{id}/fetch:
- post:
- summary: Stream a full or incremental sync
- description: |
- Answers NDJSON: "item" events carry a FetchedItem, "checkpoint"
- events a Cursor (a complete, resumable snapshot), and the stream
- ends with "end" (data = final Cursor) or "error". A stream that
- stops without either is treated as interrupted.
- parameters: [{ $ref: "#/components/parameters/ContributionID" }]
- requestBody:
- required: true
- content:
- application/json:
- schema:
- allOf:
- - $ref: "#/components/schemas/Envelope"
- - properties: { input: { $ref: "#/components/schemas/FetchInput" } }
- responses:
- "200":
- description: Event stream
- content:
- application/x-ndjson:
- schema: { $ref: "#/components/schemas/Event" }
- default: { $ref: "#/components/responses/Error" }
- /v1/parsers/{id}/parse:
- post:
- summary: Parse a document into Markdown (DocReader.Read)
- description: |
- The file arrives as base64 content (or a URL). Answer Markdown and
- the images it references; WeKnora stores the images and chunks the
- text. A non-retryable error fails the document for good; a
- retryable one (unavailable, rate_limited) is retried later.
- parameters: [{ $ref: "#/components/parameters/ContributionID" }]
- requestBody:
- required: true
- content:
- application/json:
- schema:
- allOf:
- - $ref: "#/components/schemas/Envelope"
- - properties: { input: { $ref: "#/components/schemas/ParseInput" } }
- responses:
- "200":
- description: The parsed document
- content:
- application/json:
- schema:
- type: object
- required: [output]
- properties: { output: { $ref: "#/components/schemas/ParseOutput" } }
- default: { $ref: "#/components/responses/Error" }
- /v1/ui/request:
- post:
- summary: Answer a request from one of the plugin's pages
- description: |
- Plugin pages (pages, settingsSections, kbTabs) run in a sandboxed
- iframe and reach the plugin only through the WeKnora bridge.
- WeKnora checks the caller may open the page (its minRole) and
- forwards the request with the usual envelope. Method and path are
- the page's own routing. Plugins with pages that make no requests
- answer not_found. The manifest lists "ui": ["request"] under
- contributes when the plugin answers.
- requestBody:
- required: true
- content:
- application/json:
- schema:
- allOf:
- - $ref: "#/components/schemas/Envelope"
- - properties: { input: { $ref: "#/components/schemas/UIRequest" } }
- responses:
- "200":
- description: What the page receives
- content:
- application/json:
- schema:
- type: object
- required: [output]
- properties: { output: { $ref: "#/components/schemas/UIResponse" } }
- default: { $ref: "#/components/responses/Error" }
- /v1/options/{name}:
- post:
- summary: List the choices of a form field (x-options)
- description: |
- The envelope config holds what the form holds now, stored secrets
- filled in for fields the user did not change. Answer invalid_config
- when the form lacks what the list needs. The manifest lists the
- names under contributes.options.
- parameters:
- - { name: name, in: path, required: true, schema: { type: string } }
- requestBody:
- required: true
- content:
- application/json:
- schema:
- allOf:
- - $ref: "#/components/schemas/Envelope"
- - properties: { input: { $ref: "#/components/schemas/OptionsInput" } }
- responses:
- "200":
- description: The choices
- content:
- application/json:
- schema:
- type: object
- required: [output]
- properties: { output: { $ref: "#/components/schemas/OptionsOutput" } }
- default: { $ref: "#/components/responses/Error" }
- /v1/mcp/{id}:
- post:
- summary: One Model Context Protocol message to the plugin's MCP server
- description: |
- For mcpServers contributions declared without a url: the plugin
- serves the tools itself. The input is one JSON-RPC 2.0 message
- (initialize, ping, tools/list, tools/call, or a notification); the
- output is its JSON-RPC response (empty for a notification). Servers
- are stateless and keep no session. Tool failures the model should see
- are results with isError, not protocol errors. The manifest lists the
- server IDs under contributes.mcpServers.
- parameters:
- - { name: id, in: path, required: true, schema: { type: string } }
- requestBody:
- required: true
- content:
- application/json:
- schema:
- allOf:
- - $ref: "#/components/schemas/Envelope"
- - properties: { input: { $ref: "#/components/schemas/MCPRequest" } }
- responses:
- "200":
- description: The JSON-RPC response
- content:
- application/json:
- schema:
- type: object
- required: [output]
- properties: { output: { $ref: "#/components/schemas/MCPResponse" } }
- default: { $ref: "#/components/responses/Error" }
- /v1/events:
- post:
- summary: Receive an event the plugin subscribed to (permissions.events)
- description: |
- At least once: a retryable error or a timeout brings the same event
- back later with the same id and a higher attempt, so handle it
- idempotently. A non-retryable error drops it. Ignore unknown types.
- The manifest lists "events": ["handler"] under contributes when the
- plugin handles events.
- requestBody:
- required: true
- content:
- application/json:
- schema:
- allOf:
- - $ref: "#/components/schemas/Envelope"
- - properties: { input: { $ref: "#/components/schemas/EventDelivery" } }
- responses:
- "200":
- description: Handled
- content:
- application/json:
- schema: { type: object, required: [output], properties: { output: { type: object } } }
- default: { $ref: "#/components/responses/Error" }
- /v1/webhooks/{id}:
- post:
- summary: Answer an inbound call to webhook id (contributes.webhooks)
- description: |
- Third parties call a secret per-workspace URL of WeKnora; WeKnora
- relays the call here with that workspace's envelope. Verify the
- sender (e.g. a signature header) with the workspace configuration.
- parameters: [{ $ref: "#/components/parameters/ContributionID" }]
- requestBody:
- required: true
- content:
- application/json:
- schema:
- allOf:
- - $ref: "#/components/schemas/Envelope"
- - properties: { input: { $ref: "#/components/schemas/WebhookRequest" } }
- responses:
- "200":
- description: The answer to the caller
- content:
- application/json:
- schema:
- type: object
- required: [output]
- properties: { output: { $ref: "#/components/schemas/WebhookResponse" } }
- default: { $ref: "#/components/responses/Error" }
-components:
- securitySchemes:
- hostToken: { type: http, scheme: bearer }
- signature: { type: apiKey, in: header, name: X-WeKnora-Signature }
- parameters:
- ContributionID:
- name: id
- in: path
- required: true
- description: The contribution's local ID from plugin.yaml
- schema: { type: string }
- requestBodies:
- Envelope:
- required: true
- content:
- application/json:
- schema: { $ref: "#/components/schemas/Envelope" }
- responses:
- EmptyOutput:
- description: Accepted
- content:
- application/json:
- schema:
- type: object
- required: [output]
- properties: { output: { type: object } }
- Error:
- description: A failed call
- content:
- application/json:
- schema: { $ref: "#/components/schemas/ErrorBody" }
- schemas:
- Envelope:
- type: object
- required: [context, config]
- properties:
- context:
- type: object
- required: [tenantId]
- properties:
- tenantId: { type: integer }
- userId: { type: string }
- locale: { type: string }
- requestId: { type: string }
- deadline: { type: string, format: date-time }
- host:
- type: object
- description: Host API access; absent without granted scopes
- properties:
- url: { type: string }
- token: { type: string }
- webhooks:
- type: object
- description: This workspace's webhook URLs by webhook ID, when WeKnora knows its public address
- additionalProperties: { type: string }
- config:
- type: object
- description: Configuration scopes, secrets decrypted, never merged
- properties:
- system: { type: object }
- tenant: { type: object }
- instance: { type: object }
- input: {}
- Manifest:
- type: object
- required: [id, version, apiVersion, contributes]
- properties:
- id: { type: string }
- version: { type: string }
- apiVersion: { const: weknora.plugin/v1 }
- contributes:
- type: object
- additionalProperties: { type: array, items: { type: string } }
- SearchInput:
- type: object
- required: [query]
- properties:
- query: { type: string }
- maxResults: { type: integer }
- includeDate: { type: boolean }
- region: { type: string }
- freshness: { type: string }
- SearchOutput:
- type: object
- required: [results]
- properties:
- results:
- type: array
- items:
- type: object
- required: [title, url]
- properties:
- title: { type: string }
- url: { type: string }
- snippet: { type: string }
- content: { type: string }
- age: { type: string }
- publishedAt: { type: string, format: date-time }
- Resource:
- type: object
- required: [externalId, name]
- properties:
- externalId: { type: string }
- name: { type: string }
- type: { type: string }
- description: { type: string }
- url: { type: string }
- modifiedAt: { type: string, format: date-time }
- parentId: { type: string }
- hasChildren: { type: boolean }
- metadata: { type: object }
- FetchInput:
- type: object
- required: [mode]
- properties:
- mode: { enum: [full, incremental] }
- cursor: { $ref: "#/components/schemas/Cursor" }
- resourceIds: { type: array, items: { type: string } }
- Cursor:
- type: object
- properties:
- lastSyncTime: { type: string, format: date-time }
- state: { type: object }
- FetchedItem:
- type: object
- required: [externalId, title]
- properties:
- externalId: { type: string }
- title: { type: string }
- content: { type: string, contentEncoding: base64 }
- contentType: { type: string }
- fileName: { type: string }
- url: { type: string }
- updatedAt: { type: string, format: date-time }
- createdAt: { type: string, format: date-time }
- metadata: { type: object, additionalProperties: { type: string } }
- isDeleted: { type: boolean }
- sourceResourceId: { type: string }
- ParseInput:
- type: object
- properties:
- fileName: { type: string }
- fileType: { type: string, description: "Lower-case extension without the dot" }
- content: { type: string, contentEncoding: base64 }
- url: { type: string }
- title: { type: string }
- ParseOutput:
- type: object
- required: [markdown]
- properties:
- markdown: { type: string }
- images:
- type: array
- items:
- type: object
- required: [originalRef, data]
- properties:
- originalRef: { type: string }
- mimeType: { type: string }
- data: { type: string, contentEncoding: base64 }
- metadata: { type: object, additionalProperties: { type: string } }
- OptionsInput:
- type: object
- required: [field, scope]
- properties:
- field: { type: string, description: Dotted path of the field }
- scope: { enum: [system, tenant, instance] }
- contribution: { type: string, description: '"<point>/<id>" when scope is instance' }
- query: { type: string }
- OptionsOutput:
- type: object
- required: [options]
- properties:
- options:
- type: array
- items:
- type: object
- required: [value, label]
- properties:
- value: {}
- label: { type: string }
- description: { type: string }
- MCPRequest:
- type: object
- required: [jsonrpc, method]
- properties:
- jsonrpc: { const: "2.0" }
- id: { description: Absent for a notification }
- method: { type: string }
- params: { type: object }
- MCPResponse:
- type: object
- required: [jsonrpc]
- properties:
- jsonrpc: { const: "2.0" }
- id: {}
- result: { type: object, description: "tools/call answers a ToolResult" }
- error:
- type: object
- required: [code, message]
- properties:
- code: { type: integer }
- message: { type: string }
- ToolResult:
- type: object
- required: [content]
- properties:
- content:
- type: array
- items:
- type: object
- required: [type]
- properties:
- type: { enum: [text, image] }
- text: { type: string }
- data: { type: string, description: Base64 image }
- mimeType: { type: string }
- structuredContent: { description: Data for the tool's result view (toolViews) }
- isError: { type: boolean }
- EventDelivery:
- type: object
- required: [id, type, occurredAt, attempt]
- properties:
- id: { type: string, description: Idempotency key; kept across retries }
- type: { enum: [knowledge.ingested, knowledge.failed, knowledge.deleted, chat.answered] }
- occurredAt: { type: string, format: date-time }
- attempt: { type: integer, minimum: 1 }
- data:
- oneOf:
- - $ref: "#/components/schemas/KnowledgeEventData"
- - $ref: "#/components/schemas/ChatEventData"
- KnowledgeEventData:
- type: object
- required: [knowledgeBaseId, knowledgeId]
- properties:
- knowledgeBaseId: { type: string }
- knowledgeId: { type: string }
- title: { type: string }
- fileName: { type: string }
- fileType: { type: string }
- source: { type: string }
- error: { type: string }
- ChatEventData:
- type: object
- required: [sessionId, messageId, answer]
- properties:
- sessionId: { type: string }
- messageId: { type: string }
- agentId: { type: string }
- userId: { type: string }
- question: { type: string }
- answer: { type: string }
- WebhookRequest:
- type: object
- required: [method, path]
- properties:
- method: { type: string }
- path: { type: string }
- query: { type: string }
- headers: { type: object, additionalProperties: { type: string } }
- body: { type: string, contentEncoding: base64 }
- WebhookResponse:
- type: object
- properties:
- status: { type: integer }
- contentType: { type: string }
- body: { type: string, contentEncoding: base64 }
- UIRequest:
- type: object
- required: [mount, method, path]
- properties:
- mount: { type: string, description: '"<point>/<id>" of the page, e.g. pages/links' }
- method: { type: string }
- path: { type: string }
- body: { description: JSON the page sent }
- role: { enum: [viewer, contributor, admin, owner] }
- UIResponse:
- type: object
- properties:
- status: { type: integer, description: HTTP-style status for the page; 200 when absent }
- body: { description: JSON returned to the page }
- Event:
- type: object
- required: [type]
- properties:
- type: { enum: [item, checkpoint, progress, log, error, end] }
- data: {}
- error: { $ref: "#/components/schemas/Error" }
- message: { type: string }
- level: { enum: [debug, info, warn, error] }
- Error:
- type: object
- required: [code, message]
- properties:
- code: { enum: [invalid_config, unauthorized, not_found, rate_limited, unavailable, bad_request, internal] }
- message: { type: string }
- retryable: { type: boolean }
- details:
- type: object
- properties:
- fields: { type: object, additionalProperties: { type: string } }
- retryAfter: { type: integer }
- ErrorBody:
- type: object
- required: [error]
- properties:
- error: { $ref: "#/components/schemas/Error" }
diff --git a/pluginsdk/pluginapi/options.go b/pluginsdk/pluginapi/options.go
deleted file mode 100644
index 6cc74fdc0..000000000
--- a/pluginsdk/pluginapi/options.go
+++ /dev/null
@@ -1,40 +0,0 @@
-package pluginapi
-
-// OptionsPath answers the choices of a form field whose schema names
-// x-options {name}: projects of the account the form is being filled for,
-// channels of a workspace, and so on. The envelope's config carries what
-// the form holds right now (stored secrets filled in for fields the user did
-// not change), in the scope the form edits.
-func OptionsPath(name string) string { return "/v1/options/" + name }
-
-// Option scopes: which configuration the form edits.
-const (
- OptionsScopeSystem = "system"
- OptionsScopeTenant = "tenant"
- OptionsScopeInstance = "instance"
-)
-
-// OptionsInput asks for one field's choices.
-type OptionsInput struct {
- // Field is the dotted path of the field ("settings.project").
- Field string `json:"field"`
- // Scope is system, tenant or instance.
- Scope string `json:"scope"`
- // Contribution is "<point>/<id>" of the instance's contribution
- // ("connectors/jira") when Scope is instance.
- Contribution string `json:"contribution,omitempty"`
- // Query is what the user typed, for fields that search (x-options.search).
- Query string `json:"query,omitempty"`
-}
-
-// Option is one choice.
-type Option struct {
- Value any `json:"value"`
- Label string `json:"label"`
- Description string `json:"description,omitempty"`
-}
-
-// OptionsOutput lists the choices, in display order.
-type OptionsOutput struct {
- Options []Option `json:"options"`
-}
diff --git a/pluginsdk/pluginapi/parser.go b/pluginsdk/pluginapi/parser.go
deleted file mode 100644
index 9b9379e48..000000000
--- a/pluginsdk/pluginapi/parser.go
+++ /dev/null
@@ -1,34 +0,0 @@
-package pluginapi
-
-// ParsePath is the parse endpoint of parser id.
-func ParsePath(id string) string { return "/v1/parsers/" + id + "/parse" }
-
-// ParseInput is one document to parse: its bytes (file mode) or a URL (URL
-// mode). The parser's own settings arrive in Config.System and Config.Tenant.
-type ParseInput struct {
- FileName string `json:"fileName,omitempty"`
- // FileType is the lower-case extension without the dot ("pdf").
- FileType string `json:"fileType,omitempty"`
- // Content is the file; JSON carries it as base64.
- Content []byte `json:"content,omitempty"`
- URL string `json:"url,omitempty"`
- Title string `json:"title,omitempty"`
-}
-
-// ParseOutput is the document as Markdown. WeKnora chunks it; parsers do not.
-type ParseOutput struct {
- Markdown string `json:"markdown"`
- // Images are the pictures the Markdown references: an image whose
- // OriginalRef equals a ![](target) in Markdown is stored and the link
- // rewritten. Inline data: URIs in the Markdown work too.
- Images []ParsedImage `json:"images,omitempty"`
- // Metadata such as "title" (URL mode) or "pages".
- Metadata map[string]string `json:"metadata,omitempty"`
-}
-
-// ParsedImage is one image of a parsed document.
-type ParsedImage struct {
- OriginalRef string `json:"originalRef"`
- MimeType string `json:"mimeType,omitempty"`
- Data []byte `json:"data"`
-}
diff --git a/pluginsdk/pluginapi/pluginapi_test.go b/pluginsdk/pluginapi/pluginapi_test.go
deleted file mode 100644
index f7b5b1ccd..000000000
--- a/pluginsdk/pluginapi/pluginapi_test.go
+++ /dev/null
@@ -1,48 +0,0 @@
-package pluginapi
-
-import (
- "strconv"
- "testing"
- "time"
-)
-
-func TestHandshake(t *testing.T) {
- h := Handshake{Protocol: ProtocolVersion, Network: "unix", Address: "/tmp/p.sock"}
- got, ok, err := ParseHandshake(h.String() + "\n")
- if !ok || err != nil || got != h {
- t.Fatalf("round trip = %+v %v %v", got, ok, err)
- }
- if _, ok, _ := ParseHandshake("starting up..."); ok {
- t.Fatal("other output is not a handshake")
- }
- bad := []string{
- "WEKNORA_PLUGIN|2|unix|/x", "WEKNORA_PLUGIN|1|pipe|/x", "WEKNORA_PLUGIN|1|unix|", "WEKNORA_PLUGIN|1",
- }
- for _, bad := range bad {
- if _, ok, err := ParseHandshake(bad); !ok || err == nil {
- t.Errorf("%q must be a rejected handshake", bad)
- }
- }
-}
-
-func TestSignature(t *testing.T) {
- secret, body, now := []byte("k"), []byte(`{"a":1}`), time.Now()
- ts := strconv.FormatInt(now.Unix(), 10)
- sig := Sign(secret, now.Unix(), body)
- if err := VerifySignature(secret, ts, sig, body, now); err != nil {
- t.Fatal(err)
- }
- if VerifySignature(secret, ts, sig, []byte(`{"a":2}`), now) == nil {
- t.Fatal("a changed body must fail")
- }
- if VerifySignature(secret, ts, sig, body, now.Add(10*time.Minute)) == nil {
- t.Fatal("a replayed old request must fail")
- }
-}
-
-func TestErrorStatus(t *testing.T) {
- if Errorf(CodeRateLimited, "x").Code.HTTPStatus() != 429 || !Errorf(CodeUnavailable, "x").Retryable ||
- Errorf(CodeInternal, "x").Retryable {
- t.Fatal("error codes map wrong")
- }
-}
diff --git a/pluginsdk/pluginapi/signature.go b/pluginsdk/pluginapi/signature.go
deleted file mode 100644
index 32d54783d..000000000
--- a/pluginsdk/pluginapi/signature.go
+++ /dev/null
@@ -1,37 +0,0 @@
-package pluginapi
-
-import (
- "crypto/hmac"
- "crypto/sha256"
- "encoding/hex"
- "fmt"
- "strconv"
- "time"
-)
-
-// MaxClockSkew bounds how old a signed request may be.
-const MaxClockSkew = 5 * time.Minute
-
-// Sign returns the signature of a request body at a unix timestamp.
-func Sign(secret []byte, timestamp int64, body []byte) string {
- mac := hmac.New(sha256.New, secret)
- _, _ = fmt.Fprintf(mac, "%d.", timestamp)
- _, _ = mac.Write(body)
- return hex.EncodeToString(mac.Sum(nil))
-}
-
-// VerifySignature checks a signed request against the secret and clock.
-func VerifySignature(secret []byte, timestamp, signature string, body []byte, now time.Time) error {
- ts, err := strconv.ParseInt(timestamp, 10, 64)
- if err != nil {
- return fmt.Errorf("bad %s", TimestampHeader)
- }
- if d := now.Sub(time.Unix(ts, 0)); d > MaxClockSkew || d < -MaxClockSkew {
- return fmt.Errorf("request timestamp is outside the allowed clock skew")
- }
- want := Sign(secret, ts, body)
- if !hmac.Equal([]byte(want), []byte(signature)) {
- return fmt.Errorf("bad signature")
- }
- return nil
-}
diff --git a/pluginsdk/pluginapi/stream.go b/pluginsdk/pluginapi/stream.go
deleted file mode 100644
index 7f42bc095..000000000
--- a/pluginsdk/pluginapi/stream.go
+++ /dev/null
@@ -1,34 +0,0 @@
-package pluginapi
-
-import "encoding/json"
-
-// EventType is the kind of one NDJSON line in a streaming answer.
-type EventType string
-
-const (
- // EventItem carries one result (a FetchedItem for connectors).
- EventItem EventType = "item"
- // EventCheckpoint carries a resumable cursor; WeKnora persists it.
- EventCheckpoint EventType = "checkpoint"
- // EventProgress reports progress for display.
- EventProgress EventType = "progress"
- // EventLog forwards a log line to WeKnora's logs.
- EventLog EventType = "log"
- // EventError ends the stream with an error.
- EventError EventType = "error"
- // EventEnd ends the stream successfully, with the final Data.
- EventEnd EventType = "end"
-)
-
-// Event is one line of a streaming answer. A stream that stops without an
-// "end" or "error" event was interrupted and is treated as failed.
-type Event struct {
- Type EventType `json:"type"`
- Data json.RawMessage `json:"data,omitempty"`
- // Error is set on EventError.
- Error *Error `json:"error,omitempty"`
- // Message is set on EventLog and EventProgress.
- Message string `json:"message,omitempty"`
- // Level is set on EventLog: debug, info, warn, error.
- Level string `json:"level,omitempty"`
-}
diff --git a/pluginsdk/pluginapi/ui.go b/pluginsdk/pluginapi/ui.go
deleted file mode 100644
index 57174ea9b..000000000
--- a/pluginsdk/pluginapi/ui.go
+++ /dev/null
@@ -1,31 +0,0 @@
-package pluginapi
-
-import "encoding/json"
-
-// UIRequestPath answers requests from a plugin's pages. Pages run in a
-// sandboxed iframe that can reach nothing itself; what they send through
-// the WeKnora bridge (api.request) arrives here, with the caller's context
-// and configuration in the envelope.
-const UIRequestPath = "/v1/ui/request"
-
-// UIRequest is one request a plugin page made.
-type UIRequest struct {
- // Mount is the page it came from, as "<point>/<id>" ("pages/links").
- Mount string `json:"mount"`
- // Method and Path are the page's own routing: WeKnora does not
- // interpret them. Path starts with "/".
- Method string `json:"method"`
- Path string `json:"path"`
- // Body is the JSON the page sent, if any.
- Body json.RawMessage `json:"body,omitempty"`
- // Role is the caller's workspace role: viewer, contributor, admin or
- // owner. WeKnora already refused callers below the mount's minRole.
- Role string `json:"role,omitempty"`
-}
-
-// UIResponse is what the page receives.
-type UIResponse struct {
- // Status is an HTTP-style status for the page (200 when zero).
- Status int `json:"status,omitempty"`
- Body json.RawMessage `json:"body,omitempty"`
-}
diff --git a/pluginsdk/pluginapi/websearch.go b/pluginsdk/pluginapi/websearch.go
deleted file mode 100644
index 57b3601c6..000000000
--- a/pluginsdk/pluginapi/websearch.go
+++ /dev/null
@@ -1,32 +0,0 @@
-package pluginapi
-
-import "time"
-
-// SearchPath is the web search endpoint for provider id.
-func SearchPath(id string) string { return "/v1/websearch/" + id + "/search" }
-
-// SearchInput is the input of a web search.
-type SearchInput struct {
- Query string `json:"query"`
- MaxResults int `json:"maxResults"`
- IncludeDate bool `json:"includeDate,omitempty"`
- // Region and Freshness are optional filters; a provider that cannot
- // honour a requested filter must fail rather than ignore it.
- Region string `json:"region,omitempty"`
- Freshness string `json:"freshness,omitempty"`
-}
-
-// SearchOutput is the output of a web search.
-type SearchOutput struct {
- Results []SearchResult `json:"results"`
-}
-
-// SearchResult is one hit.
-type SearchResult struct {
- Title string `json:"title"`
- URL string `json:"url"`
- Snippet string `json:"snippet,omitempty"`
- Content string `json:"content,omitempty"`
- Age string `json:"age,omitempty"`
- PublishedAt *time.Time `json:"publishedAt,omitempty"`
-}
diff --git a/pluginsdk/python/.gitignore b/pluginsdk/python/.gitignore
deleted file mode 100644
index 25aacffde..000000000
--- a/pluginsdk/python/.gitignore
+++ /dev/null
@@ -1,3 +0,0 @@
-build/
-dist/
-*.egg-info/
diff --git a/pluginsdk/python/README.md b/pluginsdk/python/README.md
deleted file mode 100644
index f981ed80d..000000000
--- a/pluginsdk/python/README.md
+++ /dev/null
@@ -1,192 +0,0 @@
-# weknora-plugin
-
-Build [WeKnora](https://github.com/Tencent/WeKnora) code plugins in Python.
-The package speaks the same extension protocol as the Go SDK. It uses only
-the standard library and supports Python 3.9 and later.
-
-```python
-from weknora_plugin import Plugin, SearchResult
-
-plugin = Plugin("acme.search", "1.0.0") # must match plugin.yaml
-
-
[email protected]_search("web")
-def search(call, q):
- # call.tenant_id, call.locale, call.system / call.tenant / call.instance
- return [SearchResult(title=q.query, url="https://example.com")]
-
-
-if __name__ == "__main__":
- plugin.serve()
-```
-
-## Contributions
-
-**Web search.**
-
-```python
-def search(call, q: SearchInput) -> list[SearchResult]: ...
-```
-
-**Parser.** Return a `ParseOutput`, or just the Markdown as a `str`.
-
-```python
-def parse(call, doc: ParseInput) -> ParseOutput: ...
-```
-
-- `doc.content` is the file as bytes.
-- Images the Markdown references as `![](ref)` go in `images` with a
- matching `original_ref`.
-
-**Connector.** A class, registered with `@plugin.connector(id)` (or pass
-an instance):
-
-```python
-class Notes:
- def validate(self, call, cfg: ConnectorConfig) -> None: ...
- def list_resources(self, call, cfg, parent_id: str) -> list[Resource]: ...
- def fetch(self, call, cfg, inp: FetchInput, stream: Stream) -> Cursor | None:
- for note in changed_since(inp.cursor):
- stream.item(FetchedItem(external_id=note.id, title=note.title, content=note.markdown))
- stream.checkpoint(Cursor(state={"after": last_id})) # at page boundaries
- return Cursor(state={"after": last_id})
- # optional: resolve_ancestors(self, call, cfg, resource_ids) -> list[str]
-```
-
-**Configuration check.**
-
-```python
-def validate(call) -> None: ...
-```
-
-**Pages.** Requests from the plugin's pages (`pages`,
-`settingsSections`, `kbTabs` in plugin.yaml) arrive at one handler. Return a
-`UIResponse`, or any JSON value for a 200:
-
-```python
-def ui(call, req: UIRequest):
- # req.mount ("pages/links"), req.method, req.path, req.body, req.role
- return UIResponse(status=200, body=links)
-```
-
-The pages themselves are HTML under `ui/` that use
-[`@weknora/plugin-ui`](../../packages/plugin-ui).
-
-**Events and webhooks.**
-
-```python
-def on_event(call, ev: EventDelivery): # permissions.events; dedupe on ev.id
- ...
-
[email protected]("notes") # contributes.webhooks
-def notes(call, req: WebhookRequest): # req.headers, req.body, req.json()
- return WebhookResponse(status=204)
-```
-
-**Dynamic choices.** A field with `x-options: {name: projects}` in a
-config schema gets its choices from the plugin. Return `Option`s or
-`(value, label)` pairs:
-
-```python
[email protected]("projects")
-def projects(call, inp: OptionsInput):
- # inp.scope, inp.field, inp.query; the form's values are in call.tenant / call.instance
- if not call.tenant.get("token"):
- raise invalid_config("enter the token first", {"token": "required"})
- return [("p1", "Project one"), ("p2", "Project two")]
-```
-
-**Agent tools.** Declare an `mcpServers` contribution without `mcp.url`
-and add tools to it:
-
-```python
[email protected]("tools", "search_issues", "Search issues",
- {"type": "object", "properties": {"text": {"type": "string"}}}, read_only=True)
-def search_issues(call, args):
- rows = [...]
- return ToolResult.structured({"issues": rows}, text="3 issues ...")
-```
-
-Return a `ToolResult`, a `str` (text for the model) or any JSON value
-(structured content for the result view). Exceptions become a failed result
-the model sees. See the [Go SDK README](../README.md#agent-tools) for
-`toolViews`.
-
-**OAuth.** An `x-oauth` field needs no code: WeKnora runs the flow, and
-the plugin finds a fresh access token where the field is (see the
-[Go SDK README](../README.md#dynamic-choices-and-oauth)).
-
-## Errors
-
-Raise `PluginError(ErrorCode.X, "message")` to choose what WeKnora sees:
-
-- `unauthorized`: credentials stopped working.
-- `rate_limited` (with `retry_after=`): back off.
-- `unavailable`: retry later.
-- `invalid_config(message, {"settings.url": "required"})`: point at form
- fields.
-
-Any other exception becomes `internal`, and its traceback goes to stderr.
-
-## Calling back into WeKnora
-
-A plugin granted Host API scopes (`permissions.hostApi` in plugin.yaml) gets
-a client per call from `call.host()`. It returns `None` without a grant.
-
-```python
-host = call.host()
-host.kv_put("cursor", {"page": 3}, ttl=3600)
-host.kv_get("cursor", default={})
-```
-
-The token behind it lasts a few minutes: use it within the call.
-
-## Running
-
-`plugin.serve()` picks its mode from the environment.
-
-- **Host mode.** The WeKnora plugin host starts the plugin with
- `WEKNORA_PLUGIN_SOCKET` and `WEKNORA_PLUGIN_TOKEN`. The plugin listens
- on that socket and prints the handshake on stdout. Keep stdout free of
- anything else, and log to stderr (`plugin.logger`).
-- **Remote mode.** Run on its own, it listens on `WEKNORA_PLUGIN_ADDR`
- (default `:8080`). It accepts only requests signed with
- `WEKNORA_PLUGIN_SECRET`, the secret shown when the plugin was registered.
-
-On SIGTERM it stops accepting calls and lets the ones in flight finish.
-Each thread handles one call. `call.deadline` says when WeKnora stops
-waiting.
-
-## Packaging for the plugin host
-
-```yaml
-runtime: { type: host, kind: python, entry: main.py }
-```
-
-WeKnora runs the entry with its own `python3`. It puts the package's
-`vendor/` directory and the package root on `PYTHONPATH`. Vendor the SDK
-and any dependencies:
-
-```bash
-pip install --target vendor weknora-plugin # or copy src/weknora_plugin
-```
-
-Pure-Python dependencies work everywhere. Ones with native code must match
-the server's platform. For a complete plugin, see
-`examples/plugins/notebooks` (a Jupyter notebook parser).
-
-## Testing
-
-`plugin.test_server()` serves the plugin on a loopback port without
-authentication, for unit tests. To check the protocol end to end, run the
-conformance suite from the Go SDK against a running plugin:
-
-```bash
-go run github.com/Tencent/WeKnora/pluginsdk/cmd/weknora-plugin-conformance -url http://localhost:8080 -secret $SECRET
-```
diff --git a/pluginsdk/python/pyproject.toml b/pluginsdk/python/pyproject.toml
deleted file mode 100644
index 08df6fb28..000000000
--- a/pluginsdk/python/pyproject.toml
+++ /dev/null
@@ -1,23 +0,0 @@
-[build-system]
-requires = ["setuptools>=64"]
-build-backend = "setuptools.build_meta"
-
-[project]
-name = "weknora-plugin"
-version = "0.1.0"
-description = "Build WeKnora code plugins in Python"
-readme = "README.md"
-requires-python = ">=3.9"
-license = { text = "MIT" }
-authors = [{ name = "WeKnora" }]
-classifiers = [
- "Programming Language :: Python :: 3",
- "Operating System :: OS Independent",
-]
-dependencies = []
-
-[project.urls]
-Homepage = "https://github.com/Tencent/WeKnora/tree/main/pluginsdk/python"
-
-[tool.setuptools.packages.find]
-where = ["src"]
diff --git a/pluginsdk/python/src/weknora_plugin/__init__.py b/pluginsdk/python/src/weknora_plugin/__init__.py
deleted file mode 100644
index bb9843af1..000000000
--- a/pluginsdk/python/src/weknora_plugin/__init__.py
+++ /dev/null
@@ -1,93 +0,0 @@
-"""Build WeKnora code plugins in Python.
-
- from weknora_plugin import Plugin, SearchResult
-
- plugin = Plugin("acme.search", "1.0.0")
-
- @plugin.web_search("web")
- def search(call, query):
- return [SearchResult(title="WeKnora", url="https://github.com/Tencent/WeKnora")]
-
- if __name__ == "__main__":
- plugin.serve()
-
-Started by the WeKnora plugin host, the plugin listens where the host says
-and accepts only the host's token. Run on its own (a remote plugin), it
-listens on WEKNORA_PLUGIN_ADDR and checks request signatures with
-WEKNORA_PLUGIN_SECRET. The package uses only the standard library.
-"""
-
-from .host import Host
-from .plugin import Call, Plugin, Stream, StreamClosed
-from .protocol import API_VERSION, PROTOCOL_VERSION, ErrorCode, PluginError, invalid_config
-from .types import (
- FETCH_FULL,
- FETCH_INCREMENTAL,
- EVENT_CHAT_ANSWERED,
- EVENT_KNOWLEDGE_DELETED,
- EVENT_KNOWLEDGE_FAILED,
- EVENT_KNOWLEDGE_INGESTED,
- ConnectorConfig,
- Cursor,
- EventDelivery,
- FetchedItem,
- FetchInput,
- Option,
- OptionsInput,
- KVEntry,
- KVList,
- ParsedImage,
- ParseInput,
- ParseOutput,
- Resource,
- SearchInput,
- SearchResult,
- ToolContent,
- ToolResult,
- UIRequest,
- UIResponse,
- WebhookRequest,
- WebhookResponse,
-)
-
-__version__ = "0.1.0"
-
-__all__ = [
- "API_VERSION",
- "PROTOCOL_VERSION",
- "FETCH_FULL",
- "FETCH_INCREMENTAL",
- "Call",
- "ConnectorConfig",
- "Cursor",
- "EVENT_CHAT_ANSWERED",
- "EVENT_KNOWLEDGE_DELETED",
- "EVENT_KNOWLEDGE_FAILED",
- "EVENT_KNOWLEDGE_INGESTED",
- "EventDelivery",
- "ErrorCode",
- "FetchInput",
- "FetchedItem",
- "Host",
- "KVEntry",
- "KVList",
- "Option",
- "OptionsInput",
- "ParseInput",
- "ParseOutput",
- "ParsedImage",
- "Plugin",
- "PluginError",
- "Resource",
- "SearchInput",
- "ToolContent",
- "ToolResult",
- "SearchResult",
- "Stream",
- "StreamClosed",
- "UIRequest",
- "UIResponse",
- "WebhookRequest",
- "WebhookResponse",
- "invalid_config",
-]
diff --git a/pluginsdk/python/src/weknora_plugin/host.py b/pluginsdk/python/src/weknora_plugin/host.py
deleted file mode 100644
index f3d7fe081..000000000
--- a/pluginsdk/python/src/weknora_plugin/host.py
+++ /dev/null
@@ -1,95 +0,0 @@
-"""The Host API client: how a plugin calls back into WeKnora during a call."""
-
-from __future__ import annotations
-
-import json
-import urllib.error
-import urllib.parse
-import urllib.request
-from datetime import timedelta
-from typing import Any, Optional, Tuple, Union
-
-from .protocol import HOST_KV_LIST_PATH, HOST_KV_PATH, ErrorCode, PluginError
-from .types import KVEntry, KVList, from_wire
-
-
-class Host:
- """Reaches the Host API with the short-lived token of one call. Use it
- within the call; the token expires in minutes."""
-
- def __init__(self, url: str, token: str, timeout: float = 30) -> None:
- self._base = url.rstrip("/")
- self._token = token
- self._timeout = timeout
-
- def _do(self, method: str, path: str, query: Optional[dict] = None, body: Any = None) -> Any:
- url = self._base + path
- if query:
- url += "?" + urllib.parse.urlencode(query)
- data = None
- headers = {"Authorization": "Bearer " + self._token}
- if body is not None:
- data = json.dumps(body).encode()
- headers["Content-Type"] = "application/json"
- req = urllib.request.Request(url, data=data, method=method, headers=headers)
- try:
- with urllib.request.urlopen(req, timeout=self._timeout) as resp:
- raw = resp.read()
- except urllib.error.HTTPError as e:
- raw = e.read()
- try:
- err = json.loads(raw)["error"]
- if err.get("code"):
- raise PluginError.from_wire(err) from None
- except (ValueError, KeyError, TypeError):
- pass
- raise PluginError(ErrorCode.INTERNAL, f"host api answered HTTP {e.code}") from None
- except (urllib.error.URLError, OSError) as e:
- raise PluginError(ErrorCode.UNAVAILABLE, f"host api: {e}") from None
- return json.loads(raw) if raw else None
-
- def kv_entry(self, key: str) -> Optional[KVEntry]:
- """Reads a key with its metadata, or None when it does not exist."""
- try:
- return from_wire(KVEntry, self._do("GET", HOST_KV_PATH, {"key": key}))
- except PluginError as e:
- if e.code == ErrorCode.NOT_FOUND:
- return None
- raise
-
- def kv_get(self, key: str, default: Any = None) -> Any:
- """Reads a key's value, or default when it does not exist."""
- entry = self.kv_entry(key)
- return default if entry is None else entry.value
-
- def kv_put(self, key: str, value: Any, ttl: Union[float, timedelta] = 0) -> None:
- """Stores a JSON value; ttl (seconds or a timedelta) 0 keeps it
- until deleted."""
- if isinstance(ttl, timedelta):
- ttl = ttl.total_seconds()
- body: dict = {"key": key, "value": value}
- if ttl:
- body["ttlSeconds"] = int(ttl)
- self._do("PUT", HOST_KV_PATH, body=body)
-
- def kv_delete(self, key: str) -> None:
- """Removes a key; a missing key is not an error."""
- self._do("DELETE", HOST_KV_PATH, {"key": key})
-
- def kv_list(self, prefix: str = "", after: str = "", limit: int = 0) -> KVList:
- """A page of keys with prefix, in key order after the given key;
- pass the result's next as after for the following page."""
- q = {"prefix": prefix, "after": after}
- if limit > 0:
- q["limit"] = str(limit)
- return from_wire(KVList, self._do("GET", HOST_KV_LIST_PATH, q))
-
- def kv_items(self, prefix: str = "") -> "Tuple[KVEntry, ...]":
- """Every entry with prefix, following pages."""
- out, after = [], ""
- while True:
- page = self.kv_list(prefix, after)
- out.extend(page.entries)
- if not page.next:
- return tuple(out)
- after = page.next
diff --git a/pluginsdk/python/src/weknora_plugin/plugin.py b/pluginsdk/python/src/weknora_plugin/plugin.py
deleted file mode 100644
index b40ded420..000000000
--- a/pluginsdk/python/src/weknora_plugin/plugin.py
+++ /dev/null
@@ -1,768 +0,0 @@
-"""Plugin: register contributions, then serve the protocol."""
-
-from __future__ import annotations
-
-import contextlib
-import hmac
-import json
-import logging
-import os
-import re
-import signal
-import socket
-import socketserver
-import sys
-import threading
-import time
-import traceback
-from datetime import datetime, timezone
-from http.server import BaseHTTPRequestHandler, HTTPServer
-from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple
-
-from . import protocol as p
-from .host import Host
-from .protocol import ErrorCode, PluginError
-from .types import (
- ConnectorConfig,
- Cursor,
- FetchInput,
- FetchedItem,
- ParseInput,
- ParseOutput,
- SearchInput,
- SearchResult,
- EventDelivery,
- ToolResult,
- OptionsInput,
- UIRequest,
- UIResponse,
- WebhookRequest,
- WebhookResponse,
- from_wire,
- parse_time,
- to_wire,
-)
-
-
-class Call:
- """One request's caller and configuration. Configuration secrets are
- already decrypted; do not keep them beyond the call."""
-
- def __init__(self, envelope: dict) -> None:
- ctx = envelope.get("context") or {}
- cfg = envelope.get("config") or {}
- self.tenant_id: int = int(ctx.get("tenantId") or 0)
- self.user_id: str = ctx.get("userId") or ""
- self.locale: str = ctx.get("locale") or ""
- self.request_id: str = ctx.get("requestId") or ""
- #: When WeKnora stops waiting; give up by then.
- self.deadline: Optional[datetime] = parse_time(ctx.get("deadline"))
- self._host = ctx.get("host") or {}
- #: This workspace's webhook URLs by webhook ID, when WeKnora knows
- #: its public address.
- self.webhooks: Dict[str, str] = ctx.get("webhooks") or {}
- #: Platform-wide configuration (config.system).
- self.system: Dict[str, Any] = cfg.get("system") or {}
- #: Workspace configuration (config.tenant).
- self.tenant: Dict[str, Any] = cfg.get("tenant") or {}
- #: The integration instance's configuration, e.g. a data source's.
- self.instance: Dict[str, Any] = cfg.get("instance") or {}
-
- def time_left(self) -> Optional[float]:
- """Seconds until the deadline, or None without one."""
- if self.deadline is None:
- return None
- return (self.deadline - datetime.now(timezone.utc)).total_seconds()
-
- def host(self) -> Optional[Host]:
- """The Host API client of this call, or None when the plugin was
- granted no Host API scopes."""
- if not self._host.get("url") or not self._host.get("token"):
- return None
- return Host(self._host["url"], self._host["token"])
-
-
-class StreamClosed(Exception):
- """WeKnora stopped listening to a stream: stop fetching and return."""
-
-
-class Stream:
- """A streaming answer: items, checkpoints and progress as NDJSON lines,
- each sent at once. Safe to use from several threads."""
-
- def __init__(self, handler: "_Handler") -> None:
- self._h = handler
- self._lock = threading.Lock()
- self._started = False
- self._closed = False
-
- def _write(self, event: dict) -> None:
- with self._lock:
- if self._closed:
- raise StreamClosed("stream already ended")
- try:
- if not self._started:
- self._h.send_response(200)
- self._h.send_header("Content-Type", p.NDJSON_CONTENT_TYPE)
- self._h.send_header("Transfer-Encoding", "chunked")
- self._h.end_headers()
- self._started = True
- line = json.dumps(event, separators=(",", ":")).encode() + b"\n"
- self._h.wfile.write(b"%x\r\n%s\r\n" % (len(line), line))
- self._h.wfile.flush()
- except OSError as e:
- self._closed = True
- raise StreamClosed(str(e)) from None
-
- def item(self, item: FetchedItem) -> None:
- """Emits one fetched item."""
- self._write({"type": "item", "data": to_wire(item)})
-
- def checkpoint(self, cursor: Cursor) -> None:
- """Emits a resumable cursor; it must be a complete snapshot."""
- self._write({"type": "checkpoint", "data": to_wire(cursor)})
-
- def progress(self, message: str) -> None:
- """Reports progress for display."""
- self._write({"type": "progress", "message": message})
-
- def log(self, level: str, message: str) -> None:
- """Forwards a line to WeKnora's logs: debug, info, warn or error."""
- self._write({"type": "log", "level": level, "message": message})
-
- def _finish(self) -> None:
- try:
- self._h.wfile.write(b"0\r\n\r\n")
- self._h.wfile.flush()
- except OSError:
- pass
- self._closed = True
-
- def _end(self, cursor: Optional[Cursor]) -> None:
- try:
- self._write({"type": "end", "data": to_wire(cursor or Cursor())})
- except StreamClosed:
- return
- with self._lock:
- self._finish()
-
- def _fail(self, err: PluginError) -> None:
- with self._lock:
- if self._closed:
- return
- started = self._started
- if not started:
- self._h._send_error(err)
- self._closed = True
- return
- try:
- self._write({"type": "error", "error": err.to_wire()})
- except StreamClosed:
- return
- with self._lock:
- self._finish()
-
-
-WebSearchFunc = Callable[[Call, SearchInput], List[SearchResult]]
-ParserFunc = Callable[[Call, ParseInput], ParseOutput]
-ConfigValidator = Callable[[Call], None]
-UIHandler = Callable[[Call, UIRequest], Any]
-EventHandler = Callable[[Call, EventDelivery], None]
-WebhookHandler = Callable[[Call, WebhookRequest], Any]
-OptionsHandler = Callable[[Call, OptionsInput], Any]
-ToolHandler = Callable[[Call, dict], Any]
-
-#: The MCP revision plugins speak.
-MCP_PROTOCOL_VERSION = "2025-06-18"
-
-_ROUTE = re.compile(r"^/v1/(websearch|connectors|parsers)/([^/]+)/([a-z-]+)$")
-
-
-class Plugin:
- """A plugin under construction: register contributions, then serve.
-
- ``id`` and ``version`` must match plugin.yaml."""
-
- def __init__(self, id: str, version: str, logger: Optional[logging.Logger] = None) -> None:
- self.id = id
- self.version = version
- self._web_search: Dict[str, WebSearchFunc] = {}
- self._connectors: Dict[str, Any] = {}
- self._parsers: Dict[str, ParserFunc] = {}
- self._validate: Optional[ConfigValidator] = None
- self._ui: Optional[UIHandler] = None
- self._events: Optional[EventHandler] = None
- self._webhooks: Dict[str, WebhookHandler] = {}
- self._options: Dict[str, OptionsHandler] = {}
- self._mcp: Dict[str, Dict[str, Tuple[dict, ToolHandler]]] = {}
- #: Seconds serve() waits for calls in flight after SIGTERM.
- self.shutdown_timeout = 60.0
- if logger is None:
- logger = logging.getLogger("weknora_plugin")
- if not logger.handlers:
- h = logging.StreamHandler(sys.stderr)
- h.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(message)s"))
- logger.addHandler(h)
- logger.setLevel(logging.INFO)
- #: Writes to stderr, which the plugin host forwards to WeKnora's
- #: logs. Keep stdout for the handshake.
- self.logger = logger
-
- # Registration. Each works as a call or as a decorator.
-
- def web_search(self, id: str, fn: Optional[WebSearchFunc] = None) -> Any:
- """Registers web search provider id: fn(call, SearchInput) returns
- a list of SearchResult."""
- return self._register(self._web_search, id, fn)
-
- def parser(self, id: str, fn: Optional[ParserFunc] = None) -> Any:
- """Registers parser id: fn(call, ParseInput) returns a ParseOutput
- (or the Markdown as a str)."""
- return self._register(self._parsers, id, fn)
-
- def connector(self, id: str, connector: Any = None) -> Any:
- """Registers connector id: an object with validate(call, cfg),
- list_resources(call, cfg, parent_id) and fetch(call, cfg, input,
- stream), and optionally resolve_ancestors(call, cfg, resource_ids).
- As a decorator on a class it registers an instance."""
- if connector is not None:
- self._connectors[id] = connector() if isinstance(connector, type) else connector
- return connector
-
- def register(c: Any) -> Any:
- self._connectors[id] = c() if isinstance(c, type) else c
- return c
-
- return register
-
- def on_event(self, fn: EventHandler) -> EventHandler:
- """Registers the handler of the events subscribed to in
- permissions.events: fn(call, EventDelivery). Deliveries repeat, so
- be idempotent on the event's id. Raise a retryable PluginError
- (unavailable, rate_limited) to be called again later; any other
- error drops the event. Ignore types you do not know."""
- self._events = fn
- return fn
-
- def webhook(self, id: str, fn: Optional[WebhookHandler] = None) -> Any:
- """Registers webhook id (contributes.webhooks): fn(call,
- WebhookRequest) returns a WebhookResponse, or any JSON value for a
- 200. call is the workspace the URL belongs to."""
- return self._register(self._webhooks, id, fn)
-
- def options(self, name: str, fn: Optional[OptionsHandler] = None) -> Any:
- """Registers the choices behind x-options {name}: fn(call,
- OptionsInput) returns a list of Option (or of (value, label)
- pairs). call's config holds what the form holds now. Raise
- invalid_config(...) when the form lacks what the list needs."""
- return self._register(self._options, name, fn)
-
- def tool(
- self,
- server: str,
- name: str,
- description: str,
- input_schema: Optional[dict] = None,
- *,
- title: str = "",
- output_schema: Optional[dict] = None,
- read_only: bool = False,
- ) -> Callable[[ToolHandler], ToolHandler]:
- """Decorator adding a tool to the MCP server the plugin serves as
- server (contributes.mcpServers[].id, declared without a url):
- fn(call, args) returns a ToolResult, a str (text for the model) or
- any JSON value (structured content, shown by the tool's result
- view). Exceptions become a failed result the model sees. call
- carries the workspace's configuration."""
- spec: dict = {"name": name, "description": description, "inputSchema": input_schema or {"type": "object"}}
- if title:
- spec["title"] = title
- if output_schema:
- spec["outputSchema"] = output_schema
- if read_only:
- spec["annotations"] = {"readOnlyHint": True}
-
- def register(fn: ToolHandler) -> ToolHandler:
- self._mcp.setdefault(server, {})[name] = (spec, fn)
- return fn
-
- return register
-
- def ui(self, fn: UIHandler) -> UIHandler:
- """Registers the handler behind the plugin's pages: fn(call,
- UIRequest) returns a UIResponse, or any JSON value for a 200."""
- self._ui = fn
- return fn
-
- def config_validator(self, fn: ConfigValidator) -> ConfigValidator:
- """Registers the check behind POST /v1/config/validate: raise
- invalid_config(...) to point at fields."""
- self._validate = fn
- return fn
-
- @staticmethod
- def _register(table: dict, id: str, fn: Any) -> Any:
- if fn is not None:
- table[id] = fn
- return fn
-
- def register(f: Any) -> Any:
- table[id] = f
- return f
-
- return register
-
- def manifest(self) -> dict:
- """What the plugin reports at GET /v1/manifest."""
- contributes = {}
- for point, table in (
- ("webSearch", self._web_search),
- ("connectors", self._connectors),
- ("parsers", self._parsers),
- ("webhooks", self._webhooks),
- ("options", self._options),
- ("mcpServers", self._mcp),
- ):
- if table:
- contributes[point] = sorted(table)
- if self._ui is not None:
- contributes["ui"] = ["request"]
- if self._events is not None:
- contributes["events"] = ["handler"]
- return {"id": self.id, "version": self.version, "apiVersion": p.API_VERSION, "contributes": contributes}
-
- # Dispatch.
-
- def _dispatch(self, h: "_Handler", method: str, path: str, body: bytes) -> None:
- if method == "GET" and path == "/v1/manifest":
- h._send_json(200, self.manifest())
- return
- if method == "GET" and path == "/v1/health":
- h._send_json(200, {"status": "ok"})
- return
- if method == "POST" and path == "/v1/config/validate":
- self._unary(h, body, lambda call, _: self._validate(call) if self._validate else None, empty=True)
- return
- if method == "POST" and path == "/v1/ui/request":
- if self._ui is None:
- h._send_error(PluginError(ErrorCode.NOT_FOUND, "this plugin's pages make no requests"))
- else:
- self._unary(h, body, lambda call, raw: _ui_output(self._ui(call, from_wire(UIRequest, raw))))
- return
- if method == "POST" and path == "/v1/events":
- if self._events is None:
- h._send_error(PluginError(ErrorCode.NOT_FOUND, "this plugin handles no events"))
- else:
- events = self._events
- self._unary(h, body, lambda call, raw: events(call, from_wire(EventDelivery, raw)), empty=True)
- return
- if method == "POST" and path.startswith("/v1/options/"):
- name = path[len("/v1/options/"):]
- fn = self._options.get(name)
- if fn is None:
- h._send_error(PluginError(ErrorCode.NOT_FOUND, f"no options {name!r}"))
- else:
- self._unary(h, body, lambda call, raw: {"options": _options_output(fn(call, from_wire(OptionsInput, raw)))})
- return
- if method == "POST" and path.startswith("/v1/mcp/"):
- sid = path[len("/v1/mcp/"):]
- server = self._mcp.get(sid)
- if server is None:
- h._send_error(PluginError(ErrorCode.NOT_FOUND, f"no MCP server {sid!r}"))
- else:
- self._unary(h, body, lambda call, raw: self._answer_mcp(call, sid, server, raw or {}))
- return
- if method == "POST" and path.startswith("/v1/webhooks/"):
- hook = self._webhooks.get(path[len("/v1/webhooks/"):])
- if hook is None:
- h._send_error(PluginError(ErrorCode.NOT_FOUND, f"no webhook {path.rsplit('/', 1)[-1]!r}"))
- else:
- self._unary(h, body, lambda call, raw: _webhook_output(hook(call, from_wire(WebhookRequest, raw))))
- return
- m = _ROUTE.match(path)
- if method == "POST" and m:
- point, cid, action = m.groups()
- if self._route(h, point, cid, action, body):
- return
- h._send_error(PluginError(ErrorCode.NOT_FOUND, f"no endpoint {method} {path}"))
-
- def _route(self, h: "_Handler", point: str, cid: str, action: str, body: bytes) -> bool:
- if point == "websearch" and action == "search":
- fn = self._web_search.get(cid)
- if fn is None:
- h._send_error(PluginError(ErrorCode.NOT_FOUND, f"no web search provider {cid!r}"))
- else:
- self._unary(h, body, lambda call, raw: {"results": list(fn(call, from_wire(SearchInput, raw)) or [])})
- return True
- if point == "parsers" and action == "parse":
- fn = self._parsers.get(cid)
- if fn is None:
- h._send_error(PluginError(ErrorCode.NOT_FOUND, f"no parser {cid!r}"))
- else:
- self._unary(h, body, lambda call, raw: _parse_output(fn(call, from_wire(ParseInput, raw))))
- return True
- if point == "connectors" and action in ("validate", "list-resources", "resolve-ancestors", "fetch"):
- c = self._connectors.get(cid)
- if c is None:
- h._send_error(PluginError(ErrorCode.NOT_FOUND, f"no connector {cid!r}"))
- elif action == "fetch":
- self._fetch(h, c, body)
- else:
- self._unary(h, body, lambda call, raw: _connector_call(c, action, call, raw), empty=action == "validate")
- return True
- return False
-
- def _answer_mcp(self, call: Call, sid: str, server: dict, req: dict) -> dict:
- resp: dict = {"jsonrpc": "2.0"}
- if "id" in req:
- resp["id"] = req["id"]
- method = req.get("method", "")
- params = req.get("params") or {}
- if method == "initialize":
- resp["result"] = {
- "protocolVersion": MCP_PROTOCOL_VERSION,
- "capabilities": {"tools": {}},
- "serverInfo": {"name": f"{self.id}/{sid}", "version": self.version},
- }
- elif method == "ping":
- resp["result"] = {}
- # Tools only: clients that list resources or prompts anyway get none.
- elif method == "resources/list":
- resp["result"] = {"resources": []}
- elif method == "resources/templates/list":
- resp["result"] = {"resourceTemplates": []}
- elif method == "prompts/list":
- resp["result"] = {"prompts": []}
- elif method == "tools/list":
- resp["result"] = {"tools": [server[n][0] for n in sorted(server)]}
- elif method == "tools/call":
- entry = server.get(params.get("name", ""))
- if entry is None:
- resp["error"] = {"code": -32602, "message": f"no tool {params.get('name', '')}"}
- else:
- result = to_wire(self._call_tool(call, entry[1], params.get("arguments") or {}))
- result.setdefault("content", [])
- resp["result"] = result
- elif "id" in req:
- resp["error"] = {"code": -32601, "message": f"method {method} is not supported"}
- return resp
-
- def _call_tool(self, call: Call, fn: ToolHandler, args: dict) -> ToolResult:
- try:
- out = fn(call, args)
- except PluginError as e:
- return ToolResult.error(e.message)
- except Exception as e: # noqa: BLE001 - the model sees the failure
- self.logger.error("tool failed: %s", "".join(traceback.format_exception(type(e), e, e.__traceback__)))
- return ToolResult.error(str(e) or type(e).__name__)
- if isinstance(out, ToolResult):
- if not out.content:
- out.content = []
- return out
- if isinstance(out, str):
- return ToolResult.text(out)
- return ToolResult.structured(out)
-
- def _unary(self, h: "_Handler", body: bytes, fn: Callable[[Call, Any], Any], empty: bool = False) -> None:
- try:
- env = _envelope(body)
- out = fn(Call(env), env.get("input"))
- h._send_json(200, {"output": {} if empty else to_wire(out)})
- except Exception as e: # noqa: BLE001 - every failure becomes a protocol error
- h._send_error(self._as_error(e))
-
- def _fetch(self, h: "_Handler", c: Any, body: bytes) -> None:
- try:
- env = _envelope(body)
- call = Call(env)
- cfg = _connector_config(call)
- inp = from_wire(FetchInput, env.get("input"))
- except Exception as e: # noqa: BLE001
- h._send_error(self._as_error(e))
- return
- s = Stream(h)
- try:
- cursor = c.fetch(call, cfg, inp, s)
- except StreamClosed:
- return
- except Exception as e: # noqa: BLE001
- s._fail(self._as_error(e))
- return
- s._end(cursor)
-
- def _as_error(self, e: Exception) -> PluginError:
- if isinstance(e, PluginError):
- return e
- self.logger.error("plugin call failed: %s", "".join(traceback.format_exception(type(e), e, e.__traceback__)))
- return PluginError(ErrorCode.INTERNAL, str(e) or type(e).__name__)
-
- # Serving.
-
- def serve(self) -> None:
- """Runs the plugin until SIGTERM or SIGINT, then drains calls in
- flight. Started by the plugin host it listens where the host says
- and prints the handshake; otherwise it serves as a remote plugin on
- WEKNORA_PLUGIN_ADDR, checking signatures with WEKNORA_PLUGIN_SECRET."""
- server, handshake = self._listen()
- stop = threading.Event()
-
- def on_signal(*_: Any) -> None:
- stop.set()
-
- for sig in (signal.SIGTERM, signal.SIGINT):
- try:
- signal.signal(sig, on_signal)
- except ValueError: # not the main thread
- pass
- t = threading.Thread(target=server.serve_forever, kwargs={"poll_interval": 0.2}, daemon=True)
- t.start()
- if handshake:
- # The host waits for this exact line; everything else on stdout
- # is treated as log output.
- print(handshake, flush=True)
- else:
- self.logger.info("plugin serving on %s", _describe(server))
- stop.wait()
- server.shutdown()
- server.drain(self.shutdown_timeout)
- server.server_close()
-
- def _listen(self) -> Tuple["_Server", Optional[str]]:
- sock_path = os.environ.get(p.ENV_SOCKET, "")
- if sock_path:
- network = os.environ.get(p.ENV_NETWORK) or "unix"
- token = os.environ.get(p.ENV_TOKEN, "")
- if not token:
- raise RuntimeError(f"{p.ENV_SOCKET} is set without {p.ENV_TOKEN}")
- auth = _bearer(token)
- if network == "unix":
- try:
- os.remove(sock_path)
- except FileNotFoundError:
- pass
- server: _Server = _UnixServer(sock_path, self, auth)
- return server, p.handshake_line("unix", sock_path)
- server = _TCPServer(_split_addr(sock_path), self, auth)
- host, port = server.server_address[:2]
- return server, p.handshake_line("tcp", f"{host}:{port}")
- secret = os.environ.get(p.ENV_SECRET, "")
- if not secret:
- raise RuntimeError(f"a remote plugin needs {p.ENV_SECRET} (the secret shown when it was registered)")
- addr = os.environ.get(p.ENV_ADDR) or ":8080"
- return _TCPServer(_split_addr(addr), self, _signed(secret.encode())), None
-
- def test_server(self, auth: Optional[Callable[["_Handler", bytes], Optional[str]]] = None) -> "_Server":
- """Serves the plugin on a free loopback port without authentication
- (or with the given check), for tests. Call shutdown() when done."""
- server = _TCPServer(("127.0.0.1", 0), self, auth or (lambda h, b: None))
- threading.Thread(target=server.serve_forever, kwargs={"poll_interval": 0.1}, daemon=True).start()
- return server
-
-
-def _envelope(body: bytes) -> dict:
- try:
- env = json.loads(body or b"{}")
- except ValueError as e:
- raise PluginError(ErrorCode.BAD_REQUEST, f"decode envelope: {e}") from None
- if not isinstance(env, dict):
- raise PluginError(ErrorCode.BAD_REQUEST, "decode envelope: not an object")
- return env
-
-
-def _connector_config(call: Call) -> ConnectorConfig:
- try:
- return from_wire(ConnectorConfig, call.instance)
- except (TypeError, ValueError) as e:
- raise PluginError(ErrorCode.BAD_REQUEST, f"decode connector config: {e}") from None
-
-
-def _connector_call(c: Any, action: str, call: Call, raw: Any) -> Any:
- cfg = _connector_config(call)
- raw = raw or {}
- if action == "validate":
- c.validate(call, cfg)
- return None
- if action == "list-resources":
- return {"resources": list(c.list_resources(call, cfg, raw.get("parentId") or "") or [])}
- resolve = getattr(c, "resolve_ancestors", None)
- if resolve is None:
- return {"ancestors": []}
- return {"ancestors": list(resolve(call, cfg, list(raw.get("resourceIds") or [])) or [])}
-
-
-def _options_output(out: Any) -> list:
- from .types import Option
-
- result = []
- for o in out or []:
- if isinstance(o, (tuple, list)) and len(o) == 2:
- o = Option(value=o[0], label=str(o[1]))
- result.append(o)
- return result
-
-
-def _webhook_output(out: Any) -> Any:
- if not isinstance(out, WebhookResponse):
- import json as _json
-
- out = WebhookResponse(content_type="application/json", body=_json.dumps(out).encode())
- wire: dict = {"status": out.status or 200}
- if out.content_type:
- wire["contentType"] = out.content_type
- if out.body:
- wire["body"] = to_wire(out.body)
- return wire
-
-
-def _ui_output(out: Any) -> Any:
- if not isinstance(out, UIResponse):
- out = UIResponse(body=out)
- wire: dict = {"status": out.status or 200}
- if out.body is not None:
- wire["body"] = to_wire(out.body)
- return wire
-
-
-def _parse_output(out: Any) -> Any:
- if isinstance(out, str):
- return ParseOutput(markdown=out)
- return out
-
-
-# Authentication: a check returns an error message, or None to let the
-# request through.
-
-
-def _bearer(token: str) -> Callable[["_Handler", bytes], Optional[str]]:
- want = "Bearer " + token
-
- def check(h: "_Handler", _: bytes) -> Optional[str]:
- if hmac.compare_digest(h.headers.get("Authorization", ""), want):
- return None
- return "missing or wrong host token"
-
- return check
-
-
-def _signed(secret: bytes) -> Callable[["_Handler", bytes], Optional[str]]:
- def check(h: "_Handler", body: bytes) -> Optional[str]:
- try:
- p.verify_signature(
- secret, h.headers.get(p.TIMESTAMP_HEADER, ""), h.headers.get(p.SIGNATURE_HEADER, ""), body
- )
- except ValueError as e:
- return str(e)
- return None
-
- return check
-
-
-class _Handler(BaseHTTPRequestHandler):
- protocol_version = "HTTP/1.1"
- server: "_Server"
-
- def _handle(self) -> None:
- length = int(self.headers.get("Content-Length") or 0)
- body = self.rfile.read(length) if length > 0 else b""
- path = self.path.split("?", 1)[0]
- problem = self.server.auth(self, body)
- if problem is not None:
- self._send_error(PluginError(ErrorCode.UNAUTHORIZED, problem))
- return
- with self.server.in_flight():
- self.server.plugin._dispatch(self, self.command, path, body)
-
- do_GET = do_POST = do_PUT = do_DELETE = do_PATCH = _handle
-
- def _send_json(self, status: int, value: Any) -> None:
- raw = json.dumps(value, separators=(",", ":")).encode()
- self.send_response(status)
- self.send_header("Content-Type", "application/json")
- self.send_header("Content-Length", str(len(raw)))
- self.end_headers()
- self.wfile.write(raw)
-
- def _send_error(self, err: PluginError) -> None:
- self._send_json(err.code.http_status, {"error": err.to_wire()})
-
- def address_string(self) -> str:
- return str(self.client_address[0]) if self.client_address else "host"
-
- def log_message(self, format: str, *args: Any) -> None:
- pass
-
-
-class _Server(socketserver.ThreadingMixIn, HTTPServer):
- daemon_threads = True
-
- def __init__(self, address: Any, plugin: Plugin, auth: Callable[[_Handler, bytes], Optional[str]]) -> None:
- self.plugin = plugin
- self.auth = auth
- self._active = 0
- self._active_lock = threading.Condition()
- super().__init__(address, _Handler)
-
- @contextlib.contextmanager
- def in_flight(self) -> Iterator[None]:
- """Counts a call being answered; idle keep-alive connections do not
- hold up a shutdown."""
- with self._active_lock:
- self._active += 1
- try:
- yield
- finally:
- with self._active_lock:
- self._active -= 1
- self._active_lock.notify_all()
-
- def handle_error(self, request: Any, client_address: Any) -> None:
- # A caller that went away mid-answer is routine.
- self.plugin.logger.debug("connection error", exc_info=True)
-
- def drain(self, timeout: float) -> None:
- """Waits for calls in flight, up to timeout seconds."""
- end = time.monotonic() + timeout
- with self._active_lock:
- while self._active and time.monotonic() < end:
- self._active_lock.wait(end - time.monotonic())
-
-
-class _TCPServer(_Server):
- address_family = socket.AF_INET
-
- def __init__(self, address: Tuple[str, int], *args: Any) -> None:
- if ":" in address[0]:
- self.address_family = socket.AF_INET6
- super().__init__(address, *args)
-
- def server_bind(self) -> None:
- # HTTPServer.server_bind looks the host up with getfqdn, a reverse
- # DNS query that can stall startup for seconds; nothing needs it.
- socketserver.TCPServer.server_bind(self)
- self.server_name, self.server_port = str(self.server_address[0]), int(self.server_address[1])
-
-
-class _UnixServer(_Server):
- address_family = getattr(socket, "AF_UNIX", socket.AF_INET)
-
- def server_bind(self) -> None:
- socketserver.TCPServer.server_bind(self)
- self.server_name, self.server_port = "plugin", 0
-
- def server_close(self) -> None:
- super().server_close()
- try:
- os.remove(self.server_address)
- except (OSError, TypeError):
- pass
-
-
-def _split_addr(addr: str) -> Tuple[str, int]:
- host, _, port = addr.rpartition(":")
- host = host.strip("[]")
- return (host or "0.0.0.0", int(port))
-
-
-def _describe(server: _Server) -> str:
- addr = server.server_address
- return f"{addr[0]}:{addr[1]}" if isinstance(addr, tuple) else str(addr)
diff --git a/pluginsdk/python/src/weknora_plugin/protocol.py b/pluginsdk/python/src/weknora_plugin/protocol.py
deleted file mode 100644
index 45e41d2a6..000000000
--- a/pluginsdk/python/src/weknora_plugin/protocol.py
+++ /dev/null
@@ -1,149 +0,0 @@
-"""Version 1 of the WeKnora extension protocol, as the Go package pluginapi
-defines it: constants, errors, request signatures and the handshake line."""
-
-from __future__ import annotations
-
-import hashlib
-import hmac
-import time
-from enum import Enum
-from typing import Dict, Optional
-
-PROTOCOL_VERSION = "1"
-API_VERSION = "weknora.plugin/v1"
-
-PROTOCOL_HEADER = "X-WeKnora-Protocol"
-REQUEST_ID_HEADER = "X-Request-Id"
-SIGNATURE_HEADER = "X-WeKnora-Signature"
-TIMESTAMP_HEADER = "X-WeKnora-Timestamp"
-NDJSON_CONTENT_TYPE = "application/x-ndjson"
-
-# Environment a host plugin is started with.
-ENV_SOCKET = "WEKNORA_PLUGIN_SOCKET"
-ENV_NETWORK = "WEKNORA_PLUGIN_NETWORK"
-ENV_TOKEN = "WEKNORA_PLUGIN_TOKEN"
-ENV_PLUGIN_ID = "WEKNORA_PLUGIN_ID"
-ENV_PLUGIN_VERSION = "WEKNORA_PLUGIN_VERSION"
-# Environment of a remote plugin.
-ENV_ADDR = "WEKNORA_PLUGIN_ADDR"
-ENV_SECRET = "WEKNORA_PLUGIN_SECRET"
-
-# How old a signed request may be, in seconds.
-MAX_CLOCK_SKEW = 5 * 60
-
-HOST_KV_PATH = "/api/v1/plugin-host/kv"
-HOST_KV_LIST_PATH = "/api/v1/plugin-host/kv/list"
-
-
-class ErrorCode(str, Enum):
- """Classifies a failed call; WeKnora reacts to each differently."""
-
- INVALID_CONFIG = "invalid_config"
- UNAUTHORIZED = "unauthorized"
- NOT_FOUND = "not_found"
- RATE_LIMITED = "rate_limited"
- UNAVAILABLE = "unavailable"
- BAD_REQUEST = "bad_request"
- INTERNAL = "internal"
-
- @property
- def http_status(self) -> int:
- return _STATUS.get(self, 500)
-
-
-_STATUS = {
- ErrorCode.INVALID_CONFIG: 400,
- ErrorCode.BAD_REQUEST: 400,
- ErrorCode.UNAUTHORIZED: 401,
- ErrorCode.NOT_FOUND: 404,
- ErrorCode.RATE_LIMITED: 429,
- ErrorCode.UNAVAILABLE: 503,
-}
-
-
-class PluginError(Exception):
- """A failed call. Raise it to choose the code WeKnora sees; any other
- exception becomes ``internal``. Unavailable and rate-limited errors are
- retryable unless told otherwise."""
-
- def __init__(
- self,
- code: ErrorCode | str,
- message: str,
- *,
- retryable: Optional[bool] = None,
- fields: Optional[Dict[str, str]] = None,
- retry_after: int = 0,
- ) -> None:
- super().__init__(message)
- try:
- self.code = ErrorCode(code)
- except ValueError:
- self.code = ErrorCode.INTERNAL
- self.message = message
- if retryable is None:
- retryable = self.code in (ErrorCode.UNAVAILABLE, ErrorCode.RATE_LIMITED)
- self.retryable = retryable
- self.fields = fields or {}
- self.retry_after = retry_after
-
- def __str__(self) -> str:
- return f"{self.code.value}: {self.message}"
-
- def to_wire(self) -> dict:
- out: dict = {"code": self.code.value, "message": self.message}
- if self.retryable:
- out["retryable"] = True
- details: dict = {}
- if self.fields:
- details["fields"] = dict(self.fields)
- if self.retry_after:
- details["retryAfter"] = self.retry_after
- if details:
- out["details"] = details
- return out
-
- @classmethod
- def from_wire(cls, body: dict) -> "PluginError":
- details = body.get("details") or {}
- return cls(
- body.get("code") or ErrorCode.INTERNAL,
- body.get("message") or "",
- retryable=bool(body.get("retryable")),
- fields=details.get("fields"),
- retry_after=int(details.get("retryAfter") or 0),
- )
-
-
-def invalid_config(message: str, fields: Dict[str, str]) -> PluginError:
- """Reports configuration problems keyed by dotted field path; WeKnora
- shows them on the form."""
- return PluginError(ErrorCode.INVALID_CONFIG, message, fields=fields)
-
-
-def sign(secret: bytes, timestamp: int, body: bytes) -> str:
- """The signature of a request body at a unix timestamp: hex HMAC-SHA256
- over ``"<timestamp>.<body>"``."""
- mac = hmac.new(secret, f"{timestamp}.".encode(), hashlib.sha256)
- mac.update(body)
- return mac.hexdigest()
-
-
-def verify_signature(
- secret: bytes, timestamp: str, signature: str, body: bytes, now: Optional[float] = None
-) -> None:
- """Checks a signed request; raises ValueError when it does not hold."""
- try:
- ts = int(timestamp)
- except (TypeError, ValueError):
- raise ValueError(f"bad {TIMESTAMP_HEADER}") from None
- now = time.time() if now is None else now
- if abs(now - ts) > MAX_CLOCK_SKEW:
- raise ValueError("request timestamp is outside the allowed clock skew")
- if not hmac.compare_digest(sign(secret, ts, body), signature or ""):
- raise ValueError("bad signature")
-
-
-def handshake_line(network: str, address: str) -> str:
- """The line a host plugin prints on stdout once it is ready."""
- return "|".join(["WEKNORA_PLUGIN", PROTOCOL_VERSION, network, address])
diff --git a/pluginsdk/python/src/weknora_plugin/types.py b/pluginsdk/python/src/weknora_plugin/types.py
deleted file mode 100644
index 50d48a2ed..000000000
--- a/pluginsdk/python/src/weknora_plugin/types.py
+++ /dev/null
@@ -1,376 +0,0 @@
-"""The protocol's messages as dataclasses, and their JSON form: camelCase
-keys, bytes as base64, times as RFC 3339. Optional fields left at their
-default are omitted, like Go's omitempty; unknown fields are ignored."""
-
-from __future__ import annotations
-
-import base64
-import dataclasses
-import re
-from dataclasses import dataclass, field
-from datetime import datetime, timezone
-from typing import Any, Dict, List, Optional, Type, TypeVar, Union
-
-T = TypeVar("T")
-
-
-def _camel(name: str) -> str:
- head, *rest = name.split("_")
- return head + "".join(p[:1].upper() + p[1:] for p in rest)
-
-
-_FRACTION = re.compile(r"\.(\d+)")
-
-
-def parse_time(value: Any) -> Optional[datetime]:
- """Reads an RFC 3339 time as Go writes it (nanoseconds, a Z suffix)."""
- if not value:
- return None
- if isinstance(value, datetime):
- return value
- # Go writes 0 to 9 fraction digits; Python before 3.11 reads exactly 3
- # or 6.
- s = _FRACTION.sub(lambda m: "." + m.group(1)[:6].ljust(6, "0"), str(value).replace("Z", "+00:00"), count=1)
- return datetime.fromisoformat(s)
-
-
-def format_time(value: datetime) -> str:
- if value.tzinfo is None:
- value = value.replace(tzinfo=timezone.utc)
- return value.astimezone(timezone.utc).isoformat().replace("+00:00", "Z")
-
-
-def to_wire(value: Any) -> Any:
- """Converts a message (or anything JSON-like holding messages) to its
- JSON form."""
- if dataclasses.is_dataclass(value) and not isinstance(value, type):
- out = {}
- for f in dataclasses.fields(value):
- v = getattr(value, f.name)
- optional = f.default is not dataclasses.MISSING or f.default_factory is not dataclasses.MISSING
- if optional and (v is None or v is False or v == "" or v == b"" or v == [] or v == {}):
- continue
- out[_camel(f.name)] = to_wire(v)
- return out
- if isinstance(value, (bytes, bytearray)):
- return base64.b64encode(value).decode()
- if isinstance(value, datetime):
- return format_time(value)
- if isinstance(value, dict):
- return {k: to_wire(v) for k, v in value.items()}
- if isinstance(value, (list, tuple)):
- return [to_wire(v) for v in value]
- return value
-
-
-def from_wire(cls: Type[T], data: Any) -> T:
- """Builds a message from its JSON form."""
- if isinstance(data, cls):
- return data
- data = data or {}
- kwargs = {}
- hints = _hints(cls)
- for f in dataclasses.fields(cls):
- key = _camel(f.name)
- if key not in data:
- continue
- kwargs[f.name] = _convert(hints[f.name], data[key])
- return cls(**kwargs)
-
-
-def _hints(cls: type) -> Dict[str, Any]:
- import typing
-
- return typing.get_type_hints(cls)
-
-
-def _convert(hint: Any, value: Any) -> Any:
- import typing
-
- if value is None:
- return None
- origin = typing.get_origin(hint)
- args = typing.get_args(hint)
- if origin is Union:
- inner = [a for a in args if a is not type(None)]
- if len(inner) == 1:
- return _convert(inner[0], value)
- if bytes in inner and isinstance(value, str):
- return base64.b64decode(value)
- return value
- if hint is bytes:
- return base64.b64decode(value) if isinstance(value, str) else bytes(value)
- if hint is datetime:
- return parse_time(value)
- if dataclasses.is_dataclass(hint):
- return from_wire(hint, value)
- if origin in (list, List) and args and isinstance(value, list):
- return [_convert(args[0], v) for v in value]
- return value
-
-
-@dataclass
-class ConnectorConfig:
- """A data source's instance configuration. Credentials are decrypted;
- do not keep them beyond the call."""
-
- credentials: Dict[str, Any] = field(default_factory=dict)
- settings: Dict[str, Any] = field(default_factory=dict)
- resource_ids: List[str] = field(default_factory=list)
-
-
-@dataclass
-class Resource:
- """Something a data source can sync: a space, a folder, a feed."""
-
- external_id: str
- name: str
- type: str = ""
- description: str = ""
- url: str = ""
- modified_at: Optional[datetime] = None
- parent_id: str = ""
- has_children: bool = False
- metadata: Optional[Dict[str, Any]] = None
-
-
-@dataclass
-class Cursor:
- """A connector's resumable state; WeKnora stores it and hands it back.
- A checkpoint must be a complete snapshot of progress so far."""
-
- last_sync_time: Optional[datetime] = None
- state: Optional[Dict[str, Any]] = None
-
-
-FETCH_FULL = "full"
-FETCH_INCREMENTAL = "incremental"
-
-
-@dataclass
-class FetchInput:
- """Starts or resumes a sync; cursor None starts from scratch."""
-
- mode: str = FETCH_FULL
- cursor: Optional[Cursor] = None
- resource_ids: List[str] = field(default_factory=list)
-
-
-@dataclass
-class FetchedItem:
- """One document fetched from the source. Content is the body, Markdown
- preferred; a str is sent as UTF-8."""
-
- external_id: str
- title: str
- content: Optional[Union[bytes, str]] = None
- content_type: str = ""
- file_name: str = ""
- url: str = ""
- updated_at: Optional[datetime] = None
- created_at: Optional[datetime] = None
- metadata: Optional[Dict[str, str]] = None
- is_deleted: bool = False
- source_resource_id: str = ""
-
- def __post_init__(self) -> None:
- if isinstance(self.content, str):
- self.content = self.content.encode()
-
-
-@dataclass
-class SearchInput:
- """A web search. A provider that cannot honour region or freshness must
- fail rather than ignore it."""
-
- query: str = ""
- max_results: int = 0
- include_date: bool = False
- region: str = ""
- freshness: str = ""
-
-
-@dataclass
-class SearchResult:
- title: str
- url: str
- snippet: str = ""
- content: str = ""
- age: str = ""
- published_at: Optional[datetime] = None
-
-
-@dataclass
-class ParseInput:
- """One document to parse: its bytes, or a URL. file_type is the
- lower-case extension without the dot."""
-
- file_name: str = ""
- file_type: str = ""
- content: bytes = b""
- url: str = ""
- title: str = ""
-
-
-@dataclass
-class ParsedImage:
- """An image the Markdown references as ![](original_ref)."""
-
- original_ref: str
- data: bytes
- mime_type: str = ""
-
-
-@dataclass
-class ParseOutput:
- """A document as Markdown; WeKnora chunks it."""
-
- markdown: str
- images: List[ParsedImage] = field(default_factory=list)
- metadata: Optional[Dict[str, str]] = None
-
-
-@dataclass
-class KVEntry:
- key: str
- value: Any = None
- expires_at: Optional[datetime] = None
- updated_at: Optional[datetime] = None
-
-
-@dataclass
-class KVList:
- entries: List[KVEntry] = field(default_factory=list)
- next: str = ""
-
-
-@dataclass
-class UIRequest:
- """A request one of the plugin's pages made through the WeKnora bridge.
- mount is "<point>/<id>" ("pages/links"); method and path are the page's
- own routing; role is the caller's workspace role."""
-
- mount: str = ""
- method: str = ""
- path: str = ""
- body: Any = None
- role: str = ""
-
-
-@dataclass
-class UIResponse:
- """What the page receives: an HTTP-style status and a JSON body."""
-
- body: Any = None
- status: int = 200
-
-
-# Event types a plugin can subscribe to in permissions.events.
-EVENT_KNOWLEDGE_INGESTED = "knowledge.ingested"
-EVENT_KNOWLEDGE_FAILED = "knowledge.failed"
-EVENT_KNOWLEDGE_DELETED = "knowledge.deleted"
-EVENT_CHAT_ANSWERED = "chat.answered"
-
-
-@dataclass
-class EventDelivery:
- """One event. Deliveries repeat (at least once): be idempotent on id.
- data is the event's payload as a dict (see the protocol for each type)."""
-
- id: str = ""
- type: str = ""
- occurred_at: Optional[datetime] = None
- attempt: int = 1
- data: Any = None
-
-
-@dataclass
-class WebhookRequest:
- """An inbound call to one of the plugin's webhooks. body is the raw
- bytes; headers carry one value each."""
-
- method: str = ""
- path: str = "/"
- query: str = ""
- headers: Dict[str, str] = field(default_factory=dict)
- body: bytes = b""
-
- def json(self) -> Any:
- import json as _json
-
- return _json.loads(self.body or b"null")
-
-
-@dataclass
-class WebhookResponse:
- """What the caller receives. A str body is sent as UTF-8."""
-
- status: int = 200
- content_type: str = ""
- body: Optional[Union[bytes, str]] = None
-
- def __post_init__(self) -> None:
- if isinstance(self.body, str):
- self.body = self.body.encode()
-
-
-@dataclass
-class OptionsInput:
- """Asks for one form field's choices (x-options). field is its dotted
- path; scope is system, tenant or instance; contribution names the
- instance's contribution ("connectors/jira"); query is what the user
- typed."""
-
- field: str = ""
- scope: str = ""
- contribution: str = ""
- query: str = ""
-
-
-@dataclass
-class Option:
- """One choice of a form field."""
-
- value: Any
- label: str
- description: str = ""
-
-
-@dataclass
-class ToolContent:
- """One content block of a tool result: text, or an image (base64
- data)."""
-
- type: str = "text"
- text: str = ""
- data: str = ""
- mime_type: str = ""
-
-
-@dataclass
-class ToolResult:
- """What a tool returns. The model reads content; structured_content is
- data for the tool's result view (toolViews in plugin.yaml). is_error
- marks a failure the model should see."""
-
- content: List[ToolContent] = field(default_factory=list)
- structured_content: Any = None
- is_error: bool = False
-
- @staticmethod
- def text(text: str) -> "ToolResult":
- return ToolResult(content=[ToolContent(text=text)])
-
- @staticmethod
- def structured(data: Any, text: str = "") -> "ToolResult":
- """Data for the result view, and text for the model (the data as
- JSON when text is empty)."""
- if not text:
- import json
-
- text = json.dumps(to_wire(data), ensure_ascii=False, separators=(",", ":"))
- return ToolResult(content=[ToolContent(text=text)], structured_content=data)
-
- @staticmethod
- def error(message: str) -> "ToolResult":
- return ToolResult(content=[ToolContent(text=message)], is_error=True)
diff --git a/pluginsdk/python/tests/fixture.py b/pluginsdk/python/tests/fixture.py
deleted file mode 100644
index 1d431c17e..000000000
--- a/pluginsdk/python/tests/fixture.py
+++ /dev/null
@@ -1,119 +0,0 @@
-"""A plugin exercising every contribution kind: the SDK's tests and the Go
-conformance suite run it."""
-
-from __future__ import annotations
-
-import os
-import sys
-import time
-
-sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src"))
-
-from weknora_plugin import ( # noqa: E402
- Cursor,
- ErrorCode,
- FetchedItem,
- ParsedImage,
- ParseOutput,
- Plugin,
- PluginError,
- Resource,
- SearchResult,
- UIResponse,
- WebhookResponse,
- invalid_config,
-)
-
-seen_events = []
-
-plugin = Plugin("acme.fixture", "1.0.0")
-
-
-def validate(call):
- if call.tenant.get("region") == "mars":
- raise invalid_config("unknown region", {"region": "no such region"})
-
-
[email protected]_search("echo")
-def search(call, q):
- if q.query == "down":
- raise PluginError(ErrorCode.UNAVAILABLE, "upstream is down")
- if q.query == "crash":
- raise RuntimeError("boom")
- if q.query == "slow":
- time.sleep(0.5)
- return [SearchResult(title=q.query, url=f"https://example.com/?tenant={call.tenant_id}", snippet=call.locale)]
-
-
-def parse(call, doc):
- text = doc.content.decode("utf-8", "replace")
- return ParseOutput(
- markdown=text.upper() + "\n\n![](img/dot.png)",
- images=[ParsedImage(original_ref="img/dot.png", data=b"\x89PNG", mime_type="image/png")],
- metadata={"fileType": doc.file_type},
- )
-
-
-class Notes:
- def validate(self, call, cfg):
- if cfg.credentials.get("token") == "bad":
- raise PluginError(ErrorCode.UNAUTHORIZED, "token rejected")
-
- def list_resources(self, call, cfg, parent_id):
- return [Resource(external_id="inbox", name="Inbox", has_children=not parent_id)]
-
- def fetch(self, call, cfg, inp, stream):
- start = int((inp.cursor.state or {}).get("after", 0)) if inp.cursor else 0
- if cfg.settings.get("fail") == "before":
- raise PluginError(ErrorCode.UNAUTHORIZED, "token rejected")
- for i in range(start, start + 3):
- stream.item(FetchedItem(external_id=f"n{i}", title=f"Note {i}", content=f"# Note {i}"))
- if cfg.settings.get("fail") == "during":
- raise PluginError(ErrorCode.RATE_LIMITED, "slow down", retry_after=7)
- stream.checkpoint(Cursor(state={"after": start + 3}))
- stream.progress("done")
- return Cursor(state={"after": start + 3})
-
-
-def on_event(call, ev):
- if ev.type == "knowledge.failed":
- raise PluginError(ErrorCode.UNAVAILABLE, "try later")
- seen_events.append((ev.id, ev.type, ev.attempt, call.tenant_id, (ev.data or {}).get("knowledgeId")))
-
-
[email protected]("issues", "search", "Search issues", {"type": "object", "properties": {"q": {"type": "string"}}}, read_only=True)
-def search_issues(call, args):
- if args.get("q") == "boom":
- raise PluginError(ErrorCode.UNAUTHORIZED, "token expired")
- if args.get("q") == "text":
- return "plain"
- return {"q": args.get("q"), "tenant": call.tenant_id}
-
-
[email protected]("projects")
-def projects(call, inp):
- if not call.tenant.get("token"):
- raise invalid_config("enter a token first", {"token": "required"})
- return [("p1", f"Project {inp.query}"), ("p2", "Other")]
-
-
-def inbox(call, req):
- if req.headers.get("X-Signature") != "ok":
- return WebhookResponse(status=401, body="bad signature")
- return {"path": req.path, "got": req.json(), "tenant": call.tenant_id}
-
-
-def ui(call, req):
- if req.path == "/missing":
- return UIResponse(status=404, body={"error": "no such thing"})
- return {"mount": req.mount, "method": req.method, "path": req.path, "role": req.role, "body": req.body}
-
-
-if __name__ == "__main__":
- plugin.serve()
diff --git a/pluginsdk/python/tests/test_plugin.py b/pluginsdk/python/tests/test_plugin.py
deleted file mode 100644
index bf78d4abc..000000000
--- a/pluginsdk/python/tests/test_plugin.py
+++ /dev/null
@@ -1,434 +0,0 @@
-from __future__ import annotations
-
-import base64
-import http.client
-import json
-import os
-import socket
-import subprocess
-import sys
-import threading
-import time
-import unittest
-from datetime import datetime, timezone
-from http.server import BaseHTTPRequestHandler, HTTPServer
-from urllib.parse import parse_qs, urlparse
-
-HERE = os.path.dirname(os.path.abspath(__file__))
-sys.path.insert(0, HERE)
-
-from fixture import plugin # noqa: E402
-from weknora_plugin import ErrorCode, Host, PluginError # noqa: E402
-from weknora_plugin import protocol as p # noqa: E402
-from weknora_plugin.types import Cursor, FetchInput, from_wire, parse_time, to_wire # noqa: E402
-
-ENVELOPE = {"context": {"tenantId": 7, "locale": "zh-CN", "requestId": "r1"}}
-
-
-class Client:
- def __init__(self, conn_factory):
- self.conn_factory = conn_factory
-
- def request(self, method, path, body=None, headers=None):
- conn = self.conn_factory()
- raw = b"" if body is None else (body if isinstance(body, bytes) else json.dumps(body).encode())
- conn.request(method, path, body=raw if method != "GET" else None, headers=headers or {})
- resp = conn.getresponse()
- data = resp.read()
- conn.close()
- return resp.status, resp.getheader("Content-Type"), data
-
- def call(self, path, input=None, config=None, **kw):
- env = dict(ENVELOPE)
- if input is not None:
- env["input"] = input
- if config is not None:
- env["config"] = config
- status, _, data = self.request("POST", path, env, **kw)
- return status, json.loads(data)
-
- def stream(self, path, input=None, config=None):
- env = dict(ENVELOPE, input=input or {}, config=config or {})
- status, ctype, data = self.request("POST", path, env)
- if ctype != p.NDJSON_CONTENT_TYPE:
- return status, json.loads(data)
- return status, [json.loads(line) for line in data.splitlines() if line]
-
-
-class PluginTest(unittest.TestCase):
- @classmethod
- def setUpClass(cls):
- cls.server = plugin.test_server()
- host, port = cls.server.server_address[:2]
- cls.c = Client(lambda: http.client.HTTPConnection(host, port, timeout=10))
-
- @classmethod
- def tearDownClass(cls):
- cls.server.shutdown()
- cls.server.server_close()
-
- def test_manifest_and_health(self):
- status, _, data = self.c.request("GET", "/v1/manifest")
- self.assertEqual(status, 200)
- self.assertEqual(
- json.loads(data),
- {
- "id": "acme.fixture",
- "version": "1.0.0",
- "apiVersion": "weknora.plugin/v1",
- "contributes": {
- "webSearch": ["echo"],
- "connectors": ["notes"],
- "parsers": ["upper"],
- "webhooks": ["inbox"],
- "options": ["projects"],
- "mcpServers": ["issues"],
- "ui": ["request"],
- "events": ["handler"],
- },
- },
- )
- self.assertEqual(json.loads(self.c.request("GET", "/v1/health")[2]), {"status": "ok"})
-
- def test_web_search(self):
- status, body = self.c.call("/v1/websearch/echo/search", {"query": "hi", "maxResults": 3})
- self.assertEqual(status, 200)
- self.assertEqual(
- body["output"]["results"],
- [{"title": "hi", "url": "https://example.com/?tenant=7", "snippet": "zh-CN"}],
- )
-
- def test_errors(self):
- status, body = self.c.call("/v1/websearch/echo/search", {"query": "down"})
- self.assertEqual((status, body["error"]), (503, {"code": "unavailable", "message": "upstream is down", "retryable": True}))
- status, body = self.c.call("/v1/websearch/echo/search", {"query": "crash"})
- self.assertEqual((status, body["error"]["code"], body["error"]["message"]), (500, "internal", "boom"))
- status, body = self.c.call("/v1/websearch/nope/search", {"query": "x"})
- self.assertEqual((status, body["error"]["code"]), (404, "not_found"))
- status, body = self.c.call("/v1/no-such-endpoint")
- self.assertEqual((status, body["error"]["code"]), (404, "not_found"))
- status, _, data = self.c.request("POST", "/v1/config/validate", b"{not json")
- self.assertEqual((status, json.loads(data)["error"]["code"]), (400, "bad_request"))
-
- def test_config_validate(self):
- status, body = self.c.call("/v1/config/validate", config={"tenant": {"region": "eu"}})
- self.assertEqual((status, body), (200, {"output": {}}))
- status, body = self.c.call("/v1/config/validate", config={"tenant": {"region": "mars"}})
- self.assertEqual(status, 400)
- self.assertEqual(body["error"]["details"], {"fields": {"region": "no such region"}})
-
- def test_parser(self):
- doc = base64.b64encode(b"hello").decode()
- status, body = self.c.call("/v1/parsers/upper/parse", {"fileName": "a.txt", "fileType": "txt", "content": doc})
- self.assertEqual(status, 200)
- out = body["output"]
- self.assertTrue(out["markdown"].startswith("HELLO"))
- self.assertEqual(out["images"], [{"originalRef": "img/dot.png", "data": base64.b64encode(b"\x89PNG").decode(), "mimeType": "image/png"}])
- self.assertEqual(out["metadata"], {"fileType": "txt"})
-
- def test_events(self):
- from fixture import seen_events
-
- ev = {"id": "e1", "type": "knowledge.ingested", "occurredAt": "2026-09-26T10:51:18.5Z", "attempt": 2, "data": {"knowledgeId": "k1"}}
- self.assertEqual(self.c.call("/v1/events", ev), (200, {"output": {}}))
- self.assertIn(("e1", "knowledge.ingested", 2, 7, "k1"), seen_events)
- status, body = self.c.call("/v1/events", dict(ev, type="knowledge.failed"))
- self.assertEqual((status, body["error"]["retryable"]), (503, True))
-
- def test_options(self):
- inp = {"field": "project", "scope": "tenant", "query": "x"}
- status, body = self.c.call("/v1/options/projects", inp)
- self.assertEqual((status, body["error"]["code"]), (400, "invalid_config"))
- status, body = self.c.call("/v1/options/projects", inp, {"tenant": {"token": "t"}})
- self.assertEqual(body["output"]["options"], [{"value": "p1", "label": "Project x"}, {"value": "p2", "label": "Other"}])
-
- def test_tools(self):
- def send(msg, config=None):
- status, body = self.c.call("/v1/mcp/issues", dict(msg, jsonrpc="2.0"), config)
- self.assertEqual(status, 200)
- return body["output"]
-
- init = send({"id": 1, "method": "initialize", "params": {"protocolVersion": "2025-06-18"}})
- self.assertEqual((init["id"], init["result"]["capabilities"]), (1, {"tools": {}}))
- tools = send({"id": 2, "method": "tools/list"})["result"]["tools"]
- self.assertEqual([t["name"] for t in tools], ["search"])
- self.assertEqual(tools[0]["annotations"], {"readOnlyHint": True})
- out = send({"id": 3, "method": "tools/call", "params": {"name": "search", "arguments": {"q": "login"}}})
- self.assertEqual(out["result"]["structuredContent"], {"q": "login", "tenant": 7})
- self.assertEqual(json.loads(out["result"]["content"][0]["text"]), {"q": "login", "tenant": 7})
- out = send({"id": 4, "method": "tools/call", "params": {"name": "search", "arguments": {"q": "text"}}})
- self.assertEqual(out["result"], {"content": [{"type": "text", "text": "plain"}]})
- out = send({"id": 5, "method": "tools/call", "params": {"name": "search", "arguments": {"q": "boom"}}})
- self.assertEqual(out["result"], {"content": [{"type": "text", "text": "token expired"}], "isError": True})
- self.assertEqual(send({"id": 6, "method": "tools/call", "params": {"name": "nope"}})["error"]["code"], -32602)
- self.assertEqual(send({"id": 7, "method": "resources/list"})["result"], {"resources": []})
- self.assertEqual(send({"id": 8, "method": "completion/complete"})["error"]["code"], -32601)
- self.assertNotIn("error", send({"method": "notifications/initialized"}))
- self.assertEqual(self.c.call("/v1/mcp/nope", {"jsonrpc": "2.0", "method": "ping"})[0], 404)
-
- def test_webhooks(self):
- body = base64.b64encode(b'{"a": 1}').decode()
- req = {"method": "POST", "path": "/issue", "headers": {"X-Signature": "ok"}, "body": body}
- status, out = self.c.call("/v1/webhooks/inbox", req)
- self.assertEqual(status, 200)
- self.assertEqual(out["output"]["contentType"], "application/json")
- self.assertEqual(json.loads(base64.b64decode(out["output"]["body"])), {"path": "/issue", "got": {"a": 1}, "tenant": 7})
- status, out = self.c.call("/v1/webhooks/inbox", dict(req, headers={}))
- self.assertEqual((out["output"]["status"], base64.b64decode(out["output"]["body"])), (401, b"bad signature"))
- self.assertEqual(self.c.call("/v1/webhooks/nope", req)[0], 404)
-
- def test_ui_requests(self):
- req = {"mount": "pages/links", "method": "PUT", "path": "/links", "body": {"a": 1}, "role": "admin"}
- status, body = self.c.call("/v1/ui/request", req)
- self.assertEqual(status, 200)
- self.assertEqual(body["output"], {"status": 200, "body": {**req}})
- status, body = self.c.call("/v1/ui/request", dict(req, path="/missing"))
- self.assertEqual(body["output"], {"status": 404, "body": {"error": "no such thing"}})
-
- def test_connector_unary(self):
- cfg = {"instance": {"credentials": {"token": "ok"}, "settings": {}, "resourceIds": []}}
- self.assertEqual(self.c.call("/v1/connectors/notes/validate", config=cfg), (200, {"output": {}}))
- bad = {"instance": {"credentials": {"token": "bad"}}}
- status, body = self.c.call("/v1/connectors/notes/validate", config=bad)
- self.assertEqual((status, body["error"]["code"]), (401, "unauthorized"))
- status, body = self.c.call("/v1/connectors/notes/list-resources", {}, cfg)
- self.assertEqual(body["output"], {"resources": [{"externalId": "inbox", "name": "Inbox", "hasChildren": True}]})
- status, body = self.c.call("/v1/connectors/notes/resolve-ancestors", {"resourceIds": ["x"]}, cfg)
- self.assertEqual(body["output"], {"ancestors": []})
-
- def test_fetch_streams(self):
- status, events = self.c.stream("/v1/connectors/notes/fetch", {"mode": "incremental", "cursor": {"state": {"after": 2}}})
- self.assertEqual(status, 200)
- self.assertEqual([e["type"] for e in events], ["item", "item", "item", "checkpoint", "progress", "end"])
- self.assertEqual(events[0]["data"]["externalId"], "n2")
- self.assertEqual(base64.b64decode(events[0]["data"]["content"]), b"# Note 2")
- self.assertEqual(events[-1]["data"], {"state": {"after": 5}})
-
- def test_fetch_failures(self):
- status, body = self.c.stream("/v1/connectors/notes/fetch", config={"instance": {"settings": {"fail": "before"}}})
- self.assertEqual((status, body["error"]["code"]), (401, "unauthorized"))
- status, events = self.c.stream("/v1/connectors/notes/fetch", config={"instance": {"settings": {"fail": "during"}}})
- self.assertEqual([e["type"] for e in events], ["item", "error"])
- self.assertEqual(events[1]["error"], {"code": "rate_limited", "message": "slow down", "retryable": True, "details": {"retryAfter": 7}})
-
-
-class BindTest(unittest.TestCase):
- def test_no_reverse_dns_on_bind(self):
- # getfqdn can stall startup for seconds where reverse DNS is slow.
- from unittest import mock
-
- with mock.patch("socket.getfqdn", side_effect=AssertionError("getfqdn called")):
- server = plugin.test_server()
- server.shutdown()
- server.server_close()
-
-
-class WireTest(unittest.TestCase):
- def test_times(self):
- t = parse_time("2026-09-25T16:22:58.135616789Z")
- self.assertEqual(t, datetime(2026, 9, 25, 16, 22, 58, 135616, tzinfo=timezone.utc))
- self.assertEqual(to_wire(Cursor(last_sync_time=t)), {"lastSyncTime": "2026-09-25T16:22:58.135616Z"})
- # Go trims trailing zeros, so any number of digits arrives.
- for raw, micros in (("2026-09-26T10:51:18.63664+08:00", 636640), ("2026-09-26T10:51:18.5Z", 500000)):
- self.assertEqual(parse_time(raw).microsecond, micros)
- naive = datetime(2026, 1, 2, 3, 4, 5)
- self.assertEqual(to_wire(naive), "2026-01-02T03:04:05Z")
-
- def test_from_wire(self):
- inp = from_wire(FetchInput, {"mode": "incremental", "cursor": {"lastSyncTime": "2026-01-02T03:04:05Z", "state": {"a": 1}}, "unknown": 1})
- self.assertEqual(inp.cursor.state, {"a": 1})
- self.assertEqual(inp.cursor.last_sync_time.year, 2026)
- self.assertEqual(inp.resource_ids, [])
-
-
-class SignatureTest(unittest.TestCase):
- def test_sign_and_verify(self):
- now = 1_700_000_000
- sig = p.sign(b"s3cret", now, b"{}")
- p.verify_signature(b"s3cret", str(now), sig, b"{}", now=now)
- with self.assertRaisesRegex(ValueError, "bad signature"):
- p.verify_signature(b"other", str(now), sig, b"{}", now=now)
- with self.assertRaisesRegex(ValueError, "clock skew"):
- p.verify_signature(b"s3cret", str(now), sig, b"{}", now=now + 600)
-
- def test_matches_go(self):
- # pluginapi.Sign([]byte("k"), 1, []byte("b")) in Go.
- import hashlib
- import hmac
-
- self.assertEqual(p.sign(b"k", 1, b"b"), hmac.new(b"k", b"1.b", hashlib.sha256).hexdigest())
-
-
-class FakeHostAPI(BaseHTTPRequestHandler):
- store: dict = {}
-
- def _reply(self, status, body=None):
- raw = b"" if body is None else json.dumps(body).encode()
- self.send_response(status)
- self.send_header("Content-Type", "application/json")
- self.send_header("Content-Length", str(len(raw)))
- self.end_headers()
- self.wfile.write(raw)
-
- def _authorized(self):
- if self.headers.get("Authorization") != "Bearer tok":
- self._reply(401, {"error": {"code": "unauthorized", "message": "bad token"}})
- return False
- return True
-
- def do_GET(self):
- if not self._authorized():
- return
- u = urlparse(self.path)
- q = {k: v[0] for k, v in parse_qs(u.query).items()}
- if u.path == p.HOST_KV_LIST_PATH:
- keys = sorted(k for k in self.store if k.startswith(q.get("prefix", "")) and k > q.get("after", ""))
- page = keys[:2]
- self._reply(200, {"entries": [{"key": k, "value": self.store[k], "updatedAt": "2026-01-01T00:00:00Z"} for k in page], "next": page[-1] if len(keys) > 2 else ""})
- elif q["key"] in self.store:
- self._reply(200, {"key": q["key"], "value": self.store[q["key"]], "updatedAt": "2026-01-01T00:00:00Z"})
- else:
- self._reply(404, {"error": {"code": "not_found", "message": "no key"}})
-
- def do_PUT(self):
- if not self._authorized():
- return
- body = json.loads(self.rfile.read(int(self.headers["Content-Length"])))
- self.store[body["key"]] = body["value"]
- self._reply(200, {"key": body["key"], "value": body["value"]})
-
- def do_DELETE(self):
- if not self._authorized():
- return
- self.store.pop(parse_qs(urlparse(self.path).query)["key"][0], None)
- self._reply(204)
-
- def log_message(self, *args):
- pass
-
-
-class HostTest(unittest.TestCase):
- def test_kv(self):
- srv = HTTPServer(("127.0.0.1", 0), FakeHostAPI)
- threading.Thread(target=srv.serve_forever, daemon=True).start()
- url = f"http://127.0.0.1:{srv.server_address[1]}"
- try:
- h = Host(url, "tok")
- self.assertIsNone(h.kv_get("missing"))
- self.assertEqual(h.kv_get("missing", 5), 5)
- h.kv_put("a", {"n": 1}, ttl=60)
- h.kv_put("b", 2)
- h.kv_put("c", [3])
- self.assertEqual(h.kv_get("a"), {"n": 1})
- self.assertEqual([e.key for e in h.kv_items()], ["a", "b", "c"])
- h.kv_delete("a")
- self.assertIsNone(h.kv_entry("a"))
- with self.assertRaises(PluginError) as ctx:
- Host(url, "wrong").kv_get("b")
- self.assertEqual(ctx.exception.code, ErrorCode.UNAUTHORIZED)
- finally:
- srv.shutdown()
- srv.server_close()
- with self.assertRaises(PluginError) as ctx:
- Host(url, "tok", timeout=2).kv_get("b")
- self.assertEqual(ctx.exception.code, ErrorCode.UNAVAILABLE)
-
-
-class UnixConnection(http.client.HTTPConnection):
- def __init__(self, path):
- super().__init__("plugin", timeout=10)
- self.path = path
-
- def connect(self):
- self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
- self.sock.connect(self.path)
-
-
-def _free_port():
- with socket.socket() as s:
- s.bind(("127.0.0.1", 0))
- return s.getsockname()[1]
-
-
[email protected](hasattr(socket, "AF_UNIX"), "needs unix sockets")
-class ServeTest(unittest.TestCase):
- def _start(self, env):
- proc = subprocess.Popen(
- [sys.executable, os.path.join(HERE, "fixture.py")],
- env={**{"PATH": os.environ.get("PATH", "")}, **env},
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- text=True,
- )
-
- def stop():
- proc.terminate()
- proc.wait(10)
- proc.stdout.close()
- proc.stderr.close()
-
- self.addCleanup(stop)
- return proc
-
- def test_host_mode(self):
- import tempfile
-
- d = tempfile.mkdtemp()
- sock = os.path.join(d, "p.sock")
- proc = self._start({p.ENV_SOCKET: sock, p.ENV_TOKEN: "tok"})
- self.assertEqual(proc.stdout.readline().strip(), f"WEKNORA_PLUGIN|1|unix|{sock}")
- c = Client(lambda: UnixConnection(sock))
- self.assertEqual(c.request("GET", "/v1/health")[0], 401)
- status, _, _ = c.request("GET", "/v1/health", headers={"Authorization": "Bearer tok"})
- self.assertEqual(status, 200)
- proc.terminate()
- self.assertEqual(proc.wait(10), 0)
- self.assertFalse(os.path.exists(sock), "the socket is removed on shutdown")
-
- def test_remote_mode(self):
- port = _free_port()
- self._start({p.ENV_ADDR: f"127.0.0.1:{port}", p.ENV_SECRET: "s3cret"})
- c = Client(lambda: http.client.HTTPConnection("127.0.0.1", port, timeout=10))
- for _ in range(100):
- try:
- c.request("GET", "/v1/health")
- break
- except OSError:
- time.sleep(0.05)
- body = json.dumps(dict(ENVELOPE, input={"query": "q"})).encode()
- ts = int(time.time())
- status, _, _ = c.request("POST", "/v1/websearch/echo/search", body, {p.TIMESTAMP_HEADER: str(ts), p.SIGNATURE_HEADER: p.sign(b"other", ts, body)})
- self.assertEqual(status, 401)
- status, _, data = c.request("POST", "/v1/websearch/echo/search", body, {p.TIMESTAMP_HEADER: str(ts), p.SIGNATURE_HEADER: p.sign(b"s3cret", ts, body)})
- self.assertEqual(status, 200, data)
-
- def test_shutdown_drains_calls_in_flight(self):
- port = _free_port()
- proc = self._start({p.ENV_ADDR: f"127.0.0.1:{port}", p.ENV_SECRET: "s3cret"})
- c = Client(lambda: http.client.HTTPConnection("127.0.0.1", port, timeout=10))
- for _ in range(100):
- try:
- c.request("GET", "/v1/health")
- break
- except OSError:
- time.sleep(0.05)
- body = json.dumps(dict(ENVELOPE, input={"query": "slow"})).encode()
- ts = int(time.time())
- headers = {p.TIMESTAMP_HEADER: str(ts), p.SIGNATURE_HEADER: p.sign(b"s3cret", ts, body)}
- result = {}
- t = threading.Thread(target=lambda: result.update(r=c.request("POST", "/v1/websearch/echo/search", body, headers)))
- t.start()
- time.sleep(0.2)
- proc.terminate()
- t.join(10)
- self.assertEqual(result["r"][0], 200)
- self.assertEqual(proc.wait(10), 0)
-
- def test_needs_a_secret(self):
- proc = self._start({})
- self.assertNotEqual(proc.wait(10), 0)
- self.assertIn(p.ENV_SECRET, proc.stderr.read())
-
-
-if __name__ == "__main__":
- unittest.main()
diff --git a/pluginsdk/stream.go b/pluginsdk/stream.go
deleted file mode 100644
index 9592a9e62..000000000
--- a/pluginsdk/stream.go
+++ /dev/null
@@ -1,130 +0,0 @@
-package pluginsdk
-
-import (
- "encoding/json"
- "net/http"
- "sync"
-
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// Stream writes a streaming answer: items, checkpoints and progress as NDJSON
-// lines, flushed one by one. It is safe for concurrent use.
-type Stream struct {
- mu sync.Mutex
- w http.ResponseWriter
- flusher http.Flusher
- started bool
- closed bool
- err error
-}
-
-func newStream(w http.ResponseWriter) *Stream {
- f, _ := w.(http.Flusher)
- return &Stream{w: w, flusher: f}
-}
-
-func (s *Stream) write(ev pluginapi.Event) error {
- s.mu.Lock()
- defer s.mu.Unlock()
- if s.err != nil {
- return s.err
- }
- if s.closed {
- return pluginapi.Errorf(pluginapi.CodeInternal, "stream already ended")
- }
- if !s.started {
- s.w.Header().Set("Content-Type", pluginapi.NDJSONContentType)
- s.w.WriteHeader(http.StatusOK)
- s.started = true
- }
- b, err := json.Marshal(ev)
- if err != nil {
- return err
- }
- if _, err := s.w.Write(append(b, '\n')); err != nil {
- // The caller went away; stop producing.
- s.err = err
- return err
- }
- if s.flusher != nil {
- s.flusher.Flush()
- }
- return nil
-}
-
-func dataEvent(t pluginapi.EventType, v any) (pluginapi.Event, error) {
- raw, err := json.Marshal(v)
- if err != nil {
- return pluginapi.Event{}, err
- }
- return pluginapi.Event{Type: t, Data: raw}, nil
-}
-
-// Item emits one fetched item. An error means WeKnora stopped listening:
-// stop fetching and return.
-func (s *Stream) Item(item pluginapi.FetchedItem) error {
- ev, err := dataEvent(pluginapi.EventItem, item)
- if err != nil {
- return err
- }
- return s.write(ev)
-}
-
-// Checkpoint emits a resumable cursor. It must be a complete snapshot.
-func (s *Stream) Checkpoint(c pluginapi.Cursor) error {
- ev, err := dataEvent(pluginapi.EventCheckpoint, c)
- if err != nil {
- return err
- }
- return s.write(ev)
-}
-
-// Progress reports progress for display.
-func (s *Stream) Progress(message string) error {
- return s.write(pluginapi.Event{Type: pluginapi.EventProgress, Message: message})
-}
-
-// Log forwards a line to WeKnora's logs (level: debug, info, warn, error).
-func (s *Stream) Log(level, message string) error {
- return s.write(pluginapi.Event{Type: pluginapi.EventLog, Level: level, Message: message})
-}
-
-func (s *Stream) end(cursor *pluginapi.Cursor) {
- if cursor == nil {
- cursor = &pluginapi.Cursor{}
- }
- ev, err := dataEvent(pluginapi.EventEnd, cursor)
- if err == nil {
- err = s.write(ev)
- }
- if err != nil {
- s.fail(err)
- return
- }
- s.mu.Lock()
- s.closed = true
- s.mu.Unlock()
-}
-
-// fail ends the stream with an error: as an error event once lines went
-// out, or as an ordinary error answer before that.
-func (s *Stream) fail(err error) {
- s.mu.Lock()
- started, closed := s.started, s.closed
- s.mu.Unlock()
- if closed {
- return
- }
- if !started {
- writeError(s.w, err)
- s.mu.Lock()
- s.closed = true
- s.mu.Unlock()
- return
- }
- _ = s.write(pluginapi.Event{Type: pluginapi.EventError, Error: toError(err)})
- s.mu.Lock()
- s.closed = true
- s.mu.Unlock()
-}
diff --git a/pluginsdk/ui.go b/pluginsdk/ui.go
deleted file mode 100644
index 498f6f191..000000000
--- a/pluginsdk/ui.go
+++ /dev/null
@@ -1,51 +0,0 @@
-package pluginsdk
-
-import (
- "context"
- "encoding/json"
- "net/http"
-
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// UIHandler answers the requests of the plugin's pages (contributes.pages,
-// settingsSections, kbTabs), which reach it through the WeKnora bridge.
-type UIHandler func(ctx context.Context, call *Call, req pluginapi.UIRequest) (*pluginapi.UIResponse, error)
-
-// UI registers the handler behind the plugin's pages.
-func (p *Plugin) UI(h UIHandler) { p.ui = h }
-
-// UIJSON is a UIResponse carrying v as JSON.
-func UIJSON(status int, v any) (*pluginapi.UIResponse, error) {
- b, err := json.Marshal(v)
- if err != nil {
- return nil, err
- }
- return &pluginapi.UIResponse{Status: status, Body: b}, nil
-}
-
-func (p *Plugin) routeUI(mux *http.ServeMux) {
- mux.HandleFunc("POST /v1/ui/request", func(w http.ResponseWriter, r *http.Request) {
- if p.ui == nil {
- writeError(w, pluginapi.Errorf(pluginapi.CodeNotFound, "this plugin's pages make no requests"))
- return
- }
- p.unary(func(ctx context.Context, call *Call, raw json.RawMessage) (any, error) {
- in, err := decodeInput[pluginapi.UIRequest](raw)
- if err != nil {
- return nil, err
- }
- out, err := p.ui(ctx, call, in)
- if err != nil {
- return nil, err
- }
- if out == nil {
- out = &pluginapi.UIResponse{}
- }
- if out.Status == 0 {
- out.Status = http.StatusOK
- }
- return out, nil
- })(w, r)
- })
-}
diff --git a/pluginsdk/websearch.go b/pluginsdk/websearch.go
deleted file mode 100644
index a9eb4289e..000000000
--- a/pluginsdk/websearch.go
+++ /dev/null
@@ -1,57 +0,0 @@
-package pluginsdk
-
-import (
- "context"
- "encoding/json"
- "net/http"
-
- "github.com/Tencent/WeKnora/pluginsdk/pluginapi"
-)
-
-// WebSearcher is a web search provider. Its instance configuration is what
-// the contribution's instanceSchema describes.
-type WebSearcher interface {
- Search(ctx context.Context, call *Call, in pluginapi.SearchInput) (*pluginapi.SearchOutput, error)
-}
-
-// WebSearchFunc adapts a function to WebSearcher.
-type WebSearchFunc func(ctx context.Context, call *Call, in pluginapi.SearchInput) (*pluginapi.SearchOutput, error)
-
-// Search implements WebSearcher.
-func (f WebSearchFunc) Search(
- ctx context.Context,
- call *Call,
- in pluginapi.SearchInput,
-) (*pluginapi.SearchOutput, error) {
- return f(ctx, call, in)
-}
-
-// WebSearch registers web search provider id (contributes.webSearch[].id).
-func (p *Plugin) WebSearch(id string, s WebSearcher) { p.webSearch[id] = s }
-
-func (p *Plugin) routeWebSearch(mux *http.ServeMux) {
- mux.HandleFunc("POST /v1/websearch/{id}/search", func(w http.ResponseWriter, r *http.Request) {
- s, ok := p.webSearch[r.PathValue("id")]
- if !ok {
- writeError(w, pluginapi.Errorf(pluginapi.CodeNotFound, "no web search provider %q", r.PathValue("id")))
- return
- }
- p.unary(func(ctx context.Context, call *Call, raw json.RawMessage) (any, error) {
- in, err := decodeInput[pluginapi.SearchInput](raw)
- if err != nil {
- return nil, err
- }
- out, err := s.Search(ctx, call, in)
- if err != nil {
- return nil, err
- }
- if out == nil {
- out = &pluginapi.SearchOutput{}
- }
- if out.Results == nil {
- out.Results = []pluginapi.SearchResult{}
- }
- return out, nil
- })(w, r)
- })
-}
diff --git a/website-docs/03-features/10-datasource.md b/website-docs/03-features/10-datasource.md
index ca0e9dd1a..5734f77ec 100644
--- a/website-docs/03-features/10-datasource.md
+++ b/website-docs/03-features/10-datasource.md
@@ -178,7 +178,7 @@
| 方法与路径 | 权限 | 说明 |
| --- | --- | --- |
-| `GET /api/v1/datasource/types` | Viewer | 已注册连接器的元数据与凭证表单 Schema(`ConnectorRegistry.Metadata`,按 Priority 排序) |
+| `GET /api/v1/datasource/types` | Viewer | 可用连接器元数据列表(`ListAvailableConnectors`,按 Priority 排序) |
| `POST /api/v1/datasource/validate-credentials` | Admin | 用裸凭据测试连通性(不落库),供创建向导的"测试连接"按钮 |
| `POST /api/v1/datasource` | Admin | 创建数据源(校验 KB 归属租户 → 校验连接器类型 → 在线 Validate → 落库 → 注册 cron) |
| `GET /api/v1/datasource?kb_id=` | Viewer | 按知识库列出数据源(附带最近一次 SyncLog) |
@@ -394,7 +394,7 @@ registry.Register(rssConnector.NewConnector()) // rss
registry.Register(gitlabConnector.NewConnector()) // gitlab
```
-> 注意:`connector.go` 中的 `ConnectorMetadataRegistry` 仍包含尚未实现的连接器(GitHub、Google Drive、OneDrive、Web Crawler、Slack、IMAP 等),`/datasource/types` 不会返回它们。当前实际注册可用的类型为:`feishu`、`lark`、`feishu_drive`、`lark_drive`、`notion`、`confluence`、`yuque`、`dingtalk`、`ima`、`rss`、`gitlab`。未注册类型在创建数据源时会被 `connectorRegistry.Get()` 以 `ErrConnectorNotFound` 拒绝。
+> 注意:`connector.go` 中的 `ConnectorMetadataRegistry` 仍包含尚未实现的连接器(GitHub、Google Drive、OneDrive、Web Crawler、Slack、IMAP 等)。当前实际注册可用的类型为:`feishu`、`lark`、`feishu_drive`、`lark_drive`、`notion`、`confluence`、`yuque`、`dingtalk`、`ima`、`rss`、`gitlab`。未注册类型在创建数据源时会被 `connectorRegistry.Get()` 以 `ErrConnectorNotFound` 拒绝。
### 数据模型(internal/types/datasource.go)
diff --git a/website-docs/03-features/12-im-integration.md b/website-docs/03-features/12-im-integration.md
index cc93f9182..a2904a876 100644
--- a/website-docs/03-features/12-im-integration.md
+++ b/website-docs/03-features/12-im-integration.md
@@ -118,9 +118,7 @@ flowchart LR
| `POST /api/v1/agents/:id/im-channels` | 为 Agent 创建渠道(校验 platform 合法性、填充默认 mode/output_mode) |
| `GET /api/v1/agents/:id/im-channels` | 列出 Agent 的渠道(不含凭据) |
| `GET /api/v1/im-channels` | 租户内跨 Agent 渠道总览 |
-| `GET /api/v1/im-channels/platforms` | 已注册平台目录:接入模式、控制台链接与凭证表单 Schema(前端按它渲染凭证表单) |
-| `GET /api/v1/im-channels/:id` | 读取单个渠道供编辑(Admin+),密钥字段返回 `***` |
-| `PUT /api/v1/im-channels/:id` | 局部更新(name/mode/output_mode/locale/session_mode/knowledge_base_id/credentials/enabled/agent_id);`knowledge_base_id` 传空字符串即解除文件知识库;`credentials` 按字段合并,密钥传空或 `***` 保留原值 |
+| `PUT /api/v1/im-channels/:id` | 局部更新(name/mode/output_mode/locale/session_mode/knowledge_base_id/credentials/enabled/agent_id);`knowledge_base_id` 传空字符串即解除文件知识库 |
| `DELETE /api/v1/im-channels/:id` | 删除 |
| `POST /api/v1/im-channels/:id/toggle` | 启用/停用 |
| `POST /api/v1/wechat/qrcode`、`POST /api/v1/wechat/qrcode/status` | 微信(iLink)扫码绑定:生成二维码并轮询状态 |
diff --git a/website-docs/03-features/25-plugins.md b/website-docs/03-features/25-plugins.md
deleted file mode 100644
index 996493e1c..000000000
--- a/website-docs/03-features/25-plugins.md
+++ /dev/null
@@ -1,170 +0,0 @@
-# 插件
-
-插件是 WeKnora 扩展能力的打包与分发单元:模型厂商、数据源、联网搜索、文档解析、技能、MCP 服务等都可以由插件提供。内置能力本身也以内置插件的形式登记,和第三方插件共用一套目录与开关。
-
-两类角色分工如下:
-
-| | 系统管理员 | 空间管理员 |
-| --- | --- | --- |
-| 做什么 | 安装、升级、回滚、卸载插件,填写平台配置 | 在本空间启用或停用插件,填写空间配置 |
-| 入口 | 「设置 → 插件管理」 | 「设置 → 插件」 |
-
-安装后的插件对所有空间可见,但**默认停用**,由各空间管理员自行启用。停用插件后,它的集成不再出现在类型列表中、不能新建,已有实例照常工作。
-
-## 插件的运行方式
-
-插件包是一个 `.wkp` 文件(根目录带 `plugin.yaml` 的 zip)。`runtime.type` 决定插件代码在哪里运行:
-
-| 运行方式 | 说明 | 适用 |
-| --- | --- | --- |
-| `declarative` | 没有代码,由 WeKnora 解释清单 | 技能、模型厂商定义、远程 MCP |
-| `host` | WeKnora 的插件宿主拉起的子进程,`kind` 为 `binary` 或 `python` | 自托管的代码插件 |
-| `remote` | 插件作者自行部署的 HTTP 服务,安装时登记地址 | SaaS 类插件、独立团队维护的服务 |
-
-代码插件与 WeKnora 之间走统一的扩展协议 v1(HTTP + JSON,流式同步用 NDJSON)。
-
-## 安装插件
-
-1. 以系统管理员身份打开「设置 → 插件管理」,点击「安装插件」。
-2. 上传 `.wkp` 或填写下载地址。WeKnora 先解析插件包,展示它提供的能力、申请的权限(可访问的外部域名、Host API 权限等)和包摘要。
-3. 确认后安装。安装请求带着审阅时的摘要,下载地址在此期间换了内容会被拒绝。
-
-同一插件再次安装更高版本即升级,旧版本保留,可在详情中回滚。
-
-### 远程插件
-
-`runtime.type: remote` 的插件在安装时还需填写服务地址:
-
-- 服务在内网时,需把地址加入 `SSRF_WHITELIST`。
-- 安装后 WeKnora 生成一个签名密钥,**只显示这一次**。把它配置为服务的 `WEKNORA_PLUGIN_SECRET` 环境变量,服务据此确认请求来自 WeKnora。
-- 插件详情中可以修改服务地址或轮换密钥。轮换后,服务换上新密钥之前的调用都会失败。
-- 服务版本必须与安装的插件包一致,否则插件显示为异常、调用被拒绝。升级时同时升级服务与插件包。
-
-## 运行代码插件
-
-### 内嵌宿主(默认)
-
-默认情况下,每个 app 进程自带插件宿主,在本机运行已安装的 `host` 插件:
-
-- 插件进程只拿到 `WEKNORA_PLUGIN_*` 环境变量,看不到数据库密码等 WeKnora 密钥。
-- 出网流量经宿主的出口代理,只放行插件在 `permissions.egress` 中声明的域名,内网地址一律拒绝。
-- 宿主检查运行的进程与安装的包一致,健康检查失败时按退避重启,并在插件详情中显示为「异常」。
-- Python 插件使用本机的 `python3`(可用 `WEKNORA_PLUGIN_PYTHON` 指定解释器)。Docker app 镜像已带 Python。
-- 在 Linux 上,插件进程按 `runtime.resources` 限制资源:
- - 内存总是受限。插件没有声明时,按 `WEKNORA_PLUGIN_MEMORY_DEFAULT`(如 `1Gi`)限制,未设置则不限。
- - CPU 需要把一个可写的 cgroup v2 目录委托给 WeKnora,并通过 `WEKNORA_PLUGIN_CGROUP` 指定。设置后,每个插件进程进入各自的子 cgroup。
-
-### 独立插件宿主
-
-插件较多、希望把插件与 app 隔开,或 app 节点不便运行 Python 时,可以单独运行 `WeKnora plugin-host`:
-
-- 它与 app 使用同一镜像,共用数据库、Redis、对象存储和 `SYSTEM_AES_KEY`(或 `JWT_SECRET`)。
-- 启动后每 5 秒在 Redis 中通告自己运行的插件。app 按版本挑选最空闲的宿主,用由 `SYSTEM_AES_KEY` 派生的密钥签名调用它。
-- 宿主停止时先撤回通告,再等进行中的调用结束。
-- 插件详情的「节点状态」中,`plugin-host:` 开头的就是独立宿主。
-
-docker compose 启用方式:在 `.env` 中设置
-
-```bash
-WEKNORA_PLUGIN_EMBEDDED_KINDS=none
-WEKNORA_PLUGIN_HOST_API_URL=http://app:8080
-```
-
-然后执行:
-
-```bash
-docker compose --profile plugin-host up -d
-```
-
-Helm 设置 `pluginHost.enabled=true` 即可。使用本地存储(`STORAGE_TYPE=local`)时,插件包存放在 data-files 卷中,该卷需支持多 Pod 读写(ReadWriteMany)。
-
-相关环境变量:
-
-| 变量 | 作用于 | 说明 |
-| --- | --- | --- |
-| `WEKNORA_PLUGIN_EMBEDDED_KINDS` | app | app 自己运行的 kind(`binary`、`python`,逗号分隔);`none` 表示全部交给独立宿主。默认本机能跑的都跑 |
-| `WEKNORA_PLUGIN_HOST_API_URL` | app、plugin-host | 不在 app 本机运行的插件(远程插件、独立宿主上的插件)回调 Host API 的地址 |
-| `WEKNORA_PLUGIN_HOST_KINDS` | plugin-host | 宿主运行的 kind,默认本机能跑的都跑 |
-| `WEKNORA_PLUGIN_HOST_ADDR` | plugin-host | 监听地址,默认 `:8081` |
-| `WEKNORA_PLUGIN_HOST_URL` | plugin-host | app 访问该宿主的地址,默认 `http://<主机名>:<端口>`;Helm 中为 Pod IP |
-| `WEKNORA_PLUGIN_PYTHON` | 两者 | Python 插件的解释器,默认 `python3` |
-
-独立宿主需要 Redis,且所有节点的 `SYSTEM_AES_KEY`(或 `JWT_SECRET`)必须一致。app 不运行某个 kind、又没有配置独立宿主时,该 kind 的插件在插件详情中显示为加载失败,并说明原因。
-
-## 插件页面
-
-插件可以在界面上加三种页面:
-
-| 贡献点 | 出现在 | 默认最低角色 |
-| --- | --- | --- |
-| `pages` | 工具箱的一个标签页 | viewer |
-| `settingsSections` | 设置窗口「插件」分组下的一节 | admin |
-| `kbTabs` | 每个知识库的一个页签 | viewer |
-
-页面是插件包 `ui/` 目录下的 HTML,WeKnora 通过 `/api/v1/plugin-ui/assets/...` 提供。
-
-- **隔离**:页面在沙箱 iframe 中运行,没有同源权限,读不到 WeKnora 的登录状态和本地存储。严格的 CSP 禁止它访问网络。
-- **通信**:页面只能经 [`@weknora/plugin-ui`](https://github.com/Tencent/WeKnora/tree/main/packages/plugin-ui) 桥与 WeKnora 通信,由 WeKnora 代发请求给插件后端,或者弹提示、确认框、跳转页面。
-- **鉴权**:每次请求,WeKnora 都校验空间已启用该插件、用户满足页面的最低角色,并把用户角色一并交给插件后端。
-
-## 事件与 Webhook
-
-**事件**:插件在 `permissions.events` 中声明要订阅的事件,安装时由系统管理员审阅。
-
-| 事件 | 时机 |
-| --- | --- |
-| `knowledge.ingested` | 文档处理完成、可被检索 |
-| `knowledge.failed` | 文档处理最终失败 |
-| `knowledge.deleted` | 文档被删除 |
-| `chat.answered` | 一次回答完成,含问题与回答内容 |
-
-- 只有启用了该插件的空间才会向它投递事件。
-- 事件经后台任务队列(有 Redis 时为 asynq)异步投递,至少一次。
-- 插件返回可重试错误时,同一事件会以相同 ID 重新投递,最多 10 次。
-
-**Webhook**:插件在 `contributes.webhooks` 中声明入站地址。
-
-- 每个空间得到各自的秘密地址 `/api/v1/plugin-callbacks/...`,空间管理员可在「设置 → 插件」的「配置」中复制。
-- 第三方系统调用该地址时,WeKnora 先校验地址、确认空间已启用插件,再把请求转给插件。
-- 插件自行用空间配置里的密钥校验调用方。
-- 设置 `APP_EXTERNAL_URL` 后,插件还能拿到完整地址,自动向第三方注册。
-
-## 插件工具
-
-插件可以给 Agent 提供工具。工具统一按 MCP 服务接入:
-
-- 启用了插件的空间,会在 MCP 服务列表中看到插件提供的服务(只读),其中的工具与其他 MCP 工具一样,可在 Agent 中选用,并沿用相同的工具审批设置。
-- 代码插件可以自己提供工具,无需单独部署 MCP 服务。WeKnora 调用工具时带上当前空间的插件配置,所以工具使用的是空间管理员在「设置 → 插件」中填写的账号。
-- 插件可以为工具结果声明展示方式(表格、卡片、键值、Markdown、插件页面),对话中按此展示结构化结果;模型读取的仍是文本结果。
-
-## 动态选项与账号连接
-
-插件的配置表单可以在填写时向插件取数据:
-
-- **动态选项**:下拉框的选项由插件实时给出,例如用填好的令牌列出项目。
- - 修改选项所依赖的字段后,列表会重新加载。
- - 编辑已保存的配置时,表单上没改动的密钥由服务端补齐,不会发回浏览器。
-- **账号连接**:字段显示为「连接账号」按钮,点击后在弹窗中完成第三方的 OAuth 授权。
- - 令牌只保存在 WeKnora 服务端(加密)。每次调用插件时,WeKnora 换上有效的访问令牌,并在过期前自动刷新。
- - 连接只属于发起它的空间。
-
-使用账号连接前,系统管理员需要:
-
-1. 在第三方平台注册 OAuth 应用,回调地址填 `<WeKnora 地址>/api/v1/plugin-oauth/callback`。
-2. 在「设置 → 插件管理」的插件详情中,把应用的 Client ID 和 Secret 填进平台配置。
-
-回调地址取自 `APP_EXTERNAL_URL`;未设置时,使用浏览器访问 WeKnora 所用的地址。
-
-## 开发插件
-
-- **Go**:[pluginsdk](https://github.com/Tencent/WeKnora/tree/main/pluginsdk),含协议定义、SDK、客户端和一致性测试工具 `weknora-plugin-conformance`。
-- **Python**:[pluginsdk/python](https://github.com/Tencent/WeKnora/tree/main/pluginsdk/python),Python 3.9+,只依赖标准库。
-- **示例**:[examples/plugins](https://github.com/Tencent/WeKnora/tree/main/examples/plugins):
- - `rss`:数据源连接器,Go;
- - `subtitles`:文档解析器,Go,使用 Host API;
- - `notebooks`:Jupyter 笔记本解析器,Python;
- - `links`:带三种页面的团队链接插件,Python;
- - `activity`:订阅事件、接收 Webhook 的空间动态插件,Python;
- - `jira`:Jira Cloud 问题同步与 Agent 工具(搜索、读取问题),支持 OAuth 授权或 API 令牌,带动态选项、增量同步与问题分诊技能,Go。
-
-同一个插件既可以打包成 `host` 插件由 WeKnora 运行,也可以作为 `remote` 服务独立部署,代码不用改。
diff --git a/website-docs/04-api/01-api-overview.md b/website-docs/04-api/01-api-overview.md
index 8e63f1b42..7653b3b6c 100644
--- a/website-docs/04-api/01-api-overview.md
+++ b/website-docs/04-api/01-api-overview.md
@@ -355,7 +355,7 @@ rerank 模型加载失败或调用出错时,请求不会失败,而是按召
| 会话、消息与聊天 | [02-api-chat.md](./02-api-chat.md) | `/sessions`、`/messages`、`/knowledge-chat`、`/agent-chat`、`/knowledge-search` |
| 模型与初始化 | [02-api-model-system.md](./02-api-model-system.md) | `/models`、`/initialization`、`/evaluation`、`/weknoracloud` |
| 系统与平台管理 | [02-api-system.md](./02-api-system.md) | `/system`、`/system/admin` |
-| 基础设施、数据源与插件 | [02-api-infra.md](./02-api-infra.md) | `/vector-stores`、`/storage-backends`、`/web-search-providers`、`/datasource`、`/plugins` |
+| 基础设施与数据源 | [02-api-infra.md](./02-api-infra.md) | `/vector-stores`、`/storage-backends`、`/web-search-providers`、`/datasource` |
| Agent 与 MCP | [02-api-agent-mcp.md](./02-api-agent-mcp.md) | `/agents`、`/mcp-services`、`/agent`、`/user/favorites` |
| 内置 MCP Server | [MCP 集成](../03-features/08-mcp.md) | `/mcp-endpoints`、`/mcp/:endpoint_id` |
| 本机浏览器 | [本机浏览器](../05-clients/09-local-browser.md) | `/me/browser`、`/local-browser` |
diff --git a/website-docs/04-api/02-api-channels.md b/website-docs/04-api/02-api-channels.md
index 45298a6c5..527b6eba0 100644
--- a/website-docs/04-api/02-api-channels.md
+++ b/website-docs/04-api/02-api-channels.md
@@ -63,29 +63,9 @@ curl $BASE/api/v1/agents/agent-1/im-channels -H "Authorization: Bearer $TOKEN"
curl $BASE/api/v1/im-channels -H "Authorization: Bearer $TOKEN"
```
-### GET /api/v1/im-channels/platforms
-
-用途:已注册的 IM 平台目录。每项含 `id`、`name`、`modes`(支持的接入模式,第一个为默认)、`supports_thread`、`links`(控制台与文档链接)和 `config_schema`(凭证表单:JSON Schema 子集,`x-secret` 标记密钥字段,仅适用于某种模式的字段带 `x-visible-if: {"$mode": "webhook"}`)。权限:Viewer+。
-
-响应:200 `{"success":true,"data":[PlatformInfo]}`
-
-```bash
-curl $BASE/api/v1/im-channels/platforms -H "Authorization: Bearer $TOKEN"
-```
-
-### GET /api/v1/im-channels/:id
-
-用途:读取单个渠道供编辑,含凭证:普通字段为原值,已设置的密钥字段返回 `***`。权限:Admin+。
-
-响应:200 `{"data":{IMChannel}}`
-
-```bash
-curl $BASE/api/v1/im-channels/ch-1 -H "Authorization: Bearer $TOKEN"
-```
-
### PUT /api/v1/im-channels/:id
-用途:更新渠道(局部更新:`name/mode/output_mode/locale/session_mode/knowledge_base_id/credentials/enabled/agent_id` 均可选;`knowledge_base_id` 传空字符串解除关联,`locale` 传空字符串恢复默认语言)。`credentials` 按字段合并:未传的字段保留原值,密钥字段传空字符串或 `***` 也保留原值,普通字段传空字符串即清空;传 `{}` 不做任何改动。权限:Admin+。
+用途:更新渠道(局部更新:`name/mode/output_mode/locale/session_mode/knowledge_base_id/credentials/enabled/agent_id` 均可选;`knowledge_base_id` 传空字符串解除关联,`locale` 传空字符串恢复默认语言)。权限:Admin+。
响应:200 `{"data":{IMChannel}}`
diff --git a/website-docs/04-api/02-api-infra.md b/website-docs/04-api/02-api-infra.md
index caa59bc45..9c61c6996 100644
--- a/website-docs/04-api/02-api-infra.md
+++ b/website-docs/04-api/02-api-infra.md
@@ -304,11 +304,9 @@ curl -X POST $BASE/api/v1/web-search-providers/wsp-1/test -H "Authorization: Bea
### GET /api/v1/datasource/types
-用途:可创建的连接器目录,只含当前部署已注册的连接器。权限:Viewer+。
+用途:可用连接器目录。权限:Viewer+。
-响应:200 `[{type,name,description,icon,priority,auth_type,capabilities,doc_url,permission_doc_url,permission_page_url,required_permissions,config_schema}]`
-
-`config_schema` 用 JSON Schema 子集描述该连接器 `config.credentials` 的字段,`x-secret: true` 标记的字段为密钥;前端按它渲染凭证表单。`doc_url`、`permission_*`、`required_permissions` 是凭证表单旁的接入指引。
+响应:200 `[{type,name,description,icon,priority,auth_type,capabilities}]`
```bash
curl $BASE/api/v1/datasource/types -H "Authorization: Bearer $TOKEN"
@@ -481,43 +479,6 @@ curl "$BASE/api/v1/datasource/ds-1/logs?limit=10" -H "Authorization: Bearer $TOK
curl $BASE/api/v1/datasource/logs/log-1 -H "Authorization: Bearer $TOKEN"
```
-## 插件(/api/v1/plugins)
-
-插件目录与空间级开关。内置能力(模型厂商、数据源、IM 渠道、联网搜索、Agent 工具、文档解析引擎)按厂商分组,以内置插件的形式列出,例如 `weknora.feishu` 同时提供飞书知识库、飞书云盘两个数据源和飞书 IM 渠道。Handler: `internal/handler/plugin.go`。
-
-### GET /api/v1/plugins
-
-用途:列出全部插件及当前空间是否启用。权限:Viewer+。
-
-响应:200 `{"success":true,"data":[{"manifest":{...},"enabled":true,"updatedAt":"..."}]}`。`manifest.contributes` 按扩展点列出贡献(`modelVendors`、`connectors`、`imChannels`、`webSearch`、`tools`、`parsers`),`required: true` 的插件不能停用。
-
-```bash
-curl $BASE/api/v1/plugins -H "Authorization: Bearer $TOKEN"
-```
-
-### GET /api/v1/plugins/:id
-
-用途:单个插件,结构同上。权限:Viewer+。
-
-### GET /api/v1/plugins/contributions
-
-用途:按扩展点列出全部贡献,可用 `?point=connectors` 只取一个扩展点。每项带 `pluginId`、`qualifiedId`(如 `weknora.feishu/feishu`)、`aliases`(存量数据使用的短名)和 `enabled`。权限:Viewer+。
-
-### PUT /api/v1/plugins/:id/enabled
-
-用途:为当前空间启用或停用插件。停用后,该插件的贡献不再出现在 `/datasource/types`、`/web-search-providers/types`、`/im-channels/platforms`、`/models/providers` 中,新建对应实例返回 400;已有实例不受影响。必需插件不能停用(400)。权限:Admin+。
-
-| 字段 | 类型 | 必填 | 说明 |
-| --- | --- | --- | --- |
-| `enabled` | bool | 是 | 是否启用 |
-
-响应:200 `{"success":true,"data":{"manifest":{...},"enabled":false}}`
-
-```bash
-curl -X PUT $BASE/api/v1/plugins/weknora.notion/enabled -H "Authorization: Bearer $TOKEN" \
- -H 'Content-Type: application/json' -d '{"enabled":false}'
-```
-
## 实现参考
路由注册:`internal/router/routes_infra.go` 的 `RegisterVectorStoreRoutes`、`RegisterStorageBackendRoutes`、`RegisterWebSearchRoutes`、`RegisterWebSearchProviderRoutes`、`RegisterDataSourceRoutes`。Handler:`internal/handler/vectorstore.go`、`internal/handler/storagebackend.go`、`internal/handler/web_search.go`、`internal/handler/web_search_provider.go`、`internal/handler/web_search_provider_credentials.go`、`internal/handler/datasource.go`、`internal/handler/datasource_credentials.go`。
diff --git a/website-docs/06-development/03-extension-points.md b/website-docs/06-development/03-extension-points.md
index 0428f414e..88d8ca305 100644
--- a/website-docs/06-development/03-extension-points.md
+++ b/website-docs/06-development/03-extension-points.md
@@ -426,7 +426,7 @@ func registerWebSearchProviders(registry *infra_web_search.Registry) {
}
```
-4. 在 `types.GetWebSearchProviderTypes()` 增加类型条目(`RequiresAPIKey` / `RequiresBaseURL` / `ConfigFields` 等)。前端按其派生的 `config_schema` 渲染参数表单,无需改前端;若字段标题引用前端文案 key(`x-i18n-keys`),把 key 加进 `frontend/src/i18n/backendSchemaKeys.ts`。租户配置持久化在 `web_search_providers` 表。
+4. 前端的 provider 下拉与参数表单如需展示新引擎,同步 `frontend/` 相应配置页组件;租户配置持久化在 `web_search_providers` 表。
---
@@ -511,7 +511,7 @@ if err := registry.Register(mysourceConnector.NewConnector()); err != nil {
```
3. **注册点二:`internal/datasource/connector.go` 的 `ConnectorMetadataRegistry`** — 增加类型常量(`internal/types` 的 `ConnectorTypeXxx`)与元数据条目(Name/Description/AuthType/Capabilities);
-4. **凭证表单:`internal/datasource/connector_forms.go` 的 `connectorForms`** — 用 config schema 描述该连接器的 `credentials` 字段(密钥字段标 `Secret`,条件字段用 `VisibleIf`),并给出接入指引链接与所需权限。前端数据源接入页按 `/datasource/types` 返回的 `config_schema` 渲染,无需改前端;引用的前端文案 key 需加进 `frontend/src/i18n/backendSchemaKeys.ts`;
+4. 同步配置结构:`types.DataSourceConfig` 若需新增凭证字段,注意加密存储约定;前端数据源接入页按元数据渲染;
5. 复用 `internal/datasource` 的公共工具:用户可填写的 API 地址先经 `ValidateConnectorBaseURL` 做 SSRF 校验,HTTP 请求使用 `NewConnectorHTTPClient`(带重定向与拨号期 SSRF 防护),生成文件名使用 `SanitizeFileName`。
---
@@ -593,7 +593,7 @@ func registerIMAdapterFactories(imService *imPkg.Service) {
}
```
-4. 在 `internal/im/platforms.go` 的 `platformInfos` 登记平台元数据:支持的接入模式(默认在前)、是否支持话题、控制台链接,以及用 config schema 描述的凭证字段(密钥标 `Secret`,仅某种模式使用的字段用 `VisibleIf: {"$mode": ...}`)。前端渠道管理页按 `/im-channels/platforms` 渲染,无需改表单;引用的前端文案 key 加进 `frontend/src/i18n/backendSchemaKeys.ts`,平台图标放 `frontend/src/assets/img/im/`。渠道配置持久化在 `im_channels` 表,会话映射在 `im_channel_sessions`。
+4. 渠道配置持久化在 `im_channels` 表,会话映射在 `im_channel_sessions`;前端渠道管理页需增加对应平台的配置表单。
---