← Catalog

@muzi3/code-button

pi 终端 UI 代码块可点击按钮基座:OSC 8 超链接 + 本地 HTTP 回调 + 按钮注册 API

Extension

Install

Install with PiG

pig install npm:@muzi3/code-button

Install with Pi

pi install npm:@muzi3/code-button
Review before installing

Packages run code with your permissions and influence agent behavior. Catalog inclusion is not a security audit. Review the source before installing a third-party package.

Pi manifest JSON

The pi field of package.json.

{
  "extensions": [
    "./extensions"
  ]
}

README

Written by the package's publisher, shown as published in version 0.2.2 (README.md). PiG does not review it.

@muzi3/code-button

pi 终端 UI 代码块可点击按钮的基座。

原理:终端里没有真正的按钮,这是唯一能做到「点一下就触发」的链路 —— 在代码块上方插入 OSC 8 超链接,点击后由系统 URL handler 转发到本扩展启动的 127.0.0.1 本地 HTTP 服务,再按 id 触发已注册的回调。

它做什么

  • 启动本地 HTTP 服务(127.0.0.1 随机端口 + 随机 token)
  • 提供 registerCodeBlockButtons(provider) —— 其他扩展注册按钮来源
  • 提供 scanCodeBlocks(markdown) —— 统一的围栏代码块扫描器
  • 提供 toast(text, color) —— 编辑器下方一次性提示
  • /code-button-install 命令 —— 安装/修复点击链路(首次使用需要):macOS 生成 ~/Applications/PiCopy.app,Linux 写 picopy.desktop + 包装脚本并用 xdg-mime 注册

它不做什么

本身不提供任何按钮。按钮由消费方(如 @muzi3/copy-code、@muzi3/run-cmd)通过 registerCodeBlockButtons 注册。

安装

pi install npm:@muzi3/code-button

首次使用运行 /code-button-install 安装点击链路。

  • macOS:生成 ~/Applications/PiCopy.app 并注册 picopy scheme(点击走 LaunchServices)
  • Linux:写 ~/.local/bin/picopy-open(把 picopy:// 转成 http:// 回环 curl)+ ~/.local/share/applications/picopy.desktop,再 xdg-mime default picopy.desktop x-scheme-handler/picopy。点击路径是 pi 的 openBrowser → xdg-open → mimeapps → picopy-open,所以需要系统里有 xdg-utils(没有时命令会直接报错;Debian/Ubuntu:sudo apt install -y xdg-utils)

点击链路只在本机闭环:pi 跑在远端、终端在本机(ssh)时 127.0.0.1 指向客户端,点不中,请用键盘入口(如 /copy-code)。

给其他扩展复用

import { registerCodeBlockButtons, scanCodeBlocks, toast } from "@muzi3/code-button";

registerCodeBlockButtons((block) => [{
  key: `my-action:${block.index}`,
  label: `[我的按钮 #${block.index}]`,
  run: () => { /* ... */ },
}]);

运行时状态挂在 globalThis 上(__piUiCodeButton),即使被多个包分别 bundle、多次加载,按钮注册表仍是同一份。

License

MIT