InvokeEsAgent

Updated at:

Invokes the Alibaba Cloud Elasticsearch Agent service based on the JSON-RPC 2.0 protocol. It supports creating and managing agent sessions, sending messages and receiving Server-Sent Events (SSE) streaming responses, resuming connections from breakpoints, canceling running tasks, and handling human-in-the-loop (HITL) interactions initiated by the agent. This service is currently available only in the China (Shanghai) region.

Try it now

Try this API in OpenAPI Explorer, no manual signing needed. Successful calls auto-generate SDK code matching your parameters. Download it with built-in credential security for local usage.

Test

RAM authorization

The table below describes the authorization required to call this API. You can define it in a Resource Access Management (RAM) policy. The table's columns are detailed below:

  • Action: The actions can be used in the Action element of RAM permission policy statements to grant permissions to perform the operation.

  • API: The API that you can call to perform the action.

  • Access level: The predefined level of access granted for each API. Valid values: create, list, get, update, and delete.

  • Resource type: The type of the resource that supports authorization to perform the action. It indicates if the action supports resource-level permission. The specified resource must be compatible with the action. Otherwise, the policy will be ineffective.

    • For APIs with resource-level permissions, required resource types are marked with an asterisk (*). Specify the corresponding Alibaba Cloud Resource Name (ARN) in the Resource element of the policy.

    • For APIs without resource-level permissions, it is shown as All Resources. Use an asterisk (*) in the Resource element of the policy.

  • Condition key: The condition keys defined by the service. The key allows for granular control, applying to either actions alone or actions associated with specific resources. In addition to service-specific condition keys, Alibaba Cloud provides a set of common condition keys applicable across all RAM-supported services.

  • Dependent action: The dependent actions required to run the action. To complete the action, the RAM user or the RAM role must have the permissions to perform all dependent actions.

Action

Access level

Resource type

Condition key

Dependent action

elasticsearch:InvokeEsAgent

none

*All Resource

*

None None

Request syntax

POST /openapi/agent/acp HTTP/1.1

Request parameters

Parameter

Type

Required

Description

Example

body

object

No

The request body in JSON-RPC 2.0 format.

Unified request parameters (all located at the top level of the request body, not inside params):

  • jsonrpc: String, required. The JSON-RPC version, fixed to 2.0. Example: 2.0.

  • method: String, required. The method to call. For valid values, see the method list below. Example: session/prompt.

  • id: String, optional. The request ID of the caller, which is passed through in the response. Example: 1774339902987004.

  • params: Object, optional. The parameters corresponding to the method. For examples, see the supplementary instructions.

  • sessionCode: String, optional. The exact query by session ID for session/list. Example: 49b82154-ac20-4f27-a6ec-eb5f4cfc5304.

  • pageNum: Integer, optional. The page number for session/list. The default value is processed by the server. Example: 1.

  • pageSize: Integer, optional. The number of entries per page for session/list. The default value is processed by the server. Example: 10.

Valid values for method:

  • session/new: Creates a session and returns JSON.

  • session/list: Queries the session list or a specific session and returns JSON.

  • session/prompt: Sends a message and returns SSE.

  • session/load: Resumes a connection from a breakpoint. Used only when _meta.isReload=true. Returns SSE.

  • session/cancel: Cancels a running task under the session and returns JSON.

  • session/delete: Deletes a session and returns JSON.

  • session/hitlRespond: Submits a HITL user response and returns JSON.

Response modes: session/prompt and session/load return an SSE event stream with the Content-Type of text/event-stream. The format of each event is data: {JSON}. Other methods return a standard JSON response with the Content-Type of application/json.

{ "jsonrpc": "2.0", "id": 5, "method": "session/list", "params": {}, "pageNum": 1, "pageSize": 10 }

1. session/new - Create a session

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "session/new",
  "params": {}
}

Pass an empty object for params. The data.result.sessionId in the response is used for all subsequent requests.

2. session/list - Session list

{
  "jsonrpc": "2.0",
  "id": 5,
  "method": "session/list",
  "params": {},
  "pageNum": 1,
  "pageSize": 10
}
ParameterLocationRequiredDescription
pageNumTop levelNoThe page number. The default value is processed by the server.
pageSizeTop levelNoThe number of entries per page. The default value is processed by the server.

3. session/prompt - Send a message (SSE streaming)

