第六章:呼叫協定

6.1 概述

呼叫協定(Invocation Protocol)定義了技能消費者如何遠端呼叫已發現的技能。協定採用非同步呼叫模型,支援狀態查詢和結果取得。

6.2 呼叫流程

三步呼叫模型

步驟HTTP 方法端點說明
1. 發起呼叫POST{invocation_endpoint}提交呼叫請求,回傳執行 ID
2. 查詢狀態GET{status_url}/{execution_id}查詢執行狀態
3. 取得結果GET{result_url}/{execution_id}取得執行結果

呼叫時序

消費者                              提供者
  │                                  │
  │── POST /invoke ──────────────────→│
  │   {caller, skill_id, inputs}     │
  │                                  │
  │←── 202 Accepted ─────────────────│
  │   {execution_id, status: "accepted"}
  │                                  │
  │── GET /status/{execution_id} ───→│
  │                                  │
  │←── {status: "running"} ──────────│
  │                                  │
  │── GET /status/{execution_id} ───→│
  │                                  │
  │←── {status: "completed"} ────────│
  │                                  │
  │── GET /result/{execution_id} ───→│
  │                                  │
  │←── {output: {...}} ──────────────│
  │                                  │

6.3 呼叫請求格式

{
  "caller": {
    "id": "ifay-instance-001",
    "type": "ifay",
    "credentials": {
      "api_key": "sk-xxxxx"
    }
  },
  "skill_id": "com.example.translate-v1",
  "inputs": {
    "text": "Hello, world!",
    "target_language": "zh-TW"
  },
  "context": {
    "trace_id": "trace-abc-123",
    "priority": "normal",
    "timeout_ms": 30000
  }
}

請求欄位

欄位型別必要說明
callerobject呼叫者身分資訊
caller.idstring呼叫者唯一識別碼
caller.typestring呼叫者類型(ifay/service/user)
caller.credentialsobject認證憑證
skill_idstring目標技能 ID
inputsobject輸入參數(鍵值對)
contextobject呼叫上下文
context.trace_idstring追蹤 ID
context.prioritystring優先順序(low/normal/high)
context.timeout_msnumber逾時時間(毫秒)

6.4 呼叫回應格式

{
  "execution_id": "exec-789-xyz",
  "status": "completed",
  "skill_id": "com.example.translate-v1",
  "output": {
    "translated_text": "你好,世界!",
    "source_language": "en",
    "target_language": "zh-TW",
    "confidence": 0.98
  },
  "timestamps": {
    "created_at": "2025-03-20T14:30:00Z",
    "updated_at": "2025-03-20T14:30:02Z",
    "completed_at": "2025-03-20T14:30:02Z"
  }
}

回應欄位

欄位型別必要說明
execution_idstring執行唯一識別碼
statusExecutionStatus執行狀態
skill_idstring目標技能 ID
outputany輸出資料(completed 時)
errorobject錯誤資訊(failed/timeout 時)
timestampsobject時間戳記資訊

執行狀態列舉

狀態說明
accepted請求已接收,等待執行
running正在執行中
completed執行成功完成
failed執行失敗
timeout執行逾時

6.5 認證流程

呼叫前,消費者需檢查技能描述符中的 auth 欄位:

消費者                              提供者
  │                                  │
  │── 讀取 descriptor.auth ──→       │
  │                                  │
  │   auth.type == "api_key"?        │
  │   → 在 Header 中附加 API Key     │
  │                                  │
  │   auth.type == "oauth2"?         │
  │   → 執行 OAuth 2.0 流程取得 Token │
  │                                  │
  │   auth.type == "none"?           │
  │   → 直接呼叫,無需憑證            │
  │                                  │
  │── POST /invoke (附帶憑證) ───────→│
  │                                  │

認證失敗回應

未提供有效憑證時,回傳 401 錯誤:

{
  "error": {
    "code": "AUTH_REQUIRED",
    "message": "Authentication is required to invoke this skill",
    "details": {
      "required_auth_type": "oauth2",
      "authorization_url": "https://example.com/oauth/authorize"
    }
  }
}

6.6 逾時與重試

逾時處理

當技能執行超過設定的 timeout_ms 時:

{
  "execution_id": "exec-789-xyz",
  "status": "timeout",
  "skill_id": "com.example.translate-v1",
  "error": {
    "code": "EXECUTION_TIMEOUT",
    "message": "Skill execution exceeded the configured timeout of 30000ms",
    "retry": {
      "suggested_delay_ms": 5000,
      "max_attempts": 3
    }
  },
  "timestamps": {
    "created_at": "2025-03-20T14:30:00Z",
    "updated_at": "2025-03-20T14:30:30Z"
  }
}

重試策略

  • 逾時回應中包含建議的重試延遲和最大重試次數
  • 消費者應實作指數退避重試策略
  • 端點不可達時,建議使用指數退避(初始延遲 × 2^n)