From 6c83d7ab479212c136094182a68ac6e238ea370c Mon Sep 17 00:00:00 2001 From: wizardchen Date: Sat, 26 Sep 2026 14:57:16 +0800 Subject: [PATCH] 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: * a829d7cc9 agent tools served by plugins (#3733) * 4f5d475c3 Jira example plugin (#3730) * 01222aac8 dynamic form options, OAuth connections and resource limits (#3729) * cf502ba46 event delivery and webhooks (#3728) * 996696057 sandboxed plugin pages (#3727) * da4ef68d9 standalone plugin hosts (#3725) * 728007ada remote runtime, Python SDK (#3724) * ff49f9491 Host API and document parsers (#3721) * 4328ab26a code plugins (#3720) * ed63e35cc declarative plugins (#3716) * c91a8d11f plugin system foundation (#3711) The tree is back to 47773b5bd, migrations included: no environment ran the plugin migrations (versioned 000115-000119, sqlite 000034-000038). --- .env.example | 20 - .github/workflows/go-lint.yml | 7 +- .github/workflows/plugin-sdk.yml | 85 -- cmd/server/main.go | 3 - cmd/server/plugin_host.go | 30 - docker-compose.yml | 65 -- docker/Dockerfile.app | 6 +- examples/plugins/activity/main.py | 87 -- examples/plugins/activity/package.sh | 19 - examples/plugins/activity/plugin.yaml | 30 - examples/plugins/activity/schemas/tenant.yaml | 18 - examples/plugins/activity/test_main.py | 66 -- examples/plugins/activity/ui/app.css | 23 - examples/plugins/activity/ui/feed.js | 54 -- examples/plugins/activity/ui/index.html | 13 - examples/plugins/jira/README.md | 64 -- examples/plugins/jira/adf.go | 213 ----- examples/plugins/jira/client.go | 374 --------- examples/plugins/jira/icon.svg | 1 - examples/plugins/jira/jira_test.go | 589 -------------- examples/plugins/jira/main.go | 471 ----------- examples/plugins/jira/messages.go | 27 - examples/plugins/jira/package.sh | 21 - examples/plugins/jira/plugin.yaml | 62 -- examples/plugins/jira/schemas/connector.yaml | 99 --- examples/plugins/jira/schemas/system.yaml | 17 - examples/plugins/jira/schemas/tenant.yaml | 73 -- .../plugins/jira/skills/jira-triage/SKILL.md | 40 - examples/plugins/jira/testdata/adf.json | 160 ---- examples/plugins/jira/tools.go | 172 ---- examples/plugins/links/main.py | 78 -- examples/plugins/links/package.sh | 18 - examples/plugins/links/plugin.yaml | 31 - examples/plugins/links/test_main.py | 56 -- examples/plugins/links/ui/app.css | 41 - examples/plugins/links/ui/index.html | 13 - examples/plugins/links/ui/kb.html | 13 - examples/plugins/links/ui/links.js | 95 --- examples/plugins/links/ui/manage.html | 13 - examples/plugins/notebooks/main.py | 106 --- examples/plugins/notebooks/package.sh | 18 - examples/plugins/notebooks/plugin.yaml | 22 - .../plugins/notebooks/schemas/tenant.yaml | 19 - examples/plugins/notebooks/test_main.py | 73 -- examples/plugins/rss/feeds.go | 282 ------- examples/plugins/rss/icon.svg | 1 - examples/plugins/rss/main.go | 229 ------ examples/plugins/rss/package.sh | 21 - examples/plugins/rss/parity_test.go | 279 ------- examples/plugins/rss/plugin.yaml | 26 - examples/plugins/rss/schemas/instance.yaml | 22 - examples/plugins/subtitles/main.go | 130 --- examples/plugins/subtitles/main_test.go | 146 ---- examples/plugins/subtitles/package.sh | 20 - examples/plugins/subtitles/plugin.yaml | 23 - frontend/src/api/agent/index.ts | 30 - frontend/src/api/datasource/index.ts | 26 +- frontend/src/api/initialization/index.ts | 3 - frontend/src/api/mcp-service.ts | 6 +- frontend/src/api/plugin.ts | 196 ----- frontend/src/api/system/index.ts | 4 - frontend/src/api/system/plugins.ts | 133 --- frontend/src/api/web-search-provider.ts | 10 +- frontend/src/components/IMChannelPanel.vue | 494 ++++++++--- frontend/src/components/ModelEditorDialog.vue | 60 +- .../src/components/modelEditorFlow.test.ts | 37 +- .../src/components/schema-form/SchemaForm.vue | 100 --- .../schema-form/SchemaFormFields.vue | 114 --- .../components/schema-form/pluginSource.ts | 50 -- .../src/components/schema-form/schema.test.ts | 165 ---- frontend/src/components/schema-form/schema.ts | 320 -------- .../src/components/schema-form/source.test.ts | 63 -- frontend/src/components/schema-form/source.ts | 105 --- .../components/schema-form/useSchemaText.ts | 15 - .../schema-form/widgets/HeadersWidget.vue | 116 --- .../schema-form/widgets/NumberWidget.vue | 38 - .../schema-form/widgets/OAuthWidget.vue | 126 --- .../schema-form/widgets/OptionsWidget.vue | 177 ---- .../schema-form/widgets/PasswordWidget.vue | 19 - .../schema-form/widgets/SelectWidget.vue | 36 - .../schema-form/widgets/SwitchWidget.vue | 20 - .../schema-form/widgets/TextWidget.vue | 16 - .../schema-form/widgets/TextareaWidget.vue | 17 - .../components/schema-form/widgets/headers.ts | 25 - .../components/schema-form/widgets/index.ts | 52 -- .../components/schema-form/widgets/types.ts | 12 - frontend/src/config/settingsAccess.test.ts | 2 +- frontend/src/config/settingsAccess.ts | 1 - frontend/src/config/toolbox.test.ts | 3 +- frontend/src/extensions/SettingsNavIcon.vue | 35 - frontend/src/extensions/builtin/settings.ts | 103 --- .../src/extensions/builtin/settingsCatalog.ts | 120 --- .../src/extensions/builtin/toolResultViews.ts | 83 -- .../extensions/pluginFrame/PluginFrame.vue | 192 ----- .../extensions/pluginFrame/bridgeHost.test.ts | 113 --- .../src/extensions/pluginFrame/bridgeHost.ts | 214 ----- .../pluginFrame/pluginPages.test.ts | 44 - .../src/extensions/pluginFrame/pluginPages.ts | 77 -- .../extensions/pluginFrame/settingsSync.ts | 41 - .../src/extensions/pluginToolView.test.ts | 71 -- frontend/src/extensions/pluginToolView.ts | 157 ---- frontend/src/extensions/registry.test.ts | 26 - frontend/src/extensions/registry.ts | 45 - frontend/src/extensions/settingsSections.ts | 101 --- frontend/src/extensions/toolResultViews.ts | 31 - frontend/src/i18n/backendSchemaKeys.ts | 85 -- frontend/src/i18n/localeKeyAudit.ts | 9 +- frontend/src/i18n/locales/en-US.ts | 223 ----- frontend/src/i18n/locales/ja-JP.ts | 223 ----- frontend/src/i18n/locales/ko-KR.ts | 223 ----- frontend/src/i18n/locales/ru-RU.ts | 223 ----- frontend/src/i18n/locales/zh-CN.ts | 223 ----- frontend/src/stores/auth.ts | 2 - .../src/stores/modelProvidersState.test.ts | 20 - frontend/src/stores/modelProvidersState.ts | 25 - frontend/src/stores/pluginPages.ts | 55 -- frontend/src/types/tool-results.ts | 3 +- frontend/src/utils/localizedText.test.ts | 12 - frontend/src/utils/localizedText.ts | 26 - frontend/src/utils/tool-capabilities.ts | 3 +- .../chat/components/AgentStreamDisplay.vue | 11 +- .../chat/components/ToolResultRenderer.vue | 135 ++- .../tool-results/PluginToolView.vue | 252 ------ .../src/views/knowledge/KnowledgeBase.vue | 49 +- .../knowledge/components/DocumentListView.vue | 3 - .../settings/DataSourceEditorDialog.test.ts | 82 +- .../settings/DataSourceEditorDialog.vue | 543 ++++++++++--- .../knowledge/settings/DataSourceSettings.vue | 30 +- .../knowledge/settings/DataSourceTypeIcon.vue | 15 +- .../knowledge/settings/KBParserSettings.vue | 6 +- .../settings/connectorLabels.test.ts | 33 - .../knowledge/settings/connectorLabels.ts | 41 - frontend/src/views/settings/McpSettings.vue | 28 +- .../views/settings/ParserEngineSettings.vue | 6 +- frontend/src/views/settings/PluginCenter.vue | 450 ---------- frontend/src/views/settings/Settings.vue | 408 ++++++++-- frontend/src/views/settings/SkillSettings.vue | 107 +-- .../src/views/settings/WebSearchSettings.vue | 276 ++++--- .../views/settings/pluginCenterState.test.ts | 76 -- .../src/views/settings/pluginCenterState.ts | 91 --- .../views/settings/settingsNavigation.test.ts | 87 +- .../src/views/system/PluginManagement.vue | 287 ------- .../system/pluginManagementState.test.ts | 91 --- .../src/views/system/pluginManagementState.ts | 140 ---- .../system/plugins/PluginDetailDrawer.vue | 428 ---------- .../system/plugins/PluginInstallDrawer.vue | 439 ---------- frontend/src/views/toolbox/Toolbox.vue | 54 +- go.mod | 3 - helm/templates/app.yaml | 9 - helm/templates/plugin-host.yaml | 157 ---- helm/values.yaml | 39 - internal/agent/tools/capabilities.go | 3 +- .../agent/tools/capabilities_frontend_test.go | 84 -- internal/agent/tools/mcp_tool.go | 1 - internal/agent/tools/mcp_tool_view.go | 38 - internal/agent/tools/mcp_tool_view_test.go | 40 - internal/application/repository/plugin.go | 90 -- internal/application/repository/plugin_kv.go | 90 -- .../application/repository/plugin_oauth.go | 49 -- .../repository/plugin_tenant_setting.go | 57 -- .../application/repository/plugin_test.go | 163 ---- .../retriever/weaviate/repository_test.go | 9 +- internal/application/service/agent_service.go | 126 ++- .../service/agent_tool_factories.go | 137 ---- .../service/agent_tool_factories_test.go | 31 - .../application/service/datasource_service.go | 9 +- .../application/service/knowledge_delete.go | 6 - .../application/service/knowledge_util.go | 8 +- .../service/tenant_skill_catalog.go | 5 +- .../service/tenant_skill_service.go | 17 - .../service/tenant_skill_source.go | 25 +- .../service/tenant_skill_source_test.go | 38 - .../service/web_search_provider.go | 47 +- internal/container/container.go | 199 +++-- internal/container/plugin_host.go | 152 ---- internal/container/plugin_runtime.go | 237 ------ internal/container/plugin_runtime_test.go | 72 -- .../migration_sqlite_versioned_schema_test.go | 6 +- .../CONNECTOR_IMPLEMENTATION_GUIDE.md | 10 - internal/datasource/connector.go | 158 +--- internal/datasource/connector_forms.go | 234 ------ internal/datasource/connector_forms_test.go | 100 --- internal/handler/datasource.go | 40 +- internal/handler/datasource_schedule_test.go | 2 +- internal/handler/datasource_test.go | 12 +- internal/handler/dto/mcp.go | 12 +- internal/handler/im.go | 67 +- internal/handler/model.go | 6 - internal/handler/model_catalog.go | 12 - internal/handler/plugin.go | 306 ------- internal/handler/plugin_admin.go | 354 -------- internal/handler/plugin_forms.go | 366 --------- internal/handler/plugin_forms_test.go | 147 ---- internal/handler/plugin_gate.go | 39 - internal/handler/plugin_test.go | 177 ---- internal/handler/plugin_ui.go | 178 ---- internal/handler/plugin_ui_test.go | 153 ---- internal/handler/plugin_webhook.go | 193 ----- internal/handler/plugin_webhook_test.go | 140 ---- internal/handler/sandbox_skill.go | 5 +- internal/handler/session/qa.go | 12 - internal/handler/system.go | 15 +- internal/handler/web_search_provider.go | 25 +- internal/im/credentials.go | 61 -- internal/im/credentials_merge_test.go | 64 -- internal/im/platforms.go | 342 -------- internal/im/platforms_test.go | 87 -- .../docparser/engine_registry.go | 108 +-- .../infrastructure/web_search/registry.go | 73 +- internal/mcp/client.go | 19 +- internal/mcp/transports.go | 44 - internal/mcp/types.go | 2 - internal/models/providers/config_schema.go | 64 -- .../models/providers/config_schema_test.go | 73 -- internal/models/runtime/overlay.go | 44 +- internal/models/runtime/plugin.go | 80 -- internal/models/runtime/plugin_test.go | 57 -- internal/models/runtime/registry.go | 2 - internal/plugin/activate/connectors.go | 354 -------- internal/plugin/activate/connectors_test.go | 202 ----- internal/plugin/activate/invoke.go | 309 ------- internal/plugin/activate/invoke_test.go | 67 -- internal/plugin/activate/mcp.go | 443 ---------- internal/plugin/activate/mcp_test.go | 237 ------ internal/plugin/activate/mcp_transport.go | 92 --- .../plugin/activate/mcp_transport_test.go | 129 --- internal/plugin/activate/parsers.go | 152 ---- internal/plugin/activate/parsers_test.go | 120 --- internal/plugin/activate/skills.go | 110 --- internal/plugin/activate/skills_test.go | 53 -- internal/plugin/activate/ui.go | 116 --- internal/plugin/activate/vendors.go | 141 ---- internal/plugin/activate/vendors_test.go | 92 --- internal/plugin/activate/websearch.go | 278 ------- internal/plugin/activate/websearch_test.go | 159 ---- internal/plugin/builtin/builtin.go | 232 ------ internal/plugin/builtin/builtin_test.go | 150 ---- internal/plugin/builtin/i18n_keys_test.go | 98 --- internal/plugin/builtin/implementations.go | 103 --- internal/plugin/builtin/live.go | 83 -- .../plugin/configschema/configschema_test.go | 264 ------ internal/plugin/configschema/schema.go | 310 ------- internal/plugin/configschema/secrets.go | 189 ----- internal/plugin/configschema/secrets_test.go | 28 - internal/plugin/configschema/validate.go | 251 ------ internal/plugin/driver/builtin.go | 61 -- internal/plugin/driver/driver.go | 100 --- internal/plugin/driver/driver_test.go | 47 -- internal/plugin/events/events.go | 151 ---- internal/plugin/events/events_test.go | 208 ----- internal/plugin/events/knowledge.go | 122 --- internal/plugin/host/gateway.go | 119 --- internal/plugin/host/host.go | 284 ------- internal/plugin/host/host_test.go | 297 ------- internal/plugin/host/limits.go | 40 - internal/plugin/host/limits_linux.go | 73 -- internal/plugin/host/limits_linux_test.go | 56 -- internal/plugin/host/limits_other.go | 6 - internal/plugin/host/limits_test.go | 25 - internal/plugin/host/proc_linux.go | 16 - internal/plugin/host/proc_unix.go | 16 - internal/plugin/host/proc_windows.go | 11 - internal/plugin/host/process.go | 533 ------------ internal/plugin/host/proxy.go | 192 ----- internal/plugin/host/python_test.go | 157 ---- internal/plugin/host/served_test.go | 30 - .../plugin/host/testdata/echoplugin/main.go | 44 - internal/plugin/hostapi/hostapi_test.go | 111 --- internal/plugin/hostapi/http.go | 125 --- internal/plugin/hostapi/kv.go | 162 ---- internal/plugin/hostapi/token.go | 110 --- internal/plugin/hostpool/hostpool.go | 261 ------ internal/plugin/hostpool/hostpool_test.go | 179 ---- internal/plugin/install/config.go | 110 --- internal/plugin/install/install.go | 553 ------------- internal/plugin/install/install_test.go | 261 ------ internal/plugin/manifest/engines.go | 62 -- internal/plugin/manifest/engines_test.go | 26 - internal/plugin/manifest/manifest.go | 514 ------------ internal/plugin/manifest/manifest_test.go | 319 -------- internal/plugin/manifest/point.go | 88 -- internal/plugin/manifest/resources.go | 59 -- internal/plugin/manifest/template.go | 64 -- internal/plugin/manifest/template_test.go | 34 - internal/plugin/manifest/text.go | 129 --- internal/plugin/manifest/toolview.go | 151 ---- internal/plugin/oauth/oauth.go | 332 -------- internal/plugin/oauth/oauth_test.go | 198 ----- internal/plugin/oauth/state.go | 94 --- internal/plugin/pkg/pkg.go | 318 -------- internal/plugin/pkg/pkg_test.go | 140 ---- internal/plugin/plugintest/plugintest.go | 268 ------ internal/plugin/reconcile/nodes.go | 154 ---- internal/plugin/reconcile/nodes_test.go | 64 -- internal/plugin/reconcile/reconcile.go | 511 ------------ internal/plugin/reconcile/reconcile_test.go | 260 ------ internal/plugin/reconcile/store.go | 52 -- internal/plugin/registry/registry.go | 218 ----- internal/plugin/registry/registry_test.go | 170 ---- internal/plugin/remote/remote.go | 248 ------ internal/plugin/remote/remote_test.go | 196 ----- internal/plugin/tenancy/config.go | 128 --- internal/plugin/tenancy/tenancy.go | 144 ---- internal/plugin/tenancy/tenancy_test.go | 243 ------ internal/plugin/webhook/webhook.go | 87 -- internal/plugin/webhook/webhook_test.go | 31 - internal/router/router.go | 35 - internal/router/routes_agent.go | 3 - internal/router/routes_plugin.go | 68 -- internal/router/routes_plugin_test.go | 41 - internal/router/sync_task.go | 7 +- internal/router/task.go | 11 +- internal/types/docparser.go | 4 - internal/types/interfaces/datasource.go | 6 +- internal/types/interfaces/plugin.go | 78 -- internal/types/mcp.go | 23 +- internal/types/plugin.go | 91 --- internal/types/plugin_tenant_setting.go | 19 - internal/types/task.go | 1 - internal/types/web_search_provider.go | 20 - internal/types/web_search_provider_schema.go | 119 --- .../types/web_search_provider_schema_test.go | 72 -- .../000034_plugin_tenant_settings.down.sql | 1 - .../000034_plugin_tenant_settings.up.sql | 10 - migrations/sqlite/000035_plugins.down.sql | 2 - migrations/sqlite/000035_plugins.up.sql | 26 - migrations/sqlite/000036_plugin_kv.down.sql | 1 - migrations/sqlite/000036_plugin_kv.up.sql | 12 - .../sqlite/000037_plugin_remote.down.sql | 2 - migrations/sqlite/000037_plugin_remote.up.sql | 3 - .../sqlite/000038_plugin_oauth.down.sql | 1 - migrations/sqlite/000038_plugin_oauth.up.sql | 16 - .../000115_plugin_tenant_settings.down.sql | 1 - .../000115_plugin_tenant_settings.up.sql | 17 - migrations/versioned/000116_plugins.down.sql | 2 - migrations/versioned/000116_plugins.up.sql | 35 - .../versioned/000117_plugin_kv.down.sql | 1 - migrations/versioned/000117_plugin_kv.up.sql | 18 - .../versioned/000118_plugin_remote.down.sql | 2 - .../versioned/000118_plugin_remote.up.sql | 7 - .../versioned/000119_plugin_oauth.down.sql | 1 - .../versioned/000119_plugin_oauth.up.sql | 22 - packages/plugin-ui/README.md | 61 -- packages/plugin-ui/index.d.ts | 63 -- packages/plugin-ui/index.js | 174 ---- packages/plugin-ui/package.json | 13 - pluginsdk/README.md | 385 --------- pluginsdk/client/client.go | 266 ------ .../cmd/weknora-plugin-conformance/main.go | 83 -- pluginsdk/conformance/conformance.go | 319 -------- pluginsdk/conformance/conformance_test.go | 118 --- pluginsdk/conformance/python_test.go | 175 ---- pluginsdk/connector.go | 160 ---- pluginsdk/events.go | 69 -- pluginsdk/go.mod | 3 - pluginsdk/host.go | 114 --- pluginsdk/mcp.go | 136 ---- pluginsdk/openapi_test.go | 50 -- pluginsdk/options.go | 42 - pluginsdk/parser.go | 55 -- pluginsdk/plugin.go | 374 --------- pluginsdk/plugin_test.go | 449 ---------- pluginsdk/pluginapi/connector.go | 96 --- pluginsdk/pluginapi/doc.go | 32 - pluginsdk/pluginapi/envelope.go | 75 -- pluginsdk/pluginapi/errors.go | 90 -- pluginsdk/pluginapi/events.go | 94 --- pluginsdk/pluginapi/handshake.go | 63 -- pluginsdk/pluginapi/hostapi.go | 48 -- pluginsdk/pluginapi/mcp.go | 114 --- pluginsdk/pluginapi/openapi.yaml | 636 --------------- pluginsdk/pluginapi/options.go | 40 - pluginsdk/pluginapi/parser.go | 34 - pluginsdk/pluginapi/pluginapi_test.go | 48 -- pluginsdk/pluginapi/signature.go | 37 - pluginsdk/pluginapi/stream.go | 34 - pluginsdk/pluginapi/ui.go | 31 - pluginsdk/pluginapi/websearch.go | 32 - pluginsdk/python/.gitignore | 3 - pluginsdk/python/README.md | 192 ----- pluginsdk/python/pyproject.toml | 23 - .../python/src/weknora_plugin/__init__.py | 93 --- pluginsdk/python/src/weknora_plugin/host.py | 95 --- pluginsdk/python/src/weknora_plugin/plugin.py | 768 ------------------ .../python/src/weknora_plugin/protocol.py | 149 ---- pluginsdk/python/src/weknora_plugin/types.py | 376 --------- pluginsdk/python/tests/fixture.py | 119 --- pluginsdk/python/tests/test_plugin.py | 434 ---------- pluginsdk/stream.go | 130 --- pluginsdk/ui.go | 51 -- pluginsdk/websearch.go | 57 -- website-docs/03-features/10-datasource.md | 4 +- website-docs/03-features/12-im-integration.md | 4 +- website-docs/03-features/25-plugins.md | 170 ---- website-docs/04-api/01-api-overview.md | 2 +- website-docs/04-api/02-api-channels.md | 22 +- website-docs/04-api/02-api-infra.md | 43 +- .../06-development/03-extension-points.md | 6 +- 398 files changed, 1860 insertions(+), 40877 deletions(-) delete mode 100644 .github/workflows/plugin-sdk.yml delete mode 100644 cmd/server/plugin_host.go delete mode 100644 examples/plugins/activity/main.py delete mode 100755 examples/plugins/activity/package.sh delete mode 100644 examples/plugins/activity/plugin.yaml delete mode 100644 examples/plugins/activity/schemas/tenant.yaml delete mode 100644 examples/plugins/activity/test_main.py delete mode 100644 examples/plugins/activity/ui/app.css delete mode 100644 examples/plugins/activity/ui/feed.js delete mode 100644 examples/plugins/activity/ui/index.html delete mode 100644 examples/plugins/jira/README.md delete mode 100644 examples/plugins/jira/adf.go delete mode 100644 examples/plugins/jira/client.go delete mode 100644 examples/plugins/jira/icon.svg delete mode 100644 examples/plugins/jira/jira_test.go delete mode 100644 examples/plugins/jira/main.go delete mode 100644 examples/plugins/jira/messages.go delete mode 100755 examples/plugins/jira/package.sh delete mode 100644 examples/plugins/jira/plugin.yaml delete mode 100644 examples/plugins/jira/schemas/connector.yaml delete mode 100644 examples/plugins/jira/schemas/system.yaml delete mode 100644 examples/plugins/jira/schemas/tenant.yaml delete mode 100644 examples/plugins/jira/skills/jira-triage/SKILL.md delete mode 100644 examples/plugins/jira/testdata/adf.json delete mode 100644 examples/plugins/jira/tools.go delete mode 100644 examples/plugins/links/main.py delete mode 100755 examples/plugins/links/package.sh delete mode 100644 examples/plugins/links/plugin.yaml delete mode 100644 examples/plugins/links/test_main.py delete mode 100644 examples/plugins/links/ui/app.css delete mode 100644 examples/plugins/links/ui/index.html delete mode 100644 examples/plugins/links/ui/kb.html delete mode 100644 examples/plugins/links/ui/links.js delete mode 100644 examples/plugins/links/ui/manage.html delete mode 100644 examples/plugins/notebooks/main.py delete mode 100755 examples/plugins/notebooks/package.sh delete mode 100644 examples/plugins/notebooks/plugin.yaml delete mode 100644 examples/plugins/notebooks/schemas/tenant.yaml delete mode 100644 examples/plugins/notebooks/test_main.py delete mode 100644 examples/plugins/rss/feeds.go delete mode 100644 examples/plugins/rss/icon.svg delete mode 100644 examples/plugins/rss/main.go delete mode 100755 examples/plugins/rss/package.sh delete mode 100644 examples/plugins/rss/parity_test.go delete mode 100644 examples/plugins/rss/plugin.yaml delete mode 100644 examples/plugins/rss/schemas/instance.yaml delete mode 100644 examples/plugins/subtitles/main.go delete mode 100644 examples/plugins/subtitles/main_test.go delete mode 100755 examples/plugins/subtitles/package.sh delete mode 100644 examples/plugins/subtitles/plugin.yaml delete mode 100644 frontend/src/api/plugin.ts delete mode 100644 frontend/src/api/system/plugins.ts delete mode 100644 frontend/src/components/schema-form/SchemaForm.vue delete mode 100644 frontend/src/components/schema-form/SchemaFormFields.vue delete mode 100644 frontend/src/components/schema-form/pluginSource.ts delete mode 100644 frontend/src/components/schema-form/schema.test.ts delete mode 100644 frontend/src/components/schema-form/schema.ts delete mode 100644 frontend/src/components/schema-form/source.test.ts delete mode 100644 frontend/src/components/schema-form/source.ts delete mode 100644 frontend/src/components/schema-form/useSchemaText.ts delete mode 100644 frontend/src/components/schema-form/widgets/HeadersWidget.vue delete mode 100644 frontend/src/components/schema-form/widgets/NumberWidget.vue delete mode 100644 frontend/src/components/schema-form/widgets/OAuthWidget.vue delete mode 100644 frontend/src/components/schema-form/widgets/OptionsWidget.vue delete mode 100644 frontend/src/components/schema-form/widgets/PasswordWidget.vue delete mode 100644 frontend/src/components/schema-form/widgets/SelectWidget.vue delete mode 100644 frontend/src/components/schema-form/widgets/SwitchWidget.vue delete mode 100644 frontend/src/components/schema-form/widgets/TextWidget.vue delete mode 100644 frontend/src/components/schema-form/widgets/TextareaWidget.vue delete mode 100644 frontend/src/components/schema-form/widgets/headers.ts delete mode 100644 frontend/src/components/schema-form/widgets/index.ts delete mode 100644 frontend/src/components/schema-form/widgets/types.ts delete mode 100644 frontend/src/extensions/SettingsNavIcon.vue delete mode 100644 frontend/src/extensions/builtin/settings.ts delete mode 100644 frontend/src/extensions/builtin/settingsCatalog.ts delete mode 100644 frontend/src/extensions/builtin/toolResultViews.ts delete mode 100644 frontend/src/extensions/pluginFrame/PluginFrame.vue delete mode 100644 frontend/src/extensions/pluginFrame/bridgeHost.test.ts delete mode 100644 frontend/src/extensions/pluginFrame/bridgeHost.ts delete mode 100644 frontend/src/extensions/pluginFrame/pluginPages.test.ts delete mode 100644 frontend/src/extensions/pluginFrame/pluginPages.ts delete mode 100644 frontend/src/extensions/pluginFrame/settingsSync.ts delete mode 100644 frontend/src/extensions/pluginToolView.test.ts delete mode 100644 frontend/src/extensions/pluginToolView.ts delete mode 100644 frontend/src/extensions/registry.test.ts delete mode 100644 frontend/src/extensions/registry.ts delete mode 100644 frontend/src/extensions/settingsSections.ts delete mode 100644 frontend/src/extensions/toolResultViews.ts delete mode 100644 frontend/src/i18n/backendSchemaKeys.ts delete mode 100644 frontend/src/stores/pluginPages.ts delete mode 100644 frontend/src/utils/localizedText.test.ts delete mode 100644 frontend/src/utils/localizedText.ts delete mode 100644 frontend/src/views/chat/components/tool-results/PluginToolView.vue delete mode 100644 frontend/src/views/knowledge/settings/connectorLabels.test.ts delete mode 100644 frontend/src/views/knowledge/settings/connectorLabels.ts delete mode 100644 frontend/src/views/settings/PluginCenter.vue delete mode 100644 frontend/src/views/settings/pluginCenterState.test.ts delete mode 100644 frontend/src/views/settings/pluginCenterState.ts delete mode 100644 frontend/src/views/system/PluginManagement.vue delete mode 100644 frontend/src/views/system/pluginManagementState.test.ts delete mode 100644 frontend/src/views/system/pluginManagementState.ts delete mode 100644 frontend/src/views/system/plugins/PluginDetailDrawer.vue delete mode 100644 frontend/src/views/system/plugins/PluginInstallDrawer.vue delete mode 100644 helm/templates/plugin-host.yaml delete mode 100644 internal/agent/tools/capabilities_frontend_test.go delete mode 100644 internal/agent/tools/mcp_tool_view.go delete mode 100644 internal/agent/tools/mcp_tool_view_test.go delete mode 100644 internal/application/repository/plugin.go delete mode 100644 internal/application/repository/plugin_kv.go delete mode 100644 internal/application/repository/plugin_oauth.go delete mode 100644 internal/application/repository/plugin_tenant_setting.go delete mode 100644 internal/application/repository/plugin_test.go delete mode 100644 internal/application/service/agent_tool_factories.go delete mode 100644 internal/application/service/agent_tool_factories_test.go delete mode 100644 internal/container/plugin_host.go delete mode 100644 internal/container/plugin_runtime.go delete mode 100644 internal/container/plugin_runtime_test.go delete mode 100644 internal/datasource/connector_forms.go delete mode 100644 internal/datasource/connector_forms_test.go delete mode 100644 internal/handler/plugin.go delete mode 100644 internal/handler/plugin_admin.go delete mode 100644 internal/handler/plugin_forms.go delete mode 100644 internal/handler/plugin_forms_test.go delete mode 100644 internal/handler/plugin_gate.go delete mode 100644 internal/handler/plugin_test.go delete mode 100644 internal/handler/plugin_ui.go delete mode 100644 internal/handler/plugin_ui_test.go delete mode 100644 internal/handler/plugin_webhook.go delete mode 100644 internal/handler/plugin_webhook_test.go delete mode 100644 internal/im/credentials_merge_test.go delete mode 100644 internal/im/platforms.go delete mode 100644 internal/im/platforms_test.go delete mode 100644 internal/mcp/transports.go delete mode 100644 internal/models/providers/config_schema.go delete mode 100644 internal/models/providers/config_schema_test.go delete mode 100644 internal/models/runtime/plugin.go delete mode 100644 internal/models/runtime/plugin_test.go delete mode 100644 internal/plugin/activate/connectors.go delete mode 100644 internal/plugin/activate/connectors_test.go delete mode 100644 internal/plugin/activate/invoke.go delete mode 100644 internal/plugin/activate/invoke_test.go delete mode 100644 internal/plugin/activate/mcp.go delete mode 100644 internal/plugin/activate/mcp_test.go delete mode 100644 internal/plugin/activate/mcp_transport.go delete mode 100644 internal/plugin/activate/mcp_transport_test.go delete mode 100644 internal/plugin/activate/parsers.go delete mode 100644 internal/plugin/activate/parsers_test.go delete mode 100644 internal/plugin/activate/skills.go delete mode 100644 internal/plugin/activate/skills_test.go delete mode 100644 internal/plugin/activate/ui.go delete mode 100644 internal/plugin/activate/vendors.go delete mode 100644 internal/plugin/activate/vendors_test.go delete mode 100644 internal/plugin/activate/websearch.go delete mode 100644 internal/plugin/activate/websearch_test.go delete mode 100644 internal/plugin/builtin/builtin.go delete mode 100644 internal/plugin/builtin/builtin_test.go delete mode 100644 internal/plugin/builtin/i18n_keys_test.go delete mode 100644 internal/plugin/builtin/implementations.go delete mode 100644 internal/plugin/builtin/live.go delete mode 100644 internal/plugin/configschema/configschema_test.go delete mode 100644 internal/plugin/configschema/schema.go delete mode 100644 internal/plugin/configschema/secrets.go delete mode 100644 internal/plugin/configschema/secrets_test.go delete mode 100644 internal/plugin/configschema/validate.go delete mode 100644 internal/plugin/driver/builtin.go delete mode 100644 internal/plugin/driver/driver.go delete mode 100644 internal/plugin/driver/driver_test.go delete mode 100644 internal/plugin/events/events.go delete mode 100644 internal/plugin/events/events_test.go delete mode 100644 internal/plugin/events/knowledge.go delete mode 100644 internal/plugin/host/gateway.go delete mode 100644 internal/plugin/host/host.go delete mode 100644 internal/plugin/host/host_test.go delete mode 100644 internal/plugin/host/limits.go delete mode 100644 internal/plugin/host/limits_linux.go delete mode 100644 internal/plugin/host/limits_linux_test.go delete mode 100644 internal/plugin/host/limits_other.go delete mode 100644 internal/plugin/host/limits_test.go delete mode 100644 internal/plugin/host/proc_linux.go delete mode 100644 internal/plugin/host/proc_unix.go delete mode 100644 internal/plugin/host/proc_windows.go delete mode 100644 internal/plugin/host/process.go delete mode 100644 internal/plugin/host/proxy.go delete mode 100644 internal/plugin/host/python_test.go delete mode 100644 internal/plugin/host/served_test.go delete mode 100644 internal/plugin/host/testdata/echoplugin/main.go delete mode 100644 internal/plugin/hostapi/hostapi_test.go delete mode 100644 internal/plugin/hostapi/http.go delete mode 100644 internal/plugin/hostapi/kv.go delete mode 100644 internal/plugin/hostapi/token.go delete mode 100644 internal/plugin/hostpool/hostpool.go delete mode 100644 internal/plugin/hostpool/hostpool_test.go delete mode 100644 internal/plugin/install/config.go delete mode 100644 internal/plugin/install/install.go delete mode 100644 internal/plugin/install/install_test.go delete mode 100644 internal/plugin/manifest/engines.go delete mode 100644 internal/plugin/manifest/engines_test.go delete mode 100644 internal/plugin/manifest/manifest.go delete mode 100644 internal/plugin/manifest/manifest_test.go delete mode 100644 internal/plugin/manifest/point.go delete mode 100644 internal/plugin/manifest/resources.go delete mode 100644 internal/plugin/manifest/template.go delete mode 100644 internal/plugin/manifest/template_test.go delete mode 100644 internal/plugin/manifest/text.go delete mode 100644 internal/plugin/manifest/toolview.go delete mode 100644 internal/plugin/oauth/oauth.go delete mode 100644 internal/plugin/oauth/oauth_test.go delete mode 100644 internal/plugin/oauth/state.go delete mode 100644 internal/plugin/pkg/pkg.go delete mode 100644 internal/plugin/pkg/pkg_test.go delete mode 100644 internal/plugin/plugintest/plugintest.go delete mode 100644 internal/plugin/reconcile/nodes.go delete mode 100644 internal/plugin/reconcile/nodes_test.go delete mode 100644 internal/plugin/reconcile/reconcile.go delete mode 100644 internal/plugin/reconcile/reconcile_test.go delete mode 100644 internal/plugin/reconcile/store.go delete mode 100644 internal/plugin/registry/registry.go delete mode 100644 internal/plugin/registry/registry_test.go delete mode 100644 internal/plugin/remote/remote.go delete mode 100644 internal/plugin/remote/remote_test.go delete mode 100644 internal/plugin/tenancy/config.go delete mode 100644 internal/plugin/tenancy/tenancy.go delete mode 100644 internal/plugin/tenancy/tenancy_test.go delete mode 100644 internal/plugin/webhook/webhook.go delete mode 100644 internal/plugin/webhook/webhook_test.go delete mode 100644 internal/router/routes_plugin.go delete mode 100644 internal/router/routes_plugin_test.go delete mode 100644 internal/types/interfaces/plugin.go delete mode 100644 internal/types/plugin.go delete mode 100644 internal/types/plugin_tenant_setting.go delete mode 100644 internal/types/web_search_provider_schema.go delete mode 100644 internal/types/web_search_provider_schema_test.go delete mode 100644 migrations/sqlite/000034_plugin_tenant_settings.down.sql delete mode 100644 migrations/sqlite/000034_plugin_tenant_settings.up.sql delete mode 100644 migrations/sqlite/000035_plugins.down.sql delete mode 100644 migrations/sqlite/000035_plugins.up.sql delete mode 100644 migrations/sqlite/000036_plugin_kv.down.sql delete mode 100644 migrations/sqlite/000036_plugin_kv.up.sql delete mode 100644 migrations/sqlite/000037_plugin_remote.down.sql delete mode 100644 migrations/sqlite/000037_plugin_remote.up.sql delete mode 100644 migrations/sqlite/000038_plugin_oauth.down.sql delete mode 100644 migrations/sqlite/000038_plugin_oauth.up.sql delete mode 100644 migrations/versioned/000115_plugin_tenant_settings.down.sql delete mode 100644 migrations/versioned/000115_plugin_tenant_settings.up.sql delete mode 100644 migrations/versioned/000116_plugins.down.sql delete mode 100644 migrations/versioned/000116_plugins.up.sql delete mode 100644 migrations/versioned/000117_plugin_kv.down.sql delete mode 100644 migrations/versioned/000117_plugin_kv.up.sql delete mode 100644 migrations/versioned/000118_plugin_remote.down.sql delete mode 100644 migrations/versioned/000118_plugin_remote.up.sql delete mode 100644 migrations/versioned/000119_plugin_oauth.down.sql delete mode 100644 migrations/versioned/000119_plugin_oauth.up.sql delete mode 100644 packages/plugin-ui/README.md delete mode 100644 packages/plugin-ui/index.d.ts delete mode 100644 packages/plugin-ui/index.js delete mode 100644 packages/plugin-ui/package.json delete mode 100644 pluginsdk/README.md delete mode 100644 pluginsdk/client/client.go delete mode 100644 pluginsdk/cmd/weknora-plugin-conformance/main.go delete mode 100644 pluginsdk/conformance/conformance.go delete mode 100644 pluginsdk/conformance/conformance_test.go delete mode 100644 pluginsdk/conformance/python_test.go delete mode 100644 pluginsdk/connector.go delete mode 100644 pluginsdk/events.go delete mode 100644 pluginsdk/go.mod delete mode 100644 pluginsdk/host.go delete mode 100644 pluginsdk/mcp.go delete mode 100644 pluginsdk/openapi_test.go delete mode 100644 pluginsdk/options.go delete mode 100644 pluginsdk/parser.go delete mode 100644 pluginsdk/plugin.go delete mode 100644 pluginsdk/plugin_test.go delete mode 100644 pluginsdk/pluginapi/connector.go delete mode 100644 pluginsdk/pluginapi/doc.go delete mode 100644 pluginsdk/pluginapi/envelope.go delete mode 100644 pluginsdk/pluginapi/errors.go delete mode 100644 pluginsdk/pluginapi/events.go delete mode 100644 pluginsdk/pluginapi/handshake.go delete mode 100644 pluginsdk/pluginapi/hostapi.go delete mode 100644 pluginsdk/pluginapi/mcp.go delete mode 100644 pluginsdk/pluginapi/openapi.yaml delete mode 100644 pluginsdk/pluginapi/options.go delete mode 100644 pluginsdk/pluginapi/parser.go delete mode 100644 pluginsdk/pluginapi/pluginapi_test.go delete mode 100644 pluginsdk/pluginapi/signature.go delete mode 100644 pluginsdk/pluginapi/stream.go delete mode 100644 pluginsdk/pluginapi/ui.go delete mode 100644 pluginsdk/pluginapi/websearch.go delete mode 100644 pluginsdk/python/.gitignore delete mode 100644 pluginsdk/python/README.md delete mode 100644 pluginsdk/python/pyproject.toml delete mode 100644 pluginsdk/python/src/weknora_plugin/__init__.py delete mode 100644 pluginsdk/python/src/weknora_plugin/host.py delete mode 100644 pluginsdk/python/src/weknora_plugin/plugin.py delete mode 100644 pluginsdk/python/src/weknora_plugin/protocol.py delete mode 100644 pluginsdk/python/src/weknora_plugin/types.py delete mode 100644 pluginsdk/python/tests/fixture.py delete mode 100644 pluginsdk/python/tests/test_plugin.py delete mode 100644 pluginsdk/stream.go delete mode 100644 pluginsdk/ui.go delete mode 100644 pluginsdk/websearch.go delete mode 100644 website-docs/03-features/25-plugins.md diff --git a/.env.example b/.env.example index 0df7082b..13e9d634 100644 --- a/.env.example +++ b/.env.example @@ -811,23 +811,3 @@ CONCURRENCY_POOL_SIZE=5 # BROWSERSKILL_INTERNAL_URL=http://10.0.0.12:8080 # 所有副本使用相同的随机密钥(至少 32 字符),从 Secret 注入,不提交到仓库。 # BROWSERSKILL_CLUSTER_SECRET= - -# ========== J4. 插件运行(可选) ========== -# 代码插件(runtime.type: host)默认在每个 app 进程内运行。多机或想把 Python 插件隔开时, -# 可以另起 `WeKnora plugin-host`(compose:--profile plugin-host;helm:pluginHost.enabled)。 -# plugin-host 与 app 共用数据库、Redis、存储和 SYSTEM_AES_KEY(或 JWT_SECRET),经 Redis 通告自己。 -# app 自己运行哪些 kind(binary,python);none 表示全部交给 plugin-host。默认本机能跑的都跑。 -# WEKNORA_PLUGIN_EMBEDDED_KINDS=none -# 不在 app 本机运行的插件(remote、plugin-host 上的)回调 Host API 的地址。 -# WEKNORA_PLUGIN_HOST_API_URL=http://app:8080 -# 仅 plugin-host:运行哪些 kind、监听地址、app 访问它的地址(默认 http://:<端口>)。 -# WEKNORA_PLUGIN_HOST_KINDS=python -# WEKNORA_PLUGIN_HOST_ADDR=:8081 -# WEKNORA_PLUGIN_HOST_URL=http://plugin-host:8081 -# Python 插件使用的解释器(默认 PATH 中的 python3)。 -# WEKNORA_PLUGIN_PYTHON=python3 -# 插件进程的内存上限(Linux),用于没有声明 runtime.resources.memory 的插件。默认不限。 -# WEKNORA_PLUGIN_MEMORY_DEFAULT=1Gi -# 委托给 WeKnora 的 cgroup v2 目录(Linux,需可写)。设置后每个插件进程进入各自的子 cgroup, -# 按 runtime.resources 设置 memory.max 与 cpu.max;不设置时只限内存(rlimit),不限 CPU。 -# WEKNORA_PLUGIN_CGROUP=/sys/fs/cgroup/weknora-plugins diff --git a/.github/workflows/go-lint.yml b/.github/workflows/go-lint.yml index c95d3ec6..af10de96 100644 --- a/.github/workflows/go-lint.yml +++ b/.github/workflows/go-lint.yml @@ -58,10 +58,10 @@ jobs: modules=() if git diff --name-only --diff-filter=ACMR "$diff_range" -- \ .golangci.yml .github/workflows/go-lint.yml | grep -q .; then - modules=("." "cli" "client" "pluginsdk") + modules=("." "cli" "client") else if git diff --name-only --diff-filter=ACMR "$diff_range" -- \ - '*.go' ':!cli/**' ':!client/**' ':!pluginsdk/**' go.mod go.sum | grep -q .; then + '*.go' ':!cli/**' ':!client/**' go.mod go.sum | grep -q .; then modules+=(".") fi if git diff --name-only --diff-filter=ACMR "$diff_range" -- 'cli/**' | grep -q .; then @@ -70,9 +70,6 @@ jobs: if git diff --name-only --diff-filter=ACMR "$diff_range" -- 'client/**' | grep -q .; then modules+=("client") fi - if git diff --name-only --diff-filter=ACMR "$diff_range" -- 'pluginsdk/**' | grep -q .; then - modules+=("pluginsdk") - fi fi json="[" first=1 diff --git a/.github/workflows/plugin-sdk.yml b/.github/workflows/plugin-sdk.yml deleted file mode 100644 index f7e629d4..00000000 --- a/.github/workflows/plugin-sdk.yml +++ /dev/null @@ -1,85 +0,0 @@ -name: Plugin SDK - -# Builds and tests the pluginsdk/ Go module (extension protocol, Go SDK, -# client and conformance suite) and the Python SDK in pluginsdk/python on -# every push / PR that touches them. The Go conformance suite also runs the -# Python SDK's test plugin. - -on: - push: - branches: [main] - paths: - - 'pluginsdk/**' - - 'examples/plugins/notebooks/**' - - 'examples/plugins/links/**' - - 'examples/plugins/activity/**' - - '.github/workflows/plugin-sdk.yml' - pull_request: - paths: - - 'pluginsdk/**' - - 'examples/plugins/notebooks/**' - - 'examples/plugins/links/**' - - 'examples/plugins/activity/**' - - '.github/workflows/plugin-sdk.yml' - -defaults: - run: - working-directory: pluginsdk - shell: bash - -permissions: - contents: read - -jobs: - test: - name: test (${{ matrix.os }}) - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest, windows-latest] - steps: - - uses: actions/checkout@v6 - - uses: actions/setup-go@v6 - with: - go-version: '1.26' - cache-dependency-path: pluginsdk/go.mod - - uses: actions/setup-python@v6 - with: - python-version: '3.13' - - name: go build - run: go build ./... - - name: go test - run: go test -race ./... - - name: go vet - run: go vet ./... - - python: - name: python ${{ matrix.python }} (${{ matrix.os }}) - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - include: - - { os: ubuntu-latest, python: '3.9' } - - { os: ubuntu-latest, python: '3.13' } - - { os: windows-latest, python: '3.13' } - defaults: - run: - working-directory: pluginsdk/python - # A job's run defaults replace the workflow's, shell included. - shell: bash - steps: - - uses: actions/checkout@v6 - - uses: actions/setup-python@v6 - with: - python-version: ${{ matrix.python }} - - name: unittest - run: python -W error::ResourceWarning -m unittest discover -s tests -v - - name: example plugins - run: | - (cd ../../examples/plugins/notebooks && python -m unittest -v test_main) - (cd ../../examples/plugins/links && python -m unittest -v test_main) - (cd ../../examples/plugins/activity && python -m unittest -v test_main) - - name: build the package - run: python -m pip install build && python -m build diff --git a/cmd/server/main.go b/cmd/server/main.go index 3525fddf..021a8bd2 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -41,9 +41,6 @@ import ( ) func main() { - if code, ran := subcommand(); ran { - os.Exit(code) - } // Set Gin mode if os.Getenv("GIN_MODE") == "release" { gin.SetMode(gin.ReleaseMode) diff --git a/cmd/server/plugin_host.go b/cmd/server/plugin_host.go deleted file mode 100644 index 3832015b..00000000 --- a/cmd/server/plugin_host.go +++ /dev/null @@ -1,30 +0,0 @@ -package main - -import ( - "context" - "os" - "os/signal" - - "github.com/Tencent/WeKnora/internal/container" - "github.com/Tencent/WeKnora/internal/logger" -) - -// runPluginHost is `WeKnora plugin-host`: a standalone plugin host that -// runs host plugins for the app nodes (see container.RunPluginHost). -func runPluginHost() int { - ctx, stop := signal.NotifyContext(context.Background(), shutdownSignals...) - defer stop() - if err := container.RunPluginHost(ctx); err != nil { - logger.Errorf(context.Background(), "[plugin-host] %v", err) - return 1 - } - return 0 -} - -// subcommand runs a subcommand named on the command line, if any. -func subcommand() (code int, ran bool) { - if len(os.Args) > 1 && os.Args[1] == "plugin-host" { - return runPluginHost(), true - } - return 0, false -} diff --git a/docker-compose.yml b/docker-compose.yml index 439e02e5..0aca5247 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -381,11 +381,6 @@ services: - OIDC_USER_INFO_MAPPING_EMAIL=${OIDC_USER_INFO_MAPPING_EMAIL:-} # 飞书云文档解析模式 export:导出为docx,blocks:根据飞书云文档块解析为markdown。默认为:export - FEISHU_DOCX_PARSE_MODE=${FEISHU_DOCX_PARSE_MODE:-export} - # ========== 插件 ========== - # app 自己运行哪些 kind 的宿主插件(binary,python;none 表示全部交给 plugin-host),默认本机能跑的都跑 - - WEKNORA_PLUGIN_EMBEDDED_KINDS=${WEKNORA_PLUGIN_EMBEDDED_KINDS:-} - # 不在本机运行的插件(remote、plugin-host 上的)回调 Host API 的地址;启用 plugin-host 时设为 http://app:8080 - - WEKNORA_PLUGIN_HOST_API_URL=${WEKNORA_PLUGIN_HOST_API_URL:-} depends_on: redis: condition: service_started @@ -399,66 +394,6 @@ services: extra_hosts: - "host.docker.internal:host-gateway" - # 独立插件宿主(可选,profile: plugin-host):在单独的容器里运行宿主插件, - # 例如把 Python 插件与 app 隔开。与 app 共用镜像、数据库、Redis 和存储, - # 通过 Redis 向 app 通告自己,app 用 SYSTEM_AES_KEY 派生的密钥签名调用它。 - # 启用:docker compose --profile plugin-host up -d,并在 .env 里设 - # WEKNORA_PLUGIN_EMBEDDED_KINDS=none(或 binary) - # WEKNORA_PLUGIN_HOST_API_URL=http://app:8080 - plugin-host: - image: wechatopenai/weknora-app:${WEKNORA_VERSION:-latest} - container_name: WeKnora-plugin-host - profiles: - - plugin-host - command: ["./WeKnora", "plugin-host"] - volumes: - # STORAGE_TYPE=local 时插件包存在这里 - - data-files:/data/files - env_file: - - .env - environment: - - GIN_MODE=${GIN_MODE:-release} - - TZ=${TZ:-Asia/Shanghai} - - LOG_LEVEL=${LOG_LEVEL:-info} - - WEKNORA_PLUGIN_HOST_ADDR=:8081 - - WEKNORA_PLUGIN_HOST_URL=http://plugin-host:8081 - - WEKNORA_PLUGIN_HOST_KINDS=${WEKNORA_PLUGIN_HOST_KINDS:-} - - WEKNORA_PLUGIN_HOST_API_URL=${WEKNORA_PLUGIN_HOST_API_URL:-http://app:8080} - - DB_DRIVER=${DB_DRIVER:-postgres} - - DB_HOST=${DB_HOST:-postgres} - - DB_PORT=${DB_PORT:-5432} - - DB_USER=${DB_USER:-} - - DB_PASSWORD=${DB_PASSWORD:-} - - DB_NAME=${DB_NAME:-} - - STORAGE_TYPE=${STORAGE_TYPE:-local} - - LOCAL_STORAGE_BASE_DIR=${LOCAL_STORAGE_BASE_DIR:-/data/files} - - MINIO_ENDPOINT=${MINIO_ENDPOINT:-minio:9000} - - MINIO_ACCESS_KEY_ID=${MINIO_ACCESS_KEY_ID:-minioadmin} - - MINIO_SECRET_ACCESS_KEY=${MINIO_SECRET_ACCESS_KEY:-minioadmin} - - REDIS_ADDR=${REDIS_ADDR:-redis:6379} - - REDIS_USERNAME=${REDIS_USERNAME:-} - - REDIS_PASSWORD=${REDIS_PASSWORD:-} - - REDIS_DB=${REDIS_DB:-} - - WEKNORA_REDIS_NAMESPACE=${WEKNORA_REDIS_NAMESPACE:-} - - SYSTEM_AES_KEY=${SYSTEM_AES_KEY:-} - - JWT_SECRET=${JWT_SECRET:-} - - SSRF_WHITELIST=${SSRF_WHITELIST:-} - healthcheck: - test: ["CMD", "curl", "-f", "http://localhost:8081/healthz"] - interval: 30s - timeout: 10s - retries: 3 - start_period: 30s - depends_on: - redis: - condition: service_started - postgres: - condition: service_healthy - networks: - - WeKnora-network - restart: unless-stopped - stop_grace_period: 75s - # Sandbox 镜像:仅用于 build/pull,非常驻。Docker 沙箱默认关闭,需 # WEKNORA_SANDBOX_DOCKER_ENABLED=true 并挂载 docker.sock 后,app 才会按会话创建容器。 sandbox: diff --git a/docker/Dockerfile.app b/docker/Dockerfile.app index 0900cd47..21a27292 100644 --- a/docker/Dockerfile.app +++ b/docker/Dockerfile.app @@ -38,12 +38,10 @@ RUN if [ -n "$APK_MIRROR_ARG" ]; then \ # Install migrate tool RUN go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest -# Copy go mod files. go.mod replace-points anydoc at ./third_party/anydoc-go -# and the plugin SDK at ./pluginsdk, so their go.mod files must exist before -# `go mod download`. +# Copy go mod files. go.mod replace-points anydoc at ./third_party/anydoc-go, +# so that module's go.mod must exist before `go mod download`. COPY go.mod go.sum ./ COPY third_party/anydoc-go/go.mod third_party/anydoc-go/go.mod -COPY pluginsdk/go.mod pluginsdk/go.mod RUN --mount=type=cache,target=/go/pkg/mod go mod download COPY cmd/download cmd/download RUN go run cmd/download/duckdb/duckdb.go diff --git a/examples/plugins/activity/main.py b/examples/plugins/activity/main.py deleted file mode 100644 index a1c45574..00000000 --- a/examples/plugins/activity/main.py +++ /dev/null @@ -1,87 +0,0 @@ -"""A WeKnora plugin that keeps an activity feed of the workspace, written -with the Python SDK. It shows the three ways data reaches a plugin without a -user asking: events WeKnora sends (at least once, so they are deduplicated -by ID), a webhook other systems call, and a page that reads the feed.""" - -from __future__ import annotations - -import hmac -from datetime import datetime, timezone - -from weknora_plugin import ErrorCode, Plugin, PluginError, UIResponse, WebhookResponse - -plugin = Plugin("weknora-examples.activity", "1.0.0") - -FEED_KEY = "feed" -MAX_ENTRIES = 100 - - -def store(call): - host = call.host() - if host is None: - # Retryable: the Host API may just be unreachable for a moment. - raise PluginError(ErrorCode.UNAVAILABLE, "the Host API is not available to this plugin") - return host - - -def describe(ev, include_answers: bool) -> dict | None: - d = ev.data or {} - title = d.get("title") or d.get("fileName") or d.get("knowledgeId", "") - if ev.type == "knowledge.ingested": - return {"kind": "ingested", "text": title} - if ev.type == "knowledge.failed": - return {"kind": "failed", "text": f"{title}: {d.get('error', '')}".strip(": ")} - if ev.type == "knowledge.deleted": - return {"kind": "deleted", "text": title} - if ev.type == "chat.answered": - text = (d.get("question") or "").strip()[:200] - if include_answers: - first = (d.get("answer") or "").strip().splitlines() - text += f" → {first[0][:200]}" if first else "" - return {"kind": "answered", "text": text} - return None # a type added after this plugin was written - - -def append(host, entry: dict) -> None: - feed = host.kv_get(FEED_KEY, []) - # Deliveries repeat after a failure; the event ID makes them harmless. - if any(e.get("id") == entry["id"] for e in feed): - return - host.kv_put(FEED_KEY, ([entry] + feed)[:MAX_ENTRIES]) - - -@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() diff --git a/examples/plugins/activity/package.sh b/examples/plugins/activity/package.sh deleted file mode 100755 index 7de66995..00000000 --- a/examples/plugins/activity/package.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env bash -# Packages the activity example plugin as a .wkp: plugin.yaml, schemas, -# main.py, the page under ui/ with the bridge library, and the Python SDK -# under vendor/. -set -euo pipefail -cd "$(dirname "$0")" -version=$(sed -n 's/^version: //p' plugin.yaml) -out=${1:-"weknora-examples-activity-${version}.wkp"} -stage=$(mktemp -d) -trap 'rm -rf "$stage"' EXIT -cp plugin.yaml main.py "$stage"/ -cp -r ui schemas "$stage"/ -cp ../../../packages/plugin-ui/index.js "$stage/ui/weknora-plugin-ui.js" -mkdir -p "$stage/vendor" -cp -r ../../../pluginsdk/python/src/weknora_plugin "$stage/vendor/" -find "$stage" -name __pycache__ -prune -exec rm -rf {} + -rm -f "$out" -(cd "$stage" && zip -qr - .) > "$out" -echo "wrote $out" diff --git a/examples/plugins/activity/plugin.yaml b/examples/plugins/activity/plugin.yaml deleted file mode 100644 index 2674f151..00000000 --- a/examples/plugins/activity/plugin.yaml +++ /dev/null @@ -1,30 +0,0 @@ -schemaVersion: 1 -id: weknora-examples.activity -version: 1.0.0 -apiVersion: weknora.plugin/v1 -name: { en-US: Activity feed, zh-CN: 动态 } -description: - en-US: A feed of what happens in the workspace (documents ingested, failed, deleted, questions answered), plus notes other systems post through a webhook. Shows events and webhooks, written in Python. - zh-CN: 空间动态:文档入库、失败、删除、问答完成,以及外部系统经 Webhook 推送的消息。演示事件与 Webhook,用 Python 编写。 -publisher: { id: weknora-examples, name: WeKnora examples } -homepage: https://github.com/Tencent/WeKnora/tree/main/examples/plugins/activity -license: MIT -runtime: { type: host, kind: python, entry: main.py } -config: - tenant: schemas/tenant.yaml -permissions: - hostApi: [kv] - # Answers include the question and the answer text. - events: [knowledge.ingested, knowledge.failed, knowledge.deleted, chat.answered] -contributes: - pages: - - id: feed - name: { en-US: Activity, zh-CN: 动态 } - description: { en-US: What happened in this workspace lately, zh-CN: 本空间最近的动态 } - entry: ui/index.html - webhooks: - - id: notes - name: { en-US: Post a note, zh-CN: 推送消息 } - description: - en-US: "POST {\"text\": \"...\"} with the header X-Activity-Secret set to the secret in this plugin's settings." - zh-CN: "POST {\"text\": \"...\"},并带上请求头 X-Activity-Secret,值为本插件设置中的密钥。" diff --git a/examples/plugins/activity/schemas/tenant.yaml b/examples/plugins/activity/schemas/tenant.yaml deleted file mode 100644 index 015cbeb7..00000000 --- a/examples/plugins/activity/schemas/tenant.yaml +++ /dev/null @@ -1,18 +0,0 @@ -type: object -properties: - webhook_secret: - type: string - title: Webhook secret - description: Callers of the note webhook send it in the X-Activity-Secret header - x-secret: true - x-i18n: - title: { zh-CN: Webhook 密钥 } - description: { zh-CN: 调用推送消息 Webhook 时放在 X-Activity-Secret 请求头里 } - include_answers: - type: boolean - title: Show answer text - description: Keep the first line of each answer in the feed, not just the question - default: false - x-i18n: - title: { zh-CN: 显示回答内容 } - description: { zh-CN: 在动态里保留每个回答的第一行,而不只是问题 } diff --git a/examples/plugins/activity/test_main.py b/examples/plugins/activity/test_main.py deleted file mode 100644 index 4b8733cc..00000000 --- a/examples/plugins/activity/test_main.py +++ /dev/null @@ -1,66 +0,0 @@ -import json -import os -import sys -import unittest - -HERE = os.path.dirname(os.path.abspath(__file__)) -sys.path.insert(0, os.path.join(HERE, "..", "..", "..", "pluginsdk", "python", "src")) -sys.path.insert(0, HERE) - -import main # noqa: E402 -from weknora_plugin import EventDelivery, UIRequest, WebhookRequest # noqa: E402 -from weknora_plugin.plugin import Call # noqa: E402 - - -class FakeHost: - def __init__(self): - self.data = {} - - def kv_get(self, key, default=None): - return self.data.get(key, default) - - def kv_put(self, key, value, ttl=0): - self.data[key] = value - - -def call(**tenant): - return Call({"context": {"tenantId": 1}, "config": {"tenant": tenant}}) - - -class ActivityTest(unittest.TestCase): - def setUp(self): - self.host = FakeHost() - main.store = lambda c: self.host - - def feed(self): - return self.host.data.get("feed", []) - - def test_events_are_recorded_once(self): - ev = EventDelivery(id="e1", type="knowledge.ingested", data={"knowledgeId": "k1", "title": "Spec"}) - main.on_event(call(), ev) - main.on_event(call(), ev) # a retry after a failure - main.on_event(call(), EventDelivery(id="e2", type="some.future.type", data={})) - self.assertEqual([(e["id"], e["kind"], e["text"]) for e in self.feed()], [("e1", "ingested", "Spec")]) - - def test_answers(self): - ev = EventDelivery(id="a", type="chat.answered", data={"question": "What is RAG?", "answer": "Retrieval.\nMore"}) - main.on_event(call(), ev) - main.on_event(call(include_answers=True), EventDelivery(id="b", type="chat.answered", data=ev.data)) - self.assertEqual([e["text"] for e in self.feed()], ["What is RAG? → Retrieval.", "What is RAG?"]) - - def test_note_webhook(self): - req = WebhookRequest(method="POST", headers={"X-Activity-Secret": "s3"}, body=json.dumps({"text": "Deploy done"}).encode()) - self.assertEqual(main.notes(call(webhook_secret="s3"), req), {"ok": True}) - self.assertEqual(self.feed()[0]["text"], "Deploy done") - self.assertEqual(main.notes(call(webhook_secret="other"), req).status, 401) - self.assertEqual(main.notes(call(), req).status, 401) - self.assertEqual(main.notes(call(webhook_secret="s3"), WebhookRequest(headers={"X-Activity-Secret": "s3"}, body=b"{}")).status, 400) - - def test_page_reads_the_feed(self): - main.on_event(call(), EventDelivery(id="e1", type="knowledge.deleted", data={"title": "Old"})) - self.assertEqual(main.ui(call(), UIRequest(method="GET", path="/feed")).body[0]["kind"], "deleted") - self.assertEqual(main.ui(call(), UIRequest(method="GET", path="/other")).status, 404) - - -if __name__ == "__main__": - unittest.main() diff --git a/examples/plugins/activity/ui/app.css b/examples/plugins/activity/ui/app.css deleted file mode 100644 index 559e3cd9..00000000 --- a/examples/plugins/activity/ui/app.css +++ /dev/null @@ -1,23 +0,0 @@ -:root { - --fg: var(--wk-text-color-primary, #1f2329); - --muted: var(--wk-text-color-secondary, #646a73); - --line: var(--wk-component-border, #dee0e3); - --card: var(--wk-bg-color-container, #fff); - --brand: var(--wk-brand-color, #0052d9); - --danger: var(--wk-error-color, #d54941); - --ok: var(--wk-success-color, #2ba471); - --radius: var(--wk-radius-default, 6px); -} -:root[data-theme='dark'] { color-scheme: dark; } -body { margin: 0; font: 14px/1.5 var(--wk-font-family, system-ui, sans-serif); color: var(--fg); background: transparent; } -main { padding: 4px 2px 12px; } -.bar { display: flex; justify-content: flex-end; margin-bottom: 8px; } -button { padding: 4px 12px; font: inherit; border-radius: var(--radius); cursor: pointer; border: 1px solid var(--line); background: var(--card); color: var(--fg); } -.empty { color: var(--muted); padding: 24px 0; text-align: center; } -ol { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; } -li { display: flex; gap: 10px; align-items: baseline; padding: 8px 12px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); } -.kind { flex: none; font-size: 12px; padding: 0 6px; border-radius: 4px; background: color-mix(in srgb, var(--brand) 12%, transparent); color: var(--brand); } -.kind.failed, .kind.deleted { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); } -.kind.ingested { color: var(--ok); background: color-mix(in srgb, var(--ok) 12%, transparent); } -.text { flex: 1; min-width: 0; overflow-wrap: anywhere; } -time { flex: none; color: var(--muted); font-size: 12px; } diff --git a/examples/plugins/activity/ui/feed.js b/examples/plugins/activity/ui/feed.js deleted file mode 100644 index b3e802f7..00000000 --- a/examples/plugins/activity/ui/feed.js +++ /dev/null @@ -1,54 +0,0 @@ -import { connect } from './weknora-plugin-ui.js' - -const LABELS = { - en: { ingested: 'Ingested', failed: 'Failed', deleted: 'Deleted', answered: 'Answered', note: 'Note', empty: 'Nothing yet.', refresh: 'Refresh' }, - zh: { ingested: '已入库', failed: '失败', deleted: '已删除', answered: '已回答', note: '消息', empty: '暂无动态。', refresh: '刷新' }, -} - -const wk = await connect() -const app = document.getElementById('app') -let text = pick(wk.context.locale) -wk.on('locale', (locale) => { - text = pick(locale) - void load() -}) - -function pick(locale) { - return String(locale || '').startsWith('zh') ? LABELS.zh : LABELS.en -} - -function el(tag, props = {}, ...children) { - const node = Object.assign(document.createElement(tag), props) - node.append(...children) - return node -} - -async function load() { - app.setAttribute('aria-busy', 'true') - try { - const { body } = await wk.get('/feed') - render(Array.isArray(body) ? body : []) - } catch (e) { - app.replaceChildren(el('p', { className: 'empty', textContent: e.message })) - } finally { - app.removeAttribute('aria-busy') - } -} - -function render(feed) { - const refresh = el('button', { type: 'button', textContent: text.refresh }) - refresh.addEventListener('click', () => void load()) - const list = el('ol') - for (const entry of feed) { - const at = new Date(entry.at) - list.append( - el('li', {}, - el('span', { className: `kind ${entry.kind}`, textContent: text[entry.kind] || entry.kind }), - el('span', { className: 'text', textContent: entry.text }), - el('time', { dateTime: entry.at, textContent: isNaN(at) ? '' : at.toLocaleString(wk.context.locale) })), - ) - } - app.replaceChildren(el('div', { className: 'bar' }, refresh), feed.length ? list : el('p', { className: 'empty', textContent: text.empty })) -} - -await load() diff --git a/examples/plugins/activity/ui/index.html b/examples/plugins/activity/ui/index.html deleted file mode 100644 index 0cb54f79..00000000 --- a/examples/plugins/activity/ui/index.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - Activity - - - -
- - - diff --git a/examples/plugins/jira/README.md b/examples/plugins/jira/README.md deleted file mode 100644 index 5ef91e4d..00000000 --- a/examples/plugins/jira/README.md +++ /dev/null @@ -1,64 +0,0 @@ -# Jira example plugin - -Syncs the issues of selected Jira Cloud projects, with their comments, into a -knowledge base. It also ships a `jira-triage` skill that looks for duplicates, -related work and owners among the synced issues. - -It is the fullest example plugin. It shows: - -- a connector with incremental sync, checkpoints and deletions; -- agent tools the plugin serves itself (`search_issues`, `get_issue`), with - result views: a table of issues, and the issue as Markdown; -- an OAuth field (`x-oauth`): WeKnora runs Atlassian's consent flow and hands - the plugin a fresh access token at every call; -- dynamic choices (`x-options`): the sites the account can reach and the - site's issue types; -- form messages in the user's language (`call.Locale`); -- a skill in the package. - -## Install - -1. Build the package with `./package.sh` and install it under **System - administration → Plugin management**. -2. To let workspaces sign in with their Atlassian accounts, register an app - first. Without it, workspaces can still use an API token. - 1. In the [Atlassian developer console](https://developer.atlassian.com/console/myapps/), - create an OAuth 2.0 (3LO) app. - 2. Under **Permissions → Jira API**, add `read:jira-work` and - `read:jira-user`. - 3. Under **Authorization**, set the callback URL to - `/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:** ` `. -- **Facts:** type, status, priority, resolution, assignee, reporter, labels, - dates, parent, and a link. -- **Description**, converted from Atlassian Document Format. -- **Comments**, when enabled. - -Attachments are not synced. - -**Incremental syncs** fetch the issues updated since the newest one seen, in -the user's time zone, as JQL requires. - -**Full syncs** also remove the documents of issues that were deleted, moved -out of the selected projects, or no longer match the filters. - -## Test - -```bash -go test ./examples/plugins/jira/ -``` - -The tests run the plugin against a fake Jira Cloud site, both directly with -an API token and through the OAuth gateway. diff --git a/examples/plugins/jira/adf.go b/examples/plugins/jira/adf.go deleted file mode 100644 index fceebbfd..00000000 --- a/examples/plugins/jira/adf.go +++ /dev/null @@ -1,213 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "strings" -) - -// adfNode is a node of the Atlassian Document Format, the JSON Jira Cloud -// returns rich text in (descriptions, comments). -type adfNode struct { - Type string `json:"type"` - Text string `json:"text"` - Attrs map[string]any `json:"attrs"` - Marks []adfMark `json:"marks"` - Content []adfNode `json:"content"` -} - -type adfMark struct { - Type string `json:"type"` - Attrs map[string]any `json:"attrs"` -} - -// adfToMarkdown renders ADF as Markdown. Unknown nodes keep their text, so -// nothing a new node type holds is lost from search. Plain strings (Jira's -// v2 API, old comments) pass through. -func adfToMarkdown(raw json.RawMessage) string { - if len(raw) == 0 || string(raw) == "null" { - return "" - } - var s string - if json.Unmarshal(raw, &s) == nil { - return strings.TrimSpace(s) - } - var doc adfNode - if err := json.Unmarshal(raw, &doc); err != nil { - return "" - } - var b strings.Builder - blocks(&b, doc.Content, "") - return strings.TrimSpace(b.String()) -} - -// blocks writes block nodes, each line prefixed with indent (list nesting, -// quotes). -func blocks(b *strings.Builder, nodes []adfNode, indent string) { - for _, n := range nodes { - switch n.Type { - case "paragraph": - line(b, indent, inline(n.Content)) - case "heading": - level := 1 - if v, ok := n.Attrs["level"].(float64); ok && v >= 1 && v <= 6 { - level = int(v) - } - line(b, indent, strings.Repeat("#", level)+" "+inline(n.Content)) - case "bulletList", "orderedList": - for i, item := range n.Content { - marker := "- " - if n.Type == "orderedList" { - marker = fmt.Sprintf("%d. ", i+1) - } - listItem(b, item, indent, marker) - } - b.WriteString("\n") - case "codeBlock": - lang, _ := n.Attrs["language"].(string) - b.WriteString(indent + "```" + lang + "\n") - for _, l := range strings.Split(plain(n.Content), "\n") { - b.WriteString(indent + l + "\n") - } - b.WriteString(indent + "```\n\n") - case "blockquote": - blocks(b, n.Content, indent+"> ") - case "rule": - line(b, indent, "---") - case "table": - table(b, n, indent) - case "panel", "expand", "nestedExpand", "layoutSection", "layoutColumn", "bodiedExtension": - if title, _ := n.Attrs["title"].(string); title != "" { - line(b, indent, "**"+title+"**") - } - blocks(b, n.Content, indent) - case "mediaSingle", "mediaGroup", "media": - // Attachments are not synced; say where one was. - line(b, indent, "[attachment]") - default: - if len(n.Content) > 0 { - blocks(b, n.Content, indent) - } else if t := inline([]adfNode{n}); t != "" { - line(b, indent, t) - } - } - } -} - -func line(b *strings.Builder, indent, text string) { - b.WriteString(indent + text + "\n\n") -} - -func listItem(b *strings.Builder, item adfNode, indent, marker string) { - first := true - for _, child := range item.Content { - switch child.Type { - case "paragraph": - prefix := indent + strings.Repeat(" ", len(marker)) - if first { - prefix = indent + marker - } - b.WriteString(prefix + inline(child.Content) + "\n") - default: - // Nested lists and other blocks go one level in. - var sub strings.Builder - blocks(&sub, []adfNode{child}, indent+" ") - b.WriteString(strings.TrimRight(sub.String(), "\n") + "\n") - } - first = false - } -} - -func table(b *strings.Builder, n adfNode, indent string) { - for i, row := range n.Content { - cells := make([]string, 0, len(row.Content)) - for _, cell := range row.Content { - var parts []string - for _, p := range cell.Content { - if t := strings.TrimSpace(inline(p.Content)); t != "" { - parts = append(parts, t) - } - } - cells = append(cells, strings.ReplaceAll(strings.Join(parts, " "), "|", "\\|")) - } - b.WriteString(indent + "| " + strings.Join(cells, " | ") + " |\n") - if i == 0 { - b.WriteString(indent + "|" + strings.Repeat(" --- |", len(cells)) + "\n") - } - } - b.WriteString("\n") -} - -// inline renders inline nodes with their marks. -func inline(nodes []adfNode) string { - var b strings.Builder - for _, n := range nodes { - switch n.Type { - case "text": - b.WriteString(marked(n.Text, n.Marks)) - case "hardBreak": - b.WriteString(" \n") - case "mention": - text, _ := n.Attrs["text"].(string) - if !strings.HasPrefix(text, "@") { - text = "@" + text - } - b.WriteString(text) - case "emoji": - text, _ := n.Attrs["text"].(string) - if text == "" { - text, _ = n.Attrs["shortName"].(string) - } - b.WriteString(text) - case "inlineCard", "blockCard": - u, _ := n.Attrs["url"].(string) - b.WriteString(u) - case "status": - text, _ := n.Attrs["text"].(string) - b.WriteString("[" + text + "]") - case "date": - if ts, ok := n.Attrs["timestamp"].(string); ok { - b.WriteString(ts) - } - default: - b.WriteString(inline(n.Content)) - } - } - return b.String() -} - -func marked(text string, marks []adfMark) string { - if strings.TrimSpace(text) == "" { - return text - } - for _, m := range marks { - switch m.Type { - case "strong": - text = "**" + text + "**" - case "em": - text = "*" + text + "*" - case "strike": - text = "~~" + text + "~~" - case "code": - text = "`" + text + "`" - case "link": - if href, _ := m.Attrs["href"].(string); href != "" { - text = "[" + text + "](" + href + ")" - } - } - } - return text -} - -// plain is the text of nodes without formatting (code blocks). -func plain(nodes []adfNode) string { - var b strings.Builder - for _, n := range nodes { - if n.Type == "hardBreak" { - b.WriteString("\n") - } - b.WriteString(n.Text) - b.WriteString(plain(n.Content)) - } - return b.String() -} diff --git a/examples/plugins/jira/client.go b/examples/plugins/jira/client.go deleted file mode 100644 index cfbc7d97..00000000 --- a/examples/plugins/jira/client.go +++ /dev/null @@ -1,374 +0,0 @@ -package main - -import ( - "bytes" - "context" - "encoding/base64" - "encoding/json" - "fmt" - "io" - "net/http" - "net/url" - "strconv" - "strings" - "time" - - "github.com/Tencent/WeKnora/pluginsdk/pluginapi" -) - -// gatewayURL is Atlassian's API gateway, where OAuth tokens are used. Tests -// point it at a fake. -var gatewayURL = "https://api.atlassian.com" - -// httpClient honors HTTPS_PROXY: a host plugin's traffic must go through the -// plugin host's egress proxy. -var httpClient = &http.Client{ - Timeout: 30 * time.Second, - Transport: &http.Transport{Proxy: http.ProxyFromEnvironment}, -} - -// Credential fields, as the instance schema names them. -type credentials struct { - Auth string `json:"auth"` - Account string `json:"account"` // an OAuth access token by the time it reaches us - Site string `json:"site"` // cloud ID - BaseURL string `json:"base_url"` - Email string `json:"email"` - APIToken string `json:"api_token"` -} - -// client calls one Jira Cloud site's REST API. -type client struct { - api string // REST root: the site, or the gateway's /ex/jira/{cloudId} - siteURL string // for links to issues - auth string // Authorization header -} - -// site is a Jira site an OAuth token can reach. -type site struct { - ID string `json:"id"` - URL string `json:"url"` - Name string `json:"name"` -} - -// accessibleSites lists the sites an OAuth token was granted. -func accessibleSites(ctx context.Context, token string) ([]site, error) { - c := &client{api: gatewayURL, auth: "Bearer " + token} - var sites []site - err := c.do(ctx, http.MethodGet, "/oauth/token/accessible-resources", nil, &sites) - return sites, err -} - -// newClient checks the credentials and connects to the site they name. -// Problems with the form come back as invalid_config on its fields. -func newClient(ctx context.Context, cr credentials, locale string) (*client, error) { - switch cr.Auth { - case "", "oauth": - if cr.Account == "" { - return nil, pluginapi.InvalidConfig(say(locale, msgConnectAccount), - map[string]string{"credentials.account": "required"}) - } - if cr.Site == "" { - return nil, pluginapi.InvalidConfig(say(locale, msgChooseSite), - map[string]string{"credentials.site": "required"}) - } - sites, err := accessibleSites(ctx, cr.Account) - if err != nil { - return nil, err - } - for _, s := range sites { - if s.ID == cr.Site { - return &client{ - api: gatewayURL + "/ex/jira/" + url.PathEscape(s.ID), - siteURL: strings.TrimSuffix(s.URL, "/"), - auth: "Bearer " + cr.Account, - }, nil - } - } - return nil, pluginapi.InvalidConfig(say(locale, msgSiteGone), - map[string]string{"credentials.site": "not granted"}) - case "token": - base := strings.TrimSuffix(strings.TrimSpace(cr.BaseURL), "/") - fields := map[string]string{} - if base == "" { - fields["credentials.base_url"] = "required" - } - if cr.Email == "" { - fields["credentials.email"] = "required" - } - if cr.APIToken == "" { - fields["credentials.api_token"] = "required" - } - if len(fields) > 0 { - return nil, pluginapi.InvalidConfig(say(locale, msgTokenFields), fields) - } - basic := cr.Email + ":" + cr.APIToken - return &client{ - api: base, siteURL: base, - auth: "Basic " + base64.StdEncoding.EncodeToString([]byte(basic)), - }, nil - default: - return nil, pluginapi.InvalidConfig(say(locale, msgUnknownAuth), - map[string]string{"credentials.auth": "enum"}) - } -} - -// do sends one request and decodes a JSON answer. Jira's failures map to -// protocol errors: WeKnora retries unavailable and rate_limited, and shows -// unauthorized as broken credentials. -func (c *client) do(ctx context.Context, method, path string, body, out any) error { - var rd io.Reader - if body != nil { - b, err := json.Marshal(body) - if err != nil { - return err - } - rd = bytes.NewReader(b) - } - req, err := http.NewRequestWithContext(ctx, method, c.api+path, rd) - if err != nil { - return err - } - req.Header.Set("Authorization", c.auth) - req.Header.Set("Accept", "application/json") - if body != nil { - req.Header.Set("Content-Type", "application/json") - } - resp, err := httpClient.Do(req) - if err != nil { - return pluginapi.Errorf(pluginapi.CodeUnavailable, "jira: %v", err) - } - defer func() { _ = resp.Body.Close() }() - data, err := io.ReadAll(io.LimitReader(resp.Body, 32<<20)) - if err != nil { - return pluginapi.Errorf(pluginapi.CodeUnavailable, "jira: %v", err) - } - if resp.StatusCode >= 300 { - return statusError(resp, data) - } - if out == nil { - return nil - } - if err := json.Unmarshal(data, out); err != nil { - return fmt.Errorf("jira %s: decode: %w", path, err) - } - return nil -} - -func statusError(resp *http.Response, body []byte) error { - msg := jiraMessage(body) - switch { - case resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden: - return pluginapi.Errorf( - pluginapi.CodeUnauthorized, - "jira refused the credentials (%d): %s", - resp.StatusCode, - msg, - ) - case resp.StatusCode == http.StatusTooManyRequests: - e := pluginapi.Errorf(pluginapi.CodeRateLimited, "jira rate limit: %s", msg) - if s, err := strconv.Atoi(resp.Header.Get("Retry-After")); err == nil && s > 0 { - e.Details = &pluginapi.ErrorDetails{RetryAfter: s} - } - return e - case resp.StatusCode >= 500: - return pluginapi.Errorf(pluginapi.CodeUnavailable, "jira %d: %s", resp.StatusCode, msg) - case resp.StatusCode == http.StatusNotFound: - return pluginapi.Errorf(pluginapi.CodeNotFound, "jira 404: %s", msg) - case resp.StatusCode == http.StatusBadRequest: - // Mostly a JQL filter Jira does not accept. - return pluginapi.InvalidConfig("jira rejected the query: "+msg, - map[string]string{"settings.jql": msg}) - default: - return fmt.Errorf("jira %d: %s", resp.StatusCode, msg) - } -} - -// jiraMessage extracts Jira's error text ({"errorMessages": [...]}). -func jiraMessage(body []byte) string { - var e struct { - ErrorMessages []string `json:"errorMessages"` - Errors map[string]string `json:"errors"` - Message string `json:"message"` - } - if json.Unmarshal(body, &e) == nil { - parts := append([]string{}, e.ErrorMessages...) - for k, v := range e.Errors { - parts = append(parts, k+": "+v) - } - if e.Message != "" { - parts = append(parts, e.Message) - } - if len(parts) > 0 { - return strings.Join(parts, "; ") - } - } - s := strings.TrimSpace(string(body)) - if len(s) > 200 { - s = s[:200] - } - return s -} - -// myself is the signed-in user. Its time zone is the one JQL dates are in. -type myself struct { - AccountID string `json:"accountId"` - DisplayName string `json:"displayName"` - TimeZone string `json:"timeZone"` -} - -func (c *client) myself(ctx context.Context) (*myself, error) { - var me myself - if err := c.do(ctx, http.MethodGet, "/rest/api/3/myself", nil, &me); err != nil { - return nil, err - } - return &me, nil -} - -type project struct { - ID string `json:"id"` - Key string `json:"key"` - Name string `json:"name"` - Description string `json:"description"` - Style string `json:"style"` -} - -// projects lists every project the user can browse. -func (c *client) projects(ctx context.Context) ([]project, error) { - var out []project - for start := 0; ; { - var page struct { - Values []project `json:"values"` - IsLast bool `json:"isLast"` - } - q := url.Values{"startAt": {strconv.Itoa(start)}, "maxResults": {"50"}, "orderBy": {"name"}} - if err := c.do(ctx, http.MethodGet, "/rest/api/3/project/search?"+q.Encode(), nil, &page); err != nil { - return nil, err - } - out = append(out, page.Values...) - if page.IsLast || len(page.Values) == 0 { - return out, nil - } - start += len(page.Values) - } -} - -type issueType struct { - Name string `json:"name"` - Subtask bool `json:"subtask"` -} - -func (c *client) issueTypes(ctx context.Context) ([]issueType, error) { - var out []issueType - err := c.do(ctx, http.MethodGet, "/rest/api/3/issuetype", nil, &out) - return out, err -} - -// issue is the part of a Jira issue the connector turns into a document. -type issue struct { - ID string `json:"id"` - Key string `json:"key"` - Fields struct { - Summary string `json:"summary"` - Description json.RawMessage `json:"description"` - Status *named `json:"status"` - IssueType *named `json:"issuetype"` - Priority *named `json:"priority"` - Resolution *named `json:"resolution"` - Assignee *user `json:"assignee"` - Reporter *user `json:"reporter"` - Labels []string `json:"labels"` - Created jiraTime `json:"created"` - Updated jiraTime `json:"updated"` - Project *project `json:"project"` - Parent *struct { - Key string `json:"key"` - } `json:"parent"` - Comment *struct { - Comments []comment `json:"comments"` - Total int `json:"total"` - } `json:"comment"` - } `json:"fields"` -} - -type named struct { - Name string `json:"name"` -} - -type user struct { - DisplayName string `json:"displayName"` -} - -type comment struct { - Author *user `json:"author"` - Created jiraTime `json:"created"` - Body json.RawMessage `json:"body"` -} - -// jiraTime parses Jira's timestamps ("2026-09-26T10:04:05.123+0800"). -type jiraTime struct{ time.Time } - -func (t *jiraTime) UnmarshalJSON(b []byte) error { - var s string - if err := json.Unmarshal(b, &s); err != nil || s == "" { - return nil - } - for _, layout := range []string{"2006-01-02T15:04:05.000-0700", time.RFC3339Nano} { - if v, err := time.Parse(layout, s); err == nil { - t.Time = v - return nil - } - } - return nil -} - -var searchFields = []string{ - "summary", "description", "status", "issuetype", "priority", "resolution", "assignee", "reporter", - "labels", "created", "updated", "project", "parent", "comment", -} - -// searchPage runs JQL for at most limit issues, without comments, and says -// whether more matched. -func (c *client) searchPage(ctx context.Context, jql string, limit int) ([]issue, bool, error) { - body := map[string]any{"jql": jql, "maxResults": limit, "fields": searchFields[:len(searchFields)-1]} - var page struct { - Issues []issue `json:"issues"` - NextPageToken string `json:"nextPageToken"` - IsLast bool `json:"isLast"` - } - if err := c.do(ctx, http.MethodPost, "/rest/api/3/search/jql", body, &page); err != nil { - return nil, false, err - } - return page.Issues, !page.IsLast && page.NextPageToken != "", nil -} - -// search runs JQL a page at a time (the enhanced search API, which pages -// with a token). fn sees each page; returning an error stops the search. -func (c *client) search(ctx context.Context, jql string, withComments bool, fn func([]issue) error) error { - fields := searchFields - if !withComments { - fields = fields[:len(fields)-1] - } - token := "" - for { - body := map[string]any{"jql": jql, "maxResults": 50, "fields": fields} - if token != "" { - body["nextPageToken"] = token - } - var page struct { - Issues []issue `json:"issues"` - NextPageToken string `json:"nextPageToken"` - IsLast bool `json:"isLast"` - } - if err := c.do(ctx, http.MethodPost, "/rest/api/3/search/jql", body, &page); err != nil { - return err - } - if err := fn(page.Issues); err != nil { - return err - } - if page.IsLast || page.NextPageToken == "" || len(page.Issues) == 0 { - return nil - } - token = page.NextPageToken - } -} diff --git a/examples/plugins/jira/icon.svg b/examples/plugins/jira/icon.svg deleted file mode 100644 index 7d7b2941..00000000 --- a/examples/plugins/jira/icon.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/examples/plugins/jira/jira_test.go b/examples/plugins/jira/jira_test.go deleted file mode 100644 index cc8c7774..00000000 --- a/examples/plugins/jira/jira_test.go +++ /dev/null @@ -1,589 +0,0 @@ -package main - -import ( - "bytes" - "context" - "encoding/json" - "fmt" - "go/parser" - "go/token" - "net/http" - "net/http/httptest" - "os" - "path/filepath" - "regexp" - "sort" - "strconv" - "strings" - "sync" - "testing" - "time" - - pluginclient "github.com/Tencent/WeKnora/pluginsdk/client" - "github.com/Tencent/WeKnora/pluginsdk/conformance" - "github.com/Tencent/WeKnora/pluginsdk/pluginapi" -) - -const ( - oauthToken = "access-token-1" - cloudID = "cloud-1" - email = "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:" reference for a token. - return map[string]any{ - "credentials": map[string]any{"auth": "oauth", "account": oauthToken, "site": site}, - "settings": map[string]any{}, - } -} - -type fetched struct { - items []pluginapi.FetchedItem - checkpoints int - cursor *pluginapi.Cursor -} - -func (r *run) fetch(instance map[string]any, mode string, cursor *pluginapi.Cursor, projects ...string) fetched { - r.t.Helper() - var out fetched - end, err := r.client.Stream(context.Background(), pluginapi.ConnectorFetchPath("jira"), - pluginapi.Envelope{Config: pluginapi.Config{Instance: instance}}, - pluginapi.FetchInput{Mode: mode, Cursor: cursor, ResourceIDs: projects}, - func(ev pluginapi.Event) error { - switch ev.Type { - case pluginapi.EventItem: - var it pluginapi.FetchedItem - if err := json.Unmarshal(ev.Data, &it); err != nil { - return err - } - out.items = append(out.items, it) - case pluginapi.EventCheckpoint: - out.checkpoints++ - } - return nil - }) - if err != nil { - r.t.Fatalf("fetch: %v", err) - } - out.cursor = &pluginapi.Cursor{} - if err := json.Unmarshal(end, out.cursor); err != nil { - r.t.Fatal(err) - } - return out -} - -func (r *run) call(path string, instance map[string]any, input, out any) error { - return r.client.Call(context.Background(), path, - pluginapi.Envelope{Config: pluginapi.Config{Instance: instance}}, input, out) -} - -func summary(items []pluginapi.FetchedItem) []string { - var out []string - for _, it := range items { - if it.IsDeleted { - out = append(out, "-"+it.ExternalID) - } else { - out = append(out, it.ExternalID+" "+it.Title) - } - } - return out -} - -func TestSyncIsIncrementalAndReportsDeletions(t *testing.T) { - f := newFakeJira(t) - f.put("101", "ENG-1", "Login fails", "2026-09-20T10:00:00.000+0800") - f.put("102", "ENG-2", "Slow search", "2026-09-21T10:00:30.000+0800") - f.put("103", "ENG-3", "Crash on save", "2026-09-22T10:00:00.000+0800") - f.put("201", "OPS-1", "Not selected", "2026-09-22T10:00:00.000+0800") - r := newRun(t) - inst := tokenInstance(f, map[string]any{"issue_types": []string{"Bug", `Odd "type"`}, "jql": "labels = x"}) - - full := r.fetch(inst, pluginapi.FetchFull, nil, "ENG") - if got := summary(full.items); strings.Join( - got, - "|", - ) != "101 ENG-1 Login fails|102 ENG-2 Slow search|103 ENG-3 Crash on save" { - t.Fatalf("full sync = %v", got) - } - if full.checkpoints != 2 { - t.Fatalf("a checkpoint per page, got %d", full.checkpoints) - } - wantJQL := `project = "ENG" AND issuetype in ("Bug", "Odd \"type\"") AND (labels = x) ORDER BY updated ASC` - if jql := f.lastJQL(); jql != wantJQL { - t.Fatalf("jql = %s", jql) - } - doc := string(full.items[0].Content) - for _, want := range []string{ - "# ENG-1: Login fails", "- **Status:** In Progress", "- **Link:** " + f.URL + "/browse/ENG-1", - "## Description\n\nSteps to **reproduce**", "### Ann · 2026-09-02 09:00 +0800\n\nSeen on staging", - "_2 more comments are not included._", - } { - if !strings.Contains(doc, want) { - t.Errorf("document lacks %q:\n%s", want, doc) - } - } - if it := full.items[0]; it.SourceResourceID != "ENG" || it.Metadata["jira_key"] != "ENG-1" || - it.FileName != "ENG-1 Login fails.md" || it.UpdatedAt == nil { - t.Fatalf("item = %+v", it) - } - - // Incremental: from the newest update seen, in the user's time zone. - f.put("102", "ENG-2", "Slow search (again)", "2026-09-25T08:00:00.000+0800") - inc := r.fetch(inst, pluginapi.FetchIncremental, full.cursor, "ENG") - if !strings.Contains(f.lastJQL(), `updated >= "2026/09/22 10:00"`) { - t.Fatalf("incremental jql = %s", f.lastJQL()) - } - if got := summary(inc.items); strings.Join(got, "|") != "103 ENG-3 Crash on save|102 ENG-2 Slow search (again)" { - t.Fatalf("incremental = %v", got) - } - - // A full sync reports what disappeared, once. - f.remove("101") - again := r.fetch(inst, pluginapi.FetchFull, inc.cursor, "ENG") - if got := summary(again.items); got[len(got)-1] != "-101" || len(got) != 3 { - t.Fatalf("full sync after a deletion = %v", got) - } - once := r.fetch(inst, pluginapi.FetchFull, again.cursor, "ENG") - for _, it := range once.items { - if it.IsDeleted { - t.Fatalf("deletion reported twice: %v", summary(once.items)) - } - } -} - -func TestOAuthAccountsReachTheirSites(t *testing.T) { - f := newFakeJira(t) - f.put("101", "ENG-1", "Login fails", "2026-09-20T10:00:00.000+0800") - r := newRun(t) - - var sites pluginapi.OptionsOutput - err := r.call(pluginapi.OptionsPath("sites"), oauthInstance(""), pluginapi.OptionsInput{Field: "site"}, &sites) - if err != nil { - t.Fatal(err) - } - if len(sites.Options) != 1 || sites.Options[0].Value != cloudID || sites.Options[0].Label != "Acme" { - t.Fatalf("sites = %+v", sites) - } - var types pluginapi.OptionsOutput - err = r.call(pluginapi.OptionsPath("issue_types"), oauthInstance(cloudID), pluginapi.OptionsInput{}, &types) - if err != nil { - t.Fatal(err) - } - if fmt.Sprint(types.Options) != "[{Bug } {Sub-task } {Task }]" { - t.Fatalf("issue types = %v", types.Options) - } - var res pluginapi.ListResourcesOutput - listPath := pluginapi.ConnectorListResourcesPath("jira") - err = r.call(listPath, oauthInstance(cloudID), pluginapi.ListResourcesInput{}, &res) - if err != nil { - t.Fatal(err) - } - if len(res.Resources) != 2 || res.Resources[1].ExternalID != "OPS" || res.Resources[0].URL != f.URL+"/browse/ENG" { - t.Fatalf("projects = %+v", res.Resources) - } - got := r.fetch(oauthInstance(cloudID), pluginapi.FetchFull, nil, "ENG") - if len(got.items) != 1 || got.items[0].URL != f.URL+"/browse/ENG-1" { - t.Fatalf("oauth sync = %v", summary(got.items)) - } -} - -func TestProblemsReachTheForm(t *testing.T) { - f := newFakeJira(t) - r := newRun(t) - cases := []struct { - name string - instance map[string]any - code pluginapi.ErrorCode - }{ - {"no account", map[string]any{"credentials": map[string]any{"auth": "oauth"}}, pluginapi.CodeInvalidConfig}, - {"site not granted", oauthInstance("cloud-2"), pluginapi.CodeInvalidConfig}, - { - "expired token", - map[string]any{"credentials": map[string]any{"auth": "oauth", "account": "old", "site": cloudID}}, - pluginapi.CodeUnauthorized, - }, - { - "missing token fields", - map[string]any{"credentials": map[string]any{"auth": "token"}}, - pluginapi.CodeInvalidConfig, - }, - {"wrong api token", map[string]any{"credentials": map[string]any{ - "auth": "token", "base_url": f.URL, "email": email, "api_token": "nope", - }}, pluginapi.CodeUnauthorized}, - } - for _, tc := range cases { - err := r.call(pluginapi.ConnectorValidatePath("jira"), tc.instance, struct{}{}, nil) - if pe, ok := pluginapi.AsError(err); !ok || pe.Code != tc.code { - t.Errorf("%s: %v", tc.name, err) - } - } - // Messages about the form speak the user's language. - err := r.client.Call(context.Background(), pluginapi.OptionsPath("sites"), pluginapi.Envelope{ - Context: pluginapi.Context{Locale: "zh-CN"}, - Config: pluginapi.Config{Instance: map[string]any{"credentials": map[string]any{"auth": "oauth"}}}, - }, pluginapi.OptionsInput{}, nil) - if pe, ok := pluginapi.AsError(err); !ok || pe.Message != msgConnectAccount.zh { - t.Fatalf("zh-CN hint = %v", err) - } - if err := r.call(pluginapi.ConnectorValidatePath("jira"), tokenInstance(f, nil), struct{}{}, nil); err != nil { - t.Fatalf("valid token: %v", err) - } - _, err = r.client.Stream( - context.Background(), - pluginapi.ConnectorFetchPath("jira"), - pluginapi.Envelope{Config: pluginapi.Config{Instance: tokenInstance(f, map[string]any{"jql": "BROKEN"})}}, - pluginapi.FetchInput{ - Mode: pluginapi.FetchFull, - ResourceIDs: []string{"ENG"}, - }, - func(pluginapi.Event) error { return nil }, - ) - if pe, ok := pluginapi.AsError(err); !ok || pe.Code != pluginapi.CodeInvalidConfig || - !strings.Contains(pe.Message, "JQL") { - t.Fatalf("bad jql = %v", err) - } -} - -func TestRateLimitsAreRetryable(t *testing.T) { - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - w.Header().Set("Retry-After", "7") - w.WriteHeader(http.StatusTooManyRequests) - })) - defer srv.Close() - c := &client{api: srv.URL} - _, err := c.myself(context.Background()) - pe, ok := pluginapi.AsError(err) - if !ok || pe.Code != pluginapi.CodeRateLimited || !pe.Retryable || pe.Details.RetryAfter != 7 { - t.Fatalf("429 = %v", err) - } -} - -func TestADFToMarkdown(t *testing.T) { - doc, err := os.ReadFile("testdata/adf.json") - if err != nil { - t.Fatal(err) - } - want := "## Plan\n\n- one\n 1. nested\n- [docs](https://example.com) by @Ann\n\n```go\nx := 1\n```\n\n" + - "| a\\|b |\n| --- |\n| 1 |\n\n[attachment]\n\nkept" - if got := adfToMarkdown(doc); got != want { - t.Fatalf("got:\n%s\nwant:\n%s", got, want) - } - if adfToMarkdown(json.RawMessage(`"plain"`)) != "plain" || adfToMarkdown(nil) != "" { - t.Fatal("plain strings pass through") - } -} - -func TestJQLDatesUseTheUsersZone(t *testing.T) { - since := time.Date(2026, 9, 22, 2, 0, 59, 0, time.UTC) - got := buildJQL("ENG", settings{}, since, shanghai) - if got != `project = "ENG" AND updated >= "2026/09/22 10:00" ORDER BY updated ASC` { - t.Fatal(got) - } -} - -// The example must stay an external plugin: the SDK and ordinary libraries, -// nothing from WeKnora's internals. -func TestPluginImportsNoInternals(t *testing.T) { - files, _ := filepath.Glob("*.go") - fset := token.NewFileSet() - for _, f := range files { - if strings.HasSuffix(f, "_test.go") { - continue - } - src, err := os.ReadFile(f) - if err != nil { - t.Fatal(err) - } - parsed, err := parser.ParseFile(fset, f, src, parser.ImportsOnly) - if err != nil { - t.Fatal(err) - } - for _, imp := range parsed.Imports { - if strings.Contains(imp.Path.Value, "github.com/Tencent/WeKnora/internal") { - t.Errorf("%s imports %s", f, imp.Path.Value) - } - } - } -} - -func TestPluginPassesConformance(t *testing.T) { - srv := httptest.NewServer(newPlugin().Handler()) - defer srv.Close() - rep := conformance.Run(context.Background(), conformance.Target{ - Client: pluginclient.New(srv.URL, nil, nil), - Raw: func(ctx context.Context, path string, body []byte) (*http.Response, error) { - req, _ := http.NewRequestWithContext(ctx, http.MethodPost, srv.URL+path, bytes.NewReader(body)) - return http.DefaultClient.Do(req) - }, - }) - for _, r := range rep.Results { - if !r.Passed { - t.Errorf("%s: %s", r.Name, r.Detail) - } - } -} - -func TestAgentTools(t *testing.T) { - f := newFakeJira(t) - f.put("101", "ENG-1", "Login fails", "2026-09-20T10:00:00.000+0800") - f.put("102", "ENG-2", "Login is slow", "2026-09-21T10:00:00.000+0800") - f.put("103", "ENG-3", "Other", "2026-09-22T10:00:00.000+0800") - r := newRun(t) - tenant := map[string]any{"auth": "token", "base_url": f.URL, "email": email, "api_token": apiToken} - call := func(tool string, args string, tenant map[string]any, locale string) pluginapi.ToolResult { - t.Helper() - req := pluginapi.MCPRequest{ - JSONRPC: "2.0", ID: json.RawMessage(`1`), Method: "tools/call", - Params: json.RawMessage(`{"name":"` + tool + `","arguments":` + args + `}`), - } - var out pluginapi.MCPResponse - env := pluginapi.Envelope{Context: pluginapi.Context{Locale: locale}, Config: pluginapi.Config{Tenant: tenant}} - if err := r.client.Call(context.Background(), pluginapi.MCPPath(toolsServer), env, req, &out); err != nil { - t.Fatal(err) - } - var res pluginapi.ToolResult - if out.Error != nil || json.Unmarshal(out.Result, &res) != nil { - t.Fatalf("%s: %+v", tool, out) - } - return res - } - - res := call("search_issues", `{"text":"login","project":"ENG","limit":1}`, tenant, "") - if f.lastJQL() != `project = "ENG" AND text ~ "login" ORDER BY updated DESC` || res.IsError { - t.Fatalf("jql = %s, %+v", f.lastJQL(), res) - } - rows, _ := json.Marshal(res.StructuredContent) - if !strings.Contains(string(rows), `"key":"ENG-1"`) || !strings.Contains(string(rows), `"more":true`) || - !strings.Contains(res.Content[0].Text, "- ENG-1 [In Progress] Login fails (unassigned") { - t.Fatalf("search = %s\n%s", rows, res.Content[0].Text) - } - call("search_issues", `{"jql":"assignee = currentUser()"}`, tenant, "") - if f.lastJQL() != "assignee = currentUser() ORDER BY updated DESC" { - t.Fatalf("jql = %s", f.lastJQL()) - } - if res := call("search_issues", `{}`, tenant, ""); !res.IsError { - t.Fatal("a search needs jql or text") - } - - res = call("get_issue", `{"key":"eng-2"}`, tenant, "") - md, _ := json.Marshal(res.StructuredContent) - if res.IsError || !strings.HasPrefix(res.Content[0].Text, "# ENG-2: Login is slow") || - !strings.Contains(string(md), `"url":"`+f.URL+`/browse/ENG-2"`) { - t.Fatalf("get_issue = %+v", res) - } - if res := call("get_issue", `{"key":"ENG-9"}`, tenant, ""); !res.IsError || - !strings.Contains(res.Content[0].Text, "does not exist") { - t.Fatalf("missing issue = %+v", res) - } - if res := call("get_issue", `{"key":"DROP TABLE"}`, tenant, ""); !res.IsError { - t.Fatal("a malformed key reaches Jira") - } - res = call("search_issues", `{"text":"x"}`, map[string]any{"auth": "oauth"}, "zh-CN") - if !res.IsError || !strings.Contains(res.Content[0].Text, "本空间尚未连接 Jira") { - t.Fatalf("unconfigured = %+v", res) - } - - // The workspace form offers sites and issue types too. - var sites pluginapi.OptionsOutput - err := r.client.Call(context.Background(), pluginapi.OptionsPath("sites"), - pluginapi.Envelope{Config: pluginapi.Config{Tenant: map[string]any{"auth": "oauth", "account": oauthToken}}}, - pluginapi.OptionsInput{Field: "site", Scope: pluginapi.OptionsScopeTenant}, &sites) - if err != nil || len(sites.Options) != 1 || sites.Options[0].Value != cloudID { - t.Fatalf("tenant sites = %+v, %v", sites, err) - } -} diff --git a/examples/plugins/jira/main.go b/examples/plugins/jira/main.go deleted file mode 100644 index 652b0052..00000000 --- a/examples/plugins/jira/main.go +++ /dev/null @@ -1,471 +0,0 @@ -// Command jira syncs Jira Cloud issues into WeKnora knowledge bases. It is the -// fullest example plugin: -// -// - a data source connector with incremental sync, checkpoints and -// deletions; -// - an OAuth field (x-oauth): WeKnora runs Atlassian's consent flow with -// the platform's OAuth app and hands the plugin a fresh access token; -// - dynamic choices (x-options): the sites an account can reach and the -// site's issue types; -// - a skill shipped in the package. -// -// Build a package with ./package.sh; install it under System administration -// → Plugin management. -package main - -import ( - "context" - "encoding/json" - "fmt" - "log" - "slices" - "sort" - "strings" - "time" - "unicode/utf8" - - "github.com/Tencent/WeKnora/pluginsdk" - "github.com/Tencent/WeKnora/pluginsdk/pluginapi" -) - -// Version must match plugin.yaml. -const Version = "1.0.0" - -func main() { - if err := newPlugin().Serve(); err != nil { - log.Fatal(err) - } -} - -func newPlugin() *pluginsdk.Plugin { - p := pluginsdk.New(pluginsdk.Info{ID: "weknora-examples.jira", Version: Version}) - p.Connector("jira", connector{}) - p.Options("sites", siteOptions) - p.Options("issue_types", issueTypeOptions) - registerTools(p) - return p -} - -// settings are the connector's x-group: settings fields. -type settings struct { - IssueTypes []string `json:"issue_types"` - JQL string `json:"jql"` - IncludeComments *bool `json:"include_comments"` -} - -func (s settings) comments() bool { return s.IncludeComments == nil || *s.IncludeComments } - -func decode(cfg pluginsdk.ConnectorConfig) (credentials, settings, error) { - var cr credentials - var st settings - if err := remarshal(cfg.Credentials, &cr); err != nil { - return cr, st, pluginapi.InvalidConfig("credentials: "+err.Error(), nil) - } - if err := remarshal(cfg.Settings, &st); err != nil { - return cr, st, pluginapi.InvalidConfig("settings: "+err.Error(), nil) - } - return cr, st, nil -} - -func remarshal(in, out any) error { - b, err := json.Marshal(in) - if err != nil { - return err - } - return json.Unmarshal(b, out) -} - -type connector struct{} - -// Validate signs in to the site. -func (connector) Validate(ctx context.Context, call *pluginsdk.Call, cfg pluginsdk.ConnectorConfig) error { - cr, _, err := decode(cfg) - if err != nil { - return err - } - c, err := newClient(ctx, cr, call.Locale) - if err != nil { - return err - } - _, err = c.myself(ctx) - return err -} - -// ListResources offers the projects the user can browse. -func (connector) ListResources( - ctx context.Context, call *pluginsdk.Call, cfg pluginsdk.ConnectorConfig, parentID string, -) ([]pluginapi.Resource, error) { - if parentID != "" { - return nil, nil - } - cr, _, err := decode(cfg) - if err != nil { - return nil, err - } - c, err := newClient(ctx, cr, call.Locale) - if err != nil { - return nil, err - } - projects, err := c.projects(ctx) - if err != nil { - return nil, err - } - out := make([]pluginapi.Resource, 0, len(projects)) - for _, p := range projects { - out = append(out, pluginapi.Resource{ - ExternalID: p.Key, Name: p.Name, Type: "project", Description: p.Key, - URL: c.siteURL + "/browse/" + p.Key, - }) - } - return out, nil -} - -// syncState is the cursor: per project, the newest update seen (where the -// next incremental sync starts) and the issues it holds (so a full sync can -// tell which were deleted). -type syncState struct { - Projects map[string]*projectState `json:"projects"` -} - -type projectState struct { - Since time.Time `json:"since"` - IDs []string `json:"ids"` -} - -func stateFrom(c *pluginapi.Cursor) syncState { - st := syncState{Projects: map[string]*projectState{}} - if c != nil { - _ = remarshal(c.State, &st) - if st.Projects == nil { - st.Projects = map[string]*projectState{} - } - } - return st -} - -func (s syncState) cursor(now time.Time) pluginapi.Cursor { - var state map[string]any - _ = remarshal(s, &state) - return pluginapi.Cursor{LastSyncTime: &now, State: state} -} - -// Fetch syncs the selected projects. Issues come oldest update first, so the -// cursor can advance with every page; a full sync also reports the issues -// that disappeared (deleted, moved out, or no longer matching the filter). -func (connector) Fetch( - ctx context.Context, call *pluginsdk.Call, cfg pluginsdk.ConnectorConfig, in pluginapi.FetchInput, - s *pluginsdk.Stream, -) (*pluginapi.Cursor, error) { - cr, st, err := decode(cfg) - if err != nil { - return nil, err - } - keys := in.ResourceIDs - if len(keys) == 0 { - keys = cfg.ResourceIDs - } - if len(keys) == 0 { - return nil, pluginapi.InvalidConfig(say(call.Locale, msgNoProjects), nil) - } - c, err := newClient(ctx, cr, call.Locale) - if err != nil { - return nil, err - } - me, err := c.myself(ctx) - if err != nil { - return nil, err - } - // JQL reads dates in the user's time zone. - loc, err := time.LoadLocation(me.TimeZone) - if err != nil || me.TimeZone == "" { - loc = time.UTC - } - - prev := stateFrom(in.Cursor) - incremental := in.Mode == pluginapi.FetchIncremental && in.Cursor != nil - // Checkpoints are complete snapshots: projects not reached yet keep - // their previous state. - next := syncState{Projects: map[string]*projectState{}} - for _, key := range keys { - if p := prev.Projects[key]; p != nil { - cp := *p - next.Projects[key] = &cp - } else { - next.Projects[key] = &projectState{} - } - } - seen := map[string]bool{} - now := time.Now().UTC() - - for _, key := range keys { - ps := next.Projects[key] - var since time.Time - if incremental { - since = ps.Since - } - known := map[string]bool{} - for _, id := range ps.IDs { - known[id] = true - } - _ = s.Progress("syncing " + key) - err := c.search(ctx, buildJQL(key, st, since, loc), st.comments(), func(page []issue) error { - for i := range page { - is := &page[i] - if err := s.Item(c.item(is, key)); err != nil { - return err - } - seen[is.ID], known[is.ID] = true, true - if is.Fields.Updated.After(ps.Since) { - ps.Since = is.Fields.Updated.Time - } - } - ps.IDs = sortedKeys(known) - return s.Checkpoint(next.cursor(now)) - }) - if err != nil { - return nil, err - } - } - - if !incremental { - // Everything was listed: what the last sync had and this one did - // not see is gone. - for _, key := range keys { - var kept []string - if p := prev.Projects[key]; p != nil { - for _, id := range p.IDs { - if seen[id] { - continue - } - gone := pluginapi.FetchedItem{ExternalID: id, IsDeleted: true, SourceResourceID: key} - if err := s.Item(gone); err != nil { - return nil, err - } - } - } - for _, id := range next.Projects[key].IDs { - if seen[id] { - kept = append(kept, id) - } - } - next.Projects[key].IDs = kept - } - } - cur := next.cursor(now) - return &cur, nil -} - -func sortedKeys(m map[string]bool) []string { - out := make([]string, 0, len(m)) - for k := range m { - out = append(out, k) - } - sort.Strings(out) - return out -} - -// buildJQL is one project's query: the settings' filters, the issues updated -// since the last sync (to the minute, JQL's precision), oldest first. -func buildJQL(key string, st settings, since time.Time, loc *time.Location) string { - parts := []string{"project = " + quote(key)} - if len(st.IssueTypes) > 0 { - types := make([]string, len(st.IssueTypes)) - for i, t := range st.IssueTypes { - types[i] = quote(t) - } - parts = append(parts, "issuetype in ("+strings.Join(types, ", ")+")") - } - if extra := strings.TrimSpace(st.JQL); extra != "" { - parts = append(parts, "("+extra+")") - } - if !since.IsZero() { - parts = append(parts, "updated >= "+quote(since.In(loc).Format("2006/01/02 15:04"))) - } - return strings.Join(parts, " AND ") + " ORDER BY updated ASC" -} - -func quote(s string) string { - return `"` + strings.NewReplacer(`\`, `\\`, `"`, `\"`).Replace(s) + `"` -} - -// item turns an issue into a Markdown document. -func (c *client) item(is *issue, projectKey string) pluginapi.FetchedItem { - f := &is.Fields - url := c.issueURL(is.Key) - created, updated := f.Created.Time, f.Updated.Time - return pluginapi.FetchedItem{ - ExternalID: is.ID, - Title: is.Key + " " + f.Summary, - Content: []byte(c.markdown(is)), - ContentType: "text/markdown", - FileName: fileName(is.Key + " " + f.Summary), - URL: url, - CreatedAt: nonZero(created), - UpdatedAt: nonZero(updated), - SourceResourceID: projectKey, - Metadata: map[string]string{ - "channel": "jira", "jira_key": is.Key, "project": projectKey, "status": nameOf(f.Status), - "issue_type": nameOf(f.IssueType), "assignee": userOf(f.Assignee), "priority": nameOf(f.Priority), - "labels": strings.Join(f.Labels, ","), - }, - } -} - -func (c *client) issueURL(key string) string { return c.siteURL + "/browse/" + key } - -// markdown renders an issue: its facts, description and comments. -func (c *client) markdown(is *issue) string { - f := &is.Fields - var b strings.Builder - fmt.Fprintf(&b, "# %s: %s\n\n", is.Key, f.Summary) - facts := [][2]string{ - {"Type", nameOf(f.IssueType)}, - {"Status", nameOf(f.Status)}, - {"Priority", nameOf(f.Priority)}, - {"Resolution", nameOf(f.Resolution)}, - {"Assignee", userOf(f.Assignee)}, - {"Reporter", userOf(f.Reporter)}, - {"Labels", strings.Join(f.Labels, ", ")}, - {"Created", stamp(f.Created)}, - {"Updated", stamp(f.Updated)}, - } - if f.Parent != nil { - facts = append(facts, [2]string{"Parent", f.Parent.Key}) - } - for _, kv := range facts { - if kv[1] != "" { - fmt.Fprintf(&b, "- **%s:** %s\n", kv[0], kv[1]) - } - } - fmt.Fprintf(&b, "- **Link:** %s\n", c.issueURL(is.Key)) - if desc := adfToMarkdown(f.Description); desc != "" { - b.WriteString("\n## Description\n\n" + desc + "\n") - } - if f.Comment != nil && len(f.Comment.Comments) > 0 { - b.WriteString("\n## Comments\n") - for _, cm := range f.Comment.Comments { - fmt.Fprintf(&b, "\n### %s · %s\n\n%s\n", userOf(cm.Author), stamp(cm.Created), adfToMarkdown(cm.Body)) - } - if more := f.Comment.Total - len(f.Comment.Comments); more > 0 { - fmt.Fprintf(&b, "\n_%d more comments are not included._\n", more) - } - } - return b.String() -} - -func nameOf(n *named) string { - if n == nil { - return "" - } - return n.Name -} - -func userOf(u *user) string { - if u == nil { - return "" - } - return u.DisplayName -} - -func stamp(t jiraTime) string { - if t.IsZero() { - return "" - } - return t.Format("2006-01-02 15:04 -0700") -} - -func nonZero(t time.Time) *time.Time { - if t.IsZero() { - return nil - } - return &t -} - -const maxFileNameBytes = 200 - -var fileNameReplacer = strings.NewReplacer( - "/", "_", "\\", "_", ":", "_", "*", "_", "?", "_", "\"", "_", "<", "_", ">", "_", "|", "_", - "\n", " ", "\r", " ", "\t", " ", -) - -// fileName makes a title safe as a file name: hostile punctuation replaced, -// at most 200 bytes without splitting a rune. -func fileName(title string) string { - name := strings.TrimSpace(fileNameReplacer.Replace(title)) - if name == "" { - return "issue.md" - } - for len(name) > maxFileNameBytes { - _, size := utf8.DecodeLastRuneInString(name) - name = name[:len(name)-size] - } - return name + ".md" -} - -// formCredentials are the connection fields of the form being filled: a -// data source's credentials, or the workspace's connection for the tools. -func formCredentials(call *pluginsdk.Call, in pluginapi.OptionsInput) (credentials, error) { - var cr credentials - if in.Scope == pluginapi.OptionsScopeTenant { - return cr, remarshal(call.Config.Tenant, &cr) - } - var cfg pluginsdk.ConnectorConfig - if err := call.DecodeInstance(&cfg); err != nil { - return cr, err - } - cr, _, err := decode(cfg) - return cr, err -} - -// siteOptions lists the Jira sites the connected account granted. -func siteOptions(ctx context.Context, call *pluginsdk.Call, in pluginapi.OptionsInput) ([]pluginapi.Option, error) { - cr, err := formCredentials(call, in) - if err != nil { - return nil, err - } - if cr.Account == "" { - return nil, pluginapi.InvalidConfig(say(call.Locale, msgConnectAccount), - map[string]string{"credentials.account": "required"}) - } - sites, err := accessibleSites(ctx, cr.Account) - if err != nil { - return nil, err - } - out := make([]pluginapi.Option, 0, len(sites)) - for _, s := range sites { - out = append(out, pluginapi.Option{Value: s.ID, Label: s.Name, Description: s.URL}) - } - return out, nil -} - -// issueTypeOptions lists the site's issue types by name (JQL matches names). -func issueTypeOptions( - ctx context.Context, - call *pluginsdk.Call, - in pluginapi.OptionsInput, -) ([]pluginapi.Option, error) { - cr, err := formCredentials(call, in) - if err != nil { - return nil, err - } - c, err := newClient(ctx, cr, call.Locale) - if err != nil { - return nil, err - } - types, err := c.issueTypes(ctx) - if err != nil { - return nil, err - } - var names []string - for _, t := range types { - if !slices.Contains(names, t.Name) { - names = append(names, t.Name) - } - } - sort.Strings(names) - out := make([]pluginapi.Option, len(names)) - for i, n := range names { - out[i] = pluginapi.Option{Value: n} - } - return out, nil -} diff --git a/examples/plugins/jira/messages.go b/examples/plugins/jira/messages.go deleted file mode 100644 index 4b2489db..00000000 --- a/examples/plugins/jira/messages.go +++ /dev/null @@ -1,27 +0,0 @@ -package main - -import "strings" - -// Messages the form shows, in the user's language (call.Locale). Errors -// that quote Jira stay in Jira's words. -type message struct{ en, zh string } - -var ( - msgConnectAccount = message{"Connect an Atlassian account first.", "请先连接 Atlassian 账号。"} - msgChooseSite = message{"Choose a site.", "请选择站点。"} - msgSiteGone = message{"The account cannot reach this site any more.", "该账号已无法访问此站点。"} - msgTokenFields = message{"Enter the site URL, email and API token first.", "请先填写站点地址、邮箱和 API 令牌。"} - msgUnknownAuth = message{"Unknown sign-in method.", "未知的登录方式。"} - msgNoProjects = message{"Select at least one project to sync.", "请至少选择一个要同步的项目。"} - msgToolsNotSetUp = message{ - "Jira is not connected for this workspace: an admin sets it up in Settings → Plugins → Jira.", - "本空间尚未连接 Jira:请管理员在「设置 → 插件 → Jira」中配置。", - } -) - -func say(locale string, m message) string { - if strings.HasPrefix(strings.ToLower(locale), "zh") { - return m.zh - } - return m.en -} diff --git a/examples/plugins/jira/package.sh b/examples/plugins/jira/package.sh deleted file mode 100755 index f9219fe2..00000000 --- a/examples/plugins/jira/package.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env bash -# Builds the Jira example plugin for the platforms WeKnora runs on and zips it -# into a .wkp package: plugin.yaml, schemas, the skill, icon and bin/-/jira. -set -euo pipefail -cd "$(dirname "$0")" -version=$(sed -n 's/^version: //p' plugin.yaml) -out=${1:-"weknora-examples-jira-${version}.wkp"} -stage=$(mktemp -d) -trap 'rm -rf "$stage"' EXIT -cp plugin.yaml icon.svg "$stage"/ -cp -r schemas skills "$stage"/ -for target in linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64; do - os=${target%/*} - arch=${target#*/} - bin="$stage/bin/${os}-${arch}/jira" - [ "$os" = windows ] && bin="$bin.exe" - CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -trimpath -ldflags="-s -w" -o "$bin" . -done -rm -f "$out" -(cd "$stage" && zip -qr - .) > "$out" -echo "wrote $out" diff --git a/examples/plugins/jira/plugin.yaml b/examples/plugins/jira/plugin.yaml deleted file mode 100644 index c6ffecab..00000000 --- a/examples/plugins/jira/plugin.yaml +++ /dev/null @@ -1,62 +0,0 @@ -schemaVersion: 1 -id: weknora-examples.jira -version: 1.0.0 -apiVersion: weknora.plugin/v1 -name: { en-US: Jira, zh-CN: Jira } -description: - en-US: Sync Jira Cloud issues, with their comments, into a knowledge base; agent tools to search and read issues; and a skill for triaging them. Connects with Atlassian OAuth or an API token; shows dynamic form options and OAuth fields. - zh-CN: 把 Jira Cloud 的问题(含评论)同步到知识库,提供搜索和读取问题的 Agent 工具,以及问题分诊技能。支持 Atlassian OAuth 授权或 API 令牌;演示表单动态选项与 OAuth 字段。 -publisher: { id: weknora-examples, name: WeKnora examples } -homepage: https://github.com/Tencent/WeKnora/tree/main/examples/plugins/jira -license: MIT -icon: icon.svg -runtime: - type: host - kind: binary - entry: "bin/{os}-{arch}/jira" - resources: { cpu: 500m, memory: 256Mi } -permissions: - # The OAuth API gateway and the sites themselves. WeKnora, not the plugin, - # talks to auth.atlassian.com. - egress: [api.atlassian.com, "*.atlassian.net"] -config: - # The platform's Atlassian OAuth 2.0 (3LO) app. Without it, workspaces can - # still connect with an API token. - system: schemas/system.yaml - # The connection the agent tools use in this workspace. - tenant: schemas/tenant.yaml -contributes: - connectors: - - id: jira - name: { en-US: Jira, zh-CN: Jira } - description: - en-US: Issues of the selected Jira Cloud projects, with comments - zh-CN: 同步所选 Jira Cloud 项目的问题及评论 - icon: icon.svg - capabilities: [incremental, streaming] - instanceSchema: schemas/connector.yaml - mcpServers: - # No url: the plugin serves these tools itself. - - id: tools - name: { en-US: Jira, zh-CN: Jira } - description: - en-US: Search Jira issues with JQL or text, and read one issue with its comments - zh-CN: 用 JQL 或关键词搜索 Jira 问题,读取单个问题及其评论 - toolViews: - search_issues: - view: table - items: issues - columns: - - { field: key, title: { en-US: Key, zh-CN: 编号 }, link: url } - - { field: summary, title: { en-US: Summary, zh-CN: 摘要 } } - - { field: status, title: { en-US: Status, zh-CN: 状态 } } - - { field: assignee, title: { en-US: Assignee, zh-CN: 负责人 } } - - { field: updated, title: { en-US: Updated, zh-CN: 更新时间 } } - get_issue: { view: markdown, field: markdown } - skills: - - id: jira-triage - name: { en-US: Jira triage, zh-CN: Jira 问题分诊 } - description: - en-US: Find duplicates, related work and owners of a Jira issue in the synced issues - zh-CN: 在已同步的问题中查找重复、相关工作和负责人 - path: skills/jira-triage diff --git a/examples/plugins/jira/schemas/connector.yaml b/examples/plugins/jira/schemas/connector.yaml deleted file mode 100644 index f45fec6d..00000000 --- a/examples/plugins/jira/schemas/connector.yaml +++ /dev/null @@ -1,99 +0,0 @@ -type: object -# Fields hidden by x-visible-if are not required. -required: [auth, account, site, base_url, email, api_token] -properties: - auth: - type: string - title: Sign in with - default: oauth - x-order: 1 - oneOf: - - const: oauth - title: Atlassian account (OAuth) - x-i18n: { title: { zh-CN: Atlassian 账号(OAuth) } } - - const: token - title: API token - x-i18n: { title: { zh-CN: API 令牌 } } - x-i18n: - title: { zh-CN: 登录方式 } - account: - type: string - title: Atlassian account - description: Needs the platform's OAuth app (plugin management → Jira) - x-order: 2 - x-visible-if: { auth: oauth } - x-oauth: - authorizeUrl: https://auth.atlassian.com/authorize - tokenUrl: https://auth.atlassian.com/oauth/token - scopes: [read:jira-work, read:jira-user, offline_access] - clientId: ${system.client_id} - clientSecret: ${system.client_secret} - params: { audience: api.atlassian.com, prompt: consent } - x-i18n: - title: { zh-CN: Atlassian 账号 } - description: { zh-CN: 需要平台先配置 OAuth 应用(插件管理 → Jira) } - site: - type: string - title: Site - description: The Jira site the account can reach - x-order: 3 - x-visible-if: { auth: oauth } - x-options: { name: sites, dependsOn: [account] } - x-i18n: - title: { zh-CN: 站点 } - description: { zh-CN: 该账号可访问的 Jira 站点 } - base_url: - type: string - title: Site URL - x-order: 4 - x-visible-if: { auth: token } - x-placeholder: https://your-team.atlassian.net - pattern: "^https://[A-Za-z0-9-]+\\.atlassian\\.net/?$" - x-i18n: - title: { zh-CN: 站点地址 } - email: - type: string - title: Email - x-order: 5 - x-visible-if: { auth: token } - format: email - x-i18n: - title: { zh-CN: 邮箱 } - api_token: - type: string - title: API token - description: Create one at id.atlassian.com → Security → API tokens - x-order: 6 - x-secret: true - x-visible-if: { auth: token } - x-i18n: - title: { zh-CN: API 令牌 } - description: { zh-CN: 在 id.atlassian.com → 安全 → API 令牌 中创建 } - issue_types: - type: array - items: { type: string } - title: Issue types - description: Only these types; leave empty for all - x-order: 10 - x-group: settings - x-options: { name: issue_types, dependsOn: [auth, site, base_url, email] } - x-i18n: - title: { zh-CN: 问题类型 } - description: { zh-CN: 只同步这些类型,留空表示全部 } - jql: - type: string - title: Extra JQL filter - description: "Added to each project's query, e.g. statusCategory != Done" - x-order: 11 - x-group: settings - x-i18n: - title: { zh-CN: 附加 JQL 条件 } - description: { zh-CN: "追加到每个项目的查询,例如 statusCategory != Done" } - include_comments: - type: boolean - title: Include comments - default: true - x-order: 12 - x-group: settings - x-i18n: - title: { zh-CN: 包含评论 } diff --git a/examples/plugins/jira/schemas/system.yaml b/examples/plugins/jira/schemas/system.yaml deleted file mode 100644 index eeef0198..00000000 --- a/examples/plugins/jira/schemas/system.yaml +++ /dev/null @@ -1,17 +0,0 @@ -type: object -properties: - client_id: - type: string - title: OAuth client ID - description: "Client ID of an OAuth 2.0 (3LO) app in the Atlassian developer console, with the Jira API scopes read:jira-work and read:jira-user and the callback URL /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 权限,回调地址填 /api/v1/plugin-oauth/callback" } - client_secret: - type: string - title: OAuth client secret - x-secret: true - x-order: 2 - x-i18n: - title: { zh-CN: OAuth 客户端密钥 } diff --git a/examples/plugins/jira/schemas/tenant.yaml b/examples/plugins/jira/schemas/tenant.yaml deleted file mode 100644 index 3db0f975..00000000 --- a/examples/plugins/jira/schemas/tenant.yaml +++ /dev/null @@ -1,73 +0,0 @@ -# The connection agent tools use in this workspace. Data sources have their -# own, so each knowledge base can sync with a different account. -type: object -# Fields hidden by x-visible-if are not required. -required: [auth, account, site, base_url, email, api_token] -properties: - auth: - type: string - title: Sign in with - default: oauth - x-order: 1 - oneOf: - - const: oauth - title: Atlassian account (OAuth) - x-i18n: { title: { zh-CN: Atlassian 账号(OAuth) } } - - const: token - title: API token - x-i18n: { title: { zh-CN: API 令牌 } } - x-i18n: - title: { zh-CN: 登录方式 } - account: - type: string - title: Atlassian account - description: Needs the platform's OAuth app (plugin management → Jira) - x-order: 2 - x-visible-if: { auth: oauth } - x-oauth: - authorizeUrl: https://auth.atlassian.com/authorize - tokenUrl: https://auth.atlassian.com/oauth/token - scopes: [read:jira-work, read:jira-user, offline_access] - clientId: ${system.client_id} - clientSecret: ${system.client_secret} - params: { audience: api.atlassian.com, prompt: consent } - x-i18n: - title: { zh-CN: Atlassian 账号 } - description: { zh-CN: 需要平台先配置 OAuth 应用(插件管理 → Jira) } - site: - type: string - title: Site - description: The Jira site the account can reach - x-order: 3 - x-visible-if: { auth: oauth } - x-options: { name: sites, dependsOn: [account] } - x-i18n: - title: { zh-CN: 站点 } - description: { zh-CN: 该账号可访问的 Jira 站点 } - base_url: - type: string - title: Site URL - x-order: 4 - x-visible-if: { auth: token } - x-placeholder: https://your-team.atlassian.net - pattern: "^https://[A-Za-z0-9-]+\\.atlassian\\.net/?$" - x-i18n: - title: { zh-CN: 站点地址 } - email: - type: string - title: Email - x-order: 5 - x-visible-if: { auth: token } - format: email - x-i18n: - title: { zh-CN: 邮箱 } - api_token: - type: string - title: API token - description: Create one at id.atlassian.com → Security → API tokens - x-order: 6 - x-secret: true - x-visible-if: { auth: token } - x-i18n: - title: { zh-CN: API 令牌 } - description: { zh-CN: 在 id.atlassian.com → 安全 → API 令牌 中创建 } diff --git a/examples/plugins/jira/skills/jira-triage/SKILL.md b/examples/plugins/jira/skills/jira-triage/SKILL.md deleted file mode 100644 index 27b5a927..00000000 --- a/examples/plugins/jira/skills/jira-triage/SKILL.md +++ /dev/null @@ -1,40 +0,0 @@ ---- -name: jira-triage -description: Triage a Jira issue against the issues synced into the knowledge base - find duplicates, related work, likely owners and a suggested priority. Use when the user pastes a Jira key or a bug report and asks whether it is known, who should take it, or how urgent it is. ---- -# Jira triage - -The Jira data source stores each issue as one Markdown document titled -` `. 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 ``, related to ``, or new. -- **Evidence:** one line per issue: key, summary, status, and why it - matches. Link each with the Link from its document. -- **Owner** and **priority** suggestions, with the reason. -- **Missing information** the reporter should add, if any. - -Only cite issues you read. The knowledge base may lag Jira by one sync; say so -when the newest issue you found is older than a day. diff --git a/examples/plugins/jira/testdata/adf.json b/examples/plugins/jira/testdata/adf.json deleted file mode 100644 index 0c0a7dc3..00000000 --- a/examples/plugins/jira/testdata/adf.json +++ /dev/null @@ -1,160 +0,0 @@ -{ - "type": "doc", - "version": 1, - "content": [ - { - "type": "heading", - "attrs": { - "level": 2 - }, - "content": [ - { - "type": "text", - "text": "Plan" - } - ] - }, - { - "type": "bulletList", - "content": [ - { - "type": "listItem", - "content": [ - { - "type": "paragraph", - "content": [ - { - "type": "text", - "text": "one" - } - ] - }, - { - "type": "orderedList", - "content": [ - { - "type": "listItem", - "content": [ - { - "type": "paragraph", - "content": [ - { - "type": "text", - "text": "nested" - } - ] - } - ] - } - ] - } - ] - }, - { - "type": "listItem", - "content": [ - { - "type": "paragraph", - "content": [ - { - "type": "text", - "text": "docs", - "marks": [ - { - "type": "link", - "attrs": { - "href": "https://example.com" - } - } - ] - }, - { - "type": "text", - "text": " by " - }, - { - "type": "mention", - "attrs": { - "text": "@Ann" - } - } - ] - } - ] - } - ] - }, - { - "type": "codeBlock", - "attrs": { - "language": "go" - }, - "content": [ - { - "type": "text", - "text": "x := 1" - } - ] - }, - { - "type": "table", - "content": [ - { - "type": "tableRow", - "content": [ - { - "type": "tableHeader", - "content": [ - { - "type": "paragraph", - "content": [ - { - "type": "text", - "text": "a|b" - } - ] - } - ] - } - ] - }, - { - "type": "tableRow", - "content": [ - { - "type": "tableCell", - "content": [ - { - "type": "paragraph", - "content": [ - { - "type": "text", - "text": "1" - } - ] - } - ] - } - ] - } - ] - }, - { - "type": "mediaSingle", - "content": [ - { - "type": "media" - } - ] - }, - { - "type": "someFutureNode", - "content": [ - { - "type": "text", - "text": "kept" - } - ] - } - ] -} diff --git a/examples/plugins/jira/tools.go b/examples/plugins/jira/tools.go deleted file mode 100644 index a0ec8980..00000000 --- a/examples/plugins/jira/tools.go +++ /dev/null @@ -1,172 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "fmt" - "net/http" - "net/url" - "regexp" - "strings" - - "github.com/Tencent/WeKnora/pluginsdk" - "github.com/Tencent/WeKnora/pluginsdk/pluginapi" -) - -// toolsServer is the mcpServers contribution the plugin serves itself. -const toolsServer = "tools" - -func registerTools(p *pluginsdk.Plugin) { - p.Tool(toolsServer, pluginapi.Tool{ - Name: "search_issues", - Title: "Search Jira issues", - Description: "Search Jira issues, newest update first. Give jql for precise queries " + - `(e.g. project = ENG AND status != Done AND assignee = currentUser()), ` + - "or text to match summaries, descriptions and comments.", - InputSchema: json.RawMessage(`{"type":"object","properties":{ - "jql":{"type":"string","description":"A JQL query; takes precedence over text and project"}, - "text":{"type":"string","description":"Words to search for"}, - "project":{"type":"string","description":"A project key to limit text search to"}, - "limit":{"type":"integer","minimum":1,"maximum":50,"default":20}}}`), - Annotations: &pluginapi.ToolAnnotations{ReadOnlyHint: true}, - }, searchIssuesTool) - p.Tool(toolsServer, pluginapi.Tool{ - Name: "get_issue", - Title: "Read a Jira issue", - Description: "Read one Jira issue by key (e.g. ENG-123): its facts, description and comments.", - InputSchema: json.RawMessage(`{"type":"object","required":["key"],"properties":{ - "key":{"type":"string","description":"The issue key"}}}`), - Annotations: &pluginapi.ToolAnnotations{ReadOnlyHint: true}, - }, getIssueTool) -} - -// toolClient connects with the workspace's connection (config.tenant). -func toolClient(ctx context.Context, call *pluginsdk.Call) (*client, error) { - var cr credentials - if err := remarshal(call.Config.Tenant, &cr); err != nil { - return nil, err - } - c, err := newClient(ctx, cr, call.Locale) - if pe, ok := pluginapi.AsError(err); ok && pe.Code == pluginapi.CodeInvalidConfig { - return nil, fmt.Errorf("%s %s", say(call.Locale, msgToolsNotSetUp), pe.Message) - } - return c, err -} - -// issueRow is one search result, as the result view shows it. -type issueRow struct { - Key string `json:"key"` - Summary string `json:"summary"` - Status string `json:"status"` - Type string `json:"type"` - Priority string `json:"priority"` - Assignee string `json:"assignee"` - Updated string `json:"updated"` - URL string `json:"url"` -} - -func searchIssuesTool(ctx context.Context, call *pluginsdk.Call, args json.RawMessage) (*pluginapi.ToolResult, error) { - var in struct { - JQL string `json:"jql"` - Text string `json:"text"` - Project string `json:"project"` - Limit int `json:"limit"` - } - if err := json.Unmarshal(args, &in); err != nil { - return nil, err - } - jql, err := toolJQL(in.JQL, in.Text, in.Project) - if err != nil { - return nil, err - } - if in.Limit <= 0 || in.Limit > 50 { - in.Limit = 20 - } - c, err := toolClient(ctx, call) - if err != nil { - return nil, err - } - issues, more, err := c.searchPage(ctx, jql, in.Limit) - if err != nil { - return nil, err - } - rows := make([]issueRow, len(issues)) - var text strings.Builder - fmt.Fprintf(&text, "%d issue(s) for %s", len(issues), jql) - if more { - text.WriteString(" (more exist; narrow the query)") - } - text.WriteString("\n") - for i := range issues { - f := &issues[i].Fields - rows[i] = issueRow{ - Key: issues[i].Key, Summary: f.Summary, Status: nameOf(f.Status), Type: nameOf(f.IssueType), - Priority: nameOf(f.Priority), Assignee: userOf(f.Assignee), Updated: stamp(f.Updated), - URL: c.issueURL(issues[i].Key), - } - r := rows[i] - fmt.Fprintf(&text, "- %s [%s] %s (%s, %s) %s\n", r.Key, r.Status, r.Summary, - firstNonEmpty(r.Assignee, "unassigned"), r.Updated, r.URL) - } - return pluginapi.StructuredResult(map[string]any{"jql": jql, "issues": rows, "more": more}, text.String()), nil -} - -var issueKeyPattern = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9_]*-[0-9]+$`) - -func getIssueTool(ctx context.Context, call *pluginsdk.Call, args json.RawMessage) (*pluginapi.ToolResult, error) { - var in struct { - Key string `json:"key"` - } - if err := json.Unmarshal(args, &in); err != nil { - return nil, err - } - key := strings.ToUpper(strings.TrimSpace(in.Key)) - if !issueKeyPattern.MatchString(key) { - return pluginapi.ToolError("%q is not an issue key like ENG-123", in.Key), nil - } - c, err := toolClient(ctx, call) - if err != nil { - return nil, err - } - var is issue - q := url.Values{"fields": {strings.Join(searchFields, ",")}} - if err := c.do(ctx, http.MethodGet, "/rest/api/3/issue/"+url.PathEscape(key)+"?"+q.Encode(), nil, &is); err != nil { - if pe, ok := pluginapi.AsError(err); ok && pe.Code == pluginapi.CodeNotFound { - return pluginapi.ToolError("issue %s does not exist or is not visible to this account", key), nil - } - return nil, err - } - md := c.markdown(&is) - return pluginapi.StructuredResult(map[string]any{"key": is.Key, "url": c.issueURL(is.Key), "markdown": md}, md), nil -} - -// toolJQL is the query a search runs: the model's JQL as it is, or a text -// search, newest update first. -func toolJQL(jql, text, project string) (string, error) { - if jql = strings.TrimSpace(jql); jql != "" { - if !strings.Contains(strings.ToUpper(jql), "ORDER BY") { - jql += " ORDER BY updated DESC" - } - return jql, nil - } - var parts []string - if p := strings.TrimSpace(project); p != "" { - parts = append(parts, "project = "+quote(p)) - } - if t := strings.TrimSpace(text); t != "" { - parts = append(parts, "text ~ "+quote(t)) - } - if len(parts) == 0 { - return "", fmt.Errorf("give jql, or text to search for") - } - return strings.Join(parts, " AND ") + " ORDER BY updated DESC", nil -} - -func firstNonEmpty(values ...string) string { - for _, v := range values { - if v != "" { - return v - } - } - return "" -} diff --git a/examples/plugins/links/main.py b/examples/plugins/links/main.py deleted file mode 100644 index 184a1c1d..00000000 --- a/examples/plugins/links/main.py +++ /dev/null @@ -1,78 +0,0 @@ -"""A WeKnora plugin with pages, written with the Python SDK: a toolbox page -listing the workspace's links, a settings section where admins edit them, -and a tab on each knowledge base with links about it. The pages run -sandboxed in the browser and reach this code only through the bridge -(wk.get / wk.put), which WeKnora relays as UI requests.""" - -from __future__ import annotations - -import re -from urllib.parse import urlparse - -from weknora_plugin import ErrorCode, Plugin, PluginError, UIResponse - -plugin = Plugin("weknora-examples.links", "1.0.0") - -MAX_LINKS = 200 -EDITORS = {"contributor", "admin", "owner"} -KB_PATH = re.compile(r"^/kb/([A-Za-z0-9_-]{1,64})/links$") - - -def clean_links(value) -> list: - """Validates links from a page: [{title, url}], http(s) only.""" - if not isinstance(value, list) or len(value) > MAX_LINKS: - raise ValueError(f"send a list of at most {MAX_LINKS} links") - out = [] - for item in value: - if not isinstance(item, dict): - raise ValueError("each link is an object with a title and a url") - title = str(item.get("title") or "").strip()[:120] - url = str(item.get("url") or "").strip() - parsed = urlparse(url) - if parsed.scheme not in ("http", "https") or not parsed.netloc: - raise ValueError(f"{url or 'an empty url'} is not an http(s) link") - out.append({"title": title or parsed.netloc, "url": url[:2000]}) - return out - - -def store(call): - host = call.host() - if host is None: - raise PluginError(ErrorCode.UNAVAILABLE, "the Host API is not available to this plugin") - return host - - -def handle(call, req) -> UIResponse: - host = store(call) - if req.path == "/links": - if req.method == "GET": - return UIResponse(body=host.kv_get("links", [])) - # Only the settings section edits the workspace's links; WeKnora - # lets admins alone open it. - if req.method == "PUT" and req.mount == "settingsSections/manage": - return save(host, "links", req.body) - m = KB_PATH.match(req.path) - if m and req.mount == "kbTabs/links": - key = f"kb:{m.group(1)}" - if req.method == "GET": - return UIResponse(body=host.kv_get(key, [])) - if req.method == "PUT": - if req.role not in EDITORS: - return UIResponse(status=403, body={"error": "only editors of the workspace can change links"}) - return save(host, key, req.body) - return UIResponse(status=404, body={"error": f"no {req.method} {req.path} here"}) - - -def save(host, key, body) -> UIResponse: - try: - links = clean_links(body) - except ValueError as e: - return UIResponse(status=400, body={"error": str(e)}) - host.kv_put(key, links) - return UIResponse(body=links) - - -plugin.ui(handle) - -if __name__ == "__main__": - plugin.serve() diff --git a/examples/plugins/links/package.sh b/examples/plugins/links/package.sh deleted file mode 100755 index 18d1b053..00000000 --- a/examples/plugins/links/package.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env bash -# Packages the links example plugin as a .wkp: plugin.yaml, main.py, the -# pages under ui/ with the bridge library, and the Python SDK under vendor/. -set -euo pipefail -cd "$(dirname "$0")" -version=$(sed -n 's/^version: //p' plugin.yaml) -out=${1:-"weknora-examples-links-${version}.wkp"} -stage=$(mktemp -d) -trap 'rm -rf "$stage"' EXIT -cp plugin.yaml main.py "$stage"/ -cp -r ui "$stage"/ -cp ../../../packages/plugin-ui/index.js "$stage/ui/weknora-plugin-ui.js" -mkdir -p "$stage/vendor" -cp -r ../../../pluginsdk/python/src/weknora_plugin "$stage/vendor/" -find "$stage" -name __pycache__ -prune -exec rm -rf {} + -rm -f "$out" -(cd "$stage" && zip -qr - .) > "$out" -echo "wrote $out" diff --git a/examples/plugins/links/plugin.yaml b/examples/plugins/links/plugin.yaml deleted file mode 100644 index 007a1a6e..00000000 --- a/examples/plugins/links/plugin.yaml +++ /dev/null @@ -1,31 +0,0 @@ -schemaVersion: 1 -id: weknora-examples.links -version: 1.0.0 -apiVersion: weknora.plugin/v1 -name: { en-US: Team links, zh-CN: 团队链接 } -description: - en-US: A page of links for the workspace and a tab of links on each knowledge base. Shows plugin pages, written in Python. - zh-CN: 空间的常用链接页,以及每个知识库上的链接页签。演示插件页面,用 Python 编写。 -publisher: { id: weknora-examples, name: WeKnora examples } -homepage: https://github.com/Tencent/WeKnora/tree/main/examples/plugins/links -license: MIT -runtime: { type: host, kind: python, entry: main.py } -permissions: - # Links live in the plugin's key-value store, per workspace. - hostApi: [kv] -contributes: - pages: - - id: links - name: { en-US: Team links, zh-CN: 团队链接 } - description: { en-US: Links the workspace shares, zh-CN: 空间共享的常用链接 } - entry: ui/index.html - settingsSections: - - id: manage - name: { en-US: Team links, zh-CN: 团队链接 } - entry: ui/manage.html - # minRole defaults to admin for settings sections - kbTabs: - - id: links - name: { en-US: Links, zh-CN: 链接 } - description: { en-US: Links about this knowledge base, zh-CN: 与本知识库相关的链接 } - entry: ui/kb.html diff --git a/examples/plugins/links/test_main.py b/examples/plugins/links/test_main.py deleted file mode 100644 index 0726c619..00000000 --- a/examples/plugins/links/test_main.py +++ /dev/null @@ -1,56 +0,0 @@ -import os -import sys -import unittest - -HERE = os.path.dirname(os.path.abspath(__file__)) -sys.path.insert(0, os.path.join(HERE, "..", "..", "..", "pluginsdk", "python", "src")) -sys.path.insert(0, HERE) - -import main # noqa: E402 -from weknora_plugin import UIRequest # noqa: E402 - - -class FakeHost: - def __init__(self): - self.data = {} - - def kv_get(self, key, default=None): - return self.data.get(key, default) - - def kv_put(self, key, value, ttl=0): - self.data[key] = value - - -class LinksTest(unittest.TestCase): - def setUp(self): - self.host = FakeHost() - main.store = lambda call: self.host - - def req(self, mount, method, path, body=None, role="viewer"): - return main.handle(None, UIRequest(mount=mount, method=method, path=path, body=body, role=role)) - - def test_workspace_links(self): - self.assertEqual(self.req("pages/links", "GET", "/links").body, []) - links = [{"title": "", "url": "https://github.com/Tencent/WeKnora"}] - # Only the settings section (admins) writes the workspace's links. - self.assertEqual(self.req("pages/links", "PUT", "/links", links, role="owner").status, 404) - saved = self.req("settingsSections/manage", "PUT", "/links", links, role="admin") - self.assertEqual(saved.body, [{"title": "github.com", "url": "https://github.com/Tencent/WeKnora"}]) - self.assertEqual(self.req("pages/links", "GET", "/links").body, saved.body) - - def test_kb_links(self): - path = "/kb/kb-1/links" - self.assertEqual(self.req("kbTabs/links", "PUT", path, [], role="viewer").status, 403) - self.assertEqual(self.req("kbTabs/links", "PUT", path, [{"title": "Spec", "url": "http://x.example"}], role="contributor").status, 200) - self.assertEqual(self.req("kbTabs/links", "GET", path).body[0]["title"], "Spec") - self.assertEqual(self.req("kbTabs/links", "GET", "/kb/kb-2/links").body, []) - self.assertEqual(self.req("pages/links", "GET", path).status, 404) - - def test_bad_links(self): - for body in ({"url": "x"}, [{"url": "javascript:alert(1)"}], [{"url": "ftp://x"}], [{}] * 201): - resp = self.req("settingsSections/manage", "PUT", "/links", body, role="admin") - self.assertEqual(resp.status, 400, body) - - -if __name__ == "__main__": - unittest.main() diff --git a/examples/plugins/links/ui/app.css b/examples/plugins/links/ui/app.css deleted file mode 100644 index d875d490..00000000 --- a/examples/plugins/links/ui/app.css +++ /dev/null @@ -1,41 +0,0 @@ -/* Colours and fonts come from the app theme (--wk-* variables, set by the - bridge), with fallbacks for opening the page on its own. */ -:root { - color-scheme: light; - --fg: var(--wk-text-color-primary, #1f2329); - --muted: var(--wk-text-color-secondary, #646a73); - --line: var(--wk-component-border, #dee0e3); - --card: var(--wk-bg-color-container, #fff); - --brand: var(--wk-brand-color, #0052d9); - --danger: var(--wk-error-color, #d54941); - --radius: var(--wk-radius-default, 6px); -} -:root[data-theme='dark'] { color-scheme: dark; } -* { box-sizing: border-box; } -body { - margin: 0; - font: 14px/1.5 var(--wk-font-family, system-ui, sans-serif); - color: var(--fg); - background: transparent; -} -main { padding: 4px 2px 12px; } -.empty { color: var(--muted); padding: 24px 0; text-align: center; } -ul.links { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; } -.links li { - display: flex; align-items: center; gap: 10px; - padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); -} -.links a { color: var(--brand); text-decoration: none; font-weight: 500; } -.links a:hover { text-decoration: underline; } -.links .url { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; } -form { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; } -input { - flex: 1; min-width: 140px; padding: 6px 10px; font: inherit; color: var(--fg); - background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); -} -button { - padding: 6px 14px; font: inherit; border-radius: var(--radius); cursor: pointer; - border: 1px solid var(--brand); background: var(--brand); color: #fff; -} -button.remove { border-color: transparent; background: transparent; color: var(--danger); padding: 4px 8px; } -button:disabled { opacity: 0.6; cursor: default; } diff --git a/examples/plugins/links/ui/index.html b/examples/plugins/links/ui/index.html deleted file mode 100644 index d71690eb..00000000 --- a/examples/plugins/links/ui/index.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - Team links - - - -
- - - diff --git a/examples/plugins/links/ui/kb.html b/examples/plugins/links/ui/kb.html deleted file mode 100644 index f1214a57..00000000 --- a/examples/plugins/links/ui/kb.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - Team links - - - -
- - - diff --git a/examples/plugins/links/ui/links.js b/examples/plugins/links/ui/links.js deleted file mode 100644 index f2bdbb40..00000000 --- a/examples/plugins/links/ui/links.js +++ /dev/null @@ -1,95 +0,0 @@ -// The three pages of the plugin. They run in a sandboxed iframe: no network, -// no storage of the app. Everything goes through the bridge (wk.*), which -// the app relays to main.py. -import { connect } from './weknora-plugin-ui.js' - -const TEXT = { - en: { - empty: 'No links yet.', emptyManage: 'No links yet. Add the first one below.', - title: 'Title', url: 'https://…', add: 'Add', remove: 'Remove', saved: 'Links saved', - confirm: 'Remove this link?', readOnly: 'Only editors of the workspace can change these links.', - }, - zh: { - empty: '还没有链接。', emptyManage: '还没有链接,在下方添加第一个。', - title: '标题', url: 'https://…', add: '添加', remove: '删除', saved: '链接已保存', - confirm: '删除这个链接?', readOnly: '只有空间的编辑者可以修改这些链接。', - }, -} - -const wk = await connect() -const app = document.getElementById('app') -const mode = document.body.dataset.mode -let text = pick(wk.context.locale) -wk.on('locale', (locale) => { - text = pick(locale) - void load() -}) -// A knowledge base tab follows the app to another knowledge base. -wk.on('init', () => void load()) - -function pick(locale) { - return String(locale || '').startsWith('zh') ? TEXT.zh : TEXT.en -} - -function path() { - return mode === 'kb' ? `/kb/${wk.context.context.knowledgeBaseId}/links` : '/links' -} - -function el(tag, props = {}, ...children) { - const node = Object.assign(document.createElement(tag), props) - node.append(...children) - return node -} - -async function load() { - app.setAttribute('aria-busy', 'true') - try { - const { body } = await wk.get(path()) - render(Array.isArray(body) ? body : []) - } catch (e) { - app.replaceChildren(el('p', { className: 'empty', textContent: e.message })) - } finally { - app.removeAttribute('aria-busy') - } -} - -async function save(links) { - try { - const { body } = await wk.put(path(), links) - await wk.toast(text.saved, 'success') - render(body) - } catch (e) { - await wk.toast(e.message, 'error') - } -} - -function render(links) { - const editable = mode === 'manage' || (mode === 'kb' && wk.context.role !== 'viewer') - const list = el('ul', { className: 'links' }) - links.forEach((link, i) => { - const a = el('a', { href: link.url, target: '_blank', rel: 'noopener noreferrer', textContent: link.title }) - const item = el('li', {}, a, el('span', { className: 'url', textContent: link.url })) - if (editable) { - const remove = el('button', { className: 'remove', type: 'button', textContent: text.remove }) - remove.addEventListener('click', async () => { - if (await wk.confirm(text.confirm)) await save(links.filter((_, j) => j !== i)) - }) - item.append(remove) - } - list.append(item) - }) - const nodes = links.length ? [list] : [el('p', { className: 'empty', textContent: mode === 'manage' ? text.emptyManage : text.empty })] - if (editable) { - const title = el('input', { placeholder: text.title, maxLength: 120 }) - const url = el('input', { placeholder: text.url, type: 'url', required: true }) - const form = el('form', {}, title, url, el('button', { type: 'submit', textContent: text.add })) - form.addEventListener('submit', async (event) => { - event.preventDefault() - await save([...links, { title: title.value, url: url.value }]) - }) - nodes.push(form) - } - app.replaceChildren(...nodes) -} - -await load() diff --git a/examples/plugins/links/ui/manage.html b/examples/plugins/links/ui/manage.html deleted file mode 100644 index 9a63b9d8..00000000 --- a/examples/plugins/links/ui/manage.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - Team links - - - -
- - - diff --git a/examples/plugins/notebooks/main.py b/examples/plugins/notebooks/main.py deleted file mode 100644 index 11a71ee6..00000000 --- a/examples/plugins/notebooks/main.py +++ /dev/null @@ -1,106 +0,0 @@ -"""A WeKnora parser plugin, written with the Python SDK: Jupyter notebooks -(.ipynb) become Markdown. Markdown cells are kept as they are, code cells -become fenced blocks, text outputs follow their cell and PNG/JPEG charts are -handed to WeKnora as images.""" - -from __future__ import annotations - -import base64 -import json - -from weknora_plugin import ErrorCode, ParsedImage, ParseOutput, Plugin, PluginError - -plugin = Plugin("weknora-examples.notebooks", "1.0.0") - -IMAGE_TYPES = {"image/png": "png", "image/jpeg": "jpg"} - - -def _text(value) -> str: - """Notebook strings are either a string or a list of lines.""" - if isinstance(value, list): - return "".join(value) - return value or "" - - -def _fence(body: str, lang: str = "") -> str: - # A fence longer than any backtick run inside, so code cannot close it. - longest, run = 0, 0 - for ch in body: - run = run + 1 if ch == "`" else 0 - longest = max(longest, run) - ticks = "`" * max(3, longest + 1) - return f"{ticks}{lang}\n{body.rstrip()}\n{ticks}" - - -def convert(nb: dict, include_outputs: bool = True, max_output_chars: int = 2000) -> ParseOutput: - lang = ((nb.get("metadata") or {}).get("kernelspec") or {}).get("language") or ( - (nb.get("metadata") or {}).get("language_info") or {} - ).get("name", "") - parts, images = [], [] - for i, cell in enumerate(nb.get("cells") or []): - kind, source = cell.get("cell_type"), _text(cell.get("source")).strip() - if kind == "markdown" and source: - parts.append(source) - elif kind == "code": - if source: - parts.append(_fence(source, lang)) - if include_outputs: - parts.extend(_outputs(i, cell.get("outputs") or [], images, max_output_chars)) - elif kind == "raw" and source: - parts.append(_fence(source)) - title = next((p.splitlines()[0][2:].strip() for p in parts if p.startswith("# ")), "") - meta = {"cells": str(len(nb.get("cells") or [])), "language": lang} - if title: - meta["title"] = title - return ParseOutput(markdown="\n\n".join(parts) + "\n", images=images, metadata=meta) - - -def _outputs(cell: int, outputs: list, images: list, limit: int) -> list: - parts = [] - for j, out in enumerate(outputs): - kind = out.get("output_type") - if kind == "stream": - text = _text(out.get("text")) - elif kind == "error": - text = f"{out.get('ename', 'Error')}: {out.get('evalue', '')}" - else: - data = out.get("data") or {} - image = next((m for m in IMAGE_TYPES if m in data), None) - if image: - ref = f"images/cell{cell}-{j}.{IMAGE_TYPES[image]}" - try: - images.append(ParsedImage(original_ref=ref, data=base64.b64decode(_text(data[image])), mime_type=image)) - parts.append(f"![Output of cell {cell + 1}]({ref})") - except ValueError: - pass - continue - if "text/markdown" in data: - parts.append(_text(data["text/markdown"]).strip()) - continue - text = _text(data.get("text/plain")) - text = text.strip() - if text: - if limit and len(text) > limit: - text = text[:limit] + "\n…" - parts.append(_fence(text, "text")) - return parts - - -@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() diff --git a/examples/plugins/notebooks/package.sh b/examples/plugins/notebooks/package.sh deleted file mode 100755 index e94ba822..00000000 --- a/examples/plugins/notebooks/package.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env bash -# Packages the notebooks example plugin as a .wkp: plugin.yaml, schemas, -# main.py and the Python SDK vendored under vendor/. Nothing is compiled; -# WeKnora runs main.py with its own python3. -set -euo pipefail -cd "$(dirname "$0")" -version=$(sed -n 's/^version: //p' plugin.yaml) -out=${1:-"weknora-examples-notebooks-${version}.wkp"} -stage=$(mktemp -d) -trap 'rm -rf "$stage"' EXIT -cp plugin.yaml main.py "$stage"/ -cp -r schemas "$stage"/ -mkdir -p "$stage/vendor" -cp -r ../../../pluginsdk/python/src/weknora_plugin "$stage/vendor/" -find "$stage" -name __pycache__ -prune -exec rm -rf {} + -rm -f "$out" -(cd "$stage" && zip -qr - .) > "$out" -echo "wrote $out" diff --git a/examples/plugins/notebooks/plugin.yaml b/examples/plugins/notebooks/plugin.yaml deleted file mode 100644 index bcbe1518..00000000 --- a/examples/plugins/notebooks/plugin.yaml +++ /dev/null @@ -1,22 +0,0 @@ -schemaVersion: 1 -id: weknora-examples.notebooks -version: 1.0.0 -apiVersion: weknora.plugin/v1 -name: { en-US: Jupyter notebooks, zh-CN: Jupyter 笔记本 } -description: - en-US: Parse Jupyter notebooks into Markdown with their code, text outputs and charts. Written in Python. - zh-CN: 把 Jupyter 笔记本解析为 Markdown,保留代码、文本输出和图表。用 Python 编写。 -publisher: { id: weknora-examples, name: WeKnora examples } -homepage: https://github.com/Tencent/WeKnora/tree/main/examples/plugins/notebooks -license: MIT -runtime: { type: host, kind: python, entry: main.py } -config: - tenant: schemas/tenant.yaml -contributes: - parsers: - - id: ipynb - name: { en-US: Jupyter notebooks, zh-CN: Jupyter 笔记本 } - description: - en-US: Markdown and code cells, text outputs and PNG/JPEG charts - zh-CN: Markdown 与代码单元格、文本输出和 PNG/JPEG 图表 - fileTypes: [ipynb] diff --git a/examples/plugins/notebooks/schemas/tenant.yaml b/examples/plugins/notebooks/schemas/tenant.yaml deleted file mode 100644 index 2c43e2a7..00000000 --- a/examples/plugins/notebooks/schemas/tenant.yaml +++ /dev/null @@ -1,19 +0,0 @@ -type: object -properties: - include_outputs: - type: boolean - title: Include cell outputs - description: Index what code cells printed and plotted, not just the code - default: true - x-i18n: - title: { zh-CN: 包含单元格输出 } - description: { zh-CN: 除代码外,也索引代码单元格的打印内容和图表 } - max_output_chars: - type: integer - title: Output length limit - description: Characters kept from each text output - default: 2000 - minimum: 0 - x-i18n: - title: { zh-CN: 输出长度上限 } - description: { zh-CN: 每段文本输出保留的字符数 } diff --git a/examples/plugins/notebooks/test_main.py b/examples/plugins/notebooks/test_main.py deleted file mode 100644 index f23d93cc..00000000 --- a/examples/plugins/notebooks/test_main.py +++ /dev/null @@ -1,73 +0,0 @@ -import base64 -import json -import os -import sys -import unittest - -HERE = os.path.dirname(os.path.abspath(__file__)) -sys.path.insert(0, os.path.join(HERE, "..", "..", "..", "pluginsdk", "python", "src")) -sys.path.insert(0, HERE) - -from main import convert, plugin # noqa: E402 -from weknora_plugin import ErrorCode, ParseInput, PluginError # noqa: E402 -from weknora_plugin.plugin import Call # noqa: E402 - -PNG = base64.b64encode(b"\x89PNG fake").decode() - -NOTEBOOK = { - "metadata": {"kernelspec": {"language": "python"}}, - "cells": [ - {"cell_type": "markdown", "source": ["# Sales report\n", "Quarterly numbers."]}, - { - "cell_type": "code", - "source": "print('total', 42)\nx = '```'", - "outputs": [ - {"output_type": "stream", "name": "stdout", "text": ["total 42\n"]}, - {"output_type": "display_data", "data": {"image/png": PNG, "text/plain": "
"}}, - {"output_type": "execute_result", "data": {"text/plain": ["'" + "a" * 50 + "'"]}}, - {"output_type": "error", "ename": "ValueError", "evalue": "bad"}, - ], - }, - {"cell_type": "code", "source": "", "outputs": []}, - ], -} - - -class ConvertTest(unittest.TestCase): - def test_cells_and_outputs(self): - out = convert(NOTEBOOK, max_output_chars=20) - md = out.markdown - self.assertTrue(md.startswith("# Sales report\nQuarterly numbers.\n\n````python\nprint('total', 42)")) - self.assertIn("```text\ntotal 42\n```", md) - self.assertIn("![Output of cell 2](images/cell1-1.png)", md) - self.assertIn("'" + "a" * 19 + "\n…", md) - self.assertIn("ValueError: bad", md) - self.assertEqual([(i.original_ref, i.data, i.mime_type) for i in out.images], [("images/cell1-1.png", b"\x89PNG fake", "image/png")]) - self.assertEqual(out.metadata, {"cells": "3", "language": "python", "title": "Sales report"}) - - def test_without_outputs(self): - out = convert(NOTEBOOK, include_outputs=False) - self.assertNotIn("total 42", out.markdown) - self.assertEqual(out.images, []) - - -class ParseTest(unittest.TestCase): - def call(self, tenant=None): - return Call({"context": {"tenantId": 1}, "config": {"tenant": tenant or {}}}) - - def test_parse(self): - doc = ParseInput(file_name="r.ipynb", file_type="ipynb", content=json.dumps(NOTEBOOK).encode()) - parse = plugin._parsers["ipynb"] - self.assertIn("total 42", parse(self.call(), doc).markdown) - self.assertNotIn("total 42", parse(self.call({"include_outputs": False}), doc).markdown) - - def test_not_a_notebook(self): - parse = plugin._parsers["ipynb"] - for content in (b"WeKnora conformance check\n", b"[]", b"\xff\xfe"): - with self.assertRaises(PluginError) as ctx: - parse(self.call(), ParseInput(file_name="x.ipynb", content=content)) - self.assertEqual(ctx.exception.code, ErrorCode.BAD_REQUEST) - - -if __name__ == "__main__": - unittest.main() diff --git a/examples/plugins/rss/feeds.go b/examples/plugins/rss/feeds.go deleted file mode 100644 index a01e1aac..00000000 --- a/examples/plugins/rss/feeds.go +++ /dev/null @@ -1,282 +0,0 @@ -package main - -import ( - "bytes" - "context" - "crypto/sha256" - "encoding/hex" - "fmt" - "io" - "maps" - "net/http" - "net/url" - "strings" - "time" - "unicode/utf8" - - readability "codeberg.org/readeck/go-readability/v2" - htmltomd "github.com/JohannesKaufmann/html-to-markdown/v2" - "github.com/mmcdole/gofeed" -) - -const ( - requestTimeout = 20 * time.Second - maxFeedSize = 10 << 20 - maxArticleSize = 5 << 20 - defaultUserAgent = "Mozilla/5.0 (compatible; WeKnora-RSS/1.0; +https://weknora.weixin.qq.com)" - maxFileNameBytes = 200 -) - -// config is the data source configuration: feed URLs in settings, optional -// auth headers (which may carry secrets) in credentials. -type config struct { - feedURLs []string - authHeaders map[string]string -} - -func parseConfig(settings, credentials map[string]any) (*config, error) { - raw, _ := settings["feed_urls"].(string) - if strings.TrimSpace(raw) == "" { - // Older rows kept the URLs with the credentials. - raw, _ = credentials["feed_urls"].(string) - } - cfg := &config{feedURLs: splitURLs(raw)} - if len(cfg.feedURLs) == 0 { - return nil, errMissingFeeds - } - headers, _ := credentials["auth_headers"].(string) - cfg.authHeaders = parseHeaders(headers) - return cfg, nil -} - -// splitURLs splits on newlines and commas, trims, dedupes (order kept). -func splitURLs(raw string) []string { - parts := strings.FieldsFunc(raw, func(r rune) bool { return r == '\n' || r == '\r' || r == ',' }) - seen := map[string]bool{} - var out []string - for _, u := range parts { - u = strings.TrimSpace(u) - if u == "" || seen[u] { - continue - } - seen[u] = true - out = append(out, u) - } - return out -} - -func parseHeaders(raw string) map[string]string { - headers := map[string]string{} - for _, line := range strings.Split(raw, "\n") { - name, value, ok := strings.Cut(strings.TrimSpace(line), ":") - name = strings.TrimSpace(name) - if !ok || name == "" { - continue - } - headers[name] = strings.TrimSpace(value) - } - if len(headers) == 0 { - return nil - } - return headers -} - -// fetcher downloads feeds and article pages. Outbound traffic goes through -// the plugin host's egress proxy (HTTPS_PROXY), which refuses private -// addresses, so the plugin needs no SSRF logic of its own. -type fetcher struct { - http *http.Client - headers map[string]string -} - -func newFetcher(headers map[string]string) *fetcher { - return &fetcher{http: &http.Client{Timeout: requestTimeout}, headers: headers} -} - -// fetch attaches auth headers only to feed fetches; article pages on -// third-party sites must not receive feed credentials. -func (f *fetcher) fetch(ctx context.Context, rawURL string, maxSize int64, withAuth bool) ([]byte, error) { - u, err := url.Parse(rawURL) - if err != nil || (u.Scheme != "http" && u.Scheme != "https") { - return nil, fmt.Errorf("invalid URL %q", rawURL) - } - req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil) - if err != nil { - return nil, err - } - if withAuth { - for k, v := range f.headers { - req.Header.Set(k, v) - } - } - if req.Header.Get("User-Agent") == "" { - req.Header.Set("User-Agent", defaultUserAgent) - } - if req.Header.Get("Accept") == "" { - req.Header.Set("Accept", "application/rss+xml, application/atom+xml, application/xml, text/xml, "+ - "application/json, text/html;q=0.9, */*;q=0.8") - } - resp, err := f.http.Do(req) - if err != nil { - return nil, fmt.Errorf("fetch failed: %w", err) - } - defer func() { _ = resp.Body.Close() }() - if resp.StatusCode < 200 || resp.StatusCode >= 300 { - return nil, fmt.Errorf("HTTP %d %s", resp.StatusCode, resp.Status) - } - body, err := io.ReadAll(io.LimitReader(resp.Body, maxSize+1)) - if err != nil { - return nil, fmt.Errorf("read body failed: %w", err) - } - if int64(len(body)) > maxSize { - return nil, fmt.Errorf("response exceeds maximum size (%d bytes)", maxSize) - } - return body, nil -} - -func (f *fetcher) feed(ctx context.Context, feedURL string) (*gofeed.Feed, error) { - data, err := f.fetch(ctx, feedURL, maxFeedSize, true) - if err != nil { - return nil, err - } - feed, err := gofeed.NewParser().Parse(bytes.NewReader(data)) - if err != nil { - return nil, fmt.Errorf("parse feed: %w", err) - } - return feed, nil -} - -// article returns the readability-cleaned main content of a page as HTML. -func (f *fetcher) article(ctx context.Context, articleURL string) (string, string, error) { - body, err := f.fetch(ctx, articleURL, maxArticleSize, false) - if err != nil { - return "", "", err - } - pageURL, _ := url.Parse(articleURL) - art, err := readability.FromReader(bytes.NewReader(body), pageURL) - if err != nil { - return "", "", fmt.Errorf("readability parse: %w", err) - } - if art.Node == nil { - return "", "", fmt.Errorf("no readable content extracted") - } - var buf bytes.Buffer - if err := art.RenderHTML(&buf); err != nil { - return "", "", fmt.Errorf("render article html: %w", err) - } - return buf.String(), art.Title(), nil -} - -// cursorState is the incremental sync state, the same shape the builtin -// connector keeps: feed URL → item ID → content fingerprint, and feed URL → -// item ID → feed-only signal that lets unchanged entries skip article fetches. -type cursorState struct { - FeedItems map[string]map[string]string - FeedSignals map[string]map[string]string -} - -func newCursorState() *cursorState { - return &cursorState{FeedItems: map[string]map[string]string{}, FeedSignals: map[string]map[string]string{}} -} - -func cursorFromState(state map[string]any) *cursorState { - c := newCursorState() - read := func(key string, into map[string]map[string]string) { - feeds, _ := state[key].(map[string]any) - for feedURL, items := range feeds { - m, _ := items.(map[string]any) - into[feedURL] = map[string]string{} - for id, v := range m { - if s, ok := v.(string); ok { - into[feedURL][id] = s - } - } - } - } - read("feed_items", c.FeedItems) - read("feed_signals", c.FeedSignals) - return c -} - -func (c *cursorState) toState() map[string]any { - return map[string]any{"feed_items": c.FeedItems, "feed_signals": c.FeedSignals} -} - -func (c *cursorState) copyFeed(prev *cursorState, feedURL string) { - if prev == nil { - return - } - if src := prev.FeedItems[feedURL]; len(src) > 0 { - c.FeedItems[feedURL] = maps.Clone(src) - } - if src := prev.FeedSignals[feedURL]; len(src) > 0 { - c.FeedSignals[feedURL] = maps.Clone(src) - } -} - -func contentFingerprint(markdown string) string { - sum := sha256.Sum256([]byte(markdown)) - return "h:" + hex.EncodeToString(sum[:])[:16] -} - -func feedSignal(item *gofeed.Item, feedContent string) string { - var b strings.Builder - b.WriteString(item.GUID + "\n" + item.Link + "\n" + item.Title + "\n") - if item.UpdatedParsed != nil && !item.UpdatedParsed.IsZero() { - b.WriteString(item.UpdatedParsed.UTC().Format(time.RFC3339)) - } - b.WriteByte('\n') - if item.PublishedParsed != nil && !item.PublishedParsed.IsZero() { - b.WriteString(item.PublishedParsed.UTC().Format(time.RFC3339)) - } - b.WriteByte('\n') - b.WriteString(feedContent) - sum := sha256.Sum256([]byte(b.String())) - return "s:" + hex.EncodeToString(sum[:])[:16] -} - -func firstNonEmpty(values ...string) string { - for _, v := range values { - if strings.TrimSpace(v) != "" { - return v - } - } - return "" -} - -func htmlToMarkdown(html string) string { - if strings.TrimSpace(html) == "" { - return "" - } - md, err := htmltomd.ConvertString(html) - if err != nil || strings.TrimSpace(md) == "" { - return strings.TrimSpace(html) - } - return strings.TrimSpace(md) -} - -var fileNameReplacer = strings.NewReplacer( - "/", "_", "\\", "_", ":", "_", "*", "_", "?", "_", "\"", "_", "<", "_", ">", "_", "|", "_", -) - -// fileName makes a feed title safe to use as a file name, the way WeKnora's -// builtin connectors do: hostile punctuation replaced, at most 200 bytes -// without splitting a rune. -func fileName(title string) string { - title = strings.TrimSpace(strings.NewReplacer("\n", " ", "\r", " ", "\t", " ").Replace(title)) - if title == "" { - return "untitled.md" - } - name := fileNameReplacer.Replace(title) - if len(name) > maxFileNameBytes { - name = name[:maxFileNameBytes] - for len(name) > 0 { - r, size := utf8.DecodeLastRuneInString(name) - if r != utf8.RuneError || size != 1 { - break - } - name = name[:len(name)-1] - } - } - return name + ".md" -} diff --git a/examples/plugins/rss/icon.svg b/examples/plugins/rss/icon.svg deleted file mode 100644 index 485e6cec..00000000 --- a/examples/plugins/rss/icon.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/examples/plugins/rss/main.go b/examples/plugins/rss/main.go deleted file mode 100644 index d1aaa55e..00000000 --- a/examples/plugins/rss/main.go +++ /dev/null @@ -1,229 +0,0 @@ -// Command rss is WeKnora's RSS/Atom/JSON feed connector written as an -// external plugin with the Go SDK. It behaves like the builtin rss connector -// (a test syncs the same feeds through both) and shows how a code plugin is -// built: it imports only the SDK and ordinary libraries, never WeKnora's -// internals. -// -// Build a package with ./package.sh; install it under System administration -// → Plugin management. -package main - -import ( - "context" - "errors" - "fmt" - "log" - "strings" - "time" - - "github.com/mmcdole/gofeed" - - "github.com/Tencent/WeKnora/pluginsdk" - "github.com/Tencent/WeKnora/pluginsdk/pluginapi" -) - -// Version must match plugin.yaml. -const Version = "1.0.0" - -var errMissingFeeds = pluginapi.InvalidConfig( - "feed_urls is required", - map[string]string{"settings.feed_urls": "required"}, -) - -func main() { - p := pluginsdk.New(pluginsdk.Info{ID: "weknora-examples.rss", Version: Version}) - p.Connector("rss", &connector{logf: p.Logger().Warn}) - if err := p.Serve(); err != nil { - log.Fatal(err) - } -} - -type connector struct { - logf func(msg string, args ...any) -} - -func (c *connector) warn(msg string, args ...any) { - if c.logf != nil { - c.logf(msg, args...) - } -} - -// Validate fetches and parses every configured feed. -func (c *connector) Validate(ctx context.Context, _ *pluginsdk.Call, in pluginsdk.ConnectorConfig) error { - cfg, err := parseConfig(in.Settings, in.Credentials) - if err != nil { - return err - } - f := newFetcher(cfg.authHeaders) - for _, u := range cfg.feedURLs { - if _, err := f.feed(ctx, u); err != nil { - return pluginapi.InvalidConfig(fmt.Sprintf("feed %s: %v", u, err), - map[string]string{"settings.feed_urls": err.Error()}) - } - } - return nil -} - -// ListResources lists one resource per feed. A feed that fails still -// appears, named by its URL, so the user can deselect it. -func (c *connector) ListResources( - ctx context.Context, _ *pluginsdk.Call, in pluginsdk.ConnectorConfig, parentID string, -) ([]pluginapi.Resource, error) { - if parentID != "" { - return nil, nil - } - cfg, err := parseConfig(in.Settings, in.Credentials) - if err != nil { - return nil, err - } - f := newFetcher(cfg.authHeaders) - out := make([]pluginapi.Resource, 0, len(cfg.feedURLs)) - for _, u := range cfg.feedURLs { - res := pluginapi.Resource{ExternalID: u, Type: "feed", Name: u, URL: u} - feed, err := f.feed(ctx, u) - if err != nil { - res.Description = "fetch failed: " + err.Error() - out = append(out, res) - continue - } - if t := strings.TrimSpace(feed.Title); t != "" { - res.Name = t - } - res.Description = strings.TrimSpace(feed.Description) - if feed.Link != "" { - res.URL = feed.Link - } - res.ModifiedAt = feed.UpdatedParsed - res.Metadata = map[string]any{"item_count": len(feed.Items)} - out = append(out, res) - } - return out, nil -} - -// Fetch syncs the selected feeds (all configured ones when none are -// selected). Incremental runs skip entries whose feed signal and content are -// unchanged; deletions are never emitted, since feeds drop old items as a -// matter of course. -func (c *connector) Fetch( - ctx context.Context, - _ *pluginsdk.Call, - in pluginsdk.ConnectorConfig, - req pluginapi.FetchInput, - s *pluginsdk.Stream, -) (*pluginapi.Cursor, error) { - cfg, err := parseConfig(in.Settings, in.Credentials) - if err != nil { - return nil, err - } - feedURLs := req.ResourceIDs - if len(feedURLs) == 0 { - feedURLs = cfg.feedURLs - } - var prev *cursorState - incremental := req.Mode == pluginapi.FetchIncremental && req.Cursor != nil - if incremental { - prev = cursorFromState(req.Cursor.State) - } - f := newFetcher(cfg.authHeaders) - next := newCursorState() - now := time.Now().UTC() - var failures []string - - for _, feedURL := range feedURLs { - feed, err := f.feed(ctx, feedURL) - if err != nil { - c.warn("feed failed", "url", feedURL, "err", err) - failures = append(failures, fmt.Sprintf("%s: %v", feedURL, err)) - next.copyFeed(prev, feedURL) - continue - } - next.FeedItems[feedURL] = map[string]string{} - next.FeedSignals[feedURL] = map[string]string{} - var prevItems, prevSignals map[string]string - if prev != nil { - prevItems, prevSignals = prev.FeedItems[feedURL], prev.FeedSignals[feedURL] - } - for _, item := range feed.Items { - if item == nil { - continue - } - itemID := firstNonEmpty(item.GUID, item.Link, item.Title) - if itemID == "" { - continue - } - feedContent := firstNonEmpty(item.Content, item.Description) - sig := feedSignal(item, feedContent) - if incremental && prevItems[itemID] != "" && prevSignals[itemID] == sig { - next.FeedItems[feedURL][itemID] = prevItems[itemID] - next.FeedSignals[feedURL][itemID] = sig - continue - } - fetched, fp := c.resolve(ctx, f, feed, item, feedURL, itemID, feedContent) - next.FeedItems[feedURL][itemID] = fp - next.FeedSignals[feedURL][itemID] = sig - if incremental && prevItems[itemID] == fp { - continue - } - if err := s.Item(fetched); err != nil { - return nil, err - } - } - // Each finished feed is a safe place to resume from. - if err := s.Checkpoint(pluginapi.Cursor{LastSyncTime: &now, State: next.toState()}); err != nil { - return nil, err - } - } - if len(failures) > 0 && len(failures) == len(feedURLs) { - return nil, pluginapi.Errorf(pluginapi.CodeUnavailable, "all feeds failed: %s", strings.Join(failures, "; ")) - } - if len(failures) > 0 { - _ = s.Log("warn", "some feeds failed: "+strings.Join(failures, "; ")) - } - return &pluginapi.Cursor{LastSyncTime: &now, State: next.toState()}, nil -} - -// resolve builds one item: the article's full text when it can be fetched, -// the feed's own content otherwise, as Markdown. -func (c *connector) resolve( - ctx context.Context, f *fetcher, feed *gofeed.Feed, item *gofeed.Item, feedURL, itemID, feedContent string, -) (pluginapi.FetchedItem, string) { - title := firstNonEmpty(item.Title, "untitled") - contentHTML := feedContent - if strings.TrimSpace(item.Link) != "" { - html, articleTitle, err := f.article(ctx, item.Link) - if err == nil { - contentHTML = html - if item.Title == "" && articleTitle != "" { - title = articleTitle - } - } else if !errors.Is(err, context.Canceled) { - c.warn("full-text fetch failed, using feed content", "url", item.Link, "err", err) - } - } - content := htmlToMarkdown(contentHTML) - updated := time.Now().UTC() - switch { - case item.UpdatedParsed != nil && !item.UpdatedParsed.IsZero(): - updated = *item.UpdatedParsed - case item.PublishedParsed != nil && !item.PublishedParsed.IsZero(): - updated = *item.PublishedParsed - } - author := "" - if item.Author != nil { - author = item.Author.Name - } - return pluginapi.FetchedItem{ - ExternalID: feedURL + ":" + itemID, - Title: title, - Content: []byte(content), - ContentType: "text/markdown", - FileName: fileName(title), - URL: item.Link, - UpdatedAt: &updated, - SourceResourceID: feedURL, - Metadata: map[string]string{ - "channel": "rss", "feed_url": feedURL, "feed_title": feed.Title, - "guid": item.GUID, "link": item.Link, "author": author, - }, - }, contentFingerprint(content) -} diff --git a/examples/plugins/rss/package.sh b/examples/plugins/rss/package.sh deleted file mode 100755 index 914d5164..00000000 --- a/examples/plugins/rss/package.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env bash -# Builds the RSS example plugin for the platforms WeKnora runs on and zips it -# into a .wkp package: plugin.yaml, schemas, icon and bin/-/rss. -set -euo pipefail -cd "$(dirname "$0")" -version=$(sed -n 's/^version: //p' plugin.yaml) -out=${1:-"weknora-examples-rss-${version}.wkp"} -stage=$(mktemp -d) -trap 'rm -rf "$stage"' EXIT -cp plugin.yaml icon.svg "$stage"/ -cp -r schemas "$stage"/ -for target in linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64; do - os=${target%/*} - arch=${target#*/} - bin="$stage/bin/${os}-${arch}/rss" - [ "$os" = windows ] && bin="$bin.exe" - CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -trimpath -ldflags="-s -w" -o "$bin" . -done -rm -f "$out" -(cd "$stage" && zip -qr - .) > "$out" -echo "wrote $out" diff --git a/examples/plugins/rss/parity_test.go b/examples/plugins/rss/parity_test.go deleted file mode 100644 index d546d92c..00000000 --- a/examples/plugins/rss/parity_test.go +++ /dev/null @@ -1,279 +0,0 @@ -package main - -import ( - "bytes" - "context" - "encoding/json" - "fmt" - "go/parser" - "go/token" - "net/http" - "net/http/httptest" - "os" - "path/filepath" - "sort" - "strings" - "sync" - "testing" - "time" - - builtin "github.com/Tencent/WeKnora/internal/datasource/connector/rss" - "github.com/Tencent/WeKnora/internal/types" - "github.com/Tencent/WeKnora/internal/utils" - "github.com/Tencent/WeKnora/pluginsdk" - "github.com/Tencent/WeKnora/pluginsdk/client" - "github.com/Tencent/WeKnora/pluginsdk/conformance" - "github.com/Tencent/WeKnora/pluginsdk/pluginapi" -) - -// feedSite serves two feeds and the article pages they link to. Changing an -// entry's text simulates a feed update between syncs. -type feedSite struct { - *httptest.Server - mu sync.Mutex - secondA string -} - -func newFeedSite(t *testing.T) *feedSite { - s := &feedSite{secondA: "Second post, first version."} - mux := http.NewServeMux() - mux.HandleFunc("/a.xml", func(w http.ResponseWriter, r *http.Request) { - s.mu.Lock() - second := s.secondA - s.mu.Unlock() - w.Header().Set("Content-Type", "application/rss+xml") - _, _ = fmt.Fprintf(w, `Feed A%[1]s -First feed -Hello: world%[1]s/articles/1a-1 -Mon, 02 Jan 2006 15:04:05 GMTTeaser one -Seconda-2Tue, 03 Jan 2006 15:04:05 GMT -%[2]s

]]>
-
`, "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, ` -Feed Burn:b2006-01-04T00:00:00Z -Atom entryurn:b:12006-01-04T00:00:00Z -Ada<h2>Atom</h2><p>body</p> -`) - }) - mux.HandleFunc("/articles/1", func(w http.ResponseWriter, _ *http.Request) { - _, _ = fmt.Fprint(w, `Article one
-

Hello world

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.

-

A second paragraph makes the article clearly the densest block on the page.

`) - }) - s.Server = httptest.NewServer(mux) - t.Cleanup(s.Close) - return s -} - -// syncBoth runs the builtin connector and the plugin over the same config. -type harness struct { - t *testing.T - builtin *builtin.Connector - plugin *client.Client - cfg *types.DataSourceConfig -} - -func newHarness(t *testing.T) (*harness, *feedSite) { - t.Helper() - site := newFeedSite(t) - // The builtin connector fetches through the SSRF-safe client. - t.Setenv("SSRF_WHITELIST", "127.0.0.1") - utils.ResetSSRFWhitelistForTest() - t.Cleanup(utils.ResetSSRFWhitelistForTest) - - p := pluginsdk.New(pluginsdk.Info{ID: "weknora-examples.rss", Version: Version}) - p.Connector("rss", &connector{}) - srv := httptest.NewServer(p.Handler()) - t.Cleanup(srv.Close) - return &harness{ - t: t, builtin: builtin.NewConnector(), plugin: client.New(srv.URL, nil, nil), - cfg: &types.DataSourceConfig{ - Type: types.ConnectorTypeRSS, - Settings: map[string]any{"feed_urls": site.URL + "/a.xml\n" + site.URL + "/b.atom"}, - }, - }, site -} - -func (h *harness) pluginFetch(mode string, cursor *types.SyncCursor) ([]types.FetchedItem, *types.SyncCursor) { - h.t.Helper() - instance := map[string]any{"settings": h.cfg.Settings, "credentials": map[string]any{}} - in := pluginapi.FetchInput{Mode: mode} - if cursor != nil { - in.Cursor = &pluginapi.Cursor{State: cursor.ConnectorCursor} - } - var items []types.FetchedItem - end, err := h.plugin.Stream(context.Background(), pluginapi.ConnectorFetchPath("rss"), - pluginapi.Envelope{Config: pluginapi.Config{Instance: instance}}, in, func(ev pluginapi.Event) error { - if ev.Type != pluginapi.EventItem { - return nil - } - var it pluginapi.FetchedItem - if err := json.Unmarshal(ev.Data, &it); err != nil { - return err - } - items = append(items, types.FetchedItem{ - ExternalID: it.ExternalID, Title: it.Title, Content: it.Content, ContentType: it.ContentType, - FileName: it.FileName, URL: it.URL, UpdatedAt: *it.UpdatedAt, Metadata: it.Metadata, - SourceResourceID: it.SourceResourceID, - }) - return nil - }) - if err != nil { - h.t.Fatalf("plugin fetch: %v", err) - } - var c pluginapi.Cursor - if err := json.Unmarshal(end, &c); err != nil { - h.t.Fatal(err) - } - return items, &types.SyncCursor{ConnectorCursor: c.State} -} - -// comparable renders items in a stable form; UpdatedAt of an undated item is -// "now" on both sides, so it is compared only when the feed dated the item. -func comparable(items []types.FetchedItem) []string { - var out []string - for _, it := range items { - meta, _ := json.Marshal(it.Metadata) - updated := it.UpdatedAt.UTC().Format(time.RFC3339) - if time.Since(it.UpdatedAt) < time.Hour { - updated = "now" - } - out = append(out, strings.Join([]string{ - it.ExternalID, it.Title, it.ContentType, it.FileName, it.URL, updated, it.SourceResourceID, - string(meta), string(it.Content), - }, " | ")) - } - sort.Strings(out) - return out -} - -func sameItems(t *testing.T, label string, want, got []types.FetchedItem) { - t.Helper() - w, g := comparable(want), comparable(got) - if strings.Join(w, "\n") != strings.Join(g, "\n") { - t.Fatalf("%s differ\nbuiltin:\n%s\n\nplugin:\n%s", label, strings.Join(w, "\n"), strings.Join(g, "\n")) - } -} - -func TestPluginMatchesBuiltinConnector(t *testing.T) { - ctx := context.Background() - h, site := newHarness(t) - - want, err := h.builtin.FetchAll(ctx, h.cfg, nil) - if err != nil { - t.Fatal(err) - } - got, pluginCursor := h.pluginFetch(pluginapi.FetchFull, nil) - if len(want) != 3 { - t.Fatalf("builtin fetched %d items, want 3", len(want)) - } - sameItems(t, "full sync", want, got) - if !strings.Contains( - string(got[0].Content)+string(got[1].Content)+string(got[2].Content), - "full text of the first article", - ) { - t.Fatal("the article page should replace the feed teaser") - } - - // An incremental run with nothing changed emits nothing on either side. - _, builtinCursor, err := h.builtin.FetchIncremental(ctx, h.cfg, nil) - if err != nil { - t.Fatal(err) - } - unchanged, _, err := h.builtin.FetchIncremental(ctx, h.cfg, builtinCursor) - if err != nil || len(unchanged) != 0 { - t.Fatalf("builtin incremental without changes = %d items, %v", len(unchanged), err) - } - if again, _ := h.pluginFetch(pluginapi.FetchIncremental, pluginCursor); len(again) != 0 { - t.Fatalf("plugin incremental without changes = %d items", len(again)) - } - - // One entry changes: both emit exactly that entry. - site.mu.Lock() - site.secondA = "Second post, edited." - site.mu.Unlock() - changedWant, _, err := h.builtin.FetchIncremental(ctx, h.cfg, builtinCursor) - if err != nil { - t.Fatal(err) - } - changedGot, _ := h.pluginFetch(pluginapi.FetchIncremental, pluginCursor) - if len(changedWant) != 1 { - t.Fatalf("builtin incremental after an edit = %d items", len(changedWant)) - } - sameItems(t, "incremental sync", changedWant, changedGot) -} - -func TestPluginValidatesLikeTheBuiltin(t *testing.T) { - h, site := newHarness(t) - ctx := context.Background() - call := func(settings map[string]any) error { - return h.plugin.Call(ctx, pluginapi.ConnectorValidatePath("rss"), pluginapi.Envelope{Config: pluginapi.Config{ - Instance: map[string]any{"settings": settings, "credentials": map[string]any{}}, - }}, nil, nil) - } - if err := call(h.cfg.Settings); err != nil { - t.Fatalf("valid feeds rejected: %v", err) - } - for name, settings := range map[string]map[string]any{ - "no feeds": {}, - "missing feed": {"feed_urls": site.URL + "/nope.xml"}, - } { - err := call(settings) - e, ok := pluginapi.AsError(err) - if !ok || e.Code != pluginapi.CodeInvalidConfig { - t.Errorf("%s: want invalid_config, got %v", name, err) - } - cfg := &types.DataSourceConfig{Settings: settings} - if h.builtin.Validate(ctx, cfg) == nil { - t.Errorf("%s: the builtin accepts what the plugin rejects", name) - } - } -} - -// The example must stay an external plugin: the SDK and ordinary libraries, -// nothing from WeKnora's internals. -func TestPluginImportsNoInternals(t *testing.T) { - files, _ := filepath.Glob("*.go") - fset := token.NewFileSet() - for _, f := range files { - if strings.HasSuffix(f, "_test.go") { - continue - } - src, err := os.ReadFile(f) - if err != nil { - t.Fatal(err) - } - parsed, err := parser.ParseFile(fset, f, src, parser.ImportsOnly) - if err != nil { - t.Fatal(err) - } - for _, imp := range parsed.Imports { - if strings.Contains(imp.Path.Value, "github.com/Tencent/WeKnora/internal") { - t.Errorf("%s imports %s", f, imp.Path.Value) - } - } - } -} - -func TestPluginPassesConformance(t *testing.T) { - p := pluginsdk.New(pluginsdk.Info{ID: "weknora-examples.rss", Version: Version}) - p.Connector("rss", &connector{}) - srv := httptest.NewServer(p.Handler()) - defer srv.Close() - rep := conformance.Run(context.Background(), conformance.Target{ - Client: client.New(srv.URL, nil, nil), - Raw: func(ctx context.Context, path string, body []byte) (*http.Response, error) { - req, _ := http.NewRequestWithContext(ctx, http.MethodPost, srv.URL+path, bytes.NewReader(body)) - return http.DefaultClient.Do(req) - }, - }) - for _, r := range rep.Results { - if !r.Passed { - t.Errorf("%s: %s", r.Name, r.Detail) - } - } -} diff --git a/examples/plugins/rss/plugin.yaml b/examples/plugins/rss/plugin.yaml deleted file mode 100644 index 39991cf0..00000000 --- a/examples/plugins/rss/plugin.yaml +++ /dev/null @@ -1,26 +0,0 @@ -schemaVersion: 1 -id: weknora-examples.rss -version: 1.0.0 -apiVersion: weknora.plugin/v1 -name: { en-US: RSS (plugin), zh-CN: RSS 订阅(插件版) } -description: - en-US: Sync RSS, Atom and JSON feeds into a knowledge base. The builtin RSS connector, rebuilt as a code plugin. - zh-CN: 把 RSS、Atom、JSON Feed 同步到知识库。内置 RSS 连接器的插件实现。 -publisher: { id: weknora-examples, name: WeKnora examples } -homepage: https://github.com/Tencent/WeKnora/tree/main/examples/plugins/rss -license: MIT -icon: icon.svg -runtime: { type: host, kind: binary, entry: "bin/{os}-{arch}/rss" } -permissions: - # Feed items link to pages anywhere; private addresses stay unreachable. - egress: ["*"] -contributes: - connectors: - - id: rss - name: { en-US: RSS (plugin), zh-CN: RSS 订阅(插件版) } - description: - en-US: Articles from RSS, Atom and JSON feeds, as full text - zh-CN: 抓取 RSS、Atom、JSON Feed 的文章全文 - icon: icon.svg - capabilities: [incremental, streaming] - instanceSchema: schemas/instance.yaml diff --git a/examples/plugins/rss/schemas/instance.yaml b/examples/plugins/rss/schemas/instance.yaml deleted file mode 100644 index e1675cec..00000000 --- a/examples/plugins/rss/schemas/instance.yaml +++ /dev/null @@ -1,22 +0,0 @@ -type: object -required: [feed_urls] -properties: - feed_urls: - type: string - title: Feed URLs - description: One feed per line - x-widget: textarea - x-group: settings - x-placeholder: https://example.com/feed.xml - x-i18n: - title: { zh-CN: 订阅地址 } - description: { zh-CN: 每行一个 } - auth_headers: - type: string - title: Request headers - description: "Optional, one per line as Name: Value; sent to feed URLs only" - x-secret: true - x-widget: textarea - x-i18n: - title: { zh-CN: 请求头 } - description: { zh-CN: "可选,每行一个,格式为 名称: 值;只发给订阅地址" } diff --git a/examples/plugins/subtitles/main.go b/examples/plugins/subtitles/main.go deleted file mode 100644 index bce45fbc..00000000 --- a/examples/plugins/subtitles/main.go +++ /dev/null @@ -1,130 +0,0 @@ -// Command subtitles is an example parser plugin: it turns SRT and WebVTT -// subtitle files into a Markdown transcript, one paragraph per cue with its -// start time. It also keeps a per-workspace count of parsed files through -// the Host API key-value store, to show how a plugin keeps state without a -// database. -// -// Build a package with ./package.sh. -package main - -import ( - "bufio" - "bytes" - "context" - "fmt" - "log" - "regexp" - "strings" - "time" - - "github.com/Tencent/WeKnora/pluginsdk" - "github.com/Tencent/WeKnora/pluginsdk/pluginapi" -) - -// Version must match plugin.yaml. -const Version = "1.0.0" - -func main() { - p := pluginsdk.New(pluginsdk.Info{ID: "weknora-examples.subtitles", Version: Version}) - p.Parser("subtitles", pluginsdk.ParserFunc(parse)) - if err := p.Serve(); err != nil { - log.Fatal(err) - } -} - -// timing matches a cue timing line: 00:01:02,500 --> 00:01:04,000 (SRT) or -// 01:02.500 --> 01:04.000 (WebVTT, hours optional). -var timing = regexp.MustCompile(`^\s*((?:\d+:)?\d{2}:\d{2}[.,]\d{3})\s*-->\s*`) - -// tags strips WebVTT / SRT inline markup such as or . -var tags = regexp.MustCompile(`<[^>]*>`) - -type cue struct { - start string - text []string -} - -func parse(ctx context.Context, call *pluginsdk.Call, in pluginapi.ParseInput) (*pluginapi.ParseOutput, error) { - if len(in.Content) == 0 { - return nil, pluginapi.Errorf(pluginapi.CodeInvalidConfig, "the subtitle file is empty") - } - cues := parseCues(in.Content) - if len(cues) == 0 { - return nil, pluginapi.Errorf( - pluginapi.CodeInvalidConfig, - "no subtitle cues found; is this an .srt or .vtt file?", - ) - } - title := strings.TrimSpace(in.Title) - if title == "" { - title = strings.TrimSuffix(in.FileName, "."+in.FileType) - } - var md strings.Builder - fmt.Fprintf(&md, "# %s\n\n", title) - for _, c := range cues { - fmt.Fprintf(&md, "**[%s]** %s\n\n", shortTime(c.start), strings.Join(c.text, " ")) - } - countParsed(ctx, call) - return &pluginapi.ParseOutput{ - Markdown: strings.TrimSpace(md.String()) + "\n", - Metadata: map[string]string{"cues": fmt.Sprint(len(cues))}, - }, nil -} - -func parseCues(data []byte) []cue { - data = bytes.TrimPrefix(data, []byte("\xef\xbb\xbf")) - var out []cue - var cur *cue - sc := bufio.NewScanner(bytes.NewReader(data)) - sc.Buffer(make([]byte, 0, 64<<10), 1<<20) - for sc.Scan() { - line := strings.TrimRight(sc.Text(), "\r") - if m := timing.FindStringSubmatch(line); m != nil { - out = append(out, cue{start: m[1]}) - cur = &out[len(out)-1] - continue - } - if strings.TrimSpace(line) == "" { - cur = nil - continue - } - if cur == nil { - continue // cue numbers, WEBVTT header, NOTE blocks - } - if text := strings.TrimSpace(tags.ReplaceAllString(line, "")); text != "" { - cur.text = append(cur.text, text) - } - } - kept := out[:0] - for _, c := range out { - if len(c.text) > 0 { - kept = append(kept, c) - } - } - return kept -} - -// shortTime drops milliseconds and a zero hour: 00:01:02,500 → 01:02. -func shortTime(t string) string { - t = strings.NewReplacer(",", ".").Replace(t) - if i := strings.IndexByte(t, '.'); i >= 0 { - t = t[:i] - } - return strings.TrimPrefix(t, "00:") -} - -// countParsed bumps the workspace's parsed-file counter. It is best effort: -// a missing grant or an unreachable host must not fail the parse. -func countParsed(ctx context.Context, call *pluginsdk.Call) { - host := call.Host() - if host == nil { - return - } - ctx, cancel := context.WithTimeout(ctx, 5*time.Second) - defer cancel() - var n int - if _, err := host.KVGet(ctx, "stats/parsed", &n); err != nil { - return - } - _ = host.KVPut(ctx, "stats/parsed", n+1, 0) -} diff --git a/examples/plugins/subtitles/main_test.go b/examples/plugins/subtitles/main_test.go deleted file mode 100644 index 66ee0f3c..00000000 --- a/examples/plugins/subtitles/main_test.go +++ /dev/null @@ -1,146 +0,0 @@ -package main - -import ( - "bytes" - "context" - "go/parser" - "go/token" - "net/http" - "net/http/httptest" - "os" - "path/filepath" - "strings" - "testing" - - "github.com/gin-gonic/gin" - "github.com/google/uuid" - "gorm.io/driver/sqlite" - "gorm.io/gorm" - - "github.com/Tencent/WeKnora/internal/application/repository" - "github.com/Tencent/WeKnora/internal/plugin/hostapi" - "github.com/Tencent/WeKnora/internal/types" - "github.com/Tencent/WeKnora/pluginsdk" - "github.com/Tencent/WeKnora/pluginsdk/client" - "github.com/Tencent/WeKnora/pluginsdk/conformance" - "github.com/Tencent/WeKnora/pluginsdk/pluginapi" -) - -const srt = "\ufeff1\r\n00:00:01,000 --> 00:00:03,500\r\nHello world.\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 -Plugins run out of process. - -1:00:00.000 --> 1:00:02.000 align:start -Bye. -` - -func TestParseSRTAndVTT(t *testing.T) { - ctx := context.Background() - out, err := parse( - ctx, - &pluginsdk.Call{}, - pluginapi.ParseInput{FileName: "talk.srt", FileType: "srt", Content: []byte(srt)}, - ) - if err != nil { - t.Fatal(err) - } - want := "# talk\n\n**[00:01]** Hello world.\n\n**[01:02]** Second line continues here.\n" - if out.Markdown != want || out.Metadata["cues"] != "2" { - t.Fatalf("srt markdown = %q", out.Markdown) - } - out, err = parse( - ctx, - &pluginsdk.Call{}, - pluginapi.ParseInput{FileName: "t.vtt", FileType: "vtt", Title: "Talk", Content: []byte(vtt)}, - ) - if err != nil { - t.Fatal(err) - } - if !strings.Contains(out.Markdown, "# Talk") || - !strings.Contains(out.Markdown, "**[01:02]** Plugins run out of process.") || - !strings.Contains(out.Markdown, "**[1:00:00]** Bye.") { - t.Fatalf("vtt markdown = %q", out.Markdown) - } - for _, bad := range []string{"", "just some text\nwith no cues\n"} { - _, err := parse( - ctx, - &pluginsdk.Call{}, - pluginapi.ParseInput{FileName: "x.srt", FileType: "srt", Content: []byte(bad)}, - ) - if e, ok := pluginapi.AsError(err); !ok || e.Code != pluginapi.CodeInvalidConfig || e.Retryable { - t.Errorf("%q: want a permanent invalid_config error, got %v", bad, err) - } - } -} - -func TestParseCountsThroughTheHostAPI(t *testing.T) { - gin.SetMode(gin.TestMode) - db, err := gorm.Open(sqlite.Open("file:"+uuid.NewString()+"?mode=memory&cache=shared"), &gorm.Config{}) - if err != nil { - t.Fatal(err) - } - if err := db.AutoMigrate(&types.PluginKV{}); err != nil { - t.Fatal(err) - } - iss := hostapi.NewIssuer([]byte("k")) - r := gin.New() - hostapi.NewHandler(iss, hostapi.NewKV(repository.NewPluginKVRepository(db))).Register(r) - srv := httptest.NewServer(r) - defer srv.Close() - - tok, _, _ := iss.Issue("weknora-examples.subtitles", Version, 3, []string{"kv"}) - call := &pluginsdk.Call{ - Context: pluginapi.Context{TenantID: 3, Host: &pluginapi.HostAccess{URL: srv.URL, Token: tok}}, - } - in := pluginapi.ParseInput{FileName: "a.srt", FileType: "srt", Content: []byte(srt)} - for range 2 { - if _, err := parse(context.Background(), call, in); err != nil { - t.Fatal(err) - } - } - var n int - if ok, err := call.Host().KVGet(context.Background(), "stats/parsed", &n); !ok || err != nil || n != 2 { - t.Fatalf("parsed count = %d (%v, %v)", n, ok, err) - } -} - -func TestConformsAndStaysExternal(t *testing.T) { - p := pluginsdk.New(pluginsdk.Info{ID: "weknora-examples.subtitles", Version: Version}) - p.Parser("subtitles", pluginsdk.ParserFunc(parse)) - srv := httptest.NewServer(p.Handler()) - defer srv.Close() - rep := conformance.Run(context.Background(), conformance.Target{ - Client: client.New(srv.URL, nil, nil), - Raw: func(ctx context.Context, path string, body []byte) (*http.Response, error) { - req, _ := http.NewRequestWithContext(ctx, http.MethodPost, srv.URL+path, bytes.NewReader(body)) - return http.DefaultClient.Do(req) - }, - }) - for _, res := range rep.Results { - if !res.Passed { - t.Errorf("%s: %s", res.Name, res.Detail) - } - } - files, _ := filepath.Glob("*.go") - for _, f := range files { - if strings.HasSuffix(f, "_test.go") { - continue - } - src, _ := os.ReadFile(f) - parsed, err := parser.ParseFile(token.NewFileSet(), f, src, parser.ImportsOnly) - if err != nil { - t.Fatal(err) - } - for _, imp := range parsed.Imports { - if strings.Contains(imp.Path.Value, "WeKnora/internal") { - t.Errorf("%s imports %s", f, imp.Path.Value) - } - } - } -} diff --git a/examples/plugins/subtitles/package.sh b/examples/plugins/subtitles/package.sh deleted file mode 100755 index 09f3d236..00000000 --- a/examples/plugins/subtitles/package.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env bash -# Builds the subtitles example plugin for the platforms WeKnora runs on and zips it -# into a .wkp package: plugin.yaml and bin/-/subtitles. -set -euo pipefail -cd "$(dirname "$0")" -version=$(sed -n 's/^version: //p' plugin.yaml) -out=${1:-"weknora-examples-subtitles-${version}.wkp"} -stage=$(mktemp -d) -trap 'rm -rf "$stage"' EXIT -cp plugin.yaml "$stage"/ -for target in linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64; do - os=${target%/*} - arch=${target#*/} - bin="$stage/bin/${os}-${arch}/subtitles" - [ "$os" = windows ] && bin="$bin.exe" - CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -trimpath -ldflags="-s -w" -o "$bin" . -done -rm -f "$out" -(cd "$stage" && zip -qr - .) > "$out" -echo "wrote $out" diff --git a/examples/plugins/subtitles/plugin.yaml b/examples/plugins/subtitles/plugin.yaml deleted file mode 100644 index 86252e3f..00000000 --- a/examples/plugins/subtitles/plugin.yaml +++ /dev/null @@ -1,23 +0,0 @@ -schemaVersion: 1 -id: weknora-examples.subtitles -version: 1.0.0 -apiVersion: weknora.plugin/v1 -name: { en-US: Subtitles, zh-CN: 字幕解析 } -description: - en-US: Parse SRT and WebVTT subtitles into a searchable transcript. - zh-CN: 把 SRT、WebVTT 字幕解析为可检索的文字稿。 -publisher: { id: weknora-examples, name: WeKnora examples } -homepage: https://github.com/Tencent/WeKnora/tree/main/examples/plugins/subtitles -license: MIT -runtime: { type: host, kind: binary, entry: "bin/{os}-{arch}/subtitles" } -permissions: - # Counts parsed files per workspace in the Host API key-value store. - hostApi: [kv] -contributes: - parsers: - - id: subtitles - name: { en-US: Subtitles, zh-CN: 字幕解析 } - description: - en-US: SRT and WebVTT subtitles as a transcript with cue times - zh-CN: 带时间点的字幕文字稿 - fileTypes: [srt, vtt] diff --git a/frontend/src/api/agent/index.ts b/frontend/src/api/agent/index.ts index 35b8ea59..457e1316 100644 --- a/frontend/src/api/agent/index.ts +++ b/frontend/src/api/agent/index.ts @@ -1,4 +1,3 @@ -import type { ConfigSchema } from '@/components/schema-form/schema' import { get, post, put, del } from "../../utils/request"; // 智能体配置 @@ -348,35 +347,6 @@ export interface IMChannelOverview { updated_at: string; } -// IMPlatformInfo describes one IM platform (internal/im.PlatformInfo): its -// connection modes, console links and the credential form. -export interface IMPlatformInfo { - id: string - name: string - names?: Record - order: number - /** Supported connection modes, default first. */ - modes: Array - mode_labels?: Record - mode_hint_key?: string - supports_thread: boolean - links?: Array<{ url: string; title: string; title_key?: string }> - config_schema?: ConfigSchema -} - -/** - * One channel for the editor, credentials included: plain fields as stored, - * set secrets as "***". Send them back unchanged to keep the stored secret. - */ -export function getIMChannel(id: string) { - return get<{ data: IMChannel }>(`/api/v1/im-channels/${id}`); -} - -/** The IM platforms this deployment has adapters for, in display order. */ -export function listIMPlatforms() { - return get<{ data: IMPlatformInfo[] }>('/api/v1/im-channels/platforms'); -} - export function listAllIMChannels() { return get<{ data: IMChannelOverview[] }>('/api/v1/im-channels'); } diff --git a/frontend/src/api/datasource/index.ts b/frontend/src/api/datasource/index.ts index e3f86792..19096307 100644 --- a/frontend/src/api/datasource/index.ts +++ b/frontend/src/api/datasource/index.ts @@ -1,4 +1,3 @@ -import type { ConfigSchema } from '@/components/schema-form/schema' import { get, post, put, del } from '../../utils/request' // --- Types --- @@ -73,20 +72,6 @@ export interface ConnectorMeta { priority: number auth_type: string capabilities: string[] - // Setup guide next to the credential form. - doc_url?: string - permission_doc_url?: string - permission_page_url?: string - required_permissions?: string[] - // Credential form: describes config.credentials for this connector. - config_schema?: ConfigSchema - // Settings form (config.settings) for connectors whose settings UI is - // not built in, such as plugin connectors. - settings_schema?: ConfigSchema - // Localized name / description of plugin connectors, keyed by locale. - names?: Record - descriptions?: Record - 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('/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, - settings?: Record, -) { - return post('/api/v1/datasource/validate-credentials', { type, credentials, settings }) +export function validateCredentials(type: string, credentials: Record) { + return post('/api/v1/datasource/validate-credentials', { type, credentials }) } // listResources lists selectable resources for a data source. Pass parentId to diff --git a/frontend/src/api/initialization/index.ts b/frontend/src/api/initialization/index.ts index fb974835..9f020cd8 100644 --- a/frontend/src/api/initialization/index.ts +++ b/frontend/src/api/initialization/index.ts @@ -1,4 +1,3 @@ -import type { ConfigSchema } from '@/components/schema-form/schema'; import { get, post, put } from '../../utils/request'; import i18n from '@/i18n' import type { ModelCapabilities, ModelSpecOverride, ReasoningEffortLevel } from '../model' @@ -662,8 +661,6 @@ export interface ModelProviderOption { defaultUrls: Record; // 按模型类型区分的默认 URL modelTypes: string[]; // 支持的模型类型 extraFields?: ModelProviderExtraField[]; - // extraFields as a config schema over extra_config (secrets included). - configSchema?: ConfigSchema; credentialLabels?: ModelProviderCredentialLabel[]; models?: ModelCatalogEntry[]; thinking?: ModelProviderThinking; diff --git a/frontend/src/api/mcp-service.ts b/frontend/src/api/mcp-service.ts index f806026d..106446d8 100644 --- a/frontend/src/api/mcp-service.ts +++ b/frontend/src/api/mcp-service.ts @@ -7,7 +7,7 @@ export interface MCPService { description: string usage_instructions?: string enabled: boolean - transport_type: 'sse' | 'http-streamable' | 'stdio' | 'plugin' + transport_type: 'sse' | 'http-streamable' | 'stdio' url?: string // Optional: required for SSE/HTTP Streamable headers?: Record auth_config?: { @@ -41,10 +41,6 @@ export interface MCPService { } env_vars?: Record // Environment variables for stdio transport is_builtin?: boolean // Whether this is a builtin MCP service - /** Installed plugin providing the service (read-only, never stored). */ - plugin_id?: string - /** Why a plugin service is off, typically missing workspace configuration. */ - plugin_error?: string // Per-field "configured?" map embedded on the main response (server-side // dto.MCPServiceResponse.Credentials). Drives the CredentialResource card // without a follow-up GET. Absent for builtin services. diff --git a/frontend/src/api/plugin.ts b/frontend/src/api/plugin.ts deleted file mode 100644 index 00b28a71..00000000 --- a/frontend/src/api/plugin.ts +++ /dev/null @@ -1,196 +0,0 @@ -import { get, post, put } from '@/utils/request' - -import type { ConfigSchema, ConfigValue } from '@/components/schema-form/schema' -import type { LocalizedText } from '@/utils/localizedText' - -export type { LocalizedText } from '@/utils/localizedText' - -export type ExtensionPoint = - | 'modelVendors' - | 'connectors' - | 'imChannels' - | 'webSearch' - | 'tools' - | 'parsers' - | 'skills' - | 'mcpServers' - | 'pages' - | 'settingsSections' - | 'kbTabs' - | 'webhooks' - -export interface PluginContribution { - id: string - name: LocalizedText - description?: LocalizedText - icon?: string - aliases?: string[] - capabilities?: string[] - order?: number - /** Package path of a skill directory or model vendor file. */ - path?: string - /** Remote MCP server an installed plugin contributes. */ - mcp?: { url: string; transport?: string; headers?: Record } - /** 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> -} - -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> -} - -/** 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 - /** "/" of the instance form ("connectors/jira"). */ - contribution?: string - /** The instance being edited; its stored secrets fill in redacted ones. */ - instanceId?: string - /** What the form holds now, in the scope's shape. */ - values: ConfigValue - query?: string -} - -/** A field's choices from the plugin (x-options). */ -export function pluginFormOptions(pluginId: string, req: PluginOptionsRequest) { - return post<{ data: { options: Array<{ value: string; label?: string; description?: string }> } }>( - `/api/v1/plugins/${encodeURIComponent(pluginId)}/options`, req, - ) -} - -/** Starts connecting an x-oauth field; the tokens stay on the server. */ -export function startPluginOAuth( - pluginId: string, - req: { scope: PluginFormScope; contribution?: string; field: string }, -) { - return post<{ data: { authorizeUrl: string; state: string; redirectUri: string } }>( - `/api/v1/plugins/${encodeURIComponent(pluginId)}/oauth/start`, req, - ) -} diff --git a/frontend/src/api/system/index.ts b/frontend/src/api/system/index.ts index 97856437..fa717354 100644 --- a/frontend/src/api/system/index.ts +++ b/frontend/src/api/system/index.ts @@ -107,10 +107,6 @@ export interface ParserEngineInfo { FileTypes: string[] Available?: boolean UnavailableReason?: string - /** Set on engines an installed plugin provides (Name is then acme.ocr/ocr). */ - PluginID?: string - /** Localized names of plugin engines, keyed by locale; "default" as fallback. */ - DisplayNames?: Record } /** 解析引擎配置(引擎连接参数存空间;聊天附件解析策略在智能体中配置) */ diff --git a/frontend/src/api/system/plugins.ts b/frontend/src/api/system/plugins.ts deleted file mode 100644 index b7f99aa7..00000000 --- a/frontend/src/api/system/plugins.ts +++ /dev/null @@ -1,133 +0,0 @@ -// System-admin plugin installation (/api/v1/system/admin/plugins). -import { del, get, post, postUpload, put } from '@/utils/request' - -import type { ConfigValue } from '@/components/schema-form/schema' -import type { PluginConfig, PluginManifest } from '@/api/plugin' - -const BASE = '/api/v1/system/admin/plugins' -const PACKAGE_TIMEOUT = 5 * 60 * 1000 - -/** What installing a package would do. */ -export type InstallChange = 'install' | 'upgrade' | 'downgrade' | 'reinstall' - -export interface PluginPreview { - manifest: PluginManifest - digest: string - size: number - change: InstallChange - installedVersion?: string -} - -export interface PluginVersion { - plugin_id: string - version: string - digest: string - manifest: PluginManifest - size: number - created_by: string - created_at: string -} - -/** This node's report on an installed plugin. */ -export interface PluginNodeStatus { - version: string - // degraded: loaded, but its process crashed or fails health checks and is - // being restarted. - state: 'ready' | 'failed' | 'degraded' - error?: string - updatedAt: string -} - -export interface InstalledPlugin { - id: string - source: { kind: 'upload' | 'url'; url?: string } - active_version: string - desired_state: 'enabled' | 'disabled' - runtime: string - granted_perms: PluginManifest['permissions'] - created_by: string - created_at: string - updated_at: string - manifest?: PluginManifest - versions: PluginVersion[] - node?: PluginNodeStatus - /** Where a remote plugin's service runs. */ - remote_url?: string - /** A remote plugin's new signing secret, only in the response that issued it. */ - issuedSecret?: string -} - -/** A package to inspect or install: an uploaded file or a URL. */ -export type PackageSource = { file: File } | { url: string } - -function packageForm(file: File, digest?: string, remoteUrl?: string) { - const form = new FormData() - form.append('file', file) - if (digest) form.append('digest', digest) - if (remoteUrl) form.append('remote_url', remoteUrl) - return form -} - -export function listInstalledPlugins() { - return get<{ data: InstalledPlugin[] }>(BASE) -} - -export function getInstalledPlugin(id: string) { - return get<{ data: InstalledPlugin }>(`${BASE}/${encodeURIComponent(id)}`) -} - -/** Reads a package and reports what installing it would do, changing nothing. */ -export function inspectPluginPackage(source: PackageSource) { - if ('file' in source) { - return postUpload(`${BASE}/inspect`, packageForm(source.file), undefined, { timeout: PACKAGE_TIMEOUT }) as Promise<{ - data: PluginPreview - }> - } - return post<{ data: PluginPreview }>(`${BASE}/inspect`, { url: source.url }, { timeout: PACKAGE_TIMEOUT }) -} - -/** - * Installs the package reviewed with inspect; digest pins it to that package. - * A remote plugin also needs the URL of its service (optional on upgrades). - */ -export function installPluginPackage(source: PackageSource, digest: string, remoteUrl?: string) { - if ('file' in source) { - return postUpload(BASE, packageForm(source.file, digest, remoteUrl), undefined, { - timeout: PACKAGE_TIMEOUT, - }) as Promise<{ data: InstalledPlugin }> - } - return post<{ data: InstalledPlugin }>( - BASE, - { url: source.url, digest, remote_url: remoteUrl || undefined }, - { timeout: PACKAGE_TIMEOUT }, - ) -} - -export function setInstalledPluginEnabled(id: string, enabled: boolean) { - return put<{ data: InstalledPlugin }>(`${BASE}/${encodeURIComponent(id)}/enabled`, { enabled }) -} - -export function activatePluginVersion(id: string, version: string) { - return put<{ data: InstalledPlugin }>(`${BASE}/${encodeURIComponent(id)}/active-version`, { version }) -} - -export function setPluginRemoteUrl(id: string, url: string) { - return put<{ data: InstalledPlugin }>(`${BASE}/${encodeURIComponent(id)}/remote-url`, { url }) -} - -/** Issues a new signing secret; the response carries it in issuedSecret. */ -export function rotatePluginSecret(id: string) { - return post<{ data: InstalledPlugin }>(`${BASE}/${encodeURIComponent(id)}/secret/rotate`, {}) -} - -export function uninstallPlugin(id: string) { - return del(`${BASE}/${encodeURIComponent(id)}`) -} - -export function getPluginSystemConfig(id: string) { - return get<{ data: PluginConfig }>(`${BASE}/${encodeURIComponent(id)}/config`) -} - -export function updatePluginSystemConfig(id: string, values: ConfigValue) { - return put<{ data: PluginConfig }>(`${BASE}/${encodeURIComponent(id)}/config`, { values }) -} diff --git a/frontend/src/api/web-search-provider.ts b/frontend/src/api/web-search-provider.ts index 7c4ae365..425c4013 100644 --- a/frontend/src/api/web-search-provider.ts +++ b/frontend/src/api/web-search-provider.ts @@ -1,4 +1,3 @@ -import type { ConfigSchema } from '@/components/schema-form/schema' import { get, post, put, del } from '@/utils/request' // WebSearchProviderEntity represents a configured web search provider instance @@ -6,8 +5,7 @@ export interface WebSearchProviderEntity { id?: string tenant_id?: number name: string - // A builtin provider ID, or a plugin provider's qualified ID (acme.search/brave). - provider: 'brave' | 'bing' | 'google' | 'duckduckgo' | 'tavily' | 'ollama' | 'baidu' | 'searxng' | 'keenable' | 'zhipu' | 'metaso' | 'exa' | 'bocha' | 'serply' | (string & {}) + provider: 'brave' | 'bing' | 'google' | 'duckduckgo' | 'tavily' | 'ollama' | 'baidu' | 'searxng' | 'keenable' | 'zhipu' | 'metaso' | 'exa' | 'bocha' | 'serply' description?: string parameters: { // api_key is never returned by the server in this shape; it lives behind @@ -39,12 +37,6 @@ export interface WebSearchProviderTypeInfo { description?: string docs_url?: string config_fields?: WebSearchProviderConfigField[] - // The parameters form as a config schema; the settings drawer renders it. - config_schema?: ConfigSchema - // Plugin provider types: localized names, an icon (data: URI), the plugin. - names?: Record - icon?: string - plugin_id?: string } export interface WebSearchProviderConfigField { diff --git a/frontend/src/components/IMChannelPanel.vue b/frontend/src/components/IMChannelPanel.vue index c05d4eb6..77e956f8 100644 --- a/frontend/src/components/IMChannelPanel.vue +++ b/frontend/src/components/IMChannelPanel.vue @@ -119,7 +119,7 @@
- + {{ item.label }}
@@ -146,19 +146,28 @@
-
+

{{ $t('agentEditor.im.sectionAccess') }}

- +
-

{{ modeHint }}

+

+ {{ formData.platform === 'mattermost' ? $t('agentEditor.im.mattermostModeHint') : + formData.platform === 'yunzhijia' ? $t('agentEditor.im.yunzhijiaModeHint') : + $t('agentEditor.im.modeHint') }} +

@@ -240,19 +249,292 @@

{{ $t('agentEditor.im.sectionCredentials') }}

- + + - - + + + + + + + + + + + + + + + + + + + +