{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "session/prompt",
  "params": {
    "sessionId": "863d643f-dadd-40ea-9399-1aef3aea11fe",
    "prompt": [
      {
        "type": "text",
        "role": "user",
        "text": "Hello, what are you good at?"
      }
    ],
    "_meta": {
      "agentMode": "AGENTSWARM",
      "enableThinking": false
    }
  }
}

Parameter description of params._meta:

ParameterTypeRequiredDescription
enableThinkingbooleanNoSpecifies whether to enable deep thinking mode.
agentModestringNoThe agent pattern. Valid values: CHAT (default) and AGENTSWARM.

Parameter description of params.prompt:

ParameterTypeRequiredDescription
typeStringYesThe content type. Valid values: text, file, and image.
textStringRequired for the text typeThe text content.
urlStringRequired for the file or image typeThe URL of the file or image.

4. session/load - Resume a session from a breakpoint

Request example:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "session/load",
  "params": {
    "sessionId": "b32ee663-b231-4676-a628-f2d9dd7c951e",
    "_meta": {
      "requestCode": "e4bcb48f-0abd-428c-980c-ab7619e36c10",
      "isReload": true,
      "beginLogOffset": 0
    }
  }
}

Parameter description of params._meta:

ParameterTypeRequiredDescription
isReloadbooleanNoSpecifies whether to use the breakpoint resumption pattern. Fixed to true.
requestCodestringNoThe conversation ID, which is the _meta.chatId of the SSE event. If this parameter is not passed, the server attempts to find the active node under the current session.
beginLogOffsetintegerNoThe log offset from which to continue reading. Default value: 0.

5. session/cancel - Cancel a node

{
  "jsonrpc": "2.0",
  "id": 9,
  "method": "session/cancel",
  "params": {
    "sessionId": "3c7c879c-bfdf-4011-b5c8-72f58466af7e"
  }
}

Parameter description of params:

ParameterTypeRequiredDescription
sessionIdStringYesThe session ID.

6. session/delete - Delete a session

{
  "jsonrpc": "2.0",
  "id": 14,
  "method": "session/delete",
  "params": {
    "sessionId": "3c7c879c-bfdf-4011-b5c8-72f58466af7e"
  }
}

Parameter description of params:

ParameterTypeRequiredDescription
sessionIdStringYesThe session ID.

7. session/hitlRespond - HITL user response

When the SSE stream of session/prompt receives sessionUpdate=hitl_question, the invoker must display the question and commit the user's response through this operation.

{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "session/hitlRespond",
  "params": {
    "sessionId": "sess_hitl_verify_1778467117",
    "requestId": "",
    "responseText": "I choose artificial intelligence and machine learning",
    "selectedOption": "Artificial intelligence and machine learning"
  }
}

Parameter description of params:

ParameterTypeRequiredDescription
sessionIdstringYesThe session ID.
requestIdstringNoThe HITL request ID. This parameter can be empty. The system automatically finds the currently active HITL request.
responseTextstringNoThe text content of the user's response.
selectedOptionstringNoThe option selected by the user. Pass the label value of the option.

You must provide at least one of responseText and selectedOption.

Response elements

Element

Type

Description

Example

object

Schema of Response

code

string

The status code. A value of 200 indicates a successful call. If the value is not 200, the message field contains the specific error description.

200

data

object

The JSON-RPC 2.0 response body. data.result contains the actual return content of the called ACP method. data.id is the id passed in the request. data.jsonrpc is fixed to 2.0. data.timestamp is the UNIX timestamp in milliseconds when the response is generated.

string

The values of the fields in the JSON-RPC response body.

{ "requestId": "598b5734-6dbe-4b04-9c17-4cdf99d58b85", "code": 200, "message": null, "data": { "requestId": "598b5734-6dbe-4b04-9c17-4cdf99d58b85", "timestamp": 1774494136385, "jsonrpc": "2.0", "id": "2", "result": { "sessions": [ { "sessionId": "7b1172ca-8ccb-4c3f-b0fb-c72dd248d3ef", "title": "New session", "description": null, "createdAt": 1774493075150, "updatedAt": 1774493094825, "_meta": { "sessionSource": "CHAT_OPEN_AGENT", "agentName": "openagent", "status": "RELEASED", "chatCount": 1 } } ], "nextCursor": null, "total": 1 } } }

message

string

The error description. The value is null when the call is successful. If the call fails, the specific error message is returned.

