跳转到内容

开发第一个 Clip

Clip 是用 TypeScript 编写的。用 @pinixai/core SDK 定义命令,pinix daemon 负责运行。

  1. 初始化项目

    Terminal window
    mkdir my-clip && cd my-clip
    bun init -y
    bun add @pinixai/core
  2. 创建 clip.json

    {
    "name": "@yourscope/my-clip",
    "version": "0.1.0",
    "description": "我的第一个 Clip",
    "main": "src/index.ts"
    }
  3. 编写 Clip

    创建 src/index.ts

    import { Clip } from "@pinixai/core";
    const clip = new Clip({
    name: "@yourscope/my-clip",
    description: "我的第一个 Clip",
    });
    clip.command("hello", {
    description: "Say hello",
    input: {
    name: { type: "string", description: "Name to greet", required: true },
    },
    handler: async ({ input }) => {
    return { message: `Hello, ${input.name}!` };
    },
    });
    clip.run();
  4. 本地安装并测试

    Terminal window
    pinix hub add local/my-clip --path .
    pinix invoke my-clip hello --name "World"
    # → { "message": "Hello, World!" }
  5. 迭代开发

    修改 src/index.ts 后,daemon 会自动重启 Clip 进程。直接再次调用即可测试。

my-clip/
├── clip.json # 包描述
├── src/
│ └── index.ts # 入口文件
├── package.json # 依赖
└── web/ # 可选:Web UI
└── index.html
clip.command("list", {
description: "List all items",
handler: async () => {
const items = await db.getAll();
return { items, total: items.length };
},
});
clip.command("add", {
description: "Add a new item",
input: {
title: { type: "string", required: true },
priority: { type: "string", enum: ["low", "medium", "high"] },
},
handler: async ({ input }) => {
const item = await db.create(input);
return item;
},
});

你的 Clip 可以通过 Hub 调用其他已安装的 Clip:

import { invoke } from "@pinixai/core";
clip.command("research", {
description: "Research a topic",
input: {
topic: { type: "string", required: true },
},
handler: async ({ input }) => {
const results = await invoke("google", "search", {
query: input.topic,
});
return { results };
},
});
Terminal window
# 查看 Clip 的完整信息(命令、参数、版本等)
pinix hub info my-clip
# 查看所有命令
pinix invoke my-clip --help
# 查看单个命令的参数
pinix invoke my-clip hello --help