跳到主要内容 / Skip to content

文档SDK 运行时、单测与热重载

SDK 运行时、单测与热重载

零依赖 runtime.mjs、Mock 单元测试、dev 热重载、多文件打包与 OneUpdate Deno 分发。

全部插件 API 参考

配合 API 0.1.41,@ishell/plugin-sdk 提供零依赖 JavaScript 运行时助手、本地 Mock 测试框架、dev 热重载开发流,以及通过 OneUpdate 自动按需分发的固定版本 Deno 运行时。

零依赖 SDK 运行时(runtime.mjs)

开发者可直接引入 @ishell/plugin-sdk(或从官网下载 runtime.mjs / ishell.d.ts),使用 definePlugin、defineCommand、defineAiTool、assertNotAborted、ok、fail 等助手减少样板代码:

import { definePlugin, defineCommand, assertNotAborted, ok } from "./runtime.mjs";

export default definePlugin({
  commands: {
    inspect: defineCommand(async (args, ctx) => {
      assertNotAborted(ctx.signal);
      const res = await ctx.ssh.exec({ command: "uname -a" });
      return ok("已读取主机内核信息", { uname: res.stdout.trim() });
    }),
  },
});

无需启动客户端的 Mock 单元测试(testing.mjs)

testing.mjs 提供 createMockContext、invokeCommand、invokeAiTool 等测试工具,支持直接使用 deno test 离线验证成功路径、参数校验、异常分支与 AbortSignal 取消逻辑:

import { createMockContext, invokeCommand } from "./testing.mjs";
import plugin from "./main.mjs";

Deno.test("inspect returns kernel summary", async () => {
  const ctx = createMockContext({
    ssh: { "uname -a": { exitCode: 0, stdout: "Linux prod-01 6.8.0\n", stderr: "" } },
  });
  const result = await invokeCommand(plugin, "inspect", {}, ctx);
  if (!result.summary.includes("内核")) throw new Error("unexpected summary");
});

热重载开发与多文件打包 CLI

# 监听插件目录变动,自动执行静态清单校验并通知客户端热重载
ishellpro plugin dev ./my-plugin

# 静态检查清单、权限、API 范围与 WebView 静态资源指纹
ishellpro plugin check ./my-plugin

# 打包为可上传至开发者中心的 JSON 发行包(自动包含 web/ 等静态资源,上限 12 MiB)
ishellpro plugin pack ./my-plugin > my-plugin.ishell.json

Deno 运行时按需分发(OneUpdate)

为避免增大 iShell Pro 桌面主安装包体积,客户端不再强制将 Deno 捆绑进在线安装包,而是在用户首次批准运行插件时按需准备固定版本(当前为 Deno 2.9.8):

  1. 优先复用本地已配置或离线预置的运行时(ISHELL_DENO_OFFLINE_SOURCE 或随包 runtime/deno)。
  2. 未命中时,自动从官方 OneUpdate 公共目录(ishell-plugin-runtime / stable)按当前操作系统与架构(Windows/macOS/Linux × x64/arm64)拉取固定版本归档,逐字节校验 SHA-256 后解压并缓存至本地数据目录。
  3. 企业内网环境可通过 ISHELL_DENO_DOWNLOAD_BASE 指向内部 HTTPS 镜像,或通过 ISHELL_DENO_OFFLINE_SOURCE 指向离线归档文件。

官方标杆示例插件

可运行示例包(plugin-examples.zip)新增两套面向 API 0.1.41 的综合标杆插件:

  • examples/plugins/k8s-ops-copilot:综合演示动态 SSH、风险分级远程 AI/MCP 工具、结构化文档库与 testing.mjs 单元测试。
  • examples/plugins/cluster-topology-webview:综合演示隔离 ui.webview(web/index.html + window.ishell)、多主机编排与 ishellpro plugin pack 多文件资源打包。