Skip to content

SDK API 概览

onin-sdk 是 Onin 插件的官方开发工具包,提供与 Onin 主程序交互的所有能力。

安装

bash
npm install onin-sdk
# 或
pnpm add onin-sdk

快速导入

typescript
import {
  command,
  storage,
  http,
  fs,
  clipboard,
  dialog,
  notification,
  scheduler,
  lifecycle,
  settings,
  pluginWindow,
  ai,
} from 'onin-sdk';

API 模块总览

模块说明需要权限
command注册指令处理器、动态注册指令
storage持久化键值存储storage
httpHTTP 网络请求http
fs文件系统读写
clipboard读写剪贴板
dialog系统对话框(文件选择、确认框)
notification系统通知notification
scheduler基于 cron 的定时任务scheduler
lifecycle插件加载/卸载/后台生命周期回调
settings插件设置页面配置
pluginWindow窗口事件监听(show/hide/focus)
ai调用用户配置的 AI 能力

典型用法模式

一个完整的插件通常按以下顺序初始化:

typescript
import { lifecycle, settings, command, notification } from 'onin-sdk';

// 1. 在 onLoad 中完成所有初始化
lifecycle.onLoad(async () => {
  // 2. 注册设置页
  await settings.useSettingsSchema([
    { key: 'apiKey', label: 'API 密钥', type: 'password', required: true },
  ]);

  // 3. 注册指令处理器
  await command.handle(async (code, args) => {
    switch (code) {
      case 'my-command':
        return handleMyCommand(args);
      default:
        throw new Error(`Unknown command: ${code}`);
    }
  });
});

async function handleMyCommand(args: any) {
  await notification.show({ title: '执行成功', body: '指令已完成' });
  return { status: 'ok' };
}

基于 MIT 协议发布