Skip to main content

Sokel Plugin SDK — Python

用 Python 写 Sokel 插件。契约写在 sokel.yaml 里(语言中立), sokel-gen 把它生成成 pydantic 模型与类型化的注册口;SDK 负责注册、传输、凭证、文件、心跳与重连。

async def issues_list(ctx: Ctx, in_: IssuesListIn) -> IssuesListOut:
    issues = await client.list_issues(in_.project, in_.state)
    return IssuesListOut(issues=issues, count=len(issues))

on_issues_list(p, issues_list)

in_.project 拼错是 IDE 里的红线,不是线上的一次失败调用——代码里没有任何 dict["key"]

pip install sokel-plugin-sdk
go install github.com/sokel-dev/sokel-plugin-sdk/cmd/sokel-gen@latest   # 生成器

sokel-gen 是个单文件二进制(Go 写的),只在生成时用到;跑插件时不需要它。

四步

sokel-gen init -lang python ./my-plugin
cd my-plugin
pip install -r requirements.txt
sokel-gen generate .     # sokel.yaml → sokel_gen.py
python main.py
  1. 声明 —— sokel.yaml:操作、事件、凭证、认证方式。格式见 docs/manifest.md
  2. 生成 —— sokel-gen generate . 产出 sokel_gen.py:每个操作一对 XxxIn / XxxOut 模型 和一个 on_xxx(p, fn);每个事件一个 payload 模型和一个 trigger_xxx(ctx, event_id, payload)
  3. 实现 —— handler 签名完全具体,可以是 async def 也可以是普通函数。
  4. 连接 —— asyncio.run(p.run())。插件出站连平台:无入站端口、无公网 IP、无防火墙洞。

能力一览

要做的事 怎么写
读凭证 credential(ctx) → 生成的 Credential 模型
取入参文件的字节 await ctx.fetch(in_.file)
产出文件 await ctx.upload(name, mime, data) → 放进出参
流式产出 out.text(...) 逐帧给人看,out.vars(Out(...)) 给下游
推事件 await trigger_message(ctx, event_id, MessageEvent(...))
常驻事件源 p.register_source(id, label, fn),循环里判 ctx.stopping.is_set()
平台代收 webhook p.register_webhook(fn),返回 ok() / text(401, "...")
协作式认证 p.register_auth(start=…, poll=…, submit=…)
会话型凭证刷新 await ctx.update_credential({"session": "…"})
自报运行态 ctx.report_status("auth_required", "…")

配置

SDK 读 SOKEL_ 前缀的环境变量:

变量 必填 含义
SOKEL_ENDPOINT nats://broker:4222,或 https:// 平台地址(经 /connect-info 发现 broker)
SOKEL_TOKEN 接入组 token(skp_…),平台据此认「插件 + 工作空间」
SOKEL_NATS_TOKEN broker 的传输层鉴权
SOKEL_NATS_CA tls:// broker 的自定义 CA
SOKEL_INSTANCE_ID 固定副本身份(默认按 token 指纹落盘复用)
SOKEL_REGION 副本的地域标注

凭证从不由插件存储:平台随每次调用把解析后的字段下发下来。

例子

examples/kitchen-sink 覆盖了全部形态——每种字段、文件、流式、 事件、webhook、协作式认证各一份,Python 与 Node 实现的是同一份声明。

cd examples/kitchen-sink/python
pip install -r requirements.txt
SOKEL_ENDPOINT=nats://localhost:4222 SOKEL_TOKEN=skp_xxx python main.py

开发本 SDK

uv venv && uv pip install -e '.[dev]'
python -m pytest -q

License

Apache-2.0.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sokel_plugin_sdk-0.3.0.tar.gz (31.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sokel_plugin_sdk-0.3.0-py3-none-any.whl (30.5 kB view details)

Uploaded Python 3

File details

Details for the file sokel_plugin_sdk-0.3.0.tar.gz.

File metadata

  • Download URL: sokel_plugin_sdk-0.3.0.tar.gz
  • Upload date:
  • Size: 31.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for sokel_plugin_sdk-0.3.0.tar.gz
Algorithm Hash digest
SHA256 dacc57eaf80b539cc36c3d3cf753441a6e49f8fdbf35fdbf6f77fa380a8c05b5
MD5 3baa3c6e92d1f71034c463d3bb62e90d
BLAKE2b-256 3c61db2a847235ef3f3a392c17c611913e1cdb67e40b2ad4f6f6e7a0023223b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for sokel_plugin_sdk-0.3.0.tar.gz:

Publisher: release.yml on sokel-dev/sokel-plugin-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sokel_plugin_sdk-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sokel_plugin_sdk-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 442340a7c8740cfe403e5744372f592a86374322004a389a3b7c86c5e08ab563
MD5 6427329b60f80ef027f3a7677dccc996
BLAKE2b-256 a0c5fe6d84a55d8df0bd03851fd34532999b51035c0ffe0c90f61231dbfb130b

See more details on using hashes here.

Provenance

The following attestation bundles were made for sokel_plugin_sdk-0.3.0-py3-none-any.whl:

Publisher: release.yml on sokel-dev/sokel-plugin-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.4.0

2 files

This release

0.3.0 This release

2 files

0.2.1

2 files

0.2.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page