Agent subscription is inactive or expired

requestId

string

Id of the request

1df6d614-96db-41bd-b8b6-ab061a1724ca

Response modes

The response format depends on the called ACP method:

MethodContent-TypeResponse format
session/prompt and session/loadtext/event-stream; charset=utf-8SSE event stream
Other methodsapplication/jsonStandard JSON response

Standard JSON response examples.

session/new

{
  "code": 200,
  "data": {
    "result": {
      "models": null,
      "modes": null,
      "sessionId": "8246b4d9-88f3-4a93-80b7-0c242f0ddbb8"
    },
    "requestId": "1df6d614-96db-41bd-b8b6-ab061a1724ca",
    "id": "1",
    "jsonrpc": "2.0",
    "timestamp": 1782723585411
  },
  "requestId": "1df6d614-96db-41bd-b8b6-ab061a1724ca",
  "message": null
}

The data.result.sessionId is used for all subsequent requests.

session/list

{
  "requestId": "598b5734-6dbe-4b04-9c17-4cdf99d58b85",
  "code": 200,
  "message": null,
  "data": {
    "requestId": "598b5734-6dbe-4b04-9c17-4cdf99d58b85",
    "timestamp": 1774494136385,
    "jsonrpc": "2.0",
    "id": "1774339902987004",
    "result": {
      "sessions": [
        {
          "userId": "test_user_doc",
          "sessionId": "7b1172ca-8ccb-4c3f-b0fb-c72dd248d3ef",
          "title": "Hello",
          "description": null,
          "createdAt": 1774493075150,
          "updatedAt": 1774493094825,
          "_meta": {
            "sessionSource": "CHAT_BI",
            "agentName": "chat_cli_chatbi",
            "type": "ACP_AGENT",
            "status": "RELEASED",
            "chatCount": 1
          }
        }
      ],
      "nextCursor": null,
      "total": 1
    }
  }
}

Valid values for sessions[]._meta.status:

ValueDescription
RUNNINGThe session is in progress.
RELEASEDThe session has ended.

session/cancel

{
  "code": 200,
  "data": {
    "result": {
      "success": true,
      "cancelledCount": 1,
      "totalActive": 1,
      "message": "The cancellation request has been sent, and 1 task has been canceled."
    },
    "requestId": "9e3c0189-3673-4f3b-8864-1e47915781a2",
    "id": "9",
    "jsonrpc": "2.0",
    "timestamp": 1782720975482
  },
  "requestId": "9e3c0189-3673-4f3b-8864-1e47915781a2",
  "message": null
}
  • cancelledCount: The actual number of canceled tasks.

  • totalActive: The number of active tasks in the session.

session/delete

{
  "code": 200,
  "data": {
    "result": {
      "success": true,
      "sessionId": "3c7c879c-bfdf-4011-b5c8-72f58466af7e",
      "message": "The session is deleted."
    },
    "requestId": "ac753513-db17-492a-9c4f-7be17f95122c",
    "id": "14",
    "jsonrpc": "2.0",
    "timestamp": 1782721288151
  },
  "requestId": "ac753513-db17-492a-9c4f-7be17f95122c",
  "message": null
}

session/hitlRespond

{
  "requestId": "578ccdae-e0df-490d-b49c-313e94c08c02",
  "code": 200,
  "message": null,
  "data": {
    "requestId": "578ccdae-e0df-490d-b49c-313e94c08c02",
    "timestamp": 1778467232264,
    "jsonrpc": "2.0",
    "id": 2,
    "result": {
      "status": "accepted"
    }
  }
}

SSE streaming responses (session/prompt and session/load)

The response is an SSE event stream. The format of each event is data: {JSON}\n\n.

Event structure

{
  "requestId": "<uuid>",
  "timestamp": 1774493075579,
  "jsonrpc": "2.0",
  "method": "session/update",
  "params": {
    "sessionId": "<session-id>",
    "update": {
      "sessionUpdate": "<event-type>",
      "content": { "type": "text", "text": "<content>" }
    },
    "_meta": { }
  }
}

sessionUpdate event types

TypeDescription
task_statusThe task status changes (created -> in_progress -> completed).
agent_message_startThe message starts.
agent_message_chunkThe message chunk from the agent (incremental).
agent_messageThe complete message from the agent.
user_message_chunkThe playback of the user message.
hitl_questionThe agent asks the user a question (HITL).
hitl_responseThe user has answered the HITL question, and the agent resumes execution.
task_completeThe task is completed.

