Skip to content

协议参考

SDK 包在 packages/sdk/src/protocol/ 中拥有协议契约。

版本

ts
export const PROTOCOL_VERSION = 1;

帧类型

Request:

ts
{
  id: string;
  method: string;
  params: unknown;
}

Response:

ts
{
  id: string;
  ok: true;
  result: unknown;
}
{
  id: string;
  ok: false;
  error: ProtocolError;
}

Event:

ts
{
  type: string;
  data: unknown;
}

Event 没有 id,也没有直接响应。

内置方法

方法作用
getServerInfo服务身份、可用时的 Photoshop 版本、已加载插件。
jsx:run执行内联 JSX 脚本。
jsx:execute带参数执行命名 JSX 资源。
event:subscribe让逻辑客户端订阅事件类型。
event:unsubscribe移除该事件订阅。
action:autoCutout执行自动抠图 action。
action:removeBackground执行移除背景 action。
layer:getInfo检查 layer 信息。
layer:getInfoById按 id 检查 layer。
layer:getInfoByIndex按 index 检查 layer。
layer:getInfoBySelectionIndex按 Photoshop selection index 检查 layer。
layer:getCurrentPreview获取当前选中 layer 的预览 payload。
layer:importImage校验图片源并导入 Photoshop 为 layer。
document:current获取当前文档信息。
document:export导出文档。
document:save保存文档。
image:exportLayer导出 layer 图片结果。
image:exportLayerWithSelectedPath导出带当前选择路径叠加层的 layer 图片。
image:getPreview获取 layer 预览图片结果。
image:exportDocument导出 document 图片结果。
selection:getArea获取当前矩形选区区域,或返回 null
selection:getPath获取当前选择路径的 SVG 元数据,或返回 null
selection:change注册 generator 侧选择变化 watcher。

插件专属方法是开放字符串名,通过插件 endpoint 连接上的 connection.invoke(...) 调用。

HTTP API

Endpoint作用
GET /health服务存活检查。
GET /plugins已加载插件发现。
GET /plugins/{id}/health已加载插件运行状态或加载失败诊断。
POST /action/auto-cutout执行 action:autoCutout,返回 boolean
POST /action/remove-background执行 action:removeBackground,返回 { success }
GET /document/current执行 document:current,返回 PsDocument
POST /document/export执行 document:export,body 使用 WS 参数。
POST /document/save执行 document:save,body 为 { savePath? }
GET /layer/info执行 layer:getInfo,参数来自 query string。
GET /layer/by-id/{layerID}执行 layer:getInfoByIdgetChildren 来自 query。
GET /layer/by-index/{layerIndex}执行 layer:getInfoByIndexgetChildren 来自 query。
GET /layer/current-preview执行 layer:getCurrentPreview
POST /layer/import-image执行 layer:importImage,body 为 LayerImportImageParams
POST /image/export-layer执行 image:exportLayer,body 使用 WS 参数。
POST /image/export-layer-with-selected-path执行 image:exportLayerWithSelectedPath,body 使用 WS 参数。
GET /image/preview/{layerSpec}执行 image:getPreviewdocumentId 来自 query。
POST /image/export-document执行 image:exportDocument,body 使用 WS 参数。
GET /selection/area执行 selection:getArea,返回 PsRect | null
GET /selection/path执行 selection:getPathexpand 来自 query。

模块 HTTP API 复用对应 WS 方法的结果形状。selection:change 仍然是 WS/event 能力,因为 HTTP 不能承载持续的选区变化事件流。

Server Info

ts
interface ServerInfo {
  name: string;
  version: string;
  psVersion?: string;
  plugins?: PluginInfo[];
}

interface PluginInfo {
  id: string;
}

type PluginStatus = "loaded" | "failed";

type PluginHealthCheck = "ok" | "failed" | "skipped";

interface PluginHealth {
  id: string;
  status: PluginStatus;
  clients: number;
  loadedAt?: number;
  lastError?: ProtocolError;
  checks?: Record<string, PluginHealthCheck>;
}

插件加载失败时,健康信息可能包含 loadregistration check。已加载插件的 生命周期 hook 错误会被隔离,插件仍然可用,但 runtime check 会变成 failedlastError 会按情况使用 PLUGIN_LOAD_FAILEDPLUGIN_REGISTRATION_FAILEDPLUGIN_LIFECYCLE_FAILED

主事件

ts
"#ready": {
  port: number;
  plugins: PluginInfo[];
}

"#closing": {
  reason: "host-close" | "process-exit";
}

"selection:changed": PsRect | null;

"layer:previewChange": LayerPreviewPayload;

"layer:selectionChange": PsLayer[] | null;

错误形状

ts
interface ProtocolError {
  code: string;
  message: string;
  details?: Record<string, unknown>;
  retryable?: boolean;
  source?: ErrorSource;
  requestId?: string;
  method?: string;
  pluginId?: string;
}

Helper

SDK 导出帧处理 helper:

ts
parseFrame(data);
serializeFrame(value);
isRequest(value);
isResponse(value);
isEvent(value);