revert: take the plugin system back out of main
Reverts the eleven plugin system PRs merged on 2026-09-26 so they can be reviewed before landing: *maina829d7cc9agent tools served by plugins (#3733) *4f5d475c3Jira example plugin (#3730) *01222aac8dynamic form options, OAuth connections and resource limits (#3729) *cf502ba46event delivery and webhooks (#3728) *996696057sandboxed plugin pages (#3727) *da4ef68d9standalone plugin hosts (#3725) *728007adaremote runtime, Python SDK (#3724) *ff49f9491Host API and document parsers (#3721) *4328ab26acode plugins (#3720) *ed63e35ccdeclarative plugins (#3716) *c91a8d11fplugin system foundation (#3711) The tree is back to47773b5bd, migrations included: no environment ran the plugin migrations (versioned 000115-000119, sqlite 000034-000038).
parent
a829d7cc9c
commit
6c83d7ab47
20
.env.example
20
.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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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])
|
||||
|
||||
|
||||
@plugin.on_event
|
||||
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)
|
||||
|
||||
|
||||
@plugin.webhook("notes")
|
||||
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}
|
||||
|
||||
|
||||
@plugin.ui
|
||||
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()
|
||||
|
|
@ -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"
|
||||
|
|
@ -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,值为本插件设置中的密钥。"
|
||||
|
|
@ -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: 在动态里保留每个回答的第一行,而不只是问题 }
|
||||
|
|
@ -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()
|
||||
|
|
@ -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; }
|
||||
|
|
@ -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()
|
||||
|
|
@ -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>
|
||||
|
|
@ -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.
|
||||
|
|
@ -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()
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
@ -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>
|
||||
|
Before Width: | Height: | Size: 784 B |
|
|
@ -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 = "me@example.com"
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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"
|
||||
|
|
@ -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
|
||||
|
|
@ -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: 包含评论 }
|
||||
|
|
@ -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 客户端密钥 }
|
||||
|
|
@ -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 令牌 中创建 }
|
||||
|
|
@ -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.
|
||||
|
|
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -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 ""
|
||||
}
|
||||
|
|
@ -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()
|
||||
|
|
@ -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"
|
||||
|
|
@ -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
|
||||
|
|
@ -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()
|
||||
|
|
@ -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; }
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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()
|
||||
|
|
@ -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>
|
||||
|
|
@ -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"")
|
||||
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
|
||||
|
||||
|
||||
@plugin.parser("ipynb")
|
||||
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()
|
||||
|
|
@ -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"
|
||||
|
|
@ -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]
|
||||
|
|
@ -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: 每段文本输出保留的字符数 }
|
||||
|
|
@ -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("", 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()
|
||||
|
|
@ -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"
|
||||
}
|
||||
|
|
@ -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>
|
||||
|
Before Width: | Height: | Size: 320 B |
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -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"
|
||||
|
|
@ -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"><h2>Atom</h2><p>body</p></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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -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: "可选,每行一个,格式为 名称: 值;只发给订阅地址" }
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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"
|
||||
|
|
@ -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]
|
||||
|
|
@ -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');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
)
|
||||
}
|
||||
|
|
@ -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>
|
||||
}
|
||||
|
||||
/** 解析引擎配置(引擎连接参数存空间;聊天附件解析策略在智能体中配置) */
|
||||
|
|
|
|||
|
|
@ -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 })
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
||||
<!-- 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" />
|
||||
<!-- 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>
|
||||
|
||||
<!-- 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;
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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', () => {
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
@ -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' }])
|
||||
})
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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: '' })
|
||||
})
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
@ -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')
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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'],
|
||||
)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -49,5 +49,4 @@ export const SYSTEM_ADMIN_SETTINGS_SECTIONS = new Set([
|
|||
'runtime-queues',
|
||||
'platform-api-keys',
|
||||
'system-audit-log',
|
||||
'plugin-admin',
|
||||
])
|
||||
|
|
|
|||
|
|
@ -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"/)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
@ -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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -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,
|
||||
})),
|
||||
)
|
||||
|
|
@ -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 })
|
||||
}
|
||||
}
|
||||
|
|
@ -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>
|
||||
|
|
@ -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)
|
||||
})
|
||||
|
|
@ -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 }
|
||||
}
|
||||
|
|
@ -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')
|
||||
})
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -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 },
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -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), {})
|
||||
})
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue