diff --git a/components/QuickSwitcher.pluginContributions.test.tsx b/components/QuickSwitcher.pluginContributions.test.tsx index c31c59c21..d1940381c 100644 --- a/components/QuickSwitcher.pluginContributions.test.tsx +++ b/components/QuickSwitcher.pluginContributions.test.tsx @@ -151,3 +151,24 @@ test('plugin palette items keep repeated placements independently addressable', commandId: 'com.example.palette.run', }]); }); + +test('packaged Starter contributions are discoverable through the real palette builder', async (context) => { + const { createRequire } = await import('node:module'); + const { mkdtempSync, rmSync } = await import('node:fs'); + const { tmpdir } = await import('node:os'); + const { join } = await import('node:path'); + const require = createRequire(import.meta.url); + const { PluginDatabase } = require('../electron/plugins/database.cjs'); + const { PluginContributionService } = require('../electron/plugins/contributionService.cjs'); + const manifest = JSON.parse(readFileSync(new URL('../examples/plugins/starter-netcatty/netcatty.plugin.json', import.meta.url), 'utf8')); + const root = mkdtempSync(join(tmpdir(), 'starter-palette-')); + const database = new PluginDatabase(join(root, 'plugins.sqlite')); + context.after(() => { database.close(); rmSync(root, { recursive: true, force: true }); }); + database.installVersion({ pluginId: manifest.id, version: manifest.version, manifest, archiveSha256: 'a'.repeat(64), packageRelativePath: 'starter' }, { enable: true }); + const service = new PluginContributionService({ database, runtimeSupervisor: {}, secretStore: {}, getLocale: () => 'en' }); + const items = buildPluginPaletteItems(service.snapshot().plugins, 'Starter'); + assert.ok(items.some((item) => item.type === 'plugin-command' && item.commandId === 'com.netcatty.starter.greet')); + assert.ok(items.some((item) => item.type === 'plugin-view' && item.id === 'com.netcatty.starter.panel')); + database.setEnabled(manifest.id, false); + assert.equal(buildPluginPaletteItems(service.snapshot().plugins, 'Starter').length, 0); +}); diff --git a/components/settings/tabs/PluginPackageManager.tsx b/components/settings/tabs/PluginPackageManager.tsx index 65213a428..571abae6c 100644 --- a/components/settings/tabs/PluginPackageManager.tsx +++ b/components/settings/tabs/PluginPackageManager.tsx @@ -1,5 +1,5 @@ -import { describePluginPermission } from '../../../domain/pluginPermissionDescriptions.cjs'; -import { describePluginRuntime } from '../../../domain/pluginRuntimePolicy.cjs'; +import { describePluginPermission } from '@/domain/pluginPermissionDescriptions.cjs'; +import { describePluginRuntime } from '@/domain/pluginRuntimePolicy.cjs'; import React, { useState } from 'react'; import { ArrowLeft, Package } from 'lucide-react'; import { usePluginManager } from '../../../application/state/usePluginManager'; diff --git a/electron/plugins/starterExample.test.cjs b/electron/plugins/starterExample.test.cjs new file mode 100644 index 000000000..8ddac4d6f --- /dev/null +++ b/electron/plugins/starterExample.test.cjs @@ -0,0 +1,42 @@ +"use strict"; + +const assert = require("node:assert/strict"); +const fs = require("node:fs"); +const path = require("node:path"); +const vm = require("node:vm"); +const { pathToFileURL } = require("node:url"); +const test = require("node:test"); + +test("Starter panel renders the actual command result as text and reports denial", async () => { + const root = path.resolve(__dirname, "../../examples/plugins/starter-netcatty"); + const plugin = (await import(pathToFileURL(path.join(root, "dist/index.js")).href)).default; + let command; + let receive; + let click; + const elements = new Map(["greet", "result", "title", "description"].map((id) => [id, { + textContent: "", disabled: false, addEventListener(_event, callback) { click = callback; }, + }])); + const greeting = ' synthetic hello'; + await plugin.activate({ + subscriptions: { add() {} }, + commands: { registerCommand(id, handler) { assert.equal(id, "com.netcatty.starter.greet"); command = handler; } }, + settings: { get: async () => greeting }, + views: { postMessage: (_id, message) => receive?.(message) }, + }); + let deny = false; + vm.runInNewContext(fs.readFileSync(path.join(root, "panel.js"), "utf8"), { + document: { getElementById: (id) => elements.get(id), documentElement: { style: {} } }, + window: { netcattyView: { + onDidChangeEnvironment: (listener) => listener({ locale: "en", theme: "light" }), + onDidReceiveMessage: (listener) => { receive = listener; }, + executeCommand: async (id) => { assert.equal(id, "com.netcatty.starter.greet"); if (deny) throw new Error("Denied"); return command(); }, + } }, + }); + await click(); + assert.equal(elements.get("result").textContent, greeting); + assert.equal(elements.get("greet").disabled, false); + deny = true; + await click(); + assert.match(elements.get("result").textContent, /Check permission approval/); + assert.equal(elements.get("greet").disabled, false); +}); diff --git a/examples/plugins/starter-netcatty/README.md b/examples/plugins/starter-netcatty/README.md index 3f523c8e6..bd0a9e8ad 100644 --- a/examples/plugins/starter-netcatty/README.md +++ b/examples/plugins/starter-netcatty/README.md @@ -1,7 +1,7 @@ # Plugin Starter A minimal first-party browser plugin for learning import, settings and lifecycle. -It requests only `commands` and `settings.read`: no terminal text, host credentials, +It requests `commands`, `settings.read`, `menus` and `views`: no terminal text, host credentials, network, files, secrets or advanced Node access. It works without OS key storage. Permission declarations are not grants; follow the app's normal mediation. @@ -15,9 +15,12 @@ node packages/plugin-cli/dist/cli.js pack examples/plugins/starter-netcatty --ou Start the actual app normally with a fresh test profile; no development flag is needed. Open **Settings > Plugins > Installed**, import the package, inspect its -permissions and enable it. Change **Greeting**, then find **Starter: Read Greeting** -in the command palette. The command reads the preference and records it in the -plugin's local log; it does not write to a terminal or connect to a host. +permissions and enable it. Change **Greeting**, then find **Starter: Greeting Panel** (入门:问候面板) +in the command palette. Review the normal permission prompts, then click +**Show greeting** to see the exact preference in the sandboxed panel. +**Starter: Read Greeting** is also listed and refreshes an already-open panel. +The panel inserts text using `textContent`, never HTML. It does not write to +a terminal or connect to a host. Denied permissions remain denied. Disable, restart and re-enable to verify the preference persists. Re-importing the same package leaves it disabled for review. Uninstall removes code and local diff --git a/examples/plugins/starter-netcatty/netcatty.plugin.json b/examples/plugins/starter-netcatty/netcatty.plugin.json index 6f52a0b11..f1267212a 100644 --- a/examples/plugins/starter-netcatty/netcatty.plugin.json +++ b/examples/plugins/starter-netcatty/netcatty.plugin.json @@ -2,17 +2,74 @@ "manifestVersion": 1, "id": "com.netcatty.starter", "name": "starter-netcatty", - "displayName": { "en": "Plugin Starter", "zh-CN": "插件入门" }, + "displayName": { + "en": "Plugin Starter", + "zh-CN": "插件入门" + }, "description": "First-party settings and command example. No terminal, network, filesystem or secret access.", - "version": "0.1.0", + "version": "0.1.1", "publisher": "netcatty", "license": "GPL-3.0-or-later", - "engines": { "netcatty": ">=0.0.0", "api": ">=0.1.0-internal <0.2.0" }, - "main": { "browser": "dist/index.js" }, - "activationEvents": ["onCommand:com.netcatty.starter.greet"], - "permissions": { "required": ["commands", "settings.read"], "optional": [] }, + "engines": { + "netcatty": ">=0.0.0", + "api": ">=0.1.0-internal <0.2.0" + }, + "main": { + "browser": "dist/index.js" + }, + "activationEvents": [ + "onCommand:com.netcatty.starter.greet", + "onView:com.netcatty.starter.panel" + ], + "permissions": { + "required": [ + "commands", + "settings.read", + "menus", + "views" + ], + "optional": [] + }, "contributes": { - "settings": [{ "id": "com.netcatty.starter.greeting", "label": { "en": "Greeting", "zh-CN": "问候语" }, "description": "A local preference for the example command.", "control": "text", "scope": "application", "default": "Hello, synthetic world!" }], - "commands": [{ "id": "com.netcatty.starter.greet", "title": { "en": "Starter: Read Greeting", "zh-CN": "入门:读取问候语" } }] + "settings": [ + { + "id": "com.netcatty.starter.greeting", + "label": { + "en": "Greeting", + "zh-CN": "问候语" + }, + "description": "A local preference for the example command.", + "control": "text", + "scope": "application", + "default": "Hello, synthetic world!" + } + ], + "commands": [ + { + "id": "com.netcatty.starter.greet", + "title": { + "en": "Starter: Read Greeting", + "zh-CN": "入门:读取问候语" + } + } + ], + "menus": [ + { + "command": "com.netcatty.starter.greet", + "location": "commandPalette", + "group": "examples" + } + ], + "views": [ + { + "id": "com.netcatty.starter.panel", + "title": { + "en": "Starter: Greeting Panel", + "zh-CN": "入门:问候面板" + }, + "location": "modal", + "entry": "panel.html" + } + ] } } diff --git a/examples/plugins/starter-netcatty/package.json b/examples/plugins/starter-netcatty/package.json index c50356525..8dd661aca 100644 --- a/examples/plugins/starter-netcatty/package.json +++ b/examples/plugins/starter-netcatty/package.json @@ -1,8 +1,12 @@ { "name": "@netcatty/example-starter-plugin", - "version": "0.1.0", + "version": "0.1.1", "private": true, "type": "module", - "scripts": { "build": "tsc -p tsconfig.json" }, - "dependencies": { "@netcatty/plugin-sdk": "0.1.0-internal" } + "scripts": { + "build": "tsc -p tsconfig.json" + }, + "dependencies": { + "@netcatty/plugin-sdk": "0.1.0-internal" + } } diff --git a/examples/plugins/starter-netcatty/panel.css b/examples/plugins/starter-netcatty/panel.css new file mode 100644 index 000000000..77becc47b --- /dev/null +++ b/examples/plugins/starter-netcatty/panel.css @@ -0,0 +1 @@ +:root { color-scheme: light dark; font: 15px/1.6 system-ui, sans-serif; } body { margin: 0; } main { padding: 32px; max-width: 640px; margin: auto; } .eyebrow { font-size: 11px; letter-spacing: .12em; opacity: .65; } h1 { font-size: 24px; } #description { opacity: .75; } button { font: inherit; color: inherit; background: transparent; border: 1px solid currentColor; border-radius: 8px; padding: 8px 16px; cursor: pointer; } button:disabled { opacity: .5; } #result { white-space: pre-wrap; overflow-wrap: anywhere; padding: 16px; border-radius: 8px; background: light-dark(#f3f4f6, #252525); min-height: 24px; } diff --git a/examples/plugins/starter-netcatty/panel.html b/examples/plugins/starter-netcatty/panel.html new file mode 100644 index 000000000..55c27123e --- /dev/null +++ b/examples/plugins/starter-netcatty/panel.html @@ -0,0 +1,3 @@ + +Plugin Starter +