The caller can ignore unlisted event types as needed.

Description of special fields in params._meta

FieldDescription
statusThe task status. Valid values: created, in_progress, and completed.
response_idThe response ID.
message_idThe message ID.
deltaA value of true indicates incremental content.
roleThe message role (assistant).

Stream end markers

  1. Receiving result.stopReason as end_turn indicates that the task ends normally.

  2. Receiving data: [DONE] indicates that the SSE stream ends.

Complete event stream example

data: {"requestId":"...","method":"session/update","params":{"update":{"sessionUpdate":"task_status"},"_meta":{"status":"created"}}}

data: {"requestId":"...","method":"session/update","params":{"update":{"sessionUpdate":"task_status"},"_meta":{"status":"in_progress"}}}

data: {"requestId":"...","method":"session/update","params":{"update":{"sessionUpdate":"agent_message_start"},"_meta":{"message_id":"msg_xxx","role":"assistant"}}}

data: {"requestId":"...","method":"session/update","params":{"update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"Hello"}},"_meta":{"message_id":"msg_xxx","delta":true}}}

data: {"requestId":"...","method":"session/update","params":{"update":{"sessionUpdate":"agent_message","content":{"type":"text","text":"Hello! Complete response..."}},"_meta":{"message_id":"msg_xxx"}}}

data: {"requestId":"...","method":"session/update","params":{"update":{"sessionUpdate":"task_complete"},"_meta":{"status":"completed"}}}

data: {"requestId":"...","id":"...","result":{"stopReason":"end_turn"}}

data: [DONE]

HITL interactions.

hitl_question event format

{
  "requestId": "...",
  "timestamp": 1778467122742,
  "jsonrpc": "2.0",
  "method": "session/update",
  "params": {
    "sessionId": "sess_xxx",
    "update": {
      "sessionUpdate": "hitl_question",
      "questionId": "call_832fe77927734a598b5bbb40",
      "question": "Which IT field development direction are you most interested in?",
      "options": [
        { "label": "Artificial intelligence and machine learning", "description": "Artificial intelligence and machine learning" },
        { "label": "Cloud computing and DevOps", "description": "Cloud computing and DevOps" },
        { "label": "Cybersecurity and data protection", "description": "Cybersecurity and data protection" }
      ],
      "timeoutSeconds": 120
    },
    "_meta": {
      "hitlType": "question",
      "call_id": "call_832fe77927734a598b5bbb40",
      "nextLogOffset": 37
    }
  }
}

SSE events after HITL completion

After you submit a response through session/hitlRespond, the SSE stream of the original session/prompt continues to output the following events:

  1. The hitl_response event, which confirms that the user has answered.

  2. The agent resumes execution and outputs subsequent events such as agent_message_chunk.

  3. task_complete, end_turn, and [DONE].

Examples

Success response

JSON format

{
  "code": "200",
  "data": {
    "key": "{\n  \"requestId\": \"598b5734-6dbe-4b04-9c17-4cdf99d58b85\",\n  \"code\": 200,\n  \"message\": null,\n  \"data\": {\n    \"requestId\": \"598b5734-6dbe-4b04-9c17-4cdf99d58b85\",\n    \"timestamp\": 1774494136385,\n    \"jsonrpc\": \"2.0\",\n    \"id\": \"2\",\n    \"result\": {\n      \"sessions\": [\n        {\n          \"sessionId\": \"7b1172ca-8ccb-4c3f-b0fb-c72dd248d3ef\",\n          \"title\": \"New session\",\n          \"description\": null,\n          \"createdAt\": 1774493075150,\n          \"updatedAt\": 1774493094825,\n          \"_meta\": {\n            \"sessionSource\": \"CHAT_OPEN_AGENT\",\n            \"agentName\": \"openagent\",\n            \"status\": \"RELEASED\",\n            \"chatCount\": 1\n          }\n        }\n      ],\n      \"nextCursor\": null,\n      \"total\": 1\n    }\n  }\n}"
  },
  "message": "Agent subscription is inactive or expired",
  "requestId": "1df6d614-96db-41bd-b8b6-ab061a1724ca"
}

Error codes

See Error Codes for a complete list.

Release notes

See Release Notes for a complete list.