# 异步运行工作流并流式获取运行事件消息
#### 操作场景
本章节指导用户通过调用API异步运行工作流，然后流式获取应用运行的事件消息并查询运行状态。
#### 前提条件
- 已开通数据智能平台（DataArts Platform）并加入工作空间并具有相应的权限，并根据开通区域确定调用API的Endpoint，详细信息请参见[地区与终端节点](https://console.huaweicloud.com/apiexplorer/#/endpoint)。
- 当您使用IAM认证方式完成认证鉴权时，需要获取用户AK/SK，然后使用AK/SK进行签名认证，具体操作请参考[认证鉴权](https://support.huaweicloud.com/api-dataartsplatform/dataartsplatform_07_0004.html)。
- 已在应用开发里开发完成工作流，并已经生产部署。
 
#### 涉及接口
异步运行及获取运行事件消息和状态涉及以下接口：
- [异步运行应用](https://support.huaweicloud.com/api-dataartsplatform/RunApplicationAsync.html)。
- [流式获取异步应用运行的事件消息](https://support.huaweicloud.com/api-dataartsplatform/ListRunEvents.html)。

- [获取应用异步运行的状态](https://support.huaweicloud.com/api-dataartsplatform/ShowRunStatus.html)。
 
#### 操作步骤
1. 异步运行应用。 
   1. 接口相关信息。 URI格式：POST /v1/{project_id}/applications/{application_id}/submit
      详情请参见[异步运行应用](https://support.huaweicloud.com/api-dataartsplatform/RunApplicationAsync.html)。
      
   
   2. 请求示例。
      ```
      POST https://{Endpoint}/v1/ddaac8e6a4334e6998b7e1e424c0ff82/applications/f9eed8ea-acb6-401a-b33a-19e6bee21659/submit
      {
          "query": "翻译“我们居住在地球村”",
          "response_mode": "streaming"
      }
      ```
      
   
   3. 响应示例。
      ```
      {
          "session_id": "b90e7bcd-5f5c-430a-b36b-1c9465f8c700",
          "run_id": "bdd745f4-03b6-42f4-873d-67fe48a75227",
          "application_id": "f9eed8ea-acb6-401a-b33a-19e6bee21659",
          "message_id": "bdd745f4-03b6-42f4-873d-67fe48a75227",
          "version": "1.0"
      }
      ```
      
   
   
   
   
2. 流式获取应用异步运行的事件消息。 
   1. 接口相关信息。 URI格式：GET /v1/{project_id}/applications/{application_id}/runs/{run_id}/events
      详情请参见[流式获取应用异步运行的事件消息](https://support.huaweicloud.com/api-dataartsplatform/ListRunEvents.html)。
      
   
   2. 请求示例。
      ```
      GET https://{Endpoint}/v1/096aaaa59ce841f8b4cc2c1305c1d084/applications/f9eed8ea-acb6-401a-b33a-19e6bee21659/runs/bdd745f4-03b6-42f4-873d-67fe48a75227/events
      ```
      
   
   3. 响应示例。
      ```
      data: {"message_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "run_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "is_finished": false, "role": "user", "event": "message", "session_id": "b90e7bcd-5f5c-430a-b36b-1c9465f8c700", "type": "text", "is_answer": false, "content": "翻译“我们居住在地球村”"}
      data: {"message_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "run_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "is_finished": false, "role": "assistant", "version": "0.0", "event": "workflow_started", "session_id": "b90e7bcd-5f5c-430a-b36b-1c9465f8c700", "data": {"inputs": {"sys.query": "翻译“我们居住在地球村”"}, "create_date": "2026-10-08T10:46:25Z"}}
      data: {"message_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "run_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "is_finished": false, "role": "assistant", "node_run_id": "126852eb-4419-47d5-bc19-67680eb28e12", "event": "node_started", "session_id": "b90e7bcd-5f5c-430a-b36b-1c9465f8c700", "data": {"node_id": "cc51ed85-9d8a-4bba-8c9a-3eb8119bbfea", "node_type": "START", "node_name": "开始", "seq_id": 1, "inputs": {"sys.query": "翻译“我们居住在地球村”", "sys.files": null}, "create_date": "2026-10-08T10:46:25Z"}}
      data: {"message_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "run_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "is_finished": false, "role": "assistant", "node_run_id": "126852eb-4419-47d5-bc19-67680eb28e12", "event": "node_finished", "session_id": "b90e7bcd-5f5c-430a-b36b-1c9465f8c700", "data": {"node_id": "cc51ed85-9d8a-4bba-8c9a-3eb8119bbfea", "node_type": "START", "node_name": "开始", "seq_id": 1, "outputs": {"sys.query": "翻译“我们居住在地球村”", "sys.files": null}, "status": "succeeded", "elapsed_time": 0.002, "create_date": "2026-10-08T10:46:25Z", "finish_date": "2026-10-08T10:46:25Z"}}
      data: {"message_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "run_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "is_finished": false, "role": "assistant", "node_run_id": "d20a3a6e-335b-46fc-8016-81b121fd6875", "event": "node_started", "session_id": "b90e7bcd-5f5c-430a-b36b-1c9465f8c700", "data": {"node_id": "c8f3bda1-26cf-483f-98bb-ac66bf9afe7c", "node_type": "AGENT", "node_name": "智能体", "seq_id": 2, "inputs": {"input": "翻译“我们居住在地球村”", "files": null}, "create_date": "2026-10-08T10:46:26Z"}}
      data: {"message_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "run_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "is_finished": false, "role": "assistant", "node_run_id": "40cc22e4-186c-4e2a-8428-c7bcddd1e4a5", "node_parent_run_id": "d20a3a6e-335b-46fc-8016-81b121fd6875", "event": "llm_started", "session_id": "b90e7bcd-5f5c-430a-b36b-1c9465f8c700", "data": {"node_id": "8a818250a04c370f01a04c683dcf000f", "node_type": "LLM", "node_name": "deepseek-v4-flash", "seq_id": 3, "inputs": {"messages": [{"content": "你是一个中英翻译助手", "role": "system"}, {"content": "翻译“我们居住在地球村”", "role": "user"}], "model": "deepseek-v4-flash", "temperature": 0.5, "tools": null}, "create_date": "2026-10-08T10:46:26Z", "metadata": {"inter_msg": true}}}
      data: ping
      data: ping
      data: {"message_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "run_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "is_finished": false, "role": "assistant", "node_run_id": "40cc22e4-186c-4e2a-8428-c7bcddd1e4a5", "node_parent_run_id": "d20a3a6e-335b-46fc-8016-81b121fd6875", "event": "llm_finished", "session_id": "b90e7bcd-5f5c-430a-b36b-1c9465f8c700", "data": {"node_id": "8a818250a04c370f01a04c683dcf000f", "node_type": "LLM", "node_name": "deepseek-v4-flash", "seq_id": 3, "outputs": {"content": "We live in the global village.", "usage": {"input_tokens": 57, "output_tokens": 106, "total_tokens": 163, "reasoning_tokens": 97}}, "status": "succeeded", "elapsed_time": 2.361, "create_date": "2026-10-08T10:46:26Z", "finish_date": "2026-10-08T10:46:28Z", "metadata": {"inter_msg": true}}}
      data: {"message_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "run_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "is_finished": false, "role": "assistant", "node_run_id": "d20a3a6e-335b-46fc-8016-81b121fd6875", "event": "node_finished", "session_id": "b90e7bcd-5f5c-430a-b36b-1c9465f8c700", "data": {"node_id": "c8f3bda1-26cf-483f-98bb-ac66bf9afe7c", "node_type": "AGENT", "node_name": "智能体", "seq_id": 2, "outputs": {"output": "We live in the global village."}, "status": "succeeded", "elapsed_time": 2.43, "create_date": "2026-10-08T10:46:25Z", "finish_date": "2026-10-08T10:46:28Z", "metadata": {"usage": {"input_tokens": 57, "output_tokens": 106, "total_tokens": 163, "cached_tokens": 0, "reasoning_tokens": 97}}}}
      data: {"message_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "run_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "is_finished": false, "role": "assistant", "node_run_id": "3564f438-a783-4ca5-94a6-5b9d81105e72", "event": "node_started", "session_id": "b90e7bcd-5f5c-430a-b36b-1c9465f8c700", "data": {"node_id": "93f1898e-fef7-44a3-878d-6ab3e58ca37e", "node_type": "END", "node_name": "结束", "seq_id": 4, "inputs": {"output": "We live in the global village."}, "create_date": "2026-10-08T10:46:29Z"}}
      data: {"message_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "run_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "is_finished": true, "role": "assistant", "event": "message", "session_id": "b90e7bcd-5f5c-430a-b36b-1c9465f8c700", "type": "text", "is_answer": true, "content": "{\"output\": \"We live in the global village.\"}"}
      data: {"message_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "run_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "is_finished": true, "role": "assistant", "node_run_id": "3564f438-a783-4ca5-94a6-5b9d81105e72", "event": "node_finished", "session_id": "b90e7bcd-5f5c-430a-b36b-1c9465f8c700", "data": {"node_id": "93f1898e-fef7-44a3-878d-6ab3e58ca37e", "node_type": "END", "node_name": "结束", "seq_id": 4, "outputs": {"output": "We live in the global village."}, "status": "succeeded", "elapsed_time": 0.002, "create_date": "2026-10-08T10:46:25Z", "finish_date": "2026-10-08T10:46:29Z"}}
      data: {"message_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "run_id": "bdd745f4-03b6-42f4-873d-67fe48a75227", "is_finished": true, "role": "assistant", "version": "0.0", "event": "workflow_finished", "session_id": "b90e7bcd-5f5c-430a-b36b-1c9465f8c700", "data": {"status": "succeeded", "outputs": {"output": "We live in the global village."}, "elapsed_time": 2.721, "total_steps": 4, "create_user": "z00563731", "create_date": "2026-10-08T10:46:25Z", "finish_date": "2026-10-08T10:46:29Z", "metadata": {"usage": {"input_tokens": 57, "output_tokens": 106, "total_tokens": 163, "cached_tokens": 0, "reasoning_tokens": 97}}}}
      ```
      请求异常时，请参见[错误码](https://support.huaweicloud.com/api-idme/ErrorCode.html)。
      
   
   
   
   
3. 获取应用运行状态。 
   1. 接口相关信息。 URI格式：GET /v1/{project_id}/applications/{application_id}/runs/{run_id}/status
      详情请参见[获取异步应用运行的状态](https://support.huaweicloud.com/api-dataartsplatform/ShowRunStatus.html)。
      
   
   2. 请求示例。
      ```
      GET https://{Endpoint}/v1/096aaaa59ce841f8b4cc2c1305c1d084/applications/f9eed8ea-acb6-401a-b33a-19e6bee21659/runs/bdd745f4-03b6-42f4-873d-67fe48a75227/status
      ```
      
   
   3. 响应示例。
      ```
      {
          "workspace_id": "ff8080817d30ff7b017d3213ba600001",
          "session_id": "b90e7bcd-5f5c-430a-b36b-1c9465f8c700",
          "run_id": "bdd745f4-03b6-42f4-873d-67fe48a75227",
          "application_id": "f9eed8ea-acb6-401a-b33a-19e6bee21659",
          "message_id": "bdd745f4-03b6-42f4-873d-67fe48a75227",
          "version": "1.0",
          "status": "succeeded",
          "inputs": {
              "sys.query": "翻译“我们居住在地球村”"
          },
          "outputs": {
              "output": "We live in a global village"
          },
          "create_date": "2026-10-08T10:46:25Z",
          "finish_date": "2026-10-08T10:46:28Z",
          "user": "test_user1",
          "debug": false
      }
      ```
      请求异常时，请参见[错误码](https://support.huaweicloud.com/api-idme/ErrorCode.html)。
      
   
   
   
   
 