NETCATTY · PLUGIN STARTER

Your first plugin

Read your Greeting preference and show it here. No hosts, terminals or secrets are accessed.

diff --git a/examples/plugins/starter-netcatty/panel.js b/examples/plugins/starter-netcatty/panel.js new file mode 100644 index 000000000..e8f207600 --- /dev/null +++ b/examples/plugins/starter-netcatty/panel.js @@ -0,0 +1,26 @@ +const button = document.getElementById('greet'); +const result = document.getElementById('result'); +let chinese = false; +window.netcattyView.onDidChangeEnvironment((environment) => { + chinese = environment.locale.startsWith('zh'); + document.documentElement.lang = chinese ? 'zh-CN' : 'en'; + document.documentElement.style.colorScheme = environment.theme === 'light' ? 'light' : 'dark'; + document.getElementById('title').textContent = chinese ? '你的第一个插件' : 'Your first plugin'; + document.getElementById('description').textContent = chinese ? '读取并显示你设置的问候语。不访问主机、终端或秘密。' : 'Read your Greeting preference and show it here. No hosts, terminals or secrets are accessed.'; + button.textContent = chinese ? '显示问候语' : 'Show greeting'; +}); +window.netcattyView.onDidReceiveMessage((message) => { + if (typeof message?.greeting === 'string') result.textContent = message.greeting; +}); +button.addEventListener('click', async () => { + button.disabled = true; + result.textContent = chinese ? '正在读取…' : 'Reading…'; + try { + const response = await window.netcattyView.executeCommand('com.netcatty.starter.greet'); + result.textContent = response.greeting; + } catch { + result.textContent = chinese ? '未能读取问候语,请检查权限授权。' : 'Could not read the greeting. Check permission approval.'; + } finally { + button.disabled = false; + } +}); diff --git a/examples/plugins/starter-netcatty/src/index.ts b/examples/plugins/starter-netcatty/src/index.ts index 7dafe44b3..1e3fb04e8 100644 --- a/examples/plugins/starter-netcatty/src/index.ts +++ b/examples/plugins/starter-netcatty/src/index.ts @@ -6,7 +6,7 @@ export default definePlugin({ "com.netcatty.starter.greet", async () => { const greeting = await context.settings.get("com.netcatty.starter.greeting") ?? "Hello, synthetic world!"; - context.logger.info("Starter greeting", { greeting }); + context.views.postMessage("com.netcatty.starter.panel", { greeting }); return { greeting }; }, )); diff --git a/package-lock.json b/package-lock.json index de5e44710..6aab42a1a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -141,7 +141,7 @@ }, "examples/plugins/starter-netcatty": { "name": "@netcatty/example-starter-plugin", - "version": "0.1.0", + "version": "0.1.1", "dependencies": { "@netcatty/plugin-sdk": "0.1.0-internal" } diff --git a/package.json b/package.json index 081eface8..df67ac210 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "pretest": "npm run build:plugin-runtime-deps && npm run check:codebuddy-sdk-contract", "test": "node --test --import tsx electron/*.test.cjs electron/preload/*.test.cjs electron/mcp/*.test.cjs electron/shared/*Privacy*.test.cjs electron/scripts/*.test.cjs electron/terminalWorker/*.test.cjs electron/capabilities/*.test.cjs electron/capabilities/*/*.test.cjs electron/capabilities/*/*/*.test.cjs electron/cli/*.test.cjs electron/hostBrowser/*Store.test.cjs electron/hostBrowser/hostBrowserManager.test.cjs electron/bridges/*.test.cjs electron/bridges/*/*.test.cjs electron/bridges/aiBridge/codexAppServer/*.test.cjs electron/bridges/aiBridge/sdk/*.test.cjs scripts/*.test.cjs application/*.test.ts application/app/*.test.ts application/i18n/locales/*.test.ts application/state/*.test.ts application/state/*/*.test.ts components/*.test.ts components/*.test.tsx components/editor/*.test.ts components/editor/*.test.tsx components/notes/*.test.ts components/notes/*.test.tsx components/port-forwarding/*.test.ts components/scripts/*.test.tsx components/systemManager/*.test.ts components/terminalLayer/*.test.ts components/settings/*.test.tsx components/settings/tabs/*.test.ts components/settings/tabs/ai/*.test.ts components/settings/tabs/ai/*.test.tsx components/ai/*.test.ts components/ai/*.test.tsx components/ai/toolArtifacts/*.test.ts components/ai/toolArtifacts/*.test.tsx components/ai-elements/*.test.tsx components/sftp/*.test.ts components/terminal/*.test.ts components/terminal/*.test.tsx components/terminal/runtime/*.test.ts components/vault/*.test.ts components/vault/*.test.tsx domain/*.test.ts domain/*/*.test.ts domain/*/*.test.cjs infrastructure/ai/*.test.ts infrastructure/ai/*/*.test.ts infrastructure/ai/sdk/*.test.ts infrastructure/config/*.test.ts infrastructure/monaco/*.test.ts infrastructure/persistence/*.test.ts infrastructure/services/*.test.ts infrastructure/services/*/*.test.ts lib/*.test.ts packages/*/src/*.test.ts", "test:plugin-contract": "node --test --import tsx packages/*/src/*.test.ts", - "pretest:plugin-runtime": "npm run build:plugin-runtime-deps", + "pretest:plugin-runtime": "npm run build:plugin-runtime-deps && npm run build --workspace @netcatty/example-starter-plugin", "test:plugin-runtime": "node --test electron/plugins/*.test.cjs domain/pluginRuntimePolicy.test.cjs domain/pluginPermissionDescriptions.test.cjs", "pretest:plugin-runtime:electron": "npm run build:plugin-runtime-deps", "test:plugin-runtime:electron": "electron electron/plugins/electronRuntime.live.test.cjs", diff --git a/scripts/container-runtime-vite.test.cjs b/scripts/container-runtime-vite.test.cjs index fdf78a7e5..4d65b141d 100644 --- a/scripts/container-runtime-vite.test.cjs +++ b/scripts/container-runtime-vite.test.cjs @@ -38,6 +38,17 @@ async function checkDevImport() { } return modules.get(url); } + const managerResponse = await fetch(new URL("/components/settings/tabs/PluginPackageManager.tsx", origin)); + assert.equal(managerResponse.status, 200); + const managerSource = await managerResponse.text(); + for (const [helper, exported] of [["pluginRuntimePolicy", "describePluginRuntime"], ["pluginPermissionDescriptions", "describePluginPermission"]]) { + const match = managerSource.match(new RegExp('from "([^"\\n]*' + helper + '[^"\\n]*)"')); + assert.ok(match, `Missing prebundled ${helper} import`); + const module = await load(new URL(match[1], origin).href); + await module.link((specifier, importer) => load(new URL(specifier, importer.identifier).href)); + await module.evaluate(); + assert.equal(typeof module.namespace.default[exported], "function"); + } const entry = await load(new URL("/domain/systemManager/containerRuntime.ts", origin).href); await entry.link((specifier, importer) => load(new URL(specifier, importer.identifier).href)); await entry.evaluate(); @@ -53,7 +64,7 @@ async function checkDevImport() { } } -test("Vite serves the renderer container runtime with browser-compatible exports", { timeout: 60_000 }, () => { +test("Vite serves the renderer shared helpers with browser-compatible exports", { timeout: 60_000 }, () => { const result = spawnSync(process.execPath, [ "--experimental-vm-modules", "--input-type=module", "-e", `(${checkDevImport.toString()})().catch(error => { console.error(error); process.exitCode = 1; });`, diff --git a/vite.config.ts b/vite.config.ts index 22e38d29d..62ff95ef9 100755 --- a/vite.config.ts +++ b/vite.config.ts @@ -85,7 +85,7 @@ export default defineConfig(() => { build: { // The shared runtime is also required by Electron; convert it for browser builds. commonjsOptions: { - include: [/node_modules/, /domain\/systemManager\/containerRuntime\.shared\.cjs$/], + include: [/node_modules/, /domain\/systemManager\/containerRuntime\.shared\.cjs$/, /domain\/plugin(?:RuntimePolicy|PermissionDescriptions)\.cjs$/], }, chunkSizeWarningLimit: 3000, target: 'esnext', // Required for top-level await in WASM modules @@ -130,6 +130,8 @@ export default defineConfig(() => { include: [ // Keep the renderer alias pre-bundled so dev serves ESM instead of raw CommonJS. '@/domain/systemManager/containerRuntime.shared.cjs', + '@/domain/pluginRuntimePolicy.cjs', + '@/domain/pluginPermissionDescriptions.cjs', 'ai', '@ai-sdk/openai', '@ai-sdk/anthropic',