公開 App 指令給 ctl
套件內有有效且需認證的 API 描述(synopkgland-api.json)時,App 就可被操控。原生 SDK App 由建置依 dsm-package.yaml 的 ctl.commands 寫出它,因此不會公開你沒有宣告的內容。呼叫端如何使用結果,見執行 ctl 指令;檢查與信任的內容,見 ctl 安全性。
| 需要 | 說明 |
|---|---|
| 後端 | 原生 SDK 後端:Go、Node.js、Python 或 Rust。binary 或 Compose App 若設定 ctl.enabled,建置會以 CTL_UNSUPPORTED_BACKEND 失敗;請改為自行實作契約。 |
| 版本 | 最新公開版本的 CLI 與模板及其固定 SDK 版本。 |
| Go 後端 | main.go 必須支援僅供建置使用的 --describe flag,模板已如此;否則建置會以 CTL_DESCRIBE_REQUIRED 失敗。 |
| 方法 | 每個指令都綁定後端註冊的方法。未註冊的方法會以 CTL_COMMAND_UNREGISTERED 失敗。 |
- 從原生模板開始。每個模板都在
dsm-package.yaml的ctl區塊宣告三個指令:
synopkgland init -t native-go --package MyApp my-appctl: enabled: true commands: status.show: cli: [status] summary: Show this App's status and verified caller effect: read flow: {type: invoke, method: app.status} paramsSchema: {type: object, additionalProperties: false} examples: [{}] greeting.get: cli: [greeting, get] summary: Read the greeting effect: read flow: {type: invoke, method: greeting.get} paramsSchema: {type: object, additionalProperties: false} examples: [{}] greeting.set: cli: [greeting, set] summary: Set the greeting effect: write flow: {type: invoke, method: greeting.set} paramsSchema: type: object required: [text] additionalProperties: false properties: text: {type: string, minLength: 1, maxLength: 200, description: Greeting text, examples: [Hello]} examples: [{text: Hello}]map 的鍵是指令 ID,cli 是呼叫端在 App alias 之後輸入的字詞,flow.method 是指令呼叫的後端方法。
- 新增後端方法與權限。這個 Go 處理常式註冊於
backend/internal/app/routes.go:
api.Handle("notes.add", cfg.addNote),func (cfg Config) addNote(_ context.Context, caller api.Caller, params json.RawMessage) (any, error) { var in struct { Text string `json:"text"` } if err := decodeParams(params, &in); err != nil { return nil, err } if n := utf8.RuneCountInString(in.Text); n < 1 || n > 200 { return nil, api.BadRequest("text must be 1 to 200 characters") } return cfg.saveNote(caller.User(), in.Text) // your storage code}在 backend/internal/api/permissions.json 設定誰可以呼叫。未列出的方法為 administrators:
{ "schema": 1, "default": "administrators", "methods": { "notes.add": "authenticated" } }| Runtime | 註冊 | 權限檔 |
|---|---|---|
| Go | api.Handle("notes.add", fn) | backend/internal/api/permissions.json |
| Node.js | app.method('notes.add', (ctx, params) => ...) | backend/app/permissions.json |
| Python | app.method("notes.add")(lambda ctx, params: ...) | backend/app/permissions.json |
| Rust | app.method("notes.add", move |ctx, params| ...) | backend/permissions.json |
- 在
ctl.commands底下宣告指令:
notes.add: cli: [notes, add] summary: Add a note for the signed-in user effect: write flow: {type: invoke, method: notes.add} paramsSchema: type: object required: [text] additionalProperties: false properties: text: {type: string, minLength: 1, maxLength: 200, description: Note text} examples: [{text: Buy milk}]- 建置並驗證。
check驗證宣告;build把每個指令與真實的路由註冊表及權限比對,並寫出synopkgland-api.json;inspect驗證 SPK 內的描述:
synopkgland checksynopkgland build --arch x86_64synopkgland inspect dist/MyApp-0.1.0-0001-x86_64.spk- 以呼叫端身分試用。請先安裝 SPK;
--dry-run不會送出任何東西:
synopkgland ctl list --host nas --app MyApp --jsonsynopkgland ctl myapp notes add --text "Buy milk" --host nas --dry-runsynopkgland ctl myapp notes add --text "Buy milk" --host nas --yesdsm-package.yaml 中的 ctl:
| 欄位 | 必填 | 規則 |
|---|---|---|
enabled | 是 | true 才會建置描述。沒有 ctl 時,App 不會被列出。 |
commands | enabled 時 | 指令 ID 對應指令的 map,1 到 1024 筆。 |
aliases | 否 | 最多 8 個短名稱,格式 ^[a-zA-Z][a-zA-Z0-9-]{0,63}$。預設:小寫的套件 ID。不要使用 host、list 或 help。兩個 App 使用同一個 alias 會得到 CTL_APP_AMBIGUOUS;此時呼叫端改用套件 ID。 |
developedBy | 否 | synopkgland、third-party 或 unknown(預設)。對呼叫端顯示為自行宣告。 |
每個指令:
| 欄位 | 必填 | 規則 |
|---|---|---|
| ID(map 鍵) | 是 | 以點連接的小寫字詞,例如 notes.add,最多 128 個字元。 |
cli | 是 | 1 到 8 個字詞,格式 ^[a-z][a-z0-9-]*$。在 App 內唯一;呼叫端在 alias 之後輸入。 |
summary | 是 | 指令的功能,最多 1024 個字元。 |
effect | 是 | read、write 或 destructive。絕不從名稱推斷。 |
flow | 是 | 下列五種流程之一。 |
paramsSchema | 否 | JSON Schema 2020-12 物件。沒有它時只接受 --input,參數會顯示為未描述。 |
resultSchema | 否 | 結果會依它驗證;不符時為 CTL_RESPONSE_INVALID。 |
examples | 否 | 最多 16 個參數物件,會成為 ctl list 中可執行的範例。 |
permission | 絕不設定 | 建置會推導它,並覆寫任何值。 |
| 流程 | 用途 | 欄位 | 上限 |
|---|---|---|---|
invoke | 一次方法呼叫。 | method | 請求與回應最多 1 MiB。 |
job | 啟動長時間工作、輪詢、回傳結果。 | start, poll, 選用的 result, jobId, idParam, state, done, failed, intervalMs, timeoutMs | start 只執行一次。poll 與 result 必須是讀取。intervalMs 為 10 到 60000;timeoutMs 從 intervalMs 到 3600000。 |
paginate | 收集清單。 | method, items, offsetParam, limitParam, pageSize, maxPages | 僅限讀取。pageSize 與 maxPages 為 1 到 1000。最多 100000 筆;達到頁數上限是錯誤,不是部分結果。 |
ndjson | 收集行串流。 | method | 回應為 application/x-ndjson。最多 8 MiB 與 10000 行,以 {items} 回傳。 |
upload | 傳送一個本機檔案。 | method, fileParam, parentParam, nameParam | 不是讀取。最多 16 GiB 的 regular file,串流一次。不續傳、不分段、不重試。 |
jobId、state 與 items 是回應中以點分隔的欄位路徑。job 範例:
scan.start: cli: [scan, start] summary: Run a scan and return its result effect: write flow: type: job start: scan.start poll: scan.status result: scan.result jobId: job_id idParam: job_id state: state done: [completed] failed: [failed, cancelled] intervalMs: 1000 timeoutMs: 60000 paramsSchema: type: object required: [target] additionalProperties: false properties: target: {type: string}流程不能包含指令碼、URL、運算式或步驟圖。屬於你 App 的順序邏輯,請放進單一後端方法。
權限與 effect 如何推導
Section titled “權限與 effect 如何推導”- 每個綁定方法的權限來自真實的註冊表:
permissions.json,或 SDK 能力方法的固定策略。你的宣告不能改變它。 - 指令的
permission是其方法中最強者(administrators高於authenticated高於public)。使用entry: admin-only時,每個指令都是administrators。 - 你宣告的
effect會套用到綁定的方法;job的poll與result方法是讀取。指令的 effect 絕不弱於它所綁定方法的 effect。 - 被兩個指令使用的方法採用較強的 effect。沒有指令綁定的方法無法透過 ctl 呼叫。
Parameters
Section titled “Parameters”string、integer、number與boolean屬性會成為 typed flag:text是--text,maxItems與max_items都是--max-items。陣列與物件需要--input。- 驗證前只會填入頂層屬性的預設值。巢狀物件內的
default不會套用:呼叫端必須送出該值,否則驗證失敗。無效輸入會在任何業務請求之前被拒絕。 - flag 名稱與 ctl flag 相同的屬性不能用 typed flag 傳入;呼叫端必須使用
--input。保留名稱列於執行 ctl 指令。 - 以
writeOnly: true標記機密、token、私有 URL 與上傳內容,包括巢狀屬性、陣列項目與$ref目標。resultSchema也一樣。請見 ctl 安全性。 - Schema 的
$ref只能指向#片段,使用 2020-12 方言,巢狀最多 32 層。不要把真實機密放進預設值或範例。
- 一個指令對應一個完整任務。不要公開
*.request之類的原始代理或內部步驟。 - 如實宣告 effect;刪除與任何不可逆的操作使用
destructive。 --yes是唯一內建的確認,對write與destructive一視同仁。需要計畫身分、時效或第二次核准的確認,屬於你的後端方法。- 受理不等於完成。呼叫回傳後仍會繼續的工作,請用
job流程,或回傳讀取指令可查詢的 job ID。 - 結果保持小而完整。超過 1 MiB 的回應會失敗;請分頁或改用
ndjson。 - 偏好
<resource> <verb>形式:讀取用list、get、export;寫入用create、update、import、run;破壞性用delete。同一個任務只用一個同義詞;pause這類領域動詞也可以。 - 回傳穩定、有文件的欄位。agent 會依賴它們。
| 錯誤碼 | 原因 |
|---|---|
CTL_DECLARATION_INVALID | ctl 缺少 enabled,或 enabled: true 缺少 commands。 |
CTL_COMMAND_UNREGISTERED | 流程指名了後端未註冊的方法。 |
CTL_DESCRIBE_REQUIRED | Go 的 main.go 不支援 --describe。請依模板更新。 |
CTL_DESCRIBE_FAILED | Go 路由匯出失敗或回傳無效 JSON。 |
CTL_DESCRIPTOR_INVALID | 產生的描述違反契約:重複的 cli 路徑、流程欄位錯誤、不安全的 schema。 |
CTL_UNSUPPORTED_BACKEND | binary 或 Compose 後端。 |
每個錯誤碼的意義與修正方法見錯誤碼;ctl 欄位見 manifest 參考。
App 如何提供它
Section titled “App 如何提供它”私有描述位於套件根目錄的 synopkgland-api.json,在所有靜態介面路徑之外。SDK 只對固定方法 synopkgland.describe,並在 DSM 工作階段、SynoToken 與 App entry 檢查之後,才從套件 CGI 提供它;匿名請求會得到錯誤,不會取得描述。envelope 為 {format:"synopkgland.api", version:1, payload, sha256},其中 hash 涵蓋除了 sha256 以外所有內容的 canonical JSON。它識別的是內容,不是作者簽章。